@livekit/agents-plugin-openai 1.4.9 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/dist/index.cjs +4 -2
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +1 -0
  4. package/dist/index.d.ts +1 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +2 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/llm.cjs +8 -2
  9. package/dist/llm.cjs.map +1 -1
  10. package/dist/llm.d.cts +4 -3
  11. package/dist/llm.d.ts +4 -3
  12. package/dist/llm.d.ts.map +1 -1
  13. package/dist/llm.js +8 -2
  14. package/dist/llm.js.map +1 -1
  15. package/dist/models.cjs +12 -3
  16. package/dist/models.cjs.map +1 -1
  17. package/dist/models.d.cts +7 -1
  18. package/dist/models.d.ts +7 -1
  19. package/dist/models.d.ts.map +1 -1
  20. package/dist/models.js +11 -3
  21. package/dist/models.js.map +1 -1
  22. package/dist/realtime/api_proto.cjs.map +1 -1
  23. package/dist/realtime/api_proto.d.cts +1 -0
  24. package/dist/realtime/api_proto.d.ts +1 -0
  25. package/dist/realtime/api_proto.d.ts.map +1 -1
  26. package/dist/realtime/api_proto.js.map +1 -1
  27. package/dist/realtime/realtime_model.cjs +85 -27
  28. package/dist/realtime/realtime_model.cjs.map +1 -1
  29. package/dist/realtime/realtime_model.d.cts +2 -1
  30. package/dist/realtime/realtime_model.d.ts +2 -1
  31. package/dist/realtime/realtime_model.d.ts.map +1 -1
  32. package/dist/realtime/realtime_model.js +85 -27
  33. package/dist/realtime/realtime_model.js.map +1 -1
  34. package/dist/realtime/realtime_model.test.cjs +28 -0
  35. package/dist/realtime/realtime_model.test.cjs.map +1 -1
  36. package/dist/realtime/realtime_model.test.js +28 -0
  37. package/dist/realtime/realtime_model.test.js.map +1 -1
  38. package/dist/responses/llm.cjs +15 -18
  39. package/dist/responses/llm.cjs.map +1 -1
  40. package/dist/responses/llm.d.cts +4 -2
  41. package/dist/responses/llm.d.ts +4 -2
  42. package/dist/responses/llm.d.ts.map +1 -1
  43. package/dist/responses/llm.js +15 -18
  44. package/dist/responses/llm.js.map +1 -1
  45. package/dist/tool_utils.cjs +53 -0
  46. package/dist/tool_utils.cjs.map +1 -0
  47. package/dist/tool_utils.d.cts +4 -0
  48. package/dist/tool_utils.d.ts +4 -0
  49. package/dist/tool_utils.d.ts.map +1 -0
  50. package/dist/tool_utils.js +29 -0
  51. package/dist/tool_utils.js.map +1 -0
  52. package/dist/tool_utils.test.cjs +77 -0
  53. package/dist/tool_utils.test.cjs.map +1 -0
  54. package/dist/tool_utils.test.d.cts +2 -0
  55. package/dist/tool_utils.test.d.ts +2 -0
  56. package/dist/tool_utils.test.d.ts.map +1 -0
  57. package/dist/tool_utils.test.js +76 -0
  58. package/dist/tool_utils.test.js.map +1 -0
  59. package/dist/tools.cjs +117 -0
  60. package/dist/tools.cjs.map +1 -0
  61. package/dist/tools.d.cts +88 -0
  62. package/dist/tools.d.ts +88 -0
  63. package/dist/tools.d.ts.map +1 -0
  64. package/dist/tools.js +90 -0
  65. package/dist/tools.js.map +1 -0
  66. package/dist/ws/llm.cjs +15 -18
  67. package/dist/ws/llm.cjs.map +1 -1
  68. package/dist/ws/llm.d.cts +5 -3
  69. package/dist/ws/llm.d.ts +5 -3
  70. package/dist/ws/llm.d.ts.map +1 -1
  71. package/dist/ws/llm.js +15 -18
  72. package/dist/ws/llm.js.map +1 -1
  73. package/package.json +7 -7
  74. package/src/index.ts +1 -0
  75. package/src/llm.ts +16 -3
  76. package/src/models.ts +18 -3
  77. package/src/realtime/api_proto.ts +1 -0
  78. package/src/realtime/realtime_model.test.ts +38 -0
  79. package/src/realtime/realtime_model.ts +104 -28
  80. package/src/responses/llm.ts +27 -23
  81. package/src/tool_utils.test.ts +84 -0
  82. package/src/tool_utils.ts +46 -0
  83. package/src/tools.ts +166 -0
  84. package/src/ws/llm.ts +25 -24
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/ws/llm.ts CHANGED
@@ -14,7 +14,9 @@ import {
14
14
  } from '@livekit/agents';
