Files
openclaw/src/plugin-sdk/session-transcript-hit.ts
Peter Steinberger 77d9ac30bb refactor: reuse shared coercion helpers (#86419)
* refactor: share talk event metric extraction

* refactor: reuse shared coercion helpers

* refactor: reuse shared primitive guards

* refactor: reuse shared record guard

* refactor: reuse shared primitive helpers

* refactor: reuse shared string guards

* refactor: reuse shared non-empty string guard

* refactor: share plugin primitive coercion helpers

* refactor: reuse plugin coercion helpers

* refactor: reuse plugin coercion helpers in more plugins

* refactor: reuse channel coercion helpers

* refactor: reuse monitor coercion helpers

* refactor: reuse provider coercion helpers

* refactor: reuse core coercion helpers

* refactor: reuse runtime coercion helpers

* refactor: reuse helper coercion in codex paths

* refactor: reuse helper coercion in runtime paths

* refactor: reuse codex app-server coercion helpers

* refactor: reuse codex record helpers

* refactor: reuse migration and qa record helpers

* refactor: reuse feishu and core helper guards

* refactor: reuse browser and policy coercion helpers

* refactor: reuse memory wiki record helper

* refactor: share boolean coercion helpers

* refactor: reuse finite number coercion

* refactor: reuse trimmed string list helpers

* refactor: reuse string list normalization

* refactor: reuse remaining string list helpers

* refactor: reuse string entry normalizer

* refactor: share sorted string helpers

* refactor: share string list normalization

* test: preserve command registry browser imports

* refactor: reuse trimmed list helpers

* refactor: reuse string dedupe helpers

* refactor: reuse local dedupe helpers

* refactor: reuse more string dedupe helpers

* refactor: reuse command string dedupe helpers

* refactor: dedupe memory path lists with helper

* refactor: expose string dedupe helpers to plugins

* refactor: reuse core string dedupe helpers

* refactor: reuse shared unique value helpers

* refactor: reuse unique helpers in agent utilities

* refactor: reuse unique helpers in config plumbing

* refactor: reuse unique helpers in extensions

* refactor: reuse unique helpers in core utilities

* refactor: reuse unique helpers in qa plugins

* refactor: reuse unique helpers in memory plugins

* refactor: reuse unique helpers in channel plugins

* refactor: reuse unique helpers in core tails

* refactor: reuse unique helper in comfy workflow

* refactor: reuse unique helpers in test utilities

* refactor: expose unique value helper to plugins

* refactor: reuse unique helpers for numeric lists

* refactor: replace index dedupe filters

* refactor: reuse string entry normalization

* refactor: reuse string normalization in plugin helpers

* refactor: reuse string normalization in extension helpers

* refactor: reuse string normalization in channel parsers

* refactor: reuse string normalization in memory search

* refactor: reuse string normalization in provider parsers

* refactor: reuse string normalization in qa helpers

* refactor: reuse string normalization in infra parsers

* refactor: reuse string normalization in messaging parsers

* refactor: reuse string normalization in core parsers

* refactor: reuse string normalization in extension parsers

* refactor: reuse string normalization in remaining parsers

* refactor: reuse string normalization in final parser spots

* refactor: reuse string normalization in qa media helpers

* refactor: reuse normalization in provider and media lists

* refactor: reuse normalization for remaining set filters

* refactor: reuse normalization in policy allowlists

* refactor: reuse normalization in session and owner lists

* refactor: centralize primitive string lists

* refactor: reuse lowercase entry helpers

* refactor: reuse sorted string helpers

* refactor: reuse unique trimmed helpers

* refactor: reuse string normalization helpers

* refactor: reuse catalog string helpers

* refactor: reuse remaining string helpers

* refactor: simplify remaining list normalization

* refactor: reuse codex auth order normalization

* chore: refresh plugin sdk api baseline

* fix: make shared string sorting deterministic

* chore: refresh plugin sdk api baseline

* fix: align host env security ordering
2026-05-25 21:20:41 +01:00

170 lines
6.5 KiB
TypeScript

import path from "node:path";
import { parseUsageCountedSessionIdFromFileName } from "../config/sessions/artifacts.js";
import type { SessionEntry } from "../config/sessions/types.js";
import { normalizeAgentId } from "../routing/session-key.js";
import { normalizeOptionalString } from "../shared/string-coerce.js";
import { uniqueStrings } from "../shared/string-normalization.js";
export { loadCombinedSessionStoreForGateway } from "../config/sessions/combined-store-gateway.js";
const QMD_ARCHIVE_STEM_RE = /^(.+)-jsonl-(reset|deleted)-(.+)$/;
const QMD_ARCHIVE_TIMESTAMP_RE =
/^(\d{4}-\d{2}-\d{2})[tT](\d{2}-\d{2}-\d{2})(?:(?:\.|-)(\d{3}))?[zZ]$/;
function restoreQmdNormalizedArchiveTimestamp(timestamp: string): string | null {
const match = QMD_ARCHIVE_TIMESTAMP_RE.exec(timestamp);
if (!match) {
return null;
}
const [, date, time, milliseconds] = match;
return `${date}T${time}${milliseconds ? `.${milliseconds}` : ""}Z`;
}
function restoreQmdNormalizedArchiveName(mdStem: string): string | null {
const match = QMD_ARCHIVE_STEM_RE.exec(mdStem);
if (!match) {
return null;
}
const [, sessionId, reason, timestamp] = match;
const restoredTimestamp = restoreQmdNormalizedArchiveTimestamp(timestamp);
return restoredTimestamp ? `${sessionId}.jsonl.${reason}.${restoredTimestamp}` : null;
}
function normalizeQmdSessionStem(stem: string): string {
return stem
.normalize("NFKD")
.toLowerCase()
.replace(/[^\p{Letter}\p{Number}]+/gu, "-")
.replace(/-{2,}/g, "-")
.replace(/^-+|-+$/g, "");
}
export type SessionTranscriptHitIdentity = {
stem: string;
liveStem?: string;
ownerAgentId?: string;
archived: boolean;
};
function parseSessionsPath(hitPath: string): { base: string; ownerAgentId?: string } {
const normalized = hitPath.replace(/\\/g, "/");
const fromSessionsRoot = normalized.startsWith("sessions/")
? normalized.slice("sessions/".length)
: normalized;
const parts = fromSessionsRoot.split("/").filter(Boolean);
const base = path.posix.basename(fromSessionsRoot);
const ownerAgentId =
normalized.startsWith("sessions/") && parts.length === 2
? normalizeAgentId(parts[0])
: undefined;
return { base, ownerAgentId };
}
/**
* Derive transcript stem `S` from a memory search hit path for `source === "sessions"`.
* Builtin index uses `sessions/<basename>.jsonl`; QMD exports use `<stem>.md`.
* Archived transcripts (`.jsonl.reset.<iso>` / `.jsonl.deleted.<iso>`) resolve
* to the same stem as the live `.jsonl` they were rotated from.
*/
export function extractTranscriptStemFromSessionsMemoryHit(hitPath: string): string | null {
return extractTranscriptIdentityFromSessionsMemoryHit(hitPath)?.stem ?? null;
}
export function extractTranscriptIdentityFromSessionsMemoryHit(
hitPath: string,
): SessionTranscriptHitIdentity | null {
const isQmdPath = hitPath.replace(/\\/g, "/").startsWith("qmd/");
const { base, ownerAgentId } = parseSessionsPath(hitPath);
const archivedStem = parseUsageCountedSessionIdFromFileName(base);
if (archivedStem && base !== `${archivedStem}.jsonl`) {
return { stem: archivedStem, ownerAgentId, archived: true };
}
if (base.endsWith(".jsonl")) {
const stem = base.slice(0, -".jsonl".length);
return stem ? { stem, ownerAgentId, archived: false } : null;
}
if (base.endsWith(".md")) {
const mdStem = base.slice(0, -".md".length);
if (!mdStem) {
return null;
}
if (isQmdPath) {
const exportedArchiveStem = parseUsageCountedSessionIdFromFileName(mdStem);
if (exportedArchiveStem && mdStem !== `${exportedArchiveStem}.jsonl`) {
return { stem: exportedArchiveStem, liveStem: mdStem, ownerAgentId, archived: true };
}
const restoredArchiveName = restoreQmdNormalizedArchiveName(mdStem);
if (restoredArchiveName) {
const archivedStem = parseUsageCountedSessionIdFromFileName(restoredArchiveName);
if (archivedStem && restoredArchiveName !== `${archivedStem}.jsonl`) {
return { stem: archivedStem, liveStem: mdStem, ownerAgentId, archived: true };
}
}
}
return { stem: mdStem, ownerAgentId, archived: false };
}
return null;
}
/**
* Map transcript stem to canonical session store keys (all agents in the combined store).
* Session tools visibility and agent-to-agent policy are enforced by the caller (e.g.
* `createSessionVisibilityGuard`), including cross-agent cases.
*/
export function resolveTranscriptStemToSessionKeys(params: {
store: Record<string, SessionEntry>;
stem: string;
archivedOwnerAgentId?: string;
allowQmdSlugFallback?: boolean;
}): string[] {
const { store } = params;
const matches: string[] = [];
const stemAsFile = params.stem.endsWith(".jsonl") ? params.stem : `${params.stem}.jsonl`;
const parsedStemId = parseUsageCountedSessionIdFromFileName(stemAsFile);
for (const [sessionKey, entry] of Object.entries(store)) {
const sessionFile = normalizeOptionalString(entry.sessionFile);
if (sessionFile) {
const base = path.basename(sessionFile);
const fileStem = base.endsWith(".jsonl") ? base.slice(0, -".jsonl".length) : base;
if (fileStem === params.stem) {
matches.push(sessionKey);
continue;
}
}
if (entry.sessionId === params.stem || (parsedStemId && entry.sessionId === parsedStemId)) {
matches.push(sessionKey);
}
}
const deduped = uniqueStrings(matches);
if (deduped.length > 0) {
return deduped;
}
const normalizedStem = normalizeQmdSessionStem(params.stem);
if (params.allowQmdSlugFallback === true && normalizedStem) {
for (const [sessionKey, entry] of Object.entries(store)) {
const sessionFile = normalizeOptionalString(entry.sessionFile);
if (sessionFile) {
const base = path.basename(sessionFile);
const fileStem = base.endsWith(".jsonl") ? base.slice(0, -".jsonl".length) : base;
if (normalizeQmdSessionStem(fileStem) === normalizedStem) {
matches.push(sessionKey);
continue;
}
}
const entrySessionId = normalizeOptionalString(entry.sessionId);
if (entrySessionId && normalizeQmdSessionStem(entrySessionId) === normalizedStem) {
matches.push(sessionKey);
}
}
}
const normalizedDeduped = uniqueStrings(matches);
if (normalizedDeduped.length > 0) {
return normalizedDeduped.length === 1 ? normalizedDeduped : [];
}
const archivedOwnerAgentId = normalizeOptionalString(params.archivedOwnerAgentId);
return archivedOwnerAgentId
? [`agent:${normalizeAgentId(archivedOwnerAgentId)}:${params.stem}`]
: [];
}