Skip to content

Commit 60eb48b

Browse files
authored
Merge pull request #28 from trailheadapps/mo/issue-13
feat: finalize the recipe instruction action references
2 parents 360036c + 8c1f9d1 commit 60eb48b

File tree

7 files changed

+177
-66
lines changed

7 files changed

+177
-66
lines changed

force-app/future_recipes/instructionActionReferences/README.md

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# InstructionActionReferences
2+
3+
## Overview
4+
5+
Learn how to **reference actions directly** within your reasoning instructions. This technique helps guide the LLM to use specific actions in specific contexts by explicitly mentioning them in the instruction text. Combined with dynamic variable expressions, you can create contextual, targeted prompts that improve action selection accuracy.
6+
7+
## Agent Flow
8+
9+
```mermaid
10+
%%{init: {'theme':'neutral'}}%%
11+
graph TD
12+
A[Start] --> B[Topic Selector]
13+
B --> C[Case Management Topic]
14+
C --> D{Evaluate next_open_time}
15+
D -->|Has Value| E[Outside Business Hours Path]
16+
E --> F["Instructions mention {[email protected]_case}"]
17+
F --> G[LLM Calls create_case Action]
18+
G --> H[Return Case Number to User]
19+
D -->|No Value| I[Inside Business Hours Path]
20+
I --> J[Help User Directly]
21+
```
22+
23+
## Key Concepts
24+
25+
- **Action References**: `{[email protected]_name}` embeds a reference to the action definition directly in the instruction text, signaling to the LLM that this tool is relevant.
26+
- **Variable Expressions**: `{[email protected]}` injects dynamic variable values into instructions for contextual prompts.
27+
- **Procedural If Blocks**: Use `if @variables.name:` to conditionally include different instruction blocks based on variable state.
28+
- **Targeted Tool Guidance**: By naming actions in instructions, you increase the likelihood the LLM will select the appropriate tool.
29+
30+
## How It Works
31+
32+
### Why Reference Actions in Instructions?
33+
34+
When the LLM receives a list of available actions, it must decide which one to use based on the user's request. By explicitly mentioning an action in the instruction text using `{[email protected]_name}`, you create a stronger association between the context and the tool. This is particularly useful when:
35+
36+
- Multiple actions could apply, but one is preferred in a specific situation
37+
- You want to ensure a particular workflow is followed
38+
- The action name alone doesn't fully convey when it should be used
39+
40+
### The Conditional Instruction Pattern
41+
42+
This recipe demonstrates combining action references with procedural logic. The `if` block checks whether `next_open_time` has a value:
43+
44+
```agentscript
45+
if @variables.next_open_time:
46+
| We are currently OUTSIDE business hours...
47+
Create a support case using {[email protected]_case}...
48+
else:
49+
| We are currently WITHIN business hours...
50+
```
51+
52+
When the condition is true, the instruction block that mentions `create_case` is included, guiding the LLM to use that specific action.
53+
54+
### How Template Expressions Resolve
55+
56+
The `{[email protected]_case}` expression doesn't just insert the action name—it embeds a reference to the full action definition. This gives the LLM richer context about the action's purpose, inputs, and outputs when making its decision.
57+
58+
Similarly, `{[email protected]_open_time}` resolves to the actual variable value (e.g., "9:00 AM Tomorrow"), allowing the agent to communicate dynamic information to the user.
59+
60+
## Key Code Snippets
61+
62+
### Variable Declaration
63+
64+
```agentscript
65+
variables:
66+
next_open_time: mutable string = "9:00 AM Tomorrow"
67+
```
68+
69+
### Action Definition
70+
71+
```agentscript
72+
actions:
73+
create_case:
74+
description: "Creates a support case"
75+
inputs:
76+
subject: string
77+
description: "Subject of the case"
78+
outputs:
79+
case_number: string
80+
description: "The created case number"
81+
target: "flow://CreateCase"
82+
```
83+
84+
### Conditional Instructions with Action Reference
85+
86+
```agentscript
87+
reasoning:
88+
instructions:->
89+
| If the user has an issue that requires support, please help them.
90+
91+
if @variables.next_open_time:
92+
| We are currently OUTSIDE business hours. Support opens at {[email protected]_open_time}.
93+
Create a support case using {[email protected]_case} and share the Case Number.
94+
else:
95+
| We are currently WITHIN business hours. Support is available now.
96+
Help the user resolve their issue directly.
97+
actions:
98+
create_case: @actions.create_case
99+
with subject=...
100+
```
101+
102+
## Try It Out
103+
104+
### Example Interaction
105+
106+
```text
107+
Agent: Hi! I can help you create support cases. If the user has an issue that requires support, please help them.
108+
109+
User: I'm having trouble logging into my account.
110+
111+
Agent: I understand you're having login issues. Since we're currently outside business hours, I've created a support case for you.
112+
113+
Your Case Number is 00001234. Our support team will follow up with you when we reopen at 9:00 AM Tomorrow.
114+
```
115+
116+
### Behind the Scenes
117+
118+
1. The user describes a login issue
119+
2. The reasoning block evaluates `next_open_time` and finds it has a value
120+
3. The conditional instruction mentioning `{[email protected]_case}` is included
121+
4. The LLM recognizes the action reference and calls `create_case` with an appropriate subject
122+
5. The flow returns a case number, which the agent shares along with the follow-up time
123+
124+
## What's Next
125+
126+
- **PromptTemplateActions**: Use Salesforce Prompt Templates for more complex generation scenarios.
127+
- **AdvancedInputBindings**: Learn more ways to bind inputs to actions dynamically.
128+
- **BeforeAfterReasoning**: Explore pre- and post-action reasoning patterns.

