gproxy-channel-api 2.5.2

Channel contracts for extending GPROXY with provider adapters
Documentation
//! A channel's explicit routing surface — the verbatim port of v1's per-channel
//! `routing_table()`. A channel declares, for each `(operation, inbound kind)`
//! cell it can serve, how to service it ([`RoutingDecision`]). `seed_default_routing`
//! materializes this list into real `routing_rules` rows; cells the channel does
//! not declare have no rule and are `Unsupported` at request time.

use crate::protocol::{
    ContentGenerationKind as Cg, Operation, OperationKey, OperationKind, Provider,
};

/// The host routing decision for one source protocol cell.
///
/// This policy belongs to the channel/host boundary rather than the generic
/// wire-transform crate.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RoutingDecision {
    Passthrough,
    TransformTo(OperationKey),
    Local,
    Unsupported,
}

/// A channel's declared routing surface: source cell → decision.
pub type RouteList = Vec<(OperationKey, RoutingDecision)>;

fn key(operation: Operation, kind: OperationKind) -> OperationKey {
    OperationKey::try_new(operation, kind).expect("channel route must use a consistent operation")
}

/// passthrough
pub fn pass(operation: Operation, kind: OperationKind) -> (OperationKey, RoutingDecision) {
    (key(operation, kind), RoutingDecision::Passthrough)
}

/// transform to a different cell
pub fn xform(
    operation: Operation,
    kind: OperationKind,
    d_op: Operation,
    d_kind: OperationKind,
) -> (OperationKey, RoutingDecision) {
    (
        key(operation, kind),
        RoutingDecision::TransformTo(key(d_op, d_kind)),
    )
}

/// explicitly unsupported
pub fn unsupported(operation: Operation, kind: OperationKind) -> (OperationKey, RoutingDecision) {
    (key(operation, kind), RoutingDecision::Unsupported)
}

/// served locally
pub fn local(operation: Operation, kind: OperationKind) -> (OperationKey, RoutingDecision) {
    (key(operation, kind), RoutingDecision::Local)
}

/// Declare downstream OpenAI Responses WebSocket content routes to a channel's
/// native streaming content target.
pub fn responses_ws_to(d_kind: OperationKind) -> RouteList {
    use Operation::{GenerateContent, StreamGenerateContent};

    vec![
        xform(
            GenerateContent,
            cg(Cg::OpenAiResponsesWebSocket),
            StreamGenerateContent,
            d_kind,
        ),
        xform(
            StreamGenerateContent,
            cg(Cg::OpenAiResponsesWebSocket),
            StreamGenerateContent,
            d_kind,
        ),
    ]
}

// kind shorthands
pub fn cg(k: Cg) -> OperationKind {
    OperationKind::ContentGeneration(k)
}

pub fn pv(p: Provider) -> OperationKind {
    OperationKind::Provider(p)
}