@@ -18,6 +18,7 @@ import {
1818 parseAssistantTextSignature ,
1919 resolveAssistantMessagePhase ,
2020} from "../shared/chat-message-content.js" ;
21+ import { sanitizeAssistantVisibleTextWithProfile } from "../shared/text/assistant-visible-text.js" ;
2122import { isOpenClawDeliveryMirrorAssistantMessage } from "../shared/transcript-only-openclaw-assistant.js" ;
2223import { stripInlineDirectiveTagsForDisplay } from "../utils/directive-tags.js" ;
2324import { stripEnvelopeFromMessages } from "./chat-sanitize.js" ;
@@ -61,6 +62,15 @@ function truncateChatHistoryText(
6162 } ;
6263}
6364
65+ function sanitizeAssistantHistoryDisplayText ( text : string ) : { text : string ; changed : boolean } {
66+ const stripped = stripInlineDirectiveTagsForDisplay ( text ) ;
67+ const sanitized = sanitizeAssistantVisibleTextWithProfile ( stripped . text , "history" ) ;
68+ return {
69+ text : sanitized ,
70+ changed : stripped . changed || sanitized !== stripped . text ,
71+ } ;
72+ }
73+
6474/** Return true for known tool-call/tool-result block type spellings in transcripts. */
6575export function isToolHistoryBlockType ( type : unknown ) : boolean {
6676 if ( typeof type !== "string" ) {
@@ -251,7 +261,7 @@ export function augmentChatHistoryWithCanvasBlocks(messages: unknown[]): unknown
251261
252262function sanitizeChatHistoryContentBlock (
253263 block : unknown ,
254- opts ?: { preserveExactToolPayload ?: boolean ; maxChars ?: number } ,
264+ opts ?: { assistantDisplayText ?: boolean ; preserveExactToolPayload ?: boolean ; maxChars ?: number } ,
255265) : { block : unknown ; changed : boolean } {
256266 if ( ! block || typeof block !== "object" ) {
257267 return { block, changed : false } ;
@@ -261,8 +271,12 @@ function sanitizeChatHistoryContentBlock(
261271 const preserveExactToolPayload =
262272 opts ?. preserveExactToolPayload === true || isToolHistoryBlockType ( entry . type ) ;
263273 const maxChars = opts ?. maxChars ?? DEFAULT_CHAT_HISTORY_TEXT_MAX_CHARS ;
274+ const sanitizeAssistantDisplayText =
275+ opts ?. assistantDisplayText === true && ! preserveExactToolPayload ;
264276 if ( typeof entry . text === "string" ) {
265- const stripped = stripInlineDirectiveTagsForDisplay ( entry . text ) ;
277+ const stripped = sanitizeAssistantDisplayText
278+ ? sanitizeAssistantHistoryDisplayText ( entry . text )
279+ : stripInlineDirectiveTagsForDisplay ( entry . text ) ;
266280 if ( preserveExactToolPayload ) {
267281 entry . text = stripped . text ;
268282 changed ||= stripped . changed ;
@@ -273,7 +287,9 @@ function sanitizeChatHistoryContentBlock(
273287 }
274288 }
275289 if ( typeof entry . content === "string" ) {
276- const stripped = stripInlineDirectiveTagsForDisplay ( entry . content ) ;
290+ const stripped = sanitizeAssistantDisplayText
291+ ? sanitizeAssistantHistoryDisplayText ( entry . content )
292+ : stripInlineDirectiveTagsForDisplay ( entry . content ) ;
277293 if ( preserveExactToolPayload ) {
278294 entry . content = stripped . text ;
279295 changed ||= stripped . changed ;
@@ -383,7 +399,7 @@ function projectAssistantTextFromMixedToolContent(
383399 if ( entry . type !== "text" || typeof entry . text !== "string" || ! entry . text . trim ( ) ) {
384400 continue ;
385401 }
386- const stripped = stripInlineDirectiveTagsForDisplay ( entry . text ) ;
402+ const stripped = sanitizeAssistantHistoryDisplayText ( entry . text ) ;
387403 const truncated = truncateChatHistoryText ( stripped . text , maxChars ) ;
388404 if ( truncated . text . trim ( ) ) {
389405 textBlocks . push ( { type : "text" , text : truncated . text } ) ;
@@ -504,8 +520,13 @@ function sanitizeChatHistoryMessage(
504520 }
505521 }
506522
523+ const isAssistant = entry . role === "assistant" ;
524+ const sanitizeAssistantDisplayText = isAssistant && ! preserveExactToolPayload ;
525+
507526 if ( typeof entry . content === "string" ) {
508- const stripped = stripInlineDirectiveTagsForDisplay ( entry . content ) ;
527+ const stripped = sanitizeAssistantDisplayText
528+ ? sanitizeAssistantHistoryDisplayText ( entry . content )
529+ : stripInlineDirectiveTagsForDisplay ( entry . content ) ;
509530 if ( preserveExactToolPayload ) {
510531 entry . content = stripped . text ;
511532 changed ||= stripped . changed ;
@@ -516,7 +537,11 @@ function sanitizeChatHistoryMessage(
516537 }
517538 } else if ( Array . isArray ( entry . content ) ) {
518539 const updated = entry . content . map ( ( block ) =>
519- sanitizeChatHistoryContentBlock ( block , { preserveExactToolPayload, maxChars } ) ,
540+ sanitizeChatHistoryContentBlock ( block , {
541+ assistantDisplayText : isAssistant ,
542+ preserveExactToolPayload,
543+ maxChars,
544+ } ) ,
520545 ) ;
521546 if ( updated . some ( ( item ) => item . changed ) ) {
522547 entry . content = updated . map ( ( item ) => item . block ) ;
@@ -541,7 +566,9 @@ function sanitizeChatHistoryMessage(
541566 }
542567
543568 if ( typeof entry . text === "string" ) {
544- const stripped = stripInlineDirectiveTagsForDisplay ( entry . text ) ;
569+ const stripped = sanitizeAssistantDisplayText
570+ ? sanitizeAssistantHistoryDisplayText ( entry . text )
571+ : stripInlineDirectiveTagsForDisplay ( entry . text ) ;
545572 if ( preserveExactToolPayload ) {
546573 entry . text = stripped . text ;
547574 changed ||= stripped . changed ;
0 commit comments