Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AI QL Tuui messageStore

From Leeroopedia
Knowledge Sources
Domains LLM, Agentic_Loop, State_Management
Last Updated 2026-10-24 00:00 GMT

Overview

Concrete tool for orchestrating the chat message lifecycle and driving the agentic tool-call loop, provided by the tuui Electron application.

Description

The useMessageStore Pinia store is the chat orchestrator. sendMessage pushes the user message and triggers startInference. processInference calls createCompletion and, on success, invokes postToolCall. postToolCall checks the last assistant message for tool_calls; if present and non-empty, it iterates each tool_call, invokes mcpStore.callTool, converts results via contentConvert, appends tool messages to the conversation, and re-infers. The loop continues until the assistant produces no tool calls. generating tracks AbortControllers and lifecycle states ('prepare', 'toolcall').

Usage

Import this store when you need to: send a user message and start the inference loop, stop generation, manage the conversation state, or inspect the tool-call loop status.

Code Reference

Source Location

  • Repository: tuui
  • File: src/renderer/store/message.ts

Signature

export const useMessageStore = defineStore('messageStore', {
  state: (): MessageStoreState => ({
    userMessage: string,
    conversation: SessionEntry,
    base64: string,
    generating: Record<string, 'prepare' | AbortController | 'toolcall'>
  }),
  actions: {
    sendMessage(): string | undefined
    startInference(): string
    processInference(sessionId: SessionId): void
    postToolCall(sessionId: SessionId): Promise<void>
    contentConvert(
      content: CallToolResultContent,
      toolCallId: string
    ): Array<UserMessage | ToolMessage>
    stop(): void
    delete(id: string): boolean
    setConversation(conversation: SessionEntry): void
    resendMessage(): void
    deleteMessage({ index, range }): void
    handleKeydown(e: KeyboardEvent): void
    clear(): void
    init(): void
    initConversation(messages: ChatConversationMessage[]): void
  }
})

Import

import { useMessageStore } from '@/renderer/store/message'

I/O Contract

Inputs

Name Type Required Description
userMessage string Yes (sendMessage) User's text input from the chat box
base64 string No (sendMessage) Optional base64 image attached to the message
sessionId SessionId Yes (processInference/postToolCall) Conversation ID
content CallToolResultContent Yes (contentConvert) MCP tool result content array
toolCallId string Yes (contentConvert) ID of the tool call this result responds to

Outputs

Name Type Description
sendMessage undefined Returns conversation ID on success
contentConvert ToolMessage> Tool result messages (1 text-only, or 2 if image present)
side effect conversation.messages Tool messages appended, then re-inference triggered

Usage Examples

Send a Message and Run the Tool Loop

import { useMessageStore } from '@/renderer/store/message'

const messageStore = useMessageStore()

// Set the user's message
messageStore.userMessage = 'What files are in /tmp?'

// Send it — this triggers createCompletion, then postToolCall
messageStore.sendMessage()

// The tool loop runs automatically:
// 1. createCompletion → LLM responds with tool_calls
// 2. postToolCall → calls MCP tools, appends results
// 3. processInference → re-infers with tool results
// 4. Repeat until no more tool calls

Stop Generation

const messageStore = useMessageStore()
messageStore.stop()
// Aborts the AbortController and removes from generating map

Convert Tool Results

const messageStore = useMessageStore()

// Text-only result:
const messages = messageStore.contentConvert(
  [{ type: 'text', text: '42' }],
  'call_123'
)
// → [{ role: 'tool', content: '42', tool_call_id: 'call_123' }]

// Result with image:
const messagesWithImage = messageStore.contentConvert(
  [{ type: 'image', mimeType: 'image/png', data: 'base64...' }],
  'call_456'
)
// → [
//   { role: 'tool', content: 'Image provided in next user message', tool_call_id: 'call_456' },
//   { role: 'user', content: [{ type: 'image_url', image_url: { url: 'data:image/png;base64,...' } }] }
// ]

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment