synaptic-macros 0.4.0

Procedural macros for the Synaptic framework (#[tool], #[chain], #[entrypoint], etc.)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{
    parse::Parser, parse2, punctuated::Punctuated, Attribute, Expr, FnArg, ItemFn, Lit, Meta, Pat,
    PatType, ReturnType, Token, Type,
};

use crate::paths;

// ---------------------------------------------------------------------------
// Attribute-level config: #[tool(name = "...", description = "...")]
// ---------------------------------------------------------------------------

struct ToolAttr {
    name: Option<String>,
    description: Option<String>,
}

fn parse_tool_attr(attr: TokenStream) -> syn::Result<ToolAttr> {
    let mut name = None;
    let mut description = None;

    if attr.is_empty() {
        return Ok(ToolAttr { name, description });
    }

    let meta_list: Punctuated<Meta, Token![,]> = Punctuated::parse_terminated.parse2(attr)?;

    for meta in meta_list {
        if let Meta::NameValue(nv) = meta {
            let key = nv
                .path
                .get_ident()
                .map(|i| i.to_string())
                .unwrap_or_default();
            if let Expr::Lit(expr_lit) = &nv.value {
                if let Lit::Str(lit_str) = &expr_lit.lit {
                    match key.as_str() {
                        "name" => name = Some(lit_str.value()),
                        "description" => description = Some(lit_str.value()),
                        _ => {
                            return Err(syn::Error::new_spanned(
                                &nv.path,
                                format!("unknown tool attribute: `{}`", key),
                            ));
                        }
                    }
                }
            }
        }
    }

    Ok(ToolAttr { name, description })
}

// ---------------------------------------------------------------------------
// Per-parameter metadata
// ---------------------------------------------------------------------------

enum InjectKind {
    State,
    Store,
    ToolCallId,
}

struct ParamInfo {
    name: String,
    ty: Type,
    is_option: bool,
    default_value: Option<Expr>,
    inject: Option<InjectKind>,
    is_field: bool,
    is_args: bool,
    doc: Option<String>,
}

fn extract_doc_comment(attrs: &[Attribute]) -> Option<String> {
    let mut lines = Vec::new();
    for attr in attrs {
        if attr.path().is_ident("doc") {
            if let Meta::NameValue(nv) = &attr.meta {
                if let Expr::Lit(expr_lit) = &nv.value {
                    if let Lit::Str(s) = &expr_lit.lit {
                        lines.push(s.value().trim().to_string());
                    }
                }
            }
        }
    }
    if lines.is_empty() {
        None
    } else {
        Some(lines.join(" "))
    }
}

fn is_option_type(ty: &Type) -> bool {
    if let Type::Path(tp) = ty {
        if let Some(seg) = tp.path.segments.last() {
            return seg.ident == "Option";
        }
    }
    false
}

fn inner_option_type(ty: &Type) -> Option<&Type> {
    if let Type::Path(tp) = ty {
        if let Some(seg) = tp.path.segments.last() {
            if seg.ident == "Option" {
                if let syn::PathArguments::AngleBracketed(args) = &seg.arguments {
                    if let Some(syn::GenericArgument::Type(inner)) = args.args.first() {
                        return Some(inner);
                    }
                }
            }
        }
    }
    None
}

