|
1 | 1 | import type { AgentMessage } from "@mariozechner/pi-agent-core"; |
2 | 2 | import type { SessionManager } from "@mariozechner/pi-coding-agent"; |
| 3 | +import { stripInboundMetadata } from "../../auto-reply/reply/strip-inbound-meta.js"; |
3 | 4 | import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
4 | 5 | import type { ProviderRuntimeModel } from "../../plugins/provider-runtime-model.types.js"; |
5 | 6 | import { |
@@ -237,6 +238,7 @@ function stripStaleAssistantUsageBeforeLatestCompaction(messages: AgentMessage[] |
237 | 238 | // content and, on Bedrock or strict OpenAI-compatible providers, can also |
238 | 239 | // trigger turn-ordering rejections. |
239 | 240 | const TRANSCRIPT_ONLY_OPENCLAW_MODELS = new Set<string>(["delivery-mirror", "gateway-injected"]); |
| 241 | +const OMITTED_INBOUND_METADATA_TEXT = "[assistant copied inbound metadata omitted]"; |
240 | 242 |
|
241 | 243 | function isTranscriptOnlyOpenclawAssistant(message: AgentMessage): boolean { |
242 | 244 | if (!message || message.role !== "assistant") { |
@@ -267,13 +269,48 @@ export function normalizeAssistantReplayContent(messages: AgentMessage[]): Agent |
267 | 269 | } |
268 | 270 | const replayContent = (message as { content?: unknown }).content; |
269 | 271 | if (typeof replayContent === "string") { |
| 272 | + const strippedText = stripInboundMetadata(replayContent); |
270 | 273 | out.push({ |
271 | 274 | ...message, |
272 | | - content: [{ type: "text", text: replayContent }], |
| 275 | + content: [ |
| 276 | + { |
| 277 | + type: "text", |
| 278 | + text: strippedText.trim() ? strippedText : OMITTED_INBOUND_METADATA_TEXT, |
| 279 | + }, |
| 280 | + ], |
273 | 281 | }); |
274 | 282 | touched = true; |
275 | 283 | continue; |
276 | 284 | } |
| 285 | + if (Array.isArray(replayContent)) { |
| 286 | + let contentTouched = false; |
| 287 | + const sanitizedContent = replayContent.map((block) => { |
| 288 | + if (!block || typeof block !== "object") { |
| 289 | + return block; |
| 290 | + } |
| 291 | + const text = (block as { text?: unknown }).text; |
| 292 | + if (typeof text !== "string") { |
| 293 | + return block; |
| 294 | + } |
| 295 | + const strippedText = stripInboundMetadata(text); |
| 296 | + if (strippedText === text) { |
| 297 | + return block; |
| 298 | + } |
| 299 | + contentTouched = true; |
| 300 | + return { |
| 301 | + ...block, |
| 302 | + text: strippedText.trim() ? strippedText : OMITTED_INBOUND_METADATA_TEXT, |
| 303 | + }; |
| 304 | + }); |
| 305 | + if (contentTouched) { |
| 306 | + out.push({ |
| 307 | + ...message, |
| 308 | + content: sanitizedContent, |
| 309 | + }); |
| 310 | + touched = true; |
| 311 | + continue; |
| 312 | + } |
| 313 | + } |
277 | 314 | if (Array.isArray(replayContent) && replayContent.length === 0) { |
278 | 315 | // An assistant turn can legitimately end with `content: []` — for |
279 | 316 | // example the silent-reply / NO_REPLY path locked in by |
|
0 commit comments