Skip to content

Commit

Permalink
Add concrete From into boxed tool to macros
Browse files Browse the repository at this point in the history
  • Loading branch information
timonv committed Dec 2, 2024
1 parent 8d2fbf6 commit f962557
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 19 deletions.
4 changes: 0 additions & 4 deletions swiftide-agents/src/default_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,5 @@ mod tests {
assert_eq!(messages.len(), 4);

assert!(context.next_completion().await.is_none());

// If the last message is from the assistant, we should not get any more completions
context.add_messages(&[assistant!("I am fine")]).await;
assert!(context.next_completion().await.is_none());
}
}
18 changes: 12 additions & 6 deletions swiftide-macros/src/tool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use darling::{ast::NestedMeta, Error, FromDeriveInput, FromMeta};
use proc_macro2::TokenStream;
use quote::quote;
use serde::ser::SerializeMap as _;
use syn::{spanned::Spanned, DeriveInput, FnArg, ItemFn, Pat, PatType};
use syn::{spanned::Spanned, DeriveInput, FnArg, ItemFn, Lifetime, Pat, PatType};

mod args;
mod tool_spec;
Expand Down Expand Up @@ -141,7 +141,7 @@ pub(crate) fn tool_impl(input_args: &TokenStream, input: &ItemFn) -> TokenStream
}
};

let boxed_from = boxed_from(tool_struct.clone());
let boxed_from = boxed_from(&tool_struct, &[]);

quote! {
#tool_args
Expand Down Expand Up @@ -243,7 +243,8 @@ pub(crate) fn tool_derive_impl(input: &DeriveInput) -> syn::Result<TokenStream>
quote! { <#(#struct_lifetimes),*> }
};

let boxed_from = boxed_from(struct_ident.clone());
// Arg should be, if empty None, else Some(&args)
let boxed_from = boxed_from(struct_ident, &struct_lifetimes);
Ok(quote! {
#tool_args

Expand Down Expand Up @@ -273,10 +274,15 @@ fn parse_args(args: TokenStream) -> Result<ToolArgs, Error> {
ToolArgs::from_list(&attr_args)
}

fn boxed_from(val: syn::Ident) -> TokenStream {
fn boxed_from(struct_ident: &syn::Ident, lifetimes: &[&Lifetime]) -> TokenStream {
if !lifetimes.is_empty() {
// TODO: Implement for existing lifetimes
return quote! {};
}

quote! {
impl From<#val> for Box<dyn ::swiftide::chat_completion::Tool> {
fn from(val: #val) -> Self {
impl<'TOOLBOXED> From<#struct_ident> for Box<dyn ::swiftide::chat_completion::Tool + 'TOOLBOXED> {
fn from(val: #struct_ident) -> Self {
Box::new(val)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ impl swiftide::chat_completion::Tool for HelloDerive {
.unwrap()
}
}
impl From<HelloDerive> for Box<dyn ::swiftide::chat_completion::Tool> {
impl<'TOOLBOXED> From<HelloDerive>
for Box<dyn ::swiftide::chat_completion::Tool + 'TOOLBOXED> {
fn from(val: HelloDerive) -> Self {
Box::new(val)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ impl swiftide::chat_completion::Tool for HelloDerive {
.unwrap()
}
}
impl From<HelloDerive> for Box<dyn ::swiftide::chat_completion::Tool> {
impl<'TOOLBOXED> From<HelloDerive>
for Box<dyn ::swiftide::chat_completion::Tool + 'TOOLBOXED> {
fn from(val: HelloDerive) -> Self {
Box::new(val)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,3 @@ impl<'a> swiftide::chat_completion::Tool for HelloDerive<'a> {
.unwrap()
}
}
impl From<HelloDerive> for Box<dyn ::swiftide::chat_completion::Tool> {
fn from(val: HelloDerive) -> Self {
Box::new(val)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ impl ::swiftide::chat_completion::Tool for SearchCode {
.unwrap()
}
}
impl From<SearchCode> for Box<dyn ::swiftide::chat_completion::Tool> {
impl<'TOOLBOXED> From<SearchCode>
for Box<dyn ::swiftide::chat_completion::Tool + 'TOOLBOXED> {
fn from(val: SearchCode) -> Self {
Box::new(val)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ impl ::swiftide::chat_completion::Tool for SearchCode {
.unwrap()
}
}
impl From<SearchCode> for Box<dyn ::swiftide::chat_completion::Tool> {
impl<'TOOLBOXED> From<SearchCode>
for Box<dyn ::swiftide::chat_completion::Tool + 'TOOLBOXED> {
fn from(val: SearchCode) -> Self {
Box::new(val)
}
Expand Down

0 comments on commit f962557

Please sign in to comment.