openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 5.46 kB
JavaScript
import{m as e}from"./transcript-plugin-instance-invocation-7y9lfaeO.mjs";import{n as t}from"./transcript-string-coerce-DTk-8HSr.mjs";import{a as n,s as r,t as i}from"./transcript-state-dir-DEo0wD2Q.mjs";import{n as a}from"./transcript-agent-id-DmscQLD6.mjs";import{o,s}from"./transcript-openclaw-agent-db.paths-DbFl2s5M.mjs";import{o as c}from"./transcript-session-key-normalization-3Mbu27cw.mjs";import{r as l}from"./transcript-session-key-MDDhuEG9.mjs";import{a as u}from"./transcript-artifacts-BcxHhf8q.mjs";import"./transcript-session-sqlite-target-paths-C_ECqUX6.mjs";import d from"node:path";import f from"node:fs";import p from"node:os";function resolveExplicitSessionStorePathForScope(e){let t=o(e.storePath,{agentId:c(e.sessionKey)?l(e.sessionKey):e.agentId,env:e.env});return t?t.path:c(e.sessionKey)?s({agentId:l(e.sessionKey),env:e.env}):e.storePath||void 0}function resolveSessionTranscriptsDirForAgent(e,t=process.env,n=()=>r(t,p.homedir)){if(!e?.trim())throw Error(`Session storage path requires an explicit agent id.`);let o=i(t,n),s=a(e);return d.join(o,`agents`,s,`sessions`)}function resolveSessionArtifactDirectory(e){let t=d.dirname(e);return d.basename(t)===`agent`?d.join(d.dirname(t),`sessions`):t}const m=/^[\p{L}\p{N}][\p{L}\p{N}\p{M}._-]{0,127}$/u;function validateSessionId(e){let t=e.trim();if(t!==t.normalize(`NFC`)||!m.test(t)||Buffer.byteLength(`${t}.jsonl`,`utf8`)>255||u(`${t}.jsonl`))throw Error(`Invalid session ID: ${e}`);return t}function resolveSessionsDir(e){let t=e?.sessionsDir?.trim();return t?d.resolve(t):resolveSessionTranscriptsDirForAgent(e?.agentId??``)}const h={realpath:t=>e(t)??void 0,exists:e=>f.existsSync(e)};function resolvePathFromAgentSessionsDir(e,t,n){let r=n.realpath(d.resolve(e))??d.resolve(e),i=d.relative(r,t);return!i||i.startsWith(`..`)||d.isAbsolute(i)?resolveRerootedSessionPath(r,t,n):d.resolve(r,i)}function resolveRerootedSessionPath(e,t,n){let r=resolveAgentSessionsPathParts(t);if(!r)return;let i=r.parts.slice(r.sessionsIndex+1);if(i.length===0)return;let a=d.resolve(e,...i),o=d.relative(e,a);if(!(!o||o.startsWith(`..`)||d.isAbsolute(o)))return n.exists(a)?a:void 0}function resolveSiblingAgentSessionsDir(e,t){let n=resolveAgentsDirFromSessionStorePath(d.join(e,`sessions.json`));return n?d.join(n,a(t),`sessions`):void 0}function resolveAgentSessionsPathParts(e){let t=d.normalize(d.resolve(e)).split(d.sep).filter(Boolean),n=t.lastIndexOf(`sessions`);return n<2||t[n-2]!==`agents`?null:{parts:t,sessionsIndex:n}}function extractAgentIdFromAbsoluteSessionPath(e){let t=resolveAgentSessionsPathParts(e);if(!t)return;let{parts:n,sessionsIndex:r}=t;return n[r-1]||void 0}function resolveStructuralSessionFallbackPath(e,n){let r=resolveAgentSessionsPathParts(e);if(!r)return;let{parts:i,sessionsIndex:o}=r,s=i[o-1];if(!s)return;let c=a(s);if(c!==t(s)||c!==a(n))return;let l=i.slice(o+1);if(l.length!==1)return;let u=l[0];if(u&&u!==`.`&&u!==`..`)return d.normalize(d.resolve(e))}function resolvePathWithinSessionsDir(e,t,n,r=h){let i=t.trim();if(!i)throw Error(`Session file path must not be empty`);let o=d.resolve(e),s=r.realpath(o)??o,c=d.isAbsolute(i)?r.realpath(i)??i:i,l=d.isAbsolute(c)?d.relative(s,c):c;if(l.startsWith(`..`)&&d.isAbsolute(c)){let tryAgentFallback=e=>{let t=a(e),n=resolveSiblingAgentSessionsDir(s,t);if(n){let e=resolvePathFromAgentSessionsDir(n,c,r);if(e)return e}return resolvePathFromAgentSessionsDir(resolveSessionTranscriptsDirForAgent(t),c,r)},e=n?.agentId?.trim();if(e){let t=tryAgentFallback(e);if(t)return t}let t=extractAgentIdFromAbsoluteSessionPath(c);if(t){let e=tryAgentFallback(t);if(e)return e;let n=resolveStructuralSessionFallbackPath(c,t);if(n)return n}}if(!l||l.startsWith(`..`)||d.isAbsolute(l))throw Error(`Session file path must be within sessions directory`);return d.resolve(s,l)}function resolveSessionFilePathCore(e,t,n){return resolveSessionFilePathWithFilesystem(e,t,n,h)}function resolveSessionFilePathWithFilesystem(e,t,n,r){let i=resolveSessionsDir(n),a=t&&`sessionFile`in t&&typeof t.sessionFile==`string`?t.sessionFile.trim():void 0;if(a){if(a.startsWith(`sqlite:`))return a;try{return resolvePathWithinSessionsDir(i,a,{agentId:n?.agentId},r)}catch{}}return resolvePathWithinSessionsDir(i,`${validateSessionId(e)}.jsonl`,void 0,r)}var SessionStoreAgentIdRequiredError=class extends Error{constructor(){super(`Session store path requires an explicit agent id.`),this.name=`SessionStoreAgentIdRequiredError`}};function resolveSessionStorePathCore(e,t){let o=t?.env??process.env,homedir=()=>r(o,p.homedir);if(!e){if(!t?.agentId?.trim())throw new SessionStoreAgentIdRequiredError;let e=a(t.agentId);return d.join(resolveSessionTranscriptsDirForAgent(e,o,homedir),`sessions.json`)}let s=e;if(s.includes(`{agentId}`)){if(!t?.agentId?.trim())throw new SessionStoreAgentIdRequiredError;let e=a(t.agentId);s=s.replaceAll(`{agentId}`,e)}return s.startsWith(`~`)&&(s=n(s,{home:r(o,homedir),env:o,homedir})),d.isAbsolute(s)?d.resolve(s):d.resolve(i(o,homedir),s)}function resolveAgentsDirFromSessionStorePath(e){let t=d.resolve(e);if(d.basename(t)!==`sessions.json`)return;let n=d.dirname(t);if(d.basename(n)!==`sessions`)return;let r=d.dirname(n),i=d.dirname(r);if(d.basename(i)===`agents`)return i}export{resolveSessionStorePathCore as a,resolveSessionFilePathCore as i,resolveExplicitSessionStorePathForScope as n,resolveSessionTranscriptsDirForAgent as o,resolveSessionArtifactDirectory as r,resolveAgentsDirFromSessionStorePath as t};