mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 22:01:24 +00:00
fix(ui): strip inbound metadata blocks and guard reply-tag streaming (clean rewrite) (#22346)
* fix(ui): strip inbound metadata blocks from user messages * chore: clean up metadata-strip format and changelog credit * Update src/shared/chat-envelope.ts Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
This commit is contained in:
@@ -39,4 +39,35 @@ describe("stripEnvelopeFromMessage", () => {
|
||||
const result = stripEnvelopeFromMessage(input) as { content?: string };
|
||||
expect(result.content).toBe("note\n[message_id: 123]");
|
||||
});
|
||||
test("removes inbound un-bracketed conversation info blocks from user messages", () => {
|
||||
const input = {
|
||||
role: "user",
|
||||
content:
|
||||
'Conversation info (untrusted metadata):\n```json\n{\n "message_id": "123"\n}\n```\n\nHello there',
|
||||
};
|
||||
const result = stripEnvelopeFromMessage(input) as { content?: string };
|
||||
expect(result.content).toBe("Hello there");
|
||||
});
|
||||
|
||||
test("removes all inbound metadata blocks before user text", () => {
|
||||
const input = {
|
||||
role: "user",
|
||||
content:
|
||||
'Thread starter (untrusted, for context):\n```json\n{"seed": 1}\n```\n\nSender (untrusted metadata):\n```json\n{"name": "alice"}\n```\n\nActual user message',
|
||||
};
|
||||
const result = stripEnvelopeFromMessage(input) as { content?: string };
|
||||
expect(result.content).toBe("Actual user message");
|
||||
});
|
||||
|
||||
test("does not strip metadata-like blocks that are not a prefix", () => {
|
||||
const input = {
|
||||
role: "user",
|
||||
content:
|
||||
'Actual text\nConversation info (untrusted metadata):\n```json\n{"message_id": "123"}\n```\n\nFollow-up',
|
||||
};
|
||||
const result = stripEnvelopeFromMessage(input) as { content?: string };
|
||||
expect(result.content).toBe(
|
||||
'Actual text\nConversation info (untrusted metadata):\n```json\n{"message_id": "123"}\n```\n\nFollow-up',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { stripEnvelope, stripMessageIdHints } from "../shared/chat-envelope.js";
|
||||
import {
|
||||
stripEnvelope,
|
||||
stripInboundMetadataBlocks,
|
||||
stripMessageIdHints,
|
||||
} from "../shared/chat-envelope.js";
|
||||
|
||||
export { stripEnvelope };
|
||||
|
||||
@@ -12,7 +16,7 @@ function stripEnvelopeFromContent(content: unknown[]): { content: unknown[]; cha
|
||||
if (entry.type !== "text" || typeof entry.text !== "string") {
|
||||
return item;
|
||||
}
|
||||
const stripped = stripMessageIdHints(stripEnvelope(entry.text));
|
||||
const stripped = stripMessageIdHints(stripEnvelope(stripInboundMetadataBlocks(entry.text)));
|
||||
if (stripped === entry.text) {
|
||||
return item;
|
||||
}
|
||||
@@ -39,7 +43,7 @@ export function stripEnvelopeFromMessage(message: unknown): unknown {
|
||||
const next: Record<string, unknown> = { ...entry };
|
||||
|
||||
if (typeof entry.content === "string") {
|
||||
const stripped = stripMessageIdHints(stripEnvelope(entry.content));
|
||||
const stripped = stripMessageIdHints(stripEnvelope(stripInboundMetadataBlocks(entry.content)));
|
||||
if (stripped !== entry.content) {
|
||||
next.content = stripped;
|
||||
changed = true;
|
||||
@@ -51,7 +55,7 @@ export function stripEnvelopeFromMessage(message: unknown): unknown {
|
||||
changed = true;
|
||||
}
|
||||
} else if (typeof entry.text === "string") {
|
||||
const stripped = stripMessageIdHints(stripEnvelope(entry.text));
|
||||
const stripped = stripMessageIdHints(stripEnvelope(stripInboundMetadataBlocks(entry.text)));
|
||||
if (stripped !== entry.text) {
|
||||
next.text = stripped;
|
||||
changed = true;
|
||||
|
||||
Reference in New Issue
Block a user