-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
277 lines (232 loc) · 6.91 KB
/
Copy pathcli.ts
File metadata and controls
277 lines (232 loc) · 6.91 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
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
import { isCancel, log, outro, text } from "@clack/prompts";
import { type } from "arktype";
import ky from "ky";
import { OpenAI } from "openai";
import yoctoSpinner from "yocto-spinner";
import { tools } from "./tools";
type ResponseInput = OpenAI.Responses.ResponseInput;
type ResponseInputItem = OpenAI.Responses.ResponseInputItem;
type ResponseOutputItem = OpenAI.Responses.ResponseOutputItem;
type CarryForwardItem = Extract<
ResponseOutputItem,
{ type: "message" | "function_call" | "reasoning" }
>;
type ToolCallItem = Extract<ResponseOutputItem, { type: "function_call" }> & {
name: "get_weather" | "research_topic";
};
type Message = {
role: "assistant" | "user";
content: string;
};
const model = "gpt-4.1-nano" as const;
const api = ky.create({
baseUrl: "https://elyos-interview-907656039105.europe-west2.run.app",
headers: {
"X-API-Key": process.env["ELYOS_API_KEY"] ?? "",
},
retry: {
backoffLimit: 1000,
jitter: true,
limit: 3,
retryOnTimeout: true,
},
});
const AssistantMessage = (message: string): Message => ({
role: "assistant",
content: message,
});
const UserMessage = (message: string): Message => ({
role: "user",
content: message,
});
const OpenAIApiKey = type(
"string > 20",
"=>",
type(/^sk-(?:proj-)?[a-z0-9_-]{20,}$/i),
);
const isCarryForwardItem = (
item: ResponseOutputItem,
): item is CarryForwardItem =>
item.type === "message" ||
item.type === "function_call" ||
item.type === "reasoning";
const isToolCallItem = (item: ResponseOutputItem): item is ToolCallItem =>
item.type === "function_call" &&
(item.name === "get_weather" || item.name === "research_topic");
const getInput = async () => {
const value = await text({ message: "Ask anything" });
if (isCancel(value)) return false;
return value.trim();
};
const isExit = (message: string | false): message is false => {
if (message === false) return true;
return ["", "quit", "exit", "q"].includes(message);
};
const callApi = async (
endpoint: string,
searchParams: Record<string, string>,
controller: AbortController,
) => {
const callApi = async () => {
try {
return await api
.get(endpoint, { searchParams, signal: controller.signal })
.text();
} catch (e) {
const error = type({ message: "string" }).assert(e);
return error.message;
}
};
let response = "";
for (let i = 0; i < 3; i++) {
response = (await callApi()).trim();
if (!response || response === "{}")
log.warn("Received empty response. Will retry");
else return response;
}
return response;
};
const JsonArguments = type("string").pipe((v) => JSON.parse(v));
const WeatherArguments = JsonArguments.pipe(type({ location: "string" }));
const ResearchArguments = JsonArguments.pipe(type({ topic: "string" }));
const runWeather = async (tool: ToolCallItem, controller: AbortController) => {
const args = WeatherArguments.assert(tool.arguments);
return callApi("/weather", args, controller);
};
const runResearch = async (tool: ToolCallItem, controller: AbortController) => {
const args = ResearchArguments.assert(tool.arguments);
log.info(`Researching ${args.topic}`);
return callApi("/research", args, controller);
};
const runTool = async (tool: ToolCallItem) => {
const s = yoctoSpinner({ handleSignals: false });
const controller = new AbortController();
process.on("SIGINT", () => {
s.stop("Cancelled");
controller.abort();
});
if (tool.name === "get_weather") {
s.start("Calling weather tool");
const output = await runWeather(tool, controller);
s.stop("Retrieved weather");
return output;
}
if (tool.name === "research_topic") {
s.start("Calling research tool");
const output = await runResearch(tool, controller);
s.stop("Retrieved research");
return output;
}
const unreachable: never = tool.name;
throw unreachable;
};
const streamOpenAIResponse = async (
openai: OpenAI,
input: ResponseInput,
): Promise<{
outputText: string;
output: ResponseOutputItem[];
cancelled: boolean;
}> => {
const controller = new AbortController();
let cancelled = false;
const onSigint = () => {
cancelled = true;
controller.abort();
process.stdout.write("\nCancelled\n");
};
process.once("SIGINT", onSigint);
let outputText = "";
let output: ResponseOutputItem[] = [];
try {
const stream = await openai.responses.create(
{
model,
input,
tools,
stream: true,
},
{ signal: controller.signal },
);
for await (const event of stream) {
switch (event.type) {
case "response.output_text.delta": {
process.stdout.write(event.delta);
outputText += event.delta;
break;
}
case "response.completed": {
output = event.response.output;
break;
}
case "response.failed": {
log.error(event.response.error?.message ?? "OpenAI response failed");
break;
}
default:
break;
}
}
} finally {
process.off("SIGINT", onSigint);
}
return { outputText, output, cancelled };
};
const processInput = async (
openai: OpenAI,
prompt: string,
conversation: ResponseInput,
) => {
const currentInput: ResponseInput = [...conversation, UserMessage(prompt)];
const firstResponse = await streamOpenAIResponse(openai, currentInput);
if (firstResponse.cancelled) return firstResponse.outputText;
const inputs = firstResponse.output.filter(isCarryForwardItem);
const toolCalls = firstResponse.output.filter(isToolCallItem);
const toolOutputs: ResponseInputItem[] = [];
if (!toolCalls.length) {
process.stdout.write("\n");
return firstResponse.outputText;
}
for (const item of toolCalls) {
const response = await runTool(item);
toolOutputs.push({
type: "function_call_output",
call_id: item.call_id,
output: response,
});
}
const finalResponse = await streamOpenAIResponse(openai, [
...currentInput,
...inputs,
...toolOutputs,
]);
process.stdout.write("\n");
return finalResponse.outputText;
};
const main = async () => {
const elyosApiKey = type("string > 0")(process.env["ELYOS_API_KEY"]);
if (elyosApiKey instanceof type.errors) {
log.error(`ELYOS_API_KEY environment variable ${elyosApiKey.summary}`);
process.exitCode = 1;
return;
}
const apiKey = OpenAIApiKey(process.env["OPENAI_API_KEY"]);
if (apiKey instanceof type.errors) {
log.error(`OPENAI_API_KEY environment variable ${apiKey.summary}`);
process.exitCode = 1;
return;
}
const openai = new OpenAI({ apiKey });
const conversation: ResponseInput = [];
while (true) {
const prompt = await getInput();
if (isExit(prompt)) {
outro("Goodbye!");
return;
}
const response = await processInput(openai, prompt, conversation);
conversation.push(UserMessage(prompt));
conversation.push(AssistantMessage(response));
}
};
void main();