@kerf-js/agent 0.0.0-stage → 0.1.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/LICENSE +16 -0
- package/README.md +63 -2
- package/docs/agent.md +187 -0
- package/package.json +62 -3
- package/plugin.d.mts +5 -0
- package/plugin.mjs +134 -0
- package/src/bridge.ts +71 -0
- package/src/bridges/anthropic.ts +168 -0
- package/src/bridges/cli.ts +152 -0
- package/src/bridges/fn.ts +105 -0
- package/src/bridges/rust.ts +188 -0
- package/src/drizzle.ts +228 -0
- package/src/index.ts +154 -0
- package/src/mcp.ts +93 -0
- package/src/server/conversations.ts +131 -0
- package/src/server/core.ts +92 -0
- package/src/server/extension.ts +115 -0
- package/src/server/run.ts +204 -0
- package/src/server/tokens.ts +119 -0
- package/src/server.ts +45 -0
- package/src/setup.mjs +232 -0
- package/src/store.ts +104 -0
- package/src/test.ts +42 -0
- package/src/tools.ts +93 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// The Anthropic API, through the official SDK: real tool use, streamed. Each step's content goes back to the API
|
|
2
|
+
// unchanged next step (a `native` part), so thinking blocks stay valid across a turn's tool rounds.
|
|
3
|
+
import type { Bridge, BridgeTurn } from '../bridge'
|
|
4
|
+
import type { Message, Part } from '../index'
|
|
5
|
+
|
|
6
|
+
/** How the Anthropic bridge calls the API. */
|
|
7
|
+
export interface AnthropicOptions {
|
|
8
|
+
/** Default `claude-sonnet-5-5`. */
|
|
9
|
+
model?: string
|
|
10
|
+
/** Default `medium`: an assistant that runs a site wants answers quickly. */
|
|
11
|
+
effort?: 'low' | 'medium' | 'high' | 'xhigh' | 'max'
|
|
12
|
+
maxTokens?: number
|
|
13
|
+
/** Default `ANTHROPIC_API_KEY`. */
|
|
14
|
+
apiKey?: string
|
|
15
|
+
id?: string
|
|
16
|
+
label?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type Block = { type: string; id?: string; name?: string; input?: unknown; text?: string }
|
|
20
|
+
type ApiMessage = { role: 'user' | 'assistant'; content: unknown[] }
|
|
21
|
+
|
|
22
|
+
const resultText = (output: unknown) =>
|
|
23
|
+
(typeof output === 'string' ? output : JSON.stringify(output ?? null)).slice(0, 20_000)
|
|
24
|
+
|
|
25
|
+
/** The conversation as the API wants it: a step's own content, then the results of its tool calls. */
|
|
26
|
+
export function toApi(messages: Message[]): ApiMessage[] {
|
|
27
|
+
const out: ApiMessage[] = []
|
|
28
|
+
const push = (role: ApiMessage['role'], content: unknown[]) => {
|
|
29
|
+
if (!content.length) return
|
|
30
|
+
const last = out.at(-1)
|
|
31
|
+
if (last?.role === role) last.content.push(...content)
|
|
32
|
+
else out.push({ role, content: [...content] })
|
|
33
|
+
}
|
|
34
|
+
for (const m of messages) {
|
|
35
|
+
if (m.role === 'user') {
|
|
36
|
+
push(
|
|
37
|
+
'user',
|
|
38
|
+
m.parts.flatMap((p) => (p.type === 'text' ? [{ type: 'text', text: p.text }] : [])),
|
|
39
|
+
)
|
|
40
|
+
continue
|
|
41
|
+
}
|
|
42
|
+
const results = new Map(
|
|
43
|
+
m.parts.flatMap((p) => (p.type === 'result' ? [[p.id, p] as const] : [])),
|
|
44
|
+
)
|
|
45
|
+
const answered = new Set<string>()
|
|
46
|
+
for (const p of m.parts) {
|
|
47
|
+
if (p.type !== 'native') continue
|
|
48
|
+
const content = p.content as Block[]
|
|
49
|
+
push('assistant', content)
|
|
50
|
+
const uses = content.filter((b) => b.type === 'tool_use' && b.id)
|
|
51
|
+
push(
|
|
52
|
+
'user',
|
|
53
|
+
uses.map((b) => {
|
|
54
|
+
const r = results.get(b.id!)
|
|
55
|
+
answered.add(b.id!)
|
|
56
|
+
return {
|
|
57
|
+
type: 'tool_result',
|
|
58
|
+
tool_use_id: b.id,
|
|
59
|
+
content: r ? resultText(r.output) : 'No result',
|
|
60
|
+
is_error: !r || r.status !== 'ok',
|
|
61
|
+
}
|
|
62
|
+
}),
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
// what another bridge wrote, and results that came later (an approval): as words
|
|
66
|
+
const loose = m.parts.filter(
|
|
67
|
+
(p): p is Exclude<Part, { type: 'native' }> =>
|
|
68
|
+
p.type !== 'native' &&
|
|
69
|
+
!(p.type === 'result' && answered.has(p.id)) &&
|
|
70
|
+
!(p.type === 'call' && answered.has(p.id)),
|
|
71
|
+
)
|
|
72
|
+
if (m.parts.some((p) => p.type === 'native')) {
|
|
73
|
+
const late = loose.filter((p) => p.type === 'result')
|
|
74
|
+
push(
|
|
75
|
+
'user',
|
|
76
|
+
late.map((p) => ({
|
|
77
|
+
type: 'text',
|
|
78
|
+
text: `[${p.tool} → ${p.status}: ${resultText(p.output)}]`,
|
|
79
|
+
})),
|
|
80
|
+
)
|
|
81
|
+
continue
|
|
82
|
+
}
|
|
83
|
+
const said = loose.filter((p) => p.type !== 'result')
|
|
84
|
+
push(
|
|
85
|
+
'assistant',
|
|
86
|
+
said.map((p) => ({
|
|
87
|
+
type: 'text',
|
|
88
|
+
text: p.type === 'text' ? p.text : `[called ${p.tool} ${JSON.stringify(p.input)}]`,
|
|
89
|
+
})),
|
|
90
|
+
)
|
|
91
|
+
push(
|
|
92
|
+
'user',
|
|
93
|
+
loose
|
|
94
|
+
.filter((p) => p.type === 'result')
|
|
95
|
+
.map((p) => ({ type: 'text', text: `[${p.tool} → ${p.status}: ${resultText(p.output)}]` })),
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
// the API wants the user to speak first
|
|
99
|
+
if (out[0]?.role === 'assistant')
|
|
100
|
+
out.unshift({ role: 'user', content: [{ type: 'text', text: '(continuing)' }] })
|
|
101
|
+
return out
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The Anthropic API as a bridge: available when there is an API key. */
|
|
105
|
+
export function anthropic(options: AnthropicOptions = {}): Bridge {
|
|
106
|
+
const key = () => options.apiKey ?? process.env.ANTHROPIC_API_KEY?.trim()
|
|
107
|
+
return {
|
|
108
|
+
id: options.id ?? 'claude',
|
|
109
|
+
label: options.label ?? 'Claude',
|
|
110
|
+
tools: 'native',
|
|
111
|
+
available: () => !!key(),
|
|
112
|
+
async step(turn: BridgeTurn) {
|
|
113
|
+
const { default: Anthropic } = await import('@anthropic-ai/sdk')
|
|
114
|
+
const client = new Anthropic({ apiKey: key(), maxRetries: 1 })
|
|
115
|
+
const stream = client.messages.stream(
|
|
116
|
+
{
|
|
117
|
+
model: options.model ?? 'claude-sonnet-5-5',
|
|
118
|
+
max_tokens: options.maxTokens ?? 16_000,
|
|
119
|
+
output_config: { effort: options.effort ?? 'medium' },
|
|
120
|
+
system: turn.system,
|
|
121
|
+
messages: toApi(turn.messages) as never,
|
|
122
|
+
...(turn.tools.length && {
|
|
123
|
+
tools: turn.tools.map((t) => ({
|
|
124
|
+
name: t.name,
|
|
125
|
+
description: t.confirm
|
|
126
|
+
? `${t.description} (the owner approves each call)`
|
|
127
|
+
: t.description,
|
|
128
|
+
input_schema: t.input as never,
|
|
129
|
+
})),
|
|
130
|
+
}),
|
|
131
|
+
} as never,
|
|
132
|
+
{ signal: turn.signal },
|
|
133
|
+
)
|
|
134
|
+
for await (const event of stream as AsyncIterable<{
|
|
135
|
+
type: string
|
|
136
|
+
delta?: { type: string; text?: string }
|
|
137
|
+
}>) {
|
|
138
|
+
if (event.type === 'content_block_delta' && event.delta?.type === 'text_delta')
|
|
139
|
+
turn.text(event.delta.text ?? '')
|
|
140
|
+
}
|
|
141
|
+
const message = (await stream.finalMessage()) as unknown as {
|
|
142
|
+
model: string
|
|
143
|
+
stop_reason: string
|
|
144
|
+
content: Block[]
|
|
145
|
+
usage: { input_tokens: number; output_tokens: number }
|
|
146
|
+
}
|
|
147
|
+
if (message.stop_reason === 'refusal') throw new Error('Claude declined this one')
|
|
148
|
+
// a tool call cut off at the limit can still parse: never run one
|
|
149
|
+
if (
|
|
150
|
+
message.stop_reason === 'max_tokens' &&
|
|
151
|
+
message.content.some((b) => b.type === 'tool_use')
|
|
152
|
+
)
|
|
153
|
+
throw new Error('The answer ran past its length limit mid-call; nothing was run')
|
|
154
|
+
return {
|
|
155
|
+
text: message.content.flatMap((b) => (b.type === 'text' ? [b.text ?? ''] : [])).join(''),
|
|
156
|
+
calls: message.content.flatMap((b) =>
|
|
157
|
+
b.type === 'tool_use' ? [{ id: b.id!, tool: b.name!, input: b.input ?? {} }] : [],
|
|
158
|
+
),
|
|
159
|
+
usage: {
|
|
160
|
+
model: message.model,
|
|
161
|
+
input: message.usage.input_tokens,
|
|
162
|
+
output: message.usage.output_tokens,
|
|
163
|
+
},
|
|
164
|
+
raw: message.content,
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// The local `claude` CLI, for a machine signed in to Claude with no API key. It runs the tools itself, through the
|
|
2
|
+
// app's MCP endpoint with a token good for this turn only, so its calls pass the same grants and guard as any other.
|
|
3
|
+
import { spawn } from 'node:child_process'
|
|
4
|
+
import { existsSync } from 'node:fs'
|
|
5
|
+
import { tmpdir } from 'node:os'
|
|
6
|
+
import { delimiter, join } from 'node:path'
|
|
7
|
+
import { transcript, type Bridge, type BridgeStep, type BridgeTurn } from '../bridge'
|
|
8
|
+
|
|
9
|
+
/** How the CLI bridge runs `claude`. */
|
|
10
|
+
export interface CliOptions {
|
|
11
|
+
/** The executable. Default `claude` on PATH. */
|
|
12
|
+
command?: string
|
|
13
|
+
/** Passed as `--model`. Default: the CLI's own. */
|
|
14
|
+
model?: string
|
|
15
|
+
/** Default 110s. */
|
|
16
|
+
timeout?: number
|
|
17
|
+
id?: string
|
|
18
|
+
label?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const onPath = (cmd: string) =>
|
|
22
|
+
cmd.includes('/')
|
|
23
|
+
? existsSync(cmd)
|
|
24
|
+
: (process.env.PATH ?? '').split(delimiter).some((d) => d && existsSync(join(d, cmd)))
|
|
25
|
+
|
|
26
|
+
type Usage = { model: string; input: number; output: number }
|
|
27
|
+
type Event = {
|
|
28
|
+
type?: string
|
|
29
|
+
subtype?: string
|
|
30
|
+
model?: string
|
|
31
|
+
is_error?: boolean
|
|
32
|
+
result?: unknown
|
|
33
|
+
usage?: { input_tokens?: number; cache_read_input_tokens?: number; output_tokens?: number }
|
|
34
|
+
event?: { type?: string; delta?: { type?: string; text?: string } }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The CLI's flags for one turn: print mode, stream-json, no built-in tools, and the app's MCP server if there is one. */
|
|
38
|
+
function argsFor(turn: BridgeTurn, options: CliOptions) {
|
|
39
|
+
const args = [
|
|
40
|
+
'-p',
|
|
41
|
+
'--output-format',
|
|
42
|
+
'stream-json',
|
|
43
|
+
'--verbose',
|
|
44
|
+
'--include-partial-messages',
|
|
45
|
+
'--no-session-persistence',
|
|
46
|
+
'--tools',
|
|
47
|
+
'',
|
|
48
|
+
'--system-prompt',
|
|
49
|
+
turn.system,
|
|
50
|
+
]
|
|
51
|
+
if (options.model) args.push('--model', options.model)
|
|
52
|
+
if (!turn.mcp) return [...args, '--strict-mcp-config']
|
|
53
|
+
const headers = { Authorization: `Bearer ${turn.mcp.token}` }
|
|
54
|
+
const config = { mcpServers: { app: { type: 'http', url: turn.mcp.url, headers } } }
|
|
55
|
+
return [
|
|
56
|
+
...args,
|
|
57
|
+
'--mcp-config',
|
|
58
|
+
JSON.stringify(config),
|
|
59
|
+
'--strict-mcp-config',
|
|
60
|
+
'--allowedTools',
|
|
61
|
+
'mcp__app',
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** What one stream-json line adds: words, the model, the final usage, or an error. */
|
|
66
|
+
function read(
|
|
67
|
+
ev: Event,
|
|
68
|
+
s: { text: string; errors: string; usage: Usage },
|
|
69
|
+
said: (d: string) => void,
|
|
70
|
+
) {
|
|
71
|
+
if (
|
|
72
|
+
ev.type === 'stream_event' &&
|
|
73
|
+
ev.event?.type === 'content_block_delta' &&
|
|
74
|
+
ev.event.delta?.type === 'text_delta'
|
|
75
|
+
) {
|
|
76
|
+
const d = ev.event.delta.text ?? ''
|
|
77
|
+
s.text += d
|
|
78
|
+
said(d)
|
|
79
|
+
} else if (ev.type === 'system' && ev.subtype === 'init' && ev.model) s.usage.model = ev.model
|
|
80
|
+
else if (ev.type === 'result') {
|
|
81
|
+
if (ev.is_error)
|
|
82
|
+
s.errors += typeof ev.result === 'string' ? ev.result : 'claude reported an error'
|
|
83
|
+
else if (!s.text && typeof ev.result === 'string') s.text = ev.result
|
|
84
|
+
s.usage.input = (ev.usage?.input_tokens ?? 0) + (ev.usage?.cache_read_input_tokens ?? 0)
|
|
85
|
+
s.usage.output = ev.usage?.output_tokens ?? 0
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Each whole JSON line of `chunk`, after what was left over last time; returns what's left over now. */
|
|
90
|
+
function lines(buffered: string, each: (ev: Event) => void) {
|
|
91
|
+
let nl: number
|
|
92
|
+
while ((nl = buffered.indexOf('\n')) >= 0) {
|
|
93
|
+
const line = buffered.slice(0, nl).trim()
|
|
94
|
+
buffered = buffered.slice(nl + 1)
|
|
95
|
+
if (!line.startsWith('{')) continue
|
|
96
|
+
try {
|
|
97
|
+
each(JSON.parse(line) as Event)
|
|
98
|
+
} catch {}
|
|
99
|
+
}
|
|
100
|
+
return buffered
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** One turn of the CLI: spawned in an empty directory, so it has nothing of the project to wander into. */
|
|
104
|
+
function run(command: string, turn: BridgeTurn, options: CliOptions): Promise<BridgeStep> {
|
|
105
|
+
return new Promise((resolve, reject) => {
|
|
106
|
+
const child = spawn(command, argsFor(turn, options), {
|
|
107
|
+
cwd: tmpdir(),
|
|
108
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
109
|
+
signal: turn.signal,
|
|
110
|
+
})
|
|
111
|
+
const timer = setTimeout(() => child.kill('SIGTERM'), options.timeout ?? 110_000)
|
|
112
|
+
const s = {
|
|
113
|
+
text: '',
|
|
114
|
+
errors: '',
|
|
115
|
+
usage: { model: options.model ?? 'claude-cli', input: 0, output: 0 },
|
|
116
|
+
}
|
|
117
|
+
let buffered = ''
|
|
118
|
+
child.stdout.setEncoding('utf8')
|
|
119
|
+
child.stdout.on(
|
|
120
|
+
'data',
|
|
121
|
+
(chunk: string) =>
|
|
122
|
+
(buffered = lines(buffered + chunk, (ev) => read(ev, s, (d) => turn.text(d)))),
|
|
123
|
+
)
|
|
124
|
+
child.stderr.setEncoding('utf8')
|
|
125
|
+
child.stderr.on('data', (c: string) => (s.errors = (s.errors + c).slice(-600)))
|
|
126
|
+
child.on('error', (e) => {
|
|
127
|
+
clearTimeout(timer)
|
|
128
|
+
reject(e)
|
|
129
|
+
})
|
|
130
|
+
child.on('close', (code) => {
|
|
131
|
+
clearTimeout(timer)
|
|
132
|
+
if (code === 0 && s.text.trim()) resolve({ text: s.text, calls: [], usage: s.usage })
|
|
133
|
+
else
|
|
134
|
+
reject(
|
|
135
|
+
new Error(`claude exited with ${code}: ${s.errors.trim().slice(0, 300) || 'no answer'}`),
|
|
136
|
+
)
|
|
137
|
+
})
|
|
138
|
+
child.stdin.end(transcript(turn.messages))
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The local `claude` CLI as a bridge: available when it is on PATH. */
|
|
143
|
+
export function claudeCli(options: CliOptions = {}): Bridge {
|
|
144
|
+
const command = options.command ?? 'claude'
|
|
145
|
+
return {
|
|
146
|
+
id: options.id ?? 'cli',
|
|
147
|
+
label: options.label ?? 'Claude (this computer)',
|
|
148
|
+
tools: 'mcp',
|
|
149
|
+
available: () => onPath(command),
|
|
150
|
+
step: (turn: BridgeTurn) => run(command, turn, options),
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Your own planner as the bridge: any server function that takes `bridgeInput` and returns `bridgeOutput` answers
|
|
2
|
+
// turns. A rules engine, a local model, a Rust sidecar (`impl: remote('rust://planner')` gets serde types for both),
|
|
3
|
+
// anything that can say what to call.
|
|
4
|
+
//
|
|
5
|
+
// export const planner = mutation({ input: bridgeInput, output: bridgeOutput, run: … })
|
|
6
|
+
// defineAgent({ bridges: [fnBridge({ fn: planner, label: 'House rules' })], … })
|
|
7
|
+
import { serverCall } from '@kerf-js/start'
|
|
8
|
+
import { z } from 'zod'
|
|
9
|
+
import { transcript, type Bridge, type BridgeTurn } from '../bridge'
|
|
10
|
+
|
|
11
|
+
/** What the planner gets: the conversation as text and as structure, and the tools it may name. */
|
|
12
|
+
export const bridgeInput = z.object({
|
|
13
|
+
system: z.string(),
|
|
14
|
+
/** The conversation as plain text, oldest first. */
|
|
15
|
+
transcript: z.string(),
|
|
16
|
+
/** The last thing the owner said. */
|
|
17
|
+
said: z.string(),
|
|
18
|
+
tools: z.array(
|
|
19
|
+
z.object({
|
|
20
|
+
name: z.string(),
|
|
21
|
+
description: z.string(),
|
|
22
|
+
kind: z.enum(['query', 'mutation']),
|
|
23
|
+
/** The input's JSON Schema, as a string: parse it if you need it. */
|
|
24
|
+
input: z.string(),
|
|
25
|
+
}),
|
|
26
|
+
),
|
|
27
|
+
/** Results of the calls it asked for this turn, oldest first: it goes on from them. */
|
|
28
|
+
results: z.array(z.object({ tool: z.string(), status: z.string(), output: z.string() })),
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
/** What it answers: words, and calls to run before it's asked again. No calls ends the turn. */
|
|
32
|
+
export const bridgeOutput = z.object({
|
|
33
|
+
text: z.string(),
|
|
34
|
+
calls: z.array(z.object({ tool: z.string(), input: z.string() })).default([]),
|
|
35
|
+
model: z.string().default('fn'),
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
/** Which function answers, and how the picker names it. */
|
|
39
|
+
export interface FnBridgeOptions {
|
|
40
|
+
/** The server function that answers, or its `$id`. */
|
|
41
|
+
fn: { $id: string } | string
|
|
42
|
+
id?: string
|
|
43
|
+
label?: string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let n = 0
|
|
47
|
+
|
|
48
|
+
/** A server function as a bridge: it gets `bridgeInput` and answers `bridgeOutput`. */
|
|
49
|
+
export function fnBridge(options: FnBridgeOptions): Bridge {
|
|
50
|
+
const id = typeof options.fn === 'string' ? options.fn : options.fn.$id
|
|
51
|
+
return {
|
|
52
|
+
id: options.id ?? 'fn',
|
|
53
|
+
label: options.label ?? 'House planner',
|
|
54
|
+
tools: 'native',
|
|
55
|
+
async step(turn: BridgeTurn) {
|
|
56
|
+
const last = turn.messages.at(-1)
|
|
57
|
+
const said =
|
|
58
|
+
[...turn.messages]
|
|
59
|
+
.reverse()
|
|
60
|
+
.find((m) => m.role === 'user')
|
|
61
|
+
?.parts.flatMap((p) => (p.type === 'text' ? [p.text] : []))
|
|
62
|
+
.join('\n') ?? ''
|
|
63
|
+
const results =
|
|
64
|
+
last?.role === 'assistant'
|
|
65
|
+
? last.parts.flatMap((p) =>
|
|
66
|
+
p.type === 'result'
|
|
67
|
+
? [
|
|
68
|
+
{
|
|
69
|
+
tool: p.tool,
|
|
70
|
+
status: p.status,
|
|
71
|
+
output: JSON.stringify(p.output ?? null).slice(0, 20_000),
|
|
72
|
+
},
|
|
73
|
+
]
|
|
74
|
+
: [],
|
|
75
|
+
)
|
|
76
|
+
: []
|
|
77
|
+
const out = bridgeOutput.parse(
|
|
78
|
+
await serverCall(id, {
|
|
79
|
+
system: turn.system,
|
|
80
|
+
transcript: transcript(turn.messages),
|
|
81
|
+
said,
|
|
82
|
+
tools: turn.tools.map((t) => ({
|
|
83
|
+
name: t.name,
|
|
84
|
+
description: t.description,
|
|
85
|
+
kind: t.kind,
|
|
86
|
+
input: JSON.stringify(t.input),
|
|
87
|
+
})),
|
|
88
|
+
results,
|
|
89
|
+
} satisfies z.input<typeof bridgeInput>),
|
|
90
|
+
)
|
|
91
|
+
if (out.text) turn.text(out.text)
|
|
92
|
+
return {
|
|
93
|
+
text: out.text,
|
|
94
|
+
calls: out.calls.map((c) => {
|
|
95
|
+
let input: unknown = {}
|
|
96
|
+
try {
|
|
97
|
+
input = JSON.parse(c.input || '{}')
|
|
98
|
+
} catch {}
|
|
99
|
+
return { id: `fn-${Date.now().toString(36)}-${++n}`, tool: c.tool, input }
|
|
100
|
+
}),
|
|
101
|
+
usage: { model: out.model, input: 0, output: 0 },
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// The Rust bridge: the Technomancy AI Bridge daemon on your own machine answers, through the claude and codex CLIs
|
|
2
|
+
// signed in there. The app holds a job queue at `/_kerf/x/agent/bridges/<id>/jobs`, and the daemon long-polls it from
|
|
3
|
+
// outside, so the machine never listens publicly and the app can be anywhere. The daemon meters and caps spend per
|
|
4
|
+
// site and per person; the agent loop here runs the tools, with the same grants as every other bridge.
|
|
5
|
+
//
|
|
6
|
+
// # bridge.toml on the machine with the CLIs
|
|
7
|
+
// [[server]]
|
|
8
|
+
// name = "shop"
|
|
9
|
+
// url = "https://shop.example"
|
|
10
|
+
// token = "<technomancy-bridge token>" # also AGENT_BRIDGE_TOKEN on the app
|
|
11
|
+
// queues = ["/_kerf/x/agent/bridges/rust/jobs"]
|
|
12
|
+
import { randomUUID, timingSafeEqual } from 'node:crypto'
|
|
13
|
+
import { transcript, type Bridge, type BridgeStep, type BridgeTurn } from '../bridge'
|
|
14
|
+
import type { ToolInfo } from '../index'
|
|
15
|
+
|
|
16
|
+
/** How the app reaches the daemon. */
|
|
17
|
+
export interface RustBridgeOptions {
|
|
18
|
+
/** The shared bearer token. Default `AGENT_BRIDGE_TOKEN`; at least 32 characters. */
|
|
19
|
+
token?: string
|
|
20
|
+
/** The model id the daemon routes on: `claude` (default) is Claude first, `terra`/`sol`/`luna` Codex first. */
|
|
21
|
+
model?: string
|
|
22
|
+
/** How long a step waits for the daemon. Default 3 minutes. */
|
|
23
|
+
timeoutMs?: number
|
|
24
|
+
id?: string
|
|
25
|
+
label?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A job as the daemon takes it. */
|
|
29
|
+
interface Job {
|
|
30
|
+
id: string
|
|
31
|
+
modelId: string
|
|
32
|
+
prompt: string
|
|
33
|
+
principal: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** What the daemon posts back. */
|
|
37
|
+
interface Done {
|
|
38
|
+
text?: string
|
|
39
|
+
error?: string
|
|
40
|
+
tokens?: { input?: number; output?: number; backend?: string }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface Queue {
|
|
44
|
+
waiting: Map<string, { job: Job; done(r: Done): void }>
|
|
45
|
+
ready: string[]
|
|
46
|
+
pollers: ((job: Job | null) => void)[]
|
|
47
|
+
seen: number
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// One queue per bridge id for the process: it survives a dev reload, so the poller and the turn meet
|
|
51
|
+
const g = globalThis as { __kerfRustBridge?: Map<string, Queue> }
|
|
52
|
+
const queues = (g.__kerfRustBridge ??= new Map())
|
|
53
|
+
const queueOf = (id: string) => {
|
|
54
|
+
let q = queues.get(id)
|
|
55
|
+
if (!q) queues.set(id, (q = { waiting: new Map(), ready: [], pollers: [], seen: 0 }))
|
|
56
|
+
return q
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const same = (a: string, b: string) => {
|
|
60
|
+
const x = Buffer.from(a)
|
|
61
|
+
const y = Buffer.from(b)
|
|
62
|
+
return x.length === y.length && timingSafeEqual(x, y)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const PROTOCOL = `Reply with exactly one JSON object and nothing else:
|
|
66
|
+
{"say": "what you tell the owner", "calls": [{"tool": "name", "input": {}}]}
|
|
67
|
+
Call only the tools listed above, with input matching its schema. After they run you get their results and answer again.
|
|
68
|
+
Leave "calls" empty when you're done. Use no tools of your own: these are the only way to see or change the site.`
|
|
69
|
+
|
|
70
|
+
const toolLine = (t: ToolInfo) =>
|
|
71
|
+
`- ${t.name}${t.confirm ? ' (the owner approves each call)' : ''}: ${t.description}\n input: ${JSON.stringify(t.input)}`
|
|
72
|
+
|
|
73
|
+
/** Everything the CLI on the other end needs in one prompt: it keeps no session. */
|
|
74
|
+
export function promptFor(turn: BridgeTurn): string {
|
|
75
|
+
const tools = turn.tools.length ? turn.tools.map(toolLine).join('\n') : '(none)'
|
|
76
|
+
return `${turn.system}\n\nTools:\n${tools}\n\n${PROTOCOL}\n\nThe conversation so far:\n\n${transcript(turn.messages)}`
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let n = 0
|
|
80
|
+
|
|
81
|
+
/** The answer's JSON, wherever the CLI put it; plain words when there is none. */
|
|
82
|
+
export function readAnswer(text: string): Pick<BridgeStep, 'text' | 'calls'> {
|
|
83
|
+
const start = text.indexOf('{')
|
|
84
|
+
const end = text.lastIndexOf('}')
|
|
85
|
+
try {
|
|
86
|
+
const out = JSON.parse(text.slice(start, end + 1)) as { say?: unknown; calls?: unknown }
|
|
87
|
+
const calls = Array.isArray(out.calls) ? out.calls : []
|
|
88
|
+
return {
|
|
89
|
+
text: typeof out.say === 'string' ? out.say : '',
|
|
90
|
+
calls: calls.flatMap((c: { tool?: unknown; input?: unknown }) =>
|
|
91
|
+
typeof c?.tool === 'string'
|
|
92
|
+
? [{ id: `rust-${Date.now().toString(36)}-${++n}`, tool: c.tool, input: c.input ?? {} }]
|
|
93
|
+
: [],
|
|
94
|
+
),
|
|
95
|
+
}
|
|
96
|
+
} catch {
|
|
97
|
+
return { text: text.trim(), calls: [] }
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function enqueue(q: Queue, job: Job, signal: AbortSignal, ms: number) {
|
|
102
|
+
return new Promise<Done>((resolve, reject) => {
|
|
103
|
+
const finish = () => {
|
|
104
|
+
clearTimeout(timer)
|
|
105
|
+
q.waiting.delete(job.id)
|
|
106
|
+
q.ready = q.ready.filter((x) => x !== job.id)
|
|
107
|
+
}
|
|
108
|
+
const timer = setTimeout(() => {
|
|
109
|
+
finish()
|
|
110
|
+
reject(new Error(`The bridge didn't answer in ${Math.round(ms / 1000)}s`))
|
|
111
|
+
}, ms)
|
|
112
|
+
signal.addEventListener('abort', () => (finish(), reject(signal.reason)), { once: true })
|
|
113
|
+
q.waiting.set(job.id, { job, done: (r) => (finish(), resolve(r)) })
|
|
114
|
+
const poller = q.pollers.shift()
|
|
115
|
+
if (poller) poller(job)
|
|
116
|
+
else q.ready.push(job.id)
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function take(q: Queue, waitMs: number) {
|
|
121
|
+
q.seen = Date.now()
|
|
122
|
+
const id = q.ready.shift()
|
|
123
|
+
const job = id && q.waiting.get(id)?.job
|
|
124
|
+
if (job) return Promise.resolve(job)
|
|
125
|
+
return new Promise<Job | null>((resolve) => {
|
|
126
|
+
const poller = (j: Job | null) => (clearTimeout(timer), resolve(j))
|
|
127
|
+
const timer = setTimeout(() => {
|
|
128
|
+
q.pollers = q.pollers.filter((p) => p !== poller)
|
|
129
|
+
resolve(null)
|
|
130
|
+
}, waitMs)
|
|
131
|
+
q.pollers.push(poller)
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The daemon's side: `GET jobs?wait=25` takes a job (204 when quiet), `POST jobs/<id>/result` answers it. */
|
|
136
|
+
async function serve(q: Queue, token: string | undefined, request: Request, path: string) {
|
|
137
|
+
const presented = request.headers.get('authorization')?.replace(/^Bearer\s+/i, '') ?? ''
|
|
138
|
+
if (!token || token.length < 32 || !same(presented, token))
|
|
139
|
+
return Response.json({ error: 'Invalid bridge token' }, { status: 401 })
|
|
140
|
+
if (path === 'jobs' && request.method === 'GET') {
|
|
141
|
+
const wait = Number(new URL(request.url).searchParams.get('wait') ?? 25)
|
|
142
|
+
const job = await take(q, Math.min(30, Math.max(0, wait || 0)) * 1000)
|
|
143
|
+
return job ? Response.json({ job }) : new Response(null, { status: 204 })
|
|
144
|
+
}
|
|
145
|
+
const done = /^jobs\/([\w-]+)\/result$/.exec(path)
|
|
146
|
+
if (done && request.method === 'POST') {
|
|
147
|
+
q.seen = Date.now()
|
|
148
|
+
const body = (await request.json().catch(() => null)) as Done | null
|
|
149
|
+
const entry = q.waiting.get(done[1]!)
|
|
150
|
+
entry?.done(body ?? { error: 'The bridge sent an empty answer' })
|
|
151
|
+
return Response.json({ ok: true, accepted: !!entry })
|
|
152
|
+
}
|
|
153
|
+
return new Response('Not found', { status: 404 })
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** The Technomancy AI Bridge daemon as a bridge: available while it is polling (seen in the last 90 seconds). */
|
|
157
|
+
export function rustBridge(options: RustBridgeOptions = {}): Bridge {
|
|
158
|
+
const id = options.id ?? 'rust'
|
|
159
|
+
const q = queueOf(id)
|
|
160
|
+
const token = () => options.token ?? process.env.AGENT_BRIDGE_TOKEN?.trim()
|
|
161
|
+
return {
|
|
162
|
+
id,
|
|
163
|
+
label: options.label ?? 'Claude through the bridge',
|
|
164
|
+
tools: 'native',
|
|
165
|
+
available: () => !!token() && Date.now() - q.seen < 90_000,
|
|
166
|
+
serve: (request, path) => serve(q, token(), request, path),
|
|
167
|
+
async step(turn) {
|
|
168
|
+
const job = {
|
|
169
|
+
id: randomUUID(),
|
|
170
|
+
modelId: options.model ?? 'claude',
|
|
171
|
+
prompt: promptFor(turn),
|
|
172
|
+
principal: turn.who,
|
|
173
|
+
}
|
|
174
|
+
const done = await enqueue(q, job, turn.signal, options.timeoutMs ?? 180_000)
|
|
175
|
+
if (done.error !== undefined) throw new Error(`The bridge: ${done.error}`)
|
|
176
|
+
const answer = readAnswer(done.text ?? '')
|
|
177
|
+
if (answer.text) turn.text(answer.text)
|
|
178
|
+
return {
|
|
179
|
+
...answer,
|
|
180
|
+
usage: {
|
|
181
|
+
model: done.tokens?.backend ?? job.modelId,
|
|
182
|
+
input: done.tokens?.input ?? 0,
|
|
183
|
+
output: done.tokens?.output ?? 0,
|
|
184
|
+
},
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
}
|
|
188
|
+
}
|