@phren/agent 0.3.19 → 0.3.20
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 +75 -31
- package/dist/agent-loop/stream.js +72 -66
- package/dist/agent-loop/types.js +2 -3
- package/dist/bin.js +7 -2
- package/dist/commands/info.js +1 -1
- package/dist/commands.js +15 -1
- package/dist/config.js +44 -10
- package/dist/context/compactor.js +6 -2
- package/dist/context/pruner.js +29 -11
- package/dist/cost.js +61 -10
- package/dist/headless.js +6 -1
- package/dist/index.js +167 -83
- 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 +16 -28
- package/dist/multi/ipc-hooks.js +34 -0
- package/dist/multi/spawner.js +37 -3
- 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 +1 -0
- package/dist/session/preview.js +99 -0
- package/dist/tools/shell.js +263 -40
- package/dist/tui/ink-entry.js +25 -2
- package/package.json +2 -2
- package/dist/memory/context-flush.js +0 -24
|
@@ -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
|
}
|