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

Environment:AI QL Tuui OpenAI Compatible LLM

From Leeroopedia
Knowledge Sources
Domains LLM, Streaming, API
Last Updated 2026-10-24 20:38 GMT

Overview

Any OpenAI-compatible LLM API endpoint supporting chat completions with streaming (SSE), tool calls, and reasoning content extraction.

Description

tuui communicates with LLM providers via the OpenAI-compatible Chat Completions API. The provider URL, model name, API key, content type, and streaming mode are all configurable through llm.json. The application uses the browser fetch API (not axios) to support streaming via ReadableStream. It supports standard Authorization: Bearer headers, x-api-key headers (for Anthropic-style APIs), dynamic JWT token refresh, reasoning effort configuration, and enable_thinking toggles for models like Qwen.

Usage

Use this environment for any implementation that sends chat completion requests to an LLM API. The createCompletion function requires a configured OpenAI-compatible endpoint.

System Requirements

Category Requirement Notes
Network HTTP/HTTPS access to LLM endpoint webSecurity: false allows cross-origin
Runtime Chromium fetch API Uses browser-native fetch with streaming support

Dependencies

NPM Packages

  • `jwt-decode` >= 4.0.0 — JWT token expiry checking for dynamic token refresh

Configuration (llm.json)

  • url — Base URL of the LLM API
  • path — API path (e.g., /v1/chat/completions)
  • model — Model identifier (e.g., gpt-4o)
  • apiKey — API key for authentication
  • apiCli — Optional CLI command for dynamic token retrieval
  • stream — Enable SSE streaming (boolean)
  • contentType — Content-Type header
  • authorization — Whether to use Authorization header (boolean)
  • authPrefix — Auth prefix (e.g., Bearer)
  • maxTokensValue / maxTokensPrefix — Max tokens configuration
  • temperature — Sampling temperature
  • topP — Top-p sampling

Credentials

The following configuration values may be required depending on the provider:

  • apiKey — API key for the LLM provider (stored in llm.json, not in code)
  • apiCli — CLI command that returns a fresh JWT token (for dynamic token providers)

NEVER commit actual API keys to the repository. The llm.json in the repo contains default/placeholder values.

Quick Install

npm install jwt-decode@^4.0.0

Code Evidence

Streaming fetch request from `src/renderer/composables/chatCompletions.ts:267-273`:

const completion = await fetch(
  chatbotConfig.url + (chatbotConfig.path ? chatbotConfig.path : ''),
  { ...request, signal: abortController.signal }
)

SSE data line parsing from `src/renderer/composables/chatCompletions.ts:380-393`:

parts
  .map((line) => line.trim())
  .filter((line) => line.length > 0)
  .forEach((line) => {
    const pos = line.indexOf(':')
    const name = line.substring(0, pos)
    if (name !== 'data') return
    const content = line.substring(pos + 1).trim()
    if (content === '[DONE]') return
    parseJson(content, target)
  })

JWT expiry check from `src/renderer/composables/chatCompletions.ts:96-106`:

const payload = jwtDecode(chatbotConfig.apiKey)
const currentTime = Math.floor(Date.now() / 1000)
const remaining = payload.exp - currentTime
if (remaining <= 1) {
  return await updateToken(chatbotConfig.apiCli)
}

Common Errors

Error Message Cause Solution
`401: Unauthorized` Invalid or expired API key Check apiKey in llm.json; configure apiCli for auto-refresh
`snackbar.parse-stream-fail` Response body is not a readable stream Ensure provider returns SSE format; check stream: true in config
`chat.token-fail` Token refresh CLI failed Check apiCli command exists and returns valid token
Redirected URL warning API returned a redirect Check the url and path configuration

Compatibility Notes

  • Anthropic-style APIs: Set authorization: false to use x-api-key header instead of Authorization: Bearer.
  • Qwen / thinking models: enable_thinking and chat_template_kwargs fields are set when configured.
  • Non-streaming mode: When stream: false, the entire response is parsed as a single JSON object.
  • Custom token providers: apiCli spawns a shell command to retrieve fresh tokens; output is read from stdout.

Related Pages

Page Connections

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