openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
129 lines (128 loc) • 4.58 kB
JavaScript
import { l as normalizeOptionalString, m as readNonBlankString } from "./string-coerce-CCIiB4H7.mjs";
import { p as asPositiveSafeInteger } from "./number-coercion-D6u-4YD8.mjs";
import { n as validateJsonSchemaValue } from "./schema-validator-NvoOmaWr.mjs";
import { i as buildModelAliasIndex, y as resolveModelRefFromString } from "./model-selection-shared-9Kz63Hhm.mjs";
import { r as textResult } from "./tool-results-CcV0sxnb.mjs";
import { s as readFiniteNumberParam, u as readPositiveIntegerParam } from "./common-njQNtpnW.mjs";
import "./string-coerce-runtime-BwAZZDf5.mjs";
import "./json-schema-runtime-OTpXIHVF.mjs";
import "./agent-runtime-DA4W1L0G.mjs";
import "./param-readers-CVZN1591.mjs";
import { t as llmTaskToolDefinition } from "./llm-task-tool-definition-OXsmlVj9.mjs";
//#region extensions/llm-task/src/llm-task-tool.ts
function createLlmTaskTool(e) {
return {
...llmTaskToolDefinition,
async execute(t, n, r) {
const i = n;
const a = typeof i.prompt === "string" ? i.prompt : "";
if (!a.trim()) throw new Error("prompt required");
const o = e.pluginConfig ?? {};
const s = e.config.agents?.defaults?.model;
const c = typeof s === "string" ? normalizeOptionalString(s) : normalizeOptionalString(s?.primary);
const l = c?.split("/")[0];
const u = c?.split("/").slice(1).join("/");
const d = normalizeOptionalString(i.provider);
const f = normalizeOptionalString(o.defaultProvider);
const p = normalizeOptionalString(i.model);
const m = normalizeOptionalString(o.defaultModel);
const h = d || f || l || void 0;
const g = p || m || u || void 0;
const _ = Boolean(d || f || p || m);
const v = normalizeOptionalString(h) ?? normalizeOptionalString(e.runtime.agent.defaults.provider);
const y = normalizeOptionalString(g);
const b = h?.trim();
let x = {
provider: h,
model: b && y?.startsWith(`${b}/`) ? y.slice(b.length + 1) : y
};
if (y && v) {
const t = e.config;
const n = buildModelAliasIndex({
cfg: t,
defaultProvider: v
});
const r = resolveModelRefFromString({
cfg: t,
raw: y,
defaultProvider: v,
aliasIndex: n
});
if (!(d || !p && f) || r?.alias) x = r?.ref ?? x;
}
const { provider: S, model: C } = x;
const w = normalizeOptionalString(i.authProfileId) ?? normalizeOptionalString(o.defaultAuthProfileId);
const T = S?.trim();
const E = C?.trim();
if (_ && (!T || !E)) throw new Error(`provider/model could not be resolved (provider=${S ?? ""}, model=${C ?? ""})`);
const D = readNonBlankString(i.thinking);
const O = D ? e.runtime.agent.normalizeThinkingLevel(D) : void 0;
if (D && !O) throw new Error(`Invalid thinking level "${D}".`);
const k = readPositiveIntegerParam(i, "timeoutMs") ?? asPositiveSafeInteger(o.timeoutMs) ?? 3e4;
const A = readFiniteNumberParam(i, "temperature");
const j = readPositiveIntegerParam(i, "maxTokens") ?? asPositiveSafeInteger(o.maxTokens);
const M = i.input;
let N;
try {
N = JSON.stringify(M ?? null, null, 2);
} catch {
throw new Error("input must be JSON-serializable");
}
const P = [
"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 F = i.schema && typeof i.schema === "object" && !Array.isArray(i.schema) ? i.schema : void 0;
const I = await e.runtime.llm.complete({
messages: [{
role: "user",
content: `TASK:\n${a}\n\nINPUT_JSON:\n${N}\n`
}],
systemPrompt: P,
model: _ ? `${T}/${E}` : void 0,
reasoning: O,
maxTokens: j,
temperature: A,
signal: r,
purpose: "llm-task",
...F ? { outputSchema: F } : {},
execution: {
mode: "isolated-agent-runtime",
authProfileId: w,
timeoutMs: k
}
});
const L = I.text.trim();
const R = L.match(/^```(?:json)?\s*([\s\S]*?)\s*```$/i);
const z = R ? (R[1] ?? "").trim() : L;
let B;
try {
B = JSON.parse(z);
} catch {
throw new Error("LLM returned invalid JSON");
}
if (F) {
const e = validateJsonSchemaValue({
schema: F,
cacheKey: "llm-task.result",
value: B,
cache: false
});
if (!e.ok) {
const t = e.errors.map((e) => e.text).join("; ") || "invalid";
throw new Error(`LLM JSON did not match schema: ${t}`);
}
}
return textResult(JSON.stringify(B, null, 2), {
json: B,
provider: I.provider,
model: I.model
});
}
};
}
//#endregion
export { createLlmTaskTool };