-
Notifications
You must be signed in to change notification settings - Fork 219
/
conversation.js
314 lines (305 loc) · 10.2 KB
/
conversation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { RealtimeUtils } from './utils.js';
/**
* Contains text and audio information about a item
* Can also be used as a delta
* @typedef {Object} ItemContentDeltaType
* @property {string} [text]
* @property {Int16Array} [audio]
* @property {string} [arguments]
* @property {string} [transcript]
*/
/**
* RealtimeConversation holds conversation history
* and performs event validation for RealtimeAPI
* @class
*/
export class RealtimeConversation {
defaultFrequency = 24_000; // 24,000 Hz
EventProcessors = {
'conversation.item.created': (event) => {
const { item } = event;
// deep copy values
const newItem = JSON.parse(JSON.stringify(item));
if (!this.itemLookup[newItem.id]) {
this.itemLookup[newItem.id] = newItem;
this.items.push(newItem);
}
newItem.formatted = {};
newItem.formatted.audio = new Int16Array(0);
newItem.formatted.text = '';
newItem.formatted.transcript = '';
// If we have a speech item, can populate audio
if (this.queuedSpeechItems[newItem.id]) {
newItem.formatted.audio = this.queuedSpeechItems[newItem.id].audio;
delete this.queuedSpeechItems[newItem.id]; // free up some memory
}
// Populate formatted text if it comes out on creation
if (newItem.content) {
const textContent = newItem.content.filter((c) =>
['text', 'input_text'].includes(c.type),
);
for (const content of textContent) {
newItem.formatted.text += content.text;
}
}
// If we have a transcript item, can pre-populate transcript
if (this.queuedTranscriptItems[newItem.id]) {
newItem.formatted.transcript = this.queuedTranscriptItems.transcript;
delete this.queuedTranscriptItems[newItem.id];
}
if (newItem.type === 'message') {
if (newItem.role === 'user') {
newItem.status = 'completed';
if (this.queuedInputAudio) {
newItem.formatted.audio = this.queuedInputAudio;
this.queuedInputAudio = null;
}
} else {
newItem.status = 'in_progress';
}
} else if (newItem.type === 'function_call') {
newItem.formatted.tool = {
type: 'function',
name: newItem.name,
call_id: newItem.call_id,
arguments: '',
};
newItem.status = 'in_progress';
} else if (newItem.type === 'function_call_output') {
newItem.status = 'completed';
newItem.formatted.output = newItem.output;
}
return { item: newItem, delta: null };
},
'conversation.item.truncated': (event) => {
const { item_id, audio_end_ms } = event;
const item = this.itemLookup[item_id];
if (!item) {
throw new Error(`item.truncated: Item "${item_id}" not found`);
}
const endIndex = Math.floor(
(audio_end_ms * this.defaultFrequency) / 1000,
);
item.formatted.transcript = '';
item.formatted.audio = item.formatted.audio.slice(0, endIndex);
return { item, delta: null };
},
'conversation.item.deleted': (event) => {
const { item_id } = event;
const item = this.itemLookup[item_id];
if (!item) {
throw new Error(`item.deleted: Item "${item_id}" not found`);
}
delete this.itemLookup[item.id];
const index = this.items.indexOf(item);
if (index > -1) {
this.items.splice(index, 1);
}
return { item, delta: null };
},
'conversation.item.input_audio_transcription.completed': (event) => {
const { item_id, content_index, transcript } = event;
const item = this.itemLookup[item_id];
// We use a single space to represent an empty transcript for .formatted values
// Otherwise it looks like no transcript provided
const formattedTranscript = transcript || ' ';
if (!item) {
// We can receive transcripts in VAD mode before item.created
// This happens specifically when audio is empty
this.queuedTranscriptItems[item_id] = {
transcript: formattedTranscript,
};
return { item: null, delta: null };
} else {
item.content[content_index].transcript = transcript;
item.formatted.transcript = formattedTranscript;
return { item, delta: { transcript } };
}
},
'input_audio_buffer.speech_started': (event) => {
const { item_id, audio_start_ms } = event;
this.queuedSpeechItems[item_id] = { audio_start_ms };
return { item: null, delta: null };
},
'input_audio_buffer.speech_stopped': (event, inputAudioBuffer) => {
const { item_id, audio_end_ms } = event;
if (!this.queuedSpeechItems[item_id]) {
this.queuedSpeechItems[item_id] = { audio_start_ms: audio_end_ms };
}
const speech = this.queuedSpeechItems[item_id];
speech.audio_end_ms = audio_end_ms;
if (inputAudioBuffer) {
const startIndex = Math.floor(
(speech.audio_start_ms * this.defaultFrequency) / 1000,
);
const endIndex = Math.floor(
(speech.audio_end_ms * this.defaultFrequency) / 1000,
);
speech.audio = inputAudioBuffer.slice(startIndex, endIndex);
}
return { item: null, delta: null };
},
'response.created': (event) => {
const { response } = event;
if (!this.responseLookup[response.id]) {
this.responseLookup[response.id] = response;
this.responses.push(response);
}
return { item: null, delta: null };
},
'response.output_item.added': (event) => {
const { response_id, item } = event;
const response = this.responseLookup[response_id];
if (!response) {
throw new Error(
`response.output_item.added: Response "${response_id}" not found`,
);
}
response.output.push(item.id);
return { item: null, delta: null };
},
'response.output_item.done': (event) => {
const { item } = event;
if (!item) {
throw new Error(`response.output_item.done: Missing "item"`);
}
const foundItem = this.itemLookup[item.id];
if (!foundItem) {
throw new Error(
`response.output_item.done: Item "${item.id}" not found`,
);
}
foundItem.status = item.status;
return { item: foundItem, delta: null };
},
'response.content_part.added': (event) => {
const { item_id, part } = event;
const item = this.itemLookup[item_id];
if (!item) {
throw new Error(
`response.content_part.added: Item "${item_id}" not found`,
);
}
item.content.push(part);
return { item, delta: null };
},
'response.audio_transcript.delta': (event) => {
const { item_id, content_index, delta } = event;
const item = this.itemLookup[item_id];
if (!item) {
throw new Error(
`response.audio_transcript.delta: Item "${item_id}" not found`,
);
}
item.content[content_index].transcript += delta;
item.formatted.transcript += delta;
return { item, delta: { transcript: delta } };
},
'response.audio.delta': (event) => {
const { item_id, content_index, delta } = event;
const item = this.itemLookup[item_id];
if (!item) {
throw new Error(`response.audio.delta: Item "${item_id}" not found`);
}
// This never gets renderered, we care about the file data instead
// item.content[content_index].audio += delta;
const arrayBuffer = RealtimeUtils.base64ToArrayBuffer(delta);
const appendValues = new Int16Array(arrayBuffer);
item.formatted.audio = RealtimeUtils.mergeInt16Arrays(
item.formatted.audio,
appendValues,
);
return { item, delta: { audio: appendValues } };
},
'response.text.delta': (event) => {
const { item_id, content_index, delta } = event;
const item = this.itemLookup[item_id];
if (!item) {
throw new Error(`response.text.delta: Item "${item_id}" not found`);
}
item.content[content_index].text += delta;
item.formatted.text += delta;
return { item, delta: { text: delta } };
},
'response.function_call_arguments.delta': (event) => {
const { item_id, delta } = event;
const item = this.itemLookup[item_id];
if (!item) {
throw new Error(
`response.function_call_arguments.delta: Item "${item_id}" not found`,
);
}
item.arguments += delta;
item.formatted.tool.arguments += delta;
return { item, delta: { arguments: delta } };
},
};
/**
* Create a new RealtimeConversation instance
* @returns {RealtimeConversation}
*/
constructor() {
this.clear();
}
/**
* Clears the conversation history and resets to default
* @returns {true}
*/
clear() {
this.itemLookup = {};
this.items = [];
this.responseLookup = {};
this.responses = [];
this.queuedSpeechItems = {};
this.queuedTranscriptItems = {};
this.queuedInputAudio = null;
return true;
}
/**
* Queue input audio for manual speech event
* @param {Int16Array} inputAudio
* @returns {Int16Array}
*/
queueInputAudio(inputAudio) {
this.queuedInputAudio = inputAudio;
return inputAudio;
}
/**
* Process an event from the WebSocket server and compose items
* @param {Object} event
* @param {...any} args
* @returns {item: import('./client.js').ItemType | null, delta: ItemContentDeltaType | null}
*/
processEvent(event, ...args) {
if (!event.event_id) {
console.error(event);
throw new Error(`Missing "event_id" on event`);
}
if (!event.type) {
console.error(event);
throw new Error(`Missing "type" on event`);
}
const eventProcessor = this.EventProcessors[event.type];
if (!eventProcessor) {
throw new Error(
`Missing conversation event processor for "${event.type}"`,
);
}
return eventProcessor.call(this, event, ...args);
}
/**
* Retrieves a item by id
* @param {string} id
* @returns {import('./client.js').ItemType}
*/
getItem(id) {
return this.itemLookup[id] || null;
}
/**
* Retrieves all items in the conversation
* @returns {import('./client.js').ItemType[]}
*/
getItems() {
return this.items.slice();
}
}