Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/opencode/src/acp/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ export class Agent implements ACPAgent {

private async handleEvent(event: Event) {
switch (event.type) {
case "session.created": {
const info = event.properties.info
if (info.parentID) {
this.sessionManager.registerChild(event.properties.sessionID, info.parentID)
}
return
}

case "session.deleted": {
this.sessionManager.unregisterChild(event.properties.sessionID)
return
}

case "permission.asked": {
const permission = event.properties
const session = this.sessionManager.tryGet(permission.sessionID)
Expand Down
21 changes: 20 additions & 1 deletion packages/opencode/src/acp/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,32 @@ const log = Log.create({ service: "acp-session-manager" })
export class ACPSessionManager {
private sessions = new Map<string, ACPSessionState>()
private sdk: OpencodeClient
private childToRoot = new Map<string, string>()

constructor(sdk: OpencodeClient) {
this.sdk = sdk
}

tryGet(sessionId: string): ACPSessionState | undefined {
return this.sessions.get(sessionId)
const direct = this.sessions.get(sessionId)
if (direct) return direct
const rootId = this.childToRoot.get(sessionId)
if (rootId) return this.sessions.get(rootId)
return undefined
}

registerChild(childSessionID: string, parentSessionID: string) {
const rootId =
this.childToRoot.get(parentSessionID) ??
(this.sessions.has(parentSessionID) ? parentSessionID : undefined)
if (rootId) {
this.childToRoot.set(childSessionID, rootId)
log.info("registered child session", { child: childSessionID, root: rootId })
}
}

unregisterChild(childSessionID: string) {
this.childToRoot.delete(childSessionID)
}

async create(cwd: string, mcpServers: McpServer[], model?: ACPSessionState["model"]): Promise<ACPSessionState> {
Expand Down
Loading