1+ import {
2+ isOffsetInProtectedRanges ,
3+ type PlainTextToolCallNameMatcher ,
4+ type PlainTextToolCallParseOptions ,
5+ type PlainTextToolCallProtectedRangeResolver ,
6+ } from "./contracts.js" ;
17// Tool Call Repair module implements payload behavior.
28import {
39 consumeLineBreak ,
@@ -29,14 +35,6 @@ export type PlainTextToolCallBlock = {
2935 start : number ;
3036} ;
3137
32- /** Parser limits and allowlist options for plain-text tool-call repair. */
33- export type PlainTextToolCallParseOptions = {
34- /** Optional allowlist of tool names that may be repaired. */
35- allowedToolNames ?: Iterable < string > ;
36- /** Maximum serialized payload size accepted for one repaired call. */
37- maxPayloadBytes ?: number ;
38- } ;
39-
4038type NormalizedPlainTextToolCallParseOptions = Omit <
4139 PlainTextToolCallParseOptions ,
4240 "allowedToolNames"
@@ -70,11 +68,6 @@ export type PlainTextJsonToolCallScan =
7068 payload : PlainTextJsonToolCallSpan ;
7169 } ) ;
7270
73- export type PlainTextToolCallNameMatcher = {
74- hasExactName ( name : string ) : boolean ;
75- hasNamePrefix ( prefix : string ) : boolean ;
76- } ;
77-
7871type PlainTextToolCallScanBranches = {
7972 json : PlainTextJsonToolCallScan ;
8073 matches : { json : boolean ; xmlish : boolean } ;
@@ -681,7 +674,10 @@ export function parseStandalonePlainTextToolCallBlocks(
681674}
682675
683676/** Removes full-line standalone plain-text tool-call blocks from user-visible text. */
684- export function stripPlainTextToolCallBlocks ( text : string ) : string {
677+ export function stripPlainTextToolCallBlocks (
678+ text : string ,
679+ options : { resolveProtectedRanges ?: PlainTextToolCallProtectedRangeResolver } = { } ,
680+ ) : string {
685681 if (
686682 ! text ||
687683 ( ! / \[ (?: t o o l : ) ? [ A - Z a - z 0 - 9 _ - ] + \] / . test ( text ) &&
@@ -692,6 +688,7 @@ export function stripPlainTextToolCallBlocks(text: string): string {
692688 ) {
693689 return text ;
694690 }
691+ const protectedRanges = options . resolveProtectedRanges ?.( text ) ?? [ ] ;
695692 let result = "" ;
696693 let cursor = 0 ;
697694 let index = 0 ;
@@ -702,6 +699,10 @@ export function stripPlainTextToolCallBlocks(text: string): string {
702699 continue ;
703700 }
704701 const blockStart = skipLineIndentation ( text , index ) ;
702+ if ( isOffsetInProtectedRanges ( blockStart , protectedRanges ) ) {
703+ index += 1 ;
704+ continue ;
705+ }
705706 const scan = scanPlainTextToolCall ( text , blockStart ) ;
706707 if ( scan . kind === "prefix" && scan . completeEnd === undefined ) {
707708 return result + text . slice ( cursor ) ;
@@ -719,6 +720,9 @@ export function stripPlainTextToolCallBlocks(text: string): string {
719720 result += text . slice ( cursor , index ) ;
720721 while ( true ) {
721722 const adjacentStart = skipLineIndentation ( text , blockEnd ) ;
723+ if ( isOffsetInProtectedRanges ( adjacentStart , protectedRanges ) ) {
724+ break ;
725+ }
722726 const adjacent = scanPlainTextToolCall ( text , adjacentStart ) ;
723727 const adjacentEnd =
724728 adjacent . kind === "complete"
0 commit comments