mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-29 07:51:17 +00:00
* 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
126 lines
4.4 KiB
TypeScript
126 lines
4.4 KiB
TypeScript
import {
|
|
clearApiProviders,
|
|
defaultApiRegistry,
|
|
getApiProvider,
|
|
registerApiProvider,
|
|
unregisterApiProviders,
|
|
} from "@openclaw/ai/internal/runtime";
|
|
import { registerBuiltInApiProviders, resetApiProviders } from "@openclaw/ai/providers";
|
|
// Covers dynamic registration of custom model API providers.
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { createAssistantMessageEventStream } from "../llm/utils/event-stream.js";
|
|
import { ensureCustomApiRegistered } from "./custom-api-registry.js";
|
|
import { buildAssistantMessageWithZeroUsage } from "./stream-message-shared.js";
|
|
|
|
function getRegisteredTestProvider() {
|
|
const provider = getApiProvider("test-custom-api");
|
|
if (!provider) {
|
|
throw new Error("expected test-custom-api provider to be registered");
|
|
}
|
|
return provider;
|
|
}
|
|
|
|
describe("ensureCustomApiRegistered", () => {
|
|
afterEach(() => {
|
|
clearApiProviders();
|
|
registerBuiltInApiProviders(defaultApiRegistry);
|
|
});
|
|
|
|
it("registers a custom api provider once", () => {
|
|
// Custom API registration is idempotent so repeated plugin setup does not
|
|
// replace provider entries or create duplicate sources.
|
|
const streamFn = vi.fn(() => createAssistantMessageEventStream());
|
|
|
|
expect(ensureCustomApiRegistered("test-custom-api", streamFn)).toBe(true);
|
|
expect(ensureCustomApiRegistered("test-custom-api", streamFn)).toBe(false);
|
|
|
|
const provider = getRegisteredTestProvider();
|
|
expect(typeof provider.stream).toBe("function");
|
|
expect(typeof provider.streamSimple).toBe("function");
|
|
});
|
|
|
|
it("delegates both stream entrypoints to the provided stream function", () => {
|
|
const stream = createAssistantMessageEventStream();
|
|
const streamFn = vi.fn(() => stream);
|
|
ensureCustomApiRegistered("test-custom-api", streamFn);
|
|
|
|
const provider = getRegisteredTestProvider();
|
|
|
|
const model = { api: "test-custom-api", provider: "custom", id: "m" };
|
|
const context = { messages: [] };
|
|
const options = { maxTokens: 32 };
|
|
|
|
expect(provider.stream(model as never, context as never, options as never)).toBe(stream);
|
|
expect(provider.streamSimple(model as never, context as never, options as never)).toBe(stream);
|
|
expect(streamFn).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("adapts async stream factories to the synchronous provider contract", async () => {
|
|
const message = buildAssistantMessageWithZeroUsage({
|
|
model: { api: "test-custom-api", provider: "custom", id: "m" },
|
|
content: [{ type: "text", text: "done" }],
|
|
stopReason: "stop",
|
|
});
|
|
const streamFn = vi.fn(async () => {
|
|
await Promise.resolve();
|
|
const stream = createAssistantMessageEventStream();
|
|
stream.push({ type: "done", reason: "stop", message });
|
|
return stream;
|
|
});
|
|
ensureCustomApiRegistered("test-custom-api", streamFn);
|
|
|
|
const provider = getRegisteredTestProvider();
|
|
const stream = provider.stream(
|
|
{ api: "test-custom-api", provider: "custom", id: "m" } as never,
|
|
{ messages: [] },
|
|
{},
|
|
);
|
|
|
|
expect(stream).not.toBeInstanceOf(Promise);
|
|
await expect(stream.result()).resolves.toBe(message);
|
|
});
|
|
|
|
it("converts async stream factory failures into terminal stream errors", async () => {
|
|
const streamFn = vi.fn(async () => {
|
|
throw new Error("factory failed");
|
|
});
|
|
ensureCustomApiRegistered("test-custom-api", streamFn);
|
|
|
|
const provider = getRegisteredTestProvider();
|
|
const stream = provider.stream(
|
|
{ api: "test-custom-api", provider: "custom", id: "m" } as never,
|
|
{ messages: [] },
|
|
{},
|
|
);
|
|
|
|
await expect(stream.result()).resolves.toMatchObject({
|
|
stopReason: "error",
|
|
errorMessage: "factory failed",
|
|
});
|
|
});
|
|
|
|
it("keeps plugin api providers when refreshing built-ins", () => {
|
|
// Built-in refresh should preserve plugin-owned API providers while
|
|
// repopulating core providers.
|
|
const sourceId = "plugin:test-reset-api";
|
|
const api = "test-reset-plugin-api";
|
|
const streamFn = vi.fn(() => createAssistantMessageEventStream());
|
|
const streamSimpleFn = vi.fn(() => createAssistantMessageEventStream());
|
|
registerApiProvider(
|
|
{
|
|
api,
|
|
stream: streamFn,
|
|
streamSimple: streamSimpleFn,
|
|
},
|
|
sourceId,
|
|
);
|
|
|
|
resetApiProviders(defaultApiRegistry);
|
|
|
|
expect(getApiProvider(api)).toBeDefined();
|
|
expect(getApiProvider("openai-responses")).toBeDefined();
|
|
|
|
unregisterApiProviders(sourceId);
|
|
});
|
|
});
|