fn parse_param_info(pat_type: &PatType) -> syn::Result<ParamInfo> {
    let name = if let Pat::Ident(pi) = &*pat_type.pat {
        pi.ident.to_string()
    } else {
        return Err(syn::Error::new_spanned(
            &pat_type.pat,
            "expected a simple identifier",
        ));
    };

    let ty = (*pat_type.ty).clone();
    let is_option = is_option_type(&ty);
    let mut default_value = None;
    let mut inject = None;
    let mut is_field = false;
    let mut is_args = false;
    let doc = extract_doc_comment(&pat_type.attrs);

    for attr in &pat_type.attrs {
        // #[default = expr]
        if attr.path().is_ident("default") {
            if let Meta::NameValue(nv) = &attr.meta {
                default_value = Some(nv.value.clone());
            }
        }
        // #[field]
        if attr.path().is_ident("field") {
            is_field = true;
        }
        // #[args]
        if attr.path().is_ident("args") {
            is_args = true;
        }
        // #[inject(state)] / #[inject(store)] / #[inject(tool_call_id)]
        if attr.path().is_ident("inject") {
            let tokens: TokenStream = attr.parse_args()?;
            let kind_str = tokens.to_string();
            inject = Some(match kind_str.as_str() {
                "state" => InjectKind::State,
                "store" => InjectKind::Store,
                "tool_call_id" => InjectKind::ToolCallId,
                _ => {
                    return Err(syn::Error::new_spanned(
                        attr,
                        "expected inject(state), inject(store), or inject(tool_call_id)",
                    ))
                }
            });
        }
    }

    if is_field && inject.is_some() {
        return Err(syn::Error::new_spanned(
            &pat_type.pat,
            "#[field] and #[inject] cannot be used on the same parameter",
        ));
    }

    if is_args && inject.is_some() {
        return Err(syn::Error::new_spanned(
            &pat_type.pat,
            "#[args] and #[inject] cannot be used on the same parameter",
        ));
    }

    if is_args && is_field {
        return Err(syn::Error::new_spanned(
            &pat_type.pat,
            "#[args] and #[field] cannot be used on the same parameter",
        ));
    }

    Ok(ParamInfo {
        name,
        ty,
        is_option,
        default_value,
        inject,
        is_field,
        is_args,
        doc,
    })
}

// ---------------------------------------------------------------------------
// Type → JSON schema string (compile-time)
// ---------------------------------------------------------------------------

fn type_to_json_schema(ty: &Type) -> TokenStream {
    let inner = if is_option_type(ty) {
        inner_option_type(ty).unwrap_or(ty)
    } else {
        ty
    };

    if let Type::Path(tp) = inner {
        if let Some(seg) = tp.path.segments.last() {
            let ident = seg.ident.to_string();
            match ident.as_str() {
                "String" | "str" => {
                    return quote! { ::serde_json::json!({"type": "string"}) };
                }
                "i8" | "i16" | "i32" | "i64" | "i128" | "isize" | "u8" | "u16" | "u32" | "u64"
                | "u128" | "usize" => {
                    return quote! { ::serde_json::json!({"type": "integer"}) };
                }
                "f32" | "f64" => {
                    return quote! { ::serde_json::json!({"type": "number"}) };
                }
                "bool" => {
                    return quote! { ::serde_json::json!({"type": "boolean"}) };
                }
                "Vec" => {
                    if let syn::PathArguments::AngleBracketed(args) = &seg.arguments {
                        if let Some(syn::GenericArgument::Type(elem_ty)) = args.args.first() {
                            let items = type_to_json_schema(elem_ty);
                            return quote! {
                                ::serde_json::json!({"type": "array", "items": #items})
                            };
                        }
                    }
                    return quote! { ::serde_json::json!({"type": "array"}) };
                }
                "Value" => {
                    return quote! { ::serde_json::json!({"type": "object"}) };
                }
                _ => {}
            }
        }
    }
    // Fallback: unknown type
    unknown_type_schema(inner)
}

#[cfg(feature = "schemars")]
fn unknown_type_schema(inner: &Type) -> TokenStream {
    let core_crate = paths::core_path();
    quote! {
        {
            let __schema = #core_crate::schemars::generate::SchemaGenerator::default()
                .into_root_schema_for::<#inner>();
            let mut __val = ::serde_json::to_value(&__schema)
                .unwrap_or(::serde_json::json!({"type": "object"}));
            if let Some(__obj) = __val.as_object_mut() {
                __obj.remove("$schema");
            }
            __val
        }
    }
}

#[cfg(not(feature = "schemars"))]
fn unknown_type_schema(_inner: &Type) -> TokenStream {
    quote! { ::serde_json::json!({"type": "object"}) }
}

// ---------------------------------------------------------------------------
// Code generation for parameter deserialization
// ---------------------------------------------------------------------------

fn gen_param_deser(param: &ParamInfo) -> TokenStream {
    let name_str = &param.name;
    let ident = format_ident!("{}", &param.name);
    let ty = &param.ty;
    let core_crate = paths::core_path();

    if param.is_option {
        let inner_ty = inner_option_type(ty).unwrap();
        quote! {
            let #ident: #ty = match __args.get(#name_str) {
                Some(::serde_json::Value::Null) | None => None,
                Some(__v) => Some(
                    ::serde_json::from_value::<#inner_ty>(__v.clone())
                        .map_err(|__e| #core_crate::SynapticError::Tool(
                            format!("invalid parameter '{}': {}", #name_str, __e)
                        ))?
                ),
            };
        }
    } else if let Some(ref default_expr) = param.default_value {
        quote! {
            let #ident: #ty = match __args.get(#name_str) {
                Some(::serde_json::Value::Null) | None => #default_expr,
                Some(__v) => ::serde_json::from_value(__v.clone())
                    .map_err(|__e| #core_crate::SynapticError::Tool(
                        format!("invalid parameter '{}': {}", #name_str, __e)
                    ))?,
            };
        }
    } else {
        quote! {
            let #ident: #ty = ::serde_json::from_value(
                __args.get(#name_str)
                    .cloned()
                    .ok_or_else(|| #core_crate::SynapticError::Tool(
                        format!("missing required parameter: {}", #name_str)
                    ))?
            ).map_err(|__e| #core_crate::SynapticError::Tool(
                format!("invalid parameter '{}': {}", #name_str, __e)
            ))?;
        }
    }
}

