@jackchen_me/open-multi-agent 0.2.0 → 1.0.0

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 (104) hide show
  1. package/.github/workflows/ci.yml +1 -1
  2. package/CLAUDE.md +11 -3
  3. package/README.md +87 -20
  4. package/README_zh.md +85 -25
  5. package/dist/agent/agent.d.ts +15 -1
  6. package/dist/agent/agent.d.ts.map +1 -1
  7. package/dist/agent/agent.js +144 -10
  8. package/dist/agent/agent.js.map +1 -1
  9. package/dist/agent/loop-detector.d.ts +39 -0
  10. package/dist/agent/loop-detector.d.ts.map +1 -0
  11. package/dist/agent/loop-detector.js +122 -0
  12. package/dist/agent/loop-detector.js.map +1 -0
  13. package/dist/agent/pool.d.ts +2 -1
  14. package/dist/agent/pool.d.ts.map +1 -1
  15. package/dist/agent/pool.js +4 -2
  16. package/dist/agent/pool.js.map +1 -1
  17. package/dist/agent/runner.d.ts +23 -1
  18. package/dist/agent/runner.d.ts.map +1 -1
  19. package/dist/agent/runner.js +113 -12
  20. package/dist/agent/runner.js.map +1 -1
  21. package/dist/index.d.ts +3 -1
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +2 -0
  24. package/dist/index.js.map +1 -1
  25. package/dist/llm/adapter.d.ts +4 -1
  26. package/dist/llm/adapter.d.ts.map +1 -1
  27. package/dist/llm/adapter.js +11 -0
  28. package/dist/llm/adapter.js.map +1 -1
  29. package/dist/llm/copilot.d.ts.map +1 -1
  30. package/dist/llm/copilot.js +2 -1
  31. package/dist/llm/copilot.js.map +1 -1
  32. package/dist/llm/gemini.d.ts +65 -0
  33. package/dist/llm/gemini.d.ts.map +1 -0
  34. package/dist/llm/gemini.js +317 -0
  35. package/dist/llm/gemini.js.map +1 -0
  36. package/dist/llm/grok.d.ts +21 -0
  37. package/dist/llm/grok.d.ts.map +1 -0
  38. package/dist/llm/grok.js +24 -0
  39. package/dist/llm/grok.js.map +1 -0
  40. package/dist/llm/openai-common.d.ts +8 -1
  41. package/dist/llm/openai-common.d.ts.map +1 -1
  42. package/dist/llm/openai-common.js +35 -2
  43. package/dist/llm/openai-common.js.map +1 -1
  44. package/dist/llm/openai.d.ts +1 -1
  45. package/dist/llm/openai.d.ts.map +1 -1
  46. package/dist/llm/openai.js +20 -2
  47. package/dist/llm/openai.js.map +1 -1
  48. package/dist/orchestrator/orchestrator.d.ts.map +1 -1
  49. package/dist/orchestrator/orchestrator.js +89 -9
  50. package/dist/orchestrator/orchestrator.js.map +1 -1
  51. package/dist/task/queue.d.ts +31 -2
  52. package/dist/task/queue.d.ts.map +1 -1
  53. package/dist/task/queue.js +69 -2
  54. package/dist/task/queue.js.map +1 -1
  55. package/dist/tool/text-tool-extractor.d.ts +32 -0
  56. package/dist/tool/text-tool-extractor.d.ts.map +1 -0
  57. package/dist/tool/text-tool-extractor.js +187 -0
  58. package/dist/tool/text-tool-extractor.js.map +1 -0
  59. package/dist/types.d.ts +139 -7
  60. package/dist/types.d.ts.map +1 -1
  61. package/dist/utils/trace.d.ts +12 -0
  62. package/dist/utils/trace.d.ts.map +1 -0
  63. package/dist/utils/trace.js +30 -0
  64. package/dist/utils/trace.js.map +1 -0
  65. package/examples/06-local-model.ts +1 -0
  66. package/examples/08-gemma4-local.ts +76 -87
  67. package/examples/09-structured-output.ts +73 -0
  68. package/examples/10-task-retry.ts +132 -0
  69. package/examples/11-trace-observability.ts +133 -0
  70. package/examples/12-grok.ts +154 -0
  71. package/examples/13-gemini.ts +48 -0
  72. package/package.json +11 -1
  73. package/src/agent/agent.ts +159 -10
  74. package/src/agent/loop-detector.ts +137 -0
  75. package/src/agent/pool.ts +9 -2
  76. package/src/agent/runner.ts +148 -19
  77. package/src/index.ts +15 -0
  78. package/src/llm/adapter.ts +12 -1
  79. package/src/llm/copilot.ts +2 -1
  80. package/src/llm/gemini.ts +378 -0
  81. package/src/llm/grok.ts +29 -0
  82. package/src/llm/openai-common.ts +41 -2
  83. package/src/llm/openai.ts +23 -3
  84. package/src/orchestrator/orchestrator.ts +105 -11
  85. package/src/task/queue.ts +73 -3
  86. package/src/tool/text-tool-extractor.ts +219 -0
  87. package/src/types.ts +157 -6
  88. package/src/utils/trace.ts +34 -0
  89. package/tests/agent-hooks.test.ts +473 -0
  90. package/tests/agent-pool.test.ts +212 -0
  91. package/tests/approval.test.ts +464 -0
  92. package/tests/built-in-tools.test.ts +393 -0
  93. package/tests/gemini-adapter.test.ts +97 -0
  94. package/tests/grok-adapter.test.ts +74 -0
  95. package/tests/llm-adapters.test.ts +357 -0
  96. package/tests/loop-detection.test.ts +456 -0
  97. package/tests/openai-fallback.test.ts +159 -0
  98. package/tests/orchestrator.test.ts +281 -0
  99. package/tests/scheduler.test.ts +221 -0
  100. package/tests/team-messaging.test.ts +329 -0
  101. package/tests/text-tool-extractor.test.ts +170 -0
  102. package/tests/trace.test.ts +453 -0
  103. package/vitest.config.ts +9 -0
  104. package/examples/09-gemma4-auto-orchestration.ts +0 -162
