golem-rust 3.0.1

Golem Rust tooling library that facilitates writing Golem backends in Rust
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
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
// Copyright 2024-2026 Golem Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(test)]
test_r::enable!();

pub use uuid::Uuid;
pub use wasip3;

pub mod http;

#[doc(hidden)]
#[cfg(feature = "export_golem_agentic")]
pub fn __link_golem_component_exports() {
    #[cfg(target_arch = "wasm32")]
    agentic::exports::raw::link();
}

/// Export the unified Golem guest world without registering an agent, tool, or middleware.
#[macro_export]
#[cfg(feature = "export_golem_agentic")]
macro_rules! export_golem_component {
    () => {
        $crate::ctor::__support::ctor_parse!(
            #[ctor]
            fn __golem_link_component_exports() {
                $crate::__link_golem_component_exports();
            }
        );
    };
}

pub use golem_schema;
pub use golem_schema::schema;
pub use golem_schema::schema::wit::direct::{FromWire, IntoWire, WireSchema};
pub use golem_schema::schema::{
    FromSchema, IntoSchema, IntoTypedSchemaValue, Quantity, QuantityUnit, Schema,
    SchemaFingerprintError, SchemaFingerprintV1, SchemaGraph, SchemaType, SchemaValue,
    TypedSchemaValue, schema_fingerprint_v1,
};
#[cfg(feature = "macro")]
pub use golem_schema::schema::{FromWire, IntoWire, WireSchema};
pub use golem_schema::{AgentId, ComponentId, EnvironmentId, PromiseId};

pub fn encode_schema_graph(
    graph: &SchemaGraph,
) -> Result<schema::wit::wire::SchemaGraph, schema::wit::EncodeError> {
    schema::wit::encode_graph(graph)
}

pub fn decode_schema_graph(
    graph: &schema::wit::wire::SchemaGraph,
) -> Result<SchemaGraph, schema::wit::DecodeError> {
    schema::wit::decode_graph(graph)
}

pub fn encode_schema_value(
    value: &SchemaValue,
) -> Result<schema::wit::wire::SchemaValueTree, schema::wit::EncodeError> {
    schema::wit::encode_value(value)
}

pub async fn encode_schema_value_async(
    value: &SchemaValue,
) -> Result<schema::wit::wire::SchemaValueTree, schema::wit::EncodeError> {
    schema::wit::encode_value_async(value).await
}

pub fn decode_schema_value(
    value: schema::wit::wire::SchemaValueTree,
) -> Result<SchemaValue, schema::wit::DecodeError> {
    schema::wit::decode_value(value)
}

pub fn encode_typed_schema_value(
    value: &TypedSchemaValue,
) -> Result<schema::wit::wire::TypedSchemaValue, schema::wit::EncodeError> {
    schema::wit::encode_typed(value)
}

pub async fn encode_typed_schema_value_async(
    value: &TypedSchemaValue,
) -> Result<schema::wit::wire::TypedSchemaValue, schema::wit::EncodeError> {
    schema::wit::encode_typed_async(value).await
}

pub fn decode_typed_schema_value(
    value: &schema::wit::wire::TypedSchemaValue,
) -> Result<TypedSchemaValue, schema::wit::DecodeError> {
    schema::wit::decode_typed(value)
}

pub fn encode_typed_schema_value_owned(
    value: TypedSchemaValue,
) -> Result<schema::wit::wire::TypedSchemaValue, schema::wit::EncodeError> {
    schema::wit::encode_typed_owned(value)
}

pub fn decode_typed_schema_value_owned(
    value: schema::wit::wire::TypedSchemaValue,
) -> Result<TypedSchemaValue, schema::wit::DecodeError> {
    schema::wit::decode_typed_owned(value)
}

// Compatibility shim for the `wasi:clocks` types the Golem WIT interfaces reference
// (`wasi:clocks/system-clock@0.3.0.{instant}` and
// `wasi:clocks/types@0.3.0.{duration}`). The `with:` remaps in every
// `generate!` block point the generated bindings at these modules so the Rust
// types come from a single source.

/// Public alias for the instant/datetime type used by Golem's scheduling APIs.
pub type ScheduledTime = wasip3::clocks::system_clock::Instant;

mod wasi_clocks_compat {
    pub mod system_clock {
        pub use wasip3::clocks::system_clock::Instant;
    }
    pub mod types {
        pub use wasip3::clocks::types::Duration;
    }
}

mod raw_bindings {
    use wit_bindgen::generate;

    generate!({
        path: "wit",
        world: "golem-rust",
        generate_all,
        generate_unused_types: true,
        pub_export_macro: true,
        with: {
            "golem:core/types@2.0.0": golem_schema::schema::wit::wire,
            "golem:tool/common@0.1.0": golem_schema::schema::tool::wit::wire,
            "wasi:clocks/system-clock@0.3.0": crate::wasi_clocks_compat::system_clock,
            "wasi:clocks/types@0.3.0": crate::wasi_clocks_compat::types,
        }
    });
}

