@livekit/agents-plugin-openai 0.0.0-next-20260624041820
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/LICENSE +201 -0
- package/README.md +18 -0
- package/dist/index.cjs +74 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/llm.cjs +382 -0
- package/dist/llm.cjs.map +1 -0
- package/dist/llm.d.cts +230 -0
- package/dist/llm.d.ts +230 -0
- package/dist/llm.d.ts.map +1 -0
- package/dist/llm.js +357 -0
- package/dist/llm.js.map +1 -0
- package/dist/llm.test.cjs +36 -0
- package/dist/llm.test.cjs.map +1 -0
- package/dist/llm.test.d.cts +2 -0
- package/dist/llm.test.d.ts +2 -0
- package/dist/llm.test.d.ts.map +1 -0
- package/dist/llm.test.js +35 -0
- package/dist/llm.test.js.map +1 -0
- package/dist/models.cjs +50 -0
- package/dist/models.cjs.map +1 -0
- package/dist/models.d.cts +26 -0
- package/dist/models.d.ts +26 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +25 -0
- package/dist/models.js.map +1 -0
- package/dist/realtime/api_proto.cjs +41 -0
- package/dist/realtime/api_proto.cjs.map +1 -0
- package/dist/realtime/api_proto.d.cts +501 -0
- package/dist/realtime/api_proto.d.ts +501 -0
- package/dist/realtime/api_proto.d.ts.map +1 -0
- package/dist/realtime/api_proto.js +13 -0
- package/dist/realtime/api_proto.js.map +1 -0
- package/dist/realtime/index.cjs +25 -0
- package/dist/realtime/index.cjs.map +1 -0
- package/dist/realtime/index.d.cts +3 -0
- package/dist/realtime/index.d.ts +3 -0
- package/dist/realtime/index.d.ts.map +1 -0
- package/dist/realtime/index.js +3 -0
- package/dist/realtime/index.js.map +1 -0
- package/dist/realtime/realtime_model.cjs +1658 -0
- package/dist/realtime/realtime_model.cjs.map +1 -0
- package/dist/realtime/realtime_model.d.cts +202 -0
- package/dist/realtime/realtime_model.d.ts +202 -0
- package/dist/realtime/realtime_model.d.ts.map +1 -0
- package/dist/realtime/realtime_model.js +1637 -0
- package/dist/realtime/realtime_model.js.map +1 -0
- package/dist/realtime/realtime_model.test.cjs +596 -0
- package/dist/realtime/realtime_model.test.cjs.map +1 -0
- package/dist/realtime/realtime_model.test.d.cts +2 -0
- package/dist/realtime/realtime_model.test.d.ts +2 -0
- package/dist/realtime/realtime_model.test.d.ts.map +1 -0
- package/dist/realtime/realtime_model.test.js +600 -0
- package/dist/realtime/realtime_model.test.js.map +1 -0
- package/dist/responses/index.cjs +23 -0
- package/dist/responses/index.cjs.map +1 -0
- package/dist/responses/index.d.cts +2 -0
- package/dist/responses/index.d.ts +2 -0
- package/dist/responses/index.d.ts.map +1 -0
- package/dist/responses/index.js +2 -0
- package/dist/responses/index.js.map +1 -0
- package/dist/responses/llm.cjs +353 -0
- package/dist/responses/llm.cjs.map +1 -0
- package/dist/responses/llm.d.cts +52 -0
- package/dist/responses/llm.d.ts +52 -0
- package/dist/responses/llm.d.ts.map +1 -0
- package/dist/responses/llm.js +327 -0
- package/dist/responses/llm.js.map +1 -0
- package/dist/responses/llm.test.cjs +37 -0
- package/dist/responses/llm.test.cjs.map +1 -0
- package/dist/responses/llm.test.d.cts +2 -0
- package/dist/responses/llm.test.d.ts +2 -0
- package/dist/responses/llm.test.d.ts.map +1 -0
- package/dist/responses/llm.test.js +36 -0
- package/dist/responses/llm.test.js.map +1 -0
- package/dist/stt.cjs +627 -0
- package/dist/stt.cjs.map +1 -0
- package/dist/stt.d.cts +105 -0
- package/dist/stt.d.ts +105 -0
- package/dist/stt.d.ts.map +1 -0
- package/dist/stt.js +605 -0
- package/dist/stt.js.map +1 -0
- package/dist/stt.test.cjs +132 -0
- package/dist/stt.test.cjs.map +1 -0
- package/dist/stt.test.d.cts +2 -0
- package/dist/stt.test.d.ts +2 -0
- package/dist/stt.test.d.ts.map +1 -0
- package/dist/stt.test.js +131 -0
- package/dist/stt.test.js.map +1 -0
- package/dist/tool_utils.cjs +53 -0
- package/dist/tool_utils.cjs.map +1 -0
- package/dist/tool_utils.d.cts +4 -0
- package/dist/tool_utils.d.ts +4 -0
- package/dist/tool_utils.d.ts.map +1 -0
- package/dist/tool_utils.js +29 -0
- package/dist/tool_utils.js.map +1 -0
- package/dist/tool_utils.test.cjs +77 -0
- package/dist/tool_utils.test.cjs.map +1 -0
- package/dist/tool_utils.test.d.cts +2 -0
- package/dist/tool_utils.test.d.ts +2 -0
- package/dist/tool_utils.test.d.ts.map +1 -0
- package/dist/tool_utils.test.js +76 -0
- package/dist/tool_utils.test.js.map +1 -0
- package/dist/tools.cjs +117 -0
- package/dist/tools.cjs.map +1 -0
- package/dist/tools.d.cts +88 -0
- package/dist/tools.d.ts +88 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +90 -0
- package/dist/tools.js.map +1 -0
- package/dist/tts.cjs +141 -0
- package/dist/tts.cjs.map +1 -0
- package/dist/tts.d.cts +42 -0
- package/dist/tts.d.ts +42 -0
- package/dist/tts.d.ts.map +1 -0
- package/dist/tts.js +116 -0
- package/dist/tts.js.map +1 -0
- package/dist/tts.test.cjs +17 -0
- package/dist/tts.test.cjs.map +1 -0
- package/dist/tts.test.d.cts +2 -0
- package/dist/tts.test.d.ts +2 -0
- package/dist/tts.test.d.ts.map +1 -0
- package/dist/tts.test.js +16 -0
- package/dist/tts.test.js.map +1 -0
- package/dist/ws/index.cjs +29 -0
- package/dist/ws/index.cjs.map +1 -0
- package/dist/ws/index.d.cts +3 -0
- package/dist/ws/index.d.ts +3 -0
- package/dist/ws/index.d.ts.map +1 -0
- package/dist/ws/index.js +5 -0
- package/dist/ws/index.js.map +1 -0
- package/dist/ws/llm.cjs +523 -0
- package/dist/ws/llm.cjs.map +1 -0
- package/dist/ws/llm.d.cts +96 -0
- package/dist/ws/llm.d.ts +96 -0
- package/dist/ws/llm.d.ts.map +1 -0
- package/dist/ws/llm.js +505 -0
- package/dist/ws/llm.js.map +1 -0
- package/dist/ws/llm.test.cjs +69 -0
- package/dist/ws/llm.test.cjs.map +1 -0
- package/dist/ws/llm.test.d.cts +2 -0
- package/dist/ws/llm.test.d.ts +2 -0
- package/dist/ws/llm.test.d.ts.map +1 -0
- package/dist/ws/llm.test.js +68 -0
- package/dist/ws/llm.test.js.map +1 -0
- package/dist/ws/types.cjs +129 -0
- package/dist/ws/types.cjs.map +1 -0
- package/dist/ws/types.d.cts +172 -0
- package/dist/ws/types.d.ts +172 -0
- package/dist/ws/types.d.ts.map +1 -0
- package/dist/ws/types.js +96 -0
- package/dist/ws/types.js.map +1 -0
- package/package.json +56 -0
- package/src/index.ts +25 -0
- package/src/llm.test.ts +38 -0
- package/src/llm.ts +553 -0
- package/src/models.ts +235 -0
- package/src/realtime/api_proto.ts +714 -0
- package/src/realtime/index.ts +5 -0
- package/src/realtime/realtime_model.test.ts +793 -0
- package/src/realtime/realtime_model.ts +2116 -0
- package/src/responses/index.ts +4 -0
- package/src/responses/llm.test.ts +39 -0
- package/src/responses/llm.ts +452 -0
- package/src/stt.test.ts +167 -0
- package/src/stt.ts +793 -0
- package/src/tool_utils.test.ts +84 -0
- package/src/tool_utils.ts +46 -0
- package/src/tools.ts +166 -0
- package/src/tts.test.ts +19 -0
- package/src/tts.ts +158 -0
- package/src/ws/index.ts +17 -0
- package/src/ws/llm.test.ts +83 -0
- package/src/ws/llm.ts +710 -0
- package/src/ws/types.ts +132 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { llm } from '@livekit/agents';
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
import { toResponsesTools } from './tool_utils.js';
|
|
8
|
+
import { CodeInterpreter, FileSearch, WebSearch } from './tools.js';
|
|
9
|
+
|
|
10
|
+
describe('toResponsesTools', () => {
|
|
11
|
+
it('serializes function tools', () => {
|
|
12
|
+
const fn = llm.tool({
|
|
13
|
+
name: 'lookup_weather',
|
|
14
|
+
description: 'Look up weather',
|
|
15
|
+
parameters: z.object({ city: z.string() }),
|
|
16
|
+
execute: async () => 'sunny',
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
expect(toResponsesTools(new llm.ToolContext([fn]), true)).toEqual([
|
|
20
|
+
{
|
|
21
|
+
type: 'function',
|
|
22
|
+
name: 'lookup_weather',
|
|
23
|
+
description: 'Look up weather',
|
|
24
|
+
parameters: {
|
|
25
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
26
|
+
type: 'object',
|
|
27
|
+
properties: { city: { type: 'string' } },
|
|
28
|
+
required: ['city'],
|
|
29
|
+
additionalProperties: false,
|
|
30
|
+
},
|
|
31
|
+
strict: true,
|
|
32
|
+
},
|
|
33
|
+
]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('serializes OpenAI provider tools', () => {
|
|
37
|
+
const tools = toResponsesTools(
|
|
38
|
+
new llm.ToolContext([
|
|
39
|
+
new WebSearch({
|
|
40
|
+
filters: { allowed_domains: ['docs.livekit.io'] },
|
|
41
|
+
searchContextSize: 'low',
|
|
42
|
+
userLocation: { type: 'approximate', country: 'US' },
|
|
43
|
+
}),
|
|
44
|
+
new FileSearch({
|
|
45
|
+
vectorStoreIds: ['vs_123'],
|
|
46
|
+
maxNumResults: 3,
|
|
47
|
+
rankingOptions: { ranker: 'auto' },
|
|
48
|
+
}),
|
|
49
|
+
new CodeInterpreter({ container: { type: 'auto', file_ids: ['file_123'] } }),
|
|
50
|
+
]),
|
|
51
|
+
false,
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
expect(tools).toEqual([
|
|
55
|
+
{
|
|
56
|
+
type: 'web_search',
|
|
57
|
+
search_context_size: 'low',
|
|
58
|
+
filters: { allowed_domains: ['docs.livekit.io'] },
|
|
59
|
+
user_location: { type: 'approximate', country: 'US' },
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
type: 'file_search',
|
|
63
|
+
vector_store_ids: ['vs_123'],
|
|
64
|
+
max_num_results: 3,
|
|
65
|
+
ranking_options: { ranker: 'auto' },
|
|
66
|
+
},
|
|
67
|
+
{ type: 'code_interpreter', container: { type: 'auto', file_ids: ['file_123'] } },
|
|
68
|
+
]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('omits the code interpreter container when unset', () => {
|
|
72
|
+
expect(toResponsesTools(new llm.ToolContext([new CodeInterpreter()]), false)).toEqual([
|
|
73
|
+
{ type: 'code_interpreter' },
|
|
74
|
+
]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('ignores non-OpenAI provider tools', () => {
|
|
78
|
+
class OtherProviderTool extends llm.ProviderTool {}
|
|
79
|
+
|
|
80
|
+
expect(
|
|
81
|
+
toResponsesTools(new llm.ToolContext([new OtherProviderTool({ id: 'other' })]), false),
|
|
82
|
+
).toBeUndefined();
|
|
83
|
+
});
|
|
84
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { llm } from '@livekit/agents';
|
|
5
|
+
import type OpenAI from 'openai';
|
|
6
|
+
import { OpenAITool } from './tools.js';
|
|
7
|
+
|
|
8
|
+
export function toResponsesTools(
|
|
9
|
+
toolCtx: llm.ToolContext,
|
|
10
|
+
strictToolSchema: boolean,
|
|
11
|
+
): OpenAI.Responses.Tool[] | undefined {
|
|
12
|
+
// Function tools are emitted first, sorted by name for deterministic payloads; provider
|
|
13
|
+
// tools follow in registration order.
|
|
14
|
+
const functionTools = llm.sortedToolEntries(toolCtx).map(([name, tool]) => {
|
|
15
|
+
const oaiParams = {
|
|
16
|
+
type: 'function' as const,
|
|
17
|
+
name,
|
|
18
|
+
description: tool.description,
|
|
19
|
+
parameters: llm.toJsonSchema(
|
|
20
|
+
tool.parameters,
|
|
21
|
+
true,
|
|
22
|
+
strictToolSchema,
|
|
23
|
+
) as unknown as OpenAI.Responses.FunctionTool['parameters'],
|
|
24
|
+
} as OpenAI.Responses.FunctionTool;
|
|
25
|
+
|
|
26
|
+
if (strictToolSchema) {
|
|
27
|
+
oaiParams.strict = true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return oaiParams;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const providerTools = toolCtx
|
|
34
|
+
.flatten()
|
|
35
|
+
.filter((tool) => !llm.isFunctionTool(tool))
|
|
36
|
+
.map((tool) =>
|
|
37
|
+
tool instanceof OpenAITool
|
|
38
|
+
? (tool.toToolConfig() as unknown as OpenAI.Responses.Tool)
|
|
39
|
+
: undefined,
|
|
40
|
+
)
|
|
41
|
+
.filter((tool): tool is OpenAI.Responses.Tool => tool !== undefined);
|
|
42
|
+
|
|
43
|
+
const tools = [...functionTools, ...providerTools];
|
|
44
|
+
|
|
45
|
+
return tools.length > 0 ? tools : undefined;
|
|
46
|
+
}
|
package/src/tools.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { llm } from '@livekit/agents';
|
|
5
|
+
import type OpenAI from 'openai';
|
|
6
|
+
|
|
7
|
+
/** Base class for OpenAI Responses API provider tools. */
|
|
8
|
+
export abstract class OpenAITool extends llm.ProviderTool {
|
|
9
|
+
/** Convert this provider tool to the OpenAI Responses API tool configuration. */
|
|
10
|
+
abstract toToolConfig(): Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* High-level guidance for the amount of context window space to use for web search.
|
|
15
|
+
* OpenAI defaults this to `medium`.
|
|
16
|
+
*/
|
|
17
|
+
export type WebSearchContextSize = 'low' | 'medium' | 'high';
|
|
18
|
+
|
|
19
|
+
/** Options for the OpenAI web search tool. */
|
|
20
|
+
export interface WebSearchOptions {
|
|
21
|
+
/**
|
|
22
|
+
* Filters for the search, such as allowed domains. If not provided, all domains are allowed.
|
|
23
|
+
*/
|
|
24
|
+
filters?: OpenAI.Responses.WebSearchTool['filters'];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Amount of context window space to use for the search. Defaults to `medium`.
|
|
28
|
+
*/
|
|
29
|
+
searchContextSize?: WebSearchContextSize | null;
|
|
30
|
+
|
|
31
|
+
/** Approximate location of the user, such as city, region, country, or timezone. */
|
|
32
|
+
userLocation?: OpenAI.Responses.WebSearchTool['user_location'];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Search the Internet for sources related to the prompt.
|
|
37
|
+
*
|
|
38
|
+
* @see https://platform.openai.com/docs/guides/tools-web-search
|
|
39
|
+
*/
|
|
40
|
+
export class WebSearch extends OpenAITool {
|
|
41
|
+
/** Filters for the search, such as allowed domains. */
|
|
42
|
+
readonly filters: OpenAI.Responses.WebSearchTool['filters'] | undefined;
|
|
43
|
+
|
|
44
|
+
/** Amount of context window space to use for the search. */
|
|
45
|
+
readonly searchContextSize: WebSearchContextSize | null;
|
|
46
|
+
|
|
47
|
+
/** Approximate location of the user. */
|
|
48
|
+
readonly userLocation: OpenAI.Responses.WebSearchTool['user_location'] | undefined;
|
|
49
|
+
|
|
50
|
+
constructor({ filters, searchContextSize = 'medium', userLocation }: WebSearchOptions = {}) {
|
|
51
|
+
super({ id: 'openai_web_search' });
|
|
52
|
+
this.filters = filters;
|
|
53
|
+
this.searchContextSize = searchContextSize;
|
|
54
|
+
this.userLocation = userLocation;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
toToolConfig(): Record<string, unknown> {
|
|
58
|
+
const result: Record<string, unknown> = {
|
|
59
|
+
type: 'web_search',
|
|
60
|
+
search_context_size: this.searchContextSize,
|
|
61
|
+
};
|
|
62
|
+
if (this.userLocation !== undefined) {
|
|
63
|
+
result.user_location = this.userLocation;
|
|
64
|
+
}
|
|
65
|
+
if (this.filters !== undefined) {
|
|
66
|
+
result.filters = this.filters;
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Options for the OpenAI file search tool. */
|
|
73
|
+
export interface FileSearchOptions {
|
|
74
|
+
/** IDs of the vector stores to search. */
|
|
75
|
+
vectorStoreIds?: string[];
|
|
76
|
+
|
|
77
|
+
/** Filter to apply to file search results. */
|
|
78
|
+
filters?: OpenAI.Responses.FileSearchTool['filters'];
|
|
79
|
+
|
|
80
|
+
/** Maximum number of results to return. This should be between 1 and 50 inclusive. */
|
|
81
|
+
maxNumResults?: number;
|
|
82
|
+
|
|
83
|
+
/** Ranking options for search, including ranker and score threshold. */
|
|
84
|
+
rankingOptions?: OpenAI.Responses.FileSearchTool.RankingOptions;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Search for relevant content from uploaded files.
|
|
89
|
+
*
|
|
90
|
+
* @see https://platform.openai.com/docs/guides/tools-file-search
|
|
91
|
+
*/
|
|
92
|
+
export class FileSearch extends OpenAITool {
|
|
93
|
+
/** IDs of the vector stores to search. */
|
|
94
|
+
readonly vectorStoreIds: string[];
|
|
95
|
+
|
|
96
|
+
/** Filter to apply to file search results. */
|
|
97
|
+
readonly filters: OpenAI.Responses.FileSearchTool['filters'] | undefined;
|
|
98
|
+
|
|
99
|
+
/** Maximum number of results to return. */
|
|
100
|
+
readonly maxNumResults: number | undefined;
|
|
101
|
+
|
|
102
|
+
/** Ranking options for search. */
|
|
103
|
+
readonly rankingOptions: OpenAI.Responses.FileSearchTool.RankingOptions | undefined;
|
|
104
|
+
|
|
105
|
+
constructor({
|
|
106
|
+
vectorStoreIds = [],
|
|
107
|
+
filters,
|
|
108
|
+
maxNumResults,
|
|
109
|
+
rankingOptions,
|
|
110
|
+
}: FileSearchOptions = {}) {
|
|
111
|
+
super({ id: 'openai_file_search' });
|
|
112
|
+
this.vectorStoreIds = [...vectorStoreIds];
|
|
113
|
+
this.filters = filters;
|
|
114
|
+
this.maxNumResults = maxNumResults;
|
|
115
|
+
this.rankingOptions = rankingOptions;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
toToolConfig(): Record<string, unknown> {
|
|
119
|
+
const result: Record<string, unknown> = {
|
|
120
|
+
type: 'file_search',
|
|
121
|
+
vector_store_ids: this.vectorStoreIds,
|
|
122
|
+
};
|
|
123
|
+
if (this.filters !== undefined) {
|
|
124
|
+
result.filters = this.filters;
|
|
125
|
+
}
|
|
126
|
+
if (this.maxNumResults !== undefined) {
|
|
127
|
+
result.max_num_results = this.maxNumResults;
|
|
128
|
+
}
|
|
129
|
+
if (this.rankingOptions !== undefined) {
|
|
130
|
+
result.ranking_options = this.rankingOptions;
|
|
131
|
+
}
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Options for the OpenAI code interpreter tool. */
|
|
137
|
+
export interface CodeInterpreterOptions {
|
|
138
|
+
/**
|
|
139
|
+
* Code interpreter container. Can be a container ID or an object that specifies uploaded file IDs
|
|
140
|
+
* to make available to the code.
|
|
141
|
+
*/
|
|
142
|
+
container?: OpenAI.Responses.Tool.CodeInterpreter['container'] | null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Run Python code to help generate a response to a prompt.
|
|
147
|
+
*
|
|
148
|
+
* @see https://platform.openai.com/docs/guides/tools-code-interpreter
|
|
149
|
+
*/
|
|
150
|
+
export class CodeInterpreter extends OpenAITool {
|
|
151
|
+
/** Code interpreter container ID or configuration. */
|
|
152
|
+
readonly container: OpenAI.Responses.Tool.CodeInterpreter['container'] | null;
|
|
153
|
+
|
|
154
|
+
constructor({ container = null }: CodeInterpreterOptions = {}) {
|
|
155
|
+
super({ id: 'openai_code_interpreter' });
|
|
156
|
+
this.container = container;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
toToolConfig(): Record<string, unknown> {
|
|
160
|
+
const result: Record<string, unknown> = { type: 'code_interpreter' };
|
|
161
|
+
if (this.container !== null) {
|
|
162
|
+
result.container = this.container;
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
}
|
package/src/tts.test.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { tts } from '@livekit/agents-plugins-test';
|
|
5
|
+
import { describe, it } from 'vitest';
|
|
6
|
+
import { STT } from './stt.js';
|
|
7
|
+
import { TTS } from './tts.js';
|
|
8
|
+
|
|
9
|
+
const hasOpenAIApiKey = Boolean(process.env.OPENAI_API_KEY);
|
|
10
|
+
|
|
11
|
+
if (hasOpenAIApiKey) {
|
|
12
|
+
describe('OpenAI', async () => {
|
|
13
|
+
await tts(new TTS(), new STT(), { streaming: false });
|
|
14
|
+
});
|
|
15
|
+
} else {
|
|
16
|
+
describe('OpenAI', () => {
|
|
17
|
+
it.skip('requires OPENAI_API_KEY', () => {});
|
|
18
|
+
});
|
|
19
|
+
}
|
package/src/tts.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { type APIConnectOptions, AudioByteStream, shortuuid, tts } from '@livekit/agents';
|
|
5
|
+
import type { AudioFrame } from '@livekit/rtc-node';
|
|
6
|
+
import { OpenAI } from 'openai';
|
|
7
|
+
import type { TTSModels, TTSVoices } from './models.js';
|
|
8
|
+
|
|
9
|
+
const OPENAI_TTS_SAMPLE_RATE = 24000;
|
|
10
|
+
const OPENAI_TTS_CHANNELS = 1;
|
|
11
|
+
|
|
12
|
+
export interface TTSOptions {
|
|
13
|
+
model: TTSModels | string;
|
|
14
|
+
voice: TTSVoices;
|
|
15
|
+
speed: number;
|
|
16
|
+
instructions?: string;
|
|
17
|
+
baseURL?: string;
|
|
18
|
+
client?: OpenAI;
|
|
19
|
+
apiKey?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const defaultTTSOptions: TTSOptions = {
|
|
23
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
24
|
+
model: 'tts-1',
|
|
25
|
+
voice: 'alloy',
|
|
26
|
+
speed: 1,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export class TTS extends tts.TTS {
|
|
30
|
+
#opts: TTSOptions;
|
|
31
|
+
#client: OpenAI;
|
|
32
|
+
label = 'openai.TTS';
|
|
33
|
+
private abortController = new AbortController();
|
|
34
|
+
|
|
35
|
+
get model(): string {
|
|
36
|
+
return this.#opts.model;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get provider(): string {
|
|
40
|
+
try {
|
|
41
|
+
const url = new URL(this.#client.baseURL);
|
|
42
|
+
return url.host;
|
|
43
|
+
} catch {
|
|
44
|
+
return 'api.openai.com';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create a new instance of OpenAI TTS.
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* `apiKey` must be set to your OpenAI API key, either using the argument or by setting the
|
|
53
|
+
* `OPENAI_API_KEY` environment variable.
|
|
54
|
+
*/
|
|
55
|
+
constructor(opts: Partial<TTSOptions> = defaultTTSOptions) {
|
|
56
|
+
super(OPENAI_TTS_SAMPLE_RATE, OPENAI_TTS_CHANNELS, { streaming: false });
|
|
57
|
+
|
|
58
|
+
this.#opts = { ...defaultTTSOptions, ...opts };
|
|
59
|
+
if (this.#opts.apiKey === undefined && !this.#opts.client) {
|
|
60
|
+
throw new Error('OpenAI API key is required, whether as an argument or as $OPENAI_API_KEY');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this.#client =
|
|
64
|
+
this.#opts.client ||
|
|
65
|
+
new OpenAI({
|
|
66
|
+
baseURL: this.#opts.baseURL,
|
|
67
|
+
apiKey: this.#opts.apiKey,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
updateOptions(opts: { model?: TTSModels | string; voice?: TTSVoices; speed?: number }) {
|
|
72
|
+
this.#opts = { ...this.#opts, ...opts };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
synthesize(
|
|
76
|
+
text: string,
|
|
77
|
+
connOptions?: APIConnectOptions,
|
|
78
|
+
abortSignal?: AbortSignal,
|
|
79
|
+
): ChunkedStream {
|
|
80
|
+
const signal = abortSignal
|
|
81
|
+
? AbortSignal.any([abortSignal, this.abortController.signal])
|
|
82
|
+
: this.abortController.signal;
|
|
83
|
+
return new ChunkedStream(
|
|
84
|
+
this,
|
|
85
|
+
text,
|
|
86
|
+
this.#client.audio.speech.create(
|
|
87
|
+
{
|
|
88
|
+
input: text,
|
|
89
|
+
model: this.#opts.model,
|
|
90
|
+
voice: this.#opts.voice,
|
|
91
|
+
instructions: this.#opts.instructions,
|
|
92
|
+
response_format: 'pcm',
|
|
93
|
+
speed: this.#opts.speed,
|
|
94
|
+
},
|
|
95
|
+
{ signal },
|
|
96
|
+
),
|
|
97
|
+
connOptions,
|
|
98
|
+
signal,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
stream(): tts.SynthesizeStream {
|
|
103
|
+
throw new Error('Streaming is not supported on OpenAI TTS');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async close(): Promise<void> {
|
|
107
|
+
this.abortController.abort();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export class ChunkedStream extends tts.ChunkedStream {
|
|
112
|
+
label = 'openai.ChunkedStream';
|
|
113
|
+
private stream: Promise<any>;
|
|
114
|
+
|
|
115
|
+
// set Promise<T> to any because OpenAI returns an annoying Response type
|
|
116
|
+
constructor(
|
|
117
|
+
tts: TTS,
|
|
118
|
+
text: string,
|
|
119
|
+
stream: Promise<any>,
|
|
120
|
+
connOptions?: APIConnectOptions,
|
|
121
|
+
abortSignal?: AbortSignal,
|
|
122
|
+
) {
|
|
123
|
+
super(text, tts, connOptions, abortSignal);
|
|
124
|
+
this.stream = stream;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
protected async run() {
|
|
128
|
+
try {
|
|
129
|
+
const buffer = await this.stream.then((r) => r.arrayBuffer());
|
|
130
|
+
const requestId = shortuuid();
|
|
131
|
+
const audioByteStream = new AudioByteStream(OPENAI_TTS_SAMPLE_RATE, OPENAI_TTS_CHANNELS);
|
|
132
|
+
const frames = audioByteStream.write(buffer);
|
|
133
|
+
|
|
134
|
+
let lastFrame: AudioFrame | undefined;
|
|
135
|
+
const sendLastFrame = (segmentId: string, final: boolean) => {
|
|
136
|
+
if (lastFrame) {
|
|
137
|
+
this.queue.put({ requestId, segmentId, frame: lastFrame, final });
|
|
138
|
+
lastFrame = undefined;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
for (const frame of frames) {
|
|
143
|
+
sendLastFrame(requestId, false);
|
|
144
|
+
lastFrame = frame;
|
|
145
|
+
}
|
|
146
|
+
sendLastFrame(requestId, true);
|
|
147
|
+
|
|
148
|
+
this.queue.close();
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
throw error;
|
|
154
|
+
} finally {
|
|
155
|
+
this.queue.close();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
package/src/ws/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
export { ResponsesWebSocket } from './llm.js';
|
|
6
|
+
export type {
|
|
7
|
+
WsErrorEvent,
|
|
8
|
+
WsFunctionCallItem,
|
|
9
|
+
WsOutputItem,
|
|
10
|
+
WsOutputItemDoneEvent,
|
|
11
|
+
WsOutputTextDeltaEvent,
|
|
12
|
+
WsResponseCompletedEvent,
|
|
13
|
+
WsResponseCreateEvent,
|
|
14
|
+
WsResponseCreatedEvent,
|
|
15
|
+
WsResponseFailedEvent,
|
|
16
|
+
WsServerEvent,
|
|
17
|
+
} from './types.js';
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2025 LiveKit, Inc.
|
|
2
|
+
//
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import { initializeLogger } from '@livekit/agents';
|
|
5
|
+
import { llm, llmStrict } from '@livekit/agents-plugins-test';
|
|
6
|
+
import { describe, expect, it } from 'vitest';
|
|
7
|
+
import { LLM } from '../responses/llm.js';
|
|
8
|
+
import { buildResponsesWsUrl } from './llm.js';
|
|
9
|
+
|
|
10
|
+
initializeLogger({ level: 'silent', pretty: false });
|
|
11
|
+
|
|
12
|
+
const hasOpenAIApiKey = Boolean(process.env.OPENAI_API_KEY);
|
|
13
|
+
|
|
14
|
+
describe('buildResponsesWsUrl', () => {
|
|
15
|
+
it('points at the OpenAI Responses WS endpoint with model when no baseURL is set', () => {
|
|
16
|
+
const url = new URL(buildResponsesWsUrl(undefined, 'gpt-4.1'));
|
|
17
|
+
|
|
18
|
+
expect(url.protocol).toBe('wss:');
|
|
19
|
+
expect(url.host).toBe('api.openai.com');
|
|
20
|
+
expect(url.pathname).toBe('/v1/responses');
|
|
21
|
+
expect(url.searchParams.get('model')).toBe('gpt-4.1');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('rewrites https baseURL to wss and appends /responses with the model', () => {
|
|
25
|
+
const url = new URL(buildResponsesWsUrl('https://gateway.example.com/v1', 'gpt-4o'));
|
|
26
|
+
|
|
27
|
+
expect(url.protocol).toBe('wss:');
|
|
28
|
+
expect(url.host).toBe('gateway.example.com');
|
|
29
|
+
expect(url.pathname).toBe('/v1/responses');
|
|
30
|
+
expect(url.searchParams.get('model')).toBe('gpt-4o');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('strips a trailing slash on baseURL before appending /responses', () => {
|
|
34
|
+
const url = new URL(buildResponsesWsUrl('https://gateway.example.com/v1/', 'gpt-4o-mini'));
|
|
35
|
+
|
|
36
|
+
expect(url.pathname).toBe('/v1/responses');
|
|
37
|
+
expect(url.searchParams.get('model')).toBe('gpt-4o-mini');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('rewrites http baseURL to ws (not wss)', () => {
|
|
41
|
+
const url = new URL(buildResponsesWsUrl('http://gateway.example.com/v1', 'gpt-4o-mini'));
|
|
42
|
+
|
|
43
|
+
expect(url.protocol).toBe('ws:');
|
|
44
|
+
expect(url.host).toBe('gateway.example.com');
|
|
45
|
+
expect(url.pathname).toBe('/v1/responses');
|
|
46
|
+
expect(url.searchParams.get('model')).toBe('gpt-4o-mini');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('strips a trailing slash on an http baseURL before appending /responses', () => {
|
|
50
|
+
const url = new URL(buildResponsesWsUrl('http://gateway.example.com/v1/', 'gpt-4o-mini'));
|
|
51
|
+
|
|
52
|
+
expect(url.protocol).toBe('ws:');
|
|
53
|
+
expect(url.pathname).toBe('/v1/responses');
|
|
54
|
+
expect(url.searchParams.get('model')).toBe('gpt-4o-mini');
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
if (hasOpenAIApiKey) {
|
|
59
|
+
describe('OpenAI Responses WS wrapper', async () => {
|
|
60
|
+
await llm(
|
|
61
|
+
new LLM({
|
|
62
|
+
temperature: 0,
|
|
63
|
+
strictToolSchema: false,
|
|
64
|
+
useWebSocket: true,
|
|
65
|
+
}),
|
|
66
|
+
true,
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('OpenAI Responses WS wrapper strict tool schema', async () => {
|
|
71
|
+
await llmStrict(
|
|
72
|
+
new LLM({
|
|
73
|
+
temperature: 0,
|
|
74
|
+
strictToolSchema: true,
|
|
75
|
+
useWebSocket: true,
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
} else {
|
|
80
|
+
describe('OpenAI Responses WS wrapper', () => {
|
|
81
|
+
it.skip('requires OPENAI_API_KEY', () => {});
|
|
82
|
+
});
|
|
83
|
+
}
|