-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession_and_state_demo.rs
More file actions
106 lines (95 loc) · 3.41 KB
/
Copy pathsession_and_state_demo.rs
File metadata and controls
106 lines (95 loc) · 3.41 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
// Copyright (c) 2025 SignalWire
// SPDX-License-Identifier: MIT
//
//! Session and State Demo — `on_summary`, global data, post-prompt features.
use serde_json::json;
use signalwire::agent::{AgentBase, AgentOptions};
use signalwire::swaig::FunctionResult;
fn main() {
let mut agent = AgentBase::new(AgentOptions {
name: "session-state-demo".to_string(),
route: Some("/session-state-demo".to_string()),
..AgentOptions::new("session-state-demo")
});
agent.add_language("English", "en-US", "inworld.Mark");
agent.prompt_add_section(
"Role",
"You are a customer service agent that tracks session state.",
vec![],
);
agent.prompt_add_section(
"Instructions",
"",
vec![
"Greet the caller and ask how you can help",
"Use update_customer_info to record information the caller provides",
"Use get_session_info to check what information has been collected",
"Use end_session when the caller is done",
],
);
// Seed session with default global data
agent.set_global_data(json!({
"status": "active",
"customer_name": "",
"issue_type": "",
}));
// Post-prompt for summary generation
agent.set_post_prompt(
"Summarize: customer name, issue type, resolution status, and any follow-up needed.",
);
// Summary callback
agent.on_summary(Box::new(|summary, raw_data, _headers| {
println!("=== Call Summary ===");
println!("{summary}");
println!("Raw data: {raw_data}");
println!("====================");
}));
// Tool: update customer info
agent.define_tool(
"update_customer_info",
"Save customer information to the session",
json!({
"field": {"type": "string", "description": "Field name (customer_name, issue_type, etc.)"},
"value": {"type": "string", "description": "Field value"}
}),
Box::new(|args, _raw| {
let field = args.get("field").and_then(|v| v.as_str()).unwrap_or("");
let value = args.get("value").and_then(|v| v.as_str()).unwrap_or("");
let mut result = FunctionResult::with_response(
&format!("Saved {field} = {value}.")
);
result.add_action(json!({"update_global_data": {field: value}}));
result
}),
false,
);
// Tool: get session info
agent.define_tool(
"get_session_info",
"Get all information collected in this session",
json!({}),
Box::new(|_args, raw_data| {
let global_data = raw_data.get("global_data").map_or_else(
|| "{}".to_string(),
|v| serde_json::to_string_pretty(v).unwrap_or_default(),
);
FunctionResult::with_response(&format!("Session data: {global_data}"))
}),
false,
);
// Tool: end session
agent.define_tool(
"end_session",
"End the current session and say goodbye",
json!({}),
Box::new(|_args, _raw| {
let mut result = FunctionResult::with_response("Thank you for calling. Goodbye!");
result.set_post_process(true);
result.add_action(json!({"update_global_data": {"status": "closed"}}));
result.add_action(json!({"hangup": {}}));
result
}),
false,
);
agent.run();
}