UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.62 kB
import{r as e}from"./transcript-string-coerce-BxRUqIXb.mjs";import{a as t,s as n,t as r}from"./transcript-state-dir-BTeq06tQ.mjs";import{n as i}from"./transcript-agent-id-DmscQLD6.mjs";import{c as a,s as o}from"./transcript-openclaw-agent-db.paths-5mfY5--s.mjs";import{s}from"./transcript-session-key-normalization-CxaUSpng.mjs";import{i as c}from"./transcript-session-key-DI46891-.mjs";import{r as l}from"./transcript-boundary-path-CPtHZQz7.mjs";import{a as u}from"./transcript-artifacts-BzUw22qv.mjs";import"./transcript-session-sqlite-target-paths-qm0GE1oO.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:s(e.sessionKey)?c(e.sessionKey):e.agentId,env:e.env});return t?t.path:s(e.sessionKey)?a({agentId:c(e.sessionKey),env:e.env}):e.storePath||void 0}function resolveSessionTranscriptsDirForAgent(e,t=process.env,a=()=>n(t,p.homedir)){if(!e?.trim())throw Error(`Session storage path requires an explicit agent id.`);let o=r(t,a),s=i(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??``)}function resolveSessionPathSync(e){let t=e.next();for(;!t.done;){let n=t.value;t=e.next(n.kind===`realpath`?l(n.path)??void 0:f.existsSync(n.path)?n.path:void 0)}return t.value}function*resolvePathFromAgentSessionsDir(e,t){let n=(yield{kind:`realpath`,path:d.resolve(e)})??d.resolve(e),r=d.relative(n,t);return!r||r.startsWith(`..`)||d.isAbsolute(r)?yield*resolveRerootedSessionPath(n,t):d.resolve(n,r)}function*resolveRerootedSessionPath(e,t){let n=resolveAgentSessionsPathParts(t);if(!n)return;let r=n.parts.slice(n.sessionsIndex+1);if(r.length===0)return;let i=d.resolve(e,...r),a=d.relative(e,i);if(!(!a||a.startsWith(`..`)||d.isAbsolute(a)))return yield{kind:`existing`,path:i}}function resolveSiblingAgentSessionsDir(e,t){let n=resolveAgentsDirFromSessionStorePath(d.join(e,`sessions.json`));return n?d.join(n,i(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(t,n){let r=resolveAgentSessionsPathParts(t);if(!r)return;let{parts:a,sessionsIndex:o}=r,s=a[o-1];if(!s)return;let c=i(s);if(c!==e(s)||c!==i(n))return;let l=a.slice(o+1);if(l.length!==1)return;let u=l[0];if(u&&u!==`.`&&u!==`..`)return d.normalize(d.resolve(t))}function*resolvePathWithinSessionsDir(e,t,n){let r=t.trim();if(!r)throw Error(`Session file path must not be empty`);let a=d.resolve(e),o=(yield{kind:`realpath`,path:a})??a,s=d.isAbsolute(r)?(yield{kind:`realpath`,path:r})??r:r,c=d.isAbsolute(s)?d.relative(o,s):s;if(c.startsWith(`..`)&&d.isAbsolute(s)){let tryAgentFallback=function*(e){let t=i(e),n=resolveSiblingAgentSessionsDir(o,t);if(n){let e=yield*resolvePathFromAgentSessionsDir(n,s);if(e)return e}return yield*resolvePathFromAgentSessionsDir(resolveSessionTranscriptsDirForAgent(t),s)},e=n?.agentId?.trim();if(e){let t=yield*tryAgentFallback(e);if(t)return t}let t=extractAgentIdFromAbsoluteSessionPath(s);if(t){let e=yield*tryAgentFallback(t);if(e)return e;let n=resolveStructuralSessionFallbackPath(s,t);if(n)return n}}if(!c||c.startsWith(`..`)||d.isAbsolute(c))throw Error(`Session file path must be within sessions directory`);return d.resolve(o,c)}function*resolveSessionFilePathCoreSteps(e,t,n){let r=resolveSessionsDir(n),i=t&&`sessionFile`in t&&typeof t.sessionFile==`string`?t.sessionFile.trim():void 0;if(i){if(i.startsWith(`sqlite:`))return i;try{return yield*resolvePathWithinSessionsDir(r,i,{agentId:n?.agentId})}catch{}}return yield*resolvePathWithinSessionsDir(r,`${validateSessionId(e)}.jsonl`)}function resolveSessionFilePathCore(e,t,n){return resolveSessionPathSync(resolveSessionFilePathCoreSteps(e,t,n))}var SessionStoreAgentIdRequiredError=class extends Error{constructor(){super(`Session store path requires an explicit agent id.`),this.name=`SessionStoreAgentIdRequiredError`}};function resolveSessionStorePathCore(e,a){let o=a?.env??process.env,homedir=()=>n(o,p.homedir);if(!e){if(!a?.agentId?.trim())throw new SessionStoreAgentIdRequiredError;return`${resolveSessionTranscriptsDirForAgent(a.agentId,o,homedir)}${d.sep}sessions.json`}let s=e;if(s.includes(`{agentId}`)){if(!a?.agentId?.trim())throw new SessionStoreAgentIdRequiredError;let e=i(a.agentId);s=s.replaceAll(`{agentId}`,e)}return s.startsWith(`~`)&&(s=t(s,{home:n(o,homedir),env:o,homedir})),d.isAbsolute(s)?d.resolve(s):d.resolve(r(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,resolveSessionArtifactDirectory as r,resolveAgentsDirFromSessionStorePath as t};