Convert new toolcallcontent

This commit is contained in:
Ben Brandt 2025-07-23 12:31:54 +02:00
parent 4cd5cceae8
commit c3e4f3800d
No known key found for this signature in database
GPG key ID: D4618C5D3B500571
2 changed files with 57 additions and 28 deletions

View file

@ -10,7 +10,7 @@ use buffer_diff::BufferDiff;
use editor::{Bias, MultiBuffer, PathKey}; use editor::{Bias, MultiBuffer, PathKey};
use futures::{FutureExt, channel::oneshot, future::BoxFuture}; use futures::{FutureExt, channel::oneshot, future::BoxFuture};
use gpui::{AppContext, AsyncApp, Context, Entity, EventEmitter, SharedString, Task, WeakEntity}; use gpui::{AppContext, AsyncApp, Context, Entity, EventEmitter, SharedString, Task, WeakEntity};
use itertools::Itertools; use itertools::{Itertools, PeekingNext};
use language::{ use language::{
Anchor, Buffer, BufferSnapshot, Capability, LanguageRegistry, OffsetRangeExt as _, Point, Anchor, Buffer, BufferSnapshot, Capability, LanguageRegistry, OffsetRangeExt as _, Point,
text_diff, text_diff,
@ -19,7 +19,7 @@ use markdown::Markdown;
use project::{AgentLocation, Project}; use project::{AgentLocation, Project};
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::fmt::{Formatter, Write}; use std::fmt::Formatter;
use std::{ use std::{
fmt::Display, fmt::Display,
mem, mem,
@ -36,7 +36,7 @@ pub struct UserMessage {
impl UserMessage { impl UserMessage {
pub fn from_acp( pub fn from_acp(
message: &[acp::ContentBlock], message: impl IntoIterator<Item = acp::ContentBlock>,
language_registry: Arc<LanguageRegistry>, language_registry: Arc<LanguageRegistry>,
cx: &mut App, cx: &mut App,
) -> Self { ) -> Self {
@ -111,7 +111,7 @@ impl AssistantMessageChunk {
pub fn from_str(chunk: &str, language_registry: &Arc<LanguageRegistry>, cx: &mut App) -> Self { pub fn from_str(chunk: &str, language_registry: &Arc<LanguageRegistry>, cx: &mut App) -> Self {
Self::Message { Self::Message {
block: ContentBlock::new( block: ContentBlock::new(
&acp::ContentBlock::Text(acp::TextContent { acp::ContentBlock::Text(acp::TextContent {
text: chunk.to_owned().into(), text: chunk.to_owned().into(),
annotations: None, annotations: None,
}), }),
@ -173,6 +173,7 @@ pub struct ToolCall {
pub id: acp::ToolCallId, pub id: acp::ToolCallId,
pub label: Entity<Markdown>, pub label: Entity<Markdown>,
pub kind: acp::ToolKind, pub kind: acp::ToolKind,
// todo! Should this be a vec?
pub content: Option<ToolCallContent>, pub content: Option<ToolCallContent>,
pub status: ToolCallStatus, pub status: ToolCallStatus,
pub locations: Vec<acp::ToolCallLocation>, pub locations: Vec<acp::ToolCallLocation>,
@ -310,7 +311,7 @@ enum ContentBlock {
impl ContentBlock { impl ContentBlock {
pub fn new( pub fn new(
block: &acp::ContentBlock, block: acp::ContentBlock,
language_registry: &Arc<LanguageRegistry>, language_registry: &Arc<LanguageRegistry>,
cx: &mut App, cx: &mut App,
) -> Self { ) -> Self {
@ -320,7 +321,7 @@ impl ContentBlock {
} }
pub fn new_combined( pub fn new_combined(
blocks: &[acp::ContentBlock], blocks: impl IntoIterator<Item = acp::ContentBlock>,
language_registry: &Arc<LanguageRegistry>, language_registry: &Arc<LanguageRegistry>,
cx: &mut App, cx: &mut App,
) -> Self { ) -> Self {
@ -333,7 +334,7 @@ impl ContentBlock {
pub fn append( pub fn append(
&mut self, &mut self,
block: &acp::ContentBlock, block: acp::ContentBlock,
language_registry: &Arc<LanguageRegistry>, language_registry: &Arc<LanguageRegistry>,
cx: &mut App, cx: &mut App,
) { ) {
@ -391,18 +392,45 @@ impl ToolCallContent {
cx: &mut App, cx: &mut App,
) -> Self { ) -> Self {
match content { match content {
acp_old::ToolCallContent::Markdown { markdown } => Self::Markdown { acp::ToolCallContent::ContentBlock { content } => Self::ContentBlock {
markdown: cx.new(|cx| Markdown::new_text(markdown.into(), cx)), content: ContentBlock::new(content, &language_registry, cx),
}, },
acp_old::ToolCallContent::Diff { diff } => Self::Diff { acp::ToolCallContent::Diff { diff } => Self::Diff {
diff: Diff::from_acp(diff, language_registry, cx), diff: Diff::from_acp(diff, language_registry, cx),
}, },
} }
} }
pub fn from_acp_contents(
content: Vec<acp::ToolCallContent>,
language_registry: Arc<LanguageRegistry>,
cx: &mut App,
) -> Vec<Self> {
content
.into_iter()
.peekable()
.batching(|it| match it.next()? {
acp::ToolCallContent::ContentBlock { content } => {
let mut block = ContentBlock::new(content, &language_registry, cx);
while let Some(acp::ToolCallContent::ContentBlock { content }) =
it.peeking_next(|c| matches!(c, acp::ToolCallContent::ContentBlock { .. }))
{
block.append(content, &language_registry, cx);
}
Some(ToolCallContent::ContentBlock { content: block })
}
content @ acp::ToolCallContent::Diff { .. } => Some(ToolCallContent::from_acp(
content,
language_registry.clone(),
cx,
)),
})
.collect()
}
fn to_markdown(&self, cx: &App) -> String { fn to_markdown(&self, cx: &App) -> String {
match self { match self {
Self::Markdown { markdown } => markdown.read(cx).source().to_string(), Self::ContentBlock { content } => content.to_markdown(cx).to_string(),
Self::Diff { diff } => diff.to_markdown(cx), Self::Diff { diff } => diff.to_markdown(cx),
} }
} }
@ -419,11 +447,11 @@ pub struct Diff {
impl Diff { impl Diff {
pub fn from_acp( pub fn from_acp(
diff: acp_old::Diff, diff: acp::Diff,
language_registry: Arc<LanguageRegistry>, language_registry: Arc<LanguageRegistry>,
cx: &mut App, cx: &mut App,
) -> Self { ) -> Self {
let acp_old::Diff { let acp::Diff {
path, path,
old_text, old_text,
new_text, new_text,
@ -718,10 +746,10 @@ impl AcpThread {
match (chunks.last_mut(), is_thought) { match (chunks.last_mut(), is_thought) {
(Some(AssistantMessageChunk::Message { block }), false) (Some(AssistantMessageChunk::Message { block }), false)
| (Some(AssistantMessageChunk::Thought { block }), true) => { | (Some(AssistantMessageChunk::Thought { block }), true) => {
block.append(&chunk, &language_registry, cx) block.append(chunk, &language_registry, cx)
} }
_ => { _ => {
let block = ContentBlock::new(&chunk, &language_registry, cx); let block = ContentBlock::new(chunk, &language_registry, cx);
if is_thought { if is_thought {
chunks.push(AssistantMessageChunk::Thought { block }) chunks.push(AssistantMessageChunk::Thought { block })
} else { } else {
@ -730,7 +758,7 @@ impl AcpThread {
} }
} }
} else { } else {
let block = ContentBlock::new(&chunk, &language_registry, cx); let block = ContentBlock::new(chunk, &language_registry, cx);
let chunk = if is_thought { let chunk = if is_thought {
AssistantMessageChunk::Thought { block } AssistantMessageChunk::Thought { block }
} else { } else {
@ -764,11 +792,10 @@ impl AcpThread {
) )
}), }),
kind: tool_call.kind, kind: tool_call.kind,
content: tool_call // todo! Do we assume there is either a coalesced content OR diff?
.content content: ToolCallContent::from_acp_contents(tool_call.content, language_registry, cx)
.into_iter() .into_iter()
.map(|content| ToolCallContent::from_acp(content, language_registry, cx)) .next(),
.collect(),
locations: tool_call.locations, locations: tool_call.locations,
status: ToolCallStatus::Allowed { status: ToolCallStatus::Allowed {
status: tool_call.status, status: tool_call.status,
@ -812,11 +839,14 @@ impl AcpThread {
) )
}), }),
kind: tool_call.kind, kind: tool_call.kind,
content: tool_call // todo! Do we assume there is either a coalesced content OR diff?
.content content: ToolCallContent::from_acp_contents(
.into_iter() tool_call.content,
.map(|content| ToolCallContent::from_acp(content, language_registry, cx)) language_registry,
.collect(), cx,
)
.into_iter()
.next(),
locations: tool_call.locations, locations: tool_call.locations,
status: ToolCallStatus::Allowed { status: ToolCallStatus::Allowed {
status: tool_call.status, status: tool_call.status,
@ -1061,7 +1091,8 @@ impl AcpThread {
message: Vec<acp::ContentBlock>, message: Vec<acp::ContentBlock>,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) -> BoxFuture<'static, Result<(), acp_old::Error>> { ) -> BoxFuture<'static, Result<(), acp_old::Error>> {
let block = ContentBlock::new_combined(&message, self.project.read(cx).languages(), cx); let block =
ContentBlock::new_combined(message.clone(), self.project.read(cx).languages(), cx);
self.push_entry( self.push_entry(
AgentThreadEntry::UserMessage(UserMessage { content: block }), AgentThreadEntry::UserMessage(UserMessage { content: block }),
cx, cx,

View file

@ -8,8 +8,6 @@
use anyhow::Result; use anyhow::Result;
use futures::channel::oneshot; use futures::channel::oneshot;
use gpui::{AsyncApp, Task}; use gpui::{AsyncApp, Task};
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value; use serde_json::Value;
use crate::client::Client; use crate::client::Client;