(history: ChatMessage[], cfg: LLMConfig)
| 745 | |
| 746 | // Core conversation loop |
| 747 | const runConversation = async (history: ChatMessage[], cfg: LLMConfig) => { |
| 748 | await seedMetaFiles(); |
| 749 | await loadActionsFromMeta(); |
| 750 | const hasImageGen = !!imageGenConfigRef.current?.apiKey; |
| 751 | const mm = modManagerRef.current; |
| 752 | const char = characterRef.current; |
| 753 | |
| 754 | const tools = [ |
| 755 | getRespondToUserToolDef(), |
| 756 | getFinishTargetToolDef(), |
| 757 | getListAppsToolDefinition(), |
| 758 | getAppActionToolDefinition(), |
| 759 | ...getFileToolDefinitions(), |
| 760 | ...getMemoryToolDefinitions(), |
| 761 | ...(hasImageGen ? getImageGenToolDefinitions() : []), |
| 762 | ]; |
| 763 | |
| 764 | const currentMemories = memoriesRef.current; |
| 765 | const fullMessages: ChatMessage[] = [ |
| 766 | { role: 'system', content: buildSystemPrompt(char, mm, hasImageGen, currentMemories) }, |
| 767 | ...history, |
| 768 | ]; |
| 769 | |
| 770 | let currentMessages = fullMessages; |
| 771 | let iterations = 0; |
| 772 | const maxIterations = 10; |
| 773 | pendingToolCallsRef.current = []; |
| 774 | |
| 775 | while (iterations < maxIterations) { |
| 776 | iterations++; |
| 777 | const response = await chat(currentMessages, tools, cfg); |
| 778 | |
| 779 | if (response.toolCalls.length === 0) { |
| 780 | // No tool calls — fallback plain text (shouldn't happen with respond_to_user requirement) |
| 781 | if (response.content) { |
| 782 | addMessage({ |
| 783 | id: String(Date.now()), |
| 784 | role: 'assistant', |
| 785 | content: response.content, |
| 786 | toolCalls: |
| 787 | pendingToolCallsRef.current.length > 0 ? [...pendingToolCallsRef.current] : undefined, |
| 788 | }); |
| 789 | setChatHistory((prev) => [...prev, { role: 'assistant', content: response.content }]); |
| 790 | pendingToolCallsRef.current = []; |
| 791 | } |
| 792 | break; |
| 793 | } |
| 794 | |
| 795 | // Has tool calls |
| 796 | const assistantMsg: ChatMessage = { |
| 797 | role: 'assistant', |
| 798 | content: response.content, |
| 799 | tool_calls: response.toolCalls, |
| 800 | }; |
| 801 | currentMessages = [...currentMessages, assistantMsg]; |
| 802 | |
| 803 | // Execute each tool call |
| 804 | for (const tc of response.toolCalls) { |
no test coverage detected