Skip to content

Commit 9fbc0b3

Browse files
committed
wip
1 parent 03dbc3d commit 9fbc0b3

File tree

4 files changed

+68
-64
lines changed

4 files changed

+68
-64
lines changed

sdks/rust/golem-rust-macro/src/agentic/config_schema_derivation.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,13 @@ pub fn derive_config_schema(input: TokenStream, golem_rust_crate_ident: &Ident)
5959
}
6060
);
6161

62-
load_entries.push(
63-
quote! {
64-
#field_ident: {
65-
let mut field_path = path.to_vec();
66-
field_path.push(#field_name.to_string());
67-
<#field_ty as #golem_rust_crate_ident::agentic::ConfigField>::load(&field_path)?
68-
}
62+
load_entries.push(quote! {
63+
#field_ident: {
64+
let mut field_path = path.to_vec();
65+
field_path.push(#field_name.to_string());
66+
<#field_ty as #golem_rust_crate_ident::agentic::ConfigField>::load(&field_path)?
6967
}
70-
);
68+
});
7169
}
7270

7371
let config_schema_impl = quote! {
@@ -94,7 +92,10 @@ pub fn derive_config_schema(input: TokenStream, golem_rust_crate_ident: &Ident)
9492
.into()
9593
}
9694

97-
fn derive_nested_config_field(ast: &DeriveInput, golem_rust_crate_ident: &Ident) -> proc_macro2::TokenStream {
95+
fn derive_nested_config_field(
96+
ast: &DeriveInput,
97+
golem_rust_crate_ident: &Ident,
98+
) -> proc_macro2::TokenStream {
9899
let ident = &ast.ident;
99100
let generics = &ast.generics;
100101
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

sdks/rust/golem-rust-macro/src/agentic/schema_derivation.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::value;
1717
use proc_macro::TokenStream;
1818
use proc_macro2::Ident;
1919
use quote::quote;
20-
use syn::{Data, DeriveInput, parse_macro_input};
20+
use syn::{parse_macro_input, Data, DeriveInput};
2121

2222
pub fn derive_schema(input: TokenStream, golem_rust_crate_ident: &Ident) -> TokenStream {
2323
let ast = parse_macro_input!(input as DeriveInput);
@@ -49,20 +49,22 @@ pub fn derive_schema(input: TokenStream, golem_rust_crate_ident: &Ident) -> Toke
4949
.into()
5050
}
5151

52-
fn derive_component_model_config_leaf(ast: &DeriveInput, golem_rust_crate_ident: &Ident) -> proc_macro2::TokenStream {
52+
fn derive_component_model_config_leaf(
53+
ast: &DeriveInput,
54+
golem_rust_crate_ident: &Ident,
55+
) -> proc_macro2::TokenStream {
5356
let ident = &ast.ident;
5457
let generics = &ast.generics;
5558
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
5659

5760
match &ast.data {
5861
// ConfigFields impls for structs are handled by ConfigSchema derivation
5962
Data::Struct(_) => quote! {},
60-
_ =>
61-
quote! {
62-
impl #impl_generics #golem_rust_crate_ident::agentic::ComponentModelConfigLeaf
63-
for #ident #ty_generics
64-
#where_clause
65-
{ }
66-
}
63+
_ => quote! {
64+
impl #impl_generics #golem_rust_crate_ident::agentic::ComponentModelConfigLeaf
65+
for #ident #ty_generics
66+
#where_clause
67+
{ }
68+
},
6769
}
6870
}

sdks/rust/golem-rust/src/agentic/agent_config.rs

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414

1515
use crate::golem_agentic::golem::agent::common::{ConfigKeyValueType, ConfigValueType};
1616
use crate::golem_agentic::golem::agent::host::get_config_value;
17-
use golem_wasm::{WitType};
18-
use std::marker::PhantomData;
1917
use crate::value_and_type::{FromValueAndType, IntoValue};
2018
use golem_wasm::golem_rpc_0_2_x::types::ValueAndType;
19+
use golem_wasm::WitType;
2120
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque};
2221
use std::hash::Hash;
22+
use std::marker::PhantomData;
23+
use std::net::IpAddr;
24+
use std::num::{
25+
NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8,
26+
};
2327
use std::ops::{Bound, Range};
24-
use std::num::{NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64};
25-
use std::time::Duration;
2628
use std::rc::Rc;
2729
use std::sync::Arc;
28-
use std::net::IpAddr;
30+
use std::time::Duration;
2931

3032
pub trait ConfigSchema: Sized {
3133
fn describe_config() -> Vec<ConfigEntry>;
@@ -50,7 +52,10 @@ impl<T> Secret<T> {
5052
T: FromValueAndType + IntoValue,
5153
{
5254
let value = get_config_value(&self.path);
53-
T::from_value_and_type(ValueAndType { value, typ: T::get_type() })
55+
T::from_value_and_type(ValueAndType {
56+
value,
57+
typ: T::get_type(),
58+
})
5459
}
5560
}
5661

@@ -77,12 +82,10 @@ impl From<ConfigEntry> for ConfigKeyValueType {
7782
}
7883
}
7984

80-
#[diagnostic::on_unimplemented(
81-
message = "\
85+
#[diagnostic::on_unimplemented(message = "\
8286
`ConfigField` is not implemented for `{Self}`. Only types that implement `ConfigField` can be\n\
8387
used as part of an agent's config. If you tried to use a struct as part of the config, make sure\n\
84-
it derives ConfigSchema.",
85-
)]
88+
it derives ConfigSchema.")]
8689
pub trait ConfigField: Sized {
8790
const IS_SHARED: bool;
8891

@@ -97,7 +100,7 @@ impl<T: IntoValue> ConfigField for Secret<T> {
97100
vec![ConfigEntry {
98101
key: path_prefix.to_vec(),
99102
shared: true,
100-
schema: T::get_type()
103+
schema: T::get_type(),
101104
}]
102105
}
103106

@@ -122,12 +125,10 @@ impl<T: ComponentModelConfigLeaf> ConfigField for T {
122125

123126
fn load(path: &[String]) -> Result<Self, String> {
124127
let value = get_config_value(path);
125-
<Self as FromValueAndType>::from_value_and_type(
126-
ValueAndType {
127-
value,
128-
typ: <Self as IntoValue>::get_type(),
129-
}
130-
)
128+
<Self as FromValueAndType>::from_value_and_type(ValueAndType {
129+
value,
130+
typ: <Self as IntoValue>::get_type(),
131+
})
131132
}
132133
}
133134

@@ -228,25 +229,34 @@ macro_rules! impl_component_model_config_leaf_for_tuples {
228229

229230
impl_component_model_config_leaf_for_tuples!(A, B, C, D, E, F, G, H, I, J, K, L);
230231

231-
impl<S: IntoValue + FromValueAndType, E: IntoValue + FromValueAndType> ComponentModelConfigLeaf for Result<S, E> {}
232+
impl<S: IntoValue + FromValueAndType, E: IntoValue + FromValueAndType> ComponentModelConfigLeaf
233+
for Result<S, E>
234+
{
235+
}
232236
impl<E: IntoValue + FromValueAndType> ComponentModelConfigLeaf for Result<(), E> {}
233237
impl<S: IntoValue + FromValueAndType> ComponentModelConfigLeaf for Result<S, ()> {}
234-
impl ComponentModelConfigLeaf for Result<(), ()> {}
238+
impl ComponentModelConfigLeaf for Result<(), ()> {}
235239

236-
impl <T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for Option<T> {}
237-
impl <T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for Bound<T> {}
238-
impl <T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for Range<T> {}
239-
impl <T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for Vec<T> {}
240-
impl <T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for VecDeque<T> {}
241-
impl <T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for LinkedList<T> {}
242-
impl <T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for Box<T> {}
243-
impl <T: FromValueAndType + IntoValue + Clone> ComponentModelConfigLeaf for Rc<T> {}
244-
impl <T: FromValueAndType + IntoValue + Clone> ComponentModelConfigLeaf for Arc<T> {}
240+
impl<T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for Option<T> {}
241+
impl<T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for Bound<T> {}
242+
impl<T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for Range<T> {}
243+
impl<T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for Vec<T> {}
244+
impl<T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for VecDeque<T> {}
245+
impl<T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for LinkedList<T> {}
246+
impl<T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for Box<T> {}
247+
impl<T: FromValueAndType + IntoValue + Clone> ComponentModelConfigLeaf for Rc<T> {}
248+
impl<T: FromValueAndType + IntoValue + Clone> ComponentModelConfigLeaf for Arc<T> {}
245249

246-
impl <T: FromValueAndType + IntoValue + Hash + Eq> ComponentModelConfigLeaf for HashSet<T> {}
247-
impl <T: FromValueAndType + IntoValue + Ord> ComponentModelConfigLeaf for BTreeSet<T> {}
250+
impl<T: FromValueAndType + IntoValue + Hash + Eq> ComponentModelConfigLeaf for HashSet<T> {}
251+
impl<T: FromValueAndType + IntoValue + Ord> ComponentModelConfigLeaf for BTreeSet<T> {}
248252

249-
impl <K: FromValueAndType + IntoValue + Hash + Eq, V: FromValueAndType + IntoValue> ComponentModelConfigLeaf for HashMap<K, V> {}
250-
impl <K: FromValueAndType + IntoValue + Ord, V: FromValueAndType + IntoValue> ComponentModelConfigLeaf for BTreeMap<K, V> {}
253+
impl<K: FromValueAndType + IntoValue + Hash + Eq, V: FromValueAndType + IntoValue>
254+
ComponentModelConfigLeaf for HashMap<K, V>
255+
{
256+
}
257+
impl<K: FromValueAndType + IntoValue + Ord, V: FromValueAndType + IntoValue>
258+
ComponentModelConfigLeaf for BTreeMap<K, V>
259+
{
260+
}
251261

252-
impl <T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for nonempty_collections::NEVec<T> {}
262+
impl<T: FromValueAndType + IntoValue> ComponentModelConfigLeaf for nonempty_collections::NEVec<T> {}

sdks/rust/golem-rust/tests/agent.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ mod tests {
574574
struct ConfigAgentConfigNested {
575575
foo: String,
576576
bar: i32,
577-
nested_secret: Secret<bool>
577+
nested_secret: Secret<bool>,
578578
}
579579

580580
#[derive(ConfigSchema)]
@@ -1329,26 +1329,17 @@ mod tests {
13291329
"WitTypeNode::PrimU32Type".to_string()
13301330
),
13311331
(
1332-
vec![
1333-
"nested".to_string(),
1334-
"foo".to_string(),
1335-
],
1332+
vec!["nested".to_string(), "foo".to_string(),],
13361333
false,
13371334
"WitTypeNode::PrimStringType".to_string()
13381335
),
13391336
(
1340-
vec![
1341-
"nested".to_string(),
1342-
"bar".to_string(),
1343-
],
1337+
vec!["nested".to_string(), "bar".to_string(),],
13441338
false,
13451339
"WitTypeNode::PrimS32Type".to_string()
13461340
),
13471341
(
1348-
vec![
1349-
"nested".to_string(),
1350-
"nested_secret".to_string(),
1351-
],
1342+
vec!["nested".to_string(), "nested_secret".to_string(),],
13521343
true,
13531344
"WitTypeNode::PrimBoolType".to_string()
13541345
),

0 commit comments

Comments
 (0)