@anvia/core 0.12.3 → 0.12.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 (65) hide show
  1. package/README.md +8 -2
  2. package/dist/agent/index.d.ts +11 -11
  3. package/dist/agent/index.js +6 -6
  4. package/dist/{agent-BBj3jvBi.d.ts → agent-BB9vVH6P.d.ts} +7 -7
  5. package/dist/audio-generation/index.d.ts +1 -1
  6. package/dist/{chunk-4NCRFQHS.js → chunk-5MUPA66B.js} +172 -86
  7. package/dist/chunk-5MUPA66B.js.map +1 -0
  8. package/dist/{chunk-SRBW3LD7.js → chunk-FUJ3POKH.js} +3 -3
  9. package/dist/chunk-FUJ3POKH.js.map +1 -0
  10. package/dist/{chunk-FBN6IEZA.js → chunk-JJ3JGL5D.js} +3 -3
  11. package/dist/{chunk-3CPOJSKK.js → chunk-N3I6HH2J.js} +4 -4
  12. package/dist/{chunk-LKLKEECY.js → chunk-PHCPUMAV.js} +33 -13
  13. package/dist/chunk-PHCPUMAV.js.map +1 -0
  14. package/dist/{chunk-5LFGEMIW.js → chunk-QVFOCNF6.js} +21 -4
  15. package/dist/chunk-QVFOCNF6.js.map +1 -0
  16. package/dist/{chunk-LLA7EW5W.js → chunk-TD6A4RYQ.js} +5 -5
  17. package/dist/{chunk-YVQBC3SJ.js → chunk-YZLOFKTT.js} +2 -2
  18. package/dist/completion/index.d.ts +3 -3
  19. package/dist/completion/index.js +2 -2
  20. package/dist/{create-completion-Y6YsYbQB.d.ts → create-completion-CC8-Ep3w.d.ts} +1 -1
  21. package/dist/evals/index.d.ts +8 -8
  22. package/dist/evals/index.js +7 -7
  23. package/dist/extractor/index.d.ts +8 -8
  24. package/dist/extractor/index.js +7 -7
  25. package/dist/guardrails/index.d.ts +1 -1
  26. package/dist/hooks/index.d.ts +3 -3
  27. package/dist/image-generation/index.d.ts +1 -1
  28. package/dist/{index-Oucb3Ljj.d.ts → index-CM-V4LS3.d.ts} +2 -2
  29. package/dist/index.d.ts +12 -12
  30. package/dist/index.js +7 -7
  31. package/dist/internal/agent.d.ts +8 -8
  32. package/dist/internal/agent.js +5 -5
  33. package/dist/loaders/index.d.ts +1 -1
  34. package/dist/mcp/index.d.ts +4 -4
  35. package/dist/memory/index.d.ts +3 -3
  36. package/dist/{middleware-ByyL6Rj0.d.ts → middleware-i-jLsLom.d.ts} +3 -3
  37. package/dist/observability/index.d.ts +3 -3
  38. package/dist/pipeline/index.d.ts +8 -8
  39. package/dist/request/index.d.ts +8 -8
  40. package/dist/request/index.js +4 -4
  41. package/dist/skills/index.d.ts +4 -4
  42. package/dist/skills/index.js +2 -2
  43. package/dist/{think-tool-B065hm73.d.ts → think-tool-BKDCOJh5.d.ts} +1 -1
  44. package/dist/tool/index.d.ts +5 -5
  45. package/dist/tool/index.js +2 -2
  46. package/dist/{tool-B8K543oQ.d.ts → tool-f2stpvUP.d.ts} +1 -1
  47. package/dist/transcription/index.d.ts +1 -1
  48. package/dist/{types-7F_IKQQW.d.ts → types-5JgWDgaM.d.ts} +2 -2
  49. package/dist/{types-q9G7Bvwl.d.ts → types-C15r7N_4.d.ts} +7 -4
  50. package/dist/{types-CGML74sE.d.ts → types-Dh7gfoGw.d.ts} +1 -1
  51. package/dist/{types-Pl4PFQWL.d.ts → types-DmvXANwl.d.ts} +1 -1
  52. package/dist/{types-BzUuhjKr.d.ts → types-b6fOVoW1.d.ts} +1 -1
  53. package/dist/{types-CqG6d7Ce.d.ts → types-bNDxj4Tt.d.ts} +1 -1
  54. package/dist/ui/index.d.ts +1 -1
  55. package/dist/ui/index.js +2 -2
  56. package/dist/vector-store/index.d.ts +4 -4
  57. package/package.json +1 -1
  58. package/dist/chunk-4NCRFQHS.js.map +0 -1
  59. package/dist/chunk-5LFGEMIW.js.map +0 -1
  60. package/dist/chunk-LKLKEECY.js.map +0 -1
  61. package/dist/chunk-SRBW3LD7.js.map +0 -1
  62. /package/dist/{chunk-FBN6IEZA.js.map → chunk-JJ3JGL5D.js.map} +0 -0
  63. /package/dist/{chunk-3CPOJSKK.js.map → chunk-N3I6HH2J.js.map} +0 -0
  64. /package/dist/{chunk-LLA7EW5W.js.map → chunk-TD6A4RYQ.js.map} +0 -0
  65. /package/dist/{chunk-YVQBC3SJ.js.map → chunk-YZLOFKTT.js.map} +0 -0
