@phren/agent 0.1.14 → 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.
Files changed (112) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +86 -0
  3. package/dist/agent-loop/index.js +239 -65
  4. package/dist/agent-loop/stream.js +212 -29
  5. package/dist/agent-loop/types.js +15 -2
  6. package/dist/bin.js +0 -0
  7. package/dist/checkpoint.js +103 -22
  8. package/dist/commands/info.js +30 -15
  9. package/dist/commands/model.js +6 -2
  10. package/dist/commands/review.js +183 -0
  11. package/dist/commands/session.js +70 -7
  12. package/dist/commands.js +168 -12
  13. package/dist/config.js +87 -10
  14. package/dist/context/compactor.js +239 -0
  15. package/dist/context/pruner.js +39 -13
  16. package/dist/context/token-counter.js +33 -1
  17. package/dist/custom-commands.js +69 -0
  18. package/dist/guards/repeat-tool-reminder.js +75 -0
  19. package/dist/headless.js +115 -0
  20. package/dist/herdr-hooks.js +71 -0
  21. package/dist/index.js +351 -48
  22. package/dist/mcp-client.js +434 -73
  23. package/dist/mcp-oauth.js +121 -0
  24. package/dist/memory/anti-patterns.js +7 -3
  25. package/dist/memory/auto-capture.js +7 -3
  26. package/dist/memory/context.js +92 -73
  27. package/dist/memory/project-context.js +24 -5
  28. package/dist/memory/provenance.js +15 -0
  29. package/dist/memory/review-triage.js +166 -0
  30. package/dist/memory/session.js +36 -1
  31. package/dist/models.js +152 -19
  32. package/dist/multi/child-entry.js +65 -10
  33. package/dist/multi/diff-renderer.js +3 -3
  34. package/dist/multi/markdown.js +125 -27
  35. package/dist/multi/provider-manager.js +8 -0
  36. package/dist/multi/publish.js +72 -0
  37. package/dist/multi/spawner.js +45 -4
  38. package/dist/multi/syntax-highlight.js +25 -4
  39. package/dist/multi/types.js +1 -1
  40. package/dist/permissions/allowlist.js +86 -13
  41. package/dist/permissions/checker.js +42 -7
  42. package/dist/permissions/kernel-sandbox.js +197 -0
  43. package/dist/permissions/prompt.js +10 -1
  44. package/dist/permissions/sandbox.js +40 -17
  45. package/dist/providers/anthropic.js +154 -7
  46. package/dist/providers/codex-auth.js +28 -0
  47. package/dist/providers/codex.js +134 -23
  48. package/dist/providers/history.js +35 -0
  49. package/dist/providers/ollama.js +37 -6
  50. package/dist/providers/openai-compat.js +171 -74
  51. package/dist/providers/openrouter.js +47 -17
  52. package/dist/providers/replay.js +62 -0
  53. package/dist/providers/resolve.js +41 -7
  54. package/dist/providers/retry.js +38 -5
  55. package/dist/providers/types.js +9 -1
  56. package/dist/repl.js +47 -5
  57. package/dist/session/log.js +217 -0
  58. package/dist/session/persist.js +190 -0
  59. package/dist/settings.js +42 -26
  60. package/dist/system-prompt.js +18 -2
  61. package/dist/tools/apply-patch.js +355 -0
  62. package/dist/tools/edit-engine.js +287 -0
  63. package/dist/tools/edit-file.js +86 -27
  64. package/dist/tools/grep.js +25 -10
  65. package/dist/tools/lint-test.js +0 -22
  66. package/dist/tools/mcp-resources.js +51 -6
  67. package/dist/tools/phren-finding.js +12 -4
  68. package/dist/tools/phren-search.js +7 -2
  69. package/dist/tools/read-file.js +37 -9
  70. package/dist/tools/read-image.js +76 -0
  71. package/dist/tools/registry.js +27 -5
  72. package/dist/tools/shell.js +134 -71
  73. package/dist/tools/skill.js +20 -20
  74. package/dist/tools/spawn-agent.js +77 -13
  75. package/dist/tools/update-plan.js +48 -0
  76. package/dist/tools/web-fetch.js +110 -12
  77. package/dist/tools/web-search.js +4 -4
  78. package/dist/tui/components/App.js +98 -26
  79. package/dist/tui/components/ApprovalPanel.js +8 -0
  80. package/dist/tui/components/InputArea.js +5 -5
  81. package/dist/tui/components/ModelPicker.js +43 -0
  82. package/dist/tui/components/PhrenInput.js +10 -5
  83. package/dist/tui/components/PlanReview.js +7 -0
  84. package/dist/tui/components/ShortcutHelp.js +5 -0
  85. package/dist/tui/components/StatusBar.js +46 -20
  86. package/dist/tui/components/SteerQueue.js +1 -1
  87. package/dist/tui/components/ThinkingIndicator.js +6 -4
  88. package/dist/tui/components/ToolCall.js +10 -4
  89. package/dist/tui/components/ToolDetail.js +21 -0
  90. package/dist/tui/help.js +6 -0
  91. package/dist/tui/hooks/useKeyboardShortcuts.js +15 -1
  92. package/dist/tui/index.js +0 -1
  93. package/dist/tui/ink-entry.js +317 -33
  94. package/dist/tui/tool-render.js +1 -1
  95. package/dist/user-hooks.js +171 -0
  96. package/package.json +18 -12
  97. package/dist/multi/multi-commands.js +0 -170
  98. package/dist/multi/multi-events.js +0 -98
  99. package/dist/multi/multi-render.js +0 -147
  100. package/dist/multi/pane.js +0 -28
  101. package/dist/multi/tui-multi.js +0 -200
  102. package/dist/permissions/privacy.js +0 -248
  103. package/dist/tui/components/AgentMessage.js +0 -5
  104. package/dist/tui/components/ChatMessage.js +0 -23
  105. package/dist/tui/components/CodeBlock.js +0 -8
  106. package/dist/tui/components/Separator.js +0 -7
  107. package/dist/tui/components/StreamingText.js +0 -5
  108. package/dist/tui/components/UserMessage.js +0 -5
  109. package/dist/tui/menu-mode.js +0 -86
  110. package/dist/tui/mouse.js +0 -71
  111. package/dist/tui/terminal-control.js +0 -62
  112. package/dist/tui.js +0 -781
