Skip to content
Open
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
6 changes: 2 additions & 4 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "AI-powered development tool",
"private": true,
"type": "module",
"packageManager": "bun@1.3.5",
"packageManager": "bun@1.3.6",
"scripts": {
"dev": "bun run --cwd packages/opencode --conditions=browser src/index.ts",
"typecheck": "bun turbo typecheck",
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"dependencies": {
"@actions/core": "1.11.1",
"@actions/github": "6.0.1",
"@agentclientprotocol/sdk": "0.5.1",
"@agentclientprotocol/sdk": "0.12.0",
"@ai-sdk/amazon-bedrock": "3.0.73",
"@ai-sdk/anthropic": "2.0.57",
"@ai-sdk/azure": "2.0.91",
Expand Down
113 changes: 113 additions & 0 deletions packages/opencode/src/acp/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import {
type AuthenticateRequest,
type AuthMethod,
type CancelNotification,
type ForkSessionRequest,
type ForkSessionResponse,
type InitializeRequest,
type InitializeResponse,
type ListSessionsRequest,
type ListSessionsResponse,
type LoadSessionRequest,
type NewSessionRequest,
type PermissionOption,
type PlanEntry,
type PromptRequest,
type SessionInfo,
type SetSessionModelRequest,
type SetSessionModeRequest,
type SetSessionModeResponse,
Expand Down Expand Up @@ -379,6 +384,10 @@ export namespace ACP {
embeddedContext: true,
image: true,
},
sessionCapabilities: {
fork: {},
list: {},
},
},
authMethods: [authMethod],
agentInfo: {
Expand Down Expand Up @@ -480,6 +489,110 @@ export namespace ACP {
}
}

async unstable_listSessions(params: ListSessionsRequest): Promise<ListSessionsResponse> {
try {
const input = params.cwd ? { directory: params.cwd } : undefined
const sessions = await this.sdk.session
.list(input, { throwOnError: true })
.then((x) => x.data ?? [])
const cursor = params.cursor ? Number(params.cursor) : undefined
const ordered = sessions.toSorted((left, right) => right.time.updated - left.time.updated)
const filtered =
cursor !== undefined && Number.isFinite(cursor)
? ordered.filter((session) => session.time.updated < cursor)
: ordered
const limit = 100
const page = filtered.slice(0, limit)
const entries: SessionInfo[] = page.map((session) => ({
sessionId: session.id,
cwd: session.directory,
title: session.title,
updatedAt: new Date(session.time.updated).toISOString(),
}))
const last = page[page.length - 1]
const next = filtered.length > limit && last ? String(last.time.updated) : undefined
const response: ListSessionsResponse = {
sessions: entries,
}
if (next) response.nextCursor = next
return response
} catch (e) {
const error = MessageV2.fromError(e, {
providerID: this.config.defaultModel?.providerID ?? "unknown",
})
if (LoadAPIKeyError.isInstance(error)) {
throw RequestError.authRequired()
}
throw e
}
}

async unstable_forkSession(params: ForkSessionRequest): Promise<ForkSessionResponse> {
const directory = params.cwd
const mcpServers = params.mcpServers ?? []

try {
const model = await defaultModel(this.config, directory)

const forked = await this.sdk.session
.fork(
{
sessionID: params.sessionId,
directory,
},
{ throwOnError: true },
)
.then((x) => x.data)

if (!forked) {
throw new Error("Fork session returned no data")
}

const sessionId = forked.id
const state = await this.sessionManager.load(sessionId, directory, mcpServers, model)

log.info("fork_session", { sessionId, mcpServers: mcpServers.length })

const mode = await this.loadSessionMode({
cwd: directory,
mcpServers,
sessionId,
})

this.setupEventSubscriptions(state)

// Replay forked session history
const messages = await this.sdk.session
.messages(
{
sessionID: sessionId,
directory,
},
{ throwOnError: true },
)
.then((x) => x.data)
.catch((err) => {
log.error("unexpected error when fetching message", { error: err })
return undefined
})

for (const msg of messages ?? []) {
log.debug("replay message", msg)
await this.processMessage(msg)
}

return mode
} catch (e) {
const error = MessageV2.fromError(e, {
providerID: this.config.defaultModel?.providerID ?? "unknown",
})
if (LoadAPIKeyError.isInstance(error)) {
throw RequestError.authRequired()
}
throw e
}
}

private async processMessage(message: SessionMessageResponse) {
log.debug("process message", message)
if (message.info.role !== "assistant" && message.info.role !== "user") return
Expand Down