Repository navigation
fix(api): 补全相对 baseUrl,修复 standalone 部署仅健康检查可用 - #185
Merged
Merged
Conversation
standalone/Docker 镜像把 VITE_API_BASE_URL 设为同源前缀 `/api`, 而 @opencode/client 内部用 `new URL(options.baseUrl)` 构造请求地址, 只接受绝对 URL。于是所有 SDK 请求在发出前就抛 Invalid URL; 健康检查用原生 fetch 支持相对路径,仍显示在线,表现为 「服务器在线但会话/模型/消息/终端全部不可用」。 在 http.ts 增加 toAbsoluteBaseUrl,getApiBaseUrl 与 SDK 客户端 统一按当前页面 origin 补全,保留反代下 `/api` 前缀部署方式。 补充相对地址、协议相对地址与绝对地址的单测。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
Docker / standalone 部署下(
docker/Dockerfile.frontend中VITE_API_BASE_URL=/api),服务器设置里的健康检查显示「在线」,但会话列表、模型、消息、终端等全部不可用。根因
@opencode/client内部用new URL(options.baseUrl)构造请求地址,只接受绝对 URL。/api是相对路径,new URL('/api')在浏览器端直接抛Invalid URL,请求根本没有发出。健康检查走的是
serverStore.checkHealth里手写的原生fetch,原生 fetch 支持相对路径,所以能通过——于是出现「在线但全废」的假象。修复
src/api/http.ts新增toAbsoluteBaseUrl(),按window.location.origin补全相对地址;file://等 origin 为null的环境保持原值。getApiBaseUrl()统一返回绝对地址,PTY WebSocket、文件上传等调用点一起受益。src/api/sdk.ts交给 SDK 的baseUrl与缓存 key 都使用补全后的地址。验证
src/api/http.test.ts、src/api/sdk.test.ts:18 个测试通过src/api除真实服务端 e2e 外:90 个测试通过/api即可正常加载会话、模型、消息和终端