@@ -1,8 +1,11 @@
1
+ import { toolResultText } from "./types.js";
1
2
  import { getAccessToken } from "./codex-auth.js";
2
- import { lookupMaxOutputTokens } from "../models.js";
3
+ import { stripForeignReasoning, IMAGE_OMITTED_MARKER } from "./history.js";
4
+ import { lookupContextWindow, lookupMaxOutputTokens, modelSupportsVision } from "../models.js";
3
5
  const CODEX_API = "https://chatgpt.com/backend-api/codex/responses";
4
- /** Convert our tool defs to Responses API tool format. */
5
- function toResponsesTools(tools) {
6
+ const PROVIDER_NAME = "openai-codex";
7
+ /** Convert our tool defs to Responses API tool format. Exported for tests. */
8
+ export function toResponsesTools(tools) {
6
9
  return tools.map((t) => ({
7
10
  type: "function",
8
11
  name: t.name,
@@ -10,10 +13,10 @@ function toResponsesTools(tools) {
10
13
  parameters: t.input_schema,
11
14
  }));
12
15
  }
13
- /** Convert our messages to Responses API input format. */
14
- function toResponsesInput(messages) {
16
+ /** Convert our messages to Responses API input format. Exported for tests. */
17
+ export function toResponsesInput(messages, vision = false) {
15
18
  const input = [];
16
- for (const msg of messages) {
19
+ for (const msg of stripForeignReasoning(messages, PROVIDER_NAME)) {
17
20
  if (msg.role === "user") {
18
21
  if (typeof msg.content === "string") {
19
22
  input.push({
@@ -29,7 +32,34 @@ function toResponsesInput(messages) {
29
32
  input.push({
30
33
  type: "function_call_output",
31
34
  call_id: block.tool_use_id,
32
- output: block.content,
35
+ output: toolResultText(block),
36
+ });
37
+ // Images cannot ride a function_call_output; they follow as a
38
+ // user message with input_image parts. Text-only models get a
39
+ // marker so durable history never produces an unsendable request.
40
+ if (Array.isArray(block.content)) {
41
+ const imageParts = block.content.filter((c) => c.type === "image");
42
+ if (imageParts.length > 0) {
43
+ input.push({
44
+ type: "message",
45
+ role: "user",
46
+ content: vision
47
+ ? imageParts.map((c) => (c.type === "image" ? {
48
+ type: "input_image",
49
+ image_url: `data:${c.source.media_type};base64,${c.source.data}`,
50
+ } : { type: "input_text", text: "" }))
51
+ : [{ type: "input_text", text: IMAGE_OMITTED_MARKER }],
52
+ });
53
+ }
54
+ }
55
+ }
56
+ else if (block.type === "image") {
57
+ input.push({
58
+ type: "message",
59
+ role: "user",
60
+ content: vision
61
+ ? [{ type: "input_image", image_url: `data:${block.source.media_type};base64,${block.source.data}` }]
62
+ : [{ type: "input_text", text: IMAGE_OMITTED_MARKER }],
33
63
  });
34
64
  }
35
65
  else if (block.type === "text") {
@@ -52,7 +82,21 @@ function toResponsesInput(messages) {
52
82
  }
53
83
  else {
54
84
  for (const block of msg.content) {
55
- if (block.type === "text") {
85
+ if (block.type === "reasoning") {
86
+ // Round-trip: with store:false the encrypted payload is the only
87
+ // way the model recovers its prior chain of thought. Content
88
+ // order already places reasoning before its sibling
89
+ // function_call, which the API requires.
90
+ if (block.id && block.encrypted_content) {
91
+ input.push({
92
+ type: "reasoning",
93
+ id: block.id,
94
+ encrypted_content: block.encrypted_content,
95
+ summary: [],
96
+ });
97
+ }
98
+ }
99
+ else if (block.type === "text") {
56
100
  input.push({
57
101
  type: "message",
58
102
  role: "assistant",
@@ -79,8 +123,18 @@ function debugLog(label, data) {
79
123
  return;
80
124
  process.stderr.write(`[codex:debug] ${label}: ${JSON.stringify(data, null, 2)}\n`);
81
125
  }
82
- /** Parse non-streaming Responses API output into our ContentBlock format. */
83
- function parseResponsesOutput(data) {
126
+ /** Join a Responses reasoning item's summary parts into display text. */
127
+ function reasoningSummaryText(item) {
128
+ const summary = item.summary;
129
+ if (!Array.isArray(summary))
130
+ return "";
131
+ return summary
132
+ .map((part) => part?.text ?? "")
133
+ .filter(Boolean)
134
+ .join("\n");
135
+ }
136
+ /** Parse non-streaming Responses API output into our ContentBlock format. Exported for tests. */
137
+ export function parseResponsesOutput(data) {
84
138
  debugLog("parseResponsesOutput input", data);
85
139
  // The Responses API may return output at top-level or nested under a "response" key.
86
140
  // Handle both shapes defensively.
@@ -124,8 +178,16 @@ function parseResponsesOutput(data) {
124
178
  });
125
179
  }
126
180
  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 });
181
+ // Keep reasoning: the id + encrypted_content pair must be re-sent on
182
+ // the next request or the model loses its chain of thought mid-task.
183
+ const encrypted = item.encrypted_content;
184
+ content.push({
185
+ type: "reasoning",
186
+ text: reasoningSummaryText(item),
187
+ provider: PROVIDER_NAME,
188
+ ...(typeof item.id === "string" ? { id: item.id } : {}),
189
+ ...(typeof encrypted === "string" ? { encrypted_content: encrypted } : {}),
190
+ });
129
191
  }
130
192
  }
131
193
  }
@@ -146,7 +208,7 @@ function parseResponsesOutput(data) {
146
208
  }
147
209
  export class CodexProvider {
148
210
  name = "openai-codex";
149
- contextWindow = 1_050_000;
211
+ contextWindow;
150
212
  maxOutputTokens;
151
213
  model;
152
214
  reasoningEffort;
@@ -154,15 +216,19 @@ export class CodexProvider {
154
216
  this.model = model ?? "gpt-5.4";
155
217
  this.maxOutputTokens = maxOutputTokens ?? lookupMaxOutputTokens(this.model, this.name);
156
218
  this.reasoningEffort = reasoningEffort;
219
+ this.contextWindow = lookupContextWindow(this.model, this.name);
157
220
  }
158
- async chat(system, messages, tools) {
221
+ async chat(system, messages, tools, signal) {
159
222
  const { accessToken } = await getAccessToken();
160
223
  const body = {
161
224
  model: this.model,
162
225
  instructions: system,
163
- input: toResponsesInput(messages),
226
+ input: toResponsesInput(messages, modelSupportsVision(this.name, this.model)),
164
227
  store: false,
165
228
  stream: true,
229
+ // Without this the API omits the encrypted payload and reasoning
230
+ // cannot round-trip (chatStream already requested it; chat() didn't).
231
+ include: ["reasoning.encrypted_content"],
166
232
  };
167
233
  if (this.reasoningEffort) {
168
234
  body.reasoning = { effort: this.reasoningEffort };
@@ -171,14 +237,14 @@ export class CodexProvider {
171
237
  body.tools = toResponsesTools(tools);
172
238
  body.tool_choice = "auto";
173
239
  }
174
- return parseResponsesOutput(await this.requestResponse(accessToken, body));
240
+ return parseResponsesOutput(await this.requestResponse(accessToken, body, signal));
175
241
  }
176
- async *chatStream(system, messages, tools) {
242
+ async *chatStream(system, messages, tools, signal) {
177
243
  const { accessToken } = await getAccessToken();
178
244
  const body = {
179
245
  model: this.model,
180
246
  instructions: system,
181
- input: toResponsesInput(messages),
247
+ input: toResponsesInput(messages, modelSupportsVision(this.name, this.model)),
182
248
  store: false,
183
249
  stream: true,
184
250
  include: ["reasoning.encrypted_content"],
@@ -193,13 +259,24 @@ export class CodexProvider {
193
259
  // OpenClaw treats transport as auto: try WebSocket first, then fall back to the
194
260
  // HTTP responses stream if the WS path is unavailable.
195
261
  try {
196
- yield* this.chatStreamWs(accessToken, body);
262
+ yield* this.chatStreamWs(accessToken, body, signal);
197
263
  }
198
264
  catch {
199
- const response = await this.requestResponse(accessToken, body);
265
+ const response = await this.requestResponse(accessToken, body, signal);
200
266
  const parsed = parseResponsesOutput(response);
201
267
  for (const block of parsed.content) {
202
- if (block.type === "text") {
268
+ if (block.type === "reasoning") {
269
+ if (block.text)
270
+ yield { type: "reasoning_delta", text: block.text };
271
+ yield {
272
+ type: "reasoning_end",
273
+ ...(block.id !== undefined ? { id: block.id } : {}),
274
+ ...(block.encrypted_content !== undefined
275
+ ? { encrypted_content: block.encrypted_content }
276
+ : {}),
277
+ };
278
+ }
279
+ else if (block.type === "text") {
203
280
  yield { type: "text_delta", text: block.text };
204
281
  }
205
282
  else if (block.type === "tool_use") {
@@ -211,7 +288,7 @@ export class CodexProvider {
211
288
  yield { type: "done", stop_reason: parsed.stop_reason, usage: parsed.usage };
212
289
  }
213
290
  }
214
- async requestResponse(accessToken, body) {
291
+ async requestResponse(accessToken, body, signal) {
215
292
  const res = await fetch(CODEX_API, {
216
293
  method: "POST",
217
294
  headers: {
@@ -219,6 +296,7 @@ export class CodexProvider {
219
296
  Authorization: `Bearer ${accessToken}`,
220
297
  },
221
298
  body: JSON.stringify(body),
299
+ signal,
222
300
  });
223
301
  if (!res.ok) {
224
302
  const text = await res.text();
@@ -274,7 +352,7 @@ export class CodexProvider {
274
352
  return finalResponse;
275
353
  }
276
354
  /** WebSocket streaming — sends request, yields deltas as they arrive. */
277
- async *chatStreamWs(accessToken, body) {
355
+ async *chatStreamWs(accessToken, body, signal) {
278
356
  const wsUrl = CODEX_API.replace(/^https:/, "wss:").replace(/^http:/, "ws:");
279
357
  // Queue for events received from the WebSocket before the consumer pulls them
280
358
  const queue = [];
@@ -328,6 +406,24 @@ export class CodexProvider {
328
406
  if (delta)
329
407
  push({ type: "text_delta", text: delta });
330
408
  }
409
+ else if (type === "response.reasoning_summary_text.delta") {
410
+ const delta = event.delta;
411
+ if (delta)
412
+ push({ type: "reasoning_delta", text: delta });
413
+ }
414
+ else if (type === "response.output_item.done") {
415
+ // The completed reasoning item carries the encrypted payload we must
416
+ // re-send on the next request.
417
+ const item = event.item;
418
+ if (item?.type === "reasoning") {
419
+ const encrypted = item.encrypted_content;
420
+ push({
421
+ type: "reasoning_end",
422
+ ...(typeof item.id === "string" ? { id: item.id } : {}),
423
+ ...(typeof encrypted === "string" ? { encrypted_content: encrypted } : {}),
424
+ });
425
+ }
426
+ }
331
427
  else if (type === "response.output_item.added") {
332
428
  if (event.item?.type === "function_call") {
333
429
  const item = event.item;
@@ -370,6 +466,20 @@ export class CodexProvider {
370
466
  done = true;
371
467
  }
372
468
  });
469
+ const onAbort = () => {
470
+ done = true;
471
+ try {
472
+ ws.close();
473
+ }
474
+ catch { /* ignore */ }
475
+ if (resolve) {
476
+ resolve();
477
+ resolve = null;
478
+ }
479
+ };
480
+ signal?.addEventListener("abort", onAbort);
481
+ if (signal?.aborted)
482
+ onAbort();
373
483
  // Async iteration: drain the queue, wait for new events
374
484
  try {
375
485
  while (true) {
@@ -387,6 +497,7 @@ export class CodexProvider {
387
497
  }
388
498
  }
389
499
  finally {
500
+ signal?.removeEventListener("abort", onAbort);
390
501
  if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
391
502
  try {
392
503
  ws.close();
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Drop reasoning blocks that did not originate from `providerName`.
3
+ *
4
+ * A reasoning block carries provider-private round-trip material (Anthropic
5
+ * signatures, Codex encrypted_content). Sending it to a different provider —
6
+ * or to the same provider under a different account/model in some cases —
7
+ * fails the request, and because history is durable that failure would repeat
8
+ * on every subsequent turn. Text-only reasoning from another provider is
9
+ * dropped too: it was that model's private scratchpad, not conversation.
10
+ *
11
+ * Untagged reasoning blocks (no `provider` field) are also dropped — they
12
+ * predate tagging (e.g. Anthropic thinking blocks that leaked through the
13
+ * batch path's unchecked cast) and cannot be trusted to round-trip.
14
+ */
15
+ export function stripForeignReasoning(messages, providerName) {
16
+ return messages.map((m) => {
17
+ if (!Array.isArray(m.content))
18
+ return m;
19
+ if (!m.content.some((b) => isForeignReasoning(b, providerName)))
20
+ return m;
21
+ return { ...m, content: m.content.filter((b) => !isForeignReasoning(b, providerName)) };
22
+ });
23
+ }
24
+ /** With no provider name, every reasoning block is foreign (strip all). */
25
+ function isForeignReasoning(block, providerName) {
26
+ return block.type === "reasoning" && (providerName === undefined || block.provider !== providerName);
27
+ }
28
+ /** Reasoning blocks of a message, in order (empty for string content). */
29
+ export function reasoningBlocks(message) {
30
+ if (!Array.isArray(message.content))
31
+ return [];
32
+ return message.content.filter((b) => b.type === "reasoning");
33
+ }
34
+ /** Marker substituted for image content when the active model is text-only. */
35
+ export const IMAGE_OMITTED_MARKER = "[image omitted: the active model does not accept image input]";
@@ -1,3 +1,7 @@
1
+ import { toolResultText } from "./types.js";
2
+ import { stripForeignReasoning, IMAGE_OMITTED_MARKER } from "./history.js";
3
+ import { lookupContextWindow } from "../models.js";
4
+ const PROVIDER_NAME = "ollama";
1
5
  /** Convert Anthropic tool defs to OpenAI function format (Ollama uses OpenAI-compat). */
2
6
  function toOllamaTools(tools) {
3
7
  return tools.map((t) => ({
@@ -7,7 +11,9 @@ function toOllamaTools(tools) {
7
11
  }
8
12
  function toOllamaMessages(system, messages) {
9
13
  const out = [{ role: "system", content: system }];
10
- for (const msg of messages) {
14
+ // Reasoning is display-only for Ollama: local models regenerate their own
15
+ // thinking each turn, so history drops it entirely (own and foreign).
16
+ for (const msg of stripForeignReasoning(messages, undefined)) {
11
17
  if (typeof msg.content === "string") {
12
18
  out.push({ role: msg.role, content: msg.content });
13
19
  }
@@ -17,7 +23,15 @@ function toOllamaMessages(system, messages) {
17
23
  out.push({ role: msg.role, content: block.text });
18
24
  }
19
25
  else if (block.type === "tool_result") {
20
- out.push({ role: "tool", tool_call_id: block.tool_use_id, content: block.content });
26
+ // Text only: local models are text-only in this integration, so
27
+ // image parts degrade to a marker rather than an unsendable body.
28
+ const text = toolResultText(block);
29
+ const hasImages = Array.isArray(block.content) && block.content.some((c) => c.type === "image");
30
+ out.push({
31
+ role: "tool",
32
+ tool_call_id: block.tool_use_id,
33
+ content: hasImages ? `${text}\n${IMAGE_OMITTED_MARKER}` : text,
34
+ });
21
35
  }
22
36
  else if (block.type === "tool_use") {
23
37
  out.push({
@@ -31,8 +45,8 @@ function toOllamaMessages(system, messages) {
31
45
  return out;
32
46
  }
33
47
  export class OllamaProvider {
34
- name = "ollama";
35
- contextWindow = 32_000;
48
+ name = PROVIDER_NAME;
49
+ contextWindow;
36
50
  maxOutputTokens;
37
51
  baseUrl;
38
52
  model;
@@ -40,20 +54,28 @@ export class OllamaProvider {
40
54
  this.baseUrl = baseUrl ?? "http://localhost:11434";
41
55
  this.model = model ?? "qwen2.5-coder:14b";
42
56
  this.maxOutputTokens = maxOutputTokens ?? 8192;
57
+ this.contextWindow = lookupContextWindow(this.model, this.name);
58
+ }
59
+ /** Thinking models (deepseek-r1 etc.) need think:true to separate reasoning from answer. */
60
+ supportsThinking() {
61
+ return /deepseek-r1|qwen3|gpt-oss/i.test(this.model);
43
62
  }
44
- async chat(system, messages, tools) {
63
+ async chat(system, messages, tools, signal) {
45
64
  const body = {
46
65
  model: this.model,
47
66
  messages: toOllamaMessages(system, messages),
48
67
  options: { num_predict: this.maxOutputTokens },
49
68
  stream: false,
50
69
  };
70
+ if (this.supportsThinking())
71
+ body.think = true;
51
72
  if (tools.length > 0)
52
73
  body.tools = toOllamaTools(tools);
53
74
  const res = await fetch(`${this.baseUrl}/api/chat`, {
54
75
  method: "POST",
55
76
  headers: { "Content-Type": "application/json" },
56
77
  body: JSON.stringify(body),
78
+ signal,
57
79
  });
58
80
  if (!res.ok) {
59
81
  const text = await res.text();
@@ -62,6 +84,9 @@ export class OllamaProvider {
62
84
  const data = await res.json();
63
85
  const message = data.message;
64
86
  const content = [];
87
+ if (message?.thinking && typeof message.thinking === "string") {
88
+ content.push({ type: "reasoning", text: message.thinking, provider: PROVIDER_NAME });
89
+ }
65
90
  if (message?.content && typeof message.content === "string") {
66
91
  content.push({ type: "text", text: message.content });
67
92
  }
@@ -80,19 +105,22 @@ export class OllamaProvider {
80
105
  const stop_reason = toolCalls && toolCalls.length > 0 ? "tool_use" : "end_turn";
81
106
  return { content, stop_reason };
82
107
  }
83
- async *chatStream(system, messages, tools) {
108
+ async *chatStream(system, messages, tools, signal) {
84
109
  const body = {
85
110
  model: this.model,
86
111
  messages: toOllamaMessages(system, messages),
87
112
  options: { num_predict: this.maxOutputTokens },
88
113
  stream: true,
89
114
  };
115
+ if (this.supportsThinking())
116
+ body.think = true;
90
117
  if (tools.length > 0)
91
118
  body.tools = toOllamaTools(tools);
92
119
  const res = await fetch(`${this.baseUrl}/api/chat`, {
93
120
  method: "POST",
94
121
  headers: { "Content-Type": "application/json" },
95
122
  body: JSON.stringify(body),
123
+ signal,
96
124
  });
97
125
  if (!res.ok) {
98
126
  const text = await res.text();
@@ -123,6 +151,9 @@ export class OllamaProvider {
123
151
  continue;
124
152
  }
125
153
  const message = chunk.message;
154
+ if (message?.thinking && typeof message.thinking === "string") {
155
+ yield { type: "reasoning_delta", text: message.thinking };
156
+ }
126
157
  if (message?.content && typeof message.content === "string") {
127
158
  yield { type: "text_delta", text: message.content };
128
159
  }