@@ -8,6 +8,7 @@ import process from 'node:process';
88
99import { DAEMON_CLIENT_NAME } from '../daemon/utils.js' ;
1010import { logger } from '../logger.js' ;
11+ import { zod , type ShapeOutput } from '../third_party/index.js' ;
1112
1213import type { LocalState , Persistence } from './persistence.js' ;
1314import { FilePersistence } from './persistence.js' ;
@@ -20,6 +21,78 @@ import {
2021import { WatchdogClient } from './WatchdogClient.js' ;
2122
2223const MS_PER_DAY = 24 * 60 * 60 * 1000 ;
24+ const PARAM_BLOCKLIST = [ 'uid' ] ;
25+
26+ function getZodType ( zodType : any ) : string {
27+ const def = zodType . _def ;
28+ const typeName = def . typeName ;
29+
30+ if (
31+ typeName === 'ZodOptional' ||
32+ typeName === 'ZodDefault' ||
33+ typeName === 'ZodNullable'
34+ ) {
35+ return getZodType ( def . innerType ) ;
36+ }
37+ if ( typeName === 'ZodEffects' ) {
38+ return getZodType ( def . schema ) ;
39+ }
40+
41+ let type : string ;
42+ switch ( typeName ) {
43+ case 'ZodString' :
44+ return 'string' ;
45+ case 'ZodNumber' :
46+ return 'number' ;
47+ case 'ZodBoolean' :
48+ return 'boolean' ;
49+ case 'ZodArray' :
50+ return 'array' ;
51+ case 'ZodEnum' :
52+ return 'enum' ;
53+ default :
54+ throw new Error ( `Unsupported zod type for tool parameter: ${ typeName } ` ) ;
55+ }
56+ }
57+
58+ function filterBlockedParams (
59+ params : ShapeOutput < zod . ZodRawShape > ,
60+ ) : ShapeOutput < zod . ZodRawShape > {
61+ const filteredParams = { ...params } ;
62+ for ( const key of PARAM_BLOCKLIST ) {
63+ if ( key in filteredParams ) {
64+ delete filteredParams [ key ] ;
65+ }
66+ }
67+ return filteredParams ;
68+ }
69+
70+ function transformParams (
71+ params : Record < string , any > ,
72+ schema : zod . ZodRawShape ,
73+ ) : Record < string , any > {
74+ const transformed : Record < string , any > = { } ;
75+ for ( const [ name , value ] of Object . entries ( params ) ) {
76+ const zodType = getZodType ( schema [ name ] ) ;
77+
78+ if ( zodType === 'string' ) {
79+ transformed [ `${ name } _length` ] = ( value as string ) . length ;
80+ } else if ( zodType === 'array' ) {
81+ transformed [ `${ name } _count` ] = ( value as any [ ] ) . length ;
82+ } else {
83+ transformed [ name ] = value ;
84+ }
85+ }
86+ return transformed ;
87+ }
88+
89+ export function sanitizeParams (
90+ params : ShapeOutput < zod . ZodRawShape > ,
91+ schema : zod . ZodRawShape ,
92+ ) : Record < string , any > {
93+ const filtered = filterBlockedParams ( params ) ;
94+ return transformParams ( filtered , schema ) ;
95+ }
2396
2497function detectOsType ( ) : OsType {
2598 switch ( process . platform ) {
0 commit comments