1+ import * as crypto from 'crypto'
2+
3+ const MAX_TOOL_NAME_LENGTH = 64
4+
5+ // Slice without breaking surrogate pairs.
6+ function safeSlice ( s : string , end : number ) : string {
7+ if ( end <= 0 ) return ''
8+ if ( end >= s . length ) return s
9+ const code = s . charCodeAt ( end - 1 )
10+ if ( code >= 0xd800 && code <= 0xdbff ) end -= 1
11+ return s . slice ( 0 , end )
12+ }
13+
14+ export function shortenToolName ( name : string ) : string {
15+ if ( ! name || name . length <= MAX_TOOL_NAME_LENGTH ) return name
16+ const hash = crypto . createHash ( 'sha256' ) . update ( name ) . digest ( 'hex' ) . slice ( 0 , 12 )
17+ const prefix = safeSlice ( name , MAX_TOOL_NAME_LENGTH - hash . length - 1 )
18+ return `${ prefix } _${ hash } `
19+ }
20+
21+ export function buildToolNameMaps ( tools : any [ ] ) : {
22+ toKiroName : ( name : string ) => string
23+ fromKiroName : ( name : string ) => string
24+ } {
25+ const originalToAlias = new Map < string , string > ( )
26+ const aliasToOriginal = new Map < string , string > ( )
27+
28+ for ( const t of tools ) {
29+ const name = t . name || t . function ?. name
30+ if ( ! name ) continue
31+ const alias = shortenToolName ( name )
32+ originalToAlias . set ( name , alias )
33+ if ( alias !== name ) aliasToOriginal . set ( alias , name )
34+ }
35+
36+ return {
37+ toKiroName : ( name : string ) => originalToAlias . get ( name ) || shortenToolName ( name ) ,
38+ fromKiroName : ( name : string ) => aliasToOriginal . get ( name ) || name
39+ }
40+ }
41+
42+ function sanitizeToolInput ( input : any ) : any {
43+ if ( ! input || typeof input !== 'object' || Array . isArray ( input ) ) return input
44+ const result : Record < string , any > = { }
45+ for ( const [ key , value ] of Object . entries ( input ) ) {
46+ if ( key === '' ) continue
47+ result [ key ] = value
48+ }
49+ return result
50+ }
51+
52+ function sanitizeSchema ( schema : any , seen : WeakSet < object > = new WeakSet ( ) ) : any {
53+ if ( ! schema || typeof schema !== 'object' || Array . isArray ( schema ) ) return schema
54+ if ( seen . has ( schema ) ) return { }
55+ seen . add ( schema )
56+
57+ const result : Record < string , any > = { }
58+ for ( const [ key , value ] of Object . entries ( schema ) ) {
59+ if ( key === 'additionalProperties' ) continue
60+ if ( key === 'required' && Array . isArray ( value ) && value . length === 0 ) continue
61+
62+ if (
63+ ( key === 'properties' ||
64+ key === 'patternProperties' ||
65+ key === '$defs' ||
66+ key === 'definitions' ) &&
67+ typeof value === 'object' &&
68+ value !== null &&
69+ ! Array . isArray ( value )
70+ ) {
71+ const props : Record < string , any > = { }
72+ for ( const [ pk , pv ] of Object . entries ( value ) ) {
73+ props [ pk ] = sanitizeSchema ( pv , seen )
74+ }
75+ result [ key ] = props
76+ } else if (
77+ ( key === 'anyOf' || key === 'oneOf' || key === 'allOf' || key === 'prefixItems' ) &&
78+ Array . isArray ( value )
79+ ) {
80+ result [ key ] = value . map ( ( v ) => sanitizeSchema ( v , seen ) )
81+ } else if (
82+ ( key === 'items' || key === 'not' || key === 'contains' ) &&
83+ typeof value === 'object'
84+ ) {
85+ result [ key ] = sanitizeSchema ( value , seen )
86+ } else {
87+ result [ key ] = value
88+ }
89+ }
90+ return result
91+ }
92+
193export function convertToolsToCodeWhisperer ( tools : any [ ] ) : any [ ] {
294 return tools . map ( ( t ) => ( {
395 toolSpecification : {
4- name : t . name || t . function ?. name ,
96+ name : shortenToolName ( t . name || t . function ?. name || '' ) ,
597 description : ( t . description || t . function ?. description || '' ) . substring ( 0 , 9216 ) ,
6- inputSchema : { json : t . input_schema || t . function ?. parameters || { } }
98+ inputSchema : {
99+ json : sanitizeSchema ( sanitizeToolInput ( t . input_schema || t . function ?. parameters || { } ) )
100+ }
7101 }
8102 } ) )
9103}
@@ -19,3 +113,19 @@ export function deduplicateToolResults(trs: any[]): any[] {
19113 }
20114 return u
21115}
116+
117+ export function deduplicateToolCallsByContent ( toolCalls : any [ ] ) : any [ ] {
118+ const seen = new Set < string > ( )
119+ const unique : any [ ] = [ ]
120+ for ( const tc of toolCalls ) {
121+ // \x00 as separator (can't appear in a tool name)
122+ const name = tc . name || tc . function ?. name || ''
123+ const input = tc . input || tc . function ?. arguments || ''
124+ const key = `${ name } \x00${ typeof input === 'string' ? input : JSON . stringify ( input ) } `
125+ if ( ! seen . has ( key ) ) {
126+ seen . add ( key )
127+ unique . push ( tc )
128+ }
129+ }
130+ return unique
131+ }
0 commit comments