@livekit/agents-plugin-openai 1.8.0 → 1.8.1

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 (48) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.js +1 -1
  3. package/dist/realtime/gpt_live_model.cjs +883 -0
  4. package/dist/realtime/gpt_live_model.cjs.map +1 -0
  5. package/dist/realtime/gpt_live_model.d.cts +183 -0
  6. package/dist/realtime/gpt_live_model.d.ts +183 -0
  7. package/dist/realtime/gpt_live_model.d.ts.map +1 -0
  8. package/dist/realtime/gpt_live_model.js +859 -0
  9. package/dist/realtime/gpt_live_model.js.map +1 -0
  10. package/dist/realtime/gpt_live_model.test.cjs +1397 -0
  11. package/dist/realtime/gpt_live_model.test.cjs.map +1 -0
  12. package/dist/realtime/gpt_live_model.test.d.cts +2 -0
  13. package/dist/realtime/gpt_live_model.test.d.ts +2 -0
  14. package/dist/realtime/gpt_live_model.test.d.ts.map +1 -0
  15. package/dist/realtime/gpt_live_model.test.js +1398 -0
  16. package/dist/realtime/gpt_live_model.test.js.map +1 -0
  17. package/dist/realtime/gpt_live_types.cjs +17 -0
  18. package/dist/realtime/gpt_live_types.cjs.map +1 -0
  19. package/dist/realtime/gpt_live_types.d.cts +170 -0
  20. package/dist/realtime/gpt_live_types.d.ts +170 -0
  21. package/dist/realtime/gpt_live_types.d.ts.map +1 -0
  22. package/dist/realtime/gpt_live_types.js +1 -0
  23. package/dist/realtime/gpt_live_types.js.map +1 -0
  24. package/dist/realtime/index.cjs +22 -1
  25. package/dist/realtime/index.cjs.map +1 -1
  26. package/dist/realtime/index.d.cts +2 -0
  27. package/dist/realtime/index.d.ts +2 -0
  28. package/dist/realtime/index.d.ts.map +1 -1
  29. package/dist/realtime/index.js +5 -0
  30. package/dist/realtime/index.js.map +1 -1
  31. package/dist/tool_utils.cjs +12 -8
  32. package/dist/tool_utils.cjs.map +1 -1
  33. package/dist/tool_utils.d.cts +1 -0
  34. package/dist/tool_utils.d.ts +1 -0
  35. package/dist/tool_utils.d.ts.map +1 -1
  36. package/dist/tool_utils.js +11 -8
  37. package/dist/tool_utils.js.map +1 -1
  38. package/dist/tool_utils.test.cjs +27 -0
  39. package/dist/tool_utils.test.cjs.map +1 -1
  40. package/dist/tool_utils.test.js +27 -0
  41. package/dist/tool_utils.test.js.map +1 -1
  42. package/package.json +5 -5
  43. package/src/realtime/gpt_live_model.test.ts +1458 -0
  44. package/src/realtime/gpt_live_model.ts +1032 -0
  45. package/src/realtime/gpt_live_types.ts +140 -0
  46. package/src/realtime/index.ts +2 -0
  47. package/src/tool_utils.test.ts +30 -0
  48. package/src/tool_utils.ts +21 -18
