Vendor OpenClaw source as Adolf fork baseline
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
This commit is contained in:
325
extensions/llm-task/src/llm-task-tool.ts
Normal file
325
extensions/llm-task/src/llm-task-tool.ts
Normal file
@@ -0,0 +1,325 @@
|
||||
// Llm Task plugin module implements llm task tool behavior.
|
||||
import path from "node:path";
|
||||
import { buildModelAliasIndex, resolveModelRefFromString } from "openclaw/plugin-sdk/agent-runtime";
|
||||
import {
|
||||
optionalFiniteNumberSchema,
|
||||
optionalPositiveIntegerSchema,
|
||||
} from "openclaw/plugin-sdk/channel-actions";
|
||||
import {
|
||||
type JsonSchemaObject,
|
||||
validateJsonSchemaValue,
|
||||
} from "openclaw/plugin-sdk/json-schema-runtime";
|
||||
import { readFiniteNumberParam, readPositiveIntegerParam } from "openclaw/plugin-sdk/param-readers";
|
||||
import {
|
||||
asPositiveSafeInteger,
|
||||
normalizeOptionalString,
|
||||
} from "openclaw/plugin-sdk/string-coerce-runtime";
|
||||
import { Type } from "typebox";
|
||||
import { resolvePreferredOpenClawTmpDir, withTempWorkspace } from "../api.js";
|
||||
import type { OpenClawPluginApi } from "../api.js";
|
||||
|
||||
function stripCodeFences(s: string): string {
|
||||
const trimmed = s.trim();
|
||||
const m = trimmed.match(/^```(?:json)?\s*([\s\S]*?)\s*```$/i);
|
||||
if (m) {
|
||||
return (m[1] ?? "").trim();
|
||||
}
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
function collectText(payloads: Array<{ text?: string; isError?: boolean }> | undefined): string {
|
||||
const texts = (payloads ?? [])
|
||||
.filter((p) => !p.isError && typeof p.text === "string")
|
||||
.map((p) => p.text ?? "");
|
||||
return texts.join("\n").trim();
|
||||
}
|
||||
|
||||
function toModelKey(provider?: string, model?: string): string | undefined {
|
||||
const p = provider?.trim();
|
||||
const m = model?.trim();
|
||||
if (!p || !m) {
|
||||
return undefined;
|
||||
}
|
||||
return `${p}/${m}`;
|
||||
}
|
||||
|
||||
function stripDuplicateProviderPrefix(provider: string | undefined, model: string | undefined) {
|
||||
const p = provider?.trim();
|
||||
const m = model?.trim();
|
||||
if (!p || !m) {
|
||||
return m || undefined;
|
||||
}
|
||||
const prefix = `${p}/`;
|
||||
return m.startsWith(prefix) ? m.slice(prefix.length) : m;
|
||||
}
|
||||
|
||||
function resolveLlmTaskModelRef(params: {
|
||||
api: OpenClawPluginApi;
|
||||
provider?: string;
|
||||
rawModel?: string;
|
||||
}): { provider?: string; model?: string } {
|
||||
const defaultProvider =
|
||||
normalizeOptionalString(params.provider) ??
|
||||
normalizeOptionalString(params.api.runtime.agent.defaults.provider);
|
||||
const rawModel = normalizeOptionalString(params.rawModel);
|
||||
if (!rawModel || !defaultProvider) {
|
||||
return {
|
||||
provider: params.provider,
|
||||
model: stripDuplicateProviderPrefix(params.provider, rawModel),
|
||||
};
|
||||
}
|
||||
|
||||
const cfg = params.api.config;
|
||||
const aliasIndex = cfg
|
||||
? buildModelAliasIndex({
|
||||
cfg,
|
||||
defaultProvider,
|
||||
})
|
||||
: undefined;
|
||||
const resolved = resolveModelRefFromString({
|
||||
cfg,
|
||||
raw: rawModel,
|
||||
defaultProvider,
|
||||
aliasIndex,
|
||||
});
|
||||
if (!resolved) {
|
||||
return {
|
||||
provider: params.provider,
|
||||
model: stripDuplicateProviderPrefix(params.provider, rawModel),
|
||||
};
|
||||
}
|
||||
return resolved.ref;
|
||||
}
|
||||
|
||||
type PluginCfg = {
|
||||
defaultProvider?: string;
|
||||
defaultModel?: string;
|
||||
defaultAuthProfileId?: string;
|
||||
allowedModels?: string[];
|
||||
maxTokens?: number;
|
||||
timeoutMs?: number;
|
||||
};
|
||||
|
||||
type LlmTaskParams = {
|
||||
prompt?: unknown;
|
||||
input?: unknown;
|
||||
schema?: unknown;
|
||||
provider?: unknown;
|
||||
model?: unknown;
|
||||
thinking?: unknown;
|
||||
authProfileId?: unknown;
|
||||
temperature?: unknown;
|
||||
maxTokens?: unknown;
|
||||
timeoutMs?: unknown;
|
||||
};
|
||||
|
||||
type ThinkingPolicy = ReturnType<OpenClawPluginApi["runtime"]["agent"]["resolveThinkingPolicy"]>;
|
||||
|
||||
export const llmTaskToolDefinition = {
|
||||
name: "llm-task",
|
||||
label: "LLM Task",
|
||||
description:
|
||||
"Run a generic JSON-only LLM task and return schema-validated JSON. Designed for orchestration from Lobster workflows via openclaw.invoke.",
|
||||
parameters: Type.Object({
|
||||
prompt: Type.String({ description: "Task instruction for the LLM." }),
|
||||
input: Type.Optional(Type.Unknown({ description: "Optional input payload for the task." })),
|
||||
schema: Type.Optional(
|
||||
Type.Unknown({ description: "Optional JSON Schema to validate the returned JSON." }),
|
||||
),
|
||||
provider: Type.Optional(
|
||||
Type.String({ description: "Provider override (e.g. openai, anthropic)." }),
|
||||
),
|
||||
model: Type.Optional(Type.String({ description: "Model id override." })),
|
||||
thinking: Type.Optional(Type.String({ description: "Thinking level override." })),
|
||||
authProfileId: Type.Optional(Type.String({ description: "Auth profile override." })),
|
||||
temperature: optionalFiniteNumberSchema({ description: "Best-effort temperature override." }),
|
||||
maxTokens: optionalPositiveIntegerSchema({
|
||||
description: "Best-effort maxTokens override.",
|
||||
}),
|
||||
timeoutMs: optionalPositiveIntegerSchema({ description: "Timeout for the LLM run." }),
|
||||
}),
|
||||
};
|
||||
|
||||
function formatThinkingPolicy(policy: ThinkingPolicy): string {
|
||||
return policy.levels.map((level) => level.label).join(", ");
|
||||
}
|
||||
|
||||
function supportsThinkingPolicyLevel(
|
||||
policy: ThinkingPolicy,
|
||||
level: ReturnType<OpenClawPluginApi["runtime"]["agent"]["normalizeThinkingLevel"]>,
|
||||
): boolean {
|
||||
return Boolean(level) && policy.levels.some((entry) => entry.id === level);
|
||||
}
|
||||
|
||||
export function createLlmTaskTool(api: OpenClawPluginApi) {
|
||||
return {
|
||||
...llmTaskToolDefinition,
|
||||
|
||||
async execute(_id: string, params: LlmTaskParams) {
|
||||
const prompt = typeof params.prompt === "string" ? params.prompt : "";
|
||||
if (!prompt.trim()) {
|
||||
throw new Error("prompt required");
|
||||
}
|
||||
|
||||
const pluginCfg = (api.pluginConfig ?? {}) as PluginCfg;
|
||||
|
||||
const defaultsModel = api.config?.agents?.defaults?.model;
|
||||
const primary =
|
||||
typeof defaultsModel === "string"
|
||||
? normalizeOptionalString(defaultsModel)
|
||||
: normalizeOptionalString(defaultsModel?.primary);
|
||||
const primaryProvider = typeof primary === "string" ? primary.split("/")[0] : undefined;
|
||||
const primaryModel =
|
||||
typeof primary === "string" ? primary.split("/").slice(1).join("/") : undefined;
|
||||
|
||||
const requestedProvider =
|
||||
(typeof params.provider === "string" && params.provider.trim()) ||
|
||||
(typeof pluginCfg.defaultProvider === "string" && pluginCfg.defaultProvider.trim()) ||
|
||||
primaryProvider ||
|
||||
undefined;
|
||||
|
||||
const rawModel =
|
||||
(typeof params.model === "string" && params.model.trim()) ||
|
||||
(typeof pluginCfg.defaultModel === "string" && pluginCfg.defaultModel.trim()) ||
|
||||
primaryModel ||
|
||||
undefined;
|
||||
const { provider: resolvedProvider, model } = resolveLlmTaskModelRef({
|
||||
api,
|
||||
provider: requestedProvider,
|
||||
rawModel,
|
||||
});
|
||||
const provider = resolvedProvider;
|
||||
|
||||
const authProfileId =
|
||||
(typeof params.authProfileId === "string" && params.authProfileId.trim()) ||
|
||||
(typeof pluginCfg.defaultAuthProfileId === "string" &&
|
||||
pluginCfg.defaultAuthProfileId.trim()) ||
|
||||
undefined;
|
||||
|
||||
const modelKey = toModelKey(provider, model);
|
||||
if (!provider || !model || !modelKey) {
|
||||
throw new Error(
|
||||
`provider/model could not be resolved (provider=${provider ?? ""}, model=${model ?? ""})`,
|
||||
);
|
||||
}
|
||||
|
||||
const allowed = Array.isArray(pluginCfg.allowedModels) ? pluginCfg.allowedModels : undefined;
|
||||
if (allowed && allowed.length > 0 && !allowed.includes(modelKey)) {
|
||||
throw new Error(
|
||||
`Model not allowed by llm-task plugin config: ${modelKey}. Allowed models: ${allowed.join(", ")}`,
|
||||
);
|
||||
}
|
||||
|
||||
const thinkingRaw =
|
||||
typeof params.thinking === "string" && params.thinking.trim() ? params.thinking : undefined;
|
||||
let thinkLevel: ReturnType<OpenClawPluginApi["runtime"]["agent"]["normalizeThinkingLevel"]> =
|
||||
undefined;
|
||||
if (thinkingRaw) {
|
||||
const thinkingPolicy = api.runtime.agent.resolveThinkingPolicy({ provider, model });
|
||||
const thinkingLevelsHint = formatThinkingPolicy(thinkingPolicy);
|
||||
thinkLevel = api.runtime.agent.normalizeThinkingLevel(thinkingRaw);
|
||||
if (!thinkLevel) {
|
||||
throw new Error(
|
||||
`Invalid thinking level "${thinkingRaw}". Use one of: ${thinkingLevelsHint}.`,
|
||||
);
|
||||
}
|
||||
if (!supportsThinkingPolicyLevel(thinkingPolicy, thinkLevel)) {
|
||||
throw new Error(
|
||||
`Thinking level "${thinkLevel}" is not supported for ${provider}/${model}. Use one of: ${thinkingLevelsHint}.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const timeoutMs =
|
||||
readPositiveIntegerParam(params as Record<string, unknown>, "timeoutMs") ??
|
||||
asPositiveSafeInteger(pluginCfg.timeoutMs) ??
|
||||
30_000;
|
||||
|
||||
const streamParams = {
|
||||
temperature: readFiniteNumberParam(params as Record<string, unknown>, "temperature"),
|
||||
maxTokens:
|
||||
readPositiveIntegerParam(params as Record<string, unknown>, "maxTokens") ??
|
||||
asPositiveSafeInteger(pluginCfg.maxTokens),
|
||||
};
|
||||
|
||||
const input = params.input;
|
||||
let inputJson: string;
|
||||
try {
|
||||
inputJson = JSON.stringify(input ?? null, null, 2);
|
||||
} catch {
|
||||
throw new Error("input must be JSON-serializable");
|
||||
}
|
||||
|
||||
const system = [
|
||||
"You are a JSON-only function.",
|
||||
"Return ONLY a valid JSON value.",
|
||||
"Do not wrap in markdown fences.",
|
||||
"Do not include commentary.",
|
||||
"Do not call tools.",
|
||||
].join(" ");
|
||||
|
||||
const fullPrompt = `${system}\n\nTASK:\n${prompt}\n\nINPUT_JSON:\n${inputJson}\n`;
|
||||
|
||||
return await withTempWorkspace(
|
||||
{ rootDir: resolvePreferredOpenClawTmpDir(), prefix: "openclaw-llm-task-" },
|
||||
async ({ dir: tmpDir }) => {
|
||||
const sessionId = `llm-task-${Date.now()}`;
|
||||
const sessionFile = path.join(tmpDir, "session.json");
|
||||
|
||||
const result = await api.runtime.agent.runEmbeddedAgent({
|
||||
sessionId,
|
||||
sessionFile,
|
||||
workspaceDir: api.config?.agents?.defaults?.workspace ?? process.cwd(),
|
||||
config: api.config,
|
||||
prompt: fullPrompt,
|
||||
timeoutMs,
|
||||
runId: `llm-task-${Date.now()}`,
|
||||
provider,
|
||||
model,
|
||||
authProfileId,
|
||||
authProfileIdSource: authProfileId ? "user" : "auto",
|
||||
thinkLevel,
|
||||
streamParams,
|
||||
disableTools: true,
|
||||
});
|
||||
|
||||
const text = collectText(
|
||||
typeof result === "object" && result !== null && "payloads" in result
|
||||
? (result as { payloads?: Array<{ text?: string; isError?: boolean }> }).payloads
|
||||
: undefined,
|
||||
);
|
||||
if (!text) {
|
||||
throw new Error("LLM returned empty output");
|
||||
}
|
||||
|
||||
const raw = stripCodeFences(text);
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
throw new Error("LLM returned invalid JSON");
|
||||
}
|
||||
|
||||
const schema = params.schema;
|
||||
if (schema && typeof schema === "object" && !Array.isArray(schema)) {
|
||||
const validation = validateJsonSchemaValue({
|
||||
schema: schema as JsonSchemaObject,
|
||||
cacheKey: "llm-task.result",
|
||||
value: parsed,
|
||||
cache: false,
|
||||
});
|
||||
if (!validation.ok) {
|
||||
const msg = validation.errors.map((error) => error.text).join("; ") || "invalid";
|
||||
throw new Error(`LLM JSON did not match schema: ${msg}`);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
content: [{ type: "text", text: JSON.stringify(parsed, null, 2) }],
|
||||
details: { json: parsed, provider, model },
|
||||
};
|
||||
},
|
||||
);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user