-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathresource_planner.ts
58 lines (52 loc) · 1.7 KB
/
resource_planner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import s from 'dedent'
import { zodResponseFormat } from 'openai/helpers/zod'
import { z } from 'zod'
import { agent, AgentOptions } from '../agent.js'
import { handoff, request, response } from '../state.js'
const defaults: AgentOptions = {
run: async (state, context, workflow) => {
const res = await workflow.team[state.agent].provider.completions({
messages: [
{
role: 'system',
content: s`
You are an agent selector that matches tasks to the most capable agent.
Analyze the task requirements and each agent's capabilities to select the best match.
Consider:
1. Required tools and skills
2. Agent's specialization
3. Model capabilities
4. Previous task context if available
`,
},
request(s`
Here are the available agents:
<agents>
${Object.entries(workflow.team).map(([name, agent]) =>
agent.description ? `<agent name="${name}">${agent.description}</agent>` : ''
)}
</agents>`),
response('What is the task?'),
...state.messages,
],
temperature: 0.1,
response_format: zodResponseFormat(
z.object({
agent: z.enum(Object.keys(workflow.team) as [string, ...string[]]),
reasoning: z.string(),
}),
'agent_selection'
),
})
const message = res.choices[0].message.parsed
if (!message) {
throw new Error('No content in response')
}
return handoff(state, message.agent, state.messages)
},
}
export const resourcePlanner = (options?: AgentOptions) =>
agent({
...defaults,
...options,
})