openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
152 lines (149 loc) • 5.28 kB
JavaScript
import { S as parseDateStringTimestampMs } from "./number-coercion-D6u-4YD8.mjs";
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { f as hasLegacyRuntimeContextEnvelope, h as labelRuntimeContextContent, s as RUNTIME_CONTEXT_CUSTOM_TYPE } from "./types-iq-cNLsd.mjs";
import { n as getOpenClawSystemUpdateKind } from "./operator-messages-B9HgXnnQ.mjs";
//#region packages/agent-core/src/harness/messages.ts
function requireSessionTimestampMs(e, t) {
const n = parseDateStringTimestampMs(e);
if (n === void 0) throw new Error(`${t} must be a valid timestamp`);
return n;
}
function normalizeCompactionSummaryTimestamp(e) {
if (typeof e === "number") return e;
return parseDateStringTimestampMs(e) ?? 0;
}
const COMPACTION_SUMMARY_PREFIX = `The conversation history before this point was compacted into the following summary:
<summary>
`;
const COMPACTION_SUMMARY_SUFFIX = `
</summary>`;
const BRANCH_SUMMARY_PREFIX = `The following is a summary of a branch that this conversation came back from:
<summary>
`;
const BRANCH_SUMMARY_SUFFIX = `</summary>`;
/** Render a shell execution record as user-visible context text for the model. */
function bashExecutionToText(e) {
let t = `Ran \`${e.command}\`\n`;
if (e.output) t += `\`\`\`\n${e.output}\n\`\`\``;
else t += "(no output)";
if (e.cancelled) t += "\n\n(command cancelled)";
else if (e.exitCode !== null && e.exitCode !== void 0 && e.exitCode !== 0) t += `\n\nCommand exited with code ${e.exitCode}`;
if (e.truncated && e.fullOutputPath) t += `\n\n[Output truncated. Full output: ${e.fullOutputPath}]`;
return t;
}
/** Build a persisted branch summary message from the repository timestamp string. */
function createBranchSummaryMessage(e, t, n) {
return {
role: "branchSummary",
summary: e,
fromId: t,
timestamp: requireSessionTimestampMs(n, "branch summary timestamp")
};
}
/** Build a persisted compaction summary message from the repository timestamp string. */
function createCompactionSummaryMessage(e, t, n) {
return {
role: "compactionSummary",
summary: e,
tokensBefore: t,
timestamp: requireSessionTimestampMs(n, "compaction summary timestamp")
};
}
/** Build a custom transcript message that can be shown and replayed into context. */
function createCustomMessage(e, t, n, r, i) {
return {
role: "custom",
customType: e,
content: t,
display: n,
details: r,
timestamp: requireSessionTimestampMs(i, "custom message timestamp")
};
}
/** Recognize the structured carrier marker shared with provider replay. */
function isRuntimeContextCarrier(e) {
const t = asOptionalRecord(e);
const n = t?.role === "custom" ? asOptionalRecord(t.details) : void 0;
return t?.role === "custom" && (t.customType === "openclaw.runtime-context" && (n?.source === "openclaw-runtime-context" && n.runtimeContextCarrier !== false || n?.source === void 0 && n?.runtimeContextCarrier === true) || getOpenClawSystemUpdateKind(e) === "runtime-context");
}
/** Convert harness transcript messages into the LLM-facing message sequence. */
function convertToLlm(e) {
const t = [];
e.forEach((e) => {
let n;
switch (e.role) {
case "bashExecution":
if (e.excludeFromContext) return;
n = [{
type: "text",
text: bashExecutionToText(e)
}];
break;
case "custom":
if (e.excludeFromContext) return;
n = typeof e.content === "string" ? [{
type: "text",
text: e.content
}] : e.content;
break;
case "branchSummary":
n = [{
type: "text",
text: BRANCH_SUMMARY_PREFIX + e.summary + BRANCH_SUMMARY_SUFFIX
}];
break;
case "compactionSummary":
n = [{
type: "text",
text: COMPACTION_SUMMARY_PREFIX + e.summary + COMPACTION_SUMMARY_SUFFIX
}];
break;
case "user":
case "assistant":
case "toolResult":
t.push(e);
return;
default: return;
}
const r = e.role === "custom" && asOptionalRecord(e.details)?.turnScoped;
if (e.role === "custom" && getOpenClawSystemUpdateKind(e) && typeof r === "boolean") {
t.push({
role: "user",
content: e.content,
timestamp: e.timestamp,
operatorMessage: { turnScoped: r }
});
return;
}
const i = e.role === "compactionSummary" ? normalizeCompactionSummaryTimestamp(e.timestamp) : e.timestamp;
if (isRuntimeContextCarrier(e)) {
if (n.some((e) => e.type === "image")) {
t.push({
role: "user",
content: n,
timestamp: i,
runtimeContextCarrier: true
});
return;
}
const r = n.filter((e) => e.type === "text");
const a = hasLegacyRuntimeContextEnvelope(r.map((e) => e.text).join("")) ? r : void 0;
const o = a ?? (typeof e.content === "string" ? e.content : r);
t.push({
role: "user",
content: a ?? labelRuntimeContextContent(o),
timestamp: i,
runtimeContext: {},
runtimeContextCarrier: true
});
} else t.push({
role: "user",
content: n,
timestamp: i,
synthetic: true
});
});
return t;
}
//#endregion
export { bashExecutionToText as a, createCompactionSummaryMessage as c, COMPACTION_SUMMARY_SUFFIX as i, createCustomMessage as l, BRANCH_SUMMARY_SUFFIX as n, convertToLlm as o, COMPACTION_SUMMARY_PREFIX as r, createBranchSummaryMessage as s, BRANCH_SUMMARY_PREFIX as t, isRuntimeContextCarrier as u };