@@ -14,6 +14,9 @@ import { handleSweepCommand } from "./commands/sweep"
1414import { handleManualToggleCommand , handleManualTriggerCommand } from "./commands/manual"
1515import { ensureSessionInitialized } from "./state/state"
1616import { cacheSystemPromptTokens } from "./ui/utils"
17+ import { scrubReadToolDefinition } from "./tools/scrub"
18+ import { clog , C } from "./compress-logger"
19+ import z from "zod"
1720
1821const INTERNAL_AGENT_SIGNATURES = [
1922 "You are a title generator" ,
@@ -128,6 +131,87 @@ export function createChatMessageTransformHandler(
128131 }
129132}
130133
134+ function zodTypeName ( schema : z . ZodTypeAny ) : string {
135+ const def = ( schema as { _def ?: { typeName ?: string } } ) . _def
136+ if ( def ?. typeName ) {
137+ return def . typeName
138+ }
139+
140+ if ( schema . constructor ?. name ) {
141+ return schema . constructor . name
142+ }
143+
144+ return "unknown"
145+ }
146+
147+ function describeZod ( schema : z . ZodTypeAny ) : {
148+ type : string
149+ description ?: string
150+ optional ?: boolean
151+ } {
152+ if ( schema instanceof z . ZodOptional ) {
153+ const inner = schema . _def . innerType as z . ZodTypeAny
154+ const base = describeZod ( inner )
155+ return {
156+ ...base ,
157+ optional : true ,
158+ }
159+ }
160+
161+ const def = ( schema as { _def ?: { description ?: string } } ) . _def
162+ const description = typeof def ?. description === "string" ? def . description : undefined
163+ return {
164+ type : zodTypeName ( schema ) ,
165+ description,
166+ }
167+ }
168+
169+ function formatParameters ( parameters : unknown ) : unknown {
170+ if ( ! ( parameters instanceof z . ZodObject ) ) {
171+ return parameters
172+ }
173+
174+ const shape = parameters . shape
175+ const properties = Object . fromEntries (
176+ Object . entries ( shape ) . map ( ( [ key , value ] ) => {
177+ return [ key , describeZod ( value as z . ZodTypeAny ) ]
178+ } ) ,
179+ )
180+
181+ return {
182+ type : "object" ,
183+ properties,
184+ }
185+ }
186+
187+ export function createToolDefinitionHandler ( logger : Logger ) {
188+ return async ( input : { toolID : string } , output : { description : string ; parameters : any } ) => {
189+ if ( input . toolID !== "read" ) {
190+ return
191+ }
192+
193+ const stats = scrubReadToolDefinition ( output )
194+ const parameters = formatParameters ( output . parameters )
195+ const descriptionLines = output . description . split ( "\n" )
196+
197+ clog . info ( C . COMPRESS , "Read tool definition (scrubbed)" , {
198+ toolID : input . toolID ,
199+ descriptionLines,
200+ parameters,
201+ descriptionLinesRemoved : stats . descriptionLinesRemoved ,
202+ removedDescriptionLines : stats . removedDescriptionLines ,
203+ removedSchemaProperties : stats . removedSchemaProperties ,
204+ } )
205+
206+ logger . debug ( "Read tool definition (scrubbed)" , {
207+ toolID : input . toolID ,
208+ descriptionLinesRemoved : stats . descriptionLinesRemoved ,
209+ removedDescriptionLines : stats . removedDescriptionLines ,
210+ removedSchemaProperties : stats . removedSchemaProperties ,
211+ } )
212+ }
213+ }
214+
131215export function createCommandExecuteHandler (
132216 client : any ,
133217 state : SessionState ,
0 commit comments