golem_rust_macro/
lib.rs

1// Copyright 2024-2025 Golem Cloud
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use proc_macro::TokenStream;
16use proc_macro2::Span;
17use proc_macro_crate::{crate_name, FoundCrate};
18use syn::DeriveInput;
19
20use crate::transaction::golem_operation_impl;
21
22mod agentic;
23pub(crate) mod recursion;
24mod transaction;
25mod value;
26
27#[proc_macro_derive(IntoValue, attributes(flatten_value, unit_case))]
28pub fn derive_into_value(input: TokenStream) -> TokenStream {
29    let ast: DeriveInput = syn::parse(input).expect("derive input");
30    let golem_rust_crate_ident = get_golem_rust_crate_ident();
31
32    value::derive_into_value(&ast, &golem_rust_crate_ident)
33}
34
35#[proc_macro_derive(FromValueAndType, attributes(flatten_value, unit_case))]
36pub fn derive_from_value_and_type(input: TokenStream) -> TokenStream {
37    let ast: DeriveInput = syn::parse(input).expect("derive input");
38    let golem_rust_crate_ident = get_golem_rust_crate_ident();
39
40    value::derive_from_value_and_type(&ast, &golem_rust_crate_ident)
41}
42
43#[proc_macro_derive(MultimodalSchema)]
44pub fn derive_multimodal(input: TokenStream) -> TokenStream {
45    agentic::derive_multimodal(input)
46}
47
48#[proc_macro_derive(Schema)]
49pub fn derive_schema(input: TokenStream) -> TokenStream {
50    let golem_rust_crate_ident = get_golem_rust_crate_ident();
51
52    agentic::derive_schema(input, &golem_rust_crate_ident)
53}
54
55#[proc_macro_derive(AllowedLanguages, attributes(code))]
56pub fn derive_allowed_languages(input: TokenStream) -> TokenStream {
57    agentic::derive_allowed_languages(input)
58}
59
60#[proc_macro_derive(AllowedMimeTypes, attributes(mime_type))]
61pub fn derive_allowed_mimetypes(input: TokenStream) -> TokenStream {
62    agentic::derive_allowed_mime_types(input)
63}
64
65#[proc_macro_attribute]
66pub fn description(_attr: TokenStream, item: TokenStream) -> TokenStream {
67    item
68}
69
70#[proc_macro_attribute]
71pub fn prompt(_attr: TokenStream, item: TokenStream) -> TokenStream {
72    item
73}
74
75/// Defines a function as an `Operation` that can be used in transactions
76#[proc_macro_attribute]
77pub fn golem_operation(attr: TokenStream, item: TokenStream) -> TokenStream {
78    golem_operation_impl(attr, item)
79}
80
81#[proc_macro_attribute]
82pub fn agent_definition(attr: TokenStream, item: TokenStream) -> TokenStream {
83    agentic::agent_definition_impl(attr, item)
84}
85
86#[proc_macro_attribute]
87pub fn agent_implementation(attr: TokenStream, item: TokenStream) -> TokenStream {
88    agentic::agent_implementation_impl(attr, item)
89}
90
91// get the identifier of golem_rust crate to use for referencing the `golem-rust` crate
92// within the macros. This handles the case where the crate is renamed in Cargo.toml
93// or when the macro is used within the `golem-rust` crate itself.
94fn get_golem_rust_crate_ident() -> syn::Ident {
95    match crate_name("golem-rust") {
96        Ok(FoundCrate::Itself) => syn::Ident::new("crate", Span::call_site()),
97        Ok(FoundCrate::Name(name)) => syn::Ident::new(&name, Span::call_site()),
98        Err(_) => syn::Ident::new("golem_rust", Span::call_site()),
99    }
100}