ModularCore Hub
← Volver al catálogo

AI Chat

ai v0.1.0 react, svelte, web

Headless AI chat core (OpenAI-compatible streaming, model fallback, token estimation, function calling with human-in-the-loop confirmation, local/backend history) with React, Svelte, and framework-free Web adapters. BYOK — the API key always comes from your app's config, never hardcoded.

Variables de entorno

ClaveRequeridaDescripción
OPENROUTER_API_KEYNoAPI key for OpenRouter, the default OpenAI-compatible provider (baseURL https://openrouter.ai/api/v1). BYOK — passed by your app into ChatConfig.apiKey, never hardcoded in this component.
OPENAI_API_KEYNoAPI key used only if you set LLM_BASE_URL to point at OpenAI directly instead of OpenRouter.
LLM_BASE_URLNoOptional override for ChatConfig.baseURL. Defaults to OpenRouter; set to any OpenAI-compatible /chat/completions endpoint (OpenAI, LiteLLM, self-hosted gateway).

Instalación manual

Descarga el tarball y copia cada archivo listado abajo a su ruta destino:

curl -L -o ai-chat.tar.gz https://TU_HOST/registry/ai-chat.tar.gz
tar -xzf ai-chat.tar.gz
OrigenDestino en tu proyecto
core/client.tssrc/modularcore/ai-chat/core/client.ts
core/stream.tssrc/modularcore/ai-chat/core/stream.ts
core/chat.tssrc/modularcore/ai-chat/core/chat.ts
core/fallback.tssrc/modularcore/ai-chat/core/fallback.ts
core/tokens.tssrc/modularcore/ai-chat/core/tokens.ts
core/tools.tssrc/modularcore/ai-chat/core/tools.ts
core/history/types.tssrc/modularcore/ai-chat/core/history/types.ts
core/history/local.tssrc/modularcore/ai-chat/core/history/local.ts
core/history/backend.tssrc/modularcore/ai-chat/core/history/backend.ts
ui/markdown.tssrc/modularcore/ai-chat/ui/markdown.ts
adapters/react/use-chat.tssrc/modularcore/ai-chat/adapters/react/use-chat.ts
adapters/svelte/create-chat.svelte.tssrc/modularcore/ai-chat/adapters/svelte/create-chat.svelte.ts
adapters/web/chat-element.tssrc/modularcore/ai-chat/adapters/web/chat-element.ts

Instalación con CLI

modularcore add ai-chat

Documentación

# @modularcore/ai-chat

Headless AI chat core — OpenAI-compatible streaming, model fallback, token usage tracking,
function calling with human-in-the-loop confirmation, and local/backend history — with thin
React, Svelte, and framework-free Web adapters on top.

BYOK (Bring Your Own Key). The core never hardcodes or reads an API key from the
environment itself: your app passes apiKey (and optionally baseURL) into ChatConfig. This
lets you point at OpenRouter (the default), OpenAI directly, or any other OpenAI-compatible
/chat/completions endpoint — including your own server-side proxy, so the key never reaches
the browser.

## What's in this package

- core/client.ts — thin fetch-based OpenAI-compatible client (requestChatCompletionStream).
- core/stream.ts — real SSE parser over the raw ReadableStream<Uint8Array> fetch returns.
- core/chat.tsChat, the headless orchestrator (streaming, tool-call loop, history).
- core/fallback.ts — tries an ordered list of models, falling back on failure.
- core/tokens.ts — parses provider usage payloads.
- core/tools.ts — function-calling registry + human-in-the-loop dispatch.
- core/history/* — pluggable conversation history (local storage or your own backend).
- ui/markdown.ts — dependency-free Markdown → HTML renderer that escapes all untrusted text
before adding any formatting markup, so its output is safe to assign to innerHTML/{@html}.
- adapters/react, adapters/svelte, adapters/web — thin bindings over Chat for each
framework (Svelte adapter uses Svelte 5 runes).

## Basic usage (Svelte 5)

import { createChat } from '@modularcore/ai-chat/svelte';

const chat = createChat({
  apiKey: import.meta.env.VITE_OPENROUTER_API_KEY, // or proxy through your own backend
  models: ['openai/gpt-4o-mini'],
});

await chat.send('Hello!');


For a public-facing playground, never ship a real key to the client — proxy requests through a
server-side endpoint that injects the key (see apps/web/src/routes/api/chat/+server.ts in this
monorepo for a hardened example: rate limiting, a model allowlist, a max-tokens cap, tools
disabled, and real streaming passthrough).

## More docs

- [docs/backend-history-contract.md](./docs/backend-history-contract.md) — the contract your
backend must implement if you use core/history/backend.ts instead of local storage.