yomo 2.0.2

A QUIC-based runtime for AI-LLM tool routing and serverless execution
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
use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;
use futures_core::Stream;

use crate::llm_provider::Provider;
use crate::llm_provider::ProviderError;
use crate::llm_provider::UnifiedEvent;
use crate::llm_provider::UnifiedResponse;
use crate::llm_provider::openai::build_openai_provider;
use crate::llm_provider::openai_compatible::build_openai_compatible_provider;
use crate::llm_provider::selection::SelectionError;
use crate::llm_provider::selection::SelectionStrategy;
use crate::llm_provider::tokenhub::build_tokenhub_provider;
use crate::llm_provider::vertexai::build_vertexai_provider;
use crate::llm_provider::vllm_deepseek::build_vllm_deepseek_provider;
use crate::openai_types::ChatCompletionRequest;
use crate::provider_error_notifier::{ProviderErrorEvent, ProviderErrorNotifier};
use crate::serve_config::ConfigError;
use crate::serve_config::ProviderConfig;

#[derive(Clone)]
pub struct ProviderEntry {
    pub provider_type: String,
    pub model_id: String,
    pub label: Option<String>,
    pub provider: Arc<dyn Provider>,
}

#[derive(Clone)]
pub struct ProviderRegistry<M> {
    providers: HashMap<String, ProviderEntry>,
    strategy: Arc<dyn SelectionStrategy<M>>,
    error_notifier: Option<Arc<dyn ProviderErrorNotifier<M>>>,
}

impl<M> ProviderRegistry<M> {
    pub fn from_providers(
        providers: &[ProviderConfig],
        strategy: Arc<dyn SelectionStrategy<M>>,
    ) -> Result<Self, ConfigError> {
        let mut registry: HashMap<String, ProviderEntry> = HashMap::new();
        let mut model_ids = std::collections::HashSet::new();
        for item in providers {
            if item.provider_type.trim().is_empty() {
                return Err(ConfigError::InvalidProvider(format!(
                    "provider type is required for {}",
                    item.model_id
                )));
            }
            if item.model_id.trim().is_empty() {
                return Err(ConfigError::InvalidProvider(
                    "model_id is required for provider".to_string(),
                ));
            }
            let normalized_model_id = item.model_id.to_ascii_lowercase();
            if !model_ids.insert(normalized_model_id) {
                return Err(ConfigError::InvalidProvider(format!(
                    "duplicate model_id: {}",
                    item.model_id
                )));
            }
        }

        for item in providers {
            let provider: Arc<dyn Provider> = match item.provider_type.as_str() {
                "openai" => Arc::new(build_openai_provider(&item.params)?),
                "openai-compatible" => Arc::new(build_openai_compatible_provider(&item.params)?),
                "tokenhub" => Arc::new(build_tokenhub_provider(&item.params)?),
                "vllm_deepseek" => Arc::new(build_vllm_deepseek_provider(&item.params)?),
                "vertexai" => Arc::new(build_vertexai_provider(&item.params)?),
                other => return Err(ConfigError::UnknownProviderType(other.to_string())),
            };

            let entry = ProviderEntry {
                provider_type: item.provider_type.clone(),
                model_id: item.model_id.clone(),
                label: item.label.clone(),
                provider,
            };
            registry.insert(item.model_id.clone(), entry);
        }

        Ok(Self::new(registry, strategy))
    }

    pub fn new(
        providers: HashMap<String, ProviderEntry>,
        strategy: Arc<dyn SelectionStrategy<M>>,
    ) -> Self {
        Self {
            providers,
            strategy,
            error_notifier: None,
        }
    }

    pub fn with_error_notifier(mut self, notifier: Arc<dyn ProviderErrorNotifier<M>>) -> Self {
        self.error_notifier = Some(notifier);
        self
    }

    pub fn select(
        &self,
        model_id: Option<&str>,
        metadata: &M,
    ) -> Result<ProviderEntry, SelectionError>
    where
        M: Clone + Send + Sync + 'static,
    {
        let selected = self
            .strategy
            .select(model_id, metadata)
            .map_err(|err| err)?;
        let mut provider = self
            .providers
            .values()
            .find(|provider| {
                provider.model_id.to_ascii_lowercase() == selected.model_id.to_ascii_lowercase()
            })
            .cloned()
            .ok_or(SelectionError::ModelNotSupported)?;
        if let Some(notifier) = &self.error_notifier {
            provider.provider = Arc::new(HookedProvider {
                inner: Arc::clone(&provider.provider),
                error_notifier: Arc::clone(notifier),
                metadata: metadata.clone(),
                model_id: provider.model_id.clone(),
                endpoint: "/v1/chat/completions".to_string(),
            });
        }
        Ok(provider)
    }

