-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_state.cpp
More file actions
50 lines (41 loc) · 1.68 KB
/
session_state.cpp
File metadata and controls
50 lines (41 loc) · 1.68 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
// Copyright (c) 2025 SignalWire — MIT License
// Session state management with global data and callbacks.
#include <signalwire/agent/agent_base.hpp>
using namespace signalwire;
using json = nlohmann::json;
int main() {
agent::AgentBase agent("stateful", "/stateful");
agent.prompt_add_section("Role", "You are a stateful assistant that remembers context.");
agent.prompt_add_section("Instructions", "", {
"Track conversation topics in global data",
"Use session tokens for secure tool calls"
});
agent.set_global_data({
{"session_type", "demo"},
{"interaction_count", 0}
});
// Summary callback
agent.on_summary([](const json& summary, const json& raw) {
(void)raw;
std::cout << "Conversation summary: " << summary.dump(2) << "\n";
});
// Debug event callback
agent.enable_debug_events(true);
agent.on_debug_event([](const json& event) {
std::cout << "Debug: " << event.dump() << "\n";
});
// Tool that updates state
agent.define_tool("update_topic", "Track the current topic",
{{"type", "object"}, {"properties", {
{"topic", {{"type", "string"}, {"description", "Current topic"}}}
}}},
[](const json& args, const json& raw) -> swaig::FunctionResult {
(void)raw;
std::string topic = args.value("topic", "general");
return swaig::FunctionResult("Topic updated to: " + topic)
.update_global_data({{"current_topic", topic}})
.set_metadata({{"last_topic_update", topic}});
}, true /* secure */);
std::cout << "Session state demo at http://0.0.0.0:3000/stateful\n";
agent.run();
}