1- /**
2- * Strip injected `§N§` tag prefixes AND defensively strip cargo-cult MC tag
3- * notation from assistant text before Pi persists the message.
4- *
5- * Mirrors OpenCode's `text-complete.ts` via {@link stripPersistedAssistantText}:
6- * whole `§N§` pairs globally, malformed hybrids, then stray `§`. Does not strip
7- * bare leading digits on the transform path.
8- *
9- * Pi persists raw assistant text from `message_end`; this hook mutates text parts
10- * before `agent-session.ts:appendMessage()` writes jsonl.
11- *
12- * Only `assistant` messages are stripped. User/tool messages keep intentional tags.
13- */
14-
15- import { stripPersistedAssistantText } from "../../plugin/src/hooks/magic-context/tag-content-primitives" ;
16-
17- /**
18- * Mutate the given assistant message's text parts in place to strip MC tag notation.
19- *
20- * Returns true if any text was modified. Production callers use `registerStripTagPrefix`.
21- */
22- export function stripTagPrefixFromAssistantMessage ( message : {
23- role : string ;
24- content : unknown ;
25- } ) : boolean {
26- if ( message . role !== "assistant" ) return false ;
27- if ( ! Array . isArray ( message . content ) ) return false ;
28-
29- let mutated = false ;
30- for ( const part of message . content ) {
31- if (
32- part === null ||
33- typeof part !== "object" ||
34- ( part as { type ?: unknown } ) . type !== "text"
35- ) {
36- continue ;
37- }
38- const textPart = part as { type : "text" ; text : unknown } ;
39- if ( typeof textPart . text !== "string" ) continue ;
40- const stripped = stripPersistedAssistantText ( textPart . text ) ;
41- if ( stripped !== textPart . text ) {
42- textPart . text = stripped ;
43- mutated = true ;
44- }
45- }
46- return mutated ;
47- }
48-
1+ /**
2+ * Strip injected `§N§` tag prefixes AND defensively strip cargo-cult MC tag
3+ * notation from assistant text before Pi persists the message.
4+ *
5+ * Mirrors OpenCode's `text-complete.ts` via {@link stripPersistedAssistantText}:
6+ * whole `§N§` pairs globally, malformed hybrids, then stray `§`. Does not strip
7+ * bare leading digits on the transform path.
8+ *
9+ * Pi persists raw assistant text from `message_end`; this hook mutates text parts
10+ * before `agent-session.ts:appendMessage()` writes jsonl.
11+ *
12+ * Only `assistant` messages are stripped. User/tool messages keep intentional tags.
13+ */
14+
15+ import { stripPersistedAssistantText } from "../../plugin/src/hooks/magic-context/tag-content-primitives" ;
16+
17+ /**
18+ * Mutate the given assistant message's text parts in place to strip MC tag notation.
19+ *
20+ * Returns true if any text was modified. Production callers use `registerStripTagPrefix`.
21+ */
22+
23+ export function stripTagPrefixFromAssistantMessage ( message : {
24+ role : string ;
25+
26+ content : unknown ;
27+ } ) : boolean {
28+ if ( message . role !== "assistant" ) return false ;
29+
30+ if ( ! Array . isArray ( message . content ) ) return false ;
31+
32+ let mutated = false ;
33+
34+ for ( const part of message . content ) {
35+ if (
36+ part === null ||
37+ typeof part !== "object" ||
38+ ( part as { type ?: unknown } ) . type !== "text"
39+ ) {
40+ continue ;
41+ }
42+
43+ const textPart = part as { type : "text" ; text : unknown } ;
44+
45+ if ( typeof textPart . text !== "string" ) continue ;
46+
47+ const stripped = stripPersistedAssistantText ( textPart . text ) ;
48+
49+ if ( stripped !== textPart . text ) {
50+ textPart . text = stripped ;
51+
52+ mutated = true ;
53+ }
54+ }
55+
56+ return mutated ;
57+ }
0 commit comments