@phren/agent 0.3.18 → 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.
@@ -1,3 +1,4 @@
1
+ import { IncompleteStreamError, RetryableProviderError, withPartialUsage } from "./types.js";
1
2
  import { stripForeignReasoning, IMAGE_OMITTED_MARKER } from "./history.js";
2
3
  /** Convert Anthropic tool defs to OpenAI function format. */
3
4
  export function toOpenAiTools(tools) {
@@ -6,23 +7,71 @@ export function toOpenAiTools(tools) {
6
7
  function: { name: t.name, description: t.description, parameters: t.input_schema },
7
8
  }));
8
9
  }
10
+ /** DeepSeek directly, or any OpenAI-compatible endpoint (OpenCode Go, a proxy) serving a DeepSeek model. */
11
+ export function isDeepSeekRoute(providerName, model) {
12
+ if (providerName === "deepseek")
13
+ return true;
14
+ return providerName === "openai-compat" && /deepseek/i.test(model ?? "");
15
+ }
16
+ /**
17
+ * Routes that require every earlier assistant turn's `reasoning_content` back
18
+ * when tools are present. DeepSeek returns HTTP 400 when a plain-answer turn
19
+ * arrives without it.
20
+ */
21
+ export function replaysAllReasoning(providerName, model) {
22
+ return isDeepSeekRoute(providerName, model);
23
+ }
24
+ /**
25
+ * DeepSeek's reasoning_effort takes none, low, high and max; it has no medium
26
+ * and treats xhigh as high, so map Phren's levels onto what it documents.
27
+ */
28
+ const DEEPSEEK_EFFORT = {
29
+ none: "none",
30
+ low: "low",
31
+ medium: "high",
32
+ high: "high",
33
+ xhigh: "max",
34
+ };
35
+ /**
36
+ * OpenAI models that take reasoning effort "none": GPT-5.1 and later, except
37
+ * the -codex variants. Older reasoning models (gpt-5, o-series) and most
38
+ * other OpenAI-compatible servers reject it with a 400.
39
+ */
40
+ export function acceptsNoReasoning(model) {
41
+ const id = (model ?? "").replace(/^.*\//, "");
42
+ return /^gpt-5\.(?:[1-9]|\d{2,})/.test(id) && !/codex/i.test(id);
43
+ }
44
+ /**
45
+ * The reasoning_effort value to send on this route. "none" goes only where
46
+ * it is accepted (DeepSeek, newer OpenAI models); elsewhere the field is
47
+ * left out and the model's default applies.
48
+ */
49
+ export function wireReasoningEffort(providerName, model, effort) {
50
+ if (!effort)
51
+ return undefined;
52
+ if (isDeepSeekRoute(providerName, model))
53
+ return DEEPSEEK_EFFORT[effort];
54
+ if (effort === "none" && !acceptsNoReasoning(model))
55
+ return undefined;
56
+ return effort;
57
+ }
9
58
  /**
10
59
  * Convert Anthropic messages to OpenAI messages.
11
60
  *
12
61
  * `providerName` scopes which reasoning blocks belong to this provider; when
13
62
  * omitted, all reasoning is stripped (conservative). Own reasoning is passed
14
- * back as `reasoning_content` only on tool-call turns — the field is ignored
15
- * on plain turns by providers that support it (DeepSeek's documented rule),
16
- * so sending it there just wastes tokens. Assistant `content` is always a
17
- * string, never null/absent: some gateways 400 on a null-content assistant
18
- * message, and history is durable, so one would poison every later turn.
63
+ * back as `reasoning_content` on tool-call turns; with `replayAllReasoning`
64
+ * (DeepSeek routes, see replaysAllReasoning) it goes on every assistant turn,
65
+ * empty when the turn had none. Assistant `content` is always a string, never
66
+ * null/absent: some gateways 400 on a null-content assistant message, and
67
+ * history is durable, so one would poison every later turn.
19
68
  */
20
- export function toOpenAiMessages(system, messages, providerName, vision = false) {
69
+ export function toOpenAiMessages(system, messages, providerName, vision = false, replayAllReasoning = false) {
21
70
  const out = [{ role: "system", content: system }];
22
71
  for (const msg of stripForeignReasoning(messages, providerName)) {
23
72
  if (msg.role === "assistant") {
24
73
  if (typeof msg.content === "string") {
25
- out.push({ role: "assistant", content: msg.content });
74
+ out.push({ role: "assistant", content: msg.content, ...(replayAllReasoning ? { reasoning_content: "" } : {}) });
26
75
  }
27
76
  else {
28
77
  const textParts = msg.content.filter((b) => b.type === "text").map((b) => b.type === "text" ? b.text : "");
@@ -36,11 +85,12 @@ export function toOpenAiMessages(system, messages, providerName, vision = false)
36
85
  return { id: b.id, type: "function", function: { name: b.name, arguments: JSON.stringify(b.input) } };
37
86
  });
38
87
  const entry = { role: "assistant", content: textParts.join("\n") };
39
- if (toolCalls.length > 0) {
88
+ if (toolCalls.length > 0)
40
89
  entry.tool_calls = toolCalls;
41
- if (reasoningParts.length > 0)
42
- entry.reasoning_content = reasoningParts.join("\n");
43
- }
90
+ if (replayAllReasoning)
91
+ entry.reasoning_content = reasoningParts.join("\n");
92
+ else if (toolCalls.length > 0 && reasoningParts.length > 0)
93
+ entry.reasoning_content = reasoningParts.join("\n");
44
94
  out.push(entry);
45
95
  }
46
96
  }
@@ -102,6 +152,52 @@ export function toOpenAiMessages(system, messages, providerName, vision = false)
102
152
  }
103
153
  return out;
104
154
  }
