@abide/bridge 0.1.0 → 0.3.0
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/CHANGELOG.md +13 -0
- package/bin/abideBridge.ts +17 -5
- package/package.json +3 -3
- package/src/bridge.ts +189 -32
- package/src/claudeTurn.ts +135 -28
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @abide/bridge
|
|
2
|
+
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- bd2a74f: Breaking: an assistant announces a tool call as the model starts writing it, and a site may ask the visitor's Claude Code for built-in tools ([`c959804`](https://github.com/briancray/abide/commit/c959804fd19c3e99fe439f3f1120f1ef13f42ff8))
|
|
8
|
+
|
|
9
|
+
## 0.2.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 9aa03a5: socket mux bounds, request ceilings, stop timeout, scroll restore, numeric and composed bind:value, and keyed rows kept across patches (16.78, 18.28-18.32, 30.29-30.31, 32.58-32.62, 32.66, 32.67, 42.17) ([`301bf47`](https://github.com/briancray/abide/commit/301bf47adf5059271b439fe3c129963861dd1c72))
|
package/bin/abideBridge.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
// `abide-bridge <site> <code
|
|
3
|
-
// assistant. `--claude <path>` names the executable where `claude`
|
|
2
|
+
// `abide-bridge <site> <code> [--claude <path>] [-- <claude options>]` — the command an abide site shows
|
|
3
|
+
// you to connect your Claude Code to its assistant. `--claude <path>` names the executable where `claude`
|
|
4
|
+
// is not on PATH; whatever follows `--` is passed to Claude Code as you wrote it, and is how you give a
|
|
5
|
+
// site what reaches this machine — `-- --tools WebSearch,WebFetch` for the built-in tools it asks for.
|
|
4
6
|
|
|
5
7
|
import { bridge } from '../src/bridge.ts'
|
|
6
8
|
|
|
7
|
-
const
|
|
9
|
+
const all = process.argv.slice(2)
|
|
10
|
+
const split = all.indexOf('--')
|
|
11
|
+
const args = split === -1 ? all : all.slice(0, split)
|
|
12
|
+
const claudeArgs = split === -1 ? [] : all.slice(split + 1)
|
|
8
13
|
const flag = args.indexOf('--claude')
|
|
9
14
|
const claude = flag === -1 ? undefined : args[flag + 1]
|
|
10
15
|
const [site, code] = args.filter(
|
|
@@ -12,7 +17,9 @@ const [site, code] = args.filter(
|
|
|
12
17
|
)
|
|
13
18
|
|
|
14
19
|
if (site === undefined || code === undefined) {
|
|
15
|
-
console.error(
|
|
20
|
+
console.error(
|
|
21
|
+
'usage: abide-bridge <site> <code> [--claude <path>] [-- <claude options>]',
|
|
22
|
+
)
|
|
16
23
|
process.exit(2)
|
|
17
24
|
}
|
|
18
25
|
if (claude === undefined && Bun.which('claude') === null) {
|
|
@@ -23,7 +30,12 @@ if (claude === undefined && Bun.which('claude') === null) {
|
|
|
23
30
|
}
|
|
24
31
|
|
|
25
32
|
try {
|
|
26
|
-
await bridge({
|
|
33
|
+
await bridge({
|
|
34
|
+
site,
|
|
35
|
+
code,
|
|
36
|
+
claudeArgs,
|
|
37
|
+
...(claude === undefined ? {} : { claude }),
|
|
38
|
+
})
|
|
27
39
|
process.exit(0)
|
|
28
40
|
} catch (thrown) {
|
|
29
41
|
console.error(thrown instanceof Error ? thrown.message : String(thrown))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abide/bridge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Connects the Claude Code on your machine to the assistant on an abide site",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,12 +27,12 @@
|
|
|
27
27
|
"CHANGELOG.md"
|
|
28
28
|
],
|
|
29
29
|
"engines": {
|
|
30
|
-
"bun": ">=1.3
|
|
30
|
+
"bun": ">=1.4.3"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@abide/abide": "workspace:*"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
|
-
"typecheck": "
|
|
36
|
+
"typecheck": "bun check"
|
|
37
37
|
}
|
|
38
38
|
}
|
package/src/bridge.ts
CHANGED
|
@@ -16,6 +16,18 @@ import { readLines } from './readLines.ts'
|
|
|
16
16
|
export const BRIDGE_PATH = '/__abide/bridge'
|
|
17
17
|
// How long reports gather before they are posted, so a streamed reply is not a post per token.
|
|
18
18
|
const BATCH_MILLISECONDS = 15
|
|
19
|
+
// 44.24 — how long a relayed MCP request waits for the site's reply.
|
|
20
|
+
const REPLY_MILLISECONDS = 120_000
|
|
21
|
+
// 44.25 — the first wait before redialing a dropped stream, doubled per failed dial up to the last.
|
|
22
|
+
const FIRST_REDIAL_MILLISECONDS = 250
|
|
23
|
+
const LAST_REDIAL_MILLISECONDS = 8_000
|
|
24
|
+
// abide's `RESUME_MILLISECONDS`, spelled out for `BRIDGE_PATH`'s reason: past it the site no longer
|
|
25
|
+
// holds the pairing, and a redial is refused.
|
|
26
|
+
export const RESUME_MILLISECONDS = 30_000
|
|
27
|
+
// abide's `HEARTBEAT_MILLISECONDS`, spelled out for `BRIDGE_PATH`'s reason.
|
|
28
|
+
export const HEARTBEAT_MILLISECONDS = 15_000
|
|
29
|
+
// 44.28 — how long the stream may go without a line before it is taken for dropped.
|
|
30
|
+
const SILENCE_MILLISECONDS = 3 * HEARTBEAT_MILLISECONDS
|
|
19
31
|
|
|
20
32
|
type SiteFrame =
|
|
21
33
|
| { type: 'paired'; token: string }
|
|
@@ -41,44 +53,99 @@ type Report =
|
|
|
41
53
|
| { type: 'end'; turn: string }
|
|
42
54
|
| { type: 'fail'; turn: string; message: string }
|
|
43
55
|
|
|
56
|
+
/** A relayed MCP request the site has yet to reply to. */
|
|
57
|
+
type Waiting = {
|
|
58
|
+
turn: string
|
|
59
|
+
method: string
|
|
60
|
+
answer: (reply: Record<string, unknown>) => void
|
|
61
|
+
deadline: ReturnType<typeof setTimeout>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** What Claude Code is answered where the site never replied: a failed call, or a JSON-RPC error. */
|
|
65
|
+
function unanswered(method: string, message: string): Record<string, unknown> {
|
|
66
|
+
return method === 'tools/call'
|
|
67
|
+
? {
|
|
68
|
+
result: {
|
|
69
|
+
content: [{ type: 'text', text: message }],
|
|
70
|
+
isError: true,
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
: { error: { code: -32603, message } }
|
|
74
|
+
}
|
|
75
|
+
|
|
44
76
|
/** Pairs with `site` by `code`, and serves its turns until the site or the stream ends it. */
|
|
45
77
|
export async function bridge(options: {
|
|
46
78
|
site: string
|
|
47
79
|
code: string
|
|
48
80
|
claude?: string
|
|
81
|
+
claudeArgs?: string[]
|
|
49
82
|
say?: (line: string) => void
|
|
83
|
+
replyMilliseconds?: number
|
|
84
|
+
silenceMilliseconds?: number
|
|
50
85
|
}): Promise<void> {
|
|
51
86
|
const site = options.site.replace(/\/+$/, '')
|
|
52
87
|
const say = options.say ?? ((line: string) => console.error(line))
|
|
53
|
-
const
|
|
88
|
+
const replyMilliseconds = options.replyMilliseconds ?? REPLY_MILLISECONDS
|
|
89
|
+
const silenceMilliseconds =
|
|
90
|
+
options.silenceMilliseconds ?? SILENCE_MILLISECONDS
|
|
91
|
+
// What cuts the stream being read, where it went silent.
|
|
92
|
+
let dialed = new AbortController()
|
|
93
|
+
let stream = await fetch(
|
|
54
94
|
`${site}${BRIDGE_PATH}?code=${encodeURIComponent(options.code)}`,
|
|
55
|
-
{
|
|
95
|
+
{
|
|
96
|
+
headers: { accept: 'text/event-stream' },
|
|
97
|
+
signal: dialed.signal,
|
|
98
|
+
},
|
|
56
99
|
)
|
|
57
|
-
if (!
|
|
100
|
+
if (!stream.ok || stream.body === null)
|
|
58
101
|
throw new Error(
|
|
59
|
-
`${site} refused the pairing (${
|
|
102
|
+
`${site} refused the pairing (${stream.status}): ${await stream.text()}`,
|
|
60
103
|
)
|
|
61
104
|
|
|
62
105
|
let token = ''
|
|
63
|
-
const
|
|
106
|
+
const waiting = new Map<number, Waiting>()
|
|
107
|
+
const answer = (id: number, reply: Record<string, unknown>): void => {
|
|
108
|
+
const waiter = waiting.get(id)
|
|
109
|
+
if (waiter === undefined) return
|
|
110
|
+
waiting.delete(id)
|
|
111
|
+
clearTimeout(waiter.deadline)
|
|
112
|
+
waiter.answer(reply)
|
|
113
|
+
}
|
|
114
|
+
const outbox: Report[] = []
|
|
64
115
|
let flushing: Promise<void> = Promise.resolve()
|
|
65
116
|
let scheduled = false
|
|
66
117
|
const flush = async (): Promise<void> => {
|
|
67
118
|
scheduled = false
|
|
68
|
-
const
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
119
|
+
const batch = outbox.splice(0)
|
|
120
|
+
if (batch.length === 0) return
|
|
121
|
+
const lines: string[] = []
|
|
122
|
+
for (const item of batch) lines.push(JSON.stringify(item))
|
|
123
|
+
let failure: string | undefined
|
|
124
|
+
try {
|
|
125
|
+
const posted = await fetch(`${site}${BRIDGE_PATH}`, {
|
|
126
|
+
method: 'POST',
|
|
127
|
+
headers: {
|
|
128
|
+
authorization: `Bearer ${token}`,
|
|
129
|
+
'content-type': 'application/jsonl',
|
|
130
|
+
},
|
|
131
|
+
body: lines.join('\n'),
|
|
132
|
+
})
|
|
133
|
+
if (!posted.ok)
|
|
134
|
+
failure = `${site} refused a report (${posted.status}): ${await posted.text()}`
|
|
135
|
+
else await posted.body?.cancel()
|
|
136
|
+
} catch (thrown) {
|
|
137
|
+
failure = `${site} could not be reached: ${thrown instanceof Error ? thrown.message : String(thrown)}`
|
|
138
|
+
}
|
|
139
|
+
if (failure === undefined) return
|
|
140
|
+
say(failure)
|
|
141
|
+
// 44.24 — a request the site never heard of is answered failed rather than waited on.
|
|
142
|
+
for (const item of batch)
|
|
143
|
+
if (item.type === 'mcp')
|
|
144
|
+
answer(item.id, unanswered(item.method, failure))
|
|
78
145
|
}
|
|
79
146
|
// One post at a time, in order: a turn's `end` never overtakes its last event.
|
|
80
147
|
const report = (item: Report): void => {
|
|
81
|
-
outbox.push(
|
|
148
|
+
outbox.push(item)
|
|
82
149
|
if (scheduled) return
|
|
83
150
|
scheduled = true
|
|
84
151
|
setTimeout(() => {
|
|
@@ -86,13 +153,23 @@ export async function bridge(options: {
|
|
|
86
153
|
}, BATCH_MILLISECONDS)
|
|
87
154
|
}
|
|
88
155
|
|
|
156
|
+
// A notice a turn repeats is said once, not once per turn.
|
|
157
|
+
const said = new Set<string>()
|
|
158
|
+
const sayOnce = (line: string): void => {
|
|
159
|
+
if (said.has(line)) return
|
|
160
|
+
said.add(line)
|
|
161
|
+
say(line)
|
|
162
|
+
}
|
|
163
|
+
|
|
89
164
|
const turns = new Map<string, AbortController>()
|
|
90
|
-
const waiting = new Map<number, (reply: Record<string, unknown>) => void>()
|
|
91
165
|
let asked = 0
|
|
92
166
|
const secret = crypto.randomUUID()
|
|
93
167
|
const shim = Bun.serve({
|
|
94
168
|
hostname: '127.0.0.1',
|
|
95
169
|
port: 0,
|
|
170
|
+
// A tool call takes as long as its handler does: the reply deadline (44.24) bounds the wait,
|
|
171
|
+
// where bun's own idle timeout would cut every call past 10 seconds.
|
|
172
|
+
idleTimeout: 0,
|
|
96
173
|
async fetch(request) {
|
|
97
174
|
const [, given, turn] = new URL(request.url).pathname.split('/')
|
|
98
175
|
if (given !== secret || turn === undefined || !turns.has(turn))
|
|
@@ -111,8 +188,25 @@ export async function bridge(options: {
|
|
|
111
188
|
if (message.id === undefined)
|
|
112
189
|
return new Response(null, { status: 202 })
|
|
113
190
|
const id = ++asked
|
|
191
|
+
const method = message.method
|
|
114
192
|
const reply = new Promise<Record<string, unknown>>((done) => {
|
|
115
|
-
waiting.set(id,
|
|
193
|
+
waiting.set(id, {
|
|
194
|
+
turn,
|
|
195
|
+
method,
|
|
196
|
+
answer: done,
|
|
197
|
+
// 44.24 — a reply lost on the way is a failure Claude Code is told of.
|
|
198
|
+
deadline: setTimeout(
|
|
199
|
+
() =>
|
|
200
|
+
answer(
|
|
201
|
+
id,
|
|
202
|
+
unanswered(
|
|
203
|
+
method,
|
|
204
|
+
`${site} sent no reply within ${replyMilliseconds} ms.`,
|
|
205
|
+
),
|
|
206
|
+
),
|
|
207
|
+
replyMilliseconds,
|
|
208
|
+
),
|
|
209
|
+
})
|
|
116
210
|
})
|
|
117
211
|
report({
|
|
118
212
|
type: 'mcp',
|
|
@@ -135,10 +229,12 @@ export async function bridge(options: {
|
|
|
135
229
|
try {
|
|
136
230
|
await claudeTurn(turn, {
|
|
137
231
|
claude: options.claude ?? 'claude',
|
|
232
|
+
claudeArgs: options.claudeArgs ?? [],
|
|
138
233
|
mcpUrl: `http://127.0.0.1:${shim.port}/${secret}/${turn.id}`,
|
|
139
234
|
signal: controller.signal,
|
|
140
235
|
report: (event) =>
|
|
141
236
|
report({ type: 'event', turn: turn.id, event }),
|
|
237
|
+
say: sayOnce,
|
|
142
238
|
})
|
|
143
239
|
report({ type: 'end', turn: turn.id })
|
|
144
240
|
} catch (thrown) {
|
|
@@ -150,24 +246,85 @@ export async function bridge(options: {
|
|
|
150
246
|
})
|
|
151
247
|
} finally {
|
|
152
248
|
turns.delete(turn.id)
|
|
249
|
+
for (const [id, waiter] of waiting)
|
|
250
|
+
if (waiter.turn === turn.id)
|
|
251
|
+
answer(id, unanswered(waiter.method, 'The turn ended.'))
|
|
153
252
|
}
|
|
154
253
|
}
|
|
155
254
|
|
|
255
|
+
// 44.25 — THE STREAM REDIALED BY ITS TOKEN, each wait twice the last, until the site refuses it or
|
|
256
|
+
// stops holding the pairing.
|
|
257
|
+
const redial = async (): Promise<Response | undefined> => {
|
|
258
|
+
if (token === '') return undefined
|
|
259
|
+
say(`Lost ${site}; reconnecting.`)
|
|
260
|
+
const until = Date.now() + RESUME_MILLISECONDS
|
|
261
|
+
let delay = FIRST_REDIAL_MILLISECONDS
|
|
262
|
+
while (Date.now() < until) {
|
|
263
|
+
await Bun.sleep(delay)
|
|
264
|
+
delay = Math.min(delay * 2, LAST_REDIAL_MILLISECONDS)
|
|
265
|
+
try {
|
|
266
|
+
dialed = new AbortController()
|
|
267
|
+
const resumed = await fetch(`${site}${BRIDGE_PATH}`, {
|
|
268
|
+
headers: {
|
|
269
|
+
accept: 'text/event-stream',
|
|
270
|
+
authorization: `Bearer ${token}`,
|
|
271
|
+
},
|
|
272
|
+
signal: dialed.signal,
|
|
273
|
+
})
|
|
274
|
+
if (resumed.ok && resumed.body !== null) {
|
|
275
|
+
say(`Reconnected to ${site}.`)
|
|
276
|
+
return resumed
|
|
277
|
+
}
|
|
278
|
+
await resumed.body?.cancel()
|
|
279
|
+
if (resumed.status < 500) return undefined
|
|
280
|
+
} catch {
|
|
281
|
+
// Unreachable for now: the next dial is the retry.
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return undefined
|
|
285
|
+
}
|
|
286
|
+
|
|
156
287
|
try {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
288
|
+
let bye = false
|
|
289
|
+
for (;;) {
|
|
290
|
+
// 44.28 — a stream silent past the bound is cut, and redialed as one that dropped.
|
|
291
|
+
const silence = setTimeout(
|
|
292
|
+
() => dialed.abort(),
|
|
293
|
+
silenceMilliseconds,
|
|
294
|
+
)
|
|
295
|
+
try {
|
|
296
|
+
for await (const line of readLines(
|
|
297
|
+
stream.body as ReadableStream<Uint8Array>,
|
|
298
|
+
)) {
|
|
299
|
+
silence.refresh()
|
|
300
|
+
if (!line.startsWith('data: ')) continue
|
|
301
|
+
const frame = JSON.parse(
|
|
302
|
+
line.slice('data: '.length),
|
|
303
|
+
) as SiteFrame
|
|
304
|
+
if (frame.type === 'paired') {
|
|
305
|
+
token = frame.token
|
|
306
|
+
say(
|
|
307
|
+
`Connected to ${site}. Keep this running while you chat; it ends when you close the page.`,
|
|
308
|
+
)
|
|
309
|
+
} else if (frame.type === 'turn') void run(frame)
|
|
310
|
+
else if (frame.type === 'reply')
|
|
311
|
+
answer(frame.id, frame.reply)
|
|
312
|
+
else if (frame.type === 'cancel')
|
|
313
|
+
turns.get(frame.turn)?.abort()
|
|
314
|
+
else if (frame.type === 'bye') {
|
|
315
|
+
bye = true
|
|
316
|
+
break
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
} catch {
|
|
320
|
+
// A stream cut mid-read is a drop like one that ended.
|
|
321
|
+
} finally {
|
|
322
|
+
clearTimeout(silence)
|
|
323
|
+
}
|
|
324
|
+
if (bye) break
|
|
325
|
+
const resumed = await redial()
|
|
326
|
+
if (resumed === undefined) break
|
|
327
|
+
stream = resumed
|
|
171
328
|
}
|
|
172
329
|
} finally {
|
|
173
330
|
for (const controller of turns.values()) controller.abort()
|
package/src/claudeTurn.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// ONE TURN OF THE CLAUDE CODE ON THIS MACHINE, run for a site and translated into the events abide's
|
|
2
2
|
// `assistant` yields. Claude Code runs its own loop; its tools are the site's, reached through the MCP
|
|
3
|
-
// server this bridge runs on loopback, and
|
|
3
|
+
// server this bridge runs on loopback, and whatever built-in tools the visitor gave this bridge.
|
|
4
4
|
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
5
|
+
// THE SITE ASKS, THE VISITOR GRANTS (44.15, 44.16, 44.30). Every option is Claude Code's own, by name:
|
|
6
|
+
// a site's is laid on as its flag where it only shapes the answer, and its `tools` are a request the
|
|
7
|
+
// visitor's own `--tools` narrows. What reaches this machine — a user's settings, a project's
|
|
8
|
+
// CLAUDE.md, the files around it, its shell, the web from its network — is the visitor's to give, on
|
|
9
|
+
// the bridge's command line, and nothing the site sends can.
|
|
8
10
|
|
|
9
11
|
import { mkdtemp, rm } from 'node:fs/promises' // bun has no temporary directory or recursive remove of its own
|
|
10
12
|
import { tmpdir } from 'node:os' // bun exposes no temporary directory of its own
|
|
@@ -13,7 +15,9 @@ import { readLines } from './readLines.ts'
|
|
|
13
15
|
|
|
14
16
|
// What Claude Code names the one MCP server it is handed, and so the prefix on each tool it calls.
|
|
15
17
|
const SERVER_NAME = 'app'
|
|
16
|
-
|
|
18
|
+
// Every tool of that server, as `--allowedTools` names them.
|
|
19
|
+
const SERVER_TOOLS = `mcp__${SERVER_NAME}`
|
|
20
|
+
const TOOL_PREFIX = `${SERVER_TOOLS}__`
|
|
17
21
|
|
|
18
22
|
/** What the site asks of one turn. */
|
|
19
23
|
export type TurnRequest = {
|
|
@@ -35,7 +39,11 @@ type McpContent = { type: string; text?: string }
|
|
|
35
39
|
type CodeLine = {
|
|
36
40
|
type: string
|
|
37
41
|
subtype?: string
|
|
38
|
-
event?: {
|
|
42
|
+
event?: {
|
|
43
|
+
type: string
|
|
44
|
+
delta?: { type: string; text?: string }
|
|
45
|
+
content_block?: { type: string; id?: string; name?: string }
|
|
46
|
+
}
|
|
39
47
|
message?: {
|
|
40
48
|
content:
|
|
41
49
|
| string
|
|
@@ -55,20 +63,110 @@ type CodeLine = {
|
|
|
55
63
|
usage?: Usage
|
|
56
64
|
}
|
|
57
65
|
|
|
58
|
-
|
|
59
|
-
|
|
66
|
+
// 44.16 — the options a site may set: each only shapes the answer, and reaches nothing of this machine.
|
|
67
|
+
const SITE_OPTIONS = new Set([
|
|
68
|
+
'model',
|
|
69
|
+
'fallbackModel',
|
|
70
|
+
'systemPrompt',
|
|
71
|
+
'appendSystemPrompt',
|
|
72
|
+
'effort',
|
|
73
|
+
'maxBudgetUsd',
|
|
74
|
+
'betas',
|
|
75
|
+
'disallowedTools',
|
|
76
|
+
])
|
|
77
|
+
|
|
78
|
+
/** Claude Code's flag for an option named in camelCase: `fallbackModel` is `--fallback-model`. */
|
|
79
|
+
function flagOf(option: string): string {
|
|
80
|
+
return `--${option.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}`
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The flags carrying what a site set, in Claude Code's own spelling, a list's values comma-joined.
|
|
85
|
+
*
|
|
86
|
+
* Throws where the site set an option it may not (44.16).
|
|
87
|
+
*/
|
|
88
|
+
function siteFlags(config: Omit<ClaudeCodeConfig, 'tools'>): string[] {
|
|
60
89
|
const flags: string[] = []
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
90
|
+
for (const option in config) {
|
|
91
|
+
// Read as it arrived: the site's config is JSON, whatever its type said.
|
|
92
|
+
const value: unknown = config[option as keyof typeof config]
|
|
93
|
+
if (value === undefined) continue
|
|
94
|
+
if (!SITE_OPTIONS.has(option))
|
|
95
|
+
throw new Error(
|
|
96
|
+
`This bridge takes no \`${option}\` from a site (44.16): what reaches the visitor's machine is theirs to pass to abide-bridge.`,
|
|
97
|
+
)
|
|
98
|
+
flags.push(
|
|
99
|
+
flagOf(option),
|
|
100
|
+
Array.isArray(value) ? value.join(',') : String(value),
|
|
101
|
+
)
|
|
102
|
+
}
|
|
69
103
|
return flags
|
|
70
104
|
}
|
|
71
105
|
|
|
106
|
+
/**
|
|
107
|
+
* The values the visitor gave a flag by any of its `spellings` — `--flag a b`, `--flag a,b` or
|
|
108
|
+
* `--flag=a` — taken out of `args`, or `undefined` where they never gave it.
|
|
109
|
+
*/
|
|
110
|
+
function takeFlag(args: string[], spellings: string[]): string[] | undefined {
|
|
111
|
+
let values: string[] | undefined
|
|
112
|
+
for (let index = 0; index < args.length; ) {
|
|
113
|
+
const arg = args[index] as string
|
|
114
|
+
const [flag, inline] = arg.split(/=(.*)/s) as [string, string?]
|
|
115
|
+
if (!spellings.includes(flag)) {
|
|
116
|
+
index++
|
|
117
|
+
continue
|
|
118
|
+
}
|
|
119
|
+
values ??= []
|
|
120
|
+
if (inline !== undefined) {
|
|
121
|
+
values.push(inline)
|
|
122
|
+
args.splice(index, 1)
|
|
123
|
+
} else {
|
|
124
|
+
let end = index + 1
|
|
125
|
+
while (end < args.length && !(args[end] as string).startsWith('-'))
|
|
126
|
+
end++
|
|
127
|
+
values.push(...args.slice(index + 1, end))
|
|
128
|
+
args.splice(index, end - index)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return values
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Every flag but the protocol's: the site's, then the visitor's — so theirs wins where both set one —
|
|
136
|
+
* then the built-in tools both named (44.30), and no setting source where the visitor named none.
|
|
137
|
+
* Beside them, the tools the site asked for that the visitor did not give.
|
|
138
|
+
*/
|
|
139
|
+
function launchFlags(
|
|
140
|
+
config: ClaudeCodeConfig,
|
|
141
|
+
visitor: string[],
|
|
142
|
+
): { flags: string[]; ungranted: string[] } {
|
|
143
|
+
const { tools: asked, ...options } = config
|
|
144
|
+
const args = [...visitor]
|
|
145
|
+
const granted = new Set<string>()
|
|
146
|
+
for (const value of takeFlag(args, ['--tools']) ?? [])
|
|
147
|
+
for (const tool of value.split(/[\s,]+/)) granted.add(tool)
|
|
148
|
+
const allowed = takeFlag(args, ['--allowedTools', '--allowed-tools'])
|
|
149
|
+
const sources = takeFlag(args, ['--setting-sources']) ?? []
|
|
150
|
+
const tools: string[] = []
|
|
151
|
+
const ungranted: string[] = []
|
|
152
|
+
for (const tool of asked ?? [])
|
|
153
|
+
(granted.has(tool) ? tools : ungranted).push(tool)
|
|
154
|
+
const flags = [
|
|
155
|
+
...siteFlags(options),
|
|
156
|
+
...args,
|
|
157
|
+
'--tools',
|
|
158
|
+
tools.join(','),
|
|
159
|
+
// The site's own tools are always approved; a built-in one is approved as granted, unless
|
|
160
|
+
// the visitor said which calls of it are.
|
|
161
|
+
'--allowedTools',
|
|
162
|
+
SERVER_TOOLS,
|
|
163
|
+
...(allowed ?? tools),
|
|
164
|
+
'--setting-sources',
|
|
165
|
+
sources.join(','),
|
|
166
|
+
]
|
|
167
|
+
return { flags, ungranted }
|
|
168
|
+
}
|
|
169
|
+
|
|
72
170
|
function textOf(content: McpContent[] | string | undefined): string {
|
|
73
171
|
if (typeof content === 'string') return content
|
|
74
172
|
let text = ''
|
|
@@ -100,11 +198,18 @@ export async function claudeTurn(
|
|
|
100
198
|
turn: TurnRequest,
|
|
101
199
|
options: {
|
|
102
200
|
claude: string
|
|
201
|
+
claudeArgs: string[]
|
|
103
202
|
mcpUrl: string
|
|
104
203
|
signal: AbortSignal
|
|
105
204
|
report: (event: AssistantEvent) => void
|
|
205
|
+
say: (line: string) => void
|
|
106
206
|
},
|
|
107
207
|
): Promise<void> {
|
|
208
|
+
const { flags, ungranted } = launchFlags(turn.config, options.claudeArgs)
|
|
209
|
+
if (ungranted.length > 0)
|
|
210
|
+
options.say(
|
|
211
|
+
`The site asked for ${ungranted.join(', ')}, which this bridge was not given. Run it with \`-- --tools ${ungranted.join(',')}\` to allow it.`,
|
|
212
|
+
)
|
|
108
213
|
const directory = await mkdtemp(`${tmpdir()}/abide-bridge-`)
|
|
109
214
|
const config = `${directory}/mcp.json`
|
|
110
215
|
await Bun.write(
|
|
@@ -118,6 +223,8 @@ export async function claudeTurn(
|
|
|
118
223
|
const child = Bun.spawn({
|
|
119
224
|
cmd: [
|
|
120
225
|
options.claude,
|
|
226
|
+
...flags,
|
|
227
|
+
// The protocol's own, last so that nothing before them overrides one.
|
|
121
228
|
'-p',
|
|
122
229
|
'--input-format',
|
|
123
230
|
'stream-json',
|
|
@@ -126,18 +233,11 @@ export async function claudeTurn(
|
|
|
126
233
|
'--verbose',
|
|
127
234
|
'--include-partial-messages',
|
|
128
235
|
'--no-session-persistence',
|
|
129
|
-
'--tools',
|
|
130
|
-
'',
|
|
131
|
-
'--setting-sources',
|
|
132
|
-
'',
|
|
133
236
|
'--strict-mcp-config',
|
|
134
237
|
'--mcp-config',
|
|
135
238
|
config,
|
|
136
|
-
'--allowedTools',
|
|
137
|
-
TOOL_PREFIX.slice(0, -2),
|
|
138
239
|
'--max-turns',
|
|
139
240
|
String(turn.maxTurns),
|
|
140
|
-
...configFlags(turn.config),
|
|
141
241
|
],
|
|
142
242
|
cwd: directory,
|
|
143
243
|
stdin: 'pipe',
|
|
@@ -156,11 +256,22 @@ export async function claudeTurn(
|
|
|
156
256
|
const line = JSON.parse(text) as CodeLine
|
|
157
257
|
if (line.type === 'stream_event') {
|
|
158
258
|
const delta = line.event?.delta
|
|
259
|
+
const started = line.event?.content_block
|
|
159
260
|
if (
|
|
160
261
|
line.event?.type === 'content_block_delta' &&
|
|
161
262
|
delta?.type === 'text_delta'
|
|
162
263
|
)
|
|
163
264
|
options.report({ type: 'text', text: delta.text as string })
|
|
265
|
+
// 44.29 — the call is announced as the model starts writing it, its input once written.
|
|
266
|
+
else if (
|
|
267
|
+
line.event?.type === 'content_block_start' &&
|
|
268
|
+
started?.type === 'tool_use'
|
|
269
|
+
)
|
|
270
|
+
options.report({
|
|
271
|
+
type: 'tool',
|
|
272
|
+
id: started.id as string,
|
|
273
|
+
name: (started.name as string).replace(TOOL_PREFIX, ''),
|
|
274
|
+
})
|
|
164
275
|
} else if (
|
|
165
276
|
(line.type === 'assistant' || line.type === 'user') &&
|
|
166
277
|
Array.isArray(line.message?.content)
|
|
@@ -168,12 +279,8 @@ export async function claudeTurn(
|
|
|
168
279
|
for (const block of line.message.content)
|
|
169
280
|
if (block.type === 'tool_use')
|
|
170
281
|
options.report({
|
|
171
|
-
type: '
|
|
282
|
+
type: 'input',
|
|
172
283
|
id: block.id as string,
|
|
173
|
-
name: (block.name as string).replace(
|
|
174
|
-
TOOL_PREFIX,
|
|
175
|
-
'',
|
|
176
|
-
),
|
|
177
284
|
input: block.input,
|
|
178
285
|
})
|
|
179
286
|
else if (block.type === 'tool_result')
|