File size: 4,294 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | import type { OpenClawConfig } from "../../config/config.js";
import { loadConfig } from "../../config/config.js";
import {
loadSessionStore,
resolveAllAgentSessionStoreTargets,
resolveStorePath,
updateSessionStore,
} from "../../config/sessions.js";
import {
mergeSessionEntry,
type SessionAcpMeta,
type SessionEntry,
} from "../../config/sessions/types.js";
import { parseAgentSessionKey } from "../../routing/session-key.js";
export type AcpSessionStoreEntry = {
cfg: OpenClawConfig;
storePath: string;
sessionKey: string;
storeSessionKey: string;
entry?: SessionEntry;
acp?: SessionAcpMeta;
storeReadFailed?: boolean;
};
function resolveStoreSessionKey(store: Record<string, SessionEntry>, sessionKey: string): string {
const normalized = sessionKey.trim();
if (!normalized) {
return "";
}
if (store[normalized]) {
return normalized;
}
const lower = normalized.toLowerCase();
if (store[lower]) {
return lower;
}
for (const key of Object.keys(store)) {
if (key.toLowerCase() === lower) {
return key;
}
}
return lower;
}
export function resolveSessionStorePathForAcp(params: {
sessionKey: string;
cfg?: OpenClawConfig;
}): { cfg: OpenClawConfig; storePath: string } {
const cfg = params.cfg ?? loadConfig();
const parsed = parseAgentSessionKey(params.sessionKey);
const storePath = resolveStorePath(cfg.session?.store, {
agentId: parsed?.agentId,
});
return { cfg, storePath };
}
export function readAcpSessionEntry(params: {
sessionKey: string;
cfg?: OpenClawConfig;
}): AcpSessionStoreEntry | null {
const sessionKey = params.sessionKey.trim();
if (!sessionKey) {
return null;
}
const { cfg, storePath } = resolveSessionStorePathForAcp({
sessionKey,
cfg: params.cfg,
});
let store: Record<string, SessionEntry>;
let storeReadFailed = false;
try {
store = loadSessionStore(storePath);
} catch {
storeReadFailed = true;
store = {};
}
const storeSessionKey = resolveStoreSessionKey(store, sessionKey);
const entry = store[storeSessionKey];
return {
cfg,
storePath,
sessionKey,
storeSessionKey,
entry,
acp: entry?.acp,
storeReadFailed,
};
}
export async function listAcpSessionEntries(params: {
cfg?: OpenClawConfig;
env?: NodeJS.ProcessEnv;
}): Promise<AcpSessionStoreEntry[]> {
const cfg = params.cfg ?? loadConfig();
const storeTargets = await resolveAllAgentSessionStoreTargets(
cfg,
params.env ? { env: params.env } : undefined,
);
const entries: AcpSessionStoreEntry[] = [];
for (const target of storeTargets) {
const storePath = target.storePath;
let store: Record<string, SessionEntry>;
try {
store = loadSessionStore(storePath);
} catch {
continue;
}
for (const [sessionKey, entry] of Object.entries(store)) {
if (!entry?.acp) {
continue;
}
entries.push({
cfg,
storePath,
sessionKey,
storeSessionKey: sessionKey,
entry,
acp: entry.acp,
});
}
}
return entries;
}
export async function upsertAcpSessionMeta(params: {
sessionKey: string;
cfg?: OpenClawConfig;
mutate: (
current: SessionAcpMeta | undefined,
entry: SessionEntry | undefined,
) => SessionAcpMeta | null | undefined;
}): Promise<SessionEntry | null> {
const sessionKey = params.sessionKey.trim();
if (!sessionKey) {
return null;
}
const { storePath } = resolveSessionStorePathForAcp({
sessionKey,
cfg: params.cfg,
});
return await updateSessionStore(
storePath,
(store) => {
const storeSessionKey = resolveStoreSessionKey(store, sessionKey);
const currentEntry = store[storeSessionKey];
const nextMeta = params.mutate(currentEntry?.acp, currentEntry);
if (nextMeta === undefined) {
return currentEntry ?? null;
}
if (nextMeta === null && !currentEntry) {
return null;
}
const nextEntry = mergeSessionEntry(currentEntry, {
acp: nextMeta ?? undefined,
});
if (nextMeta === null) {
delete nextEntry.acp;
}
store[storeSessionKey] = nextEntry;
return nextEntry;
},
{
activeSessionKey: sessionKey.toLowerCase(),
},
);
}
|