@alvin0/ai-agent-sdk-provider-openai

中文文档
0.1.16发布于 2026/11/06MITTypeScriptES Module构建来源已验证近 30 天下载 2006

Universal OpenAI adapter and provider plugin for ai-agent-sdk

原始使用文档
文档目录4 节
  1. Connecting a compatible endpoint
  2. Prompt caching
  3. Default configuration precedence
  4. Native Decisions API

@alvin0/ai-agent-sdk-provider-openai

Runtime: Universal (Edge/Worker, browser, Deno, Bun, and Node).

Compatible gateways can configure baseUrl, models, fetch, and headers (a string record or synchronous function returning one per operation). Generation uses Responses; embedding uses the OpenAI embeddings protocol.

Project examples and live generation/decision tests use gpt-6-luna as the minimum OpenAI model. Select it or a newer model explicitly; do not fall back to older models when a request fails. Embedding model selection is separate. The embedding adapter/plugin also supports custom headers. Header names are case-insensitive. A headers entry overrides what the SDK would otherwise send for that name (accept, content-type, user-agent, protocol headers) — only a connection-level name fetch itself forbids (host, content-length, …) or a credential-shaped name (must go through apiKey/auth instead, so it can be redacted in logs) stays rejected. Supply credentials with apiKey. Static records are copied; prepared embedding calls keep the same header snapshot across all batches. Trusted local HTTP gateways require allowInsecureHttp: true.

pnpm add @alvin0/ai-agent-sdk-core @alvin0/ai-agent-sdk-provider-openai

Universal OpenAI adapter and transactional provider plugin. Credentials are injected; this package never reads environment variables or files.

import { ModelRegistry } from '@alvin0/ai-agent-sdk-core'
import { openAiPlugin } from '@alvin0/ai-agent-sdk-provider-openai'

const registry = new ModelRegistry()
registry.install(openAiPlugin({ apiKey: apiKeyFromSecretStore })) // injected string or CredentialSource

Use openAiAdapter() for manual route registration. Both APIs are Universal and require an explicit apiKey; environment lookup belongs to a Node wrapper.

Both accept models: [{ id, contextWindow, maxTokens }] plus provider-level defaultContextWindow and defaultMaxTokens fallbacks. A catalog model's maxTokens is its SDK output ceiling and, unless models[].defaultMaxTokens is set, its default output budget. Provider fallback budgets are not hard ceilings. Keep the operating context below any extended-context price threshold. See the provider model limits guide for an example and precedence rules. These declarations do not increase server-side limits.

Composition: runtime.providers. Lifecycle: inert-runtime-owned-registration; the runtime activates and removes the captured provider registration.

Connecting a compatible endpoint

This package speaks the whole OpenAI API family, not just api.openai.com. Two wires are supported — api: 'responses' (default) and api: 'chat-completions' — because most third-party OpenAI-compatible endpoints (DeepSeek, Groq, Together, Qwen/DashScope, vLLM, Ollama, LM Studio, many gateways) only implement /chat/completions. Give each vendor its own plugin instance with id, baseUrl, displayName (so an error names the real vendor, not "OpenAI"), and api:

import { envCredential } from '@alvin0/ai-agent-sdk-auth-node'

openAiPlugin({
  id: 'deepseek',
  displayName: 'DeepSeek',
  baseUrl: 'https://api.deepseek.com',
  api: 'chat-completions',
  apiKey: envCredential('DEEPSEEK_API_KEY'),
  compat: { reasoningFormat: 'deepseek' }, // effort → thinking + reasoning_effort
})

compat exposes the Chat Completions wire knobs one endpoint at a time: reasoningFormat ('openai' default — reasoning_effort verbatim — or 'deepseek', or false to send no effort field at all), maxTokensField ('max_tokens' default, or 'max_completion_tokens', or false), systemRole, structuredOutputs, tools, parallelToolCalls, streamUsage, stop, seed, promptCacheKey. Unset knobs keep the protocol's own conservative defaults.

Prompt caching

Set promptCaching: true to generate one stable prompt_cache_key per adapter instance, or pass promptCacheKey when the application already owns a stable session key. The same key is used by Responses and Chat Completions, including a mixed per-model route. Keep one adapter/plugin instance scoped to one cache identity; do not share an auto-generated key across unrelated tenants.

Compatible gateways are handled opportunistically: if a gateway returns a 400 that specifically names prompt_cache_key, the adapter retries that call once without the field and remembers the downgrade for the rest of its lifetime. Unrelated request errors are never swallowed.

An endpoint mounted somewhere other than its protocol's own path (an Azure OpenAI deployment, a gateway that rewrites the route) can override path and add query (never for secrets — those belong in auth/apiKey):

openAiPlugin({
  id: 'azure-gpt',
  baseUrl: 'https://my-res.openai.azure.com/openai/deployments/gpt-6-luna',
  path: '/chat/completions',
  api: 'chat-completions',
  query: { 'api-version': '2026-06-01' },
  apiKey: envCredential('AZURE_KEY'),
})

