Skip to main content

completions_handler

Function completions_handler 

Source
pub async fn completions_handler(
    __arg0: State<Arc<AppState>>,
    tenant: Option<Extension<TenantId>>,
    auth: Option<Extension<AuthContext>>,
    payload: Result<Json<CompletionsRequest>, JsonRejection>,
) -> Response
Expand description

POST /v1/completions — OpenAI completions shim, wired through to the internal invoke protocol (T41, v0.4).

Flow:

  1. Apply the tenant-scope gate (T2) on the bearer token’s crate::token_scope::TokenScope against the resolved tenant (TenantId(0) for OpenAI SDKs that don’t send X-TensorWasm-Tenant).
  2. Parse the request body. Malformed JSON → 400 openai_invalid_request.
  3. Resolve req.model against AppState::openai_model_map. Miss → 404 model_not_found.
  4. Look up the resolved function id in AppState::functions. Miss → 404 model_not_found (the map points at a deleted record). A hit owned by a different tenant → 403 tenant_scope_denied (per-resource owner check, api S-IDOR).
  5. If stream: true, build a StreamingContext
    • receiver pair (the T34 plumbing), spawn the call, and stream chunks back as OpenAI data: { ... } SSE frames terminated by data: [DONE]\n\n.
  6. If stream: false, run the call synchronously, collect any emitted chunks (or fall back to a host-stamped placeholder), and respond with the OpenAI text_completion envelope.