/// Component Model `stream` constructor generated by `wit_bindgen` for the
/// primary bindings world. Used by callers that need to hand a writable
/// `stream<u8>` to host imports such as `wasi:blobstore` outgoing values.
pub use crate::raw_bindings::wit_stream;

pub mod bindings {
    pub use crate::raw_bindings::wasi;

    pub mod golem {
        pub mod api {
            pub(crate) use crate::raw_bindings::golem::api::host;
            pub use crate::raw_bindings::golem::api::{context, oplog, retry};
        }

        pub mod durability {
            pub(crate) use crate::raw_bindings::golem::durability::durability;
        }

        pub mod quota {
            pub(crate) use crate::raw_bindings::golem::quota::types;
        }

        pub mod secrets {
            pub use crate::raw_bindings::golem::secrets::{reveal, types};
        }

        pub mod agent {
            pub use crate::raw_bindings::golem::agent::{common, durable_streams, host};
        }

        pub mod permissions {
            pub(crate) use crate::raw_bindings::golem::permissions::{
                derive, inspect, kernel_introspection, revoke, types, wallet,
            };
        }

        pub mod tool {
            pub use crate::raw_bindings::golem::tool::{host, streams};
            pub use crate::schema::tool::wit::wire as common;
        }

        pub use crate::raw_bindings::golem::{rdbms, websocket};
    }
}

#[cfg(feature = "export_load_snapshot")]
pub mod load_snapshot {
    use wit_bindgen::generate;

    generate!({
        path: "wit",
        world: "golem-rust-load-snapshot",
        async: ["export:golem:api/load-snapshot@1.5.0#load"],
        generate_all,
        generate_unused_types: true,
        pub_export_macro: true,
        with: {
            "golem:core/types@2.0.0": golem_schema::schema::wit::wire,
            "golem:tool/common@0.1.0": golem_schema::schema::tool::wit::wire,
            "wasi:clocks/system-clock@0.3.0": crate::wasi_clocks_compat::system_clock,
            "wasi:clocks/types@0.3.0": crate::wasi_clocks_compat::types,

            "golem:api/host@1.5.0": crate::bindings::golem::api::host,
            "golem:tool/host@0.1.0": crate::bindings::golem::tool::host,
            "golem:api/retry@1.5.0": crate::bindings::golem::api::retry,
            "golem:api/oplog@1.5.0": crate::bindings::golem::api::oplog,
            "golem:api/context@1.5.0": crate::bindings::golem::api::context,
            "golem:durability/durability@1.6.0": crate::bindings::golem::durability::durability,
            "golem:quota/types@1.5.0": crate::bindings::golem::quota::types,
            "golem:secrets/types@0.1.0": crate::bindings::golem::secrets::types,
            "golem:secrets/reveal@0.1.0": crate::bindings::golem::secrets::reveal,
            "golem:permissions/types@0.1.0": crate::bindings::golem::permissions::types,
            "golem:permissions/derive@0.1.0": crate::bindings::golem::permissions::derive,
            "golem:permissions/inspect@0.1.0": crate::bindings::golem::permissions::inspect,
            "golem:permissions/revoke@0.1.0": crate::bindings::golem::permissions::revoke,
            "golem:permissions/wallet@0.1.0": crate::bindings::golem::permissions::wallet,
            "golem:permissions/kernel-introspection@0.1.0": crate::bindings::golem::permissions::kernel_introspection,
            "golem:rdbms/mysql@1.5.0": crate::bindings::golem::rdbms::mysql,
            "golem:rdbms/postgres@1.5.0": crate::bindings::golem::rdbms::postgres,
            "golem:rdbms/types@1.5.0": crate::bindings::golem::rdbms::types,
            "wasi:blobstore/blobstore": crate::bindings::wasi::blobstore::blobstore,
            "wasi:blobstore/container": crate::bindings::wasi::blobstore::container,
            "wasi:blobstore/types": crate::bindings::wasi::blobstore::types,
            "wasi:keyvalue/eventual-batch@0.1.0": crate::bindings::wasi::keyvalue::eventual_batch,
            "wasi:keyvalue/eventual@0.1.0": crate::bindings::wasi::keyvalue::eventual,
            "wasi:keyvalue/types@0.1.0": crate::bindings::wasi::keyvalue::types,
            "wasi:keyvalue/wasi-keyvalue-error@0.1.0": crate::bindings::wasi::keyvalue::wasi_keyvalue_error,
            "wasi:logging/logging": crate::bindings::wasi::logging::logging,
        }
    });

    pub use __export_golem_rust_load_snapshot_impl as export_load_snapshot;
}

#[cfg(feature = "export_save_snapshot")]
pub mod save_snapshot {
    use wit_bindgen::generate;