package/README.md CHANGED
@@ -1,8 +1,14 @@
1
1
  # @anvia/core
2
2
 
3
- Core runtime primitives for Anvia agents, tools, structured extraction, pipelines, streaming, RAG, MCP, skills, and observability.
3
+ Small, explicit, embeddable runtime contracts for Anvia agents, tools, structured extraction, pipelines, streaming, RAG, MCP, skills, and observability.
4
4
 
5
- This package is provider-neutral. Pair it with a provider adapter such as `@anvia/openai`, `@anvia/anthropic`, or `@anvia/gemini` to create runnable models.
5
+ This package is provider-neutral. Pair it with a provider adapter such as `@anvia/openai`, `@anvia/anthropic`, or `@anvia/gemini` to create runnable model objects, then pass those objects into agents, extractors, pipelines, or direct completion helpers.
6
+
7
+ ## Design Philosophy
8
+
9
+ `@anvia/core` owns the model/tool loop and the runtime contracts around it. Your application owns provider client construction, credentials, product data access, permissions, storage, deployment, observability backends, and response shape.
10
+
11
+ The package is dependency-injection oriented: create provider models, typed tools, memory stores, vector indexes, observers, and services in application code, then pass the relevant objects into agents, prompt requests, runners, or adapters. Core receives those objects and coordinates the run without taking over product architecture.
6
12
 
7
13
  ## Installation
8
14
 
@@ -1,16 +1,16 @@
1
- import { D as DynamicContextOptions, a as DynamicToolOptions, b as AgentEventStore, c as AgentEventStoreOptions, A as Agent } from '../agent-BBj3jvBi.js';
2
- export { d as AgentEventAppendInput, e as AgentEventRecord, f as AgentEventStoreInclude, g as AgentSession, h as AgentToolOptions } from '../agent-BBj3jvBi.js';
1
+ import { D as DynamicContextOptions, a as DynamicToolOptions, b as AgentEventStore, c as AgentEventStoreOptions, A as Agent } from '../agent-BB9vVH6P.js';
2
+ export { d as AgentEventAppendInput, e as AgentEventRecord, f as AgentEventStoreInclude, g as AgentSession, h as AgentToolOptions } from '../agent-BB9vVH6P.js';
3
3
  import { Z as ZodSchema } from '../zod-schema-C7F4clpm.js';
4
- import { C as CompletionModel, e as JsonValue, n as ToolChoice } from '../types-q9G7Bvwl.js';
4
+ import { C as CompletionModel, e as JsonValue, n as ToolChoice } from '../types-C15r7N_4.js';
5
5
  import { GuardrailPolicyInput } from '../guardrails/index.js';
