Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

subagentRoute.md

99 lines2.4 KB

1---
2id: subagentRoute
3title: subagentRoute
4---
5​
6```ts
7function subagentRoute<TAgents>(agents, options?): object;
8```
9​
10Defined in: packages/ai/src/activities/chat/agents/route.ts:94
11​
12Build `decide()` questions for a subagent router.
13​
14One yes/no question per agent, plus an `order` choice.
15`pick` returns `main`, one name, `{ names, order }`, or `{ steps }`.
16Names follow the `agents` array order.
17`{ names, order }` overrides `subagents.order` for that turn.
18`then`: agents that run after the other selected agents. The lead group
19starts together. The `then` agents then run one after another in list
20order, and each reads the text so far. Used only when the router picks at
21least one agent from each group. Otherwise `pick` returns `{ names, order }`.
22​
23An agent with `inputSchema` needs input. `needsInput(result)` lists the
24picked agents that need it, with their schemas. Make each input, then pass
25them as `pick(result, { inputs })`. Each name with an input becomes
26`{ name, input }`. `pick` throws when a picked agent with a schema has no
27input.
28​
29## Type Parameters
30​
31### TAgents
32​
33`TAgents` *extends* readonly [`DefinedAgent`](../interfaces/DefinedAgent.md)\<`string`, readonly `SubagentTool`[], [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined`, readonly [`InterruptDefinition`](../interfaces/InterruptDefinition.md)\<`any`, `any`, `any`, `any`, `any`\>[], `any`, `unknown`, [`AgentProduces`](../type-aliases/AgentProduces.md) \| `undefined`\>[]
34​
35## Parameters
36​
37### agents
38​
39`TAgents`
40​
41### options?
42​
43[`SubagentRouteOptions`](../interfaces/SubagentRouteOptions.md)\<`TAgents`\>
44​
45## Returns
46​
47### needsInput
48​
49```ts
50needsInput: (result) => InputNeed<TAgents[number]>[];
51```
52​
53The picked agents that have an `inputSchema`, in agent-list order. Make
54an input for each one and pass it to `pick` in `inputs`.
55​
56#### Parameters
57​
58##### result
59​
60`RouteAnswers`\<`TAgents`\>
61​
62#### Returns
63​
64`InputNeed`\<`TAgents`\[`number`\]\>[]
65​
66### pick
67​
68```ts
69pick: (result, pickOptions?) => SubagentRouterPick;
70```
71​
72#### Parameters
73​
74##### result
75​
76`RouteAnswers`\<`TAgents`\>
77​
78##### pickOptions?
79​
80`RoutePickOptions`\<`TAgents`\>
81​
82#### Returns
83​
84[`SubagentRouterPick`](../type-aliases/SubagentRouterPick.md)
85​
86### questions
87​
88```ts
89questions: RouteQuestions<TAgents>;
90```
91​
92## Example
93​
94```ts
95const route = subagentRoute(agents)
96const result = await decide({ adapter, state, questions: route.questions })
97const inputs = { pricer: { sku: 'A-1' } }
98return route.pick(result, { inputs })
99```