@livekit/agents 1.0.34 → 1.0.36-dev.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 (187) hide show
  1. package/dist/cli.cjs.map +1 -1
  2. package/dist/index.cjs +3 -1
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +1 -0
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/inference/api_protos.d.cts +4 -4
  10. package/dist/inference/api_protos.d.ts +4 -4
  11. package/dist/inference/interruption/AdaptiveInterruptionDetector.cjs +152 -0
  12. package/dist/inference/interruption/AdaptiveInterruptionDetector.cjs.map +1 -0
  13. package/dist/inference/interruption/AdaptiveInterruptionDetector.d.cts +50 -0
  14. package/dist/inference/interruption/AdaptiveInterruptionDetector.d.ts +50 -0
  15. package/dist/inference/interruption/AdaptiveInterruptionDetector.d.ts.map +1 -0
  16. package/dist/inference/interruption/AdaptiveInterruptionDetector.js +125 -0
  17. package/dist/inference/interruption/AdaptiveInterruptionDetector.js.map +1 -0
  18. package/dist/inference/interruption/InterruptionStream.cjs +310 -0
  19. package/dist/inference/interruption/InterruptionStream.cjs.map +1 -0
  20. package/dist/inference/interruption/InterruptionStream.d.cts +57 -0
  21. package/dist/inference/interruption/InterruptionStream.d.ts +57 -0
  22. package/dist/inference/interruption/InterruptionStream.d.ts.map +1 -0
  23. package/dist/inference/interruption/InterruptionStream.js +288 -0
  24. package/dist/inference/interruption/InterruptionStream.js.map +1 -0
  25. package/dist/inference/interruption/defaults.cjs +76 -0
  26. package/dist/inference/interruption/defaults.cjs.map +1 -0
  27. package/dist/inference/interruption/defaults.d.cts +14 -0
  28. package/dist/inference/interruption/defaults.d.ts +14 -0
  29. package/dist/inference/interruption/defaults.d.ts.map +1 -0
  30. package/dist/inference/interruption/defaults.js +42 -0
  31. package/dist/inference/interruption/defaults.js.map +1 -0
  32. package/dist/inference/interruption/errors.cjs +2 -0
  33. package/dist/inference/interruption/errors.cjs.map +1 -0
  34. package/dist/inference/interruption/errors.d.cts +2 -0
  35. package/dist/inference/interruption/errors.d.ts +2 -0
  36. package/dist/inference/interruption/errors.d.ts.map +1 -0
  37. package/dist/inference/interruption/errors.js +1 -0
  38. package/dist/inference/interruption/errors.js.map +1 -0
  39. package/dist/inference/interruption/http_transport.cjs +57 -0
  40. package/dist/inference/interruption/http_transport.cjs.map +1 -0
  41. package/dist/inference/interruption/http_transport.d.cts +23 -0
  42. package/dist/inference/interruption/http_transport.d.ts +23 -0
  43. package/dist/inference/interruption/http_transport.d.ts.map +1 -0
  44. package/dist/inference/interruption/http_transport.js +33 -0
  45. package/dist/inference/interruption/http_transport.js.map +1 -0
  46. package/dist/inference/interruption/index.cjs +34 -0
  47. package/dist/inference/interruption/index.cjs.map +1 -0
  48. package/dist/inference/interruption/index.d.cts +5 -0
  49. package/dist/inference/interruption/index.d.ts +5 -0
  50. package/dist/inference/interruption/index.d.ts.map +1 -0
  51. package/dist/inference/interruption/index.js +7 -0
  52. package/dist/inference/interruption/index.js.map +1 -0
  53. package/dist/inference/interruption/interruption.cjs +85 -0
  54. package/dist/inference/interruption/interruption.cjs.map +1 -0
  55. package/dist/inference/interruption/interruption.d.cts +48 -0
  56. package/dist/inference/interruption/interruption.d.ts +48 -0
  57. package/dist/inference/interruption/interruption.d.ts.map +1 -0
  58. package/dist/inference/interruption/interruption.js +59 -0
  59. package/dist/inference/interruption/interruption.js.map +1 -0
  60. package/dist/inference/llm.cjs +30 -3
  61. package/dist/inference/llm.cjs.map +1 -1
  62. package/dist/inference/llm.d.cts +3 -1
  63. package/dist/inference/llm.d.ts +3 -1
  64. package/dist/inference/llm.d.ts.map +1 -1
  65. package/dist/inference/llm.js +30 -3
  66. package/dist/inference/llm.js.map +1 -1
  67. package/dist/inference/utils.cjs +15 -2
  68. package/dist/inference/utils.cjs.map +1 -1
  69. package/dist/inference/utils.d.cts +1 -0
  70. package/dist/inference/utils.d.ts +1 -0
  71. package/dist/inference/utils.d.ts.map +1 -1
  72. package/dist/inference/utils.js +13 -1
  73. package/dist/inference/utils.js.map +1 -1
  74. package/dist/inference/utils.test.cjs +20 -0
  75. package/dist/inference/utils.test.cjs.map +1 -0
  76. package/dist/inference/utils.test.js +19 -0
  77. package/dist/inference/utils.test.js.map +1 -0
  78. package/dist/ipc/inference_proc_executor.cjs.map +1 -1
  79. package/dist/ipc/job_proc_executor.cjs.map +1 -1
  80. package/dist/ipc/job_proc_lazy_main.cjs +1 -1
  81. package/dist/ipc/job_proc_lazy_main.cjs.map +1 -1
  82. package/dist/ipc/job_proc_lazy_main.js +1 -1
  83. package/dist/ipc/job_proc_lazy_main.js.map +1 -1
  84. package/dist/llm/chat_context.cjs +20 -2
  85. package/dist/llm/chat_context.cjs.map +1 -1
  86. package/dist/llm/chat_context.d.cts +9 -0
  87. package/dist/llm/chat_context.d.ts +9 -0
  88. package/dist/llm/chat_context.d.ts.map +1 -1
  89. package/dist/llm/chat_context.js +20 -2
  90. package/dist/llm/chat_context.js.map +1 -1
  91. package/dist/llm/llm.cjs.map +1 -1
  92. package/dist/llm/llm.d.cts +1 -0
  93. package/dist/llm/llm.d.ts +1 -0
  94. package/dist/llm/llm.d.ts.map +1 -1
  95. package/dist/llm/llm.js.map +1 -1
  96. package/dist/llm/provider_format/openai.cjs +43 -20
  97. package/dist/llm/provider_format/openai.cjs.map +1 -1
  98. package/dist/llm/provider_format/openai.d.ts.map +1 -1
  99. package/dist/llm/provider_format/openai.js +43 -20
  100. package/dist/llm/provider_format/openai.js.map +1 -1
  101. package/dist/llm/provider_format/openai.test.cjs +35 -0
  102. package/dist/llm/provider_format/openai.test.cjs.map +1 -1
  103. package/dist/llm/provider_format/openai.test.js +35 -0
  104. package/dist/llm/provider_format/openai.test.js.map +1 -1
  105. package/dist/llm/provider_format/utils.cjs +1 -1
  106. package/dist/llm/provider_format/utils.cjs.map +1 -1
  107. package/dist/llm/provider_format/utils.d.ts.map +1 -1
  108. package/dist/llm/provider_format/utils.js +1 -1
  109. package/dist/llm/provider_format/utils.js.map +1 -1
  110. package/dist/stream/stream_channel.cjs +3 -0
  111. package/dist/stream/stream_channel.cjs.map +1 -1
  112. package/dist/stream/stream_channel.d.cts +3 -2
  113. package/dist/stream/stream_channel.d.ts +3 -2
  114. package/dist/stream/stream_channel.d.ts.map +1 -1
  115. package/dist/stream/stream_channel.js +3 -0
  116. package/dist/stream/stream_channel.js.map +1 -1
  117. package/dist/telemetry/trace_types.cjs +15 -0
  118. package/dist/telemetry/trace_types.cjs.map +1 -1
  119. package/dist/telemetry/trace_types.d.cts +5 -0
  120. package/dist/telemetry/trace_types.d.ts +5 -0
  121. package/dist/telemetry/trace_types.d.ts.map +1 -1
  122. package/dist/telemetry/trace_types.js +10 -0
  123. package/dist/telemetry/trace_types.js.map +1 -1
  124. package/dist/utils/ws_transport.cjs +51 -0
  125. package/dist/utils/ws_transport.cjs.map +1 -0
  126. package/dist/utils/ws_transport.d.cts +9 -0
  127. package/dist/utils/ws_transport.d.ts +9 -0
  128. package/dist/utils/ws_transport.d.ts.map +1 -0
  129. package/dist/utils/ws_transport.js +17 -0
  130. package/dist/utils/ws_transport.js.map +1 -0
  131. package/dist/utils/ws_transport.test.cjs +212 -0
  132. package/dist/utils/ws_transport.test.cjs.map +1 -0
  133. package/dist/utils/ws_transport.test.js +211 -0
  134. package/dist/utils/ws_transport.test.js.map +1 -0
  135. package/dist/voice/agent_activity.cjs +49 -0
  136. package/dist/voice/agent_activity.cjs.map +1 -1
  137. package/dist/voice/agent_activity.d.cts +14 -0
  138. package/dist/voice/agent_activity.d.ts +14 -0
  139. package/dist/voice/agent_activity.d.ts.map +1 -1
  140. package/dist/voice/agent_activity.js +49 -0
  141. package/dist/voice/agent_activity.js.map +1 -1
  142. package/dist/voice/agent_session.cjs +12 -1
  143. package/dist/voice/agent_session.cjs.map +1 -1
  144. package/dist/voice/agent_session.d.cts +3 -0
  145. package/dist/voice/agent_session.d.ts +3 -0
  146. package/dist/voice/agent_session.d.ts.map +1 -1
  147. package/dist/voice/agent_session.js +12 -1
  148. package/dist/voice/agent_session.js.map +1 -1
  149. package/dist/voice/audio_recognition.cjs +124 -2
  150. package/dist/voice/audio_recognition.cjs.map +1 -1
  151. package/dist/voice/audio_recognition.d.cts +32 -1
  152. package/dist/voice/audio_recognition.d.ts +32 -1
  153. package/dist/voice/audio_recognition.d.ts.map +1 -1
  154. package/dist/voice/audio_recognition.js +127 -2
  155. package/dist/voice/audio_recognition.js.map +1 -1
  156. package/dist/voice/background_audio.cjs.map +1 -1
  157. package/dist/voice/generation.cjs +2 -1
  158. package/dist/voice/generation.cjs.map +1 -1
  159. package/dist/voice/generation.d.ts.map +1 -1
  160. package/dist/voice/generation.js +2 -1
  161. package/dist/voice/generation.js.map +1 -1
  162. package/package.json +2 -1
  163. package/src/index.ts +2 -0
  164. package/src/inference/interruption/AdaptiveInterruptionDetector.ts +166 -0
  165. package/src/inference/interruption/InterruptionStream.ts +397 -0
  166. package/src/inference/interruption/defaults.ts +33 -0
  167. package/src/inference/interruption/errors.ts +0 -0
  168. package/src/inference/interruption/http_transport.ts +61 -0
  169. package/src/inference/interruption/index.ts +4 -0
  170. package/src/inference/interruption/interruption.ts +88 -0
  171. package/src/inference/llm.ts +42 -3
  172. package/src/inference/utils.test.ts +31 -0
  173. package/src/inference/utils.ts +15 -0
  174. package/src/ipc/job_proc_lazy_main.ts +1 -1
  175. package/src/llm/chat_context.ts +32 -2
  176. package/src/llm/llm.ts +1 -0
  177. package/src/llm/provider_format/openai.test.ts +40 -0
  178. package/src/llm/provider_format/openai.ts +46 -19
  179. package/src/llm/provider_format/utils.ts +5 -1
  180. package/src/stream/stream_channel.ts +6 -2
  181. package/src/telemetry/trace_types.ts +7 -0
  182. package/src/utils/ws_transport.test.ts +282 -0
  183. package/src/utils/ws_transport.ts +22 -0
  184. package/src/voice/agent_activity.ts +61 -0
  185. package/src/voice/agent_session.ts +22 -2
  186. package/src/voice/audio_recognition.ts +161 -1
  187. package/src/voice/generation.ts +1 -0
package/dist/llm/llm.d.ts CHANGED
@@ -8,6 +8,7 @@ export interface ChoiceDelta {
8
8
  role: ChatRole;
9
9
  content?: string;
10
10
  toolCalls?: FunctionCall[];
11
+ extra?: Record<string, unknown>;
11
12
  }
