@abide/bridge 0.6.1 → 0.7.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 +27 -0
- package/bin/abideBridge.ts +20 -6
- package/package.json +6 -1
- package/src/BRIDGE_PATH.ts +3 -0
- package/src/HEARTBEAT_MILLISECONDS.ts +2 -0
- package/src/PAIRING_SCHEME.ts +2 -0
- package/src/RESUME_MILLISECONDS.ts +3 -0
- package/src/allowedSites.ts +33 -13
- package/src/bridge.ts +34 -182
- package/src/claudeTurn.ts +49 -21
- package/src/desktop.ts +191 -83
- package/src/grant.ts +15 -5
- package/src/install.ts +37 -10
- package/src/macAlert.ts +144 -0
- package/src/opened.ts +245 -65
- package/src/pairingLink.ts +9 -5
- package/src/requestedLines.ts +66 -0
- package/src/serveTurns.ts +325 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// WHAT A LINK ASKS OF THIS MACHINE, AS A VISITOR READS IT (44.92): each built-in tool by what it lets
|
|
2
|
+
// the site do, several tools sharing one line, and every MCP tool on one line naming its server and how
|
|
3
|
+
// many of that server's tools it asks for — a site asking for many tools one by one is a few lines, and
|
|
4
|
+
// the count says how much it asked.
|
|
5
|
+
|
|
6
|
+
import { serverOf } from './grant.ts'
|
|
7
|
+
|
|
8
|
+
// The built-in tools by what they let a site do, in the order the lines are read: what reaches this
|
|
9
|
+
// machine furthest first. A tool that only keeps Claude Code's own notes is no line at all.
|
|
10
|
+
const CAPABILITIES: readonly { line: string; tools: readonly string[] }[] = [
|
|
11
|
+
{ line: 'Run commands', tools: ['Bash', 'BashOutput', 'KillShell'] },
|
|
12
|
+
{
|
|
13
|
+
line: 'Change your files',
|
|
14
|
+
tools: ['Edit', 'Write', 'MultiEdit', 'NotebookEdit'],
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
line: 'Read and search your files',
|
|
18
|
+
tools: ['Read', 'Glob', 'Grep', 'LS'],
|
|
19
|
+
},
|
|
20
|
+
{ line: 'Search the web', tools: ['WebSearch'] },
|
|
21
|
+
{ line: 'Open web pages', tools: ['WebFetch'] },
|
|
22
|
+
{ line: 'Start helper agents', tools: ['Task'] },
|
|
23
|
+
]
|
|
24
|
+
const UNSHOWN = new Set(['TodoWrite', 'ExitPlanMode'])
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The lines naming what `tools` ask of this machine: one per capability their built-in tools give, in
|
|
28
|
+
* `CAPABILITIES`' order, then any built-in it does not name by its own name, then one naming every MCP
|
|
29
|
+
* server with how many of its tools are asked for — `all tools` where a whole server is.
|
|
30
|
+
*/
|
|
31
|
+
export function requestedLines(tools: readonly string[]): string[] {
|
|
32
|
+
const lines: string[] = []
|
|
33
|
+
for (const { line, tools: covered } of CAPABILITIES)
|
|
34
|
+
for (const tool of covered)
|
|
35
|
+
if (tools.includes(tool)) {
|
|
36
|
+
lines.push(line)
|
|
37
|
+
break
|
|
38
|
+
}
|
|
39
|
+
const servers = new Map<string, number | 'all'>()
|
|
40
|
+
for (const tool of tools) {
|
|
41
|
+
const server = serverOf(tool)
|
|
42
|
+
if (server === undefined) {
|
|
43
|
+
let named = UNSHOWN.has(tool)
|
|
44
|
+
for (const { tools: covered } of CAPABILITIES)
|
|
45
|
+
if (covered.includes(tool)) named = true
|
|
46
|
+
if (!named) lines.push(`Use ${tool}`)
|
|
47
|
+
continue
|
|
48
|
+
}
|
|
49
|
+
const counted = servers.get(server) ?? 0
|
|
50
|
+
servers.set(
|
|
51
|
+
server,
|
|
52
|
+
counted === 'all' || tool === `mcp__${server}`
|
|
53
|
+
? 'all'
|
|
54
|
+
: counted + 1,
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
if (servers.size > 0) {
|
|
58
|
+
const named: string[] = []
|
|
59
|
+
for (const [server, count] of servers)
|
|
60
|
+
named.push(
|
|
61
|
+
`${server} (${count === 'all' ? 'all tools' : count === 1 ? '1 tool' : `${count} tools`})`,
|
|
62
|
+
)
|
|
63
|
+
lines.push(`MCP servers: ${named.join(', ')}`)
|
|
64
|
+
}
|
|
65
|
+
return lines
|
|
66
|
+
}
|
|
@@ -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
|
+
}
|