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;
23mod transaction;
24mod value;
25
26#[proc_macro_derive(IntoValue, attributes(flatten_value, unit_case))]
27pub fn derive_into_value(input: TokenStream) -> TokenStream {
28    let ast: DeriveInput = syn::parse(input).expect("derive input");
29    let golem_rust_crate_ident = get_golem_rust_crate_ident();
30
31    value::derive_into_value(&ast, &golem_rust_crate_ident)
32}
33
34#[proc_macro_derive(FromValueAndType, attributes(flatten_value, unit_case))]
35pub fn derive_from_value_and_type(input: TokenStream) -> TokenStream {
36    let ast: DeriveInput = syn::parse(input).expect("derive input");
37    let golem_rust_crate_ident = get_golem_rust_crate_ident();
38
39    value::derive_from_value_and_type(&ast, &golem_rust_crate_ident)
40}
41
42#[proc_macro_derive(MultimodalSchema)]
43pub fn derive_multimodal(input: TokenStream) -> TokenStream {
44    agentic::derive_multimodal(input)
45}
46
47#[proc_macro_derive(Schema)]
48pub fn derive_schema(input: TokenStream) -> TokenStream {
49    let golem_rust_crate_ident = get_golem_rust_crate_ident();
50
51    agentic::derive_schema(input, &golem_rust_crate_ident)
52}
53
54#[proc_macro_derive(AllowedLanguages, attributes(code))]
55pub fn derive_allowed_languages(input: TokenStream) -> TokenStream {
56    agentic::derive_allowed_languages(input)
57}
58
59#[proc_macro_derive(AllowedMimeTypes, attributes(mime_type))]
60pub fn derive_allowed_mimetypes(input: TokenStream) -> TokenStream {
61    agentic::derive_allowed_mime_types(input)
62}
63
64#[proc_macro_attribute]
65pub fn description(_attr: TokenStream, item: TokenStream) -> TokenStream {
66    item
67}
68
69#[proc_macro_attribute]
70pub fn prompt(_attr: TokenStream, item: TokenStream) -> TokenStream {
71    item
72}
73
74/// Defines a function as an `Operation` that can be used in transactions
75#[proc_macro_attribute]
76pub fn golem_operation(attr: TokenStream, item: TokenStream) -> TokenStream {
77    golem_operation_impl(attr, item)
78}
79
80#[proc_macro_attribute]
81pub fn agent_definition(attr: TokenStream, item: TokenStream) -> TokenStream {
82    agentic::agent_definition_impl(attr, item)
83}
84
85#[proc_macro_attribute]
86pub fn agent_implementation(attr: TokenStream, item: TokenStream) -> TokenStream {
87    agentic::agent_implementation_impl(attr, item)
88}
89
90// get the identifier of golem_rust crate to use for referencing the `golem-rust` crate
91// within the macros. This handles the case where the crate is renamed in Cargo.toml
92// or when the macro is used within the `golem-rust` crate itself.
93fn get_golem_rust_crate_ident() -> syn::Ident {
94    match crate_name("golem-rust") {
95        Ok(FoundCrate::Itself) => syn::Ident::new("crate", Span::call_site()),
96        Ok(FoundCrate::Name(name)) => syn::Ident::new(&name, Span::call_site()),
97        Err(_) => syn::Ident::new("golem_rust", Span::call_site()),
98    }
99}