@phren/agent 0.3.19 → 0.3.21
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/dist/agent-loop/index.js +185 -47
- package/dist/agent-loop/stream.js +74 -66
- package/dist/agent-loop/types.js +2 -3
- package/dist/attach-images.js +57 -0
- package/dist/bin.js +7 -2
- package/dist/commands/info.js +7 -3
- package/dist/commands/model.js +55 -13
- package/dist/commands/session.js +79 -18
- package/dist/commands.js +24 -3
- package/dist/config.js +89 -10
- package/dist/context/clear-tool-results.js +43 -0
- package/dist/context/compactor.js +13 -3
- package/dist/context/pruner.js +29 -11
- package/dist/context/usage.js +26 -0
- package/dist/cost.js +68 -10
- package/dist/headless.js +47 -1
- package/dist/index.js +335 -127
- package/dist/mcp-client.js +38 -1
- package/dist/mcp-prompts.js +94 -0
- package/dist/memory/chat.js +70 -0
- package/dist/memory/context.js +1 -1
- package/dist/model-overrides.js +17 -0
- package/dist/models-list.js +73 -0
- package/dist/models.js +42 -15
- package/dist/multi/child-entry.js +18 -28
- package/dist/multi/ipc-hooks.js +34 -0
- package/dist/multi/spawner.js +49 -3
- package/dist/permissions/allowlist.js +19 -4
- package/dist/permissions/checker.js +37 -20
- package/dist/permissions/kernel-sandbox.js +32 -10
- package/dist/permissions/rules.js +173 -0
- package/dist/permissions/shell-classify.js +173 -0
- package/dist/plan.js +6 -2
- package/dist/providers/anthropic.js +119 -75
- package/dist/providers/codex.js +10 -9
- package/dist/providers/ollama.js +5 -2
- package/dist/providers/openai-compat.js +219 -92
- package/dist/providers/openrouter.js +12 -7
- package/dist/providers/resolve.js +47 -1
- package/dist/providers/retry.js +20 -7
- package/dist/providers/types.js +31 -0
- package/dist/repl.js +17 -1
- package/dist/session/persist.js +9 -4
- package/dist/session/preview.js +99 -0
- package/dist/settings.js +14 -0
- package/dist/structured-output.js +85 -0
- package/dist/system-prompt.js +117 -14
- package/dist/tools/apply-patch.js +15 -1
- package/dist/tools/edit-file.js +8 -1
- package/dist/tools/file-state.js +88 -0
- package/dist/tools/glob.js +53 -35
- package/dist/tools/grep.js +139 -53
- package/dist/tools/lint-test.js +24 -0
- package/dist/tools/read-file.js +2 -0
- package/dist/tools/registry.js +8 -1
- package/dist/tools/search-support.js +141 -0
- package/dist/tools/shell.js +266 -41
- package/dist/tools/spawn-agent.js +15 -3
- package/dist/tools/syntax-check.js +116 -0
- package/dist/tools/write-file.js +11 -3
- package/dist/tui/components/App.js +5 -4
- package/dist/tui/components/Banner.js +1 -1
- package/dist/tui/components/ListPicker.js +32 -0
- package/dist/tui/ink-entry.js +79 -15
- package/dist/user-hooks.js +26 -4
- package/package.json +3 -2
- package/dist/memory/context-flush.js +0 -24
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a shell command line does, coarsely: enough to tell a read-only or
|
|
3
|
+
* build-and-test command, which auto-confirm may run unprompted, from
|
|
4
|
+
* anything else, which it must ask about. The blocklist in shell-safety.ts
|
|
5
|
+
* only names the catastrophic cases; everything it misses (`rm -rf src`,
|
|
6
|
+
* `git push`, `npm publish`) used to count as safe.
|
|
7
|
+
*
|
|
8
|
+
* This is a conservative reader, not a shell parser: anything it doesn't
|
|
9
|
+
* understand (redirection into a file, substitution, a leading variable
|
|
10
|
+
* assignment, background `&`) is not auto-approvable.
|
|
11
|
+
*/
|
|
12
|
+
/** Commands that only read. */
|
|
13
|
+
const READ_ONLY = new Set([
|
|
14
|
+
"ls", "cat", "head", "tail", "wc", "grep", "egrep", "fgrep", "rg", "pwd", "echo", "printf", "which", "type",
|
|
15
|
+
"file", "stat", "du", "df", "tree", "diff", "cmp", "sort", "uniq", "cut", "tr", "basename", "dirname", "realpath",
|
|
16
|
+
"readlink", "date", "true", "false", "test", "[", "jq", "nl", "column", "sha256sum", "shasum", "md5sum", "whoami",
|
|
17
|
+
"uname", "hostname", "cd", "find", "sed",
|
|
18
|
+
]);
|
|
19
|
+
/** Build, test and type-check tools, and the subcommands of multi-command tools that read, build or test. */
|
|
20
|
+
const RUNNERS = new Set(["tsc", "vitest", "jest", "mocha", "pytest", "mypy", "pyright"]);
|
|
21
|
+
const SUBCOMMANDS = {
|
|
22
|
+
git: new Set(["status", "diff", "log", "show", "rev-parse", "ls-files", "blame", "grep", "describe", "shortlog"]),
|
|
23
|
+
npm: new Set(["test", "t", "run", "ls", "view", "outdated"]),
|
|
24
|
+
pnpm: new Set(["test", "t", "run", "lint", "build", "typecheck", "ls", "list", "why", "outdated"]),
|
|
25
|
+
yarn: new Set(["test", "run", "lint", "build", "typecheck", "list", "why"]),
|
|
26
|
+
bun: new Set(["test", "run"]),
|
|
27
|
+
npx: new Set(["tsc", "vitest", "jest", "mocha", "eslint", "biome"]),
|
|
28
|
+
cargo: new Set(["build", "check", "test", "clippy", "doc", "tree", "metadata"]),
|
|
29
|
+
go: new Set(["build", "test", "vet", "list", "version", "doc"]),
|
|
30
|
+
ruff: new Set(["check"]),
|
|
31
|
+
eslint: new Set(),
|
|
32
|
+
biome: new Set(["check", "lint", "ci"]),
|
|
33
|
+
};
|
|
34
|
+
/** Arguments that turn a read-only command into one that writes or runs things. */
|
|
35
|
+
const WRITING_ARGS = {
|
|
36
|
+
find: /^-(exec|execdir|ok|okdir|delete|fprint|fprint0|fprintf|fls)$/,
|
|
37
|
+
// -i edits in place; a script's w command writes a file and e runs one.
|
|
38
|
+
sed: /^(-i|--in-place)|(^|[;{}\n]|\/[gpIiMm0-9]*)\s*[we](\s|$)/,
|
|
39
|
+
eslint: /^--fix/,
|
|
40
|
+
git: /^(-o|--output)/,
|
|
41
|
+
};
|
|
42
|
+
/** Package scripts that `npm run` and friends may run unprompted. */
|
|
43
|
+
const SAFE_SCRIPT = /^(test|build|lint|check|typecheck|type-check|tsc|compile)(:[\w:-]+)?$/;
|
|
44
|
+
const SCRIPT_RUNNERS = new Set(["npm", "pnpm", "yarn", "bun"]);
|
|
45
|
+
/** Multi-command tools whose allowlist entries are scoped to the subcommand. */
|
|
46
|
+
export const MULTI_COMMAND_TOOLS = new Set([
|
|
47
|
+
"git", "npm", "pnpm", "yarn", "bun", "npx", "cargo", "go", "docker", "kubectl", "gh", "pip", "pip3", "uv", "poetry",
|
|
48
|
+
"brew", "dotnet", "mvn", "gradle", "terraform", "helm", "systemctl", "apt", "apt-get", "ruff", "biome",
|
|
49
|
+
]);
|
|
50
|
+
/** A command line split into its simple commands, or null when it has constructs this reader won't vouch for. */
|
|
51
|
+
export function splitCommandLine(command) {
|
|
52
|
+
const segments = [];
|
|
53
|
+
let tokens = [];
|
|
54
|
+
let current = "";
|
|
55
|
+
let inToken = false;
|
|
56
|
+
let quote = null;
|
|
57
|
+
const endToken = () => {
|
|
58
|
+
if (inToken)
|
|
59
|
+
tokens.push(current);
|
|
60
|
+
current = "";
|
|
61
|
+
inToken = false;
|
|
62
|
+
};
|
|
63
|
+
const endSegment = () => {
|
|
64
|
+
endToken();
|
|
65
|
+
if (tokens.length > 0)
|
|
66
|
+
segments.push(tokens);
|
|
67
|
+
tokens = [];
|
|
68
|
+
};
|
|
69
|
+
for (let i = 0; i < command.length; i++) {
|
|
70
|
+
const c = command[i];
|
|
71
|
+
if (quote) {
|
|
72
|
+
if (c === quote)
|
|
73
|
+
quote = null;
|
|
74
|
+
else if (quote === '"' && (c === "`" || (c === "$" && command[i + 1] === "(")))
|
|
75
|
+
return null;
|
|
76
|
+
else
|
|
77
|
+
current += c;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (c === "'" || c === '"') {
|
|
81
|
+
quote = c;
|
|
82
|
+
inToken = true;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (c === "\\") {
|
|
86
|
+
current += command[i + 1] ?? "";
|
|
87
|
+
inToken = true;
|
|
88
|
+
i++;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (c === "`" || (c === "$" && command[i + 1] === "(") || c === "<" && command[i + 1] === "(")
|
|
92
|
+
return null;
|
|
93
|
+
if (c === "&" && command[i + 1] === "&") {
|
|
94
|
+
endSegment();
|
|
95
|
+
i++;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (c === "|") {
|
|
99
|
+
endSegment();
|
|
100
|
+
if (command[i + 1] === "|")
|
|
101
|
+
i++;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (c === ";" || c === "\n") {
|
|
105
|
+
endSegment();
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (c === "&" || c === ">" || c === "<") {
|
|
109
|
+
// Only the harmless redirections: stderr into stdout, output thrown away.
|
|
110
|
+
const rest = command.slice(i);
|
|
111
|
+
const harmless = /^(?:>&[12]|>\s*\/dev\/null|&>\s*\/dev\/null)/.exec(rest);
|
|
112
|
+
const fd = current === "1" || current === "2" ? current : null;
|
|
113
|
+
if (!harmless || (inToken && !fd))
|
|
114
|
+
return null;
|
|
115
|
+
current = "";
|
|
116
|
+
inToken = false;
|
|
117
|
+
i += harmless[0].length - 1;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (c === " " || c === "\t") {
|
|
121
|
+
endToken();
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
current += c;
|
|
125
|
+
inToken = true;
|
|
126
|
+
}
|
|
127
|
+
if (quote)
|
|
128
|
+
return null;
|
|
129
|
+
endSegment();
|
|
130
|
+
return segments;
|
|
131
|
+
}
|
|
132
|
+
function segmentIsSafe(tokens) {
|
|
133
|
+
const [binary, ...args] = tokens;
|
|
134
|
+
// A leading assignment (PATH=… cmd) can change what runs.
|
|
135
|
+
if (!binary || /^[A-Za-z_][A-Za-z0-9_]*=/.test(binary))
|
|
136
|
+
return false;
|
|
137
|
+
const name = binary.includes("/") ? "" : binary;
|
|
138
|
+
const writing = WRITING_ARGS[name];
|
|
139
|
+
if (writing && args.some((a) => writing.test(a)))
|
|
140
|
+
return false;
|
|
141
|
+
if (READ_ONLY.has(name) || RUNNERS.has(name))
|
|
142
|
+
return true;
|
|
143
|
+
const subs = SUBCOMMANDS[name];
|
|
144
|
+
if (!subs)
|
|
145
|
+
return false;
|
|
146
|
+
if (subs.size === 0)
|
|
147
|
+
return true;
|
|
148
|
+
const positional = args.filter((a) => !a.startsWith("-"));
|
|
149
|
+
const sub = positional[0];
|
|
150
|
+
if (sub === undefined || !subs.has(sub))
|
|
151
|
+
return false;
|
|
152
|
+
// `npm run deploy` runs whatever the package says; only the usual check scripts pass.
|
|
153
|
+
if (sub === "run" && SCRIPT_RUNNERS.has(name))
|
|
154
|
+
return positional[1] !== undefined && SAFE_SCRIPT.test(positional[1]);
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
/** True when every part of the command line only reads, builds or tests. */
|
|
158
|
+
export function isAutoApprovableCommand(command) {
|
|
159
|
+
const segments = splitCommandLine(command);
|
|
160
|
+
return segments !== null && segments.length > 0 && segments.every(segmentIsSafe);
|
|
161
|
+
}
|
|
162
|
+
/** The allowlist pattern for one simple command: the binary, plus the subcommand for multi-command tools. */
|
|
163
|
+
export function commandPattern(tokens) {
|
|
164
|
+
const [binary, ...args] = tokens;
|
|
165
|
+
if (!binary)
|
|
166
|
+
return "*";
|
|
167
|
+
if (MULTI_COMMAND_TOOLS.has(binary)) {
|
|
168
|
+
const sub = args.find((a) => !a.startsWith("-"));
|
|
169
|
+
if (sub)
|
|
170
|
+
return `${binary} ${sub}`;
|
|
171
|
+
}
|
|
172
|
+
return binary;
|
|
173
|
+
}
|
package/dist/plan.js
CHANGED
|
@@ -4,13 +4,17 @@ const PLAN_SUFFIX = `
|
|
|
4
4
|
|
|
5
5
|
## Plan mode
|
|
6
6
|
|
|
7
|
-
Before
|
|
7
|
+
Before changing anything, propose a plan for the user to approve. You can
|
|
8
|
+
use the read-only tools (read_file, glob, grep, git_status, git_diff and the
|
|
9
|
+
like) to explore the code first; edits, writes and shell commands are refused
|
|
10
|
+
until the plan is approved. Then describe:
|
|
8
11
|
1. What you understand about the task
|
|
9
12
|
2. What files you'll need to read or modify
|
|
10
13
|
3. What approach you'll take
|
|
11
14
|
4. Any risks or uncertainties
|
|
12
15
|
|
|
13
|
-
|
|
16
|
+
End your reply with the plan, without a tool call; the user approves it or
|
|
17
|
+
asks for changes.`;
|
|
14
18
|
/** Append plan instruction to the system prompt. */
|
|
15
19
|
export function injectPlanPrompt(systemPrompt) {
|
|
16
20
|
return systemPrompt + PLAN_SUFFIX;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { IncompleteStreamError, RetryableProviderError, withPartialUsage } from "./types.js";
|
|
1
2
|
import { stripForeignReasoning } from "./history.js";
|
|
2
3
|
import { getModelMetadata } from "../models.js";
|
|
3
4
|
const PROVIDER_NAME = "anthropic";
|
|
4
5
|
/** Thinking budget per effort level; always clamped below max_tokens. */
|
|
5
6
|
const THINKING_BUDGETS = {
|
|
7
|
+
none: 0,
|
|
6
8
|
low: 2048,
|
|
7
9
|
medium: 8192,
|
|
8
10
|
high: 16384,
|
|
@@ -31,7 +33,8 @@ export class AnthropicProvider {
|
|
|
31
33
|
this.model = model ?? "claude-sonnet-5";
|
|
32
34
|
this.maxOutputTokens = maxOutputTokens ?? 8192;
|
|
33
35
|
this.cacheEnabled = cacheEnabled;
|
|
34
|
-
|
|
36
|
+
// "none" is thinking off, which on Anthropic means sending no thinking config.
|
|
37
|
+
this.reasoningEffort = reasoningEffort === "none" ? undefined : reasoningEffort;
|
|
35
38
|
const metadata = getModelMetadata(PROVIDER_NAME, this.model);
|
|
36
39
|
if (metadata)
|
|
37
40
|
this.contextWindow = metadata.contextWindow;
|
|
@@ -78,7 +81,7 @@ export class AnthropicProvider {
|
|
|
78
81
|
return {
|
|
79
82
|
content,
|
|
80
83
|
stop_reason: stop_reason,
|
|
81
|
-
usage: usage ?
|
|
84
|
+
usage: usage ? anthropicUsage(usage) : undefined,
|
|
82
85
|
};
|
|
83
86
|
}
|
|
84
87
|
async *chatStream(system, messages, tools, signal) {
|
|
@@ -105,88 +108,97 @@ export class AnthropicProvider {
|
|
|
105
108
|
// Thinking blocks are index-tracked too: the signature arrives as a
|
|
106
109
|
// delta and must ride the reasoning_end at content_block_stop.
|
|
107
110
|
const thinkingByIndex = new Map();
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
if (
|
|
114
|
-
|
|
115
|
-
const id = block.id;
|
|
116
|
-
indexToToolId.set(index, id);
|
|
117
|
-
yield { type: "tool_use_start", id, name: block.name };
|
|
118
|
-
}
|
|
119
|
-
else if (block.type === "thinking") {
|
|
120
|
-
thinkingByIndex.set(data.index, {});
|
|
111
|
+
let stopped = false;
|
|
112
|
+
try {
|
|
113
|
+
for await (const event of parseSSE(res)) {
|
|
114
|
+
const type = event.event;
|
|
115
|
+
const data = event.data;
|
|
116
|
+
if (type === "error") {
|
|
117
|
+
throw streamErrorEvent(data);
|
|
121
118
|
}
|
|
122
|
-
else if (
|
|
123
|
-
|
|
124
|
-
yield {
|
|
125
|
-
type: "reasoning_end",
|
|
126
|
-
redacted: true,
|
|
127
|
-
...(typeof block.data === "string" ? { data: block.data } : {}),
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
else if (type === "content_block_delta") {
|
|
132
|
-
const delta = data.delta;
|
|
133
|
-
if (delta.type === "text_delta") {
|
|
134
|
-
yield { type: "text_delta", text: delta.text };
|
|
119
|
+
else if (type === "message_stop") {
|
|
120
|
+
stopped = true;
|
|
135
121
|
}
|
|
136
|
-
else if (
|
|
137
|
-
|
|
122
|
+
else if (type === "content_block_start") {
|
|
123
|
+
const block = data.content_block;
|
|
124
|
+
if (block.type === "tool_use") {
|
|
125
|
+
const index = data.index;
|
|
126
|
+
const id = block.id;
|
|
127
|
+
indexToToolId.set(index, id);
|
|
128
|
+
yield { type: "tool_use_start", id, name: block.name };
|
|
129
|
+
}
|
|
130
|
+
else if (block.type === "thinking") {
|
|
131
|
+
thinkingByIndex.set(data.index, {});
|
|
132
|
+
}
|
|
133
|
+
else if (block.type === "redacted_thinking") {
|
|
134
|
+
// Arrives complete: opaque payload, no deltas, no signature.
|
|
135
|
+
yield {
|
|
136
|
+
type: "reasoning_end",
|
|
137
|
+
redacted: true,
|
|
138
|
+
...(typeof block.data === "string" ? { data: block.data } : {}),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
138
141
|
}
|
|
139
|
-
else if (
|
|
140
|
-
const
|
|
141
|
-
if (
|
|
142
|
-
|
|
142
|
+
else if (type === "content_block_delta") {
|
|
143
|
+
const delta = data.delta;
|
|
144
|
+
if (delta.type === "text_delta") {
|
|
145
|
+
yield { type: "text_delta", text: delta.text };
|
|
146
|
+
}
|
|
147
|
+
else if (delta.type === "thinking_delta") {
|
|
148
|
+
yield { type: "reasoning_delta", text: delta.thinking };
|
|
149
|
+
}
|
|
150
|
+
else if (delta.type === "signature_delta") {
|
|
151
|
+
const state = thinkingByIndex.get(data.index);
|
|
152
|
+
if (state)
|
|
153
|
+
state.signature = (state.signature ?? "") + delta.signature;
|
|
154
|
+
}
|
|
155
|
+
else if (delta.type === "input_json_delta") {
|
|
156
|
+
const index = data.index;
|
|
157
|
+
const id = indexToToolId.get(index) ?? String(index);
|
|
158
|
+
yield { type: "tool_use_delta", id, json: delta.partial_json };
|
|
159
|
+
}
|
|
143
160
|
}
|
|
144
|
-
else if (
|
|
161
|
+
else if (type === "content_block_stop") {
|
|
145
162
|
const index = data.index;
|
|
146
|
-
|
|
147
|
-
|
|
163
|
+
if (thinkingByIndex.has(index)) {
|
|
164
|
+
const state = thinkingByIndex.get(index);
|
|
165
|
+
thinkingByIndex.delete(index);
|
|
166
|
+
yield {
|
|
167
|
+
type: "reasoning_end",
|
|
168
|
+
...(state.signature !== undefined ? { signature: state.signature } : {}),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
if (indexToToolId.has(index)) {
|
|
172
|
+
yield { type: "tool_use_end", id: indexToToolId.get(index) };
|
|
173
|
+
}
|
|
148
174
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
if (indexToToolId.has(index)) {
|
|
161
|
-
yield { type: "tool_use_end", id: indexToToolId.get(index) };
|
|
175
|
+
else if (type === "message_delta") {
|
|
176
|
+
const delta = data.delta;
|
|
177
|
+
if (delta.stop_reason === "tool_use")
|
|
178
|
+
stopReason = "tool_use";
|
|
179
|
+
else if (delta.stop_reason === "max_tokens")
|
|
180
|
+
stopReason = "max_tokens";
|
|
181
|
+
// message_delta carries output_tokens (and, on newer API versions,
|
|
182
|
+
// cumulative input and cache counts) — merge with message_start's.
|
|
183
|
+
const u = data.usage;
|
|
184
|
+
if (u)
|
|
185
|
+
usage = mergeUsage(usage, u);
|
|
162
186
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
// message_delta carries output_tokens — merge with existing input_tokens from message_start
|
|
171
|
-
const u = data.usage;
|
|
172
|
-
if (u) {
|
|
173
|
-
usage = {
|
|
174
|
-
input_tokens: usage?.input_tokens ?? 0,
|
|
175
|
-
output_tokens: u.output_tokens ?? 0,
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
else if (type === "message_start") {
|
|
180
|
-
// message_start carries input_tokens — initialize usage
|
|
181
|
-
const u = data.message?.usage;
|
|
182
|
-
if (u) {
|
|
183
|
-
logCacheUsage(u);
|
|
184
|
-
usage = {
|
|
185
|
-
input_tokens: u.input_tokens ?? 0,
|
|
186
|
-
output_tokens: usage?.output_tokens ?? 0,
|
|
187
|
-
};
|
|
187
|
+
else if (type === "message_start") {
|
|
188
|
+
// message_start carries input and cache tokens — initialize usage
|
|
189
|
+
const u = data.message?.usage;
|
|
190
|
+
if (u) {
|
|
191
|
+
logCacheUsage(u);
|
|
192
|
+
usage = mergeUsage(usage, u);
|
|
193
|
+
}
|
|
188
194
|
}
|
|
189
195
|
}
|
|
196
|
+
if (!stopped)
|
|
197
|
+
throw new IncompleteStreamError("Anthropic stream ended before message_stop");
|
|
198
|
+
}
|
|
199
|
+
catch (err) {
|
|
200
|
+
// The input was billed even though this attempt failed.
|
|
201
|
+
throw withPartialUsage(err, usage);
|
|
190
202
|
}
|
|
191
203
|
yield { type: "done", stop_reason: stopReason, usage };
|
|
192
204
|
}
|
|
@@ -315,6 +327,38 @@ export function parseWireContent(raw) {
|
|
|
315
327
|
}
|
|
316
328
|
return content;
|
|
317
329
|
}
|
|
330
|
+
/**
|
|
331
|
+
* A mid-stream `error` event (it arrives on an HTTP 200 response).
|
|
332
|
+
* overloaded_error and api_error are transient on Anthropic's side, so a
|
|
333
|
+
* fresh request is worth making; anything else is not.
|
|
334
|
+
*/
|
|
335
|
+
function streamErrorEvent(data) {
|
|
336
|
+
const error = data.error;
|
|
337
|
+
const type = typeof error?.type === "string" ? error.type : "error";
|
|
338
|
+
const message = `Anthropic API error: ${type}: ${error?.message ?? JSON.stringify(data)}`;
|
|
339
|
+
return type === "overloaded_error" || type === "api_error" ? new RetryableProviderError(message) : new Error(message);
|
|
340
|
+
}
|
|
341
|
+
/** Anthropic usage → TokenUsage; input_tokens already excludes cache reads and writes. */
|
|
342
|
+
function anthropicUsage(u) {
|
|
343
|
+
return {
|
|
344
|
+
input_tokens: u.input_tokens ?? 0,
|
|
345
|
+
output_tokens: u.output_tokens ?? 0,
|
|
346
|
+
...(u.cache_read_input_tokens ? { cache_read_input_tokens: u.cache_read_input_tokens } : {}),
|
|
347
|
+
...(u.cache_creation_input_tokens ? { cache_creation_input_tokens: u.cache_creation_input_tokens } : {}),
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
/** Fold a stream event's usage into what earlier events reported; absent fields keep their value. */
|
|
351
|
+
function mergeUsage(prev, u) {
|
|
352
|
+
const next = anthropicUsage(u);
|
|
353
|
+
return {
|
|
354
|
+
input_tokens: u.input_tokens !== undefined ? next.input_tokens : prev?.input_tokens ?? 0,
|
|
355
|
+
output_tokens: u.output_tokens !== undefined ? next.output_tokens : prev?.output_tokens ?? 0,
|
|
356
|
+
...(next.cache_read_input_tokens ?? prev?.cache_read_input_tokens
|
|
357
|
+
? { cache_read_input_tokens: next.cache_read_input_tokens ?? prev?.cache_read_input_tokens } : {}),
|
|
358
|
+
...(next.cache_creation_input_tokens ?? prev?.cache_creation_input_tokens
|
|
359
|
+
? { cache_creation_input_tokens: next.cache_creation_input_tokens ?? prev?.cache_creation_input_tokens } : {}),
|
|
360
|
+
};
|
|
361
|
+
}
|
|
318
362
|
/** Log cache hit/creation stats to stderr (visible in verbose mode). */
|
|
319
363
|
function logCacheUsage(usage) {
|
|
320
364
|
if (!usage)
|
package/dist/providers/codex.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { toolResultText } from "./types.js";
|
|
1
|
+
import { IncompleteStreamError, toolResultText } from "./types.js";
|
|
2
2
|
import { getAccessToken } from "./codex-auth.js";
|
|
3
3
|
import { stripForeignReasoning, IMAGE_OMITTED_MARKER } from "./history.js";
|
|
4
|
+
import { wireReasoningEffort } from "./openai-compat.js";
|
|
4
5
|
import { lookupContextWindow, lookupMaxOutputTokens, modelSupportsVision } from "../models.js";
|
|
5
6
|
const CODEX_API = "https://chatgpt.com/backend-api/codex/responses";
|
|
6
7
|
const PROVIDER_NAME = "openai-codex";
|
|
@@ -230,9 +231,9 @@ export class CodexProvider {
|
|
|
230
231
|
// cannot round-trip (chatStream already requested it; chat() didn't).
|
|
231
232
|
include: ["reasoning.encrypted_content"],
|
|
232
233
|
};
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
234
|
+
const effort = wireReasoningEffort(this.name, this.model, this.reasoningEffort);
|
|
235
|
+
if (effort)
|
|
236
|
+
body.reasoning = { effort };
|
|
236
237
|
if (tools.length > 0) {
|
|
237
238
|
body.tools = toResponsesTools(tools);
|
|
238
239
|
body.tool_choice = "auto";
|
|
@@ -249,9 +250,9 @@ export class CodexProvider {
|
|
|
249
250
|
stream: true,
|
|
250
251
|
include: ["reasoning.encrypted_content"],
|
|
251
252
|
};
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
253
|
+
const effort = wireReasoningEffort(this.name, this.model, this.reasoningEffort);
|
|
254
|
+
if (effort)
|
|
255
|
+
body.reasoning = { effort };
|
|
255
256
|
if (tools.length > 0) {
|
|
256
257
|
body.tools = toResponsesTools(tools);
|
|
257
258
|
body.tool_choice = "auto";
|
|
@@ -347,7 +348,7 @@ export class CodexProvider {
|
|
|
347
348
|
debugLog("no finalResponse found, seenEventTypes", [...seenEventTypes]);
|
|
348
349
|
}
|
|
349
350
|
if (!finalResponse) {
|
|
350
|
-
throw new
|
|
351
|
+
throw new IncompleteStreamError("Codex stream ended without response.completed event");
|
|
351
352
|
}
|
|
352
353
|
return finalResponse;
|
|
353
354
|
}
|
|
@@ -462,7 +463,7 @@ export class CodexProvider {
|
|
|
462
463
|
});
|
|
463
464
|
ws.addEventListener("close", () => {
|
|
464
465
|
if (!done) {
|
|
465
|
-
push(new
|
|
466
|
+
push(new IncompleteStreamError("Codex WebSocket closed before response.completed"));
|
|
466
467
|
done = true;
|
|
467
468
|
}
|
|
468
469
|
});
|
package/dist/providers/ollama.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { toolResultText } from "./types.js";
|
|
1
|
+
import { IncompleteStreamError, toolResultText } from "./types.js";
|
|
2
2
|
import { stripForeignReasoning, IMAGE_OMITTED_MARKER } from "./history.js";
|
|
3
3
|
import { lookupContextWindow } from "../models.js";
|
|
4
4
|
const PROVIDER_NAME = "ollama";
|
|
@@ -133,6 +133,7 @@ export class OllamaProvider {
|
|
|
133
133
|
let buf = "";
|
|
134
134
|
let stopReason = "end_turn";
|
|
135
135
|
let toolCallIndex = 0;
|
|
136
|
+
let finished = false;
|
|
136
137
|
for (;;) {
|
|
137
138
|
const { done, value } = await reader.read();
|
|
138
139
|
if (done)
|
|
@@ -170,9 +171,11 @@ export class OllamaProvider {
|
|
|
170
171
|
}
|
|
171
172
|
}
|
|
172
173
|
if (chunk.done === true)
|
|
173
|
-
|
|
174
|
+
finished = true;
|
|
174
175
|
}
|
|
175
176
|
}
|
|
177
|
+
if (!finished)
|
|
178
|
+
throw new IncompleteStreamError("Ollama stream ended before done");
|
|
176
179
|
yield { type: "done", stop_reason: stopReason };
|
|
177
180
|
}
|
|
178
181
|
}
|