@abide/bridge 0.1.0 → 0.2.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 +7 -0
- package/package.json +3 -3
- package/src/bridge.ts +178 -32
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @abide/bridge
|
|
2
|
+
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 9aa03a5: socket mux bounds, request ceilings, stop timeout, scroll restore, numeric and composed bind:value, and keyed rows kept across patches (16.78, 18.28-18.32, 30.29-30.31, 32.58-32.62, 32.66, 32.67, 42.17) ([`301bf47`](https://github.com/briancray/abide/commit/301bf47adf5059271b439fe3c129963861dd1c72))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abide/bridge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Connects the Claude Code on your machine to the assistant on an abide site",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,12 +27,12 @@
|
|
|
27
27
|
"CHANGELOG.md"
|
|
28
28
|
],
|
|
29
29
|
"engines": {
|
|
30
|
-
"bun": ">=1.3
|
|
30
|
+
"bun": ">=1.4.3"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@abide/abide": "workspace:*"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
|
-
"typecheck": "
|
|
36
|
+
"typecheck": "bun check"
|
|
37
37
|
}
|
|
38
38
|
}
|
package/src/bridge.ts
CHANGED
|
@@ -16,6 +16,18 @@ import { readLines } from './readLines.ts'
|
|
|
16
16
|
export const BRIDGE_PATH = '/__abide/bridge'
|
|
17
17
|
// How long reports gather before they are posted, so a streamed reply is not a post per token.
|
|
18
18
|
const BATCH_MILLISECONDS = 15
|
|
19
|
+
// 44.24 — how long a relayed MCP request waits for the site's reply.
|
|
20
|
+
const REPLY_MILLISECONDS = 120_000
|
|
21
|
+
// 44.25 — the first wait before redialing a dropped stream, doubled per failed dial up to the last.
|
|
22
|
+
const FIRST_REDIAL_MILLISECONDS = 250
|
|
23
|
+
const LAST_REDIAL_MILLISECONDS = 8_000
|
|
24
|
+
// abide's `RESUME_MILLISECONDS`, spelled out for `BRIDGE_PATH`'s reason: past it the site no longer
|
|
25
|
+
// holds the pairing, and a redial is refused.
|
|
26
|
+
export const RESUME_MILLISECONDS = 30_000
|
|
27
|
+
// abide's `HEARTBEAT_MILLISECONDS`, spelled out for `BRIDGE_PATH`'s reason.
|
|
28
|
+
export const HEARTBEAT_MILLISECONDS = 15_000
|
|
29
|
+
// 44.28 — how long the stream may go without a line before it is taken for dropped.
|
|
30
|
+
const SILENCE_MILLISECONDS = 3 * HEARTBEAT_MILLISECONDS
|
|
19
31
|
|
|
20
32
|
type SiteFrame =
|
|
21
33
|
| { type: 'paired'; token: string }
|
|
@@ -41,44 +53,98 @@ type Report =
|
|
|
41
53
|
| { type: 'end'; turn: string }
|
|
42
54
|
| { type: 'fail'; turn: string; message: string }
|
|
43
55
|
|
|
56
|
+
/** A relayed MCP request the site has yet to reply to. */
|
|
57
|
+
type Waiting = {
|
|
58
|
+
turn: string
|
|
59
|
+
method: string
|
|
60
|
+
answer: (reply: Record<string, unknown>) => void
|
|
61
|
+
deadline: ReturnType<typeof setTimeout>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** What Claude Code is answered where the site never replied: a failed call, or a JSON-RPC error. */
|
|
65
|
+
function unanswered(method: string, message: string): Record<string, unknown> {
|
|
66
|
+
return method === 'tools/call'
|
|
67
|
+
? {
|
|
68
|
+
result: {
|
|
69
|
+
content: [{ type: 'text', text: message }],
|
|
70
|
+
isError: true,
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
: { error: { code: -32603, message } }
|
|
74
|
+
}
|
|
75
|
+
|
|
44
76
|
/** Pairs with `site` by `code`, and serves its turns until the site or the stream ends it. */
|
|
45
77
|
export async function bridge(options: {
|
|
46
78
|
site: string
|
|
47
79
|
code: string
|
|
48
80
|
claude?: string
|
|
49
81
|
say?: (line: string) => void
|
|
82
|
+
replyMilliseconds?: number
|
|
83
|
+
silenceMilliseconds?: number
|
|
50
84
|
}): Promise<void> {
|
|
51
85
|
const site = options.site.replace(/\/+$/, '')
|
|
52
86
|
const say = options.say ?? ((line: string) => console.error(line))
|
|
53
|
-
const
|
|
87
|
+
const replyMilliseconds = options.replyMilliseconds ?? REPLY_MILLISECONDS
|
|
88
|
+
const silenceMilliseconds =
|
|
89
|
+
options.silenceMilliseconds ?? SILENCE_MILLISECONDS
|
|
90
|
+
// What cuts the stream being read, where it went silent.
|
|
91
|
+
let dialed = new AbortController()
|
|
92
|
+
let stream = await fetch(
|
|
54
93
|
`${site}${BRIDGE_PATH}?code=${encodeURIComponent(options.code)}`,
|
|
55
|
-
{
|
|
94
|
+
{
|
|
95
|
+
headers: { accept: 'text/event-stream' },
|
|
96
|
+
signal: dialed.signal,
|
|
97
|
+
},
|
|
56
98
|
)
|
|
57
|
-
if (!
|
|
99
|
+
if (!stream.ok || stream.body === null)
|
|
58
100
|
throw new Error(
|
|
59
|
-
`${site} refused the pairing (${
|
|
101
|
+
`${site} refused the pairing (${stream.status}): ${await stream.text()}`,
|
|
60
102
|
)
|
|
61
103
|
|
|
62
104
|
let token = ''
|
|
63
|
-
const
|
|
105
|
+
const waiting = new Map<number, Waiting>()
|
|
106
|
+
const answer = (id: number, reply: Record<string, unknown>): void => {
|
|
107
|
+
const waiter = waiting.get(id)
|
|
108
|
+
if (waiter === undefined) return
|
|
109
|
+
waiting.delete(id)
|
|
110
|
+
clearTimeout(waiter.deadline)
|
|
111
|
+
waiter.answer(reply)
|
|
112
|
+
}
|
|
113
|
+
const outbox: Report[] = []
|
|
64
114
|
let flushing: Promise<void> = Promise.resolve()
|
|
65
115
|
let scheduled = false
|
|
66
116
|
const flush = async (): Promise<void> => {
|
|
67
117
|
scheduled = false
|
|
68
|
-
const
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
118
|
+
const batch = outbox.splice(0)
|
|
119
|
+
if (batch.length === 0) return
|
|
120
|
+
const lines: string[] = []
|
|
121
|
+
for (const item of batch) lines.push(JSON.stringify(item))
|
|
122
|
+
let failure: string | undefined
|
|
123
|
+
try {
|
|
124
|
+
const posted = await fetch(`${site}${BRIDGE_PATH}`, {
|
|
125
|
+
method: 'POST',
|
|
126
|
+
headers: {
|
|
127
|
+
authorization: `Bearer ${token}`,
|
|
128
|
+
'content-type': 'application/jsonl',
|
|
129
|
+
},
|
|
130
|
+
body: lines.join('\n'),
|
|
131
|
+
})
|
|
132
|
+
if (!posted.ok)
|
|
133
|
+
failure = `${site} refused a report (${posted.status}): ${await posted.text()}`
|
|
134
|
+
else await posted.body?.cancel()
|
|
135
|
+
} catch (thrown) {
|
|
136
|
+
failure = `${site} could not be reached: ${thrown instanceof Error ? thrown.message : String(thrown)}`
|
|
137
|
+
}
|
|
138
|
+
if (failure === undefined) return
|
|
139
|
+
say(failure)
|
|
140
|
+
// 44.24 — a request the site never heard of is answered failed rather than waited on.
|
|
141
|
+
for (const item of batch)
|
|
142
|
+
if (item.type === 'mcp')
|
|
143
|
+
answer(item.id, unanswered(item.method, failure))
|
|
78
144
|
}
|
|
79
145
|
// One post at a time, in order: a turn's `end` never overtakes its last event.
|
|
80
146
|
const report = (item: Report): void => {
|
|
81
|
-
outbox.push(
|
|
147
|
+
outbox.push(item)
|
|
82
148
|
if (scheduled) return
|
|
83
149
|
scheduled = true
|
|
84
150
|
setTimeout(() => {
|
|
@@ -87,12 +153,14 @@ export async function bridge(options: {
|
|
|
87
153
|
}
|
|
88
154
|
|
|
89
155
|
const turns = new Map<string, AbortController>()
|
|
90
|
-
const waiting = new Map<number, (reply: Record<string, unknown>) => void>()
|
|
91
156
|
let asked = 0
|
|
92
157
|
const secret = crypto.randomUUID()
|
|
93
158
|
const shim = Bun.serve({
|
|
94
159
|
hostname: '127.0.0.1',
|
|
95
160
|
port: 0,
|
|
161
|
+
// A tool call takes as long as its handler does: the reply deadline (44.24) bounds the wait,
|
|
162
|
+
// where bun's own idle timeout would cut every call past 10 seconds.
|
|
163
|
+
idleTimeout: 0,
|
|
96
164
|
async fetch(request) {
|
|
97
165
|
const [, given, turn] = new URL(request.url).pathname.split('/')
|
|
98
166
|
if (given !== secret || turn === undefined || !turns.has(turn))
|
|
@@ -111,8 +179,25 @@ export async function bridge(options: {
|
|
|
111
179
|
if (message.id === undefined)
|
|
112
180
|
return new Response(null, { status: 202 })
|
|
113
181
|
const id = ++asked
|
|
182
|
+
const method = message.method
|
|
114
183
|
const reply = new Promise<Record<string, unknown>>((done) => {
|
|
115
|
-
waiting.set(id,
|
|
184
|
+
waiting.set(id, {
|
|
185
|
+
turn,
|
|
186
|
+
method,
|
|
187
|
+
answer: done,
|
|
188
|
+
// 44.24 — a reply lost on the way is a failure Claude Code is told of.
|
|
189
|
+
deadline: setTimeout(
|
|
190
|
+
() =>
|
|
191
|
+
answer(
|
|
192
|
+
id,
|
|
193
|
+
unanswered(
|
|
194
|
+
method,
|
|
195
|
+
`${site} sent no reply within ${replyMilliseconds} ms.`,
|
|
196
|
+
),
|
|
197
|
+
),
|
|
198
|
+
replyMilliseconds,
|
|
199
|
+
),
|
|
200
|
+
})
|
|
116
201
|
})
|
|
117
202
|
report({
|
|
118
203
|
type: 'mcp',
|
|
@@ -150,24 +235,85 @@ export async function bridge(options: {
|
|
|
150
235
|
})
|
|
151
236
|
} finally {
|
|
152
237
|
turns.delete(turn.id)
|
|
238
|
+
for (const [id, waiter] of waiting)
|
|
239
|
+
if (waiter.turn === turn.id)
|
|
240
|
+
answer(id, unanswered(waiter.method, 'The turn ended.'))
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// 44.25 — THE STREAM REDIALED BY ITS TOKEN, each wait twice the last, until the site refuses it or
|
|
245
|
+
// stops holding the pairing.
|
|
246
|
+
const redial = async (): Promise<Response | undefined> => {
|
|
247
|
+
if (token === '') return undefined
|
|
248
|
+
say(`Lost ${site}; reconnecting.`)
|
|
249
|
+
const until = Date.now() + RESUME_MILLISECONDS
|
|
250
|
+
let delay = FIRST_REDIAL_MILLISECONDS
|
|
251
|
+
while (Date.now() < until) {
|
|
252
|
+
await Bun.sleep(delay)
|
|
253
|
+
delay = Math.min(delay * 2, LAST_REDIAL_MILLISECONDS)
|
|
254
|
+
try {
|
|
255
|
+
dialed = new AbortController()
|
|
256
|
+
const resumed = await fetch(`${site}${BRIDGE_PATH}`, {
|
|
257
|
+
headers: {
|
|
258
|
+
accept: 'text/event-stream',
|
|
259
|
+
authorization: `Bearer ${token}`,
|
|
260
|
+
},
|
|
261
|
+
signal: dialed.signal,
|
|
262
|
+
})
|
|
263
|
+
if (resumed.ok && resumed.body !== null) {
|
|
264
|
+
say(`Reconnected to ${site}.`)
|
|
265
|
+
return resumed
|
|
266
|
+
}
|
|
267
|
+
await resumed.body?.cancel()
|
|
268
|
+
if (resumed.status < 500) return undefined
|
|
269
|
+
} catch {
|
|
270
|
+
// Unreachable for now: the next dial is the retry.
|
|
271
|
+
}
|
|
153
272
|
}
|
|
273
|
+
return undefined
|
|
154
274
|
}
|
|
155
275
|
|
|
156
276
|
try {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
277
|
+
let bye = false
|
|
278
|
+
for (;;) {
|
|
279
|
+
// 44.28 — a stream silent past the bound is cut, and redialed as one that dropped.
|
|
280
|
+
const silence = setTimeout(
|
|
281
|
+
() => dialed.abort(),
|
|
282
|
+
silenceMilliseconds,
|
|
283
|
+
)
|
|
284
|
+
try {
|
|
285
|
+
for await (const line of readLines(
|
|
286
|
+
stream.body as ReadableStream<Uint8Array>,
|
|
287
|
+
)) {
|
|
288
|
+
silence.refresh()
|
|
289
|
+
if (!line.startsWith('data: ')) continue
|
|
290
|
+
const frame = JSON.parse(
|
|
291
|
+
line.slice('data: '.length),
|
|
292
|
+
) as SiteFrame
|
|
293
|
+
if (frame.type === 'paired') {
|
|
294
|
+
token = frame.token
|
|
295
|
+
say(
|
|
296
|
+
`Connected to ${site}. Keep this running while you chat; it ends when you close the page.`,
|
|
297
|
+
)
|
|
298
|
+
} else if (frame.type === 'turn') void run(frame)
|
|
299
|
+
else if (frame.type === 'reply')
|
|
300
|
+
answer(frame.id, frame.reply)
|
|
301
|
+
else if (frame.type === 'cancel')
|
|
302
|
+
turns.get(frame.turn)?.abort()
|
|
303
|
+
else if (frame.type === 'bye') {
|
|
304
|
+
bye = true
|
|
305
|
+
break
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
} catch {
|
|
309
|
+
// A stream cut mid-read is a drop like one that ended.
|
|
310
|
+
} finally {
|
|
311
|
+
clearTimeout(silence)
|
|
312
|
+
}
|
|
313
|
+
if (bye) break
|
|
314
|
+
const resumed = await redial()
|
|
315
|
+
if (resumed === undefined) break
|
|
316
|
+
stream = resumed
|
|
171
317
|
}
|
|
172
318
|
} finally {
|
|
173
319
|
for (const controller of turns.values()) controller.abort()
|