12
13
  export interface CompletionUsage {
13
14
  completionTokens: number;
@@ -1 +1 @@
1
- {"version":3,"file":"llm.d.ts","sourceRoot":"","sources":["../../src/llm/llm.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,IAAI,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAKhF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErD,OAAO,EAAE,KAAK,iBAAiB,EAAoB,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAA6B,MAAM,aAAa,CAAC;AAC5E,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,QAAQ,EAAE,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACvF,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEjE,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,QAAQ,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,YAAY,EAAE,CAAC;CAC5B;AAED,MAAM,WAAW,eAAe;IAC9B,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,KAAK,CAAC,EAAE,eAAe,CAAC;CACzB;AAED,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,WAAW,CAAC;IAClB,SAAS,EAAE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ACvC,GAAG,SAAS;IAEb;;OAEG;IACH,OAAO,IAAI,IAAI;IAIT,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAG9B;AAED,8BAAsB,SAAU,YAAW,qBAAqB,CAAC,SAAS,CAAC;;IACzE,SAAS,CAAC,MAAM,gCAAuC;IACvD,SAAS,CAAC,KAAK,gCAAuC;IACtD,SAAS,CAAC,MAAM,UAAS;IACzB,SAAS,CAAC,eAAe,kBAAyB;IAClD,SAAS,CAAC,YAAY,EAAE,iBAAiB,CAAC;IAC1C,SAAS,CAAC,MAAM,wBAAS;gBAQvB,GAAG,EAAE,GAAG,EACR,EACE,OAAO,EACP,OAAO,EACP,WAAW,GACZ,EAAE;QACD,OAAO,EAAE,WAAW,CAAC;QACrB,OAAO,CAAC,EAAE,WAAW,CAAC;QACtB,WAAW,EAAE,iBAAiB,CAAC;KAChC;IAoBH,OAAO,CAAC,aAAa,CAgDnB;IAEF,OAAO,CAAC,QAAQ,CAIX;IAEL,OAAO,CAAC,SAAS;cAUD,cAAc;IAmE9B,SAAS,CAAC,QAAQ,CAAC,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;IAEvC,2CAA2C;IAC3C,IAAI,OAAO,IAAI,WAAW,GAAG,SAAS,CAErC;IAED,+CAA+C;IAC/C,IAAI,OAAO,IAAI,WAAW,CAEzB;IAED,8CAA8C;IAC9C,IAAI,WAAW,IAAI,iBAAiB,CAEnC;IAED,IAAI,IAAI,OAAO,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;IAI1C,KAAK;IAIL,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,SAAS;CAGpC"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/llm/llm.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';\nimport type { Span } from '@opentelemetry/api';\nimport { EventEmitter } from 'node:events';\nimport { APIConnectionError, APIError } from '../_exceptions.js';\nimport { log } from '../log.js';\nimport type { LLMMetrics } from '../metrics/base.js';\nimport { recordException, traceTypes, tracer } from '../telemetry/index.js';\nimport { type APIConnectOptions, intervalForRetry } from '../types.js';\nimport { AsyncIterableQueue, delay, startSoon, toError } from '../utils.js';\nimport { type ChatContext, type ChatRole, type FunctionCall } from './chat_context.js';\nimport type { ToolChoice, ToolContext } from './tool_context.js';\n\nexport interface ChoiceDelta {\n role: ChatRole;\n content?: string;\n toolCalls?: FunctionCall[];\n}\n\nexport interface CompletionUsage {\n completionTokens: number;\n promptTokens: number;\n promptCachedTokens: number;\n totalTokens: number;\n}\n\nexport interface ChatChunk {\n id: string;\n delta?: ChoiceDelta;\n usage?: CompletionUsage;\n}\n\nexport interface LLMError {\n type: 'llm_error';\n timestamp: number;\n label: string;\n error: Error;\n recoverable: boolean;\n}\n\nexport type LLMCallbacks = {\n ['metrics_collected']: (metrics: LLMMetrics) => void;\n ['error']: (error: LLMError) => void;\n};\n\nexport abstract class LLM extends (EventEmitter as new () => TypedEmitter<LLMCallbacks>) {\n constructor() {\n super();\n }\n\n abstract label(): string;\n\n /**\n * Get the model name/identifier for this LLM instance.\n *\n * @returns The model name if available, \"unknown\" otherwise.\n *\n * @remarks\n * Plugins should override this property to provide their model information.\n */\n get model(): string {\n return 'unknown';\n }\n\n /**\n * Returns a {@link LLMStream} that can be used to push text and receive LLM responses.\n */\n abstract chat({\n chatCtx,\n toolCtx,\n connOptions,\n parallelToolCalls,\n toolChoice,\n extraKwargs,\n }: {\n chatCtx: ChatContext;\n toolCtx?: ToolContext;\n connOptions?: APIConnectOptions;\n parallelToolCalls?: boolean;\n toolChoice?: ToolChoice;\n extraKwargs?: Record<string, unknown>;\n }): LLMStream;\n\n /**\n * Pre-warm connection to the LLM service\n */\n prewarm(): void {\n // Default implementation - subclasses can override\n }\n\n async aclose(): Promise<void> {\n // Default implementation - subclasses can override\n }\n}\n\nexport abstract class LLMStream implements AsyncIterableIterator<ChatChunk> {\n protected output = new AsyncIterableQueue<ChatChunk>();\n protected queue = new AsyncIterableQueue<ChatChunk>();\n protected closed = false;\n protected abortController = new AbortController();\n protected _connOptions: APIConnectOptions;\n protected logger = log();\n\n #llm: LLM;\n #chatCtx: ChatContext;\n #toolCtx?: ToolContext;\n #llmRequestSpan?: Span;\n\n constructor(\n llm: LLM,\n {\n chatCtx,\n toolCtx,\n connOptions,\n }: {\n chatCtx: ChatContext;\n toolCtx?: ToolContext;\n connOptions: APIConnectOptions;\n },\n ) {\n this.#llm = llm;\n this.#chatCtx = chatCtx;\n this.#toolCtx = toolCtx;\n this._connOptions = connOptions;\n this.monitorMetrics();\n this.abortController.signal.addEventListener('abort', () => {\n // TODO (AJS-37) clean this up when we refactor with streams\n this.output.close();\n this.closed = true;\n });\n\n // this is a hack to immitate asyncio.create_task so that mainTask\n // is run **after** the constructor has finished. Otherwise we get\n // runtime error when trying to access class variables in the\n // `run` method.\n startSoon(() => this.mainTask().finally(() => this.queue.close()));\n }\n\n private _mainTaskImpl = async (span: Span) => {\n this.#llmRequestSpan = span;\n span.setAttribute(traceTypes.ATTR_GEN_AI_REQUEST_MODEL, this.#llm.model);\n\n for (let i = 0; i < this._connOptions.maxRetry + 1; i++) {\n try {\n return await tracer.startActiveSpan(\n async (attemptSpan) => {\n attemptSpan.setAttribute(traceTypes.ATTR_RETRY_COUNT, i);\n try {\n return await this.run();\n } catch (error) {\n recordException(attemptSpan, toError(error));\n throw error;\n }\n },\n { name: 'llm_request_run' },\n );\n } catch (error) {\n if (error instanceof APIError) {\n const retryInterval = intervalForRetry(this._connOptions, i);\n\n if (this._connOptions.maxRetry === 0 || !error.retryable) {\n this.emitError({ error, recoverable: false });\n throw error;\n } else if (i === this._connOptions.maxRetry) {\n this.emitError({ error, recoverable: false });\n throw new APIConnectionError({\n message: `failed to generate LLM completion after ${this._connOptions.maxRetry + 1} attempts`,\n options: { retryable: false },\n });\n } else {\n this.emitError({ error, recoverable: true });\n this.logger.warn(\n { llm: this.#llm.label(), attempt: i + 1, error },\n `failed to generate LLM completion, retrying in ${retryInterval}s`,\n );\n }\n\n if (retryInterval > 0) {\n await delay(retryInterval);\n }\n } else {\n this.emitError({ error: toError(error), recoverable: false });\n throw error;\n }\n }\n }\n };\n\n private mainTask = async () =>\n tracer.startActiveSpan(async (span) => this._mainTaskImpl(span), {\n name: 'llm_request',\n endOnExit: false,\n });\n\n private emitError({ error, recoverable }: { error: Error; recoverable: boolean }) {\n this.#llm.emit('error', {\n type: 'llm_error',\n timestamp: Date.now(),\n label: this.#llm.label(),\n error,\n recoverable,\n });\n }\n\n protected async monitorMetrics() {\n const startTime = process.hrtime.bigint();\n let ttft: bigint = BigInt(-1);\n let requestId = '';\n let usage: CompletionUsage | undefined;\n let completionStartTime: string | undefined;\n\n for await (const ev of this.queue) {\n if (this.abortController.signal.aborted) {\n break;\n }\n this.output.put(ev);\n requestId = ev.id;\n if (ttft === BigInt(-1)) {\n ttft = process.hrtime.bigint() - startTime;\n completionStartTime = new Date().toISOString();\n }\n if (ev.usage) {\n usage = ev.usage;\n }\n }\n this.output.close();\n\n const duration = process.hrtime.bigint() - startTime;\n const durationMs = Math.trunc(Number(duration / BigInt(1000000)));\n const metrics: LLMMetrics = {\n type: 'llm_metrics',\n timestamp: Date.now(),\n requestId,\n ttftMs: ttft === BigInt(-1) ? -1 : Math.trunc(Number(ttft / BigInt(1000000))),\n durationMs,\n cancelled: this.abortController.signal.aborted,\n label: this.#llm.label(),\n completionTokens: usage?.completionTokens || 0,\n promptTokens: usage?.promptTokens || 0,\n promptCachedTokens: usage?.promptCachedTokens || 0,\n totalTokens: usage?.totalTokens || 0,\n tokensPerSecond: (() => {\n if (durationMs <= 0) {\n return 0;\n }\n return (usage?.completionTokens || 0) / (durationMs / 1000);\n })(),\n };\n\n if (this.#llmRequestSpan) {\n this.#llmRequestSpan.setAttribute(traceTypes.ATTR_LLM_METRICS, JSON.stringify(metrics));\n\n this.#llmRequestSpan.setAttributes({\n [traceTypes.ATTR_GEN_AI_USAGE_INPUT_TOKENS]: metrics.promptTokens,\n [traceTypes.ATTR_GEN_AI_USAGE_OUTPUT_TOKENS]: metrics.completionTokens,\n });\n\n if (completionStartTime) {\n this.#llmRequestSpan.setAttribute(\n traceTypes.ATTR_LANGFUSE_COMPLETION_START_TIME,\n completionStartTime,\n );\n }\n\n // End the span now that metrics are collected\n this.#llmRequestSpan.end();\n }\n\n this.#llm.emit('metrics_collected', metrics);\n }\n\n protected abstract run(): Promise<void>;\n\n /** The function context of this stream. */\n get toolCtx(): ToolContext | undefined {\n return this.#toolCtx;\n }\n\n /** The initial chat context of this stream. */\n get chatCtx(): ChatContext {\n return this.#chatCtx;\n }\n\n /** The connection options for this stream. */\n get connOptions(): APIConnectOptions {\n return this._connOptions;\n }\n\n next(): Promise<IteratorResult<ChatChunk>> {\n return this.output.next();\n }\n\n close() {\n this.abortController.abort();\n }\n\n [Symbol.asyncIterator](): LLMStream {\n return this;\n }\n}\n"],"mappings":"AAKA,SAAS,oBAAoB;AAC7B,SAAS,oBAAoB,gBAAgB;AAC7C,SAAS,WAAW;AAEpB,SAAS,iBAAiB,YAAY,cAAc;AACpD,SAAiC,wBAAwB;AACzD,SAAS,oBAAoB,OAAO,WAAW,eAAe;AAC9D,eAAmE;AAmC5D,MAAe,YAAa,aAAsD;AAAA,EACvF,cAAc;AACZ,UAAM;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,IAAI,QAAgB;AAClB,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAwBA,UAAgB;AAAA,EAEhB;AAAA,EAEA,MAAM,SAAwB;AAAA,EAE9B;AACF;AAEO,MAAe,UAAsD;AAAA,EAChE,SAAS,IAAI,mBAA8B;AAAA,EAC3C,QAAQ,IAAI,mBAA8B;AAAA,EAC1C,SAAS;AAAA,EACT,kBAAkB,IAAI,gBAAgB;AAAA,EACtC;AAAA,EACA,SAAS,IAAI;AA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;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,cAAiC;AACnC,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,OAA2C;AACzC,WAAO,KAAK,OAAO,KAAK;AAAA,EAC1B;AAAA,EAEA,QAAQ;AACN,SAAK,gBAAgB,MAAM;AAAA,EAC7B;AAAA,EAEA,CAAC,OAAO,aAAa,IAAe;AAClC,WAAO;AAAA,EACT;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/llm/llm.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter';\nimport type { Span } from '@opentelemetry/api';\nimport { EventEmitter } from 'node:events';\nimport { APIConnectionError, APIError } from '../_exceptions.js';\nimport { log } from '../log.js';\nimport type { LLMMetrics } from '../metrics/base.js';\nimport { recordException, traceTypes, tracer } from '../telemetry/index.js';\nimport { type APIConnectOptions, intervalForRetry } from '../types.js';\nimport { AsyncIterableQueue, delay, startSoon, toError } from '../utils.js';\nimport { type ChatContext, type ChatRole, type FunctionCall } from './chat_context.js';\nimport type { ToolChoice, ToolContext } from './tool_context.js';\n\nexport interface ChoiceDelta {\n role: ChatRole;\n content?: string;\n toolCalls?: FunctionCall[];\n extra?: Record<string, unknown>;\n}\n\nexport interface CompletionUsage {\n completionTokens: number;\n promptTokens: number;\n promptCachedTokens: number;\n totalTokens: number;\n}\n\nexport interface ChatChunk {\n id: string;\n delta?: ChoiceDelta;\n usage?: CompletionUsage;\n}\n\nexport interface LLMError {\n type: 'llm_error';\n timestamp: number;\n label: string;\n error: Error;\n recoverable: boolean;\n}\n\nexport type LLMCallbacks = {\n ['metrics_collected']: (metrics: LLMMetrics) => void;\n ['error']: (error: LLMError) => void;\n};\n\nexport abstract class LLM extends (EventEmitter as new () => TypedEmitter<LLMCallbacks>) {\n constructor() {\n super();\n }\n\n abstract label(): string;\n\n /**\n * Get the model name/identifier for this LLM instance.\n *\n * @returns The model name if available, \"unknown\" otherwise.\n *\n * @remarks\n * Plugins should override this property to provide their model information.\n */\n get model(): string {\n return 'unknown';\n }\n\n /**\n * Returns a {@link LLMStream} that can be used to push text and receive LLM responses.\n */\n abstract chat({\n chatCtx,\n toolCtx,\n connOptions,\n parallelToolCalls,\n toolChoice,\n extraKwargs,\n }: {\n chatCtx: ChatContext;\n toolCtx?: ToolContext;\n connOptions?: APIConnectOptions;\n parallelToolCalls?: boolean;\n toolChoice?: ToolChoice;\n extraKwargs?: Record<string, unknown>;\n }): LLMStream;\n\n /**\n * Pre-warm connection to the LLM service\n */\n prewarm(): void {\n // Default implementation - subclasses can override\n }\n\n async aclose(): Promise<void> {\n // Default implementation - subclasses can override\n }\n}\n\nexport abstract class LLMStream implements AsyncIterableIterator<ChatChunk> {\n protected output = new AsyncIterableQueue<ChatChunk>();\n protected queue = new AsyncIterableQueue<ChatChunk>();\n protected closed = false;\n protected abortController = new AbortController();\n protected _connOptions: APIConnectOptions;\n protected logger = log();\n\n #llm: LLM;\n #chatCtx: ChatContext;\n #toolCtx?: ToolContext;\n #llmRequestSpan?: Span;\n\n constructor(\n llm: LLM,\n {\n chatCtx,\n toolCtx,\n connOptions,\n }: {\n chatCtx: ChatContext;\n toolCtx?: ToolContext;\n connOptions: APIConnectOptions;\n },\n ) {\n this.#llm = llm;\n this.#chatCtx = chatCtx;\n this.#toolCtx = toolCtx;\n this._connOptions = connOptions;\n this.monitorMetrics();\n this.abortController.signal.addEventListener('abort', () => {\n // TODO (AJS-37) clean this up when we refactor with streams\n this.output.close();\n this.closed = true;\n });\n\n // this is a hack to immitate asyncio.create_task so that mainTask\n // is run **after** the constructor has finished. Otherwise we get\n // runtime error when trying to access class variables in the\n // `run` method.\n startSoon(() => this.mainTask().finally(() => this.queue.close()));\n }\n\n private _mainTaskImpl = async (span: Span) => {\n this.#llmRequestSpan = span;\n span.setAttribute(traceTypes.ATTR_GEN_AI_REQUEST_MODEL, this.#llm.model);\n\n for (let i = 0; i < this._connOptions.maxRetry + 1; i++) {\n try {\n return await tracer.startActiveSpan(\n async (attemptSpan) => {\n attemptSpan.setAttribute(traceTypes.ATTR_RETRY_COUNT, i);\n try {\n return await this.run();\n } catch (error) {\n recordException(attemptSpan, toError(error));\n throw error;\n }\n },\n { name: 'llm_request_run' },\n );\n } catch (error) {\n if (error instanceof APIError) {\n const retryInterval = intervalForRetry(this._connOptions, i);\n\n if (this._connOptions.maxRetry === 0 || !error.retryable) {\n this.emitError({ error, recoverable: false });\n throw error;\n } else if (i === this._connOptions.maxRetry) {\n this.emitError({ error, recoverable: false });\n throw new APIConnectionError({\n message: `failed to generate LLM completion after ${this._connOptions.maxRetry + 1} attempts`,\n options: { retryable: false },\n });\n } else {\n this.emitError({ error, recoverable: true });\n this.logger.warn(\n { llm: this.#llm.label(), attempt: i + 1, error },\n `failed to generate LLM completion, retrying in ${retryInterval}s`,\n );\n }\n\n if (retryInterval > 0) {\n await delay(retryInterval);\n }\n } else {\n this.emitError({ error: toError(error), recoverable: false });\n throw error;\n }\n }\n }\n };\n\n private mainTask = async () =>\n tracer.startActiveSpan(async (span) => this._mainTaskImpl(span), {\n name: 'llm_request',\n endOnExit: false,\n });\n\n private emitError({ error, recoverable }: { error: Error; recoverable: boolean }) {\n this.#llm.emit('error', {\n type: 'llm_error',\n timestamp: Date.now(),\n label: this.#llm.label(),\n error,\n recoverable,\n });\n }\n\n protected async monitorMetrics() {\n const startTime = process.hrtime.bigint();\n let ttft: bigint = BigInt(-1);\n let requestId = '';\n let usage: CompletionUsage | undefined;\n let completionStartTime: string | undefined;\n\n for await (const ev of this.queue) {\n if (this.abortController.signal.aborted) {\n break;\n }\n this.output.put(ev);\n requestId = ev.id;\n if (ttft === BigInt(-1)) {\n ttft = process.hrtime.bigint() - startTime;\n completionStartTime = new Date().toISOString();\n }\n if (ev.usage) {\n usage = ev.usage;\n }\n }\n this.output.close();\n\n const duration = process.hrtime.bigint() - startTime;\n const durationMs = Math.trunc(Number(duration / BigInt(1000000)));\n const metrics: LLMMetrics = {\n type: 'llm_metrics',\n timestamp: Date.now(),\n requestId,\n ttftMs: ttft === BigInt(-1) ? -1 : Math.trunc(Number(ttft / BigInt(1000000))),\n durationMs,\n cancelled: this.abortController.signal.aborted,\n label: this.#llm.label(),\n completionTokens: usage?.completionTokens || 0,\n promptTokens: usage?.promptTokens || 0,\n promptCachedTokens: usage?.promptCachedTokens || 0,\n totalTokens: usage?.totalTokens || 0,\n tokensPerSecond: (() => {\n if (durationMs <= 0) {\n return 0;\n }\n return (usage?.completionTokens || 0) / (durationMs / 1000);\n })(),\n };\n\n if (this.#llmRequestSpan) {\n this.#llmRequestSpan.setAttribute(traceTypes.ATTR_LLM_METRICS, JSON.stringify(metrics));\n\n this.#llmRequestSpan.setAttributes({\n [traceTypes.ATTR_GEN_AI_USAGE_INPUT_TOKENS]: metrics.promptTokens,\n [traceTypes.ATTR_GEN_AI_USAGE_OUTPUT_TOKENS]: metrics.completionTokens,\n });\n\n if (completionStartTime) {\n this.#llmRequestSpan.setAttribute(\n traceTypes.ATTR_LANGFUSE_COMPLETION_START_TIME,\n completionStartTime,\n );\n }\n\n // End the span now that metrics are collected\n this.#llmRequestSpan.end();\n }\n\n this.#llm.emit('metrics_collected', metrics);\n }\n\n protected abstract run(): Promise<void>;\n\n /** The function context of this stream. */\n get toolCtx(): ToolContext | undefined {\n return this.#toolCtx;\n }\n\n /** The initial chat context of this stream. */\n get chatCtx(): ChatContext {\n return this.#chatCtx;\n }\n\n /** The connection options for this stream. */\n get connOptions(): APIConnectOptions {\n return this._connOptions;\n }\n\n next(): Promise<IteratorResult<ChatChunk>> {\n return this.output.next();\n }\n\n close() {\n this.abortController.abort();\n }\n\n [Symbol.asyncIterator](): LLMStream {\n return this;\n }\n}\n"],"mappings":"AAKA,SAAS,oBAAoB;AAC7B,SAAS,oBAAoB,gBAAgB;AAC7C,SAAS,WAAW;AAEpB,SAAS,iBAAiB,YAAY,cAAc;A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AAA,IACL;AAEA,QAAI,KAAK,iBAAiB;AACxB,WAAK,gBAAgB,aAAa,WAAW,kBAAkB,KAAK,UAAU,OAAO,CAAC;AAEtF,WAAK,gBAAgB,cAAc;AAAA,QACjC,CAAC,WAAW,8BAA8B,GAAG,QAAQ;AAAA,QACrD,CAAC,WAAW,+BAA+B,GAAG,QAAQ;AAAA,MACxD,CAAC;AAED,UAAI,qBAAqB;AACvB,aAAK,gBAAgB;AAAA,UACnB,WAAW;AAAA,UACX;AAAA,QACF;AAAA,MACF;AAGA,WAAK,gBAAgB,IAAI;AAAA,IAC3B;AAEA,SAAK,KAAK,KAAK,qBAAqB,OAAO;AAAA,EAC7C;AAAA;AAAA,EAKA,IAAI,UAAmC;AACrC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,UAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,cAAiC;AACnC,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,OAA2C;AACzC,WAAO,KAAK,OAAO,KAAK;AAAA,EAC1B;AAAA,EAEA,QAAQ;AACN,SAAK,gBAAgB,MAAM;AAAA,EAC7B;AAAA,EAEA,CAAC,OAAO,aAAa,IAAe;AAClC,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -29,11 +29,18 @@ async function toChatCtx(chatCtx, injectDummyUserMessage = true) {
29
29
  for (const group of itemGroups) {
30
30
  if (group.isEmpty) continue;
31
31
  const message = group.message ? await toChatItem(group.message) : { role: "assistant" };
32
- const toolCalls = group.toolCalls.map((toolCall) => ({
33
- type: "function",
34
- id: toolCall.callId,
35
- function: { name: toolCall.name, arguments: toolCall.args }
36
- }));
32
+ const toolCalls = group.toolCalls.map((toolCall) => {
33
+ const tc = {
34
+ type: "function",
35
+ id: toolCall.callId,
36
+ function: { name: toolCall.name, arguments: toolCall.args }
37
+ };
38
+ const googleExtra = getGoogleExtra(toolCall);
39
+ if (googleExtra) {
40
+ tc.extra_content = { google: googleExtra };
41
+ }
42
+ return tc;
43
+ });
37
44
  if (toolCalls.length > 0) {
38
45
  message["tool_calls"] = toolCalls;
39
46
  }
@@ -48,28 +55,39 @@ async function toChatItem(item) {
48
55
  if (item.type === "message") {
49
56
  const listContent = [];
50
57
  let textContent = "";
51
- for (const content2 of item.content) {
52
- if (typeof content2 === "string") {
58
+ for (const content of item.content) {
59
+ if (typeof content === "string") {
53
60
  if (textContent) textContent += "\n";
54
- textContent += content2;
55
- } else if (content2.type === "image_content") {
56
- listContent.push(await toImageContent(content2));
61
+ textContent += content;
62
+ } else if (content.type === "image_content") {
63
+ listContent.push(await toImageContent(content));
57
64
  } else {
58
- throw new Error(`Unsupported content type: ${content2.type}`);
65
+ throw new Error(`Unsupported content type: ${content.type}`);
66
+ }
67
+ }
68
+ const result = { role: item.role };
69
+ if (listContent.length === 0) {
70
+ result.content = textContent;
71
+ } else {
72
+ if (textContent.length > 0) {
73
+ listContent.push({ type: "text", text: textContent });
59
74
  }
75
+ result.content = listContent;
60
76
  }
61
- const content = listContent.length == 0 ? textContent : textContent.length == 0 ? listContent : [...listContent, { type: "text", text: textContent }];
62
- return { role: item.role, content };
77
+ return result;
63
78
  } else if (item.type === "function_call") {
79
+ const tc = {
80
+ id: item.callId,
81
+ type: "function",
82
+ function: { name: item.name, arguments: item.args }
83
+ };
84
+ const googleExtra = getGoogleExtra(item);
85
+ if (googleExtra) {
86
+ tc.extra_content = { google: googleExtra };
87
+ }
64
88
  return {
65
89
  role: "assistant",
66
- tool_calls: [
67
- {
68
- id: item.callId,
69
- type: "function",
70
- function: { name: item.name, arguments: item.args }
71
- }
72
- ]
90
+ tool_calls: [tc]
73
91
  };
74
92
  } else if (item.type === "function_call_output") {
75
93
  return {
@@ -80,6 +98,11 @@ async function toChatItem(item) {
80
98
  }
81
99
  throw new Error(`Unsupported item type: ${item["type"]}`);
82
100
  }
101
+ function getGoogleExtra(item) {
102
+ var _a;
103
+ const googleExtra = ((_a = item.extra) == null ? void 0 : _a.google) || (item.thoughtSignature ? { thoughtSignature: item.thoughtSignature } : void 0);
104
+ return googleExtra;
105
+ }
83
106
  async function toImageContent(content) {
84
107
  const cacheKey = "serialized_image";
85
108
  let serialized;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/llm/provider_format/openai.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChatContext, ChatItem, ImageContent } from '../chat_context.js';\nimport { type SerializedImage, serializeImage } from '../utils.js';\nimport { groupToolCalls } from './utils.js';\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport async function toChatCtx(chatCtx: ChatContext, injectDummyUserMessage: boolean = true) {\n const itemGroups = groupToolCalls(chatCtx);\n const messages: Record<string, any>[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n for (const group of itemGroups) {\n if (group.isEmpty) continue;\n\n const message: Record<string, any> = group.message // eslint-disable-line @typescript-eslint/no-explicit-any\n ? await toChatItem(group.message)\n : { role: 'assistant' };\n\n const toolCalls = group.toolCalls.map((toolCall) => ({\n type: 'function',\n id: toolCall.callId,\n function: { name: toolCall.name, arguments: toolCall.args },\n }));\n\n if (toolCalls.length > 0) {\n message['tool_calls'] = toolCalls;\n }\n\n messages.push(message);\n\n for (const toolOutput of group.toolOutputs) {\n messages.push(await toChatItem(toolOutput));\n }\n }\n\n return messages;\n}\n\nasync function toChatItem(item: ChatItem) {\n if (item.type === 'message') {\n const listContent: Record<string, any>[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any\n let textContent = '';\n\n for (const content of item.content) {\n if (typeof content === 'string') {\n if (textContent) textContent += '\\n';\n textContent += content;\n } else if (content.type === 'image_content') {\n listContent.push(await toImageContent(content));\n } else {\n throw new Error(`Unsupported content type: ${content.type}`);\n }\n }\n\n const content =\n listContent.length == 0\n ? textContent\n : textContent.length == 0\n ? listContent\n : [...listContent, { type: 'text', text: textContent }];\n\n return { role: item.role, content };\n } else if (item.type === 'function_call') {\n return {\n role: 'assistant',\n tool_calls: [\n {\n id: item.callId,\n type: 'function',\n function: { name: item.name, arguments: item.args },\n },\n ],\n };\n } else if (item.type === 'function_call_output') {\n return {\n role: 'tool',\n tool_call_id: item.callId,\n content: item.output,\n };\n }\n // Skip other item types (e.g., agent_handoff)\n // These should be filtered by groupToolCalls, but this is a safety net\n throw new Error(`Unsupported item type: ${item['type']}`);\n}\n\nasync function toImageContent(content: ImageContent) {\n const cacheKey = 'serialized_image'; // TODO: use hash of encoding options if available\n let serialized: SerializedImage;\n\n if (content._cache[cacheKey] === undefined) {\n serialized = await serializeImage(content);\n content._cache[cacheKey] = serialized;\n }\n serialized = content._cache[cacheKey];\n\n // Convert SerializedImage to OpenAI format\n if (serialized.externalUrl) {\n return {\n type: 'image_url',\n image_url: {\n url: serialized.externalUrl,\n detail: serialized.inferenceDetail,\n },\n };\n }\n\n if (serialized.base64Data === undefined) {\n throw new Error('Serialized image has no data bytes');\n }\n\n return {\n type: 'image_url',\n image_url: {\n url: `data:${serialized.mimeType};base64,${serialized.base64Data}`,\n detail: serialized.inferenceDetail,\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,mBAAqD;AACrD,IAAAA,gBAA+B;AAG/B,eAAsB,UAAU,SAAsB,yBAAkC,MAAM;AAC5F,QAAM,iBAAa,8BAAe,OAAO;AACzC,QAAM,WAAkC,CAAC;AAEzC,aAAW,SAAS,YAAY;AAC9B,QAAI,MAAM,QAAS;AAEnB,UAAM,UAA+B,MAAM,UACvC,MAAM,WAAW,MAAM,OAAO,IAC9B,EAAE,MAAM,YAAY;AAExB,UAAM,YAAY,MAAM,UAAU,IAAI,CAAC,cAAc;AAAA,MACnD,MAAM;AAAA,MACN,IAAI,SAAS;AAAA,MACb,UAAU,EAAE,MAAM,SAAS,MAAM,WAAW,SAAS,KAAK;AAAA,IAC5D,EAAE;AAEF,QAAI,UAAU,SAAS,GAAG;AACxB,cAAQ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AAM,QAAW;AAC1C,iBAAa,UAAM,6BAAe,OAAO;AACzC,YAAQ,OAAO,QAAQ,IAAI;AAAA,EAC7B;AACA,eAAa,QAAQ,OAAO,QAAQ;AAGpC,MAAI,WAAW,aAAa;AAC1B,WAAO;AAAA,MACL,MAAM;AAAA,MACN,WAAW;AAAA,QACT,KAAK,WAAW;AAAA,QAChB,QAAQ,WAAW;AAAA,MACrB;AAAA,IACF;AAAA,EACF;AAEA,MAAI,WAAW,eAAe,QAAW;AACvC,UAAM,IAAI,MAAM,oCAAoC;AAAA,EACtD;AAEA,SAAO;AAAA,IACL,MAAM;AAAA,IACN,WAAW;AAAA,MACT,KAAK,QAAQ,WAAW,QAAQ,WAAW,WAAW,UAAU;AAAA,MAChE,QAAQ,WAAW;AAAA,IACrB;AAAA,EACF;AACF;","names":["import_utils","content"]}
1
+ {"version":3,"sources":["../../../src/llm/provider_format/openai.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChatContext, ChatItem, ImageContent } from '../chat_context.js';\nimport { type SerializedImage, serializeImage } from '../utils.js';\nimport { groupToolCalls } from './utils.js';\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport async function toChatCtx(chatCtx: ChatContext, injectDummyUserMessage: boolean = true) {\n const itemGroups = groupToolCalls(chatCtx);\n const messages: Record<string, any>[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n for (const group of itemGroups) {\n if (group.isEmpty) continue;\n\n const message: Record<string, any> = group.message // eslint-disable-line @typescript-eslint/no-explicit-any\n ? await toChatItem(group.message)\n : { role: 'assistant' };\n\n const toolCalls = group.toolCalls.map((toolCall) => {\n const tc: Record<string, any> = {\n type: 'function',\n id: toolCall.callId,\n function: { name: toolCall.name, arguments: toolCall.args },\n };\n\n // Include provider-specific extra content (e.g., Google thought signatures)\n const googleExtra = getGoogleExtra(toolCall);\n if (googleExtra) {\n tc.extra_content = { google: googleExtra };\n }\n return tc;\n });\n\n if (toolCalls.length > 0) {\n message['tool_calls'] = toolCalls;\n }\n\n messages.push(message);\n\n for (const toolOutput of group.toolOutputs) {\n messages.push(await toChatItem(toolOutput));\n }\n }\n\n return messages;\n}\n\nasync function toChatItem(item: ChatItem) {\n if (item.type === 'message') {\n const listContent: Record<string, any>[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any\n let textContent = '';\n\n for (const content of item.content) {\n if (typeof content === 'string') {\n if (textContent) textContent += '\\n';\n textContent += content;\n } else if (content.type === 'image_content') {\n listContent.push(await toImageContent(content));\n } else {\n throw new Error(`Unsupported content type: ${content.type}`);\n }\n }\n\n const result: Record<string, any> = { role: item.role };\n if (listContent.length === 0) {\n result.content = textContent;\n } else {\n if (textContent.length > 0) {\n listContent.push({ type: 'text', text: textContent });\n }\n result.content = listContent;\n }\n\n return result;\n } else if (item.type === 'function_call') {\n const tc: Record<string, any> = {\n id: item.callId,\n type: 'function',\n function: { name: item.name, arguments: item.args },\n };\n\n // Include provider-specific extra content (e.g., Google thought signatures)\n const googleExtra = getGoogleExtra(item);\n if (googleExtra) {\n tc.extra_content = { google: googleExtra };\n }\n\n return {\n role: 'assistant',\n tool_calls: [tc],\n };\n } else if (item.type === 'function_call_output') {\n return {\n role: 'tool',\n tool_call_id: item.callId,\n content: item.output,\n };\n }\n // Skip other item types (e.g., agent_handoff)\n // These should be filtered by groupToolCalls, but this is a safety net\n throw new Error(`Unsupported item type: ${item['type']}`);\n}\n\nfunction getGoogleExtra(\n item: Partial<{ extra?: Record<string, unknown>; thoughtSignature?: string }>,\n): Record<string, unknown> | undefined {\n const googleExtra =\n (item.extra?.google as Record<string, unknown> | undefined) ||\n (item.thoughtSignature ? { thoughtSignature: item.thoughtSignature } : undefined);\n return googleExtra;\n}\n\nasync function toImageContent(content: ImageContent) {\n const cacheKey = 'serialized_image'; // TODO: use hash of encoding options if available\n let serialized: SerializedImage;\n\n if (content._cache[cacheKey] === undefined) {\n serialized = await serializeImage(content);\n content._cache[cacheKey] = serialized;\n }\n serialized = content._cache[cacheKey];\n\n // Convert SerializedImage to OpenAI format\n if (serialized.externalUrl) {\n return {\n type: 'image_url',\n image_url: {\n url: serialized.externalUrl,\n detail: serialized.inferenceDetail,\n },\n };\n }\n\n if (serialized.base64Data === undefined) {\n throw new Error('Serialized image has no data bytes');\n }\n\n return {\n type: 'image_url',\n image_url: {\n url: `data:${serialized.mimeType};base64,${serialized.base64Data}`,\n detail: serialized.inferenceDetail,\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,mBAAqD;AACrD,IAAAA,gBAA+B;AAG/B,eAAsB,UAAU,SAAsB,yBAAkC,MAAM;AAC5F,QAAM,iBAAa,8BAAe,OAAO;AACzC,QAAM,WAAkC,CAAC;AAEzC,aAAW,SAAS,YAAY;AAC9B,QAAI,MAAM,QAAS;AAEnB,UAAM,UAA+B,MAAM,UACvC,MAAM,WAAW,MAAM,OAAO,IAC9B,EAAE,MAAM,YAAY;AAExB,UAAM,YAAY,MAAM,UAAU,IAAI,CAAC,aAAa;AAClD,YAAM,KAA0B;AAAA,QAC9B,MAAM;AAAA,QACN,IAAI,SAAS;AAAA,QACb,UAAU,EAAE,MAAM,SAAS,MAAM,WAAW,SAAS,KAAK;AAAA,MAC5D;AAGA,YAAM,cAAc,eAAe,QAAQ;AAC3C,UAAI,aAAa;AACf,WAAG,gBAAgB,EAAE,QAAQ,YAAY;AAAA,MAC3C;AACA,aAAO;AAAA,IACT,CAAC;AAED,QAAI,UAAU,SAAS,GAAG;AACxB,cAAQ,YAAY,IAAI;AAAA,IAC1B;AAEA,aAAS,KAAK,OAAO;AAErB,eAAW,cAAc,MAAM,aAAa;AAC1C,eAAS,KAAK,MAAM,WAAW,UAAU,CAAC;AAAA,IAC5C;AAAA,EACF;AAEA,SAAO;AACT;AAEA,eAAe,WAAW,MAAgB;AACxC,MAAI,KAAK,SAAS,WAAW;AAC3B,UAAM,c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AO;AACT;AAEA,eAAe,eAAe,SAAuB;AACnD,QAAM,WAAW;AACjB,MAAI;AAEJ,MAAI,QAAQ,OAAO,QAAQ,MAAM,QAAW;AAC1C,iBAAa,UAAM,6BAAe,OAAO;AACzC,YAAQ,OAAO,QAAQ,IAAI;AAAA,EAC7B;AACA,eAAa,QAAQ,OAAO,QAAQ;AAGpC,MAAI,WAAW,aAAa;AAC1B,WAAO;AAAA,MACL,MAAM;AAAA,MACN,WAAW;AAAA,QACT,KAAK,WAAW;AAAA,QAChB,QAAQ,WAAW;AAAA,MACrB;AAAA,IACF;AAAA,EACF;AAEA,MAAI,WAAW,eAAe,QAAW;AACvC,UAAM,IAAI,MAAM,oCAAoC;AAAA,EACtD;AAEA,SAAO;AAAA,IACL,MAAM;AAAA,IACN,WAAW;AAAA,MACT,KAAK,QAAQ,WAAW,QAAQ,WAAW,WAAW,UAAU;AAAA,MAChE,QAAQ,WAAW;AAAA,IACrB;AAAA,EACF;AACF;","names":["import_utils"]}
@@ -1 +1 @@
1
- {"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../../src/llm/provider_format/openai.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAA0B,MAAM,oBAAoB,CAAC;AAK9E,wBAAsB,SAAS,CAAC,OAAO,EAAE,WAAW,EAAE,sBAAsB,GAAE,OAAc,kCA6B3F"}
1
+ {"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../../src/llm/provider_format/openai.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAA0B,MAAM,oBAAoB,CAAC;AAK9E,wBAAsB,SAAS,CAAC,OAAO,EAAE,WAAW,EAAE,sBAAsB,GAAE,OAAc,kCAsC3F"}
@@ -6,11 +6,18 @@ async function toChatCtx(chatCtx, injectDummyUserMessage = true) {
6
6
  for (const group of itemGroups) {
7
7
  if (group.isEmpty) continue;
8
8
  const message = group.message ? await toChatItem(group.message) : { role: "assistant" };
9
- const toolCalls = group.toolCalls.map((toolCall) => ({
10
- type: "function",
11
- id: toolCall.callId,
12
- function: { name: toolCall.name, arguments: toolCall.args }
13
- }));
9
+ const toolCalls = group.toolCalls.map((toolCall) => {
10
+ const tc = {
11
+ type: "function",
12
+ id: toolCall.callId,
13
+ function: { name: toolCall.name, arguments: toolCall.args }
14
+ };
15
+ const googleExtra = getGoogleExtra(toolCall);
16
+ if (googleExtra) {
17
+ tc.extra_content = { google: googleExtra };
18
+ }
19
+ return tc;
20
+ });
14
21
  if (toolCalls.length > 0) {
15
22
  message["tool_calls"] = toolCalls;
16
23
  }
@@ -25,28 +32,39 @@ async function toChatItem(item) {
25
32
  if (item.type === "message") {
26
33
  const listContent = [];
27
34
  let textContent = "";
28
- for (const content2 of item.content) {
29
- if (typeof content2 === "string") {
35
+ for (const content of item.content) {
36
+ if (typeof content === "string") {
30
37
  if (textContent) textContent += "\n";
31
- textContent += content2;
32
- } else if (content2.type === "image_content") {
33
- listContent.push(await toImageContent(content2));
38
+ textContent += content;
39
+ } else if (content.type === "image_content") {
40
+ listContent.push(await toImageContent(content));
34
41
  } else {
35
- throw new Error(`Unsupported content type: ${content2.type}`);
42
+ throw new Error(`Unsupported content type: ${content.type}`);
43
+ }
44
+ }
45
+ const result = { role: item.role };
46
+ if (listContent.length === 0) {
47
+ result.content = textContent;
48
+ } else {
49
+ if (textContent.length > 0) {
50
+ listContent.push({ type: "text", text: textContent });
36
51
  }
52
+ result.content = listContent;
37
53
  }
38
- const content = listContent.length == 0 ? textContent : textContent.length == 0 ? listContent : [...listContent, { type: "text", text: textContent }];
39
- return { role: item.role, content };
54
+ return result;
40
55
  } else if (item.type === "function_call") {
56
+ const tc = {
57
+ id: item.callId,
58
+ type: "function",
59
+ function: { name: item.name, arguments: item.args }
60
+ };
61
+ const googleExtra = getGoogleExtra(item);
62
+ if (googleExtra) {
63
+ tc.extra_content = { google: googleExtra };
64
+ }
41
65
  return {
42
66
  role: "assistant",
43
- tool_calls: [
44
- {
45
- id: item.callId,
46
- type: "function",
47
- function: { name: item.name, arguments: item.args }
48
- }
49
- ]
67
+ tool_calls: [tc]
50
68
  };
51
69
  } else if (item.type === "function_call_output") {
52
70
  return {
@@ -57,6 +75,11 @@ async function toChatItem(item) {
57
75
  }
58
76
  throw new Error(`Unsupported item type: ${item["type"]}`);
59
77
  }
78
+ function getGoogleExtra(item) {
79
+ var _a;
80
+ const googleExtra = ((_a = item.extra) == null ? void 0 : _a.google) || (item.thoughtSignature ? { thoughtSignature: item.thoughtSignature } : void 0);
81
+ return googleExtra;
82
+ }
60
83
  async function toImageContent(content) {
61
84
  const cacheKey = "serialized_image";
62
85
  let serialized;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/llm/provider_format/openai.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChatContext, ChatItem, ImageContent } from '../chat_context.js';\nimport { type SerializedImage, serializeImage } from '../utils.js';\nimport { groupToolCalls } from './utils.js';\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport async function toChatCtx(chatCtx: ChatContext, injectDummyUserMessage: boolean = true) {\n const itemGroups = groupToolCalls(chatCtx);\n const messages: Record<string, any>[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n for (const group of itemGroups) {\n if (group.isEmpty) continue;\n\n const message: Record<string, any> = group.message // eslint-disable-line @typescript-eslint/no-explicit-any\n ? await toChatItem(group.message)\n : { role: 'assistant' };\n\n const toolCalls = group.toolCalls.map((toolCall) => ({\n type: 'function',\n id: toolCall.callId,\n function: { name: toolCall.name, arguments: toolCall.args },\n }));\n\n if (toolCalls.length > 0) {\n message['tool_calls'] = toolCalls;\n }\n\n messages.push(message);\n\n for (const toolOutput of group.toolOutputs) {\n messages.push(await toChatItem(toolOutput));\n }\n }\n\n return messages;\n}\n\nasync function toChatItem(item: ChatItem) {\n if (item.type === 'message') {\n const listContent: Record<string, any>[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any\n let textContent = '';\n\n for (const content of item.content) {\n if (typeof content === 'string') {\n if (textContent) textContent += '\\n';\n textContent += content;\n } else if (content.type === 'image_content') {\n listContent.push(await toImageContent(content));\n } else {\n throw new Error(`Unsupported content type: ${content.type}`);\n }\n }\n\n const content =\n listContent.length == 0\n ? textContent\n : textContent.length == 0\n ? listContent\n : [...listContent, { type: 'text', text: textContent }];\n\n return { role: item.role, content };\n } else if (item.type === 'function_call') {\n return {\n role: 'assistant',\n tool_calls: [\n {\n id: item.callId,\n type: 'function',\n function: { name: item.name, arguments: item.args },\n },\n ],\n };\n } else if (item.type === 'function_call_output') {\n return {\n role: 'tool',\n tool_call_id: item.callId,\n content: item.output,\n };\n }\n // Skip other item types (e.g., agent_handoff)\n // These should be filtered by groupToolCalls, but this is a safety net\n throw new Error(`Unsupported item type: ${item['type']}`);\n}\n\nasync function toImageContent(content: ImageContent) {\n const cacheKey = 'serialized_image'; // TODO: use hash of encoding options if available\n let serialized: SerializedImage;\n\n if (content._cache[cacheKey] === undefined) {\n serialized = await serializeImage(content);\n content._cache[cacheKey] = serialized;\n }\n serialized = content._cache[cacheKey];\n\n // Convert SerializedImage to OpenAI format\n if (serialized.externalUrl) {\n return {\n type: 'image_url',\n image_url: {\n url: serialized.externalUrl,\n detail: serialized.inferenceDetail,\n },\n };\n }\n\n if (serialized.base64Data === undefined) {\n throw new Error('Serialized image has no data bytes');\n }\n\n return {\n type: 'image_url',\n image_url: {\n url: `data:${serialized.mimeType};base64,${serialized.base64Data}`,\n detail: serialized.inferenceDetail,\n },\n };\n}\n"],"mappings":"AAIA,SAA+B,sBAAsB;AACrD,SAAS,sBAAsB;AAG/B,eAAsB,UAAU,SAAsB,yBAAkC,MAAM;AAC5F,QAAM,aAAa,eAAe,OAAO;AACzC,QAAM,WAAkC,CAAC;AAEzC,aAAW,SAAS,YAAY;AAC9B,QAAI,MAAM,QAAS;AAEnB,UAAM,UAA+B,MAAM,UACvC,MAAM,WAAW,MAAM,OAAO,I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ACL,MAAM;AAAA,MACN,cAAc,KAAK;AAAA,MACnB,SAAS,KAAK;AAAA,IAChB;AAAA,EACF;AAGA,QAAM,IAAI,MAAM,0BAA0B,KAAK,MAAM,CAAC,EAAE;AAC1D;AAEA,eAAe,eAAe,SAAuB;AACnD,QAAM,WAAW;AACjB,MAAI;AAEJ,MAAI,QAAQ,OAAO,QAAQ,MAAM,QAAW;AAC1C,iBAAa,MAAM,eAAe,OAAO;AACzC,YAAQ,OAAO,QAAQ,IAAI;AAAA,EAC7B;AACA,eAAa,QAAQ,OAAO,QAAQ;AAGpC,MAAI,WAAW,aAAa;AAC1B,WAAO;AAAA,MACL,MAAM;AAAA,MACN,WAAW;AAAA,QACT,KAAK,WAAW;AAAA,QAChB,QAAQ,WAAW;AAAA,MACrB;AAAA,IACF;AAAA,EACF;AAEA,MAAI,WAAW,eAAe,QAAW;AACvC,UAAM,IAAI,MAAM,oCAAoC;AAAA,EACtD;AAEA,SAAO;AAAA,IACL,MAAM;AAAA,IACN,WAAW;AAAA,MACT,KAAK,QAAQ,WAAW,QAAQ,WAAW,WAAW,UAAU;AAAA,MAChE,QAAQ,WAAW;AAAA,IACrB;AAAA,EACF;AACF;","names":["content"]}
1
+ {"version":3,"sources":["../../../src/llm/provider_format/openai.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { ChatContext, ChatItem, ImageContent } from '../chat_context.js';\nimport { type SerializedImage, serializeImage } from '../utils.js';\nimport { groupToolCalls } from './utils.js';\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nexport async function toChatCtx(chatCtx: ChatContext, injectDummyUserMessage: boolean = true) {\n const itemGroups = groupToolCalls(chatCtx);\n const messages: Record<string, any>[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any\n\n for (const group of itemGroups) {\n if (group.isEmpty) continue;\n\n const message: Record<string, any> = group.message // eslint-disable-line @typescript-eslint/no-explicit-any\n ? await toChatItem(group.message)\n : { role: 'assistant' };\n\n const toolCalls = group.toolCalls.map((toolCall) => {\n const tc: Record<string, any> = {\n type: 'function',\n id: toolCall.callId,\n function: { name: toolCall.name, arguments: toolCall.args },\n };\n\n // Include provider-specific extra content (e.g., Google thought signatures)\n const googleExtra = getGoogleExtra(toolCall);\n if (googleExtra) {\n tc.extra_content = { google: googleExtra };\n }\n return tc;\n });\n\n if (toolCalls.length > 0) {\n message['tool_calls'] = toolCalls;\n }\n\n messages.push(message);\n\n for (const toolOutput of group.toolOutputs) {\n messages.push(await toChatItem(toolOutput));\n }\n }\n\n return messages;\n}\n\nasync function toChatItem(item: ChatItem) {\n if (item.type === 'message') {\n const listContent: Record<string, any>[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any\n let textContent = '';\n\n for (const content of item.content) {\n if (typeof content === 'string') {\n if (textContent) textContent += '\\n';\n textContent += content;\n } else if (content.type === 'image_content') {\n listContent.push(await toImageContent(content));\n } else {\n throw new Error(`Unsupported content type: ${content.type}`);\n }\n }\n\n const result: Record<string, any> = { role: item.role };\n if (listContent.length === 0) {\n result.content = textContent;\n } else {\n if (textContent.length > 0) {\n listContent.push({ type: 'text', text: textContent });\n }\n result.content = listContent;\n }\n\n return result;\n } else if (item.type === 'function_call') {\n const tc: Record<string, any> = {\n id: item.callId,\n type: 'function',\n function: { name: item.name, arguments: item.args },\n };\n\n // Include provider-specific extra content (e.g., Google thought signatures)\n const googleExtra = getGoogleExtra(item);\n if (googleExtra) {\n tc.extra_content = { google: googleExtra };\n }\n\n return {\n role: 'assistant',\n tool_calls: [tc],\n };\n } else if (item.type === 'function_call_output') {\n return {\n role: 'tool',\n tool_call_id: item.callId,\n content: item.output,\n };\n }\n // Skip other item types (e.g., agent_handoff)\n // These should be filtered by groupToolCalls, but this is a safety net\n throw new Error(`Unsupported item type: ${item['type']}`);\n}\n\nfunction getGoogleExtra(\n item: Partial<{ extra?: Record<string, unknown>; thoughtSignature?: string }>,\n): Record<string, unknown> | undefined {\n const googleExtra =\n (item.extra?.google as Record<string, unknown> | undefined) ||\n (item.thoughtSignature ? { thoughtSignature: item.thoughtSignature } : undefined);\n return googleExtra;\n}\n\nasync function toImageContent(content: ImageContent) {\n const cacheKey = 'serialized_image'; // TODO: use hash of encoding options if available\n let serialized: SerializedImage;\n\n if (content._cache[cacheKey] === undefined) {\n serialized = await serializeImage(content);\n content._cache[cacheKey] = serialized;\n }\n serialized = content._cache[cacheKey];\n\n // Convert SerializedImage to OpenAI format\n if (serialized.externalUrl) {\n return {\n type: 'image_url',\n image_url: {\n url: serialized.externalUrl,\n detail: serialized.inferenceDetail,\n },\n };\n }\n\n if (serialized.base64Data === undefined) {\n throw new Error('Serialized image has no data bytes');\n }\n\n return {\n type: 'image_url',\n image_url: {\n url: `data:${serialized.mimeType};base64,${serialized.base64Data}`,\n detail: serialized.inferenceDetail,\n },\n };\n}\n"],"mappings":"AAIA,SAA+B,sBAAsB;AACrD,SAAS,sBAAsB;AAG/B,eAAsB,UAAU,SAAsB,yBAAkC,MAAM;AAC5F,QAAM,aAAa,eAAe,OAAO;AACzC,QAAM,WAAkC,CAAC;AAEzC,aAAW,SAAS,YAAY;AAC9B,QAAI,MAAM,QAAS;AAEnB,UAAM,UAA+B,MAAM,UACvC,MAAM,WAAW,MAAM,OAAO,IAC9B,EAAE,MAAM,YAAY;AAExB,UAAM,YAAY,MAAM,UAAU,IAAI,CAAC,aAAa;AAClD,YAAM,KAA0B;AAAA,QAC9B,MAAM;AAAA,QACN,IAAI,SAAS;AAAA,QACb,UAAU,EAAE,MAAM,SAAS,MAAM,WAAW,SAAS,KAAK;AAAA,MAC5D;AAGA,YAAM,cAAc,eAAe,QAAQ;AAC3C,UAAI,aAAa;AACf,WAAG,gBAAgB,EAAE,QAAQ,YAAY;AAAA,MAC3C;AACA,aAAO;AAAA,IACT,CAAC;AAED,QAAI,UAAU,SAAS,GAAG;AACxB,cAAQ,YAAY,IAAI;AAAA,IAC1B;AAEA,aAAS,KAAK,OAAO;AAErB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MAAM,0BAA0B,KAAK,MAAM,CAAC,EAAE;AAC1D;AAEA,SAAS,eACP,MACqC;AA1GvC;AA2GE,QAAM,gBACH,UAAK,UAAL,mBAAY,YACZ,KAAK,mBAAmB,EAAE,kBAAkB,KAAK,iBAAiB,IAAI;AACzE,SAAO;AACT;AAEA,eAAe,eAAe,SAAuB;AACnD,QAAM,WAAW;AACjB,MAAI;AAEJ,MAAI,QAAQ,OAAO,QAAQ,MAAM,QAAW;AAC1C,iBAAa,MAAM,eAAe,OAAO;AACzC,YAAQ,OAAO,QAAQ,IAAI;AAAA,EAC7B;AACA,eAAa,QAAQ,OAAO,QAAQ;AAGpC,MAAI,WAAW,aAAa;AAC1B,WAAO;AAAA,MACL,MAAM;AAAA,MACN,WAAW;AAAA,QACT,KAAK,WAAW;AAAA,QAChB,QAAQ,WAAW;AAAA,MACrB;AAAA,IACF;AAAA,EACF;AAEA,MAAI,WAAW,eAAe,QAAW;AACvC,UAAM,IAAI,MAAM,oCAAoC;AAAA,EACtD;AAEA,SAAO;AAAA,IACL,MAAM;AAAA,IACN,WAAW;AAAA,MACT,KAAK,QAAQ,WAAW,QAAQ,WAAW,WAAW,UAAU;AAAA,MAChE,QAAQ,WAAW;AAAA,IACrB;AAAA,EACF;AACF;","names":[]}
@@ -207,6 +207,41 @@ import_vitest.vi.mock("../utils.js", () => ({
207
207
  }
208
208
  ]);
209
209
  });
210
+ (0, import_vitest.it)("should include provider-specific extra content on tool calls", async () => {
211
+ const ctx = import_chat_context.ChatContext.empty();
212
+ const msg = ctx.addMessage({ role: "assistant", content: "Running tool" });
213
+ const toolCall = import_chat_context.FunctionCall.create({
214
+ id: `${msg.id}/tool_1`,
215
+ callId: "call_789",
216
+ name: "google_call",
217
+ args: "{}",
218
+ extra: { google: { thoughtSignature: "sig-123" } }
219
+ });
220
+ const toolOutput = import_chat_context.FunctionCallOutput.create({
221
+ callId: "call_789",
222
+ output: '{"result": "ok"}',
223
+ isError: false
224
+ });
225
+ ctx.insert([toolCall, toolOutput]);
226
+ const result = await (0, import_openai.toChatCtx)(ctx);
227
+ (0, import_vitest.expect)(result[0]).toEqual({
228
+ role: "assistant",
229
+ content: "Running tool",
230
+ tool_calls: [
231
+ {
232
+ type: "function",
233
+ id: "call_789",
234
+ function: { name: "google_call", arguments: "{}" },
235
+ extra_content: { google: { thoughtSignature: "sig-123" } }
236
+ }
237
+ ]
238
+ });
239
+ (0, import_vitest.expect)(result[1]).toEqual({
240
+ role: "tool",
241
+ tool_call_id: "call_789",
242
+ content: '{"result": "ok"}'
243
+ });
244
+ });
210
245
  (0, import_vitest.it)("should handle multiple tool calls in one message", async () => {
211
246
  const ctx = import_chat_context.ChatContext.empty();
212
247
  const msg = ctx.addMessage({ role: "assistant", content: "I'll check both locations." });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/llm/provider_format/openai.test.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { VideoBufferType, VideoFrame } from '@livekit/rtc-node';\nimport { beforeEach, describe, expect, it, vi } from 'vitest';\nimport { initializeLogger } from '../../log.js';\nimport {\n AgentHandoffItem,\n ChatContext,\n FunctionCall,\n FunctionCallOutput,\n} from '../chat_context.js';\nimport { serializeImage } from '../utils.js';\nimport { toChatCtx } from './openai.js';\n\n// Mock the serializeImage function\nvi.mock('../utils.js', () => ({\n serializeImage: vi.fn(),\n}));\n\ndescribe('toChatCtx', () => {\n const serializeImageMock = vi.mocked(serializeImage);\n\n // initialize logger at start of test\n initializeLogger({ level: 'silent', pretty: false });\n\n beforeEach(async () => {\n vi.clearAllMocks();\n });\n\n it('should convert simple text messages', async () => {\n const ctx = ChatContext.empty();\n ctx.addMessage({ role: 'user', content: 'Hello' });\n ctx.addMessage({ role: 'assistant', content: 'Hi there!' });\n\n const result = await toChatCtx(ctx);\n\n // Messages should be in the result, order may vary due to ID-based sorting\n expect(result).toHaveLength(2);\n expect(result[0]).toEqual({ role: 'user', content: 'Hello' });\n expect(result[1]).toEqual({ role: 'assistant', content: 'Hi there!' });\n });\n\n it('should handle system messages', async () => {\n const ctx = ChatContext.empty();\n ctx.addMessage({ role: 'system', content: 'You are a helpful assistant' });\n ctx.addMessage({ role: 'user', content: 'Hello' });\n\n const result = await toChatCtx(ctx);\n\n // Messages should be in the result, order may vary due to ID-based sorting\n expect(result).toHaveLength(2);\n expect(result[0]).toEqual({ role: 'system', content: 'You are a helpful assistant' });\n expect(result[1]).toEqual({ role: 'user', content: 'Hello' });\n });\n\n it('should handle multi-line text content', async () => {\n const ctx = ChatContext.empty();\n ctx.addMessage({ role: 'user', content: ['Line 1', 'Line 2', 'Line 3'] });\n\n const result = await toChatCtx(ctx);\n\n expect(result).toHaveLength(1);\n expect(result[0]).toEqual({ role: 'user', content: 'Line 1\\nLine 2\\nLine 3' });\n });\n\n it('should handle messages with external URL images', async () => {\n serializeImageMock.mockResolvedValue({\n inferenceDetail: 'high',\n externalUrl: 'https://example.com/image.jpg',\n });\n\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'user',\n content: [\n 'Check out this image:',\n {\n id: 'img1',\n type: 'image_content',\n image: 'https://example.com/image.jpg',\n inferenceDetail: 'high',\n _cache: {},\n },\n ],\n });\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'user',\n content: [\n {\n type: 'image_url',\n image_url: {\n url: 'https://example.com/image.jpg',\n detail: 'high',\n },\n },\n { type: 'text', text: 'Check out this image:' },\n ],\n },\n ]);\n });\n\n it('should handle messages with base64 images', async () => {\n serializeImageMock.mockResolvedValue({\n inferenceDetail: 'auto',\n mimeType: 'image/png',\n base64Data: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB',\n });\n\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'assistant',\n content: [\n {\n id: 'img1',\n type: 'image_content',\n image: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB',\n inferenceDetail: 'auto',\n _cache: {},\n },\n 'Here is the image you requested',\n ],\n });\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'assistant',\n content: [\n {\n type: 'image_url',\n image_url: {\n url: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB',\n detail: 'auto',\n },\n },\n { type: 'text', text: 'Here is the image you requested' },\n ],\n },\n ]);\n });\n\n it('should handle VideoFrame images', async () => {\n serializeImageMock.mockResolvedValue({\n inferenceDetail: 'low',\n mimeType: 'image/jpeg',\n base64Data: '/9j/4AAQSkZJRg==',\n });\n\n const frameData = new Uint8Array(4 * 4 * 4); // 4x4 RGBA\n const videoFrame = new VideoFrame(frameData, 4, 4, VideoBufferType.RGBA);\n\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'user',\n content: [\n {\n id: 'frame1',\n type: 'image_content',\n image: videoFrame,\n inferenceDetail: 'low',\n _cache: {},\n },\n ],\n });\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'user',\n content: [\n {\n type: 'image_url',\n image_url: {\n url: 'data:image/jpeg;base64,/9j/4AAQSkZJRg==',\n detail: 'low',\n },\n },\n ],\n },\n ]);\n });\n\n it('should cache serialized images', async () => {\n serializeImageMock.mockResolvedValue({\n inferenceDetail: 'high',\n mimeType: 'image/png',\n base64Data: 'cached-data',\n });\n\n const imageContent = {\n id: 'img1',\n type: 'image_content' as const,\n image: 'https://example.com/image.jpg',\n inferenceDetail: 'high' as const,\n _cache: {},\n };\n\n const ctx = ChatContext.empty();\n ctx.addMessage({ role: 'user', content: [imageContent] });\n\n // Call twice to test caching\n await toChatCtx(ctx);\n await toChatCtx(ctx);\n\n // serializeImage should only be called once due to caching\n expect(serializeImageMock).toHaveBeenCalledTimes(1);\n expect(imageContent._cache).toHaveProperty('serialized_image');\n });\n\n it('should handle tool calls and outputs', async () => {\n const ctx = ChatContext.empty();\n\n // Add an assistant message with tool calls\n const msg = ctx.addMessage({ role: 'assistant', content: 'Let me help you with that.' });\n const toolCall = FunctionCall.create({\n id: msg.id + '/tool_1',\n callId: 'call_123',\n name: 'get_weather',\n args: '{\"location\": \"San Francisco\"}',\n });\n const toolOutput = FunctionCallOutput.create({\n callId: 'call_123',\n output: '{\"temperature\": 72, \"condition\": \"sunny\"}',\n isError: false,\n });\n\n ctx.insert([toolCall, toolOutput]);\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'assistant',\n content: 'Let me help you with that.',\n tool_calls: [\n {\n type: 'function',\n id: 'call_123',\n function: {\n name: 'get_weather',\n arguments: '{\"location\": \"San Francisco\"}',\n },\n },\n ],\n },\n {\n role: 'tool',\n tool_call_id: 'call_123',\n content: '{\"temperature\": 72, \"condition\": \"sunny\"}',\n },\n ]);\n });\n\n it('should handle multiple tool calls in one message', async () => {\n const ctx = ChatContext.empty();\n\n const msg = ctx.addMessage({ role: 'assistant', content: \"I'll check both locations.\" });\n const toolCall1 = new FunctionCall({\n id: msg.id + '/tool_1',\n callId: 'call_1',\n name: 'get_weather',\n args: '{\"location\": \"NYC\"}',\n });\n const toolCall2 = new FunctionCall({\n id: msg.id + '/tool_2',\n callId: 'call_2',\n name: 'get_weather',\n args: '{\"location\": \"LA\"}',\n });\n const toolOutput1 = new FunctionCallOutput({\n callId: 'call_1',\n output: '{\"temperature\": 65}',\n isError: false,\n });\n const toolOutput2 = new FunctionCallOutput({\n callId: 'call_2',\n output: '{\"temperature\": 78}',\n isError: false,\n });\n\n ctx.insert([toolCall1, toolCall2, toolOutput1, toolOutput2]);\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'assistant',\n content: \"I'll check both locations.\",\n tool_calls: [\n {\n type: 'function',\n id: 'call_1',\n function: { name: 'get_weather', arguments: '{\"location\": \"NYC\"}' },\n },\n {\n type: 'function',\n id: 'call_2',\n function: { name: 'get_weather', arguments: '{\"location\": \"LA\"}' },\n },\n ],\n },\n {\n role: 'tool',\n tool_call_id: 'call_1',\n content: '{\"temperature\": 65}',\n },\n {\n role: 'tool',\n tool_call_id: 'call_2',\n content: '{\"temperature\": 78}',\n },\n ]);\n });\n\n it('should handle tool calls without accompanying message', async () => {\n const ctx = ChatContext.empty();\n\n const toolCall = new FunctionCall({\n id: 'func_123',\n callId: 'call_456',\n name: 'calculate',\n args: '{\"a\": 5, \"b\": 3}',\n });\n const toolOutput = new FunctionCallOutput({\n callId: 'call_456',\n output: '{\"result\": 8}',\n isError: false,\n });\n\n ctx.insert([toolCall, toolOutput]);\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'assistant',\n tool_calls: [\n {\n type: 'function',\n id: 'call_456',\n function: { name: 'calculate', arguments: '{\"a\": 5, \"b\": 3}' },\n },\n ],\n },\n {\n role: 'tool',\n tool_call_id: 'call_456',\n content: '{\"result\": 8}',\n },\n ]);\n });\n\n it('should skip empty groups', async () => {\n const ctx = ChatContext.empty();\n ctx.addMessage({ role: 'user', content: 'Hello', createdAt: 1000 });\n\n // Create an isolated tool output without corresponding call (will be filtered)\n const orphanOutput = new FunctionCallOutput({\n callId: 'orphan_call',\n output: 'This should be ignored',\n isError: false,\n createdAt: 2000,\n });\n ctx.insert(orphanOutput);\n\n ctx.addMessage({ role: 'assistant', content: 'Hi!', createdAt: 3000 });\n\n const result = await toChatCtx(ctx);\n\n // Messages should be in the result, orphan output should be filtered out\n expect(result).toHaveLength(2);\n expect(result).toContainEqual({ role: 'user', content: 'Hello' });\n expect(result).toContainEqual({ role: 'assistant', content: 'Hi!' });\n });\n\n it('should handle mixed content with text and multiple images', async () => {\n serializeImageMock\n .mockResolvedValueOnce({\n inferenceDetail: 'high',\n externalUrl: 'https://example.com/image1.jpg',\n })\n .mockResolvedValueOnce({\n inferenceDetail: 'low',\n mimeType: 'image/png',\n base64Data: 'base64data',\n });\n\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'user',\n content: [\n 'Here are two images:',\n {\n id: 'img1',\n type: 'image_content',\n image: 'https://example.com/image1.jpg',\n inferenceDetail: 'high',\n _cache: {},\n },\n 'And the second one:',\n {\n id: 'img2',\n type: 'image_content',\n image: 'data:image/png;base64,base64data',\n inferenceDetail: 'low',\n _cache: {},\n },\n 'What do you think?',\n ],\n });\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'user',\n content: [\n {\n type: 'image_url',\n image_url: {\n url: 'https://example.com/image1.jpg',\n detail: 'high',\n },\n },\n {\n type: 'image_url',\n image_url: {\n url: 'data:image/png;base64,base64data',\n detail: 'low',\n },\n },\n {\n type: 'text',\n text: 'Here are two images:\\nAnd the second one:\\nWhat do you think?',\n },\n ],\n },\n ]);\n });\n\n it('should handle content with only images and no text', async () => {\n serializeImageMock.mockResolvedValue({\n inferenceDetail: 'auto',\n externalUrl: 'https://example.com/image.jpg',\n });\n\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'user',\n content: [\n {\n id: 'img1',\n type: 'image_content',\n image: 'https://example.com/image.jpg',\n inferenceDetail: 'auto',\n _cache: {},\n },\n ],\n });\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'user',\n content: [\n {\n type: 'image_url',\n image_url: {\n url: 'https://example.com/image.jpg',\n detail: 'auto',\n },\n },\n ],\n },\n ]);\n });\n\n it('should throw error for unsupported content type', async () => {\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'user',\n content: [\n {\n type: 'audio_content',\n frame: [],\n },\n ],\n });\n\n await expect(toChatCtx(ctx)).rejects.toThrow('Unsupported content type: audio_content');\n });\n\n it('should throw error when serialized image has no data', async () => {\n serializeImageMock.mockResolvedValue({\n inferenceDetail: 'high',\n // No base64Data or externalUrl\n });\n\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'user',\n content: [\n {\n id: 'img1',\n type: 'image_content',\n image: 'invalid-image',\n inferenceDetail: 'high',\n _cache: {},\n },\n ],\n });\n\n await expect(toChatCtx(ctx)).rejects.toThrow('Serialized image has no data bytes');\n });\n\n it('should filter out standalone function calls without outputs', async () => {\n const ctx = ChatContext.empty();\n\n // Add standalone function call without output\n const funcCall = new FunctionCall({\n id: 'func_standalone',\n callId: 'call_999',\n name: 'standalone_function',\n args: '{}',\n });\n\n ctx.insert(funcCall);\n\n const result = await toChatCtx(ctx);\n\n // Standalone function calls without outputs are filtered out by groupToolCalls\n expect(result).toEqual([]);\n });\n\n it('should handle function call output correctly', async () => {\n const ctx = ChatContext.empty();\n\n // First add a function call\n const funcCall = new FunctionCall({\n id: 'func_1',\n callId: 'call_output_test',\n name: 'test_function',\n args: '{}',\n });\n\n // Then add its output\n const funcOutput = new FunctionCallOutput({\n callId: 'call_output_test',\n output: 'Function executed successfully',\n isError: false,\n });\n\n ctx.insert([funcCall, funcOutput]);\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'assistant',\n tool_calls: [\n {\n id: 'call_output_test',\n type: 'function',\n function: {\n name: 'test_function',\n arguments: '{}',\n },\n },\n ],\n },\n {\n role: 'tool',\n tool_call_id: 'call_output_test',\n content: 'Function executed successfully',\n },\n ]);\n });\n\n it('should filter out agent handoff items', async () => {\n const ctx = ChatContext.empty();\n\n ctx.addMessage({ role: 'user', content: 'Hello' });\n\n // Insert an agent handoff item\n const handoff = new AgentHandoffItem({\n oldAgentId: 'agent_1',\n newAgentId: 'agent_2',\n });\n ctx.insert(handoff);\n\n ctx.addMessage({ role: 'assistant', content: 'Hi there!' });\n\n const result = await toChatCtx(ctx);\n\n // Agent handoff should be filtered out, only messages should remain\n expect(result).toEqual([\n { role: 'user', content: 'Hello' },\n { role: 'assistant', content: 'Hi there!' },\n ]);\n });\n\n it('should handle multiple agent handoffs without errors', async () => {\n const ctx = ChatContext.empty();\n\n ctx.addMessage({ role: 'user', content: 'Start' });\n\n // Multiple handoffs\n ctx.insert(new AgentHandoffItem({ oldAgentId: undefined, newAgentId: 'agent_1' }));\n ctx.addMessage({ role: 'assistant', content: 'Response from agent 1' });\n\n ctx.insert(new AgentHandoffItem({ oldAgentId: 'agent_1', newAgentId: 'agent_2' }));\n ctx.addMessage({ role: 'assistant', content: 'Response from agent 2' });\n\n ctx.insert(new AgentHandoffItem({ oldAgentId: 'agent_2', newAgentId: 'agent_3' }));\n ctx.addMessage({ role: 'assistant', content: 'Response from agent 3' });\n\n const result = await toChatCtx(ctx);\n\n // All handoffs should be filtered out\n expect(result).toEqual([\n { role: 'user', content: 'Start' },\n { role: 'assistant', content: 'Response from agent 1' },\n { role: 'assistant', content: 'Response from agent 2' },\n { role: 'assistant', content: 'Response from agent 3' },\n ]);\n });\n});\n"],"mappings":";AAGA,sBAA4C;AAC5C,oBAAqD;AACrD,iBAAiC;AACjC,0BAKO;AACP,mBAA+B;AAC/B,oBAA0B;AAG1B,iBAAG,KAAK,eAAe,OAAO;AAAA,EAC5B,gBAAgB,iBAAG,GAAG;AACxB,EAAE;AAAA,IAEF,wBAAS,aAAa,MAAM;AAC1B,QAAM,qBAAqB,iBAAG,OAAO,2BAAc;AAGnD,mCAAiB,EAAE,OAAO,UAAU,QAAQ,MAAM,CAAC;AAEnD,gCAAW,YAAY;AACrB,qBAAG,cAAc;AAAA,EACnB,CAAC;AAED,wBAAG,uCAAuC,YAAY;AACpD,UAAM,MAAM,gCAAY,MAAM;AAC9B,QAAI,WAAW,EAAE,MAAM,QAAQ,SAAS,QAAQ,CAAC;AACjD,QAAI,WAAW,EAAE,MAAM,aAAa,SAAS,YAAY,CAAC;AAE1D,UAAM,SAAS,UAAM,yBAAU,GAAG;AAGlC,8BAAO,MAAM,EAAE,aAAa,CAAC;AAC7B,8BAAO,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,QAAQ,SAAS,QAAQ,CAAC;AAC5D,8BAAO,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,aAAa,SAAS,YAAY,CAAC;AAAA,EACvE,CAAC;AAED,wBAAG,iCAAiC,YAAY;AAC9C,UAAM,MAAM,gCAAY,MAAM;AAC9B,QAAI,WAAW,EAAE,MAAM,UAAU,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":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { VideoBufferType, VideoFrame } from '@livekit/rtc-node';\nimport { beforeEach, describe, expect, it, vi } from 'vitest';\nimport { initializeLogger } from '../../log.js';\nimport {\n AgentHandoffItem,\n ChatContext,\n FunctionCall,\n FunctionCallOutput,\n} from '../chat_context.js';\nimport { serializeImage } from '../utils.js';\nimport { toChatCtx } from './openai.js';\n\n// Mock the serializeImage function\nvi.mock('../utils.js', () => ({\n serializeImage: vi.fn(),\n}));\n\ndescribe('toChatCtx', () => {\n const serializeImageMock = vi.mocked(serializeImage);\n\n // initialize logger at start of test\n initializeLogger({ level: 'silent', pretty: false });\n\n beforeEach(async () => {\n vi.clearAllMocks();\n });\n\n it('should convert simple text messages', async () => {\n const ctx = ChatContext.empty();\n ctx.addMessage({ role: 'user', content: 'Hello' });\n ctx.addMessage({ role: 'assistant', content: 'Hi there!' });\n\n const result = await toChatCtx(ctx);\n\n // Messages should be in the result, order may vary due to ID-based sorting\n expect(result).toHaveLength(2);\n expect(result[0]).toEqual({ role: 'user', content: 'Hello' });\n expect(result[1]).toEqual({ role: 'assistant', content: 'Hi there!' });\n });\n\n it('should handle system messages', async () => {\n const ctx = ChatContext.empty();\n ctx.addMessage({ role: 'system', content: 'You are a helpful assistant' });\n ctx.addMessage({ role: 'user', content: 'Hello' });\n\n const result = await toChatCtx(ctx);\n\n // Messages should be in the result, order may vary due to ID-based sorting\n expect(result).toHaveLength(2);\n expect(result[0]).toEqual({ role: 'system', content: 'You are a helpful assistant' });\n expect(result[1]).toEqual({ role: 'user', content: 'Hello' });\n });\n\n it('should handle multi-line text content', async () => {\n const ctx = ChatContext.empty();\n ctx.addMessage({ role: 'user', content: ['Line 1', 'Line 2', 'Line 3'] });\n\n const result = await toChatCtx(ctx);\n\n expect(result).toHaveLength(1);\n expect(result[0]).toEqual({ role: 'user', content: 'Line 1\\nLine 2\\nLine 3' });\n });\n\n it('should handle messages with external URL images', async () => {\n serializeImageMock.mockResolvedValue({\n inferenceDetail: 'high',\n externalUrl: 'https://example.com/image.jpg',\n });\n\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'user',\n content: [\n 'Check out this image:',\n {\n id: 'img1',\n type: 'image_content',\n image: 'https://example.com/image.jpg',\n inferenceDetail: 'high',\n _cache: {},\n },\n ],\n });\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'user',\n content: [\n {\n type: 'image_url',\n image_url: {\n url: 'https://example.com/image.jpg',\n detail: 'high',\n },\n },\n { type: 'text', text: 'Check out this image:' },\n ],\n },\n ]);\n });\n\n it('should handle messages with base64 images', async () => {\n serializeImageMock.mockResolvedValue({\n inferenceDetail: 'auto',\n mimeType: 'image/png',\n base64Data: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB',\n });\n\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'assistant',\n content: [\n {\n id: 'img1',\n type: 'image_content',\n image: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB',\n inferenceDetail: 'auto',\n _cache: {},\n },\n 'Here is the image you requested',\n ],\n });\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'assistant',\n content: [\n {\n type: 'image_url',\n image_url: {\n url: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB',\n detail: 'auto',\n },\n },\n { type: 'text', text: 'Here is the image you requested' },\n ],\n },\n ]);\n });\n\n it('should handle VideoFrame images', async () => {\n serializeImageMock.mockResolvedValue({\n inferenceDetail: 'low',\n mimeType: 'image/jpeg',\n base64Data: '/9j/4AAQSkZJRg==',\n });\n\n const frameData = new Uint8Array(4 * 4 * 4); // 4x4 RGBA\n const videoFrame = new VideoFrame(frameData, 4, 4, VideoBufferType.RGBA);\n\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'user',\n content: [\n {\n id: 'frame1',\n type: 'image_content',\n image: videoFrame,\n inferenceDetail: 'low',\n _cache: {},\n },\n ],\n });\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'user',\n content: [\n {\n type: 'image_url',\n image_url: {\n url: 'data:image/jpeg;base64,/9j/4AAQSkZJRg==',\n detail: 'low',\n },\n },\n ],\n },\n ]);\n });\n\n it('should cache serialized images', async () => {\n serializeImageMock.mockResolvedValue({\n inferenceDetail: 'high',\n mimeType: 'image/png',\n base64Data: 'cached-data',\n });\n\n const imageContent = {\n id: 'img1',\n type: 'image_content' as const,\n image: 'https://example.com/image.jpg',\n inferenceDetail: 'high' as const,\n _cache: {},\n };\n\n const ctx = ChatContext.empty();\n ctx.addMessage({ role: 'user', content: [imageContent] });\n\n // Call twice to test caching\n await toChatCtx(ctx);\n await toChatCtx(ctx);\n\n // serializeImage should only be called once due to caching\n expect(serializeImageMock).toHaveBeenCalledTimes(1);\n expect(imageContent._cache).toHaveProperty('serialized_image');\n });\n\n it('should handle tool calls and outputs', async () => {\n const ctx = ChatContext.empty();\n\n // Add an assistant message with tool calls\n const msg = ctx.addMessage({ role: 'assistant', content: 'Let me help you with that.' });\n const toolCall = FunctionCall.create({\n id: msg.id + '/tool_1',\n callId: 'call_123',\n name: 'get_weather',\n args: '{\"location\": \"San Francisco\"}',\n });\n const toolOutput = FunctionCallOutput.create({\n callId: 'call_123',\n output: '{\"temperature\": 72, \"condition\": \"sunny\"}',\n isError: false,\n });\n\n ctx.insert([toolCall, toolOutput]);\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'assistant',\n content: 'Let me help you with that.',\n tool_calls: [\n {\n type: 'function',\n id: 'call_123',\n function: {\n name: 'get_weather',\n arguments: '{\"location\": \"San Francisco\"}',\n },\n },\n ],\n },\n {\n role: 'tool',\n tool_call_id: 'call_123',\n content: '{\"temperature\": 72, \"condition\": \"sunny\"}',\n },\n ]);\n });\n\n it('should include provider-specific extra content on tool calls', async () => {\n const ctx = ChatContext.empty();\n const msg = ctx.addMessage({ role: 'assistant', content: 'Running tool' });\n\n const toolCall = FunctionCall.create({\n id: `${msg.id}/tool_1`,\n callId: 'call_789',\n name: 'google_call',\n args: '{}',\n extra: { google: { thoughtSignature: 'sig-123' } },\n });\n const toolOutput = FunctionCallOutput.create({\n callId: 'call_789',\n output: '{\"result\": \"ok\"}',\n isError: false,\n });\n\n ctx.insert([toolCall, toolOutput]);\n\n const result = await toChatCtx(ctx);\n\n expect(result[0]).toEqual({\n role: 'assistant',\n content: 'Running tool',\n tool_calls: [\n {\n type: 'function',\n id: 'call_789',\n function: { name: 'google_call', arguments: '{}' },\n extra_content: { google: { thoughtSignature: 'sig-123' } },\n },\n ],\n });\n expect(result[1]).toEqual({\n role: 'tool',\n tool_call_id: 'call_789',\n content: '{\"result\": \"ok\"}',\n });\n });\n\n it('should handle multiple tool calls in one message', async () => {\n const ctx = ChatContext.empty();\n\n const msg = ctx.addMessage({ role: 'assistant', content: \"I'll check both locations.\" });\n const toolCall1 = new FunctionCall({\n id: msg.id + '/tool_1',\n callId: 'call_1',\n name: 'get_weather',\n args: '{\"location\": \"NYC\"}',\n });\n const toolCall2 = new FunctionCall({\n id: msg.id + '/tool_2',\n callId: 'call_2',\n name: 'get_weather',\n args: '{\"location\": \"LA\"}',\n });\n const toolOutput1 = new FunctionCallOutput({\n callId: 'call_1',\n output: '{\"temperature\": 65}',\n isError: false,\n });\n const toolOutput2 = new FunctionCallOutput({\n callId: 'call_2',\n output: '{\"temperature\": 78}',\n isError: false,\n });\n\n ctx.insert([toolCall1, toolCall2, toolOutput1, toolOutput2]);\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'assistant',\n content: \"I'll check both locations.\",\n tool_calls: [\n {\n type: 'function',\n id: 'call_1',\n function: { name: 'get_weather', arguments: '{\"location\": \"NYC\"}' },\n },\n {\n type: 'function',\n id: 'call_2',\n function: { name: 'get_weather', arguments: '{\"location\": \"LA\"}' },\n },\n ],\n },\n {\n role: 'tool',\n tool_call_id: 'call_1',\n content: '{\"temperature\": 65}',\n },\n {\n role: 'tool',\n tool_call_id: 'call_2',\n content: '{\"temperature\": 78}',\n },\n ]);\n });\n\n it('should handle tool calls without accompanying message', async () => {\n const ctx = ChatContext.empty();\n\n const toolCall = new FunctionCall({\n id: 'func_123',\n callId: 'call_456',\n name: 'calculate',\n args: '{\"a\": 5, \"b\": 3}',\n });\n const toolOutput = new FunctionCallOutput({\n callId: 'call_456',\n output: '{\"result\": 8}',\n isError: false,\n });\n\n ctx.insert([toolCall, toolOutput]);\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'assistant',\n tool_calls: [\n {\n type: 'function',\n id: 'call_456',\n function: { name: 'calculate', arguments: '{\"a\": 5, \"b\": 3}' },\n },\n ],\n },\n {\n role: 'tool',\n tool_call_id: 'call_456',\n content: '{\"result\": 8}',\n },\n ]);\n });\n\n it('should skip empty groups', async () => {\n const ctx = ChatContext.empty();\n ctx.addMessage({ role: 'user', content: 'Hello', createdAt: 1000 });\n\n // Create an isolated tool output without corresponding call (will be filtered)\n const orphanOutput = new FunctionCallOutput({\n callId: 'orphan_call',\n output: 'This should be ignored',\n isError: false,\n createdAt: 2000,\n });\n ctx.insert(orphanOutput);\n\n ctx.addMessage({ role: 'assistant', content: 'Hi!', createdAt: 3000 });\n\n const result = await toChatCtx(ctx);\n\n // Messages should be in the result, orphan output should be filtered out\n expect(result).toHaveLength(2);\n expect(result).toContainEqual({ role: 'user', content: 'Hello' });\n expect(result).toContainEqual({ role: 'assistant', content: 'Hi!' });\n });\n\n it('should handle mixed content with text and multiple images', async () => {\n serializeImageMock\n .mockResolvedValueOnce({\n inferenceDetail: 'high',\n externalUrl: 'https://example.com/image1.jpg',\n })\n .mockResolvedValueOnce({\n inferenceDetail: 'low',\n mimeType: 'image/png',\n base64Data: 'base64data',\n });\n\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'user',\n content: [\n 'Here are two images:',\n {\n id: 'img1',\n type: 'image_content',\n image: 'https://example.com/image1.jpg',\n inferenceDetail: 'high',\n _cache: {},\n },\n 'And the second one:',\n {\n id: 'img2',\n type: 'image_content',\n image: 'data:image/png;base64,base64data',\n inferenceDetail: 'low',\n _cache: {},\n },\n 'What do you think?',\n ],\n });\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'user',\n content: [\n {\n type: 'image_url',\n image_url: {\n url: 'https://example.com/image1.jpg',\n detail: 'high',\n },\n },\n {\n type: 'image_url',\n image_url: {\n url: 'data:image/png;base64,base64data',\n detail: 'low',\n },\n },\n {\n type: 'text',\n text: 'Here are two images:\\nAnd the second one:\\nWhat do you think?',\n },\n ],\n },\n ]);\n });\n\n it('should handle content with only images and no text', async () => {\n serializeImageMock.mockResolvedValue({\n inferenceDetail: 'auto',\n externalUrl: 'https://example.com/image.jpg',\n });\n\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'user',\n content: [\n {\n id: 'img1',\n type: 'image_content',\n image: 'https://example.com/image.jpg',\n inferenceDetail: 'auto',\n _cache: {},\n },\n ],\n });\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'user',\n content: [\n {\n type: 'image_url',\n image_url: {\n url: 'https://example.com/image.jpg',\n detail: 'auto',\n },\n },\n ],\n },\n ]);\n });\n\n it('should throw error for unsupported content type', async () => {\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'user',\n content: [\n {\n type: 'audio_content',\n frame: [],\n },\n ],\n });\n\n await expect(toChatCtx(ctx)).rejects.toThrow('Unsupported content type: audio_content');\n });\n\n it('should throw error when serialized image has no data', async () => {\n serializeImageMock.mockResolvedValue({\n inferenceDetail: 'high',\n // No base64Data or externalUrl\n });\n\n const ctx = ChatContext.empty();\n ctx.addMessage({\n role: 'user',\n content: [\n {\n id: 'img1',\n type: 'image_content',\n image: 'invalid-image',\n inferenceDetail: 'high',\n _cache: {},\n },\n ],\n });\n\n await expect(toChatCtx(ctx)).rejects.toThrow('Serialized image has no data bytes');\n });\n\n it('should filter out standalone function calls without outputs', async () => {\n const ctx = ChatContext.empty();\n\n // Add standalone function call without output\n const funcCall = new FunctionCall({\n id: 'func_standalone',\n callId: 'call_999',\n name: 'standalone_function',\n args: '{}',\n });\n\n ctx.insert(funcCall);\n\n const result = await toChatCtx(ctx);\n\n // Standalone function calls without outputs are filtered out by groupToolCalls\n expect(result).toEqual([]);\n });\n\n it('should handle function call output correctly', async () => {\n const ctx = ChatContext.empty();\n\n // First add a function call\n const funcCall = new FunctionCall({\n id: 'func_1',\n callId: 'call_output_test',\n name: 'test_function',\n args: '{}',\n });\n\n // Then add its output\n const funcOutput = new FunctionCallOutput({\n callId: 'call_output_test',\n output: 'Function executed successfully',\n isError: false,\n });\n\n ctx.insert([funcCall, funcOutput]);\n\n const result = await toChatCtx(ctx);\n\n expect(result).toEqual([\n {\n role: 'assistant',\n tool_calls: [\n {\n id: 'call_output_test',\n type: 'function',\n function: {\n name: 'test_function',\n arguments: '{}',\n },\n },\n ],\n },\n {\n role: 'tool',\n tool_call_id: 'call_output_test',\n content: 'Function executed successfully',\n },\n ]);\n });\n\n it('should filter out agent handoff items', async () => {\n const ctx = ChatContext.empty();\n\n ctx.addMessage({ role: 'user', content: 'Hello' });\n\n // Insert an agent handoff item\n const handoff = new AgentHandoffItem({\n oldAgentId: 'agent_1',\n newAgentId: 'agent_2',\n });\n ctx.insert(handoff);\n\n ctx.addMessage({ role: 'assistant', content: 'Hi there!' });\n\n const result = await toChatCtx(ctx);\n\n // Agent handoff should be filtered out, only messages should remain\n expect(result).toEqual([\n { role: 'user', content: 'Hello' },\n { role: 'assistant', content: 'Hi there!' },\n ]);\n });\n\n it('should handle multiple agent handoffs without errors', async () => {\n const ctx = ChatContext.empty();\n\n ctx.addMessage({ role: 'user', content: 'Start' });\n\n // Multiple handoffs\n ctx.insert(new AgentHandoffItem({ oldAgentId: undefined, newAgentId: 'agent_1' }));\n ctx.addMessage({ role: 'assistant', content: 'Response from agent 1' });\n\n ctx.insert(new AgentHandoffItem({ oldAgentId: 'agent_1', newAgentId: 'agent_2' }));\n ctx.addMessage({ role: 'assistant', content: 'Response from agent 2' });\n\n ctx.insert(new AgentHandoffItem({ oldAgentId: 'agent_2', newAgentId: 'agent_3' }));\n ctx.addMessage({ role: 'assistant', content: 'Response from agent 3' });\n\n const result = await toChatCtx(ctx);\n\n // All handoffs should be filtered out\n expect(result).toEqual([\n { role: 'user', content: 'Start' },\n { role: 'assistant', content: 'Response from agent 1' },\n { role: 'assistant', content: 'Response from agent 2' },\n { role: 'assistant', content: 'Response from agent 3' },\n ]);\n });\n});\n"],"mappings":";AAGA,sBAA4C;AAC5C,oBAAqD;AACrD,iBAAiC;AACjC,0BAKO;AACP,mBAA+B;AAC/B,oBAA0B;AAG1B,iBAAG,KAAK,eAAe,OAAO;AAAA,EAC5B,gBAAgB,iBAAG,GAAG;AACxB,EAAE;AAAA,IAEF,wBAAS,aAAa,MAAM;AAC1B,QAAM,qBAAqB,iBAAG,OAAO,2BAAc;AAGnD,mCAAiB,EAAE,OAAO,UAAU,QAAQ,MAAM,CAAC;AAEnD,gCAAW,YAAY;AACrB,qBAAG,cAAc;AAAA,EACnB,CAAC;AAED,wBAAG,uCAAuC,YAAY;AACpD,UAAM,MAAM,gCAAY,MAAM;AAC9B,QAAI,WAAW,EAAE,MAAM,QAAQ,SAAS,QAAQ,CAAC;AACjD,QAAI,WAAW,EAAE,MAAM,aAAa,SAAS,YAAY,CAAC;AAE1D,UAAM,SAAS,UAAM,yBAAU,GAAG;AAGlC,8BAAO,MAAM,EAAE,aAAa,CAAC;AAC7B,8BAAO,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,QAAQ,SAAS,QAAQ,CAAC;AAC5D,8BAAO,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,aAAa,SAAS,YAAY,CAAC;AAAA,EACvE,CAAC;AAED,wBAAG,iCAAiC,YAAY;AAC9C,UAAM,MAAM,gCAAY,MAAM;AAC9B,QAAI,WAA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ACjC,EAAE,MAAM,aAAa,SAAS,YAAY;AAAA,IAC5C,CAAC;AAAA,EACH,CAAC;AAED,wBAAG,wDAAwD,YAAY;AACrE,UAAM,MAAM,gCAAY,MAAM;AAE9B,QAAI,WAAW,EAAE,MAAM,QAAQ,SAAS,QAAQ,CAAC;AAGjD,QAAI,OAAO,IAAI,qCAAiB,EAAE,YAAY,QAAW,YAAY,UAAU,CAAC,CAAC;AACjF,QAAI,WAAW,EAAE,MAAM,aAAa,SAAS,wBAAwB,CAAC;AAEtE,QAAI,OAAO,IAAI,qCAAiB,EAAE,YAAY,WAAW,YAAY,UAAU,CAAC,CAAC;AACjF,QAAI,WAAW,EAAE,MAAM,aAAa,SAAS,wBAAwB,CAAC;AAEtE,QAAI,OAAO,IAAI,qCAAiB,EAAE,YAAY,WAAW,YAAY,UAAU,CAAC,CAAC;AACjF,QAAI,WAAW,EAAE,MAAM,aAAa,SAAS,wBAAwB,CAAC;AAEtE,UAAM,SAAS,UAAM,yBAAU,GAAG;AAGlC,8BAAO,MAAM,EAAE,QAAQ;AAAA,MACrB,EAAE,MAAM,QAAQ,SAAS,QAAQ;AAAA,MACjC,EAAE,MAAM,aAAa,SAAS,wBAAwB;AAAA,MACtD,EAAE,MAAM,aAAa,SAAS,wBAAwB;AAAA,MACtD,EAAE,MAAM,aAAa,SAAS,wBAAwB;AAAA,IACxD,CAAC;AAAA,EACH,CAAC;AACH,CAAC;","names":[]}
@@ -211,6 +211,41 @@ describe("toChatCtx", () => {
211
211
  }
212
212
  ]);
213
213
  });
214
+ it("should include provider-specific extra content on tool calls", async () => {
215
+ const ctx = ChatContext.empty();
216
+ const msg = ctx.addMessage({ role: "assistant", content: "Running tool" });
217
+ const toolCall = FunctionCall.create({
218
+ id: `${msg.id}/tool_1`,
219
+ callId: "call_789",
220
+ name: "google_call",
221
+ args: "{}",
222
+ extra: { google: { thoughtSignature: "sig-123" } }
223
+ });
224
+ const toolOutput = FunctionCallOutput.create({
225
+ callId: "call_789",
226
+ output: '{"result": "ok"}',
227
+ isError: false
228
+ });
229
+ ctx.insert([toolCall, toolOutput]);
230
+ const result = await toChatCtx(ctx);
231
+ expect(result[0]).toEqual({
232
+ role: "assistant",
233
+ content: "Running tool",
234
+ tool_calls: [
235
+ {
236
+ type: "function",
237
+ id: "call_789",
238
+ function: { name: "google_call", arguments: "{}" },
239
+ extra_content: { google: { thoughtSignature: "sig-123" } }
240
+ }
241
+ ]
242
+ });
243
+ expect(result[1]).toEqual({
244
+ role: "tool",
245
+ tool_call_id: "call_789",
246
+ content: '{"result": "ok"}'
247
+ });
248
+ });
214
249
  it("should handle multiple tool calls in one message", async () => {
215
250
  const ctx = ChatContext.empty();
216
251
  const msg = ctx.addMessage({ role: "assistant", content: "I'll check both locations." });