rea
morluto
Reverse engineer anything with agents, from app behavior down to native binaries.
PROJECT TOPICS
INSTALL REFERENCE
dsh plugin --profile web add github:zhexuany/dsh-stateful-responses
该命令指向仓库当前默认分支;尚无绑定当前 commit 的完整验证结果。
PROJECT README
让 dsh 调用 GPT 模型时使用有状态的 Responses API:每一步只把新增的 input item 和上一轮的
previous_response_id 发给上游,而不是把整段会话重放一遍。
插件只改写发往上游的请求 body,并且只在上游明确证明它保存了上一轮(响应
store: true且带 response id)之后才发送previous_response_id。任何不确定的情况都会退回全量请求,因此最坏行为与 未安装插件完全一致。
store + previous_response_id 才有收益;不支持时自动降级为全量dsh 的 agent loop 在每一步都重新构造完整对话——system instructions、工具定义、历史消息、工具结果—— 一并交给 provider。对于官方 OpenAI Responses API 这类服务端有状态的接口,这会造成两处重复:
Responses API 的 continuation 机制正是为此设计的:上游保存上一次 response 的输入与输出,客户端只需要
发送新增 item 和 previous_response_id。
dsh 当前在这里有一个缺口:
llm-pi-ai 的 api: openai-responses 适配负责协议转换,但没有 continuation 接线;因此本插件把改写点放在进程内最后一层:globalThis.fetch。
agent loop
└── llm-pi-ai (api: openai-responses)
└── OpenAI SDK
└── globalThis.fetch ← dsh-stateful-responses 在这里拦截
└── upstream POST /v1/responses
| 模块 | 职责 |
|---|---|
index.js |
插件入口:name + apply(ctx, config),安装/卸载拦截器 |
lib/interceptor.js |
核心:命中判定、请求改写、响应追踪、回退与降级 |
lib/chain.js |
有界 LRU,保存会话链(response id + item 哈希序列) |
lib/items.js |
item 规范化与哈希、"前缀扩展"判定 |
lib/sse.js |
SSE 终止事件(completed/failed/incomplete)收集 |
lib/config.js |
严格配置校验 |
cordis.patch.yml |
bundle patch:以 insert 行挂载插件 |
选择 fetch 层而不是 adapter 层的原因:
fetch,body 是 JSON 字符串——这是稳定的调用契约;只有同时满足以下条件才处理,否则原样放行:
POST;/responses 结尾,且命中配置的 baseURLs;model 与数组 input;model 命中配置的 models(支持结尾 * 通配);previous_response_id。链的键由请求的稳定要素组成:
chain key = url + model + prompt_cache_key + hash(tools) + hash(instructions)
不同模型、不同工具集、不同 system instructions 会自然分链;prompt_cache_key(dsh 会把会话 id 传给
provider)让不同会话不串链。链存放在有界 LRU 中(maxChains,默认 32),超出后淘汰最久未使用的链。
每个 input / output item 先规范化再取 sha256。规范化会剔除上游可以重新生成的易变字段:
id、status、created_at、completed_at、annotations、logprobs、obfuscation。
这些字段只影响比较;真正发出去的永远是未经修改的 delta 原文,因此剔除不会破坏会话内容。
只有当"本次请求的 item 序列是链中 item 序列的严格前缀扩展"时才允许续传:
否则说明会话分叉、被编辑或换了上下文,插件丢弃该链并按全量发送。
命中时把 body 改成等价的增量请求:
{
"model": "gpt-6.1-sol",
"store": true,
"previous_response_id": "resp_...",
"input": [ "…only the new items…" ]
}
其余字段(tools、instructions、reasoning 参数、采样参数等)保持不变。
response.body 做 tee(),一份原样交给 pi-ai,另一份交给内部收集器解析终止事件;response.clone().json() 读取,原响应不受影响。无论哪种形式,插件都不会修改模型返回给 dsh 的字节内容。
只有响应满足 store === true 且带非空 id 时才更新链:
items = 链旧 items + 本次 delta + 本次响应 output 的 item 哈希
这条规则的直接推论是:链只在上游证明它保存了之后才存在。
| 情况 | 行为 |
|---|---|
完成响应 store !== true |
该 baseURL 标记为不支持,清空全部链,之后永远全量(每个 baseURL 只提示一次) |
响应 failed / cancelled |
只丢弃当前链,不标记不支持——失败不能证明上游不存储 |
续传请求非 2xx,且错误文本匹配 previous_response_id / previous response / not found |
丢弃链,用原始全量 body 重试一次 |
| 续传请求网络错误(非用户取消) | 丢弃链,用原始全量 body 重试一次 |
| 解析、规划、追踪等内部异常 | 放行原请求,不打断模型调用 |
store: true 的响应,永远不会生成 previous_response_id;tools / instructions / model / URL / prompt_cache_key 任一变化都不命中旧链;previous_response_id 一律放行,不参与链管理;tee 分支);Request 对象、流式 body 等)一律放行;previous_response_id 的前提是链存在;链存在的前提是上游返回过 store: true 且有 id;input 是本次请求中"比链多出来的部分"的原文,不是插件重建的内容;插件会比较 prompt item,但不会持久化、不会写入日志、不会发送到第三方。日志只包含模型名、item 数量、 response id 等元数据,不包含 prompt 或补全内容。
ctx.effect 生命周期);POST …/responses)。llm-deepseek 使用的 Messages 协议没有等价的
continuation 字段,不在覆盖范围内;encrypted_content)在下一次请求中
被带回;否则前缀校验失败,插件退回全量——安全但没有收益。插件是一个标准 dsh bundle:包内的 cordis.patch.yml 通过 insert 增加一个插件行。
把仓库放到任意目录,例如 ~/dev/dsh-stateful-responses。
在目标 profile(~/.dsh/profiles/<name>/package.json)中添加依赖与 bundle:
{
"dependencies": {
"dsh-stateful-responses": "link:/home/you/dev/dsh-stateful-responses"
},
"dsh": {
"profile": {
"bundles": ["…", "dsh-stateful-responses"]
}
}
}
在该 profile 目录执行 pnpm install。
重启 dsh(bundle 列表在启动时读取)。
插件默认不限定 baseURL 与模型(空数组 = 全部命中)。生产使用时建议把
baseURLs/models收窄到真正支持 Responses 状态的上游与模型,避免向其它端点发送store: true。
| 键 | 类型 | 默认 | 说明 |
|---|---|---|---|
enabled |
boolean | true |
关闭后不安装拦截器 |
forceStore |
boolean | true |
改写时强制 store: true |
baseURLs |
string[] | [] |
只拦截这些 URL 前缀下的 /responses;空 = 全部 |
models |
string[] | [] |
只拦截这些模型 id;支持结尾 * 通配;空 = 全部 |
maxChains |
positive integer | 32 |
最多保留多少条会话链 |
debug |
boolean | false |
输出每次改写 / 回退的决策日志 |
示例(cordis.patch.yml):
- id: stateful-responses
name: dsh-stateful-responses
config:
enabled: true
forceStore: true
baseURLs: ["https://api.openai.com/v1"]
models: ["gpt-*"]
maxChains: 32
debug: false
配置是严格的:未知键或类型错误会在激活时输出 warn,并且插件不会启用,而不是静默忽略。
debug: true 时会输出:
rewriting request:本轮改写为增量(模型、item 数、delta 数、previous_response_id);chain updated:链已推进(response id、item 总数、本轮是全量还是增量);continuation rejected by upstream; retrying once with full history:续传被拒并已回退;response did not complete; chain dropped:失败响应导致链丢弃(上游仍被视为可用)。每个 baseURL 只会出现一次的 info 级提示:
upstream does not retain responses (store is not true); using full history from now on
启用时输出 stateful Responses API interceptor installed;卸载时输出 restoring global fetch 以及本次
进程累计统计:requests / continuations / deltaItems / fullItems / fallbacks /
unsupportedBases / errors / chains。
没有命中任何条件时,插件不产生日志,也不改变请求。
index.js 插件入口(name + apply)
lib/config.js 严格配置
lib/items.js item 规范化与哈希
lib/chain.js 有界 LRU
lib/interceptor.js fetch 拦截、改写、追踪、回退
lib/sse.js SSE 终止事件收集
cordis.patch.yml bundle patch(insert 行)
test/ node:test 用例(本地 mock 上游)
运行测试:
npm test # 等价于 node --test test/*.test.mjs
测试覆盖:
previous_response_id 正确串联;previous_response_id:完全不处理;prompt_cache_key 隔离;失败响应不误标记;配置校验;item 规范化;LRU 淘汰;SSE 分帧。无第三方依赖,使用 Node.js 内置 node:test。
Request 对象或流式请求体(pi-ai 当前不使用这些形式)。MIT © Zhexuan Yang
CLASSIFICATION EVIDENCE
系统优先读取 GitHub Topics,再与站内分类词典和词根规则比对。当前命中: openai。