UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

191 lines (190 loc) • 6.78 kB
import { m as asSafeIntegerInRange, p as asPositiveSafeInteger, w as parseStrictFiniteNumber } from "./number-coercion-D6u-4YD8.mjs"; import { l as normalizeSingleOrTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs"; import { h as readLocalFileSafely } from "./fs-safe-BbWvmdVn.mjs"; import { r as textResult } from "./tool-results-CcV0sxnb.mjs"; import { n as detectMime } from "./mime-D2wVBzpa.mjs"; import { t as readSnakeCaseParamRaw } from "./param-key-NQVXaOwt.mjs"; import { n as ToolInputError } from "./tool-input-error-B1hgavSw.mjs"; //#region src/agents/tools/common.ts function createActionGate(e) { return (t, n = true) => { const r = e?.[t]; if (r === void 0) return n; return r !== false; }; } function readToolStringParam(e, t, n = {}) { const { required: r = false, trim: i = true, label: a = t, allowEmpty: o = false } = n; const s = readSnakeCaseParamRaw(e, t); const c = typeof s === "string" ? i ? s.trim() : s : void 0; if (c === void 0 || !c && !o) { if (r) throw new ToolInputError(`${a} required`); return; } return c; } /** "default" resets a model override to its configured fallback. */ function normalizeToolModelOverride(e) { if (typeof e !== "string") return; const t = e.trim(); if (!t || t.toLowerCase() === "default") return; return t; } function readStringOrNumberParam(e, t, n = {}) { const { required: r = false, label: i = t } = n; const a = readSnakeCaseParamRaw(e, t); if (typeof a === "number" && Number.isFinite(a)) return String(a); if (typeof a === "string") { const e = a.trim(); if (e) return e; } if (r) throw new ToolInputError(`${i} required`); } function readNumberParam(e, t, n = {}) { const { required: r = false, label: i = t, integer: a = false, strict: o = false, positiveInteger: s = false, nonNegativeInteger: c = false } = n; const l = readSnakeCaseParamRaw(e, t); let u; if (typeof l === "number" && Number.isFinite(l)) u = l; else if (typeof l === "string") { const e = l.trim(); if (e) { const t = o ? parseStrictFiniteNumber(e) : Number.parseFloat(e); if (t !== void 0 && Number.isFinite(t)) u = t; } } if (u === void 0) { if (r) throw new ToolInputError(`${i} required`); return; } if (s) return asPositiveSafeInteger(u); if (c) return asSafeIntegerInRange(u, { min: 0 }); return a ? Math.trunc(u) : u; } function readStrictNumberParam(e, t, n, r = false) { const i = readNumberParam(e, t, { strict: true, nonNegativeInteger: r }); if (i === void 0) { const r = readSnakeCaseParamRaw(e, t); if (r != null && !(typeof r === "string" && r.trim() === "")) throw new ToolInputError(n); } return i; } function readPositiveIntegerParam(e, t, n = {}) { const r = n.message ?? `${t} must be a positive integer`; const i = readNonNegativeIntegerParam(e, t, { ...n, message: r }); if (i === 0) throw new ToolInputError(r); return i; } function readNonNegativeIntegerParam(e, t, n = {}) { const r = n.message ?? `${t} must be a non-negative integer`; const i = readStrictNumberParam(e, t, r, true); if (i !== void 0 && n.max !== void 0 && i > n.max) throw new ToolInputError(r); return i; } function readFiniteNumberParam(e, t, n = {}) { const r = n.message ?? `${t} must be a finite number`; const i = readStrictNumberParam(e, t, r); if (i === void 0) return; if (n.min !== void 0 && (n.minExclusive ? i <= n.min : i < n.min) || n.max !== void 0 && (n.maxExclusive ? i >= n.max : i > n.max)) throw new ToolInputError(r); return i; } function readStringArrayParam(e, t, n = {}) { const { required: r = false, label: i = t, allowEmpty: a = false } = n; const o = normalizeSingleOrTrimmedStringList(readSnakeCaseParamRaw(e, t)); if (o.length > 0) return o; if (a && Array.isArray(readSnakeCaseParamRaw(e, t))) return []; if (r) throw new ToolInputError(`${i} required`); } function readReactionParams(e, t) { const n = t.emojiKey ?? "emoji"; const r = t.removeKey ?? "remove"; const i = typeof e[r] === "boolean" ? e[r] : false; const a = readToolStringParam(e, n, { required: true, allowEmpty: true }); if (i && !a) throw new ToolInputError(t.removeErrorMessage); return { emoji: a, remove: i, isEmpty: !a }; } function stringifyToolPayload(e) { if (typeof e === "string") return e; try { const t = JSON.stringify(e, null, 2); if (typeof t === "string") return t; } catch {} return String(e); } function failedTextResult(e, t) { return textResult(e, t); } function payloadTextResult(e) { return textResult(stringifyToolPayload(e), e); } function scheduleToolProgress(e, t, n, r = {}) { if (!e || r.signal?.aborted) return () => {}; const clear = () => { clearTimeout(i); r.signal?.removeEventListener("abort", clear); }; const i = setTimeout(() => { clear(); const n = t.text.trim(); if (!n) return; try { e({ content: [], details: void 0, progress: { text: n, visibility: "channel", privacy: "public", ...t.id ? { id: t.id } : {} } }); } catch {} }, n); r.signal?.addEventListener("abort", clear, { once: true }); return clear; } async function imageResultFromFile(e) { const t = (await readLocalFileSafely({ filePath: e.path })).buffer; const n = await detectMime({ buffer: t.slice(0, 256) }) ?? "image/png"; const r = [...e.extraText ? [{ type: "text", text: e.extraText }] : [], { type: "image", data: t.toString("base64"), mimeType: n }]; const i = e.details?.media && typeof e.details.media === "object" && !Array.isArray(e.details.media) ? e.details.media : void 0; const a = { content: r, details: { path: e.path, ...e.details, media: { ...i, mediaUrl: e.path } } }; const { sanitizeToolResultImages: o } = await import("./agents/tool-images.runtime.js"); return await o(a, e.label, e.imageSanitization); } function parseAvailableTags(e) { if (!Array.isArray(e)) return; const t = e.filter((e) => typeof e === "object" && e !== null && typeof e.name === "string").map((e) => Object.assign({}, typeof e.id === "string" ? { id: e.id } : {}, { name: e.name }, typeof e.moderated === `boolean` ? { moderated: e.moderated } : {}, e.emoji_id === null || typeof e.emoji_id === `string` ? { emoji_id: e.emoji_id } : {}, e.emoji_name === null || typeof e.emoji_name === `string` ? { emoji_name: e.emoji_name } : {})); return t.length ? t : void 0; } //#endregion export { parseAvailableTags as a, readNonNegativeIntegerParam as c, readReactionParams as d, readStringArrayParam as f, scheduleToolProgress as h, normalizeToolModelOverride as i, readNumberParam as l, readToolStringParam as m, failedTextResult as n, payloadTextResult as o, readStringOrNumberParam as p, imageResultFromFile as r, readFiniteNumberParam as s, createActionGate as t, readPositiveIntegerParam as u };