@signalwire/sdk 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +27 -0
- package/.github/workflows/test.yml +17 -0
- package/CLAUDE.md +112 -0
- package/README.md +254 -0
- package/docs/MIGRATION-2.0.md +81 -0
- package/docs/agent-guide.md +869 -0
- package/docs/agent_guide.md +1026 -0
- package/docs/api-reference.md +2420 -0
- package/docs/api_reference.md +1301 -0
- package/docs/architecture.md +843 -0
- package/docs/cli-guide.md +579 -0
- package/docs/cli_guide.md +448 -0
- package/docs/cloud_functions_guide.md +474 -0
- package/docs/configuration.md +422 -0
- package/docs/contexts-guide.md +787 -0
- package/docs/contexts_guide.md +511 -0
- package/docs/datamap-guide.md +1141 -0
- package/docs/datamap_guide.md +595 -0
- package/docs/llm_parameters.md +239 -0
- package/docs/mcp_gateway_reference.md +648 -0
- package/docs/mcp_integration.md +108 -0
- package/docs/prefabs-guide.md +800 -0
- package/docs/sdk_features.md +477 -0
- package/docs/security.md +436 -0
- package/docs/serverless-guide.md +513 -0
- package/docs/skills-guide.md +857 -0
- package/docs/skills-system.md +301 -0
- package/docs/skills_parameter_schema.md +392 -0
- package/docs/skills_system.md +467 -0
- package/docs/swaig-reference.md +1627 -0
- package/docs/swaig_reference.md +910 -0
- package/docs/swml_service_guide.md +600 -0
- package/docs/third_party_skills.md +533 -0
- package/docs/web_service.md +677 -0
- package/examples/README.md +131 -0
- package/examples/advanced-datamap.ts +100 -0
- package/examples/advanced-dynamic-config.ts +70 -0
- package/examples/auto-vivified.ts +94 -0
- package/examples/call-flow.ts +63 -0
- package/examples/comprehensive-dynamic.ts +125 -0
- package/examples/contexts-steps.ts +65 -0
- package/examples/custom-path.ts +45 -0
- package/examples/datamap-tools.ts +55 -0
- package/examples/datasphere-multi-instance.ts +63 -0
- package/examples/datasphere-serverless-env.ts +68 -0
- package/examples/datasphere-webhook-env.ts +66 -0
- package/examples/datasphere.ts +37 -0
- package/examples/declarative.ts +100 -0
- package/examples/dynamic-config.ts +54 -0
- package/examples/dynamic-info-gatherer.ts +69 -0
- package/examples/dynamic-swml-service.ts +67 -0
- package/examples/gather-info.ts +71 -0
- package/examples/gather-per-question-functions-demo.ts +140 -0
- package/examples/joke-agent.ts +34 -0
- package/examples/kubernetes-agent.ts +45 -0
- package/examples/llm-params.ts +69 -0
- package/examples/mcp-agent.ts +84 -0
- package/examples/mcp-gateway.ts +35 -0
- package/examples/multi-agent.ts +73 -0
- package/examples/multi-endpoint.ts +79 -0
- package/examples/pom-prompt.ts +71 -0
- package/examples/prefab-concierge.ts +47 -0
- package/examples/prefab-faq.ts +54 -0
- package/examples/prefab-info-gatherer.ts +33 -0
- package/examples/prefab-receptionist.ts +36 -0
- package/examples/prefab-survey.ts +68 -0
- package/examples/record-call.ts +67 -0
- package/examples/relay-demo.ts +33 -0
- package/examples/room-and-sip.ts +94 -0
- package/examples/serverless-lambda.ts +35 -0
- package/examples/session-state.ts +83 -0
- package/examples/simple-agent.ts +36 -0
- package/examples/simple-static.ts +29 -0
- package/examples/skills-demo.ts +38 -0
- package/examples/step-function-inheritance-demo.ts +124 -0
- package/examples/swaig-features.ts +187 -0
- package/examples/swml-service-routing.ts +66 -0
- package/examples/swml-service.ts +38 -0
- package/examples/tap.ts +68 -0
- package/examples/verb-methods.ts +45 -0
- package/examples/web-search-multi-instance.ts +72 -0
- package/examples/web-search.ts +38 -0
- package/examples/wikipedia.ts +36 -0
- package/livewire/README.md +140 -0
- package/livewire/docs/migration-guide.md +228 -0
- package/livewire/examples/livewire-basic-agent.ts +74 -0
- package/livewire/examples/livewire-handoff.ts +197 -0
- package/livewire/examples/livewire-multi-tool.ts +117 -0
- package/package.json +38 -0
- package/relay/README.md +70 -0
- package/relay/RELAY_IMPLEMENTATION_GUIDE.md +1257 -0
- package/relay/docs/call-methods.md +474 -0
- package/relay/docs/client-reference.md +142 -0
- package/relay/docs/events.md +134 -0
- package/relay/docs/getting-started.md +134 -0
- package/relay/docs/guide.md +306 -0
- package/relay/docs/messaging.md +182 -0
- package/relay/examples/relay-inbound.ts +66 -0
- package/relay/examples/relay-messaging.ts +71 -0
- package/relay/examples/relay-outbound.ts +76 -0
- package/rest/README.md +95 -0
- package/rest/docs/calling.md +273 -0
- package/rest/docs/client-reference.md +107 -0
- package/rest/docs/compat.md +204 -0
- package/rest/docs/fabric.md +183 -0
- package/rest/docs/getting-started.md +110 -0
- package/rest/docs/guide.md +351 -0
- package/rest/docs/namespaces.md +285 -0
- package/rest/examples/rest-10dlc-registration.ts +161 -0
- package/rest/examples/rest-calling-ivr-and-ai.ts +102 -0
- package/rest/examples/rest-calling-play-and-record.ts +91 -0
- package/rest/examples/rest-client.ts +65 -0
- package/rest/examples/rest-compat-laml.ts +210 -0
- package/rest/examples/rest-datasphere-search.ts +82 -0
- package/rest/examples/rest-fabric-conferences-and-routing.ts +144 -0
- package/rest/examples/rest-fabric-subscribers-and-sip.ts +108 -0
- package/rest/examples/rest-fabric-swml-and-callflows.ts +99 -0
- package/rest/examples/rest-manage-resources.ts +65 -0
- package/rest/examples/rest-phone-number-management.ts +197 -0
- package/rest/examples/rest-queues-mfa-and-recordings.ts +143 -0
- package/rest/examples/rest-video-rooms.ts +186 -0
- package/src/AgentBase.ts +2489 -0
- package/src/AgentServer.ts +415 -0
- package/src/AuthHandler.ts +256 -0
- package/src/ConfigLoader.ts +447 -0
- package/src/ContextBuilder.ts +1150 -0
- package/src/DataMap.ts +449 -0
- package/src/FunctionResult.ts +866 -0
- package/src/Logger.ts +337 -0
- package/src/PomBuilder.ts +439 -0
- package/src/PromptManager.ts +133 -0
- package/src/SWMLHandler.ts +298 -0
- package/src/SWMLService.ts +717 -0
- package/src/SchemaUtils.ts +361 -0
- package/src/SecurityUtils.ts +153 -0
- package/src/ServerlessAdapter.ts +221 -0
- package/src/SessionManager.ts +311 -0
- package/src/SslConfig.ts +118 -0
- package/src/SwaigFunction.ts +276 -0
- package/src/SwmlBuilder.ts +258 -0
- package/src/SwmlVerbMethods.generated.ts +234 -0
- package/src/TypeInference.ts +181 -0
- package/src/WebService.ts +619 -0
- package/src/cli/agent-loader.ts +166 -0
- package/src/cli/mock-data.ts +111 -0
- package/src/cli/swaig-test.ts +452 -0
- package/src/generateVerbTypes.ts +322 -0
- package/src/index.ts +167 -0
- package/src/livewire/index.ts +950 -0
- package/src/prefabs/ConciergeAgent.ts +287 -0
- package/src/prefabs/FAQBotAgent.ts +384 -0
- package/src/prefabs/InfoGathererAgent.ts +339 -0
- package/src/prefabs/ReceptionistAgent.ts +366 -0
- package/src/prefabs/SurveyAgent.ts +646 -0
- package/src/prefabs/index.ts +27 -0
- package/src/relay/Action.ts +314 -0
- package/src/relay/Call.ts +854 -0
- package/src/relay/Deferred.ts +71 -0
- package/src/relay/Message.ts +148 -0
- package/src/relay/RelayClient.ts +1036 -0
- package/src/relay/RelayError.ts +14 -0
- package/src/relay/RelayEvent.ts +892 -0
- package/src/relay/constants.ts +132 -0
- package/src/relay/index.ts +123 -0
- package/src/relay/types.ts +149 -0
- package/src/rest/HttpClient.ts +103 -0
- package/src/rest/RestError.ts +31 -0
- package/src/rest/base/BaseResource.ts +19 -0
- package/src/rest/base/CrudResource.ts +51 -0
- package/src/rest/base/CrudWithAddresses.ts +26 -0
- package/src/rest/index.ts +200 -0
- package/src/rest/namespaces/addresses.ts +32 -0
- package/src/rest/namespaces/calling.ts +187 -0
- package/src/rest/namespaces/chat.ts +20 -0
- package/src/rest/namespaces/compat.ts +352 -0
- package/src/rest/namespaces/datasphere.ts +45 -0
- package/src/rest/namespaces/fabric.ts +222 -0
- package/src/rest/namespaces/imported-numbers.ts +20 -0
- package/src/rest/namespaces/logs.ts +84 -0
- package/src/rest/namespaces/lookup.ts +21 -0
- package/src/rest/namespaces/mfa.ts +30 -0
- package/src/rest/namespaces/number-groups.ts +38 -0
- package/src/rest/namespaces/phone-numbers.ts +44 -0
- package/src/rest/namespaces/project.ts +36 -0
- package/src/rest/namespaces/pubsub.ts +20 -0
- package/src/rest/namespaces/queues.ts +33 -0
- package/src/rest/namespaces/recordings.ts +28 -0
- package/src/rest/namespaces/registry.ts +101 -0
- package/src/rest/namespaces/short-codes.ts +28 -0
- package/src/rest/namespaces/sip-profile.ts +25 -0
- package/src/rest/namespaces/verified-callers.ts +27 -0
- package/src/rest/namespaces/video.ts +165 -0
- package/src/rest/pagination.ts +84 -0
- package/src/rest/types.ts +61 -0
- package/src/schema.json +12250 -0
- package/src/skills/SkillBase.ts +547 -0
- package/src/skills/SkillManager.ts +415 -0
- package/src/skills/SkillRegistry.ts +367 -0
- package/src/skills/builtin/api_ninjas_trivia.ts +280 -0
- package/src/skills/builtin/ask_claude.ts +241 -0
- package/src/skills/builtin/claude_skills.ts +978 -0
- package/src/skills/builtin/custom_skills.ts +312 -0
- package/src/skills/builtin/datasphere.ts +438 -0
- package/src/skills/builtin/datasphere_serverless.ts +339 -0
- package/src/skills/builtin/datetime.ts +130 -0
- package/src/skills/builtin/google_maps.ts +651 -0
- package/src/skills/builtin/index.ts +88 -0
- package/src/skills/builtin/info_gatherer.ts +405 -0
- package/src/skills/builtin/joke.ts +191 -0
- package/src/skills/builtin/math.ts +142 -0
- package/src/skills/builtin/mcp_gateway.ts +677 -0
- package/src/skills/builtin/native_vector_search.ts +901 -0
- package/src/skills/builtin/play_background_file.ts +358 -0
- package/src/skills/builtin/spider.ts +869 -0
- package/src/skills/builtin/swml_transfer.ts +635 -0
- package/src/skills/builtin/weather_api.ts +248 -0
- package/src/skills/builtin/web_search.ts +1074 -0
- package/src/skills/builtin/wikipedia_search.ts +299 -0
- package/src/skills/index.ts +12 -0
- package/src/types.ts +132 -0
- package/tests/AgentBase.test.ts +1093 -0
- package/tests/AgentServer.test.ts +126 -0
- package/tests/AuthHandler.test.ts +133 -0
- package/tests/ConfigLoader.test.ts +151 -0
- package/tests/ContextBuilder.test.ts +108 -0
- package/tests/DataMap.test.ts +367 -0
- package/tests/EphemeralSkillCopy.test.ts +107 -0
- package/tests/FunctionResult.test.ts +298 -0
- package/tests/GracefulShutdown.test.ts +46 -0
- package/tests/Logger.test.ts +420 -0
- package/tests/PomBuilder.test.ts +228 -0
- package/tests/ProxyDetection.test.ts +228 -0
- package/tests/SWMLService.test.ts +320 -0
- package/tests/SWMLVerbHandler.test.ts +523 -0
- package/tests/SchemaUtils.test.ts +106 -0
- package/tests/SchemaUtils.verb.test.ts +206 -0
- package/tests/SecurityUtils.test.ts +192 -0
- package/tests/ServerlessAdapter.test.ts +222 -0
- package/tests/SessionManager.test.ts +257 -0
- package/tests/SkillBaseFeatures.test.ts +130 -0
- package/tests/SkillMultiInstance.test.ts +102 -0
- package/tests/SkillParameterSchemas.test.ts +134 -0
- package/tests/SkillRegistrySchema.test.ts +84 -0
- package/tests/SslConfig.test.ts +97 -0
- package/tests/SwmlBuilder.test.ts +400 -0
- package/tests/TypeInference.test.ts +165 -0
- package/tests/VerbHandlerRegistry.test.ts +128 -0
- package/tests/cli/mock-data.test.ts +113 -0
- package/tests/cli/swaig-test.test.ts +108 -0
- package/tests/examples.test.ts +194 -0
- package/tests/fixtures/scrape-parity/README.md +53 -0
- package/tests/fixtures/scrape-parity/expected/01_simple_article.json +5 -0
- package/tests/fixtures/scrape-parity/expected/02_class_based_noise.json +5 -0
- package/tests/fixtures/scrape-parity/expected/03_nested_containers.json +5 -0
- package/tests/fixtures/scrape-parity/expected/04_bare_body.json +5 -0
- package/tests/fixtures/scrape-parity/expected/05_malformed.json +5 -0
- package/tests/fixtures/scrape-parity/expected/06_multi_class.json +5 -0
- package/tests/fixtures/scrape-parity/expected/07_anchor_pattern.json +5 -0
- package/tests/fixtures/scrape-parity/expected/08_inline_script.json +5 -0
- package/tests/fixtures/scrape-parity/expected/09_whitespace_nbsp.json +5 -0
- package/tests/fixtures/scrape-parity/expected/10_entity_unicode.json +5 -0
- package/tests/fixtures/scrape-parity/expected/11_cdata_comments.json +5 -0
- package/tests/fixtures/scrape-parity/expected/12_deep_nested_removals.json +5 -0
- package/tests/fixtures/scrape-parity/html/01_simple_article.html +18 -0
- package/tests/fixtures/scrape-parity/html/02_class_based_noise.html +18 -0
- package/tests/fixtures/scrape-parity/html/03_nested_containers.html +13 -0
- package/tests/fixtures/scrape-parity/html/04_bare_body.html +9 -0
- package/tests/fixtures/scrape-parity/html/05_malformed.html +11 -0
- package/tests/fixtures/scrape-parity/html/06_multi_class.html +18 -0
- package/tests/fixtures/scrape-parity/html/07_anchor_pattern.html +12 -0
- package/tests/fixtures/scrape-parity/html/08_inline_script.html +17 -0
- package/tests/fixtures/scrape-parity/html/09_whitespace_nbsp.html +21 -0
- package/tests/fixtures/scrape-parity/html/10_entity_unicode.html +10 -0
- package/tests/fixtures/scrape-parity/html/11_cdata_comments.html +13 -0
- package/tests/fixtures/scrape-parity/html/12_deep_nested_removals.html +24 -0
- package/tests/fixtures/scrape-parity/python_extract.py +109 -0
- package/tests/livewire/livewire.test.ts +780 -0
- package/tests/mcp.test.ts +221 -0
- package/tests/prefabs/prefabs.test.ts +1093 -0
- package/tests/relay/Action.test.ts +392 -0
- package/tests/relay/Call.test.ts +535 -0
- package/tests/relay/Deferred.test.ts +71 -0
- package/tests/relay/Message.test.ts +164 -0
- package/tests/relay/RelayClient.test.ts +779 -0
- package/tests/relay/RelayEvent.test.ts +463 -0
- package/tests/relay/helpers.ts +79 -0
- package/tests/rest/HttpClient.test.ts +274 -0
- package/tests/rest/RestError.test.ts +53 -0
- package/tests/rest/calling.test.ts +156 -0
- package/tests/rest/client.test.ts +197 -0
- package/tests/rest/compat.test.ts +335 -0
- package/tests/rest/datasphere.test.ts +71 -0
- package/tests/rest/fabric.test.ts +186 -0
- package/tests/rest/helpers.ts +95 -0
- package/tests/rest/logs.test.ts +68 -0
- package/tests/rest/pagination.test.ts +133 -0
- package/tests/rest/registry.test.ts +94 -0
- package/tests/rest/relay-rest.test.ts +290 -0
- package/tests/rest/video.test.ts +173 -0
- package/tests/setup.ts +6 -0
- package/tests/skills/SkillBase.test.ts +108 -0
- package/tests/skills/SkillManager.test.ts +268 -0
- package/tests/skills/SkillRegistry.test.ts +176 -0
- package/tests/skills/builtin.test.ts +1472 -0
- package/tests/skills/claude-skills.test.ts +54 -0
- package/tests/skills/custom-skills.test.ts +53 -0
- package/tests/skills/datasphere-serverless.test.ts +70 -0
- package/tests/skills/datasphere.test.ts +107 -0
- package/tests/skills/datetime.test.ts +101 -0
- package/tests/skills/google-maps.test.ts +78 -0
- package/tests/skills/info-gatherer.test.ts +411 -0
- package/tests/skills/joke.test.ts +81 -0
- package/tests/skills/math.test.ts +68 -0
- package/tests/skills/mcp-gateway.test.ts +101 -0
- package/tests/skills/play-background.test.ts +54 -0
- package/tests/skills/spider.test.ts +108 -0
- package/tests/skills/swml-transfer.test.ts +178 -0
- package/tests/skills/trivia.test.ts +54 -0
- package/tests/skills/vector-search.test.ts +135 -0
- package/tests/skills/weather-api.test.ts +77 -0
- package/tests/skills/web-search-parity.test.ts +59 -0
- package/tests/skills/web-search.test.ts +103 -0
- package/tests/skills/wikipedia.test.ts +63 -0
- package/tsconfig.json +18 -0
- package/vitest.config.ts +9 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# Examples
|
|
2
|
+
|
|
3
|
+
TypeScript examples demonstrating the SignalWire AI Agents SDK. Each example can be run with `npx tsx`.
|
|
4
|
+
|
|
5
|
+
## Agent Examples
|
|
6
|
+
|
|
7
|
+
| File | Description |
|
|
8
|
+
|------|-------------|
|
|
9
|
+
| [simple-agent.ts](simple-agent.ts) | Minimal agent with prompt, hints, language, and a tool |
|
|
10
|
+
| [simple-static.ts](simple-static.ts) | Static agent with voice, params, and structured prompts |
|
|
11
|
+
| [dynamic-config.ts](dynamic-config.ts) | Per-request dynamic configuration callback |
|
|
12
|
+
| [advanced-dynamic-config.ts](advanced-dynamic-config.ts) | Advanced dynamic config with tier selection |
|
|
13
|
+
| [comprehensive-dynamic.ts](comprehensive-dynamic.ts) | Tier-based dynamic config with industry prompts, A/B testing |
|
|
14
|
+
| [declarative.ts](declarative.ts) | Declarative prompt sections with tools and post-prompt |
|
|
15
|
+
| [custom-path.ts](custom-path.ts) | Agent on a custom HTTP path with query-param personalization |
|
|
16
|
+
| [multi-agent.ts](multi-agent.ts) | Multiple agents on different routes via AgentServer |
|
|
17
|
+
| [multi-endpoint.ts](multi-endpoint.ts) | Single agent with multiple SWML routes alongside /health |
|
|
18
|
+
| [pom-prompt.ts](pom-prompt.ts) | Prompt Object Model sections and structured prompts |
|
|
19
|
+
|
|
20
|
+
## Contexts, Steps, and Gather Info
|
|
21
|
+
|
|
22
|
+
| File | Description |
|
|
23
|
+
|------|-------------|
|
|
24
|
+
| [contexts-steps.ts](contexts-steps.ts) | Multi-step workflows using contexts and steps |
|
|
25
|
+
| [gather-info.ts](gather-info.ts) | GatherInfo with typed questions for data collection |
|
|
26
|
+
|
|
27
|
+
## DataMap (Server-Side Tools)
|
|
28
|
+
|
|
29
|
+
| File | Description |
|
|
30
|
+
|------|-------------|
|
|
31
|
+
| [datamap-tools.ts](datamap-tools.ts) | Server-side DataMap tools (weather API, calculator) |
|
|
32
|
+
| [advanced-datamap.ts](advanced-datamap.ts) | Advanced DataMap: expressions, webhooks, foreach |
|
|
33
|
+
|
|
34
|
+
## Skills
|
|
35
|
+
|
|
36
|
+
| File | Description |
|
|
37
|
+
|------|-------------|
|
|
38
|
+
| [skills-demo.ts](skills-demo.ts) | Built-in skills: datetime, math |
|
|
39
|
+
| [web-search.ts](web-search.ts) | Web search skill via Google Custom Search API |
|
|
40
|
+
| [web-search-multi-instance.ts](web-search-multi-instance.ts) | Multiple web search instances (general, news, quick) |
|
|
41
|
+
| [wikipedia.ts](wikipedia.ts) | Wikipedia search skill for factual retrieval |
|
|
42
|
+
| [datasphere.ts](datasphere.ts) | DataSphere skill for knowledge base search |
|
|
43
|
+
| [datasphere-multi-instance.ts](datasphere-multi-instance.ts) | Multiple DataSphere instances for separate knowledge bases |
|
|
44
|
+
| [datasphere-serverless-env.ts](datasphere-serverless-env.ts) | DataSphere serverless from environment variables |
|
|
45
|
+
| [datasphere-webhook-env.ts](datasphere-webhook-env.ts) | DataSphere webhook from environment variables |
|
|
46
|
+
| [mcp-agent.ts](mcp-agent.ts) | MCP client integration for Model Context Protocol |
|
|
47
|
+
| [mcp-gateway.ts](mcp-gateway.ts) | MCP gateway skill for bridging MCP server tools |
|
|
48
|
+
|
|
49
|
+
## SWAIG Features and FunctionResult Actions
|
|
50
|
+
|
|
51
|
+
| File | Description |
|
|
52
|
+
|------|-------------|
|
|
53
|
+
| [swaig-features.ts](swaig-features.ts) | FunctionResult actions: connect, hangup, hold, say, metadata |
|
|
54
|
+
| [joke-agent.ts](joke-agent.ts) | Raw data_map configuration (API Ninjas jokes) |
|
|
55
|
+
| [record-call.ts](record-call.ts) | Start/stop call recording via FunctionResult |
|
|
56
|
+
| [room-and-sip.ts](room-and-sip.ts) | Room joining, SIP REFER, and conferences |
|
|
57
|
+
| [tap.ts](tap.ts) | TAP configuration for WebSocket/RTP media monitoring |
|
|
58
|
+
|
|
59
|
+
## Call Flow and AI Configuration
|
|
60
|
+
|
|
61
|
+
| File | Description |
|
|
62
|
+
|------|-------------|
|
|
63
|
+
| [call-flow.ts](call-flow.ts) | 5-phase call flow with pre/post answer verbs |
|
|
64
|
+
| [llm-params.ts](llm-params.ts) | LLM parameter tuning (precise, creative, customer service) |
|
|
65
|
+
| [session-state.ts](session-state.ts) | Global data, session tracking, and on_summary callback |
|
|
66
|
+
| [verb-methods.ts](verb-methods.ts) | SWML verb methods for building SWML documents |
|
|
67
|
+
|
|
68
|
+
## Prefab Agents
|
|
69
|
+
|
|
70
|
+
| File | Description |
|
|
71
|
+
|------|-------------|
|
|
72
|
+
| [prefab-info-gatherer.ts](prefab-info-gatherer.ts) | InfoGatherer prefab for structured data collection |
|
|
73
|
+
| [dynamic-info-gatherer.ts](dynamic-info-gatherer.ts) | Dynamic InfoGatherer with callback-based question selection |
|
|
74
|
+
| [prefab-survey.ts](prefab-survey.ts) | Survey prefab with typed questions and validation |
|
|
75
|
+
| [prefab-concierge.ts](prefab-concierge.ts) | Concierge prefab for venue amenities and services |
|
|
76
|
+
| [prefab-faq.ts](prefab-faq.ts) | FAQ bot prefab with keyword-based lookup |
|
|
77
|
+
| [prefab-receptionist.ts](prefab-receptionist.ts) | Receptionist prefab with department routing |
|
|
78
|
+
|
|
79
|
+
## SWML Service (Non-AI)
|
|
80
|
+
|
|
81
|
+
| File | Description |
|
|
82
|
+
|------|-------------|
|
|
83
|
+
| [swml-service.ts](swml-service.ts) | Basic SWML service: voicemail, IVR, recording |
|
|
84
|
+
| [dynamic-swml-service.ts](dynamic-swml-service.ts) | Dynamic SWML generation based on request data |
|
|
85
|
+
| [swml-service-routing.ts](swml-service-routing.ts) | Path-based routing with multiple SWML sections |
|
|
86
|
+
| [auto-vivified.ts](auto-vivified.ts) | Auto-vivified verb methods on SWMLService |
|
|
87
|
+
|
|
88
|
+
## Deployment
|
|
89
|
+
|
|
90
|
+
| File | Description |
|
|
91
|
+
|------|-------------|
|
|
92
|
+
| [kubernetes-agent.ts](kubernetes-agent.ts) | K8s-ready agent with /health, /ready endpoints |
|
|
93
|
+
| [serverless-lambda.ts](serverless-lambda.ts) | Serverless pattern for AWS Lambda deployment |
|
|
94
|
+
|
|
95
|
+
## Client Examples
|
|
96
|
+
|
|
97
|
+
| File | Description |
|
|
98
|
+
|------|-------------|
|
|
99
|
+
| [relay-demo.ts](relay-demo.ts) | RELAY WebSocket client: answer calls, play TTS |
|
|
100
|
+
|
|
101
|
+
## Running Examples
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
# Run any example
|
|
105
|
+
npx tsx examples/simple-agent.ts
|
|
106
|
+
|
|
107
|
+
# For skill examples that need API keys:
|
|
108
|
+
GOOGLE_SEARCH_API_KEY=your-key GOOGLE_SEARCH_CX=your-cx npx tsx examples/web-search.ts
|
|
109
|
+
API_NINJAS_KEY=your-key npx tsx examples/joke-agent.ts
|
|
110
|
+
|
|
111
|
+
# For RELAY/DataSphere examples:
|
|
112
|
+
export SIGNALWIRE_PROJECT_ID=your-project-id
|
|
113
|
+
export SIGNALWIRE_API_TOKEN=your-api-token
|
|
114
|
+
export SIGNALWIRE_SPACE=your-space.signalwire.com
|
|
115
|
+
npx tsx examples/relay-demo.ts
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Environment Variables
|
|
119
|
+
|
|
120
|
+
| Variable | Description | Default |
|
|
121
|
+
|----------|-------------|---------|
|
|
122
|
+
| `SWML_BASIC_AUTH_USER` | Override auth username | `user` |
|
|
123
|
+
| `SWML_BASIC_AUTH_PASSWORD` | Override auth password | `pass` |
|
|
124
|
+
| `SIGNALWIRE_PROJECT_ID` | Project ID (RELAY/DataSphere) | - |
|
|
125
|
+
| `SIGNALWIRE_API_TOKEN` | API token (RELAY/DataSphere) | - |
|
|
126
|
+
| `SIGNALWIRE_SPACE` | Space hostname | - |
|
|
127
|
+
| `API_NINJAS_KEY` | API Ninjas key (joke agent) | - |
|
|
128
|
+
| `GOOGLE_SEARCH_API_KEY` | Google Custom Search key | - |
|
|
129
|
+
| `GOOGLE_SEARCH_CX` | Google Search Engine ID | - |
|
|
130
|
+
| `DATASPHERE_DOCUMENT_ID` | DataSphere document ID | - |
|
|
131
|
+
| `MCP_GATEWAY_URL` | MCP gateway service URL | - |
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advanced DataMap Example
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates advanced DataMap patterns: expressions, foreach iteration,
|
|
5
|
+
* POST webhooks with bodies, error keys, and chained data_map tools.
|
|
6
|
+
* Run: npx tsx examples/advanced-datamap.ts
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
AgentBase,
|
|
11
|
+
DataMap,
|
|
12
|
+
FunctionResult,
|
|
13
|
+
createExpressionTool,
|
|
14
|
+
} from '../src/index.js';
|
|
15
|
+
|
|
16
|
+
export const agent = new AgentBase({
|
|
17
|
+
name: 'advanced-datamap-agent',
|
|
18
|
+
route: '/',
|
|
19
|
+
basicAuth: [
|
|
20
|
+
process.env['SWML_BASIC_AUTH_USER'] ?? 'user',
|
|
21
|
+
process.env['SWML_BASIC_AUTH_PASSWORD'] ?? 'pass',
|
|
22
|
+
],
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
agent.setPromptText(
|
|
26
|
+
'You are a helpful assistant with tools for language detection, stock lookup, ' +
|
|
27
|
+
'and currency conversion. Use the appropriate tool based on the user request.',
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// Pattern 1: Expression-based tool (no HTTP call)
|
|
31
|
+
// Detects simple greetings and responds appropriately
|
|
32
|
+
const greetingTool = createExpressionTool({
|
|
33
|
+
name: 'detect_greeting',
|
|
34
|
+
patterns: {
|
|
35
|
+
'${args.text}': [
|
|
36
|
+
'(?i)(hello|hi|hey|good morning|good afternoon)',
|
|
37
|
+
new FunctionResult('The user greeted with: ${args.text}. Respond warmly.'),
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
parameters: {
|
|
41
|
+
text: { type: 'string', description: 'The text to analyze', required: true },
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
agent.registerSwaigFunction(greetingTool.toSwaigFunction());
|
|
45
|
+
|
|
46
|
+
// Pattern 2: POST webhook with JSON body and error keys
|
|
47
|
+
const translationTool = new DataMap('lookup_definition')
|
|
48
|
+
.purpose('Look up the definition of a word in the dictionary')
|
|
49
|
+
.parameter('word', 'string', 'The word to look up', { required: true })
|
|
50
|
+
.webhook('GET', 'https://api.dictionaryapi.dev/api/v2/entries/en/${args.word}')
|
|
51
|
+
.output(
|
|
52
|
+
new FunctionResult(
|
|
53
|
+
'Definition of ${args.word}: The word was found in the dictionary.',
|
|
54
|
+
),
|
|
55
|
+
)
|
|
56
|
+
.errorKeys(['title'])
|
|
57
|
+
.fallbackOutput(
|
|
58
|
+
new FunctionResult('Could not find a definition for that word.'),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
agent.registerSwaigFunction(translationTool.toSwaigFunction());
|
|
62
|
+
|
|
63
|
+
// Pattern 3: DataMap with foreach to iterate over list results
|
|
64
|
+
const newsTool = new DataMap('get_news')
|
|
65
|
+
.purpose('Get the latest news headlines')
|
|
66
|
+
.parameter('topic', 'string', 'News topic to search for')
|
|
67
|
+
.webhook('GET', 'https://newsapi.org/v2/everything?q=${args.topic}&pageSize=3&apiKey=${ENV.SW_NEWS_API_KEY}')
|
|
68
|
+
.foreach({
|
|
69
|
+
input_key: 'response.articles',
|
|
70
|
+
output_key: 'headlines',
|
|
71
|
+
append: '- ${this.title}: ${this.description}',
|
|
72
|
+
max: 5,
|
|
73
|
+
})
|
|
74
|
+
.output(new FunctionResult('Latest news on ${args.topic}:\n${headlines}'))
|
|
75
|
+
.fallbackOutput(new FunctionResult('No news found for that topic.'));
|
|
76
|
+
|
|
77
|
+
// Enable env expansion for the news API key
|
|
78
|
+
newsTool.enableEnvExpansion(true);
|
|
79
|
+
newsTool.setAllowedEnvPrefixes(['SW_']);
|
|
80
|
+
|
|
81
|
+
agent.registerSwaigFunction(newsTool.toSwaigFunction());
|
|
82
|
+
|
|
83
|
+
// Pattern 4: Expression with nomatch fallback
|
|
84
|
+
const statusChecker = new DataMap('check_status')
|
|
85
|
+
.purpose('Check the status of a service by name')
|
|
86
|
+
.parameter('service', 'string', 'Service name to check', { required: true })
|
|
87
|
+
.expression(
|
|
88
|
+
'${args.service}',
|
|
89
|
+
'(?i)(api|web|database)',
|
|
90
|
+
new FunctionResult('The ${args.service} service is currently operational.'),
|
|
91
|
+
new FunctionResult(
|
|
92
|
+
'Unknown service "${args.service}". Available services: api, web, database.',
|
|
93
|
+
),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
agent.registerSwaigFunction(statusChecker.toSwaigFunction());
|
|
97
|
+
|
|
98
|
+
agent.addLanguage({ name: 'English', code: 'en-US', voice: 'rachel' });
|
|
99
|
+
|
|
100
|
+
agent.run();
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advanced Dynamic Config Example
|
|
3
|
+
*
|
|
4
|
+
* Per-request agent customization: VIP detection, department routing,
|
|
5
|
+
* language selection, and tiered service levels.
|
|
6
|
+
* Run: npx tsx examples/advanced-dynamic-config.ts
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { AgentBase, FunctionResult } from '../src/index.js';
|
|
10
|
+
|
|
11
|
+
export const agent = new AgentBase({
|
|
12
|
+
name: 'smart-router',
|
|
13
|
+
route: '/',
|
|
14
|
+
basicAuth: [
|
|
15
|
+
process.env['SWML_BASIC_AUTH_USER'] ?? 'user',
|
|
16
|
+
process.env['SWML_BASIC_AUTH_PASSWORD'] ?? 'pass',
|
|
17
|
+
],
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
agent.setPromptText('You are a helpful customer service assistant.');
|
|
21
|
+
agent.addLanguage({ name: 'English', code: 'en-US', voice: 'rachel' });
|
|
22
|
+
|
|
23
|
+
agent.defineTool({
|
|
24
|
+
name: 'lookup_account',
|
|
25
|
+
description: 'Look up a customer account by phone number',
|
|
26
|
+
parameters: {
|
|
27
|
+
phone: { type: 'string', description: 'The customer phone number' },
|
|
28
|
+
},
|
|
29
|
+
handler: (args) => {
|
|
30
|
+
return new FunctionResult(`Account lookup for ${args.phone}: Active, Premium tier.`);
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// VIP numbers get premium treatment
|
|
35
|
+
const VIP_NUMBERS = ['+15551234567', '+15559876543'];
|
|
36
|
+
|
|
37
|
+
agent.setDynamicConfigCallback(async (queryParams, bodyParams, _headers, agentCopy) => {
|
|
38
|
+
const copy = agentCopy as AgentBase;
|
|
39
|
+
const callerNumber = (bodyParams['call'] as Record<string, unknown>)?.['from'] as string ?? '';
|
|
40
|
+
|
|
41
|
+
// VIP detection
|
|
42
|
+
if (VIP_NUMBERS.includes(callerNumber)) {
|
|
43
|
+
copy.setPromptText(
|
|
44
|
+
'You are a premium concierge assistant. This is a VIP caller — provide priority service, ' +
|
|
45
|
+
'be extra attentive, and offer to escalate any issues immediately.',
|
|
46
|
+
);
|
|
47
|
+
copy.setParam('temperature', 0.3);
|
|
48
|
+
copy.updateGlobalData({ tier: 'vip', caller: callerNumber });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Department routing via query params
|
|
52
|
+
const dept = queryParams['department'];
|
|
53
|
+
if (dept === 'sales') {
|
|
54
|
+
copy.setPromptText('You are a sales assistant. Help the caller with product info and pricing.');
|
|
55
|
+
} else if (dept === 'support') {
|
|
56
|
+
copy.setPromptText('You are a technical support agent. Help diagnose and resolve issues.');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Language selection
|
|
60
|
+
const lang = queryParams['lang'];
|
|
61
|
+
if (lang === 'es') {
|
|
62
|
+
copy.setPromptText('Eres un asistente de servicio al cliente. Responde siempre en espanol.');
|
|
63
|
+
copy.setLanguages([{ name: 'Spanish', code: 'es-ES', voice: 'polly.Lucia' }]);
|
|
64
|
+
} else if (lang === 'fr') {
|
|
65
|
+
copy.setPromptText('Vous etes un assistant du service client. Repondez toujours en francais.');
|
|
66
|
+
copy.setLanguages([{ name: 'French', code: 'fr-FR', voice: 'polly.Celine' }]);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
agent.run();
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-Vivified SWML Service Example
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates auto-vivification: calling verb methods directly on a SWMLService
|
|
5
|
+
* instead of using addVerb(). Builds voicemail, IVR, and transfer services.
|
|
6
|
+
* Run: npx tsx examples/auto-vivified.ts
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { SWMLService } from '../src/index.js';
|
|
10
|
+
|
|
11
|
+
// --- Voicemail Service ---
|
|
12
|
+
const voicemail = new SWMLService({
|
|
13
|
+
name: 'voicemail',
|
|
14
|
+
route: '/voicemail',
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
voicemail.addAnswerVerb();
|
|
18
|
+
|
|
19
|
+
// Auto-vivified verb calls (direct method instead of addVerb)
|
|
20
|
+
voicemail.play({ url: 'say:Hello, you have reached the voicemail service. Please leave a message after the beep.' });
|
|
21
|
+
voicemail.sleep(1000);
|
|
22
|
+
voicemail.play({ url: 'https://example.com/beep.wav' });
|
|
23
|
+
voicemail.record({
|
|
24
|
+
format: 'mp3',
|
|
25
|
+
stereo: false,
|
|
26
|
+
beep: false,
|
|
27
|
+
max_length: 120,
|
|
28
|
+
terminators: '#',
|
|
29
|
+
status_url: 'https://example.com/voicemail-status',
|
|
30
|
+
});
|
|
31
|
+
voicemail.play({ url: 'say:Thank you for your message. Goodbye!' });
|
|
32
|
+
voicemail.addHangupVerb();
|
|
33
|
+
|
|
34
|
+
// --- IVR Menu Service ---
|
|
35
|
+
const ivr = new SWMLService({
|
|
36
|
+
name: 'ivr',
|
|
37
|
+
route: '/ivr',
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
ivr.addAnswerVerb();
|
|
41
|
+
|
|
42
|
+
ivr.addSection('main_menu');
|
|
43
|
+
ivr.addVerbToSection('main_menu', 'prompt', {
|
|
44
|
+
play: 'say:Press 1 for sales, 2 for support, or 3 to leave a message.',
|
|
45
|
+
max_digits: 1,
|
|
46
|
+
terminators: '#',
|
|
47
|
+
digit_timeout: 5.0,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
ivr.addVerbToSection('main_menu', 'switch', {
|
|
51
|
+
variable: 'prompt_digits',
|
|
52
|
+
case: {
|
|
53
|
+
'1': [{ transfer: { dest: 'sales' } }],
|
|
54
|
+
'2': [{ transfer: { dest: 'support' } }],
|
|
55
|
+
'3': [{ transfer: { dest: 'voicemail_section' } }],
|
|
56
|
+
},
|
|
57
|
+
default: [
|
|
58
|
+
{ play: { url: "say:Sorry, I didn't understand." } },
|
|
59
|
+
{ transfer: { dest: 'main_menu' } },
|
|
60
|
+
],
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
ivr.addSection('sales');
|
|
64
|
+
ivr.addVerbToSection('sales', 'play', { url: 'say:Connecting you to sales.' });
|
|
65
|
+
ivr.addVerbToSection('sales', 'connect', { to: '+15551234567' });
|
|
66
|
+
|
|
67
|
+
ivr.addVerb('transfer', { dest: 'main_menu' });
|
|
68
|
+
|
|
69
|
+
// --- Call Transfer Service ---
|
|
70
|
+
const transfer = new SWMLService({
|
|
71
|
+
name: 'transfer',
|
|
72
|
+
route: '/transfer',
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
transfer.addAnswerVerb();
|
|
76
|
+
transfer.addVerb('play', { url: 'say:Connecting you with the next available agent.' });
|
|
77
|
+
transfer.addVerb('connect', {
|
|
78
|
+
from: '+15551234567',
|
|
79
|
+
timeout: 30,
|
|
80
|
+
answer_on_bridge: true,
|
|
81
|
+
ringback: ['ring:us'],
|
|
82
|
+
parallel: [
|
|
83
|
+
{ to: '+15552223333' },
|
|
84
|
+
{ to: '+15554445555' },
|
|
85
|
+
],
|
|
86
|
+
});
|
|
87
|
+
transfer.addVerb('play', { url: 'say:All agents are busy. Please leave a message.' });
|
|
88
|
+
transfer.addVerb('record', { format: 'mp3', beep: true, max_length: 120, terminators: '#' });
|
|
89
|
+
transfer.addHangupVerb();
|
|
90
|
+
|
|
91
|
+
// Run the voicemail service by default (switch via --service flag)
|
|
92
|
+
const service = process.argv[2] === 'ivr' ? ivr : process.argv[2] === 'transfer' ? transfer : voicemail;
|
|
93
|
+
console.log(`Starting ${service.name} service...`);
|
|
94
|
+
service.run();
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Call Flow Verbs Example
|
|
3
|
+
*
|
|
4
|
+
* Control what happens before, during, and after the AI conversation
|
|
5
|
+
* using SWML verbs in the 5-phase rendering pipeline.
|
|
6
|
+
* Run: npx tsx examples/call-flow.ts
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { AgentBase, FunctionResult } from '../src/index.js';
|
|
10
|
+
|
|
11
|
+
export const agent = new AgentBase({
|
|
12
|
+
name: 'call-flow-agent',
|
|
13
|
+
route: '/',
|
|
14
|
+
basicAuth: [
|
|
15
|
+
process.env['SWML_BASIC_AUTH_USER'] ?? 'user',
|
|
16
|
+
process.env['SWML_BASIC_AUTH_PASSWORD'] ?? 'pass',
|
|
17
|
+
],
|
|
18
|
+
recordCall: true, // Record the call (Phase 3)
|
|
19
|
+
recordFormat: 'mp4',
|
|
20
|
+
recordStereo: true,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
agent.setPromptText('You are a receptionist. Help callers schedule appointments.');
|
|
24
|
+
|
|
25
|
+
// Phase 1: Play ringing before answering
|
|
26
|
+
agent.addPreAnswerVerb('play', {
|
|
27
|
+
urls: ['ring:us'],
|
|
28
|
+
auto_answer: false,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Phase 3: Play a welcome message after answering but before the AI takes over
|
|
32
|
+
agent.addPostAnswerVerb('play', {
|
|
33
|
+
url: 'say:Welcome to Acme Corp. Please hold while I connect you with our AI assistant.',
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Phase 5: Hangup after the AI conversation ends
|
|
37
|
+
agent.addPostAiVerb('hangup', {});
|
|
38
|
+
|
|
39
|
+
// AI config
|
|
40
|
+
agent.addLanguage({ name: 'English', code: 'en-US', voice: 'rachel' });
|
|
41
|
+
agent.addPronunciation({ replace: 'Acme', with: 'Ak-mee' });
|
|
42
|
+
agent.setParam('temperature', 0.7);
|
|
43
|
+
|
|
44
|
+
// Post-prompt for call summary
|
|
45
|
+
agent.setPostPrompt('Provide a JSON summary with: caller_name, appointment_date, appointment_reason');
|
|
46
|
+
|
|
47
|
+
// Tool for scheduling
|
|
48
|
+
agent.defineTool({
|
|
49
|
+
name: 'schedule_appointment',
|
|
50
|
+
description: 'Schedule an appointment for the caller',
|
|
51
|
+
parameters: {
|
|
52
|
+
date: { type: 'string', description: 'Appointment date (YYYY-MM-DD)' },
|
|
53
|
+
time: { type: 'string', description: 'Appointment time (HH:MM)' },
|
|
54
|
+
reason: { type: 'string', description: 'Reason for the appointment' },
|
|
55
|
+
},
|
|
56
|
+
handler: (args) => {
|
|
57
|
+
return new FunctionResult(
|
|
58
|
+
`Appointment scheduled for ${args.date} at ${args.time}. Reason: ${args.reason}`,
|
|
59
|
+
);
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
agent.run();
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Comprehensive Dynamic Agent Example
|
|
3
|
+
*
|
|
4
|
+
* Tier-based dynamic config (standard/premium/enterprise) with industry-specific
|
|
5
|
+
* prompts, voice selection, LLM parameter tuning, and A/B testing.
|
|
6
|
+
* Run: npx tsx examples/comprehensive-dynamic.ts
|
|
7
|
+
* Test: curl "http://user:pass@localhost:3000/?tier=premium&industry=healthcare"
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { AgentBase, FunctionResult } from '../src/index.js';
|
|
11
|
+
|
|
12
|
+
export const agent = new AgentBase({
|
|
13
|
+
name: 'comprehensive-dynamic',
|
|
14
|
+
route: '/',
|
|
15
|
+
basicAuth: [
|
|
16
|
+
process.env['SWML_BASIC_AUTH_USER'] ?? 'user',
|
|
17
|
+
process.env['SWML_BASIC_AUTH_PASSWORD'] ?? 'pass',
|
|
18
|
+
],
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
agent.setDynamicConfigCallback((queryParams, bodyParams, headers, ephemeral) => {
|
|
22
|
+
const tier = queryParams.tier ?? 'standard';
|
|
23
|
+
const industry = queryParams.industry ?? 'general';
|
|
24
|
+
const abGroup = Math.random() < 0.5 ? 'A' : 'B';
|
|
25
|
+
|
|
26
|
+
// Voice and language based on tier
|
|
27
|
+
const voiceMap: Record<string, string> = {
|
|
28
|
+
enterprise: 'alloy',
|
|
29
|
+
premium: 'nova',
|
|
30
|
+
standard: 'rachel',
|
|
31
|
+
};
|
|
32
|
+
ephemeral.addLanguage({
|
|
33
|
+
name: 'English',
|
|
34
|
+
code: 'en-US',
|
|
35
|
+
voice: voiceMap[tier] ?? 'rachel',
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// LLM parameters by tier
|
|
39
|
+
const tempMap: Record<string, number> = { enterprise: 0.2, premium: 0.4, standard: 0.6 };
|
|
40
|
+
ephemeral.setParams({
|
|
41
|
+
temperature: tempMap[tier] ?? 0.5,
|
|
42
|
+
top_p: tier === 'enterprise' ? 0.85 : 0.95,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Global data
|
|
46
|
+
ephemeral.setGlobalData({
|
|
47
|
+
tier,
|
|
48
|
+
industry,
|
|
49
|
+
ab_group: abGroup,
|
|
50
|
+
priority_queue: tier !== 'standard',
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Industry-specific prompts
|
|
54
|
+
const industryPrompts: Record<string, { title: string; body: string; bullets: string[] }> = {
|
|
55
|
+
healthcare: {
|
|
56
|
+
title: 'Healthcare Expertise',
|
|
57
|
+
body: 'You are a healthcare support specialist.',
|
|
58
|
+
bullets: [
|
|
59
|
+
'Follow HIPAA guidelines for patient information',
|
|
60
|
+
'Use proper medical terminology',
|
|
61
|
+
'Escalate urgent medical concerns immediately',
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
finance: {
|
|
65
|
+
title: 'Financial Services',
|
|
66
|
+
body: 'You are a financial services representative.',
|
|
67
|
+
bullets: [
|
|
68
|
+
'Never provide specific investment advice',
|
|
69
|
+
'Verify account identity before sharing details',
|
|
70
|
+
'Offer to connect with a licensed advisor for complex questions',
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
general: {
|
|
74
|
+
title: 'General Support',
|
|
75
|
+
body: 'You are a versatile customer service agent.',
|
|
76
|
+
bullets: [
|
|
77
|
+
'Help with product questions and troubleshooting',
|
|
78
|
+
'Process returns and exchanges',
|
|
79
|
+
'Escalate complex issues to a specialist',
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const prompt = industryPrompts[industry] ?? industryPrompts.general;
|
|
85
|
+
ephemeral.promptAddSection(prompt.title, prompt.body, prompt.bullets);
|
|
86
|
+
|
|
87
|
+
// Tier-specific features
|
|
88
|
+
if (tier === 'enterprise' || tier === 'premium') {
|
|
89
|
+
ephemeral.defineTool({
|
|
90
|
+
name: 'schedule_callback',
|
|
91
|
+
description: 'Schedule a priority callback from a specialist',
|
|
92
|
+
parameters: {
|
|
93
|
+
time_slot: { type: 'string', description: 'Preferred callback time' },
|
|
94
|
+
},
|
|
95
|
+
handler: (args) => {
|
|
96
|
+
return new FunctionResult(
|
|
97
|
+
`Priority callback scheduled for ${args.time_slot}. A specialist will call you.`,
|
|
98
|
+
);
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// A/B test: greeting style
|
|
104
|
+
if (abGroup === 'A') {
|
|
105
|
+
ephemeral.promptAddSection('Greeting', 'Start with a warm, conversational greeting.');
|
|
106
|
+
} else {
|
|
107
|
+
ephemeral.promptAddSection('Greeting', 'Start with a brief, professional greeting.');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Tool available to all tiers
|
|
111
|
+
ephemeral.defineTool({
|
|
112
|
+
name: 'check_status',
|
|
113
|
+
description: 'Check the status of an order or ticket',
|
|
114
|
+
parameters: {
|
|
115
|
+
reference: { type: 'string', description: 'Order or ticket number' },
|
|
116
|
+
},
|
|
117
|
+
handler: (args) => {
|
|
118
|
+
return new FunctionResult(
|
|
119
|
+
`Reference ${args.reference} is being processed. Estimated completion: 2 business days.`,
|
|
120
|
+
);
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
agent.run();
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contexts & Steps Example
|
|
3
|
+
*
|
|
4
|
+
* Multi-step conversation workflows where the AI follows a structured
|
|
5
|
+
* sequence of steps, each with its own prompt, criteria, and allowed tools.
|
|
6
|
+
* Run: npx tsx examples/contexts-steps.ts
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { AgentBase, FunctionResult } from '../src/index.js';
|
|
10
|
+
|
|
11
|
+
export const agent = new AgentBase({
|
|
12
|
+
name: 'quiz-agent',
|
|
13
|
+
route: '/',
|
|
14
|
+
basicAuth: [
|
|
15
|
+
process.env['SWML_BASIC_AUTH_USER'] ?? 'user',
|
|
16
|
+
process.env['SWML_BASIC_AUTH_PASSWORD'] ?? 'pass',
|
|
17
|
+
],
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
agent.setPromptText('You are a fun quiz host named Quizzy.');
|
|
21
|
+
|
|
22
|
+
// Define tools
|
|
23
|
+
agent.defineTool({
|
|
24
|
+
name: 'get_score',
|
|
25
|
+
description: 'Get the current score for the player',
|
|
26
|
+
parameters: {},
|
|
27
|
+
handler: () => new FunctionResult('The player has 3 out of 5 correct.'),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
agent.defineTool({
|
|
31
|
+
name: 'submit_answer',
|
|
32
|
+
description: 'Submit an answer to the current question',
|
|
33
|
+
parameters: {
|
|
34
|
+
answer: { type: 'string', description: 'The answer to check' },
|
|
35
|
+
},
|
|
36
|
+
handler: (args) => {
|
|
37
|
+
return new FunctionResult(`The answer "${args.answer}" has been recorded.`);
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Define the conversation flow
|
|
42
|
+
const ctx = agent.defineContexts();
|
|
43
|
+
const quiz = ctx.addContext('default');
|
|
44
|
+
|
|
45
|
+
// Step 1: Greet the player
|
|
46
|
+
quiz
|
|
47
|
+
.addStep('greeting', { task: 'Welcome the player and explain the quiz rules.' })
|
|
48
|
+
.setStepCriteria('Player has acknowledged the rules and is ready to start')
|
|
49
|
+
.setFunctions('none') // No tools needed for greeting
|
|
50
|
+
.setValidSteps(['question']);
|
|
51
|
+
|
|
52
|
+
// Step 2: Ask questions
|
|
53
|
+
quiz
|
|
54
|
+
.addStep('question', { task: 'Ask the player a trivia question and evaluate their answer.' })
|
|
55
|
+
.setStepCriteria('Player has answered the question')
|
|
56
|
+
.setFunctions(['submit_answer'])
|
|
57
|
+
.setValidSteps(['question', 'results']);
|
|
58
|
+
|
|
59
|
+
// Step 3: Show results
|
|
60
|
+
quiz
|
|
61
|
+
.addStep('results', { task: 'Show the final score and thank the player for playing.' })
|
|
62
|
+
.setFunctions(['get_score'])
|
|
63
|
+
.setEnd(true);
|
|
64
|
+
|
|
65
|
+
agent.run();
|