fn gen_inject_deser(param: &ParamInfo) -> TokenStream {
    let ident = format_ident!("{}", &param.name);
    let ty = &param.ty;
    let core_crate = paths::core_path();

    match param.inject.as_ref().unwrap() {
        InjectKind::State => {
            quote! {
                let #ident: #ty = ::serde_json::from_value(
                    __runtime.state.clone().unwrap_or(::serde_json::Value::Null)
                ).map_err(|__e| #core_crate::SynapticError::Tool(
                    format!("failed to inject state: {}", __e)
                ))?;
            }
        }
        InjectKind::Store => {
            quote! {
                let #ident: #ty = __runtime.store.clone()
                    .ok_or_else(|| #core_crate::SynapticError::Tool(
                        "inject(store): no store in runtime".into()
                    ))?;
            }
        }
        InjectKind::ToolCallId => {
            quote! {
                let #ident: #ty = __runtime.tool_call_id.clone();
            }
        }
    }
}

fn gen_field_deser(param: &ParamInfo) -> TokenStream {
    let ident = format_ident!("{}", &param.name);
    quote! {
        let #ident = self.#ident.clone();
    }
}

fn gen_args_deser(param: &ParamInfo) -> TokenStream {
    let ident = format_ident!("{}", &param.name);
    let ty = &param.ty;
    quote! {
        let #ident: #ty = __args;
    }
}

// ---------------------------------------------------------------------------
// Main expansion
// ---------------------------------------------------------------------------