    generate!({
        path: "wit",
        world: "golem-rust-save-snapshot",
        async: ["export:golem:api/save-snapshot@1.5.0#save"],
        generate_all,
        generate_unused_types: true,
        pub_export_macro: true,
        with: {
            "golem:core/types@2.0.0": golem_schema::schema::wit::wire,
            "golem:tool/common@0.1.0": golem_schema::schema::tool::wit::wire,
            "wasi:clocks/system-clock@0.3.0": crate::wasi_clocks_compat::system_clock,
            "wasi:clocks/types@0.3.0": crate::wasi_clocks_compat::types,

            "golem:api/host@1.5.0": crate::bindings::golem::api::host,
            "golem:tool/host@0.1.0": crate::bindings::golem::tool::host,
            "golem:api/retry@1.5.0": crate::bindings::golem::api::retry,
            "golem:api/oplog@1.5.0": crate::bindings::golem::api::oplog,
            "golem:api/context@1.5.0": crate::bindings::golem::api::context,
            "golem:durability/durability@1.6.0": crate::bindings::golem::durability::durability,
            "golem:quota/types@1.5.0": crate::bindings::golem::quota::types,
            "golem:secrets/types@0.1.0": crate::bindings::golem::secrets::types,
            "golem:secrets/reveal@0.1.0": crate::bindings::golem::secrets::reveal,
            "golem:permissions/types@0.1.0": crate::bindings::golem::permissions::types,
            "golem:permissions/derive@0.1.0": crate::bindings::golem::permissions::derive,
            "golem:permissions/inspect@0.1.0": crate::bindings::golem::permissions::inspect,
            "golem:permissions/revoke@0.1.0": crate::bindings::golem::permissions::revoke,
            "golem:permissions/wallet@0.1.0": crate::bindings::golem::permissions::wallet,
            "golem:permissions/kernel-introspection@0.1.0": crate::bindings::golem::permissions::kernel_introspection,
            "golem:rdbms/mysql@1.5.0": crate::bindings::golem::rdbms::mysql,
            "golem:rdbms/postgres@1.5.0": crate::bindings::golem::rdbms::postgres,
            "golem:rdbms/types@1.5.0": crate::bindings::golem::rdbms::types,
            "wasi:blobstore/blobstore": crate::bindings::wasi::blobstore::blobstore,
            "wasi:blobstore/container": crate::bindings::wasi::blobstore::container,
            "wasi:blobstore/types": crate::bindings::wasi::blobstore::types,
            "wasi:keyvalue/eventual-batch@0.1.0": crate::bindings::wasi::keyvalue::eventual_batch,
            "wasi:keyvalue/eventual@0.1.0": crate::bindings::wasi::keyvalue::eventual,
            "wasi:keyvalue/types@0.1.0": crate::bindings::wasi::keyvalue::types,
            "wasi:keyvalue/wasi-keyvalue-error@0.1.0": crate::bindings::wasi::keyvalue::wasi_keyvalue_error,
            "wasi:logging/logging": crate::bindings::wasi::logging::logging,
        }
    });

    pub use __export_golem_rust_save_snapshot_impl as export_save_snapshot;
}

#[cfg(feature = "export_golem_agentic")]
pub mod golem_agentic {
    use wit_bindgen::generate;

    generate!({
        path: "wit",
        world: "golem-agentic",
        async: [
            "export:golem:agent/guest@2.0.0#initialize",
            "export:golem:agent/guest@2.0.0#invoke",
            "export:golem:tool/guest@0.1.0#invoke",
            "export:golem:tool/tool-middleware-guest@0.1.0#invoke-tool-middleware",
        ],
        generate_all,
        generate_unused_types: true,
        pub_export_macro: true,

        with: {
            "golem:core/types@2.0.0": golem_schema::schema::wit::wire,
            "golem:tool/common@0.1.0": golem_schema::schema::tool::wit::wire,
            "wasi:clocks/system-clock@0.3.0": crate::wasi_clocks_compat::system_clock,
            "wasi:clocks/types@0.3.0": crate::wasi_clocks_compat::types,

            "golem:api/host@1.5.0": crate::bindings::golem::api::host,
            "golem:api/retry@1.5.0": crate::bindings::golem::api::retry,
            "golem:api/oplog@1.5.0": crate::bindings::golem::api::oplog,
            "golem:api/context@1.5.0": crate::bindings::golem::api::context,
            "golem:durability/durability@1.6.0": crate::bindings::golem::durability::durability,
            "golem:quota/types@1.5.0": crate::bindings::golem::quota::types,
            "golem:secrets/types@0.1.0": crate::bindings::golem::secrets::types,
            "golem:secrets/reveal@0.1.0": crate::bindings::golem::secrets::reveal,
            "golem:permissions/types@0.1.0": crate::bindings::golem::permissions::types,
            "golem:permissions/derive@0.1.0": crate::bindings::golem::permissions::derive,
            "golem:permissions/inspect@0.1.0": crate::bindings::golem::permissions::inspect,
            "golem:permissions/revoke@0.1.0": crate::bindings::golem::permissions::revoke,
            "golem:permissions/wallet@0.1.0": crate::bindings::golem::permissions::wallet,
            "golem:permissions/kernel-introspection@0.1.0": crate::bindings::golem::permissions::kernel_introspection,
            "golem:rdbms/mysql@1.5.0": crate::bindings::golem::rdbms::mysql,
            "golem:rdbms/postgres@1.5.0": crate::bindings::golem::rdbms::postgres,
            "golem:rdbms/types@1.5.0": crate::bindings::golem::rdbms::types,
            "wasi:blobstore/blobstore": crate::bindings::wasi::blobstore::blobstore,
            "wasi:blobstore/container": crate::bindings::wasi::blobstore::container,
            "wasi:blobstore/types": crate::bindings::wasi::blobstore::types,
            "wasi:keyvalue/eventual-batch@0.1.0": crate::bindings::wasi::keyvalue::eventual_batch,
            "wasi:keyvalue/eventual@0.1.0": crate::bindings::wasi::keyvalue::eventual,
            "wasi:keyvalue/types@0.1.0": crate::bindings::wasi::keyvalue::types,
            "wasi:keyvalue/wasi-keyvalue-error@0.1.0": crate::bindings::wasi::keyvalue::wasi_keyvalue_error,
            "wasi:logging/logging": crate::bindings::wasi::logging::logging,
            "wasi:config/store@0.2.0-draft": crate::bindings::wasi::config::store,
        }
    });