    pub fn providers(&self) -> &HashMap<String, ProviderEntry> {
        &self.providers
    }

    pub fn model_list(&self) -> Vec<String> {
        self.providers
            .values()
            .map(|provider| provider.model_id.clone())
            .collect()
    }
}

#[derive(Clone)]
struct HookedProvider<M> {
    inner: Arc<dyn Provider>,
    error_notifier: Arc<dyn ProviderErrorNotifier<M>>,
    metadata: M,
    model_id: String,
    endpoint: String,
}

#[async_trait]
impl<M> Provider for HookedProvider<M>
where
    M: Clone + Send + Sync + 'static,
{
    fn model_id(&self) -> &str {
        self.inner.model_id()
    }

    async fn complete(
        &self,
        request: ChatCompletionRequest,
    ) -> Result<UnifiedResponse, ProviderError> {
        self.inner
            .complete(request)
            .await
            .map_err(|err| self.notify_error(err))
    }

    async fn stream<'a>(
        &'a self,
        request: ChatCompletionRequest,
    ) -> Result<
        std::pin::Pin<Box<dyn Stream<Item = Result<UnifiedEvent, ProviderError>> + Send + 'a>>,
        ProviderError,
    > {
        self.inner
            .stream(request)
            .await
            .map_err(|err| self.notify_error(err))
    }
}

