-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-state.ts
More file actions
83 lines (73 loc) · 2.62 KB
/
Copy pathsession-state.ts
File metadata and controls
83 lines (73 loc) · 2.62 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
/**
* Session State Example
*
* Demonstrates onSummary callback for post-call processing,
* global data for per-call context, and post-prompt instructions.
* Run: npx tsx examples/session-state.ts
*/
import { AgentBase, FunctionResult } from '../src/index.js';
import type { AgentOptions, PostPrompt, PostPromptData } from '../src/index.js';
class OrderAgent extends AgentBase {
static override PROMPT_SECTIONS = [
{
title: 'Role',
body: 'You are an order status assistant. Help callers check the status of their orders and update shipping preferences.',
},
{
title: 'Rules',
bullets: [
'Always ask for the order number first.',
'Use the lookup_order tool to find order details.',
'Confirm changes before applying them.',
],
},
];
constructor(opts: AgentOptions) {
super(opts);
this.defineTools();
}
protected override defineTools(): void {
this.defineTool({
name: 'lookup_order',
description: 'Look up an order by its order number',
parameters: {
order_number: { type: 'string', description: 'The order number (e.g., ORD-12345)' },
},
required: ['order_number'],
handler: (args, rawData) => {
// args.order_number: string (required); rawData is the SWAIG webhook body
// (SwaigRequestData) — call_id is a typed top-level field.
this.updateGlobalData({
last_order_number: args.order_number,
call_id: rawData.call_id,
});
return new FunctionResult(
`Order ${args.order_number}: Status: Shipped, ETA: 2 business days, Carrier: FedEx`,
);
},
});
}
override async onSummary(summary: PostPromptData | null, rawData: PostPrompt): Promise<void> {
console.log('=== Call Summary ===');
// call_id is a top-level field on the post-prompt payload (PostPrompt).
console.log('Call ID:', rawData.call_id);
console.log('Summary:', JSON.stringify(summary, null, 2));
console.log('===================');
}
}
export const agent = new OrderAgent({
name: 'order-agent',
route: '/',
basicAuth: [
process.env['SWML_BASIC_AUTH_USER'] ?? 'user',
process.env['SWML_BASIC_AUTH_PASSWORD'] ?? 'pass',
],
});
// Global data available to the AI throughout the call
agent.setGlobalData({ company: 'Acme Corp', support_hours: '9am-5pm EST' });
// Post-prompt instructs the AI to generate a structured summary
agent.setPostPrompt(
'Summarize this call as JSON: { caller_intent, order_numbers_discussed, actions_taken, follow_up_needed }',
);
agent.addLanguage({ name: 'English', code: 'en-US', voice: 'rachel' });
agent.serve();