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
70 changes: 42 additions & 28 deletions packages/opencode/src/acp/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export namespace ACP {
private eventStarted = false
private bashSnapshots = new Map<string, string>()
private toolStarts = new Set<string>()
private textSnapshots = new Map<string, string>()
private permissionQueues = new Map<string, Promise<void>>()
private permissionOptions: PermissionOption[] = [
{ optionId: "once", kind: "allow_once", name: "Allow once" },
Expand Down Expand Up @@ -446,6 +447,18 @@ export namespace ACP {
return
}
}

if (part.type === "text" && part.ignored !== true) {
const delta = this.textDelta(part.id, part.text)
if (delta) await this.sendTextChunk(sessionId, "agent_message_chunk", delta)
return
}

if (part.type === "reasoning") {
const delta = this.textDelta(part.id, part.text)
if (delta) await this.sendTextChunk(sessionId, "agent_thought_chunk", delta)
return
}
return
}

Expand Down Expand Up @@ -476,38 +489,14 @@ export namespace ACP {
if (!part) return

if (part.type === "text" && props.field === "text" && part.ignored !== true) {
await this.connection
.sessionUpdate({
sessionId,
update: {
sessionUpdate: "agent_message_chunk",
content: {
type: "text",
text: props.delta,
},
},
})
.catch((error) => {
log.error("failed to send text delta to ACP", { error })
})
this.textSnapshots.set(part.id, (this.textSnapshots.get(part.id) ?? "") + props.delta)
await this.sendTextChunk(sessionId, "agent_message_chunk", props.delta)
return
}

if (part.type === "reasoning" && props.field === "text") {
await this.connection
.sessionUpdate({
sessionId,
update: {
sessionUpdate: "agent_thought_chunk",
content: {
type: "text",
text: props.delta,
},
},
})
.catch((error) => {
log.error("failed to send reasoning delta to ACP", { error })
})
this.textSnapshots.set(part.id, (this.textSnapshots.get(part.id) ?? "") + props.delta)
await this.sendTextChunk(sessionId, "agent_thought_chunk", props.delta)
}
return
}
Expand Down Expand Up @@ -1085,6 +1074,31 @@ export namespace ACP {
return output
}

private textDelta(id: string, next: string) {
const prev = this.textSnapshots.get(id) ?? ""
this.textSnapshots.set(id, next)
if (!next) return ""
if (next.startsWith(prev)) return next.slice(prev.length)
// Fallback: if text was edited mid-stream (rare), send full text to ensure client gets complete content
return next
}

private async sendTextChunk(
sessionId: string,
update: "agent_message_chunk" | "agent_thought_chunk",
text: string,
) {
await this.connection
.sessionUpdate({
sessionId,
update: {
sessionUpdate: update,
content: { type: "text", text },
},
})
.catch((error) => log.error(`failed to send ${update} to ACP`, { error }))
}

private async toolStart(sessionId: string, part: ToolPart) {
if (this.toolStarts.has(part.callID)) return
this.toolStarts.add(part.callID)
Expand Down
Loading
Loading