55
66import path from 'path'
77import fs from '../../shared/fs/fs'
8- import { Writable } from 'stream'
9- import { FsRead , FsReadParams } from './fsRead'
10- import { FsWrite , FsWriteParams } from './fsWrite'
11- import { ExecuteBash , ExecuteBashParams } from './executeBash'
12- import { ToolResult , ToolResultContentBlock , ToolResultStatus , ToolUse } from '@amzn/codewhisperer-streaming'
138
149export const maxToolResponseSize = 30720 // 30KB
1510
@@ -25,122 +20,6 @@ export interface InvokeOutput {
2520 }
2621}
2722
28- export enum ToolType {
29- FsRead = 'fsRead' ,
30- FsWrite = 'fsWrite' ,
31- ExecuteBash = 'executeBash' ,
32- }
33-
34- export type Tool =
35- | { type : ToolType . FsRead ; tool : FsRead }
36- | { type : ToolType . FsWrite ; tool : FsWrite }
37- | { type : ToolType . ExecuteBash ; tool : ExecuteBash }
38-
39- export class ToolUtils {
40- static displayName ( tool : Tool ) : string {
41- switch ( tool . type ) {
42- case ToolType . FsRead :
43- return 'Read from filesystem'
44- case ToolType . FsWrite :
45- return 'Write to filesystem'
46- case ToolType . ExecuteBash :
47- return 'Execute shell command'
48- }
49- }
50-
51- static requiresAcceptance ( tool : Tool ) {
52- switch ( tool . type ) {
53- case ToolType . FsRead :
54- return false
55- case ToolType . FsWrite :
56- return true
57- case ToolType . ExecuteBash :
58- return tool . tool . requiresAcceptance ( )
59- }
60- }
61-
62- static async invoke ( tool : Tool , updates : Writable ) : Promise < InvokeOutput > {
63- switch ( tool . type ) {
64- case ToolType . FsRead :
65- return tool . tool . invoke ( updates )
66- case ToolType . FsWrite :
67- return tool . tool . invoke ( updates )
68- case ToolType . ExecuteBash :
69- return tool . tool . invoke ( updates )
70- }
71- }
72-
73- static queueDescription ( tool : Tool , updates : Writable ) : void {
74- switch ( tool . type ) {
75- case ToolType . FsRead :
76- tool . tool . queueDescription ( updates )
77- break
78- case ToolType . FsWrite :
79- tool . tool . queueDescription ( updates )
80- break
81- case ToolType . ExecuteBash :
82- tool . tool . queueDescription ( updates )
83- break
84- }
85- }
86-
87- static async validate ( tool : Tool ) : Promise < void > {
88- switch ( tool . type ) {
89- case ToolType . FsRead :
90- return tool . tool . validate ( )
91- case ToolType . FsWrite :
92- return tool . tool . validate ( )
93- case ToolType . ExecuteBash :
94- return tool . tool . validate ( )
95- }
96- }
97-
98- static tryFromToolUse ( value : ToolUse ) : Tool | ToolResult {
99- const mapErr = ( parseError : any ) : ToolResult => ( {
100- toolUseId : value . toolUseId ,
101- content : [
102- {
103- type : 'text' ,
104- text : `Failed to validate tool parameters: ${ parseError } . The model has either suggested tool parameters which are incompatible with the existing tools, or has suggested one or more tool that does not exist in the list of known tools.` ,
105- } as ToolResultContentBlock ,
106- ] ,
107- status : ToolResultStatus . ERROR ,
108- } )
109-
110- try {
111- switch ( value . name ) {
112- case ToolType . FsRead :
113- return {
114- type : ToolType . FsRead ,
115- tool : new FsRead ( value . input as unknown as FsReadParams ) ,
116- }
117- case ToolType . FsWrite :
118- return {
119- type : ToolType . FsWrite ,
120- tool : new FsWrite ( value . input as unknown as FsWriteParams ) ,
121- }
122- case ToolType . ExecuteBash :
123- return {
124- type : ToolType . ExecuteBash ,
125- tool : new ExecuteBash ( value . input as unknown as ExecuteBashParams ) ,
126- }
127- default :
128- return {
129- toolUseId : value . toolUseId ,
130- content : [
131- {
132- type : 'text' ,
133- text : `The tool, "${ value . name } " is not supported by the client` ,
134- } as ToolResultContentBlock ,
135- ] ,
136- }
137- }
138- } catch ( error ) {
139- return mapErr ( error )
140- }
141- }
142- }
143-
14423export function sanitizePath ( inputPath : string ) : string {
14524 let sanitized = inputPath . trim ( )
14625
0 commit comments