mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-11 03:44:31 +00:00
fix: hide synthetic untrusted metadata in chat history
This commit is contained in:
@@ -3,7 +3,10 @@ import { stripEnvelope, stripMessageIdHints } from "../shared/chat-envelope.js";
|
||||
|
||||
export { stripEnvelope };
|
||||
|
||||
function stripEnvelopeFromContent(content: unknown[]): { content: unknown[]; changed: boolean } {
|
||||
function stripEnvelopeFromContentWithRole(
|
||||
content: unknown[],
|
||||
stripUserEnvelope: boolean,
|
||||
): { content: unknown[]; changed: boolean } {
|
||||
let changed = false;
|
||||
const next = content.map((item) => {
|
||||
if (!item || typeof item !== "object") {
|
||||
@@ -13,7 +16,10 @@ function stripEnvelopeFromContent(content: unknown[]): { content: unknown[]; cha
|
||||
if (entry.type !== "text" || typeof entry.text !== "string") {
|
||||
return item;
|
||||
}
|
||||
const stripped = stripMessageIdHints(stripEnvelope(stripInboundMetadata(entry.text)));
|
||||
const inboundStripped = stripInboundMetadata(entry.text);
|
||||
const stripped = stripUserEnvelope
|
||||
? stripMessageIdHints(stripEnvelope(inboundStripped))
|
||||
: inboundStripped;
|
||||
if (stripped === entry.text) {
|
||||
return item;
|
||||
}
|
||||
@@ -32,27 +38,31 @@ export function stripEnvelopeFromMessage(message: unknown): unknown {
|
||||
}
|
||||
const entry = message as Record<string, unknown>;
|
||||
const role = typeof entry.role === "string" ? entry.role.toLowerCase() : "";
|
||||
if (role !== "user") {
|
||||
return message;
|
||||
}
|
||||
const stripUserEnvelope = role === "user";
|
||||
|
||||
let changed = false;
|
||||
const next: Record<string, unknown> = { ...entry };
|
||||
|
||||
if (typeof entry.content === "string") {
|
||||
const stripped = stripMessageIdHints(stripEnvelope(stripInboundMetadata(entry.content)));
|
||||
const inboundStripped = stripInboundMetadata(entry.content);
|
||||
const stripped = stripUserEnvelope
|
||||
? stripMessageIdHints(stripEnvelope(inboundStripped))
|
||||
: inboundStripped;
|
||||
if (stripped !== entry.content) {
|
||||
next.content = stripped;
|
||||
changed = true;
|
||||
}
|
||||
} else if (Array.isArray(entry.content)) {
|
||||
const updated = stripEnvelopeFromContent(entry.content);
|
||||
const updated = stripEnvelopeFromContentWithRole(entry.content, stripUserEnvelope);
|
||||
if (updated.changed) {
|
||||
next.content = updated.content;
|
||||
changed = true;
|
||||
}
|
||||
} else if (typeof entry.text === "string") {
|
||||
const stripped = stripMessageIdHints(stripEnvelope(stripInboundMetadata(entry.text)));
|
||||
const inboundStripped = stripInboundMetadata(entry.text);
|
||||
const stripped = stripUserEnvelope
|
||||
? stripMessageIdHints(stripEnvelope(inboundStripped))
|
||||
: inboundStripped;
|
||||
if (stripped !== entry.text) {
|
||||
next.text = stripped;
|
||||
changed = true;
|
||||
|
||||
Reference in New Issue
Block a user