6
- import { P as PromptHook } from '../types-CGML74sE.js';
7
- import { a as McpServer } from '../types-7F_IKQQW.js';
8
- import { M as MemoryStore, a as MemoryOptions } from '../types-CqG6d7Ce.js';
9
- import { b as AgentObserver, O as ObserveOptions } from '../index-Oucb3Ljj.js';
10
- import { a as SkillSet } from '../types-BzUuhjKr.js';
11
- import { k as ToolSearchDocument, l as ToolSet, A as AgentMiddleware, e as ToolMiddleware } from '../middleware-ByyL6Rj0.js';
12
- import { A as AnyTool, e as ToolApprovalsOptions } from '../tool-B8K543oQ.js';
13
- import { a as VectorSearchIndex } from '../types-Pl4PFQWL.js';
6
+ import { P as PromptHook } from '../types-Dh7gfoGw.js';
7
+ import { a as McpServer } from '../types-5JgWDgaM.js';
8
+ import { M as MemoryStore, a as MemoryOptions } from '../types-bNDxj4Tt.js';
9
+ import { b as AgentObserver, O as ObserveOptions } from '../index-CM-V4LS3.js';
10
+ import { a as SkillSet } from '../types-b6fOVoW1.js';
11
+ import { k as ToolSearchDocument, l as ToolSet, A as AgentMiddleware, e as ToolMiddleware } from '../middleware-i-jLsLom.js';
12
+ import { A as AnyTool, e as ToolApprovalsOptions } from '../tool-f2stpvUP.js';
13
+ import { a as VectorSearchIndex } from '../types-DmvXANwl.js';
14
14
  import 'zod';
15
15
  import '@modelcontextprotocol/sdk/client/sse.js';
16
16
  import '@modelcontextprotocol/sdk/client/stdio.js';
@@ -1,17 +1,17 @@
1
1
  import {
2
2
  AgentBuilder
3
- } from "../chunk-FBN6IEZA.js";
4
- import "../chunk-3CPOJSKK.js";
5
- import "../chunk-4NCRFQHS.js";
3
+ } from "../chunk-JJ3JGL5D.js";
4
+ import "../chunk-N3I6HH2J.js";
5
+ import "../chunk-5MUPA66B.js";
6
6
  import "../chunk-YK4WAAS4.js";
7
7
  import "../chunk-XUUY2L2D.js";
8
- import "../chunk-YVQBC3SJ.js";
8
+ import "../chunk-YZLOFKTT.js";
9
9
  import "../chunk-QKA6OQTH.js";
10
10
  import "../chunk-NPZDYOE6.js";
11
11
  import "../chunk-MMHG7WAM.js";
12
12
  import "../chunk-M6RPFHG7.js";
13
- import "../chunk-SRBW3LD7.js";
14
- import "../chunk-5LFGEMIW.js";
13
+ import "../chunk-FUJ3POKH.js";
14
+ import "../chunk-QVFOCNF6.js";
15
15
  import "../chunk-WQKHFADH.js";
16
16
  import "../chunk-JS5MGVNM.js";
17
17
  import "../chunk-OIMLU4SF.js";
@@ -1,11 +1,11 @@
1
- import { C as CompletionModel, M as Message, D as Document, e as JsonValue, n as ToolChoice, J as JsonObject } from './types-q9G7Bvwl.js';
1
+ import { C as CompletionModel, M as Message, D as Document, e as JsonValue, n as ToolChoice, J as JsonObject } from './types-C15r7N_4.js';
2
2
  import { GuardrailPolicyInput, GuardrailPolicy } from './guardrails/index.js';
3
- import { P as PromptHook } from './types-CGML74sE.js';
4
- import { c as MemoryContext, e as MemoryRegistration, S as SessionOptions } from './types-CqG6d7Ce.js';
5
- import { c as AgentTraceOptions, P as PromptResponse, a as AgentStreamEvent, d as AgentObserverRegistration } from './index-Oucb3Ljj.js';
6
- import { e as ToolApprovalsOptions, T as Tool, A as AnyTool, f as ToolCallContext, N as NormalizedToolOutput } from './tool-B8K543oQ.js';
7
- import { A as AgentMiddleware, e as ToolMiddleware, k as ToolSearchDocument, l as ToolSet } from './middleware-ByyL6Rj0.js';
8
- import { V as VectorFilter, c as VectorSearchResult, a as VectorSearchIndex } from './types-Pl4PFQWL.js';
3
+ import { P as PromptHook } from './types-Dh7gfoGw.js';
4
+ import { c as MemoryContext, e as MemoryRegistration, S as SessionOptions } from './types-bNDxj4Tt.js';
5
+ import { c as AgentTraceOptions, P as PromptResponse, a as AgentStreamEvent, d as AgentObserverRegistration } from './index-CM-V4LS3.js';
6
+ import { e as ToolApprovalsOptions, T as Tool, A as AnyTool, f as ToolCallContext, N as NormalizedToolOutput } from './tool-f2stpvUP.js';
7
+ import { A as AgentMiddleware, e as ToolMiddleware, k as ToolSearchDocument, l as ToolSet } from './middleware-i-jLsLom.js';
8
+ import { V as VectorFilter, c as VectorSearchResult, a as VectorSearchIndex } from './types-DmvXANwl.js';
9
9
 
