UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

4 lines • 3.99 kB
import{t as asNonArrayRecord}from"./record-coerce-BTbqdJ3-.mjs";import{o as writeRuntimeJson,r as defaultRuntime}from"./runtime-DIcLIHcp.mjs";import{o as getAsyncWorkSignal}from"./async-work-scope-DBcxYU4O.mjs";import{t as formatCliCommand}from"./command-format-D7KhNLP0.mjs";import{t as escapeMarkdownText}from"./escape-markdown-DT_pBTQP.mjs";import{n as isRich,r as theme}from"./theme-DS2CtI9X.mjs";import"./http-body-CsPV_uT5.mjs";import{i as readResponseWithLimit}from"./http-response-body-BJ35MG8h.mjs";import{t as formatDocsLink}from"./links-C_FK9uVv.mjs";import{n as runCommandWithRuntime}from"./cli-utils-Dm67QQ71.mjs";import{t as formatDocsHelp}from"./help-format-DLfgiheF.mjs";import{n as parseStrictPositiveIntOption}from"./helpers-C9M4s1yb.mjs"; //!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;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: ${escapeMarkdownText(i)}`,``];if(o.length===0)s.push(`_No results._`);for(const e of o){const t=escapeMarkdownText(e.title);const n=e.snippet?escapeMarkdownText(e.snippet):``;const r=n?` - ${n}`:``;s.push(`- [${t}](${e.link})${r}`)}defaultRuntime.log(s.join(` `).trimEnd())})})}export{registerDocsCli};