opencode-acp-provider 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -23
- package/package.json +3 -2
- package/src/acp-client.ts +67 -4
- package/src/acp-tools/antigravity.ts +117 -0
- package/src/acp-tools/claude.ts +105 -0
- package/src/acp-tools/codex.ts +204 -0
- package/src/acp-tools/command-code.ts +135 -0
- package/src/acp-tools/fallback.ts +92 -0
- package/src/acp-tools/shared.ts +436 -0
- package/src/acp-tools.ts +119 -241
- package/src/agent-command.ts +1 -1
- package/src/agent-pool.ts +5 -1
- package/src/agents/README.md +4 -2
- package/src/agents/cmd/index.ts +1 -1
- package/src/agents/cmd/mods/no-native-orchestration.ts +33 -11
- package/src/index.ts +139 -47
- package/src/inventory.ts +14 -0
- package/src/language-model.ts +27 -13
- package/src/mcp-bridge.mjs +34 -4
- package/src/mcp-server.ts +87 -61
- package/src/prompt.ts +68 -14
- package/src/server-manager.ts +517 -74
- package/src/shim.ts +75 -35
- package/src/unsolicited.ts +388 -0
- package/src/util.ts +60 -2
- package/src/mcp-config.ts +0 -198
package/src/index.ts
CHANGED
|
@@ -6,16 +6,17 @@ import { join } from "node:path"
|
|
|
6
6
|
import {
|
|
7
7
|
AcpServerManager,
|
|
8
8
|
type AcpCompactResult,
|
|
9
|
+
type AcpModelInfo,
|
|
9
10
|
type AcpServerConfig,
|
|
10
11
|
type AcpServerState,
|
|
11
12
|
type AcpSessionStore,
|
|
13
|
+
type AcpUnsolicitedTurnInfo,
|
|
12
14
|
} from "./server-manager.ts"
|
|
13
15
|
import { sharedAgentPool } from "./agent-pool.ts"
|
|
14
16
|
import { createShim } from "./shim.ts"
|
|
15
|
-
import { boundedMessages, historyLimits, recentConversation } from "./prompt.ts"
|
|
17
|
+
import { boundedMessages, historyLimits, recentConversation, textOf } from "./prompt.ts"
|
|
16
18
|
import { createAcpLanguageModel } from "./language-model.ts"
|
|
17
19
|
import { createMcpServer } from "./mcp-server.ts"
|
|
18
|
-
import { createOpenCodeMcpSource } from "./mcp-config.ts"
|
|
19
20
|
import { createSteering, type LocalService } from "./steering.ts"
|
|
20
21
|
import { createSkillMirror } from "./skills.ts"
|
|
21
22
|
import { createCommandBridge } from "./commands.ts"
|
|
@@ -88,6 +89,38 @@ function storedSessionKey(poolKey: string) {
|
|
|
88
89
|
return `acp-session/${createHash("sha256").update(poolKey).digest("hex")}`
|
|
89
90
|
}
|
|
90
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Resolve an ACP continuation token when OpenCode is running a turn prompted by
|
|
94
|
+
* a background-completion synthetic message rather than a new user prompt.
|
|
95
|
+
* A wake queued behind another pending message is left for a normal prompt, so
|
|
96
|
+
* only a lone user message at the turn boundary can attach a continuation.
|
|
97
|
+
*/
|
|
98
|
+
function continuationTokenOf(
|
|
99
|
+
messages: ReadonlyArray<{
|
|
100
|
+
role: string
|
|
101
|
+
content?: string | ReadonlyArray<{ type?: string; text?: unknown }>
|
|
102
|
+
metadata?: Record<string, unknown>
|
|
103
|
+
}>,
|
|
104
|
+
sessionID: string,
|
|
105
|
+
manager: Pick<AcpServerManager, "findContinuationByText">,
|
|
106
|
+
): string | undefined {
|
|
107
|
+
let pending: (typeof messages)[number] | undefined
|
|
108
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
109
|
+
const message = messages[i]
|
|
110
|
+
if (!message) continue
|
|
111
|
+
if (message.role === "assistant" || message.role === "tool") break
|
|
112
|
+
if (message.role !== "user") continue
|
|
113
|
+
if (!textOf(message).trim()) continue
|
|
114
|
+
// More than one pending message means the turn is not only the wake.
|
|
115
|
+
if (pending) return undefined
|
|
116
|
+
pending = message
|
|
117
|
+
}
|
|
118
|
+
if (!pending) return undefined
|
|
119
|
+
const token = pending.metadata?.source === "acp-background" ? pending.metadata.acpContinuation : undefined
|
|
120
|
+
if (typeof token === "string" && token) return token
|
|
121
|
+
return sessionID ? manager.findContinuationByText(sessionID, textOf(pending)) : undefined
|
|
122
|
+
}
|
|
123
|
+
|
|
91
124
|
|
|
92
125
|
/**
|
|
93
126
|
* A `serve --service` server records its address and password next to its
|
|
@@ -172,15 +205,6 @@ export default Plugin.define({
|
|
|
172
205
|
}
|
|
173
206
|
}
|
|
174
207
|
|
|
175
|
-
// Forward the MCP servers OpenCode is configured with so a server is set up
|
|
176
|
-
// once and reaches every ACP agent, alongside this plugin's own bridge.
|
|
177
|
-
let openCodeMcp: Awaited<ReturnType<typeof createOpenCodeMcpSource>> | undefined
|
|
178
|
-
try {
|
|
179
|
-
openCodeMcp = await createOpenCodeMcpSource(ctx, log)
|
|
180
|
-
} catch (error) {
|
|
181
|
-
log("warn", `OpenCode MCP servers disabled: ${errorMessage(error)}`)
|
|
182
|
-
}
|
|
183
|
-
|
|
184
208
|
// A plugin reload or a crashed agent starts a new process with no in-memory
|
|
185
209
|
// sessions. Persist which ACP session belongs to each OpenCode session so
|
|
186
210
|
// the next process can resume it instead of losing the conversation.
|
|
@@ -202,35 +226,69 @@ export default Plugin.define({
|
|
|
202
226
|
// Assigned once the agents are up and their skills are known, but the
|
|
203
227
|
// manager reports command updates from the first `session/new` onward.
|
|
204
228
|
let commandBridge: Awaited<ReturnType<typeof createCommandBridge>> | undefined
|
|
229
|
+
const continuations = new Map<string, string>()
|
|
230
|
+
let disposed = false
|
|
231
|
+
let manager: AcpServerManager
|
|
232
|
+
let serverForProvider = new Map<string, string>()
|
|
233
|
+
|
|
234
|
+
const wakeSessionForUnsolicited = async (turn: AcpUnsolicitedTurnInfo) => {
|
|
235
|
+
if (disposed || typeof ctx.session?.synthetic !== "function") {
|
|
236
|
+
manager.dropContinuation(turn.sessionKey, turn.token)
|
|
237
|
+
return
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
if (typeof ctx.session.get === "function") {
|
|
241
|
+
const session = await ctx.session.get({ sessionID: turn.sessionKey as any }).catch(() => undefined)
|
|
242
|
+
const providerID = session?.model?.providerID
|
|
243
|
+
if (providerID && serverForProvider.get(providerID) !== turn.server) {
|
|
244
|
+
log(
|
|
245
|
+
"debug",
|
|
246
|
+
`skipping ACP background wake for ${turn.sessionKey}: session switched to provider ${providerID}`,
|
|
247
|
+
)
|
|
248
|
+
manager.dropContinuation(turn.sessionKey, turn.token)
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
await ctx.session.synthetic({
|
|
253
|
+
sessionID: turn.sessionKey as any,
|
|
254
|
+
text: turn.text,
|
|
255
|
+
description: turn.description,
|
|
256
|
+
metadata: {
|
|
257
|
+
source: "acp-background",
|
|
258
|
+
server: turn.server,
|
|
259
|
+
acpContinuation: turn.token,
|
|
260
|
+
},
|
|
261
|
+
delivery: "queue",
|
|
262
|
+
resume: true,
|
|
263
|
+
})
|
|
264
|
+
} catch (error) {
|
|
265
|
+
log("warn", `could not wake OpenCode session ${turn.sessionKey} for ACP background task: ${errorMessage(error)}`)
|
|
266
|
+
manager.dropContinuation(turn.sessionKey, turn.token)
|
|
267
|
+
}
|
|
268
|
+
}
|
|
205
269
|
|
|
206
|
-
|
|
270
|
+
manager = new AcpServerManager({
|
|
207
271
|
servers,
|
|
208
272
|
defaultCwd: ctx.location.directory,
|
|
209
273
|
log,
|
|
210
274
|
sessions: sessionStore,
|
|
211
275
|
agents: sharedAgentPool(),
|
|
276
|
+
discoverCommands: options.commands !== false,
|
|
212
277
|
onCommands: (server) => void commandBridge?.sync(server),
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
} catch (error) {
|
|
228
|
-
lease?.release()
|
|
229
|
-
throw error
|
|
230
|
-
}
|
|
231
|
-
},
|
|
232
|
-
}
|
|
233
|
-
: undefined,
|
|
278
|
+
// A model's effort levels are learned when it is first selected; refresh its variants.
|
|
279
|
+
onModels: (server) => {
|
|
280
|
+
const state = manager.get(server)
|
|
281
|
+
if (!state || disposed || !registered.has(server)) return
|
|
282
|
+
void publishInventory(state).then(() => persistInventory(state))
|
|
283
|
+
},
|
|
284
|
+
onUnsolicited: (turn) => {
|
|
285
|
+
if (!disposed) void wakeSessionForUnsolicited(turn)
|
|
286
|
+
},
|
|
287
|
+
mcp: mcp
|
|
288
|
+
? {
|
|
289
|
+
servers: async (sessionKey) => mcp.issue(sessionKey),
|
|
290
|
+
}
|
|
291
|
+
: undefined,
|
|
234
292
|
additionalDirectories: skills ? [skills.root] : undefined,
|
|
235
293
|
cmdSkillDir: skills?.agentsDir,
|
|
236
294
|
cmdOrchestration: mcp !== undefined,
|
|
@@ -261,7 +319,6 @@ export default Plugin.define({
|
|
|
261
319
|
const outputLimit = options.outputLimit ?? 32_000
|
|
262
320
|
const shimToken = randomUUID()
|
|
263
321
|
|
|
264
|
-
let disposed = false
|
|
265
322
|
const retryTimers = new Set<ReturnType<typeof setTimeout>>()
|
|
266
323
|
const registrations: Awaited<ReturnType<typeof ctx.provider.transform>>[] = []
|
|
267
324
|
const registered = new Set<string>()
|
|
@@ -282,10 +339,12 @@ export default Plugin.define({
|
|
|
282
339
|
}
|
|
283
340
|
|
|
284
341
|
const providerModels = (state: AcpServerState, providerID: ReturnType<typeof Provider.ID.make>, inventory: AcpInventory) => {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
342
|
+
// Efforts can differ per model, and a model may offer none at all.
|
|
343
|
+
const variantsFor = (model: AcpModelInfo) =>
|
|
344
|
+
effortLevels(model.efforts ?? inventory.efforts).map((effort) => ({
|
|
345
|
+
id: Model.VariantID.make(effort),
|
|
346
|
+
settings: { reasoningEffort: effort },
|
|
347
|
+
}))
|
|
289
348
|
return inventory.models.map((model) => ({
|
|
290
349
|
...Model.Info.default(providerID, Model.ID.make(model.id)),
|
|
291
350
|
modelID: Model.ID.make(`${state.config.id}::${model.id}`),
|
|
@@ -300,7 +359,7 @@ export default Plugin.define({
|
|
|
300
359
|
output: ["text"],
|
|
301
360
|
temperature: false,
|
|
302
361
|
},
|
|
303
|
-
variants,
|
|
362
|
+
variants: variantsFor(model),
|
|
304
363
|
// The agent keeps the conversation in its reused ACP session and
|
|
305
364
|
// compacts it itself. A known window would let OpenCode auto-compact,
|
|
306
365
|
// which only spends a turn on a summary the agent never reads. OpenCode
|
|
@@ -351,7 +410,15 @@ export default Plugin.define({
|
|
|
351
410
|
if (left.models.length !== right.models.length || left.efforts.length !== right.efforts.length) return false
|
|
352
411
|
const modelsMatch = left.models.every((model, index) => {
|
|
353
412
|
const other = right.models[index]
|
|
354
|
-
return
|
|
413
|
+
return (
|
|
414
|
+
other !== undefined &&
|
|
415
|
+
model.id === other.id &&
|
|
416
|
+
model.name === other.name &&
|
|
417
|
+
model.description === other.description &&
|
|
418
|
+
model.defaultEffort === other.defaultEffort &&
|
|
419
|
+
(model.efforts ?? []).join("\u0000") === (other.efforts ?? []).join("\u0000") &&
|
|
420
|
+
(model.efforts === undefined) === (other.efforts === undefined)
|
|
421
|
+
)
|
|
355
422
|
})
|
|
356
423
|
return modelsMatch && left.efforts.every((effort, index) => effort === right.efforts[index])
|
|
357
424
|
}
|
|
@@ -448,12 +515,12 @@ export default Plugin.define({
|
|
|
448
515
|
await manager.stop().catch(() => {})
|
|
449
516
|
await mcp?.close().catch(() => {})
|
|
450
517
|
await skills?.close().catch(() => {})
|
|
451
|
-
await openCodeMcp?.close().catch(() => {})
|
|
452
518
|
throw error
|
|
453
519
|
}
|
|
454
520
|
|
|
455
521
|
const cleanup = async () => {
|
|
456
522
|
disposed = true
|
|
523
|
+
continuations.clear()
|
|
457
524
|
for (const timer of retryTimers) clearTimeout(timer)
|
|
458
525
|
retryTimers.clear()
|
|
459
526
|
for (const registration of registrations) await registration.dispose().catch(() => {})
|
|
@@ -467,7 +534,6 @@ export default Plugin.define({
|
|
|
467
534
|
await manager.stop().catch(() => {})
|
|
468
535
|
await skills?.close().catch(() => {})
|
|
469
536
|
await mcp?.close().catch(() => {})
|
|
470
|
-
await openCodeMcp?.close().catch(() => {})
|
|
471
537
|
})()
|
|
472
538
|
}
|
|
473
539
|
|
|
@@ -476,7 +542,7 @@ export default Plugin.define({
|
|
|
476
542
|
// late cannot steal the id of one that already registered.
|
|
477
543
|
for (const state of manager.list) providerIDForServer(state.config.id)
|
|
478
544
|
// Every provider id is reserved above, so this reverse map is complete.
|
|
479
|
-
|
|
545
|
+
serverForProvider = new Map([...providerIDs].map(([server, providerID]) => [providerID, server]))
|
|
480
546
|
|
|
481
547
|
// A server whose models were discovered before, in another location or an
|
|
482
548
|
// earlier process, is registered now, so this location resolves them
|
|
@@ -523,9 +589,37 @@ export default Plugin.define({
|
|
|
523
589
|
await ctx.session.hook("model.request", (event) => {
|
|
524
590
|
if (!serverForProvider.has(event.model.providerID)) return
|
|
525
591
|
event.headers["x-acp-request-kind"] = event.kind
|
|
592
|
+
if (event.kind === "primary" && event.sessionID) {
|
|
593
|
+
const token = continuations.get(event.sessionID)
|
|
594
|
+
if (token) {
|
|
595
|
+
continuations.delete(event.sessionID)
|
|
596
|
+
event.headers["x-acp-continuation"] = token
|
|
597
|
+
}
|
|
598
|
+
}
|
|
526
599
|
}),
|
|
527
600
|
)
|
|
528
601
|
const limits = historyLimits(options)
|
|
602
|
+
// Detect background continuation turns on every route, and trim fallback
|
|
603
|
+
// HTTP requests when the server cannot register an AI SDK provider.
|
|
604
|
+
registrations.push(
|
|
605
|
+
await ctx.session.hook("context", (event) => {
|
|
606
|
+
if (!serverForProvider.has(event.model.providerID)) return
|
|
607
|
+
if (event.sessionID) {
|
|
608
|
+
const token = continuationTokenOf(event.messages, event.sessionID, manager)
|
|
609
|
+
if (token) continuations.set(event.sessionID, token)
|
|
610
|
+
else continuations.delete(event.sessionID)
|
|
611
|
+
}
|
|
612
|
+
if (!nativeToolParts) {
|
|
613
|
+
const includeSystem = options.includeSystem === true
|
|
614
|
+
if (!includeSystem) event.system = []
|
|
615
|
+
const messages: typeof event.messages = []
|
|
616
|
+
for (const { message, text } of boundedMessages(event.messages, limits, includeSystem)) {
|
|
617
|
+
if (message.role !== "tool") messages.push({ ...message, content: [{ type: "text", text }] })
|
|
618
|
+
}
|
|
619
|
+
event.messages = messages
|
|
620
|
+
}
|
|
621
|
+
}),
|
|
622
|
+
)
|
|
529
623
|
if (nativeToolParts) {
|
|
530
624
|
registrations.push(
|
|
531
625
|
await ctx.aisdk.hook("sdk", (evt) => {
|
|
@@ -551,13 +645,11 @@ export default Plugin.define({
|
|
|
551
645
|
} else {
|
|
552
646
|
// The native HTTP provider serializes these messages itself. Trim
|
|
553
647
|
// its model request before that happens, without changing the transcript.
|
|
554
|
-
for (const kind of ["
|
|
648
|
+
for (const kind of ["generate", "title"] as const) {
|
|
555
649
|
registrations.push(await ctx.session.hook(kind, (event) => {
|
|
556
650
|
if (!serverForProvider.has(event.model.providerID)) return
|
|
557
|
-
const includeSystem = kind !== "context" || options.includeSystem === true
|
|
558
|
-
if (!includeSystem) event.system = []
|
|
559
651
|
const messages: typeof event.messages = []
|
|
560
|
-
for (const { message, text } of boundedMessages(event.messages, limits,
|
|
652
|
+
for (const { message, text } of boundedMessages(event.messages, limits, true)) {
|
|
561
653
|
if (message.role !== "tool") messages.push({ ...message, content: [{ type: "text", text }] })
|
|
562
654
|
}
|
|
563
655
|
event.messages = messages
|
package/src/inventory.ts
CHANGED
|
@@ -11,6 +11,14 @@ export interface AcpInventory {
|
|
|
11
11
|
efforts: string[]
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
/** Live discovery, shared by locations for the lifetime of the service. */
|
|
15
|
+
export interface AcpCatalog extends AcpInventory {
|
|
16
|
+
defaultModel?: string
|
|
17
|
+
modelConfigID?: string
|
|
18
|
+
effortConfigID?: string
|
|
19
|
+
onModels: Set<() => void>
|
|
20
|
+
}
|
|
21
|
+
|
|
14
22
|
/** Bump when the payload shape changes; an older entry is treated as a miss. */
|
|
15
23
|
const VERSION = 1
|
|
16
24
|
const MAX_MODELS = 1_000
|
|
@@ -29,6 +37,8 @@ function cacheInput(config: AcpServerConfig) {
|
|
|
29
37
|
return {
|
|
30
38
|
command: Array.isArray(config.command) ? config.command : [config.command],
|
|
31
39
|
env: Object.entries(config.env ?? {}).sort(([a], [b]) => a.localeCompare(b)),
|
|
40
|
+
...(config.meta ? { meta: config.meta } : {}),
|
|
41
|
+
...(config.setup?.trim() ? { setup: config.setup.trim() } : {}),
|
|
32
42
|
models: config.models ?? null,
|
|
33
43
|
}
|
|
34
44
|
}
|
|
@@ -62,6 +72,10 @@ export function parseInventory(value: unknown): AcpInventory | undefined {
|
|
|
62
72
|
if (typeof entry.name !== "string" || !entry.name) return undefined
|
|
63
73
|
const model: AcpModelInfo = { id: entry.id, name: entry.name }
|
|
64
74
|
if (typeof entry.description === "string" && entry.description) model.description = entry.description
|
|
75
|
+
if (Array.isArray(entry.efforts)) {
|
|
76
|
+
model.efforts = effortLevels(entry.efforts.filter((effort): effort is string => typeof effort === "string"))
|
|
77
|
+
}
|
|
78
|
+
if (typeof entry.defaultEffort === "string" && entry.defaultEffort) model.defaultEffort = entry.defaultEffort
|
|
65
79
|
models.push(model)
|
|
66
80
|
}
|
|
67
81
|
|
package/src/language-model.ts
CHANGED
|
@@ -34,6 +34,7 @@ export function createAcpLanguageModel(options: AcpLanguageModelOptions) {
|
|
|
34
34
|
async doStream(call: CallOptions) {
|
|
35
35
|
const sessionID = header(call.headers, "x-opencode-session-id")
|
|
36
36
|
const requestKind = header(call.headers, "x-acp-request-kind")
|
|
37
|
+
const continuation = header(call.headers, "x-acp-continuation")
|
|
37
38
|
options.log?.(
|
|
38
39
|
"debug",
|
|
39
40
|
`acp language model ${options.server}/${options.model} session=${sessionID ?? "-"} kind=${requestKind ?? "primary"} messages=${call.prompt?.length ?? 0}`,
|
|
@@ -49,6 +50,7 @@ export function createAcpLanguageModel(options: AcpLanguageModelOptions) {
|
|
|
49
50
|
...(options.token ? { authorization: `Bearer ${options.token}` } : {}),
|
|
50
51
|
...(sessionID ? { "x-opencode-session-id": sessionID } : {}),
|
|
51
52
|
...(requestKind ? { "x-acp-request-kind": requestKind } : {}),
|
|
53
|
+
...(continuation ? { "x-acp-continuation": continuation } : {}),
|
|
52
54
|
},
|
|
53
55
|
body: JSON.stringify({
|
|
54
56
|
model: `${options.server}::${options.model}`,
|
|
@@ -270,26 +272,34 @@ function translate(body: ReadableStream<Uint8Array>, log?: LogFn) {
|
|
|
270
272
|
const handleTool = (event: any) => {
|
|
271
273
|
if (!event || typeof event.id !== "string") return
|
|
272
274
|
const existing = tools.get(event.id)
|
|
273
|
-
|
|
275
|
+
if (existing?.settled) return
|
|
276
|
+
const incomingName = typeof event.name === "string" && event.name ? event.name : undefined
|
|
277
|
+
const name = existing?.called ? existing.name : (incomingName ?? existing?.name ?? "unknown")
|
|
274
278
|
const current: ToolProgress = existing ?? { name, started: false, called: false, settled: false }
|
|
275
279
|
if (!existing) tools.set(event.id, current)
|
|
280
|
+
if (!current.called) current.name = name
|
|
276
281
|
if (typeof event.title === "string" && event.title) current.title = event.title
|
|
277
|
-
if (typeof event.output === "string"
|
|
278
|
-
closeText()
|
|
279
|
-
closeReasoning()
|
|
280
|
-
if (!current.started) {
|
|
281
|
-
push({ type: "tool-input-start", id: event.id, toolName: name, providerExecuted: true })
|
|
282
|
-
current.started = true
|
|
283
|
-
}
|
|
282
|
+
if (typeof event.output === "string") current.output = event.output
|
|
284
283
|
const input = event.input && typeof event.input === "object" && !Array.isArray(event.input) ? event.input : {}
|
|
285
284
|
const done = event.done === true
|
|
286
285
|
const keys = Object.keys(input)
|
|
287
|
-
// Agents stream edit/write input in pieces. Wait for the actual
|
|
288
|
-
// the card is created with its diff instead of a path-only
|
|
286
|
+
// Agents stream edit/write/patch input in pieces. Wait for the actual
|
|
287
|
+
// change so the card is created with its diff instead of a path-only
|
|
288
|
+
// placeholder, and so tool-input-start commits the final tool name.
|
|
289
289
|
const partial =
|
|
290
|
-
(name === "edit" && (input.oldString === undefined || input.newString === undefined)) ||
|
|
291
|
-
(name === "write" && input.content === undefined)
|
|
292
|
-
|
|
290
|
+
(current.name === "edit" && (input.oldString === undefined || input.newString === undefined)) ||
|
|
291
|
+
(current.name === "write" && input.content === undefined) ||
|
|
292
|
+
(current.name === "patch" && input.patchText === undefined)
|
|
293
|
+
const ready = done || (keys.length > 0 && !partial)
|
|
294
|
+
if (ready) {
|
|
295
|
+
closeText()
|
|
296
|
+
closeReasoning()
|
|
297
|
+
if (!current.started) {
|
|
298
|
+
push({ type: "tool-input-start", id: event.id, toolName: current.name, providerExecuted: true })
|
|
299
|
+
current.started = true
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (!current.called && ready) {
|
|
293
303
|
callTool(event.id, current, input)
|
|
294
304
|
current.called = true
|
|
295
305
|
}
|
|
@@ -355,6 +365,10 @@ function translate(body: ReadableStream<Uint8Array>, log?: LogFn) {
|
|
|
355
365
|
// failed turn cannot leave a card stuck in the streaming state.
|
|
356
366
|
for (const [id, current] of tools) {
|
|
357
367
|
if (current.settled) continue
|
|
368
|
+
if (!current.started) {
|
|
369
|
+
push({ type: "tool-input-start", id, toolName: current.name, providerExecuted: true })
|
|
370
|
+
current.started = true
|
|
371
|
+
}
|
|
358
372
|
if (!current.called) {
|
|
359
373
|
callTool(id, current, {})
|
|
360
374
|
current.called = true
|
package/src/mcp-bridge.mjs
CHANGED
|
@@ -16,8 +16,38 @@ let protocolVersion = null
|
|
|
16
16
|
let buffer = ""
|
|
17
17
|
const aborters = new Map()
|
|
18
18
|
|
|
19
|
+
function extractErrorMessage(error, seen) {
|
|
20
|
+
if (error === null || error === undefined) return undefined
|
|
21
|
+
if (typeof error === "string") return error.trim() ? error : undefined
|
|
22
|
+
if (typeof error !== "object") {
|
|
23
|
+
try {
|
|
24
|
+
return String(error)
|
|
25
|
+
} catch {
|
|
26
|
+
return undefined
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (seen.has(error)) return undefined
|
|
30
|
+
seen.add(error)
|
|
31
|
+
if (typeof error.message === "string" && error.message.trim()) return error.message
|
|
32
|
+
if (error.cause !== undefined) {
|
|
33
|
+
const caused = extractErrorMessage(error.cause, seen)
|
|
34
|
+
if (caused) return caused
|
|
35
|
+
}
|
|
36
|
+
if (typeof error.name === "string" && error.name.trim()) return error.name
|
|
37
|
+
try {
|
|
38
|
+
const text = String(error).trim()
|
|
39
|
+
return text && text !== "[object Object]" ? text : undefined
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function errorMessage(error) {
|
|
46
|
+
return extractErrorMessage(error, new Set()) ?? "Unknown error"
|
|
47
|
+
}
|
|
48
|
+
|
|
19
49
|
process.stdout.on("error", (error) => {
|
|
20
|
-
process.stderr.write(`mcp-bridge: stdout failed: ${error
|
|
50
|
+
process.stderr.write(`mcp-bridge: stdout failed: ${errorMessage(error)}\n`)
|
|
21
51
|
process.exit(1)
|
|
22
52
|
})
|
|
23
53
|
process.stderr.on("error", () => process.exit(1))
|
|
@@ -95,7 +125,7 @@ async function handle(message) {
|
|
|
95
125
|
try {
|
|
96
126
|
await post(message)
|
|
97
127
|
} catch (error) {
|
|
98
|
-
log(
|
|
128
|
+
log(errorMessage(error))
|
|
99
129
|
}
|
|
100
130
|
return
|
|
101
131
|
}
|
|
@@ -117,7 +147,7 @@ async function handle(message) {
|
|
|
117
147
|
write({
|
|
118
148
|
jsonrpc: "2.0",
|
|
119
149
|
id: message.id,
|
|
120
|
-
error: { code: -32000, message:
|
|
150
|
+
error: { code: -32000, message: errorMessage(error) },
|
|
121
151
|
})
|
|
122
152
|
} finally {
|
|
123
153
|
aborters.delete(message.id)
|
|
@@ -145,6 +175,6 @@ process.stdin.on("data", (chunk) => {
|
|
|
145
175
|
process.stdin.on("end", () => process.exit(0))
|
|
146
176
|
process.stdin.on("close", () => process.exit(0))
|
|
147
177
|
process.stdin.on("error", (error) => {
|
|
148
|
-
log(error
|
|
178
|
+
log(errorMessage(error))
|
|
149
179
|
process.exit(1)
|
|
150
180
|
})
|