Skip to main content

sim_lib_openai_server/plan/
address.rs

1use sim_kernel::{Error, Result, Symbol};
2
3/// Resolved description of a plan atom address, identifying the backend that
4/// should serve it.
5#[derive(Clone, Debug, PartialEq, Eq)]
6pub struct BackendDescriptor {
7    /// Full atom address (for example `openai/gpt-4o`).
8    pub address: String,
9    /// Leading address segment naming the backend family (for example `openai`).
10    pub head: String,
11    /// Runner symbol derived from the full address.
12    pub runner: Symbol,
13    /// Whether the address designates a built-in fixture backend.
14    pub fixture: bool,
15}
16
17impl BackendDescriptor {
18    /// Returns `true` when this atom should dispatch through a registered runner.
19    pub fn is_runner_backed(&self) -> bool {
20        !self.fixture && self.head != "gateway"
21    }
22
23    /// Returns `true` when this atom should dispatch through gateway federation.
24    pub fn is_gateway(&self) -> bool {
25        self.head == "gateway"
26    }
27}
28
29/// Resolves an atom address into a [`BackendDescriptor`], erroring when the
30/// backend head is unknown.
31pub fn resolve_atom_address(address: &str) -> Result<BackendDescriptor> {
32    let Some((head, _)) = address.split_once('/') else {
33        return Err(model_not_found(address));
34    };
35    if !KNOWN_PROVIDER_PREFIXES.contains(&head) {
36        return Err(model_not_found(address));
37    }
38    Ok(BackendDescriptor {
39        address: address.to_owned(),
40        head: head.to_owned(),
41        runner: Symbol::new(address.to_owned()),
42        fixture: head == "fixture",
43    })
44}
45
46fn model_not_found(address: &str) -> Error {
47    Error::Eval(format!("model_not_found: {address}"))
48}
49
50const KNOWN_PROVIDER_PREFIXES: &[&str] = &[
51    "openai",
52    "anthropic",
53    "ollama",
54    "lm-studio",
55    "lemonade",
56    "process",
57    "runner",
58    "agent",
59    "skill",
60    "sim",
61    "fixture",
62    "gateway",
63];
64
65/// Returns the open set of accepted gateway plan address prefixes.
66pub fn provider_prefixes() -> &'static [&'static str] {
67    KNOWN_PROVIDER_PREFIXES
68}