Principle:3641397194 wq Gpt6 Astra Relay API Adapter
| Knowledge Sources | |
|---|---|
| Domains | API_Design, Adapter_Pattern, Networking |
| Last Updated | 2026-10-26 12:00 GMT |
Overview
An adapter pattern that provides a unified interface for AI model relay across three modes — preview (deterministic mock), OpenAI-compatible (public API paths), and remote (explicit endpoint contract) — with credential isolation and HTTPS enforcement.
Description
Relay API Adapter is the design pattern for abstracting AI model access behind a single interface that can operate in three distinct modes without changing the calling code. It addresses the problem of environment-dependent API access: during development you need deterministic mocks, in production you need a public OpenAI-compatible endpoint, and in controlled deployments you need explicit per-endpoint configuration.
The three modes:
- Preview: deterministic, performs no I/O. Returns mock catalog, entitlement, and usage. `submit` returns a queued task receipt. Used for UI development and testing.
- OpenAI: uses the public-compatible `/models` and `/responses` (or `/chat/completions`) paths. Requires `baseUrl` + `authToken`. Falls back from `responses` to `chat` API on 404/405.
- Remote: requires four explicit endpoint paths (`catalog`, `entitlement`, `usage`, `submit`). Each must be same-origin HTTPS. Used for custom relay deployments.
A critical cross-cutting concern is credential isolation: the `authToken` is kept in memory only, never included in `status()`, errors, or preview payloads. All responses are redacted before crossing IPC boundaries.
Usage
Use this principle when you need to submit tasks to an AI model relay with varying deployment configurations while maintaining a consistent interface. It is the access layer for the toolbox agent loop and the relay submit workflow.
Theoretical Basis
The adapter normalizes configuration into a frozen object and dispatches by mode:
# Abstract algorithm (NOT real implementation)
class RelayAdapter:
def __init__(self, config):
self.config = normalize_config(config) # frozen
def submit(self, task):
if self.config.mode == "openai":
return self._submit_openai(task)
preflight = self.preflight(task)
if not preflight.ok:
raise PermissionError(preflight.errors)
if self.config.mode == "preview":
return {taskId: "preview-####", status: "queued"}
return self._request("submit", method="POST", body=task)
def _submit_openai(self, task):
preflight = self._preflight_openai(task)
body = {model: preflight.provider.model,
input: assemble_prompt(task), stream: False}
# Try responses API, fall back to chat on 404/405
try:
return self._request_openai("responses", body)
except HttpStatusError as e:
if e.status in (404, 405):
return self._request_openai("chat/completions", chat_body)
raise
Key design properties:
- Configuration is frozen: `normalizeConfig` returns `Object.freeze` — no mutation after construction.
- HTTPS enforcement: `validateApiUrl` rejects non-HTTPS except loopback (`localhost`, `127.0.0.1`, `[::1]`). Credentials in URLs are rejected.
- Same-origin constraint: all endpoints must share the `baseUrl` origin.
- Provider presets: `gpt-6-astra` and `gpt-6.1-sol` define full-access presets with reasoning effort, context, and tool flags.
- Wire API negotiation: `auto` mode tries `responses` first, falls back to `chat` on 404/405. Explicit `responses` or `chat` skips fallback.
- Streaming rejected: `stream: true` throws — the adapter uses complete-response mode only.
Practical Guide
- Use `status()` to check readiness — it reports `configured`, `ready`, `missing` fields without exposing the token.
- `preflight()` checks catalog, entitlement, and usage in parallel via `Promise.all` before submission.
- `testConnection()` is OpenAI-mode only — it fetches `/models` and validates the model list.
- Error classes: `RelayConfigError` (missing config), `RelayRequestError` (transport failure), `RelayPermissionError` (preflight failed).
- `PROVIDER_ALIASES` maps common misspellings to canonical provider IDs.