-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContextsDemo.java
More file actions
85 lines (71 loc) · 3.51 KB
/
ContextsDemo.java
File metadata and controls
85 lines (71 loc) · 3.51 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
/**
* Structured workflows with contexts and steps.
*
* Demonstrates a multi-step onboarding flow where each step has
* specific criteria, valid navigation targets, and function restrictions.
*/
import com.signalwire.sdk.agent.AgentBase;
import com.signalwire.sdk.swaig.FunctionResult;
import java.util.List;
import java.util.Map;
public class ContextsDemo {
public static void main(String[] args) throws Exception {
var agent = AgentBase.builder()
.name("onboarding-agent")
.route("/")
.port(3000)
.build();
agent.promptAddSection("Role",
"You are an onboarding assistant. Guide users through setup.");
// Define contexts and steps
var ctxBuilder = agent.defineContexts();
var defaultCtx = ctxBuilder.addContext("default");
// Step 1: Welcome
var welcomeStep = defaultCtx.addStep("welcome");
welcomeStep.setText(
"Greet the user and ask for their name and email.");
welcomeStep.setStepCriteria(
"User has provided their name and email address.");
welcomeStep.setValidSteps(List.of("next", "preferences"));
welcomeStep.setFunctions(List.of("collect_info"));
// Step 2: Preferences
var prefsStep = defaultCtx.addStep("preferences");
prefsStep.setText(
"Ask about the user's preferences and interests.");
prefsStep.setStepCriteria(
"User has shared at least 2 preferences.");
prefsStep.setValidSteps(List.of("next", "welcome"));
prefsStep.setFunctions(List.of("save_preference"));
// Step 3: Confirmation
var confirmStep = defaultCtx.addStep("confirmation");
confirmStep.setText(
"Summarize the collected information and confirm with the user.");
confirmStep.setStepCriteria(
"User has confirmed their information is correct.");
confirmStep.setFunctions(List.of("finalize_onboarding"));
// Define tools
agent.defineTool("collect_info", "Collect user info",
Map.of("type", "object", "properties", Map.of(
"name", Map.of("type", "string", "description", "User name"),
"email", Map.of("type", "string", "description", "User email")
), "required", List.of("name", "email")),
(toolArgs, raw) -> new FunctionResult(
"Collected: " + toolArgs.get("name") + " (" + toolArgs.get("email") + ")")
.updateGlobalData(Map.of(
"user_name", toolArgs.get("name"),
"user_email", toolArgs.get("email")
)));
agent.defineTool("save_preference", "Save a user preference",
Map.of("type", "object", "properties", Map.of(
"preference", Map.of("type", "string", "description", "User preference")
), "required", List.of("preference")),
(toolArgs, raw) -> new FunctionResult(
"Saved preference: " + toolArgs.get("preference")));
agent.defineTool("finalize_onboarding", "Complete the onboarding process",
Map.of("type", "object", "properties", Map.of()),
(toolArgs, raw) -> new FunctionResult(
"Onboarding complete! Welcome aboard."));
System.out.println("Starting onboarding agent on port 3000...");
agent.run();
}
}