@@ -0,0 +1,378 @@
1
+ /**
2
+ * @fileoverview Google Gemini adapter implementing {@link LLMAdapter}.
3
+ *
4
+ * Built for `@google/genai` (the unified Google Gen AI SDK, v1.x), NOT the
5
+ * legacy `@google/generative-ai` package.
6
+ *
7
+ * Converts between the framework's internal {@link ContentBlock} types and the
8
+ * `@google/genai` SDK's wire format, handling tool definitions, system prompts,
9
+ * and both batch and streaming response paths.
10
+ *
11
+ * API key resolution order:
12
+ * 1. `apiKey` constructor argument
13
+ * 2. `GEMINI_API_KEY` environment variable
14
+ * 3. `GOOGLE_API_KEY` environment variable
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { GeminiAdapter } from './gemini.js'
19
+ *
20
+ * const adapter = new GeminiAdapter()
21
+ * const response = await adapter.chat(messages, {
22
+ * model: 'gemini-2.5-flash',
23
+ * maxTokens: 1024,
24
+ * })
25
+ * ```
26
+ */
27
+
28
+ import {
29
+ GoogleGenAI,
30
+ FunctionCallingConfigMode,
31
+ type Content,
32
+ type FunctionDeclaration,
33
+ type GenerateContentConfig,
34
+ type GenerateContentResponse,
35
+ type Part,
36
+ type Tool as GeminiTool,
37
+ } from '@google/genai'
38
+
39
+ import type {
40
+ ContentBlock,
41
+ LLMAdapter,
42
+ LLMChatOptions,
43
+ LLMMessage,
44
+ LLMResponse,
45
+ LLMStreamOptions,
46
+ LLMToolDef,
47
+ StreamEvent,
48
+ ToolUseBlock,
49
+ } from '../types.js'
50
+
51
+ // ---------------------------------------------------------------------------
52
+ // Internal helpers
53
+ // ---------------------------------------------------------------------------
54
+
55
+ /**
56
+ * Map framework role names to Gemini role names.
57
+ *
58
+ * Gemini uses `"model"` instead of `"assistant"`.
59
+ */
60
+ function toGeminiRole(role: 'user' | 'assistant'): string {
61
+ return role === 'assistant' ? 'model' : 'user'
62
+ }
63
+
64
+ /**
65
+ * Convert framework messages into Gemini's {@link Content}[] format.
66
+ *
67
+ * Key differences from Anthropic:
68
+ * - Gemini uses `"model"` instead of `"assistant"`.
69
+ * - `functionResponse` parts (tool results) must appear in `"user"` turns.
70
+ * - `functionCall` parts appear in `"model"` turns.
71
+ * - We build a name lookup map from tool_use blocks so tool_result blocks
72
+ * can resolve the function name required by Gemini's `functionResponse`.
73
+ */
74
+ function toGeminiContents(messages: LLMMessage[]): Content[] {
75
+ // First pass: build id → name map for resolving tool results.
76
+ const toolNameById = new Map<string, string>()
77
+ for (const msg of messages) {
78
+ for (const block of msg.content) {
79
+ if (block.type === 'tool_use') {
80
+ toolNameById.set(block.id, block.name)
81
+ }
82
+ }
83
+ }
84
+
85
+ return messages.map((msg): Content => {
86
+ const parts: Part[] = msg.content.map((block): Part => {
87
+ switch (block.type) {
88
+ case 'text':
89
+ return { text: block.text }
90
+
91
+ case 'tool_use':
92
+ return {
93
+ functionCall: {
94
+ id: block.id,
95
+ name: block.name,
96
+ args: block.input,
97
+ },
98
+ }
99
+
100
+ case 'tool_result': {
101
+ const name = toolNameById.get(block.tool_use_id) ?? block.tool_use_id
102
+ return {
103
+ functionResponse: {
104
+ id: block.tool_use_id,
105
+ name,
106
+ response: {
107
+ content:
108
+ typeof block.content === 'string'
109
+ ? block.content
110
+ : JSON.stringify(block.content),
111
+ isError: block.is_error ?? false,
112
+ },
113
+ },
114
+ }
115
+ }
116
+
117
+ case 'image':
118
+ return {
119
+ inlineData: {
120
+ mimeType: block.source.media_type,
121
+ data: block.source.data,
122
+ },
123
+ }
124
+
125
+ default: {
126
+ const _exhaustive: never = block
127
+ throw new Error(`Unhandled content block type: ${JSON.stringify(_exhaustive)}`)
128
+ }
129
+ }
130
+ })
131
+
132
+ return { role: toGeminiRole(msg.role), parts }
133
+ })
134
+ }
135
+
136
+ /**
137
+ * Convert framework {@link LLMToolDef}s into a Gemini `tools` config array.
138
+ *
139
+ * In `@google/genai`, function declarations use `parametersJsonSchema` (not
140
+ * `parameters` or `input_schema`). All declarations are grouped under a single
141
+ * tool entry.
142
+ */
143
+ function toGeminiTools(tools: readonly LLMToolDef[]): GeminiTool[] {
144
+ const functionDeclarations: FunctionDeclaration[] = tools.map((t) => ({
145
+ name: t.name,
146
+ description: t.description,
147
+ parametersJsonSchema: t.inputSchema as Record<string, unknown>,
148
+ }))
149
+ return [{ functionDeclarations }]
150
+ }
151
+
152
+ /**
153
+ * Build the {@link GenerateContentConfig} shared by chat() and stream().
154
+ */
155
+ function buildConfig(
156
+ options: LLMChatOptions | LLMStreamOptions,
157
+ ): GenerateContentConfig {
158
+ return {
159
+ maxOutputTokens: options.maxTokens ?? 4096,
160
+ temperature: options.temperature,
161
+ systemInstruction: options.systemPrompt,
162
+ tools: options.tools ? toGeminiTools(options.tools) : undefined,
163
+ toolConfig: options.tools
164
+ ? { functionCallingConfig: { mode: FunctionCallingConfigMode.AUTO } }
165
+ : undefined,
166
+ }
167
+ }
168
+
169
+ /**
170
+ * Generate a stable pseudo-random ID string for tool use blocks.
171
+ *
172
+ * Gemini may not always return call IDs (especially in streaming), so we
173
+ * fabricate them when absent to satisfy the framework's {@link ToolUseBlock}
174
+ * contract.
175
+ */
176
+ function generateId(): string {
177
+ return `gemini-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`
178
+ }
179
+
180
+ /**
181
+ * Extract the function call ID from a Gemini part, or generate one.
182
+ *
183
+ * The `id` field exists in newer API versions but may be absent in older
184
+ * responses, so we cast conservatively and fall back to a generated ID.
185
+ */
186
+ function getFunctionCallId(part: Part): string {
187
+ return (part.functionCall as { id?: string } | undefined)?.id ?? generateId()
188
+ }
189
+
190
+ /**
191
+ * Convert a Gemini {@link GenerateContentResponse} into a framework
192
+ * {@link LLMResponse}.
193
+ */
194
+ function fromGeminiResponse(
195
+ response: GenerateContentResponse,
196
+ id: string,
197
+ model: string,
198
+ ): LLMResponse {
199
+ const candidate = response.candidates?.[0]
200
+ const content: ContentBlock[] = []
201
+
202
+ for (const part of candidate?.content?.parts ?? []) {
203
+ if (part.text !== undefined && part.text !== '') {
204
+ content.push({ type: 'text', text: part.text })
205
+ } else if (part.functionCall !== undefined) {
206
+ content.push({
207
+ type: 'tool_use',
208
+ id: getFunctionCallId(part),
209
+ name: part.functionCall.name ?? '',
210
+ input: (part.functionCall.args ?? {}) as Record<string, unknown>,
211
+ })
212
+ }
213
+ // inlineData echoes and other part types are silently ignored.
214
+ }
215
+
216
+ // Map Gemini finish reasons to framework stop_reason vocabulary.
217
+ const finishReason = candidate?.finishReason as string | undefined
218
+ let stop_reason: LLMResponse['stop_reason'] = 'end_turn'
219
+ if (finishReason === 'MAX_TOKENS') {
220
+ stop_reason = 'max_tokens'
221
+ } else if (content.some((b) => b.type === 'tool_use')) {
222
+ // Gemini may report STOP even when it returned function calls.
223
+ stop_reason = 'tool_use'
224
+ }
225
+
226
+ const usage = response.usageMetadata
227
+ return {
228
+ id,
229
+ content,
230
+ model,
231
+ stop_reason,
232
+ usage: {
233
+ input_tokens: usage?.promptTokenCount ?? 0,
234
+ output_tokens: usage?.candidatesTokenCount ?? 0,
235
+ },
236
+ }
237
+ }
238
+
239
+ // ---------------------------------------------------------------------------
240
+ // Adapter implementation
241
+ // ---------------------------------------------------------------------------
242
+
243
+ /**
244
+ * LLM adapter backed by the Google Gemini API via `@google/genai`.
245
+ *
246
+ * Thread-safe — a single instance may be shared across concurrent agent runs.
247
+ * The underlying SDK client is stateless across requests.
248
+ */
249
+ export class GeminiAdapter implements LLMAdapter {
250
+ readonly name = 'gemini'
251
+
252
+ readonly #client: GoogleGenAI
253
+
254
+ constructor(apiKey?: string) {
255
+ this.#client = new GoogleGenAI({
256
+ apiKey: apiKey ?? process.env['GEMINI_API_KEY'] ?? process.env['GOOGLE_API_KEY'],
257
+ })
258
+ }
259
+
260
+ // -------------------------------------------------------------------------
261
+ // chat()
262
+ // -------------------------------------------------------------------------
263
+
264
+ /**
265
+ * Send a synchronous (non-streaming) chat request and return the complete
266
+ * {@link LLMResponse}.
267
+ *
268
+ * Uses `ai.models.generateContent()` with the full conversation as `contents`,
269
+ * which is the idiomatic pattern for `@google/genai`.
270
+ */
271
+ async chat(messages: LLMMessage[], options: LLMChatOptions): Promise<LLMResponse> {
272
+ const id = generateId()
273
+ const contents = toGeminiContents(messages)
274
+
275
+ const response = await this.#client.models.generateContent({
276
+ model: options.model,
277
+ contents,
278
+ config: buildConfig(options),
279
+ })
280
+
281
+ return fromGeminiResponse(response, id, options.model)
282
+ }
283
+
284
+ // -------------------------------------------------------------------------
285
+ // stream()
286
+ // -------------------------------------------------------------------------
287
+
288
+ /**
289
+ * Send a streaming chat request and yield {@link StreamEvent}s as they
290
+ * arrive from the API.
291
+ *
292
+ * Uses `ai.models.generateContentStream()` which returns an
293
+ * `AsyncGenerator<GenerateContentResponse>`. Each yielded chunk has the same
294
+ * shape as a full response but contains only the delta for that chunk.
295
+ *
296
+ * Because `@google/genai` doesn't expose a `finalMessage()` helper like the
297
+ * Anthropic SDK, we accumulate content and token counts as we stream so that
298
+ * the terminal `done` event carries a complete and accurate {@link LLMResponse}.
299
+ *
300
+ * Sequence guarantees (matching the Anthropic adapter):
301
+ * - Zero or more `text` events with incremental deltas
302
+ * - Zero or more `tool_use` events (one per call; Gemini doesn't stream args)
303
+ * - Exactly one terminal event: `done` or `error`
304
+ */
305
+ async *stream(
306
+ messages: LLMMessage[],
307
+ options: LLMStreamOptions,
308
+ ): AsyncIterable<StreamEvent> {
309
+ const id = generateId()
310
+ const contents = toGeminiContents(messages)
311
+
312
+ try {
313
+ const streamResponse = await this.#client.models.generateContentStream({
314
+ model: options.model,
315
+ contents,
316
+ config: buildConfig(options),
317
+ })
318
+
319
+ // Accumulators for building the done payload.
320
+ const accumulatedContent: ContentBlock[] = []
321
+ let inputTokens = 0
322
+ let outputTokens = 0
323
+ let lastFinishReason: string | undefined
324
+
325
+ for await (const chunk of streamResponse) {
326
+ const candidate = chunk.candidates?.[0]
327
+
328
+ // Accumulate token counts — the API emits these on the final chunk.
329
+ if (chunk.usageMetadata) {
330
+ inputTokens = chunk.usageMetadata.promptTokenCount ?? inputTokens
331
+ outputTokens = chunk.usageMetadata.candidatesTokenCount ?? outputTokens
332
+ }
333
+ if (candidate?.finishReason) {
334
+ lastFinishReason = candidate.finishReason as string
335
+ }
336
+
337
+ for (const part of candidate?.content?.parts ?? []) {
338
+ if (part.text) {
339
+ accumulatedContent.push({ type: 'text', text: part.text })
340
+ yield { type: 'text', data: part.text } satisfies StreamEvent
341
+ } else if (part.functionCall) {
342
+ const toolId = getFunctionCallId(part)
343
+ const toolUseBlock: ToolUseBlock = {
344
+ type: 'tool_use',
345
+ id: toolId,
346
+ name: part.functionCall.name ?? '',
347
+ input: (part.functionCall.args ?? {}) as Record<string, unknown>,
348
+ }
349
+ accumulatedContent.push(toolUseBlock)
350
+ yield { type: 'tool_use', data: toolUseBlock } satisfies StreamEvent
351
+ }
352
+ }
353
+ }
354
+
355
+ // Determine stop_reason from the accumulated response.
356
+ const hasToolUse = accumulatedContent.some((b) => b.type === 'tool_use')
357
+ let stop_reason: LLMResponse['stop_reason'] = 'end_turn'
358
+ if (lastFinishReason === 'MAX_TOKENS') {
359
+ stop_reason = 'max_tokens'
360
+ } else if (hasToolUse) {
361
+ stop_reason = 'tool_use'
362
+ }
363
+
364
+ const finalResponse: LLMResponse = {
365
+ id,
366
+ content: accumulatedContent,
367
+ model: options.model,
368
+ stop_reason,
369
+ usage: { input_tokens: inputTokens, output_tokens: outputTokens },
370
+ }
371
+
372
+ yield { type: 'done', data: finalResponse } satisfies StreamEvent
373
+ } catch (err) {
374
+ const error = err instanceof Error ? err : new Error(String(err))
375
+ yield { type: 'error', data: error } satisfies StreamEvent
376
+ }
377
+ }
378
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @fileoverview Grok (xAI) adapter.
3
+ *
4
+ * Thin wrapper around OpenAIAdapter that hard-codes the official xAI endpoint
5
+ * and XAI_API_KEY environment variable fallback.
6
+ */
7
+
8
+ import { OpenAIAdapter } from './openai.js'
9
+
10
+ /**
11
+ * LLM adapter for Grok models (grok-4 series and future models).
12
+ *
13
+ * Thread-safe. Can be shared across agents.
14
+ *
15
+ * Usage:
16
+ * provider: 'grok'
17
+ * model: 'grok-4' (or any current Grok model name)
18
+ */
19
+ export class GrokAdapter extends OpenAIAdapter {
20
+ readonly name = 'grok'
21
+
22
+ constructor(apiKey?: string, baseURL?: string) {
23
+ // Allow override of baseURL (for proxies or future changes) but default to official xAI endpoint.
24
+ super(
25
+ apiKey ?? process.env['XAI_API_KEY'],
26
+ baseURL ?? 'https://api.x.ai/v1'
27
+ )
28
+ }
29
+ }
@@ -25,6 +25,7 @@ import type {
25
25
  TextBlock,
26
26
  ToolUseBlock,
27
27
  } from '../types.js'
