@phren/agent 0.1.8 → 0.1.10

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.
Files changed (58) hide show
  1. package/dist/commands/info.js +1 -0
  2. package/dist/commands/model.js +5 -4
  3. package/dist/commands/session.js +24 -2
  4. package/dist/commands.js +36 -1
  5. package/dist/config.js +27 -11
  6. package/dist/cost.js +11 -74
  7. package/dist/index.js +92 -35
  8. package/dist/mcp-client.js +4 -2
  9. package/dist/memory/context.js +56 -24
  10. package/dist/memory/session.js +21 -88
  11. package/dist/models.js +390 -0
  12. package/dist/multi/agent-types.js +170 -0
  13. package/dist/multi/child-entry.js +248 -78
  14. package/dist/multi/coordinator.js +69 -1
  15. package/dist/multi/diff-renderer.js +193 -17
  16. package/dist/multi/markdown.js +49 -23
  17. package/dist/multi/model-picker.js +12 -21
  18. package/dist/multi/multi-events.js +17 -0
  19. package/dist/multi/multi-render.js +1 -0
  20. package/dist/multi/provider-manager.js +23 -13
  21. package/dist/multi/spawner.js +84 -7
  22. package/dist/multi/syntax-highlight.js +64 -48
  23. package/dist/multi/worktree.js +43 -0
  24. package/dist/package-metadata.js +11 -0
  25. package/dist/permissions/checker.js +21 -1
  26. package/dist/providers/codex-auth.js +48 -34
  27. package/dist/providers/codex.js +110 -40
  28. package/dist/providers/ollama.js +5 -4
  29. package/dist/providers/openrouter.js +12 -5
  30. package/dist/providers/resolve.js +50 -32
  31. package/dist/providers/types.js +0 -1
  32. package/dist/repl.js +19 -34
  33. package/dist/tools/grep.js +111 -23
  34. package/dist/tools/mcp-resources.js +32 -0
  35. package/dist/tools/registry.js +13 -2
  36. package/dist/tools/shell.js +101 -3
  37. package/dist/tools/skill.js +44 -0
  38. package/dist/tools/spawn-agent.js +158 -0
  39. package/dist/tui/ansi.js +15 -11
  40. package/dist/tui/components/App.js +180 -20
  41. package/dist/tui/components/Banner.js +7 -4
  42. package/dist/tui/components/InputArea.js +54 -12
  43. package/dist/tui/components/PhrenInput.js +329 -0
  44. package/dist/tui/components/StatusBar.js +3 -1
  45. package/dist/tui/components/SteerQueue.js +4 -2
  46. package/dist/tui/components/ThinkingIndicator.js +12 -11
  47. package/dist/tui/components/ToolCall.js +17 -11
  48. package/dist/tui/components/ToolSpinner.js +13 -0
  49. package/dist/tui/hooks/useKeyboardShortcuts.js +65 -24
  50. package/dist/tui/hooks/useSearch.js +47 -0
  51. package/dist/tui/hooks/useSlashCommands.js +16 -19
  52. package/dist/tui/ink-entry.js +317 -18
  53. package/dist/tui/mouse.js +71 -0
  54. package/dist/tui/terminal-control.js +62 -0
  55. package/dist/tui/themes.js +398 -0
  56. package/dist/tui/tool-render.js +12 -1
  57. package/dist/tui.js +2 -1
  58. package/package.json +1 -3
@@ -1,4 +1,5 @@
1
1
  import { getAccessToken } from "./codex-auth.js";
2
+ import { lookupMaxOutputTokens } from "../models.js";
2
3
  const CODEX_API = "https://chatgpt.com/backend-api/codex/responses";
3
4
  /** Convert our tool defs to Responses API tool format. */
4
5
  function toResponsesTools(tools) {
@@ -72,44 +73,71 @@ function toResponsesInput(messages) {
72
73
  }
73
74
  return input;
74
75
  }
76
+ const DEBUG = process.env.PHREN_DEBUG === "1";
77
+ function debugLog(label, data) {
78
+ if (!DEBUG)
79
+ return;
80
+ process.stderr.write(`[codex:debug] ${label}: ${JSON.stringify(data, null, 2)}\n`);
81
+ }
75
82
  /** Parse non-streaming Responses API output into our ContentBlock format. */