    pub use __export_golem_agentic_impl as export_golem_agentic;
}
#[cfg(feature = "export_golem_agentic")]
pub(crate) use golem_agentic::golem::tool::underlying as tool_underlying_bindings;

#[cfg(feature = "export_golem_agentic")]
pub use ctor;

#[cfg(feature = "export_golem_agentic")]
pub use async_trait;

#[cfg(feature = "export_golem_agentic")]
pub use serde;

#[cfg(feature = "export_golem_agentic")]
pub use serde_json;

#[cfg(feature = "export_oplog_processor")]
pub mod oplog_processor {
    use wit_bindgen::generate;

    pub mod host {
        pub use crate::raw_bindings::golem::api::host::AgentMetadata;
    }

    generate!({
        path: "wit",
        world: "golem-rust-oplog-processor",
        generate_all,
        generate_unused_types: true,
        pub_export_macro: true,
        with: {
            "golem:core/types@2.0.0": golem_schema::schema::wit::wire,
            "golem:tool/common@0.1.0": golem_schema::schema::tool::wit::wire,
            "wasi:clocks/system-clock@0.3.0": crate::wasi_clocks_compat::system_clock,
            "wasi:clocks/types@0.3.0": crate::wasi_clocks_compat::types,

            "golem:api/host@1.5.0": crate::bindings::golem::api::host,
            "golem:tool/host@0.1.0": crate::bindings::golem::tool::host,
            "golem:api/retry@1.5.0": crate::bindings::golem::api::retry,
            "golem:api/oplog@1.5.0": crate::bindings::golem::api::oplog,
            "golem:api/context@1.5.0": crate::bindings::golem::api::context,
            "golem:durability/durability@1.6.0": crate::bindings::golem::durability::durability,
            "golem:quota/types@1.5.0": crate::bindings::golem::quota::types,
            "golem:secrets/types@0.1.0": crate::bindings::golem::secrets::types,
            "golem:secrets/reveal@0.1.0": crate::bindings::golem::secrets::reveal,
            "golem:permissions/types@0.1.0": crate::bindings::golem::permissions::types,
            "golem:permissions/derive@0.1.0": crate::bindings::golem::permissions::derive,
            "golem:permissions/inspect@0.1.0": crate::bindings::golem::permissions::inspect,
            "golem:permissions/revoke@0.1.0": crate::bindings::golem::permissions::revoke,
            "golem:permissions/wallet@0.1.0": crate::bindings::golem::permissions::wallet,
            "golem:permissions/kernel-introspection@0.1.0": crate::bindings::golem::permissions::kernel_introspection,
            "golem:rdbms/mysql@1.5.0": crate::bindings::golem::rdbms::mysql,
            "golem:rdbms/postgres@1.5.0": crate::bindings::golem::rdbms::postgres,
            "golem:rdbms/types@1.5.0": crate::bindings::golem::rdbms::types,
            "wasi:blobstore/blobstore": crate::bindings::wasi::blobstore::blobstore,
            "wasi:blobstore/container": crate::bindings::wasi::blobstore::container,
            "wasi:blobstore/types": crate::bindings::wasi::blobstore::types,
            "wasi:keyvalue/eventual-batch@0.1.0": crate::bindings::wasi::keyvalue::eventual_batch,
            "wasi:keyvalue/eventual@0.1.0": crate::bindings::wasi::keyvalue::eventual,
            "wasi:keyvalue/types@0.1.0": crate::bindings::wasi::keyvalue::types,
            "wasi:keyvalue/wasi-keyvalue-error@0.1.0": crate::bindings::wasi::keyvalue::wasi_keyvalue_error,
            "wasi:logging/logging": crate::bindings::wasi::logging::logging,
        }
    });

    pub use __export_golem_rust_oplog_processor_impl as export_oplog_processor;
}

#[cfg(feature = "export_golem_agentic")]
pub mod agentic;

#[cfg(feature = "export_golem_agentic")]
pub use agentic::reflection::*;

#[cfg(feature = "durability")]
pub mod durability;

#[cfg(feature = "json")]
mod json;

#[cfg(feature = "json")]
pub use json::*;