10
10
  declare class PromptRequest<M extends CompletionModel = CompletionModel> {
11
11
  private readonly agent;
@@ -1,4 +1,4 @@
1
- import { e as JsonValue } from '../types-q9G7Bvwl.js';
1
+ import { e as JsonValue } from '../types-C15r7N_4.js';
2
2
 
3
3
  type AudioGenerationRequest = {
4
4
  text: string;
@@ -4,7 +4,7 @@ import {
4
4
  import {
5
5
  parseToolArgs,
6
6
  toolResultContentToText
7
- } from "./chunk-YVQBC3SJ.js";
7
+ } from "./chunk-YZLOFKTT.js";
8
8
  import {
9
9
  runControl,
10
10
  toolCallControl
@@ -15,7 +15,7 @@ import {
15
15
  import {
16
16
  CompletionRequestBuilder,
17
17
  isStreamingCompletionModel
18
- } from "./chunk-SRBW3LD7.js";
18
+ } from "./chunk-FUJ3POKH.js";
19
19
  import {
20
20
  AssistantContent,
21
21
  Message,
@@ -23,7 +23,7 @@ import {
23
23
  Usage,
24
24
  assertCompletionRequestSupported,
25
25
  textFromAssistantContent
26
- } from "./chunk-5LFGEMIW.js";
26
+ } from "./chunk-QVFOCNF6.js";
27
27
  import {
28
28
  mapWithConcurrency
29
29
  } from "./chunk-OIMLU4SF.js";
@@ -289,48 +289,34 @@ function formatMetadata(metadata) {
289
289
  }
290
290
 
291
291
  // src/internal/prompt-runtime/stream-accumulator.ts
292
- var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
293
- static defaultReasoningKey = "reasoning";
294
- text = "";
295
- reasoningById = /* @__PURE__ */ new Map();
296
- reasoningOrder = [];
292
+ var CompletionStreamAccumulator = class {
293
+ orderedParts = [];
294
+ textParts = /* @__PURE__ */ new Map();
295
+ reasoningByKey = /* @__PURE__ */ new Map();
296
+ reasoningKeyById = /* @__PURE__ */ new Map();
297
297
  toolCalls = /* @__PURE__ */ new Map();
298
- toolCallOrder = [];
299
298
  finalResponse;
300
299
  messageId;
300
+ nextTextKey = 0;
301
+ nextReasoningKey = 0;
301
302
  accept(event) {
302
303
  if (event.type === "text_delta") {
303
- this.text += event.delta;
304
+ this.appendText(event.delta);
304
305
  return { type: "text_delta", delta: event.delta };
305
306
  }
306
307
  if (event.type === "reasoning_delta") {
307
- const key = event.id ?? _CompletionStreamAccumulator.defaultReasoningKey;
308
- const existing = this.reasoningById.get(key);
309
- const reasoning = existing ?? { text: "" };
310
- if (!existing) {
311
- this.reasoningOrder.push(key);
312
- }
308
+ const reasoning = this.reasoningStateForEvent(event);
313
309
  this.appendReasoning(reasoning, event);
314
- this.reasoningById.set(key, reasoning);
315
310
  return reasoningDeltaEvent(event);
316
311
  }
317
312
  if (event.type === "tool_call_delta") {
318
- const existing = this.toolCalls.get(event.id);
319
- const toolCall = existing ?? {
320
- id: event.id,
321
- name: "",
322
- argumentsText: ""
323
- };
324
- if (!existing) {
325
- this.toolCallOrder.push(event.id);
326
- }
313
+ const toolCall = this.toolCallStateForId(event.id);
327
314
  if (event.callId !== void 0) toolCall.callId = event.callId;
328
315
  if (event.name !== void 0) toolCall.name = event.name;
329
316
  if (event.signature !== void 0) toolCall.signature = event.signature;
330
317
  if (event.argumentsDelta !== void 0) {
331
318
  toolCall.argumentsText += event.argumentsDelta;
332
319
  }
333
- this.toolCalls.set(event.id, toolCall);
334
320
  return void 0;
335
321
  }
336
322
  if (event.type === "tool_call") {
@@ -350,16 +336,8 @@ var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
350
336
  response() {
351
337
  const accumulatedResponse = this.buildAccumulatedResponse();
352
338
  if (this.finalResponse !== void 0) {
353
- if (this.finalResponse.choice.length === 0) {
354
- const response = {
355
- ...accumulatedResponse,
356
- usage: this.finalResponse.usage,
357
- rawResponse: this.finalResponse.rawResponse
358
- };
359
- if (this.finalResponse.messageId !== void 0) {
360
- response.messageId = this.finalResponse.messageId;
361
- }
362
- return response;
339
+ if (accumulatedResponse.choice.length === 0) {
340
+ return this.withMessageIdFallback(this.finalResponse, accumulatedResponse);
363
341
  }
364
342
  return this.mergeFinalResponse(accumulatedResponse, this.finalResponse);
365
343
  }
@@ -367,33 +345,24 @@ var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
367
345
  }
368
346
  buildAccumulatedResponse() {
369
347
  const choice = [];
370
- if (this.text.length > 0) {
371
- choice.push({ type: "text", text: this.text });
372
- }
373
- for (const key of this.reasoningOrder) {
374
- const reasoning = this.reasoningById.get(key) ?? { text: "" };
375
- const id = key === _CompletionStreamAccumulator.defaultReasoningKey ? void 0 : key;
376
- const content = reasoning.content === void 0 ? { type: "reasoning", text: reasoning.text } : { type: "reasoning", text: reasoning.text, content: reasoning.content };
377
- choice.push(id === void 0 ? content : { ...content, id });
378
- }
379
- for (const id of this.toolCallOrder) {
380
- const toolCall = this.toolCalls.get(id);
381
- if (toolCall !== void 0) {
382
- const content = {
383
- type: "tool_call",
384
- id: toolCall.id,
385
- function: {
386
- name: toolCall.name,
387
- arguments: parseJsonValue(toolCall.argumentsText)
388
- }
389
- };
390
- if (toolCall.callId !== void 0) {
391
- content.callId = toolCall.callId;
348
+ for (const part of this.orderedParts) {
349
+ if (part.type === "text") {
350
+ const text = this.textParts.get(part.key) ?? "";
351
+ if (text.length > 0) {
352
+ choice.push({ type: "text", text });
392
353
  }
393
- if (toolCall.signature !== void 0) {
394
- content.signature = toolCall.signature;
354
+ continue;
355
+ }
356
+ if (part.type === "reasoning") {
357
+ const reasoning = this.reasoningByKey.get(part.key);
358
+ if (reasoning !== void 0) {
359
+ choice.push(reasoningContent(reasoning));
395
360
  }
396
- choice.push(content);
361
+ continue;
362
+ }
363
+ const toolCall = this.toolCalls.get(part.key);
364
+ if (toolCall !== void 0) {
365
+ choice.push(toolCallContent(toolCall));
397
366
  }
398
367
  }
399
368
  const response = {
@@ -408,7 +377,7 @@ var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
408
377
  }
409
378
  upsertToolCall(toolCall) {
410
379
  if (!this.toolCalls.has(toolCall.id)) {
411
- this.toolCallOrder.push(toolCall.id);
380
+ this.orderedParts.push({ type: "tool_call", key: toolCall.id });
412
381
  }
413
382
  const partial = {
414
383
  id: toolCall.id,
@@ -421,39 +390,120 @@ var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
421
390
  if (toolCall.signature !== void 0) {
422
391
  partial.signature = toolCall.signature;
423
392
  }
393
+ if (toolCall.additionalParams !== void 0) {
394
+ partial.additionalParams = toolCall.additionalParams;
395
+ }
424
396
  this.toolCalls.set(toolCall.id, partial);
425
397
  }
426
398
  mergeFinalResponse(accumulatedResponse, finalResponse) {
427
- const accumulatedById = /* @__PURE__ */ new Map();
428
- const accumulatedByCallId = /* @__PURE__ */ new Map();
429
- for (const content of accumulatedResponse.choice) {
399
+ if (finalResponse.choice.length === 0) {
400
+ return this.withMessageIdFallback(
401
+ {
402
+ ...accumulatedResponse,
403
+ usage: finalResponse.usage,
404
+ rawResponse: finalResponse.rawResponse,
405
+ ...finalResponse.messageId === void 0 ? {} : { messageId: finalResponse.messageId }
406
+ },
407
+ accumulatedResponse
408
+ );
409
+ }
410
+ const finalById = /* @__PURE__ */ new Map();
411
+ const finalByCallId = /* @__PURE__ */ new Map();
412
+ for (const content of finalResponse.choice) {
430
413
  if (content.type !== "tool_call") {
431
414
  continue;
432
415
  }
433
- accumulatedById.set(content.id, content);
416
+ finalById.set(content.id, content);
434
417
  if (content.callId !== void 0) {
435
- accumulatedByCallId.set(content.callId, content);
418
+ finalByCallId.set(content.callId, content);
436
419
  }
437
420
  }
438
- return {
439
- ...finalResponse,
440
- choice: finalResponse.choice.map((content) => {
441
- if (content.type !== "tool_call" || !isEmptyToolArguments(content.function.arguments)) {
442
- return content;
443
- }
444
- const accumulated = accumulatedById.get(content.id) ?? (content.callId === void 0 ? void 0 : accumulatedByCallId.get(content.callId));
445
- if (accumulated === void 0 || isEmptyToolArguments(accumulated.function.arguments)) {
446
- return content;
421
+ const matchedFinalToolCalls = /* @__PURE__ */ new Set();
422
+ const choice = accumulatedResponse.choice.map((content) => {
423
+ if (content.type !== "tool_call") {
424
+ return content;
425
+ }
426
+ const finalToolCall = finalById.get(content.id) ?? (content.callId === void 0 ? void 0 : finalByCallId.get(content.callId));
427
+ if (finalToolCall === void 0) {
428
+ return content;
429
+ }
430
+ matchedFinalToolCalls.add(finalToolCall);
431
+ return mergeFinalToolCall(content, finalToolCall);
432
+ });
433
+ for (const content of finalResponse.choice) {
434
+ if (content.type !== "tool_call") {
435
+ continue;
436
+ }
437
+ if (!matchedFinalToolCalls.has(content)) {
438
+ choice.push(content);
439
+ }
440
+ }
441
+ return this.withMessageIdFallback({ ...finalResponse, choice }, accumulatedResponse);
442
+ }
443
+ appendText(delta) {
444
+ const lastPart = this.orderedParts.at(-1);
445
+ const key = lastPart?.type === "text" ? lastPart.key : this.createTextKey();
446
+ if (lastPart?.type !== "text") {
447
+ this.orderedParts.push({ type: "text", key });
448
+ }
449
+ this.textParts.set(key, `${this.textParts.get(key) ?? ""}${delta}`);
450
+ }
451
+ reasoningStateForEvent(event) {
452
+ if (event.id !== void 0) {
453
+ const existingKey = this.reasoningKeyById.get(event.id);
454
+ if (existingKey !== void 0) {
455
+ const existing = this.reasoningByKey.get(existingKey);
456
+ if (existing !== void 0) {
457
+ return existing;
447
458
  }
448
- return {
449
- ...content,
450
- function: {
451
- ...content.function,
452
- arguments: accumulated.function.arguments
453
- }
454
- };
455
- })
459
+ }
460
+ const key2 = this.createReasoningKey();
461
+ const reasoning2 = { id: event.id, text: "" };
462
+ this.reasoningKeyById.set(event.id, key2);
463
+ this.reasoningByKey.set(key2, reasoning2);
464
+ this.orderedParts.push({ type: "reasoning", key: key2 });
465
+ return reasoning2;
466
+ }
467
+ const lastPart = this.orderedParts.at(-1);
468
+ if (lastPart?.type === "reasoning") {
469
+ const lastReasoning = this.reasoningByKey.get(lastPart.key);
470
+ if (lastReasoning !== void 0 && lastReasoning.id === void 0) {
471
+ return lastReasoning;
472
+ }
473
+ }
474
+ const key = this.createReasoningKey();
475
+ const reasoning = { text: "" };
476
+ this.reasoningByKey.set(key, reasoning);
477
+ this.orderedParts.push({ type: "reasoning", key });
478
+ return reasoning;
479
+ }
480
+ toolCallStateForId(id) {
481
+ const existing = this.toolCalls.get(id);
482
+ if (existing !== void 0) {
483
+ return existing;
484
+ }
485
+ const toolCall = {
486
+ id,
487
+ name: "",
488
+ argumentsText: ""
456
489
  };
490
+ this.toolCalls.set(id, toolCall);
491
+ this.orderedParts.push({ type: "tool_call", key: id });
492
+ return toolCall;
493
+ }
494
+ withMessageIdFallback(response, accumulatedResponse) {
495
+ if (response.messageId !== void 0 || accumulatedResponse.messageId === void 0) {
496
+ return response;
497
+ }
498
+ return { ...response, messageId: accumulatedResponse.messageId };
499
+ }
500
+ createTextKey() {
501
+ this.nextTextKey += 1;
502
+ return `text_${this.nextTextKey.toString()}`;
503
+ }
504
+ createReasoningKey() {
505
+ this.nextReasoningKey += 1;
506
+ return `reasoning_${this.nextReasoningKey.toString()}`;
457
507
  }
458
508
  appendReasoning(reasoning, event) {
459
509
  const contentType = event.contentType ?? "text";
@@ -493,6 +543,42 @@ var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
493
543
  reasoning.content.push({ type: "redacted", data: event.delta });
494
544
  }
495
545
  };
546
+ function reasoningContent(reasoning) {
547
+ const content = reasoning.content === void 0 ? { type: "reasoning", text: reasoning.text } : { type: "reasoning", text: reasoning.text, content: reasoning.content };
548
+ return reasoning.id === void 0 ? content : { ...content, id: reasoning.id };
549
+ }
550
+ function toolCallContent(toolCall) {
551
+ const content = {
552
+ type: "tool_call",
553
+ id: toolCall.id,
554
+ function: {
555
+ name: toolCall.name,
556
+ arguments: parseJsonValue(toolCall.argumentsText)
557
+ }
558
+ };
559
+ if (toolCall.callId !== void 0) {
560
+ content.callId = toolCall.callId;
561
+ }
562
+ if (toolCall.signature !== void 0) {
563
+ content.signature = toolCall.signature;
564
+ }
565
+ if (toolCall.additionalParams !== void 0) {
566
+ content.additionalParams = toolCall.additionalParams;
567
+ }
568
+ return content;
569
+ }
570
+ function mergeFinalToolCall(accumulated, finalToolCall) {
571
+ const argumentsValue = isEmptyToolArguments(finalToolCall.function.arguments) ? accumulated.function.arguments : finalToolCall.function.arguments;
572
+ return {
573
+ ...accumulated,
574
+ ...finalToolCall,
575
+ function: {
576
+ ...accumulated.function,
577
+ ...finalToolCall.function,
578
+ arguments: argumentsValue
579
+ }
580
+ };
581
+ }
496
582
  function reasoningDeltaEvent(event) {
497
583
  const mapped = { type: "reasoning_delta", delta: event.delta };
498
584
  if (event.id !== void 0) mapped.id = event.id;
@@ -2014,4 +2100,4 @@ export {
2014
2100
  extractRagText,
2015
2101
  PromptRequest
2016
2102
  };
2017
- //# sourceMappingURL=chunk-4NCRFQHS.js.map
2103
+ //# sourceMappingURL=chunk-5MUPA66B.js.map