Skip to main content

gproxy_channel_api/
routes.rs

1//! A channel's explicit routing surface — the verbatim port of v1's per-channel
2//! `routing_table()`. A channel declares, for each `(operation, inbound kind)`
3//! cell it can serve, how to service it ([`RoutingDecision`]). `seed_default_routing`
4//! materializes this list into real `routing_rules` rows; cells the channel does
5//! not declare have no rule and are `Unsupported` at request time.
6
7use crate::protocol::{
8    ContentGenerationKind as Cg, Operation, OperationKey, OperationKind, Provider,
9};
10
11/// The host routing decision for one source protocol cell.
12///
13/// This policy belongs to the channel/host boundary rather than the generic
14/// wire-transform crate.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum RoutingDecision {
17    Passthrough,
18    TransformTo(OperationKey),
19    Local,
20    Unsupported,
21}
22
23/// A channel's declared routing surface: source cell → decision.
24pub type RouteList = Vec<(OperationKey, RoutingDecision)>;
25
26fn key(operation: Operation, kind: OperationKind) -> OperationKey {
27    OperationKey::try_new(operation, kind).expect("channel route must use a consistent operation")
28}
29
30/// passthrough
31pub fn pass(operation: Operation, kind: OperationKind) -> (OperationKey, RoutingDecision) {
32    (key(operation, kind), RoutingDecision::Passthrough)
33}
34
35/// transform to a different cell
36pub fn xform(
37    operation: Operation,
38    kind: OperationKind,
39    d_op: Operation,
40    d_kind: OperationKind,
41) -> (OperationKey, RoutingDecision) {
42    (
43        key(operation, kind),
44        RoutingDecision::TransformTo(key(d_op, d_kind)),
45    )
46}
47
48/// explicitly unsupported
49pub fn unsupported(operation: Operation, kind: OperationKind) -> (OperationKey, RoutingDecision) {
50    (key(operation, kind), RoutingDecision::Unsupported)
51}
52
53/// served locally
54pub fn local(operation: Operation, kind: OperationKind) -> (OperationKey, RoutingDecision) {
55    (key(operation, kind), RoutingDecision::Local)
56}
57
58/// Declare downstream OpenAI Responses WebSocket content routes to a channel's
59/// native streaming content target.
60pub fn responses_ws_to(d_kind: OperationKind) -> RouteList {
61    use Operation::{GenerateContent, StreamGenerateContent};
62
63    vec![
64        xform(
65            GenerateContent,
66            cg(Cg::OpenAiResponsesWebSocket),
67            StreamGenerateContent,
68            d_kind,
69        ),
70        xform(
71            StreamGenerateContent,
72            cg(Cg::OpenAiResponsesWebSocket),
73            StreamGenerateContent,
74            d_kind,
75        ),
76    ]
77}
78
79// kind shorthands
80pub fn cg(k: Cg) -> OperationKind {
81    OperationKind::ContentGeneration(k)
82}
83
84pub fn pv(p: Provider) -> OperationKind {
85    OperationKind::Provider(p)
86}