@openai/agents-realtime 0.0.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.
- package/LICENSE +21 -0
- package/README.md +13 -0
- package/dist/bundle/openai-realtime-agents.mjs +8777 -0
- package/dist/bundle/openai-realtime-agents.umd.js +11 -0
- package/dist/clientMessages.d.ts +40 -0
- package/dist/clientMessages.js +2 -0
- package/dist/clientMessages.js.map +1 -0
- package/dist/clientMessages.mjs +2 -0
- package/dist/clientMessages.mjs.map +1 -0
- package/dist/guardrail.d.ts +32 -0
- package/dist/guardrail.js +34 -0
- package/dist/guardrail.js.map +1 -0
- package/dist/guardrail.mjs +34 -0
- package/dist/guardrail.mjs.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +13 -0
- package/dist/index.mjs.map +1 -0
- package/dist/items.d.ts +183 -0
- package/dist/items.js +47 -0
- package/dist/items.js.map +1 -0
- package/dist/items.mjs +47 -0
- package/dist/items.mjs.map +1 -0
- package/dist/logger.d.ts +2 -0
- package/dist/logger.js +4 -0
- package/dist/logger.js.map +1 -0
- package/dist/logger.mjs +4 -0
- package/dist/logger.mjs.map +1 -0
- package/dist/metadata.d.ts +9 -0
- package/dist/metadata.js +11 -0
- package/dist/metadata.js.map +1 -0
- package/dist/metadata.mjs +11 -0
- package/dist/metadata.mjs.map +1 -0
- package/dist/openaiRealtimeBase.d.ts +143 -0
- package/dist/openaiRealtimeBase.js +449 -0
- package/dist/openaiRealtimeBase.js.map +1 -0
- package/dist/openaiRealtimeBase.mjs +449 -0
- package/dist/openaiRealtimeBase.mjs.map +1 -0
- package/dist/openaiRealtimeEvents.d.ts +3242 -0
- package/dist/openaiRealtimeEvents.js +439 -0
- package/dist/openaiRealtimeEvents.js.map +1 -0
- package/dist/openaiRealtimeEvents.mjs +439 -0
- package/dist/openaiRealtimeEvents.mjs.map +1 -0
- package/dist/openaiRealtimeWebRtc.d.ts +102 -0
- package/dist/openaiRealtimeWebRtc.js +245 -0
- package/dist/openaiRealtimeWebRtc.js.map +1 -0
- package/dist/openaiRealtimeWebRtc.mjs +245 -0
- package/dist/openaiRealtimeWebRtc.mjs.map +1 -0
- package/dist/openaiRealtimeWebsocket.d.ts +126 -0
- package/dist/openaiRealtimeWebsocket.js +293 -0
- package/dist/openaiRealtimeWebsocket.js.map +1 -0
- package/dist/openaiRealtimeWebsocket.mjs +293 -0
- package/dist/openaiRealtimeWebsocket.mjs.map +1 -0
- package/dist/realtimeAgent.d.ts +49 -0
- package/dist/realtimeAgent.js +37 -0
- package/dist/realtimeAgent.js.map +1 -0
- package/dist/realtimeAgent.mjs +37 -0
- package/dist/realtimeAgent.mjs.map +1 -0
- package/dist/realtimeSession.d.ts +210 -0
- package/dist/realtimeSession.js +469 -0
- package/dist/realtimeSession.js.map +1 -0
- package/dist/realtimeSession.mjs +469 -0
- package/dist/realtimeSession.mjs.map +1 -0
- package/dist/realtimeSessionEvents.d.ts +118 -0
- package/dist/realtimeSessionEvents.js +2 -0
- package/dist/realtimeSessionEvents.js.map +1 -0
- package/dist/realtimeSessionEvents.mjs +2 -0
- package/dist/realtimeSessionEvents.mjs.map +1 -0
- package/dist/shims/shims-browser.d.ts +9 -0
- package/dist/shims/shims-browser.js +6 -0
- package/dist/shims/shims-browser.js.map +1 -0
- package/dist/shims/shims-browser.mjs +6 -0
- package/dist/shims/shims-browser.mjs.map +1 -0
- package/dist/shims/shims-node.d.ts +2 -0
- package/dist/shims/shims-node.js +5 -0
- package/dist/shims/shims-node.js.map +1 -0
- package/dist/shims/shims-node.mjs +5 -0
- package/dist/shims/shims-node.mjs.map +1 -0
- package/dist/shims/shims.d.ts +1 -0
- package/dist/shims/shims.js +2 -0
- package/dist/shims/shims.js.map +1 -0
- package/dist/shims/shims.mjs +2 -0
- package/dist/shims/shims.mjs.map +1 -0
- package/dist/transportLayer.d.ts +96 -0
- package/dist/transportLayer.js +2 -0
- package/dist/transportLayer.js.map +1 -0
- package/dist/transportLayer.mjs +2 -0
- package/dist/transportLayer.mjs.map +1 -0
- package/dist/transportLayerEvents.d.ts +99 -0
- package/dist/transportLayerEvents.js +2 -0
- package/dist/transportLayerEvents.js.map +1 -0
- package/dist/transportLayerEvents.mjs +2 -0
- package/dist/transportLayerEvents.mjs.map +1 -0
- package/dist/utils.d.ts +61 -0
- package/dist/utils.js +183 -0
- package/dist/utils.js.map +1 -0
- package/dist/utils.mjs +183 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +77 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shims-node.js","sourceRoot":"","sources":["../../src/shims/shims-node.ts"],"names":[],"mappings":"OAAO,EAAE,SAAS,EAAE,MAAM,IAAI;AAC9B,MAAM,UAAU,oBAAoB;IAClC,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shims-node.mjs","sourceRoot":"","sources":["../../src/shims/shims-node.ts"],"names":[],"mappings":"OAAO,EAAE,SAAS,EAAE,MAAM,IAAI;AAC9B,MAAM,UAAU,oBAAoB;IAClC,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './shims-node';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shims.js","sourceRoot":"","sources":["../../src/shims/shims.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shims.mjs","sourceRoot":"","sources":["../../src/shims/shims.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { EventEmitter } from '@openai/agents-core/_shims';
|
|
2
|
+
import { RealtimeClientMessage, RealtimeSessionConfig, RealtimeUserInput } from './clientMessages';
|
|
3
|
+
import { RealtimeItem } from './items';
|
|
4
|
+
import { RealtimeTranportEventTypes, TransportToolCallEvent } from './transportLayerEvents';
|
|
5
|
+
/**
|
|
6
|
+
* The type of the API key. Can be a string or a function that returns a string or a promise that
|
|
7
|
+
* resolves to a string.
|
|
8
|
+
*/
|
|
9
|
+
export type ApiKey = string | (() => string | Promise<string>);
|
|
10
|
+
/**
|
|
11
|
+
* The options for the connection to the model.
|
|
12
|
+
*/
|
|
13
|
+
export type RealtimeTransportLayerConnectOptions = {
|
|
14
|
+
/**
|
|
15
|
+
* The API key to use for the connection.
|
|
16
|
+
*/
|
|
17
|
+
apiKey: ApiKey;
|
|
18
|
+
/**
|
|
19
|
+
* The model to use for the connection.
|
|
20
|
+
*/
|
|
21
|
+
model?: string;
|
|
22
|
+
/**
|
|
23
|
+
* The URL to use for the connection.
|
|
24
|
+
*/
|
|
25
|
+
url?: string;
|
|
26
|
+
/**
|
|
27
|
+
* The initial session config to use for the session.
|
|
28
|
+
*/
|
|
29
|
+
initialSessionConfig?: Partial<RealtimeSessionConfig>;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The transport layer is the layer that handles the connection to the model
|
|
33
|
+
* and the communication with the model.
|
|
34
|
+
*/
|
|
35
|
+
export interface RealtimeTransportLayer extends EventEmitter<RealtimeTranportEventTypes> {
|
|
36
|
+
status: 'connected' | 'disconnected' | 'connecting' | 'disconnecting';
|
|
37
|
+
/**
|
|
38
|
+
* Establishes the connection to the model and keeps the connection alive
|
|
39
|
+
* @param options - The options for the connection
|
|
40
|
+
*/
|
|
41
|
+
connect(options: RealtimeTransportLayerConnectOptions): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Whether the input audio track is currently muted
|
|
44
|
+
* null if the muting is not handled by the transport layer
|
|
45
|
+
*/
|
|
46
|
+
readonly muted: boolean | null;
|
|
47
|
+
/**
|
|
48
|
+
* Sends a raw event to the model
|
|
49
|
+
* @param event - The event to send
|
|
50
|
+
*/
|
|
51
|
+
sendEvent(event: RealtimeClientMessage): void;
|
|
52
|
+
/**
|
|
53
|
+
* Sends a text message to the model
|
|
54
|
+
* @param message - The message to send
|
|
55
|
+
* @param otherEventData - Additional event data, will be merged into the event
|
|
56
|
+
*/
|
|
57
|
+
sendMessage(message: RealtimeUserInput, otherEventData: Record<string, any>): void;
|
|
58
|
+
/**
|
|
59
|
+
* Sends a raw audio buffer to the model
|
|
60
|
+
* @param audio - The audio buffer to send
|
|
61
|
+
* @param options - Additional options
|
|
62
|
+
* @param options.commit - Whether to commit the audio buffer to the model. If the model does not do turn detection, this can be used to indicate the turn is completed.
|
|
63
|
+
*/
|
|
64
|
+
sendAudio(audio: ArrayBuffer, options: {
|
|
65
|
+
commit?: boolean;
|
|
66
|
+
}): void;
|
|
67
|
+
/**
|
|
68
|
+
* Sends an updated session configuration to the model. Used to update for example the model instructions during a handoff
|
|
69
|
+
* @param config - The new session config
|
|
70
|
+
*/
|
|
71
|
+
updateSessionConfig(config: Partial<RealtimeSessionConfig>): void;
|
|
72
|
+
/**
|
|
73
|
+
* Closes the connection to the model
|
|
74
|
+
*/
|
|
75
|
+
close(): void;
|
|
76
|
+
/**
|
|
77
|
+
* Mutes the input audio track
|
|
78
|
+
* @param muted - Whether to mute the input audio track
|
|
79
|
+
*/
|
|
80
|
+
mute(muted: boolean): void;
|
|
81
|
+
/**
|
|
82
|
+
* Sends a function call output to the model
|
|
83
|
+
* @param toolCall - The tool call to send
|
|
84
|
+
* @param output - The output of the tool call
|
|
85
|
+
*/
|
|
86
|
+
sendFunctionCallOutput(toolCall: TransportToolCallEvent, output: string, startResponse: boolean): void;
|
|
87
|
+
/**
|
|
88
|
+
* Interrupts the current turn. Used for example when a guardrail is triggered
|
|
89
|
+
*/
|
|
90
|
+
interrupt(): void;
|
|
91
|
+
/**
|
|
92
|
+
* Resets the conversation history / context to a specific state
|
|
93
|
+
* @param history - The history to reset to
|
|
94
|
+
*/
|
|
95
|
+
resetHistory(oldHistory: RealtimeItem[], newHistory: RealtimeItem[]): void;
|
|
96
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transportLayer.js","sourceRoot":"","sources":["../src/transportLayer.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transportLayer.mjs","sourceRoot":"","sources":["../src/transportLayer.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { protocol, Usage } from '@openai/agents-core';
|
|
2
|
+
import { RealtimeBaseItem, RealtimeItem } from './items';
|
|
3
|
+
/**
|
|
4
|
+
* Represents an error that occurred on the transport layer.
|
|
5
|
+
*/
|
|
6
|
+
export type TransportError = {
|
|
7
|
+
type: 'error';
|
|
8
|
+
error: unknown;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Event representing an attempted tool call by the model on the transport layer.
|
|
12
|
+
*/
|
|
13
|
+
export type TransportToolCallEvent = {
|
|
14
|
+
id?: string;
|
|
15
|
+
type: 'function_call';
|
|
16
|
+
name: string;
|
|
17
|
+
callId: string;
|
|
18
|
+
arguments: string;
|
|
19
|
+
previousItemId?: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Event representing audio data from the model on the transport layer.
|
|
23
|
+
*/
|
|
24
|
+
export type TransportLayerAudio = {
|
|
25
|
+
type: 'audio';
|
|
26
|
+
data: ArrayBuffer;
|
|
27
|
+
responseId: string;
|
|
28
|
+
};
|
|
29
|
+
export type TransportLayerTranscriptDelta = {
|
|
30
|
+
type: 'transcript_delta';
|
|
31
|
+
itemId: string;
|
|
32
|
+
delta: string;
|
|
33
|
+
};
|
|
34
|
+
export type TransportLayerResponseCompleted = protocol.StreamEventResponseCompleted;
|
|
35
|
+
export type TransportLayerResponseStarted = protocol.StreamEventResponseStarted;
|
|
36
|
+
export type ConnectionStatus = 'connecting' | 'connected' | 'disconnected';
|
|
37
|
+
export type TransportEvent = TransportError | TransportToolCallEvent | {
|
|
38
|
+
type: string;
|
|
39
|
+
[key: string]: any;
|
|
40
|
+
};
|
|
41
|
+
export type RealtimeTranportEventTypes = {
|
|
42
|
+
/**
|
|
43
|
+
* A raw event from the transport layer. Allows a user to tap directly into the events of the
|
|
44
|
+
* transport layer.
|
|
45
|
+
*/
|
|
46
|
+
'*': [event: TransportEvent];
|
|
47
|
+
/**
|
|
48
|
+
* Triggered if the model / transport layer encountered an error
|
|
49
|
+
*/
|
|
50
|
+
error: [error: TransportError];
|
|
51
|
+
/**
|
|
52
|
+
* Triggered when the model is trying to call a function.
|
|
53
|
+
*/
|
|
54
|
+
function_call: [event: TransportToolCallEvent];
|
|
55
|
+
/**
|
|
56
|
+
* Triggered when there is new audio data available. Might not be triggered if the transport layer
|
|
57
|
+
* handles the audio internally (WebRTC).
|
|
58
|
+
*/
|
|
59
|
+
audio: [event: TransportLayerAudio];
|
|
60
|
+
/**
|
|
61
|
+
* Triggered when the model detected that it was interrupted. This can be used by the client
|
|
62
|
+
* to stop audio playback.
|
|
63
|
+
*/
|
|
64
|
+
audio_interrupted: [];
|
|
65
|
+
/**
|
|
66
|
+
* Triggered when there is a new text delta of the transcript available.
|
|
67
|
+
*/
|
|
68
|
+
audio_transcript_delta: [deltaEvent: TransportLayerTranscriptDelta];
|
|
69
|
+
/**
|
|
70
|
+
* Triggered when the audio generation is done.
|
|
71
|
+
*/
|
|
72
|
+
audio_done: [];
|
|
73
|
+
/**
|
|
74
|
+
* Triggered when the usage update is available.
|
|
75
|
+
*/
|
|
76
|
+
usage_update: [usage: Usage];
|
|
77
|
+
/**
|
|
78
|
+
* Triggered when the history is added or updated.
|
|
79
|
+
*/
|
|
80
|
+
item_update: [item: RealtimeItem];
|
|
81
|
+
/**
|
|
82
|
+
* Triggered when an item is deleted.
|
|
83
|
+
*/
|
|
84
|
+
item_deleted: [item: RealtimeBaseItem];
|
|
85
|
+
/**
|
|
86
|
+
* Triggered whenever the connection status of the transport changes.
|
|
87
|
+
* Emits the new status after the change.
|
|
88
|
+
*/
|
|
89
|
+
connection_change: [status: ConnectionStatus];
|
|
90
|
+
/**
|
|
91
|
+
* Triggered when the model starts generating a response for a turn.
|
|
92
|
+
*/
|
|
93
|
+
turn_started: [event: TransportLayerResponseStarted];
|
|
94
|
+
/**
|
|
95
|
+
* Triggered when the model is done generating a response for a turn.
|
|
96
|
+
*/
|
|
97
|
+
turn_done: [event: TransportLayerResponseCompleted];
|
|
98
|
+
[key: string]: [...args: any[]];
|
|
99
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transportLayerEvents.js","sourceRoot":"","sources":["../src/transportLayerEvents.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transportLayerEvents.mjs","sourceRoot":"","sources":["../src/transportLayerEvents.ts"],"names":[],"mappings":""}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { RealtimeItem, RealtimeMessageItem } from './items';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a base64 string to an ArrayBuffer
|
|
4
|
+
* @param {string} base64
|
|
5
|
+
* @returns {ArrayBuffer}
|
|
6
|
+
*/
|
|
7
|
+
export declare function base64ToArrayBuffer(base64: string): ArrayBuffer;
|
|
8
|
+
/**
|
|
9
|
+
* Converts an ArrayBuffer to a base64 string
|
|
10
|
+
* @param {ArrayBuffer} arrayBuffer
|
|
11
|
+
* @returns {string}
|
|
12
|
+
*/
|
|
13
|
+
export declare function arrayBufferToBase64(arrayBuffer: ArrayBuffer): string;
|
|
14
|
+
/**
|
|
15
|
+
* Get the last text from an audio output message
|
|
16
|
+
* @param item
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
export declare function getLastTextFromAudioOutputMessage(item: unknown): string | undefined;
|
|
20
|
+
export type RealtimeHistoryDiff = {
|
|
21
|
+
removals: RealtimeItem[];
|
|
22
|
+
additions: RealtimeItem[];
|
|
23
|
+
updates: RealtimeItem[];
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Compare two conversation histories to determine the removals, additions, and updates.
|
|
27
|
+
* @param oldHistory - The old history.
|
|
28
|
+
* @param newHistory - The new history.
|
|
29
|
+
* @returns A diff of the two histories.
|
|
30
|
+
*/
|
|
31
|
+
export declare function diffRealtimeHistory(oldHistory: RealtimeItem[], newHistory: RealtimeItem[]): RealtimeHistoryDiff;
|
|
32
|
+
/**
|
|
33
|
+
* Check if the browser supports WebRTC.
|
|
34
|
+
* @returns True if WebRTC is supported, false otherwise.
|
|
35
|
+
*/
|
|
36
|
+
export declare function hasWebRTCSupport(): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Removes the audio data from all content in a message by setting it to null.
|
|
39
|
+
* @param item
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
export declare function removeAudioFromContent(item: RealtimeMessageItem): RealtimeMessageItem;
|
|
43
|
+
/**
|
|
44
|
+
* Updates the realtime history array based on the incoming event and options.
|
|
45
|
+
* @param history - The current history array.
|
|
46
|
+
* @param event - The event to process (RealtimeItem).
|
|
47
|
+
* @param shouldIncludeAudioData - Whether to include audio data in message items.
|
|
48
|
+
* @returns The updated history array.
|
|
49
|
+
*/
|
|
50
|
+
export declare function updateRealtimeHistory(history: RealtimeItem[], event: RealtimeItem, shouldIncludeAudioData: boolean): RealtimeItem[];
|
|
51
|
+
/**
|
|
52
|
+
* The headers to use for the Realtime API.
|
|
53
|
+
*/
|
|
54
|
+
export declare const HEADERS: {
|
|
55
|
+
'User-Agent': string;
|
|
56
|
+
'X-OpenAI-Agents-SDK': string;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Browser websocket header
|
|
60
|
+
*/
|
|
61
|
+
export declare const WEBSOCKET_META: string;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import METADATA from "./metadata.js";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a base64 string to an ArrayBuffer
|
|
4
|
+
* @param {string} base64
|
|
5
|
+
* @returns {ArrayBuffer}
|
|
6
|
+
*/
|
|
7
|
+
export function base64ToArrayBuffer(base64) {
|
|
8
|
+
const binaryString = atob(base64);
|
|
9
|
+
const len = binaryString.length;
|
|
10
|
+
const bytes = new Uint8Array(len);
|
|
11
|
+
for (let i = 0; i < len; i++) {
|
|
12
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
13
|
+
}
|
|
14
|
+
return bytes.buffer;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Converts an ArrayBuffer to a base64 string
|
|
18
|
+
* @param {ArrayBuffer} arrayBuffer
|
|
19
|
+
* @returns {string}
|
|
20
|
+
*/
|
|
21
|
+
export function arrayBufferToBase64(arrayBuffer) {
|
|
22
|
+
const binaryString = String.fromCharCode(...new Uint8Array(arrayBuffer));
|
|
23
|
+
return btoa(binaryString);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Get the last text from an audio output message
|
|
27
|
+
* @param item
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
export function getLastTextFromAudioOutputMessage(item) {
|
|
31
|
+
if (typeof item === 'undefined' ||
|
|
32
|
+
item === null ||
|
|
33
|
+
typeof item !== 'object' ||
|
|
34
|
+
!('type' in item) ||
|
|
35
|
+
typeof item.type !== 'string' ||
|
|
36
|
+
!item.type) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
if (item.type !== 'message') {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
if (!('content' in item) ||
|
|
43
|
+
!Array.isArray(item.content) ||
|
|
44
|
+
item.content.length < 1) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
const lastContentItem = item.content[item.content.length - 1];
|
|
48
|
+
if (!('type' in lastContentItem) ||
|
|
49
|
+
typeof lastContentItem.type !== 'string') {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
if (lastContentItem.type === 'text') {
|
|
53
|
+
return typeof lastContentItem.text === 'string'
|
|
54
|
+
? lastContentItem.text
|
|
55
|
+
: undefined;
|
|
56
|
+
}
|
|
57
|
+
if (lastContentItem.type === 'audio') {
|
|
58
|
+
return typeof lastContentItem.transcript === 'string'
|
|
59
|
+
? lastContentItem.transcript
|
|
60
|
+
: undefined;
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Compare two conversation histories to determine the removals, additions, and updates.
|
|
66
|
+
* @param oldHistory - The old history.
|
|
67
|
+
* @param newHistory - The new history.
|
|
68
|
+
* @returns A diff of the two histories.
|
|
69
|
+
*/
|
|
70
|
+
export function diffRealtimeHistory(oldHistory, newHistory) {
|
|
71
|
+
const removals = oldHistory.filter((item) => !newHistory.some((newItem) => newItem.itemId === item.itemId));
|
|
72
|
+
const additions = newHistory.filter((item) => !oldHistory.some((oldItem) => oldItem.itemId === item.itemId));
|
|
73
|
+
const updates = newHistory.filter((item) => oldHistory.some((oldItem) => oldItem.itemId === item.itemId &&
|
|
74
|
+
JSON.stringify(oldItem) !== JSON.stringify(item)));
|
|
75
|
+
return {
|
|
76
|
+
removals,
|
|
77
|
+
additions,
|
|
78
|
+
updates,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Check if the browser supports WebRTC.
|
|
83
|
+
* @returns True if WebRTC is supported, false otherwise.
|
|
84
|
+
*/
|
|
85
|
+
export function hasWebRTCSupport() {
|
|
86
|
+
if (typeof window === 'undefined') {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
return typeof window['RTCPeerConnection'] !== 'undefined';
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Removes the audio data from all content in a message by setting it to null.
|
|
93
|
+
* @param item
|
|
94
|
+
* @returns
|
|
95
|
+
*/
|
|
96
|
+
export function removeAudioFromContent(item) {
|
|
97
|
+
if (item.role === 'system') {
|
|
98
|
+
return item;
|
|
99
|
+
}
|
|
100
|
+
if (item.role === 'assistant') {
|
|
101
|
+
return {
|
|
102
|
+
...item,
|
|
103
|
+
content: item.content.map((entry) => {
|
|
104
|
+
if (entry.type === 'audio') {
|
|
105
|
+
return {
|
|
106
|
+
...entry,
|
|
107
|
+
audio: null,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return entry;
|
|
111
|
+
}),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (item.role === 'user') {
|
|
115
|
+
return {
|
|
116
|
+
...item,
|
|
117
|
+
content: item.content.map((entry) => {
|
|
118
|
+
if (entry.type === 'input_audio') {
|
|
119
|
+
return {
|
|
120
|
+
...entry,
|
|
121
|
+
audio: null,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return entry;
|
|
125
|
+
}),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return item;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Updates the realtime history array based on the incoming event and options.
|
|
132
|
+
* @param history - The current history array.
|
|
133
|
+
* @param event - The event to process (RealtimeItem).
|
|
134
|
+
* @param shouldIncludeAudioData - Whether to include audio data in message items.
|
|
135
|
+
* @returns The updated history array.
|
|
136
|
+
*/
|
|
137
|
+
export function updateRealtimeHistory(history, event, shouldIncludeAudioData) {
|
|
138
|
+
const newEvent = !shouldIncludeAudioData && event.type === 'message'
|
|
139
|
+
? removeAudioFromContent(event)
|
|
140
|
+
: event;
|
|
141
|
+
const existingIndex = history.findIndex((item) => item.itemId === event.itemId);
|
|
142
|
+
if (existingIndex !== -1) {
|
|
143
|
+
// Update existing item
|
|
144
|
+
return history.map((item, idx) => {
|
|
145
|
+
if (idx === existingIndex) {
|
|
146
|
+
return newEvent;
|
|
147
|
+
}
|
|
148
|
+
if (!shouldIncludeAudioData && item.type === 'message') {
|
|
149
|
+
return removeAudioFromContent(item);
|
|
150
|
+
}
|
|
151
|
+
return item;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
else if (event.previousItemId) {
|
|
155
|
+
// Insert after previousItemId if found, else at end
|
|
156
|
+
const prevIndex = history.findIndex((item) => item.itemId === event.previousItemId);
|
|
157
|
+
if (prevIndex !== -1) {
|
|
158
|
+
return [
|
|
159
|
+
...history.slice(0, prevIndex + 1),
|
|
160
|
+
newEvent,
|
|
161
|
+
...history.slice(prevIndex + 1),
|
|
162
|
+
];
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
return [...history, newEvent];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
return [...history, newEvent];
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The headers to use for the Realtime API.
|
|
174
|
+
*/
|
|
175
|
+
export const HEADERS = {
|
|
176
|
+
'User-Agent': `Agents/JavaScript ${METADATA.version}`,
|
|
177
|
+
'X-OpenAI-Agents-SDK': `openai-agents-sdk.${METADATA.version}`,
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Browser websocket header
|
|
181
|
+
*/
|
|
182
|
+
export const WEBSOCKET_META = `openai-agents-sdk.${METADATA.version}`;
|
|
183
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"OACO,QAAQ;AAEf;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAc;IAChD,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;IAClC,MAAM,GAAG,GAAG,YAAY,CAAC,MAAM,CAAC;IAChC,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;IAClC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7B,KAAK,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IACxC,CAAC;IACD,OAAO,KAAK,CAAC,MAAM,CAAC;AACtB,CAAC;AAED;;;;GAIG;AACH,M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