Skip to content

Commit a627b79

Browse files
committed
Add pattern tool notes
1 parent fa6f69e commit a627b79

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/common/RECIPES.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,16 @@ Example: A meta-analyzer that finds all Person charms should use `wish("#allChar
13901390

13911391
Patterns can export functions that chatbots and other patterns can call programmatically using `patternTool`.
13921392

1393+
### When to Use patternTool
1394+
1395+
**Use `patternTool` when:**
1396+
- You need to parametrize the operation (e.g., supply a search term, filter criteria)
1397+
- The operation requires input from the caller beyond what's in the pattern's state
1398+
1399+
**Don't use `patternTool` when:**
1400+
- The chatbot just needs to read data - simply return the data alongside your UI
1401+
- Example: `return { [UI]: <MyUI />, items }` - chatbot can read `items` directly
1402+
13931403
### Basic Pattern Tool
13941404

13951405
```tsx
@@ -1413,12 +1423,33 @@ return {
14131423
};
14141424
```
14151425

1426+
### How Parameter Splitting Works
1427+
1428+
The second argument to `patternTool` determines which parameters are pre-filled vs. callable:
1429+
1430+
```tsx
1431+
// Function signature has: { items: Item[], query: string }
1432+
// Second argument supplies: { items }
1433+
// Result: query becomes the tool parameter
1434+
searchItems: patternTool(
1435+
({ items, query }: { items: Item[], query: string }) => {
1436+
return derive({ items, query }, ({ items, query }) =>
1437+
items.filter(item => item.text.includes(query))
1438+
);
1439+
},
1440+
{ items } // Pre-fill items, query is left as a parameter
1441+
)
1442+
```
1443+
1444+
**Key insight:** Only supply **some** of the function's inputs in the second argument. The remainder become tool parameters that the caller (chatbot/pattern) must provide.
1445+
14161446
### Pattern Tool Best Practices
14171447

14181448
1. **Return derived values**: patternTool functions should return `derive()` results
14191449
2. **Bind to pattern fields**: Second argument connects to your pattern's data
14201450
3. **Clear function signatures**: Type your inputs for better tool calling
14211451
4. **Useful operations**: Export things chatbots would want to do (search, summarize, extract)
1452+
5. **Parameter splitting**: Use the second argument to pre-fill pattern data, leaving caller-specific params open
14221453

14231454
### Example: Person Pattern Tools
14241455

0 commit comments

Comments
 (0)