assistant: Add basic tool invocation (#17368)

This PR adds the initial groundwork for invoking tools in response to
tool uses from the model.

Tool uses are run when the model responds with a `stop_reason` of
`tool_use`.

Currently the tool results are just inserted as text into the user
message. We'll want to include these as `tool_result` content on the
message, but Claude seems to understand it regardless.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-09-04 14:32:20 -04:00 committed by GitHub
parent 7fb94c4c4d
commit 01525f17fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 92 additions and 10 deletions

View file

@ -20,6 +20,7 @@ use crate::{
};
use anyhow::{anyhow, Result};
use assistant_slash_command::{SlashCommand, SlashCommandOutputSection};
use assistant_tool::ToolRegistry;
use client::{proto, Client, Status};
use collections::{BTreeSet, HashMap, HashSet};
use editor::{
@ -2091,6 +2092,27 @@ impl ContextEditor {
}
}
}
ContextEvent::UsePendingTools => {
let pending_tool_uses = self
.context
.read(cx)
.pending_tool_uses()
.into_iter()
.filter(|tool_use| tool_use.status.is_idle())
.cloned()
.collect::<Vec<_>>();
for tool_use in pending_tool_uses {
let tool_registry = ToolRegistry::global(cx);
if let Some(tool) = tool_registry.tool(&tool_use.name) {
let task = tool.run(tool_use.input, self.workspace.clone(), cx);
self.context.update(cx, |context, cx| {
context.insert_tool_output(tool_use.id.clone(), task, cx);
});
}
}
}
ContextEvent::Operation(_) => {}
ContextEvent::ShowAssistError(error_message) => {
self.error_message = Some(error_message.clone());