Skip to content

feat(openai): add OpenAI protocol adapter with configurable API mode#74

Open
CJackHwang wants to merge 2 commits into
mainfrom
codex/add-protocol-adaptation-layer-to-openaichatservice
Open

feat(openai): add OpenAI protocol adapter with configurable API mode#74
CJackHwang wants to merge 2 commits into
mainfrom
codex/add-protocol-adaptation-layer-to-openaichatservice

Conversation

@CJackHwang
Copy link
Copy Markdown
Owner

Motivation

  • 为了兼容不同 OpenAI 上游(/v1/responses/v1/chat/completions)并支持特殊网关/厂商差异,新增可配置的“协议模式”以提高互操作性与容错性。
  • 需要对工具调用与响应格式做兼容适配,避免依赖单一 message.tool_calls 结构导致在不同 API 形态下失效。

Description

  • OpenAIServiceConfig 中新增 apiMode?: 'auto' | 'chat_completions' | 'responses' 并在调用链中透传(useSettingsuseChatStreamStreamControllerapiServiceOpenAIChatService)。
  • OpenAIChatService 中实现协议适配层:新增 buildChatCompletionsPayload(...)buildResponsesPayload(...),以及 normalizeBaseUrl(...)shouldFallbackToChatCompletions(...) 等辅助方法用于构建请求与判断回退条件。
  • apiMode: 'auto' 下优先尝试 /v1/responses,当返回 404/501 或明显的“不支持/unsupported/unknown endpoint/responses” 字样时自动回退到 /v1/chat/completions;非流式的 responses 路径会读取 output_text 并通过现有的 chunk 回调发出以保持兼容。
  • 增加 normalizeToolCalls(...) 以兼容不同位置的工具调用信息(除了 message.tool_calls 也会从 message.content 中查找 tool_call 部分),并将构造器结果用于 streamWithTools 的工具执行流程中。
  • 在设置页添加 OpenAI 的“协议模式”选择器(Auto/Responses/Chat Completions)并补充对应 i18n 文案(EN / ZH-CN / JA)。

Testing

  • 运行 npm run build 构建通过,生产包成功生成(构建过程中输出了原有的若干警告,但不影响本次改动的编译通过)。
  • 代码静态类型/编译检查通过且新配置已在运行时路径(设置 → 流调用)被正确读取与传递。

Codex Task

@vercel
Copy link
Copy Markdown

vercel Bot commented May 19, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
hey-mean Ready Ready Preview, Comment May 19, 2026 7:39pm

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 95e8ceacd4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +167 to +170
const data = await response.json();
const outputText = data?.output_text;
if (outputText) onChunk(outputText);
return;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Parse Responses text from output instead of output_text

In apiMode: 'auto' (the new default), a successful /responses call returns early after reading only data?.output_text. The REST response body typically carries assistant text under output[].content[] (e.g. type: "output_text"), so output_text can be absent and this path emits no chunk, yielding an empty assistant reply while skipping fallback because the status is 200. This makes normal OpenAI responses appear blank for users on the new mode.

Useful? React with 👍 / 👎.

@@ -216,7 +288,7 @@ export class OpenAIChatService implements IChatService<OpenAIServiceConfig> {
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep normalized tool calls in assistant history message

The new compatibility path detects tool calls from message.content via normalizeToolCalls(message), but the assistant turn pushed into messages still stores only message.tool_calls. When a provider returns tool calls only in content, the code executes tools and then sends role: "tool" messages without a matching assistant tool_calls record in history, which commonly breaks the next chat-completions request validation and stops the tool loop.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant