-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlib.rs
More file actions
1118 lines (1025 loc) · 43.6 KB
/
Copy pathlib.rs
File metadata and controls
1118 lines (1025 loc) · 43.6 KB
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
//! aimux-python: Python binding (PyO3, native path).
//!
//! Directly uses aimux-providers, bypassing aimux-ffi.
//! PyO3 maps Rust async to Python via a tokio runtime + async generator.
// pyo3 0.22 macros generate unsafe-op-in-unsafe-fn calls that trigger
// edition-2024 lint. Suppress until pyo3 0.23+ lands.
#![allow(unsafe_op_in_unsafe_fn)]
mod error;
mod multimodal;
pub use multimodal::*;
use std::sync::Arc;
use crate::error::{
AiMuxBindingError, BindingError, binding_py_err, serialize_result, to_py_err, wire_json,
};
use aimux_core::AiMuxError;
use aimux_core::generate::{
GenerateTextOptions, generate_object, generate_text, generate_text_as_openai, stream_text,
stream_text_as_openai,
};
use aimux_core::language_model::LanguageModel;
use aimux_core::message::ModelPrompt;
use aimux_core::openai_output::OpenAiStreamOptions;
use pyo3::prelude::*;
// ─────────────────────────────────────────────────────────────────────────────
// Global tokio runtime
// ─────────────────────────────────────────────────────────────────────────────
pub(crate) fn runtime() -> &'static tokio::runtime::Runtime {
use std::sync::OnceLock;
static RT: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
RT.get_or_init(|| tokio::runtime::Runtime::new().expect("failed to build tokio runtime"))
}
// ─────────────────────────────────────────────────────────────────────────────
// Model — a provider model instance accessible from Python.
// ─────────────────────────────────────────────────────────────────────────────
#[pyclass]
struct Model {
inner: Arc<dyn LanguageModel>,
/// Probe store — `Some` only for traced models (RFC-0015).
trace_store: Option<Arc<aimux_core::trace::RingTraceStore>>,
}
#[pymethods]
impl Model {
/// Wrap this model in a cache-probe layer (RFC-0015) WITHOUT an
/// auditor (records fingerprints only; verdicts stay None).
fn trace(&self) -> PyResult<Model> {
let store = Arc::new(aimux_core::trace::RingTraceStore::new());
let layer = aimux_core::trace::TraceLayer::new(self.inner.clone(), store.clone());
Ok(Model {
inner: Arc::new(layer),
trace_store: Some(store),
})
}
/// Wrap this model in a probe layer with the built-in rules auditor.
/// `strict=True` = strict mode (self-hosted single instance);
/// `strict=False` = shared mode (safe default).
#[pyo3(signature = (strict=false))]
fn trace_audited(&self, strict: bool) -> PyResult<Model> {
let store = Arc::new(aimux_core::trace::RingTraceStore::new());
let layer = aimux_core::trace::TraceLayer::new(self.inner.clone(), store.clone())
.with_rules_auditor(strict);
Ok(Model {
inner: Arc::new(layer),
trace_store: Some(store),
})
}
/// Aggregated probe statistics (RFC-0015 §5.3), filtered by an optional
/// JSON `TraceFilter`. Returns a JSON `TraceStats[]` string.
#[pyo3(signature = (filter_json=None))]
fn trace_aggregate(&self, filter_json: Option<&str>) -> PyResult<String> {
let Some(store) = &self.trace_store else {
return Err(to_py_err(&AiMuxError::InvalidArgument(
"model is not traced; call trace() first".into(),
)));
};
let filter = match filter_json {
Some(f) => wire_json("filter_json", f)?,
None => Default::default(),
};
serialize_result(&store.aggregate(&filter))
}
/// One session's chain view. Returns a JSON `SessionChainView` string.
fn trace_session_chain(&self, session_id: &str) -> PyResult<String> {
let Some(store) = &self.trace_store else {
return Err(to_py_err(&AiMuxError::InvalidArgument(
"model is not traced; call trace() first".into(),
)));
};
let view = store
.session_chain(session_id)
.ok_or_else(|| to_py_err(&AiMuxError::InvalidArgument("unknown session".into())))?;
serialize_result(&view)
}
/// Export all probe records as JSONL (one `TraceRecord` per line).
fn trace_export_jsonl(&self) -> PyResult<String> {
let Some(store) = &self.trace_store else {
return Err(to_py_err(&AiMuxError::InvalidArgument(
"model is not traced; call trace() first".into(),
)));
};
let mut buf = Vec::new();
store.export_jsonl(&mut buf).map_err(|e| {
binding_py_err(&BindingError::ResultSerialization {
message: format!("export: {e}"),
})
})?;
String::from_utf8(buf).map_err(|e| {
binding_py_err(&BindingError::ResultSerialization {
message: format!("utf8: {e}"),
})
})
}
/// Clear all probe records of this traced model.
fn trace_clear(&self) {
if let Some(store) = &self.trace_store {
store.clear();
}
}
/// Generate text (non-streaming).
///
/// prompt_json: JSON string (bare prompt or {"prompt": ...})
/// opts_json: optional JSON-serialized GenerateTextOptions
/// Returns JSON-serialized GenerateTextResult.
#[pyo3(signature = (prompt_json, opts_json=None))]
fn generate_text(&self, prompt_json: &str, opts_json: Option<&str>) -> PyResult<String> {
let prompt = parse_prompt(prompt_json)?;
let opts = parse_opts(opts_json)?;
let rt = runtime();
let result = rt.block_on(async move { generate_text(&*self.inner, prompt, opts).await });
match result {
Ok(r) => serialize_result(&r),
Err(e) => Err(to_py_err(&e)),
}
}
/// Generate a structured JSON object from the model (M12, RFC-0016).
///
/// prompt_json: JSON string (bare prompt or {"prompt": ...})
/// opts_json: optional JSON-serialized GenerateTextOptions
/// Returns JSON-serialized GenerateObjectResult. Pass
/// `response_format: { "Json": { ... } }` via opts_json for schema
/// control; the function applies JSON repair before parsing.
#[pyo3(signature = (prompt_json, opts_json=None))]
fn generate_object(&self, prompt_json: &str, opts_json: Option<&str>) -> PyResult<String> {
let prompt = parse_prompt(prompt_json)?;
let opts = parse_opts(opts_json)?;
let rt = runtime();
let result = rt.block_on(async move { generate_object(&*self.inner, prompt, opts).await });
match result {
Ok(r) => serialize_result(&r),
Err(e) => Err(to_py_err(&e)),
}
}
/// Consume a stream to completion and return the aggregated result
/// (M11, RFC-0016).
///
/// prompt_json: JSON string (bare prompt or {"prompt": ...})
/// opts_json: optional JSON-serialized GenerateTextOptions
/// Returns JSON-serialized StreamTextResultAggregated.
#[pyo3(signature = (prompt_json, opts_json=None))]
fn consume_stream_text(&self, prompt_json: &str, opts_json: Option<&str>) -> PyResult<String> {
let prompt = parse_prompt(prompt_json)?;
let opts = parse_opts(opts_json)?;
let rt = runtime();
let result = rt.block_on(async move {
let stream_result = stream_text(&*self.inner, prompt, opts).await?;
stream_result.consume().await
});
match result {
Ok(r) => serialize_result(&r),
Err(e) => Err(to_py_err(&e)),
}
}
/// Stream text from the model.
///
/// Returns a StreamIterator that yields StreamPart JSON strings.
#[pyo3(signature = (prompt_json, opts_json=None))]
fn stream_text(&self, prompt_json: &str, opts_json: Option<&str>) -> PyResult<StreamIterator> {
let prompt = parse_prompt(prompt_json)?;
let opts = parse_opts(opts_json)?;
let model = self.inner.clone();
let (tx, rx) =
tokio::sync::mpsc::channel::<Result<String, crate::error::AiMuxBindingError>>(64);
rt_spawn(async move {
match stream_text(&*model, prompt, opts).await {
Ok(stream_result) => {
use futures::StreamExt;
let mut stream = stream_result.stream;
while let Some(item) = stream.next().await {
match item {
Ok(part) => {
// A part that cannot be serialized ends the stream with the
// binding's ResultSerialization — never a silent "{}".
let json = match serde_json::to_string(&part) {
Ok(j) => j,
Err(e) => {
let _ = tx
.send(Err(
crate::error::BindingError::ResultSerialization {
message: format!("stream part: {e}"),
}
.into(),
))
.await;
break;
}
};
if tx.send(Ok(json)).await.is_err() {
break;
}
}
Err(e) => {
let _ = tx.send(Err(e.into())).await;
break;
}
}
}
}
Err(e) => {
let _ = tx.send(Err(e.into())).await;
}
}
});
Ok(StreamIterator { rx })
}
/// Generate text as an OpenAI Chat Completion (non-streaming, RFC-0026).
///
/// prompt_json: JSON string (bare prompt or {"prompt": ...})
/// opts_json: optional JSON-serialized GenerateTextOptions
/// Returns JSON-serialized ChatCompletion (OpenAI `chat.completion` object).
#[pyo3(signature = (prompt_json, opts_json=None))]
fn generate_text_as_openai(
&self,
prompt_json: &str,
opts_json: Option<&str>,
) -> PyResult<String> {
let prompt = parse_prompt(prompt_json)?;
let opts = parse_opts(opts_json)?;
let rt = runtime();
let result =
rt.block_on(async move { generate_text_as_openai(&*self.inner, prompt, opts).await });
match result {
Ok(r) => serialize_result(&r),
Err(e) => Err(to_py_err(&e)),
}
}
/// Stream text as OpenAI Chat Completion chunks (RFC-0026).
///
/// Returns a StreamIterator that yields ChatCompletionChunk JSON strings.
/// Stream options (`include_usage`, `include_reasoning`) are read from
/// `opts.provider_options.openai.stream_options` (both default to `true`).
#[pyo3(signature = (prompt_json, opts_json=None))]
fn stream_text_as_openai(
&self,
prompt_json: &str,
opts_json: Option<&str>,
) -> PyResult<StreamIterator> {
let prompt = parse_prompt(prompt_json)?;
let opts = parse_opts(opts_json)?;
let model = self.inner.clone();
// Extract OpenAI stream options from opts.provider_options.openai.stream_options
// (same logic as aimux-ffi's stream_text_as_openai_with_signal).
let stream_options = opts
.provider_options
.as_ref()
.and_then(|po| po.get("openai"))
.and_then(|o| o.get("stream_options"))
.cloned()
.map(|v| OpenAiStreamOptions {
include_usage: v
.get("include_usage")
.and_then(|b| b.as_bool())
.unwrap_or(true),
include_reasoning: v
.get("include_reasoning")
.and_then(|b| b.as_bool())
.unwrap_or(true),
})
.unwrap_or_default();
let (tx, rx) =
tokio::sync::mpsc::channel::<Result<String, crate::error::AiMuxBindingError>>(64);
rt_spawn(async move {
match stream_text_as_openai(&*model, prompt, opts, stream_options).await {
Ok(stream_result) => {
use futures::StreamExt;
let mut stream = stream_result.stream;
while let Some(item) = stream.next().await {
match item {
Ok(chunk) => {
let json = match serde_json::to_string(&chunk) {
Ok(j) => j,
Err(e) => {
let _ = tx
.send(Err(
crate::error::BindingError::ResultSerialization {
message: format!("stream chunk: {e}"),
}
.into(),
))
.await;
break;
}
};
if tx.send(Ok(json)).await.is_err() {
break;
}
}
Err(e) => {
let _ = tx.send(Err(e.into())).await;
break;
}
}
}
}
Err(e) => {
let _ = tx.send(Err(e.into())).await;
}
}
});
Ok(StreamIterator { rx })
}
}
/// Python iterator that yields StreamPart JSON strings from a tokio channel.
#[pyclass]
struct StreamIterator {
rx: tokio::sync::mpsc::Receiver<Result<String, crate::error::AiMuxBindingError>>,
}
#[pymethods]
impl StreamIterator {
fn __iter__(slf: Py<Self>) -> Py<Self> {
slf
}
fn __next__(&mut self, py: Python<'_>) -> PyResult<Option<PyObject>> {
// Block on the next channel item, allowing other Python threads to run.
let item = py.allow_threads(|| runtime().block_on(self.rx.recv()));
match item {
Some(Ok(json)) => Ok(Some(json.to_object(py).into())),
Some(Err(f)) => Err(f.to_py_err()),
None => Ok(None), // stream finished
}
}
}
// ─────────────────────────────────────────────────────────────────────────────
// Provider constructors
// ─────────────────────────────────────────────────────────────────────────────
/// Create an OpenAI model instance.
#[pyfunction]
#[pyo3(signature = (api_key, model_id, base_url=None))]
fn openai(api_key: &str, model_id: &str, base_url: Option<&str>) -> PyResult<Model> {
use aimux_core::provider::Provider;
use aimux_providers::openai::{OpenAIConfig, OpenAIProvider};
let mut config = OpenAIConfig::new(api_key);
if let Some(url) = base_url {
config = config.with_base_url(url);
}
let provider = OpenAIProvider::new(config);
let model = provider
.language_model(model_id)
.map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(model),
trace_store: None,
})
}
/// Create an Anthropic model instance.
#[pyfunction]
#[pyo3(signature = (api_key, model_id, base_url=None))]
fn anthropic(api_key: &str, model_id: &str, base_url: Option<&str>) -> PyResult<Model> {
use aimux_core::provider::Provider;
use aimux_providers::anthropic::{AnthropicConfig, AnthropicProvider};
let mut config = AnthropicConfig::new(api_key);
if let Some(url) = base_url {
config = config.with_base_url(url);
}
let provider = AnthropicProvider::new(config);
let model = provider
.language_model(model_id)
.map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(model),
trace_store: None,
})
}
/// Create a DeepSeek model instance (registry-backed since RFC-0017 phase 4).
#[pyfunction]
#[pyo3(signature = (api_key, model_id, base_url=None))]
fn deepseek(api_key: &str, model_id: &str, base_url: Option<&str>) -> PyResult<Model> {
let options = base_url.map(|url| aimux_providers::ProviderOptions {
base_url: Some(url.to_string()),
..Default::default()
});
let model = aimux_providers::provider("deepseek", Some(api_key.to_string()), model_id, options)
.map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(model),
trace_store: None,
})
}
/// Create a Google Gemini language model instance (native `generateContent`
/// protocol — not OpenAI-compatible, so not registry-backed).
#[pyfunction]
#[pyo3(signature = (api_key, model_id, base_url=None))]
fn google(api_key: &str, model_id: &str, base_url: Option<&str>) -> PyResult<Model> {
use aimux_core::provider::Provider;
use aimux_providers::google::{GoogleConfig, GoogleProvider};
let mut config = GoogleConfig::new(api_key);
if let Some(url) = base_url {
config = config.with_base_url(url);
}
let provider = GoogleProvider::new(config);
let model = provider
.language_model(model_id)
.map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(model),
trace_store: None,
})
}
/// Create a Cohere language model instance.
#[pyfunction]
#[pyo3(signature = (api_key, model_id, base_url=None))]
fn cohere(api_key: &str, model_id: &str, base_url: Option<&str>) -> PyResult<Model> {
use aimux_core::provider::Provider;
use aimux_providers::cohere::{CohereConfig, CohereProvider};
let mut config = CohereConfig::new(api_key);
if let Some(url) = base_url {
config = config.with_base_url(url);
}
let provider = CohereProvider::new(config);
let model = provider
.language_model(model_id)
.map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(model),
trace_store: None,
})
}
/// Create a Mistral language model instance.
#[pyfunction]
#[pyo3(signature = (api_key, model_id, base_url=None))]
fn mistral(api_key: &str, model_id: &str, base_url: Option<&str>) -> PyResult<Model> {
use aimux_core::provider::Provider;
use aimux_providers::mistral::{MistralConfig, MistralProvider};
let mut config = MistralConfig::new(api_key);
if let Some(url) = base_url {
config = config.with_base_url(url);
}
let provider = MistralProvider::new(config);
let model = provider
.language_model(model_id)
.map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(model),
trace_store: None,
})
}
/// Create an xAI language model instance.
#[pyfunction]
#[pyo3(signature = (api_key, model_id, base_url=None))]
fn xai(api_key: &str, model_id: &str, base_url: Option<&str>) -> PyResult<Model> {
use aimux_core::provider::Provider;
use aimux_providers::xai::{XAIConfig, XAIProvider};
let mut config = XAIConfig::new(api_key);
if let Some(url) = base_url {
config = config.with_base_url(url);
}
let provider = XAIProvider::new(config);
let model = provider
.language_model(model_id)
.map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(model),
trace_store: None,
})
}
/// Create a Bedrock language model instance (AWS SigV4 credentials).
#[pyfunction]
#[pyo3(signature = (access_key_id, secret_access_key, region, model_id, base_url=None))]
fn bedrock(
access_key_id: &str,
secret_access_key: &str,
region: &str,
model_id: &str,
base_url: Option<&str>,
) -> PyResult<Model> {
use aimux_core::provider::Provider;
use aimux_providers::bedrock::{BedrockProvider, BedrockProviderConfig};
let mut config = BedrockProviderConfig::new(access_key_id, secret_access_key, region);
if let Some(url) = base_url {
config = config.with_base_url(url);
}
let provider = BedrockProvider::new(config);
let model = provider
.language_model(model_id)
.map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(model),
trace_store: None,
})
}
/// Create a Vertex AI language model instance (GCP bearer token).
#[pyfunction]
#[pyo3(signature = (access_token, project, location, model_id, base_url=None))]
fn vertex(
access_token: &str,
project: &str,
location: &str,
model_id: &str,
base_url: Option<&str>,
) -> PyResult<Model> {
use aimux_core::provider::Provider;
use aimux_providers::vertex::{VertexProvider, VertexProviderConfig};
let mut config = VertexProviderConfig::new(access_token, project, location);
if let Some(url) = base_url {
config = config.with_base_url(url);
}
let provider = VertexProvider::new(config);
let model = provider
.language_model(model_id)
.map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(model),
trace_store: None,
})
}
/// Create an Anthropic-on-AWS language model instance (API key + region).
#[pyfunction]
#[pyo3(signature = (api_key, region, model_id, base_url=None))]
fn anthropic_aws(
api_key: &str,
region: &str,
model_id: &str,
base_url: Option<&str>,
) -> PyResult<Model> {
use aimux_core::provider::Provider;
use aimux_providers::anthropic_aws::{AnthropicAwsProvider, AnthropicAwsProviderConfig};
let mut config = AnthropicAwsProviderConfig::with_api_key(api_key, region);
if let Some(url) = base_url {
config = config.with_base_url(url);
}
let provider = AnthropicAwsProvider::new(config);
let model = provider
.language_model(model_id)
.map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(model),
trace_store: None,
})
}
/// Create an Azure OpenAI language model instance (API key + resource name).
///
/// The deployment name is passed as `model_id`; `api_version` is optional.
#[pyfunction]
#[pyo3(signature = (api_key, resource_name, deployment, api_version=None, base_url=None))]
fn azure(
api_key: &str,
resource_name: &str,
deployment: &str,
api_version: Option<&str>,
base_url: Option<&str>,
) -> PyResult<Model> {
use aimux_core::provider::Provider;
use aimux_providers::azure::{AzureConfig, AzureProvider};
let mut config = AzureConfig::new().with_api_key(api_key);
if let Some(url) = base_url {
config = config.with_base_url(url);
}
if let Some(version) = api_version {
if !version.is_empty() {
config = config.with_api_version(version);
}
}
if !resource_name.is_empty() {
config = config.with_resource_name(resource_name);
}
let provider = AzureProvider::new(config).map_err(|e| to_py_err(&e))?;
let model = provider
.language_model(deployment)
.map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(model),
trace_store: None,
})
}
/// Create a language model from the built-in registry by provider name
/// (RFC-0017 phase 4). `api_key=None` reads the provider's env var.
/// `config_json` is a serialized `ProviderOptions` object (`base_url` /
/// `headers` / `organization` / `project` / `max_retries` /
/// `body_overrides`); the `base_url` parameter wins over the JSON field.
#[pyfunction]
#[pyo3(signature = (name, api_key, model_id, base_url=None, config_json=None))]
fn provider(
name: &str,
api_key: Option<String>,
model_id: &str,
base_url: Option<&str>,
config_json: Option<&str>,
) -> PyResult<Model> {
let mut options: Option<aimux_providers::ProviderOptions> = match config_json {
Some(s) if !s.trim().is_empty() && s.trim() != "null" => Some(wire_json("config_json", s)?),
_ => None,
};
if let Some(url) = base_url {
options.get_or_insert_with(Default::default).base_url = Some(url.to_string());
}
let model =
aimux_providers::provider(name, api_key, model_id, options).map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(model),
trace_store: None,
})
}
// ─────────────────────────────────────────────────────────────────────────────
// Provider handles (RFC-0027) — createProvider / listModels / model
// ─────────────────────────────────────────────────────────────────────────────
/// A provider handle — created by `create_provider`, supports `list_models()`
/// (runtime discovery) and `model()` (build a model from a discovered id).
#[pyclass]
struct ProviderHandle {
inner: Arc<dyn aimux_core::provider::Provider>,
}
#[pymethods]
impl ProviderHandle {
/// List models available on this provider (runtime discovery + anya2a spec).
/// Returns a JSON array of RuntimeModel.
fn list_models(&self) -> PyResult<String> {
let rt = runtime();
let models = rt
.block_on(async { self.inner.list_models().await })
.map_err(|e| to_py_err(&e))?;
serialize_result(&models)
}
/// Build a language model from a discovered model id.
fn model(&self, model_id: &str) -> PyResult<Model> {
let m = self
.inner
.language_model(model_id)
.map_err(|e| to_py_err(&e))?;
Ok(Model {
inner: Arc::from(m),
trace_store: None,
})
}
}
/// Create a **provider handle** (RFC-0027) for a registry-backed provider.
///
/// Unlike `provider()` (which binds to a single model_id), this returns a
/// `ProviderHandle` that supports `list_models()` and `model()`.
#[pyfunction]
#[pyo3(signature = (name, api_key=None, base_url=None, config_json=None))]
fn create_provider(
name: &str,
api_key: Option<String>,
base_url: Option<&str>,
config_json: Option<&str>,
) -> PyResult<ProviderHandle> {
let mut options: Option<aimux_providers::ProviderOptions> = match config_json {
Some(s) if !s.trim().is_empty() && s.trim() != "null" => Some(wire_json("config_json", s)?),
_ => None,
};
if let Some(url) = base_url {
options.get_or_insert_with(Default::default).base_url = Some(url.to_string());
}
let p = aimux_providers::provider_handle(name, api_key, options).map_err(|e| to_py_err(&e))?;
Ok(ProviderHandle {
inner: Arc::from(p),
})
}
/// Fetch the community model catalogue (RFC-0027) and return it as a JSON
/// string (serialized `Catalogue`). `source_url` defaults to the anya2a
/// `dist/all.json`.
#[pyfunction]
fn get_model_specs(source_url: Option<&str>) -> PyResult<String> {
let catalogue = runtime()
.block_on(async { aimux_providers::get_model_specs(source_url).await })
.map_err(|e| to_py_err(&e))?;
serialize_result(&catalogue)
}
// ─────────────────────────────────────────────────────────────────────────────
// Module
// ─────────────────────────────────────────────────────────────────────────────
/// 初始化全局日志(RFC-0014)。幂等:多次调用无副作用;宿主已自建
/// subscriber 时 no-op。级别:off|error|warn|info|debug|trace(空串回退
/// warn)。`AIMUX_LOG` / `AIMUX_LOG_LEVEL` 环境变量优先级更高。
/// 日志输出到 stderr。
#[pyfunction]
fn init_logging(level: &str) {
let level = if level.trim().is_empty() {
"warn"
} else {
level
};
aimux_providers::init_logging(level);
}
/// Register external OpenAI-compatible providers from a JSON config string
/// (RFC-0020). Entries override same-named built-ins or add new ones.
///
/// `config_json` shape: `{ "providers": [ { "name": "...", "base_url": "...", ... } ] }`.
/// Malformed JSON text raises `ValueError`; a well-formed document the
/// registry rejects (bad base_url scheme, empty name, unsupported protocol,
/// wrong shape) raises `InvalidArgumentError`.
#[pyfunction]
fn register_providers(config_json: &str) -> PyResult<()> {
let _: serde_json::Value = wire_json("config_json", config_json)?;
aimux_providers::load_providers_from_json(config_json).map_err(|e| match e {
AiMuxError::JsonParse(m) => {
to_py_err(&AiMuxError::InvalidArgument(format!("config_json: {m}")))
}
e => to_py_err(&e),
})
}
/// Set the global proxy configuration (M6, RFC-0016). Must be called before the
/// first `generate_text` / `stream_text` call; a no-op if the shared HTTP
/// client is already initialised.
///
/// `config_json` shape: `{ "http_url": "...", "https_url": "...", "all_url":
/// "...", "no_proxy": "..." }` (all fields optional). Raises `ValueError` on
/// malformed JSON, `AimuxError` on a bad value.
#[pyfunction]
fn init_proxy(config: &str) -> PyResult<()> {
let proxy_config: aimux_provider_utils::ProxyConfig = wire_json("config", config)?;
// `init_proxy` returns false when the shared client is already up; treat
// that as success (idempotent).
let _ = aimux_provider_utils::init_proxy(proxy_config);
Ok(())
}
/// Register the global session store (RFC-0024). Replaces any previous one.
/// Until called, calls are not grouped and the query functions return empty
/// results.
#[pyfunction]
fn init_session_store() {
aimux_core::session::init_session_store(std::sync::Arc::new(
aimux_core::session::SessionStore::new(),
));
}
/// Enable/disable the global session inferer (RFC-0024, opt-in, off by
/// default). Explicit `session_id` values always win regardless.
#[pyfunction]
fn init_session_infer(enabled: bool) {
aimux_core::session::init_session_infer(enabled);
}
/// Query: all calls of a session (RFC-0024), as a JSON-serialized
/// `SessionCall[]` (ordered by step). Empty array if unknown / no store.
#[pyfunction]
fn session_calls(session_id: &str) -> PyResult<String> {
serialize_result(&aimux_core::session::session_calls(session_id))
}
/// Query: all known sessions (RFC-0024), as a JSON-serialized `SessionView[]`.
#[pyfunction]
fn list_sessions() -> PyResult<String> {
serialize_result(&aimux_core::session::list_sessions())
}
// ─────────────────────────────────────────────────────────────────────────────
// Recording + mock replay (RFC-0023)
// ─────────────────────────────────────────────────────────────────────────────
/// 启动录制(RFC-0023):把完整 `Recording` 写 JSONL 到 `{dir}/recordings.jsonl`
/// (目录自动创建)。录制 opt-in;再次调用(不同 dir)替换 recorder。
/// Raises `RecordingError` (``code`` "Init" / "OpenFile" / "Spawn") when the
/// recorder cannot be set up; the previous recorder, if any, stays in place.
#[pyfunction]
fn init_recording(dir: &str) -> PyResult<()> {
let rec = aimux_core::recording::JsonlRecorder::try_new(dir.to_string())
.map_err(|e| AiMuxBindingError::from(e).to_py_err())?;
aimux_core::recording::init_recording(Some(std::sync::Arc::new(rec)));
Ok(())
}
/// 启动内存有界录制(RFC-0023 P6):FIFO ring,丢弃计数可查。
///
/// `cap` 可省略:省略时使用库默认容量(等价于 FFI `aimux_init_recording_ring_default()`;
/// 本绑定直接依赖 aimux-core 而非 aimux-ffi,故调用等价 core API `RingRecorder::default()`)。
/// 显式传 `cap == 0` 报错(保持与各绑定统一的"传 0 报错"语义)。
#[pyfunction]
#[pyo3(signature = (cap=None))]
fn init_recording_ring(cap: Option<u64>) -> PyResult<()> {
match cap {
// 省略 cap:库默认容量(镜像 FFI default 变体)。
None => aimux_core::recording::init_recording(Some(std::sync::Arc::new(
aimux_core::recording::RingRecorder::default(),
))),
// 显式 cap == 0:报错(保持统一语义)。
Some(0) => {
return Err(to_py_err(&AiMuxError::InvalidArgument(
"init_recording_ring: cap must be > 0".into(),
)));
}
// 显式 cap > 0:指定容量的有界 ring。
Some(c) => aimux_core::recording::init_recording(Some(std::sync::Arc::new(
aimux_core::recording::RingRecorder::with_capacity(c as usize),
))),
}
Ok(())
}
/// 停止录制:全局 recorder = None(新调用不再录制)。
#[pyfunction]
fn recording_stop() {
aimux_core::recording::init_recording(None);
}
/// 刷盘全局 recorder(阻塞至 JSONL 落盘;ring 模式 no-op)。
#[pyfunction]
fn recording_flush() {
if let Some(rec) = aimux_core::recording::recorder() {
rec.flush();
}
}
/// Flush the global recorder and **report write failures**: raises
/// `RecordingError` (``code`` is "WriterGone" / "FlushTimeout" / "Write") when
/// the data could not be confirmed on disk. `RecordingError` is its own
/// exception type, not an `AimuxError`. Returns normally when nothing is
/// recording. The legacy `recording_flush` stays and never reports.
#[pyfunction]
fn recording_try_flush() -> PyResult<()> {
let Some(rec) = aimux_core::recording::recorder() else {
return Ok(());
};
rec.try_flush()
.map_err(|e| AiMuxBindingError::from(e).to_py_err())
}
/// 从录制 JSONL 创建 mock 回放 model(RFC-0023 P3):按输入匹配录制响应,
/// 不发真实 API。返回的 Model 可用于 generate_text / stream_text。
#[pyfunction]
fn mock_replay(recordings_jsonl: &str) -> PyResult<Model> {
let mut recordings: Vec<aimux_core::recording::Recording> = Vec::new();
for (idx, line) in recordings_jsonl.lines().enumerate() {
let line = line.trim();
if line.is_empty() {
continue;
}
// Same split as everywhere else: text that does not parse is the
// binding's (ValueError); a parsed line with the wrong shape is core's
// InvalidArgument. Both name the line.
let rec: aimux_core::recording::Recording = serde_json::from_str(line).map_err(|e| {
let msg = format!("recordings line {}: {e}", idx + 1);
match e.classify() {
serde_json::error::Category::Data => to_py_err(&AiMuxError::InvalidArgument(msg)),
_ => binding_py_err(&BindingError::InvalidWireJson {
argument: "recordings_jsonl",
message: msg,
}),
}
})?;
recordings.push(rec);
}
if recordings.is_empty() {
return Err(to_py_err(&AiMuxError::InvalidArgument(
"no recordings".into(),
)));
}
let model = aimux_core::replay::MockReplayModel::new(
recordings[0].provider.provider.clone(),
recordings[0].provider.model_id.clone(),
recordings,
);
Ok(Model {
inner: Arc::new(model),
trace_store: None,
})
}
/// Create a RouterModel (RFC-0021) over the given child models. The returned
/// model routes each call to one child and falls back across the rest on error
/// (per config_json). `models` must be non-empty.
///
/// `config_json` (optional): {"router": "rule"|"weighted", "weights": [...],
/// "fallback": "on_error"|"none", "provider_name", "model_id"}.
#[pyfunction]
#[pyo3(signature = (models, config_json=None))]
fn router(models: Vec<PyRef<Model>>, config_json: Option<&str>) -> PyResult<Model> {
if models.is_empty() {
return Err(to_py_err(&AiMuxError::InvalidArgument(
"router: models must be non-empty".into(),
)));
}
let children: Vec<Arc<dyn LanguageModel>> = models.iter().map(|m| m.inner.clone()).collect();
let cfg: RouterFfiConfig = match config_json {
Some(json) => wire_json("config_json", json)?,
None => RouterFfiConfig::default(),
};
let router: Box<dyn aimux_core::router::Router> = match cfg.router.as_deref() {
Some("weighted") => {
let weights = cfg.weights.unwrap_or_else(|| vec![1.0; children.len()]);
Box::new(aimux_core::router::WeightedRouter::new(weights))
}
_ => Box::new(aimux_core::router::RuleRouter),
};
let fallback = if cfg.fallback.as_deref() == Some("none") {
aimux_core::router::FallbackPolicy::None
} else {
aimux_core::router::FallbackPolicy::OnError
};
let router_cfg = aimux_core::router::RouterConfig {
provider_name: cfg.provider_name.unwrap_or_else(|| "router".into()),
model_id: cfg.model_id.unwrap_or_else(|| "router".into()),
};
let model = aimux_core::router::RouterModel::new(children, router, fallback, router_cfg);
Ok(Model {
inner: Arc::new(model),
trace_store: None,
})
}
/// Create a MoaModel (RFC-0022) over reference models + one aggregator.
/// References fan out in parallel, then the aggregator synthesizes a final
/// answer. `references` may be empty (runs aggregator only).
///
/// `config_json` (optional) is a serialized MoaConfig.
#[pyfunction]
#[pyo3(signature = (references, aggregator, config_json=None))]
fn moa(
references: Vec<PyRef<Model>>,
aggregator: PyRef<Model>,
config_json: Option<&str>,
) -> PyResult<Model> {
let refs: Vec<Arc<dyn LanguageModel>> = references.iter().map(|m| m.inner.clone()).collect();
let cfg: aimux_core::moa::MoaConfig = match config_json {
Some(json) => wire_json("config_json", json)?,
None => aimux_core::moa::MoaConfig::default(),
};
let model = aimux_core::moa::MoaModel::new(refs, aggregator.inner.clone(), cfg);
Ok(Model {