-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-realtime-tools.ts
More file actions
226 lines (211 loc) · 6.85 KB
/
Copy pathai-realtime-tools.ts
File metadata and controls
226 lines (211 loc) · 6.85 KB
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
import {
executeBuiltinAiTool,
type AiCitationSource,
type AiWebSearchResult,
} from "./ai-tools";
import type { DataState, LocationValue } from "./live-state";
import type { McpServerConfig } from "./mcp-servers";
import type { PhoneLocale } from "./phone-types";
type FunctionCall = {
readonly callId: string;
readonly name: string;
readonly arguments: string;
};
export type AiToolKind = "time" | "location" | "web-search" | "mcp" | "generic";
export type AiActiveTool = {
readonly id: string;
readonly kind: AiToolKind;
readonly displayName?: string;
};
export type AiToolLifecycleUpdate =
| { readonly activeTool: AiActiveTool }
| { readonly clearId: string };
export type AiMcpApproval = {
readonly id: string;
readonly serverLabel: string;
readonly serverName: string;
readonly toolName: string;
readonly argumentsSummary: string;
};
function record(value: unknown): Record<string, unknown> | undefined {
return typeof value === "object" && value !== null
? value as Record<string, unknown>
: undefined;
}
function boundedIdentifier(value: unknown, maximum = 128): string | undefined {
return typeof value === "string" && value.length > 0
? value.replace(/[\u0000-\u001f\u007f]/g, "").slice(0, maximum)
: undefined;
}
function builtinToolKind(name: string): AiToolKind {
if (name === "get_current_time") return "time";
if (name === "get_current_location") return "location";
if (name === "search_web") return "web-search";
return "generic";
}
export function mcpToolLifecycleUpdate(
event: unknown,
): AiToolLifecycleUpdate | undefined {
const envelope = record(event);
if (!envelope) return undefined;
const type = envelope?.type;
if (type === "response.mcp_call.in_progress") {
const id = boundedIdentifier(envelope.item_id ?? envelope.call_id);
if (!id) return undefined;
return {
activeTool: {
id,
kind: "mcp",
displayName: boundedIdentifier(envelope.name, 96),
},
};
}
if (type === "response.output_item.done") {
const item = record(envelope.item);
const id = item?.type === "mcp_call"
? boundedIdentifier(item.id ?? item.call_id)
: undefined;
return id ? { clearId: id } : undefined;
}
if (type === "response.mcp_call.failed") {
const id = boundedIdentifier(envelope.item_id ?? envelope.call_id);
return id ? { clearId: id } : undefined;
}
return undefined;
}
export function responseFunctionCalls(event: unknown): readonly FunctionCall[] {
const response = record(record(event)?.response);
if (!Array.isArray(response?.output)) return [];
return response.output.flatMap((value) => {
const item = record(value);
return item?.type === "function_call"
&& typeof item.call_id === "string"
&& typeof item.name === "string"
&& typeof item.arguments === "string"
? [{
callId: item.call_id.slice(0, 128),
name: item.name.slice(0, 96),
arguments: item.arguments,
}]
: [];
});
}
export function mcpApprovalRequest(
event: unknown,
servers: readonly McpServerConfig[],
): AiMcpApproval | undefined {
const envelope = record(event);
if (envelope?.type !== "conversation.item.done") return undefined;
const item = record(envelope.item);
if (
item?.type !== "mcp_approval_request"
|| typeof item.id !== "string"
|| typeof item.server_label !== "string"
|| typeof item.name !== "string"
) return undefined;
const match = servers.find(({ id }) => `mcp_${id}` === item.server_label);
return {
id: item.id.slice(0, 128),
serverLabel: item.server_label.slice(0, 80),
serverName: match?.name ?? item.server_label.slice(0, 80),
toolName: item.name.slice(0, 96),
argumentsSummary: typeof item.arguments === "string"
? item.arguments.replace(/\s+/g, " ").trim().slice(0, 180)
: "{}",
};
}
export function mcpApprovalResponse(id: string, approve: boolean) {
return {
type: "conversation.item.create" as const,
item: {
type: "mcp_approval_response" as const,
approval_request_id: id,
approve,
},
};
}
export function createAiRealtimeToolRunner(options: {
readonly locale: PhoneLocale;
readonly now: () => Date;
readonly getLocation: () => DataState<LocationValue>;
readonly searchWeb: (
query: string,
locale: PhoneLocale,
signal: AbortSignal,
) => Promise<AiWebSearchResult>;
readonly send: (event: unknown) => void;
readonly onSources: (sources: readonly AiCitationSource[]) => void;
readonly onSearchUsage: (usage: {
readonly model: string;
readonly inputTokens: number;
readonly cachedInputTokens: number;
readonly outputTokens: number;
readonly webSearchCalls: number;
}) => void;
readonly onActiveTool: (activeTool: AiActiveTool | undefined) => void;
}) {
const completed = new Set<string>();
const activeCalls = new Map<string, AiActiveTool>();
let active: AbortController | undefined;
const publishActive = () => {
options.onActiveTool(activeCalls.values().next().value);
};
return {
handle(event: unknown, beforeStart?: () => void): boolean {
const calls = responseFunctionCalls(event).filter(({ callId }) => (
!completed.has(callId)
));
if (calls.length === 0 || active) return false;
const controller = new AbortController();
active = controller;
for (const call of calls) completed.add(call.callId);
beforeStart?.();
void Promise.all(calls.map(async (call) => {
activeCalls.set(call.callId, {
id: call.callId,
kind: builtinToolKind(call.name),
});
publishActive();
try {
const result = await executeBuiltinAiTool(call.name, call.arguments, {
locale: options.locale,
now: options.now,
getLocation: options.getLocation,
searchWeb: (query, locale) => options.searchWeb(
query,
locale,
controller.signal,
),
});
if (controller.signal.aborted) return;
if (result.ok && result.sources?.length) {
options.onSources(result.sources);
}
if (result.ok && result.usage) options.onSearchUsage(result.usage);
options.send({
type: "conversation.item.create",
item: {
type: "function_call_output",
call_id: call.callId,
output: JSON.stringify(result),
},
});
} finally {
activeCalls.delete(call.callId);
publishActive();
}
})).then(() => {
if (!controller.signal.aborted) options.send({ type: "response.create" });
}).finally(() => {
if (active === controller) active = undefined;
});
return true;
},
cancel() {
active?.abort();
active = undefined;
activeCalls.clear();
publishActive();
},
};
}