@@ -0,0 +1,140 @@
1
+ // SPDX-FileCopyrightText: 2026 LiveKit, Inc.
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ import type OpenAI from 'openai';
5
+ import type { Reasoning } from 'openai/resources/shared.js';
6
+
7
+ /** GPT-Live wire types. Field names follow the service protocol. @public */
8
+ export type DelegationTarget = 'responses' | 'client';
9
+ /** Roles accepted in initial session history. @public */
10
+ export type InputRole = 'developer' | 'user' | 'assistant';
11
+ /** A text message used to seed session history. @public */
12
+ export interface InputItem {
13
+ type: 'message';
14
+ role: InputRole;
15
+ content: { type: 'input_text' | 'output_text'; text: string }[];
16
+ }
17
+ /** Backend Responses API options, using wire field names. @public */
18
+ export interface ResponsesConfig {
19
+ model?: string;
20
+ instructions?: string;
21
+ tools?: OpenAI.Responses.Tool[];
22
+ tool_choice?: string | { type: 'function'; name: string };
23
+ parallel_tool_calls?: boolean;
24
+ reasoning?: Reasoning;
25
+ text?: OpenAI.Responses.ResponseTextConfig;
26
+ service_tier?: 'auto' | 'default' | 'flex' | 'priority';
27
+ max_output_tokens?: number;
28
+ }
29
+ /** Choose the backend Responses model or application callbacks. @public */
30
+ export type Delegation = { type: 'client' } | { type: 'responses'; responses: ResponsesConfig };
31
+ /** Initial voice, audio, context, and delegation configuration. @public */
32
+ export interface SessionConfig {
33
+ model: string;
34
+ instructions?: string;
35
+ input?: InputItem[];
36
+ audio?: {
37
+ format?: { type: 'audio/pcm' | 'audio/pcmu' | 'audio/pcma'; rate: number };
38
+ output?: { voice?: string | Record<string, unknown> };
39
+ };
40
+ delegation?: Delegation;
41
+ }
42
+ /** Commands sent to the GPT-Live WebSocket. @public */
43
+ export type ClientEvent = { event_id?: string } & (
44
+ | { type: 'session.start'; session: SessionConfig }
45
+ | { type: 'session.update'; session: { delegation: Delegation } }
46
+ | { type: 'session.input_audio.append'; audio: string }
47
+ | {
48
+ type:
49
+ | 'session.input_audio.mute'
50
+ | 'session.input_audio.unmute'
51
+ | 'session.close'
52
+ | 'response.create';
53
+ }
54
+ | {
55
+ type: 'session.instructions.append' | 'session.thinking.append' | 'session.commentary.append';
56
+ /** Required on every append, including when no delegation is being answered. */
57
+ delegation_id: string | null;
58
+ content: string;
59
+ }
60
+ | { type: 'response.item.create'; item: OpenAI.Responses.ResponseInputItem }
61
+ );
62
+ /** Backend response token usage, separate from voice session duration. @public */
63
+ export interface ResponseUsage {
64
+ input_tokens?: number;
65
+ input_tokens_details?: { cached_tokens?: number; cache_write_tokens?: number };
66
+ output_tokens?: number;
67
+ output_tokens_details?: { reasoning_tokens?: number };
68
+ total_tokens?: number;
69
+ }
70
+ /** Responses API events nested inside a GPT-Live response.event. @public */
71
+ export interface ResponsesEvent {
72
+ type: string;
73
+ response?: {
74
+ id?: string | null;
75
+ model?: string | null;
76
+ usage?: ResponseUsage | null;
77
+ error?: Record<string, unknown> | null;
78
+ incomplete_details?: Record<string, unknown> | null;
79
+ } | null;
80
+ item?: {
81
+ id?: string | null;
82
+ type?: string | null;
83
+ call_id?: string | null;
84
+ name?: string | null;
85
+ arguments?: string | null;
86
+ } | null;
87
+ }
88
+ /** Provider error details. Messages can contain customer content. @public */
89
+ export interface ErrorBody {
90
+ type?: string | null;
91
+ code?: string | null;
92
+ message?: string;
93
+ param?: string | null;
94
+ client_event_id?: string | null;
95
+ }
96
+ /** Server fields are optional so missing fields do not end a session. @public */
97
+ export type ServerEvent =
98
+ | { type: 'session.started'; session?: { id?: string | null } }
99
+ | { type: 'session.output_audio.delta'; delta?: string }
100
+ | {
101
+ type: 'session.output_transcript.delta' | 'session.input_transcript.delta';
102
+ delta?: string;
103
+ start_ms?: number | null;
104
+ end_ms?: number | null;
105
+ }
106
+ | {
107
+ type: 'session.delegation.created';
108
+ delegation?: { id?: string | null; target?: DelegationTarget | null };
109
+ }
110
+ | { type: 'response.event'; delegation_id?: string | null; event?: ResponsesEvent }
111
+ | {
112
+ type: 'session.usage.updated';
113
+ usage?: { seconds?: number };
114
+ context_window?: { usage_ratio?: number | null } | null;
115
+ }
116
+ | {
117
+ type: 'session.closed';
118
+ reason?:
119
+ | 'close_requested'
120
+ | 'expired'
121
+ | 'content'
122
+ | 'remote_hangup'
123
+ | 'connection_lost'
124
+ | null;
125
+ usage?: { seconds?: number };
126
+ context_window?: { usage_ratio?: number | null } | null;
127
+ }
128
+ | { type: 'error'; error?: ErrorBody }
129
+ | {
130
+ type: 'session.updated' | 'session.input_audio.muted' | 'session.input_audio.unmuted';
131
+ client_event_id?: string;
132
+ }
133
+ | {
134
+ /** Sent at the estimated context-injection end; does not indicate speech completion. */
135
+ type:
136
+ | 'session.instructions.appended'
137
+ | 'session.thinking.appended'
138
+ | 'session.commentary.appended';
139
+ client_event_id?: string;
140
+ };
@@ -3,3 +3,5 @@
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  export * from './api_proto.js';
5
5
  export * from './realtime_model.js';
