|
| 1 | +import type { AgentSideConnection } from "@agentclientprotocol/sdk" |
| 2 | +import * as Log from "@opencode-ai/core/util/log" |
| 3 | +import type { |
| 4 | + Event, |
| 5 | + EventMessagePartDelta, |
| 6 | + EventMessagePartUpdated, |
| 7 | + OpencodeClient, |
| 8 | + Part, |
| 9 | + SessionMessageResponse, |
| 10 | +} from "@opencode-ai/sdk/v2" |
| 11 | +import { Effect } from "effect" |
| 12 | +import { ACPNextSession } from "./session" |
| 13 | + |
| 14 | +const log = Log.create({ service: "acp-next-event" }) |
| 15 | + |
| 16 | +type Connection = Pick<AgentSideConnection, "sessionUpdate"> |
| 17 | +type GlobalEventEnvelope = { |
| 18 | + payload?: Event |
| 19 | +} |
| 20 | +type GlobalEventStream = { |
| 21 | + stream: AsyncIterable<GlobalEventEnvelope> |
| 22 | +} |
| 23 | + |
| 24 | +export function start(input: { |
| 25 | + sdk: OpencodeClient |
| 26 | + connection: Connection |
| 27 | + session: ACPNextSession.Interface |
| 28 | +}) { |
| 29 | + const subscription = new Subscription(input) |
| 30 | + subscription.start() |
| 31 | + return subscription |
| 32 | +} |
| 33 | + |
| 34 | +export class Subscription { |
| 35 | + private readonly abort = new AbortController() |
| 36 | + private started = false |
| 37 | + |
| 38 | + constructor( |
| 39 | + private readonly input: { |
| 40 | + sdk: OpencodeClient |
| 41 | + connection: Connection |
| 42 | + session: ACPNextSession.Interface |
| 43 | + }, |
| 44 | + ) {} |
| 45 | + |
| 46 | + start() { |
| 47 | + if (this.started) return |
| 48 | + this.started = true |
| 49 | + this.run().catch((error: unknown) => { |
| 50 | + if (this.abort.signal.aborted) return |
| 51 | + log.error("event subscription failed", { error }) |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + stop() { |
| 56 | + this.abort.abort() |
| 57 | + } |
| 58 | + |
| 59 | + async handle(event: Event) { |
| 60 | + switch (event.type) { |
| 61 | + case "message.part.updated": |
| 62 | + return this.handlePartUpdated(event) |
| 63 | + case "message.part.delta": |
| 64 | + return this.handlePartDelta(event) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private async run() { |
| 69 | + while (!this.abort.signal.aborted) { |
| 70 | + const events = (await this.input.sdk.global.event({ |
| 71 | + signal: this.abort.signal, |
| 72 | + })) as GlobalEventStream |
| 73 | + |
| 74 | + for await (const event of events.stream) { |
| 75 | + if (this.abort.signal.aborted) return |
| 76 | + if (!event.payload) continue |
| 77 | + await this.handle(event.payload).catch((error: unknown) => { |
| 78 | + log.error("failed to handle event", { error, type: event.payload?.type }) |
| 79 | + }) |
| 80 | + } |
| 81 | + if (!this.abort.signal.aborted) await new Promise((resolve) => setTimeout(resolve, 1000)) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private async handlePartUpdated(event: EventMessagePartUpdated) { |
| 86 | + const part = event.properties.part |
| 87 | + const sessionId = part.sessionID || event.properties.sessionID |
| 88 | + const session = await Effect.runPromise(this.input.session.tryGet(sessionId)) |
| 89 | + if (!session) return |
| 90 | + |
| 91 | + await Effect.runPromise( |
| 92 | + this.input.session.recordPartMetadata({ |
| 93 | + sessionId: session.id, |
| 94 | + messageId: part.messageID, |
| 95 | + partId: part.id, |
| 96 | + partType: part.type, |
| 97 | + role: part.type === "reasoning" ? "assistant" : undefined, |
| 98 | + ignored: part.type === "text" ? part.ignored : undefined, |
| 99 | + toolCallId: part.type === "tool" ? part.callID : undefined, |
| 100 | + metadata: "metadata" in part ? part.metadata : undefined, |
| 101 | + }), |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + private async handlePartDelta(event: EventMessagePartDelta) { |
| 106 | + const props = event.properties |
| 107 | + const session = await Effect.runPromise(this.input.session.tryGet(props.sessionID)) |
| 108 | + if (!session) return |
| 109 | + |
| 110 | + const known = await Effect.runPromise( |
| 111 | + this.input.session.tryGetPartMetadata({ |
| 112 | + sessionId: session.id, |
| 113 | + messageId: props.messageID, |
| 114 | + partId: props.partID, |
| 115 | + }), |
| 116 | + ) |
| 117 | + const metadata = |
| 118 | + known?.role && known.partType |
| 119 | + ? known |
| 120 | + : await this.fetchPartMetadata(session.id, session.cwd, props.messageID, props.partID) |
| 121 | + if (metadata?.role !== "assistant") return |
| 122 | + if (metadata.partType === "text" && props.field === "text" && metadata.ignored !== true) { |
| 123 | + await this.input.connection.sessionUpdate({ |
| 124 | + sessionId: session.id, |
| 125 | + update: { |
| 126 | + sessionUpdate: "agent_message_chunk", |
| 127 | + messageId: props.messageID, |
| 128 | + content: { |
| 129 | + type: "text", |
| 130 | + text: props.delta, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }) |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + if (metadata.partType === "reasoning" && props.field === "text") { |
| 138 | + await this.input.connection.sessionUpdate({ |
| 139 | + sessionId: session.id, |
| 140 | + update: { |
| 141 | + sessionUpdate: "agent_thought_chunk", |
| 142 | + messageId: props.messageID, |
| 143 | + content: { |
| 144 | + type: "text", |
| 145 | + text: props.delta, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private async fetchPartMetadata(sessionId: string, cwd: string, messageId: string, partId: string) { |
| 153 | + const message = await this.input.sdk.session |
| 154 | + .message( |
| 155 | + { |
| 156 | + sessionID: sessionId, |
| 157 | + messageID: messageId, |
| 158 | + directory: cwd, |
| 159 | + }, |
| 160 | + { throwOnError: true }, |
| 161 | + ) |
| 162 | + .then((response) => response.data) |
| 163 | + .catch((error: unknown) => { |
| 164 | + log.error("unexpected error when fetching message for delta metadata", { error, messageId, partId }) |
| 165 | + return undefined |
| 166 | + }) |
| 167 | + if (!message) return |
| 168 | + |
| 169 | + const part = message.parts.find((item) => item.id === partId) |
| 170 | + if (!part) return |
| 171 | + return await this.recordFetchedPart(sessionId, message, part) |
| 172 | + } |
| 173 | + |
| 174 | + private async recordFetchedPart(sessionId: string, message: SessionMessageResponse, part: Part) { |
| 175 | + return await Effect.runPromise( |
| 176 | + this.input.session.recordPartMetadata({ |
| 177 | + sessionId, |
| 178 | + messageId: part.messageID, |
| 179 | + partId: part.id, |
| 180 | + partType: part.type, |
| 181 | + role: message.info.role, |
| 182 | + ignored: part.type === "text" ? part.ignored : undefined, |
| 183 | + toolCallId: part.type === "tool" ? part.callID : undefined, |
| 184 | + metadata: "metadata" in part ? part.metadata : undefined, |
| 185 | + }), |
| 186 | + ) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +export * as ACPNextEvent from "./event" |
0 commit comments