openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
101 lines (100 loc) • 3.94 kB
JavaScript
import { c as resolveRuntimeServiceCommit, o as resolveRuntimeServiceBuildId, t as VERSION } from "./version-GlxUMdxI.mjs";
import { v as backupDoctorSqliteRepair } from "./openclaw-state-db-ARhwdPJg.mjs";
import { s as runOpenClawAgentWriteTransaction } from "./openclaw-agent-db-CwXW4hGa.mjs";
import { t as INBOUND_CONTEXT_MARKER } from "./inbound-context-marker-CxvFQl8I.mjs";
import { o as updateSqliteTranscriptEventJsonInTransaction } from "./session-accessor.sqlite-transcript-store-DRtj0wNT.mjs";
import { _ as readTranscriptEventRows } from "./session-accessor.sqlite-read-DA1Bsaij.mjs";
import { n as normalizeLegacyInboundContextLabels, t as mayContainLegacyInboundContextLabels } from "./legacy-inbound-context-labels-CqRjmK8P.mjs";
import { t as note } from "./note-CnLgHvug.mjs";
import { t as countLabel } from "./doctor-state-integrity-format-XVQ-DkEv.mjs";
import { n as transcriptSnapshotsMatch, t as scanDoctorSessionTranscripts } from "./doctor-session-transcript-scan-DFw1p_PR.mjs";
//#region src/commands/doctor-session-transcript-labels.ts
const NOTE_TITLE = "Session transcript labels";
async function noteSessionTranscriptLabelHealth(e) {
const t = e.env ?? process.env;
let n = 0;
let r = 0;
let i = 0;
let a = 0;
const o = /* @__PURE__ */ new Set();
scanDoctorSessionTranscripts({
cfg: e.cfg,
env: t,
title: NOTE_TITLE,
failureLabel: "Failed to inspect or rewrite labels",
cleanScanIdentity: (n) => [
"session-transcript-labels:v1",
VERSION,
resolveRuntimeServiceBuildId(),
resolveRuntimeServiceCommit(),
process.versions.node,
INBOUND_CONTEXT_MARKER,
t.OPENCLAW_PROFILE ?? null,
n.agentId,
n.storePath,
e.shouldRepair,
e.integrityCheck ?? "quick"
]
}, ({ reader: t, target: s, databaseOptions: c, reportError: l, sessionId: u }) => {
const { agentId: d } = s;
const f = t.labelRepairSnapshot(u, normalizeLegacyInboundContextLabels, mayContainLegacyInboundContextLabels);
if (!f.ok) {
l(`- Failed to read transcript for session ${u} (${d})`, f.error);
return false;
}
const p = [];
let m = false;
for (const e of f.rows) {
let t;
try {
t = JSON.parse(e.eventJson);
} catch {
m = true;
continue;
}
if (normalizeLegacyInboundContextLabels(t)) p.push({
seq: e.seq,
eventJson: JSON.stringify(t)
});
}
if (p.length === 0) return true;
n += 1;
r += p.length;
if (e.shouldRepair) try {
if (m) throw new Error(`transcript contains malformed event JSON for ${u}`);
runOpenClawAgentWriteTransaction((e) => {
const t = readTranscriptEventRows(e, u);
if (!transcriptSnapshotsMatch(f.rows, t)) throw new Error(`transcript changed while preparing rewrite for ${u}`);
if (!o.has(s.sqlitePath)) {
const e = backupDoctorSqliteRepair(s.sqlitePath, "transcript-labels");
o.add(s.sqlitePath);
note(`- Saved pre-repair SQLite backup: ${e}`, NOTE_TITLE);
}
updateSqliteTranscriptEventJsonInTransaction(e, u, p);
}, c, {
operationLabel: "doctor.session-transcript-labels",
repairAdmission: {
integrityCheck: e.integrityCheck ?? "quick",
integrityTables: [
"transcript_events",
"transcript_rewrite_watermarks",
"session_windows",
"session_transcript_index_state",
"session_transcript_active_events",
"session_transcript_fts_rows",
"session_transcript_fts"
]
}
});
i += 1;
a += p.length;
} catch (e) {
l(`- Failed to rewrite labels for session ${u} (${d})`, e);
}
return false;
});
if (e.shouldRepair && i > 0) note(`- Rewrote legacy inbound-context labels in ${countLabel(i, "session")} (${countLabel(a, "event")}).`, NOTE_TITLE);
else if (!e.shouldRepair && r > 0) note([`- Found ${countLabel(n, "session")} with legacy inbound-context labels.`, "- Run \"openclaw doctor --fix\" to rewrite them."].join("\n"), NOTE_TITLE);
}
//#endregion
export { noteSessionTranscriptLabelHealth };