For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Log inSign up
Support
ReferenceGuides
ReferenceGuides
    • Quickstart
  • Basics
    • Control program flow
    • Deploy SWML from web servers
    • Handle incoming calls from code
    • Send simple HTTP requests
  • Recipes
    • Call whisper
    • Forwarding calls
    • Making and receiving phone calls
    • Recording calls
    • Setting up voicemail
    • Simple IVR
  • SWAIG Functions
    • Overview
    • Enable functions dynamically
    • Execute SWML from a function
    • Handle SWAIG function calls inline
    • Store data outside LLM context
    • Switch AI context mid-call
LogoLogoSignalWire Docs
Log inSign up
Support
On this page
  • transfer function
  • get_joke function
  • Full example
SWAIG Functions

Enable functions dynamically

|View as Markdown|Open in Claude|
Was this page helpful?
Edit this page
Previous

Execute SWML from a function

Next
Built with

In this example, the transfer function is toggled off from the start. The AI agent will toggle this function on after the get_joke function is called and will also toggle the get_joke function off. This creates a scenario where a user can only be transferred after hearing a joke from the AI, and can only request one joke. The AI agent will then match the transfer destination based on the user’s input, with the meta_data table serving as a directory for the transfer destinations. If no match is found, the AI agent will fall back to the ".*" expression, which will inform the user that the transfer was unsuccessful and requires a valid input.

transfer function

1{
2 "SWAIG": {
3 "functions": [
4 {
5 "function": "transfer",
6 "active": "false",
7 "description": "use to transfer to a target",
8 "parameters": {
9 "type": "object",
10 "properties": {
11 "target": {
12 "description": "the target to transfer to",
13 "type": "string"
14 }
15 }
16 },
17 "data_map": {
18 "expressions": [
19 {
20 "pattern": "\\w+",
21 "string": "${meta_data.table.${lc:args.target}}",
22 "output": {
23 "action": [
24 { "say": "Please stand by while I connect your call." },
25 {
26 "transfer": true,
27 "SWML": {
28 "version": "1.0.0",
29 "sections": {
30 "main": [
31 {
32 "connect": {
33 "to": "${meta_data.table.${lc:args.target}}"
34 }
35 }
36 ]
37 }
38 }
39 },
40 { "stop": true }
41 ],
42 "response": "transferred, the call has ended."
43 }
44 },
45 {
46 "string": "${args.target}",
47 "pattern": ".*",
48 "output": {
49 "response": "I'm sorry, I was unable to transfer your call to ${input.args.target}."
50 }
51 }
52 ]
53 },
54 "meta_data": {
55 "table": {
56 "support": "+1XXXXXXXXXX",
57 "sales": "+1YYYYYYYYYY"
58 }
59 }
60 }
61 ]
62 }
63}

When passing a SWML object that includes a connect (or other method that transfers the call) in output.action[], your script must be JSON, and "transfer": true, must be included before the SWML in the same action object. Refer to the example above.

We set the function to being toggled off with the following line:

1active: "false"

get_joke function

1{
2 "function": "get_joke",
3 "description": "use to get a joke",
4 "data_map": {
5 "webhooks": [
6 {
7 "url": "https://example.com/v1/${args.type}",
8 "headers": {
9 "X-Api-Key": "api-token-here"
10 },
11 "method": "GET",
12 "output": {
13 "response": "Tell the user: ${array[0].joke}.",
14 "action": [
15 {
16 "toggle_functions": [
17 { "active": true, "function": "transfer" },
18 { "active": false, "function": "get_joke" }
19 ]
20 }
21 ]
22 }
23 }
24 ]
25 }
26}

In the above function, we set the transfer function to be toggled on. Now when a user asks to be transferred, the AI agent will now be able to do so because the transfer function is toggled on. Additionally, the get_joke function is toggled off, so the user will not be able to request another joke.

1{
2 "toggle_functions": [
3 { "active": true, "function": "transfer" },
4 { "active": false, "function": "get_joke" }
5 ]
6}

Full example

1{
2 "sections": {
3 "main": [
4 {
5 "ai": {
6 "prompt": {
7 "text": "Your name is Frank. You help transfer to the right department. Use the 'get_joke' function to get a joke.\n\n# Greetings\nGreet the user, and inform them that your name is Frank and you can help assist them in transferring the call to Support or Sales. However, let them know the only way to be transferred is to hear a joke.\n\n# Rules\nA user cannot be transferred until they have asked for at least one joke throughout the call. When a user is able to be transferred, use the 'transfer' function. Only provide one joke to the user, if the user asks for more jokes after the first one, tell them you are no longer giving out jokes, but can help with transferring the call to a different department."
8 },
9 "post_prompt": "Summarize the call as a json object",
10 "post_prompt_url": "https://example.com/post_prompt",
11 "SWAIG": {
12 "functions": [
13 {
14 "function": "transfer",
15 "active": "false",
16 "description": "use to transfer to a target",
17 "parameters": {
18 "type": "object",
19 "properties": {
20 "target": {
21 "description": "the target to transfer to",
22 "type": "string"
23 }
24 }
25 },
26 "data_map": {
27 "expressions": [
28 {
29 "pattern": "\\w+",
30 "string": "${meta_data.table.${lc:args.target}}",
31 "output": {
32 "action": [
33 { "say": "Please stand by while I connect your call." },
34 {
35 "transfer": true,
36 "SWML": {
37 "version": "1.0.0",
38 "sections": {
39 "main": [
40 {
41 "connect": {
42 "to": "${meta_data.table.${lc:args.target}}"
43 }
44 }
45 ]
46 }
47 }
48 },
49 { "stop": true }
50 ],
51 "response": "transferred, the call has ended."
52 }
53 },
54 {
55 "string": "${args.target}",
56 "pattern": ".*",
57 "output": {
58 "response": "I'm sorry, I was unable to transfer your call to ${input.args.target}."
59 }
60 }
61 ]
62 },
63 "meta_data": {
64 "table": {
65 "support": "+1XXXXXXXXXX",
66 "sales": "+1YYYYYYYYY"
67 }
68 }
69 },
70 {
71 "function": "get_joke",
72 "description": "used to get a joke",
73 "parameters": {
74 "type": "object",
75 "properties": {
76 "type": {
77 "type": "string",
78 "description": "must either be 'jokes' or 'dadjokes'"
79 }
80 }
81 },
82 "data_map": {
83 "webhooks": [
84 {
85 "url": "https://example.com/secret_bank?name=${lc:args.type}",
86 "method": "GET",
87 "output": {
88 "response": "Tell the user: ${array[0].joke}.",
89 "action": [
90 {
91 "toggle_functions": [
92 { "active": true, "function": "transfer" },
93 { "active": false, "function": "get_joke" }
94 ]
95 }
96 ]
97 }
98 }
99 ]
100 }
101 }
102 ]
103 }
104 }
105 }
106 ]
107 }
108}