(message: string, onFirstText?: () => void)
| 19 | } |
| 20 | |
| 21 | async process(message: string, onFirstText?: () => void): Promise<string> { |
| 22 | const session = sessionManager.current(); |
| 23 | |
| 24 | // Add user message to session |
| 25 | sessionManager.addMessage(session.id, { |
| 26 | id: randomUUID(), |
| 27 | role: 'user', |
| 28 | content: message, |
| 29 | timestamp: Date.now() |
| 30 | }); |
| 31 | |
| 32 | await messageBus.publish('agent:message', { message }, { |
| 33 | sessionId: session.id, |
| 34 | agentId: this.config.id |
| 35 | }); |
| 36 | |
| 37 | emitCliTimelineEvent({ |
| 38 | kind: 'command', |
| 39 | phase: 'running', |
| 40 | title: 'Task Received', |
| 41 | text: message, |
| 42 | }); |
| 43 | |
| 44 | // Select provider with fallback support |
| 45 | const { provider, resolvedModel } = selectProvider( |
| 46 | this.config.model, |
| 47 | this.config.provider |
| 48 | ); |
| 49 | |
| 50 | const tools = this.getTools(); |
| 51 | |
| 52 | // Execute with automatic fallback on error |
| 53 | let fullResponse = ''; |
| 54 | let continueLoop = true; |
| 55 | let loopCount = 0; |
| 56 | const maxLoops = 5; // Prevent infinite loops |
| 57 | let firstTextReceived = false; |
| 58 | |
| 59 | while (continueLoop && loopCount < maxLoops) { |
| 60 | loopCount++; |
| 61 | continueLoop = false; |
| 62 | const toolCalls: Array<{ id: string; name: string; arguments: string }> = []; |
| 63 | let currentToolCall: { id: string; name: string; arguments: string } | null = null; |
| 64 | |
| 65 | await executeWithFallback(provider, resolvedModel, async (activeProvider) => { |
| 66 | const toolsFormatted = formatToolsForProvider(activeProvider, tools); |
| 67 | const stream = activeProvider.streamText({ |
| 68 | model: resolvedModel, |
| 69 | messages: session.messages as any, |
| 70 | tools: toolsFormatted as any, |
| 71 | systemPrompt: this.config.systemPrompt, |
| 72 | temperature: this.config.temperature, |
| 73 | maxTokens: this.config.maxTokens |
| 74 | }); |
| 75 | |
| 76 | for await (const chunk of stream) { |
| 77 | if (chunk.type === 'text' && chunk.content) { |
| 78 | // On first text, clear spinner and keep "Agent:" label |
no test coverage detected