@abide/bridge 0.6.2 → 0.7.1
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 +27 -0
- package/bin/abideBridge.ts +20 -6
- package/package.json +6 -1
- package/src/allowedSites.ts +33 -13
- package/src/bridge.ts +31 -174
- package/src/claudeTurn.ts +48 -20
- package/src/desktop.ts +191 -83
- package/src/grant.ts +16 -6
- package/src/install.ts +36 -9
- package/src/macAlert.ts +166 -0
- package/src/opened.ts +244 -64
- package/src/pairingLink.ts +8 -3
- package/src/requestedLines.ts +66 -0
- package/src/serveTurns.ts +325 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
// WHAT A BRIDGE DOES WITH WHAT A SITE SENDS IT, apart from how it is sent: each turn run with this
|
|
2
|
+
// machine's Claude Code, each MCP request and permission prompt it makes reported for the site to
|
|
3
|
+
// answer, and each reply handed back to it. `bridge.ts` carries it over a server-sent stream and posts; the native app's own
|
|
4
|
+
// bridge (44.77) carries it in its own process — one core, so a frame either learns is one both learn.
|
|
5
|
+
|
|
6
|
+
import type { ProviderEvent } from '@abide/abide/server'
|
|
7
|
+
|
|
8
|
+
import { claudeTurn, type TurnRequest } from './claudeTurn.ts'
|
|
9
|
+
import { PERMISSION_SERVER } from './grant.ts'
|
|
10
|
+
|
|
11
|
+
// 44.24 — how long a relayed MCP request waits for the site's reply.
|
|
12
|
+
const REPLY_MILLISECONDS = 120_000
|
|
13
|
+
|
|
14
|
+
/** What a site sends a bridge about its turns. */
|
|
15
|
+
export type TurnFrame =
|
|
16
|
+
| ({ type: 'turn' } & TurnRequest)
|
|
17
|
+
| {
|
|
18
|
+
type: 'reply'
|
|
19
|
+
turn: string
|
|
20
|
+
id: number
|
|
21
|
+
reply: Record<string, unknown>
|
|
22
|
+
}
|
|
23
|
+
| { type: 'cancel'; turn: string }
|
|
24
|
+
| { type: 'permit'; turn: string; id: number; allow: boolean }
|
|
25
|
+
|
|
26
|
+
/** What a bridge reports of a turn. */
|
|
27
|
+
export type TurnReport =
|
|
28
|
+
| { type: 'event'; turn: string; event: ProviderEvent }
|
|
29
|
+
| {
|
|
30
|
+
type: 'mcp'
|
|
31
|
+
turn: string
|
|
32
|
+
id: number
|
|
33
|
+
method: string
|
|
34
|
+
params: unknown
|
|
35
|
+
}
|
|
36
|
+
| {
|
|
37
|
+
type: 'permission'
|
|
38
|
+
turn: string
|
|
39
|
+
id: number
|
|
40
|
+
name: string
|
|
41
|
+
input: unknown
|
|
42
|
+
}
|
|
43
|
+
| { type: 'end'; turn: string }
|
|
44
|
+
| { type: 'fail'; turn: string; message: string }
|
|
45
|
+
|
|
46
|
+
/** A relayed MCP request the site has yet to reply to. */
|
|
47
|
+
type Waiting = {
|
|
48
|
+
turn: string
|
|
49
|
+
method: string
|
|
50
|
+
answer: (reply: Record<string, unknown>) => void
|
|
51
|
+
deadline: ReturnType<typeof setTimeout>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 44.46 — the abort reason of a turn the site missed part of, which it reports failed with. */
|
|
55
|
+
class MissedTurn extends Error {}
|
|
56
|
+
|
|
57
|
+
/** A permission prompt's answer, as Claude Code reads its `--permission-prompt-tool`'s text. 44.81. */
|
|
58
|
+
function decided(
|
|
59
|
+
allow: boolean,
|
|
60
|
+
input: unknown,
|
|
61
|
+
message: string,
|
|
62
|
+
): Record<string, unknown> {
|
|
63
|
+
const decision = allow
|
|
64
|
+
? { behavior: 'allow', updatedInput: input }
|
|
65
|
+
: { behavior: 'deny', message }
|
|
66
|
+
return {
|
|
67
|
+
result: { content: [{ type: 'text', text: JSON.stringify(decision) }] },
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* What Claude Code is answered where the site never replied: a prompt denied, a failed call, or a
|
|
73
|
+
* JSON-RPC error.
|
|
74
|
+
*/
|
|
75
|
+
function unanswered(method: string, message: string): Record<string, unknown> {
|
|
76
|
+
if (method === 'permission') return decided(false, undefined, message)
|
|
77
|
+
return method === 'tools/call'
|
|
78
|
+
? {
|
|
79
|
+
result: {
|
|
80
|
+
content: [{ type: 'text', text: message }],
|
|
81
|
+
isError: true,
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
: { error: { code: -32603, message } }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** What the bridge's own permission server answers Claude Code, short of a prompt. 44.81. */
|
|
88
|
+
function permissionServer(method: string, params: unknown): unknown {
|
|
89
|
+
if (method === 'initialize')
|
|
90
|
+
return {
|
|
91
|
+
protocolVersion: (params as { protocolVersion?: string })
|
|
92
|
+
?.protocolVersion,
|
|
93
|
+
capabilities: { tools: {} },
|
|
94
|
+
serverInfo: { name: PERMISSION_SERVER, version: '1' },
|
|
95
|
+
}
|
|
96
|
+
if (method === 'tools/list')
|
|
97
|
+
return {
|
|
98
|
+
tools: [
|
|
99
|
+
{
|
|
100
|
+
name: 'ask',
|
|
101
|
+
description:
|
|
102
|
+
'Asks the person at the page whether a tool call may run.',
|
|
103
|
+
inputSchema: {
|
|
104
|
+
type: 'object',
|
|
105
|
+
properties: {
|
|
106
|
+
tool_name: { type: 'string' },
|
|
107
|
+
input: { type: 'object' },
|
|
108
|
+
tool_use_id: { type: 'string' },
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
}
|
|
114
|
+
return {}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Serves the turns `site` sends, reporting each through `report`.
|
|
119
|
+
*
|
|
120
|
+
* `full` gives a turn everything `claude` has, as the visitor running the native app has (44.78,
|
|
121
|
+
* 44.82); otherwise the visitor's `--tools` in `claudeArgs` narrows what a turn asks for (44.29).
|
|
122
|
+
* `beforeTurn` runs ahead of each turn.
|
|
123
|
+
*/
|
|
124
|
+
export function serveTurns(options: {
|
|
125
|
+
site: string
|
|
126
|
+
claude: string
|
|
127
|
+
claudeArgs: string[]
|
|
128
|
+
full: boolean
|
|
129
|
+
report: (report: TurnReport) => void
|
|
130
|
+
say: (line: string) => void
|
|
131
|
+
beforeTurn: () => Promise<void>
|
|
132
|
+
replyMilliseconds?: number
|
|
133
|
+
}): {
|
|
134
|
+
/** Hands the core a frame the site sent. */
|
|
135
|
+
take: (frame: TurnFrame) => void
|
|
136
|
+
/** Ends `turn`, reporting it failed with `message`. 44.46. */
|
|
137
|
+
miss: (turn: string, message: string) => void
|
|
138
|
+
/** Ends every turn, the way a cancel does. */
|
|
139
|
+
cancelAll: () => void
|
|
140
|
+
/** Ends every turn and stops the MCP server. */
|
|
141
|
+
stop: () => void
|
|
142
|
+
} {
|
|
143
|
+
const { site, report } = options
|
|
144
|
+
const replyMilliseconds = options.replyMilliseconds ?? REPLY_MILLISECONDS
|
|
145
|
+
const waiting = new Map<number, Waiting>()
|
|
146
|
+
// Each prompt's input, handed back as Claude Code's `updatedInput` where the page allows it.
|
|
147
|
+
const inputs = new Map<number, unknown>()
|
|
148
|
+
const answer = (id: number, reply: Record<string, unknown>): void => {
|
|
149
|
+
const waiter = waiting.get(id)
|
|
150
|
+
if (waiter === undefined) return
|
|
151
|
+
waiting.delete(id)
|
|
152
|
+
inputs.delete(id)
|
|
153
|
+
clearTimeout(waiter.deadline)
|
|
154
|
+
waiter.answer(reply)
|
|
155
|
+
}
|
|
156
|
+
const turns = new Map<string, AbortController>()
|
|
157
|
+
let asked = 0
|
|
158
|
+
const secret = crypto.randomUUID()
|
|
159
|
+
// TOOLS ARE RELAYED, NEVER CALLED FROM HERE: Claude Code is handed an MCP server on loopback, behind
|
|
160
|
+
// a path no other process is told, and each request it makes there is reported for the site to
|
|
161
|
+
// answer. Beside it, under `/permission`, the server whose one tool is each permission prompt
|
|
162
|
+
// (44.81): answered here but for the prompt itself, which is reported for the page to answer.
|
|
163
|
+
const shim = Bun.serve({
|
|
164
|
+
hostname: '127.0.0.1',
|
|
165
|
+
port: 0,
|
|
166
|
+
// A tool call takes as long as its handler does: the reply deadline (44.24) bounds the wait,
|
|
167
|
+
// where bun's own idle timeout would cut every call past 10 seconds.
|
|
168
|
+
idleTimeout: 0,
|
|
169
|
+
async fetch(request) {
|
|
170
|
+
const [, given, turn, server] = new URL(request.url).pathname.split(
|
|
171
|
+
'/',
|
|
172
|
+
)
|
|
173
|
+
if (
|
|
174
|
+
given !== secret ||
|
|
175
|
+
turn === undefined ||
|
|
176
|
+
!turns.has(turn) ||
|
|
177
|
+
(server !== undefined && server !== 'permission')
|
|
178
|
+
)
|
|
179
|
+
return new Response(null, { status: 404 })
|
|
180
|
+
if (request.method !== 'POST')
|
|
181
|
+
return new Response(null, {
|
|
182
|
+
status: 405,
|
|
183
|
+
headers: { allow: 'POST' },
|
|
184
|
+
})
|
|
185
|
+
const message = (await request.json()) as {
|
|
186
|
+
id?: number | string
|
|
187
|
+
method: string
|
|
188
|
+
params?: unknown
|
|
189
|
+
}
|
|
190
|
+
// A notification is acknowledged here; there is nothing for the site to answer.
|
|
191
|
+
if (message.id === undefined)
|
|
192
|
+
return new Response(null, { status: 202 })
|
|
193
|
+
const prompted =
|
|
194
|
+
server === 'permission' && message.method === 'tools/call'
|
|
195
|
+
if (server === 'permission' && !prompted)
|
|
196
|
+
return Response.json({
|
|
197
|
+
jsonrpc: '2.0',
|
|
198
|
+
id: message.id,
|
|
199
|
+
result: permissionServer(message.method, message.params),
|
|
200
|
+
})
|
|
201
|
+
const id = ++asked
|
|
202
|
+
const method = prompted ? 'permission' : message.method
|
|
203
|
+
const reply = new Promise<Record<string, unknown>>((done) => {
|
|
204
|
+
waiting.set(id, {
|
|
205
|
+
turn,
|
|
206
|
+
method,
|
|
207
|
+
answer: done,
|
|
208
|
+
// 44.24 — a reply lost on the way is a failure Claude Code is told of.
|
|
209
|
+
deadline: setTimeout(
|
|
210
|
+
() =>
|
|
211
|
+
answer(
|
|
212
|
+
id,
|
|
213
|
+
unanswered(
|
|
214
|
+
method,
|
|
215
|
+
`${site} sent no reply within ${replyMilliseconds} ms.`,
|
|
216
|
+
),
|
|
217
|
+
),
|
|
218
|
+
replyMilliseconds,
|
|
219
|
+
),
|
|
220
|
+
})
|
|
221
|
+
})
|
|
222
|
+
if (prompted) {
|
|
223
|
+
const { tool_name, input } = ((
|
|
224
|
+
message.params as { arguments?: unknown }
|
|
225
|
+
)?.arguments ?? {}) as { tool_name?: string; input?: unknown }
|
|
226
|
+
inputs.set(id, input)
|
|
227
|
+
report({
|
|
228
|
+
type: 'permission',
|
|
229
|
+
turn,
|
|
230
|
+
id,
|
|
231
|
+
name: tool_name ?? '',
|
|
232
|
+
input,
|
|
233
|
+
})
|
|
234
|
+
} else
|
|
235
|
+
report({
|
|
236
|
+
type: 'mcp',
|
|
237
|
+
turn,
|
|
238
|
+
id,
|
|
239
|
+
method: message.method,
|
|
240
|
+
params: message.params,
|
|
241
|
+
})
|
|
242
|
+
return Response.json({
|
|
243
|
+
jsonrpc: '2.0',
|
|
244
|
+
id: message.id,
|
|
245
|
+
...(await reply),
|
|
246
|
+
})
|
|
247
|
+
},
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
const run = async (turn: TurnRequest): Promise<void> => {
|
|
251
|
+
const controller = new AbortController()
|
|
252
|
+
turns.set(turn.id, controller)
|
|
253
|
+
await options.beforeTurn()
|
|
254
|
+
// A cancel or a signal aborts with no reason of its own, and ends the turn as asked.
|
|
255
|
+
const missed = (): string | undefined =>
|
|
256
|
+
controller.signal.reason instanceof MissedTurn
|
|
257
|
+
? controller.signal.reason.message
|
|
258
|
+
: undefined
|
|
259
|
+
try {
|
|
260
|
+
await claudeTurn(turn, {
|
|
261
|
+
claude: options.claude,
|
|
262
|
+
claudeArgs: options.claudeArgs,
|
|
263
|
+
full: options.full,
|
|
264
|
+
mcpUrl: `http://127.0.0.1:${shim.port}/${secret}/${turn.id}`,
|
|
265
|
+
signal: controller.signal,
|
|
266
|
+
report: (event) =>
|
|
267
|
+
report({ type: 'event', turn: turn.id, event }),
|
|
268
|
+
say: options.say,
|
|
269
|
+
})
|
|
270
|
+
const message = missed()
|
|
271
|
+
if (message === undefined) report({ type: 'end', turn: turn.id })
|
|
272
|
+
else report({ type: 'fail', turn: turn.id, message })
|
|
273
|
+
} catch (thrown) {
|
|
274
|
+
report({
|
|
275
|
+
type: 'fail',
|
|
276
|
+
turn: turn.id,
|
|
277
|
+
message:
|
|
278
|
+
missed() ??
|
|
279
|
+
(thrown instanceof Error ? thrown.message : String(thrown)),
|
|
280
|
+
})
|
|
281
|
+
} finally {
|
|
282
|
+
turns.delete(turn.id)
|
|
283
|
+
for (const [id, waiter] of waiting)
|
|
284
|
+
if (waiter.turn === turn.id)
|
|
285
|
+
answer(id, unanswered(waiter.method, 'The turn ended.'))
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const cancelAll = (): void => {
|
|
290
|
+
for (const controller of turns.values()) controller.abort()
|
|
291
|
+
}
|
|
292
|
+
return {
|
|
293
|
+
take(frame) {
|
|
294
|
+
if (frame.type === 'turn') void run(frame)
|
|
295
|
+
else if (frame.type === 'reply') {
|
|
296
|
+
// A reply answers an MCP request, and a permit a prompt: neither stands in for the other.
|
|
297
|
+
if (waiting.get(frame.id)?.method !== 'permission')
|
|
298
|
+
answer(frame.id, frame.reply)
|
|
299
|
+
} else if (frame.type === 'permit') {
|
|
300
|
+
const waiter = waiting.get(frame.id)
|
|
301
|
+
if (
|
|
302
|
+
waiter?.method !== 'permission' ||
|
|
303
|
+
waiter.turn !== frame.turn
|
|
304
|
+
)
|
|
305
|
+
return
|
|
306
|
+
answer(
|
|
307
|
+
frame.id,
|
|
308
|
+
decided(
|
|
309
|
+
frame.allow,
|
|
310
|
+
inputs.get(frame.id),
|
|
311
|
+
'The person at the page said no.',
|
|
312
|
+
),
|
|
313
|
+
)
|
|
314
|
+
} else turns.get(frame.turn)?.abort()
|
|
315
|
+
},
|
|
316
|
+
miss(turn, message) {
|
|
317
|
+
turns.get(turn)?.abort(new MissedTurn(message))
|
|
318
|
+
},
|
|
319
|
+
cancelAll,
|
|
320
|
+
stop() {
|
|
321
|
+
cancelAll()
|
|
322
|
+
shim.stop(true)
|
|
323
|
+
},
|
|
324
|
+
}
|
|
325
|
+
}
|