Files
openclaw/examples/ai-chat/index.mjs
Peter Steinberger 062f88e3e3 refactor: extract reusable AI runtime package (#99059)
* refactor: extract reusable AI runtime package

* refactor: complete AI provider relocation

* refactor: keep llm core internal

* refactor(ai): make @openclaw/ai self-contained with host policy ports

Move pure transport helpers (tool projections, strict-schema normalization,
prompt-cache boundary, stream guards, anthropic/openai compat, request
activity) from src into packages/ai; move utf16-slice into
normalization-core. Inject host policy (guarded fetch, redaction,
strict-tool defaults, diagnostics logging) through AiTransportHost with
inert library defaults installed by src/llm/stream.ts. Narrow the public
barrel to instance-scoped createApiRegistry/createLlmRuntime; the
process-default runtime moves behind internal/ and
registerBuiltInApiProviders takes an explicit registry. Delete the
src/llm/api-registry re-export facade.

* fix(ai): teach node, jiti, and vite resolvers the @openclaw/ai and utf16-slice subpaths

The workspace alias tables in root-alias.cjs, plugin-sdk-native-resolver,
sdk-alias, the shared vitest config, and the Control UI vite config only
knew @openclaw/llm-core; Node-side plugin loading resolved @openclaw/ai
through the pnpm symlink to the unbuilt dist (checks-node-compact CI
failures), and the Control UI build broke on the new
normalization-core/utf16-slice subpath.

* chore(ui): drop leftover service-worker debug logging

* build(release): ship @openclaw/ai with its own shrinkwrap and honest dependency set

packages/ai declares only its six real runtime deps (kysely, chalk, json5,
tslog, zod, fs-safe, and proxyline were never imported); orphaned root deps
removed. generate-npm-shrinkwrap now treats publishable packages/* like
publishable plugins so the AI tarball pins its transitive tree even though
workspace deps are omitted from the root shrinkwrap. knip learns the
package entry points; the tsdown dts neverBundle option moves to its
documented deps.dts home; the README documents the no-semver internal/*
contract and host ports.

* docs(ai): add minimal external-consumer example app

examples/ai-chat consumes only the public @openclaw/ai surface (built dist
via the workspace link): isolated runtime, built-in provider registration,
one streamed completion. Supports Anthropic/OpenAI via env keys and a
keyless local Ollama target; live-verified against Ollama.

* docs(ai): document the @openclaw/ai package and workspace shrinkwrap boundary

* chore(check): include examples/ in duplicate-scan targets

* fix: emit normalization package subpaths

* fix: complete AI package boundary artifacts

* fix: align AI package boundary contracts

* fix(ci): stabilize package release contracts

* test: align documentation contract checks

* test: keep cron docs guard aligned

* test: align restored docs contract guards

* test: follow upstream docs contracts

* docs: drop superseded talk wording
2026-07-05 01:56:40 -04:00

86 lines
2.8 KiB
JavaScript

// Minimal @openclaw/ai consumer: one isolated runtime, built-in providers,
// one streamed completion. Uses only the public package surface — no OpenClaw
// application code. Run with:
// ANTHROPIC_API_KEY=... node index.mjs "your prompt"
// OPENAI_API_KEY=... node index.mjs --provider openai "your prompt"
import { createLlmRuntime } from "@openclaw/ai";
import { registerBuiltInApiProviders } from "@openclaw/ai/providers";
const MODELS = {
anthropic: {
id: "claude-sonnet-4-6",
name: "Claude Sonnet 4.6",
api: "anthropic-messages",
provider: "anthropic",
baseUrl: "https://api.anthropic.com",
reasoning: true,
input: ["text"],
cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
contextWindow: 200_000,
maxTokens: 8192,
},
openai: {
id: "gpt-5.5",
name: "GPT-5.5",
api: "openai-responses",
provider: "openai",
baseUrl: "https://api.openai.com/v1",
reasoning: true,
input: ["text"],
cost: { input: 1.25, output: 10, cacheRead: 0.125, cacheWrite: 0 },
contextWindow: 400_000,
maxTokens: 16_384,
},
// Local Ollama server; no API key required.
ollama: {
id: process.env.OLLAMA_MODEL || "llama3.2:latest",
name: "Ollama",
api: "openai-completions",
provider: "ollama",
baseUrl: "http://localhost:11434/v1",
reasoning: false,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 32_000,
maxTokens: 4096,
},
};
const args = process.argv.slice(2);
const providerFlag = args.indexOf("--provider");
const provider = providerFlag === -1 ? "anthropic" : args[providerFlag + 1];
const prompt =
args.filter((_, i) => i !== providerFlag && i !== providerFlag + 1).join(" ") ||
"Reply with one short sentence: what is @openclaw/ai?";
const model = MODELS[provider];
if (!model) {
console.error(`Unknown provider "${provider}". Use one of: ${Object.keys(MODELS).join(", ")}`);
process.exit(1);
}
const runtime = createLlmRuntime();
registerBuiltInApiProviders(runtime.registry);
const stream = runtime.streamSimple(
model,
{ messages: [{ role: "user", content: prompt, timestamp: Date.now() }] },
// Ollama ignores credentials but the OpenAI-compatible transport requires one.
provider === "ollama" ? { apiKey: "ollama" } : undefined,
);
for await (const event of stream) {
if (event.type === "text_delta") {
process.stdout.write(event.delta);
}
}
const result = await stream.result();
process.stdout.write("\n");
if (result.stopReason === "error" || result.stopReason === "aborted") {
console.error(`error: ${result.errorMessage ?? result.stopReason}`);
process.exit(1);
}
const { input, output } = result.usage;
console.error(`[${model.id}] stop=${result.stopReason} tokens in=${input} out=${output}`);