Repository navigation
perf(openai): 避免 WS 转 HTTP 时复制仅 Grok 需要的长请求 - #1022
Draft
seanshigit wants to merge 2 commits into
Draft
seanshigit wants to merge 2 commits into
seanshigit wants to merge 2 commits into
Conversation
ranxi2001
marked this pull request as draft
November 22, 2026 23:32
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.
解决的问题与行为变化
客户端使用 WebSocket、上游使用 HTTP 时,网关会先复制整个请求,再把请求解码为
map[string]json.RawMessage,只为判断这轮有没有显式的tools。这份快照和标记只有 Grok 的工具继承与缓存路由会读取,OpenAI 的 OAuth 和 API Key 请求也付出了两份请求体大小的分配。把快照和两项工具意图检查放入已有的 Grok 分支,位置仍在 Grok 清理输入之前。API Key 自己需要的工具转换继续运行。基于
production@991cd30e2910d5eeb6ab55f6b7fc93e9a494e1a6,可以独立合并。关联记录与来源
原功能独立基线991cd30,关联#1013–#1021性能剖析。此次维护仅普通合入production@919d111;无新增第三方代码/依赖,保留作者历史。
管理员新功能提示
提示ID/登记、展示入口、关闭行为、沿用/重新公告及A→B→C/多入口/刷新重登持久化逐项不适用:无UI或新功能公告。
复现与验收
新增完整桥接轮次基准,使用合成长对话、函数工具声明、本地模拟 HTTP 上游和终止 SSE 事件,每次断言返回的 token 用量。16.6 MiB 样例超过默认 15 MiB 桥接阈值。可在基线上复制同一基准,分别从
backend运行下面的命令。Apple M5,macOS arm64,Go 1.27.4。基线和补丁进程交叉运行,各六轮,表中为中位数。内存列为每次完整桥接轮次的累计分配。
fast-anything 0.7.0 在基线上采样了 400 个约 1 MiB 的 OpenAI OAuth 桥接请求。这次额外字段读取在 3.95 秒 CPU 样本中累计 660 毫秒。同一组输入运行补丁后二者只在 Grok 分支执行。Grok 的六轮耗时区间重叠(原版 26.3–30.0 ms,补丁 26.1–32.9 ms),仅用作对照。上述耗时都来自本机网关,生产网络和模型生成时延尚未测量。
协议、调度或传输改动补充
仅将Grok专用快照与意图读取移入Grok分支,仍在sanitize前。OpenAI API Key工具转换、Grok工具继承/缓存路由/重试及身份/资源关闭/usage不变;畸形请求仍经原prepare/decoder拒绝。原作者benchmark只直接调用完整turn方法并使用内存HTTP替身,未经过真实WS ingress或HTTP监听,阈值路由、续轮/取消/故障等由既有测试分别覆盖;不把benchmark当线上链路验收。
实际验证
-race。gofmt -l、git diff --check,均通过。兼容性与运行影响
传输方式、重试、Grok 的显式或继承工具及计费规则沿用原行为。无需数据库迁移和配置更新,可直接回退此提交。管理员新功能提示与用户文档不适用,这次仅减少网关内部工作。
文档与用户可见变化
纯后端分配优化,无新API/config/migration/UI/i18n;截图不适用。回滚该功能提交恢复额外复制;不改变账务或生产配置。
Agent 使用声明
Codex,
gpt-6.2-astra,推理强度max。用于定位、实现、离线基准和验证、整理 PR。离线剖析用的模拟账号仓库参考 perf(openai): 减少长请求的会话元数据扫描 #1013 的夹具,由 Codex,
gpt-6.2-sol,默认推理强度编写。本 PR 的桥接基准使用仓库已有的 HTTP 测试替身。维护独立审查Codex:gpt-6.1-sol / ultra,完整生产diff与实际Grok/非Grok调用链静态审查;fast仅requested,实际tier未知。父级Hermes gpt-6.1-sol,推理强度未提供,负责tree/CI/正文及普通base集成;未本机运行install/test/lint/typecheck/build/format,未独立重跑作者性能数字。
提交前自查