@abide/bridge 0.2.0 → 0.3.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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @abide/bridge
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - bd2a74f: Breaking: an assistant announces a tool call as the model starts writing it, and a site may ask the visitor's Claude Code for built-in tools ([`c959804`](https://github.com/briancray/abide/commit/c959804fd19c3e99fe439f3f1120f1ef13f42ff8))
8
+
3
9
  ## 0.2.0
4
10
 
5
11
  ### Minor Changes
@@ -1,10 +1,15 @@
1
1
  #!/usr/bin/env bun
2
- // `abide-bridge <site> <code>` — the command an abide site shows you to connect your Claude Code to its
3
- // assistant. `--claude <path>` names the executable where `claude` is not on PATH.
2
+ // `abide-bridge <site> <code> [--claude <path>] [-- <claude options>]` — the command an abide site shows
3
+ // you to connect your Claude Code to its assistant. `--claude <path>` names the executable where `claude`
4
+ // is not on PATH; whatever follows `--` is passed to Claude Code as you wrote it, and is how you give a
5
+ // site what reaches this machine — `-- --tools WebSearch,WebFetch` for the built-in tools it asks for.
4
6
 
5
7
  import { bridge } from '../src/bridge.ts'
6
8
 
7
- const args = process.argv.slice(2)
9
+ const all = process.argv.slice(2)
10
+ const split = all.indexOf('--')
11
+ const args = split === -1 ? all : all.slice(0, split)
12
+ const claudeArgs = split === -1 ? [] : all.slice(split + 1)
8
13
  const flag = args.indexOf('--claude')
9
14
  const claude = flag === -1 ? undefined : args[flag + 1]