#[cfg(feature = "json")]
pub mod durable_streams;

pub mod permissions;
pub mod quota;
pub mod secrets;
pub mod tool;
mod transaction;

use bindings::golem::api::host as host_api;

pub type OplogIndex = u64;

pub mod websocket;
pub use quota::*;
pub use secrets::*;
pub mod retry;
pub use transaction::*;
pub use websocket::{WebSocketCloseInfo, WebSocketError, WebSocketMessage, WebsocketConnection};

#[cfg(feature = "macro")]
pub use golem_rust_macro::*;

fn schema_uuid_to_wire(value: Uuid) -> schema::wit::wire::Uuid {
    let (high_bits, low_bits) = value.as_u64_pair();
    schema::wit::wire::Uuid {
        high_bits,
        low_bits,
    }
}

fn wire_uuid_to_schema(value: schema::wit::wire::Uuid) -> Uuid {
    Uuid::from_u64_pair(value.high_bits, value.low_bits)
}

fn schema_component_id_to_wire(value: ComponentId) -> schema::wit::wire::ComponentId {
    schema::wit::wire::ComponentId {
        uuid: schema_uuid_to_wire(value.uuid),
    }
}

fn wire_component_id_to_schema(value: schema::wit::wire::ComponentId) -> ComponentId {
    ComponentId {
        uuid: wire_uuid_to_schema(value.uuid),
    }
}

fn schema_agent_id_to_wire(value: AgentId) -> schema::wit::wire::AgentId {
    schema::wit::wire::AgentId {
        component_id: schema_component_id_to_wire(value.component_id),
        agent_id: value.agent_id,
    }
}

fn wire_agent_id_to_schema(value: schema::wit::wire::AgentId) -> AgentId {
    AgentId {
        component_id: wire_component_id_to_schema(value.component_id),
        agent_id: value.agent_id,
    }
}

fn schema_promise_id_to_wire(value: &PromiseId) -> schema::wit::wire::PromiseId {
    schema::wit::wire::PromiseId {
        agent_id: schema_agent_id_to_wire(value.agent_id.clone()),
        oplog_idx: value.oplog_idx,
    }
}

fn wire_promise_id_to_schema(value: schema::wit::wire::PromiseId) -> PromiseId {
    PromiseId {
        agent_id: wire_agent_id_to_schema(value.agent_id),
        oplog_idx: value.oplog_idx,
    }
}

fn schema_environment_id_to_host(value: EnvironmentId) -> host_api::EnvironmentId {
    host_api::EnvironmentId {
        uuid: schema_uuid_to_wire(value.uuid),
    }
}

fn host_environment_id_to_schema(value: host_api::EnvironmentId) -> EnvironmentId {
    EnvironmentId {
        uuid: wire_uuid_to_schema(value.uuid),
    }
}

#[derive(
    Clone, Copy, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema,
)]
pub enum UpdateMode {
    Automatic,
    SnapshotBased,
}

impl From<host_api::UpdateMode> for UpdateMode {
    fn from(value: host_api::UpdateMode) -> Self {
        match value {
            host_api::UpdateMode::Automatic => Self::Automatic,
            host_api::UpdateMode::SnapshotBased => Self::SnapshotBased,
        }
    }
}

impl From<UpdateMode> for host_api::UpdateMode {
    fn from(value: UpdateMode) -> Self {
        match value {
            UpdateMode::Automatic => Self::Automatic,
            UpdateMode::SnapshotBased => Self::SnapshotBased,
        }
    }
}

#[derive(
    Clone, Copy, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema,
)]
pub enum FilterComparator {
    Equal,
    NotEqual,
    GreaterEqual,
    Greater,
    LessEqual,
    Less,
}

impl From<host_api::FilterComparator> for FilterComparator {
    fn from(value: host_api::FilterComparator) -> Self {
        match value {
            host_api::FilterComparator::Equal => Self::Equal,
            host_api::FilterComparator::NotEqual => Self::NotEqual,
            host_api::FilterComparator::GreaterEqual => Self::GreaterEqual,
            host_api::FilterComparator::Greater => Self::Greater,
            host_api::FilterComparator::LessEqual => Self::LessEqual,
            host_api::FilterComparator::Less => Self::Less,
        }
    }
}

impl From<FilterComparator> for host_api::FilterComparator {
    fn from(value: FilterComparator) -> Self {
        match value {
            FilterComparator::Equal => Self::Equal,
            FilterComparator::NotEqual => Self::NotEqual,
            FilterComparator::GreaterEqual => Self::GreaterEqual,
            FilterComparator::Greater => Self::Greater,
            FilterComparator::LessEqual => Self::LessEqual,
            FilterComparator::Less => Self::Less,
        }
    }
}

#[derive(
    Clone, Copy, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema,
)]
pub enum StringFilterComparator {
    Equal,
    NotEqual,
    Like,
    NotLike,
    StartsWith,
}

