fix(gateway): strip inline directive tags from displayed text

This commit is contained in:
Peter Steinberger
2026-02-21 20:08:13 +01:00
parent 4540790cb6
commit f9108120c2
9 changed files with 199 additions and 19 deletions

View File

@@ -10,6 +10,7 @@ import type { MsgContext } from "../../auto-reply/templating.js";
import { createReplyPrefixOptions } from "../../channels/reply-prefix.js";
import { resolveSessionFilePath } from "../../config/sessions.js";
import { resolveSendPolicy } from "../../sessions/send-policy.js";
import { stripInlineDirectiveTagsForDisplay } from "../../utils/directive-tags.js";
import { INTERNAL_MESSAGE_CHANNEL } from "../../utils/message-channel.js";
import {
abortChatRunById,
@@ -103,9 +104,10 @@ function sanitizeChatHistoryContentBlock(block: unknown): { block: unknown; chan
const entry = { ...(block as Record<string, unknown>) };
let changed = false;
if (typeof entry.text === "string") {
const res = truncateChatHistoryText(entry.text);
const stripped = stripInlineDirectiveTagsForDisplay(entry.text);
const res = truncateChatHistoryText(stripped.text);
entry.text = res.text;
changed ||= res.truncated;
changed ||= stripped.changed || res.truncated;
}
if (typeof entry.partialJson === "string") {
const res = truncateChatHistoryText(entry.partialJson);
@@ -158,9 +160,10 @@ function sanitizeChatHistoryMessage(message: unknown): { message: unknown; chang
}
if (typeof entry.content === "string") {
const res = truncateChatHistoryText(entry.content);
const stripped = stripInlineDirectiveTagsForDisplay(entry.content);
const res = truncateChatHistoryText(stripped.text);
entry.content = res.text;
changed ||= res.truncated;
changed ||= stripped.changed || res.truncated;
} else if (Array.isArray(entry.content)) {
const updated = entry.content.map((block) => sanitizeChatHistoryContentBlock(block));
if (updated.some((item) => item.changed)) {
@@ -170,9 +173,10 @@ function sanitizeChatHistoryMessage(message: unknown): { message: unknown; chang
}
if (typeof entry.text === "string") {
const res = truncateChatHistoryText(entry.text);
const stripped = stripInlineDirectiveTagsForDisplay(entry.text);
const res = truncateChatHistoryText(stripped.text);
entry.text = res.text;
changed ||= res.truncated;
changed ||= stripped.changed || res.truncated;
}
return { message: changed ? entry : message, changed };