openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
98 lines (97 loc) • 4.48 kB
JavaScript
import { c as normalizeOptionalString } from "./string-coerce-mnp54Vah.js";
import { a as wrapWebContent } from "./external-content-pX-Pk1Iu.js";
import "./string-coerce-runtime-CEGJWkQ_.js";
import { i as buildSearchCacheKey, v as resolveSiteName } from "./web-search-provider-common-DisZTXTb.js";
import "./provider-web-search-9G87vZMY.js";
//#region extensions/parallel/src/parallel-search-normalize.ts
const PARALLEL_MAX_SEARCH_COUNT = 40;
const PARALLEL_MAX_SEARCH_QUERY_CHARS = 200;
const PARALLEL_MAX_OBJECTIVE_CHARS = 5e3;
const PARALLEL_MAX_SEARCH_QUERIES = 5;
const PARALLEL_SESSION_ID_MAX_LENGTH = 1e3;
const PARALLEL_CLIENT_MODEL_MAX_LENGTH = 100;
function resolveParallelSearchCount(value) {
return Math.max(1, Math.min(PARALLEL_MAX_SEARCH_COUNT, Math.floor(value)));
}
function normalizeParallelSessionId(value, maxLength) {
const trimmed = normalizeOptionalString(value);
return trimmed && trimmed.length <= maxLength ? trimmed : void 0;
}
function normalizeParallelObjective(value) {
const trimmed = normalizeOptionalString(value);
if (!trimmed) return;
return trimmed.length <= PARALLEL_MAX_OBJECTIVE_CHARS ? trimmed : trimmed.slice(0, PARALLEL_MAX_OBJECTIVE_CHARS);
}
function normalizeParallelClientModel(value) {
const trimmed = normalizeOptionalString(value);
if (!trimmed) return;
return trimmed.length <= PARALLEL_CLIENT_MODEL_MAX_LENGTH ? trimmed : trimmed.slice(0, PARALLEL_CLIENT_MODEL_MAX_LENGTH);
}
function normalizeParallelSearchQueries(value) {
const candidates = Array.isArray(value) ? value : [];
const seen = /* @__PURE__ */ new Set();
const out = [];
for (const entry of candidates) {
if (typeof entry !== "string") continue;
const trimmed = entry.trim();
if (!trimmed) continue;
const capped = trimmed.length <= PARALLEL_MAX_SEARCH_QUERY_CHARS ? trimmed : trimmed.slice(0, PARALLEL_MAX_SEARCH_QUERY_CHARS);
if (seen.has(capped)) continue;
seen.add(capped);
out.push(capped);
if (out.length === PARALLEL_MAX_SEARCH_QUERIES) break;
}
return out;
}
function invalidSearchQueriesPayload() {
return {
error: "invalid_search_queries",
message: "search_queries must be a non-empty array of keyword strings (max 5, max 200 chars each). See https://docs.parallel.ai/search/best-practices.",
docs: "https://docs.openclaw.ai/tools/parallel-search"
};
}
function normalizeParallelResults(payload) {
if (!payload || typeof payload !== "object") return [];
const results = payload.results;
if (!Array.isArray(results)) return [];
return results.filter((entry) => Boolean(entry && typeof entry === "object" && !Array.isArray(entry)));
}
/** Maps a Parallel v1 response into wrapped `web_search` result entries. */
function mapParallelResults(response) {
return normalizeParallelResults(response).map((entry) => {
const title = typeof entry.title === "string" ? entry.title : "";
const url = typeof entry.url === "string" ? entry.url : "";
const published = typeof entry.publish_date === "string" && entry.publish_date ? entry.publish_date : void 0;
const excerpts = Array.isArray(entry.excerpts) ? entry.excerpts.filter((e) => typeof e === "string").map((e) => wrapWebContent(e, "web_search")) : [];
const description = excerpts.join("\n\n");
return Object.assign({
title: title ? wrapWebContent(title, "web_search") : "",
url,
description,
siteName: resolveSiteName(url) || void 0
}, published ? { published } : {}, excerpts.length > 0 ? { excerpts } : {});
});
}
/**
* Drops a Parallel-generated `sessionId` before caching. Identical queries from
* unrelated tasks would otherwise share that id; caller-supplied session ids are
* part of the cache key, so a cache hit only ever returns the matching id.
*/
function stripParallelGeneratedSessionId(payload) {
if (!("sessionId" in payload)) return payload;
const { sessionId: _omitted, ...rest } = payload;
return rest;
}
function buildParallelCacheKey(params) {
return buildSearchCacheKey([
"parallel",
params.endpoint,
params.objective,
params.searchQueries.join("\0"),
params.count,
params.sessionId,
params.clientModel
]);
}
//#endregion
export { normalizeParallelClientModel as a, normalizeParallelSearchQueries as c, stripParallelGeneratedSessionId as d, mapParallelResults as i, normalizeParallelSessionId as l, buildParallelCacheKey as n, normalizeParallelObjective as o, invalidSearchQueriesPayload as r, normalizeParallelResults as s, PARALLEL_SESSION_ID_MAX_LENGTH as t, resolveParallelSearchCount as u };