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:0xranx OpenContext Tauri AI Commands

From Leeroopedia
Knowledge Sources
Domains AI, Tauri_Desktop, Streaming
Last Updated 2026-10-24 00:00 GMT

Overview

Tauri command handlers for AI configuration management and streaming chat completions, supporting both OpenAI-compatible APIs and local Ollama inference with Tauri event-based streaming.

Description

The ai.rs module in src-tauri/src/commands/ provides four Tauri commands: get_ai_config (reads AI provider, API key, model, and system prompt from the config file with key masking), save_ai_config (persists AI configuration to ~/.opencontext/config.json), ai_chat (streams chat completions via Tauri window events), and helper functions extract_stream_content and content_for_ollama for parsing multi-format stream responses. The ai_chat command supports two providers: OpenAI (SSE data: lines with [DONE] sentinel) and Ollama (NDJSON stream). Both emit AIStreamEvent objects via window.emit() with content, done, and error fields.

Usage

These commands are invoked from the React frontend via Tauri's invoke() API. Use get_ai_config / save_ai_config in settings screens, and ai_chat for streaming AI reflection responses in the idea journaling workflow.

Code Reference

Source Location

Signature

#[tauri::command]
pub(crate) fn get_ai_config() -> CmdResult<serde_json::Value>

#[tauri::command]
pub(crate) fn save_ai_config(options: SaveAIConfigOptions) -> CmdResult<serde_json::Value>

#[tauri::command]
pub(crate) async fn ai_chat(
    window: tauri::Window,
    options: AIChatOptions,
) -> CmdResult<()>

pub(crate) struct AIStreamEvent {
    content: Option<String>,
    done: Option<bool>,
    error: Option<String>,
}

pub(crate) struct SaveAIConfigOptions {
    provider: Option<String>,
    api_key: Option<String>,
    api_base: Option<String>,
    model: Option<String>,
    prompt: Option<String>,
}

pub(crate) struct AIChatOptions {
    messages: Vec<ChatMessage>,
    request_id: Option<String>,
    model: Option<String>,
}

Import

// Registered in src-tauri/src/main.rs invoke_handler:
// tauri::generate_handler![
//     commands::ai::get_ai_config,
//     commands::ai::save_ai_config,
//     commands::ai::ai_chat,
// ]

I/O Contract

Inputs

Name Type Required Description
messages Vec<ChatMessage> Yes Chat history with role and content
request_id Option<String> No Unique ID for event name (default "ai-stream")
model Option<String> No Override config model (default gpt-4o)
provider Option<String> No "openai" or "ollama" (default openai)
api_key Option<String> No OpenAI API key (masked on read)
api_base Option<String> No API base URL
prompt Option<String> No System prompt override

Outputs

Name Type Description
get_ai_config returns JSON provider, model, api_base, api_key_masked, has_api_key, prompt, default_prompt
save_ai_config returns JSON { success: true, config_path: String }
ai_chat emits AIStreamEvent Streamed via Tauri event "ai-stream-{requestId}"

Usage Examples

Frontend: Get AI Config

import { invoke } from '@tauri-apps/api/core';

const config = await invoke('get_ai_config');
console.log(config.model, config.has_api_key);

Frontend: Save AI Config

await invoke('save_ai_config', {
  options: {
    provider: 'openai',
    apiKey: 'sk-...',
    apiBase: 'https://api.openai.com/v1',
    model: 'gpt-4o',
    prompt: 'You are a helpful assistant.'
  }
});

Frontend: Stream AI Chat

import { invoke } from '@tauri-apps/api/core';
import { listen } from '@tauri-apps/api/event';

const requestId = crypto.randomUUID();

// Listen for stream events
const unlisten = await listen(`ai-stream-${requestId}`, (event) => {
  const { content, done, error } = event.payload;
  if (content) appendToUI(content);
  if (done) finishStream();
  if (error) showError(error);
});

// Start streaming
await invoke('ai_chat', {
  options: {
    messages: [{ role: 'user', content: 'Hello!' }],
    requestId,
  }
});

// Cleanup
await unlisten();

Related Pages

</content> </invoke>

Page Connections

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