15
15
  import type OpenAI from 'openai';
16
16
  import { WebSocket } from 'ws';
17
- import type { ChatModels } from '../models.js';
17
+ import type { ChatModels, Reasoning } from '../models.js';
18
+ import { defaultReasoningEffort } from '../models.js';
19
+ import { toResponsesTools } from '../tool_utils.js';
18
20
  import type {
19
21
  WsOutputItemDoneEvent,
20
22
  WsOutputTextDeltaEvent,
@@ -169,6 +171,8 @@ export interface WSLLMOptions {
169
171
  serviceTier?: string;
170
172
  /** Upper bound for the number of tokens that can be generated for a response. */
171
173
  maxOutputTokens?: number;
174
+ /** Configuration options for reasoning models. */
175
+ reasoning?: Reasoning | null;
172
176
  }
173
177
 
174
178
  const defaultLLMOptions: WSLLMOptions = {
@@ -203,6 +207,13 @@ export class WSLLM extends llm.LLM {
203
207
  super();
204
208
 
205
209
  this.#opts = { ...defaultLLMOptions, ...opts };
210
+ if (this.#opts.reasoning === undefined) {
211
+ const effort = defaultReasoningEffort(this.#opts.model);
212
+ if (effort !== undefined) {
213
+ this.#opts.reasoning = { effort };
214
+ }
215
+ }
216
+
206
217
  if (!this.#opts.apiKey) {
207
218
  throw new Error('OpenAI API key is required, whether as an argument or as $OPENAI_API_KEY');
208
219
  }
@@ -253,24 +264,29 @@ export class WSLLM extends llm.LLM {
253
264
 
254
265
  chat({
255
266
  chatCtx,
256
- toolCtx,
267
+ toolCtx: toolCtxInput,
257
268
  connOptions = DEFAULT_API_CONNECT_OPTIONS,
258
269
  parallelToolCalls,
259
270
  toolChoice,
260
271
  extraKwargs,
261
272
  }: {
262
273
  chatCtx: llm.ChatContext;
263
- toolCtx?: llm.ToolContext;
274
+ toolCtx?: llm.ToolContextLike;
264
275
  connOptions?: APIConnectOptions;
265
276
  parallelToolCalls?: boolean;
266
277
  toolChoice?: llm.ToolChoice;
267
278
  extraKwargs?: Record<string, unknown>;
268
279
  }): WSLLMStream {
280
+ const toolCtx = llm.toToolContext(toolCtxInput);
269
281
  const modelOptions: Record<string, unknown> = { ...(extraKwargs ?? {}) };
270
282
 
271
283
  parallelToolCalls =
272
284
  parallelToolCalls !== undefined ? parallelToolCalls : this.#opts.parallelToolCalls;
273
- if (toolCtx && Object.keys(toolCtx).length > 0 && parallelToolCalls !== undefined) {
285
+ if (
286
+ toolCtx &&
287
+ Object.keys(toolCtx.functionTools).length > 0 &&
288
+ parallelToolCalls !== undefined
289
+ ) {
274
290
  modelOptions.parallel_tool_calls = parallelToolCalls;
275
291
  }
276
292
 
@@ -300,6 +316,10 @@ export class WSLLM extends llm.LLM {
300
316
  modelOptions.max_output_tokens = this.#opts.maxOutputTokens;
301
317
  }
302
318
 
319
+ if (this.#opts.reasoning !== undefined) {
320
+ modelOptions.reasoning = this.#opts.reasoning;
321
+ }
322
+
303
323
  let inputChatCtx = chatCtx;
304
324
  let prevResponseId: string | undefined;
305
325
  const canUseStoredResponse = modelOptions.store !== false;
@@ -446,26 +466,7 @@ export class WSLLMStream extends llm.LLMStream {
446
466
  'openai.responses',
447
467
  )) as OpenAI.Responses.ResponseInputItem[];
448
468
 
449
- const tools = this.toolCtx
450
- ? llm.sortedToolEntries(this.toolCtx).map(([name, func]) => {
451
- const oaiParams = {
452
- type: 'function' as const,
453
- name,
454
- description: func.description,
455
- parameters: llm.toJsonSchema(
456
- func.parameters,
457
- true,
458
- this.#strictToolSchema,
459
- ) as unknown as OpenAI.Responses.FunctionTool['parameters'],
460
- } as OpenAI.Responses.FunctionTool;
461
-
462
- if (this.#strictToolSchema) {
463
- oaiParams.strict = true;
464
- }
465
-
466
- return oaiParams;
467
- })
468
- : undefined;
469
+ const tools = this.toolCtx ? toResponsesTools(this.toolCtx, this.#strictToolSchema) : undefined;
469
470
 
470
471
  const requestOptions: Record<string, unknown> = { ...this.#modelOptions };
471
472
  if (!tools) {