Skip to content

Commit cccadfe

Browse files
committed
feat: add variables set in the interaction tests
1 parent 6b88bec commit cccadfe

8 files changed

Lines changed: 1052 additions & 4 deletions

File tree

docs/docs/static/conversationtest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@
9797
},
9898
"agent": {
9999
"$ref": "#/$defs/Agent"
100+
},
101+
"variables": {
102+
"type": "object",
103+
"additionalProperties": true,
104+
"description": "Variables to set in the agent state after this interaction completes"
100105
}
101106
},
102107
"additionalProperties": false,

docs/docs/tests/interaction-tests.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ description: These are some tests
2020
interactions:
2121
# The ID of the interactions
2222
- id: test_1
23+
user:
24+
# the input type
25+
# it could be text, audio or prompt
26+
type: launch
27+
# Optional: Variables to set in the agent state after this interaction
28+
variables:
29+
user_name: "John Doe"
30+
account_type: "premium"
31+
agent:
32+
validate:
33+
- type: traceType
34+
value: speak
35+
36+
- id: test_2
2337
user:
2438
# the input type
2539
# it could be text, audio or prompt
@@ -247,7 +261,92 @@ interactions:
247261
value: "You selected yes"
248262
```
249263

264+
## Setting Variables
265+
266+
You can set variables in the agent's state during a test using the `variables` property on any interaction. Variables are set using the [Voiceflow State Variables API](https://docs.voiceflow.com/reference/updatestatevariables-1) **after** the interaction completes. This is especially important for `launch` interactions, where the session must exist before variables can be set.
267+
268+
### When to Use Variables
269+
270+
- **Initialize agent state**: Set variables after launching a session to configure the agent's behavior
271+
- **Simulate pre-existing data**: Set user data or context variables that your agent expects
272+
- **Test different scenarios**: Use different variable values to test how your agent responds under various conditions
273+
274+
### How It Works
275+
276+
1. **For `launch` interactions**: The launch is executed first to create the session, then variables are set afterwards (the session must exist before variables can be set)
277+
2. **For all other interactions**: Variables are set **before** the interaction executes, so the agent has access to them during the interaction
278+
3. Subsequent interactions will have access to the updated variables
279+
280+
### Example
281+
282+
```yaml
283+
name: Variable Setting Example
284+
description: Test demonstrating variable setting in interaction tests
285+
286+
interactions:
287+
- id: launch_conversation
288+
user:
289+
type: launch
290+
# Variables are set AFTER the launch interaction creates the session
291+
variables:
292+
user_name: "John Doe"
293+
account_type: "premium"
294+
service_list: "basic, premium, enterprise"
295+
agent:
296+
validate:
297+
- type: traceType
298+
value: speak
299+
300+
- id: ask_about_account
301+
user:
302+
type: text
303+
text: "What is my account type?"
304+
agent:
305+
validate:
306+
- type: contains
307+
value: "premium"
308+
309+
- id: update_preferences
310+
user:
311+
type: text
312+
text: "Update my preferences"
313+
# For non-launch interactions, variables are set BEFORE the interaction
314+
variables:
315+
preferred_language: "Spanish"
316+
agent:
317+
validate:
318+
- type: contains
319+
value: "preferences updated"
320+
```
321+
322+
### Variable Types
323+
324+
Variables support any value type including strings, numbers, booleans, arrays, and objects:
325+
326+
```yaml
327+
variables:
328+
# String
329+
user_name: "Jane Smith"
330+
# Number
331+
retry_count: 3
332+
# Boolean
333+
is_vip: true
334+
# Object
335+
user_profile:
336+
name: "Jane Smith"
337+
email: "jane@example.com"
338+
# Array/List
339+
selected_items:
340+
- "item1"
341+
- "item2"
342+
```
343+
344+
### Important Notes
250345

346+
- For `launch` interactions, variables are set **after** the interaction (the session must exist first)
347+
- For all other interactions, variables are set **before** the interaction executes
348+
- The `variables` property uses the same underlying API as the [Agent-to-Agent testing variables](/tests/agent-to-agent-tests/#voiceflowagenttargetconfig-voiceflow-agent-to-agent-testing-only)
349+
- Variables are merged with existing state variables (PATCH operation), not replacing them entirely
251350

252351
## Validation types
253352

examples/tests/test_1.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ interactions:
44
- id: test_1_1
55
user:
66
type: launch
7+
variables:
8+
user_name: "John Doe"
9+
account_type: "premium"
710
agent:
811
validate:
912
- type: contains
@@ -14,6 +17,9 @@ interactions:
1417
user:
1518
type: text
1619
text: 'I am doing well'
20+
variables:
21+
user_name: "John Does"
22+
account_type: "premium"
1723
agent:
1824
validate:
1925
- type: traceType

internal/types/tests/testTypes.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ type UserInfo struct {
3737
}
3838

3939
type Interaction struct {
40-
ID string `yaml:"id" json:"id"`
41-
User User `yaml:"user" json:"user"`
42-
Agent Agent `yaml:"agent" json:"agent"`
43-
Buttons []Button `yaml:"-" json:"-"` // Stores buttons from previous response, not serialized
40+
ID string `yaml:"id" json:"id"`
41+
User User `yaml:"user" json:"user"`
42+
Agent Agent `yaml:"agent" json:"agent"`
43+
Variables map[string]interface{} `yaml:"variables,omitempty" json:"variables,omitempty"` // Variables to set in the agent state
44+
Buttons []Button `yaml:"-" json:"-"` // Stores buttons from previous response, not serialized
4445
}
4546

4647
type User struct {

pkg/test/runner.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,31 @@ func runTest(environmentName, userID string, test tests.Test, apiKeyOverride, su
4444
logCollector.AddLog("\tInteraction Request Button: " + interaction.User.Value)
4545
}
4646

47+
// For non-launch interactions, set variables BEFORE the interaction
48+
if interaction.User.Type != "launch" && len(interaction.Variables) > 0 {
49+
logCollector.AddLog(fmt.Sprintf("\tSetting %d variables in agent state before interaction", len(interaction.Variables)))
50+
err := voiceflow.UpdateStateVariables(environmentName, userID, interaction.Variables, apiKeyOverride, subdomainOverride)
51+
if err != nil {
52+
return fmt.Errorf("error setting variables for Interaction ID %s: %v", interaction.ID, err)
53+
}
54+
logCollector.AddLog("\tSuccessfully updated agent state variables")
55+
}
56+
4757
interactionResponses, err := voiceflow.DialogManagerInteract(environmentName, userID, interaction, apiKeyOverride, subdomainOverride, availableButtons)
4858
if err != nil {
4959
return err
5060
}
61+
62+
// For launch interactions, set variables AFTER the interaction
63+
// (the session must exist before variables can be set)
64+
if interaction.User.Type == "launch" && len(interaction.Variables) > 0 {
65+
logCollector.AddLog(fmt.Sprintf("\tSetting %d variables in agent state after launch", len(interaction.Variables)))
66+
err = voiceflow.UpdateStateVariables(environmentName, userID, interaction.Variables, apiKeyOverride, subdomainOverride)
67+
if err != nil {
68+
return fmt.Errorf("error setting variables for Interaction ID %s: %v", interaction.ID, err)
69+
}
70+
logCollector.AddLog("\tSuccessfully updated agent state variables")
71+
}
5172
validations := interaction.Agent.Validate
5273
validations = autoGenerateValidationsIDs(validations)
5374

server/docs/docs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ const docTemplate = `{
392392
},
393393
"user": {
394394
"$ref": "#/definitions/tests.User"
395+
},
396+
"variables": {
397+
"description": "Variables to set in the agent state",
398+
"type": "object",
399+
"additionalProperties": true
395400
}
396401
}
397402
},

0 commit comments

Comments
 (0)