openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
202 lines (201 loc) • 8.64 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { a as emitAgentEvent } from "./agent-events-BdfWYmot.mjs";
import { l as isSyntheticMissingToolResult } from "./tool-result-pairing-gfPySfH_.mjs";
import { s as readSessionTranscriptRunId } from "./transcript-events-BHwlF03q.mjs";
import { a as readToolErrorFlag, i as isToolResultContentType, n as isToolCallContentType, s as resolveToolUseId } from "./tool-content-DG-uYHoo.mjs";
import { a as readNestedToolActivity, r as nestedToolActivityContent } from "./nested-tool-activity-BiEuCyrz.mjs";
import { n as projectAgentActivityItem, r as resolveCompletedActivityWrappers } from "./agent-activity-presentation-BIQtAp6v.mjs";
import { n as isToolResultError } from "./tool-result-error-B2GHe83l.mjs";
import { i as isCommandBearingToolCall, o as resolveToolDisplay, r as inferToolMetaFromArgsCore, s as unwrapToolCallForDisplay } from "./tool-display-BQXZgFy6.mjs";
//#region src/infra/agent-activity-events.ts
function projectAgentToolActivity(e) {
const t = {
name: e.name,
args: e.args
};
const n = unwrapToolCallForDisplay(t);
const r = n === t && e.presentationName ? {
name: e.presentationName,
args: e.args
} : n;
const i = e.meta ?? inferToolMetaFromArgsCore(r.name, r.args);
const a = resolveToolDisplay(r).label;
const o = asOptionalRecord(asOptionalRecord(e.result)?.details);
const s = e.phase === "result" && (o?.status === "approval-pending" || o?.status === "approval-unavailable");
const c = e.phase === "result" && o?.status === "skipped";
const l = e.phase !== "result" ? "running" : c && o.deniedReason === "steering" ? "skipped" : s || c ? "blocked" : e.status === "unknown" ? void 0 : e.status ?? (e.isError === true ? "failed" : e.isError === false ? "completed" : void 0);
return {
...projectAgentActivityItem({
itemId: `tool:${e.toolCallId}`,
toolCallId: e.toolCallId,
name: e.name,
kind: "tool",
phase: e.phase === "result" ? "end" : e.phase,
title: i ? `${a} ${i}` : a,
...l ? { status: l } : { summary: "Outcome unknown" },
...s ? {
approvalId: normalizeOptionalString(o?.approvalId),
approvalSlug: normalizeOptionalString(o?.approvalSlug),
summary: o?.status === "approval-pending" ? "Awaiting approval before command can run." : "Command is blocked because no interactive approval route is available."
} : {},
...c ? { summary: "Skipped" } : {},
...i ? { meta: i } : {},
commandBearing: isCommandBearingToolCall(r.name, r.args),
...e.hideFromChannelProgress ? { hideFromChannelProgress: true } : {}
}, {
args: e.args,
result: e.result,
nativeOperation: e.nativeOperation
}),
name: r.name
};
}
/** Poll is the only process action that returns an aggregate for one session. */
function isProcessPollResultDetails(e) {
const t = asOptionalRecord(e);
return Boolean(t && typeof t.sessionId === "string" && (t.status === "running" || t.status === "completed") && (typeof t.aggregated === "string" || t.persistedDetailsTruncated === true && Array.isArray(t.originalDetailKeys) && t.originalDetailKeys.includes("aggregated")));
}
function projectAgentHistoryActivity(e) {
const t = /* @__PURE__ */ new Map();
let n = 0;
const r = e.map(({ messageId: e, message: t }) => {
const r = asOptionalRecord(t);
const i = asOptionalRecord(r?.["__openclaw"]);
if (r?.role === "user" && !i?.steerTargetRunId && !(r.excludeFromContext === true && i?.contextFreeCommand === true)) n += 1;
const a = readNestedToolActivity(t);
const o = a?.details;
const s = (a ? nestedToolActivityContent(a) : Array.isArray(r?.content) ? r.content : []).flatMap((e) => {
const t = asOptionalRecord(e);
return t && (isToolCallContentType(t.type) || isToolResultContentType(t.type)) ? [t] : [];
});
if (r && (isToolResultContentType(r.role) || r.role === "tool" || r.role === "function")) s.splice(0, s.length, r);
return {
messageId: e,
hasTools: s.length > 0,
blocks: s.map((i, a) => {
const s = normalizeOptionalString(i.toolCallId) ?? normalizeOptionalString(i.tool_call_id) ?? normalizeOptionalString(i.toolUseId) ?? normalizeOptionalString(i.tool_use_id) ?? (i === r ? void 0 : resolveToolUseId(i));
const c = normalizeOptionalString(i.runId) ?? o?.runId ?? readSessionTranscriptRunId(t) ?? normalizeOptionalString(r?.runId);
return {
block: i,
runId: c ? JSON.stringify([n, c]) : void 0,
parentToolCallId: o && o.toolCallId === s ? o.parentToolCallId : normalizeOptionalString(i.parentToolCallId),
key: s ? JSON.stringify([
n,
o?.runId ?? readSessionTranscriptRunId(t),
o?.scopeId,
s
]) : "history:" + e + ":" + a,
toolCallId: s ?? "history:" + e + ":" + a,
callId: s,
executedArgs: o && o.toolCallId === s ? o.input : void 0,
isError: readToolErrorFlag(i) ?? (r ? readToolErrorFlag(r) : void 0) ?? (typeof o?.isError === "boolean" ? o.isError : void 0)
};
})
};
});
const i = /* @__PURE__ */ new Map();
for (const { blocks: e } of r) for (const { block: t, key: n } of e) {
const e = i.get(n) ?? {
calls: 0,
results: 0,
synthetic: 0
};
e[isToolCallContentType(t.type) ? "calls" : isSyntheticMissingToolResult(t) ? "synthetic" : "results"] += 1;
i.set(n, e);
}
for (const e of r) {
e.blocks = e.blocks.filter(({ block: e, key: t }) => {
const n = i.get(t);
return !(n.calls <= 1 && n.results === 1 && isSyntheticMissingToolResult(e));
});
for (const [t, n] of e.blocks.entries()) {
const r = i.get(n.key);
if (r.calls > 1 || r.results + (r.results === 1 ? 0 : r.synthetic) > 1) n.key = "history:" + e.messageId + ":" + t;
}
}
for (const { blocks: e } of r) for (const { block: n, key: r, toolCallId: i, callId: a, executedArgs: o, runId: s, parentToolCallId: c } of e) if (isToolCallContentType(n.type)) {
const e = normalizeOptionalString(n.name) ?? normalizeOptionalString(n.toolName) ?? "Tool";
const l = o ?? n.arguments ?? n.args ?? n.input;
t.set(r, {
toolCallId: i,
callId: a,
runId: s,
parentToolCallId: c,
name: e,
presentationName: unwrapToolCallForDisplay({
name: e,
args: l
}).name,
phase: "result",
args: l
});
}
for (const { blocks: e } of r) for (const { block: n, key: r, toolCallId: i, callId: a, executedArgs: o, isError: s, runId: c, parentToolCallId: l } of e) {
if (isToolCallContentType(n.type)) continue;
const e = t.get(r);
const u = e?.name ?? normalizeOptionalString(n.toolName) ?? normalizeOptionalString(n.name) ?? "Tool";
const d = s === true || isToolResultError(n);
const f = asOptionalRecord(n.details);
const p = (u === "exec" || u === "bash" || u === "process") && f?.status === "completed" && typeof f.exitCode === "number" && Number.isFinite(f.exitCode) && f.exitCode !== 0 && f.persistedDetailsTruncated === true && f.exitReason === void 0 && Array.isArray(f.originalDetailKeys) && f.originalDetailKeys.includes("exitReason");
const m = u === "process" && isProcessPollResultDetails(f);
t.set(r, {
...e,
toolCallId: i,
callId: a,
runId: e?.runId ?? c,
parentToolCallId: e?.parentToolCallId ?? l,
name: u,
phase: "result",
args: o ?? e?.args,
result: n,
isError: d ? true : m ? false : s,
...!d && p ? { status: "unknown" } : {},
...m ? { nativeOperation: "process.poll" } : {}
});
}
const a = [...t].map(([e, t]) => {
const n = projectAgentToolActivity({
...t,
...t.name === "collab.wait" ? { nativeOperation: "wait" } : {}
});
if (t.result === void 0) {
n.unpairedCall = true;
delete n.summary;
}
return {
key: e,
callId: t.callId,
runId: t.runId,
parentToolCallId: t.parentToolCallId,
activity: n
};
});
const o = resolveCompletedActivityWrappers(a);
const s = new Map(a.map((e) => [e.key, e]));
return r.flatMap(({ messageId: e, blocks: t, hasTools: n }) => {
if (!n) return [];
const r = /* @__PURE__ */ new Map();
for (const { key: e } of t) {
const t = s.get(e);
if (!t || o.has(t)) continue;
const n = t.activity;
if (!n.hideFromChannelProgress && !n.suppressChannelProgress) r.set(n.itemId, n);
}
return [{
messageId: e,
items: [...r.values()]
}];
});
}
/** Emits a typed activity event on the shared agent event bus. */
function emitAgentActivityEvent(e) {
emitAgentEvent({
runId: e.runId,
stream: e.stream,
data: e.data,
...e.sessionKey ? { sessionKey: e.sessionKey } : {}
});
}
//#endregion
export { projectAgentHistoryActivity as n, projectAgentToolActivity as r, emitAgentActivityEvent as t };