impl<M> HookedProvider<M>
where
    M: Clone + Send + Sync + 'static,
{
    fn notify_error(&self, err: ProviderError) -> ProviderError {
        let http_status = match &err {
            ProviderError::Public { status, .. } => Some(status.as_u16()),
            ProviderError::Internal {
                upstream_http_status,
                ..
            } => Some(upstream_http_status.as_u16()),
        };
        self.error_notifier
            .notify_provider_error(ProviderErrorEvent {
                model: Some(self.model_id.clone()),
                metadata: self.metadata.clone(),
                http_status,
                error: err.to_string(),
                endpoint: Some(self.endpoint.clone()),
            });
        err
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::pin::Pin;
    use std::sync::Arc;

    use async_trait::async_trait;
    use axum::http::StatusCode;
    use futures_core::Stream;

    use super::{ProviderEntry, ProviderRegistry};
    use crate::llm_provider::selection::{ByModel, SelectionStrategy};
    use crate::llm_provider::{Provider, ProviderError, UnifiedEvent, UnifiedResponse};
    use crate::openai_types::{ChatCompletionRequest, ErrorDetail};
    use std::sync::Mutex;

    use crate::provider_error_notifier::{ProviderErrorEvent, ProviderErrorNotifier};

    struct FailingProvider;

    struct InternalFailingProvider;

    #[async_trait]
    impl Provider for FailingProvider {
        fn model_id(&self) -> &str {
            "demo-model"
        }

        async fn complete(
            &self,
            _request: ChatCompletionRequest,
        ) -> Result<UnifiedResponse, ProviderError> {
            Err(ProviderError::Public {
                status: StatusCode::PAYMENT_REQUIRED,
                error: ErrorDetail {
                    message: "upstream_402".to_string(),
                    r#type: "invalid_request_error".to_string(),
                    code: None,
                    param: None,
                },
            })
        }

        async fn stream<'a>(
            &'a self,
            _request: ChatCompletionRequest,
        ) -> Result<
            Pin<Box<dyn Stream<Item = Result<UnifiedEvent, ProviderError>> + Send + 'a>>,
            ProviderError,
        > {
            Err(ProviderError::internal("stream_fail".to_string()))
        }
    }

    #[async_trait]
    impl Provider for InternalFailingProvider {
        fn model_id(&self) -> &str {
            "demo-model"
        }

        async fn complete(
            &self,
            _request: ChatCompletionRequest,
        ) -> Result<UnifiedResponse, ProviderError> {
            Err(ProviderError::internal_with_upstream_status(
                StatusCode::PAYMENT_REQUIRED,
                "upstream_402",
            ))
        }

        async fn stream<'a>(
            &'a self,
            _request: ChatCompletionRequest,
        ) -> Result<
            Pin<Box<dyn Stream<Item = Result<UnifiedEvent, ProviderError>> + Send + 'a>>,
            ProviderError,
        > {
            Err(ProviderError::internal("stream_fail".to_string()))
        }
    }

    struct CollectNotifier {
        calls: Mutex<Vec<ProviderErrorEvent<()>>>,
    }

    impl ProviderErrorNotifier<()> for CollectNotifier {
        fn notify_provider_error(&self, event: ProviderErrorEvent<()>) {
            self.calls.lock().expect("lock notifier calls").push(event);
        }
    }

    #[tokio::test]
    async fn provider_registry_error_notifier_receives_complete_error() {
        let mut providers = HashMap::new();
        providers.insert(
            "demo-model".to_string(),
            ProviderEntry {
                provider_type: "openai-compatible".to_string(),
                model_id: "demo-model".to_string(),
                label: Some("demo".to_string()),
                provider: Arc::new(FailingProvider),
            },
        );
        let notifier = Arc::new(CollectNotifier {
            calls: Mutex::new(Vec::new()),
        });
        let strategy: Arc<dyn SelectionStrategy<()>> = Arc::new(ByModel);
        let registry = ProviderRegistry::new(providers, strategy)
            .with_error_notifier(Arc::clone(&notifier) as Arc<dyn ProviderErrorNotifier<()>>);

        let entry = registry
            .select(Some("demo-model"), &())
            .expect("select provider");

        let err = entry
            .provider
            .complete(empty_request())
            .await
            .expect_err("provider should fail");
        match err {
            ProviderError::Public { status, .. } => {
                assert_eq!(status, StatusCode::PAYMENT_REQUIRED)
            }
            other => panic!("unexpected error: {other}"),
        }
        let calls = notifier.calls.lock().expect("lock calls");
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].model.as_deref(), Some("demo-model"));
        assert_eq!(
            calls[0].http_status,
            Some(StatusCode::PAYMENT_REQUIRED.as_u16())
        );
        assert_eq!(calls[0].endpoint.as_deref(), Some("/v1/chat/completions"));
    }

    #[tokio::test]
    async fn provider_registry_error_notifier_receives_internal_upstream_status() {
        let mut providers = HashMap::new();
        providers.insert(
            "demo-model".to_string(),
            ProviderEntry {
                provider_type: "openai-compatible".to_string(),
                model_id: "demo-model".to_string(),
                label: Some("demo".to_string()),
                provider: Arc::new(InternalFailingProvider),
            },
        );
        let notifier = Arc::new(CollectNotifier {
            calls: Mutex::new(Vec::new()),
        });
        let strategy: Arc<dyn SelectionStrategy<()>> = Arc::new(ByModel);
        let registry = ProviderRegistry::new(providers, strategy)
            .with_error_notifier(Arc::clone(&notifier) as Arc<dyn ProviderErrorNotifier<()>>);

        let entry = registry
            .select(Some("demo-model"), &())
            .expect("select provider");

        let err = entry
            .provider
            .complete(empty_request())
            .await
            .expect_err("provider should fail");
        match err {
            ProviderError::Internal {
                upstream_http_status,
                ..
            } => {
                assert_eq!(upstream_http_status, StatusCode::PAYMENT_REQUIRED)
            }
            other => panic!("unexpected error: {other}"),
        }

        let calls = notifier.calls.lock().expect("lock calls");
        assert_eq!(calls.len(), 1);
        assert_eq!(
            calls[0].http_status,
            Some(StatusCode::PAYMENT_REQUIRED.as_u16())
        );
    }

    fn empty_request() -> ChatCompletionRequest {
        ChatCompletionRequest {
            model: "demo-model".to_string(),
            messages: Vec::new(),
            n: None,
            temperature: None,
            top_p: None,
            presence_penalty: None,
            frequency_penalty: None,
            logprobs: None,
            top_logprobs: None,
            modalities: None,
            audio: None,
            max_completion_tokens: None,
            stop: None,
            response_format: None,
            reasoning_effort: None,
            chat_template_kwargs: None,
            prediction: None,
            verbosity: None,
            tools: None,
            tool_choice: None,
            allowed_tools: None,
            parallel_tool_calls: None,
            service_tier: None,
            seed: None,
            stream: None,
            stream_options: None,
            metadata: None,
            agent_context: None,
            thinking: None,
        }
    }
}