Skip to content

Commit 3707ae6

Browse files
committed
make sure requiredToFetch context reflects that the context fetch was intentionally skipped rather than failed
1 parent b179a5c commit 3707ae6

File tree

6 files changed

+484
-4
lines changed

6 files changed

+484
-4
lines changed

agents-docs/content/docs/typescript-sdk/context-fetchers.mdx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,59 @@ transform: "user.profile.displayName"; // Result: "John Doe"
130130
transform: "items[0].name"; // Result: "First Item"
131131
```
132132

133+
## Optional Context Fetches
134+
135+
Sometimes you want to fetch context data only when certain conditions are met—for example, when a specific header is provided. The `requiredToFetch` property lets you specify template variables that must resolve to non-empty values for the fetch to execute.
136+
137+
If any required variable is missing or resolves to an empty string, the fetch is **skipped** (not treated as an error), and the `defaultValue` is used if provided.
138+
139+
```typescript
140+
import { agent, subAgent } from "@inkeep/agents-sdk";
141+
import { contextConfig, fetchDefinition, headers } from "@inkeep/agents-core";
142+
import { z } from "zod";
143+
144+
// Define headers schema - x-conversation-id is optional
145+
const chatHeaders = headers({
146+
schema: z.object({
147+
"x-tenant-id": z.string(),
148+
"x-project-id": z.string(),
149+
"x-conversation-id": z.string().optional(), // Optional header
150+
})
151+
});
152+
153+
// This fetch only runs when x-conversation-id header is provided
154+
const conversationHistoryFetcher = fetchDefinition({
155+
id: "conversation-history",
156+
name: "Conversation History",
157+
trigger: "initialization",
158+
fetchConfig: {
159+
url: `https://api.example.com/conversations/${chatHeaders.toTemplate('x-conversation-id')}`,
160+
method: "GET",
161+
// Only fetch if x-conversation-id header is present and non-empty
162+
requiredToFetch: [chatHeaders.toTemplate('x-conversation-id')],
163+
},
164+
responseSchema: z.object({
165+
messages: z.array(z.object({
166+
role: z.string(),
167+
content: z.string(),
168+
})),
169+
}),
170+
defaultValue: { messages: [] }, // Used when header is not provided
171+
});
172+
173+
const chatContext = contextConfig({
174+
headers: chatHeaders,
175+
contextVariables: {
176+
history: conversationHistoryFetcher,
177+
},
178+
});
179+
```
180+
181+
**When to use `requiredToFetch`:**
182+
- Fetching user-specific data when an optional user ID header is provided
183+
- Loading conversation history only when continuing an existing conversation
184+
- Conditional data loading based on optional API keys or authentication headers
185+
133186
## Best Practices
134187

135188
1. **Use Appropriate Triggers**
@@ -142,6 +195,11 @@ transform: "items[0].name"; // Result: "First Item"
142195
- Always provide a `defaultValue`
143196
- Use appropriate response schemas
144197

198+
3. **Use `requiredToFetch` for Optional Dependencies**
199+
200+
- Specify required template variables for conditional fetches
201+
- Provide a `defaultValue` to handle skipped fetches gracefully
202+
145203
## Related documentation
146204

147205
- [Headers](/typescript-sdk/headers) - Learn how to pass dynamic context to your agents via HTTP headers

agents-docs/content/docs/visual-builder/context-fetchers.mdx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Each key in the JSON should map to a fetch definition with the following propert
5353
- **`body`** (optional): Request body for POST/PUT/PATCH requests
5454
- **`transform`** (optional): JSONPath expression or JavaScript transform function to extract specific data from the response
5555
- **`timeout`** (optional): Request timeout in milliseconds (defaults to 10000)
56+
- **`requiredToFetch`** (optional): Array of template variables that must resolve to non-empty values for the fetch to execute. If any variable is missing or empty, the fetch is skipped and `defaultValue` is used instead. Useful for optional fetches that depend on request headers.
5657
- **`responseSchema`** (optional): Valid JSON Schema object to validate the API response structure.
5758
- **`defaultValue`** (optional): Default value to use if the fetch fails or returns no data
5859
- **`credential`** (optional): Reference to stored credentials for authentication
@@ -91,6 +92,33 @@ Here is an example of a valid Context Variables JSON object:
9192
}
9293
```
9394

95+
### Optional Context Fetches
96+
97+
Use `requiredToFetch` when you want a fetch to only execute when certain headers are provided. This is useful for optional data that enhances the agent experience but isn't required.
98+
99+
Here's an example that fetches conversation history only when the `x-conversation-id` header is provided:
100+
101+
```json
102+
{
103+
"conversationHistory": {
104+
"id": "conversation-history",
105+
"name": "Conversation History",
106+
"trigger": "initialization",
107+
"fetchConfig": {
108+
"url": "https://api.example.com/conversations/{{headers.x-conversation-id}}",
109+
"method": "GET",
110+
"requiredToFetch": ["{{headers.x-conversation-id}}"]
111+
},
112+
"defaultValue": { "messages": [] }
113+
}
114+
}
115+
```
116+
117+
When `x-conversation-id` is not provided in the request headers:
118+
- The fetch is **skipped** (not treated as an error)
119+
- The `defaultValue` is used instead
120+
- The agent continues normally with an empty message history
121+
94122
## Using Context Variables
95123
Once you have defined your context variables, you can use them in your agent prompts.
96124
1. Click on the agent you want to modify.

0 commit comments

Comments
 (0)