UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

118 lines (117 loc) • 4.75 kB
import { r as resolveGlobalMap } from "../../global-singleton-D7yf05nR.mjs"; import { n as sha256Hex } from "../../node-crypto-B6yuTjmm.mjs"; import { a as getRuntimeConfigSnapshot } from "../../runtime-snapshot-WbwbY5b7.mjs"; import { n as jsonResult } from "../../tool-results-CcV0sxnb.mjs"; import { f as readStringArrayParam, m as readToolStringParam } from "../../common-njQNtpnW.mjs"; import "../../runtime-config-snapshot-Dy0VFqWf.mjs"; import { a as readCache, l as resolveTimeoutSeconds, s as resolveCacheTtlMs, u as writeCache } from "../../web-shared-DLIuWlIC.mjs"; import { l as normalizeToIsoDate } from "../../web-search-provider-common-BIurMuHZ.mjs"; import "../../provider-web-search-hr4kzF3y.mjs"; import { r as resolveXaiToolApiKeyWithAuth, t as isXaiToolEnabled } from "../../tool-auth-shared-CKBbJvB4.mjs"; import { t as resolveEffectiveXSearchConfig } from "../../x-search-config-Dwl9bsAA.mjs"; import { n as buildMissingXSearchApiKeyPayload, r as createXSearchToolDefinition, t as X_SEARCH_HANDLE_LIMIT } from "../../x-search-tool-shared-DwqLlXbx.mjs"; import { n as resolvePositiveIntegerToolConfig, s as resolveXaiResponsesEndpoint, t as resolveNormalizedXaiToolModel } from "../../tool-config-shared-BeKYC4x9.mjs"; import { n as buildXaiXSearchPayload, r as requestXaiXSearch, t as XAI_DEFAULT_X_SEARCH_MODEL } from "../../x-search-shared-C0N_N2a8.mjs"; //#region extensions/xai/x-search.ts var PluginToolInputError = class extends Error { constructor(e) { super(e); this.name = "ToolInputError"; } }; const X_SEARCH_CACHE = resolveGlobalMap(Symbol.for("openclaw.xai.x-search.cache")); function normalizeOptionalIsoDate(e, t) { if (!e) return; const n = e.trim(); if (!n) return; if (!/^\d{4}-\d{2}-\d{2}$/.test(n)) throw new PluginToolInputError(`${t} must use YYYY-MM-DD`); if (!normalizeToIsoDate(n)) throw new PluginToolInputError(`${t} must be a valid calendar date`); return n; } function createXSearchTool(e) { const t = resolveEffectiveXSearchConfig(e?.config); const n = e?.runtimeConfig ?? getRuntimeConfigSnapshot(); if (!isXaiToolEnabled({ enabled: typeof t?.enabled === "boolean" ? t.enabled : void 0, runtimeConfig: n ?? void 0, sourceConfig: e?.config, auth: e?.auth })) return null; return createXSearchToolDefinition(async (r, i, a) => { a?.throwIfAborted(); const o = await resolveXaiToolApiKeyWithAuth({ sourceConfig: e?.config, runtimeConfig: n ?? void 0, auth: e?.auth }); if (!o) return jsonResult(buildMissingXSearchApiKeyPayload()); const s = readToolStringParam(i, "query", { required: true }); const c = readStringArrayParam(i, "allowed_x_handles"); const l = readStringArrayParam(i, "excluded_x_handles"); if (c && l) throw new PluginToolInputError("allowed_x_handles and excluded_x_handles cannot be used together"); for (const [e, t] of [["allowed_x_handles", c], ["excluded_x_handles", l]]) if (t && t.length > 20) throw new PluginToolInputError(`${e} cannot contain more than ${20} handles`); const u = normalizeOptionalIsoDate(readToolStringParam(i, "from_date"), "from_date"); const d = normalizeOptionalIsoDate(readToolStringParam(i, "to_date"), "to_date"); if (u && d && u > d) throw new PluginToolInputError("from_date must be on or before to_date"); const f = { query: s, allowedXHandles: c, excludedXHandles: l, fromDate: u, toDate: d, enableImageUnderstanding: i.enable_image_understanding === true, enableVideoUnderstanding: i.enable_video_understanding === true }; const p = resolveNormalizedXaiToolModel({ config: t, defaultModel: XAI_DEFAULT_X_SEARCH_MODEL }); const m = resolveXaiResponsesEndpoint(t?.baseUrl); const h = t?.inlineCitations === true; const g = resolvePositiveIntegerToolConfig(t, "maxTurns"); const _ = JSON.stringify([ "x_search", sha256Hex(o), p, m, s, h, g ?? null, f.allowedXHandles ?? null, f.excludedXHandles ?? null, f.fromDate ?? null, f.toDate ?? null, f.enableImageUnderstanding ?? false, f.enableVideoUnderstanding ?? false ]); const v = resolveCacheTtlMs(t?.cacheTtlMinutes, 15); const y = readCache(X_SEARCH_CACHE, _, v); if (y) return jsonResult({ ...y.value, cached: true }); const b = Date.now(); const x = await requestXaiXSearch({ apiKey: o, endpoint: m, model: p, timeoutSeconds: resolveTimeoutSeconds(t?.timeoutSeconds, 30), inlineCitations: h, maxTurns: g, options: f, ...a ? { signal: a } : {} }); a?.throwIfAborted(); const S = buildXaiXSearchPayload({ query: s, model: p, tookMs: Date.now() - b, ...x, options: f }); writeCache(X_SEARCH_CACHE, _, S, v); return jsonResult(S); }); } //#endregion export { createXSearchTool };