mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-12 02:56:07 +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
135 lines
4.6 KiB
TypeScript
135 lines
4.6 KiB
TypeScript
// Normalizes provider model compatibility metadata from plugins.
|
|
import {
|
|
resolveUnsupportedToolSchemaKeywords,
|
|
shouldOmitEmptyArrayItems,
|
|
} from "@openclaw/ai/internal/openai";
|
|
import { detectOpenAICompletionsCompat } from "../agents/openai-completions-compat.js";
|
|
import type { ModelCompatConfig } from "../config/types.models.js";
|
|
import type { Model } from "../llm/types.js";
|
|
|
|
export function extractModelCompat(
|
|
modelOrCompat: { compat?: unknown } | ModelCompatConfig | undefined,
|
|
): ModelCompatConfig | undefined {
|
|
if (!modelOrCompat || typeof modelOrCompat !== "object") {
|
|
return undefined;
|
|
}
|
|
if ("compat" in modelOrCompat) {
|
|
const compat = (modelOrCompat as { compat?: unknown }).compat;
|
|
return compat && typeof compat === "object" ? (compat as ModelCompatConfig) : undefined;
|
|
}
|
|
return modelOrCompat as ModelCompatConfig;
|
|
}
|
|
|
|
/** @deprecated Provider-owned model compat helper; do not use from third-party plugins. */
|
|
export function applyModelCompatPatch<T extends { compat?: ModelCompatConfig }>(
|
|
model: T,
|
|
patch: Partial<ModelCompatConfig> & Record<string, unknown>,
|
|
): T {
|
|
const nextCompat = { ...model.compat, ...patch } as ModelCompatConfig;
|
|
const currentCompat = model.compat as (Record<string, unknown> & ModelCompatConfig) | undefined;
|
|
if (
|
|
model.compat &&
|
|
Object.entries(patch).every(([key, value]) => currentCompat?.[key] === value)
|
|
) {
|
|
return model;
|
|
}
|
|
return {
|
|
...model,
|
|
compat: nextCompat,
|
|
};
|
|
}
|
|
|
|
export function hasToolSchemaProfile(
|
|
modelOrCompat: { compat?: unknown } | ModelCompatConfig | undefined,
|
|
profile: string,
|
|
): boolean {
|
|
return extractModelCompat(modelOrCompat)?.toolSchemaProfile === profile;
|
|
}
|
|
|
|
export function hasNativeWebSearchTool(
|
|
modelOrCompat: { compat?: unknown } | ModelCompatConfig | undefined,
|
|
): boolean {
|
|
return extractModelCompat(modelOrCompat)?.nativeWebSearchTool === true;
|
|
}
|
|
|
|
export function resolveToolCallArgumentsEncoding(
|
|
modelOrCompat: { compat?: unknown } | ModelCompatConfig | undefined,
|
|
): ModelCompatConfig["toolCallArgumentsEncoding"] | undefined {
|
|
return extractModelCompat(modelOrCompat)?.toolCallArgumentsEncoding;
|
|
}
|
|
|
|
// Tool-schema compat predicates moved into @openclaw/ai (agent-tools-parameter-schema);
|
|
// re-export so existing core/plugin callers keep one canonical import site.
|
|
export { resolveUnsupportedToolSchemaKeywords, shouldOmitEmptyArrayItems };
|
|
|
|
function isOpenAiCompletionsModel(model: Model): model is Model<"openai-completions"> {
|
|
return model.api === "openai-completions";
|
|
}
|
|
|
|
function isAnthropicMessagesModel(model: Model): model is Model<"anthropic-messages"> {
|
|
return model.api === "anthropic-messages";
|
|
}
|
|
|
|
function normalizeAnthropicBaseUrl(baseUrl: string): string {
|
|
return baseUrl.replace(/\/v1\/?$/, "");
|
|
}
|
|
|
|
export function normalizeModelCompat(model: Model): Model {
|
|
const baseUrl = model.baseUrl ?? "";
|
|
|
|
if (isAnthropicMessagesModel(model) && baseUrl) {
|
|
const normalized = normalizeAnthropicBaseUrl(baseUrl);
|
|
if (normalized !== baseUrl) {
|
|
return { ...model, baseUrl: normalized } as Model<"anthropic-messages">;
|
|
}
|
|
}
|
|
|
|
if (!isOpenAiCompletionsModel(model)) {
|
|
return model;
|
|
}
|
|
|
|
const compat = model.compat ?? undefined;
|
|
const detectedCompatDefaults = baseUrl
|
|
? detectOpenAICompletionsCompat(model).defaults
|
|
: undefined;
|
|
const needsForce = Boolean(
|
|
detectedCompatDefaults &&
|
|
(!detectedCompatDefaults.supportsDeveloperRole ||
|
|
!detectedCompatDefaults.supportsUsageInStreaming ||
|
|
!detectedCompatDefaults.supportsStrictMode),
|
|
);
|
|
if (!needsForce) {
|
|
return model;
|
|
}
|
|
const forcedDeveloperRole = compat?.supportsDeveloperRole === true;
|
|
const hasStreamingUsageOverride = compat?.supportsUsageInStreaming !== undefined;
|
|
const targetStrictMode = compat?.supportsStrictMode ?? detectedCompatDefaults?.supportsStrictMode;
|
|
if (
|
|
compat?.supportsDeveloperRole !== undefined &&
|
|
hasStreamingUsageOverride &&
|
|
compat?.supportsStrictMode !== undefined
|
|
) {
|
|
return model;
|
|
}
|
|
|
|
return {
|
|
...model,
|
|
compat: compat
|
|
? {
|
|
...compat,
|
|
supportsDeveloperRole: forcedDeveloperRole || false,
|
|
...(hasStreamingUsageOverride
|
|
? {}
|
|
: {
|
|
supportsUsageInStreaming: detectedCompatDefaults?.supportsUsageInStreaming ?? false,
|
|
}),
|
|
supportsStrictMode: targetStrictMode,
|
|
}
|
|
: {
|
|
supportsDeveloperRole: false,
|
|
supportsUsageInStreaming: detectedCompatDefaults?.supportsUsageInStreaming ?? false,
|
|
supportsStrictMode: detectedCompatDefaults?.supportsStrictMode ?? false,
|
|
},
|
|
} as typeof model;
|
|
}
|