@livekit/agents 1.0.34 → 1.0.35
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.
- package/dist/cli.cjs.map +1 -1
- package/dist/inference/api_protos.d.cts +4 -4
- package/dist/inference/api_protos.d.ts +4 -4
- package/dist/inference/llm.cjs +30 -3
- package/dist/inference/llm.cjs.map +1 -1
- package/dist/inference/llm.d.cts +3 -1
- package/dist/inference/llm.d.ts +3 -1
- package/dist/inference/llm.d.ts.map +1 -1
- package/dist/inference/llm.js +30 -3
- package/dist/inference/llm.js.map +1 -1
- package/dist/ipc/inference_proc_executor.cjs.map +1 -1
- package/dist/ipc/job_proc_executor.cjs.map +1 -1
- package/dist/ipc/job_proc_lazy_main.cjs +1 -1
- package/dist/ipc/job_proc_lazy_main.cjs.map +1 -1
- package/dist/ipc/job_proc_lazy_main.js +1 -1
- package/dist/ipc/job_proc_lazy_main.js.map +1 -1
- package/dist/llm/chat_context.cjs +20 -2
- package/dist/llm/chat_context.cjs.map +1 -1
- package/dist/llm/chat_context.d.cts +9 -0
- package/dist/llm/chat_context.d.ts +9 -0
- package/dist/llm/chat_context.d.ts.map +1 -1
- package/dist/llm/chat_context.js +20 -2
- package/dist/llm/chat_context.js.map +1 -1
- package/dist/llm/llm.cjs.map +1 -1
- package/dist/llm/llm.d.cts +1 -0
- package/dist/llm/llm.d.ts +1 -0
- package/dist/llm/llm.d.ts.map +1 -1
- package/dist/llm/llm.js.map +1 -1
- package/dist/llm/provider_format/openai.cjs +43 -20
- package/dist/llm/provider_format/openai.cjs.map +1 -1
- package/dist/llm/provider_format/openai.d.ts.map +1 -1
- package/dist/llm/provider_format/openai.js +43 -20
- package/dist/llm/provider_format/openai.js.map +1 -1
- package/dist/llm/provider_format/openai.test.cjs +35 -0
- package/dist/llm/provider_format/openai.test.cjs.map +1 -1
- package/dist/llm/provider_format/openai.test.js +35 -0
- package/dist/llm/provider_format/openai.test.js.map +1 -1
- package/dist/llm/provider_format/utils.cjs +1 -1
- package/dist/llm/provider_format/utils.cjs.map +1 -1
- package/dist/llm/provider_format/utils.d.ts.map +1 -1
- package/dist/llm/provider_format/utils.js +1 -1
- package/dist/llm/provider_format/utils.js.map +1 -1
- package/dist/voice/background_audio.cjs.map +1 -1
- package/dist/voice/generation.cjs +2 -1
- package/dist/voice/generation.cjs.map +1 -1
- package/dist/voice/generation.d.ts.map +1 -1
- package/dist/voice/generation.js +2 -1
- package/dist/voice/generation.js.map +1 -1
- package/package.json +1 -1
- package/src/inference/llm.ts +42 -3
- package/src/ipc/job_proc_lazy_main.ts +1 -1
- package/src/llm/chat_context.ts +32 -2
- package/src/llm/llm.ts +1 -0
- package/src/llm/provider_format/openai.test.ts +40 -0
- package/src/llm/provider_format/openai.ts +46 -19
- package/src/llm/provider_format/utils.ts +5 -1
- package/src/voice/generation.ts +1 -0
|
@@ -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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AAC;AACH,CAAC;","names":[]}
|
|
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 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,EACv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AGlC,8BAAO,MAAM,EAAE,QAAQ;AAAA,MACrB,EAAE,MAAM,QAAQ,SAAS,QAAQ;AAAA,MACjC,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." });
|
|
@@ -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,SAAS,iBAAiB,kBAAkB;AAC5C,SAAS,YAAY,UAAU,QAAQ,IAAI,UAAU;AACrD,SAAS,wBAAwB;AACjC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,sBAAsB;AAC/B,SAAS,iBAAiB;AAG1B,GAAG,KAAK,eAAe,OAAO;AAAA,EAC5B,gBAAgB,GAAG,GAAG;AACxB,EAAE;AAEF,SAAS,aAAa,MAAM;AAC1B,QAAM,qBAAqB,GAAG,OAAO,cAAc;AAGnD,mBAAiB,EAAE,OAAO,UAAU,QAAQ,MAAM,CAAC;AAEnD,aAAW,YAAY;AACrB,OAAG,cAAc;AAAA,EACnB,CAAC;AAED,KAAG,uCAAuC,YAAY;AACpD,UAAM,MAAM,Y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S,wBAAwB,CAAC;AAEtE,UAAM,SAAS,MAAM,UAAU,GAAG;AAGlC,WAAO,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":[]}
|
|
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 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,SAAS,iBAAiB,kBAAkB;AAC5C,SAAS,YAAY,UAAU,QAAQ,IAAI,UAAU;AACrD,SAAS,wBAAwB;AACjC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,sBAAsB;AAC/B,SAAS,iBAAiB;AAG1B,GAAG,KAAK,eAAe,OAAO;AAAA,EAC5B,gBAAgB,GAAG,GAAG;AACxB,EAAE;AAEF,SAAS,aAAa,MAAM;AAC1B,QAAM,qBAAqB,GAAG,OAAO,cAAc;AAGnD,mBAAiB,EAAE,OAAO,UAAU,QAAQ,MAAM,CAAC;AAEnD,aAAW,YAAY;AACrB,OAAG,cAAc;AAAA,EACnB,CAAC;AAED,KAAG,uCAAuC,YAAY;AACpD,UA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C;","names":[]}
|
|
@@ -103,7 +103,7 @@ function groupToolCalls(chatCtx) {
|
|
|
103
103
|
const isFunctionCall = item.type === "function_call";
|
|
104
104
|
const isFunctionCallOutput = item.type === "function_call_output";
|
|
105
105
|
if (isAssistantMessage || isFunctionCall) {
|
|
106
|
-
const groupId = item.id.split("/")[0];
|
|
106
|
+
const groupId = item.type === "function_call" && item.groupId ? item.groupId : item.id.split("/")[0];
|
|
107
107
|
if (itemGroups[groupId] === void 0) {
|
|
108
108
|
itemGroups[groupId] = ChatItemGroup.create();
|
|
109
109
|
insertionOrder[groupId] = insertionIndex;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/llm/provider_format/utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { log } from '../../log.js';\nimport type {\n ChatContext,\n ChatItem,\n ChatMessage,\n FunctionCall,\n FunctionCallOutput,\n} from '../chat_context.js';\n\nclass ChatItemGroup {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n logger = log();\n\n constructor(params: {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n }) {\n this.message = params.message;\n this.toolCalls = params.toolCalls;\n this.toolOutputs = params.toolOutputs;\n }\n\n static create(params?: {\n message?: ChatMessage;\n toolCalls?: FunctionCall[];\n toolOutputs?: FunctionCallOutput[];\n }) {\n const { message, toolCalls = [], toolOutputs = [] } = params ?? {};\n return new ChatItemGroup({ message, toolCalls, toolOutputs });\n }\n\n get isEmpty() {\n return (\n this.message === undefined && this.toolCalls.length === 0 && this.toolOutputs.length === 0\n );\n }\n\n add(item: ChatItem) {\n if (item.type === 'message') {\n if (this.message) {\n throw new Error('only one message is allowed in a group');\n }\n this.message = item;\n } else if (item.type === 'function_call') {\n this.toolCalls.push(item);\n } else if (item.type === 'function_call_output') {\n this.toolOutputs.push(item);\n }\n return this;\n }\n\n removeInvalidToolCalls() {\n if (this.toolCalls.length === this.toolOutputs.length) {\n return;\n }\n\n const toolCallIds = new Set(this.toolCalls.map((call) => call.callId));\n const toolOutputIds = new Set(this.toolOutputs.map((output) => output.callId));\n\n // intersection of tool call ids and tool output ids\n const validCallIds = intersection(toolCallIds, toolOutputIds);\n\n // filter out tool calls that don't have a corresponding tool output\n this.toolCalls = this.toolCalls.filter((call) => {\n if (validCallIds.has(call.callId)) return true;\n this.logger.warn(\n {\n callId: call.callId,\n toolName: call.name,\n },\n 'function call missing the corresponding function output, ignoring',\n );\n return false;\n });\n\n // filter out tool outputs that don't have a corresponding tool call\n this.toolOutputs = this.toolOutputs.filter((output) => {\n if (validCallIds.has(output.callId)) return true;\n this.logger.warn(\n {\n callId: output.callId,\n toolName: output.name,\n },\n 'function output missing the corresponding function call, ignoring',\n );\n return false;\n });\n }\n\n flatten() {\n const items: ChatItem[] = [];\n if (this.message) items.push(this.message);\n items.push(...this.toolCalls, ...this.toolOutputs);\n return items;\n }\n}\n\nfunction intersection<T>(set1: Set<T>, set2: Set<T>): Set<T> {\n return new Set([...set1].filter((item) => set2.has(item)));\n}\n\n/**\n * Group chat items (messages, function calls, and function outputs)\n * into coherent groups based on their item IDs and call IDs.\n *\n * Each group will contain:\n * - Zero or one assistant message\n * - Zero or more function/tool calls\n * - The corresponding function/tool outputs matched by call_id\n *\n * User and system messages are placed in their own individual groups.\n *\n * @param chatCtx - The chat context containing all conversation items\n * @returns A list of ChatItemGroup objects representing the grouped conversation\n */\nexport function groupToolCalls(chatCtx: ChatContext) {\n const itemGroups: Record<string, ChatItemGroup> = {};\n const insertionOrder: Record<string, number> = {};\n const toolOutputs: FunctionCallOutput[] = [];\n const logger = log();\n\n let insertionIndex = 0;\n for (const item of chatCtx.items) {\n const isAssistantMessage = item.type === 'message' && item.role === 'assistant';\n const isFunctionCall = item.type === 'function_call';\n const isFunctionCallOutput = item.type === 'function_call_output';\n\n if (isAssistantMessage || isFunctionCall) {\n // only assistant messages and function calls can be grouped\n const groupId
|
|
1
|
+
{"version":3,"sources":["../../../src/llm/provider_format/utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { log } from '../../log.js';\nimport type {\n ChatContext,\n ChatItem,\n ChatMessage,\n FunctionCall,\n FunctionCallOutput,\n} from '../chat_context.js';\n\nclass ChatItemGroup {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n logger = log();\n\n constructor(params: {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n }) {\n this.message = params.message;\n this.toolCalls = params.toolCalls;\n this.toolOutputs = params.toolOutputs;\n }\n\n static create(params?: {\n message?: ChatMessage;\n toolCalls?: FunctionCall[];\n toolOutputs?: FunctionCallOutput[];\n }) {\n const { message, toolCalls = [], toolOutputs = [] } = params ?? {};\n return new ChatItemGroup({ message, toolCalls, toolOutputs });\n }\n\n get isEmpty() {\n return (\n this.message === undefined && this.toolCalls.length === 0 && this.toolOutputs.length === 0\n );\n }\n\n add(item: ChatItem) {\n if (item.type === 'message') {\n if (this.message) {\n throw new Error('only one message is allowed in a group');\n }\n this.message = item;\n } else if (item.type === 'function_call') {\n this.toolCalls.push(item);\n } else if (item.type === 'function_call_output') {\n this.toolOutputs.push(item);\n }\n return this;\n }\n\n removeInvalidToolCalls() {\n if (this.toolCalls.length === this.toolOutputs.length) {\n return;\n }\n\n const toolCallIds = new Set(this.toolCalls.map((call) => call.callId));\n const toolOutputIds = new Set(this.toolOutputs.map((output) => output.callId));\n\n // intersection of tool call ids and tool output ids\n const validCallIds = intersection(toolCallIds, toolOutputIds);\n\n // filter out tool calls that don't have a corresponding tool output\n this.toolCalls = this.toolCalls.filter((call) => {\n if (validCallIds.has(call.callId)) return true;\n this.logger.warn(\n {\n callId: call.callId,\n toolName: call.name,\n },\n 'function call missing the corresponding function output, ignoring',\n );\n return false;\n });\n\n // filter out tool outputs that don't have a corresponding tool call\n this.toolOutputs = this.toolOutputs.filter((output) => {\n if (validCallIds.has(output.callId)) return true;\n this.logger.warn(\n {\n callId: output.callId,\n toolName: output.name,\n },\n 'function output missing the corresponding function call, ignoring',\n );\n return false;\n });\n }\n\n flatten() {\n const items: ChatItem[] = [];\n if (this.message) items.push(this.message);\n items.push(...this.toolCalls, ...this.toolOutputs);\n return items;\n }\n}\n\nfunction intersection<T>(set1: Set<T>, set2: Set<T>): Set<T> {\n return new Set([...set1].filter((item) => set2.has(item)));\n}\n\n/**\n * Group chat items (messages, function calls, and function outputs)\n * into coherent groups based on their item IDs and call IDs.\n *\n * Each group will contain:\n * - Zero or one assistant message\n * - Zero or more function/tool calls\n * - The corresponding function/tool outputs matched by call_id\n *\n * User and system messages are placed in their own individual groups.\n *\n * @param chatCtx - The chat context containing all conversation items\n * @returns A list of ChatItemGroup objects representing the grouped conversation\n */\nexport function groupToolCalls(chatCtx: ChatContext) {\n const itemGroups: Record<string, ChatItemGroup> = {};\n const insertionOrder: Record<string, number> = {};\n const toolOutputs: FunctionCallOutput[] = [];\n const logger = log();\n\n let insertionIndex = 0;\n for (const item of chatCtx.items) {\n const isAssistantMessage = item.type === 'message' && item.role === 'assistant';\n const isFunctionCall = item.type === 'function_call';\n const isFunctionCallOutput = item.type === 'function_call_output';\n\n if (isAssistantMessage || isFunctionCall) {\n // only assistant messages and function calls can be grouped\n // For function calls, use group_id if available (for parallel function calls),\n // otherwise fall back to id-based grouping for backwards compatibility\n const groupId =\n item.type === 'function_call' && item.groupId ? item.groupId : item.id.split('/')[0]!;\n\n if (itemGroups[groupId] === undefined) {\n itemGroups[groupId] = ChatItemGroup.create();\n\n // we use insertion order to sort the groups as they are added to the context\n // simulating the OrderedDict in python\n insertionOrder[groupId] = insertionIndex;\n insertionIndex++;\n }\n itemGroups[groupId]!.add(item);\n } else if (isFunctionCallOutput) {\n toolOutputs.push(item);\n } else {\n itemGroups[item.id] = ChatItemGroup.create().add(item);\n }\n }\n\n // add tool outputs to their corresponding groups\n const callIdToGroup: Record<string, ChatItemGroup> = {};\n for (const group of Object.values(itemGroups)) {\n for (const toolCall of group.toolCalls) {\n callIdToGroup[toolCall.callId] = group;\n }\n }\n\n for (const toolOutput of toolOutputs) {\n const group = callIdToGroup[toolOutput.callId];\n if (group === undefined) {\n logger.warn(\n { callId: toolOutput.callId, toolName: toolOutput.name },\n 'function output missing the corresponding function call, ignoring',\n );\n continue;\n }\n group.add(toolOutput);\n }\n\n // validate that each group and remove invalid tool calls and tool outputs\n for (const group of Object.values(itemGroups)) {\n group.removeInvalidToolCalls();\n }\n\n // sort groups by their item id\n const orderedGroups = Object.entries(itemGroups)\n .sort((a, b) => insertionOrder[a[0]]! - insertionOrder[b[0]]!)\n .map(([, group]) => group);\n return orderedGroups;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAAoB;AASpB,MAAM,cAAc;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA,EACA,aAAS,gBAAI;AAAA,EAEb,YAAY,QAIT;AACD,SAAK,UAAU,OAAO;AACtB,SAAK,YAAY,OAAO;AACxB,SAAK,cAAc,OAAO;AAAA,EAC5B;AAAA,EAEA,OAAO,OAAO,QAIX;AACD,UAAM,EAAE,SAAS,YAAY,CAAC,GAAG,cAAc,CAAC,EAAE,IAAI,UAAU,CAAC;AACjE,WAAO,IAAI,cAAc,EAAE,SAAS,WAAW,YAAY,CAAC;AAAA,EAC9D;AAAA,EAEA,IAAI,UAAU;AACZ,WACE,KAAK,YAAY,UAAa,KAAK,UAAU,WAAW,KAAK,KAAK,YAAY,WAAW;AAAA,EAE7F;AAAA,EAEA,IAAI,MAAgB;AAClB,QAAI,KAAK,SAAS,WAAW;AAC3B,UAAI,KAAK,SAAS;AAChB,cAAM,IAAI,MAAM,wCAAwC;AAAA,MAC1D;AACA,WAAK,UAAU;AAAA,IACjB,WAAW,KAAK,SAAS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,KAAK,CAAC,GAAG,MAAM,eAAe,EAAE,CAAC,CAAC,IAAK,eAAe,EAAE,CAAC,CAAC,CAAE,EAC5D,IAAI,CAAC,CAAC,EAAE,KAAK,MAAM,KAAK;AAC3B,SAAO;AACT;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/llm/provider_format/utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,WAAW,EACX,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,kBAAkB,EACnB,MAAM,oBAAoB,CAAC;AAE5B,cAAM,aAAa;IACjB,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,SAAS,EAAE,YAAY,EAAE,CAAC;IAC1B,WAAW,EAAE,kBAAkB,EAAE,CAAC;IAClC,MAAM,wBAAS;gBAEH,MAAM,EAAE;QAClB,OAAO,CAAC,EAAE,WAAW,CAAC;QACtB,SAAS,EAAE,YAAY,EAAE,CAAC;QAC1B,WAAW,EAAE,kBAAkB,EAAE,CAAC;KACnC;IAMD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE;QACrB,OAAO,CAAC,EAAE,WAAW,CAAC;QACtB,SAAS,CAAC,EAAE,YAAY,EAAE,CAAC;QAC3B,WAAW,CAAC,EAAE,kBAAkB,EAAE,CAAC;KACpC;IAKD,IAAI,OAAO,YAIV;IAED,GAAG,CAAC,IAAI,EAAE,QAAQ;IAclB,sBAAsB;IAsCtB,OAAO;CAMR;AAMD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,WAAW,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/llm/provider_format/utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,WAAW,EACX,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,kBAAkB,EACnB,MAAM,oBAAoB,CAAC;AAE5B,cAAM,aAAa;IACjB,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,SAAS,EAAE,YAAY,EAAE,CAAC;IAC1B,WAAW,EAAE,kBAAkB,EAAE,CAAC;IAClC,MAAM,wBAAS;gBAEH,MAAM,EAAE;QAClB,OAAO,CAAC,EAAE,WAAW,CAAC;QACtB,SAAS,EAAE,YAAY,EAAE,CAAC;QAC1B,WAAW,EAAE,kBAAkB,EAAE,CAAC;KACnC;IAMD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE;QACrB,OAAO,CAAC,EAAE,WAAW,CAAC;QACtB,SAAS,CAAC,EAAE,YAAY,EAAE,CAAC;QAC3B,WAAW,CAAC,EAAE,kBAAkB,EAAE,CAAC;KACpC;IAKD,IAAI,OAAO,YAIV;IAED,GAAG,CAAC,IAAI,EAAE,QAAQ;IAclB,sBAAsB;IAsCtB,OAAO;CAMR;AAMD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,WAAW,mBAiElD"}
|
|
@@ -80,7 +80,7 @@ function groupToolCalls(chatCtx) {
|
|
|
80
80
|
const isFunctionCall = item.type === "function_call";
|
|
81
81
|
const isFunctionCallOutput = item.type === "function_call_output";
|
|
82
82
|
if (isAssistantMessage || isFunctionCall) {
|
|
83
|
-
const groupId = item.id.split("/")[0];
|
|
83
|
+
const groupId = item.type === "function_call" && item.groupId ? item.groupId : item.id.split("/")[0];
|
|
84
84
|
if (itemGroups[groupId] === void 0) {
|
|
85
85
|
itemGroups[groupId] = ChatItemGroup.create();
|
|
86
86
|
insertionOrder[groupId] = insertionIndex;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/llm/provider_format/utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { log } from '../../log.js';\nimport type {\n ChatContext,\n ChatItem,\n ChatMessage,\n FunctionCall,\n FunctionCallOutput,\n} from '../chat_context.js';\n\nclass ChatItemGroup {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n logger = log();\n\n constructor(params: {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n }) {\n this.message = params.message;\n this.toolCalls = params.toolCalls;\n this.toolOutputs = params.toolOutputs;\n }\n\n static create(params?: {\n message?: ChatMessage;\n toolCalls?: FunctionCall[];\n toolOutputs?: FunctionCallOutput[];\n }) {\n const { message, toolCalls = [], toolOutputs = [] } = params ?? {};\n return new ChatItemGroup({ message, toolCalls, toolOutputs });\n }\n\n get isEmpty() {\n return (\n this.message === undefined && this.toolCalls.length === 0 && this.toolOutputs.length === 0\n );\n }\n\n add(item: ChatItem) {\n if (item.type === 'message') {\n if (this.message) {\n throw new Error('only one message is allowed in a group');\n }\n this.message = item;\n } else if (item.type === 'function_call') {\n this.toolCalls.push(item);\n } else if (item.type === 'function_call_output') {\n this.toolOutputs.push(item);\n }\n return this;\n }\n\n removeInvalidToolCalls() {\n if (this.toolCalls.length === this.toolOutputs.length) {\n return;\n }\n\n const toolCallIds = new Set(this.toolCalls.map((call) => call.callId));\n const toolOutputIds = new Set(this.toolOutputs.map((output) => output.callId));\n\n // intersection of tool call ids and tool output ids\n const validCallIds = intersection(toolCallIds, toolOutputIds);\n\n // filter out tool calls that don't have a corresponding tool output\n this.toolCalls = this.toolCalls.filter((call) => {\n if (validCallIds.has(call.callId)) return true;\n this.logger.warn(\n {\n callId: call.callId,\n toolName: call.name,\n },\n 'function call missing the corresponding function output, ignoring',\n );\n return false;\n });\n\n // filter out tool outputs that don't have a corresponding tool call\n this.toolOutputs = this.toolOutputs.filter((output) => {\n if (validCallIds.has(output.callId)) return true;\n this.logger.warn(\n {\n callId: output.callId,\n toolName: output.name,\n },\n 'function output missing the corresponding function call, ignoring',\n );\n return false;\n });\n }\n\n flatten() {\n const items: ChatItem[] = [];\n if (this.message) items.push(this.message);\n items.push(...this.toolCalls, ...this.toolOutputs);\n return items;\n }\n}\n\nfunction intersection<T>(set1: Set<T>, set2: Set<T>): Set<T> {\n return new Set([...set1].filter((item) => set2.has(item)));\n}\n\n/**\n * Group chat items (messages, function calls, and function outputs)\n * into coherent groups based on their item IDs and call IDs.\n *\n * Each group will contain:\n * - Zero or one assistant message\n * - Zero or more function/tool calls\n * - The corresponding function/tool outputs matched by call_id\n *\n * User and system messages are placed in their own individual groups.\n *\n * @param chatCtx - The chat context containing all conversation items\n * @returns A list of ChatItemGroup objects representing the grouped conversation\n */\nexport function groupToolCalls(chatCtx: ChatContext) {\n const itemGroups: Record<string, ChatItemGroup> = {};\n const insertionOrder: Record<string, number> = {};\n const toolOutputs: FunctionCallOutput[] = [];\n const logger = log();\n\n let insertionIndex = 0;\n for (const item of chatCtx.items) {\n const isAssistantMessage = item.type === 'message' && item.role === 'assistant';\n const isFunctionCall = item.type === 'function_call';\n const isFunctionCallOutput = item.type === 'function_call_output';\n\n if (isAssistantMessage || isFunctionCall) {\n // only assistant messages and function calls can be grouped\n const groupId
|
|
1
|
+
{"version":3,"sources":["../../../src/llm/provider_format/utils.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { log } from '../../log.js';\nimport type {\n ChatContext,\n ChatItem,\n ChatMessage,\n FunctionCall,\n FunctionCallOutput,\n} from '../chat_context.js';\n\nclass ChatItemGroup {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n logger = log();\n\n constructor(params: {\n message?: ChatMessage;\n toolCalls: FunctionCall[];\n toolOutputs: FunctionCallOutput[];\n }) {\n this.message = params.message;\n this.toolCalls = params.toolCalls;\n this.toolOutputs = params.toolOutputs;\n }\n\n static create(params?: {\n message?: ChatMessage;\n toolCalls?: FunctionCall[];\n toolOutputs?: FunctionCallOutput[];\n }) {\n const { message, toolCalls = [], toolOutputs = [] } = params ?? {};\n return new ChatItemGroup({ message, toolCalls, toolOutputs });\n }\n\n get isEmpty() {\n return (\n this.message === undefined && this.toolCalls.length === 0 && this.toolOutputs.length === 0\n );\n }\n\n add(item: ChatItem) {\n if (item.type === 'message') {\n if (this.message) {\n throw new Error('only one message is allowed in a group');\n }\n this.message = item;\n } else if (item.type === 'function_call') {\n this.toolCalls.push(item);\n } else if (item.type === 'function_call_output') {\n this.toolOutputs.push(item);\n }\n return this;\n }\n\n removeInvalidToolCalls() {\n if (this.toolCalls.length === this.toolOutputs.length) {\n return;\n }\n\n const toolCallIds = new Set(this.toolCalls.map((call) => call.callId));\n const toolOutputIds = new Set(this.toolOutputs.map((output) => output.callId));\n\n // intersection of tool call ids and tool output ids\n const validCallIds = intersection(toolCallIds, toolOutputIds);\n\n // filter out tool calls that don't have a corresponding tool output\n this.toolCalls = this.toolCalls.filter((call) => {\n if (validCallIds.has(call.callId)) return true;\n this.logger.warn(\n {\n callId: call.callId,\n toolName: call.name,\n },\n 'function call missing the corresponding function output, ignoring',\n );\n return false;\n });\n\n // filter out tool outputs that don't have a corresponding tool call\n this.toolOutputs = this.toolOutputs.filter((output) => {\n if (validCallIds.has(output.callId)) return true;\n this.logger.warn(\n {\n callId: output.callId,\n toolName: output.name,\n },\n 'function output missing the corresponding function call, ignoring',\n );\n return false;\n });\n }\n\n flatten() {\n const items: ChatItem[] = [];\n if (this.message) items.push(this.message);\n items.push(...this.toolCalls, ...this.toolOutputs);\n return items;\n }\n}\n\nfunction intersection<T>(set1: Set<T>, set2: Set<T>): Set<T> {\n return new Set([...set1].filter((item) => set2.has(item)));\n}\n\n/**\n * Group chat items (messages, function calls, and function outputs)\n * into coherent groups based on their item IDs and call IDs.\n *\n * Each group will contain:\n * - Zero or one assistant message\n * - Zero or more function/tool calls\n * - The corresponding function/tool outputs matched by call_id\n *\n * User and system messages are placed in their own individual groups.\n *\n * @param chatCtx - The chat context containing all conversation items\n * @returns A list of ChatItemGroup objects representing the grouped conversation\n */\nexport function groupToolCalls(chatCtx: ChatContext) {\n const itemGroups: Record<string, ChatItemGroup> = {};\n const insertionOrder: Record<string, number> = {};\n const toolOutputs: FunctionCallOutput[] = [];\n const logger = log();\n\n let insertionIndex = 0;\n for (const item of chatCtx.items) {\n const isAssistantMessage = item.type === 'message' && item.role === 'assistant';\n const isFunctionCall = item.type === 'function_call';\n const isFunctionCallOutput = item.type === 'function_call_output';\n\n if (isAssistantMessage || isFunctionCall) {\n // only assistant messages and function calls can be grouped\n // For function calls, use group_id if available (for parallel function calls),\n // otherwise fall back to id-based grouping for backwards compatibility\n const groupId =\n item.type === 'function_call' && item.groupId ? item.groupId : item.id.split('/')[0]!;\n\n if (itemGroups[groupId] === undefined) {\n itemGroups[groupId] = ChatItemGroup.create();\n\n // we use insertion order to sort the groups as they are added to the context\n // simulating the OrderedDict in python\n insertionOrder[groupId] = insertionIndex;\n insertionIndex++;\n }\n itemGroups[groupId]!.add(item);\n } else if (isFunctionCallOutput) {\n toolOutputs.push(item);\n } else {\n itemGroups[item.id] = ChatItemGroup.create().add(item);\n }\n }\n\n // add tool outputs to their corresponding groups\n const callIdToGroup: Record<string, ChatItemGroup> = {};\n for (const group of Object.values(itemGroups)) {\n for (const toolCall of group.toolCalls) {\n callIdToGroup[toolCall.callId] = group;\n }\n }\n\n for (const toolOutput of toolOutputs) {\n const group = callIdToGroup[toolOutput.callId];\n if (group === undefined) {\n logger.warn(\n { callId: toolOutput.callId, toolName: toolOutput.name },\n 'function output missing the corresponding function call, ignoring',\n );\n continue;\n }\n group.add(toolOutput);\n }\n\n // validate that each group and remove invalid tool calls and tool outputs\n for (const group of Object.values(itemGroups)) {\n group.removeInvalidToolCalls();\n }\n\n // sort groups by their item id\n const orderedGroups = Object.entries(itemGroups)\n .sort((a, b) => insertionOrder[a[0]]! - insertionOrder[b[0]]!)\n .map(([, group]) => group);\n return orderedGroups;\n}\n"],"mappings":"AAGA,SAAS,WAAW;AASpB,MAAM,cAAc;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA,EACA,SAAS,IAAI;AAAA,EAEb,YAAY,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AAE,IAAI,IAAI;AAAA,IACvD;AAAA,EACF;AAGA,QAAM,gBAA+C,CAAC;AACtD,aAAW,SAAS,OAAO,OAAO,UAAU,GAAG;AAC7C,eAAW,YAAY,MAAM,WAAW;AACtC,oBAAc,SAAS,MAAM,IAAI;AAAA,IACnC;AAAA,EACF;AAEA,aAAW,cAAc,aAAa;AACpC,UAAM,QAAQ,cAAc,WAAW,MAAM;AAC7C,QAAI,UAAU,QAAW;AACvB,aAAO;AAAA,QACL,EAAE,QAAQ,WAAW,QAAQ,UAAU,WAAW,KAAK;AAAA,QACvD;AAAA,MACF;AACA;AAAA,IACF;AACA,UAAM,IAAI,UAAU;AAAA,EACtB;AAGA,aAAW,SAAS,OAAO,OAAO,UAAU,GAAG;AAC7C,UAAM,uBAAuB;AAAA,EAC/B;AAGA,QAAM,gBAAgB,OAAO,QAAQ,UAAU,EAC5C,KAAK,CAAC,GAAG,MAAM,eAAe,EAAE,CAAC,CAAC,IAAK,eAAe,EAAE,CAAC,CAAC,CAAE,EAC5D,IAAI,CAAC,CAAC,EAAE,KAAK,MAAM,KAAK;AAC3B,SAAO;AACT;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/voice/background_audio.ts","../../../node_modules/.pnpm/tsup@8.4.0_@microsoft+api-extractor@7.43.7_@types+node@22.15.30__postcss@8.4.38_tsx@4.20.4_typescript@5.4.5/node_modules/tsup/assets/cjs_shims.js"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport {\n AudioFrame,\n AudioMixer,\n AudioSource,\n LocalAudioTrack,\n type LocalTrackPublication,\n type Room,\n TrackPublishOptions,\n} from '@livekit/rtc-node';\nimport { dirname, join } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { audioFramesFromFile, loopAudioFramesFromFile } from '../audio.js';\nimport { log } from '../log.js';\nimport { Future, Task, cancelAndWait } from '../utils.js';\nimport type { AgentSession } from './agent_session.js';\nimport { AgentSessionEventTypes, type AgentStateChangedEvent } from './events.js';\n\nconst TASK_TIMEOUT_MS = 500;\n\nexport enum BuiltinAudioClip {\n OFFICE_AMBIENCE = 'office-ambience.ogg',\n KEYBOARD_TYPING = 'keyboard-typing.ogg',\n KEYBOARD_TYPING2 = 'keyboard-typing2.ogg',\n}\n\nexport function isBuiltinAudioClip(\n source: AudioSourceType | AudioConfig | AudioConfig[],\n): source is BuiltinAudioClip {\n return (\n typeof source === 'string' &&\n Object.values(BuiltinAudioClip).includes(source as BuiltinAudioClip)\n );\n}\n\nexport function getBuiltinAudioPath(clip: BuiltinAudioClip): string {\n const resourcesPath = join(dirname(fileURLToPath(import.meta.url)), '../../resources');\n return join(resourcesPath, clip);\n}\n\nexport type AudioSourceType = string | BuiltinAudioClip | AsyncIterable<AudioFrame>;\n\nexport interface AudioConfig {\n source: AudioSourceType;\n volume?: number;\n probability?: number;\n}\n\nexport interface BackgroundAudioPlayerOptions {\n /**\n * Ambient sound to play continuously in the background.\n * Can be a file path, BuiltinAudioClip, or AudioConfig.\n * File paths will be looped automatically.\n */\n ambientSound?: AudioSourceType | AudioConfig | AudioConfig[];\n\n /**\n * Sound to play when the agent is thinking.\n * Plays when agent state changes to 'thinking' and stops when it changes to other states.\n */\n thinkingSound?: AudioSourceType | AudioConfig | AudioConfig[];\n\n /**\n * Stream timeout in milliseconds\n * @defaultValue 200\n */\n streamTimeoutMs?: number;\n}\n\nexport interface BackgroundAudioStartOptions {\n room: Room;\n agentSession?: AgentSession;\n trackPublishOptions?: TrackPublishOptions;\n}\n\n// Queue size for AudioSource buffer (400ms)\n// Kept small to avoid abrupt cutoffs when removing sounds\nconst AUDIO_SOURCE_BUFFER_MS = 400;\n\nexport class PlayHandle {\n private doneFuture = new Future<void>();\n private stopFuture = new Future<void>();\n\n done(): boolean {\n return this.doneFuture.done;\n }\n\n stop(): void {\n if (this.done()) return;\n\n if (!this.stopFuture.done) {\n this.stopFuture.resolve();\n }\n\n this._markPlayoutDone();\n }\n\n async waitForPlayout(): Promise<void> {\n return this.doneFuture.await;\n }\n\n _markPlayoutDone(): void {\n if (!this.doneFuture.done) {\n this.doneFuture.resolve();\n }\n }\n}\n\n/**\n * Manages background audio playback for LiveKit agent sessions\n *\n * This class handles playing ambient sounds and manages audio track publishing.\n * It supports:\n * - Continuous ambient sound playback with looping\n * - Thinking sound playback during agent processing\n * - Multiple simultaneous audio streams via AudioMixer\n * - Volume control and probability-based sound selection\n * - Integration with LiveKit rooms and agent sessions\n *\n * @example\n * ```typescript\n * const player = new BackgroundAudioPlayer({\n * ambientSound: { source: BuiltinAudioClip.OFFICE_AMBIENCE, volume: 0.8 },\n * thinkingSound: { source: BuiltinAudioClip.KEYBOARD_TYPING, volume: 0.6 },\n * });\n *\n * await player.start({ room, agentSession });\n * ```\n */\nexport class BackgroundAudioPlayer {\n private ambientSound?: AudioSourceType | AudioConfig | AudioConfig[];\n private thinkingSound?: AudioSourceType | AudioConfig | AudioConfig[];\n private streamTimeoutMs: number;\n\n private playTasks: Task<void>[] = [];\n private audioSource = new AudioSource(48000, 1, AUDIO_SOURCE_BUFFER_MS);\n private audioMixer: AudioMixer;\n private mixerTask?: Task<void>;\n\n private room?: Room;\n private agentSession?: AgentSession;\n private publication?: LocalTrackPublication;\n private trackPublishOptions?: TrackPublishOptions;\n private republishTask?: Task<void>;\n\n private ambientHandle?: PlayHandle;\n private thinkingHandle?: PlayHandle;\n\n private closed = true;\n\n // TODO (Brian): add lock\n\n #logger = log();\n\n constructor(options?: BackgroundAudioPlayerOptions) {\n const { ambientSound, thinkingSound, streamTimeoutMs = 200 } = options || {};\n\n this.ambientSound = ambientSound;\n this.thinkingSound = thinkingSound;\n this.streamTimeoutMs = streamTimeoutMs;\n\n this.audioMixer = new AudioMixer(48000, 1, {\n blocksize: 4800, // 100ms at 48kHz\n capacity: 1,\n streamTimeoutMs: this.streamTimeoutMs,\n });\n }\n\n /**\n * Select a sound from a list of background sound based on probability weights\n * Return undefined if no sound is selected (when sum of probabilities < 1.0).\n */\n private selectSoundFromList(sounds: AudioConfig[]): AudioConfig | undefined {\n const totalProbability = sounds.reduce((sum, sound) => sum + (sound.probability ?? 1.0), 0);\n\n if (totalProbability <= 0) {\n return undefined;\n }\n\n if (totalProbability < 1.0 && Math.random() > totalProbability) {\n return undefined;\n }\n\n const normalizeFactor = totalProbability <= 1.0 ? 1.0 : totalProbability;\n const r = Math.random() * Math.min(totalProbability, 1.0);\n let cumulative = 0.0;\n\n for (const sound of sounds) {\n const prob = sound.probability ?? 1.0;\n if (prob <= 0) {\n continue;\n }\n\n const normProb = prob / normalizeFactor;\n cumulative += normProb;\n\n if (r <= cumulative) {\n return sound;\n }\n }\n\n return sounds[sounds.length - 1];\n }\n\n private normalizeSoundSource(\n source?: AudioSourceType | AudioConfig | AudioConfig[],\n ): { source: AudioSourceType; volume: number } | undefined {\n if (source === undefined) {\n return undefined;\n }\n\n if (typeof source === 'string') {\n return {\n source: this.normalizeBuiltinAudio(source),\n volume: 1.0,\n };\n }\n\n if (Array.isArray(source)) {\n const selected = this.selectSoundFromList(source);\n if (selected === undefined) {\n return undefined;\n }\n\n return {\n source: selected.source,\n volume: selected.volume ?? 1.0,\n };\n }\n\n if (typeof source === 'object' && 'source' in source) {\n return {\n source: this.normalizeBuiltinAudio(source.source),\n volume: source.volume ?? 1.0,\n };\n }\n\n return { source, volume: 1.0 };\n }\n\n private normalizeBuiltinAudio(source: AudioSourceType): AudioSourceType {\n if (isBuiltinAudioClip(source)) {\n return getBuiltinAudioPath(source);\n }\n return source;\n }\n\n play(audio: AudioSourceType | AudioConfig | AudioConfig[], loop = false): PlayHandle {\n const normalized = this.normalizeSoundSource(audio);\n if (normalized === undefined) {\n const handle = new PlayHandle();\n handle._markPlayoutDone();\n return handle;\n }\n\n const { source, volume } = normalized;\n const playHandle = new PlayHandle();\n\n const task = Task.from(async ({ signal }) => {\n await this.playTask({ playHandle, sound: source, volume, loop, signal });\n });\n\n task.addDoneCallback(() => {\n playHandle._markPlayoutDone();\n this.playTasks.splice(this.playTasks.indexOf(task), 1);\n });\n\n this.playTasks.push(task);\n return playHandle;\n }\n\n /**\n * Start the background audio system, publishing the audio track\n * and beginning playback of any configured ambient sound.\n *\n * If `ambientSound` is provided (and contains file paths), they will loop\n * automatically. If `ambientSound` contains AsyncIterators, they are assumed\n * to be already infinite or looped.\n *\n * @param options - Options for starting background audio playback\n */\n async start(options: BackgroundAudioStartOptions): Promise<void> {\n const { room, agentSession, trackPublishOptions } = options;\n this.room = room;\n this.agentSession = agentSession;\n this.trackPublishOptions = trackPublishOptions;\n\n this.closed = false;\n\n await this.publishTrack();\n\n // TODO (Brian): check job context is not fake\n\n this.mixerTask = Task.from(async () => {\n try {\n await this.runMixerTask();\n } catch (err) {\n if (this.closed) return; // expected when AudioSource is closed\n throw err;\n }\n });\n\n this.room.on('reconnected', this.onReconnected);\n\n this.agentSession?.on(AgentSessionEventTypes.AgentStateChanged, this.onAgentStateChanged);\n if (!this.ambientSound) return;\n\n const normalized = this.normalizeSoundSource(this.ambientSound);\n if (!normalized) return;\n\n const { source, volume } = normalized;\n const selectedSound: AudioConfig = { source, volume, probability: 1.0 };\n this.ambientHandle = this.play(selectedSound, typeof source === 'string');\n }\n\n /**\n * Close and cleanup the background audio system\n */\n async close(): Promise<void> {\n this.closed = true;\n\n await cancelAndWait(this.playTasks, TASK_TIMEOUT_MS);\n\n if (this.republishTask) {\n await this.republishTask.cancelAndWait(TASK_TIMEOUT_MS);\n }\n\n await this.audioMixer.aclose();\n await this.audioSource.close();\n\n if (this.mixerTask) {\n await this.mixerTask.cancelAndWait(TASK_TIMEOUT_MS);\n }\n\n this.agentSession?.off(AgentSessionEventTypes.AgentStateChanged, this.onAgentStateChanged);\n this.room?.off('reconnected', this.onReconnected);\n\n if (this.publication && this.publication.sid) {\n await this.room?.localParticipant?.unpublishTrack(this.publication.sid);\n }\n }\n\n /**\n * Get the current track publication\n */\n getPublication(): LocalTrackPublication | undefined {\n return this.publication;\n }\n\n private async publishTrack(): Promise<void> {\n if (this.publication !== undefined) {\n return;\n }\n\n const track = LocalAudioTrack.createAudioTrack('background_audio', this.audioSource);\n\n if (this.room?.localParticipant === undefined) {\n throw new Error('Local participant not available');\n }\n\n const publication = await this.room.localParticipant.publishTrack(\n track,\n this.trackPublishOptions ?? new TrackPublishOptions(),\n );\n\n this.publication = publication;\n this.#logger.debug(`Background audio track published: ${this.publication.sid}`);\n }\n\n private onReconnected = (): void => {\n if (this.republishTask) {\n this.republishTask.cancel();\n }\n\n this.publication = undefined;\n this.republishTask = Task.from(async () => {\n await this.republishTrackTask();\n });\n };\n\n private async republishTrackTask(): Promise<void> {\n // TODO (Brian): add lock protection when implementing lock\n await this.publishTrack();\n }\n\n private async runMixerTask(): Promise<void> {\n for await (const frame of this.audioMixer) {\n await this.audioSource.captureFrame(frame);\n }\n }\n\n private onAgentStateChanged = (ev: AgentStateChangedEvent): void => {\n if (!this.thinkingSound) {\n return;\n }\n\n if (ev.newState === 'thinking') {\n if (this.thinkingHandle && !this.thinkingHandle.done()) {\n return;\n }\n\n const normalized = this.normalizeSoundSource(this.thinkingSound);\n if (normalized) {\n const { source, volume } = normalized;\n const selectedSound: AudioConfig = { source, volume, probability: 1.0 };\n // Loop thinking sound while in thinking state (same as ambient)\n this.thinkingHandle = this.play(selectedSound, typeof source === 'string');\n }\n } else {\n this.thinkingHandle?.stop();\n }\n };\n\n // Note: Python uses numpy, TS uses typed arrays for equivalent logic\n private applyVolumeToFrame(frame: AudioFrame, volume: number): AudioFrame {\n const int16Data = new Int16Array(\n frame.data.buffer,\n frame.data.byteOffset,\n frame.data.byteLength / 2,\n );\n const float32Data = new Float32Array(int16Data.length);\n\n for (let i = 0; i < int16Data.length; i++) {\n float32Data[i] = int16Data[i]!;\n }\n\n const volumeFactor = 10 ** Math.log10(volume);\n for (let i = 0; i < float32Data.length; i++) {\n float32Data[i]! *= volumeFactor;\n }\n\n const outputData = new Int16Array(float32Data.length);\n for (let i = 0; i < float32Data.length; i++) {\n const clipped = Math.max(-32768, Math.min(32767, float32Data[i]!));\n outputData[i] = Math.round(clipped);\n }\n\n return new AudioFrame(outputData, frame.sampleRate, frame.channels, frame.samplesPerChannel);\n }\n\n private async playTask({\n playHandle,\n sound,\n volume,\n loop,\n signal,\n }: {\n playHandle: PlayHandle;\n sound: AudioSourceType;\n volume: number;\n loop: boolean;\n signal: AbortSignal;\n }): Promise<void> {\n if (isBuiltinAudioClip(sound)) {\n sound = getBuiltinAudioPath(sound);\n }\n\n let audioStream: AsyncIterable<AudioFrame>;\n if (typeof sound === 'string') {\n audioStream = loop\n ? loopAudioFramesFromFile(sound, { abortSignal: signal })\n : audioFramesFromFile(sound, { abortSignal: signal });\n } else {\n audioStream = sound;\n }\n\n const applyVolume = this.applyVolumeToFrame.bind(this);\n async function* genWrapper(): AsyncGenerator<AudioFrame> {\n for await (const frame of audioStream) {\n if (signal.aborted || playHandle.done()) break;\n yield volume !== 1.0 ? applyVolume(frame, volume) : frame;\n }\n playHandle._markPlayoutDone();\n }\n\n const gen = genWrapper();\n try {\n this.audioMixer.addStream(gen);\n await playHandle.waitForPlayout();\n } finally {\n this.audioMixer.removeStream(gen);\n playHandle._markPlayoutDone();\n\n if (playHandle.done()) {\n await gen.return(undefined);\n }\n }\n }\n}\n","// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () =>\n typeof document === 'undefined'\n ? new URL(`file:${__filename}`).href\n : (document.currentScript && document.currentScript.src) ||\n new URL('main.js', document.baseURI).href\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;ACKA,IAAM,mBAAmB,MACvB,OAAO,aAAa,cAChB,IAAI,IAAI,QAAQ,UAAU,EAAE,EAAE,OAC7B,SAA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AAEA,UAAM,cAAc,KAAK,mBAAmB,KAAK,IAAI;AACrD,oBAAgB,aAAyC;AACvD,uBAAiB,SAAS,aAAa;AACrC,YAAI,OAAO,WAAW,WAAW,KAAK,EAAG;AACzC,cAAM,WAAW,IAAM,YAAY,OAAO,MAAM,IAAI;AAAA,MACtD;AACA,iBAAW,iBAAiB;AAAA,IAC9B;AAEA,UAAM,MAAM,WAAW;AACvB,QAAI;AACF,WAAK,WAAW,UAAU,GAAG;AAC7B,YAAM,WAAW,eAAe;AAAA,IAClC,UAAE;AACA,WAAK,WAAW,aAAa,GAAG;AAChC,iBAAW,iBAAiB;AAE5B,UAAI,WAAW,KAAK,GAAG;AACrB,cAAM,IAAI,OAAO,MAAS;AAAA,MAC5B;AAAA,IACF;AAAA,EACF;AACF;","names":["BuiltinAudioClip"]}
|
|
1
|
+
{"version":3,"sources":["../../src/voice/background_audio.ts","../../../node_modules/.pnpm/tsup@8.4.0_@microsoft+api-extractor@7.43.7_@types+node@22.15.30__postcss@8.5.6_tsx@4.20.4_typescript@5.4.5/node_modules/tsup/assets/cjs_shims.js"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport {\n AudioFrame,\n AudioMixer,\n AudioSource,\n LocalAudioTrack,\n type LocalTrackPublication,\n type Room,\n TrackPublishOptions,\n} from '@livekit/rtc-node';\nimport { dirname, join } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { audioFramesFromFile, loopAudioFramesFromFile } from '../audio.js';\nimport { log } from '../log.js';\nimport { Future, Task, cancelAndWait } from '../utils.js';\nimport type { AgentSession } from './agent_session.js';\nimport { AgentSessionEventTypes, type AgentStateChangedEvent } from './events.js';\n\nconst TASK_TIMEOUT_MS = 500;\n\nexport enum BuiltinAudioClip {\n OFFICE_AMBIENCE = 'office-ambience.ogg',\n KEYBOARD_TYPING = 'keyboard-typing.ogg',\n KEYBOARD_TYPING2 = 'keyboard-typing2.ogg',\n}\n\nexport function isBuiltinAudioClip(\n source: AudioSourceType | AudioConfig | AudioConfig[],\n): source is BuiltinAudioClip {\n return (\n typeof source === 'string' &&\n Object.values(BuiltinAudioClip).includes(source as BuiltinAudioClip)\n );\n}\n\nexport function getBuiltinAudioPath(clip: BuiltinAudioClip): string {\n const resourcesPath = join(dirname(fileURLToPath(import.meta.url)), '../../resources');\n return join(resourcesPath, clip);\n}\n\nexport type AudioSourceType = string | BuiltinAudioClip | AsyncIterable<AudioFrame>;\n\nexport interface AudioConfig {\n source: AudioSourceType;\n volume?: number;\n probability?: number;\n}\n\nexport interface BackgroundAudioPlayerOptions {\n /**\n * Ambient sound to play continuously in the background.\n * Can be a file path, BuiltinAudioClip, or AudioConfig.\n * File paths will be looped automatically.\n */\n ambientSound?: AudioSourceType | AudioConfig | AudioConfig[];\n\n /**\n * Sound to play when the agent is thinking.\n * Plays when agent state changes to 'thinking' and stops when it changes to other states.\n */\n thinkingSound?: AudioSourceType | AudioConfig | AudioConfig[];\n\n /**\n * Stream timeout in milliseconds\n * @defaultValue 200\n */\n streamTimeoutMs?: number;\n}\n\nexport interface BackgroundAudioStartOptions {\n room: Room;\n agentSession?: AgentSession;\n trackPublishOptions?: TrackPublishOptions;\n}\n\n// Queue size for AudioSource buffer (400ms)\n// Kept small to avoid abrupt cutoffs when removing sounds\nconst AUDIO_SOURCE_BUFFER_MS = 400;\n\nexport class PlayHandle {\n private doneFuture = new Future<void>();\n private stopFuture = new Future<void>();\n\n done(): boolean {\n return this.doneFuture.done;\n }\n\n stop(): void {\n if (this.done()) return;\n\n if (!this.stopFuture.done) {\n this.stopFuture.resolve();\n }\n\n this._markPlayoutDone();\n }\n\n async waitForPlayout(): Promise<void> {\n return this.doneFuture.await;\n }\n\n _markPlayoutDone(): void {\n if (!this.doneFuture.done) {\n this.doneFuture.resolve();\n }\n }\n}\n\n/**\n * Manages background audio playback for LiveKit agent sessions\n *\n * This class handles playing ambient sounds and manages audio track publishing.\n * It supports:\n * - Continuous ambient sound playback with looping\n * - Thinking sound playback during agent processing\n * - Multiple simultaneous audio streams via AudioMixer\n * - Volume control and probability-based sound selection\n * - Integration with LiveKit rooms and agent sessions\n *\n * @example\n * ```typescript\n * const player = new BackgroundAudioPlayer({\n * ambientSound: { source: BuiltinAudioClip.OFFICE_AMBIENCE, volume: 0.8 },\n * thinkingSound: { source: BuiltinAudioClip.KEYBOARD_TYPING, volume: 0.6 },\n * });\n *\n * await player.start({ room, agentSession });\n * ```\n */\nexport class BackgroundAudioPlayer {\n private ambientSound?: AudioSourceType | AudioConfig | AudioConfig[];\n private thinkingSound?: AudioSourceType | AudioConfig | AudioConfig[];\n private streamTimeoutMs: number;\n\n private playTasks: Task<void>[] = [];\n private audioSource = new AudioSource(48000, 1, AUDIO_SOURCE_BUFFER_MS);\n private audioMixer: AudioMixer;\n private mixerTask?: Task<void>;\n\n private room?: Room;\n private agentSession?: AgentSession;\n private publication?: LocalTrackPublication;\n private trackPublishOptions?: TrackPublishOptions;\n private republishTask?: Task<void>;\n\n private ambientHandle?: PlayHandle;\n private thinkingHandle?: PlayHandle;\n\n private closed = true;\n\n // TODO (Brian): add lock\n\n #logger = log();\n\n constructor(options?: BackgroundAudioPlayerOptions) {\n const { ambientSound, thinkingSound, streamTimeoutMs = 200 } = options || {};\n\n this.ambientSound = ambientSound;\n this.thinkingSound = thinkingSound;\n this.streamTimeoutMs = streamTimeoutMs;\n\n this.audioMixer = new AudioMixer(48000, 1, {\n blocksize: 4800, // 100ms at 48kHz\n capacity: 1,\n streamTimeoutMs: this.streamTimeoutMs,\n });\n }\n\n /**\n * Select a sound from a list of background sound based on probability weights\n * Return undefined if no sound is selected (when sum of probabilities < 1.0).\n */\n private selectSoundFromList(sounds: AudioConfig[]): AudioConfig | undefined {\n const totalProbability = sounds.reduce((sum, sound) => sum + (sound.probability ?? 1.0), 0);\n\n if (totalProbability <= 0) {\n return undefined;\n }\n\n if (totalProbability < 1.0 && Math.random() > totalProbability) {\n return undefined;\n }\n\n const normalizeFactor = totalProbability <= 1.0 ? 1.0 : totalProbability;\n const r = Math.random() * Math.min(totalProbability, 1.0);\n let cumulative = 0.0;\n\n for (const sound of sounds) {\n const prob = sound.probability ?? 1.0;\n if (prob <= 0) {\n continue;\n }\n\n const normProb = prob / normalizeFactor;\n cumulative += normProb;\n\n if (r <= cumulative) {\n return sound;\n }\n }\n\n return sounds[sounds.length - 1];\n }\n\n private normalizeSoundSource(\n source?: AudioSourceType | AudioConfig | AudioConfig[],\n ): { source: AudioSourceType; volume: number } | undefined {\n if (source === undefined) {\n return undefined;\n }\n\n if (typeof source === 'string') {\n return {\n source: this.normalizeBuiltinAudio(source),\n volume: 1.0,\n };\n }\n\n if (Array.isArray(source)) {\n const selected = this.selectSoundFromList(source);\n if (selected === undefined) {\n return undefined;\n }\n\n return {\n source: selected.source,\n volume: selected.volume ?? 1.0,\n };\n }\n\n if (typeof source === 'object' && 'source' in source) {\n return {\n source: this.normalizeBuiltinAudio(source.source),\n volume: source.volume ?? 1.0,\n };\n }\n\n return { source, volume: 1.0 };\n }\n\n private normalizeBuiltinAudio(source: AudioSourceType): AudioSourceType {\n if (isBuiltinAudioClip(source)) {\n return getBuiltinAudioPath(source);\n }\n return source;\n }\n\n play(audio: AudioSourceType | AudioConfig | AudioConfig[], loop = false): PlayHandle {\n const normalized = this.normalizeSoundSource(audio);\n if (normalized === undefined) {\n const handle = new PlayHandle();\n handle._markPlayoutDone();\n return handle;\n }\n\n const { source, volume } = normalized;\n const playHandle = new PlayHandle();\n\n const task = Task.from(async ({ signal }) => {\n await this.playTask({ playHandle, sound: source, volume, loop, signal });\n });\n\n task.addDoneCallback(() => {\n playHandle._markPlayoutDone();\n this.playTasks.splice(this.playTasks.indexOf(task), 1);\n });\n\n this.playTasks.push(task);\n return playHandle;\n }\n\n /**\n * Start the background audio system, publishing the audio track\n * and beginning playback of any configured ambient sound.\n *\n * If `ambientSound` is provided (and contains file paths), they will loop\n * automatically. If `ambientSound` contains AsyncIterators, they are assumed\n * to be already infinite or looped.\n *\n * @param options - Options for starting background audio playback\n */\n async start(options: BackgroundAudioStartOptions): Promise<void> {\n const { room, agentSession, trackPublishOptions } = options;\n this.room = room;\n this.agentSession = agentSession;\n this.trackPublishOptions = trackPublishOptions;\n\n this.closed = false;\n\n await this.publishTrack();\n\n // TODO (Brian): check job context is not fake\n\n this.mixerTask = Task.from(async () => {\n try {\n await this.runMixerTask();\n } catch (err) {\n if (this.closed) return; // expected when AudioSource is closed\n throw err;\n }\n });\n\n this.room.on('reconnected', this.onReconnected);\n\n this.agentSession?.on(AgentSessionEventTypes.AgentStateChanged, this.onAgentStateChanged);\n if (!this.ambientSound) return;\n\n const normalized = this.normalizeSoundSource(this.ambientSound);\n if (!normalized) return;\n\n const { source, volume } = normalized;\n const selectedSound: AudioConfig = { source, volume, probability: 1.0 };\n this.ambientHandle = this.play(selectedSound, typeof source === 'string');\n }\n\n /**\n * Close and cleanup the background audio system\n */\n async close(): Promise<void> {\n this.closed = true;\n\n await cancelAndWait(this.playTasks, TASK_TIMEOUT_MS);\n\n if (this.republishTask) {\n await this.republishTask.cancelAndWait(TASK_TIMEOUT_MS);\n }\n\n await this.audioMixer.aclose();\n await this.audioSource.close();\n\n if (this.mixerTask) {\n await this.mixerTask.cancelAndWait(TASK_TIMEOUT_MS);\n }\n\n this.agentSession?.off(AgentSessionEventTypes.AgentStateChanged, this.onAgentStateChanged);\n this.room?.off('reconnected', this.onReconnected);\n\n if (this.publication && this.publication.sid) {\n await this.room?.localParticipant?.unpublishTrack(this.publication.sid);\n }\n }\n\n /**\n * Get the current track publication\n */\n getPublication(): LocalTrackPublication | undefined {\n return this.publication;\n }\n\n private async publishTrack(): Promise<void> {\n if (this.publication !== undefined) {\n return;\n }\n\n const track = LocalAudioTrack.createAudioTrack('background_audio', this.audioSource);\n\n if (this.room?.localParticipant === undefined) {\n throw new Error('Local participant not available');\n }\n\n const publication = await this.room.localParticipant.publishTrack(\n track,\n this.trackPublishOptions ?? new TrackPublishOptions(),\n );\n\n this.publication = publication;\n this.#logger.debug(`Background audio track published: ${this.publication.sid}`);\n }\n\n private onReconnected = (): void => {\n if (this.republishTask) {\n this.republishTask.cancel();\n }\n\n this.publication = undefined;\n this.republishTask = Task.from(async () => {\n await this.republishTrackTask();\n });\n };\n\n private async republishTrackTask(): Promise<void> {\n // TODO (Brian): add lock protection when implementing lock\n await this.publishTrack();\n }\n\n private async runMixerTask(): Promise<void> {\n for await (const frame of this.audioMixer) {\n await this.audioSource.captureFrame(frame);\n }\n }\n\n private onAgentStateChanged = (ev: AgentStateChangedEvent): void => {\n if (!this.thinkingSound) {\n return;\n }\n\n if (ev.newState === 'thinking') {\n if (this.thinkingHandle && !this.thinkingHandle.done()) {\n return;\n }\n\n const normalized = this.normalizeSoundSource(this.thinkingSound);\n if (normalized) {\n const { source, volume } = normalized;\n const selectedSound: AudioConfig = { source, volume, probability: 1.0 };\n // Loop thinking sound while in thinking state (same as ambient)\n this.thinkingHandle = this.play(selectedSound, typeof source === 'string');\n }\n } else {\n this.thinkingHandle?.stop();\n }\n };\n\n // Note: Python uses numpy, TS uses typed arrays for equivalent logic\n private applyVolumeToFrame(frame: AudioFrame, volume: number): AudioFrame {\n const int16Data = new Int16Array(\n frame.data.buffer,\n frame.data.byteOffset,\n frame.data.byteLength / 2,\n );\n const float32Data = new Float32Array(int16Data.length);\n\n for (let i = 0; i < int16Data.length; i++) {\n float32Data[i] = int16Data[i]!;\n }\n\n const volumeFactor = 10 ** Math.log10(volume);\n for (let i = 0; i < float32Data.length; i++) {\n float32Data[i]! *= volumeFactor;\n }\n\n const outputData = new Int16Array(float32Data.length);\n for (let i = 0; i < float32Data.length; i++) {\n const clipped = Math.max(-32768, Math.min(32767, float32Data[i]!));\n outputData[i] = Math.round(clipped);\n }\n\n return new AudioFrame(outputData, frame.sampleRate, frame.channels, frame.samplesPerChannel);\n }\n\n private async playTask({\n playHandle,\n sound,\n volume,\n loop,\n signal,\n }: {\n playHandle: PlayHandle;\n sound: AudioSourceType;\n volume: number;\n loop: boolean;\n signal: AbortSignal;\n }): Promise<void> {\n if (isBuiltinAudioClip(sound)) {\n sound = getBuiltinAudioPath(sound);\n }\n\n let audioStream: AsyncIterable<AudioFrame>;\n if (typeof sound === 'string') {\n audioStream = loop\n ? loopAudioFramesFromFile(sound, { abortSignal: signal })\n : audioFramesFromFile(sound, { abortSignal: signal });\n } else {\n audioStream = sound;\n }\n\n const applyVolume = this.applyVolumeToFrame.bind(this);\n async function* genWrapper(): AsyncGenerator<AudioFrame> {\n for await (const frame of audioStream) {\n if (signal.aborted || playHandle.done()) break;\n yield volume !== 1.0 ? applyVolume(frame, volume) : frame;\n }\n playHandle._markPlayoutDone();\n }\n\n const gen = genWrapper();\n try {\n this.audioMixer.addStream(gen);\n await playHandle.waitForPlayout();\n } finally {\n this.audioMixer.removeStream(gen);\n playHandle._markPlayoutDone();\n\n if (playHandle.done()) {\n await gen.return(undefined);\n }\n }\n }\n}\n","// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () =>\n typeof document === 'undefined'\n ? new URL(`file:${__filename}`).href\n : (document.currentScript && document.currentScript.src) ||\n new URL('main.js', document.baseURI).href\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;ACKA,IAAM,mBAAmB,MACvB,OAAO,aAAa,cAChB,IAAI,IAAI,QAAQ,UAAU,EAAE,EAAE,OAC7B,SAAS,iBAAiB,SAAS,cAAc,OAClD,IAAI,IAAI,WAAW,SAAS,OAAO,EAAE;AAEpC,IAAM,gBAAgC,iCAAiB;ADR9D,sBAQO;AACP,uBAA8B;AAC9B,sBAA8B;AAC9B,mBAA6D;AAC7D,iBAAoB;AACpB,mBAA4C;AAE5C,oBAAoE;AAEpE,MAAM,kBA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AAA,IAC9B;AAEA,UAAM,MAAM,WAAW;AACvB,QAAI;AACF,WAAK,WAAW,UAAU,GAAG;AAC7B,YAAM,WAAW,eAAe;AAAA,IAClC,UAAE;AACA,WAAK,WAAW,aAAa,GAAG;AAChC,iBAAW,iBAAiB;AAE5B,UAAI,WAAW,KAAK,GAAG;AACrB,cAAM,IAAI,OAAO,MAAS;AAAA,MAC5B;AAAA,IACF;AAAA,EACF;AACF;","names":["BuiltinAudioClip"]}
|
|
@@ -355,7 +355,8 @@ function performLLMInference(node, chatCtx, toolCtx, modelSettings, controller)
|
|
|
355
355
|
name: tool.name,
|
|
356
356
|
args: tool.args,
|
|
357
357
|
// Preserve thought signature for Gemini 3+ thinking mode
|
|
358
|
-
thoughtSignature: tool.thoughtSignature
|
|
358
|
+
thoughtSignature: tool.thoughtSignature,
|
|
359
|
+
extra: tool.extra || {}
|
|
359
360
|
});
|
|
360
361
|
data.generatedToolCalls.push(toolCall);
|
|
361
362
|
await toolCallWriter.write(toolCall);
|