UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 6.25 kB
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{o as getAsyncWorkSignal}from"./async-work-scope-DBcxYU4O.mjs";import{c as resolveExecutablePath}from"./executable-path-BYWCW0PG.mjs";import{a as hasCommandProcessCleanupError,t as CommandProcessCleanupError}from"./exec-result-TD4w1Z1i.mjs";import{i as runUtf8CommandWithTimeout}from"./exec-runner-BiCq-T1F.mjs";import{a as resolveCommandProcessSignal}from"./exec-spawn-DNCbDcyJ.mjs";import"./exec-c5tqm8tb.mjs";import{t as resolveBrewExecutable}from"./brew-C9OlIcVE.mjs";import{S as resolveGogServeInvocation}from"./gmail-DTnq9xhd.mjs";import path from"node:path";import fs from"node:fs/promises"; //!src/hooks/gmail-gog.ts const GOG_FORMULAE=/* @__PURE__ */ new Set([`gogcli`,`openclaw/tap/gogcli`,`steipete/tap/gogcli`]);function completeOutput(e){if(e.cleanup===`uncertain`)throw new CommandProcessCleanupError;return e.code===0&&e.signal===null&&e.termination===`exit`&&!e.outputLimitExceeded&&!e.stdoutTruncatedBytes&&!e.stderrTruncatedBytes}async function probeGogTokenEnv(e,t={}){const n=resolveCommandProcessSignal(t.signal??getAsyncWorkSignal());n?.throwIfAborted();try{const r=resolveGogServeInvocation([`--json`,`--no-input`,`schema`,`gmail`,`watch`,`serve`],e);const i=await runUtf8CommandWithTimeout([r.command,...r.args],{timeoutMs:5e3,env:t.env,signal:n,windowsVerbatimArguments:r.windowsVerbatimArguments,maxOutputBytes:{stdout:262144,stderr:16384},maxCombinedOutputBytes:278528,terminateOnOutputLimit:true});n?.throwIfAborted();if(!completeOutput(i))return`indeterminate`;const a=JSON.parse(i.stdout);if(!isRecord(a)||a.schema_version!==1||!isRecord(a.command)||a.command.type!==`command`||a.command.name!==`serve`||!Array.isArray(a.command.flags))return`indeterminate`;const o=/* @__PURE__ */ new Map;for(const e of a.command.flags){if(!isRecord(e)||typeof e.name!==`string`||o.has(e.name))return`indeterminate`;o.set(e.name,e.type)}if(o.get(`token`)!==`string`||o.get(`hook-token`)!==`string`||[`token-env`,`hook-token-env`].some(e=>o.has(e)&&o.get(e)!==`string`))return`indeterminate`;return o.has(`token-env`)&&o.has(`hook-token-env`)?`supported`:`unsupported`}catch(e){n?.throwIfAborted();if(hasCommandProcessCleanupError(e))throw e;return`indeterminate`}}async function inspectGog(e={}){const t=resolveCommandProcessSignal(e.signal??getAsyncWorkSignal());t?.throwIfAborted();const n=resolveExecutablePath(`gog`,{env:e.env,useCache:false});return{executable:n,support:n?await probeGogTokenEnv(n,{...e,signal:t}):`indeterminate`}}async function inspectBrew(e,t,n){const r=await runUtf8CommandWithTimeout([e,...t],{timeoutMs:1e4,env:{...n.env,HOMEBREW_NO_AUTO_UPDATE:`1`},signal:n.signal,maxOutputBytes:{stdout:262144,stderr:16384},maxCombinedOutputBytes:278528,terminateOnOutputLimit:true});n.signal?.throwIfAborted();if(!completeOutput(r))throw new Error(`Homebrew inspection did not complete.`);return r.stdout.trim()}async function inspectGogUpgrade(e,t={}){const n=resolveCommandProcessSignal(t.signal??getAsyncWorkSignal());n?.throwIfAborted();if(!e.executable||process.platform===`win32`)return{eligible:false,reason:`Install or update Gog with its installation manager.`};const r=resolveBrewExecutable();if(!r)return{eligible:false,reason:`Update Gog with its installation manager; Homebrew was not found.`};const i={...t,signal:n};try{const t=(await inspectBrew(r,[`list`,`--formula`,`--full-name`],i)).split(/\r?\n/u).filter(e=>GOG_FORMULAE.has(e));const a=t.length===1?t[0]:void 0;if(!a)return{eligible:false,reason:`Homebrew does not manage a supported Gog formula. Update Gog with its installation manager.`};if((await inspectBrew(r,[`list`,`--formula`,`--pinned`],i)).split(/\r?\n/u).some(e=>e===`gogcli`||e===a))return{eligible:false,reason:`Gog is pinned in Homebrew. Review the pin and update Gog manually.`};const o=await inspectBrew(r,[`--cellar`,a],i);const[s,c]=await Promise.all([fs.realpath(e.executable),fs.realpath(o)]);n?.throwIfAborted();const l=path.relative(c,s).split(path.sep);if(l.length!==3||l[0]===`..`||l[1]!==`bin`||l[2]!==`gog`)return{eligible:false,reason:`The selected Gog executable is not Homebrew's Gog binary. Update the selected installation or adjust PATH.`};return{eligible:true,brew:r,formula:a,executable:e.executable}}catch(e){n?.throwIfAborted();if(hasCommandProcessCleanupError(e))throw e;return{eligible:false,reason:`Could not verify Homebrew ownership and pins. Update Gog manually, then restart the Gmail watcher.`}}}async function upgradeGog(e,t={}){const n=resolveCommandProcessSignal(t.signal??getAsyncWorkSignal());const r={...t,signal:n};let i=`Homebrew did not provide Gog with both token environment flags. Update Gog manually, then restart the Gmail watcher.`;try{for(const a of[[`update`],[`upgrade`,e.formula]]){const o=await inspectGog(r);if(o.support===`supported`)return{inspection:o,message:`Gog supports secure token environment sources. Restart any existing Gmail watcher to use them.`};const s=await inspectGogUpgrade(o,r);n?.throwIfAborted();if(!s.eligible)return{inspection:o,message:s.reason};if(s.brew!==e.brew||s.formula!==e.formula||s.executable!==e.executable)return{inspection:o,message:`The selected Gog installation changed. Review it before retrying the upgrade.`};const c=await runUtf8CommandWithTimeout([s.brew,...a],{timeoutMs:a[0]===`update`?3e5:9e5,env:{...t.env,HOMEBREW_NO_AUTO_UPDATE:`1`},signal:n,outputCapture:`discard`,killProcessTree:true});n?.throwIfAborted();if(c.cleanup===`uncertain`)throw new CommandProcessCleanupError;if(c.code!==0||c.termination!==`exit`||c.signal!==null){i=`Homebrew ${a[0]} did not complete successfully. Check the selected Gog installation and retry, then restart the Gmail watcher.`;break}}}catch(e){n?.throwIfAborted();if(hasCommandProcessCleanupError(e))throw e;i=`The Gog upgrade failed. Check the selected installation and retry, then restart the Gmail watcher.`}const a=await inspectGog(r);if(a.support===`supported`)i=`Gog supports secure token environment sources. Restart any existing Gmail watcher to use them.`;else if(!a.executable)i=`Gog is no longer available on PATH. Repair its installation before restarting the Gmail watcher.`;return{inspection:a,message:i}}export{inspectGogUpgrade as n,upgradeGog as r,inspectGog as t};