openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
128 lines (127 loc) • 4.75 kB
JavaScript
import { t as asNonArrayRecord } from "./record-coerce-CVPuDqcc.mjs";
import { o as writeRuntimeJson, r as defaultRuntime } from "./runtime-B-SLYP-m.mjs";
import { o as getAsyncWorkSignal } from "./async-work-scope-BXagjUap.mjs";
import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs";
import { n as isRich, r as theme } from "./theme-BOI0xyRZ.mjs";
import "./http-body-B9w7rzPz.mjs";
import { i as readResponseWithLimit } from "./http-response-body-B-i_0116.mjs";
import { t as formatDocsLink } from "./links-41lO3DKP.mjs";
import { n as runCommandWithRuntime } from "./cli-utils-BPmaaZlG.mjs";
import { t as formatDocsHelp } from "./help-format-D2ytHW5E.mjs";
import { n as parseStrictPositiveIntOption } from "./helpers-Cxs4nwgv.mjs";
//#region src/cli/docs-cli.ts
const SEARCH_API = "https://docs.openclaw.ai/api/search";
const SEARCH_TIMEOUT_MS = 3e4;
const DOCS_SEARCH_RESPONSE_MAX_BYTES = 8388608;
function escapeMarkdown(e) {
return e.replace(/[()[\]]/g, "\\$&");
}
async function fetchDocsSearch(e, t) {
const n = new URL(SEARCH_API);
n.searchParams.set("q", e);
const r = new AbortController();
const i = setTimeout(() => r.abort(), SEARCH_TIMEOUT_MS);
try {
const e = await fetch(n, {
headers: { Accept: "application/json" },
signal: t ? AbortSignal.any([t, r.signal]) : r.signal
});
if (!e.ok) {
e.body?.cancel().catch(() => void 0);
throw new Error(`HTTP ${e.status}`);
}
const i = await readResponseWithLimit(e, DOCS_SEARCH_RESPONSE_MAX_BYTES, { onOverflow: ({ maxBytes: e }) => /* @__PURE__ */ new Error(`Docs search response exceeds ${e} bytes`) });
let a;
try {
a = asNonArrayRecord(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(i)));
} catch (e) {
throw new Error("Docs search response is malformed JSON", { cause: e });
}
const o = a.results;
if (!Array.isArray(o)) throw new Error("Docs search response is malformed: expected results array");
const s = [];
for (const e of o) {
const t = asNonArrayRecord(e);
if (typeof t.title !== "string" || typeof t.link !== "string") continue;
s.push({
title: t.title,
link: t.link,
snippet: typeof t.snippet === "string" && t.snippet.trim() ? t.snippet : void 0
});
}
return s;
} finally {
clearTimeout(i);
r.abort();
}
}
function registerDocsCli(e) {
e.command("docs").description("Search the live OpenClaw docs").argument("[query...]", "Search query").option("--json", "Output JSON", false).option("--limit <count>", "Maximum results to return", (e) => parseStrictPositiveIntOption(e, "--limit")).addHelpText("after", () => formatDocsHelp("/cli/docs")).action(async (e, t) => {
await runCommandWithRuntime(defaultRuntime, async () => {
const { json: n, limit: r } = t;
const i = e.join(" ").trim();
if (!i) {
if (n) {
writeRuntimeJson(defaultRuntime, {
query: null,
url: "https://docs.openclaw.ai/",
results: []
});
return;
}
const e = formatDocsLink("/", "docs.openclaw.ai");
if (isRich()) {
defaultRuntime.log(`${theme.muted("Docs:")} ${e}`);
defaultRuntime.log(`${theme.muted("Search:")} ${formatCliCommand("openclaw docs \"your query\"")}`);
} else {
defaultRuntime.log("Docs: https://docs.openclaw.ai/");
defaultRuntime.log(`Search: ${formatCliCommand("openclaw docs \"your query\"")}`);
}
return;
}
const a = getAsyncWorkSignal();
if (a?.aborted) return;
let o;
try {
o = await fetchDocsSearch(i, a);
} catch (e) {
if (a?.aborted) return;
const t = e instanceof Error ? e.message : String(e);
throw new Error(`Docs search failed: ${t}`, { cause: e });
}
if (a?.aborted) return;
if (r !== void 0) o = o.slice(0, r);
if (n) {
writeRuntimeJson(defaultRuntime, {
query: i,
results: o
});
return;
}
if (isRich()) {
defaultRuntime.log(`${theme.heading("Docs search:")} ${theme.info(i)}`);
if (o.length === 0) {
defaultRuntime.log(theme.muted("No results."));
return;
}
for (const e of o) {
const t = formatDocsLink(e.link, e.link.replace(/^https?:\/\//i, ""));
defaultRuntime.log(`${theme.muted("-")} ${theme.command(e.title)} ${theme.muted("(")}${t}${theme.muted(")")}`);
if (e.snippet) defaultRuntime.log(` ${theme.muted(e.snippet)}`);
}
return;
}
const s = [`# Docs search: ${escapeMarkdown(i)}`, ""];
if (o.length === 0) s.push("_No results._");
for (const e of o) {
const t = escapeMarkdown(e.title);
const n = e.snippet ? escapeMarkdown(e.snippet) : "";
const r = n ? ` - ${n}` : "";
s.push(`- [${t}](${e.link})${r}`);
}
defaultRuntime.log(s.join("\n").trimEnd());
});
});
}
//#endregion
export { registerDocsCli };