impl From<host_api::StringFilterComparator> for StringFilterComparator {
    fn from(value: host_api::StringFilterComparator) -> Self {
        match value {
            host_api::StringFilterComparator::Equal => Self::Equal,
            host_api::StringFilterComparator::NotEqual => Self::NotEqual,
            host_api::StringFilterComparator::Like => Self::Like,
            host_api::StringFilterComparator::NotLike => Self::NotLike,
            host_api::StringFilterComparator::StartsWith => Self::StartsWith,
        }
    }
}

impl From<StringFilterComparator> for host_api::StringFilterComparator {
    fn from(value: StringFilterComparator) -> Self {
        match value {
            StringFilterComparator::Equal => Self::Equal,
            StringFilterComparator::NotEqual => Self::NotEqual,
            StringFilterComparator::Like => Self::Like,
            StringFilterComparator::NotLike => Self::NotLike,
            StringFilterComparator::StartsWith => Self::StartsWith,
        }
    }
}

#[derive(
    Clone, Copy, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema,
)]
pub enum AgentStatus {
    Running,
    Idle,
    Suspended,
    Interrupted,
    Retrying,
    Failed,
    Exited,
}

impl From<host_api::AgentStatus> for AgentStatus {
    fn from(value: host_api::AgentStatus) -> Self {
        match value {
            host_api::AgentStatus::Running => Self::Running,
            host_api::AgentStatus::Idle => Self::Idle,
            host_api::AgentStatus::Suspended => Self::Suspended,
            host_api::AgentStatus::Interrupted => Self::Interrupted,
            host_api::AgentStatus::Retrying => Self::Retrying,
            host_api::AgentStatus::Failed => Self::Failed,
            host_api::AgentStatus::Exited => Self::Exited,
        }
    }
}

