diff --git a/README.md b/README.md index 264ab5a..61d1737 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ These packages aren't OpenCode plugins. magpie runs them in its gateway, on the | [system-prompt](packages/system-prompt) | Your own system prompt on every request, or on some agents' or models', in each API's own place | | [word-guard](packages/word-guard) | New API's 敏感词过滤: turn away or mask words and patterns in what users send, and in replies | | [think-tags](packages/think-tags) | Take `…` out of a reply's text, or put a Chat reply's `reasoning_content` into it (`thinking_to_content`) | +| [codex-history](packages/codex-history) | Opt-in Responses history compatibility after switching providers: remove historical ciphertext and optional item IDs while preserving readable content and tool-call pairing | ```sh magpie plugin add @magpie-community/middleware- diff --git a/packages/codex-history/LICENSE b/packages/codex-history/LICENSE new file mode 100644 index 0000000..a2a7261 --- /dev/null +++ b/packages/codex-history/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 Codex Filter Manager contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/codex-history/PROVENANCE.json b/packages/codex-history/PROVENANCE.json new file mode 100644 index 0000000..2189ad9 --- /dev/null +++ b/packages/codex-history/PROVENANCE.json @@ -0,0 +1,19 @@ +{ + "source": "https://github.com/unisong/codex-filter-manager", + "commit": "40f98308f364ca8533c30c11a2cede4ea00542f3", + "version": "v1.0.5", + "license": "MIT", + "files": { + "magpie-plugin/history-filter.mjs": { + "sha256": "45d969daf0a6185fba2cbf8b76910d7bcb63e012d4767c46863b520f17c80925" + }, + "magpie-plugin/history.middleware.js": { + "sha256": "b4da0d27961861904ba8577633717c6e14c36e78508ff95074fa24fb98d76a06" + } + }, + "changes": [ + "Inlined the shared history algorithm and request hook into one import-free middleware file.", + "Removed helper exports; only onRequest is exported.", + "No model detection, guard, dashboard, proxy service or provider configuration is included." + ] +} diff --git a/packages/codex-history/README.md b/packages/codex-history/README.md new file mode 100644 index 0000000..e8ed3fe --- /dev/null +++ b/packages/codex-history/README.md @@ -0,0 +1,85 @@ +# @magpie-community/middleware-codex-history + +**Magpie 原生历史过滤插件 / Native history compatibility middleware** + +缓解 Codex 切换供应商、账号或上游资源后,旧会话出现密文验证失败、部分历史 item ID 不属于当前资源的问题。只处理发出的 Responses 请求,不修改原会话文件或 Codex `base_url`。 + +**插件只提供历史过滤,不包含 ModelTrace、降智检测或模型保护。** 无独立后台服务、管理端口、运行时依赖或额外模型请求。规则默认全部关闭,在 Magpie 自带的插件 Options 中按供应商设置。 + +## 安装 + +在 Magpie 的 **Plugins → Discover → Gateway middleware** 中安装 **Codex History Filter**,或使用命令行: + +```sh +magpie plugin add @magpie-community/middleware-codex-history +``` + +也可从 [独立 GitHub 仓库](https://github.com/unisong/magpie-codex-history-filter) 安装同一份过滤代码。无需启动 Codex Filter Manager,不要同时安装两份。需要支持 gateway middleware 的 Magpie;本包已在 0.1.1265 的隔离实例中验证,其他版本需核对插件加载状态。 + +## 设置 + +打开 **Plugins → Installed → 本插件 → Options**,保存以下示例,替换 `demo` 和 `backup` 为你实际使用的 Magpie 供应商 ID: + +```json +{ + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + }, + "magpie:backup": { + "historyFilter": false, + "preserveEncryptedInclude": true + } + }, + "aliases": {} +} +``` + +- 请求模型为 `demo/gpt-6-astra` 时,使用 `magpie:demo` 的规则;ID 是模型前缀,不是供应商显示名称。 +- `historyFilter: true` 开启过滤;`false`、删除该规则或空配置均为直通。不同会话按各自请求独立处理,可同时过滤多个供应商。 +- `preserveEncryptedInclude: true` 保留客户端对**新响应**中 `reasoning.encrypted_content` 的请求,推荐开启。它不会保留**旧历史**中的密文。省略或设置为 `false` 会移除这一 include 值,不影响其他 include 项。 +- 供应商改名后,旧会话可能仍发送旧前缀;可设置 `"aliases": {"old-demo": "demo"}`。别名只用于选择过滤规则,不改写请求模型,也不替代 Magpie 自己的路由别名。 +- 无 `供应商/模型` 前缀或非 Responses 协议的请求保持原样。未配置的供应商不会被自动过滤。 + +保存 Options 后,Magpie 在后续请求加载规则。无需另开网页或重启 Codex;是否成功加载以 Magpie 插件页的错误与调用统计为准。 + +## 原理与边界 + +开启后,插件移除旧 reasoning 条目、明确标记的加密内容,以及可独立携带内容的消息和工具条目中的可选服务器 ID;设置 `store: false`。保留正文、工具参数、结果、`call_id` 配对、文件引用及普通业务 ID,不改变请求模型或推理强度。 + +`previous_response_id`、`conversation`、`item_reference`、加密压缩历史和 `/responses/compact` 无法安全跨资源迁移时,会明确拒绝,提示整理文字摘要后新建会话,不会静默丢弃这些上下文。 + +- 不能解密旧密文、迁移所有服务器资源引用,或保证消除全部 Azure 历史错误。移除内部推理状态也可能影响长任务连续性。 +- Magpie 请求钩子限时 250 毫秒;加载失败、异常或超时可能按 Magpie 的机制放行原请求。插件不是强制安全边界。 +- 只覆盖进入 Magpie agent middleware 的 Responses 请求;Magpie 自身的标题、分类器等内部请求不经过此钩子。WebSocket、Chat Completions、Lite HTTP 标头修改不在本插件范围内。 +- 规则按进入钩子时的模型前缀选择。自动路由组、fallback、其他插件随后改写路由时,不保证规则对应最终上游。 +- 不采集或保存凭据、请求正文,不执行检测、重试或模型调用。上游正常请求仍按上游规则计费。 + +## 停用、升级与旧版迁移 + +在 Magpie 插件页关闭本插件即可停止全部过滤;删除某条供应商规则只停用该供应商。卸载也在 Magpie 中完成,不需要恢复 `base_url`。通过本地路径安装时,移动或删除目录前先卸载旧插件。 + +若此前使用 Codex Filter Manager v1.0.5 自动安装的本地 `magpie-plugin`,先在旧管理页关闭该供应商的自动保护(若有旧设置),停止旧管理服务,再在 Magpie 中停用或卸载旧的本地历史插件,最后安装本插件并复制所需历史规则。原有规则的 `stripResponsesLiteHeader` 会被忽略,可删除。不要同时启用两份历史过滤插件。 + +升级固定 GitHub 标签时,在 Magpie 中用新标签地址更新;现有配置存于 Magpie 的 `plugins.json`,更新前可备份自己的 Options。不要上传该文件,它可能包含其他插件的私有设置。 + +## English + +Opt-in Magpie gateway middleware for Codex Responses history after switching providers or accounts. It strips old reasoning/encrypted items and optional server IDs from portable history, preserving readable content and tool-call pairing. **History filtering only: no model detector, guard, background server, credentials or extra upstream calls.** + +Install from **Plugins → Discover → Gateway middleware**, or run `magpie plugin add @magpie-community/middleware-codex-history`. In the plugin's **Options**, configure `providers["magpie:"]` as shown above. Every provider defaults to passthrough. The request's `provider/model` prefix selects its rule; requests from different sessions remain independent. `aliases` maps an old prefix to a rule ID only. `preserveEncryptedInclude: true` keeps requests for new encrypted response content while still removing historical ciphertext. + +Unsafe server-only references and encrypted compaction are rejected explicitly. Magpie may fail open on plugin errors/timeouts, and downstream route changes are outside the plugin's control. Disable or remove it in Magpie; it never edits Codex configuration. + +## 开发与来源 + +```sh +bun test packages/codex-history +``` + +测试只使用本地虚构数据,不调用模型。核心代码来自 [Codex Filter Manager v1.0.5](https://github.com/unisong/codex-filter-manager/tree/v1.0.5),来源和 SHA-256 记录于 [PROVENANCE.json](PROVENANCE.json)。为匹配社区 middleware 包格式,将共用算法与请求钩子合并为单个无 import 文件。 + +接口参考:[Magpie middleware 文档](https://usemagpie.ai/docs/plugins#middleware)。 + +MIT,保留原项目贡献者署名,见 [LICENSE](LICENSE)。 diff --git a/packages/codex-history/cases.json b/packages/codex-history/cases.json new file mode 100644 index 0000000..c55595f --- /dev/null +++ b/packages/codex-history/cases.json @@ -0,0 +1,1333 @@ +[ + { + "name": "default configuration passes history unchanged", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": {}, + "request": { + "model": "demo/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "reasoning", + "id": "rs_old", + "encrypted_content": "foreign" + }, + { + "type": "message", + "id": "msg_old", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + }, + { + "type": "encrypted_content", + "data": "foreign" + } + ] + }, + { + "type": "function_call", + "id": "fc_old", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "id": "out_old", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ] + } + }, + { + "name": "unknown provider passes through", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "backup/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "reasoning", + "id": "rs_old", + "encrypted_content": "foreign" + }, + { + "type": "message", + "id": "msg_old", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + }, + { + "type": "encrypted_content", + "data": "foreign" + } + ] + }, + { + "type": "function_call", + "id": "fc_old", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "id": "out_old", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ] + } + }, + { + "name": "disabled provider passes through", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": false + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "reasoning", + "id": "rs_old", + "encrypted_content": "foreign" + }, + { + "type": "message", + "id": "msg_old", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + }, + { + "type": "encrypted_content", + "data": "foreign" + } + ] + }, + { + "type": "function_call", + "id": "fc_old", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "id": "out_old", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ] + } + }, + { + "name": "non-Responses protocol passes through", + "ctx": { + "protocol": "anthropic" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "reasoning", + "id": "rs_old", + "encrypted_content": "foreign" + }, + { + "type": "message", + "id": "msg_old", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + }, + { + "type": "encrypted_content", + "data": "foreign" + } + ] + }, + { + "type": "function_call", + "id": "fc_old", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "id": "out_old", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ] + } + }, + { + "name": "bare model cannot accidentally match provider rule", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "reasoning", + "id": "rs_old", + "encrypted_content": "foreign" + }, + { + "type": "message", + "id": "msg_old", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + }, + { + "type": "encrypted_content", + "data": "foreign" + } + ] + }, + { + "type": "function_call", + "id": "fc_old", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "id": "out_old", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ] + } + }, + { + "name": "portable history removes ciphertext and IDs but keeps text and call pairs", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "reasoning", + "id": "rs_old", + "encrypted_content": "foreign" + }, + { + "type": "message", + "id": "msg_old", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + }, + { + "type": "encrypted_content", + "data": "foreign" + } + ] + }, + { + "type": "function_call", + "id": "fc_old", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "id": "out_old", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ] + }, + "expect_request": { + "model": "demo/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "message", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + } + ] + }, + { + "type": "function_call", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ], + "store": false + } + }, + { + "name": "filtering twice is idempotent", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "message", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + } + ] + }, + { + "type": "function_call", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ], + "store": false + }, + "expect_request": { + "model": "demo/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "message", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + } + ] + }, + { + "type": "function_call", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ], + "store": false + } + }, + { + "name": "include compatibility can be turned off", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": false + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "reasoning", + "id": "rs_old", + "encrypted_content": "foreign" + }, + { + "type": "message", + "id": "msg_old", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + }, + { + "type": "encrypted_content", + "data": "foreign" + } + ] + }, + { + "type": "function_call", + "id": "fc_old", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "id": "out_old", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ] + }, + "expect_request": { + "model": "demo/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "message.output_text.logprobs" + ], + "input": [ + { + "type": "message", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + } + ] + }, + { + "type": "function_call", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ], + "store": false + } + }, + { + "name": "old prefix aliases only select rule and preserve the model", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + }, + "aliases": { + "old-demo": "demo" + } + }, + "request": { + "model": "old-demo/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "reasoning", + "id": "rs_old", + "encrypted_content": "foreign" + }, + { + "type": "message", + "id": "msg_old", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + }, + { + "type": "encrypted_content", + "data": "foreign" + } + ] + }, + { + "type": "function_call", + "id": "fc_old", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "id": "out_old", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ] + }, + "expect_request": { + "model": "old-demo/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "message", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + } + ] + }, + { + "type": "function_call", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ], + "store": false + } + }, + { + "name": "second configured supplier uses its own include preference", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + }, + "magpie:backup": { + "historyFilter": true, + "preserveEncryptedInclude": false + } + } + }, + "request": { + "model": "backup/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "reasoning.encrypted_content", + "message.output_text.logprobs" + ], + "input": [ + { + "type": "reasoning", + "id": "rs_old", + "encrypted_content": "foreign" + }, + { + "type": "message", + "id": "msg_old", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + }, + { + "type": "encrypted_content", + "data": "foreign" + } + ] + }, + { + "type": "function_call", + "id": "fc_old", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "id": "out_old", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ] + }, + "expect_request": { + "model": "backup/gpt-6-astra", + "reasoning": { + "effort": "high" + }, + "include": [ + "message.output_text.logprobs" + ], + "input": [ + { + "type": "message", + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Keep answer." + } + ] + }, + { + "type": "function_call", + "call_id": "pair", + "name": "lookup", + "arguments": "{\"id\":\"business\"}" + }, + { + "type": "function_call_output", + "call_id": "pair", + "output": "keep result" + }, + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_file", + "file_id": "file_keep" + } + ] + } + ], + "store": false + } + }, + { + "name": "tool search histories keep schemas and call IDs", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "input": [ + { + "type": "tool_search_call", + "id": "tsc_old", + "call_id": "search_pair", + "execution": "client", + "arguments": { + "query": "lookup" + }, + "status": "completed" + }, + { + "type": "tool_search_output", + "id": "tso_old", + "call_id": "search_pair", + "execution": "client", + "tools": [ + { + "type": "function", + "name": "lookup", + "parameters": { + "type": "object", + "properties": { + "id": { + "type": "string" + } + } + } + } + ] + }, + { + "type": "additional_tools", + "id": "tools_old", + "role": "developer", + "tools": [] + } + ] + }, + "expect_request": { + "model": "demo/gpt-6-astra", + "store": false, + "input": [ + { + "type": "tool_search_call", + "call_id": "search_pair", + "execution": "client", + "arguments": { + "query": "lookup" + }, + "status": "completed" + }, + { + "type": "tool_search_output", + "call_id": "search_pair", + "execution": "client", + "tools": [ + { + "type": "function", + "name": "lookup", + "parameters": { + "type": "object", + "properties": { + "id": { + "type": "string" + } + } + } + } + ] + }, + { + "type": "additional_tools", + "role": "developer", + "tools": [] + } + ] + } + }, + { + "name": "custom tools and role-only messages retain content", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "input": [ + { + "type": "custom_tool_call", + "id": "ctc_old", + "call_id": "custom", + "name": "patch", + "input": "keep patch" + }, + { + "type": "custom_tool_call_output", + "id": "ctco_old", + "call_id": "custom", + "output": "done" + }, + { + "role": "user", + "id": "msg_old", + "content": "Continue" + } + ] + }, + "expect_request": { + "model": "demo/gpt-6-astra", + "store": false, + "input": [ + { + "type": "custom_tool_call", + "call_id": "custom", + "name": "patch", + "input": "keep patch" + }, + { + "type": "custom_tool_call_output", + "call_id": "custom", + "output": "done" + }, + { + "role": "user", + "content": "Continue" + } + ] + } + }, + { + "name": "unknown server tool resource IDs are not guessed away", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "input": [ + { + "type": "web_search_call", + "id": "ws_keep", + "status": "completed", + "action": { + "type": "search", + "query": "example" + } + } + ] + }, + "expect_request": { + "model": "demo/gpt-6-astra", + "store": false, + "input": [ + { + "type": "web_search_call", + "id": "ws_keep", + "status": "completed", + "action": { + "type": "search", + "query": "example" + } + } + ] + } + }, + { + "name": "plain string input is retained", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "input": "Hello" + }, + "expect_request": { + "model": "demo/gpt-6-astra", + "input": "Hello", + "store": false + } + }, + { + "name": "reject previous response reference", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "input": "Hello", + "previous_response_id": "resp_old" + }, + "reject": { + "status": 400, + "message": "Codex history filter: Complete message/tool history" + } + }, + { + "name": "reject conversation reference", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "input": "Hello", + "conversation": "conv_old" + }, + "reject": { + "status": 400, + "message": "Codex history filter: Complete message/tool history" + } + }, + { + "name": "reject item reference", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "input": [ + { + "type": "item_reference", + "id": "item_old" + } + ] + }, + "reject": { + "status": 400, + "message": "Codex history filter: Encrypted compaction/context references" + } + }, + { + "name": "reject encrypted compaction history", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "input": [ + { + "type": "compaction", + "encrypted_content": "sealed" + } + ] + }, + "reject": { + "status": 400, + "message": "Codex history filter: Encrypted compaction/context references" + } + }, + { + "name": "reject automatic compaction", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "input": "Hello", + "context_management": [ + { + "type": "compaction" + } + ] + }, + "reject": { + "status": 400, + "message": "Codex history filter: Encrypted compaction is disabled" + } + }, + { + "name": "reject invalid include", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "input": "Hello", + "include": "reasoning.encrypted_content" + }, + "reject": { + "status": 400, + "message": "Codex history filter: include must be an array" + } + }, + { + "name": "reject invalid input", + "ctx": { + "protocol": "responses", + "path": "/v1/responses" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "input": { + "type": "message" + } + }, + "reject": { + "status": 400, + "message": "Codex history filter: input must be a string or an array" + } + }, + { + "name": "reject compact endpoint", + "ctx": { + "protocol": "responses", + "path": "/v1/responses/compact" + }, + "options": { + "providers": { + "magpie:demo": { + "historyFilter": true, + "preserveEncryptedInclude": true + } + } + }, + "request": { + "model": "demo/gpt-6-astra", + "input": "Hello" + }, + "reject": { + "status": 409, + "message": "Codex history filter: encrypted compaction" + } + } +] diff --git a/packages/codex-history/codex-history.middleware.js b/packages/codex-history/codex-history.middleware.js new file mode 100644 index 0000000..47785b5 --- /dev/null +++ b/packages/codex-history/codex-history.middleware.js @@ -0,0 +1,110 @@ +// Derived from Codex Filter Manager v1.0.5 (MIT). See PROVENANCE.json. +// Standalone Magpie middleware: no server, credentials or model probes. +// These input forms carry their own contents; the server-issued item id is optional. +// Other tool/resource types may require their IDs, so don't recursively strip IDs. +const PORTABLE_ITEMS = new Set(['message', 'agent_message', 'function_call', 'function_call_output', + 'custom_tool_call', 'custom_tool_call_output', 'tool_search_call', 'tool_search_output', 'additional_tools']); + +class FilterError extends Error { + constructor(code, message, status = 400) { + super(message); + this.code = code; + this.status = status; + } +} + +function filterResponsesRequest(body, {preserveEncryptedInclude = false} = {}) { + if (!body || typeof body !== 'object' || Array.isArray(body)) { + throw new FilterError('invalid_request_json', 'Responses request must be a JSON object.'); + } + if (body.previous_response_id || body.conversation) { + throw new FilterError('response_reference_not_portable', + 'Complete message/tool history is required, not previous_response_id or conversation references. Start a new session with a text summary.'); + } + if (body.include !== undefined && !Array.isArray(body.include)) { + throw new FilterError('invalid_include', 'include must be an array.'); + } + if (body.input !== undefined && typeof body.input !== 'string' && !Array.isArray(body.input)) { + throw new FilterError('invalid_input', 'input must be a string or an array.'); + } + if (Array.isArray(body.context_management) && body.context_management.some(item => item?.type === 'compaction')) { + throw new FilterError('encrypted_compaction_disabled', + 'Encrypted compaction is disabled by this filter. Use a text summary and start a new session.'); + } + const clean = {...body, store: false}; + let removed = 0, removedItemIds = 0, removedEncryptedContentItems = 0; + // include controls the NEW response, not the historical ciphertext in input. + // AnyRouter rejects Codex requests when this requested response field is removed. + if (body.include) clean.include = preserveEncryptedInclude ? [...body.include] + : body.include.filter(value => value !== 'reasoning.encrypted_content'); + if (Array.isArray(body.input)) { + clean.input = body.input.filter(item => { + if (item?.type === 'reasoning') { removed++; return false; } + return true; + }); + clean.input = clean.input.map(item => { + if (item?.type === 'compaction' || item?.type === 'item_reference' || item?.encrypted_content) { + throw new FilterError('encrypted_context_not_portable', + 'Encrypted compaction/context references cannot safely cross accounts. Start a new session with a text summary; history has NOT been silently discarded.'); + } + const isMessage = item && item.type === undefined && typeof item.role === 'string' && item.content !== undefined; + if (item && typeof item === 'object') { + const portable = removeEncryptedContent(item, () => { removedEncryptedContentItems++; }); + if (PORTABLE_ITEMS.has(item.type) || isMessage) { + if (Object.hasOwn(portable, 'id')) { + delete portable.id; + removedItemIds++; + } + return portable; + } + return portable; + } + return item; + }); + } + // call_id pairs calls/results and is not the server-side item id. + // Preserve it, this turn's effort, all message contents, and nested resource IDs. + return {body: clean, removed, removedItemIds, removedEncryptedContentItems}; +} + +// Historical agent/tool messages can carry account-bound ciphertext inside their +// content arrays. Remove only those explicitly typed items; ordinary IDs and +// opaque values in tool payloads remain untouched. +function removeEncryptedContent(value, onRemoved) { + if (Array.isArray(value)) { + return value.filter(item => { + if (item && item.type === 'encrypted_content') { + onRemoved(); + return false; + } + return true; + }).map(item => removeEncryptedContent(item, onRemoved)); + } + if (!value || typeof value !== 'object') return value; + const copy = {...value}; + for (const [key, child] of Object.entries(copy)) copy[key] = removeEncryptedContent(child, onRemoved); + return copy; +} + +export function onRequest(body, ctx) { + if (ctx.protocol !== 'responses' || !body || typeof body.model !== 'string' || !/^[^/]+\/.+/.test(body.model)) return; + const prefix = body.model.split('/')[0]; + const options = ctx.options || {}; + const aliases = options.aliases || {}; + const id = Object.prototype.hasOwnProperty.call(aliases, prefix) ? aliases[prefix] : prefix; + const rules = options.providers || {}; + const rule = rules['magpie:' + id]; + if (!Object.prototype.hasOwnProperty.call(rules, 'magpie:' + id) || rule?.historyFilter !== true) return; + try { + if (/\/compact\/?$/.test(ctx.path || '')) { + ctx.reject(409, 'Codex history filter: encrypted compaction cannot cross resources. Use a text summary and a new session.'); + return; + } + return filterResponsesRequest(body, {preserveEncryptedInclude: rule.preserveEncryptedInclude === true}).body; + } catch (error) { + // Explicit rejection is required: Magpie forwards the original request if + // a hook throws. Never silently discard non-portable context references. + ctx.reject(error instanceof FilterError ? error.status : 409, + 'Codex history filter: ' + (error instanceof FilterError ? error.message : 'history could not be safely filtered.')); + } +} diff --git a/packages/codex-history/codex-history.test.mjs b/packages/codex-history/codex-history.test.mjs new file mode 100644 index 0000000..09bf9b0 --- /dev/null +++ b/packages/codex-history/codex-history.test.mjs @@ -0,0 +1,4 @@ +import * as mod from "./codex-history.middleware.js" +import { check } from "../../scripts/middleware.mjs" + +check(import.meta.dir, mod) diff --git a/packages/codex-history/package.json b/packages/codex-history/package.json new file mode 100644 index 0000000..05911f0 --- /dev/null +++ b/packages/codex-history/package.json @@ -0,0 +1,32 @@ +{ + "name": "@magpie-community/middleware-codex-history", + "version": "0.1.0", + "description": "Opt-in, per-provider Codex Responses history compatibility for Magpie", + "type": "module", + "license": "MIT", + "files": [ + "codex-history.middleware.js", + "README.md", + "LICENSE", + "PROVENANCE.json" + ], + "keywords": [ + "magpie", + "magpie-middleware", + "codex", + "responses", + "history" + ], + "repository": { + "type": "git", + "url": "git+https://github.com/magpie-community/plugins.git", + "directory": "packages/codex-history" + }, + "magpie": { + "middleware": "./codex-history.middleware.js", + "options": { + "providers": {}, + "aliases": {} + } + } +} diff --git a/registry.json b/registry.json index 31135c1..599ee87 100644 --- a/registry.json +++ b/registry.json @@ -47,6 +47,8 @@ "summary": { "en": "New API's sensitive-word filter: turn away or mask words and patterns in what you send, and in replies.", "zh": "New API 的敏感词过滤:拒绝或替换你发出的内容和回复里的词与正则。" } }, { "package": "@magpie-community/middleware-think-tags", "name": "Think Tags", "kind": "middleware", "community": true, "summary": { "en": "Take a model's … out of its reply's text, or put a Chat reply's reasoning into it.", "zh": "去掉回复正文开头的 …,或把 Chat 回复的思考内容放进正文。" } }, + { "package": "@magpie-community/middleware-codex-history", "name": "Codex History Filter", "kind": "middleware", "community": true, + "summary": { "en": "Opt-in, per-provider Codex Responses history filtering after switching providers. Preserves readable content and tool-call pairing; no model probes.", "zh": "按供应商开关 Codex Responses 历史过滤,缓解切换上游后的密文和历史 ID 兼容问题;保留正文与工具配对,不运行模型检测。" } }, { "package": "@magpie-community/agent-aider", "name": "Aider", "kind": "agent", "community": true, "summary": { "en": "Aider (aider.chat) on magpie's Agents page: pick one of magpie's models for it, and switch it off to put its config back.", "zh": "把 Aider(aider.chat)加到 magpie 的 Agent 页:给它选 magpie 的模型,关掉即恢复它原来的配置。" } }, { "package": "@magpie-community/agent-type4me", "name": "Type4Me", "kind": "agent", "community": true,