@abide/bridge 0.5.2 → 0.6.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 +12 -0
- package/bin/abideBridge.ts +26 -7
- package/package.json +1 -1
- package/src/allowedSites.ts +83 -0
- package/src/bridge.ts +40 -24
- package/src/claudeTurn.ts +28 -58
- package/src/desktop.ts +164 -0
- package/src/grant.ts +210 -0
- package/src/install.ts +204 -54
- package/src/opened.ts +162 -56
- package/src/pairingLink.ts +11 -5
- package/src/readLines.ts +2 -2
- package/src/siteTls.ts +17 -0
package/src/opened.ts
CHANGED
|
@@ -1,15 +1,28 @@
|
|
|
1
1
|
// WHAT RUNS WHEN A PAIRING LINK IS OPENED: the handler `abide-bridge install` bundles beside the app it
|
|
2
|
-
// registers, started
|
|
3
|
-
//
|
|
4
|
-
// the
|
|
2
|
+
// registers, started with the link. It asks the visitor first (44.42) — unless they always allow the
|
|
3
|
+
// site for whoever the link's code was minted for (44.62) — then runs the release of the bridge the
|
|
4
|
+
// link names, as the command a site shows would — so a site is answered by the bridge that speaks its
|
|
5
|
+
// relay, while what asks is the one the visitor installed.
|
|
5
6
|
//
|
|
6
|
-
// RUN BY THE
|
|
7
|
+
// RUN BY THE DESKTOP, NOT A SHELL: nothing of the visitor's login is here, so `bun`, `claude` and the
|
|
7
8
|
// `PATH` are what install found (`settings.json`) — an npm-installed `claude` is a `node` script, and
|
|
8
9
|
// finds `node` only on the PATH it was installed from. Every line the bridge says is written to the
|
|
9
|
-
// log the
|
|
10
|
+
// log in the install directory and shown as a notification.
|
|
10
11
|
|
|
12
|
+
import { appendFile } from 'node:fs/promises' // bun has no append of its own
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
allowAlways,
|
|
16
|
+
allowedIn,
|
|
17
|
+
allowedSites,
|
|
18
|
+
allowsUnasked,
|
|
19
|
+
} from './allowedSites.ts'
|
|
20
|
+
import { BRIDGE_PATH } from './bridge.ts'
|
|
21
|
+
import { type Answer, ask, notify } from './desktop.ts'
|
|
22
|
+
import { grantFor, serversNamed } from './grant.ts'
|
|
11
23
|
import { type Pairing, pairingOf } from './pairingLink.ts'
|
|
12
24
|
import { readLines } from './readLines.ts'
|
|
25
|
+
import { siteTls } from './siteTls.ts'
|
|
13
26
|
|
|
14
27
|
/**
|
|
15
28
|
* What install found, kept beside this file: `bun`, `claude`, and the `PATH` it was run on — and what
|
|
@@ -24,22 +37,29 @@ export type HandlerSettings = {
|
|
|
24
37
|
}
|
|
25
38
|
|
|
26
39
|
/**
|
|
27
|
-
* What the visitor is asked before `pairing` runs
|
|
28
|
-
*
|
|
29
|
-
* site would pair any page's link to whoever minted its code.
|
|
40
|
+
* What the visitor is asked before `pairing` runs, naming each MCP tool it asks for of a server their
|
|
41
|
+
* Claude Code is not configured with, `unserved` (44.65).
|
|
30
42
|
*/
|
|
31
|
-
export function question(
|
|
43
|
+
export function question(
|
|
44
|
+
pairing: Pairing,
|
|
45
|
+
settings: HandlerSettings,
|
|
46
|
+
unserved: readonly string[] = [],
|
|
47
|
+
): string {
|
|
32
48
|
const tools =
|
|
33
49
|
pairing.tools.length === 0
|
|
34
|
-
? 'It gets none of this
|
|
35
|
-
: `It also asks for these tools of this
|
|
50
|
+
? 'It gets none of this computer’s tools.'
|
|
51
|
+
: `It also asks for these tools of this computer’s: ${pairing.tools.join(', ')}.`
|
|
52
|
+
const missing =
|
|
53
|
+
unserved.length === 0
|
|
54
|
+
? ''
|
|
55
|
+
: `\n\nYour Claude Code has no ${serversNamed(unserved)} MCP server, so it can’t give ${unserved.join(', ')}. Add it with \`claude mcp add --scope user\` before you ask.`
|
|
36
56
|
// A variable's value is only named, never shown: it is as likely a token as a host.
|
|
37
57
|
const names = Object.keys(settings.env)
|
|
38
58
|
const kept =
|
|
39
59
|
settings.claudeArgs.length === 0 && names.length === 0
|
|
40
60
|
? ''
|
|
41
61
|
: `\n\nIt also gets what you gave install for every site: ${[...settings.claudeArgs, ...names].join(' ')}.`
|
|
42
|
-
return `${pairing.site} wants to use your Claude Code as its assistant, with its own tools, on your Claude account.\n\n${tools}${kept}\n\nIt runs @abide/bridge ${pairing.version}. Allow it only if you just asked to pair on that site.`
|
|
62
|
+
return `${pairing.site} wants to use your Claude Code as its assistant, with its own tools, on your Claude account.\n\n${tools}${missing}${kept}\n\nIt runs @abide/bridge ${pairing.version}. Allow it only if you just asked to pair on that site.`
|
|
43
63
|
}
|
|
44
64
|
|
|
45
65
|
/**
|
|
@@ -50,9 +70,7 @@ export function bridgeArguments(
|
|
|
50
70
|
pairing: Pairing,
|
|
51
71
|
settings: HandlerSettings,
|
|
52
72
|
): string[] {
|
|
53
|
-
const claudeArgs =
|
|
54
|
-
if (pairing.tools.length > 0)
|
|
55
|
-
claudeArgs.push('--tools', pairing.tools.join(','))
|
|
73
|
+
const claudeArgs = claudeArgsOf(pairing, settings)
|
|
56
74
|
return [
|
|
57
75
|
settings.bun,
|
|
58
76
|
'x',
|
|
@@ -65,53 +83,113 @@ export function bridgeArguments(
|
|
|
65
83
|
]
|
|
66
84
|
}
|
|
67
85
|
|
|
68
|
-
/** The
|
|
86
|
+
/** The Claude Code options the bridge for `pairing` is run with: install's, then the link's tools. */
|
|
87
|
+
function claudeArgsOf(pairing: Pairing, settings: HandlerSettings): string[] {
|
|
88
|
+
const claudeArgs = [...settings.claudeArgs]
|
|
89
|
+
if (pairing.tools.length > 0)
|
|
90
|
+
claudeArgs.push('--tools', pairing.tools.join(','))
|
|
91
|
+
return claudeArgs
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The `PATH` the bridge runs on: the directories of `bun` and `claude`, then the one install was run
|
|
96
|
+
* on, joined by the platform's separator — `;` on Windows.
|
|
97
|
+
*/
|
|
69
98
|
export function searchPath(settings: HandlerSettings): string {
|
|
99
|
+
const separator = process.platform === 'win32' ? ';' : ':'
|
|
70
100
|
const directories: string[] = []
|
|
71
101
|
for (const directory of [
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
...settings.path.split(
|
|
102
|
+
directoryOf(settings.bun),
|
|
103
|
+
directoryOf(settings.claude),
|
|
104
|
+
...settings.path.split(separator),
|
|
75
105
|
])
|
|
76
106
|
if (directory !== '' && !directories.includes(directory))
|
|
77
107
|
directories.push(directory)
|
|
78
|
-
return directories.join(
|
|
108
|
+
return directories.join(separator)
|
|
79
109
|
}
|
|
80
110
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
111
|
+
function directoryOf(file: string): string {
|
|
112
|
+
return file.slice(
|
|
113
|
+
0,
|
|
114
|
+
Math.max(file.lastIndexOf('/'), file.lastIndexOf('\\')),
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Who `pairing`'s code was minted for, as `/__abide/bridge` names them (44.63), or `undefined` where the
|
|
120
|
+
* site names no one — a release before it did, or a code it did not mint.
|
|
121
|
+
*/
|
|
122
|
+
export async function holderOf(pairing: Pairing): Promise<string | undefined> {
|
|
123
|
+
try {
|
|
124
|
+
const answer = await fetch(
|
|
125
|
+
`${pairing.site}${BRIDGE_PATH}?holder=${encodeURIComponent(pairing.code)}`,
|
|
126
|
+
{ tls: siteTls(), signal: AbortSignal.timeout(10_000) },
|
|
127
|
+
)
|
|
128
|
+
if (!answer.ok) {
|
|
129
|
+
await answer.body?.cancel()
|
|
130
|
+
return undefined
|
|
131
|
+
}
|
|
132
|
+
const { holder } = (await answer.json()) as { holder?: unknown }
|
|
133
|
+
return typeof holder === 'string' && holder !== '' ? holder : undefined
|
|
134
|
+
} catch {
|
|
135
|
+
return undefined
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The MCP tools `pairing` asks for whose server the visitor's Claude Code is not configured with. 44.65. */
|
|
140
|
+
export async function unservedOf(
|
|
141
|
+
pairing: Pairing,
|
|
142
|
+
settings: HandlerSettings,
|
|
143
|
+
): Promise<string[]> {
|
|
144
|
+
// An unreadable config is no server here: the bridge says why when it runs.
|
|
145
|
+
const { grant } = await grantFor(
|
|
146
|
+
pairing.tools,
|
|
147
|
+
claudeArgsOf(pairing, settings),
|
|
148
|
+
() => {},
|
|
149
|
+
)
|
|
150
|
+
return grant.unserved
|
|
95
151
|
}
|
|
96
152
|
|
|
97
|
-
/**
|
|
98
|
-
|
|
153
|
+
/**
|
|
154
|
+
* Whether `pairing` may pair: unasked where the visitor always allows its site for the holder its code
|
|
155
|
+
* was minted for and it asks for no tool beyond those allowed then (44.62), else as `askVisitor`
|
|
156
|
+
* answers — offering to always allow only where the site named a holder — keeping an always in
|
|
157
|
+
* `allowedFile`.
|
|
158
|
+
*/
|
|
159
|
+
export async function consented(
|
|
99
160
|
pairing: Pairing,
|
|
100
161
|
settings: HandlerSettings,
|
|
162
|
+
allowedFile: string,
|
|
163
|
+
askVisitor: (question: string, offerAlways: boolean) => Promise<Answer>,
|
|
164
|
+
say: (line: string) => void,
|
|
101
165
|
): Promise<boolean> {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
166
|
+
// None waits on another, and the holder is a round trip to the site.
|
|
167
|
+
const [holder, allowed, unserved] = await Promise.all([
|
|
168
|
+
holderOf(pairing),
|
|
169
|
+
allowedSites(allowedFile),
|
|
170
|
+
unservedOf(pairing, settings),
|
|
171
|
+
])
|
|
172
|
+
if (
|
|
173
|
+
holder !== undefined &&
|
|
174
|
+
allowsUnasked(allowed, pairing.site, holder, pairing.tools)
|
|
175
|
+
) {
|
|
176
|
+
say(
|
|
177
|
+
`Pairing with ${new URL(pairing.site).host}, which you always allow.`,
|
|
178
|
+
)
|
|
179
|
+
return true
|
|
180
|
+
}
|
|
181
|
+
const answer = await askVisitor(
|
|
182
|
+
question(pairing, settings, unserved),
|
|
183
|
+
holder !== undefined,
|
|
108
184
|
)
|
|
109
|
-
|
|
185
|
+
if (answer === 'always' && holder !== undefined)
|
|
186
|
+
await allowAlways(allowedFile, pairing.site, holder, pairing.tools)
|
|
187
|
+
return answer !== 'deny'
|
|
110
188
|
}
|
|
111
189
|
|
|
112
190
|
/**
|
|
113
|
-
* Shows `line` as a notification, coalesced: one
|
|
114
|
-
*
|
|
191
|
+
* Shows `line` as a notification, coalesced: one at a time, and a line arriving while one shows
|
|
192
|
+
* replaces any still waiting, so a burst on the bridge's stderr is never read at its pace.
|
|
115
193
|
*/
|
|
116
194
|
function notifier(): { show(line: string): void; settled(): Promise<void> } {
|
|
117
195
|
let waiting: string | undefined
|
|
@@ -120,12 +198,7 @@ function notifier(): { show(line: string): void; settled(): Promise<void> } {
|
|
|
120
198
|
while (waiting !== undefined) {
|
|
121
199
|
const line = waiting
|
|
122
200
|
waiting = undefined
|
|
123
|
-
await
|
|
124
|
-
[
|
|
125
|
-
'display notification (item 1 of argv) with title "abide bridge"',
|
|
126
|
-
],
|
|
127
|
-
line,
|
|
128
|
-
)
|
|
201
|
+
await notify(line)
|
|
129
202
|
}
|
|
130
203
|
showing = undefined
|
|
131
204
|
}
|
|
@@ -143,20 +216,55 @@ function notifier(): { show(line: string): void; settled(): Promise<void> } {
|
|
|
143
216
|
/** Opens `link`: asks the visitor, then runs the bridge until it ends. Answers the exit code. */
|
|
144
217
|
async function open(link: string): Promise<number> {
|
|
145
218
|
const notifications = notifier()
|
|
219
|
+
const log = `${import.meta.dir}/../bridge.log`
|
|
220
|
+
let logged = Promise.resolve()
|
|
146
221
|
function say(line: string): void {
|
|
147
222
|
console.error(line)
|
|
223
|
+
logged = logged.then(() =>
|
|
224
|
+
appendFile(log, `${new Date().toISOString()} ${line}\n`).catch(
|
|
225
|
+
() => {},
|
|
226
|
+
),
|
|
227
|
+
)
|
|
148
228
|
notifications.show(line)
|
|
149
229
|
}
|
|
230
|
+
async function settled(code: number): Promise<number> {
|
|
231
|
+
await notifications.settled()
|
|
232
|
+
await logged
|
|
233
|
+
return code
|
|
234
|
+
}
|
|
235
|
+
try {
|
|
236
|
+
return settled(await pairWith(link, say))
|
|
237
|
+
} catch (thrown) {
|
|
238
|
+
// The applet discards this process's output, so a throw is only seen if it is said.
|
|
239
|
+
say(
|
|
240
|
+
`The bridge could not open: ${thrown instanceof Error ? thrown.message : String(thrown)}`,
|
|
241
|
+
)
|
|
242
|
+
return settled(1)
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async function pairWith(
|
|
247
|
+
link: string,
|
|
248
|
+
say: (line: string) => void,
|
|
249
|
+
): Promise<number> {
|
|
150
250
|
const pairing = pairingOf(link)
|
|
151
251
|
if (pairing === undefined) {
|
|
152
252
|
say('That link isn’t one this bridge can pair with.')
|
|
153
|
-
await notifications.settled()
|
|
154
253
|
return 1
|
|
155
254
|
}
|
|
156
255
|
const settings = (await Bun.file(
|
|
157
256
|
`${import.meta.dir}/settings.json`,
|
|
158
257
|
).json()) as HandlerSettings
|
|
159
|
-
if (
|
|
258
|
+
if (
|
|
259
|
+
!(await consented(
|
|
260
|
+
pairing,
|
|
261
|
+
settings,
|
|
262
|
+
allowedIn(import.meta.dir),
|
|
263
|
+
ask,
|
|
264
|
+
say,
|
|
265
|
+
))
|
|
266
|
+
)
|
|
267
|
+
return 0
|
|
160
268
|
const running = Bun.spawn(bridgeArguments(pairing, settings), {
|
|
161
269
|
stdout: 'ignore',
|
|
162
270
|
stderr: 'pipe',
|
|
@@ -169,9 +277,7 @@ async function open(link: string): Promise<number> {
|
|
|
169
277
|
const said = line.trim()
|
|
170
278
|
if (said !== '') say(said)
|
|
171
279
|
}
|
|
172
|
-
|
|
173
|
-
await notifications.settled()
|
|
174
|
-
return code
|
|
280
|
+
return running.exited
|
|
175
281
|
}
|
|
176
282
|
|
|
177
283
|
if (import.meta.main) process.exit(await open(process.argv[2] ?? ''))
|
package/src/pairingLink.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// A PAIRING AS A LINK: what a site shows beside its command (44.40), opened by the handler
|
|
2
2
|
// `abide-bridge install` registers. It carries what the command does — the site, the code, the bridge
|
|
3
|
-
// release that speaks the site's relay and the
|
|
4
|
-
// handler asks the visitor before it runs one (44.42).
|
|
3
|
+
// release that speaks the site's relay and the tools of this machine it asks for — and nothing more,
|
|
4
|
+
// and the handler asks the visitor before it runs one (44.42).
|
|
5
5
|
|
|
6
6
|
// abide's `BRIDGE_SCHEME`, spelled out for `BRIDGE_PATH`'s reason — a test holds the two equal.
|
|
7
7
|
export const PAIRING_SCHEME = 'abide-bridge'
|
|
@@ -18,8 +18,9 @@ export type Pairing = {
|
|
|
18
18
|
const RELEASE = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/
|
|
19
19
|
// abide's codes are base64url.
|
|
20
20
|
const CODE = /^[\w-]+$/
|
|
21
|
-
// A tool as `--tools` names one: `WebSearch`,
|
|
22
|
-
|
|
21
|
+
// A tool as the bridge's `--tools` names one: a built-in, `WebSearch`, or an MCP server's,
|
|
22
|
+
// `mcp__calendar__meetings`, or all of one server's, `mcp__calendar` (44.29).
|
|
23
|
+
const TOOL = /^(?:[A-Za-z][A-Za-z0-9]*|mcp__[A-Za-z0-9_-]+)$/
|
|
23
24
|
const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]'])
|
|
24
25
|
|
|
25
26
|
/**
|
|
@@ -62,9 +63,14 @@ export function pairingOf(link: string): Pairing | undefined {
|
|
|
62
63
|
return undefined
|
|
63
64
|
for (const tool of tools) if (!TOOL.test(tool)) return undefined
|
|
64
65
|
return {
|
|
65
|
-
site:
|
|
66
|
+
site: siteOf(siteUrl),
|
|
66
67
|
code,
|
|
67
68
|
version,
|
|
68
69
|
tools,
|
|
69
70
|
}
|
|
70
71
|
}
|
|
72
|
+
|
|
73
|
+
/** A site as the bridge keeps it: its origin and path, with no trailing slash. */
|
|
74
|
+
export function siteOf(url: URL): string {
|
|
75
|
+
return `${url.origin}${url.pathname}`.replace(/\/+$/, '')
|
|
76
|
+
}
|
package/src/readLines.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The lines `body` carries, each as it lands, the last one whether or not a newline ends it.
|
|
3
3
|
*
|
|
4
|
-
* The relay's pairing stream and the claude child's stdout. Its own
|
|
5
|
-
*
|
|
4
|
+
* The relay's pairing stream and the claude child's stdout. Its own because this CLI carries no
|
|
5
|
+
* abide at run time, and abide exports no line reader of its own.
|
|
6
6
|
*/
|
|
7
7
|
export async function* readLines(
|
|
8
8
|
body: ReadableStream<Uint8Array>,
|
package/src/siteTls.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { getCACertificates } from 'node:tls' // bun's fetch reads this machine's trust store only under `--use-system-ca`, a flag `bun x` and the link handler's launch cannot pass
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 44.47 — bun's own roots and this machine's, so a site whose certificate an organization's authority
|
|
5
|
+
* signed is reached as the browser reaches it. A `ca` replaces bun's roots rather than adding to them,
|
|
6
|
+
* so both are passed.
|
|
7
|
+
*/
|
|
8
|
+
export function siteTls(): { ca: string[] } {
|
|
9
|
+
return {
|
|
10
|
+
ca: [
|
|
11
|
+
...new Set([
|
|
12
|
+
...getCACertificates('bundled'),
|
|
13
|
+
...getCACertificates('system'),
|
|
14
|
+
]),
|
|
15
|
+
],
|
|
16
|
+
}
|
|
17
|
+
}
|