UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 6.37 kB
import{u as normalizePluginsConfig}from"./config-state-C1eV6UWf.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{n as normalizeAgentId}from"./agent-id-cOfqmfln.mjs";import"./session-key-BPgKfU2m.mjs";import{i as listAgentIds}from"./agent-roster-4XrgUgPK.mjs";import{_ as resolveDefaultAgentId,t as AgentSelectionRequiredError}from"./agent-scope-config-CG9CFpGX.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{n as loadBundledPluginPublicArtifactModuleSync}from"./public-surface-loader-BNZQgUD9.mjs";import{i as ErrorCodes}from"./missing-scope-error-CkgBgh6d.mjs";import{p as errorShape}from"./error-codes-CQNGhXT-.mjs";import{i as withChannelReadAuthority,n as captureChannelReadScope}from"./channel-read-authority-BheGEIiJ.mjs";import{t as respondError}from"./response-BYbuAh7a.mjs";import{i as readMemorySearchManagerStatus,s as resolveMemorySearchStaleness}from"./types-C4nBVSG1.mjs";import{l as resolveActiveMemoryBackendConfig,o as getActiveMemorySearchManagerCore,s as isActiveMemoryProviderNative}from"./memory-runtime-CqK_5Mw-.mjs";import{n as localStateOwnerChangedError,t as captureLocalStateMutationGuard}from"./local-state-owner-DweIAMz2.mjs"; //!src/gateway/server-methods/memory-search.ts const DEFAULT_MAX_RESULTS=20;const MAX_RESULTS=50;function resolveSearchMode(e){const t=e.custom?.searchMode;if(t===`hybrid`||t===`fts-only`)return t;return e.provider===`none`||e.vector?.enabled===false?`fts-only`:`hybrid`}function resolveSearchOptions(e,t=false){const n=e.maxResults;if(n!==void 0&&(typeof n!==`number`||!Number.isFinite(n)))return null;if(t&&n!==void 0&&(!Number.isInteger(n)||n<1))return null;const r=Math.min(MAX_RESULTS,Math.max(1,Math.floor(n??DEFAULT_MAX_RESULTS)));const i=e.minScore;if(i!==void 0&&(typeof i!==`number`||!Number.isFinite(i)))return null;return{...t?n===void 0?{}:{maxResults:n}:{maxResults:r},...i===void 0?{}:{minScore:i}}}function hasUsableAgentIdInput(e){return normalizeAgentId(`${e}a`)!==`a`}async function searchMemory(e,t){const{params:n,respond:r,context:i}=e;if(!t&&n?.version===2){const{memoryProviderHandlers:t}=await import(`./memory-provider-CmRuSVbl.mjs`);await t[`memory.search`](e);return}if(n?.version!==void 0&&n.version!==1){respondError(r,ErrorCodes.INVALID_REQUEST,`unsupported memory version`);return}const a=n&&typeof n===`object`?n:{};const o=typeof a.query===`string`?a.query:``;const s=t?o:o.trim();if(!o.trim()){respondError(r,ErrorCodes.INVALID_REQUEST,`query must be a non-empty string`);return}const c=resolveSearchOptions(a,Boolean(t));if(!c){respondError(r,ErrorCodes.INVALID_REQUEST,t?`maxResults must be a positive integer and minScore a finite number when provided`:`maxResults and minScore must be finite numbers when provided`);return}const l=i.getRuntimeConfig();if(t){if(normalizePluginsConfig(l.plugins).slots.memory!==`memory-core`){respondError(r,ErrorCodes.INVALID_REQUEST,`Memory Core search is unavailable for the selected memory slot. Use the selected plugin's memory tools.`);return}}const u=Object.hasOwn(a,`agentId`);if(u&&typeof a.agentId!==`string`){respondError(r,ErrorCodes.INVALID_REQUEST,`agentId must be a string`);return}if(u&&!hasUsableAgentIdInput(a.agentId)){respondError(r,ErrorCodes.INVALID_REQUEST,`unknown agentId`);return}const d=u?normalizeAgentId(a.agentId):null;if(d!==null&&!listAgentIds(l).includes(d)){respondError(r,ErrorCodes.INVALID_REQUEST,`unknown agentId`);return}let f=d;if(!f)try{f=resolveDefaultAgentId(l,{surface:`memory search`,hint:`Pass agentId to select a configured agent.`})}catch(e){if(!(e instanceof AgentSelectionRequiredError))throw e;respondError(r,ErrorCodes.INVALID_REQUEST,e.message);return}const p=isActiveMemoryProviderNative({cfg:l,agentId:f})?resolveActiveMemoryBackendConfig({cfg:l,agentId:f}):null;if(p?.backend===`provider-runtime`){respondError(r,ErrorCodes.INVALID_REQUEST,`memory plugin "${p.providerId}" uses the provider runtime; retry memory.search with version: 2`);return}let m;(t?captureChannelReadScope():void 0)?.registerResource({key:`memory-search-manager`,settle:async()=>{await m?.manager?.close?.()}});try{m=await getActiveMemorySearchManagerCore({cfg:l,agentId:f,purpose:t?`cli`:`default`,...t?{inspectSources:true}:{}})}catch(e){respondError(r,ErrorCodes.UNAVAILABLE,`memory search unavailable: ${formatErrorMessage(e)}`);return}const{manager:h,error:g}=m;if(!h){if(t){t();r(true,g?.trim()?{agentId:f,status:`failed`,error:g}:{agentId:f,status:`disabled`},void 0);return}respondError(r,ErrorCodes.UNAVAILABLE,g??`memory search unavailable`);return}let readRebuildWarning=()=>void 0;try{if(t){t();if(!m.searchForCli)throw new Error(`The memory runtime does not support CLI search.`);const e=await m.searchForCli({manager:h,cfg:l,agentId:f,query:s,...c,assertCurrent:t});t();r(true,e,void 0);return}const{captureMemoryRebuildNotice:e}=loadBundledPluginPublicArtifactModuleSync({dirName:`memory-core`,artifactBasename:`search-api.js`});readRebuildWarning=e(await readMemorySearchManagerStatus(h,{includeCounts:false}));const n=await h.search(s,c);const i=await readMemorySearchManagerStatus(h,{includeCounts:false});const a=resolveMemorySearchStaleness(i,f);const o=[a?.warning,readRebuildWarning()].filter(e=>typeof e===`string`).join(` `);r(true,{agentId:f,provider:i.provider,searchMode:resolveSearchMode(i),results:n,...a,...o?{warning:o}:{}},void 0)}catch(e){respondError(r,ErrorCodes.UNAVAILABLE,[`memory search failed: ${formatErrorMessage(e)}`,readRebuildWarning()].filter(Boolean).join(` `))}}const memorySearchHandlers={"memory.get":async e=>{const{memoryProviderHandlers:t}=await import(`./memory-provider-CmRuSVbl.mjs`);await t[`memory.get`](e)},"memory.status":async e=>{const{memoryProviderHandlers:t}=await import(`./memory-provider-CmRuSVbl.mjs`);await t[`memory.status`](e)},"memory.search":e=>searchMemory(e),"memory.search.owner":async e=>{const t=e.params?.expectedOwnerId;if(typeof t!==`string`||!t.trim()){respondError(e.respond,ErrorCodes.INVALID_REQUEST,`expectedOwnerId is required`);return}let n=false;try{const r=captureLocalStateMutationGuard(t,e);await withChannelReadAuthority(r,async()=>{n=true;let t;await searchMemory({...e,respond:(...e)=>{t=e}},r);return t},e.signal,t=>{if(t)e.respond(...t)})}catch(t){e.respond(false,void 0,n?errorShape(ErrorCodes.UNAVAILABLE,formatErrorMessage(t)):localStateOwnerChangedError(t))}}};export{memorySearchHandlers};