Default configuration precedence

contextWindow, maxTokens, and inputModalities resolve from explicit agent/call configuration to model declarations, route defaults, runtime defaults and SDK fallback policy. Agent contextWindow and inputModalities overrides are implemented and remain in effect across a per-call model switch. Inherited effort and maxTokens are dropped when switching models; explicit per-call maxTokens takes precedence. An omitted modality declaration means unknown, not text-only. Declare verified limits for custom routes rather than treating fallback budgets as a vendor's technical ceiling.

Native Decisions API

Install the companion runtime as a direct dependency:

pnpm add @alvin0/ai-agent-sdk-core @alvin0/ai-agent-sdk-decision-adapter @alvin0/ai-agent-sdk-provider-openai

Use the /decisions entry point with the companion decision runtime:

import { createDecisionRuntime, choiceQuestion, scoreQuestion, booleanQuestion }
  from '@alvin0/ai-agent-sdk-decision-adapter'
import { openAiDecisionPlugin } from '@alvin0/ai-agent-sdk-provider-openai/decisions'

const runtime = createDecisionRuntime({ providers: [openAiDecisionPlugin({
  apiKey: process.env.OPENAI_API_KEY!,
  // baseUrl: 'https://api.openai.com/v1', // API root, including /v1
})] })
try {
  const result = await runtime.decisionModel({ provider: 'openai', model: 'gpt-6-luna' }).evaluate({
    state: { message: 'Please refund my order today.' },
    questions: {
      route: choiceQuestion('Choose the department.', { billing: 'Payments and refunds', support: 'Technical issues' }),
      urgency: scoreQuestion('How urgent is this?', ['Routine', 'Urgent']),
      refund: booleanQuestion('Does the customer request a refund?'),
    },
  })
  console.log(result.answers, result.usage)
} finally {
  await runtime.close()
}

openAiDecisionAdapter also works directly with DecisionRequest. API keys may be strings or SDK credential sources. Custom routes, API roots, headers, retry policies, safetyIdentifier, deadlines and request/response byte limits are configurable. The adapter performs one HTTP attempt; the decision runtime owns retries. It never falls back to Responses after a Decisions error.

String state and instructions remain text; structured JSON descriptions are serialized as text. This entry point currently accepts the SDK's text/JSON DecisionInput, rather than native image messages. Choice options use their SDK record keys as wire values. Score labels are zero-based indexes, with the SDK level description preserved as the wire description. SDK boolean questions map to native predicates; value uses a probability threshold of 0.5 and probabilityTrue retains the provider probability. Use decision evidence gates when your application needs stronger confidence.

Answers retain provider probability provenance. Usage maps cached and uncached input into disjoint SDK buckets, and zero output tokens remain authoritative. A native refusal throws a non-retryable ModelError with code OPENAI_DECISION_REFUSED; it produces no fabricated answer. Attempt accounting retains reported usage even when a refusal or invalid answer prevents a result.

The current native model catalog contains gpt-6-luna. Explicit model IDs remain configurable for consumers and compatible gateways; the server determines availability. Native Decisions requires an OpenAI API key. Codex uses CODEX_GUARDIAN_DECISIONS_API_KEY (or OPENAI_API_KEY) for this call independently of its ChatGPT OAuth Responses credentials; OAuth login alone does not configure Decisions access. See the Decisions guide and Codex startup source.

版本与发布记录

@alvin0/ai-agent-sdk-provider-openai 共发布 17 个版本,首次发布于 2026/09/10;过去 12 个月发布了 17 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.16latest2026/11/061 天前
0.1.152026/11/025 天前
0.1.142026/10/2117 天前
0.1.132026/10/1721 天前
0.1.122026/10/1127 天前
0.1.112026/10/1028 天前

关于 @alvin0/ai-agent-sdk-provider-openai 的常见问题

@alvin0/ai-agent-sdk-provider-openai 是什么?

Universal OpenAI adapter and provider plugin for ai-agent-sdk

如何安装 @alvin0/ai-agent-sdk-provider-openai?

在项目目录运行 npm install @alvin0/ai-agent-sdk-provider-openai,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@alvin0/ai-agent-sdk-core(^0.1.16)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@alvin0/ai-agent-sdk-provider-openai 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@alvin0/ai-agent-sdk-provider-openai 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@alvin0/ai-agent-sdk-provider-openai 可以商用吗?

@alvin0/ai-agent-sdk-provider-openai 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@alvin0/ai-agent-sdk-provider-openai 的下载量有多少?

根据 npm 官方下载统计,@alvin0/ai-agent-sdk-provider-openai 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,006 次,日均约 67 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alvin0/ai-agent-sdk-provider-openai 还在维护吗?

最新版本 0.1.16 发布于 2026/11/06(1 天前);至今共发布 17 个版本,其中过去 12 个月发布了 17 个;首次发布于 2026/09/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。