@phren/agent 0.1.15 → 0.3.5
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 +86 -0
- package/dist/agent-loop/index.js +239 -65
- package/dist/agent-loop/stream.js +212 -29
- package/dist/agent-loop/types.js +15 -2
- package/dist/checkpoint.js +103 -22
- package/dist/commands/info.js +30 -15
- package/dist/commands/model.js +6 -2
- package/dist/commands/review.js +183 -0
- package/dist/commands/session.js +70 -7
- package/dist/commands.js +168 -12
- package/dist/config.js +87 -10
- package/dist/context/compactor.js +239 -0
- package/dist/context/pruner.js +39 -13
- package/dist/context/token-counter.js +33 -1
- package/dist/custom-commands.js +69 -0
- package/dist/guards/repeat-tool-reminder.js +75 -0
- package/dist/headless.js +115 -0
- package/dist/herdr-hooks.js +71 -0
- package/dist/index.js +351 -48
- package/dist/mcp-client.js +434 -73
- package/dist/mcp-oauth.js +121 -0
- package/dist/memory/anti-patterns.js +7 -3
- package/dist/memory/auto-capture.js +7 -3
- package/dist/memory/context.js +92 -73
- package/dist/memory/project-context.js +24 -5
- package/dist/memory/provenance.js +15 -0
- package/dist/memory/review-triage.js +166 -0
- package/dist/memory/session.js +36 -1
- package/dist/models.js +152 -19
- package/dist/multi/child-entry.js +65 -10
- package/dist/multi/diff-renderer.js +3 -3
- package/dist/multi/markdown.js +125 -27
- package/dist/multi/provider-manager.js +8 -0
- package/dist/multi/publish.js +72 -0
- package/dist/multi/spawner.js +45 -4
- package/dist/multi/syntax-highlight.js +25 -4
- package/dist/multi/types.js +1 -1
- package/dist/permissions/allowlist.js +86 -13
- package/dist/permissions/checker.js +42 -7
- package/dist/permissions/kernel-sandbox.js +197 -0
- package/dist/permissions/prompt.js +10 -1
- package/dist/permissions/sandbox.js +40 -17
- package/dist/providers/anthropic.js +154 -7
- package/dist/providers/codex-auth.js +28 -0
- package/dist/providers/codex.js +134 -23
- package/dist/providers/history.js +35 -0
- package/dist/providers/ollama.js +37 -6
- package/dist/providers/openai-compat.js +171 -74
- package/dist/providers/openrouter.js +47 -17
- package/dist/providers/replay.js +62 -0
- package/dist/providers/resolve.js +41 -7
- package/dist/providers/retry.js +38 -5
- package/dist/providers/types.js +9 -1
- package/dist/repl.js +47 -5
- package/dist/session/log.js +217 -0
- package/dist/session/persist.js +190 -0
- package/dist/settings.js +42 -26
- package/dist/system-prompt.js +18 -2
- package/dist/tools/apply-patch.js +355 -0
- package/dist/tools/edit-engine.js +287 -0
- package/dist/tools/edit-file.js +86 -27
- package/dist/tools/grep.js +25 -10
- package/dist/tools/lint-test.js +0 -22
- package/dist/tools/mcp-resources.js +51 -6
- package/dist/tools/phren-finding.js +12 -4
- package/dist/tools/phren-search.js +7 -2
- package/dist/tools/read-file.js +37 -9
- package/dist/tools/read-image.js +76 -0
- package/dist/tools/registry.js +27 -5
- package/dist/tools/shell.js +134 -71
- package/dist/tools/skill.js +20 -20
- package/dist/tools/spawn-agent.js +77 -13
- package/dist/tools/update-plan.js +48 -0
- package/dist/tools/web-fetch.js +110 -12
- package/dist/tools/web-search.js +4 -4
- package/dist/tui/components/App.js +98 -26
- package/dist/tui/components/ApprovalPanel.js +8 -0
- package/dist/tui/components/InputArea.js +5 -5
- package/dist/tui/components/ModelPicker.js +43 -0
- package/dist/tui/components/PhrenInput.js +10 -5
- package/dist/tui/components/PlanReview.js +7 -0
- package/dist/tui/components/ShortcutHelp.js +5 -0
- package/dist/tui/components/StatusBar.js +46 -20
- package/dist/tui/components/SteerQueue.js +1 -1
- package/dist/tui/components/ThinkingIndicator.js +6 -4
- package/dist/tui/components/ToolCall.js +10 -4
- package/dist/tui/components/ToolDetail.js +21 -0
- package/dist/tui/help.js +6 -0
- package/dist/tui/hooks/useKeyboardShortcuts.js +15 -1
- package/dist/tui/index.js +0 -1
- package/dist/tui/ink-entry.js +317 -33
- package/dist/tui/tool-render.js +1 -1
- package/dist/user-hooks.js +171 -0
- package/package.json +14 -8
- package/dist/multi/multi-commands.js +0 -170
- package/dist/multi/multi-events.js +0 -98
- package/dist/multi/multi-render.js +0 -147
- package/dist/multi/pane.js +0 -28
- package/dist/multi/tui-multi.js +0 -200
- package/dist/permissions/privacy.js +0 -248
- package/dist/tui/components/AgentMessage.js +0 -5
- package/dist/tui/components/ChatMessage.js +0 -23
- package/dist/tui/components/CodeBlock.js +0 -8
- package/dist/tui/components/Separator.js +0 -7
- package/dist/tui/components/StreamingText.js +0 -5
- package/dist/tui/components/UserMessage.js +0 -5
- package/dist/tui/menu-mode.js +0 -86
- package/dist/tui/mouse.js +0 -71
- package/dist/tui/terminal-control.js +0 -62
- package/dist/tui.js +0 -781
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { stripForeignReasoning, IMAGE_OMITTED_MARKER } from "./history.js";
|
|
1
2
|
/** Convert Anthropic tool defs to OpenAI function format. */
|
|
2
3
|
export function toOpenAiTools(tools) {
|
|
3
4
|
return tools.map((t) => ({
|
|
@@ -5,26 +6,41 @@ export function toOpenAiTools(tools) {
|
|
|
5
6
|
function: { name: t.name, description: t.description, parameters: t.input_schema },
|
|
6
7
|
}));
|
|
7
8
|
}
|
|
8
|
-
/**
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Convert Anthropic messages to OpenAI messages.
|
|
11
|
+
*
|
|
12
|
+
* `providerName` scopes which reasoning blocks belong to this provider; when
|
|
13
|
+
* 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.
|
|
19
|
+
*/
|
|
20
|
+
export function toOpenAiMessages(system, messages, providerName, vision = false) {
|
|
10
21
|
const out = [{ role: "system", content: system }];
|
|
11
|
-
for (const msg of messages) {
|
|
22
|
+
for (const msg of stripForeignReasoning(messages, providerName)) {
|
|
12
23
|
if (msg.role === "assistant") {
|
|
13
24
|
if (typeof msg.content === "string") {
|
|
14
25
|
out.push({ role: "assistant", content: msg.content });
|
|
15
26
|
}
|
|
16
27
|
else {
|
|
17
28
|
const textParts = msg.content.filter((b) => b.type === "text").map((b) => b.type === "text" ? b.text : "");
|
|
29
|
+
const reasoningParts = msg.content
|
|
30
|
+
.filter((b) => b.type === "reasoning")
|
|
31
|
+
.map((b) => (b.type === "reasoning" ? b.text : ""))
|
|
32
|
+
.filter(Boolean);
|
|
18
33
|
const toolCalls = msg.content.filter((b) => b.type === "tool_use").map((b) => {
|
|
19
34
|
if (b.type !== "tool_use")
|
|
20
35
|
throw new Error("unreachable");
|
|
21
36
|
return { id: b.id, type: "function", function: { name: b.name, arguments: JSON.stringify(b.input) } };
|
|
22
37
|
});
|
|
23
|
-
const entry = { role: "assistant" };
|
|
24
|
-
if (
|
|
25
|
-
entry.content = textParts.join("\n");
|
|
26
|
-
if (toolCalls.length > 0)
|
|
38
|
+
const entry = { role: "assistant", content: textParts.join("\n") };
|
|
39
|
+
if (toolCalls.length > 0) {
|
|
27
40
|
entry.tool_calls = toolCalls;
|
|
41
|
+
if (reasoningParts.length > 0)
|
|
42
|
+
entry.reasoning_content = reasoningParts.join("\n");
|
|
43
|
+
}
|
|
28
44
|
out.push(entry);
|
|
29
45
|
}
|
|
30
46
|
}
|
|
@@ -35,7 +51,47 @@ export function toOpenAiMessages(system, messages) {
|
|
|
35
51
|
else {
|
|
36
52
|
for (const block of msg.content) {
|
|
37
53
|
if (block.type === "tool_result") {
|
|
38
|
-
|
|
54
|
+
if (typeof block.content === "string") {
|
|
55
|
+
out.push({ role: "tool", tool_call_id: block.tool_use_id, content: block.content });
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
// Mixed text/image result: the tool message carries the text;
|
|
59
|
+
// images follow as a user message with image_url parts (Chat
|
|
60
|
+
// Completions has no image slot on tool messages). Text-only
|
|
61
|
+
// models get a marker instead of a request that would 400 on
|
|
62
|
+
// every later turn of the durable history.
|
|
63
|
+
const textParts = block.content.filter((c) => c.type === "text").map((c) => (c.type === "text" ? c.text : ""));
|
|
64
|
+
const imageParts = block.content.filter((c) => c.type === "image");
|
|
65
|
+
out.push({ role: "tool", tool_call_id: block.tool_use_id, content: textParts.join("\n") });
|
|
66
|
+
if (imageParts.length > 0) {
|
|
67
|
+
if (vision) {
|
|
68
|
+
out.push({
|
|
69
|
+
role: "user",
|
|
70
|
+
content: imageParts.map((c) => (c.type === "image" ? {
|
|
71
|
+
type: "image_url",
|
|
72
|
+
image_url: { url: `data:${c.source.media_type};base64,${c.source.data}` },
|
|
73
|
+
} : { type: "text", text: "" })),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
out.push({ role: "user", content: IMAGE_OMITTED_MARKER });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else if (block.type === "image") {
|
|
83
|
+
if (vision) {
|
|
84
|
+
out.push({
|
|
85
|
+
role: "user",
|
|
86
|
+
content: [{
|
|
87
|
+
type: "image_url",
|
|
88
|
+
image_url: { url: `data:${block.source.media_type};base64,${block.source.data}` },
|
|
89
|
+
}],
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
out.push({ role: "user", content: IMAGE_OMITTED_MARKER });
|
|
94
|
+
}
|
|
39
95
|
}
|
|
40
96
|
else if (block.type === "text") {
|
|
41
97
|
out.push({ role: "user", content: block.text });
|
|
@@ -46,11 +102,38 @@ export function toOpenAiMessages(system, messages) {
|
|
|
46
102
|
}
|
|
47
103
|
return out;
|
|
48
104
|
}
|
|
105
|
+
/** HTTP 200 can still carry an upstream error, including inside SSE data events. */
|
|
106
|
+
function throwProviderError(data) {
|
|
107
|
+
const choice = data.choices?.[0];
|
|
108
|
+
if (data.error == null && choice?.finish_reason !== "error")
|
|
109
|
+
return;
|
|
110
|
+
const detail = data.error;
|
|
111
|
+
const error = detail && typeof detail === "object" ? detail : undefined;
|
|
112
|
+
const code = error?.code;
|
|
113
|
+
// Preserve the numeric status so the existing retry policy can classify it.
|
|
114
|
+
const status = /^(?:[45]\d{2})$/.test(String(code)) ? ` ${code}` : "";
|
|
115
|
+
const message = typeof error?.message === "string" ? error.message
|
|
116
|
+
: typeof detail === "string" ? detail : "Provider reported a generation error";
|
|
117
|
+
throw new Error(`API error${status}: ${message}`);
|
|
118
|
+
}
|
|
49
119
|
/** Parse OpenAI response into Anthropic content blocks. */
|
|
50
|
-
export function parseOpenAiResponse(data) {
|
|
120
|
+
export function parseOpenAiResponse(data, providerName) {
|
|
121
|
+
throwProviderError(data);
|
|
51
122
|
const choice = data.choices?.[0] ?? {};
|
|
52
123
|
const message = choice.message;
|
|
53
124
|
const content = [];
|
|
125
|
+
// Visible reasoning: DeepSeek/Qwen use reasoning_content, OpenRouter's
|
|
126
|
+
// unified field is reasoning. Reasoning precedes the answer.
|
|
127
|
+
const reasoningText = typeof message?.reasoning_content === "string"
|
|
128
|
+
? message.reasoning_content
|
|
129
|
+
: typeof message?.reasoning === "string" ? message.reasoning : "";
|
|
130
|
+
if (reasoningText) {
|
|
131
|
+
content.push({
|
|
132
|
+
type: "reasoning",
|
|
133
|
+
text: reasoningText,
|
|
134
|
+
...(providerName !== undefined ? { provider: providerName } : {}),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
54
137
|
if (message?.content && typeof message.content === "string") {
|
|
55
138
|
content.push({ type: "text", text: message.content });
|
|
56
139
|
}
|
|
@@ -93,76 +176,90 @@ export async function* parseOpenAiStream(res) {
|
|
|
93
176
|
const activeTools = new Map(); // index -> tool_call id
|
|
94
177
|
let stopReason = "end_turn";
|
|
95
178
|
let usage;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
activeTools.clear();
|
|
113
|
-
yield { type: "done", stop_reason: stopReason, usage };
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
let chunk;
|
|
117
|
-
try {
|
|
118
|
-
chunk = JSON.parse(raw);
|
|
119
|
-
}
|
|
120
|
-
catch {
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
// Usage from final chunk (OpenAI includes it when stream_options.include_usage is set)
|
|
124
|
-
const u = chunk.usage;
|
|
125
|
-
if (u) {
|
|
126
|
-
usage = { input_tokens: u.prompt_tokens ?? 0, output_tokens: u.completion_tokens ?? 0 };
|
|
127
|
-
}
|
|
128
|
-
const choice = chunk.choices?.[0];
|
|
129
|
-
if (!choice)
|
|
130
|
-
continue;
|
|
131
|
-
const finishReason = choice.finish_reason;
|
|
132
|
-
if (finishReason === "tool_calls")
|
|
133
|
-
stopReason = "tool_use";
|
|
134
|
-
else if (finishReason === "length")
|
|
135
|
-
stopReason = "max_tokens";
|
|
136
|
-
const delta = choice.delta;
|
|
137
|
-
if (!delta)
|
|
138
|
-
continue;
|
|
139
|
-
// Text content
|
|
140
|
-
if (delta.content && typeof delta.content === "string") {
|
|
141
|
-
yield { type: "text_delta", text: delta.content };
|
|
142
|
-
}
|
|
143
|
-
// Tool calls
|
|
144
|
-
const toolCalls = delta.tool_calls;
|
|
145
|
-
if (toolCalls) {
|
|
146
|
-
for (const tc of toolCalls) {
|
|
147
|
-
const idx = tc.index;
|
|
148
|
-
const fn = tc.function;
|
|
149
|
-
// New tool call starts when id is present
|
|
150
|
-
if (tc.id && typeof tc.id === "string") {
|
|
151
|
-
activeTools.set(idx, tc.id);
|
|
152
|
-
yield { type: "tool_use_start", id: tc.id, name: fn?.name ?? "" };
|
|
179
|
+
try {
|
|
180
|
+
for (;;) {
|
|
181
|
+
const { done, value } = await reader.read();
|
|
182
|
+
if (done)
|
|
183
|
+
break;
|
|
184
|
+
buf += decoder.decode(value, { stream: true });
|
|
185
|
+
const lines = buf.split("\n");
|
|
186
|
+
buf = lines.pop();
|
|
187
|
+
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 };
|
|
153
195
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
+
}
|
|
158
251
|
}
|
|
159
252
|
}
|
|
160
253
|
}
|
|
161
254
|
}
|
|
255
|
+
// Emit tool_use_end for all active tools, then done
|
|
256
|
+
for (const [, toolId] of activeTools) {
|
|
257
|
+
yield { type: "tool_use_end", id: toolId };
|
|
258
|
+
}
|
|
259
|
+
yield { type: "done", stop_reason: stopReason, usage };
|
|
162
260
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
261
|
+
finally {
|
|
262
|
+
await reader.cancel().catch(() => { });
|
|
263
|
+
reader.releaseLock();
|
|
166
264
|
}
|
|
167
|
-
yield { type: "done", stop_reason: stopReason, usage };
|
|
168
265
|
}
|
|
@@ -1,24 +1,34 @@
|
|
|
1
1
|
import { toOpenAiTools, toOpenAiMessages, parseOpenAiResponse, parseOpenAiStream } from "./openai-compat.js";
|
|
2
|
-
import { lookupMaxOutputTokens } from "../models.js";
|
|
2
|
+
import { lookupContextWindow, lookupMaxOutputTokens, modelSupportsVision } from "../models.js";
|
|
3
3
|
export class OpenRouterProvider {
|
|
4
4
|
name = "openrouter";
|
|
5
|
-
contextWindow
|
|
5
|
+
contextWindow;
|
|
6
6
|
maxOutputTokens;
|
|
7
7
|
apiKey;
|
|
8
8
|
model;
|
|
9
|
+
reasoningEffort;
|
|
9
10
|
baseUrl;
|
|
10
|
-
constructor(apiKey, model, baseUrl, maxOutputTokens) {
|
|
11
|
+
constructor(apiKey, model, baseUrl, maxOutputTokens, reasoningEffort) {
|
|
11
12
|
this.apiKey = apiKey;
|
|
12
13
|
this.model = model ?? "anthropic/claude-sonnet-4-20250514";
|
|
13
14
|
this.baseUrl = baseUrl ?? "https://openrouter.ai/api/v1";
|
|
14
15
|
this.maxOutputTokens = maxOutputTokens ?? lookupMaxOutputTokens(this.model, this.name);
|
|
16
|
+
this.reasoningEffort = reasoningEffort;
|
|
17
|
+
this.contextWindow = lookupContextWindow(this.model, this.name);
|
|
15
18
|
}
|
|
16
|
-
|
|
19
|
+
applyReasoning(body) {
|
|
20
|
+
if (!this.reasoningEffort)
|
|
21
|
+
return;
|
|
22
|
+
const effort = this.reasoningEffort === "xhigh" ? "high" : this.reasoningEffort;
|
|
23
|
+
body.reasoning = { effort };
|
|
24
|
+
}
|
|
25
|
+
async chat(system, messages, tools, signal) {
|
|
17
26
|
const body = {
|
|
18
27
|
model: this.model,
|
|
19
|
-
messages: toOpenAiMessages(system, messages),
|
|
28
|
+
messages: toOpenAiMessages(system, messages, this.name, modelSupportsVision(this.name, this.model)),
|
|
20
29
|
max_tokens: this.maxOutputTokens,
|
|
21
30
|
};
|
|
31
|
+
this.applyReasoning(body);
|
|
22
32
|
if (tools.length > 0)
|
|
23
33
|
body.tools = toOpenAiTools(tools);
|
|
24
34
|
const res = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
@@ -30,21 +40,23 @@ export class OpenRouterProvider {
|
|
|
30
40
|
"X-Title": "phren-agent",
|
|
31
41
|
},
|
|
32
42
|
body: JSON.stringify(body),
|
|
43
|
+
signal,
|
|
33
44
|
});
|
|
34
45
|
if (!res.ok) {
|
|
35
46
|
const text = await res.text();
|
|
36
47
|
throw new Error(`OpenRouter API error ${res.status}: ${text}`);
|
|
37
48
|
}
|
|
38
|
-
return parseOpenAiResponse(await res.json());
|
|
49
|
+
return parseOpenAiResponse(await res.json(), this.name);
|
|
39
50
|
}
|
|
40
|
-
async *chatStream(system, messages, tools) {
|
|
51
|
+
async *chatStream(system, messages, tools, signal) {
|
|
41
52
|
const body = {
|
|
42
53
|
model: this.model,
|
|
43
|
-
messages: toOpenAiMessages(system, messages),
|
|
54
|
+
messages: toOpenAiMessages(system, messages, this.name, modelSupportsVision(this.name, this.model)),
|
|
44
55
|
max_tokens: this.maxOutputTokens,
|
|
45
56
|
stream: true,
|
|
46
57
|
stream_options: { include_usage: true },
|
|
47
58
|
};
|
|
59
|
+
this.applyReasoning(body);
|
|
48
60
|
if (tools.length > 0)
|
|
49
61
|
body.tools = toOpenAiTools(tools);
|
|
50
62
|
const res = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
@@ -56,6 +68,7 @@ export class OpenRouterProvider {
|
|
|
56
68
|
"X-Title": "phren-agent",
|
|
57
69
|
},
|
|
58
70
|
body: JSON.stringify(body),
|
|
71
|
+
signal,
|
|
59
72
|
});
|
|
60
73
|
if (!res.ok) {
|
|
61
74
|
const text = await res.text();
|
|
@@ -64,10 +77,13 @@ export class OpenRouterProvider {
|
|
|
64
77
|
yield* parseOpenAiStream(res);
|
|
65
78
|
}
|
|
66
79
|
}
|
|
67
|
-
/**
|
|
80
|
+
/**
|
|
81
|
+
* OpenAI Chat Completions provider. Also serves any OpenAI-compatible endpoint
|
|
82
|
+
* (DeepSeek, OpenCode Go/Zen, vLLM…) through `baseUrl` and a provider `name`.
|
|
83
|
+
*/
|
|
68
84
|
export class OpenAiProvider {
|
|
69
85
|
name = "openai";
|
|
70
|
-
contextWindow
|
|
86
|
+
contextWindow;
|
|
71
87
|
maxOutputTokens;
|
|
72
88
|
apiKey;
|
|
73
89
|
model;
|
|
@@ -79,11 +95,23 @@ export class OpenAiProvider {
|
|
|
79
95
|
this.baseUrl = baseUrl ?? "https://api.openai.com/v1";
|
|
80
96
|
this.maxOutputTokens = maxOutputTokens ?? lookupMaxOutputTokens(this.model, this.name);
|
|
81
97
|
this.reasoningEffort = reasoningEffort;
|
|
98
|
+
this.contextWindow = lookupContextWindow(this.model, this.name);
|
|
99
|
+
}
|
|
100
|
+
/** Rename for an OpenAI-compatible endpoint; limits are looked up again under the new name. */
|
|
101
|
+
withName(name, maxOutputTokens) {
|
|
102
|
+
this.name = name;
|
|
103
|
+
this.maxOutputTokens = maxOutputTokens ?? lookupMaxOutputTokens(this.model, name);
|
|
104
|
+
this.contextWindow = lookupContextWindow(this.model, name);
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
apiError(status, text) {
|
|
108
|
+
const label = this.name === "openai" ? "OpenAI" : this.name;
|
|
109
|
+
return new Error(`${label} API error ${status}: ${text}`);
|
|
82
110
|
}
|
|
83
|
-
async chat(system, messages, tools) {
|
|
111
|
+
async chat(system, messages, tools, signal) {
|
|
84
112
|
const body = {
|
|
85
113
|
model: this.model,
|
|
86
|
-
messages: toOpenAiMessages(system, messages),
|
|
114
|
+
messages: toOpenAiMessages(system, messages, this.name, modelSupportsVision(this.name, this.model)),
|
|
87
115
|
max_tokens: this.maxOutputTokens,
|
|
88
116
|
};
|
|
89
117
|
if (this.reasoningEffort)
|
|
@@ -94,17 +122,18 @@ export class OpenAiProvider {
|
|
|
94
122
|
method: "POST",
|
|
95
123
|
headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}` },
|
|
96
124
|
body: JSON.stringify(body),
|
|
125
|
+
signal,
|
|
97
126
|
});
|
|
98
127
|
if (!res.ok) {
|
|
99
128
|
const text = await res.text();
|
|
100
|
-
throw
|
|
129
|
+
throw this.apiError(res.status, text);
|
|
101
130
|
}
|
|
102
|
-
return parseOpenAiResponse(await res.json());
|
|
131
|
+
return parseOpenAiResponse(await res.json(), this.name);
|
|
103
132
|
}
|
|
104
|
-
async *chatStream(system, messages, tools) {
|
|
133
|
+
async *chatStream(system, messages, tools, signal) {
|
|
105
134
|
const body = {
|
|
106
135
|
model: this.model,
|
|
107
|
-
messages: toOpenAiMessages(system, messages),
|
|
136
|
+
messages: toOpenAiMessages(system, messages, this.name, modelSupportsVision(this.name, this.model)),
|
|
108
137
|
max_tokens: this.maxOutputTokens,
|
|
109
138
|
stream: true,
|
|
110
139
|
stream_options: { include_usage: true },
|
|
@@ -117,10 +146,11 @@ export class OpenAiProvider {
|
|
|
117
146
|
method: "POST",
|
|
118
147
|
headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}` },
|
|
119
148
|
body: JSON.stringify(body),
|
|
149
|
+
signal,
|
|
120
150
|
});
|
|
121
151
|
if (!res.ok) {
|
|
122
152
|
const text = await res.text();
|
|
123
|
-
throw
|
|
153
|
+
throw this.apiError(res.status, text);
|
|
124
154
|
}
|
|
125
155
|
yield* parseOpenAiStream(res);
|
|
126
156
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { loadEventLog } from "../session/persist.js";
|
|
2
|
+
export class ReplayExhaustedError extends Error {
|
|
3
|
+
constructor(requests, scripted) {
|
|
4
|
+
super(`Replay exhausted: the loop made request #${requests} but the recording only has ${scripted} assistant response(s). ` +
|
|
5
|
+
"The conversation diverged from the recorded session.");
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
function toStopReason(raw) {
|
|
9
|
+
return raw === "tool_use" || raw === "max_tokens" ? raw : "end_turn";
|
|
10
|
+
}
|
|
11
|
+
function scriptFromEvents(events) {
|
|
12
|
+
const script = [];
|
|
13
|
+
for (const event of events) {
|
|
14
|
+
if (event.type !== "assistant/message")
|
|
15
|
+
continue;
|
|
16
|
+
const data = event.data;
|
|
17
|
+
const content = typeof data.message.content === "string"
|
|
18
|
+
? [{ type: "text", text: data.message.content }]
|
|
19
|
+
: data.message.content;
|
|
20
|
+
script.push({ content, stop_reason: toStopReason(data.stop_reason), usage: data.usage });
|
|
21
|
+
}
|
|
22
|
+
return script;
|
|
23
|
+
}
|
|
24
|
+
export class ReplayProvider {
|
|
25
|
+
name = "replay";
|
|
26
|
+
model;
|
|
27
|
+
contextWindow = 200_000;
|
|
28
|
+
script;
|
|
29
|
+
cursor = 0;
|
|
30
|
+
/** Every request the loop made, for post-run assertions. */
|
|
31
|
+
requests = [];
|
|
32
|
+
constructor(script, sourceLabel = "inline") {
|
|
33
|
+
this.script = script;
|
|
34
|
+
this.model = `replay:${sourceLabel}`;
|
|
35
|
+
}
|
|
36
|
+
static fromEventLog(file) {
|
|
37
|
+
const { header, events } = loadEventLog(file);
|
|
38
|
+
return new ReplayProvider(scriptFromEvents(events), header.sessionId);
|
|
39
|
+
}
|
|
40
|
+
static fromMessages(assistantMessages) {
|
|
41
|
+
return new ReplayProvider(assistantMessages.map((m) => ({ content: m.content, stop_reason: m.stop_reason ?? "end_turn" })));
|
|
42
|
+
}
|
|
43
|
+
/** Responses remaining in the script. */
|
|
44
|
+
get remaining() {
|
|
45
|
+
return this.script.length - this.cursor;
|
|
46
|
+
}
|
|
47
|
+
async chat(system, messages, tools, signal) {
|
|
48
|
+
this.requests.push({ system, messages, toolCount: tools.length });
|
|
49
|
+
if (this.cursor >= this.script.length) {
|
|
50
|
+
throw new ReplayExhaustedError(this.requests.length, this.script.length);
|
|
51
|
+
}
|
|
52
|
+
const next = this.script[this.cursor++];
|
|
53
|
+
return { content: next.content, stop_reason: next.stop_reason, usage: next.usage };
|
|
54
|
+
}
|
|
55
|
+
/** Throws unless every scripted response was consumed exactly once. */
|
|
56
|
+
assertFullyConsumed() {
|
|
57
|
+
if (this.cursor !== this.script.length) {
|
|
58
|
+
throw new Error(`Replay incomplete: ${this.cursor}/${this.script.length} scripted responses consumed. ` +
|
|
59
|
+
"The loop made fewer requests than the recorded session.");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -2,7 +2,8 @@ import { OpenRouterProvider, OpenAiProvider } from "./openrouter.js";
|
|
|
2
2
|
import { AnthropicProvider } from "./anthropic.js";
|
|
3
3
|
import { OllamaProvider } from "./ollama.js";
|
|
4
4
|
import { CodexProvider } from "./codex.js";
|
|
5
|
-
import {
|
|
5
|
+
import { ReplayProvider } from "./replay.js";
|
|
6
|
+
import { codexConfiguredModel, hasCodexToken } from "./codex-auth.js";
|
|
6
7
|
import { resolveApiKey } from "@phren/cli/auth/profiles";
|
|
7
8
|
import { getDefaultModel, getDefaultReasoningEffort, lookupMaxOutputTokens, normalizeProviderId, normalizeReasoningEffort, } from "../models.js";
|
|
8
9
|
function normalizeProviderSelection(overrideProvider, overrideModel) {
|
|
@@ -20,8 +21,32 @@ function normalizeProviderSelection(overrideProvider, overrideModel) {
|
|
|
20
21
|
}
|
|
21
22
|
return { provider, model };
|
|
22
23
|
}
|
|
23
|
-
export
|
|
24
|
+
export const DEEPSEEK_BASE_URL = "https://api.deepseek.com";
|
|
25
|
+
export function resolveProvider(overrideProvider, overrideModel, overrideMaxOutput, overrideReasoning, options = {}) {
|
|
26
|
+
// Keyless replay of a recorded session — takes precedence over everything
|
|
27
|
+
// so regression tests can run the real binary with no credentials at all.
|
|
28
|
+
if (process.env.PHREN_AGENT_REPLAY) {
|
|
29
|
+
return ReplayProvider.fromEventLog(process.env.PHREN_AGENT_REPLAY);
|
|
30
|
+
}
|
|
31
|
+
// Generic OpenAI-compatible endpoint: not a catalog provider, so it is
|
|
32
|
+
// handled before normalization and needs an explicit model.
|
|
33
|
+
const rawProvider = (overrideProvider ?? process.env.PHREN_AGENT_PROVIDER)?.toLowerCase();
|
|
34
|
+
if (rawProvider === "openai-compat" || rawProvider === "compat") {
|
|
35
|
+
const baseUrl = (options.baseUrl ?? process.env.PHREN_AGENT_BASE_URL)?.replace(/\/+$/, "");
|
|
36
|
+
if (!baseUrl) {
|
|
37
|
+
throw new Error("openai-compat needs an endpoint: pass --base-url <url> or set PHREN_AGENT_BASE_URL (e.g. https://host/v1).");
|
|
38
|
+
}
|
|
39
|
+
if (!overrideModel)
|
|
40
|
+
throw new Error("openai-compat needs --model <id> (the endpoint's model name).");
|
|
41
|
+
const key = process.env.PHREN_AGENT_API_KEY ?? "";
|
|
42
|
+
const reasoning = normalizeReasoningEffort(overrideReasoning ?? process.env.PHREN_AGENT_REASONING);
|
|
43
|
+
return new OpenAiProvider(key, overrideModel, baseUrl, overrideMaxOutput, reasoning).withName("openai-compat", overrideMaxOutput);
|
|
44
|
+
}
|
|
24
45
|
const { provider: explicit, model: normalizedModel } = normalizeProviderSelection(overrideProvider, overrideModel);
|
|
46
|
+
if (rawProvider && !explicit) {
|
|
47
|
+
// A typo must not silently fall through to auto-detection.
|
|
48
|
+
throw new Error(`Unknown provider "${rawProvider}". Supported: openai-codex, openai, openrouter, anthropic, deepseek, openai-compat, ollama.`);
|
|
49
|
+
}
|
|
25
50
|
const normalizedReasoning = normalizeReasoningEffort(overrideReasoning ?? process.env.PHREN_AGENT_REASONING);
|
|
26
51
|
const openRouterKey = resolveApiKey("openrouter", "OPENROUTER_API_KEY");
|
|
27
52
|
const anthropicKey = resolveApiKey("anthropic", "ANTHROPIC_API_KEY");
|
|
@@ -31,7 +56,7 @@ export function resolveProvider(overrideProvider, overrideModel, overrideMaxOutp
|
|
|
31
56
|
const resolveReasoning = (provider, model) => normalizedReasoning ?? getDefaultReasoningEffort(provider, model);
|
|
32
57
|
// Prefer Codex subscription and GPT-5.4 when available.
|
|
33
58
|
if (explicit === "openai-codex" || (!explicit && hasCodexToken())) {
|
|
34
|
-
const model = normalizedModel ?? getDefaultModel("openai-codex");
|
|
59
|
+
const model = normalizedModel ?? codexConfiguredModel() ?? getDefaultModel("openai-codex");
|
|
35
60
|
return new CodexProvider(model, resolveLimit("openai-codex", model), resolveReasoning("openai-codex", model));
|
|
36
61
|
}
|
|
37
62
|
if (explicit === "openai" || (!explicit && openAiKey)) {
|
|
@@ -44,13 +69,22 @@ export function resolveProvider(overrideProvider, overrideModel, overrideMaxOutp
|
|
|
44
69
|
if (!openRouterKey)
|
|
45
70
|
throw new Error("OpenRouter credentials are required. Set OPENROUTER_API_KEY or run 'phren auth set-key openrouter'.");
|
|
46
71
|
const model = normalizedModel ?? getDefaultModel("openrouter");
|
|
47
|
-
return new OpenRouterProvider(openRouterKey, model, undefined, resolveLimit("openrouter", model));
|
|
72
|
+
return new OpenRouterProvider(openRouterKey, model, undefined, resolveLimit("openrouter", model), resolveReasoning("openrouter", model));
|
|
48
73
|
}
|
|
49
74
|
if (explicit === "anthropic" || (!explicit && anthropicKey)) {
|
|
50
75
|
if (!anthropicKey)
|
|
51
76
|
throw new Error("Anthropic credentials are required. Set ANTHROPIC_API_KEY or run 'phren auth set-key anthropic'.");
|
|
52
77
|
const model = normalizedModel ?? getDefaultModel("anthropic");
|
|
53
|
-
return new AnthropicProvider(anthropicKey, model, resolveLimit("anthropic", model));
|
|
78
|
+
return new AnthropicProvider(anthropicKey, model, resolveLimit("anthropic", model), true, resolveReasoning("anthropic", model));
|
|
79
|
+
}
|
|
80
|
+
const deepseekKey = process.env.DEEPSEEK_API_KEY;
|
|
81
|
+
if (explicit === "deepseek" || (!explicit && deepseekKey)) {
|
|
82
|
+
if (!deepseekKey)
|
|
83
|
+
throw new Error("DeepSeek credentials are required. Set DEEPSEEK_API_KEY.");
|
|
84
|
+
const model = normalizedModel ?? getDefaultModel("deepseek");
|
|
85
|
+
const baseUrl = (options.baseUrl ?? DEEPSEEK_BASE_URL).replace(/\/+$/, "");
|
|
86
|
+
return new OpenAiProvider(deepseekKey, model, baseUrl, resolveLimit("deepseek", model), resolveReasoning("deepseek", model))
|
|
87
|
+
.withName("deepseek", overrideMaxOutput ?? lookupMaxOutputTokens(model, "deepseek"));
|
|
54
88
|
}
|
|
55
89
|
if (explicit === "ollama" || (!explicit && process.env.PHREN_OLLAMA_URL && process.env.PHREN_OLLAMA_URL !== "off")) {
|
|
56
90
|
const model = normalizedModel ?? getDefaultModel("ollama");
|
|
@@ -61,6 +95,6 @@ export function resolveProvider(overrideProvider, overrideModel, overrideMaxOutp
|
|
|
61
95
|
const model = normalizedModel ?? getDefaultModel("ollama");
|
|
62
96
|
return new OllamaProvider(model, undefined, resolveLimit("ollama", model));
|
|
63
97
|
}
|
|
64
|
-
throw new Error(`Unknown provider "${explicit}". Supported: openrouter, anthropic,
|
|
65
|
-
"Set one of: OPENROUTER_API_KEY, ANTHROPIC_API_KEY, OPENAI_API_KEY, or run 'phren auth login' for Codex.");
|
|
98
|
+
throw new Error(`Unknown provider "${explicit}". Supported: openai-codex, openai, openrouter, anthropic, deepseek, openai-compat, ollama.\n` +
|
|
99
|
+
"Set one of: OPENROUTER_API_KEY, ANTHROPIC_API_KEY, OPENAI_API_KEY, DEEPSEEK_API_KEY, or run 'phren auth login' for Codex.");
|
|
66
100
|
}
|
package/dist/providers/retry.js
CHANGED
|
@@ -27,19 +27,52 @@ function extractRetryAfter(error) {
|
|
|
27
27
|
const match = msg.match(/retry[- ]?after[:\s]+(\d+)/i);
|
|
28
28
|
return match ? parseInt(match[1], 10) * 1000 : null;
|
|
29
29
|
}
|
|
30
|
-
function sleep(ms) {
|
|
31
|
-
return new Promise((resolve) =>
|
|
30
|
+
function sleep(ms, signal) {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
if (signal?.aborted) {
|
|
33
|
+
reject(new Error("Aborted"));
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const onAbort = () => { clearTimeout(timer); reject(new Error("Aborted")); };
|
|
37
|
+
const timer = setTimeout(() => { signal?.removeEventListener("abort", onAbort); resolve(); }, ms);
|
|
38
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The provider rejected the request because the prompt is too long for the
|
|
43
|
+
* model. Retrying unchanged cannot help; compacting the history can. Matches
|
|
44
|
+
* the wording of Anthropic, OpenAI (Chat and Responses), OpenRouter,
|
|
45
|
+
* DeepSeek, Gemini-via-gateway and Ollama errors.
|
|
46
|
+
*/
|
|
47
|
+
export function isContextOverflowError(error) {
|
|
48
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
49
|
+
return /context[_ ]length[_ ]exceeded|maximum context length|context window|prompt is too long|input is too long|too many (input )?tokens|exceeds? the (model's )?(maximum )?context|reduce the length of the messages|request too large|input length and `max_tokens` exceed/i.test(msg)
|
|
50
|
+
|| extractStatus(error) === 413;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A 429 that means "out of quota" (a subscription's weekly limit, an empty
|
|
54
|
+
* API balance) rather than "slow down". It resets in hours or never, so
|
|
55
|
+
* backing off for seconds only delays the error.
|
|
56
|
+
*/
|
|
57
|
+
export function isQuotaExhausted(error) {
|
|
58
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
59
|
+
return /usage_limit_reached|insufficient_quota|insufficient[_ ]balance|exceeded your current quota|credit balance is too low/i.test(msg);
|
|
32
60
|
}
|
|
33
61
|
/** Wrap an async function with exponential backoff retry. */
|
|
34
|
-
export async function withRetry(fn, config, verbose) {
|
|
62
|
+
export async function withRetry(fn, config, verbose, signal) {
|
|
35
63
|
const cfg = { ...DEFAULT_CONFIG, ...config };
|
|
36
64
|
for (let attempt = 0;; attempt++) {
|
|
65
|
+
if (signal?.aborted)
|
|
66
|
+
throw new Error("Aborted");
|
|
37
67
|
try {
|
|
38
68
|
return await fn();
|
|
39
69
|
}
|
|
40
70
|
catch (error) {
|
|
71
|
+
if (signal?.aborted)
|
|
72
|
+
throw error;
|
|
41
73
|
const status = extractStatus(error);
|
|
42
|
-
const isRetryable = (
|
|
74
|
+
const isRetryable = !isQuotaExhausted(error)
|
|
75
|
+
&& ((status !== null && cfg.retryableStatuses.has(status)) || isNetworkError(error));
|
|
43
76
|
if (!isRetryable || attempt >= cfg.maxRetries) {
|
|
44
77
|
throw error;
|
|
45
78
|
}
|
|
@@ -49,7 +82,7 @@ export async function withRetry(fn, config, verbose) {
|
|
|
49
82
|
if (verbose) {
|
|
50
83
|
process.stderr.write(`Retry ${attempt + 1}/${cfg.maxRetries} after ${Math.round(delayMs)}ms (status ${status})\n`);
|
|
51
84
|
}
|
|
52
|
-
await sleep(delayMs);
|
|
85
|
+
await sleep(delayMs, signal);
|
|
53
86
|
}
|
|
54
87
|
}
|
|
55
88
|
}
|