@@ -35,39 +35,13 @@ pub enum Tool {
3535}
3636
3737impl Tool {
38- pub fn from_tool_use ( tool_use : ToolUse ) -> Result < Self , ToolResult > {
39- let map_err = |parse_error| ToolResult {
40- tool_use_id : tool_use. id . clone ( ) ,
41- content : vec ! [ ToolResultContentBlock :: Text ( format!(
42- "failed to deserialize with the following error: {parse_error}"
43- ) ) ] ,
44- status : ToolResultStatus :: Error ,
45- } ;
46-
47- Ok ( match tool_use. name . as_str ( ) {
48- "fs_read" => Self :: FsRead ( serde_json:: from_value :: < FsRead > ( tool_use. args ) . map_err ( map_err) ?) ,
49- "fs_write" => Self :: FsWrite ( serde_json:: from_value :: < FsWrite > ( tool_use. args ) . map_err ( map_err) ?) ,
50- "execute_bash" => Self :: ExecuteBash ( serde_json:: from_value :: < ExecuteBash > ( tool_use. args ) . map_err ( map_err) ?) ,
51- "use_aws" => Self :: UseAws ( serde_json:: from_value :: < UseAws > ( tool_use. args ) . map_err ( map_err) ?) ,
52- unknown => {
53- return Err ( ToolResult {
54- tool_use_id : tool_use. id ,
55- content : vec ! [ ToolResultContentBlock :: Text ( format!(
56- "The tool, \" {unknown}\" is not supported by the client"
57- ) ) ] ,
58- status : ToolResultStatus :: Error ,
59- } ) ;
60- } ,
61- } )
62- }
63-
6438 /// The display name of a tool
65- pub fn display_name ( & self ) -> String {
39+ pub fn display_name ( & self ) -> & ' static str {
6640 match self {
67- Tool :: FsRead ( _) => FsRead :: display_name ( ) ,
68- Tool :: FsWrite ( _) => FsWrite :: display_name ( ) ,
69- Tool :: ExecuteBash ( _) => ExecuteBash :: display_name ( ) ,
70- Tool :: UseAws ( _) => UseAws :: display_name ( ) ,
41+ Tool :: FsRead ( _) => "Read from filesystem" ,
42+ Tool :: FsWrite ( _) => "Write to filesystem" ,
43+ Tool :: ExecuteBash ( _) => "Execute shell command" ,
44+ Tool :: UseAws ( _) => "Read AWS resources" ,
7145 }
7246 }
7347
@@ -82,11 +56,11 @@ impl Tool {
8256 }
8357
8458 /// Queues up a tool's intention in a human readable format
85- pub fn show_readable_intention ( & self , updates : & mut impl Write ) -> Result < ( ) > {
59+ pub fn queue_description ( & self , updates : & mut impl Write ) -> Result < ( ) > {
8660 match self {
87- Tool :: FsRead ( fs_read) => fs_read. show_readable_intention ( updates) ,
88- Tool :: FsWrite ( fs_write) => fs_write. show_readable_intention ( updates) ,
89- Tool :: ExecuteBash ( execute_bash) => execute_bash. show_readable_intention ( updates) ,
61+ Tool :: FsRead ( fs_read) => fs_read. queue_description ( updates) ,
62+ Tool :: FsWrite ( fs_write) => fs_write. queue_description ( updates) ,
63+ Tool :: ExecuteBash ( execute_bash) => execute_bash. queue_description ( updates) ,
9064 Tool :: UseAws ( use_aws) => use_aws. show_readable_intention ( updates) ,
9165 }
9266 }
@@ -102,6 +76,36 @@ impl Tool {
10276 }
10377}
10478
79+ impl TryFrom < ToolUse > for Tool {
80+ type Error = ToolResult ;
81+
82+ fn try_from ( value : ToolUse ) -> std:: result:: Result < Self , Self :: Error > {
83+ let map_err = |parse_error| ToolResult {
84+ tool_use_id : value. id . clone ( ) ,
85+ content : vec ! [ ToolResultContentBlock :: Text ( format!(
86+ "failed to deserialize with the following error: {parse_error}"
87+ ) ) ] ,
88+ status : ToolResultStatus :: Error ,
89+ } ;
90+
91+ Ok ( match value. name . as_str ( ) {
92+ "fs_read" => Self :: FsRead ( serde_json:: from_value :: < FsRead > ( value. args ) . map_err ( map_err) ?) ,
93+ "fs_write" => Self :: FsWrite ( serde_json:: from_value :: < FsWrite > ( value. args ) . map_err ( map_err) ?) ,
94+ "execute_bash" => Self :: ExecuteBash ( serde_json:: from_value :: < ExecuteBash > ( value. args ) . map_err ( map_err) ?) ,
95+ "use_aws" => Self :: UseAws ( serde_json:: from_value :: < UseAws > ( value. args ) . map_err ( map_err) ?) ,
96+ unknown => {
97+ return Err ( ToolResult {
98+ tool_use_id : value. id ,
99+ content : vec ! [ ToolResultContentBlock :: Text ( format!(
100+ "The tool, \" {unknown}\" is not supported by the client"
101+ ) ) ] ,
102+ status : ToolResultStatus :: Error ,
103+ } ) ;
104+ } ,
105+ } )
106+ }
107+ }
108+
105109/// A tool specification to be sent to the model as part of a conversation. Maps to
106110/// [BedrockToolSpecification].
107111#[ derive( Debug , Clone , Deserialize ) ]
0 commit comments