impl From<AgentStatus> for host_api::AgentStatus {
    fn from(value: AgentStatus) -> Self {
        match value {
            AgentStatus::Running => Self::Running,
            AgentStatus::Idle => Self::Idle,
            AgentStatus::Suspended => Self::Suspended,
            AgentStatus::Interrupted => Self::Interrupted,
            AgentStatus::Retrying => Self::Retrying,
            AgentStatus::Failed => Self::Failed,
            AgentStatus::Exited => Self::Exited,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema)]
pub struct AgentNameFilter {
    pub comparator: StringFilterComparator,
    pub value: String,
}

impl From<host_api::AgentNameFilter> for AgentNameFilter {
    fn from(value: host_api::AgentNameFilter) -> Self {
        Self {
            comparator: Into::into(value.comparator),
            value: value.value,
        }
    }
}

impl From<AgentNameFilter> for host_api::AgentNameFilter {
    fn from(value: AgentNameFilter) -> Self {
        Self {
            comparator: Into::into(value.comparator),
            value: value.value,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema)]
pub struct AgentStatusFilter {
    pub comparator: FilterComparator,
    pub value: AgentStatus,
}

impl From<host_api::AgentStatusFilter> for AgentStatusFilter {
    fn from(value: host_api::AgentStatusFilter) -> Self {
        Self {
            comparator: Into::into(value.comparator),
            value: Into::into(value.value),
        }
    }
}

impl From<AgentStatusFilter> for host_api::AgentStatusFilter {
    fn from(value: AgentStatusFilter) -> Self {
        Self {
            comparator: Into::into(value.comparator),
            value: Into::into(value.value),
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema)]
pub struct AgentVersionFilter {
    pub comparator: FilterComparator,
    pub value: u64,
}

impl From<host_api::AgentVersionFilter> for AgentVersionFilter {
    fn from(value: host_api::AgentVersionFilter) -> Self {
        Self {
            comparator: Into::into(value.comparator),
            value: value.value,
        }
    }
}

impl From<AgentVersionFilter> for host_api::AgentVersionFilter {
    fn from(value: AgentVersionFilter) -> Self {
        Self {
            comparator: Into::into(value.comparator),
            value: value.value,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema)]
pub struct AgentCreatedAtFilter {
    pub comparator: FilterComparator,
    pub value: u64,
}

impl From<host_api::AgentCreatedAtFilter> for AgentCreatedAtFilter {
    fn from(value: host_api::AgentCreatedAtFilter) -> Self {
        Self {
            comparator: Into::into(value.comparator),
            value: value.value,
        }
    }
}

impl From<AgentCreatedAtFilter> for host_api::AgentCreatedAtFilter {
    fn from(value: AgentCreatedAtFilter) -> Self {
        Self {
            comparator: Into::into(value.comparator),
            value: value.value,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema)]
pub struct AgentEnvFilter {
    pub name: String,
    pub comparator: StringFilterComparator,
    pub value: String,
}

impl From<host_api::AgentEnvFilter> for AgentEnvFilter {
    fn from(value: host_api::AgentEnvFilter) -> Self {
        Self {
            name: value.name,
            comparator: Into::into(value.comparator),
            value: value.value,
        }
    }
}

impl From<AgentEnvFilter> for host_api::AgentEnvFilter {
    fn from(value: AgentEnvFilter) -> Self {
        Self {
            name: value.name,
            comparator: Into::into(value.comparator),
            value: value.value,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema)]
pub struct AgentConfigVarsFilter {
    pub name: String,
    pub comparator: StringFilterComparator,
    pub value: String,
}

impl From<host_api::AgentConfigVarsFilter> for AgentConfigVarsFilter {
    fn from(value: host_api::AgentConfigVarsFilter) -> Self {
        Self {
            name: value.name,
            comparator: Into::into(value.comparator),
            value: value.value,
        }
    }
}

impl From<AgentConfigVarsFilter> for host_api::AgentConfigVarsFilter {
    fn from(value: AgentConfigVarsFilter) -> Self {
        Self {
            name: value.name,
            comparator: Into::into(value.comparator),
            value: value.value,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema)]
pub enum AgentPropertyFilter {
    Name(AgentNameFilter),
    Status(AgentStatusFilter),
    Version(AgentVersionFilter),
    CreatedAt(AgentCreatedAtFilter),
    Env(AgentEnvFilter),
    Config(AgentConfigVarsFilter),
}

impl From<host_api::AgentPropertyFilter> for AgentPropertyFilter {
    fn from(value: host_api::AgentPropertyFilter) -> Self {
        match value {
            host_api::AgentPropertyFilter::Name(value) => Self::Name(Into::into(value)),
            host_api::AgentPropertyFilter::Status(value) => Self::Status(Into::into(value)),
            host_api::AgentPropertyFilter::Version(value) => Self::Version(Into::into(value)),
            host_api::AgentPropertyFilter::CreatedAt(value) => Self::CreatedAt(Into::into(value)),
            host_api::AgentPropertyFilter::Env(value) => Self::Env(Into::into(value)),
            host_api::AgentPropertyFilter::Config(value) => Self::Config(Into::into(value)),
        }
    }
}

impl From<AgentPropertyFilter> for host_api::AgentPropertyFilter {
    fn from(value: AgentPropertyFilter) -> Self {
        match value {
            AgentPropertyFilter::Name(value) => Self::Name(Into::into(value)),
            AgentPropertyFilter::Status(value) => Self::Status(Into::into(value)),
            AgentPropertyFilter::Version(value) => Self::Version(Into::into(value)),
            AgentPropertyFilter::CreatedAt(value) => Self::CreatedAt(Into::into(value)),
            AgentPropertyFilter::Env(value) => Self::Env(Into::into(value)),
            AgentPropertyFilter::Config(value) => Self::Config(Into::into(value)),
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema)]
pub struct AgentAllFilter {
    pub filters: Vec<AgentPropertyFilter>,
}

impl From<host_api::AgentAllFilter> for AgentAllFilter {
    fn from(value: host_api::AgentAllFilter) -> Self {
        Self {
            filters: value.filters.into_iter().map(Into::into).collect(),
        }
    }
}

impl From<AgentAllFilter> for host_api::AgentAllFilter {
    fn from(value: AgentAllFilter) -> Self {
        Self {
            filters: value.filters.into_iter().map(Into::into).collect(),
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema)]
pub struct AgentAnyFilter {
    pub filters: Vec<AgentAllFilter>,
}

impl From<host_api::AgentAnyFilter> for AgentAnyFilter {
    fn from(value: host_api::AgentAnyFilter) -> Self {
        Self {
            filters: value.filters.into_iter().map(Into::into).collect(),
        }
    }
}

impl From<AgentAnyFilter> for host_api::AgentAnyFilter {
    fn from(value: AgentAnyFilter) -> Self {
        Self {
            filters: value.filters.into_iter().map(Into::into).collect(),
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema)]
pub struct AgentMetadata {
    pub agent_id: AgentId,
    pub args: Vec<String>,
    pub env: Vec<(String, String)>,
    pub config: Vec<(String, String)>,
    pub status: AgentStatus,
    pub component_revision: u64,
    pub retry_count: u64,
    pub environment_id: EnvironmentId,
}

impl From<host_api::AgentMetadata> for AgentMetadata {
    fn from(value: host_api::AgentMetadata) -> Self {
        Self {
            agent_id: wire_agent_id_to_schema(value.agent_id),
            args: value.args,
            env: value.env,
            config: value.config,
            status: Into::into(value.status),
            component_revision: value.component_revision,
            retry_count: value.retry_count,
            environment_id: host_environment_id_to_schema(value.environment_id),
        }
    }
}

impl From<AgentMetadata> for host_api::AgentMetadata {
    fn from(value: AgentMetadata) -> Self {
        Self {
            agent_id: schema_agent_id_to_wire(value.agent_id),
            args: value.args,
            env: value.env,
            config: value.config,
            status: Into::into(value.status),
            component_revision: value.component_revision,
            retry_count: value.retry_count,
            environment_id: schema_environment_id_to_host(value.environment_id),
        }
    }
}

pub struct GetAgents {
    raw: host_api::GetAgents,
}

pub use host_api::AgentOperationError;

impl GetAgents {
    pub fn new(component_id: ComponentId, filter: Option<&AgentAnyFilter>, precise: bool) -> Self {
        let raw_filter = filter.cloned().map(host_api::AgentAnyFilter::from);
        Self {
            raw: host_api::GetAgents::new(
                schema_component_id_to_wire(component_id),
                raw_filter.as_ref(),
                precise,
            ),
        }
    }

