@livekit/agents-plugin-openai 1.3.0 → 1.3.3
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/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/llm.cjs +1 -1
- package/dist/llm.cjs.map +1 -1
- package/dist/llm.js +1 -1
- package/dist/llm.js.map +1 -1
- package/dist/models.cjs.map +1 -1
- package/dist/models.d.cts +2 -2
- package/dist/models.d.ts +2 -2
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js.map +1 -1
- package/dist/realtime/realtime_model.cjs +46 -31
- package/dist/realtime/realtime_model.cjs.map +1 -1
- package/dist/realtime/realtime_model.d.cts +1 -0
- package/dist/realtime/realtime_model.d.ts +1 -0
- package/dist/realtime/realtime_model.d.ts.map +1 -1
- package/dist/realtime/realtime_model.js +46 -31
- package/dist/realtime/realtime_model.js.map +1 -1
- package/dist/realtime/realtime_model.test.cjs +58 -0
- package/dist/realtime/realtime_model.test.cjs.map +1 -1
- package/dist/realtime/realtime_model.test.js +60 -2
- package/dist/realtime/realtime_model.test.js.map +1 -1
- package/package.json +7 -7
- package/src/llm.ts +1 -1
- package/src/models.ts +4 -14
- package/src/realtime/realtime_model.test.ts +91 -2
- package/src/realtime/realtime_model.ts +53 -36
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/realtime/realtime_model.test.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { llm } from '@livekit/agents';\nimport { describe, expect, it } from 'vitest';\nimport type * as api_proto from './api_proto.js';\nimport { livekitItemToOpenAIItem } from './realtime_model.js';\n\ndescribe('livekitItemToOpenAIItem', () => {\n describe('message items', () => {\n it('should use output_text type for assistant messages', async () => {\n const assistantMessage = new llm.ChatMessage({\n role: 'assistant',\n content: 'Hello, how can I help you?',\n id: 'test-assistant-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(assistantMessage)) as api_proto.AssistantItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('assistant');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('output_text');\n expect((content as api_proto.OutputTextContent).text).toBe('Hello, how can I help you?');\n });\n\n it('should use input_text type for user messages', async () => {\n const userMessage = new llm.ChatMessage({\n role: 'user',\n content: 'What is the weather like?',\n id: 'test-user-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(userMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('user');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('input_text');\n expect((content as api_proto.InputTextContent).text).toBe('What is the weather like?');\n });\n\n it('should use input_text type for system messages', async () => {\n const systemMessage = new llm.ChatMessage({\n role: 'system',\n content: 'You are a helpful assistant.',\n id: 'test-system-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(systemMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('input_text');\n });\n\n it('should convert developer role to system role', async () => {\n const developerMessage = new llm.ChatMessage({\n role: 'developer',\n content: 'System instructions.',\n id: 'test-developer-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(developerMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n const content = result.content[0]!;\n expect(content.type).toBe('input_text');\n });\n\n it('should handle multiple content items for assistant', async () => {\n const multiContentMessage = new llm.ChatMessage({\n role: 'assistant',\n content: ['First part.', 'Second part.'],\n id: 'test-multi-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(\n multiContentMessage,\n )) as api_proto.AssistantItem;\n\n expect(result.content).toHaveLength(2);\n const content0 = result.content[0]!;\n const content1 = result.content[1]!;\n expect(content0.type).toBe('output_text');\n expect(content1.type).toBe('output_text');\n });\n\n it('should convert image content to input_image for user messages', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const userMessage = new llm.ChatMessage({\n role: 'user',\n content: [imageContent],\n id: 'test-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(userMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('user');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('input_image');\n expect((content as api_proto.InputImageContent).image_url).toBe(\n `data:image/png;base64,${base64Data}`,\n );\n });\n\n it('should ignore image content for assistant messages', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const assistantMessage = new llm.ChatMessage({\n role: 'assistant',\n content: [imageContent],\n id: 'test-assistant-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(assistantMessage)) as api_proto.AssistantItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('assistant');\n expect(result.content).toHaveLength(0);\n });\n\n it('should ignore image content for system messages', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const systemMessage = new llm.ChatMessage({\n role: 'system',\n content: [imageContent],\n id: 'test-system-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(systemMessage)) as api_proto.SystemItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n expect(result.content).toHaveLength(0);\n });\n\n it('should ignore image content for developer messages mapped to system', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const developerMessage = new llm.ChatMessage({\n role: 'developer',\n content: [imageContent],\n id: 'test-developer-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(developerMessage)) as api_proto.SystemItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n expect(result.content).toHaveLength(0);\n });\n\n it('should handle mixed text and image content', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const userMessage = new llm.ChatMessage({\n role: 'user',\n content: ['Describe this image:', imageContent],\n id: 'test-mixed-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(userMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.content).toHaveLength(2);\n expect(result.content[0]!.type).toBe('input_text');\n expect(result.content[1]!.type).toBe('input_image');\n });\n });\n\n describe('function_call items', () => {\n it('should convert function call items correctly', async () => {\n const functionCall = new llm.FunctionCall({\n callId: 'call-123',\n name: 'get_weather',\n args: '{\"location\": \"San Francisco\"}',\n id: 'test-func-call',\n });\n\n const result = (await livekitItemToOpenAIItem(functionCall)) as api_proto.FunctionCallItem;\n\n expect(result.type).toBe('function_call');\n expect(result.id).toBe('test-func-call');\n expect(result.call_id).toBe('call-123');\n expect(result.name).toBe('get_weather');\n expect(result.arguments).toBe('{\"location\": \"San Francisco\"}');\n });\n });\n\n describe('function_call_output items', () => {\n it('should convert function call output items correctly', async () => {\n const functionOutput = new llm.FunctionCallOutput({\n callId: 'call-123',\n output: 'The weather in San Francisco is sunny.',\n isError: false,\n id: 'test-func-output',\n });\n\n const result = (await livekitItemToOpenAIItem(\n functionOutput,\n )) as api_proto.FunctionCallOutputItem;\n\n expect(result.type).toBe('function_call_output');\n expect(result.id).toBe('test-func-output');\n expect(result.call_id).toBe('call-123');\n expect(result.output).toBe('The weather in San Francisco is sunny.');\n });\n });\n});\n"],"mappings":";AAGA,oBAAoB;AACpB,oBAAqC;AAErC,4BAAwC;AAAA,IAExC,wBAAS,2BAA2B,MAAM;AACxC,8BAAS,iBAAiB,MAAM;AAC9B,0BAAG,sDAAsD,YAAY;AACnE,YAAM,mBAAmB,IAAI,kBAAI,YAAY;AAAA,QAC3C,MAAM;AAAA,QACN,SAAS;AAAA,QACT,IAAI;AAAA,MACN,CAAC;AAED,YAAM,SAAU,UAAM,+CAAwB,gBAAgB;AAE9D,gCAAO,OAAO,IAAI,EAAE,KAAK,SAAS;AAClC,gCAAO,OAAO,IAAI,EAAE,KAAK,WAAW;AACpC,gCAAO,OAAO,OAAO,EAAE,aAAa,CAAC;AACrC,YAAM,UAAU,OAAO,QAAQ,CAAC;AAChC,gCAAO,QAAQ,IAAI,EAAE,KAAK,aAAa;AACvC,gCAAQ,QAAwC,IAAI,EAAE,KAAK,4BAA4B;AAAA,IACzF,CAAC;AAED,0BAAG,gDAAgD,YAAY;AAC7D,YAAM,cAAc,IAAI,kBAAI,YAAY;AAAA,QACtC,MAAM;AAAA,QACN,SAAS;AAAA,QACT,IAAI;AAAA,MACN,CAAC;AAED,YAAM,SAAU,UAAM,+CAAwB,WAAW;AAEzD,gCAAO,OAAO,IAAI,EAAE,KAAK,SAAS;AAClC,gCAAO,OAAO,IAAI,EAAE,KAAK,MAAM;AAC/B,gCAAO,OAAO,OAAO,EAAE,aAAa,CAAC;AACrC,YAAM,UAAU,OAAO,QAAQ,CAAC;AAChC,gCAAO,QAAQ,IAAI,EAAE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,IAAI,EAAE,KAAK,YAAY;AACjD,gCAAO,OAAO,QAAQ,CAAC,EAAG,IAAI,EAAE,KAAK,aAAa;AAAA,IACpD,CAAC;AAAA,EACH,CAAC;AAED,8BAAS,uBAAuB,MAAM;AACpC,0BAAG,gDAAgD,YAAY;AAC7D,YAAM,eAAe,IAAI,kBAAI,aAAa;AAAA,QACxC,QAAQ;AAAA,QACR,MAAM;AAAA,QACN,MAAM;AAAA,QACN,IAAI;AAAA,MACN,CAAC;AAED,YAAM,SAAU,UAAM,+CAAwB,YAAY;AAE1D,gCAAO,OAAO,IAAI,EAAE,KAAK,eAAe;AACxC,gCAAO,OAAO,EAAE,EAAE,KAAK,gBAAgB;AACvC,gCAAO,OAAO,OAAO,EAAE,KAAK,UAAU;AACtC,gCAAO,OAAO,IAAI,EAAE,KAAK,aAAa;AACtC,gCAAO,OAAO,SAAS,EAAE,KAAK,+BAA+B;AAAA,IAC/D,CAAC;AAAA,EACH,CAAC;AAED,8BAAS,8BAA8B,MAAM;AAC3C,0BAAG,uDAAuD,YAAY;AACpE,YAAM,iBAAiB,IAAI,kBAAI,mBAAmB;AAAA,QAChD,QAAQ;AAAA,QACR,QAAQ;AAAA,QACR,SAAS;AAAA,QACT,IAAI;AAAA,MACN,CAAC;AAED,YAAM,SAAU,UAAM;AAAA,QACpB;AAAA,MACF;AAEA,gCAAO,OAAO,IAAI,EAAE,KAAK,sBAAsB;AAC/C,gCAAO,OAAO,EAAE,EAAE,KAAK,kBAAkB;AACzC,gCAAO,OAAO,OAAO,EAAE,KAAK,UAAU;AACtC,gCAAO,OAAO,MAAM,EAAE,KAAK,wCAAwC;AAAA,IACrE,CAAC;AAAA,EACH,CAAC;AACH,CAAC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/realtime/realtime_model.test.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { llm } from '@livekit/agents';\nimport { afterEach, describe, expect, it, vi } from 'vitest';\nimport type * as api_proto from './api_proto.js';\nimport { RealtimeModel, livekitItemToOpenAIItem } from './realtime_model.js';\n\ndescribe('livekitItemToOpenAIItem', () => {\n describe('message items', () => {\n it('should use output_text type for assistant messages', async () => {\n const assistantMessage = new llm.ChatMessage({\n role: 'assistant',\n content: 'Hello, how can I help you?',\n id: 'test-assistant-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(assistantMessage)) as api_proto.AssistantItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('assistant');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('output_text');\n expect((content as api_proto.OutputTextContent).text).toBe('Hello, how can I help you?');\n });\n\n it('should use input_text type for user messages', async () => {\n const userMessage = new llm.ChatMessage({\n role: 'user',\n content: 'What is the weather like?',\n id: 'test-user-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(userMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('user');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('input_text');\n expect((content as api_proto.InputTextContent).text).toBe('What is the weather like?');\n });\n\n it('should use input_text type for system messages', async () => {\n const systemMessage = new llm.ChatMessage({\n role: 'system',\n content: 'You are a helpful assistant.',\n id: 'test-system-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(systemMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('input_text');\n });\n\n it('should convert developer role to system role', async () => {\n const developerMessage = new llm.ChatMessage({\n role: 'developer',\n content: 'System instructions.',\n id: 'test-developer-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(developerMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n const content = result.content[0]!;\n expect(content.type).toBe('input_text');\n });\n\n it('should handle multiple content items for assistant', async () => {\n const multiContentMessage = new llm.ChatMessage({\n role: 'assistant',\n content: ['First part.', 'Second part.'],\n id: 'test-multi-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(\n multiContentMessage,\n )) as api_proto.AssistantItem;\n\n expect(result.content).toHaveLength(2);\n const content0 = result.content[0]!;\n const content1 = result.content[1]!;\n expect(content0.type).toBe('output_text');\n expect(content1.type).toBe('output_text');\n });\n\n it('should convert image content to input_image for user messages', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const userMessage = new llm.ChatMessage({\n role: 'user',\n content: [imageContent],\n id: 'test-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(userMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('user');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('input_image');\n expect((content as api_proto.InputImageContent).image_url).toBe(\n `data:image/png;base64,${base64Data}`,\n );\n });\n\n it('should ignore image content for assistant messages', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const assistantMessage = new llm.ChatMessage({\n role: 'assistant',\n content: [imageContent],\n id: 'test-assistant-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(assistantMessage)) as api_proto.AssistantItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('assistant');\n expect(result.content).toHaveLength(0);\n });\n\n it('should ignore image content for system messages', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const systemMessage = new llm.ChatMessage({\n role: 'system',\n content: [imageContent],\n id: 'test-system-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(systemMessage)) as api_proto.SystemItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n expect(result.content).toHaveLength(0);\n });\n\n it('should ignore image content for developer messages mapped to system', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const developerMessage = new llm.ChatMessage({\n role: 'developer',\n content: [imageContent],\n id: 'test-developer-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(developerMessage)) as api_proto.SystemItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n expect(result.content).toHaveLength(0);\n });\n\n it('should handle mixed text and image content', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const userMessage = new llm.ChatMessage({\n role: 'user',\n content: ['Describe this image:', imageContent],\n id: 'test-mixed-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(userMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.content).toHaveLength(2);\n expect(result.content[0]!.type).toBe('input_text');\n expect(result.content[1]!.type).toBe('input_image');\n });\n });\n\n describe('function_call items', () => {\n it('should convert function call items correctly', async () => {\n const functionCall = new llm.FunctionCall({\n callId: 'call-123',\n name: 'get_weather',\n args: '{\"location\": \"San Francisco\"}',\n id: 'test-func-call',\n });\n\n const result = (await livekitItemToOpenAIItem(functionCall)) as api_proto.FunctionCallItem;\n\n expect(result.type).toBe('function_call');\n expect(result.id).toBe('test-func-call');\n expect(result.call_id).toBe('call-123');\n expect(result.name).toBe('get_weather');\n expect(result.arguments).toBe('{\"location\": \"San Francisco\"}');\n });\n });\n\n describe('function_call_output items', () => {\n it('should convert function call output items correctly', async () => {\n const functionOutput = new llm.FunctionCallOutput({\n callId: 'call-123',\n output: 'The weather in San Francisco is sunny.',\n isError: false,\n id: 'test-func-output',\n });\n\n const result = (await livekitItemToOpenAIItem(\n functionOutput,\n )) as api_proto.FunctionCallOutputItem;\n\n expect(result.type).toBe('function_call_output');\n expect(result.id).toBe('test-func-output');\n expect(result.call_id).toBe('call-123');\n expect(result.output).toBe('The weather in San Francisco is sunny.');\n });\n });\n});\n\ndescribe('RealtimeSession.updateOptions', () => {\n afterEach(() => {\n vi.restoreAllMocks();\n });\n\n const stubTaskRuntime = () => {\n // Prevent background realtime tasks from opening network connections in unit tests.\n vi.spyOn(llm, 'RealtimeSession', 'get');\n const agentsModule = require('@livekit/agents') as typeof import('@livekit/agents'); // eslint-disable-line @typescript-eslint/no-var-requires\n vi.spyOn(agentsModule.Task, 'from').mockReturnValue({\n cancel: vi.fn(),\n done: true,\n result: Promise.resolve(undefined),\n } as unknown as import('@livekit/agents').Task<void>);\n };\n\n it('emits session.update when toolChoice changes', () => {\n stubTaskRuntime();\n\n const model = new RealtimeModel({ apiKey: 'test-key' });\n const session = model.session() as unknown as {\n updateOptions: (opts: { toolChoice?: llm.ToolChoice }) => void;\n sendEvent: (event: api_proto.ClientEvent) => void;\n };\n const sendEventSpy = vi.spyOn(session, 'sendEvent');\n sendEventSpy.mockClear();\n\n session.updateOptions({ toolChoice: 'required' });\n\n expect(sendEventSpy).toHaveBeenCalledTimes(1);\n expect(sendEventSpy).toHaveBeenCalledWith(\n expect.objectContaining({\n type: 'session.update',\n session: expect.objectContaining({\n type: 'realtime',\n tool_choice: 'required',\n }),\n }),\n );\n });\n\n it('does not emit session.update when toolChoice is unchanged', () => {\n stubTaskRuntime();\n\n const model = new RealtimeModel({ apiKey: 'test-key' });\n const session = model.session() as unknown as {\n updateOptions: (opts: { toolChoice?: llm.ToolChoice }) => void;\n sendEvent: (event: api_proto.ClientEvent) => void;\n };\n const sendEventSpy = vi.spyOn(session, 'sendEvent');\n sendEventSpy.mockClear();\n\n session.updateOptions({ toolChoice: 'auto' });\n\n expect(sendEventSpy).not.toHaveBeenCalled();\n });\n\n it('keeps toolChoice state isolated across sessions from same model', () => {\n stubTaskRuntime();\n\n const model = new RealtimeModel({ apiKey: 'test-key' });\n const sessionA = model.session() as unknown as {\n updateOptions: (opts: { toolChoice?: llm.ToolChoice }) => void;\n sendEvent: (event: api_proto.ClientEvent) => void;\n _options: { toolChoice?: llm.ToolChoice };\n };\n const sessionB = model.session() as unknown as {\n updateOptions: (opts: { toolChoice?: llm.ToolChoice }) => void;\n sendEvent: (event: api_proto.ClientEvent) => void;\n _options: { toolChoice?: llm.ToolChoice };\n };\n\n const sendEventSpyA = vi.spyOn(sessionA, 'sendEvent');\n const sendEventSpyB = vi.spyOn(sessionB, 'sendEvent');\n sendEventSpyA.mockClear();\n sendEventSpyB.mockClear();\n\n sessionA.updateOptions({ toolChoice: 'required' });\n\n expect(sessionA._options.toolChoice).toBe('required');\n expect(sessionB._options.toolChoice).toBe('auto');\n expect(sendEventSpyA).toHaveBeenCalledTimes(1);\n expect(sendEventSpyB).toHaveBeenCalledTimes(0);\n\n sessionB.updateOptions({ toolChoice: 'required' });\n expect(sendEventSpyB).toHaveBeenCalledTimes(1);\n });\n});\n"],"mappings":";AAGA,oBAAoB;AACpB,oBAAoD;AAEpD,4BAAuD;AAAA,IAEvD,wBAAS,2BAA2B,MAAM;AACxC,8BAAS,iBAAiB,MAAM;AAC9B,0BAAG,sDAAsD,YAAY;AACnE,YAAM,mBAAmB,IAAI,kBAAI,YAAY;AAAA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
|
-
import { livekitItemToOpenAIItem } from "./realtime_model.js";
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { RealtimeModel, livekitItemToOpenAIItem } from "./realtime_model.js";
|
|
4
4
|
describe("livekitItemToOpenAIItem", () => {
|
|
5
5
|
describe("message items", () => {
|
|
6
6
|
it("should use output_text type for assistant messages", async () => {
|
|
@@ -192,4 +192,62 @@ describe("livekitItemToOpenAIItem", () => {
|
|
|
192
192
|
});
|
|
193
193
|
});
|
|
194
194
|
});
|
|
195
|
+
describe("RealtimeSession.updateOptions", () => {
|
|
196
|
+
afterEach(() => {
|
|
197
|
+
vi.restoreAllMocks();
|
|
198
|
+
});
|
|
199
|
+
const stubTaskRuntime = () => {
|
|
200
|
+
vi.spyOn(llm, "RealtimeSession", "get");
|
|
201
|
+
const agentsModule = require("@livekit/agents");
|
|
202
|
+
vi.spyOn(agentsModule.Task, "from").mockReturnValue({
|
|
203
|
+
cancel: vi.fn(),
|
|
204
|
+
done: true,
|
|
205
|
+
result: Promise.resolve(void 0)
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
it("emits session.update when toolChoice changes", () => {
|
|
209
|
+
stubTaskRuntime();
|
|
210
|
+
const model = new RealtimeModel({ apiKey: "test-key" });
|
|
211
|
+
const session = model.session();
|
|
212
|
+
const sendEventSpy = vi.spyOn(session, "sendEvent");
|
|
213
|
+
sendEventSpy.mockClear();
|
|
214
|
+
session.updateOptions({ toolChoice: "required" });
|
|
215
|
+
expect(sendEventSpy).toHaveBeenCalledTimes(1);
|
|
216
|
+
expect(sendEventSpy).toHaveBeenCalledWith(
|
|
217
|
+
expect.objectContaining({
|
|
218
|
+
type: "session.update",
|
|
219
|
+
session: expect.objectContaining({
|
|
220
|
+
type: "realtime",
|
|
221
|
+
tool_choice: "required"
|
|
222
|
+
})
|
|
223
|
+
})
|
|
224
|
+
);
|
|
225
|
+
});
|
|
226
|
+
it("does not emit session.update when toolChoice is unchanged", () => {
|
|
227
|
+
stubTaskRuntime();
|
|
228
|
+
const model = new RealtimeModel({ apiKey: "test-key" });
|
|
229
|
+
const session = model.session();
|
|
230
|
+
const sendEventSpy = vi.spyOn(session, "sendEvent");
|
|
231
|
+
sendEventSpy.mockClear();
|
|
232
|
+
session.updateOptions({ toolChoice: "auto" });
|
|
233
|
+
expect(sendEventSpy).not.toHaveBeenCalled();
|
|
234
|
+
});
|
|
235
|
+
it("keeps toolChoice state isolated across sessions from same model", () => {
|
|
236
|
+
stubTaskRuntime();
|
|
237
|
+
const model = new RealtimeModel({ apiKey: "test-key" });
|
|
238
|
+
const sessionA = model.session();
|
|
239
|
+
const sessionB = model.session();
|
|
240
|
+
const sendEventSpyA = vi.spyOn(sessionA, "sendEvent");
|
|
241
|
+
const sendEventSpyB = vi.spyOn(sessionB, "sendEvent");
|
|
242
|
+
sendEventSpyA.mockClear();
|
|
243
|
+
sendEventSpyB.mockClear();
|
|
244
|
+
sessionA.updateOptions({ toolChoice: "required" });
|
|
245
|
+
expect(sessionA._options.toolChoice).toBe("required");
|
|
246
|
+
expect(sessionB._options.toolChoice).toBe("auto");
|
|
247
|
+
expect(sendEventSpyA).toHaveBeenCalledTimes(1);
|
|
248
|
+
expect(sendEventSpyB).toHaveBeenCalledTimes(0);
|
|
249
|
+
sessionB.updateOptions({ toolChoice: "required" });
|
|
250
|
+
expect(sendEventSpyB).toHaveBeenCalledTimes(1);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
195
253
|
//# sourceMappingURL=realtime_model.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/realtime/realtime_model.test.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { llm } from '@livekit/agents';\nimport { describe, expect, it } from 'vitest';\nimport type * as api_proto from './api_proto.js';\nimport { livekitItemToOpenAIItem } from './realtime_model.js';\n\ndescribe('livekitItemToOpenAIItem', () => {\n describe('message items', () => {\n it('should use output_text type for assistant messages', async () => {\n const assistantMessage = new llm.ChatMessage({\n role: 'assistant',\n content: 'Hello, how can I help you?',\n id: 'test-assistant-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(assistantMessage)) as api_proto.AssistantItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('assistant');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('output_text');\n expect((content as api_proto.OutputTextContent).text).toBe('Hello, how can I help you?');\n });\n\n it('should use input_text type for user messages', async () => {\n const userMessage = new llm.ChatMessage({\n role: 'user',\n content: 'What is the weather like?',\n id: 'test-user-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(userMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('user');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('input_text');\n expect((content as api_proto.InputTextContent).text).toBe('What is the weather like?');\n });\n\n it('should use input_text type for system messages', async () => {\n const systemMessage = new llm.ChatMessage({\n role: 'system',\n content: 'You are a helpful assistant.',\n id: 'test-system-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(systemMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('input_text');\n });\n\n it('should convert developer role to system role', async () => {\n const developerMessage = new llm.ChatMessage({\n role: 'developer',\n content: 'System instructions.',\n id: 'test-developer-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(developerMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n const content = result.content[0]!;\n expect(content.type).toBe('input_text');\n });\n\n it('should handle multiple content items for assistant', async () => {\n const multiContentMessage = new llm.ChatMessage({\n role: 'assistant',\n content: ['First part.', 'Second part.'],\n id: 'test-multi-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(\n multiContentMessage,\n )) as api_proto.AssistantItem;\n\n expect(result.content).toHaveLength(2);\n const content0 = result.content[0]!;\n const content1 = result.content[1]!;\n expect(content0.type).toBe('output_text');\n expect(content1.type).toBe('output_text');\n });\n\n it('should convert image content to input_image for user messages', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const userMessage = new llm.ChatMessage({\n role: 'user',\n content: [imageContent],\n id: 'test-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(userMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('user');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('input_image');\n expect((content as api_proto.InputImageContent).image_url).toBe(\n `data:image/png;base64,${base64Data}`,\n );\n });\n\n it('should ignore image content for assistant messages', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const assistantMessage = new llm.ChatMessage({\n role: 'assistant',\n content: [imageContent],\n id: 'test-assistant-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(assistantMessage)) as api_proto.AssistantItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('assistant');\n expect(result.content).toHaveLength(0);\n });\n\n it('should ignore image content for system messages', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const systemMessage = new llm.ChatMessage({\n role: 'system',\n content: [imageContent],\n id: 'test-system-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(systemMessage)) as api_proto.SystemItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n expect(result.content).toHaveLength(0);\n });\n\n it('should ignore image content for developer messages mapped to system', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const developerMessage = new llm.ChatMessage({\n role: 'developer',\n content: [imageContent],\n id: 'test-developer-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(developerMessage)) as api_proto.SystemItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n expect(result.content).toHaveLength(0);\n });\n\n it('should handle mixed text and image content', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const userMessage = new llm.ChatMessage({\n role: 'user',\n content: ['Describe this image:', imageContent],\n id: 'test-mixed-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(userMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.content).toHaveLength(2);\n expect(result.content[0]!.type).toBe('input_text');\n expect(result.content[1]!.type).toBe('input_image');\n });\n });\n\n describe('function_call items', () => {\n it('should convert function call items correctly', async () => {\n const functionCall = new llm.FunctionCall({\n callId: 'call-123',\n name: 'get_weather',\n args: '{\"location\": \"San Francisco\"}',\n id: 'test-func-call',\n });\n\n const result = (await livekitItemToOpenAIItem(functionCall)) as api_proto.FunctionCallItem;\n\n expect(result.type).toBe('function_call');\n expect(result.id).toBe('test-func-call');\n expect(result.call_id).toBe('call-123');\n expect(result.name).toBe('get_weather');\n expect(result.arguments).toBe('{\"location\": \"San Francisco\"}');\n });\n });\n\n describe('function_call_output items', () => {\n it('should convert function call output items correctly', async () => {\n const functionOutput = new llm.FunctionCallOutput({\n callId: 'call-123',\n output: 'The weather in San Francisco is sunny.',\n isError: false,\n id: 'test-func-output',\n });\n\n const result = (await livekitItemToOpenAIItem(\n functionOutput,\n )) as api_proto.FunctionCallOutputItem;\n\n expect(result.type).toBe('function_call_output');\n expect(result.id).toBe('test-func-output');\n expect(result.call_id).toBe('call-123');\n expect(result.output).toBe('The weather in San Francisco is sunny.');\n });\n });\n});\n"],"mappings":"AAGA,SAAS,WAAW;AACpB,SAAS,UAAU,QAAQ,UAAU;AAErC,SAAS,+BAA+B;AAExC,SAAS,2BAA2B,MAAM;AACxC,WAAS,iBAAiB,MAAM;AAC9B,OAAG,sDAAsD,YAAY;AACnE,YAAM,mBAAmB,IAAI,IAAI,YAAY;AAAA,QAC3C,MAAM;AAAA,QACN,SAAS;AAAA,QACT,IAAI;AAAA,MACN,CAAC;AAED,YAAM,SAAU,MAAM,wBAAwB,gBAAgB;AAE9D,aAAO,OAAO,IAAI,EAAE,KAAK,SAAS;AAClC,aAAO,OAAO,IAAI,EAAE,KAAK,WAAW;AACpC,aAAO,OAAO,OAAO,EAAE,aAAa,CAAC;AACrC,YAAM,UAAU,OAAO,QAAQ,CAAC;AAChC,aAAO,QAAQ,IAAI,EAAE,KAAK,aAAa;AACvC,aAAQ,QAAwC,IAAI,EAAE,KAAK,4BAA4B;AAAA,IACzF,CAAC;AAED,OAAG,gDAAgD,YAAY;AAC7D,YAAM,cAAc,IAAI,IAAI,YAAY;AAAA,QACtC,MAAM;AAAA,QACN,SAAS;AAAA,QACT,IAAI;AAAA,MACN,CAAC;AAED,YAAM,SAAU,MAAM,wBAAwB,WAAW;AAEzD,aAAO,OAAO,IAAI,EAAE,KAAK,SAAS;AAClC,aAAO,OAAO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AClC,aAAO,OAAO,OAAO,EAAE,aAAa,CAAC;AACrC,aAAO,OAAO,QAAQ,CAAC,EAAG,IAAI,EAAE,KAAK,YAAY;AACjD,aAAO,OAAO,QAAQ,CAAC,EAAG,IAAI,EAAE,KAAK,aAAa;AAAA,IACpD,CAAC;AAAA,EACH,CAAC;AAED,WAAS,uBAAuB,MAAM;AACpC,OAAG,gDAAgD,YAAY;AAC7D,YAAM,eAAe,IAAI,IAAI,aAAa;AAAA,QACxC,QAAQ;AAAA,QACR,MAAM;AAAA,QACN,MAAM;AAAA,QACN,IAAI;AAAA,MACN,CAAC;AAED,YAAM,SAAU,MAAM,wBAAwB,YAAY;AAE1D,aAAO,OAAO,IAAI,EAAE,KAAK,eAAe;AACxC,aAAO,OAAO,EAAE,EAAE,KAAK,gBAAgB;AACvC,aAAO,OAAO,OAAO,EAAE,KAAK,UAAU;AACtC,aAAO,OAAO,IAAI,EAAE,KAAK,aAAa;AACtC,aAAO,OAAO,SAAS,EAAE,KAAK,+BAA+B;AAAA,IAC/D,CAAC;AAAA,EACH,CAAC;AAED,WAAS,8BAA8B,MAAM;AAC3C,OAAG,uDAAuD,YAAY;AACpE,YAAM,iBAAiB,IAAI,IAAI,mBAAmB;AAAA,QAChD,QAAQ;AAAA,QACR,QAAQ;AAAA,QACR,SAAS;AAAA,QACT,IAAI;AAAA,MACN,CAAC;AAED,YAAM,SAAU,MAAM;AAAA,QACpB;AAAA,MACF;AAEA,aAAO,OAAO,IAAI,EAAE,KAAK,sBAAsB;AAC/C,aAAO,OAAO,EAAE,EAAE,KAAK,kBAAkB;AACzC,aAAO,OAAO,OAAO,EAAE,KAAK,UAAU;AACtC,aAAO,OAAO,MAAM,EAAE,KAAK,wCAAwC;AAAA,IACrE,CAAC;AAAA,EACH,CAAC;AACH,CAAC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/realtime/realtime_model.test.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2025 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport { llm } from '@livekit/agents';\nimport { afterEach, describe, expect, it, vi } from 'vitest';\nimport type * as api_proto from './api_proto.js';\nimport { RealtimeModel, livekitItemToOpenAIItem } from './realtime_model.js';\n\ndescribe('livekitItemToOpenAIItem', () => {\n describe('message items', () => {\n it('should use output_text type for assistant messages', async () => {\n const assistantMessage = new llm.ChatMessage({\n role: 'assistant',\n content: 'Hello, how can I help you?',\n id: 'test-assistant-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(assistantMessage)) as api_proto.AssistantItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('assistant');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('output_text');\n expect((content as api_proto.OutputTextContent).text).toBe('Hello, how can I help you?');\n });\n\n it('should use input_text type for user messages', async () => {\n const userMessage = new llm.ChatMessage({\n role: 'user',\n content: 'What is the weather like?',\n id: 'test-user-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(userMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('user');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('input_text');\n expect((content as api_proto.InputTextContent).text).toBe('What is the weather like?');\n });\n\n it('should use input_text type for system messages', async () => {\n const systemMessage = new llm.ChatMessage({\n role: 'system',\n content: 'You are a helpful assistant.',\n id: 'test-system-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(systemMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('input_text');\n });\n\n it('should convert developer role to system role', async () => {\n const developerMessage = new llm.ChatMessage({\n role: 'developer',\n content: 'System instructions.',\n id: 'test-developer-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(developerMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n const content = result.content[0]!;\n expect(content.type).toBe('input_text');\n });\n\n it('should handle multiple content items for assistant', async () => {\n const multiContentMessage = new llm.ChatMessage({\n role: 'assistant',\n content: ['First part.', 'Second part.'],\n id: 'test-multi-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(\n multiContentMessage,\n )) as api_proto.AssistantItem;\n\n expect(result.content).toHaveLength(2);\n const content0 = result.content[0]!;\n const content1 = result.content[1]!;\n expect(content0.type).toBe('output_text');\n expect(content1.type).toBe('output_text');\n });\n\n it('should convert image content to input_image for user messages', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const userMessage = new llm.ChatMessage({\n role: 'user',\n content: [imageContent],\n id: 'test-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(userMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('user');\n expect(result.content).toHaveLength(1);\n const content = result.content[0]!;\n expect(content.type).toBe('input_image');\n expect((content as api_proto.InputImageContent).image_url).toBe(\n `data:image/png;base64,${base64Data}`,\n );\n });\n\n it('should ignore image content for assistant messages', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const assistantMessage = new llm.ChatMessage({\n role: 'assistant',\n content: [imageContent],\n id: 'test-assistant-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(assistantMessage)) as api_proto.AssistantItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('assistant');\n expect(result.content).toHaveLength(0);\n });\n\n it('should ignore image content for system messages', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const systemMessage = new llm.ChatMessage({\n role: 'system',\n content: [imageContent],\n id: 'test-system-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(systemMessage)) as api_proto.SystemItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n expect(result.content).toHaveLength(0);\n });\n\n it('should ignore image content for developer messages mapped to system', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const developerMessage = new llm.ChatMessage({\n role: 'developer',\n content: [imageContent],\n id: 'test-developer-image-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(developerMessage)) as api_proto.SystemItem;\n\n expect(result.type).toBe('message');\n expect(result.role).toBe('system');\n expect(result.content).toHaveLength(0);\n });\n\n it('should handle mixed text and image content', async () => {\n const base64Data =\n 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==';\n const imageContent = llm.createImageContent({\n image: `data:image/png;base64,${base64Data}`,\n mimeType: 'image/png',\n });\n\n const userMessage = new llm.ChatMessage({\n role: 'user',\n content: ['Describe this image:', imageContent],\n id: 'test-mixed-msg',\n });\n\n const result = (await livekitItemToOpenAIItem(userMessage)) as api_proto.UserItem;\n\n expect(result.type).toBe('message');\n expect(result.content).toHaveLength(2);\n expect(result.content[0]!.type).toBe('input_text');\n expect(result.content[1]!.type).toBe('input_image');\n });\n });\n\n describe('function_call items', () => {\n it('should convert function call items correctly', async () => {\n const functionCall = new llm.FunctionCall({\n callId: 'call-123',\n name: 'get_weather',\n args: '{\"location\": \"San Francisco\"}',\n id: 'test-func-call',\n });\n\n const result = (await livekitItemToOpenAIItem(functionCall)) as api_proto.FunctionCallItem;\n\n expect(result.type).toBe('function_call');\n expect(result.id).toBe('test-func-call');\n expect(result.call_id).toBe('call-123');\n expect(result.name).toBe('get_weather');\n expect(result.arguments).toBe('{\"location\": \"San Francisco\"}');\n });\n });\n\n describe('function_call_output items', () => {\n it('should convert function call output items correctly', async () => {\n const functionOutput = new llm.FunctionCallOutput({\n callId: 'call-123',\n output: 'The weather in San Francisco is sunny.',\n isError: false,\n id: 'test-func-output',\n });\n\n const result = (await livekitItemToOpenAIItem(\n functionOutput,\n )) as api_proto.FunctionCallOutputItem;\n\n expect(result.type).toBe('function_call_output');\n expect(result.id).toBe('test-func-output');\n expect(result.call_id).toBe('call-123');\n expect(result.output).toBe('The weather in San Francisco is sunny.');\n });\n });\n});\n\ndescribe('RealtimeSession.updateOptions', () => {\n afterEach(() => {\n vi.restoreAllMocks();\n });\n\n const stubTaskRuntime = () => {\n // Prevent background realtime tasks from opening network connections in unit tests.\n vi.spyOn(llm, 'RealtimeSession', 'get');\n const agentsModule = require('@livekit/agents') as typeof import('@livekit/agents'); // eslint-disable-line @typescript-eslint/no-var-requires\n vi.spyOn(agentsModule.Task, 'from').mockReturnValue({\n cancel: vi.fn(),\n done: true,\n result: Promise.resolve(undefined),\n } as unknown as import('@livekit/agents').Task<void>);\n };\n\n it('emits session.update when toolChoice changes', () => {\n stubTaskRuntime();\n\n const model = new RealtimeModel({ apiKey: 'test-key' });\n const session = model.session() as unknown as {\n updateOptions: (opts: { toolChoice?: llm.ToolChoice }) => void;\n sendEvent: (event: api_proto.ClientEvent) => void;\n };\n const sendEventSpy = vi.spyOn(session, 'sendEvent');\n sendEventSpy.mockClear();\n\n session.updateOptions({ toolChoice: 'required' });\n\n expect(sendEventSpy).toHaveBeenCalledTimes(1);\n expect(sendEventSpy).toHaveBeenCalledWith(\n expect.objectContaining({\n type: 'session.update',\n session: expect.objectContaining({\n type: 'realtime',\n tool_choice: 'required',\n }),\n }),\n );\n });\n\n it('does not emit session.update when toolChoice is unchanged', () => {\n stubTaskRuntime();\n\n const model = new RealtimeModel({ apiKey: 'test-key' });\n const session = model.session() as unknown as {\n updateOptions: (opts: { toolChoice?: llm.ToolChoice }) => void;\n sendEvent: (event: api_proto.ClientEvent) => void;\n };\n const sendEventSpy = vi.spyOn(session, 'sendEvent');\n sendEventSpy.mockClear();\n\n session.updateOptions({ toolChoice: 'auto' });\n\n expect(sendEventSpy).not.toHaveBeenCalled();\n });\n\n it('keeps toolChoice state isolated across sessions from same model', () => {\n stubTaskRuntime();\n\n const model = new RealtimeModel({ apiKey: 'test-key' });\n const sessionA = model.session() as unknown as {\n updateOptions: (opts: { toolChoice?: llm.ToolChoice }) => void;\n sendEvent: (event: api_proto.ClientEvent) => void;\n _options: { toolChoice?: llm.ToolChoice };\n };\n const sessionB = model.session() as unknown as {\n updateOptions: (opts: { toolChoice?: llm.ToolChoice }) => void;\n sendEvent: (event: api_proto.ClientEvent) => void;\n _options: { toolChoice?: llm.ToolChoice };\n };\n\n const sendEventSpyA = vi.spyOn(sessionA, 'sendEvent');\n const sendEventSpyB = vi.spyOn(sessionB, 'sendEvent');\n sendEventSpyA.mockClear();\n sendEventSpyB.mockClear();\n\n sessionA.updateOptions({ toolChoice: 'required' });\n\n expect(sessionA._options.toolChoice).toBe('required');\n expect(sessionB._options.toolChoice).toBe('auto');\n expect(sendEventSpyA).toHaveBeenCalledTimes(1);\n expect(sendEventSpyB).toHaveBeenCalledTimes(0);\n\n sessionB.updateOptions({ toolChoice: 'required' });\n expect(sendEventSpyB).toHaveBeenCalledTimes(1);\n });\n});\n"],"mappings":"AAGA,SAAS,WAAW;AACpB,SAAS,WAAW,UAAU,QAAQ,IAAI,UAAU;AAEpD,SAAS,eAAe,+BAA+B;AAEvD,SAAS,2BAA2B,MAAM;AACxC,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.3",
|
|
4
4
|
"description": "OpenAI plugin for LiveKit Node Agents",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"require": "dist/index.cjs",
|
|
@@ -25,14 +25,14 @@
|
|
|
25
25
|
"README.md"
|
|
26
26
|
],
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@livekit/rtc-node": "^0.13.
|
|
28
|
+
"@livekit/rtc-node": "^0.13.27",
|
|
29
29
|
"@microsoft/api-extractor": "^7.35.0",
|
|
30
30
|
"@types/ws": "^8.5.10",
|
|
31
31
|
"tsup": "^8.3.5",
|
|
32
32
|
"typescript": "^5.0.0",
|
|
33
|
-
"@livekit/agents": "1.3.
|
|
34
|
-
"@livekit/agents-plugin-silero": "1.3.
|
|
35
|
-
"@livekit/agents-plugins-test": "1.3.
|
|
33
|
+
"@livekit/agents": "1.3.3",
|
|
34
|
+
"@livekit/agents-plugin-silero": "1.3.3",
|
|
35
|
+
"@livekit/agents-plugins-test": "1.3.3"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@livekit/mutex": "^1.1.1",
|
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
"ws": "^8.18.0"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"@livekit/rtc-node": "^0.13.
|
|
43
|
+
"@livekit/rtc-node": "^0.13.27",
|
|
44
44
|
"zod": "^3.25.76 || ^4.1.8",
|
|
45
|
-
"@livekit/agents": "1.3.
|
|
45
|
+
"@livekit/agents": "1.3.3"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "tsup --onSuccess \"pnpm build:types\"",
|
package/src/llm.ts
CHANGED
package/src/models.ts
CHANGED
|
@@ -100,22 +100,12 @@ export type PerplexityChatModels =
|
|
|
100
100
|
| 'llama-3.1-70b-instruct';
|
|
101
101
|
|
|
102
102
|
export type GroqChatModels =
|
|
103
|
-
| 'llama-3.1-405b-reasoning'
|
|
104
103
|
| 'llama-3.1-8b-instant'
|
|
105
104
|
| 'llama-3.3-70b-versatile'
|
|
106
|
-
| '
|
|
107
|
-
| '
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
| 'llama3-8b-8192'
|
|
111
|
-
| 'mixtral-8x7b-32768'
|
|
112
|
-
| 'gemma-7b-it'
|
|
113
|
-
| 'gemma2-9b-it';
|
|
114
|
-
|
|
115
|
-
export type GroqAudioModels =
|
|
116
|
-
| 'whisper-large-v3'
|
|
117
|
-
| 'distil-whisper-large-v3-en'
|
|
118
|
-
| 'whisper-large-v3-turbo';
|
|
105
|
+
| 'openai/gpt-oss-120b'
|
|
106
|
+
| 'openai/gpt-oss-20b';
|
|
107
|
+
|
|
108
|
+
export type GroqAudioModels = 'whisper-large-v3' | 'whisper-large-v3-turbo';
|
|
119
109
|
|
|
120
110
|
export type DeepSeekChatModels = 'deepseek-coder' | 'deepseek-chat';
|
|
121
111
|
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
//
|
|
3
3
|
// SPDX-License-Identifier: Apache-2.0
|
|
4
4
|
import { llm } from '@livekit/agents';
|
|
5
|
-
import { describe, expect, it } from 'vitest';
|
|
5
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
6
6
|
import type * as api_proto from './api_proto.js';
|
|
7
|
-
import { livekitItemToOpenAIItem } from './realtime_model.js';
|
|
7
|
+
import { RealtimeModel, livekitItemToOpenAIItem } from './realtime_model.js';
|
|
8
8
|
|
|
9
9
|
describe('livekitItemToOpenAIItem', () => {
|
|
10
10
|
describe('message items', () => {
|
|
@@ -242,3 +242,92 @@ describe('livekitItemToOpenAIItem', () => {
|
|
|
242
242
|
});
|
|
243
243
|
});
|
|
244
244
|
});
|
|
245
|
+
|
|
246
|
+
describe('RealtimeSession.updateOptions', () => {
|
|
247
|
+
afterEach(() => {
|
|
248
|
+
vi.restoreAllMocks();
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
const stubTaskRuntime = () => {
|
|
252
|
+
// Prevent background realtime tasks from opening network connections in unit tests.
|
|
253
|
+
vi.spyOn(llm, 'RealtimeSession', 'get');
|
|
254
|
+
const agentsModule = require('@livekit/agents') as typeof import('@livekit/agents'); // eslint-disable-line @typescript-eslint/no-var-requires
|
|
255
|
+
vi.spyOn(agentsModule.Task, 'from').mockReturnValue({
|
|
256
|
+
cancel: vi.fn(),
|
|
257
|
+
done: true,
|
|
258
|
+
result: Promise.resolve(undefined),
|
|
259
|
+
} as unknown as import('@livekit/agents').Task<void>);
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
it('emits session.update when toolChoice changes', () => {
|
|
263
|
+
stubTaskRuntime();
|
|
264
|
+
|
|
265
|
+
const model = new RealtimeModel({ apiKey: 'test-key' });
|
|
266
|
+
const session = model.session() as unknown as {
|
|
267
|
+
updateOptions: (opts: { toolChoice?: llm.ToolChoice }) => void;
|
|
268
|
+
sendEvent: (event: api_proto.ClientEvent) => void;
|
|
269
|
+
};
|
|
270
|
+
const sendEventSpy = vi.spyOn(session, 'sendEvent');
|
|
271
|
+
sendEventSpy.mockClear();
|
|
272
|
+
|
|
273
|
+
session.updateOptions({ toolChoice: 'required' });
|
|
274
|
+
|
|
275
|
+
expect(sendEventSpy).toHaveBeenCalledTimes(1);
|
|
276
|
+
expect(sendEventSpy).toHaveBeenCalledWith(
|
|
277
|
+
expect.objectContaining({
|
|
278
|
+
type: 'session.update',
|
|
279
|
+
session: expect.objectContaining({
|
|
280
|
+
type: 'realtime',
|
|
281
|
+
tool_choice: 'required',
|
|
282
|
+
}),
|
|
283
|
+
}),
|
|
284
|
+
);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it('does not emit session.update when toolChoice is unchanged', () => {
|
|
288
|
+
stubTaskRuntime();
|
|
289
|
+
|
|
290
|
+
const model = new RealtimeModel({ apiKey: 'test-key' });
|
|
291
|
+
const session = model.session() as unknown as {
|
|
292
|
+
updateOptions: (opts: { toolChoice?: llm.ToolChoice }) => void;
|
|
293
|
+
sendEvent: (event: api_proto.ClientEvent) => void;
|
|
294
|
+
};
|
|
295
|
+
const sendEventSpy = vi.spyOn(session, 'sendEvent');
|
|
296
|
+
sendEventSpy.mockClear();
|
|
297
|
+
|
|
298
|
+
session.updateOptions({ toolChoice: 'auto' });
|
|
299
|
+
|
|
300
|
+
expect(sendEventSpy).not.toHaveBeenCalled();
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it('keeps toolChoice state isolated across sessions from same model', () => {
|
|
304
|
+
stubTaskRuntime();
|
|
305
|
+
|
|
306
|
+
const model = new RealtimeModel({ apiKey: 'test-key' });
|
|
307
|
+
const sessionA = model.session() as unknown as {
|
|
308
|
+
updateOptions: (opts: { toolChoice?: llm.ToolChoice }) => void;
|
|
309
|
+
sendEvent: (event: api_proto.ClientEvent) => void;
|
|
310
|
+
_options: { toolChoice?: llm.ToolChoice };
|
|
311
|
+
};
|
|
312
|
+
const sessionB = model.session() as unknown as {
|
|
313
|
+
updateOptions: (opts: { toolChoice?: llm.ToolChoice }) => void;
|
|
314
|
+
sendEvent: (event: api_proto.ClientEvent) => void;
|
|
315
|
+
_options: { toolChoice?: llm.ToolChoice };
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const sendEventSpyA = vi.spyOn(sessionA, 'sendEvent');
|
|
319
|
+
const sendEventSpyB = vi.spyOn(sessionB, 'sendEvent');
|
|
320
|
+
sendEventSpyA.mockClear();
|
|
321
|
+
sendEventSpyB.mockClear();
|
|
322
|
+
|
|
323
|
+
sessionA.updateOptions({ toolChoice: 'required' });
|
|
324
|
+
|
|
325
|
+
expect(sessionA._options.toolChoice).toBe('required');
|
|
326
|
+
expect(sessionB._options.toolChoice).toBe('auto');
|
|
327
|
+
expect(sendEventSpyA).toHaveBeenCalledTimes(1);
|
|
328
|
+
expect(sendEventSpyB).toHaveBeenCalledTimes(0);
|
|
329
|
+
|
|
330
|
+
sessionB.updateOptions({ toolChoice: 'required' });
|
|
331
|
+
expect(sendEventSpyB).toHaveBeenCalledTimes(1);
|
|
332
|
+
});
|
|
333
|
+
});
|