@molecule/api-ai-openai
v1.1.0
Published
<!-- AUTO-GENERATED — DO NOT EDIT THIS FILE. Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json. Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates). To change this document, edit th
Readme
@molecule/api-ai-openai
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
OpenAI (GPT) AI provider for molecule.dev.
Type
provider
Installation
npm install @molecule/api-ai-openai @molecule/api-ai @molecule/api-bond @molecule/api-secretsAPI
Interfaces
OpenaiConfig
OpenAI provider configuration.
interface OpenaiConfig {
/** Override the API key (defaults to `process.env.OPENAI_API_KEY`). */
apiKey?: string
/** Default model when callers don't specify one. */
defaultModel?: string
/** Default max output tokens. */
maxTokens?: number
/** Override the API base URL (for proxies / Azure). */
baseUrl?: string
/** Called on each rate-limited/overloaded upstream response, before any retry sleep. */
onRateLimit?: AiRateLimitCallback
}ProcessEnv
Environment variables read by this provider.
interface ProcessEnv {
/** OpenAI API key (required unless `config.apiKey` is passed). */
OPENAI_API_KEY: string
/** Base URL override (for proxies / Azure-compatible gateways). Defaults to `https://api.openai.com`. */
OPENAI_BASE_URL?: string
}Classes
OpenaiAIProvider
OpenAI Chat Completions provider implementing the AIProvider interface.
Functions
createProvider(config)
Create an OpenAI AI provider instance.
function createProvider(config?: OpenaiConfig): AIProviderconfig— OpenAI-specific configuration.
Returns: An AIProvider backed by OpenAI's Chat Completions API.
Constants
aiOpenaiSecretDefinitions
Secret definitions required by the OpenAI AI bond.
const aiOpenaiSecretDefinitions: SecretDefinition[]provider
The provider implementation.
const provider: AIProviderCore Interface
Implements @molecule/api-ai interface.
Bond Wiring
Setup function to register this provider with the bond system:
import { bond } from '@molecule/api-bond'
import { provider } from '@molecule/api-ai-openai'
export function setupAiOpenai(): void {
bond('ai', 'openai', provider)
}Injection Notes
Requirements
Peer dependencies:
@molecule/api-ai^1.0.1@molecule/api-bond^1.0.1@molecule/api-secrets^1.0.1
Environment Variables
OPENAI_API_KEY(required) — OpenAI API key- Setup: Create a secret key on the OpenAI platform (API keys page).
- Get it here: https://platform.openai.com/api-keys
- Example:
sk-proj-...
Runtime Dependencies
@molecule/api-ai@molecule/api-bond@molecule/api-secrets
Config: OPENAI_API_KEY (SERVER-side only) plus an optional default model id/base URL.
Missing OPENAI_API_KEY fails fast: the provider throws naming the exact env var on
first use (the exported provider is a lazy proxy, so this fires on the first chat() call,
not at bond/module-load time) — it never silently sends an empty key.
Error message disambiguation: a plain 400 that ISN'T a context-length error (bad param, malformed tool schema) gets its own non-retryable message distinct from the generic "AI service error. Please try again." used for retryable failures.
E2E Tests
Integration checklist — drive the real UI (live preview, no mocks), adapt each item to this app's actual chat/AI screens, and check every box off one by one. A box you can't check is an integration bug to fix — not a skip. The sandbox HAS an AI provider bonded, so the flow runs live end-to-end; AI output is NON-DETERMINISTIC, so assert on STRUCTURE/behavior, not exact text:
- [ ] A message sent through the real chat UI comes back as a RELEVANT AI reply — not an echo of the prompt, a hardcoded stub, or an empty bubble. Ask something with a checkable answer (e.g. "What is 2 + 2?") and confirm the response actually contains it ("4"), proving a live model answered.
- [ ] If the app streams, tokens render INCREMENTALLY — text grows word by
word in the UI, not one final blob dumped after a long frozen spinner. (A
streamed
chat()yieldstextchunks then a finaldone; a single late blob means the reply was awaited whole and streaming is broken.) - [ ] Multi-turn CONTEXT is preserved: a follow-up that refers back to the
previous turn (e.g. after "2 + 2", ask "now double that" -> understood as 8) works — proving the full
messageshistory is sent, not just the last line. - [ ] A provider failure (bad/missing key, rate limit, timeout) surfaces as a graceful in-UI error message, NOT a crash, blank screen, a spinner that never resolves, or an unhandled 500. Force one and watch the UI recover.
- [ ] The provider key + the provider call are SERVER-side only: the key
never reaches the browser (check the network tab, the JS bundle, and page
globals), and no route proxies arbitrary prompts to the model without auth
- a token cap — an open AI endpoint is an unbounded bill and abuse vector.
