Skip to content

Commit

Permalink
fix: agent multi-steps too eager
Browse files Browse the repository at this point in the history
  • Loading branch information
grabbou committed Dec 11, 2024
1 parent 3df8cf5 commit 4e70c58
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions packages/framework/src/supervisor/runAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function runAgent(
agent: Agent,
agentContext: Message[],
agentRequest: Message[]
): Promise<[Message, 'step' | 'complete' | 'tool']> {
): Promise<[Message[], 'step' | 'complete' | 'tool']> {
const tools = agent.tools
? Object.entries(agent.tools).map(([name, tool]) =>
zodFunction({
Expand Down Expand Up @@ -60,15 +60,10 @@ export async function runAgent(
name: z.string().describe('The name of the step'),
result: z.string().describe('The result of the step'),
reasoning: z.string().describe('The reasoning for this step'),
}),
z.object({
kind: z.literal('complete'),
result: z
nextStep: z
.string()
.describe(
'The final result of the task. Include all relevant information from previous steps.'
),
reasoning: z.string().describe('The reasoning for completing the task'),
.nullable()
.describe('The next step to complete the task, or null if task is complete'),
}),
z.object({
kind: z.literal('error'),
Expand All @@ -81,7 +76,7 @@ export async function runAgent(
})

if (response.choices[0].message.tool_calls.length > 0) {
return [response.choices[0].message, 'tool']
return [[response.choices[0].message], 'tool']
}

const result = response.choices[0].message.parsed
Expand All @@ -93,11 +88,25 @@ export async function runAgent(
throw new Error(result.response.reasoning)
}

return [
const agentResponse = [
{
role: 'assistant',
role: 'assistant' as const,
content: result.response.result,
},
result.response.kind,
]

if (result.response.nextStep) {
return [
[
...agentResponse,
{
role: 'user',
content: result.response.nextStep,
},
],
'step',
]
}

return [agentResponse, 'complete']
}

0 comments on commit 4e70c58

Please sign in to comment.