-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.ts
More file actions
345 lines (299 loc) · 10.6 KB
/
main.ts
File metadata and controls
345 lines (299 loc) · 10.6 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import "dotenv/config";
import { task } from "@renderinc/sdk/workflows";
import OpenAI from "openai";
import type { ChatCompletionMessageParam, ChatCompletionTool } from "openai/resources/chat/completions";
const retry = {
maxRetries: 3,
waitDurationMs: 2000,
backoffScaling: 2.0,
};
function createOpenAIClient(): OpenAI {
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
throw new Error(
"OPENAI_API_KEY environment variable not set. " +
"Please set it in your Render environment variables.",
);
}
return new OpenAI({ apiKey });
}
// ---- Tool Functions ----
const getOrderStatus = task(
{ name: "getOrderStatus", retry },
function getOrderStatus(orderId: string) {
console.log(`[TOOL] Looking up order status for: ${orderId}`);
const mockOrders: { [key: string]: { status: string; tracking: string | null; eta: string } } = {
"ORD-001": { status: "shipped", tracking: "1Z999AA1234567890", eta: "2024-10-15" },
"ORD-002": { status: "processing", tracking: null, eta: "2024-10-12" },
"ORD-003": { status: "delivered", tracking: "1Z999AA9876543210", eta: "2024-10-08" },
};
if (orderId in mockOrders) {
const order = mockOrders[orderId];
console.log(`[TOOL] Order ${orderId} found: ${order.status}`);
return { success: true, order_id: orderId, ...order };
}
console.warn(`[TOOL] Order ${orderId} not found`);
return { success: false, order_id: orderId, error: "Order not found" };
},
);
// No retry: processing a refund is non-idempotent
const processRefund = task(
{ name: "processRefund" },
function processRefund(orderId: string, reason: string) {
console.log(`[TOOL] Processing refund for order: ${orderId}`);
console.log(`[TOOL] Refund reason: ${reason}`);
const refundId = `REF-${orderId}-${new Date().toISOString().replace(/[-:T.]/g, "").slice(0, 14)}`;
const result = {
success: true,
refund_id: refundId,
order_id: orderId,
reason,
amount: 99.99,
processed_at: new Date().toISOString(),
};
console.log(`[TOOL] Refund processed: ${refundId}`);
return result;
},
);
const searchKnowledgeBase = task(
{ name: "searchKnowledgeBase", retry },
function searchKnowledgeBase(query: string) {
console.log(`[TOOL] Searching knowledge base: ${query}`);
const knowledge: { [key: string]: { title: string; content: string } } = {
shipping: {
title: "Shipping Policy",
content:
"We offer free shipping on orders over $50. Standard shipping takes 3-5 business days. Express shipping is available for $15 and takes 1-2 business days.",
},
returns: {
title: "Return Policy",
content:
"We accept returns within 30 days of purchase. Items must be unused and in original packaging. Refunds are processed within 5-7 business days.",
},
warranty: {
title: "Warranty Information",
content:
"All products come with a 1-year manufacturer warranty. Extended warranties are available for purchase.",
},
};
const queryLower = query.toLowerCase();
const matches = Object.entries(knowledge)
.filter(
([key, article]) =>
queryLower.includes(key) ||
queryLower.split(" ").some((word) => article.content.toLowerCase().includes(word)),
)
.map(([, article]) => article);
console.log(`[TOOL] Found ${matches.length} knowledge base articles`);
return { success: true, query, results: matches, count: matches.length };
},
);
// ---- Agent Tasks ----
const tools: ChatCompletionTool[] = [
{
type: "function",
function: {
name: "get_order_status",
description: "Look up the status of a customer order by order ID",
parameters: {
type: "object",
properties: {
order_id: { type: "string", description: "The order ID (e.g., ORD-001)" },
},
required: ["order_id"],
},
},
},
{
type: "function",
function: {
name: "process_refund",
description: "Process a refund for an order",
parameters: {
type: "object",
properties: {
order_id: { type: "string", description: "The order ID to refund" },
reason: { type: "string", description: "Reason for the refund" },
},
required: ["order_id", "reason"],
},
},
},
{
type: "function",
function: {
name: "search_knowledge_base",
description: "Search the knowledge base for help articles and information",
parameters: {
type: "object",
properties: {
query: { type: "string", description: "The search query" },
},
required: ["query"],
},
},
},
];
const callLlmWithTools = task(
{ name: "callLlmWithTools", retry },
async function callLlmWithTools(
messages: ChatCompletionMessageParam[],
toolDefs: ChatCompletionTool[],
model: string = "gpt-5.4",
) {
console.log(`[AGENT] Calling ${model} with ${toolDefs.length} tools available`);
const client = createOpenAIClient();
const response = await client.chat.completions.create({
model,
messages,
tools: toolDefs,
tool_choice: "auto",
});
const message = response.choices[0].message;
const result: {
content: string | null;
tool_calls: { id: string; type: string; function: { name: string; arguments: string } }[];
} = { content: message.content, tool_calls: [] };
if (message.tool_calls) {
result.tool_calls = message.tool_calls.map((tc) => ({
id: tc.id,
type: "function",
function: { name: tc.function.name, arguments: tc.function.arguments },
}));
console.log(`[AGENT] Model requested ${result.tool_calls.length} tool calls`);
}
return result;
},
);
const executeTool = task(
{ name: "executeTool", retry },
async function executeTool(toolName: string, args: { [key: string]: string }) {
console.log(`[AGENT] Executing tool: ${toolName}`);
try {
switch (toolName) {
case "get_order_status":
return await getOrderStatus(args.order_id);
case "process_refund":
return await processRefund(args.order_id, args.reason);
case "search_knowledge_base":
return await searchKnowledgeBase(args.query);
default:
console.error(`[AGENT] Unknown tool: ${toolName}`);
return { error: `Unknown tool: ${toolName}` };
}
} catch (error) {
console.error(`[AGENT] Tool execution failed: ${error}`);
return { error: String(error) };
}
},
);
const agentTurn = task(
{ name: "agentTurn", retry },
async function agentTurn(
userMessage: string,
conversationHistory: ChatCompletionMessageParam[] = [],
) {
console.log("[AGENT TURN] Starting agent turn");
if (typeof userMessage !== "string") {
return {
success: false,
error: `user_message must be a string, got ${typeof userMessage}`,
response: "I'm sorry, there was an error processing your message. Please try again.",
};
}
const systemMessage: ChatCompletionMessageParam = {
role: "system",
content:
"You are a helpful customer support agent. You can look up order " +
"status, process refunds, and search the knowledge base for information. " +
"Be polite, professional, and helpful. Use tools when necessary to " +
"assist the customer.",
};
const messages: ChatCompletionMessageParam[] = [
systemMessage,
...conversationHistory,
{ role: "user", content: userMessage },
];
const llmResponse = await callLlmWithTools(messages, tools);
if (!llmResponse.tool_calls.length) {
console.log("[AGENT TURN] No tool calls, returning response");
return {
response: llmResponse.content,
conversation_history: [
...conversationHistory,
{ role: "user" as const, content: userMessage },
{ role: "assistant" as const, content: llmResponse.content },
],
tool_calls: [],
};
}
console.log(`[AGENT TURN] Executing ${llmResponse.tool_calls.length} tool calls`);
const toolResults: { tool: string; result: unknown }[] = [];
for (const toolCall of llmResponse.tool_calls) {
const result = await executeTool(
toolCall.function.name,
JSON.parse(toolCall.function.arguments),
);
toolResults.push({ tool: toolCall.function.name, result });
}
const toolMessages: ChatCompletionMessageParam[] = llmResponse.tool_calls.map((tc, i) => ({
role: "tool" as const,
tool_call_id: tc.id,
content: JSON.stringify(toolResults[i].result),
}));
const finalMessages: ChatCompletionMessageParam[] = [
...messages,
{
role: "assistant" as const,
content: llmResponse.content,
tool_calls: llmResponse.tool_calls.map((tc) => ({
id: tc.id,
type: "function" as const,
function: { name: tc.function.name, arguments: tc.function.arguments },
})),
},
...toolMessages,
];
const finalResponse = await callLlmWithTools(finalMessages, tools);
console.log("[AGENT TURN] Agent turn complete");
return {
response: finalResponse.content,
conversation_history: [
...conversationHistory,
{ role: "user" as const, content: userMessage },
{ role: "assistant" as const, content: finalResponse.content },
],
tool_calls: toolResults,
};
},
);
// Root task: multi-turn conversation
task(
{ name: "multiTurnConversation", retry, timeoutSeconds: 300 },
async function multiTurnConversation(...messages: string[]) {
console.log("=".repeat(80));
console.log(`[CONVERSATION] Starting multi-turn conversation with ${messages.length} messages`);
console.log("=".repeat(80));
let conversationHistory: ChatCompletionMessageParam[] = [];
const responses: { turn: number; user: string; assistant: string | null; tool_calls: unknown[] }[] = [];
for (let i = 0; i < messages.length; i++) {
console.log(`[CONVERSATION] Turn ${i + 1}/${messages.length}`);
const turnResult = await agentTurn(messages[i], conversationHistory);
responses.push({
turn: i + 1,
user: messages[i],
assistant: turnResult.response,
tool_calls: turnResult.tool_calls ?? [],
});
conversationHistory = turnResult.conversation_history ?? [];
}
console.log("=".repeat(80));
console.log("[CONVERSATION] Multi-turn conversation complete");
console.log("=".repeat(80));
return {
turns: responses,
total_turns: responses.length,
conversation_history: conversationHistory,
};
},
);