force-app/future_recipes/instructionActionReferences/aiAuthoringBundles/InstructionActionReferences/InstructionActionReferences.agent renamed to force-app/main/02_actionConfiguration/instructionActionReferences/aiAuthoringBundles/InstructionActionReferences/InstructionActionReferences.agent

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ variables:
1111

1212
system:
1313
messages:
14-
welcome: "Hi! I can help you create support cases."
14+
welcome: "Hi! I can help you create support cases. If the user has an issue that requires support, please help them."
1515
error: "Something went wrong."
1616

1717
instructions: "You are a support assistant."
@@ -33,22 +33,23 @@ topic case_management:
3333
description: "Creates a support case"
3434
inputs:
3535
subject: string
36-
description: "Subject of the case"
36+
description: "Subject of the case"
3737
outputs:
3838
case_number: string
39-
description: "The created case number"
39+
description: "The created case number"
4040
target: "flow://CreateCase"
4141

4242
reasoning:
4343
instructions:->
4444
| If the user has an issue that requires support, please help them.
4545

46-
If not within business hours, create a support case by using {[email protected]_case}. # Referencing the action directly guides the model to use it
47-
Share the Case Number and when to expect follow-up ({[email protected]_open_time}). # and also provides context variables in the same prompt
48-
46+
if @variables.next_open_time: # Procedural if block checks the variable directly
47+
| We are currently OUTSIDE business hours. Support opens at {[email protected]_open_time}.
48+
Create a support case using {[email protected]_case} and share the Case Number.
49+
else:
50+
| We are currently WITHIN business hours. Support is available now.
51+
Help the user resolve their issue directly.
4952
actions:
50-
create_case: @actions.create_case
51-
with subject=...
52-
# We don't need to manually set variables if the model reads the output directly
53-
# but usually good practice to do so.
53+
create_case: @actions.create_case
54+
with subject=...
5455

force-app/future_recipes/instructionActionReferences/aiAuthoringBundles/InstructionActionReferences/InstructionActionReferences.bundle-meta.xml renamed to force-app/main/02_actionConfiguration/instructionActionReferences/aiAuthoringBundles/InstructionActionReferences/InstructionActionReferences.bundle-meta.xml

File renamed without changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @description Test class for Instruction Action References Flows
3+
* Tests the CreateCase flow used in the instruction action references recipe
4+
*/
5+
@IsTest
6+
private class InstructionActionReferencesFlowTest {
7+
@IsTest
8+
static void testCreateCaseSuccess() {
9+
// Set input variables for the flow
10+
Map<String, Object> inputs = new Map<String, Object>();
11+
inputs.put('subject', 'Test Case Subject');
12+
13+
// Run the flow
14+
Test.startTest();
15+
Flow.Interview.CreateCase flowInterview = new Flow.Interview.CreateCase(
16+
inputs
17+
);
18+
flowInterview.start();
19+
Test.stopTest();
20+
21+
// Verify the expected outcome
22+
String caseNumber = (String) flowInterview.getVariableValue(
23+
'case_number'
24+
);
25+
26+
Assert.areNotEqual(null, caseNumber, 'Case number should be returned');
27+
Assert.areEqual(
28+
'00001234',
29+
caseNumber,
30+
'Case number should match the expected stub value'
31+
);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>65.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

force-app/future_recipes/instructionActionReferences/flows/CreateCase.flow-meta.xml renamed to force-app/main/02_actionConfiguration/instructionActionReferences/flows/CreateCase.flow-meta.xml

File renamed without changes.

0 commit comments

Comments
 (0)