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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
use proc_macro::{TokenStream, TokenTree};
use quote::{format_ident, quote, ToTokens};
use syn::{parse_macro_input, Attribute, Data, DeriveInput, Expr, Ident, Lit, Meta};

/// The `arg_description` attribute is a procedural macro used to provide additional description for an enum.
///
/// This attribute does not modify the code it annotates but instead attaches metadata in the form of a description.
/// This can be helpful for better code readability and understanding the purpose of different enums.
///
/// # Usage
///
/// ```rust
/// #[arg_description(description = "This is a sample enum.", tokens = 5)]
/// #[derive(EnumDescriptor)]
/// pub enum SampleEnum {
///     Variant1,
///     Variant2,
/// }
/// ```
///
/// Note: The actual usage of the description and tokens provided through this attribute happens
/// in the `EnumDescriptor` derive macro and is retrieved in the `enum_descriptor_derive` function.
///
/// The `arg_description` attribute takes one argument, `description`, which is a string literal.
#[proc_macro_attribute]
pub fn arg_description(_args: TokenStream, input: TokenStream) -> TokenStream {
    input
}

/// A derive procedural macro for the `EnumDescriptor` trait.
///
/// The `EnumDescriptor` trait should have a function `name_with_token_count`
/// that returns a tuple with the name of the enum type as a string and the
/// token count for the name as an `usize`.
///
/// This procedural macro generates an implementation of `EnumDescriptor` for
/// the type on which it's applied. The `name_with_token_count` function, in the
/// generated implementation, returns the name of the type and its token count.
///
/// # Usage
///
/// Use the `#[derive(EnumDescriptor)]` attribute on an enum to derive the
/// `EnumDescriptor` trait for it.
///
/// ```
/// #[derive(EnumDescriptor)]
/// enum MyEnum {
///     Variant1,
///     Variant2,
/// }
/// ```
///
/// This will generate:
///
/// ```
/// impl EnumDescriptor for MyEnum {
///     fn name_with_token_count() -> (String, usize) {
///         (String::from("MyEnum"), /* token count of "MyEnum" */)
///     }
/// }
/// ```
///
/// The actual token count is computed during compile time using the
/// `calculate_token_count` function.
#[proc_macro_derive(EnumDescriptor, attributes(arg_description))]
pub fn enum_descriptor_derive(input: TokenStream) -> TokenStream {
    let DeriveInput { ident, attrs, .. } = parse_macro_input!(input as DeriveInput);

    let name_str = format!("{}", ident);
    let name_token_count = calculate_token_count(&name_str);

    let mut description = String::new();
    let mut desc_tokens = 0_usize;

    for attr in &attrs {
        if attr.path().is_ident("arg_description") {
            let _result = attr.parse_nested_meta(|meta| {
                let content = meta.input;

                if !content.is_empty() {
                    if meta.path.is_ident("description") {
                        let value = meta.value()?;
                        if let Ok(Lit::Str(value)) = value.parse() {
                            description = value.value();
                        }
                    } else if meta.path.is_ident("tokens") {
                        let value = meta.value()?;
                        if let Ok(Lit::Int(value)) = value.parse() {
                            desc_tokens = value.base10_parse::<usize>()?;
                            return Ok(());
                        }
                    }
                    return Ok(());
                }

                Err(meta.error("unrecognized my_attribute"))
            });

            if _result.is_err() {
                println!("Error parsing attribute:   {:#?}", _result);
            }
        }
    }

    let expanded = quote! {
        impl EnumDescriptor for #ident {
            fn name_with_token_count() -> (String, usize) {
                (String::from(#name_str), #name_token_count)
            }
            fn arg_description_with_token_count() -> (String, usize) {
                (String::from(#description), #desc_tokens)
            }
        }
    };

    TokenStream::from(expanded)
}

/// A derive procedural macro for the `VariantDescriptors` trait.
///
/// This macro generates an implementation of the `VariantDescriptors` trait for
/// an enum. The trait provides two methods:
///
/// 1. `variant_names_with_token_counts`: Returns a `Vec` containing tuples,
/// each with a string representation of a variant's name and its token count.
///
/// 2. `variant_name_with_token_count`: Takes an enum variant as input and
/// returns a tuple with the variant's name as a string and its token count.
///
/// Note: This macro will panic if it is used on anything other than an enum.
///
/// # Usage
///
/// ```
/// #[derive(VariantDescriptors)]
/// enum MyEnum {
///     Variant1,
///     Variant2,
/// }
/// ```
///
/// This will generate the following:
///
/// ```
/// impl VariantDescriptors for MyEnum {
///     fn variant_names_with_token_counts() -> Vec<(String, usize)> {
///         vec![
///             (String::from("Variant1"), /* token count of "Variant1" */),
///             (String::from("Variant2"), /* token count of "Variant2" */),
///         ]
///     }
///
///     fn variant_name_with_token_count(&self) -> (String, usize) {
///         match self {
///             Self::Variant1 => (String::from("Variant1"), /* token count of "Variant1" */),
///             Self::Variant2 => (String::from("Variant2"), /* token count of "Variant2" */),
///         }
///     }
/// }
/// ```
///
/// The actual token count is computed during compile time using the
/// `calculate_token_count` function.
#[proc_macro_derive(VariantDescriptors)]
pub fn variant_descriptors_derive(input: TokenStream) -> TokenStream {
    let ast = parse_macro_input!(input as DeriveInput);

    let enum_name = &ast.ident;

    let variants = if let syn::Data::Enum(ref e) = ast.data {
        e.variants
            .iter()
            .map(|v| {
                let variant_name = &v.ident;
                let token_count = calculate_token_count(&variant_name.to_string());

                (variant_name, token_count)
            })
            .collect::<Vec<_>>()
    } else {
        panic!("VariantDescriptors can only be used with enums");
    };

    let variant_names_with_token_counts: Vec<_> = variants
        .iter()
        .map(|(variant_name, token_count)| {
            quote! { (stringify!(#variant_name).to_string(), #token_count) }
        })
        .collect();

    let variant_name_with_token_count: Vec<_> = variants
        .iter()
        .map(|(variant_name, token_count)| {
            quote! { Self::#variant_name => (stringify!(#variant_name).to_string(), #token_count) }
        })
        .collect();

    let expanded = quote! {
        impl VariantDescriptors for #enum_name {
            fn variant_names_with_token_counts() -> Vec<(String, usize)> {
                vec![
                    #(#variant_names_with_token_counts),*
                ]
            }

            fn variant_name_with_token_count(&self) -> (String, usize) {
                match self {
                    #(#variant_name_with_token_count,)*
                }
            }
        }
    };

    TokenStream::from(expanded)
}

/// A procedural macro to generate information about an enum.
///
/// This macro generates code that uses the `EnumDescriptor` and `VariantDescriptors`
/// traits to extract information about an enum, including its name, variant names,
/// and their corresponding token counts. Additionally, it uses the `FunctionArgument` trait
/// to fetch the argument description. All this information is serialized into JSON.
///
/// The macro returns a tuple containing the JSON and the total token count.
///
/// # Usage
///
/// The generated code will look like this:
///
/// ```rust
/// {
///     use serde_json::Value;
///     let mut total_tokens = 0;
///
///     let (arg_desc, arg_count) = <MyEnum as ::openai_func_enums::FunctionArgument>::argument_description_with_token_count();
///     total_tokens += arg_count;
///
///     // When this is consumed by the function that creates the overall function,
///     // we are going to be requiring all the arguments, which means we will repeat
///     // their names in the "required" part of openai's function schema. So we will
///     // count the tokens associated with this enum name twice here.
///     let enum_name = <MyEnum as EnumDescriptor>::name_with_token_count();
///     total_tokens += enum_name.1;
///     total_tokens += enum_name.1;
///
///     let enum_variants = <MyEnum as VariantDescriptors>::variant_names_with_token_counts();
///     total_tokens += enum_variants.iter().map(|(_, token_count)| *token_count).sum::<usize>();
///
///     let json_enum = serde_json::json!({
///         enum_name.0: {
///             "type": "string",
///             "enum": enum_variants.iter().map(|(name, _)| name.clone()).collect::<Vec<_>>(),
///             "description": arg_desc,
///         }
///     });
///
///     total_tokens += 11;
///
///     (json_enum, total_tokens)
/// }
/// ```
///
/// Note: It is assumed that the enum implements the `EnumDescriptor` and `VariantDescriptors` traits.
/// The actual token count is computed during compile time using these traits' methods.
#[proc_macro]
pub fn generate_enum_info(input: TokenStream) -> TokenStream {
    let enum_ident = parse_macro_input!(input as Ident);

    let output = quote! {
        {
            let mut total_tokens = 0;

            let (arg_desc, arg_count) = <#enum_ident as EnumDescriptor>::arg_description_with_token_count();
            total_tokens += arg_count;

            let enum_name = <#enum_ident as EnumDescriptor>::name_with_token_count();
            total_tokens += enum_name.1;
            total_tokens += enum_name.1;

            let enum_variants = <#enum_ident as VariantDescriptors>::variant_names_with_token_counts();
            total_tokens += enum_variants.iter().map(|(_, token_count)| *token_count).sum::<usize>();

            let json_enum = serde_json::json!({
                enum_name.0: {
                    "type": "string",
                    "enum": enum_variants.iter().map(|(name, _)| name.clone()).collect::<Vec<_>>(),
                    "description": arg_desc,
                }
            });

            total_tokens += 11;

            (json_enum, total_tokens)
        }
    };

    output.into()
}

#[proc_macro]
pub fn generate_value_arg_info(input: TokenStream) -> TokenStream {
    let mut type_and_name_values = Vec::new();

    let tokens = input.into_iter().collect::<Vec<TokenTree>>();
    for token in tokens {
        if let TokenTree::Ident(ident) = &token {
            type_and_name_values.push(ident.to_string());
        }
    }

    if type_and_name_values.len() == 2 {
        let name = type_and_name_values[1].clone();
        let name_tokens = calculate_token_count(name.as_str());
        let type_name = type_and_name_values[0].clone();
        let type_name_tokens = calculate_token_count(type_name.as_str());
        let output = quote! {
            {
                let mut total_tokens = 0;
                total_tokens += #name_tokens;
                total_tokens += #type_name_tokens;
                if #type_name == "array" {
                    let json_enum = serde_json::json!({
                        #name: {
                            "type": #type_name,
                            "items": {
                                "type": "string",
                            },
                        }
                    });

                    total_tokens += 22;

                    (json_enum, total_tokens)

                } else {
                    let json_enum = serde_json::json!({
                        #name: {
                            "type": #type_name,
                        }
                    });

                    total_tokens += 11;

                    (json_enum, total_tokens)

                }
            }
        };
        return output.into();
    }

    let gen = quote! {};
    gen.into()
}

/// This procedural macro attribute is used to specify a description for an enum variant.
///
/// The `func_description` attribute does not modify the input it is given.
/// It's only used to attach metadata (i.e., a description) to enum variants.
///
/// # Usage
///
/// ```rust
/// enum MyEnum {
///     #[func_description(description="This function does a thing.")]
///     DoAThing,
///     #[func_description(description="This function does another thing.")]
///     DoAnotherThing,
/// }
/// ```
///
/// Note: The actual usage of the description provided through this attribute happens
/// in the `FunctionCallResponse` derive macro and is retrieved in the `impl_function_call_response` function.
#[proc_macro_attribute]
pub fn func_description(_args: TokenStream, input: TokenStream) -> TokenStream {
    input
}

/// This procedural macro derives the `FunctionCallResponse` trait for an enum.
///
/// The derive macro expects an enum and it generates a new struct for each variant of the enum.
/// The generated struct is named by appending "Response" to the variant's name. Each struct has the same fields as the variant.
/// Also, a `name`, `to_function_call` and `get_function_json` method is implemented for each struct.
///
/// In the `get_function_json` method, any description provided through the `func_description` attribute is used.
///
/// # Usage
///
/// ```rust
/// #[derive(FunctionCallResponse)]
/// #[func_description]
/// enum MyEnum {
///     Variant1,
///     Variant2,
/// }
/// ```
///
/// Note: This macro can only be applied to enums and it requires the `func_description` attribute to be applied to the enum.
#[proc_macro_derive(FunctionCallResponse, attributes(func_description))]
pub fn derive_function_call_response(input: TokenStream) -> TokenStream {
    let ast: DeriveInput = syn::parse(input).unwrap();

    let gen = impl_function_call_response(&ast);

    gen.into()
}

/// This function generates a `FunctionCallResponse` implementation for each variant of an enum.
///
/// For each enum variant, it creates a new struct with the same fields as the variant and also
/// generates `name`, `to_function_call`, and `get_function_json` methods for the struct.
///
/// In the `get_function_json` method, it utilizes the description provided through the `func_description` attribute.
///
/// This function is used by the `FunctionCallResponse` derive macro.
fn impl_function_call_response(ast: &DeriveInput) -> proc_macro2::TokenStream {
    match &ast.data {
        Data::Enum(enum_data) => {
            let mut generated_structs = Vec::new();
            let mut json_generator_functions = Vec::new();

            for variant in &enum_data.variants {
                let variant_name = &variant.ident;
                let struct_name = format_ident!("{}Response", variant_name);

                let mut description = String::new();
                let mut desc_tokens = 0_usize;

                for attr in &variant.attrs {
                    if attr.path().is_ident("func_description") {
                        let attribute_parsed = attr.parse_nested_meta(|meta| {
                            let content = meta.input;

                            if !content.is_empty() {
                                if meta.path.is_ident("description") {
                                    let value = meta.value()?;
                                    if let Ok(Lit::Str(value)) = value.parse() {
                                        description = value.value();
                                        desc_tokens = calculate_token_count(description.as_str());
                                    }
                                }

                                return Ok(());
                            }
                            Err(meta.error("unrecognized attribute"))
                        });
                        match attribute_parsed {
                            Ok(_attribute_parsed) => {}
                            Err(e) => {
                                println!("Error parsing attribute:   {:#?}", e);
                            }
                        }
                    }
                }

                let fields: Vec<_> = variant
                    .fields
                    .iter()
                    .map(|f| {
                        let field_name =
                            format_ident!("{}", to_snake_case(&f.ty.to_token_stream().to_string()));
                        let field_type = &f.ty;
                        quote! {
                            pub #field_name: #field_type,
                        }
                    })
                    .collect();

                let field_info: Vec<_> = variant
                    .fields
                    .iter()
                    .map(|f| {
                        let field_type = &f.ty;
                        quote! {
                            generate_enum_info!(#field_type)
                        }
                    })
                    .collect();

                json_generator_functions.push(quote! {
                    impl #struct_name {
                        pub fn name() -> String {
                            stringify!(#struct_name).to_string()
                        }

                        pub fn to_function_call() -> ChatCompletionFunctionCall {
                            ChatCompletionFunctionCall::Function {
                                name: stringify!(#struct_name).to_string(),
                            }
                        }

                        pub fn to_tool_choice() -> ChatCompletionToolChoiceOption {
                            ChatCompletionToolChoiceOption::Named(ChatCompletionNamedToolChoice {
                                r#type: ChatCompletionToolType::Function,
                                function: FunctionName { name: stringify!(#struct_name).to_string() }
                            })
                        }

                        pub fn get_function_json() -> (serde_json::Value, usize) {
                            let mut parameters = serde_json::Map::new();
                            let mut total_tokens = 0;
                            for (arg_json, arg_tokens) in vec![#(#field_info),*] {
                                total_tokens += arg_tokens;
                                parameters.insert(
                                    arg_json.as_object().unwrap().keys().next().unwrap().clone(),
                                    arg_json
                                        .as_object()
                                        .unwrap()
                                        .values()
                                        .next()
                                        .unwrap()
                                        .clone(),
                                );
                            }

                            let function_json = json!({
                                "name": stringify!(#struct_name),
                                "description": #description,
                                "parameters": {
                                    "type": "object",
                                    "properties": parameters,
                                    "required": parameters.keys().collect::<Vec<_>>()
                                }
                            });

                            total_tokens += 12;
                            total_tokens += #desc_tokens;

                            (function_json, total_tokens)
                        }
                    }
                });

                generated_structs.push(quote! {
                    #[derive(serde::Deserialize, Debug)]
                    #[serde(rename_all = "PascalCase")]
                    pub struct #struct_name {
                        #(#fields)*
                    }
                });
            }

            let gen = quote! {
                use async_openai::types::{ChatCompletionFunctionCall, ChatCompletionNamedToolChoice, ChatCompletionToolChoiceOption, FunctionName,};
                use serde_json::{json, Value};
                use openai_func_enums::generate_enum_info;

                #(#generated_structs)*

                #(#json_generator_functions)*

            };

            gen
        }
        _ => panic!("FunctionCallResponse can only be derived for enums"),
    }
}

/// The `SubcommandGPT` procedural macro is used to derive a structure
/// which encapsulates various chat completion commands.
///
/// This macro should be applied to an enum. It generates various supporting
/// structures and methods, including structures representing the command arguments,
/// methods for converting between the argument structures and the original enum,
/// JSON conversion methods, and an implementation of the original enum that provides
/// methods for executing the commands and dealing with the responses.
///
/// Each variant of the original enum will be converted into a corresponding structure,
/// and each field in the variant will become a field in the generated structure.
/// The generated structures will derive `serde::Deserialize` and `Debug` automatically.
///
/// This macro also generates methods for calculating the token count of a string and
/// for executing commands based on function calls received from the chat API.
///
/// The types of fields in the enum variants determine how the corresponding fields in the
/// generated structures are treated. For example, fields of type `String` or `&str` are
/// converted to JSON value arguments with type `"string"`, while fields of type `u8`, `u16`,
/// `u32`, `u64`, `usize`, `i8`, `i16`, `i32`, `i64`, `isize`, `f32` or `f64` are converted
/// to JSON value arguments with type `"integer"` or `"number"` respectively.
/// For fields with a tuple type, currently this macro simply prints that the field is of a tuple type.
/// For fields with an array type, they are converted to JSON value arguments with type `"array"`.
///
/// When running the chat command, a custom system message can be optionally provided.
/// If provided, this message will be used as the system message in the chat request.
/// If not provided, a default system message will be used.
///
/// If the total token count of the request exceeds a specified limit, an error will be returned.
///
/// The `derive_subcommand_gpt` function consumes a `TokenStream` representing the enum
/// to which the macro is applied and produces a `TokenStream` representing the generated code.
///
/// # Panics
/// This macro will panic (only at compile time) if it is applied to a non-enum item.
#[proc_macro_derive(SubcommandGPT)]
pub fn derive_subcommand_gpt(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let name = input.ident;

    let data = match input.data {
        Data::Enum(data) => data,
        _ => panic!("SubcommandGPT can only be implemented for enums"),
    };

    let mut generated_structs = Vec::new();
    let mut json_generator_functions = Vec::new();
    let mut generated_clap_gpt_enum = Vec::new();
    let mut generated_struct_names = Vec::new();

    for variant in data.variants.iter() {
        let variant_name = &variant.ident;
        let struct_name = format_ident!("{}", variant_name);
        generated_struct_names.push(struct_name.clone());
        let mut variant_desc = String::new();
        let mut variant_desc_tokens = 0_usize;

        // let description = get_comment_from_attr(&variant_attrs);
        for variant_attrs in &variant.attrs {
            let description = get_comment_from_attr(variant_attrs);
            if let Some(description) = description {
                variant_desc = description;
                variant_desc_tokens = calculate_token_count(variant_desc.as_str());
            }
        }

        let fields: Vec<_> = variant
            .fields
            .iter()
            .map(|f| {
                // If the field has an identifier (i.e., it is a named field),
                // use it. Otherwise, use the type as the name.
                let field_name = if let Some(ident) = &f.ident {
                    format_ident!("{}", ident)
                } else {
                    format_ident!("{}", to_snake_case(&f.ty.to_token_stream().to_string()))
                };
                let field_type = &f.ty;
                quote! {
                    pub #field_name: #field_type,
                }
            })
            .collect();

        let execute_command_parameters: Vec<_> = variant
            .fields
            .iter()
            .map(|field| {
                let field_name = &field.ident;
                quote! { #field_name: self.#field_name.clone() }
            })
            .collect();

        let number_type = "number";
        let number_ident = format_ident!("{}", number_type);
        let integer_type = "integer";
        let integer_ident = format_ident!("{}", integer_type);
        let string_type = "string";
        let string_ident = format_ident!("{}", string_type);
        let array_type = "array";
        let array_ident = format_ident!("{}", array_type);

        let field_info: Vec<_> = variant
            .fields
            .iter()
            .map(|f| {
                let field_name = if let Some(ident) = &f.ident {
                    format_ident!("{}", ident)
                } else {
                    format_ident!("{}", to_snake_case(&f.ty.to_token_stream().to_string()))
                };
                let field_type = &f.ty;

                match field_type {
                    syn::Type::Path(typepath) if typepath.qself.is_none() => {
                        let type_ident = &typepath.path.segments.last().unwrap().ident;

                        match type_ident.to_string().as_str() {
                            "f32" | "f64" => {
                                return quote! {
                                    generate_value_arg_info!(#number_ident, #field_name)
                                };
                            }
                            "u8" | "u16" | "u32" | "u64" | "u128" | "usize" | "i8" | "i16"
                            | "i32" | "i64" | "i128" | "isize" => {
                                return quote! {
                                    generate_value_arg_info!(#integer_ident, #field_name)
                                };
                            }
                            "String" | "&str" => {
                                return quote! {
                                    generate_value_arg_info!(#string_ident, #field_name)
                                };
                            }
                            "Vec" => {
                                return quote! {
                                    generate_value_arg_info!(#array_ident, #field_name)
                                };
                            }
                            _ => {
                                return quote! {
                                    generate_enum_info!(#field_type)
                                };
                            }
                        }
                    }
                    syn::Type::Tuple(_) => {
                        println!("Field {} is of tuple type", field_name);
                    }
                    syn::Type::Array(_) => {
                        println!("Field {} is of array type", field_name);
                        return quote! {
                            generate_value_arg_info!(#array_ident, #field_name)
                        };
                    }
                    _ => {
                        println!("Field {} is of another type.", field_name);
                    }
                }
                quote! {}
            })
            .collect();

        json_generator_functions.push(quote! {
            impl #struct_name {
                pub fn name() -> String {
                    stringify!(#struct_name).to_string()
                }

                pub fn to_function_call() -> ChatCompletionFunctionCall {
                    ChatCompletionFunctionCall::Function {
                        name: stringify!(#struct_name).to_string(),
                    }
                }

                pub fn to_tool_choice() -> ChatCompletionToolChoiceOption {
                    ChatCompletionToolChoiceOption::Named(ChatCompletionNamedToolChoice {
                        r#type: ChatCompletionToolType::Function,
                        function: FunctionName { name: stringify!(#struct_name).to_string() }
                    })
                }

                pub fn execute_command(&self) -> #name {
                    #name::#variant_name {
                        #(#execute_command_parameters),*
                    }
                }

                pub fn get_function_json() -> (serde_json::Value, usize) {
                    let mut parameters = serde_json::Map::new();
                    let mut total_tokens = 0;

                    for (arg_json, arg_tokens) in vec![#(#field_info),*] {
                        total_tokens += arg_tokens;
                        parameters.insert(
                            arg_json.as_object().unwrap().keys().next().unwrap().clone(),
                            arg_json
                                .as_object()
                                .unwrap()
                                .values()
                                .next()
                                .unwrap()
                                .clone(),
                        );
                    }

                    let function_json = serde_json::json!({
                        "name": stringify!(#struct_name),
                        "description": #variant_desc,
                        "parameters": {
                            "type": "object",
                            "properties": parameters,
                            "required": parameters.keys().collect::<Vec<_>>()
                        }
                    });

                    total_tokens += 12;
                    total_tokens += #variant_desc_tokens;

                    (function_json, total_tokens)
                }
            }
        });

        generated_structs.push(quote! {
            #[derive(Clone, serde::Deserialize, Debug)]
            pub struct #struct_name {
                #(#fields)*
            }
        });
    }

    let all_function_calls = quote! {
        pub fn all_function_jsons() -> (serde_json::Value, usize) {
            let results = vec![#(#generated_struct_names::get_function_json(),)*];
            let combined_json = serde_json::Value::Array(results.iter().map(|(json, _)| json.clone()).collect());
            let total_tokens = results.iter().map(|(_, tokens)| tokens).sum();
            (combined_json, total_tokens)
        }
    };

    generated_clap_gpt_enum.push(quote! {
        #[derive(Subcommand)]
        pub enum CommandsGPT {
            GPT { a: String },
        }
    });

    let struct_names: Vec<String> = generated_struct_names
        .iter()
        .map(|name| format!("{}", name))
        .collect();

    let match_arms: Vec<_> = generated_struct_names
        .iter()
        .map(|struct_name| {
            let response_name = format_ident!("{}", struct_name);

            quote! {
                Ok(FunctionResponse::#response_name(response)) => {
                    let result = response.execute_command();
                    let run_result = result.run(execution_strategy_clone).await;
                    match run_result {
                        Ok(run_result) => {
                            {
                                let prior_result_clone = prior_result.clone();
                                let mut prior_result_lock = prior_result_clone.lock().await;
                                *prior_result_lock = run_result;
                            }
                            return Ok(());
                        }
                        Err(e) => {
                            println!("{:#?}", e);
                        }
                    }
                }
            }
        })
        .collect();

    let match_arms_no_return: Vec<_> = generated_struct_names
        .iter()
        .map(|struct_name| {
            let response_name = format_ident!("{}", struct_name);

            quote! {
                Ok(FunctionResponse::#response_name(response)) => {
                    let result = response.execute_command();
                    let run_result = result.run(execution_strategy_clone).await;
                    match run_result {
                        Ok(run_result) => {
                            {
                                let mut prior_result_lock = prior_result_clone.lock().await;
                                *prior_result_lock = run_result;
                            }
                        }
                        Err(e) => {
                            println!("{:#?}", e);
                        }
                    }
                }
            }
        })
        .collect();

    let commands_gpt_impl = quote! {
        #[derive(Clone, Debug, Deserialize)]
        pub enum FunctionResponse {
            #(
                #generated_struct_names(#generated_struct_names),
            )*
        }

        impl CommandsGPT {
            #all_function_calls

            fn to_snake_case(camel_case: &str) -> String {
                let mut snake_case = String::new();
                for (i, ch) in camel_case.char_indices() {
                    if i > 0 && ch.is_uppercase() {
                        snake_case.push('_');
                    }
                    snake_case.extend(ch.to_lowercase());
                }
                snake_case
            }

            pub fn parse_gpt_function_call(function_call: &FunctionCall) -> Result<FunctionResponse, Box<dyn std::error::Error + Send + Sync + 'static>> {
                match function_call.name.as_str() {
                    #(
                    #struct_names => {
                        match serde_json::from_str::<#generated_struct_names>(&function_call.arguments) {
                            Ok(arguments) => Ok(FunctionResponse::#generated_struct_names(arguments)),
                            Err(_) => {
                                let snake_case_args = function_call.arguments
                                    .as_str()
                                    .split(',')
                                    .map(|s| {
                                        let mut parts = s.split(':');
                                        match (parts.next(), parts.next()) {
                                            (Some(key), Some(value)) => {
                                                let key_trimmed = key.trim_matches(|c: char| !c.is_alphanumeric()).trim();
                                                let key_snake_case = Self::to_snake_case(key_trimmed);
                                                format!("\"{}\":{}", key_snake_case, value)
                                            },
                                            _ => s.to_string()
                                        }
                                    })
                                    .collect::<Vec<String>>()
                                    .join(",");

                                let snake_case_args = format!("{{{}", snake_case_args);

                                match serde_json::from_str::<#generated_struct_names>(&snake_case_args) {
                                    Ok(arguments) => {
                                        Ok(FunctionResponse::#generated_struct_names(arguments))
                                    }
                                    Err(e) => {
                                        Err(Box::new(CommandError::new("There was an issue deserializing function arguments.")))
                                    }
                                }
                            }
                        }
                    },
                    )*
                    _ => {
                        println!("{:#?}", function_call);
                        Err(Box::new(CommandError::new("Unknown function name")))
                    }
                }
            }

            fn calculate_token_count(text: &str) -> usize {
                let bpe = tiktoken_rs::cl100k_base().unwrap();
                bpe.encode_ordinary(&text).len()
            }

            pub async fn run(
                prompt: &String,
                model_name: &str,
                request_token_limit: usize,
                max_response_tokens: u16,
                custom_system_message: Option<String>,
                prior_result: Arc<Mutex<Option<String>>>,
                execution_strategy: ToolCallExecutionStrategy,
            ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
                let tool_args = get_tool_chat_completion_args(CommandsGPT::all_function_jsons)?;

                let mut system_message_tokens = 7;
                let mut system_message = String::from("You are a helpful function calling bot.");
                if let Some(custom_system_message) = custom_system_message {
                    system_message = custom_system_message;
                    system_message_tokens = Self::calculate_token_count(system_message.as_str());
                }

                let request_token_total = tool_args.1 + system_message_tokens + Self::calculate_token_count(prompt.as_str());
                if request_token_total > request_token_limit {
                    return Err(Box::new(CommandError::new("Request token count is too high")));
                }

                let request = CreateChatCompletionRequestArgs::default()
                    .max_tokens(max_response_tokens)
                    .model(model_name)
                    // .temperature(0.0)
                    .messages([ChatCompletionRequestMessage::System(ChatCompletionRequestSystemMessageArgs::default()
                        .content(system_message)
                        .build()?),
                    ChatCompletionRequestMessage::User(ChatCompletionRequestUserMessageArgs::default()
                        .content(prompt.to_string())
                        .build()?)])
                    .tools(tool_args.0)
                    // What is going on here? Fails on their end if this is included.
                    // .tool_choice("auto")
                    .build()?;

                let client = Client::new();
                let response_message = client
                    .chat()
                    .create(request)
                    .await?
                    .choices
                    .get(0)
                    .unwrap()
                    .message
                    .clone();

                if let Some(tool_calls) = response_message.tool_calls {
                    if tool_calls.len() == 1 {
                        let execution_strategy_clone = execution_strategy.clone();

                        match Self::parse_gpt_function_call(&tool_calls.first().unwrap().function) {
                            #(#match_arms,)*
                            Err(e) => {
                                println!("{:#?}", e);
                                return Err(Box::new(CommandError::new("Error running GPT command")));
                            }
                        };
                    } else {
                        // TODO: FML. Can't get feature flags to get passed through from consuming
                        // code. Compilation context issue.
                        // #[cfg(feature = "verbose")]
                        // {
                        //     println!("Called {} tools. Execution strategy set to {:#?}.", tool_calls.len(), execution_strategy);
                        //     println!();
                        //
                        // }
                        println!("Called {} tools. Execution strategy set to {:#?}.", tool_calls.len(), execution_strategy);
                        println!();

                        match execution_strategy {
                            ToolCallExecutionStrategy::Async => {
                                let mut tasks = Vec::new();

                                for tool_call in tool_calls.iter() {
                                    match tool_call.r#type {
                                        ChatCompletionToolType::Function => {
                                            let function = tool_call.function.clone();
                                            let prior_result_clone = prior_result.clone();
                                            let execution_strategy_clone = execution_strategy.clone();

                                            let task = tokio::spawn( async move {
                                                match Self::parse_gpt_function_call(&function) {
                                                    #(#match_arms_no_return,)*
                                                    Err(e) => {
                                                        println!("{:#?}", e);
                                                    }
                                                }
                                            });
                                            tasks.push(task);
                                        },
                                    }
                                }

                                for task in tasks {
                                    let _ = task.await;
                                }
                            },
                            ToolCallExecutionStrategy::Synchronous => {
                                for tool_call in tool_calls.iter() {
                                    match tool_call.r#type {
                                        ChatCompletionToolType::Function => {
                                            let prior_result_clone = prior_result.clone();
                                            let execution_strategy_clone = execution_strategy.clone();

                                            match Self::parse_gpt_function_call(&tool_call.function) {
                                                #(#match_arms_no_return,)*
                                                Err(e) => {
                                                    println!("{:#?}", e);
                                                }
                                            }
                                        },
                                    }
                                }
                            },
                            ToolCallExecutionStrategy::Parallel => {
                                let mut handles = Vec::new();

                                for tool_call in tool_calls.iter() {
                                    match tool_call.r#type {
                                        ChatCompletionToolType::Function => {
                                            let function = tool_call.function.clone();
                                            let prior_result_clone = prior_result.clone();
                                            // let execution_strategy_clone = execution_strategy.clone();
                                            // TODO: Think through. There's a lot of overhead to
                                            // make os threads this way. For now assume that if
                                            // strategy is set to "Parallel" that we only want to
                                            // put the intially returned tool calls on threads, and
                                            // if they themselves contain something multi-step we
                                            // will run those as if they are io-bound. Potentially
                                            // makes sense to support letting variants get
                                            // decorated with a execution strategy preference like
                                            // "this is io bound" or "this is cpu bound" and then
                                            let execution_strategy_clone = ToolCallExecutionStrategy::Async;

                                            let handle = std::thread::spawn(move || {
                                                let rt = tokio::runtime::Runtime::new().unwrap();
                                                rt.block_on(async {
                                                    match Self::parse_gpt_function_call(&function) {
                                                        #(#match_arms_no_return,)*
                                                        Err(e) => {
                                                            println!("{:#?}", e);
                                                        }
                                                    }

                                                })
                                            });
                                            handles.push(handle);
                                        },
                                    }
                                }

                                for handle in handles {
                                    let _ = handle.join();
                                }
                            },
                        }
                    }
                    Ok(())
                } else {
                    return Ok(());
                }
            }
        }
    };

    let gen = quote! {
        use serde::Deserialize;
        use serde_json::{json, Value};
        use openai_func_enums::{
            generate_enum_info, generate_value_arg_info, get_tool_chat_completion_args,
        };

        use async_trait::async_trait;
        use async_openai::{
            types::{
                ChatCompletionFunctionCall, ChatCompletionNamedToolChoice, ChatCompletionRequestMessage,
                ChatCompletionRequestSystemMessageArgs, ChatCompletionRequestUserMessageArgs,
                ChatCompletionToolChoiceOption, ChatCompletionToolType, CreateChatCompletionRequestArgs,
                FunctionCall, FunctionName,
            },
            Client,
        };

        #(#generated_structs)*

        #(#json_generator_functions)*

        #(#generated_clap_gpt_enum)*

        #commands_gpt_impl
    };

    gen.into()
}

fn get_comment_from_attr(attr: &Attribute) -> Option<String> {
    if attr.path().is_ident("doc") {
        if let Meta::NameValue(meta) = &attr.meta {
            if meta.path.is_ident("doc") {
                let value = meta.value.clone();
                match value {
                    Expr::Lit(value) => match value.lit {
                        Lit::Str(value) => {
                            return Some(value.value());
                        }
                        _ => {
                            return None;
                        }
                    },
                    _ => {
                        return None;
                    }
                }
            }
        }
    }
    None
}

/// Calculate the token count of a given text string using the Byte Pair Encoding (BPE) tokenizer.
///
/// This function utilizes the BPE tokenizer from the `cl100k_base` library. It tokenizes the given text and
/// returns the count of the tokens. This can be used to measure how many tokens a particular text string
/// consumes, which is often relevant in the context of natural language processing tasks.
///
/// # Arguments
///
/// * `text` - A string slice that holds the text to tokenize.
///
/// # Returns
///
/// * `usize` - The count of tokens in the text.
///
/// # Example
///
/// ```
/// let text = "Hello, world!";
/// let token_count = calculate_token_count(text);
/// println!("Token count: {}", token_count);
/// ```
///
/// Note: This function can fail if the `cl100k_base` tokenizer is not properly initialized or the text cannot be tokenized.
fn calculate_token_count(text: &str) -> usize {
    let bpe = tiktoken_rs::cl100k_base().unwrap();
    bpe.encode_ordinary(text).len()
}

/// Convert a camelCase or PascalCase string into a snake_case string.
///
/// This function iterates over each character in the input string. If the character is an uppercase letter, it adds an
/// underscore before it (except if it's the first character) and then appends the lowercase version of the character
/// to the output string.
///
/// # Arguments
///
/// * `camel_case` - A string slice that holds the camelCase or PascalCase string to convert.
///
/// # Returns
///
/// * `String` - The converted snake_case string.
///
/// # Example
///
/// ```
/// let camel_case = "HelloWorld";
/// let snake_case = to_snake_case(camel_case);
/// assert_eq!(snake_case, "hello_world");
/// ```
fn to_snake_case(camel_case: &str) -> String {
    let mut snake_case = String::new();
    for (i, ch) in camel_case.char_indices() {
        if i > 0 && ch.is_uppercase() {
            snake_case.push('_');
        }
        snake_case.extend(ch.to_lowercase());
    }
    snake_case
}