@abide/bridge 0.5.0 → 0.5.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 +6 -0
- package/package.json +1 -1
- package/src/bridge.ts +48 -14
- package/src/claudeTurn.ts +1 -1
- package/src/opened.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @abide/bridge
|
|
2
2
|
|
|
3
|
+
## 0.5.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 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))
|
|
8
|
+
|
|
3
9
|
## 0.5.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
package/package.json
CHANGED
package/src/bridge.ts
CHANGED
|
@@ -62,6 +62,9 @@ type Waiting = {
|
|
|
62
62
|
deadline: ReturnType<typeof setTimeout>
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
+
/** 44.46 — the abort reason of a turn the site missed part of, which it reports failed with. */
|
|
66
|
+
class MissedTurn extends Error {}
|
|
67
|
+
|
|
65
68
|
/** What Claude Code is answered where the site never replied: a failed call, or a JSON-RPC error. */
|
|
66
69
|
function unanswered(method: string, message: string): Record<string, unknown> {
|
|
67
70
|
return method === 'tools/call'
|
|
@@ -85,6 +88,9 @@ export async function bridge(options: {
|
|
|
85
88
|
silenceMilliseconds?: number
|
|
86
89
|
}): Promise<void> {
|
|
87
90
|
const site = options.site.replace(/\/+$/, '')
|
|
91
|
+
// WHAT IS SAID IS READ AS A NOTIFICATION, where a link opened the bridge (`opened.ts`): one short
|
|
92
|
+
// sentence naming the site by its host. The detail goes to Claude Code, in the failure it is answered.
|
|
93
|
+
const host = new URL(site).host
|
|
88
94
|
const say = options.say ?? ((line: string) => console.error(line))
|
|
89
95
|
const replyMilliseconds = options.replyMilliseconds ?? REPLY_MILLISECONDS
|
|
90
96
|
const silenceMilliseconds =
|
|
@@ -100,7 +106,7 @@ export async function bridge(options: {
|
|
|
100
106
|
)
|
|
101
107
|
if (!stream.ok || stream.body === null)
|
|
102
108
|
throw new Error(
|
|
103
|
-
`${
|
|
109
|
+
`${host} refused to pair (${stream.status}). ${await stream.text()}`,
|
|
104
110
|
)
|
|
105
111
|
|
|
106
112
|
let token = ''
|
|
@@ -115,6 +121,9 @@ export async function bridge(options: {
|
|
|
115
121
|
const outbox: Report[] = []
|
|
116
122
|
let flushing: Promise<void> = Promise.resolve()
|
|
117
123
|
let scheduled = false
|
|
124
|
+
// 44.45 — `failing` once a report the site did not take has been said, until one is taken; a
|
|
125
|
+
// redial is what is said while it lasts (44.25), and no failed report is.
|
|
126
|
+
let link: 'up' | 'failing' | 'redialing' = 'up'
|
|
118
127
|
const flush = async (): Promise<void> => {
|
|
119
128
|
scheduled = false
|
|
120
129
|
const batch = outbox.splice(0)
|
|
@@ -122,6 +131,7 @@ export async function bridge(options: {
|
|
|
122
131
|
const lines: string[] = []
|
|
123
132
|
for (const item of batch) lines.push(JSON.stringify(item))
|
|
124
133
|
let failure: string | undefined
|
|
134
|
+
let notice = ''
|
|
125
135
|
try {
|
|
126
136
|
const posted = await fetch(`${site}${BRIDGE_PATH}`, {
|
|
127
137
|
method: 'POST',
|
|
@@ -131,18 +141,32 @@ export async function bridge(options: {
|
|
|
131
141
|
},
|
|
132
142
|
body: lines.join('\n'),
|
|
133
143
|
})
|
|
134
|
-
if (!posted.ok)
|
|
144
|
+
if (!posted.ok) {
|
|
135
145
|
failure = `${site} refused a report (${posted.status}): ${await posted.text()}`
|
|
136
|
-
|
|
146
|
+
notice = `${host} didn’t take Claude Code’s reply (${posted.status}), so it stopped.`
|
|
147
|
+
} else await posted.body?.cancel()
|
|
137
148
|
} catch (thrown) {
|
|
138
149
|
failure = `${site} could not be reached: ${thrown instanceof Error ? thrown.message : String(thrown)}`
|
|
150
|
+
notice = `Can’t reach ${host}, so Claude Code stopped its reply.`
|
|
151
|
+
}
|
|
152
|
+
if (failure === undefined) {
|
|
153
|
+
if (link === 'failing') link = 'up'
|
|
154
|
+
return
|
|
139
155
|
}
|
|
140
|
-
if (
|
|
141
|
-
|
|
142
|
-
|
|
156
|
+
if (link === 'up') {
|
|
157
|
+
say(notice)
|
|
158
|
+
link = 'failing'
|
|
159
|
+
}
|
|
160
|
+
// 44.46 — a turn the site missed part of has no answer left to give, and is ended: what it
|
|
161
|
+
// still waits on is answered as `run` ends it, and the reason is what it reports it failed with.
|
|
143
162
|
for (const item of batch)
|
|
144
|
-
|
|
145
|
-
|
|
163
|
+
turns
|
|
164
|
+
.get(item.turn)
|
|
165
|
+
?.abort(
|
|
166
|
+
new MissedTurn(
|
|
167
|
+
`The site missed part of this turn, so it was ended: ${failure}`,
|
|
168
|
+
),
|
|
169
|
+
)
|
|
146
170
|
}
|
|
147
171
|
// One post at a time, in order: a turn's `end` never overtakes its last event.
|
|
148
172
|
const report = (item: Report): void => {
|
|
@@ -236,6 +260,11 @@ export async function bridge(options: {
|
|
|
236
260
|
const run = async (turn: TurnRequest): Promise<void> => {
|
|
237
261
|
const controller = new AbortController()
|
|
238
262
|
turns.set(turn.id, controller)
|
|
263
|
+
// A cancel or a signal aborts with no reason of its own, and ends the turn as asked.
|
|
264
|
+
const missed = (): string | undefined =>
|
|
265
|
+
controller.signal.reason instanceof MissedTurn
|
|
266
|
+
? controller.signal.reason.message
|
|
267
|
+
: undefined
|
|
239
268
|
try {
|
|
240
269
|
await claudeTurn(turn, {
|
|
241
270
|
claude: options.claude ?? 'claude',
|
|
@@ -246,13 +275,16 @@ export async function bridge(options: {
|
|
|
246
275
|
report({ type: 'event', turn: turn.id, event }),
|
|
247
276
|
say: sayOnce,
|
|
248
277
|
})
|
|
249
|
-
|
|
278
|
+
const message = missed()
|
|
279
|
+
if (message === undefined) report({ type: 'end', turn: turn.id })
|
|
280
|
+
else report({ type: 'fail', turn: turn.id, message })
|
|
250
281
|
} catch (thrown) {
|
|
251
282
|
report({
|
|
252
283
|
type: 'fail',
|
|
253
284
|
turn: turn.id,
|
|
254
285
|
message:
|
|
255
|
-
|
|
286
|
+
missed() ??
|
|
287
|
+
(thrown instanceof Error ? thrown.message : String(thrown)),
|
|
256
288
|
})
|
|
257
289
|
} finally {
|
|
258
290
|
turns.delete(turn.id)
|
|
@@ -266,7 +298,8 @@ export async function bridge(options: {
|
|
|
266
298
|
// stops holding the pairing.
|
|
267
299
|
const redial = async (): Promise<Response | undefined> => {
|
|
268
300
|
if (token === '') return undefined
|
|
269
|
-
say(`Lost ${
|
|
301
|
+
say(`Lost ${host}. Reconnecting…`)
|
|
302
|
+
link = 'redialing'
|
|
270
303
|
const until = Date.now() + RESUME_MILLISECONDS
|
|
271
304
|
let delay = FIRST_REDIAL_MILLISECONDS
|
|
272
305
|
while (Date.now() < until) {
|
|
@@ -282,7 +315,8 @@ export async function bridge(options: {
|
|
|
282
315
|
signal: dialed.signal,
|
|
283
316
|
})
|
|
284
317
|
if (resumed.ok && resumed.body !== null) {
|
|
285
|
-
say(`Reconnected to ${
|
|
318
|
+
say(`Reconnected to ${host}.`)
|
|
319
|
+
link = 'up'
|
|
286
320
|
return resumed
|
|
287
321
|
}
|
|
288
322
|
await resumed.body?.cancel()
|
|
@@ -314,7 +348,7 @@ export async function bridge(options: {
|
|
|
314
348
|
if (frame.type === 'paired') {
|
|
315
349
|
token = frame.token
|
|
316
350
|
say(
|
|
317
|
-
`Connected to ${
|
|
351
|
+
`Connected to ${host}. Close the page to disconnect.`,
|
|
318
352
|
)
|
|
319
353
|
} else if (frame.type === 'turn') void run(frame)
|
|
320
354
|
else if (frame.type === 'reply')
|
|
@@ -343,6 +377,6 @@ export async function bridge(options: {
|
|
|
343
377
|
await flushing
|
|
344
378
|
await flush()
|
|
345
379
|
shim.stop(true)
|
|
346
|
-
say(`Disconnected from ${
|
|
380
|
+
say(`Disconnected from ${host}.`)
|
|
347
381
|
}
|
|
348
382
|
}
|
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
|
}
|