Skip to content

fix(stage-ui): proxy openai-compatible requests in electron#2011

Open
jim139129 wants to merge 7 commits into
moeru-ai:mainfrom
jim139129:jim139129/fix/openai-compatible-electron-fetch
Open

fix(stage-ui): proxy openai-compatible requests in electron#2011
jim139129 wants to merge 7 commits into
moeru-ai:mainfrom
jim139129:jim139129/fix/openai-compatible-electron-fetch

Conversation

@jim139129

Copy link
Copy Markdown
Contributor

Description

这个PR试图解决部分中转站不兼容的问题,已经本地测试两个基于sub2api的中转站均可用
修改思路:

  1. 在 shared 层定义一个 Eventa invoke 合同,用于 OpenAI-compatible HTTP 请求转发:
    packages/stage-shared/src/openai-compatible.ts:17

  2. 在 Electron main process 注册 fetch bridge,由 main process 发真实 HTTP(S) 请求,绕过 renderer 的 CORS 限制:
    apps/stage-tamagotchi/src/main/services/airi/openai-compatible-fetch.ts:6
    注册点在:
    apps/stage-tamagotchi/src/main/index.ts:263

  3. 在 renderer/provider 层新增 resolveOpenAICompatibleFetch():
    packages/stage-ui/src/libs/providers/openaiCompatibleFetch.ts:75

    它的策略是:先用原生 globalThis.fetch,这样 CORS 正常的 provider 仍然保留原行为和流式能力;只有遇到 TypeError 这类
    浏览器网络失败时,才 fallback 到 Electron main fetch。AbortError 不会 fallback,避免用户取消请求后又被重新发起。

  4. 把这个 fetch 注入到所有 OpenAI-compatible 相关路径:

    • provider 创建:
      packages/stage-ui/src/libs/providers/providers/openai-compatible/index.ts:44

    • 模型列表和聊天校验 validator:
      packages/stage-ui/src/libs/providers/validators/openai-compatible.ts:82
      packages/stage-ui/src/libs/providers/validators/openai-compatible.ts:143
      packages/stage-ui/src/libs/providers/validators/openai-compatible.ts:259

    • LLM store 里的模型获取:
      packages/stage-ui/src/stores/llm.ts:111

    • provider metadata/converter 的模型获取:
      packages/stage-ui/src/stores/providers/converters.ts:147

  5. provider 创建处没有继续用 createOpenAI(...),而是显式组合 createChatProvider 和 createModelProvider,这样可以注入自
    定义 fetch,并且分别传入独立 options,避免 chat provider 对 options 的内部改写影响 model provider:
    packages/stage-ui/src/libs/providers/providers/openai-compatible/index.ts:45

整体效果是:Web 和非 Electron 环境不受影响;Electron 下 CORS 正常的接口保持原路径;只有 renderer 网络失败的 OpenAI-
compatible 请求才走 main process 代理,从而修复 sub2api 类网关的模型列表和对话不可用问题。

Linked Issues

#2010

Additional Context

@github-actions

github-actions Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

⏳ Approval required for deploying to Cloudflare Workers (Preview) for stage-web.

Name Link
🔭 Waiting for approval For maintainers, approve here

Hey, maintainers, kindly take some time to review and approve this deployment when you are available. Thank you! 🙏

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

Copy link
Copy Markdown

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: eb0931243d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/stage-ui/src/libs/providers/openaiCompatibleFetch.ts Outdated
@jim139129
jim139129 force-pushed the jim139129/fix/openai-compatible-electron-fetch branch from eb09312 to 6bad1ca Compare June 26, 2026 04:45

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

Copy link
Copy Markdown

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: 6bad1ca392

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/stage-ui/src/libs/providers/openaiCompatibleFetch.ts
@jim139129
jim139129 force-pushed the jim139129/fix/openai-compatible-electron-fetch branch from 6bad1ca to cfd0ac6 Compare June 26, 2026 05:01

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

Copy link
Copy Markdown

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: cfd0ac6972

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/stage-ui/src/libs/providers/validators/openai-compatible.ts Outdated

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

Copy link
Copy Markdown

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: bcb4331e5b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread apps/stage-tamagotchi/src/main/services/airi/openai-compatible-fetch.ts Outdated

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

Copy link
Copy Markdown

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: 044e06e10a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread apps/stage-tamagotchi/src/main/services/airi/openai-compatible-fetch.ts Outdated
Comment thread apps/stage-tamagotchi/src/main/services/airi/openai-compatible-fetch.ts Outdated
@jim139129
jim139129 marked this pull request as draft June 26, 2026 09:09
@jim139129
jim139129 force-pushed the jim139129/fix/openai-compatible-electron-fetch branch from 044e06e to 8b475ab Compare June 26, 2026 09:17
@jim139129
jim139129 marked this pull request as ready for review June 26, 2026 09:18

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

Copy link
Copy Markdown

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: 3259b7beba

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread apps/stage-tamagotchi/src/main/services/airi/openai-compatible-fetch.ts Outdated

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

Copy link
Copy Markdown

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: 82f6e4f3b0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread apps/stage-tamagotchi/src/main/index.ts Outdated

@typed-sigterm typed-sigterm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It makes sense but I'm not sure if it's the best way to implement it...

@lietblue lietblue added apps/stage-tamagotchi Desktop App: Windows & macOS & Linux bug Something isn't working scope/providers Scope related to providers we support labels Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

apps/stage-tamagotchi Desktop App: Windows & macOS & Linux bug Something isn't working scope/providers Scope related to providers we support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants