picocode-core 0.9.238 → 0.9.239
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/README.md +1 -1
- package/package.json +1 -1
- package/src/acp/README.md +41 -0
- package/src/acp/connect.js +108 -0
- package/src/acp/engine.js +531 -0
- package/src/acp/pico-agent.js +295 -0
- package/src/acp/rpc.js +75 -0
- package/src/acp/translate.js +163 -0
- package/src/antigravity/README.md +42 -0
- package/src/antigravity/adapter.js +146 -0
- package/src/antigravity/conversations.js +22 -0
- package/src/antigravity/engine.js +27 -0
- package/src/antigravity/models.js +91 -0
- package/src/antigravity/process.js +60 -0
- package/src/boot.js +3 -2
- package/src/catalog-snapshot.json +1 -1
- package/src/claude-code/README.md +14 -20
- package/src/claude-code/adapter.js +278 -0
- package/src/claude-code/cli.js +6 -2
- package/src/claude-code/engine.js +30 -398
- package/src/claude-code/models.js +7 -4
- package/src/claude-code/token.js +23 -0
- package/src/claude-code/translate.js +65 -180
- package/src/controller.js +2 -0
- package/src/engine-history.js +87 -0
- package/src/engines.js +3 -1
- package/src/local-session.js +49 -0
- package/src/mcp.js +12 -1
- package/src/model-runtime.js +94 -0
- package/src/openai-auth.js +9 -1
- package/src/sign-in/antigravity.js +123 -0
- package/src/sign-in/chatgpt.js +32 -0
- package/src/sign-in/claude-code.js +73 -0
- package/src/sign-in/index.js +32 -0
- package/src/sign-in/screen.js +56 -0
- package/src/sign-in/supergrok.js +21 -0
- package/src/{claude-code/mcp-server.js → tool-bridge-server.js} +3 -3
- package/src/{claude-code/bridge.js → tool-bridge.js} +42 -12
- package/src/xai-auth.js +6 -1
|
@@ -1,43 +1,37 @@
|
|
|
1
|
-
# Claude Code
|
|
1
|
+
# Claude Code
|
|
2
2
|
|
|
3
3
|
Runs a pico session's turns through Claude Code, on the user's own Claude subscription, instead of through a model API.
|
|
4
4
|
|
|
5
5
|
## Why
|
|
6
6
|
|
|
7
|
-
A Claude subscription can't be used as an API key. The supported way to use it is the `claude` command.
|
|
7
|
+
A Claude subscription can't be used as an API key. The supported way to use it is the `claude` command. pico drives that command headless, so a pico session (and any host's agent) can use Claude models, with Claude Code's own tools, at subscription cost. pico still owns the session: its log, transcript, peers, questions and host tools work the same as with any other model.
|
|
8
8
|
|
|
9
9
|
## How
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Claude Code is one of the agents pico reaches over ACP (`../acp/README.md`). The adapter makes it an ACP agent, and the ACP engine plays pico's turns through it.
|
|
12
12
|
|
|
13
13
|
| File | Role |
|
|
14
14
|
| --- | --- |
|
|
15
|
-
| `
|
|
15
|
+
| `adapter.js` | Claude Code as an ACP agent: one `claude` process per session, its stream-json as ACP updates |
|
|
16
16
|
| `cli.js` | Spawns `claude -p` with stream-json in and out; the control requests (initialize, interrupt) |
|
|
17
|
-
| `translate.js` | Pure mapping: Claude
|
|
18
|
-
| `
|
|
17
|
+
| `translate.js` | Pure mapping: Claude's tools, results, usage and content to ACP's |
|
|
18
|
+
| `engine.js` | The ACP engine over the adapter, with what pico tells Claude Code and the tools it does not offer it |
|
|
19
19
|
| `models.js` | The model catalog (`claude-code/fable`, `opus`, `sonnet`, `haiku`) and the sign-in check |
|
|
20
|
+
| `token.js` | The long-lived token Claude Code runs with, where pico keeps one |
|
|
20
21
|
|
|
21
|
-
**
|
|
22
|
+
**Sessions.** An ACP session's id is Claude Code's own session id (`--session-id`), so a session resumes with `--resume` after any restart and forks with `--fork-session`. Changing the model or effort restarts the process on the same session.
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
- if pico's history changed underneath it (rewind, steer, clear, compaction, or a conversation started with another model), a fresh Claude session starts with the earlier conversation as a transcript;
|
|
25
|
-
- after a restart, the saved Claude session is resumed with `--resume`, and a forked pico session forks it with `--fork-session`.
|
|
24
|
+
**Tools.** Claude's tool calls carry the pico tool each amounts to (`Bash` as `bash`, `Edit` as `edit`), and an edit's update carries the whole file before and after, so the transcript shows the diff and rewind can undo it. pico tools Claude Code has its own version of are not offered over the bridge; pico's subagent tool is offered only for other providers' models.
|
|
26
25
|
|
|
27
|
-
**
|
|
26
|
+
**Sign-in.** Claude Code runs on its own claude.ai login, or on a long-lived token from `claude setup-token` (`../sign-in/claude-code.js`), which pico keeps in its home or a host passes as `CLAUDE_CODE_OAUTH_TOKEN`.
|
|
28
27
|
|
|
29
28
|
## Rules
|
|
30
29
|
|
|
31
|
-
- **Subscription only.** `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN` are removed from the child's environment. Either one would make Claude Code bill the API, silently.
|
|
30
|
+
- **Subscription only.** `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN` are removed from the child's environment. Either one would make Claude Code bill the API, silently.
|
|
32
31
|
- **No permission prompts.** Claude Code runs with `--dangerously-skip-permissions`, as pico does. Questions go through pico's `ask_user`, so `AskUserQuestion` is disallowed, and so are `EnterWorktree` and `ExitWorktree` because the host owns worktrees.
|
|
33
|
-
- **Compaction.** Claude Code compacts its own context, so pico doesn't auto-compact these sessions. A manual compact summarizes with the same model
|
|
34
|
-
- **Subagents.** Claude's subagents show in pico's activity list, with their tool calls and reports
|
|
35
|
-
|
|
36
|
-
## Not covered
|
|
37
|
-
|
|
38
|
-
- pico's own subagents and deliberation. Claude Code has its own.
|
|
39
|
-
- The subscription's rate-limit status.
|
|
32
|
+
- **Compaction.** Claude Code compacts its own context, so pico doesn't auto-compact these sessions. A manual compact summarizes with the same model in a bare session.
|
|
33
|
+
- **Subagents.** Claude's subagents show in pico's activity list, with their tool calls and reports. Background tasks are turned off (`CLAUDE_CODE_DISABLE_BACKGROUND_TASKS`): work that outlives a turn would be invisible to pico.
|
|
40
34
|
|
|
41
35
|
## Checking it
|
|
42
36
|
|
|
43
|
-
`test/claude-code.test.js` runs the engine against a scripted Claude Code; no model is called. `node scripts/claude-code-smoke.js [model]` runs real turns through the installed `claude` (haiku by default): an edit, bash, ask_user, an interrupt, parallel subagents, and a resume after restart. Run it after upgrading Claude Code, since the stream-json control protocol is the Agent SDK's and isn't separately documented.
|
|
37
|
+
`test/claude-code.test.js` runs the engine and adapter against a scripted Claude Code; no model is called. `node scripts/claude-code-smoke.js [model]` runs real turns through the installed `claude` (haiku by default): an edit, bash, ask_user, an interrupt, parallel subagents, and a resume after restart. Run it after upgrading Claude Code, since the stream-json control protocol is the Agent SDK's and isn't separately documented.
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// Claude Code as an ACP agent. Claude Code speaks its own stream-json
|
|
2
|
+
// (the protocol the Claude Agent SDK drives it with); this adapter speaks
|
|
3
|
+
// ACP to its client and stream-json to one headless `claude` process per
|
|
4
|
+
// session, on the user's own Claude subscription.
|
|
5
|
+
//
|
|
6
|
+
// - A session's id is Claude Code's own session id, so a session resumes
|
|
7
|
+
// with --resume after any restart, and forks with --fork-session.
|
|
8
|
+
// - The model and effort are session options; changing one restarts the
|
|
9
|
+
// process on the same session.
|
|
10
|
+
// - Claude's tool calls become ACP tool calls, labelled with the pico tool
|
|
11
|
+
// each amounts to; an edit's update carries the whole file before and
|
|
12
|
+
// after, so a client can show the change and undo it.
|
|
13
|
+
// - A subagent's traffic carries the id of the call that started it.
|
|
14
|
+
// - A prompt sent while one runs is folded into the running turn, as
|
|
15
|
+
// Claude Code takes a message sent mid-turn; both answer when it ends.
|
|
16
|
+
// - `_meta.pico.ephemeral` opens a bare session: no tools, nothing saved.
|
|
17
|
+
|
|
18
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
19
|
+
import { randomUUID } from 'node:crypto'
|
|
20
|
+
import { RpcError } from '../acp/rpc.js'
|
|
21
|
+
import { spawnClaude } from './cli.js'
|
|
22
|
+
import { claudeCommand, FAMILIES } from './models.js'
|
|
23
|
+
import { acpKind, addUsage, callTitle, claudeContent, emptyUsage, mcpConfig, picoTool, promptSize, SUBAGENT_TOOLS, toolPath, toolResultText, usageOf } from './translate.js'
|
|
24
|
+
|
|
25
|
+
const CONTEXT = 1000000
|
|
26
|
+
const EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max']
|
|
27
|
+
|
|
28
|
+
function configOptions(session) {
|
|
29
|
+
return [
|
|
30
|
+
{ id: 'model', name: 'Model', category: 'model', type: 'select', currentValue: session.model, options: FAMILIES.map(({ alias, label }) => ({ value: alias, name: label })) },
|
|
31
|
+
{ id: 'effort', name: 'Effort', category: 'thought_level', type: 'select', currentValue: session.effort ?? 'auto', options: [{ value: 'auto', name: 'Auto' }, ...EFFORTS.map((value) => ({ value, name: value }))] },
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const readText = (path) => {
|
|
36
|
+
try { return existsSync(path) ? readFileSync(path, 'utf8') : null } catch { return null }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function createClaudeCodeAgent(client, { exit = () => {} } = {}, { command = claudeCommand(), cwd: defaultCwd = process.cwd(), env = {}, spawn = spawnClaude } = {}) {
|
|
40
|
+
const sessions = new Map()
|
|
41
|
+
|
|
42
|
+
function session(id) {
|
|
43
|
+
const found = sessions.get(id)
|
|
44
|
+
if (!found) throw new RpcError(`no session ${id}`, -32602)
|
|
45
|
+
return found
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function open(params, id, from = {}) {
|
|
49
|
+
const meta = params._meta?.pico ?? {}
|
|
50
|
+
const entry = {
|
|
51
|
+
id,
|
|
52
|
+
cwd: params.cwd ?? defaultCwd,
|
|
53
|
+
servers: params.mcpServers ?? [],
|
|
54
|
+
systemPrompt: meta.systemPrompt ?? '',
|
|
55
|
+
ephemeral: !!meta.ephemeral,
|
|
56
|
+
model: 'opus',
|
|
57
|
+
effort: null,
|
|
58
|
+
// how the process starts: as a new session, resuming this one, or forking another into this one
|
|
59
|
+
resume: from.resume ?? null,
|
|
60
|
+
fork: !!from.fork,
|
|
61
|
+
proc: null,
|
|
62
|
+
turn: null,
|
|
63
|
+
}
|
|
64
|
+
sessions.set(id, entry)
|
|
65
|
+
return entry
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function stop(entry) {
|
|
69
|
+
entry.proc?.close()
|
|
70
|
+
entry.proc = null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const send = (entry, update) => client.notify('session/update', { sessionId: entry.id, update })
|
|
74
|
+
|
|
75
|
+
// the process for a session, started as its options say
|
|
76
|
+
function processFor(entry) {
|
|
77
|
+
if (entry.proc?.alive) return entry.proc
|
|
78
|
+
const proc = spawn({
|
|
79
|
+
command,
|
|
80
|
+
cwd: entry.cwd,
|
|
81
|
+
env,
|
|
82
|
+
model: entry.model,
|
|
83
|
+
effort: entry.effort,
|
|
84
|
+
resume: entry.resume,
|
|
85
|
+
fork: entry.fork,
|
|
86
|
+
sessionId: entry.resume && !entry.fork ? null : entry.id,
|
|
87
|
+
oneShot: entry.ephemeral,
|
|
88
|
+
...(entry.ephemeral ? {} : { mcpConfig: mcpConfig(entry.servers) }),
|
|
89
|
+
...(entry.systemPrompt ? { appendSystemPrompt: entry.systemPrompt } : {}),
|
|
90
|
+
onMessage: (message) => { if (entry.proc === proc) onMessage(entry, message) },
|
|
91
|
+
onExit: (ended) => {
|
|
92
|
+
if (entry.proc !== proc) return
|
|
93
|
+
entry.proc = null
|
|
94
|
+
const turn = entry.turn
|
|
95
|
+
if (!turn) return
|
|
96
|
+
entry.turn = null
|
|
97
|
+
const error = new Error(ended.error?.message || ended.stderr || 'Claude Code stopped')
|
|
98
|
+
if (turn.cancelled) for (const waiter of turn.waiters) waiter.resolve({ stopReason: 'cancelled', usage: turn.usage })
|
|
99
|
+
else {
|
|
100
|
+
for (const waiter of turn.waiters) waiter.reject(error)
|
|
101
|
+
// what Claude Code kept may not match what the client holds, so the connection ends with it
|
|
102
|
+
exit(error)
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
})
|
|
106
|
+
entry.proc = proc
|
|
107
|
+
// from here on the session exists on Claude Code's side, under its own id
|
|
108
|
+
entry.resume = entry.id
|
|
109
|
+
entry.fork = false
|
|
110
|
+
return proc
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ---- one turn's messages, as ACP updates ----
|
|
114
|
+
|
|
115
|
+
function onMessage(entry, message) {
|
|
116
|
+
const turn = entry.turn
|
|
117
|
+
if (!turn) return
|
|
118
|
+
const parent = message.parent_tool_use_id ?? null
|
|
119
|
+
const meta = (extra = {}) => ({ _meta: { pico: { ...(parent ? { parentToolCallId: parent } : {}), ...extra } } })
|
|
120
|
+
if (message.type === 'stream_event' && !parent) {
|
|
121
|
+
const event = message.event
|
|
122
|
+
if (event.type === 'message_start') { turn.streaming = event.message?.id ?? null; countUsage(entry, turn.streaming, event.message?.usage) }
|
|
123
|
+
else if (event.type === 'message_delta') countUsage(entry, turn.streaming, event.usage)
|
|
124
|
+
else if (event.type === 'content_block_delta' && event.delta?.type === 'text_delta' && event.delta.text) send(entry, { sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: event.delta.text } })
|
|
125
|
+
else if (event.type === 'content_block_delta' && event.delta?.type === 'thinking_delta' && event.delta.thinking) send(entry, { sessionUpdate: 'agent_thought_chunk', content: { type: 'text', text: event.delta.thinking } })
|
|
126
|
+
} else if (message.type === 'assistant') {
|
|
127
|
+
for (const block of message.message?.content ?? []) {
|
|
128
|
+
// the main agent's text came as it streamed; a subagent's comes whole
|
|
129
|
+
if (block.type === 'text' && block.text && parent) send(entry, { sessionUpdate: 'agent_message_chunk', content: { type: 'text', text: block.text }, ...meta() })
|
|
130
|
+
if (block.type !== 'tool_use') continue
|
|
131
|
+
const tool = picoTool(block.name, block.input, entry.cwd, entry.servers.map((s) => s.name))
|
|
132
|
+
const path = toolPath(block.input)
|
|
133
|
+
turn.uses.set(block.id, { name: block.name, input: block.input, before: path && acpKind(block.name) === 'edit' ? readText(path) : null })
|
|
134
|
+
send(entry, {
|
|
135
|
+
sessionUpdate: 'tool_call',
|
|
136
|
+
toolCallId: block.id,
|
|
137
|
+
title: callTitle(block.name, block.input, entry.cwd),
|
|
138
|
+
kind: acpKind(block.name),
|
|
139
|
+
status: 'in_progress',
|
|
140
|
+
rawInput: block.input,
|
|
141
|
+
...(path ? { locations: [{ path }] } : {}),
|
|
142
|
+
...meta({
|
|
143
|
+
tool: { name: tool.name, args: tool.args },
|
|
144
|
+
...(tool.bridged ? { bridged: true } : {}),
|
|
145
|
+
...(SUBAGENT_TOOLS.has(block.name) ? { subagent: { description: block.input?.description, prompt: block.input?.prompt ?? '', ...(block.input?.model ? { model: block.input.model } : {}) } } : {}),
|
|
146
|
+
}),
|
|
147
|
+
})
|
|
148
|
+
}
|
|
149
|
+
if (!parent && message.message?.id && !turn.raws.has(message.message.id)) countUsage(entry, message.message.id, message.message.usage)
|
|
150
|
+
} else if (message.type === 'user') {
|
|
151
|
+
const blocks = (Array.isArray(message.message?.content) ? message.message.content : []).filter((block) => block.type === 'tool_result')
|
|
152
|
+
for (const block of blocks) {
|
|
153
|
+
const use = turn.uses.get(block.tool_use_id)
|
|
154
|
+
turn.uses.delete(block.tool_use_id)
|
|
155
|
+
// the structured result describes the message's only call
|
|
156
|
+
const extra = blocks.length === 1 ? message.tool_use_result : null
|
|
157
|
+
const text = toolResultText(Array.isArray(extra?.content) && use && SUBAGENT_TOOLS.has(use.name) ? extra.content : block.content)
|
|
158
|
+
const update = {
|
|
159
|
+
sessionUpdate: 'tool_call_update',
|
|
160
|
+
toolCallId: block.tool_use_id,
|
|
161
|
+
status: block.is_error ? 'failed' : 'completed',
|
|
162
|
+
content: [{ type: 'content', content: { type: 'text', text: block.is_error ? text.replace(/^<tool_use_error>|<\/tool_use_error>$/g, '') : text } }],
|
|
163
|
+
...(extra && typeof extra === 'object' ? { rawOutput: extra } : {}),
|
|
164
|
+
}
|
|
165
|
+
// an edit's whole file, before and after, for the client to show and undo
|
|
166
|
+
const file = !block.is_error && extra && typeof extra.filePath === 'string' ? extra.filePath : null
|
|
167
|
+
if (file && use && acpKind(use.name) === 'edit') {
|
|
168
|
+
const before = typeof extra.originalFile === 'string' ? extra.originalFile : (extra.type === 'create' ? null : use.before)
|
|
169
|
+
const after = readText(file) ?? (typeof extra.content === 'string' ? extra.content : null)
|
|
170
|
+
if (after !== null) {
|
|
171
|
+
update.content.push({ type: 'diff', path: file, oldText: before, newText: after })
|
|
172
|
+
Object.assign(update, meta({ wholeFile: true }))
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (parent && !update._meta) Object.assign(update, meta())
|
|
176
|
+
send(entry, update)
|
|
177
|
+
}
|
|
178
|
+
} else if (message.type === 'result') {
|
|
179
|
+
entry.turn = null
|
|
180
|
+
const reply = { stopReason: turn.cancelled ? 'cancelled' : 'end_turn', usage: turn.usage }
|
|
181
|
+
const failure = message.is_error && !turn.cancelled ? new RpcError(message.result || message.errors?.join('; ') || message.subtype || 'Claude Code failed') : null
|
|
182
|
+
for (const waiter of turn.waiters) failure ? waiter.reject(failure) : waiter.resolve(reply)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// a response's usage arrives in parts: input at its start, output
|
|
187
|
+
// growing until its end
|
|
188
|
+
function countUsage(entry, id, raw) {
|
|
189
|
+
const turn = entry.turn
|
|
190
|
+
if (!turn || !id || !raw) return
|
|
191
|
+
turn.raws.set(id, { ...turn.raws.get(id), ...raw })
|
|
192
|
+
const each = [...turn.raws.values()].map(usageOf)
|
|
193
|
+
turn.usage = each.reduce(addUsage, emptyUsage())
|
|
194
|
+
const last = usageOf(turn.raws.get(id))
|
|
195
|
+
send(entry, { sessionUpdate: 'usage_update', used: promptSize(last), size: CONTEXT, _meta: { pico: { usage: picoUsage(turn.usage) } } })
|
|
196
|
+
}
|
|
197
|
+
const picoUsage = (u) => ({ promptTokens: promptSize(u), completionTokens: u.outputTokens, totalTokens: u.totalTokens, cachedTokens: u.cachedReadTokens, thoughtTokens: 0 })
|
|
198
|
+
|
|
199
|
+
// ---- ACP methods ----
|
|
200
|
+
|
|
201
|
+
const methods = {
|
|
202
|
+
initialize: () => ({
|
|
203
|
+
protocolVersion: 1,
|
|
204
|
+
agentCapabilities: {
|
|
205
|
+
loadSession: false,
|
|
206
|
+
promptCapabilities: { image: true, audio: false, embeddedContext: true },
|
|
207
|
+
sessionCapabilities: { resume: {}, fork: {}, close: {} },
|
|
208
|
+
},
|
|
209
|
+
agentInfo: { name: 'pico-claude-code', title: 'Claude Code', version: '1' },
|
|
210
|
+
authMethods: [],
|
|
211
|
+
}),
|
|
212
|
+
'session/new': (params) => {
|
|
213
|
+
const entry = open(params, randomUUID())
|
|
214
|
+
return { sessionId: entry.id, configOptions: configOptions(entry) }
|
|
215
|
+
},
|
|
216
|
+
'session/resume': (params) => {
|
|
217
|
+
const entry = sessions.get(params.sessionId) ?? open(params, params.sessionId, { resume: params.sessionId })
|
|
218
|
+
return { configOptions: configOptions(entry) }
|
|
219
|
+
},
|
|
220
|
+
'session/fork': (params) => {
|
|
221
|
+
const entry = open(params, randomUUID(), { resume: params.sessionId, fork: true })
|
|
222
|
+
return { sessionId: entry.id, configOptions: configOptions(entry) }
|
|
223
|
+
},
|
|
224
|
+
'session/set_config_option': ({ sessionId, configId, value }) => {
|
|
225
|
+
const entry = session(sessionId)
|
|
226
|
+
const key = configId === 'model' ? 'model' : configId === 'effort' ? 'effort' : null
|
|
227
|
+
if (!key) throw new RpcError(`no option ${configId}`, -32602)
|
|
228
|
+
const next = key === 'effort' && value === 'auto' ? null : String(value)
|
|
229
|
+
if (entry[key] !== next) {
|
|
230
|
+
entry[key] = next
|
|
231
|
+
// a running turn keeps its process; the next one starts on the new option
|
|
232
|
+
if (!entry.turn) stop(entry)
|
|
233
|
+
}
|
|
234
|
+
return { configOptions: configOptions(entry) }
|
|
235
|
+
},
|
|
236
|
+
'session/prompt': ({ sessionId, prompt }) => {
|
|
237
|
+
const entry = session(sessionId)
|
|
238
|
+
const content = claudeContent(prompt)
|
|
239
|
+
return new Promise((resolve, reject) => {
|
|
240
|
+
// a prompt sent while a turn runs joins it, and answers with it
|
|
241
|
+
if (entry.turn) {
|
|
242
|
+
entry.turn.waiters.push({ resolve, reject })
|
|
243
|
+
entry.proc?.send(content)
|
|
244
|
+
return
|
|
245
|
+
}
|
|
246
|
+
entry.turn = { waiters: [{ resolve, reject }], uses: new Map(), raws: new Map(), usage: emptyUsage(), streaming: null, cancelled: false }
|
|
247
|
+
const proc = processFor(entry)
|
|
248
|
+
if (!proc.send(content)) {
|
|
249
|
+
entry.turn = null
|
|
250
|
+
reject(new RpcError(proc.stderr || 'Claude Code is not running'))
|
|
251
|
+
}
|
|
252
|
+
})
|
|
253
|
+
},
|
|
254
|
+
'session/close': ({ sessionId }) => {
|
|
255
|
+
const entry = sessions.get(sessionId)
|
|
256
|
+
if (entry) { stop(entry); sessions.delete(sessionId) }
|
|
257
|
+
return {}
|
|
258
|
+
},
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
async handle(method, params) {
|
|
263
|
+
if (!methods[method]) throw new RpcError(`${method} is not handled here`, -32601)
|
|
264
|
+
return methods[method](params ?? {})
|
|
265
|
+
},
|
|
266
|
+
notification(method, params) {
|
|
267
|
+
if (method !== 'session/cancel') return
|
|
268
|
+
const entry = sessions.get(params.sessionId)
|
|
269
|
+
if (!entry?.turn) return
|
|
270
|
+
entry.turn.cancelled = true
|
|
271
|
+
entry.proc?.interrupt().catch(() => {})
|
|
272
|
+
},
|
|
273
|
+
close() {
|
|
274
|
+
for (const entry of sessions.values()) stop(entry)
|
|
275
|
+
sessions.clear()
|
|
276
|
+
},
|
|
277
|
+
}
|
|
278
|
+
}
|
package/src/claude-code/cli.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import { spawn } from 'node:child_process'
|
|
7
7
|
import { createInterface } from 'node:readline'
|
|
8
8
|
import { randomUUID } from 'node:crypto'
|
|
9
|
+
import { claudeToken } from './token.js'
|
|
9
10
|
|
|
10
11
|
// these make Claude Code bill an API key instead of the claude.ai login,
|
|
11
12
|
// silently; a Claude Code session in pico always runs on the subscription
|
|
@@ -26,7 +27,7 @@ const DISALLOWED = ['AskUserQuestion', 'EnterWorktree', 'ExitWorktree']
|
|
|
26
27
|
|
|
27
28
|
// `oneShot` is a bare run with no tools and nothing saved, for work like
|
|
28
29
|
// summarizing that is not part of the conversation
|
|
29
|
-
export function claudeArgs({ model, effort, resume, fork, mcpConfig, appendSystemPrompt, oneShot = false }) {
|
|
30
|
+
export function claudeArgs({ model, effort, resume, fork, sessionId, mcpConfig, appendSystemPrompt, oneShot = false }) {
|
|
30
31
|
const args = [
|
|
31
32
|
'-p', '--input-format', 'stream-json', '--output-format', 'stream-json', '--verbose', '--include-partial-messages',
|
|
32
33
|
// pico and its hosts run without permission prompts, as the user runs
|
|
@@ -42,6 +43,8 @@ export function claudeArgs({ model, effort, resume, fork, mcpConfig, appendSyste
|
|
|
42
43
|
if (effort) args.push('--effort', effort)
|
|
43
44
|
if (resume) args.push('--resume', resume)
|
|
44
45
|
if (resume && fork) args.push('--fork-session')
|
|
46
|
+
// the id the session is known by from now on: a new one's, or a fork's
|
|
47
|
+
if (sessionId && !oneShot) args.push('--session-id', sessionId)
|
|
45
48
|
if (mcpConfig) args.push('--mcp-config', JSON.stringify(mcpConfig))
|
|
46
49
|
if (appendSystemPrompt) args.push('--append-system-prompt', appendSystemPrompt)
|
|
47
50
|
return args
|
|
@@ -51,7 +54,8 @@ export function spawnClaude({ command = 'claude', cwd, env, onMessage, onExit, .
|
|
|
51
54
|
// background tasks keep running after a turn ends, where pico cannot see
|
|
52
55
|
// them and the idle stop would kill them; in the foreground, subagents
|
|
53
56
|
// still run in parallel, and the turn waits for them
|
|
54
|
-
const
|
|
57
|
+
const token = claudeToken({ ...process.env, ...env })
|
|
58
|
+
const childEnv = { ...claudeEnv({ ...process.env, ...env }), CLAUDE_CODE_DISABLE_BACKGROUND_TASKS: '1', CLAUDE_CODE_DISABLE_AUTO_MEMORY: '1', ...(token ? { CLAUDE_CODE_OAUTH_TOKEN: token } : {}) }
|
|
55
59
|
const child = spawn(command, claudeArgs(options), { cwd, env: childEnv, stdio: ['pipe', 'pipe', 'pipe'] })
|
|
56
60
|
const pending = new Map()
|
|
57
61
|
let stderr = ''
|