openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 10.3 kB
JavaScript
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{c as resolveUserPath}from"./home-dir-DEN5DisQ.mjs";import"./utils-Dlk0IYAa.mjs";import{r as isRootFileMissingFailure}from"./boundary-file-read-bB9An35K.mjs";import{l as resolveAgentIdFromSessionKey}from"./session-key-BPgKfU2m.mjs";import{_ as resolveDefaultAgentId,f as resolveAgentWorkspaceDir,r as resolveAgentConfig,x as resolveSessionAgentId}from"./agent-scope-config-CG9CFpGX.mjs";import{n as rememberBoundedKey}from"./dedupe-CsfmSvUH.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{l as triggerInternalHook,n as createInternalHookEvent}from"./internal-hooks-C8TThetJ.mjs";import{a as hasMultipleSessionSharingIdentities,c as prepareUserProfileCatalog,o as isUserProfileCatalogReady,v as readUserProfileIdentity}from"./user-profile-list-DJsakwXy.mjs";import{i as getAgentWorkspaceAccess}from"./workspace-access-GnBgOJqh.mjs";import{t as prepareSessionTranscriptHydration}from"./session-transcript-hydration-DJ1WzN04.mjs";import{g as resolveWorkspaceBootstrapPath,i as DEFAULT_MEMORY_FILENAME,n as DEFAULT_BOOTSTRAP_FILENAME,s as DEFAULT_USER_FILENAME,t as DEFAULT_AGENTS_FILENAME}from"./workspace-bootstrap-policy-B71K4ysN.mjs";import{t as settleCurrentReadPreparations}from"./current-read-authority-J_xgisvq.mjs";import{_ as loadWorkspaceBootstrapFiles,g as getWorkspaceFileSourceRelativePath,h as createLoadedWorkspaceBootstrapFile,o as isWorkspaceSetupCompleted,r as filterBootstrapFilesForSession,v as readWorkspaceFileWithGuards,y as workspaceFilesShareSourceIdentity}from"./workspace-1G3FI-0P.mjs";import{r as isMemoryOriginEligibleForAutomaticInjection}from"./types-C4nBVSG1.mjs";import{n as classifyActiveMemoryWorkspacePaths}from"./memory-runtime-CqK_5Mw-.mjs";import{r as buildBootstrapContextForFiles}from"./bootstrap-CbSK3Geh.mjs";import path from"node:path";
//!src/agents/workspace-personal-bootstrap.ts
async function loadPersonalUserBootstrapFile(e,t,n){if(!t)return;const r=isUserProfileCatalogReady()?void 0:await prepareUserProfileCatalog();try{if(!hasMultipleSessionSharingIdentities())return;const r=readUserProfileIdentity(t)?.profileId;if(!r||!/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,127}$/.test(r))return;const i=resolveUserPath(e);const a=path.join(i,`users`,r,DEFAULT_USER_FILENAME);const o=await readWorkspaceFileWithGuards({filePath:a,workspaceDir:i,rejectAliases:true});if(!o.ok){if(!isRootFileMissingFailure(o))n?.(`Personal USER.md could not be read safely; using shared defaults.`);return}if(!hasMultipleSessionSharingIdentities()||readUserProfileIdentity(t)?.profileId!==r)return;const s=createLoadedWorkspaceBootstrapFile(DEFAULT_USER_FILENAME,a,o,true);s.origin={kind:`personal`,rootPath:path.dirname(a)};return s}finally{r?.release()}}
//!src/agents/bootstrap-files.ts
const CONTINUATION_SCAN_MAX_RECORDS=500;const FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE=`openclaw:bootstrap-context:full`;const BOOTSTRAP_WARNING_DEDUPE_LIMIT=1024;const seenBootstrapWarnings=/* @__PURE__ */ new Set;function resolveContextInjectionMode(e,t){const n=e&&t?resolveAgentConfig(e,t)?.contextInjection:void 0;if(n===`always`||n===`continuation-skip`||n===`never`)return n;return e?.agents?.defaults?.contextInjection??`always`}async function hasCompletedBootstrapTurn(e){const{agentId:t,sessionId:n,sessionKey:r,storePath:i}=e??{};if(!t||!n||!r||!i)return false;try{const e=prepareSessionTranscriptHydration({agentId:t,sessionId:n,sessionKey:r,storePath:i});const a=await e.readRecentActiveEvents(CONTINUATION_SCAN_MAX_RECORDS);e.assertCurrent();for(const e of a.toReversed()){const t=e;if(t?.type===`compaction`||t?.type===`reset`)return false;if(t?.type===`custom`&&t.customType===`openclaw:bootstrap-context:full`)return true}return false}catch{return false}}function makeBootstrapWarn(e){const t=e.warn;if(!t)return;const n=e.workspaceDir??``;return(r,i)=>{const a=i??`${n}\u0000${e.sessionLabel}\u0000${r}`;if(!rememberBoundedKey(seenBootstrapWarnings,a,BOOTSTRAP_WARNING_DEDUPE_LIMIT))return;t(`${r} (sessionKey=${e.sessionLabel})`)}}function filterCompletedWorkspaceBootstrapFile(e,t,n){if(!t)return e;const r=resolveUserPath(n);const i=path.join(r,DEFAULT_BOOTSTRAP_FILENAME);return e.filter(e=>{if(e.name!==`BOOTSTRAP.md`)return true;const t=normalizeOptionalString(e.path);if(!t)return true;return resolveWorkspaceBootstrapPath(r,t)!==i})}async function isWorkspaceSetupCompletedForContext(e,t=false){try{return await isWorkspaceSetupCompleted(e,t?{readOnly:true}:{})}catch{return false}}async function resolveIneligibleAutomaticMemoryFiles(e){const t=e.files.filter(e=>!e.missing&&(e.name===DEFAULT_MEMORY_FILENAME||e.name===`USER.md`));if(t.length===0||!e.config)return[];let n;try{n=e.agentId??resolveDefaultAgentId(e.config)}catch(n){e.warn?.(`excluding automatic memory context: ${String(n)}`);return t}const r=t.map(t=>path.relative(resolveUserPath(e.workspaceDir),t.path).replaceAll(path.sep,`/`));let i;try{const a=getAgentWorkspaceAccess(e.workspaceDir);i=await classifyActiveMemoryWorkspacePaths({cfg:e.config,agentId:n,workspaceDir:e.workspaceDir,relativePaths:r,...a?{readSources:t.map((e,t)=>({relativePath:r[t],canonicalRelativePath:getWorkspaceFileSourceRelativePath(e)}))}:{}});if(a&&getAgentWorkspaceAccess(e.workspaceDir)!==a)throw new Error(`Workspace access changed during memory classification`)}catch(n){e.warn?.(`excluding automatic memory context: ${String(n)}`);return t}if(i.status===`unavailable`)return[];if(i.status===`unsupported`){e.warn?.(`excluding automatic memory context: selected memory runtime does not support provenance classification`);return t}const a=new Map(i.classifications.map(e=>[e.relativePath,e.originClass]));return t.filter((t,n)=>{const i=r[n];const o=a.get(i);if(isMemoryOriginEligibleForAutomaticInjection(o))return false;const s=`memory-provenance-exclusion\u0000${resolveUserPath(e.workspaceDir)}\u0000${i}\u0000${o??``}`;if(e.warn&&rememberBoundedKey(seenBootstrapWarnings,s,BOOTSTRAP_WARNING_DEDUPE_LIMIT))e.warn(`excluding ${i} from automatic memory context: memory provenance is ${o??`unclassified`}; run \`openclaw doctor\` for the last recorded write`);return true})}async function resolveBootstrapFilesForPreparation(e){return resolveBootstrapFiles({...e,readOnlyState:true},`none`)}async function resolveBootstrapFilesForRun(e){const t=await resolveBootstrapFiles(e,`registered`);const n=e.executionWorkspaceDir&&resolveUserPath(e.executionWorkspaceDir);if(n&&n!==resolveUserPath(e.workspaceDir)){const r=path.join(n,DEFAULT_AGENTS_FILENAME);for(const i of await resolveBootstrapFiles({...e,workspaceDir:n,bootstrapUserProfileId:void 0},`registered`))if(i.name===`AGENTS.md`&&!i.missing&&path.resolve(i.path)===r){i.origin={kind:`execution-project`,rootPath:n};t.push(i)}}return t}async function resolveBootstrapFiles(e,t){const n=getAgentWorkspaceAccess(e.workspaceDir);const r=resolveUserPath(e.bootstrapUserProfileId&&e.config?resolveAgentWorkspaceDir(e.config,resolveSessionAgentId(e)):e.workspaceDir);const i=getAgentWorkspaceAccess(r);const a={sessionKey:e.sessionKey??e.sessionId,chatType:e.chatType,workspaceDir:e.workspaceDir};const[o,s,c]=await settleCurrentReadPreparations([isWorkspaceSetupCompletedForContext(e.workspaceDir,e.readOnlyState),loadWorkspaceBootstrapFiles(e.workspaceDir),loadPersonalUserBootstrapFile(r,e.bootstrapUserProfileId,e.warn)]);for(const t of s)t.origin={kind:`agent-workspace`,rootPath:resolveUserPath(e.workspaceDir)};const l=s.findIndex(e=>e.name===DEFAULT_USER_FILENAME);if(c)s.splice(l<0?s.length:l+1,0,c);const u=await resolveIneligibleAutomaticMemoryFiles({files:c&&r!==resolveUserPath(e.workspaceDir)?s.filter(e=>e!==c):s,workspaceDir:e.workspaceDir,config:e.config,agentId:e.agentId,warn:e.warn});if(c&&r!==resolveUserPath(e.workspaceDir))u.push(...await resolveIneligibleAutomaticMemoryFiles({files:[c],workspaceDir:r,config:e.config,agentId:e.agentId,warn:e.warn}));const d=s.find(e=>e.name===DEFAULT_MEMORY_FILENAME&&!e.missing);const f=d&&filterBootstrapFilesForSession([d],a).length===0?d:void 0;const p=[...f?[f]:[],...u];const m=filterCompletedWorkspaceBootstrapFile(filterBootstrapFilesForSession(s,a).filter(e=>!u.some(t=>workspaceFilesShareSourceIdentity(e,t))),o,e.workspaceDir);const h=e.contextMode===`lightweight`?[]:m;let g=h;if(t===`registered`){const t=e.sessionKey??e.sessionId??`unknown`;const n=e.agentId??(e.sessionKey?resolveAgentIdFromSessionKey(e.sessionKey):void 0);const r={workspaceDir:e.workspaceDir,bootstrapFiles:h,cfg:e.config,sessionKey:e.sessionKey,sessionId:e.sessionId,agentId:n};const i=createInternalHookEvent(`agent`,`bootstrap`,t,r);await triggerInternalHook(i);const a=i.context.bootstrapFiles;g=Array.isArray(a)?a:h}const _=typeof t===`object`?[...g,...t.projected]:g;const v=filterBootstrapFilesForSession(_,a);const y=filterCompletedWorkspaceBootstrapFile(p.length===0?v:v.filter(e=>!p.some(t=>{if(workspaceFilesShareSourceIdentity(e,t))return true;const n=normalizeOptionalString(e.path);const r=normalizeOptionalString(t.path);return Boolean(n&&r&&path.resolve(n)===path.resolve(r))})),o,e.workspaceDir);if(getAgentWorkspaceAccess(e.workspaceDir)!==n||getAgentWorkspaceAccess(r)!==i)throw new Error(`Workspace access changed while preparing bootstrap context`);const{workspaceDir:b,warn:x}=e;const S=resolveUserPath(b);const C=/* @__PURE__ */ new Set;const w=[];for(const e of y){const t=normalizeOptionalString(e.path)??``;if(!t){x?.(`skipping bootstrap file "${e.name}" — missing or invalid "path" field (hook may have used "filePath" instead)`);continue}const n=resolveWorkspaceBootstrapPath(S,t);const r=path.normalize(path.relative(S,n));if(C.has(r))continue;C.add(r);w.push({...e,path:n})}return w}async function resolveBootstrapContextForRun(e){const t=await resolveBootstrapFilesForRun(e);return{bootstrapFiles:t,contextFiles:buildBootstrapContextForFiles(t,e)}}async function resolveBootstrapContextWithProjectedHookFiles(e,t){const n=await resolveBootstrapFiles({...e,readOnlyState:true},{projected:t});return{bootstrapFiles:n,contextFiles:buildBootstrapContextForFiles(n,e)}}export{resolveBootstrapContextWithProjectedHookFiles as a,resolveContextInjectionMode as c,resolveBootstrapContextForRun as i,hasCompletedBootstrapTurn as n,resolveBootstrapFilesForPreparation as o,makeBootstrapWarn as r,resolveBootstrapFilesForRun as s,FULL_BOOTSTRAP_COMPLETED_CUSTOM_TYPE as t};