155
+ /**
156
+ * Chat Completions usage → TokenUsage. prompt_tokens includes cache hits:
157
+ * DeepSeek reports them as prompt_cache_hit_tokens, OpenAI (and most relays)
158
+ * as prompt_tokens_details.cached_tokens.
159
+ */
160
+ export function parseOpenAiUsage(u) {
161
+ const num = (v) => (typeof v === "number" && Number.isFinite(v) ? v : 0);
162
+ const prompt = num(u.prompt_tokens);
163
+ const details = u.prompt_tokens_details;
164
+ const cached = Math.min(prompt, num(u.prompt_cache_hit_tokens) || num(details?.cached_tokens));
165
+ return {
166
+ input_tokens: prompt - cached,
167
+ output_tokens: num(u.completion_tokens),
168
+ ...(cached > 0 ? { cache_read_input_tokens: cached } : {}),
169
+ };
170
+ }
171
+ /**
172
+ * Parse a tool call's JSON arguments. Empty arguments are a no-argument call;
173
+ * anything else that isn't a JSON object is an error for the model to fix.
174
+ */
175
+ export function parseToolArguments(raw) {
176
+ if (raw.trim() === "")
177
+ return { input: {} };
178
+ try {
179
+ const parsed = JSON.parse(raw);
180
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed))
181
+ return { input: parsed };
182
+ return { error: "arguments must be a JSON object" };
183
+ }
184
+ catch (err) {
185
+ return { error: err instanceof Error ? err.message : String(err) };
186
+ }
187
+ }
188
+ /**
189
+ * DeepSeek ends a response with finish_reason insufficient_system_resource
190
+ * when it runs out of capacity mid-generation: the output is incomplete, and
191
+ * a fresh request usually succeeds.
192
+ */
193
+ function throwOnAbnormalFinish(finishReason) {
194
+ if (finishReason === "insufficient_system_resource") {
195
+ throw new RetryableProviderError("Provider ran out of capacity mid-response (finish_reason: insufficient_system_resource)");
196
+ }
197
+ if (finishReason === "aborted") {
198
+ throw new IncompleteStreamError("Provider aborted the response (finish_reason: aborted)");
199
+ }
200
+ }
105
201
  /** HTTP 200 can still carry an upstream error, including inside SSE data events. */