10
15
  const [site, code] = args.filter(
@@ -12,7 +17,9 @@ const [site, code] = args.filter(
12
17
  )
13
18
 
14
19
  if (site === undefined || code === undefined) {
15
- console.error('usage: abide-bridge <site> <code> [--claude <path>]')
20
+ console.error(
21
+ 'usage: abide-bridge <site> <code> [--claude <path>] [-- <claude options>]',
22
+ )
16
23
  process.exit(2)
17
24
  }
18
25
  if (claude === undefined && Bun.which('claude') === null) {
@@ -23,7 +30,12 @@ if (claude === undefined && Bun.which('claude') === null) {
23
30
  }
24
31
 
25
32
  try {
26
- await bridge({ site, code, ...(claude === undefined ? {} : { claude }) })
33
+ await bridge({
34
+ site,
35
+ code,
36
+ claudeArgs,
37
+ ...(claude === undefined ? {} : { claude }),
38
+ })
27
39
  process.exit(0)
28
40
  } catch (thrown) {
29
41
  console.error(thrown instanceof Error ? thrown.message : String(thrown))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abide/bridge",
3
- "version": "0.2.0",
3
+ "version": "0.3.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",
package/src/bridge.ts CHANGED
@@ -78,6 +78,7 @@ export async function bridge(options: {
78
78
  site: string
79
79
  code: string
80
80
  claude?: string
81
+ claudeArgs?: string[]
81
82
  say?: (line: string) => void
82
83
  replyMilliseconds?: number
83
84
  silenceMilliseconds?: number
@@ -152,6 +153,14 @@ export async function bridge(options: {
152
153
  }, BATCH_MILLISECONDS)
153
154
  }
154
155
 
156
+ // A notice a turn repeats is said once, not once per turn.
157
+ const said = new Set<string>()
158
+ const sayOnce = (line: string): void => {
159
+ if (said.has(line)) return
160
+ said.add(line)
161
+ say(line)
162
+ }
163
+
155
164
  const turns = new Map<string, AbortController>()
156
165
  let asked = 0
157
166
  const secret = crypto.randomUUID()
@@ -220,10 +229,12 @@ export async function bridge(options: {
220
229
  try {
221
230
  await claudeTurn(turn, {
222
231
  claude: options.claude ?? 'claude',
232
+ claudeArgs: options.claudeArgs ?? [],
223
233
  mcpUrl: `http://127.0.0.1:${shim.port}/${secret}/${turn.id}`,
224
234
  signal: controller.signal,
225
235
  report: (event) =>
226
236
  report({ type: 'event', turn: turn.id, event }),
237
+ say: sayOnce,
227
238
  })
228
239
  report({ type: 'end', turn: turn.id })
229
240
  } catch (thrown) {
package/src/claudeTurn.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  // ONE TURN OF THE CLAUDE CODE ON THIS MACHINE, run for a site and translated into the events abide's
2
2
  // `assistant` yields. Claude Code runs its own loop; its tools are the site's, reached through the MCP
3
- // server this bridge runs on loopback, and nothing else.
3
+ // server this bridge runs on loopback, and whatever built-in tools the visitor gave this bridge.
4
4
  //
5
- // ITS OWN TOOLS OFF, NO SETTINGS READ, NO MCP SERVER BUT THE BRIDGE'S, IN AN EMPTY DIRECTORY: the site
6
- // chose the prompt, and nothing of this machine — a user's settings, a project's CLAUDE.md, the files
7
- // around it, its shell — is the site's to reach. None of that is something the site can ask for.
5
+ // THE SITE ASKS, THE VISITOR GRANTS (44.15, 44.16, 44.30). Every option is Claude Code's own, by name:
6
+ // a site's is laid on as its flag where it only shapes the answer, and its `tools` are a request the
7
+ // visitor's own `--tools` narrows. What reaches this machine — a user's settings, a project's
8
+ // CLAUDE.md, the files around it, its shell, the web from its network — is the visitor's to give, on
9
+ // the bridge's command line, and nothing the site sends can.
8
10
 
9
11
  import { mkdtemp, rm } from 'node:fs/promises' // bun has no temporary directory or recursive remove of its own
10
12
  import { tmpdir } from 'node:os' // bun exposes no temporary directory of its own
@@ -13,7 +15,9 @@ import { readLines } from './readLines.ts'
13
15
 
14
16
  // What Claude Code names the one MCP server it is handed, and so the prefix on each tool it calls.
15
17
  const SERVER_NAME = 'app'
16
- const TOOL_PREFIX = `mcp__${SERVER_NAME}__`
18
+ // Every tool of that server, as `--allowedTools` names them.
19
+ const SERVER_TOOLS = `mcp__${SERVER_NAME}`
20
+ const TOOL_PREFIX = `${SERVER_TOOLS}__`
17
21
 
18
22
  /** What the site asks of one turn. */
19
23
  export type TurnRequest = {
@@ -35,7 +39,11 @@ type McpContent = { type: string; text?: string }
35
39
  type CodeLine = {
36
40
  type: string
37
41
  subtype?: string
38
- event?: { type: string; delta?: { type: string; text?: string } }
42
+ event?: {
43
+ type: string
44
+ delta?: { type: string; text?: string }
45
+ content_block?: { type: string; id?: string; name?: string }
46
+ }
39
47
  message?: {
40
48
  content:
41
49
  | string
@@ -55,20 +63,110 @@ type CodeLine = {
55
63
  usage?: Usage
56
64
  }
57
65
 
58
- /** The flags that carry what a site may set, in Claude Code's own spelling. */
59
- function configFlags(config: ClaudeCodeConfig): string[] {
66
+ // 44.16 — the options a site may set: each only shapes the answer, and reaches nothing of this machine.
67
+ const SITE_OPTIONS = new Set([
68
+ 'model',
69
+ 'fallbackModel',
70
+ 'systemPrompt',
71
+ 'appendSystemPrompt',
72
+ 'effort',
73
+ 'maxBudgetUsd',
74
+ 'betas',
75
+ 'disallowedTools',
76
+ ])
77
+
78
+ /** Claude Code's flag for an option named in camelCase: `fallbackModel` is `--fallback-model`. */
79
+ function flagOf(option: string): string {
80
+ return `--${option.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}`
81
+ }
82
+
83
+ /**
84
+ * The flags carrying what a site set, in Claude Code's own spelling, a list's values comma-joined.
85
+ *
86
+ * Throws where the site set an option it may not (44.16).
87
+ */
88
+ function siteFlags(config: Omit<ClaudeCodeConfig, 'tools'>): string[] {
60
89
  const flags: string[] = []
61
- if (config.model !== undefined) flags.push('--model', config.model)
62
- if (config.fallbackModel !== undefined)
63
- flags.push('--fallback-model', config.fallbackModel)
64
- if (config.systemPrompt !== undefined)
65
- flags.push('--system-prompt', config.systemPrompt)
66
- if (config.appendSystemPrompt !== undefined)
67
- flags.push('--append-system-prompt', config.appendSystemPrompt)
68
- if (config.effort !== undefined) flags.push('--effort', config.effort)
90
+ for (const option in config) {
91
+ // Read as it arrived: the site's config is JSON, whatever its type said.
92
+ const value: unknown = config[option as keyof typeof config]
93
+ if (value === undefined) continue
94
+ if (!SITE_OPTIONS.has(option))
95
+ throw new Error(
96
+ `This bridge takes no \`${option}\` from a site (44.16): what reaches the visitor's machine is theirs to pass to abide-bridge.`,
97
+ )
98
+ flags.push(
99
+ flagOf(option),
100
+ Array.isArray(value) ? value.join(',') : String(value),
101
+ )
102
+ }
69
103
  return flags
70
104
  }
71
105
 
106
+ /**
107
+ * The values the visitor gave a flag by any of its `spellings` — `--flag a b`, `--flag a,b` or
108
+ * `--flag=a` — taken out of `args`, or `undefined` where they never gave it.
109
+ */
110
+ function takeFlag(args: string[], spellings: string[]): string[] | undefined {
111
+ let values: string[] | undefined
112
+ for (let index = 0; index < args.length; ) {
113
+ const arg = args[index] as string
114
+ const [flag, inline] = arg.split(/=(.*)/s) as [string, string?]
115
+ if (!spellings.includes(flag)) {
116
+ index++
117
+ continue
118
+ }
119
+ values ??= []
120
+ if (inline !== undefined) {
121
+ values.push(inline)
122
+ args.splice(index, 1)
123
+ } else {
124
+ let end = index + 1
125
+ while (end < args.length && !(args[end] as string).startsWith('-'))
126
+ end++
127
+ values.push(...args.slice(index + 1, end))
128
+ args.splice(index, end - index)
129
+ }
130
+ }
131
+ return values
132
+ }
133
+
134
+ /**
135
+ * Every flag but the protocol's: the site's, then the visitor's — so theirs wins where both set one —
136
+ * then the built-in tools both named (44.30), and no setting source where the visitor named none.
137
+ * Beside them, the tools the site asked for that the visitor did not give.
138
+ */
139
+ function launchFlags(
140
+ config: ClaudeCodeConfig,
141
+ visitor: string[],
142
+ ): { flags: string[]; ungranted: string[] } {
143
+ const { tools: asked, ...options } = config
144
+ const args = [...visitor]
145
+ const granted = new Set<string>()
146
+ for (const value of takeFlag(args, ['--tools']) ?? [])
147
+ for (const tool of value.split(/[\s,]+/)) granted.add(tool)
148
+ const allowed = takeFlag(args, ['--allowedTools', '--allowed-tools'])
149
+ const sources = takeFlag(args, ['--setting-sources']) ?? []
150
+ const tools: string[] = []
151
+ const ungranted: string[] = []
152
+ for (const tool of asked ?? [])
153
+ (granted.has(tool) ? tools : ungranted).push(tool)
154
+ const flags = [
155
+ ...siteFlags(options),
156
+ ...args,
157
+ '--tools',
158
+ tools.join(','),
159
+ // The site's own tools are always approved; a built-in one is approved as granted, unless
160
+ // the visitor said which calls of it are.
161
+ '--allowedTools',
162
+ SERVER_TOOLS,
163
+ ...(allowed ?? tools),
164
+ '--setting-sources',
165
+ sources.join(','),
166
+ ]
167
+ return { flags, ungranted }
168
+ }
169
+
72
170
  function textOf(content: McpContent[] | string | undefined): string {
73
171
  if (typeof content === 'string') return content
74
172
  let text = ''
@@ -100,11 +198,18 @@ export async function claudeTurn(
100
198
  turn: TurnRequest,
101
199
  options: {
102
200
  claude: string
201
+ claudeArgs: string[]
103
202
  mcpUrl: string
104
203
  signal: AbortSignal
105
204
  report: (event: AssistantEvent) => void
205
+ say: (line: string) => void
106
206
  },
107
207
  ): Promise<void> {
208
+ const { flags, ungranted } = launchFlags(turn.config, options.claudeArgs)
209
+ if (ungranted.length > 0)
210
+ options.say(
211
+ `The site asked for ${ungranted.join(', ')}, which this bridge was not given. Run it with \`-- --tools ${ungranted.join(',')}\` to allow it.`,
212
+ )
108
213
  const directory = await mkdtemp(`${tmpdir()}/abide-bridge-`)
109
214
  const config = `${directory}/mcp.json`
110
215
  await Bun.write(
@@ -118,6 +223,8 @@ export async function claudeTurn(
118
223
  const child = Bun.spawn({
119
224
  cmd: [
120
225
  options.claude,
226
+ ...flags,
227
+ // The protocol's own, last so that nothing before them overrides one.
121
228
  '-p',
122
229
  '--input-format',
123
230
  'stream-json',
@@ -126,18 +233,11 @@ export async function claudeTurn(
126
233
  '--verbose',
127
234
  '--include-partial-messages',
128
235
  '--no-session-persistence',
129
- '--tools',
130
- '',
131
- '--setting-sources',
132
- '',
133
236
  '--strict-mcp-config',
134
237
  '--mcp-config',
135
238
  config,
136
- '--allowedTools',
137
- TOOL_PREFIX.slice(0, -2),
138
239
  '--max-turns',
139
240
  String(turn.maxTurns),
140
- ...configFlags(turn.config),
141
241
  ],
142
242
  cwd: directory,
143
243
  stdin: 'pipe',
@@ -156,11 +256,22 @@ export async function claudeTurn(
156
256
  const line = JSON.parse(text) as CodeLine
157
257
  if (line.type === 'stream_event') {
158
258
  const delta = line.event?.delta
259
+ const started = line.event?.content_block
159
260
  if (
160
261
  line.event?.type === 'content_block_delta' &&
161
262
  delta?.type === 'text_delta'
162
263
  )
163
264
  options.report({ type: 'text', text: delta.text as string })
265
+ // 44.29 — the call is announced as the model starts writing it, its input once written.
266
+ else if (
267
+ line.event?.type === 'content_block_start' &&
268
+ started?.type === 'tool_use'
269
+ )
270
+ options.report({
271
+ type: 'tool',
272
+ id: started.id as string,
273
+ name: (started.name as string).replace(TOOL_PREFIX, ''),
274
+ })
164
275
  } else if (
165
276
  (line.type === 'assistant' || line.type === 'user') &&
166
277
  Array.isArray(line.message?.content)
@@ -168,12 +279,8 @@ export async function claudeTurn(
168
279
  for (const block of line.message.content)
169
280
  if (block.type === 'tool_use')
170
281
  options.report({
171
- type: 'tool',
282
+ type: 'input',
172
283
  id: block.id as string,
173
- name: (block.name as string).replace(
174
- TOOL_PREFIX,
175
- '',
176
- ),
177
284
  input: block.input,
178
285
  })
179
286
  else if (block.type === 'tool_result')