@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
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
# @abide/bridge
|
|
2
2
|
|
|
3
|
+
## 0.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- f929fa5: one headline at every level — "<app> (<site>) is requesting access to Claude Code." — with what it asks for first in the body — 44.42 ([`3b6b3f5`](https://github.com/briancray/abide/commit/3b6b3f54ae9e6cb47eedc877c662461606e89727))
|
|
8
|
+
- f929fa5: the question's buttons are laid out as each desktop lays its own — Allow the default, Don't Allow on Escape ([`498200a`](https://github.com/briancray/abide/commit/498200a9d30faa9c1fc57a3b96bfb2d4621edf64))
|
|
9
|
+
- f929fa5: the question names what a link asks of this machine by what it lets the site do, and every MCP tool on one line counted per server — 44.65, 44.92 ([`73be13a`](https://github.com/briancray/abide/commit/73be13ab61845b0026946cdf2cb0c409be004fd7))
|
|
10
|
+
- f929fa5: every level of the question says what it requests the same way, offers Only Site Tools wherever it asks for anything of this machine, and remembers which was granted — 44.62, 44.86, 44.88 ([`7803bbe`](https://github.com/briancray/abide/commit/7803bbe849dc7450d9e11e95a9707c2cf7b9c2ae))
|
|
11
|
+
- f929fa5: Allow first, Only Site Tools beside it, Don't Allow last; each button answers by what it says — 44.86 ([`b92e292`](https://github.com/briancray/abide/commit/b92e292e8e9d0245768caad361c41b94099a3728))
|
|
12
|
+
- f929fa5: Allow is the question's default — blue, pressed by Return — and Escape is Don't Allow ([`cc2d66a`](https://github.com/briancray/abide/commit/cc2d66a38499b779f90daa5a42798a2b8cdb49e3))
|
|
13
|
+
- 7e887bb: The bridge runs Claude Code in auto mode, and asks the page about anything auto mode would ask you: `provider.claudeCode` yields each prompt as a `permission` event, and `provider.claudeCode.permit(id, allow)` answers it. A granted tool is no longer approved just for being granted. `abide-bridge --full` (and `abide-bridge install --full`) gives a site everything your Claude Code has, and the native app's own bridge always runs that way.
|
|
14
|
+
- fac557b: `abide-bridge install` run from a checkout of the bridge — `bun packages/bridge/bin/abideBridge.ts install` — makes every pairing link open with that checkout's handler and run its bridge, whatever release the link names, so an edit there is what the next link runs.
|
|
15
|
+
- af74604: A site can ask a visitor's Claude Code for full access: `provider.claudeCode.bridgeCommand({ full: true })` and `provider.claudeCode.bridge({ full: true })` carry `--full` in the command and ask for it in the link, and `ABIDE_BRIDGE_FULL` does the same for `pairing()`. Opening such a link names no tool: it says the site is requesting full access, and offers Allow, Only Site Tools or Don't Allow. Allow is now kept for the site and the person its code was minted for, with no separate Always Allow, and an ask for full access is kept as it was answered; `abide-bridge forget` undoes it. The question is a macOS alert with a bold headline, and no longer names the bridge release, which the handler's log keeps.
|
|
16
|
+
- 8d12435: A native app built with `abide bundle` now pairs its window with Claude Code on the same machine. Nothing needs to be installed, no link needs clicking and no code is spent (RULEBOOK 44.77). `provider.claudeCode` runs turns there, inside the app's process, and pages see an ordinary paired `BridgeStatus`. The window's turns get every tool the site asks for, both built-in tools and MCP tools whose server is in the visitor's Claude Code user scope. `missing` lists only MCP tools whose server it doesn't find (RULEBOOK 44.78). Only the window counts as local. It proves this with a cookie minted at each launch, never by its user agent (RULEBOOK 44.79).
|
|
17
|
+
|
|
18
|
+
The native app runs with the `PATH` the visitor's login shell sets, so it finds `claude` even when opened from the Finder (RULEBOOK 44.80). The window and the menu bar extra's panel now hand links of other schemes, such as `abide-bridge:` and `mailto:`, to macOS instead of failing them (RULEBOOK 38.174).
|
|
19
|
+
|
|
20
|
+
`@abide/abide` now depends on `@abide/bridge`. The bridge's turn handling is now one module, `@abide/bridge/serveTurns`, shared by `abide-bridge` and the native app.
|
|
21
|
+
- 9d74469: The question a pairing link asks is each desktop's own, by the asking app: on macOS a native `NSAlert` the bridge shows itself, in front, with the site's `/icon.png` as its icon; on Windows a dialog with a bold headline, the icon and labelled buttons in place of Yes/No/Cancel; on Linux zenity or kdialog titled by the app, with its icon. The headline names the app beside its site — `CRM (crm.example)` — which `/__abide/bridge`'s holder answer now carries as `name`.
|
|
22
|
+
- 6531d98: An app can say why it asks to pair: `provider.claudeCode.bridgeCommand({ note })` and `provider.claudeCode.bridge({ note })`, or `ABIDE_BRIDGE_NOTE` for `pairing()`, keep a note with the pairing's code, and the question a pairing link asks shows it under its headline as the app's own words — `From CRM: …`, one line of at most 200 characters. The rest of the question stays the bridge's.
|
|
23
|
+
|
|
24
|
+
## 0.6.2
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- 068afc4: constants abide's tests read are leaves, and grant.ts exports only what is imported ([`c16c791`](https://github.com/briancray/abide/commit/c16c791c1cee3a1a4f06e648608b42e375679dc0))
|
|
29
|
+
|
|
3
30
|
## 0.6.1
|
|
4
31
|
|
|
5
32
|
### Patch Changes
|
package/bin/abideBridge.ts
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
// `abide-bridge <site> <code> [--claude <path>] [--env NAME=VALUE]... [-- <claude options>]` — the
|
|
3
|
-
// an abide site shows you to connect your Claude Code to its assistant. `--claude <path>` names
|
|
4
|
-
// executable where `claude` is not on PATH; each `--env` sets a variable Claude Code runs with;
|
|
5
|
-
// follows `--` is passed to Claude Code as you wrote it, and is how you give a site what
|
|
6
|
-
// machine — `-- --tools WebSearch,WebFetch` for the built-in tools it asks for
|
|
2
|
+
// `abide-bridge <site> <code> [--full] [--claude <path>] [--env NAME=VALUE]... [-- <claude options>]` — the
|
|
3
|
+
// command an abide site shows you to connect your Claude Code to its assistant. `--claude <path>` names
|
|
4
|
+
// the executable where `claude` is not on PATH; each `--env` sets a variable Claude Code runs with;
|
|
5
|
+
// whatever follows `--` is passed to Claude Code as you wrote it, and is how you give a site what
|
|
6
|
+
// reaches this machine — `-- --tools WebSearch,WebFetch` for the built-in tools it asks for — and
|
|
7
|
+
// `--full` gives it everything your Claude Code has.
|
|
7
8
|
//
|
|
8
9
|
// `abide-bridge install` takes the same options and builds and registers, on macOS, Linux or Windows,
|
|
9
10
|
// what opens the pairing link a site shows beside its command, so a click pairs instead of a terminal —
|
|
10
11
|
// every link it opens runs with the `--env` and the `--` options given here (44.44); `abide-bridge
|
|
11
12
|
// uninstall` takes it away. `abide-bridge forget [<site>]` stops always allowing a site, or every site.
|
|
13
|
+
// Run from a checkout — `bun packages/bridge/bin/abideBridge.ts install` — every link opens with the
|
|
14
|
+
// checkout's handler and runs its bridge, whatever release the link names (44.89).
|
|
12
15
|
|
|
13
16
|
import { allowedIn, forget } from '../src/allowedSites.ts'
|
|
14
17
|
import { bridge } from '../src/bridge.ts'
|
|
15
18
|
import {
|
|
19
|
+
checkoutOf,
|
|
16
20
|
handlerDirectory,
|
|
17
21
|
install,
|
|
18
22
|
installDirectory,
|
|
@@ -20,19 +24,21 @@ import {
|
|
|
20
24
|
} from '../src/install.ts'
|
|
21
25
|
|
|
22
26
|
const USAGE =
|
|
23
|
-
'usage: abide-bridge <site> <code> [--claude <path>] [--env NAME=VALUE]... [-- <claude options>]\n abide-bridge install [--claude <path>] [--env NAME=VALUE]... [-- <claude options>]\n abide-bridge uninstall\n abide-bridge forget [<site>]'
|
|
27
|
+
'usage: abide-bridge <site> <code> [--full] [--claude <path>] [--env NAME=VALUE]... [-- <claude options>]\n abide-bridge install [--full] [--claude <path>] [--env NAME=VALUE]... [-- <claude options>]\n abide-bridge uninstall\n abide-bridge forget [<site>]'
|
|
24
28
|
|
|
25
29
|
const all = process.argv.slice(2)
|
|
26
30
|
const split = all.indexOf('--')
|
|
27
31
|
const args = split === -1 ? all : all.slice(0, split)
|
|
28
32
|
const claudeArgs = split === -1 ? [] : all.slice(split + 1)
|
|
29
33
|
let claude: string | undefined
|
|
34
|
+
let full = false
|
|
30
35
|
const env: Record<string, string> = {}
|
|
31
36
|
const positional: string[] = []
|
|
32
37
|
let malformed = false
|
|
33
38
|
for (let index = 0; index < args.length; index++) {
|
|
34
39
|
const arg = args[index] as string
|
|
35
40
|
if (arg === '--claude') claude = args[++index]
|
|
41
|
+
else if (arg === '--full') full = true
|
|
36
42
|
else if (arg === '--env') {
|
|
37
43
|
const assignment = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/s.exec(
|
|
38
44
|
args[++index] ?? '',
|
|
@@ -79,16 +85,23 @@ try {
|
|
|
79
85
|
: `--claude ${claude} is no executable here. Name the path to Claude Code, and run this again.`,
|
|
80
86
|
)
|
|
81
87
|
if (site === 'install') {
|
|
88
|
+
const checkout = checkoutOf(import.meta.dir)
|
|
82
89
|
const app = await install({
|
|
83
90
|
bun: process.execPath,
|
|
84
91
|
claude: found,
|
|
85
92
|
path: Bun.env.PATH ?? '',
|
|
86
93
|
claudeArgs,
|
|
94
|
+
full,
|
|
87
95
|
env,
|
|
96
|
+
...(checkout === undefined ? {} : { checkout }),
|
|
88
97
|
})
|
|
89
98
|
console.error(
|
|
90
99
|
`Installed ${app}. A site's pairing link now opens here, and asks you before it pairs. \`abide-bridge uninstall\` removes it.`,
|
|
91
100
|
)
|
|
101
|
+
if (checkout !== undefined)
|
|
102
|
+
console.error(
|
|
103
|
+
`Every link runs the bridge in ${checkout}, whatever release it names, and your edits there are what the next link runs. Run install from a release to stop.`,
|
|
104
|
+
)
|
|
92
105
|
} else {
|
|
93
106
|
// Claude Code is started with this process's environment.
|
|
94
107
|
Object.assign(process.env, env)
|
|
@@ -96,6 +109,7 @@ try {
|
|
|
96
109
|
site,
|
|
97
110
|
code: code as string,
|
|
98
111
|
claudeArgs,
|
|
112
|
+
full,
|
|
99
113
|
claude: found,
|
|
100
114
|
})
|
|
101
115
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abide/bridge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.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",
|
|
@@ -18,6 +18,11 @@
|
|
|
18
18
|
"access": "public",
|
|
19
19
|
"provenance": true
|
|
20
20
|
},
|
|
21
|
+
"exports": {
|
|
22
|
+
"./serveTurns": "./src/serveTurns.ts",
|
|
23
|
+
"./grant": "./src/grant.ts",
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
21
26
|
"bin": {
|
|
22
27
|
"abide-bridge": "./bin/abideBridge.ts"
|
|
23
28
|
},
|
package/src/allowedSites.ts
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
// THE SITES A VISITOR ALWAYS ALLOWS (44.62), kept beside the handler install wrote so uninstall takes
|
|
2
2
|
// them too. ONE PER SITE AND HOLDER: a link pairs unasked only where its code was minted for the same
|
|
3
|
-
// holder `/__abide/bridge` named when the visitor
|
|
4
|
-
//
|
|
3
|
+
// holder `/__abide/bridge` named when the visitor allowed it — a code an attacker minted is minted for
|
|
4
|
+
// the attacker, and is asked about. A visitor who allowed a site full access, or only its own tools
|
|
5
|
+
// (44.86), is not asked again: it runs as they allowed it, whatever it asks for.
|
|
5
6
|
import { siteOf } from './pairingLink.ts'
|
|
6
7
|
|
|
7
|
-
/**
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* A site paired unasked, for one holder, while a link asks for none but `tools` — or whatever it asks
|
|
10
|
+
* for, where the visitor `granted` it full access or only its own tools.
|
|
11
|
+
*/
|
|
12
|
+
export type AllowedSite = {
|
|
13
|
+
site: string
|
|
14
|
+
holder: string
|
|
15
|
+
tools: string[]
|
|
16
|
+
granted?: 'full' | 'siteTools'
|
|
17
|
+
}
|
|
9
18
|
|
|
10
19
|
/** The file the sites `directory`'s handler always allows are kept in. */
|
|
11
20
|
export function allowedIn(directory: string): string {
|
|
@@ -18,28 +27,38 @@ export async function allowedSites(file: string): Promise<AllowedSite[]> {
|
|
|
18
27
|
return (await kept.exists()) ? ((await kept.json()) as AllowedSite[]) : []
|
|
19
28
|
}
|
|
20
29
|
|
|
21
|
-
/**
|
|
22
|
-
|
|
30
|
+
/**
|
|
31
|
+
* The site `sites` let a link for `site`, minted for `holder`, pair unasked as, or `undefined` where
|
|
32
|
+
* they do not: it asks for a tool beyond those allowed, or for full access, and was granted neither
|
|
33
|
+
* full access nor only its own tools. 44.62.
|
|
34
|
+
*/
|
|
35
|
+
export function allowedAs(
|
|
23
36
|
sites: readonly AllowedSite[],
|
|
24
37
|
site: string,
|
|
25
38
|
holder: string,
|
|
26
|
-
tools: readonly string[],
|
|
27
|
-
):
|
|
39
|
+
asked: { tools: readonly string[]; full: boolean },
|
|
40
|
+
): AllowedSite | undefined {
|
|
28
41
|
for (const allowed of sites)
|
|
29
42
|
if (allowed.site === site && allowed.holder === holder) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
43
|
+
if (allowed.granted !== undefined) return allowed
|
|
44
|
+
if (asked.full) return undefined
|
|
45
|
+
for (const tool of asked.tools)
|
|
46
|
+
if (!allowed.tools.includes(tool)) return undefined
|
|
47
|
+
return allowed
|
|
33
48
|
}
|
|
34
|
-
return
|
|
49
|
+
return undefined
|
|
35
50
|
}
|
|
36
51
|
|
|
37
|
-
/**
|
|
52
|
+
/**
|
|
53
|
+
* Keeps `site` always allowed for `holder`, with `tools` beside the ones allowed before, and — where
|
|
54
|
+
* the visitor answered so — `granted` full access or only its own tools.
|
|
55
|
+
*/
|
|
38
56
|
export async function allowAlways(
|
|
39
57
|
file: string,
|
|
40
58
|
site: string,
|
|
41
59
|
holder: string,
|
|
42
60
|
tools: readonly string[],
|
|
61
|
+
granted?: 'full' | 'siteTools',
|
|
43
62
|
): Promise<void> {
|
|
44
63
|
const sites = await allowedSites(file)
|
|
45
64
|
let entry = sites.find(
|
|
@@ -51,6 +70,7 @@ export async function allowAlways(
|
|
|
51
70
|
}
|
|
52
71
|
for (const tool of tools)
|
|
53
72
|
if (!entry.tools.includes(tool)) entry.tools.push(tool)
|
|
73
|
+
if (granted !== undefined) entry.granted = granted
|
|
54
74
|
await Bun.write(file, JSON.stringify(sites, null, 4))
|
|
55
75
|
}
|
|
56
76
|
|
package/src/bridge.ts
CHANGED
|
@@ -7,79 +7,29 @@
|
|
|
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
|
|
11
|
-
|
|
12
|
-
import { claudeTurn, type TurnRequest } from './claudeTurn.ts'
|
|
10
|
+
import { BRIDGE_PATH } from './BRIDGE_PATH.ts'
|
|
13
11
|
import { grantFor, withheldNotices } from './grant.ts'
|
|
12
|
+
import { HEARTBEAT_MILLISECONDS } from './HEARTBEAT_MILLISECONDS.ts'
|
|
14
13
|
import { siteOf } from './pairingLink.ts'
|
|
15
14
|
import { readLines } from './readLines.ts'
|
|
15
|
+
import { RESUME_MILLISECONDS } from './RESUME_MILLISECONDS.ts'
|
|
16
|
+
import { serveTurns, type TurnFrame, type TurnReport } from './serveTurns.ts'
|
|
16
17
|
import { siteTls } from './siteTls.ts'
|
|
17
18
|
|
|
18
|
-
// abide's `BRIDGE_ADDRESS`, spelled out because the bridge carries no abide at run time — a test
|
|
19
|
-
// holds the two equal.
|
|
20
|
-
export const BRIDGE_PATH = '/__abide/bridge'
|
|
21
19
|
// How long reports gather before they are posted, so a streamed reply is not a post per token.
|
|
22
20
|
const BATCH_MILLISECONDS = 15
|
|
23
|
-
// 44.24 — how long a relayed MCP request waits for the site's reply.
|
|
24
|
-
const REPLY_MILLISECONDS = 120_000
|
|
25
21
|
// 44.25 — the first wait before redialing a dropped stream, doubled per failed dial up to the last.
|
|
26
22
|
const FIRST_REDIAL_MILLISECONDS = 250
|
|
27
23
|
const LAST_REDIAL_MILLISECONDS = 8_000
|
|
28
|
-
// abide's `RESUME_MILLISECONDS`, spelled out for `BRIDGE_PATH`'s reason: past it the site no longer
|
|
29
|
-
// holds the pairing, and a redial is refused.
|
|
30
|
-
export const RESUME_MILLISECONDS = 60_000
|
|
31
|
-
// abide's `HEARTBEAT_MILLISECONDS`, spelled out for `BRIDGE_PATH`'s reason.
|
|
32
|
-
export const HEARTBEAT_MILLISECONDS = 15_000
|
|
33
24
|
// 44.27 — how long the stream may go without a line before it is taken for dropped.
|
|
34
25
|
export const SILENCE_MILLISECONDS = 3 * HEARTBEAT_MILLISECONDS
|
|
35
26
|
|
|
36
27
|
type SiteFrame =
|
|
37
28
|
| { type: 'paired'; token: string; tools?: string[] }
|
|
38
|
-
|
|
|
39
|
-
| {
|
|
40
|
-
type: 'reply'
|
|
41
|
-
turn: string
|
|
42
|
-
id: number
|
|
43
|
-
reply: Record<string, unknown>
|
|
44
|
-
}
|
|
45
|
-
| { type: 'cancel'; turn: string }
|
|
29
|
+
| TurnFrame
|
|
46
30
|
| { type: 'bye' }
|
|
47
31
|
|
|
48
|
-
type Report =
|
|
49
|
-
| { type: 'event'; turn: string; event: ProviderEvent }
|
|
50
|
-
| {
|
|
51
|
-
type: 'mcp'
|
|
52
|
-
turn: string
|
|
53
|
-
id: number
|
|
54
|
-
method: string
|
|
55
|
-
params: unknown
|
|
56
|
-
}
|
|
57
|
-
| { type: 'end'; turn: string }
|
|
58
|
-
| { type: 'fail'; turn: string; message: string }
|
|
59
|
-
| { type: 'missing'; tools: string[] }
|
|
60
|
-
|
|
61
|
-
/** A relayed MCP request the site has yet to reply to. */
|
|
62
|
-
type Waiting = {
|
|
63
|
-
turn: string
|
|
64
|
-
method: string
|
|
65
|
-
answer: (reply: Record<string, unknown>) => void
|
|
66
|
-
deadline: ReturnType<typeof setTimeout>
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/** 44.46 — the abort reason of a turn the site missed part of, which it reports failed with. */
|
|
70
|
-
class MissedTurn extends Error {}
|
|
71
|
-
|
|
72
|
-
/** What Claude Code is answered where the site never replied: a failed call, or a JSON-RPC error. */
|
|
73
|
-
function unanswered(method: string, message: string): Record<string, unknown> {
|
|
74
|
-
return method === 'tools/call'
|
|
75
|
-
? {
|
|
76
|
-
result: {
|
|
77
|
-
content: [{ type: 'text', text: message }],
|
|
78
|
-
isError: true,
|
|
79
|
-
},
|
|
80
|
-
}
|
|
81
|
-
: { error: { code: -32603, message } }
|
|
82
|
-
}
|
|
32
|
+
type Report = TurnReport | { type: 'missing'; tools: string[] }
|
|
83
33
|
|
|
84
34
|
/** Pairs with `site` by `code`, and serves its turns until the site or the stream ends it. */
|
|
85
35
|
export async function bridge(options: {
|
|
@@ -87,6 +37,8 @@ export async function bridge(options: {
|
|
|
87
37
|
code: string
|
|
88
38
|
claude?: string
|
|
89
39
|
claudeArgs?: string[]
|
|
40
|
+
/** Everything `claude` has is the site's to use, prompts asked of its page. 44.82. */
|
|
41
|
+
full?: boolean
|
|
90
42
|
say?: (line: string) => void
|
|
91
43
|
replyMilliseconds?: number
|
|
92
44
|
silenceMilliseconds?: number
|
|
@@ -97,7 +49,7 @@ export async function bridge(options: {
|
|
|
97
49
|
// sentence naming the site by its host. The detail goes to Claude Code, in the failure it is answered.
|
|
98
50
|
const host = new URL(site).host
|
|
99
51
|
const say = options.say ?? ((line: string) => console.error(line))
|
|
100
|
-
const
|
|
52
|
+
const full = options.full ?? false
|
|
101
53
|
const silenceMilliseconds =
|
|
102
54
|
options.silenceMilliseconds ?? SILENCE_MILLISECONDS
|
|
103
55
|
// What cuts the stream being read, where it went silent.
|
|
@@ -116,14 +68,6 @@ export async function bridge(options: {
|
|
|
116
68
|
)
|
|
117
69
|
|
|
118
70
|
let token = ''
|
|
119
|
-
const waiting = new Map<number, Waiting>()
|
|
120
|
-
const answer = (id: number, reply: Record<string, unknown>): void => {
|
|
121
|
-
const waiter = waiting.get(id)
|
|
122
|
-
if (waiter === undefined) return
|
|
123
|
-
waiting.delete(id)
|
|
124
|
-
clearTimeout(waiter.deadline)
|
|
125
|
-
waiter.answer(reply)
|
|
126
|
-
}
|
|
127
71
|
const outbox: Report[] = []
|
|
128
72
|
let flushing: Promise<void> = Promise.resolve()
|
|
129
73
|
let scheduled = false
|
|
@@ -170,13 +114,10 @@ export async function bridge(options: {
|
|
|
170
114
|
// A lost `missing` is said again by the next look, rather than taken as said.
|
|
171
115
|
if (item.type === 'missing') reported = undefined
|
|
172
116
|
else
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
`The site missed part of this turn, so it was ended: ${failure}`,
|
|
178
|
-
),
|
|
179
|
-
)
|
|
117
|
+
core.miss(
|
|
118
|
+
item.turn,
|
|
119
|
+
`The site missed part of this turn, so it was ended: ${failure}`,
|
|
120
|
+
)
|
|
180
121
|
}
|
|
181
122
|
// One post at a time, in order: a turn's `end` never overtakes its last event.
|
|
182
123
|
const report = (item: Report): void => {
|
|
@@ -204,6 +145,7 @@ export async function bridge(options: {
|
|
|
204
145
|
const { grant } = await grantFor(
|
|
205
146
|
pairingAsked,
|
|
206
147
|
options.claudeArgs ?? [],
|
|
148
|
+
full,
|
|
207
149
|
sayOnce,
|
|
208
150
|
)
|
|
209
151
|
const missing = [...grant.ungranted, ...grant.unserved]
|
|
@@ -214,114 +156,27 @@ export async function bridge(options: {
|
|
|
214
156
|
report({ type: 'missing', tools: missing })
|
|
215
157
|
}
|
|
216
158
|
|
|
217
|
-
const
|
|
159
|
+
const core = serveTurns({
|
|
160
|
+
site,
|
|
161
|
+
claude: options.claude ?? 'claude',
|
|
162
|
+
claudeArgs: options.claudeArgs ?? [],
|
|
163
|
+
full,
|
|
164
|
+
report,
|
|
165
|
+
say: sayOnce,
|
|
166
|
+
beforeTurn: reportMissing,
|
|
167
|
+
...(options.replyMilliseconds === undefined
|
|
168
|
+
? {}
|
|
169
|
+
: { replyMilliseconds: options.replyMilliseconds }),
|
|
170
|
+
})
|
|
218
171
|
// 44.30 — A SIGNAL ENDS THE RUNS WITH THE BRIDGE: exiting on one skips every `finally`, so a run
|
|
219
172
|
// still going would outlive it, orphaned.
|
|
220
173
|
const signals = { SIGHUP: 129, SIGINT: 130, SIGTERM: 143 } as const
|
|
221
174
|
const stopped = (signal: keyof typeof signals): void => {
|
|
222
|
-
|
|
175
|
+
core.cancelAll()
|
|
223
176
|
process.exit(signals[signal])
|
|
224
177
|
}
|
|
225
178
|
for (const signal of Object.keys(signals) as (keyof typeof signals)[])
|
|
226
179
|
process.on(signal, stopped)
|
|
227
|
-
let asked = 0
|
|
228
|
-
const secret = crypto.randomUUID()
|
|
229
|
-
const shim = Bun.serve({
|
|
230
|
-
hostname: '127.0.0.1',
|
|
231
|
-
port: 0,
|
|
232
|
-
// A tool call takes as long as its handler does: the reply deadline (44.24) bounds the wait,
|
|
233
|
-
// where bun's own idle timeout would cut every call past 10 seconds.
|
|
234
|
-
idleTimeout: 0,
|
|
235
|
-
async fetch(request) {
|
|
236
|
-
const [, given, turn] = new URL(request.url).pathname.split('/')
|
|
237
|
-
if (given !== secret || turn === undefined || !turns.has(turn))
|
|
238
|
-
return new Response(null, { status: 404 })
|
|
239
|
-
if (request.method !== 'POST')
|
|
240
|
-
return new Response(null, {
|
|
241
|
-
status: 405,
|
|
242
|
-
headers: { allow: 'POST' },
|
|
243
|
-
})
|
|
244
|
-
const message = (await request.json()) as {
|
|
245
|
-
id?: number | string
|
|
246
|
-
method: string
|
|
247
|
-
params?: unknown
|
|
248
|
-
}
|
|
249
|
-
// A notification is acknowledged here; there is nothing for the site to answer.
|
|
250
|
-
if (message.id === undefined)
|
|
251
|
-
return new Response(null, { status: 202 })
|
|
252
|
-
const id = ++asked
|
|
253
|
-
const method = message.method
|
|
254
|
-
const reply = new Promise<Record<string, unknown>>((done) => {
|
|
255
|
-
waiting.set(id, {
|
|
256
|
-
turn,
|
|
257
|
-
method,
|
|
258
|
-
answer: done,
|
|
259
|
-
// 44.24 — a reply lost on the way is a failure Claude Code is told of.
|
|
260
|
-
deadline: setTimeout(
|
|
261
|
-
() =>
|
|
262
|
-
answer(
|
|
263
|
-
id,
|
|
264
|
-
unanswered(
|
|
265
|
-
method,
|
|
266
|
-
`${site} sent no reply within ${replyMilliseconds} ms.`,
|
|
267
|
-
),
|
|
268
|
-
),
|
|
269
|
-
replyMilliseconds,
|
|
270
|
-
),
|
|
271
|
-
})
|
|
272
|
-
})
|
|
273
|
-
report({
|
|
274
|
-
type: 'mcp',
|
|
275
|
-
turn,
|
|
276
|
-
id,
|
|
277
|
-
method: message.method,
|
|
278
|
-
params: message.params,
|
|
279
|
-
})
|
|
280
|
-
return Response.json({
|
|
281
|
-
jsonrpc: '2.0',
|
|
282
|
-
id: message.id,
|
|
283
|
-
...(await reply),
|
|
284
|
-
})
|
|
285
|
-
},
|
|
286
|
-
})
|
|
287
|
-
|
|
288
|
-
const run = async (turn: TurnRequest): Promise<void> => {
|
|
289
|
-
const controller = new AbortController()
|
|
290
|
-
turns.set(turn.id, controller)
|
|
291
|
-
await reportMissing()
|
|
292
|
-
// A cancel or a signal aborts with no reason of its own, and ends the turn as asked.
|
|
293
|
-
const missed = (): string | undefined =>
|
|
294
|
-
controller.signal.reason instanceof MissedTurn
|
|
295
|
-
? controller.signal.reason.message
|
|
296
|
-
: undefined
|
|
297
|
-
try {
|
|
298
|
-
await claudeTurn(turn, {
|
|
299
|
-
claude: options.claude ?? 'claude',
|
|
300
|
-
claudeArgs: options.claudeArgs ?? [],
|
|
301
|
-
mcpUrl: `http://127.0.0.1:${shim.port}/${secret}/${turn.id}`,
|
|
302
|
-
signal: controller.signal,
|
|
303
|
-
report: (event) =>
|
|
304
|
-
report({ type: 'event', turn: turn.id, event }),
|
|
305
|
-
say: sayOnce,
|
|
306
|
-
})
|
|
307
|
-
const message = missed()
|
|
308
|
-
if (message === undefined) report({ type: 'end', turn: turn.id })
|
|
309
|
-
else report({ type: 'fail', turn: turn.id, message })
|
|
310
|
-
} catch (thrown) {
|
|
311
|
-
report({
|
|
312
|
-
type: 'fail',
|
|
313
|
-
turn: turn.id,
|
|
314
|
-
message:
|
|
315
|
-
missed() ??
|
|
316
|
-
(thrown instanceof Error ? thrown.message : String(thrown)),
|
|
317
|
-
})
|
|
318
|
-
} finally {
|
|
319
|
-
turns.delete(turn.id)
|
|
320
|
-
for (const [id, waiter] of waiting)
|
|
321
|
-
if (waiter.turn === turn.id)
|
|
322
|
-
answer(id, unanswered(waiter.method, 'The turn ended.'))
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
180
|
|
|
326
181
|
// 44.25 — THE STREAM REDIALED BY ITS TOKEN, each wait twice the last, until the site refuses it or
|
|
327
182
|
// stops holding the pairing.
|
|
@@ -379,18 +234,15 @@ export async function bridge(options: {
|
|
|
379
234
|
token = frame.token
|
|
380
235
|
pairingAsked = frame.tools ?? []
|
|
381
236
|
say(
|
|
382
|
-
|
|
237
|
+
full
|
|
238
|
+
? `Connected to ${host}, which can use everything your Claude Code can. Close the page to disconnect.`
|
|
239
|
+
: `Connected to ${host}. Close the page to disconnect.`,
|
|
383
240
|
)
|
|
384
241
|
void reportMissing()
|
|
385
|
-
} else if (frame.type === '
|
|
386
|
-
else if (frame.type === 'reply')
|
|
387
|
-
answer(frame.id, frame.reply)
|
|
388
|
-
else if (frame.type === 'cancel')
|
|
389
|
-
turns.get(frame.turn)?.abort()
|
|
390
|
-
else if (frame.type === 'bye') {
|
|
242
|
+
} else if (frame.type === 'bye') {
|
|
391
243
|
bye = true
|
|
392
244
|
break
|
|
393
|
-
}
|
|
245
|
+
} else core.take(frame)
|
|
394
246
|
}
|
|
395
247
|
} catch {
|
|
396
248
|
// A stream cut mid-read is a drop like one that ended.
|
|
@@ -405,10 +257,10 @@ export async function bridge(options: {
|
|
|
405
257
|
} finally {
|
|
406
258
|
for (const signal of Object.keys(signals) as (keyof typeof signals)[])
|
|
407
259
|
process.off(signal, stopped)
|
|
408
|
-
|
|
260
|
+
core.cancelAll()
|
|
409
261
|
await flushing
|
|
410
262
|
await flush()
|
|
411
|
-
|
|
263
|
+
core.stop()
|
|
412
264
|
say(`Disconnected from ${host}.`)
|
|
413
265
|
}
|
|
414
266
|
}
|