Docs/TypeScript/AI Module Overview
AIai/README
AI Module Overview
The AI module provides a unified streaming interface across multiple LLM providers. It exposes model definitions, provider adapters, message types, and utility helpers.
Primary entrypoint: indusagi/ai.
The
indusagi/aisubpath resolves tosrc/facade/ai.ts, which simply re-exportssrc/facade/ml/index.ts. Everything documented here lives undersrc/facade/ml/.
Directory Map
src/facade/ai.tsis the published subpath; it doesexport * from "./ml/index".src/facade/ml/index.tsis the barrel that re-exports every public symbol.src/facade/ml/stream.tsprovidesstream,complete,streamSimple,completeSimple, andstreamByApi.src/facade/ml/models.tsholds the in-memoryModelRegistryand cost helpers.src/facade/ml/models.generated.tsis the generated model catalog (the registry seed).src/facade/ml/types.tsdefines message, tool, model, and event types plus runtime validators.src/facade/ml/api-registry.tsis the backend (provider) registry keyed by API tag.src/facade/ml/env-api-keys.tsresolves provider credentials from the environment.src/facade/ml/adapters/*contains provider adapters (Anthropic, OpenAI, Google, Bedrock, etc.).src/facade/ml/kit/*includes parsing, validation, overflow, event-stream, and TypeBox helpers.src/facade/ml/kit/auth/*contains the OAuth provider implementations.
The underlying clean-room implementation lives in src/llmgateway/, which the
facade layer surfaces under stable, public names.
Conceptual Flow
- Build a
Contextwith a system prompt, messages, and optional tool definitions. - Select a
Modelfrom the registry (getModel) or register a custom one. - Call
streamorstreamSimpleto get an async iterable of events. - Consume events to render incremental text, thinking, and tool calls.
- Optionally call
completeorcompleteSimpleto await the finalAssistantMessage.
The same Context and Message structure is used across all providers.
Provider adapters handle conversion to each vendor API format.
What This Module Does
- Normalizes message formats across providers.
- Handles tool calls and tool results.
- Supports text and image inputs where the model allows them.
- Emits a unified stream of incremental
AssistantMessageEventvalues. - Calculates token usage cost per model.
- Supports reasoning / thinking controls when the provider allows it (via
streamSimple).
Quick Start
import { getModel, streamSimple } from "indusagi/ai";
const model = getModel("anthropic", "claude-sonnet-4-5");
const stream = streamSimple(model, {
systemPrompt: "You are concise.",
messages: [{ role: "user", content: "Hello", timestamp: Date.now() }],
}, { reasoning: "low" });
for await (const event of stream) {
if (event.type === "text_delta") process.stdout.write(event.delta);
}
const final = await stream.result();
console.log(final.usage.totalTokens);
For details, see:
