Skip to main content

sim_lib_openai_server/
manifest.rs

1use std::sync::Arc;
2
3use sim_kernel::{
4    AbiVersion, Export, Lib, LibManifest, LibTarget, Linker, Result, Symbol, Version,
5};
6
7use crate::codec_openai::{install_openai_codec, openai_codec_symbol};
8use crate::ops::{
9    OpenAiGatewayFunction, cache_stats_symbol, capability_report_symbol, events_symbol,
10    fabric_symbol, health_symbol, key_add_symbol, key_list_symbol, model_health_symbol,
11    models_symbol, plan_check_symbol, plan_combinators_symbol, plan_explain_symbol,
12    plan_parse_symbol, plan_run_symbol, run_get_symbol, runs_symbol, serve_symbol,
13    storage_stats_symbol,
14};
15
16const OPENAI_GATEWAY_LIB_ID: &str = "openai-gateway";
17
18/// Loadable library that installs the OpenAI-compatible gateway surface.
19///
20/// Implements [`Lib`]: its manifest declares the gateway's id, version, ABI,
21/// and exports, and its loader registers the OpenAI codec plus every
22/// [`OpenAiGatewayFunction`] under its operation symbol.
23pub struct OpenAiGatewayLib;
24
25impl Lib for OpenAiGatewayLib {
26    fn manifest(&self) -> LibManifest {
27        LibManifest {
28            id: manifest_name(),
29            version: Version(env!("CARGO_PKG_VERSION").to_owned()),
30            abi: AbiVersion { major: 0, minor: 1 },
31            target: LibTarget::HostRegistered,
32            requires: Vec::new(),
33            capabilities: Vec::new(),
34            exports: openai_gateway_exports(),
35        }
36    }
37
38    fn load(&self, cx: &mut sim_kernel::LoadCx, linker: &mut Linker<'_>) -> Result<()> {
39        install_openai_codec(linker)?;
40        for function in [
41            OpenAiGatewayFunction::serve(),
42            OpenAiGatewayFunction::health(),
43            OpenAiGatewayFunction::models(),
44            OpenAiGatewayFunction::plan_parse(),
45            OpenAiGatewayFunction::plan_check(),
46            OpenAiGatewayFunction::plan_run(),
47            OpenAiGatewayFunction::plan_explain(),
48            OpenAiGatewayFunction::plan_combinators(),
49            OpenAiGatewayFunction::fabric(),
50            OpenAiGatewayFunction::key_add(),
51            OpenAiGatewayFunction::key_list(),
52            OpenAiGatewayFunction::runs(),
53            OpenAiGatewayFunction::run_get(),
54            OpenAiGatewayFunction::events(),
55            OpenAiGatewayFunction::storage_stats(),
56            OpenAiGatewayFunction::model_health(),
57            OpenAiGatewayFunction::cache_stats(),
58            OpenAiGatewayFunction::capability_report(),
59        ] {
60            linker.function_value(function.symbol(), cx.factory().opaque(Arc::new(function))?)?;
61        }
62        Ok(())
63    }
64}
65
66/// Returns the library's manifest id symbol, `sim/openai-gateway`.
67pub fn manifest_name() -> Symbol {
68    Symbol::qualified("sim", OPENAI_GATEWAY_LIB_ID)
69}
70
71/// Returns the full export list for the gateway library: every operation
72/// function symbol plus the OpenAI codec.
73pub fn openai_gateway_exports() -> Vec<Export> {
74    let mut exports = [
75        serve_symbol(),
76        health_symbol(),
77        models_symbol(),
78        plan_parse_symbol(),
79        plan_check_symbol(),
80        plan_run_symbol(),
81        plan_explain_symbol(),
82        plan_combinators_symbol(),
83        fabric_symbol(),
84        key_add_symbol(),
85        key_list_symbol(),
86        runs_symbol(),
87        run_get_symbol(),
88        events_symbol(),
89        storage_stats_symbol(),
90        model_health_symbol(),
91        cache_stats_symbol(),
92        capability_report_symbol(),
93    ]
94    .into_iter()
95    .map(|symbol| Export::Function {
96        symbol,
97        function_id: None,
98    })
99    .collect::<Vec<_>>();
100    exports.push(Export::Codec {
101        symbol: openai_codec_symbol(),
102        codec_id: None,
103    });
104    exports
105}