pub fn expand_tool(attr: TokenStream, item: TokenStream) -> syn::Result<TokenStream> {
    let tool_attr = parse_tool_attr(attr)?;
    let func: ItemFn = parse2(item)?;

    // Extract function name & visibility
    let fn_name = &func.sig.ident;
    let vis = &func.vis;
    let struct_name = format_ident!("{}Tool", to_pascal_case(&fn_name.to_string()));
    let impl_fn_name = format_ident!("{}_impl", fn_name);

    // Extract doc comment from the function for description
    let fn_doc = extract_doc_comment(&func.attrs);
    let tool_name_str = tool_attr.name.unwrap_or_else(|| fn_name.to_string());
    let tool_desc_str = tool_attr.description.or(fn_doc).unwrap_or_default();

    // Parse parameters
    let mut params: Vec<ParamInfo> = Vec::new();
    for arg in &func.sig.inputs {
        if let FnArg::Typed(pat_type) = arg {
            params.push(parse_param_info(pat_type)?);
        }
    }

    let has_inject = params.iter().any(|p| p.inject.is_some());
    let has_field = params.iter().any(|p| p.is_field);
    let args_count = params.iter().filter(|p| p.is_args).count();

    if args_count > 1 {
        return Err(syn::Error::new_spanned(
            &func.sig,
            "at most one parameter can be marked with #[args]",
        ));
    }

    // Build JSON schema properties & required list
    let schema_params: Vec<&ParamInfo> = params
        .iter()
        .filter(|p| p.inject.is_none() && !p.is_field && !p.is_args)
        .collect();

    let mut prop_entries = Vec::new();
    let mut required_entries = Vec::new();

    for p in &schema_params {
        let name_str = &p.name;
        let schema = type_to_json_schema(&p.ty);

        let with_desc = if let Some(ref doc) = p.doc {
            quote! {
                {
                    let mut __s = #schema;
                    if let Some(__obj) = __s.as_object_mut() {
                        __obj.insert("description".into(), ::serde_json::Value::String(#doc.into()));
                    }
                    __s
                }
            }
        } else {
            schema
        };

        let with_default = if let Some(ref def) = p.default_value {
            let def_str = quote!(#def).to_string();
            quote! {
                {
                    let mut __s = #with_desc;
                    if let Some(__obj) = __s.as_object_mut() {
                        // Try to parse as JSON, fallback to string
                        let __default_val: ::serde_json::Value =
                            ::serde_json::from_str(#def_str).unwrap_or(
                                ::serde_json::Value::String(#def_str.into())
                            );
                        __obj.insert("default".into(), __default_val);
                    }
                    __s
                }
            }
        } else {
            with_desc
        };

        prop_entries.push(quote! {
            __props.insert(#name_str.to_string(), #with_default);
        });

        // Required: not Option, not has default
        if !p.is_option && p.default_value.is_none() {
            required_entries.push(quote! { #name_str.to_string() });
        }
    }

    // Build deserialization code
    let deser_stmts: Vec<TokenStream> = params
        .iter()
        .map(|p| {
            if p.is_field {
                gen_field_deser(p)
            } else if p.is_args {
                gen_args_deser(p)
            } else if p.inject.is_some() {
                gen_inject_deser(p)
            } else {
                gen_param_deser(p)
            }
        })
        .collect();

    let param_idents: Vec<_> = params
        .iter()
        .map(|p| format_ident!("{}", &p.name))
        .collect();

    // Extract function body and return type
    let fn_body = &func.block;
    let fn_ret = &func.sig.output;
    let asyncness = &func.sig.asyncness;

    // Strip attributes from parameters for the impl function
    let clean_params: Vec<TokenStream> = params
        .iter()
        .map(|p| {
            let ident = format_ident!("{}", &p.name);
            let ty = &p.ty;
            quote! { #ident: #ty }
        })
        .collect();

    // Determine return type (extract T from Result<T, SynapticError>)
    let _ret_type = match fn_ret {
        ReturnType::Default => quote! { () },
        ReturnType::Type(_, ty) => quote! { #ty },
    };

    // Collect field params for struct generation
    let field_params: Vec<&ParamInfo> = params.iter().filter(|p| p.is_field).collect();

    // Struct definition: empty or with fields
    let struct_def = if has_field {
        let field_defs: Vec<TokenStream> = field_params
            .iter()
            .map(|p| {
                let ident = format_ident!("{}", &p.name);
                let ty = &p.ty;
                quote! { #ident: #ty }
            })
            .collect();
        quote! { #vis struct #struct_name { #(#field_defs),* } }
    } else {
        quote! { #vis struct #struct_name; }
    };

    // Factory function params and construction
    let factory_params: Vec<TokenStream> = field_params
        .iter()
        .map(|p| {
            let ident = format_ident!("{}", &p.name);
            let ty = &p.ty;
            quote! { #ident: #ty }
        })
        .collect();

    let factory_construction = if has_field {
        let field_inits: Vec<TokenStream> = field_params
            .iter()
            .map(|p| {
                let ident = format_ident!("{}", &p.name);
                quote! { #ident }
            })
            .collect();
        quote! { #struct_name { #(#field_inits),* } }
    } else {
        quote! { #struct_name }
    };

    // Generate parameters() body: None if no schema params, Some(...) otherwise
    let parameters_body = if schema_params.is_empty() {
        quote! { None }
    } else {
        quote! {
            let mut __props = ::serde_json::Map::new();
            #(#prop_entries)*
            let __required: Vec<String> = vec![#(#required_entries),*];
            Some(::serde_json::json!({
                "type": "object",
                "properties": ::serde_json::Value::Object(__props),
                "required": __required,
            }))
        }
    };

    let core_crate = paths::core_path();

    if has_inject {
        // Generate RuntimeAwareTool impl
        Ok(quote! {
            #asyncness fn #impl_fn_name(#(#clean_params),*) #fn_ret
                #fn_body

            #struct_def

            #[::async_trait::async_trait]
            impl #core_crate::RuntimeAwareTool for #struct_name {
                fn name(&self) -> &'static str {
                    #tool_name_str
                }

                fn description(&self) -> &'static str {
                    #tool_desc_str
                }

                fn parameters(&self) -> Option<::serde_json::Value> {
                    #parameters_body
                }

                async fn call_with_runtime(
                    &self,
                    __args: ::serde_json::Value,
                    __runtime: #core_crate::ToolRuntime,
                ) -> Result<::serde_json::Value, #core_crate::SynapticError> {
                    #(#deser_stmts)*
                    let __result = #impl_fn_name(#(#param_idents),*).await?;
                    ::serde_json::to_value(__result)
                        .map_err(|__e| #core_crate::SynapticError::Tool(
                            format!("failed to serialize result: {}", __e)
                        ))
                }
            }

            #vis fn #fn_name(#(#factory_params),*) -> ::std::sync::Arc<dyn #core_crate::RuntimeAwareTool> {
                ::std::sync::Arc::new(#factory_construction)
            }
        })
    } else {
        // Generate plain Tool impl
        Ok(quote! {
            #asyncness fn #impl_fn_name(#(#clean_params),*) #fn_ret
                #fn_body

            #struct_def

            #[::async_trait::async_trait]
            impl #core_crate::Tool for #struct_name {
                fn name(&self) -> &'static str {
                    #tool_name_str
                }

                fn description(&self) -> &'static str {
                    #tool_desc_str
                }

                fn parameters(&self) -> Option<::serde_json::Value> {
                    #parameters_body
                }

                async fn call(&self, __args: ::serde_json::Value) -> Result<::serde_json::Value, #core_crate::SynapticError> {
                    #(#deser_stmts)*
                    let __result = #impl_fn_name(#(#param_idents),*).await?;
                    ::serde_json::to_value(__result)
                        .map_err(|__e| #core_crate::SynapticError::Tool(
                            format!("failed to serialize result: {}", __e)
                        ))
                }
            }

            #vis fn #fn_name(#(#factory_params),*) -> ::std::sync::Arc<dyn #core_crate::Tool> {
                ::std::sync::Arc::new(#factory_construction)
            }
        })
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn to_pascal_case(s: &str) -> String {
    s.split('_')
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                None => String::new(),
                Some(c) => {
                    let mut result = c.to_uppercase().to_string();
                    result.extend(chars);
                    result
                }
            }
        })
        .collect()
}