UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 14 kB
import{c as e,o as t,s as n}from"./transcript-path-guards-u0N7Z9_x.mjs";import{t as r}from"./transcript-subsystem-C0UTtuvS.mjs";import{t as i}from"./transcript-state-dir-DEo0wD2Q.mjs";import"./transcript-paths-BSkqmV9_.mjs";import{n as a,t as o}from"./transcript-agent-id-DmscQLD6.mjs";import{a as s,n as c}from"./transcript-openclaw-agent-db.paths-DbFl2s5M.mjs";import"./transcript-session-key-MDDhuEG9.mjs";import{c as l,d as u,f as d,g as f,i as p,l as m,p as h,r as g,t as _}from"./transcript-agent-scope-config-l-KZcQvf.mjs";import{i as v,t as y}from"./transcript-session-sqlite-target-paths-C_ECqUX6.mjs";import{a as b,t as x}from"./transcript-paths-CuGliRzI.mjs";import{n as S,t as C}from"./transcript-targets-configured-agents-DkRzTT-S.mjs";import{r as w}from"./transcript-session-store-read-candidates-rRmKiW_Z.mjs";import{a as T,i as E}from"./transcript-session-sqlite-target-BPLWhZV1.mjs";import{t as D}from"./transcript-dedupe-by-key-D0gWUgdn.mjs";import O from"node:path";import k from"node:fs";import"node:fs/promises";function resolveSessionStoreCompatibilityAgentId(e){let t=e.agents?.defaults?.sessionStore?.agentId?.trim();return t?a(t):m(e)??`main`}function mapAgentSessionDirs(e,t,n){return t.filter(e=>e.isDirectory()&&(n?.(e.name)??!0)).map(t=>O.join(e,t.name,`sessions`)).sort((e,t)=>e.localeCompare(t))}function resolveAgentSessionDirsFromAgentsDirSync(e,t){let r;try{r=k.readdirSync(e,{withFileTypes:!0})}catch(e){return n(e)??[]}return mapAgentSessionDirs(e,r,t)}const A=r(`sessions/targets`);function dedupeSessionStoreTargetsBySqliteTarget(e,t){let n,readRegistry=()=>n??=E(t.registeredDatabases,t.env),reportDiagnostic=e=>{t.onDiagnostic?t.onDiagnostic(e):A.warn(e.message)},r=/* @__PURE__ */ new Map,i=/* @__PURE__ */ new Map,o=c(),s=o.createIndex(),l=o.createIndex(),resolveGroupKey=(e,t,n)=>e.size===0?O.resolve(n):(e.size===1&&t(e.keys().next().value),t(n));for(let n of e){let e=T(n.storePath,{agentId:n.agentId,defaultAgentId:t.defaultAgentId,env:t.env,get registeredDatabases(){return readRegistry()},isSameDatabasePath:o,readCandidates:t.readCandidates}),a=resolveGroupKey(r,s,e.path),c=r.get(a)??[];if(c.push({target:n,shared:e.shared===!0,...e.agentId?{databaseOwnerAgentId:e.agentId}:{}}),r.set(a,c),e.shared)continue;let u=resolveGroupKey(i,l,O.resolve(v(n.storePath).path)),d=i.get(u)??[];d.push({target:n,...e.ownerSource?{ownerSource:e.ownerSource}:{},...e.unsuffixedOwnerAgentId?{unsuffixedOwnerAgentId:e.unsuffixedOwnerAgentId}:{}}),i.set(u,d)}for(let[e,t]of i){let n=[...new Set(t.map(({target:e})=>a(e.agentId)))];if(n.length<=1||t.some(e=>!e.ownerSource))continue;let r=t[0]?.unsuffixedOwnerAgentId,i=t[0]?.ownerSource??`configured-default`,o=r?n.filter(e=>e!==r):n;reportDiagnostic({message:r?`Session store target collision at ${e}: owner "${r}" selected by ${i}; suffixed owner(s): ${o.map(e=>`"${e}"`).join(`, `)}.`:i===`registered-suffixed`?`Session store target collision at ${e}: configured default retains its registered suffixed target; all claimant(s) use suffixed targets: ${o.map(e=>`"${e}"`).join(`, `)}.`:i===`occupied-unsuffixed`?`Session store target collision at ${e}: unsuffixed target is occupied without a durable owner; all claimant(s) use suffixed targets: ${o.map(e=>`"${e}"`).join(`, `)}.`:`Session store target collision at ${e}: registry ownership is ambiguous; all claimant(s) use suffixed targets: ${o.map(e=>`"${e}"`).join(`, `)}.`,sqlitePath:e,...r?{ownerAgentId:r}:{},ignoredAgentIds:o,ownerSource:i})}let u=[];for(let[e,n]of r){let r=new Map(n.map(({target:e})=>[a(e.agentId),e])),i=[...new Set(n.flatMap(e=>e.databaseOwnerAgentId??[]))],s=i.length===1?[]:[...new Set(readRegistry().filter(t=>o(t.path,e)).map(e=>a(e.agentId)))],c=r.size>1;if(i.length!==1&&s.length>1){reportDiagnostic({message:`Session store target collision at ${e}: registry ownership is ambiguous across ${s.map(e=>`"${e}"`).join(`, `)}; no owner selected.`,sqlitePath:e,ignoredAgentIds:[...r.keys()],ownerSource:`ambiguous-registry`});continue}let l=i.length===1?`database-path`:s.length===1?`database-registry`:`configured-default`,d=a(i[0]??s[0]??(c?t.defaultAgentId:n[0].target.agentId)),f=r.get(d)??(n.some(e=>e.shared)?r.get(a(t.defaultAgentId))??n[0]?.target:void 0);if(f&&(u.push(f),t.onResolvedTarget?.(f,{agentId:d,storePath:e}),t.onSharedTarget)){let e=new Set(n.filter(e=>e.shared).map(e=>e.target.storePath));e.size>0&&t.onSharedTarget(f,e)}let p=f?a(f.agentId):d,m=[...r.keys()].filter(e=>e!==p);if(!f||m.length>0){let t=f?m:[...r.keys()];reportDiagnostic({message:`Session store target collision at ${e}: owner "${d}" selected by ${l}; ignored owner(s): ${t.map(e=>`"${e}"`).join(`, `)}.`,sqlitePath:e,ownerAgentId:d,ignoredAgentIds:t,ownerSource:l})}}return u}const j=/* @__PURE__ */ new Set([`EACCES`,`ELOOP`,`ENOENT`,`ENOTDIR`,`EPERM`,`ESTALE`]);function dedupeTargetsByStorePath(e){return D(e,e=>e.storePath)}function shouldSkipDiscoveryError(t){let n=e(t)?t.code:void 0;return typeof n==`string`&&j.has(n)}function createRealAgentsRootResolver(){let e=/* @__PURE__ */ new Map;return t=>{if(e.has(t))return e.get(t);try{let n=k.realpathSync.native(t);return e.set(t,n),n}catch(n){if(shouldSkipDiscoveryError(n)){e.set(t,void 0);return}throw n}}}function isWithinRoot(e,t){return e===t||e.startsWith(`${t}${O.sep}`)}function shouldSkipDiscoveredAgentDirName(e,t){return!/[a-z0-9]/i.test(e)||!o(t)||t===`main`&&e.toLowerCase()!==`main`}function resolveValidatedManagedFilePathSync(e){try{let t=k.lstatSync(e.filePath);return t.isSymbolicLink()||!t.isFile()?void 0:isWithinRoot(k.realpathSync.native(e.filePath),e.realAgentsRoot??k.realpathSync.native(e.agentsRoot))?e.filePath:void 0}catch(e){if(shouldSkipDiscoveryError(e))return;throw e}}function resolveValidatedDiscoveredStorePathSync(e){let t=O.join(e.sessionsDir,`sessions.json`);if(!e.sqliteOnly){let n=resolveValidatedManagedFilePathSync({agentsRoot:e.agentsRoot,filePath:t,realAgentsRoot:e.realAgentsRoot});if(n)return n}let n=T(t).path;return resolveValidatedManagedFilePathSync({agentsRoot:e.agentsRoot,filePath:n,realAgentsRoot:e.realAgentsRoot})?t:void 0}function isValidatedRecoveryCandidateSessionsDir(e){let n=O.dirname(e.sessionsDir);try{let r=k.lstatSync(n);if(r.isSymbolicLink()||!r.isDirectory()||!isWithinRoot(k.realpathSync.native(n),e.realAgentsRoot))return!1;try{let t=k.lstatSync(e.sessionsDir);return!t.isSymbolicLink()&&t.isDirectory()&&isWithinRoot(k.realpathSync.native(e.sessionsDir),e.realAgentsRoot)}catch(e){return t(e,`ENOENT`)}}catch(n){if(t(n,`ENOENT`))return e.allowMissingAgentDir===!0;if(shouldSkipDiscoveryError(n))return!1;throw n}}function toDiscoveredSessionStoreTarget(e,t){let n=O.basename(O.dirname(e)),r=a(n);if(!shouldSkipDiscoveredAgentDirName(n,r))return{agentId:r,storePath:t}}function resolveExplicitSessionStoreTarget(e){let t=b(e.store,{agentId:e.defaultAgentId,env:e.env});return(x(t)?toDiscoveredSessionStoreTarget(O.dirname(t),t):void 0)??{agentId:e.defaultAgentId,storePath:t}}function resolveConfiguredSessionStoreTargets(e,t,n){return S(e).map(r=>({agentId:r,storePath:w(e.session?.store,r,t,n)}))}function resolveSessionStoreDiscoveryState(e,t){let n=t.env??process.env,r=t.agentIds,a=r?resolveConfiguredSessionStoreTargets(e,n,t.readPaths):resolveSessionStoreTargets(e,{allAgents:!0},t);if(!h(e.session?.store))for(let r of t.fixedStoreAgentIds??[])C(e,r)||a.push({agentId:r,storePath:w(e.session?.store,r,n,t.readPaths)});let o=/* @__PURE__ */ new Set;for(let e of a){let t=x(e.storePath);t&&o.add(t)}return o.add(O.join(i(n),`agents`)),{configuredTargets:r?a.filter(e=>r.has(e.agentId)):a,agentsRoots:[...o]}}function resolveAllAgentSessionStoreTargetsSync(e,t={}){return resolveAllAgentSessionStoreTargets(e,t,!1)}function resolveAllAgentSessionStoreTargets(e,t,n){let r=t.env??process.env,{configuredTargets:i,agentsRoots:a}=resolveSessionStoreDiscoveryState(e,{env:r,registeredDatabases:t.registeredDatabases,readCandidates:t.readCandidates,readPaths:t.readPaths,agentIds:t.agentIds,fixedStoreAgentIds:t.fixedStoreAgentIds,excludeTarget:t.excludeTarget}),o=createRealAgentsRootResolver(),s=i.flatMap(e=>{if(t.excludeTarget?.(e))return[];let r=x(e.storePath);if(!r||n&&!k.existsSync(r))return[e];let i=o(r);if(!i)return[];if(n)return isValidatedRecoveryCandidateSessionsDir({allowMissingAgentDir:!0,realAgentsRoot:i,sessionsDir:O.dirname(e.storePath)})?[e]:[];let a=resolveValidatedDiscoveredStorePathSync({sessionsDir:O.dirname(e.storePath),agentsRoot:r,realAgentsRoot:i});return a?[{...e,storePath:a}]:[]}),c=a.flatMap(e=>{try{let r=o(e);return r?resolveAgentSessionDirsFromAgentsDirSync(e).flatMap(i=>{let a=toDiscoveredSessionStoreTarget(i,O.join(i,`sessions.json`));if(a&&t.excludeTarget?.(a))return[];let o=n?isValidatedRecoveryCandidateSessionsDir({realAgentsRoot:r,sessionsDir:i})?O.join(i,`sessions.json`):void 0:resolveValidatedDiscoveredStorePathSync({sessionsDir:i,agentsRoot:e,realAgentsRoot:r}),s=o?toDiscoveredSessionStoreTarget(i,o):void 0;return s?[s]:[]}):[]}catch(e){if(shouldSkipDiscoveryError(e))return[];throw e}}),l=[...s,...c],u=t.agentIds;return dedupeSessionStoreTargetsBySqliteTarget(u?l.filter(e=>u.has(e.agentId)):l,{defaultAgentId:resolveSessionStoreCompatibilityAgentId(e),env:r,onResolvedTarget:t.onResolvedTarget,registeredDatabases:t.registeredDatabases,readCandidates:t.readCandidates})}function resolveAgentSessionStoreTargetsSync(e,t,n={}){return resolveAgentSessionStoreTargets(e,t,n)}function resolveAgentSessionStoreTargets(e,t,n){let r=n.env??process.env,i=a(t),o=/* @__PURE__ */ new Set([w(e.session?.store,i,r,n.readPaths),w(e.session?.store,i,r,n.readPaths,`default`)]),s=[],c=createRealAgentsRootResolver();for(let e of o){if(n.excludeTarget?.({agentId:i,storePath:e}))continue;let t=x(e);if(!t){if(n.sqliteOnly){let t=T(e,{agentId:i,env:r,registeredDatabases:n.registeredDatabases,readCandidates:n.readCandidates}).path;if(!k.existsSync(t))continue}s.push({agentId:i,storePath:e});continue}let a=c(t);if(!a)continue;let o=resolveValidatedDiscoveredStorePathSync({sessionsDir:O.dirname(e),agentsRoot:t,realAgentsRoot:a,sqliteOnly:n.sqliteOnly});o&&s.push({agentId:i,storePath:o})}if(C(e,i))return dedupeTargetsByStorePath(s);let{agentsRoots:l}=resolveSessionStoreDiscoveryState(e,{...n,env:r});for(let e of l)try{let t=c(e);if(!t)continue;for(let r of resolveAgentSessionDirsFromAgentsDirSync(e,e=>a(e)===i)){let i=toDiscoveredSessionStoreTarget(r,O.join(r,`sessions.json`));if(!i||n.excludeTarget?.(i))continue;let a=resolveValidatedDiscoveredStorePathSync({sessionsDir:r,agentsRoot:e,realAgentsRoot:t,sqliteOnly:n.sqliteOnly});a&&s.push({...i,storePath:a})}}catch(e){if(shouldSkipDiscoveryError(e))continue;throw e}return dedupeTargetsByStorePath(s)}function resolveConfiguredAgentDatabaseCandidatePaths(e,t){let n=S(e);return[.../* @__PURE__ */ new Set([...n.flatMap(n=>y(b(e.session?.store,{agentId:n,env:t.env}),n)),...[.../* @__PURE__ */ new Set([...n,...f(e)])].map(n=>O.join(_(e,n,t.env),`openclaw-agent.sqlite`))])]}function resolveConfiguredAgentDatabaseTargets(e,t){let{env:n}=t,r=t.agentId===void 0?void 0:a(t.agentId),i=/* @__PURE__ */ new Map,add=(e,t)=>{(r===void 0||e===r)&&i.set(`${e}\0${t}`,{agentId:e,path:t})};dedupeSessionStoreTargetsBySqliteTarget(resolveConfiguredSessionStoreTargets(e,n).filter(e=>!t.excludeTarget?.(e)),{...t,defaultAgentId:resolveSessionStoreCompatibilityAgentId(e),onResolvedTarget:(e,t)=>add(t.agentId,t.storePath)});for(let i of f(e))(r===void 0||i===r)&&(!t.nativeAgentIds||t.nativeAgentIds.has(i))&&add(i,O.join(_(e,i,n),`openclaw-agent.sqlite`));return[...i.values()]}function isConfiguredAgentDatabaseTarget(e,t,n){return e.some(e=>(t===void 0||e.agentId===void 0||e.agentId===t)&&(e.path===n||k.statSync(e.path,{throwIfNoEntry:!1})&&k.statSync(n,{throwIfNoEntry:!1})&&s(e.path,n)))}function resolveSessionStoreTargets(e,t,n={}){let r=n.env??process.env,i=t.agent?.trim();if(t.agent!==void 0&&!i)throw Error(`--agent must not be blank`);if(t.store!==void 0&&!t.store.trim())throw Error(`--store must not be blank`);let o=i!==void 0,s=t.allAgents===!0;if(o&&s)throw Error(`--agent and --all-agents cannot be used together`);if(t.store&&s)throw Error(`--store cannot be combined with --all-agents`);if(t.store){let n=d({config:e,sessionKey:`main`,storePath:t.store,env:r});if(n.kind===`retired`)throw Error(`Session store owner is retired: ${n.agentId}`);let s=i?a(i):void 0;if(s&&n.kind===`configured`&&n.agentId!==s)throw Error(`Session store belongs to agent "${n.agentId}", not requested agent "${s}".`);let c=s??(n.kind===`configured`?n.agentId:void 0)??l(e)??p(e);o&&g(e,c);let u=resolveExplicitSessionStoreTarget({defaultAgentId:c,env:r,store:t.store});if((o||n.kind===`configured`)&&u.agentId!==c)throw Error(`Session store belongs to agent "${u.agentId}", not requested agent "${c}".`);return[u]}if(s){let t=resolveSessionStoreCompatibilityAgentId(e);return dedupeSessionStoreTargetsBySqliteTarget(resolveConfiguredSessionStoreTargets(e,r,n.readPaths).filter(e=>!n.excludeTarget?.(e)),{defaultAgentId:t,env:r,registeredDatabases:n.registeredDatabases,readCandidates:n.readCandidates,...n.diagnostics?{onDiagnostic:e=>n.diagnostics?.push(e.message)}:{}})}if(o){let t=a(i);return g(e,t),[{agentId:t,storePath:b(e.session?.store,{agentId:t,env:r})}]}let c=u(e);if(c.kind===`retired`)throw Error(`Session store owner is retired: ${c.agentId}`);let f=(c.kind===`configured`?c.agentId:void 0)??l(e)??p(e);return[{agentId:f,storePath:b(e.session?.store,{agentId:f,env:r})}]}export{resolveConfiguredAgentDatabaseCandidatePaths as a,resolveSessionStoreTargets as c,resolveAgentSessionDirsFromAgentsDirSync as d,resolveSessionStoreCompatibilityAgentId as f,resolveAllAgentSessionStoreTargetsSync as i,shouldSkipDiscoveryError as l,resolveAgentSessionStoreTargets as n,resolveConfiguredAgentDatabaseTargets as o,resolveAgentSessionStoreTargetsSync as r,resolveConfiguredSessionStoreTargets as s,isConfiguredAgentDatabaseTarget as t,dedupeSessionStoreTargetsBySqliteTarget as u};