-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleDynamicAgent.java
More file actions
64 lines (53 loc) · 2.35 KB
/
Copy pathSimpleDynamicAgent.java
File metadata and controls
64 lines (53 loc) · 2.35 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
/**
* Agent with per-request dynamic configuration.
*
* The dynamic config callback receives query params, POST body, and headers,
* allowing the agent to customize its behavior per-request (e.g., multi-tenancy).
*/
import com.signalwire.sdk.agent.AgentBase;
import com.signalwire.sdk.swaig.FunctionResult;
import java.util.List;
import java.util.Map;
public class SimpleDynamicAgent {
public static void main(String[] args) throws Exception {
var agent = AgentBase.builder()
.name("dynamic-agent")
.route("/")
.port(3000)
.build();
agent.promptAddSection("Role",
"You are a customizable assistant.");
// Dynamic config callback: customize per request
agent.setDynamicConfigCallback((queryParams, bodyParams, headers, configAgent) -> {
// Customize based on query params
String tenant = queryParams.getOrDefault("tenant", "default");
String language = queryParams.getOrDefault("lang", "en");
configAgent.promptAddSection("Tenant",
"You are serving tenant: " + tenant);
if ("es".equals(language)) {
configAgent.addLanguage("Spanish", "es", "es-ES-Standard-A");
configAgent.promptAddSection("Language",
"Respond in Spanish.");
}
// Customize based on headers
List<String> customHeader = headers.getOrDefault("X-Custom-Config", List.of());
if (!customHeader.isEmpty()) {
configAgent.promptAddSection("Custom",
"Custom config: " + customHeader.get(0));
}
});
agent.defineTool("greet", "Greet the user by name",
Map.of("type", "object",
"properties", Map.of(
"name", Map.of("type", "string", "description", "User name")
),
"required", List.of("name")),
(toolArgs, raw) -> {
String name = (String) toolArgs.get("name");
return new FunctionResult("Hello, " + name + "! Welcome.");
});
System.out.println("Starting dynamic agent on port 3000...");
System.out.println("Try: ?tenant=acme&lang=es");
agent.run();
}
}