Feature: Sharing Sessions
Relevant source files
The following files were used as context for generating this wiki page:The Session Sharing feature in OpenCode allows users to generate a shareable link for any session. This enables collaboration, debugging, and referencing of agent interactions. The system is designed with privacy in mind, offering different sharing backends and configuration options to control how and when sessions are shared.
The feature is integrated into various parts of the application, including the core session management, the enterprise-level services, and the GitHub workflow automation. It supports both manual and automatic sharing, which can be configured by the user. When a session is shared, its data is synced to a backend, and a unique URL is generated.
Core Sharing Logic
The primary logic for session sharing is managed within the Session module. It provides functions to create, manage, and remove shareable links for a session.
Sources: packages/opencode/src/session/index.ts
Sharing Process
The Session.share(sessionID) function initiates the sharing process. It checks the user's configuration to determine if sharing is enabled. If so, it proceeds to create a shareable link using one of two available backends: a default implementation or an enterprise-specific one if an enterprise URL is configured.
Once a share link is created, the session's data, including messages and their parts, is synchronized with the sharing backend. The generated URL is then stored within the session's metadata.
The following diagram illustrates the sequence of creating a shared session.
Unsharing Process
The Session.unshare(sessionID) function revokes access to a shared session. It removes the share record from the backend and clears the share information from the session's metadata in local storage. Like the sharing process, it can interact with either the default or the enterprise backend.
Sources: packages/opencode/src/session/index.ts:240-261
Configuration
Session sharing behavior is controlled by the share property in the user's configuration.
| Value | Description |
|---|---|
manual | Allows users to share sessions manually via commands. |
auto | Automatically shares newly created sessions. |
disabled | Disables all session sharing functionality. |
The autoshare: boolean field is deprecated in favor of the share field.
Sources: packages/opencode/src/session/index.ts:188-190, packages/sdk/stainless/openapi.json
API Endpoints
The session sharing feature is exposed through the OpenCode API, allowing for programmatic control over sharing.
Sources: packages/sdk/stainless/openapi.json
| Endpoint | Method | Description |
|---|---|---|
/session/{id}/share | POST | Creates a shareable link for a session. |
/session/{id}/share | DELETE | Revokes the shareable link for a session. |
POST /session/{id}/share
This endpoint initiates the sharing process for a given session ID.
- Path Parameters:
id(string, required): The ID of the session to share.
- Responses:
200 OK: Returns the updatedSessionobject with the share information.400 Bad Request: If the request is invalid.404 Not Found: If the session does not exist.
DELETE /session/{id}/share
This endpoint unshares a previously shared session.
- Path Parameters:
id(string, required): The ID of the session to unshare.
- Responses:
200 OK: Returns the updatedSessionobject with the share information removed.400 Bad Request: If the request is invalid.404 Not Found: If the session does not exist.
Sources: packages/sdk/stainless/openapi.json
Enterprise Sharing Implementation
For enterprise environments, a more robust sharing mechanism is used, which provides better control and data management. This implementation is activated when an enterprise.url is present in the configuration.
Sources: packages/enterprise/src/core/share.ts
Architecture
The enterprise sharing system uses a centralized Storage backend to manage share metadata and session data. When a session is shared, a Share.Info object is created, containing a short public ID, the original session ID, and a secret for authentication.
The data model for enterprise sharing is as follows:
This diagram shows the relationship between the share metadata (ShareInfo) and the synchronized data (ShareData), which is tracked via events (ShareEvent).
Sources: packages/enterprise/src/core/share.ts:10-40
Data Synchronization and Compaction
Session data is synchronized to the enterprise backend via the sync function. It writes batches of Share.Data to a time-series event log. To optimize data retrieval, a compaction process runs, which consolidates the event log into a single, up-to-date state for each shared session. This process reads all pending events, merges them into a compacted state, and stores the result, significantly speeding up reads for shared session pages.
The flow for retrieving data for a shared session is as follows:
GitHub Integration
The sharing feature is integrated into the opencode-agent GitHub Action, allowing the agent to automatically create and post share links in pull request or issue comments.
Sources: github/index.ts
Workflow
When the GitHub Action is triggered, it can create an OpenCode session to perform tasks. If sharing is enabled, it generates a shareId and constructs a share URL.
The decision to share is based on:
- The
SHAREenvironment variable (trueorfalse). - If the
SHAREvariable is not set, it defaults to sharing only for public repositories.
The generated share link, along with a dynamically generated social card image, is appended to the agent's comments on GitHub.
This diagram shows the logic for creating a share link within the GitHub Action.
Privacy on Shared Pages
The application's public-facing pages emphasize that data is only stored when a user explicitly creates a shareable link, reassuring users about the privacy of their code and conversations.