106
202
  function throwProviderError(data) {
107
203
  const choice = data.choices?.[0];
@@ -120,6 +216,7 @@ function throwProviderError(data) {
120
216
  export function parseOpenAiResponse(data, providerName) {
121
217
  throwProviderError(data);
122
218
  const choice = data.choices?.[0] ?? {};
219
+ throwOnAbnormalFinish(choice.finish_reason);
123
220
  const message = choice.message;
124
221
  const content = [];
125
222
  // Visible reasoning: DeepSeek/Qwen use reasoning_content, OpenRouter's
@@ -138,20 +235,17 @@ export function parseOpenAiResponse(data, providerName) {
138
235
  content.push({ type: "text", text: message.content });
139
236
  }
140
237
  const toolCalls = message?.tool_calls;
238
+ const invalidToolCalls = [];
141
239
  if (toolCalls) {
142
240
  for (const tc of toolCalls) {
143
241
  const fn = tc.function;
144
- let input = {};
145
- try {
146
- input = JSON.parse(fn.arguments);
147
- }
148
- catch { /* malformed arguments */ }
149
- content.push({
150
- type: "tool_use",
151
- id: tc.id,
152
- name: fn.name,
153
- input,
154
- });
242
+ const raw = typeof fn.arguments === "string" ? fn.arguments : JSON.stringify(fn.arguments ?? {});
243
+ const parsed = parseToolArguments(raw);
244
+ const id = tc.id;
245
+ const name = fn.name;
246
+ if ("error" in parsed)
247
+ invalidToolCalls.push({ id, name, raw, error: parsed.error });
248
+ content.push({ type: "tool_use", id, name, input: "input" in parsed ? parsed.input : {} });
155
249
  }
156
250
  }
157
251
  const finishReason = choice.finish_reason;
@@ -162,10 +256,18 @@ export function parseOpenAiResponse(data, providerName) {
162
256
  return {
163
257
  content,
164
258
  stop_reason,
165
- usage: usage ? { input_tokens: usage.prompt_tokens ?? 0, output_tokens: usage.completion_tokens ?? 0 } : undefined,
259
+ usage: usage ? parseOpenAiUsage(usage) : undefined,
260
+ ...(invalidToolCalls.length > 0 ? { invalidToolCalls } : {}),
166
261
  };
167
262
  }
168
- /** Parse OpenAI-compatible SSE stream into StreamDelta events. */
263
+ /**
264
+ * Parse OpenAI-compatible SSE stream into StreamDelta events.
265
+ *
266
+ * A response is complete only when the provider says so: a finish_reason or
267
+ * the `[DONE]` sentinel. A connection that closes before either is an error,
268
+ * not a successful end_turn with partial output, and so is DeepSeek's
269
+ * `aborted` finish reason. One that drops after a finish_reason is done.
270
+ */
169
271
  export async function* parseOpenAiStream(res) {
170
272
  if (!res.body)
171
273
  throw new Error("Provider returned empty response body");
@@ -176,83 +278,108 @@ export async function* parseOpenAiStream(res) {
176
278
  const activeTools = new Map(); // index -> tool_call id
177
279
  let stopReason = "end_turn";
178
280
  let usage;
281
+ let finished = false;
282
+ /** Handle one SSE line; returns true on the [DONE] sentinel. */
283
+ function* handleLine(line) {
284
+ if (!line.startsWith("data:"))
285
+ return false;
286
+ const raw = line.slice(5).trim();
287
+ if (raw === "[DONE]")
288
+ return true;
289
+ let chunk;
290
+ try {
291
+ chunk = JSON.parse(raw);
292
+ }
293
+ catch {
294
+ return false;
295
+ }
296
+ throwProviderError(chunk);
297
+ // Usage from final chunk (OpenAI includes it when stream_options.include_usage is set)
298
+ const u = chunk.usage;
299
+ if (u)
300
+ usage = parseOpenAiUsage(u);
301
+ const choice = chunk.choices?.[0];
302
+ if (!choice)
303
+ return false;
304
+ const finishReason = choice.finish_reason;
305
+ throwOnAbnormalFinish(finishReason);
306
+ if (finishReason)
307
+ finished = true;
308
+ if (finishReason === "tool_calls")
309
+ stopReason = "tool_use";
310
+ else if (finishReason === "length")
311
+ stopReason = "max_tokens";
312
+ const delta = choice.delta;
313
+ if (!delta)
314
+ return false;
315
+ // Reasoning content (DeepSeek/Qwen reasoning_content, OpenRouter reasoning)
316
+ const reasoningDelta = typeof delta.reasoning_content === "string"
317
+ ? delta.reasoning_content
318
+ : typeof delta.reasoning === "string" ? delta.reasoning : "";
319
+ if (reasoningDelta) {
320
+ yield { type: "reasoning_delta", text: reasoningDelta };
321
+ }
322
+ // Text content
323
+ if (delta.content && typeof delta.content === "string") {
324
+ yield { type: "text_delta", text: delta.content };
325
+ }
326
+ // Tool calls
327
+ const toolCalls = delta.tool_calls;
328
+ if (toolCalls) {
329
+ for (const tc of toolCalls) {
330
+ const idx = tc.index;
331
+ const fn = tc.function;
332
+ // New tool call starts when id is present
333
+ if (tc.id && typeof tc.id === "string") {
334
+ activeTools.set(idx, tc.id);
335
+ yield { type: "tool_use_start", id: tc.id, name: fn?.name ?? "" };
336
+ }
337
+ // Argument deltas
338
+ if (fn?.arguments && typeof fn.arguments === "string") {
339
+ const toolId = activeTools.get(idx) ?? String(idx);
340
+ yield { type: "tool_use_delta", id: toolId, json: fn.arguments };
341
+ }
342
+ }
343
+ }
344
+ return false;
345
+ }
179
346
  try {
180
- for (;;) {
181
- const { done, value } = await reader.read();
182
- if (done)
347
+ let sawDone = false;
348
+ read: for (;;) {
349
+ let chunk;
350
+ try {
351
+ chunk = await reader.read();
352
+ }
353
+ catch (err) {
354
+ // The provider already said the response is complete (a usage-only
355
+ // chunk and [DONE] may still have been due): keep it rather than
356
+ // requesting the whole response again.
357
+ if (finished)
358
+ break;
359
+ throw withPartialUsage(err, usage);
360
+ }
361
+ const { done, value } = chunk;
362
+ if (done) {
363
+ // A final event with no trailing newline is still an event.
364
+ buf += decoder.decode();
365
+ if (buf && (yield* handleLine(buf)))
366
+ sawDone = true;
183
367
  break;
368
+ }
184
369
  buf += decoder.decode(value, { stream: true });
185
370
  const lines = buf.split("\n");
186
371
  buf = lines.pop();
187
372
  for (const line of lines) {
188
- if (!line.startsWith("data: "))
189
- continue;
190
- const raw = line.slice(6).trim();
191
- if (raw === "[DONE]") {
192
- // Close out any active tool calls before signaling done
193
- for (const [, toolId] of activeTools) {
194
- yield { type: "tool_use_end", id: toolId };
195
- }
196
- activeTools.clear();
197
- yield { type: "done", stop_reason: stopReason, usage };
198
- return;
199
- }
200
- let chunk;
201
- try {
202
- chunk = JSON.parse(raw);
203
- }
204
- catch {
205
- continue;
206
- }
207
- throwProviderError(chunk);
208
- // Usage from final chunk (OpenAI includes it when stream_options.include_usage is set)
209
- const u = chunk.usage;
210
- if (u) {
211
- usage = { input_tokens: u.prompt_tokens ?? 0, output_tokens: u.completion_tokens ?? 0 };
212
- }
213
- const choice = chunk.choices?.[0];
214
- if (!choice)
215
- continue;
216
- const finishReason = choice.finish_reason;
217
- if (finishReason === "tool_calls")
218
- stopReason = "tool_use";
219
- else if (finishReason === "length")
220
- stopReason = "max_tokens";
221
- const delta = choice.delta;
222
- if (!delta)
223
- continue;
224
- // Reasoning content (DeepSeek/Qwen reasoning_content, OpenRouter reasoning)
225
- const reasoningDelta = typeof delta.reasoning_content === "string"
226
- ? delta.reasoning_content
227
- : typeof delta.reasoning === "string" ? delta.reasoning : "";
228
- if (reasoningDelta) {
229
- yield { type: "reasoning_delta", text: reasoningDelta };
230
- }
231
- // Text content
232
- if (delta.content && typeof delta.content === "string") {
233
- yield { type: "text_delta", text: delta.content };
234
- }
235
- // Tool calls
236
- const toolCalls = delta.tool_calls;
237
- if (toolCalls) {
238
- for (const tc of toolCalls) {
239
- const idx = tc.index;
240
- const fn = tc.function;
241
- // New tool call starts when id is present
242
- if (tc.id && typeof tc.id === "string") {
243
- activeTools.set(idx, tc.id);
244
- yield { type: "tool_use_start", id: tc.id, name: fn?.name ?? "" };
245
- }
246
- // Argument deltas
247
- if (fn?.arguments && typeof fn.arguments === "string") {
248
- const toolId = activeTools.get(idx) ?? String(idx);
249
- yield { type: "tool_use_delta", id: toolId, json: fn.arguments };
250
- }
251
- }
373
+ if (yield* handleLine(line)) {
374
+ sawDone = true;
375
+ break read;
252
376
  }
253
377
  }
254
378
  }
255
- // Emit tool_use_end for all active tools, then done
379
+ if (!finished && !sawDone) {
380
+ throw withPartialUsage(new IncompleteStreamError("Provider stream ended before the response was complete (no finish_reason or [DONE])"), usage);
381
+ }
382
+ // Close out any active tool calls before signaling done
256
383
  for (const [, toolId] of activeTools) {
257
384
  yield { type: "tool_use_end", id: toolId };
258
385
  }
@@ -1,4 +1,4 @@
1
- import { toOpenAiTools, toOpenAiMessages, parseOpenAiResponse, parseOpenAiStream } from "./openai-compat.js";
1
+ import { toOpenAiTools, toOpenAiMessages, parseOpenAiResponse, parseOpenAiStream, replaysAllReasoning, wireReasoningEffort, } from "./openai-compat.js";
2
2
  import { lookupContextWindow, lookupMaxOutputTokens, modelSupportsVision } from "../models.js";
3
3
  export class OpenRouterProvider {
4
4
  name = "openrouter";
@@ -104,6 +104,9 @@ export class OpenAiProvider {
104
104
  this.contextWindow = lookupContextWindow(this.model, name);
105
105
  return this;
106
106
  }
107
+ toMessages(system, messages) {
108
+ return toOpenAiMessages(system, messages, this.name, modelSupportsVision(this.name, this.model), replaysAllReasoning(this.name, this.model));
109
+ }
107
110
  apiError(status, text) {
108
111
  const label = this.name === "openai" ? "OpenAI" : this.name;
109
112
  return new Error(`${label} API error ${status}: ${text}`);
@@ -111,11 +114,12 @@ export class OpenAiProvider {
111
114
  async chat(system, messages, tools, signal) {
112
115
  const body = {
113
116
  model: this.model,
114
- messages: toOpenAiMessages(system, messages, this.name, modelSupportsVision(this.name, this.model)),
117
+ messages: this.toMessages(system, messages),
115
118
  max_tokens: this.maxOutputTokens,
116
119
  };
117
- if (this.reasoningEffort)
118
- body.reasoning_effort = this.reasoningEffort;
120
+ const effort = wireReasoningEffort(this.name, this.model, this.reasoningEffort);
121
+ if (effort)
122
+ body.reasoning_effort = effort;
119
123
  if (tools.length > 0)
120
124
  body.tools = toOpenAiTools(tools);
121
125
  const res = await fetch(`${this.baseUrl}/chat/completions`, {
@@ -133,13 +137,14 @@ export class OpenAiProvider {
133
137
  async *chatStream(system, messages, tools, signal) {
134
138
  const body = {
135
139
  model: this.model,
136
- messages: toOpenAiMessages(system, messages, this.name, modelSupportsVision(this.name, this.model)),
140
+ messages: this.toMessages(system, messages),
137
141
  max_tokens: this.maxOutputTokens,
138
142
  stream: true,
139
143
  stream_options: { include_usage: true },
140
144
  };
141
- if (this.reasoningEffort)
142
- body.reasoning_effort = this.reasoningEffort;
145
+ const effort = wireReasoningEffort(this.name, this.model, this.reasoningEffort);
146
+ if (effort)
147
+ body.reasoning_effort = effort;
143
148
  if (tools.length > 0)
144
149
  body.tools = toOpenAiTools(tools);
145
150
  const res = await fetch(`${this.baseUrl}/chat/completions`, {
@@ -1,3 +1,4 @@
1
+ import { overridesFor } from "../model-overrides.js";
1
2
  import { OpenRouterProvider, OpenAiProvider } from "./openrouter.js";
2
3
  import { AnthropicProvider } from "./anthropic.js";
3
4
  import { OllamaProvider } from "./ollama.js";
@@ -22,7 +23,52 @@ function normalizeProviderSelection(overrideProvider, overrideModel) {
22
23
  return { provider, model };
23
24
  }
24
25
  export const DEEPSEEK_BASE_URL = "https://api.deepseek.com";
26
+ /**
27
+ * The DeepSeek endpoint this session started on, when it was not the default
28
+ * (a proxy via --base-url). /model switches resolve the provider again with
29
+ * no options, so a switch back to a DeepSeek model would otherwise go to
30
+ * api.deepseek.com. Kept per provider rather than read from
31
+ * PHREN_AGENT_BASE_URL, which a session started on openai-compat sets to its
32
+ * relay: a DeepSeek key must never be sent there.
33
+ */
34
+ let sessionDeepSeekUrl;
35
+ /** Remember the session's provider endpoint for later /model switches. */
36
+ export function keepSessionEndpoint(provider) {
37
+ sessionDeepSeekUrl = provider.name === "deepseek" && provider.baseUrl && provider.baseUrl !== DEEPSEEK_BASE_URL
38
+ ? provider.baseUrl
39
+ : undefined;
40
+ }
41
+ /**
42
+ * DeepSeek's endpoint: an explicit option, else the session's own DeepSeek
43
+ * endpoint, else PHREN_AGENT_BASE_URL when the environment also names
44
+ * DeepSeek as the provider, else DeepSeek's API.
45
+ */
46
+ function deepSeekBaseUrl(options) {
47
+ const fromEnv = normalizeProviderId(process.env.PHREN_AGENT_PROVIDER) === "deepseek"
48
+ ? process.env.PHREN_AGENT_BASE_URL
49
+ : undefined;
50
+ return (options.baseUrl ?? sessionDeepSeekUrl ?? fromEnv ?? DEEPSEEK_BASE_URL).replace(/\/+$/, "");
51
+ }
52
+ /** A positive integer token count from a flag or env value, else undefined. */
53
+ export function parseTokenCount(raw) {
54
+ if (raw === undefined || raw === "")
55
+ return undefined;
56
+ const value = typeof raw === "number" ? raw : Number(String(raw).replace(/_/g, ""));
57
+ return Number.isInteger(value) && value > 0 ? value : undefined;
58
+ }
25
59
  export function resolveProvider(overrideProvider, overrideModel, overrideMaxOutput, overrideReasoning, options = {}) {
60
+ const provider = resolveCatalogProvider(overrideProvider, overrideModel, overrideMaxOutput, overrideReasoning, options);
61
+ // The catalog can't know every relay's window (and compaction keys off
62
+ // it): an explicit option wins, then --context-window when this is the
63
+ // model it was given for, then PHREN_AGENT_CONTEXT_WINDOW for any model.
64
+ const contextWindow = options.contextWindow
65
+ ?? overridesFor(provider.model).contextWindow
66
+ ?? parseTokenCount(process.env.PHREN_AGENT_CONTEXT_WINDOW);
67
+ if (contextWindow)
68
+ provider.contextWindow = contextWindow;
69
+ return provider;
70
+ }
71
+ function resolveCatalogProvider(overrideProvider, overrideModel, overrideMaxOutput, overrideReasoning, options = {}) {
26
72
  // Keyless replay of a recorded session — takes precedence over everything
27
73
  // so regression tests can run the real binary with no credentials at all.
28
74
  if (process.env.PHREN_AGENT_REPLAY) {
@@ -82,7 +128,7 @@ export function resolveProvider(overrideProvider, overrideModel, overrideMaxOutp
82
128
  if (!deepseekKey)
83
129
  throw new Error("DeepSeek credentials are required. Set DEEPSEEK_API_KEY.");
84
130
  const model = normalizedModel ?? getDefaultModel("deepseek");
85
- const baseUrl = (options.baseUrl ?? DEEPSEEK_BASE_URL).replace(/\/+$/, "");
131
+ const baseUrl = deepSeekBaseUrl(options);
86
132
  return new OpenAiProvider(deepseekKey, model, baseUrl, resolveLimit("deepseek", model), resolveReasoning("deepseek", model))
87
133
  .withName("deepseek", overrideMaxOutput ?? lookupMaxOutputTokens(model, "deepseek"));
88
134
  }
@@ -2,25 +2,37 @@ const DEFAULT_CONFIG = {
2
2
  maxRetries: 3,
3
3
  baseDelayMs: 1000,
4
4
  maxDelayMs: 60_000,
5
- retryableStatuses: new Set([429, 500, 502, 503, 529]),
5
+ retryableStatuses: new Set([429, 500, 502, 503, 504, 529]),
6
6
  };
7
- const RETRYABLE_NETWORK_CODES = new Set(["ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "EPIPE"]);
7
+ const RETRYABLE_NETWORK_CODES = new Set(["ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "EPIPE", "UND_ERR_SOCKET"]);
8
8
  /** Extract HTTP status from error message like "API error 429: ..." */
9
9
  function extractStatus(error) {
10
10
  const msg = error instanceof Error ? error.message : String(error);
11
11
  const match = msg.match(/\berror\s+(\d{3})\b/i);
12
12
  return match ? parseInt(match[1], 10) : null;
13
13
  }
14
- /** Check if the error is a retryable network error by code or message. */
14
+ /**
15
+ * Check if the error is a retryable network error by code or message. A
16
+ * connection dropped mid-body surfaces from fetch (undici) as
17
+ * `TypeError: terminated` with the socket error as its cause.
18
+ */
15
19
  function isNetworkError(error) {
16
- if (error instanceof Error && "code" in error) {
17
- const code = error.code;
18
- if (code && RETRYABLE_NETWORK_CODES.has(code))
20
+ if (error instanceof Error) {
21
+ for (const e of [error, error.cause]) {
22
+ const code = e && typeof e === "object" && "code" in e ? e.code : undefined;
23
+ if (typeof code === "string" && RETRYABLE_NETWORK_CODES.has(code))
24
+ return true;
25
+ }
26
+ if (error.message === "terminated")
19
27
  return true;
20
28
  }
21
29
  const msg = error instanceof Error ? error.message : String(error);
22
30
  return RETRYABLE_NETWORK_CODES.has(msg) || Array.from(RETRYABLE_NETWORK_CODES).some((c) => msg.includes(c));
23
31
  }
32
+ /** Errors the provider layer marked as fixable by a fresh request. */
33
+ function isMarkedRetryable(error) {
34
+ return error instanceof Error && error.retryable === true;
35
+ }
24
36
  /** Extract Retry-After hint from error message. */
25
37
  function extractRetryAfter(error) {
26
38
  const msg = error instanceof Error ? error.message : String(error);
@@ -72,7 +84,7 @@ export async function withRetry(fn, config, verbose, signal) {
72
84
  throw error;
73
85
  const status = extractStatus(error);
74
86
  const isRetryable = !isQuotaExhausted(error)
75
- && ((status !== null && cfg.retryableStatuses.has(status)) || isNetworkError(error));
87
+ && ((status !== null && cfg.retryableStatuses.has(status)) || isNetworkError(error) || isMarkedRetryable(error));
76
88
  if (!isRetryable || attempt >= cfg.maxRetries) {
77
89
  throw error;
78
90
  }
@@ -82,6 +94,7 @@ export async function withRetry(fn, config, verbose, signal) {
82
94
  if (verbose) {
83
95
  process.stderr.write(`Retry ${attempt + 1}/${cfg.maxRetries} after ${Math.round(delayMs)}ms (status ${status})\n`);
84
96
  }
97
+ cfg.onRetry?.(attempt + 1, error, delayMs);
85
98
  await sleep(delayMs, signal);
86
99
  }
87
100
  }
@@ -7,3 +7,34 @@ export function toolResultText(block) {
7
7
  .map((part) => part.text)
8
8
  .join("\n");
9
9
  }
10
+ /** A provider failure that a fresh request can fix; withRetry retries it. */
11
+ export class RetryableProviderError extends Error {
12
+ retryable = true;
13
+ constructor(message) {
14
+ super(message);
15
+ this.name = "RetryableProviderError";
16
+ }
17
+ }
18
+ /**
19
+ * Mark a failed stream with the usage the provider reported before it
20
+ * failed. The attempt is retried, but its tokens were billed, so the loop
21
+ * records them for --budget.
22
+ */
23
+ export function withPartialUsage(error, usage) {
24
+ if (usage && error instanceof Error && !error.usage) {
25
+ error.usage = usage;
26
+ }
27
+ return error;
28
+ }
29
+ /** The usage withPartialUsage attached to an error, if any. */
30
+ export function partialUsage(error) {
31
+ const usage = error instanceof Error ? error.usage : undefined;
32
+ return usage && typeof usage === "object" ? usage : undefined;
33
+ }
34
+ /** Thrown when a stream ends without the provider saying the response is complete. */
35
+ export class IncompleteStreamError extends RetryableProviderError {
36
+ constructor(message) {
37
+ super(message);
38
+ this.name = "IncompleteStreamError";
39
+ }
40
+ }
package/dist/repl.js CHANGED
@@ -85,6 +85,7 @@ export async function startRepl(config) {
85
85
  provider: config.provider,
86
86
  systemPrompt: config.systemPrompt,
87
87
  registry: config.registry,
88
+ promote: config.promote,
88
89
  onModelChange: (result) => {
89
90
  config.provider = resolveProvider(config.provider.name, result.model, undefined, result.reasoning ?? undefined);
90
91
  },