@abide/bridge 0.5.0 → 0.5.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/CHANGELOG.md +12 -0
- package/package.json +1 -1
- package/src/bridge.ts +64 -14
- package/src/claudeTurn.ts +1 -1
- package/src/opened.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @abide/bridge
|
|
2
2
|
|
|
3
|
+
## 0.5.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- ccbee47: the bridge trusts the authorities this machine trusts as well as bun's own, so it pairs with a site whose certificate an organization's authority signed — 44.47
|
|
8
|
+
|
|
9
|
+
## 0.5.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 0e5f83e: a bridge ends a turn the site missed part of and reports it failed, says a lost site once, and speaks in shorter notifications (RULEBOOK 44.24, 44.45, 44.46) ([`ad9706e`](https://github.com/briancray/abide/commit/ad9706ee850811219c29cd58fe5289219da93c61))
|
|
14
|
+
|
|
3
15
|
## 0.5.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/package.json
CHANGED
package/src/bridge.ts
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
// inside the page's own request, as the person at the page. No credential of theirs reaches this
|
|
8
8
|
// machine, and none of this machine's reaches the site.
|
|
9
9
|
|
|
10
|
+
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
|
|
11
|
+
|
|
10
12
|
import type { AssistantEvent } from '@abide/abide/server'
|
|
11
13
|
|
|
12
14
|
import { claudeTurn, type TurnRequest } from './claudeTurn.ts'
|
|
@@ -62,6 +64,9 @@ type Waiting = {
|
|
|
62
64
|
deadline: ReturnType<typeof setTimeout>
|
|
63
65
|
}
|
|
64
66
|
|
|
67
|
+
/** 44.46 — the abort reason of a turn the site missed part of, which it reports failed with. */
|
|
68
|
+
class MissedTurn extends Error {}
|
|
69
|
+
|
|
65
70
|
/** What Claude Code is answered where the site never replied: a failed call, or a JSON-RPC error. */
|
|
66
71
|
function unanswered(method: string, message: string): Record<string, unknown> {
|
|
67
72
|
return method === 'tools/call'
|
|
@@ -85,6 +90,20 @@ export async function bridge(options: {
|
|
|
85
90
|
silenceMilliseconds?: number
|
|
86
91
|
}): Promise<void> {
|
|
87
92
|
const site = options.site.replace(/\/+$/, '')
|
|
93
|
+
// 44.47 — bun's own roots and this machine's, so a site whose certificate an organization's
|
|
94
|
+
// authority signed is reached as the browser reaches it. a `ca` replaces bun's roots rather than
|
|
95
|
+
// adding to them, so both are passed.
|
|
96
|
+
const tls = {
|
|
97
|
+
ca: [
|
|
98
|
+
...new Set([
|
|
99
|
+
...getCACertificates('bundled'),
|
|
100
|
+
...getCACertificates('system'),
|
|
101
|
+
]),
|
|
102
|
+
],
|
|
103
|
+
}
|
|
104
|
+
// WHAT IS SAID IS READ AS A NOTIFICATION, where a link opened the bridge (`opened.ts`): one short
|
|
105
|
+
// sentence naming the site by its host. The detail goes to Claude Code, in the failure it is answered.
|
|
106
|
+
const host = new URL(site).host
|
|
88
107
|
const say = options.say ?? ((line: string) => console.error(line))
|
|
89
108
|
const replyMilliseconds = options.replyMilliseconds ?? REPLY_MILLISECONDS
|
|
90
109
|
const silenceMilliseconds =
|
|
@@ -96,11 +115,12 @@ export async function bridge(options: {
|
|
|
96
115
|
{
|
|
97
116
|
headers: { accept: 'text/event-stream' },
|
|
98
117
|
signal: dialed.signal,
|
|
118
|
+
tls,
|
|
99
119
|
},
|
|
100
120
|
)
|
|
101
121
|
if (!stream.ok || stream.body === null)
|
|
102
122
|
throw new Error(
|
|
103
|
-
`${
|
|
123
|
+
`${host} refused to pair (${stream.status}). ${await stream.text()}`,
|
|
104
124
|
)
|
|
105
125
|
|
|
106
126
|
let token = ''
|
|
@@ -115,6 +135,9 @@ export async function bridge(options: {
|
|
|
115
135
|
const outbox: Report[] = []
|
|
116
136
|
let flushing: Promise<void> = Promise.resolve()
|
|
117
137
|
let scheduled = false
|
|
138
|
+
// 44.45 — `failing` once a report the site did not take has been said, until one is taken; a
|
|
139
|
+
// redial is what is said while it lasts (44.25), and no failed report is.
|
|
140
|
+
let link: 'up' | 'failing' | 'redialing' = 'up'
|
|
118
141
|
const flush = async (): Promise<void> => {
|
|
119
142
|
scheduled = false
|
|
120
143
|
const batch = outbox.splice(0)
|
|
@@ -122,6 +145,7 @@ export async function bridge(options: {
|
|
|
122
145
|
const lines: string[] = []
|
|
123
146
|
for (const item of batch) lines.push(JSON.stringify(item))
|
|
124
147
|
let failure: string | undefined
|
|
148
|
+
let notice = ''
|
|
125
149
|
try {
|
|
126
150
|
const posted = await fetch(`${site}${BRIDGE_PATH}`, {
|
|
127
151
|
method: 'POST',
|
|
@@ -130,19 +154,34 @@ export async function bridge(options: {
|
|
|
130
154
|
'content-type': 'application/jsonl',
|
|
131
155
|
},
|
|
132
156
|
body: lines.join('\n'),
|
|
157
|
+
tls,
|
|
133
158
|
})
|
|
134
|
-
if (!posted.ok)
|
|
159
|
+
if (!posted.ok) {
|
|
135
160
|
failure = `${site} refused a report (${posted.status}): ${await posted.text()}`
|
|
136
|
-
|
|
161
|
+
notice = `${host} didn’t take Claude Code’s reply (${posted.status}), so it stopped.`
|
|
162
|
+
} else await posted.body?.cancel()
|
|
137
163
|
} catch (thrown) {
|
|
138
164
|
failure = `${site} could not be reached: ${thrown instanceof Error ? thrown.message : String(thrown)}`
|
|
165
|
+
notice = `Can’t reach ${host}, so Claude Code stopped its reply.`
|
|
139
166
|
}
|
|
140
|
-
if (failure === undefined)
|
|
141
|
-
|
|
142
|
-
|
|
167
|
+
if (failure === undefined) {
|
|
168
|
+
if (link === 'failing') link = 'up'
|
|
169
|
+
return
|
|
170
|
+
}
|
|
171
|
+
if (link === 'up') {
|
|
172
|
+
say(notice)
|
|
173
|
+
link = 'failing'
|
|
174
|
+
}
|
|
175
|
+
// 44.46 — a turn the site missed part of has no answer left to give, and is ended: what it
|
|
176
|
+
// still waits on is answered as `run` ends it, and the reason is what it reports it failed with.
|
|
143
177
|
for (const item of batch)
|
|
144
|
-
|
|
145
|
-
|
|
178
|
+
turns
|
|
179
|
+
.get(item.turn)
|
|
180
|
+
?.abort(
|
|
181
|
+
new MissedTurn(
|
|
182
|
+
`The site missed part of this turn, so it was ended: ${failure}`,
|
|
183
|
+
),
|
|
184
|
+
)
|
|
146
185
|
}
|
|
147
186
|
// One post at a time, in order: a turn's `end` never overtakes its last event.
|
|
148
187
|
const report = (item: Report): void => {
|
|
@@ -236,6 +275,11 @@ export async function bridge(options: {
|
|
|
236
275
|
const run = async (turn: TurnRequest): Promise<void> => {
|
|
237
276
|
const controller = new AbortController()
|
|
238
277
|
turns.set(turn.id, controller)
|
|
278
|
+
// A cancel or a signal aborts with no reason of its own, and ends the turn as asked.
|
|
279
|
+
const missed = (): string | undefined =>
|
|
280
|
+
controller.signal.reason instanceof MissedTurn
|
|
281
|
+
? controller.signal.reason.message
|
|
282
|
+
: undefined
|
|
239
283
|
try {
|
|
240
284
|
await claudeTurn(turn, {
|
|
241
285
|
claude: options.claude ?? 'claude',
|
|
@@ -246,13 +290,16 @@ export async function bridge(options: {
|
|
|
246
290
|
report({ type: 'event', turn: turn.id, event }),
|
|
247
291
|
say: sayOnce,
|
|
248
292
|
})
|
|
249
|
-
|
|
293
|
+
const message = missed()
|
|
294
|
+
if (message === undefined) report({ type: 'end', turn: turn.id })
|
|
295
|
+
else report({ type: 'fail', turn: turn.id, message })
|
|
250
296
|
} catch (thrown) {
|
|
251
297
|
report({
|
|
252
298
|
type: 'fail',
|
|
253
299
|
turn: turn.id,
|
|
254
300
|
message:
|
|
255
|
-
|
|
301
|
+
missed() ??
|
|
302
|
+
(thrown instanceof Error ? thrown.message : String(thrown)),
|
|
256
303
|
})
|
|
257
304
|
} finally {
|
|
258
305
|
turns.delete(turn.id)
|
|
@@ -266,7 +313,8 @@ export async function bridge(options: {
|
|
|
266
313
|
// stops holding the pairing.
|
|
267
314
|
const redial = async (): Promise<Response | undefined> => {
|
|
268
315
|
if (token === '') return undefined
|
|
269
|
-
say(`Lost ${
|
|
316
|
+
say(`Lost ${host}. Reconnecting…`)
|
|
317
|
+
link = 'redialing'
|
|
270
318
|
const until = Date.now() + RESUME_MILLISECONDS
|
|
271
319
|
let delay = FIRST_REDIAL_MILLISECONDS
|
|
272
320
|
while (Date.now() < until) {
|
|
@@ -280,9 +328,11 @@ export async function bridge(options: {
|
|
|
280
328
|
authorization: `Bearer ${token}`,
|
|
281
329
|
},
|
|
282
330
|
signal: dialed.signal,
|
|
331
|
+
tls,
|
|
283
332
|
})
|
|
284
333
|
if (resumed.ok && resumed.body !== null) {
|
|
285
|
-
say(`Reconnected to ${
|
|
334
|
+
say(`Reconnected to ${host}.`)
|
|
335
|
+
link = 'up'
|
|
286
336
|
return resumed
|
|
287
337
|
}
|
|
288
338
|
await resumed.body?.cancel()
|
|
@@ -314,7 +364,7 @@ export async function bridge(options: {
|
|
|
314
364
|
if (frame.type === 'paired') {
|
|
315
365
|
token = frame.token
|
|
316
366
|
say(
|
|
317
|
-
`Connected to ${
|
|
367
|
+
`Connected to ${host}. Close the page to disconnect.`,
|
|
318
368
|
)
|
|
319
369
|
} else if (frame.type === 'turn') void run(frame)
|
|
320
370
|
else if (frame.type === 'reply')
|
|
@@ -343,6 +393,6 @@ export async function bridge(options: {
|
|
|
343
393
|
await flushing
|
|
344
394
|
await flush()
|
|
345
395
|
shim.stop(true)
|
|
346
|
-
say(`Disconnected from ${
|
|
396
|
+
say(`Disconnected from ${host}.`)
|
|
347
397
|
}
|
|
348
398
|
}
|
package/src/claudeTurn.ts
CHANGED
|
@@ -210,7 +210,7 @@ export async function claudeTurn(
|
|
|
210
210
|
const { flags, ungranted } = launchFlags(turn.config, options.claudeArgs)
|
|
211
211
|
if (ungranted.length > 0)
|
|
212
212
|
options.say(
|
|
213
|
-
`The site asked for ${ungranted.join(', ')}, which this bridge
|
|
213
|
+
`The site asked for ${ungranted.join(', ')}, which this bridge wasn’t given. Add \`-- --tools ${ungranted.join(',')}\` to allow it.`,
|
|
214
214
|
)
|
|
215
215
|
const directory = await mkdtemp(`${tmpdir()}/abide-bridge-`)
|
|
216
216
|
const config = `${directory}/mcp.json`
|
package/src/opened.ts
CHANGED
|
@@ -149,7 +149,7 @@ async function open(link: string): Promise<number> {
|
|
|
149
149
|
}
|
|
150
150
|
const pairing = pairingOf(link)
|
|
151
151
|
if (pairing === undefined) {
|
|
152
|
-
say('That link
|
|
152
|
+
say('That link isn’t one this bridge can pair with.')
|
|
153
153
|
await notifications.settled()
|
|
154
154
|
return 1
|
|
155
155
|
}
|