6
+ export * from './gpt_live_model.js';
7
+ export * as GPTLive from './gpt_live_types.js';
@@ -8,6 +8,36 @@ import { toResponsesTools } from './tool_utils.js';
8
8
  import { CodeInterpreter, FileSearch, WebSearch } from './tools.js';
9
9
 
10
10
  describe('toResponsesTools', () => {
11
+ it('sorts function tools before provider tools without making optional parameters strict', () => {
12
+ const makeTool = (name: string) =>
13
+ llm.tool({
14
+ name,
15
+ description: 'Look up weather',
16
+ parameters: z.object({ city: z.string().optional() }),
17
+ execute: async () => 'sunny',
18
+ });
19
+
20
+ expect(
21
+ toResponsesTools(
22
+ new llm.ToolContext([makeTool('zulu'), new WebSearch(), makeTool('alpha')]),
23
+ false,
24
+ ),
25
+ ).toEqual([
26
+ ...['alpha', 'zulu'].map((name) => ({
27
+ type: 'function',
28
+ name,
29
+ description: 'Look up weather',
30
+ parameters: {
31
+ $schema: 'http://json-schema.org/draft-07/schema#',
32
+ type: 'object',
33
+ properties: { city: { type: 'string' } },
34
+ additionalProperties: false,
35
+ },
36
+ })),
37
+ { type: 'web_search', search_context_size: 'medium' },
38
+ ]);
39
+ });
40
+
11
41
  it('serializes function tools', () => {
12
42
  const fn = llm.tool({
13
43
  name: 'lookup_weather',
package/src/tool_utils.ts CHANGED
@@ -5,16 +5,14 @@ import { llm } from '@livekit/agents';
5
5
  import type OpenAI from 'openai';
6
6
  import { OpenAITool } from './tools.js';
7
7
 
8
- export function toResponsesTools(
9
- toolCtx: llm.ToolContext,
8
+ export function toResponsesTool(
9
+ tool: llm.Tool,
10
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]) => {
11
+ ): OpenAI.Responses.Tool | undefined {
12
+ if (llm.isFunctionTool(tool)) {
15
13
  const oaiParams = {
16
14
  type: 'function' as const,
17
- name,
15
+ name: tool.name,
18
16
  description: tool.description,
19
17
  parameters: llm.toJsonSchema(
20
18
  tool.parameters,
@@ -28,19 +26,24 @@ export function toResponsesTools(
28
26
  }
29
27
 
30
28
  return oaiParams;
31
- });
29
+ }
32
30
 
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);
31
+ return tool instanceof OpenAITool
32
+ ? (tool.toToolConfig() as unknown as OpenAI.Responses.Tool)
33
+ : undefined;
34
+ }
42
35
 
43
- const tools = [...functionTools, ...providerTools];
36
+ export function toResponsesTools(
37
+ toolCtx: llm.ToolContext,
38
+ strictToolSchema: boolean,
39
+ ): OpenAI.Responses.Tool[] | undefined {
40
+ // Function tools are emitted first, sorted by name for deterministic payloads; provider
41
+ // tools follow in registration order.
42
+ const functionTools = llm.sortedToolEntries(toolCtx).map(([, tool]) => tool);
43
+ const providerTools = toolCtx.flatten().filter((tool) => !llm.isFunctionTool(tool));
44
+ const tools = [...functionTools, ...providerTools]
45
+ .map((tool) => toResponsesTool(tool, strictToolSchema))
46
+ .filter((tool): tool is OpenAI.Responses.Tool => tool !== undefined);
44
47
 
45
48
  return tools.length > 0 ? tools : undefined;
46
49
  }