76
83
  function parseResponsesOutput(data) {
77
- const output = data.output;
84
+ debugLog("parseResponsesOutput input", data);
85
+ // The Responses API may return output at top-level or nested under a "response" key.
86
+ // Handle both shapes defensively.
87
+ const root = (data.output !== undefined ? data : data.response ?? data);
88
+ const output = root.output;
78
89
  const content = [];
79
90
  let hasToolUse = false;
80
91
  if (output) {
81
92
  for (const item of output) {
93
+ debugLog("output item", item);
82
94
  if (item.type === "message") {
95
+ // Content may be an array of blocks or a plain string
83
96
  const msgContent = item.content;
84
- if (msgContent) {
97
+ if (Array.isArray(msgContent)) {
85
98
  for (const c of msgContent) {
86
- if (c.type === "output_text" && c.text) {
99
+ if ((c.type === "output_text" || c.type === "text") && c.text) {
87
100
  content.push({ type: "text", text: c.text });
88
101
  }
89
102
  }
90
103
  }
104
+ else if (typeof msgContent === "string" && msgContent) {
105
+ content.push({ type: "text", text: msgContent });
106
+ }
91
107
  }
92
108
  else if (item.type === "function_call") {
93
109
  hasToolUse = true;
94
110
  let input = {};
95
- try {
96
- input = JSON.parse(item.arguments);
111
+ const rawArgs = item.arguments;
112
+ if (rawArgs) {
113
+ try {
114
+ input = JSON.parse(rawArgs);
115
+ }
116
+ catch { /* malformed arguments */ }
97
117
  }
98
- catch { /* malformed arguments */ }
118
+ const callId = (item.call_id ?? item.id);
99
119
  content.push({
100
120
  type: "tool_use",
101
- id: item.call_id,
121
+ id: callId,
102
122
  name: item.name,
103
123
  input,
104
124
  });
105
125
  }
126
+ else if (item.type === "reasoning") {
127
+ // Reasoning items are informational — skip them, they don't map to content blocks
128
+ debugLog("skipping reasoning item", { id: item.id });
129
+ }
106
130
  }
107
131
  }
108
- const status = data.status;
132
+ else {
133
+ debugLog("no output array found in response", { keys: Object.keys(data) });
134
+ }
135
+ const status = (root.status ?? data.status);
109
136
  const stop_reason = hasToolUse ? "tool_use"
110
137
  : status === "incomplete" ? "max_tokens"
111
138
  : "end_turn";
112
- const usage = data.usage;
139
+ const usage = (root.usage ?? data.usage);
140
+ debugLog("parseResponsesOutput result", { content, stop_reason, usage });
113
141
  return {
114
142
  content,
115
143
  stop_reason,
@@ -117,13 +145,15 @@ function parseResponsesOutput(data) {
117
145
  };
118
146
  }
119
147
  export class CodexProvider {
120
- name = "codex";
121
- contextWindow = 128_000;
148
+ name = "openai-codex";
149
+ contextWindow = 1_050_000;
122
150
  maxOutputTokens;
123
151
  model;
124
- constructor(model, maxOutputTokens) {
125
- this.model = model ?? "gpt-5.3-codex";
126
- this.maxOutputTokens = maxOutputTokens ?? 8192;
152
+ reasoningEffort;
153
+ constructor(model, maxOutputTokens, reasoningEffort) {
154
+ this.model = model ?? "gpt-5.4";
155
+ this.maxOutputTokens = maxOutputTokens ?? lookupMaxOutputTokens(this.model, this.name);
156
+ this.reasoningEffort = reasoningEffort;
127
157
  }
128
158
  async chat(system, messages, tools) {
129
159
  const { accessToken } = await getAccessToken();
@@ -134,30 +164,73 @@ export class CodexProvider {
134
164
  store: false,
135
165
  stream: true,
136
166
  };
167
+ if (this.reasoningEffort) {
168
+ body.reasoning = { effort: this.reasoningEffort };
169
+ }
170
+ if (tools.length > 0) {
171
+ body.tools = toResponsesTools(tools);
172
+ body.tool_choice = "auto";
173
+ }
174
+ return parseResponsesOutput(await this.requestResponse(accessToken, body));
175
+ }
176
+ async *chatStream(system, messages, tools) {
177
+ const { accessToken } = await getAccessToken();
178
+ const body = {
179
+ model: this.model,
180
+ instructions: system,
181
+ input: toResponsesInput(messages),
182
+ store: false,
183
+ stream: true,
184
+ include: ["reasoning.encrypted_content"],
185
+ };
186
+ if (this.reasoningEffort) {
187
+ body.reasoning = { effort: this.reasoningEffort };
188
+ }
137
189
  if (tools.length > 0) {
138
190
  body.tools = toResponsesTools(tools);
139
191
  body.tool_choice = "auto";
140
192
  }
141
- const bodyStr = JSON.stringify(body);
193
+ // OpenClaw treats transport as auto: try WebSocket first, then fall back to the
194
+ // HTTP responses stream if the WS path is unavailable.
195
+ try {
196
+ yield* this.chatStreamWs(accessToken, body);
197
+ }
198
+ catch {
199
+ const response = await this.requestResponse(accessToken, body);
200
+ const parsed = parseResponsesOutput(response);
201
+ for (const block of parsed.content) {
202
+ if (block.type === "text") {
203
+ yield { type: "text_delta", text: block.text };
204
+ }
205
+ else if (block.type === "tool_use") {
206
+ yield { type: "tool_use_start", id: block.id, name: block.name };
207
+ yield { type: "tool_use_delta", id: block.id, json: JSON.stringify(block.input) };
208
+ yield { type: "tool_use_end", id: block.id };
209
+ }
210
+ }
211
+ yield { type: "done", stop_reason: parsed.stop_reason, usage: parsed.usage };
212
+ }
213
+ }
214
+ async requestResponse(accessToken, body) {
142
215
  const res = await fetch(CODEX_API, {
143
216
  method: "POST",
144
217
  headers: {
145
218
  "Content-Type": "application/json",
146
219
  Authorization: `Bearer ${accessToken}`,
147
220
  },
148
- body: bodyStr,
221
+ body: JSON.stringify(body),
149
222
  });
150
223
  if (!res.ok) {
151
224
  const text = await res.text();
152
225
  throw new Error(`Codex API error ${res.status}: ${text}`);
153
226
  }
154
- // Stream is mandatory for Codex backend — consume it and collect the final response
155
227
  if (!res.body)
156
228
  throw new Error("Provider returned empty response body");
157
229
  const reader = res.body.getReader();
158
230
  const decoder = new TextDecoder();
159
231
  let buffer = "";
160
232
  let finalResponse = null;
233
+ const seenEventTypes = new Set();
161
234
  while (true) {
162
235
  const { done, value } = await reader.read();
163
236
  if (done)
@@ -173,35 +246,32 @@ export class CodexProvider {
173
246
  continue;
174
247
  try {
175
248
  const event = JSON.parse(data);
176
- if (event.type === "response.completed" && event.response) {
249
+ const evType = event.type;
250
+ if (evType)
251
+ seenEventTypes.add(evType);
252
+ debugLog("SSE event", { type: evType });
253
+ // Accept response.completed or response.done (API may use either)
254
+ if ((evType === "response.completed" || evType === "response.done") &&
255
+ event.response) {
177
256
  finalResponse = event.response;
178
257
  }
258
+ else if (evType === "response.completed" || evType === "response.done") {
259
+ // Response data at the top level (no nested .response key)
260
+ if (event.output !== undefined || event.status !== undefined) {
261
+ finalResponse = event;
262
+ }
263
+ }
179
264
  }
180
- catch { /* skip */ }
265
+ catch { /* skip malformed events */ }
181
266
  }
182
267
  }
183
- if (finalResponse)
184
- return parseResponsesOutput(finalResponse);
185
- // No response.completed event received
186
- throw new Error("Codex stream ended without response.completed event");
187
- }
188
- async *chatStream(system, messages, tools) {
189
- const { accessToken } = await getAccessToken();
190
- const body = {
191
- model: this.model,
192
- instructions: system,
193
- input: toResponsesInput(messages),
194
- store: false,
195
- stream: true,
196
- include: ["reasoning.encrypted_content"],
197
- };
198
- if (tools.length > 0) {
199
- body.tools = toResponsesTools(tools);
200
- body.tool_choice = "auto";
268
+ if (!finalResponse) {
269
+ debugLog("no finalResponse found, seenEventTypes", [...seenEventTypes]);
270
+ }
271
+ if (!finalResponse) {
272
+ throw new Error("Codex stream ended without response.completed event");
201
273
  }
202
- // Use WebSocket for true token-by-token streaming (matches Codex CLI behavior).
203
- // The HTTP SSE endpoint batches the entire response before flushing.
204
- yield* this.chatStreamWs(accessToken, body);
274
+ return finalResponse;
205
275
  }
206
276
  /** WebSocket streaming — sends request, yields deltas as they arrive. */
207
277
  async *chatStreamWs(accessToken, body) {
@@ -67,11 +67,11 @@ export class OllamaProvider {
67
67
  }
68
68
  const toolCalls = message?.tool_calls;
69
69
  if (toolCalls) {
70
- for (const tc of toolCalls) {
71
- const fn = tc.function;
70
+ for (let i = 0; i < toolCalls.length; i++) {
71
+ const fn = toolCalls[i].function;
72
72
  content.push({
73
73
  type: "tool_use",
74
- id: `ollama-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
74
+ id: `call_${i}`,
75
75
  name: fn.name,
76
76
  input: typeof fn.arguments === "string" ? JSON.parse(fn.arguments) : fn.arguments,
77
77
  });
@@ -104,6 +104,7 @@ export class OllamaProvider {
104
104
  const decoder = new TextDecoder();
105
105
  let buf = "";
106
106
  let stopReason = "end_turn";
107
+ let toolCallIndex = 0;
107
108
  for (;;) {
108
109
  const { done, value } = await reader.read();
109
110
  if (done)
@@ -131,7 +132,7 @@ export class OllamaProvider {
131
132
  stopReason = "tool_use";
132
133
  for (const tc of tcalls) {
133
134
  const fn = tc.function;
134
- const id = `ollama-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
135
+ const id = `call_${toolCallIndex++}`;
135
136
  yield { type: "tool_use_start", id, name: fn.name };
136
137
  yield { type: "tool_use_delta", id, json: JSON.stringify(typeof fn.arguments === "string" ? JSON.parse(fn.arguments) : fn.arguments) };
137
138
  yield { type: "tool_use_end", id };
@@ -1,4 +1,5 @@
1
1
  import { toOpenAiTools, toOpenAiMessages, parseOpenAiResponse, parseOpenAiStream } from "./openai-compat.js";
2
+ import { lookupMaxOutputTokens } from "../models.js";
2
3
  export class OpenRouterProvider {
3
4
  name = "openrouter";
4
5
  contextWindow = 200_000;
@@ -10,7 +11,7 @@ export class OpenRouterProvider {
10
11
  this.apiKey = apiKey;
11
12
  this.model = model ?? "anthropic/claude-sonnet-4-20250514";
12
13
  this.baseUrl = baseUrl ?? "https://openrouter.ai/api/v1";
13
- this.maxOutputTokens = maxOutputTokens ?? 8192;
14
+ this.maxOutputTokens = maxOutputTokens ?? lookupMaxOutputTokens(this.model, this.name);
14
15
  }
15
16
  async chat(system, messages, tools) {
16
17
  const body = {
@@ -66,16 +67,18 @@ export class OpenRouterProvider {
66
67
  /** OpenAI-native provider (same protocol, different base URL). */
67
68
  export class OpenAiProvider {
68
69
  name = "openai";
69
- contextWindow = 128_000;
70
+ contextWindow = 1_050_000;
70
71
  maxOutputTokens;
71
72
  apiKey;
72
73
  model;
74
+ reasoningEffort;
73
75
  baseUrl;
74
- constructor(apiKey, model, baseUrl, maxOutputTokens) {
76
+ constructor(apiKey, model, baseUrl, maxOutputTokens, reasoningEffort) {
75
77
  this.apiKey = apiKey;
76
- this.model = model ?? "gpt-4o";
78
+ this.model = model ?? "gpt-5.4";
77
79
  this.baseUrl = baseUrl ?? "https://api.openai.com/v1";
78
- this.maxOutputTokens = maxOutputTokens ?? 8192;
80
+ this.maxOutputTokens = maxOutputTokens ?? lookupMaxOutputTokens(this.model, this.name);
81
+ this.reasoningEffort = reasoningEffort;
79
82
  }
80
83
  async chat(system, messages, tools) {
81
84
  const body = {
@@ -83,6 +86,8 @@ export class OpenAiProvider {
83
86
  messages: toOpenAiMessages(system, messages),
84
87
  max_tokens: this.maxOutputTokens,
85
88
  };
89
+ if (this.reasoningEffort)
90
+ body.reasoning_effort = this.reasoningEffort;
86
91
  if (tools.length > 0)
87
92
  body.tools = toOpenAiTools(tools);
88
93
  const res = await fetch(`${this.baseUrl}/chat/completions`, {
@@ -104,6 +109,8 @@ export class OpenAiProvider {
104
109
  stream: true,
105
110
  stream_options: { include_usage: true },
106
111
  };
112
+ if (this.reasoningEffort)
113
+ body.reasoning_effort = this.reasoningEffort;
107
114
  if (tools.length > 0)
108
115
  body.tools = toOpenAiTools(tools);
109
116
  const res = await fetch(`${this.baseUrl}/chat/completions`, {
@@ -3,46 +3,64 @@ import { AnthropicProvider } from "./anthropic.js";
3
3
  import { OllamaProvider } from "./ollama.js";
4
4
  import { CodexProvider } from "./codex.js";
5
5
  import { hasCodexToken } from "./codex-auth.js";
6
- import { lookupMaxOutputTokens } from "../cost.js";
7
- export function resolveProvider(overrideProvider, overrideModel, overrideMaxOutput) {
8
- const explicit = overrideProvider ?? process.env.PHREN_AGENT_PROVIDER;
6
+ import { resolveApiKey } from "@phren/cli/auth/profiles";
7
+ import { getDefaultModel, getDefaultReasoningEffort, lookupMaxOutputTokens, normalizeProviderId, normalizeReasoningEffort, } from "../models.js";
8
+ function normalizeProviderSelection(overrideProvider, overrideModel) {
9
+ let provider = normalizeProviderId(overrideProvider ?? process.env.PHREN_AGENT_PROVIDER);
10
+ let model = overrideModel;
11
+ if (model) {
12
+ if ((!provider || provider === "openai") && model.startsWith("openai/")) {
13
+ provider = "openai";
14
+ model = model.slice("openai/".length);
15
+ }
16
+ else if ((!provider || provider === "openai-codex") && model.startsWith("openai-codex/")) {
17
+ provider = "openai-codex";
18
+ model = model.slice("openai-codex/".length);
19
+ }
20
+ }
21
+ return { provider, model };
22
+ }
23
+ export function resolveProvider(overrideProvider, overrideModel, overrideMaxOutput, overrideReasoning) {
24
+ const { provider: explicit, model: normalizedModel } = normalizeProviderSelection(overrideProvider, overrideModel);
25
+ const normalizedReasoning = normalizeReasoningEffort(overrideReasoning ?? process.env.PHREN_AGENT_REASONING);
26
+ const openRouterKey = resolveApiKey("openrouter", "OPENROUTER_API_KEY");
27
+ const anthropicKey = resolveApiKey("anthropic", "ANTHROPIC_API_KEY");
28
+ const openAiKey = resolveApiKey("openai", "OPENAI_API_KEY");
9
29
  // Resolve max output tokens: CLI override > model lookup > default 8192
10
- const resolveLimit = (model) => overrideMaxOutput ?? lookupMaxOutputTokens(model);
11
- if (explicit === "openrouter" || (!explicit && process.env.OPENROUTER_API_KEY)) {
12
- const key = process.env.OPENROUTER_API_KEY;
13
- if (!key)
14
- throw new Error("OPENROUTER_API_KEY is required for OpenRouter provider.");
15
- const model = overrideModel ?? "anthropic/claude-sonnet-4-20250514";
16
- return new OpenRouterProvider(key, overrideModel, undefined, resolveLimit(model));
30
+ const resolveLimit = (provider, model) => overrideMaxOutput ?? lookupMaxOutputTokens(model, provider);
31
+ const resolveReasoning = (provider, model) => normalizedReasoning ?? getDefaultReasoningEffort(provider, model);
32
+ // Prefer Codex subscription and GPT-5.4 when available.
33
+ if (explicit === "openai-codex" || (!explicit && hasCodexToken())) {
34
+ const model = normalizedModel ?? getDefaultModel("openai-codex");
35
+ return new CodexProvider(model, resolveLimit("openai-codex", model), resolveReasoning("openai-codex", model));
17
36
  }
18
- if (explicit === "anthropic" || (!explicit && process.env.ANTHROPIC_API_KEY)) {
19
- const key = process.env.ANTHROPIC_API_KEY;
20
- if (!key)
21
- throw new Error("ANTHROPIC_API_KEY is required for Anthropic provider.");
22
- const model = overrideModel ?? "claude-sonnet-4-20250514";
23
- return new AnthropicProvider(key, overrideModel, resolveLimit(model));
37
+ if (explicit === "openai" || (!explicit && openAiKey)) {
38
+ if (!openAiKey)
39
+ throw new Error("OpenAI credentials are required. Set OPENAI_API_KEY or run 'phren auth set-key openai'.");
40
+ const model = normalizedModel ?? getDefaultModel("openai");
41
+ return new OpenAiProvider(openAiKey, model, undefined, resolveLimit("openai", model), resolveReasoning("openai", model));
24
42
  }
25
- if (explicit === "openai" || (!explicit && process.env.OPENAI_API_KEY)) {
26
- const key = process.env.OPENAI_API_KEY;
27
- if (!key)
28
- throw new Error("OPENAI_API_KEY is required for OpenAI provider.");
29
- const model = overrideModel ?? "gpt-4o";
30
- return new OpenAiProvider(key, overrideModel, undefined, resolveLimit(model));
43
+ if (explicit === "openrouter" || (!explicit && openRouterKey)) {
44
+ if (!openRouterKey)
45
+ throw new Error("OpenRouter credentials are required. Set OPENROUTER_API_KEY or run 'phren auth set-key openrouter'.");
46
+ const model = normalizedModel ?? getDefaultModel("openrouter");
47
+ return new OpenRouterProvider(openRouterKey, model, undefined, resolveLimit("openrouter", model));
31
48
  }
32
- // Codex: uses your ChatGPT subscription directly — no API key, no middleman
33
- if (explicit === "codex" || (!explicit && hasCodexToken())) {
34
- const model = overrideModel ?? "gpt-5.2-codex";
35
- return new CodexProvider(overrideModel, resolveLimit(model));
49
+ if (explicit === "anthropic" || (!explicit && anthropicKey)) {
50
+ if (!anthropicKey)
51
+ throw new Error("Anthropic credentials are required. Set ANTHROPIC_API_KEY or run 'phren auth set-key anthropic'.");
52
+ const model = normalizedModel ?? getDefaultModel("anthropic");
53
+ return new AnthropicProvider(anthropicKey, model, resolveLimit("anthropic", model));
36
54
  }
37
55
  if (explicit === "ollama" || (!explicit && process.env.PHREN_OLLAMA_URL && process.env.PHREN_OLLAMA_URL !== "off")) {
38
- const model = overrideModel ?? "qwen2.5-coder:14b";
39
- return new OllamaProvider(overrideModel, process.env.PHREN_OLLAMA_URL, resolveLimit(model));
56
+ const model = normalizedModel ?? getDefaultModel("ollama");
57
+ return new OllamaProvider(model, process.env.PHREN_OLLAMA_URL, resolveLimit("ollama", model));
40
58
  }
41
59
  // Last resort: try Ollama at default URL
42
60
  if (!explicit) {
43
- const model = overrideModel ?? "qwen2.5-coder:14b";
44
- return new OllamaProvider(overrideModel, undefined, resolveLimit(model));
61
+ const model = normalizedModel ?? getDefaultModel("ollama");
62
+ return new OllamaProvider(model, undefined, resolveLimit("ollama", model));
45
63
  }
46
- throw new Error(`Unknown provider "${explicit}". Supported: openrouter, anthropic, openai, codex, ollama.\n` +
47
- "Set one of: OPENROUTER_API_KEY, ANTHROPIC_API_KEY, OPENAI_API_KEY, or run 'phren-agent auth login' for Codex.");
64
+ throw new Error(`Unknown provider "${explicit}". Supported: openrouter, anthropic, openai, openai-codex, ollama.\n` +
65
+ "Set one of: OPENROUTER_API_KEY, ANTHROPIC_API_KEY, OPENAI_API_KEY, or run 'phren auth login' for Codex.");
48
66
  }
@@ -1,2 +1 @@
1
- /** LLM provider types — Anthropic content-block format internally. */
2
1
  export {};
package/dist/repl.js CHANGED
@@ -5,7 +5,8 @@ import * as path from "node:path";
5
5
  import * as os from "node:os";
6
6
  import { createSession, runTurn } from "./agent-loop.js";
7
7
  import { handleCommand } from "./commands.js";
8
- import { loadInputMode, saveInputMode, savePermissionMode } from "./settings.js";
8
+ import { resolveProvider } from "./providers/resolve.js";
9
+ import { loadInputMode } from "./settings.js";
9
10
  const HISTORY_DIR = path.join(os.homedir(), ".phren-agent");
10
11
  const HISTORY_FILE = path.join(HISTORY_DIR, "repl-history.txt");
11
12
  const MAX_HISTORY = 500;
@@ -50,6 +51,21 @@ export async function startRepl(config) {
50
51
  process.stderr.write(`${DIM}phren-agent interactive mode (${modeLabel}). Type /help for commands, Ctrl+D to exit.${RESET}\n`);
51
52
  rl.prompt();
52
53
  const allHistory = [...history];
54
+ const buildCommandContext = () => ({
55
+ session,
56
+ contextLimit,
57
+ undoStack: [],
58
+ phrenCtx: config.phrenCtx,
59
+ providerName: config.provider.name,
60
+ currentModel: config.provider.model,
61
+ currentReasoning: config.provider.reasoningEffort ?? null,
62
+ provider: config.provider,
63
+ systemPrompt: config.systemPrompt,
64
+ registry: config.registry,
65
+ onModelChange: (result) => {
66
+ config.provider = resolveProvider(config.provider.name, result.model, undefined, result.reasoning ?? undefined);
67
+ },
68
+ });
53
69
  for await (const line of rl) {
54
70
  const trimmed = line.trim();
55
71
  if (!trimmed) {
@@ -58,31 +74,7 @@ export async function startRepl(config) {
58
74
  }
59
75
  allHistory.push(trimmed);
60
76
  // Handle slash commands
61
- if (trimmed === "/mode") {
62
- const newMode = inputMode === "steering" ? "queue" : "steering";
63
- inputMode = newMode;
64
- saveInputMode(newMode);
65
- process.stderr.write(`${YELLOW}Input mode: ${newMode}${RESET}\n`);
66
- rl.prompt();
67
- continue;
68
- }
69
- if (trimmed.startsWith("/permissions")) {
70
- const VALID_MODES = ["suggest", "auto-confirm", "full-auto"];
71
- const arg = trimmed.split(/\s+/)[1];
72
- if (!arg || !VALID_MODES.includes(arg)) {
73
- const current = config.registry.permissionConfig.mode;
74
- process.stderr.write(`${DIM}Permission mode: ${current}${RESET}\n`);
75
- process.stderr.write(`${DIM}Usage: /permissions <suggest|auto-confirm|full-auto>${RESET}\n`);
76
- }
77
- else {
78
- config.registry.setPermissions({ ...config.registry.permissionConfig, mode: arg });
79
- savePermissionMode(arg);
80
- process.stderr.write(`${YELLOW}Permission mode: ${arg}${RESET}\n`);
81
- }
82
- rl.prompt();
83
- continue;
84
- }
85
- if (handleCommand(trimmed, { session, contextLimit, undoStack: [], phrenCtx: config.phrenCtx })) {
77
+ if (handleCommand(trimmed, buildCommandContext())) {
86
78
  rl.prompt();
87
79
  continue;
88
80
  }
@@ -112,14 +104,7 @@ export async function startRepl(config) {
112
104
  pendingInput = null;
113
105
  allHistory.push(queued);
114
106
  if (queued.startsWith("/")) {
115
- if (queued === "/mode") {
116
- inputMode = inputMode === "steering" ? "queue" : "steering";
117
- saveInputMode(inputMode);
118
- process.stderr.write(`${YELLOW}Input mode: ${inputMode}${RESET}\n`);
119
- }
120
- else {
121
- handleCommand(queued, { session, contextLimit, undoStack: [], phrenCtx: config.phrenCtx });
122
- }
107
+ handleCommand(queued, buildCommandContext());
123
108
  break;
124
109
  }
125
110
  agentRunning = true;