* Simulate a full agent loop: call step() repeatedly until there are * no more tool calls, appending assistant + tool messages to history.
(
provider: ChatProvider,
toolset: SimpleToolset,
systemPrompt: string = '',
initialHistory: Message[] = [
{ role: 'user', content: [{ type: 'text', text: 'go' }], toolCalls: [] },
],
)
| 69 | * no more tool calls, appending assistant + tool messages to history. |
| 70 | */ |
| 71 | async function runAgentLoop( |
| 72 | provider: ChatProvider, |
| 73 | toolset: SimpleToolset, |
| 74 | systemPrompt: string = '', |
| 75 | initialHistory: Message[] = [ |
| 76 | { role: 'user', content: [{ type: 'text', text: 'go' }], toolCalls: [] }, |
| 77 | ], |
| 78 | ): Promise<{ messages: Message[]; turns: number }> { |
| 79 | const history: Message[] = [...initialHistory]; |
| 80 | let turns = 0; |
| 81 | const maxTurns = 10; |
| 82 | |
| 83 | while (turns < maxTurns) { |
| 84 | turns++; |
| 85 | const result = await step(provider, systemPrompt, toolset, history); |
| 86 | history.push(result.message); |
| 87 | |
| 88 | if (result.toolCalls.length === 0) { |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | const toolResults = await result.toolResults(); |
| 93 | for (const tr of toolResults) { |
| 94 | history.push({ |
| 95 | role: 'tool', |
| 96 | content: [ |
| 97 | { |
| 98 | type: 'text', |
| 99 | text: typeof tr.returnValue.output === 'string' ? tr.returnValue.output : '', |
| 100 | }, |
| 101 | ], |
| 102 | toolCallId: tr.toolCallId, |
| 103 | toolCalls: [], |
| 104 | }); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return { messages: history, turns }; |
| 109 | } |
| 110 | describe('e2e: multi-step agent loop', () => { |
| 111 | it('2-step loop: tool_call → result → final text', async () => { |
| 112 | const toolCall: ToolCall = { |
no test coverage detected