    pub fn get_next(&self) -> Result<Option<Vec<AgentMetadata>>, AgentOperationError> {
        self.raw
            .get_next()
            .map(|values| values.map(|values| values.into_iter().map(Into::into).collect()))
    }
}

#[derive(Clone, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema)]
pub struct ForkDetails {
    pub forked_phantom_id: Uuid,
}

#[derive(Clone, Debug, Eq, PartialEq, IntoSchema, FromSchema, IntoWire, FromWire, WireSchema)]
pub enum ForkResult {
    Original(ForkDetails),
    Forked(ForkDetails),
}

impl From<host_api::ForkResult> for ForkResult {
    fn from(value: host_api::ForkResult) -> Self {
        match value {
            host_api::ForkResult::Original(details) => Self::Original(ForkDetails {
                forked_phantom_id: wire_uuid_to_schema(details.forked_phantom_id),
            }),
            host_api::ForkResult::Forked(details) => Self::Forked(ForkDetails {
                forked_phantom_id: wire_uuid_to_schema(details.forked_phantom_id),
            }),
        }
    }
}

pub struct PromiseResult {
    raw: host_api::GetPromiseResult,
}

impl PromiseResult {
    /// Awaits the promise result. The agent is suspended until the promise is
    /// completed; the completed payload is then returned.
    ///
    /// On WASI P2 this was a non-blocking `get() -> Option<...>` paired with a
    /// `subscribe() -> pollable`; the P3 `wasi:io/poll` pollable is gone, so the
    /// host `get-promise-result.get` is now an `async func` that awaits directly.
    pub async fn get(&self) -> Vec<u8> {
        self.raw.get().await
    }
}

pub fn create_promise() -> PromiseId {
    wire_promise_id_to_schema(host_api::create_promise())
}

pub fn get_promise(promise_id: &PromiseId) -> PromiseResult {
    PromiseResult {
        raw: host_api::get_promise(&schema_promise_id_to_wire(promise_id)),
    }
}

pub fn complete_promise(promise_id: &PromiseId, data: &[u8]) -> bool {
    host_api::complete_promise(&schema_promise_id_to_wire(promise_id), data)
}

pub fn oplog_commit(replicas: u8) {
    host_api::oplog_commit(replicas)
}

/// Read full self metadata. Requires view permission on the running agent,
/// admitted by the effective permission card and permitted by `initialCard`.
/// Stored environment and configuration fields are not filtered by the per-key
/// permissions used for WASI reads. Pure initialization does not call this API.
/// Permission denials and backend failures remain typed `AgentOperationError`s.
pub fn get_self_metadata() -> Result<AgentMetadata, AgentOperationError> {
    host_api::get_self_metadata().map(Into::into)
}

pub fn get_agent_metadata(agent_id: &AgentId) -> Option<AgentMetadata> {
    host_api::get_agent_metadata(&schema_agent_id_to_wire(agent_id.clone())).map(Into::into)
}

pub fn update_agent(
    agent_id: &AgentId,
    target_revision: u64,
    mode: UpdateMode,
) -> Result<(), AgentOperationError> {
    host_api::update_agent(
        &schema_agent_id_to_wire(agent_id.clone()),
        target_revision,
        Into::into(mode),
    )
}

pub fn resolve_component_id(component_reference: &str) -> Option<ComponentId> {
    host_api::resolve_component_id(component_reference).map(wire_component_id_to_schema)
}

pub fn resolve_agent_id(component_reference: &str, agent_name: &str) -> Option<AgentId> {
    host_api::resolve_agent_id(component_reference, agent_name).map(wire_agent_id_to_schema)
}

pub fn resolve_agent_id_strict(component_reference: &str, agent_name: &str) -> Option<AgentId> {
    host_api::resolve_agent_id_strict(component_reference, agent_name).map(wire_agent_id_to_schema)
}

pub fn fork() -> Result<ForkResult, AgentOperationError> {
    host_api::fork().map(Into::into)
}

/// Awaits a promise blocking the execution of the agent. The agent is going to be
/// suspended until the promise is completed.
///
/// Use `await_promise` for an async version of this function, allowing to interleave
/// awaiting of the promise with other operations.
pub fn blocking_await_promise(promise_id: &PromiseId) -> Vec<u8> {
    let promise = get_promise(promise_id);
    wit_bindgen::block_on(async move { promise.get().await })
}

/// Awaits a promise.
///
/// If only promises or timeouts are awaited simultaneously, the agent is going to be
/// suspended until any of them completes.
pub async fn await_promise(promise_id: &PromiseId) -> Vec<u8> {
    let promise = get_promise(promise_id);
    promise.get().await
}

/// Generates an idempotency key. This operation will never be replayed —
/// i.e. not only is this key generated, but it is persisted and committed, such that the key can be used in third-party systems (e.g. payment processing)
/// to introduce idempotence.
pub fn generate_idempotency_key() -> uuid::Uuid {
    Into::into(host_api::generate_idempotency_key())
}