UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.26 kB
import{i as isSessionAccessRefreshingError}from"../../session-mutation-authorization-error-D4Cgq_Qr.mjs";import"../../gateway-runtime-CCd3DVdi.mjs";import{i as parseGoogleResearchInput,n as executeGoogleResearch,t as GoogleResearchError}from"../../research-Cb1AshCV.mjs";import{createHash}from"node:crypto"; //!extensions/google/research-gateway.ts const retentionMs=6048e5;const bindingKey=e=>createHash(`sha256`).update(JSON.stringify([e.profileId,e.interactionId])).digest(`hex`);async function handleGoogleResearch(e,t){const{params:n,respond:r,sessionAccessAuthority:i}=t;let a=false;let o=false;let s;let c;try{const l=t.assertHandlerCurrent;if(!i||!l)throw new Error(`Research requires current session and provider authority.`);const assertProviderCurrent=()=>{try{l()}catch(e){s=`retryable`;throw e}};const assertCurrent=()=>{assertProviderCurrent();try{i.assertCurrent()}catch(e){s=isSessionAccessRefreshingError(e)?`retryable`:`blocked`;throw e}};const u={...i.target,lifecycleRevision:i.target.lifecycleRevision??null};const d=e.pluginConfig?.research;if(!d||typeof d!==`object`||Array.isArray(d)||!(`enabled`in d)||d.enabled!==true||!(`profileId`in d)||typeof d.profileId!==`string`||!d.profileId.trim())throw new GoogleResearchError(`Google managed research is not explicitly enabled. Enable plugins.entries.google.config.research with an API-key profileId.`,`blocked`);const f=d.profileId;const p=n.request;if(!p||typeof p!==`object`||!(`version`in p)||p.version!==1)throw new GoogleResearchError(`Use Google managed-research request version 1.`,`blocked`);const{version:m,...h}=p;if(`profileId`in h)throw new GoogleResearchError(`Research profile selection is provider-owned.`,`blocked`);const g=parseGoogleResearchInput(`action`in h&&h.action===`start`?{...h,profileId:f}:h);if(g.action===`poll`||g.action===`cancel`){o=true;c=g.handle}assertCurrent();if((g.action===`poll`||g.action===`cancel`)&&g.handle.profileId!==f)throw new GoogleResearchError(`Research credential owner changed; the recorded job cannot switch profiles. Restore its original configured profile before retrying.`,`blocked`,g.handle);const _=e.runtime.state.openKeyedStoreV2({namespace:`research-interactions-v1`,maxEntries:4096,overflowPolicy:`reject-new`},{assertCurrent:assertProviderCurrent});let v;if(g.action===`poll`||g.action===`cancel`){v=await _.lookup(bindingKey(g.handle));assertCurrent();if(!v||v.agentId!==u.agentId||v.sessionKey!==u.sessionKey||v.sessionId!==u.sessionId||v.lifecycleRevision!==u.lifecycleRevision||v.profileId!==f)throw new Error(`Research interaction is not owned by this conversation generation.`)}const y=await executeGoogleResearch(g,{api:e,agentId:i.target.agentId,signal:t.signal??AbortSignal.timeout(6e4),assertCurrent,prepareAccount:async e=>{assertCurrent();if(v){if(v.accountIdentity!==e)throw new Error(`Research credential account changed.`)}else{if(await _.count()>=4096)throw new Error(`Research interaction capacity reached.`);assertCurrent();v={...u,profileId:f,accountIdentity:e}}},recordReceipt:async e=>{c=e;a=g.action===`start`;assertProviderCurrent();if(g.action===`start`){if(!v||!await _.registerIfAbsent(bindingKey(e),v,{ttlMs:retentionMs}))throw new Error(`Research interaction binding was not recorded.`);assertProviderCurrent()}}});a=g.action===`start`;if(`handle`in y)c=y.handle;assertCurrent();r(true,{version:1,ok:true,value:y})}catch(e){if(!s)try{t.assertHandlerCurrent?.()}catch{s=`retryable`}r(true,o&&s?{version:1,ok:false,state:s===`retryable`?`indeterminate`:`blocked`,...s===`retryable`?{retryAfterMs:5e3}:{},...c?{handle:c}:{},reason:`Google research reconciliation requires fresh provider or session authority.`}:e instanceof GoogleResearchError?{version:1,ok:false,state:e.state,reason:e.message,...e.handle?{handle:e.handle}:{},...e.state===`blocked`&&s===`retryable`?{retryAfterMs:5e3}:e.retryAfterMs===void 0?{}:{retryAfterMs:e.retryAfterMs}}:{version:1,ok:false,state:a?`indeterminate`:`blocked`,...!a&&s===`retryable`?{retryAfterMs:5e3}:{},...c?{handle:c}:{},reason:a?`Google research returned a submission, but authority changed before delivery. Do not repeat the start request.`:`Google research is unavailable under the current configuration or session authority.`})}}export{handleGoogleResearch};