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
|
import { Item } from "@/components/types";
export default function handleRealtimeEvent(
ev: any,
setItems: React.Dispatch<React.SetStateAction<Item[]>>
) {
// Helper function to create a new item with default fields
function createNewItem(base: Partial<Item>): Item {
return {
object: "realtime.item",
timestamp: new Date().toLocaleTimeString(),
...base,
} as Item;
}
// Helper function to update an existing item if found by id, or add a new one if not.
// We can also pass partial updates to reduce repetitive code.
function updateOrAddItem(id: string, updates: Partial<Item>): void {
setItems((prev) => {
const idx = prev.findIndex((m) => m.id === id);
if (idx >= 0) {
const updated = [...prev];
updated[idx] = { ...updated[idx], ...updates };
return updated;
} else {
return [...prev, createNewItem({ id, ...updates })];
}
});
}
const { type } = ev;
switch (type) {
case "session.created": {
// Starting a new session, clear all items
setItems([]);
break;
}
case "input_audio_buffer.speech_started": {
// Create a user message item with running status and placeholder content
const { item_id } = ev;
setItems((prev) => [
...prev,
createNewItem({
id: item_id,
type: "message",
role: "user",
content: [{ type: "text", text: "..." }],
status: "running",
}),
]);
break;
}
case "conversation.item.created": {
const { item } = ev;
if (item.type === "message") {
// A completed message from user or assistant
const updatedContent =
item.content && item.content.length > 0 ? item.content : [];
setItems((prev) => {
const idx = prev.findIndex((m) => m.id === item.id);
if (idx >= 0) {
const updated = [...prev];
updated[idx] = {
...updated[idx],
...item,
content: updatedContent,
status: "completed",
timestamp:
updated[idx].timestamp || new Date().toLocaleTimeString(),
};
return updated;
} else {
return [
...prev,
createNewItem({
...item,
content: updatedContent,
status: "completed",
}),
];
}
});
}
// NOTE: We no longer handle function_call items here.
// The handling of function_call items has been moved to the "response.output_item.done" event.
else if (item.type === "function_call_output") {
// Function call output item created
// Add the output item and mark the corresponding function_call as completed
// Also display in transcript as tool message with the response
setItems((prev) => {
const newItems = [
...prev,
createNewItem({
...item,
role: "tool",
content: [
{
type: "text",
text: `Function call response: ${item.output}`,
},
],
status: "completed",
}),
];
return newItems.map((m) =>
m.call_id === item.call_id && m.type === "function_call"
? { ...m, status: "completed" }
: m
);
});
}
break;
}
case "conversation.item.input_audio_transcription.completed": {
// Update the user message with the final transcript
const { item_id, transcript } = ev;
setItems((prev) =>
prev.map((m) =>
m.id === item_id && m.type === "message" && m.role === "user"
? {
...m,
content: [{ type: "text", text: transcript }],
status: "completed",
}
: m
)
);
break;
}
case "response.content_part.added": {
const { item_id, part, output_index } = ev;
// Append new content to the assistant message if output_index == 0
if (part.type === "text" && output_index === 0) {
setItems((prev) => {
const idx = prev.findIndex((m) => m.id === item_id);
if (idx >= 0) {
const updated = [...prev];
const existingContent = updated[idx].content || [];
updated[idx] = {
...updated[idx],
content: [
...existingContent,
{ type: part.type, text: part.text },
],
};
return updated;
} else {
// If the item doesn't exist yet, create it as a running assistant message
return [
...prev,
createNewItem({
id: item_id,
type: "message",
role: "assistant",
content: [{ type: part.type, text: part.text }],
status: "running",
}),
];
}
});
}
break;
}
case "response.audio_transcript.delta": {
// Streaming transcript text (assistant)
const { item_id, delta, output_index } = ev;
if (output_index === 0 && delta) {
setItems((prev) => {
const idx = prev.findIndex((m) => m.id === item_id);
if (idx >= 0) {
const updated = [...prev];
const existingContent = updated[idx].content || [];
updated[idx] = {
...updated[idx],
content: [...existingContent, { type: "text", text: delta }],
};
return updated;
} else {
return [
...prev,
createNewItem({
id: item_id,
type: "message",
role: "assistant",
content: [{ type: "text", text: delta }],
status: "running",
}),
];
}
});
}
break;
}
case "response.output_item.done": {
const { item } = ev;
if (item.type === "function_call") {
// A new function call item
// Display it in the transcript as an assistant message indicating a function is being requested
console.log("function_call", item);
setItems((prev) => [
...prev,
createNewItem({
...item,
role: "assistant",
content: [
{
type: "text",
text: `${item.name}(${JSON.stringify(
JSON.parse(item.arguments)
)})`,
},
],
status: "running",
}),
]);
}
break;
}
default:
break;
}
}
|