28
+ import { extractToolCallsFromText } from '../tool/text-tool-extractor.js'
28
29
 
29
30
  // ---------------------------------------------------------------------------
30
31
  // Framework → OpenAI
@@ -166,8 +167,18 @@ function toOpenAIAssistantMessage(msg: LLMMessage): ChatCompletionAssistantMessa
166
167
  *
167
168
  * Takes only the first choice (index 0), consistent with how the framework
168
169
  * is designed for single-output agents.
170
+ *
171
+ * @param completion - The raw OpenAI completion.
172
+ * @param knownToolNames - Optional whitelist of tool names. When the model
173
+ * returns no `tool_calls` but the text contains JSON
174
+ * that looks like a tool call, the fallback extractor
175
+ * uses this list to validate matches. Pass the names
176
+ * of tools sent in the request for best results.
169
177
  */
170
- export function fromOpenAICompletion(completion: ChatCompletion): LLMResponse {
178
+ export function fromOpenAICompletion(
179
+ completion: ChatCompletion,
180
+ knownToolNames?: string[],
181
+ ): LLMResponse {
171
182
  const choice = completion.choices[0]
172
183
  if (choice === undefined) {
173
184
  throw new Error('OpenAI returned a completion with no choices')
@@ -201,7 +212,35 @@ export function fromOpenAICompletion(completion: ChatCompletion): LLMResponse {
201
212
  content.push(toolUseBlock)
202
213
  }
203
214
 
204
- const stopReason = normalizeFinishReason(choice.finish_reason ?? 'stop')
215
+ // ---------------------------------------------------------------------------
216
+ // Fallback: extract tool calls from text when native tool_calls is empty.
217
+ //
218
+ // Some local models (Ollama thinking models, misconfigured vLLM) return tool
219
+ // calls as plain text instead of using the tool_calls wire format. When we
220
+ // have text but no tool_calls, try to extract them from the text.
221
+ // ---------------------------------------------------------------------------
222
+ const hasNativeToolCalls = (message.tool_calls ?? []).length > 0
223
+ if (
224
+ !hasNativeToolCalls &&
225
+ knownToolNames !== undefined &&
226
+ knownToolNames.length > 0 &&
227
+ message.content !== null &&
228
+ message.content !== undefined &&
229
+ message.content.length > 0
230
+ ) {
231
+ const extracted = extractToolCallsFromText(message.content, knownToolNames)
232
+ if (extracted.length > 0) {
233
+ content.push(...extracted)
234
+ }
235
+ }
236
+
237
+ const hasToolUseBlocks = content.some(b => b.type === 'tool_use')
238
+ const rawStopReason = choice.finish_reason ?? 'stop'
239
+ // If we extracted tool calls from text but the finish_reason was 'stop',
240
+ // correct it to 'tool_use' so the agent runner continues the loop.
241
+ const stopReason = hasToolUseBlocks && rawStopReason === 'stop'
242
+ ? 'tool_use'
243
+ : normalizeFinishReason(rawStopReason)
205
244
 
206
245
  return {
207
246
  id: completion.id,
package/src/llm/openai.ts CHANGED
@@ -54,6 +54,7 @@ import {
54
54
  normalizeFinishReason,
55
55
  buildOpenAIMessageList,
56
56
  } from './openai-common.js'
57
+ import { extractToolCallsFromText } from '../tool/text-tool-extractor.js'
57
58
 
58
59
  // ---------------------------------------------------------------------------
59
60
  // Adapter implementation
@@ -65,7 +66,7 @@ import {
65
66
  * Thread-safe — a single instance may be shared across concurrent agent runs.
66
67
  */
67
68
  export class OpenAIAdapter implements LLMAdapter {
68
- readonly name = 'openai'
69
+ readonly name: string = 'openai'
69
70
 
70
71
  readonly #client: OpenAI
71
72
 
@@ -104,7 +105,8 @@ export class OpenAIAdapter implements LLMAdapter {
104
105
  },
105
106
  )
106
107
 
107
- return fromOpenAICompletion(completion)
108
+ const toolNames = options.tools?.map(t => t.name)
109
+ return fromOpenAICompletion(completion, toolNames)
108
110
  }
109
111
 
110
112
  // -------------------------------------------------------------------------
@@ -241,11 +243,29 @@ export class OpenAIAdapter implements LLMAdapter {
241
243
  }
242
244
  doneContent.push(...finalToolUseBlocks)
243
245
 
246
+ // Fallback: extract tool calls from text when streaming produced no
247
+ // native tool_calls (same logic as fromOpenAICompletion).
248
+ if (finalToolUseBlocks.length === 0 && fullText.length > 0 && options.tools) {
249
+ const toolNames = options.tools.map(t => t.name)
250
+ const extracted = extractToolCallsFromText(fullText, toolNames)
251
+ if (extracted.length > 0) {
252
+ doneContent.push(...extracted)
253
+ for (const block of extracted) {
254
+ yield { type: 'tool_use', data: block } satisfies StreamEvent
255
+ }
256
+ }
257
+ }
258
+
259
+ const hasToolUseBlocks = doneContent.some(b => b.type === 'tool_use')
260
+ const resolvedStopReason = hasToolUseBlocks && finalFinishReason === 'stop'
261
+ ? 'tool_use'
262
+ : normalizeFinishReason(finalFinishReason)
263
+
244
264
  const finalResponse: LLMResponse = {
245
265
  id: completionId,
246
266
  content: doneContent,
247
267
  model: completionModel,
248
- stop_reason: normalizeFinishReason(finalFinishReason),
268
+ stop_reason: resolvedStopReason,
249
269
  usage: { input_tokens: inputTokens, output_tokens: outputTokens },
250
270
  }
251
271