pub async fn completions_handler(
__arg0: State<Arc<AppState>>,
tenant: Option<Extension<TenantId>>,
auth: Option<Extension<AuthContext>>,
payload: Result<Json<CompletionsRequest>, JsonRejection>,
) -> ResponseExpand description
POST /v1/completions — OpenAI completions shim, wired through to
the internal invoke protocol (T41, v0.4).
Flow:
- Apply the tenant-scope gate (T2) on the bearer token’s
crate::token_scope::TokenScopeagainst the resolved tenant (TenantId(0)for OpenAI SDKs that don’t sendX-TensorWasm-Tenant). - Parse the request body. Malformed JSON →
400 openai_invalid_request. - Resolve
req.modelagainstAppState::openai_model_map. Miss →404 model_not_found. - 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). - If
stream: true, build aStreamingContext- receiver pair (the T34 plumbing), spawn the call, and stream
chunks back as OpenAI
data: { ... }SSE frames terminated bydata: [DONE]\n\n.
- receiver pair (the T34 plumbing), spawn the call, and stream
chunks back as OpenAI
- If
stream: false, run the call synchronously, collect any emitted chunks (or fall back to a host-stamped placeholder), and respond with the OpenAItext_completionenvelope.