从 ~/.pi/agent/extensions/ 的本地实验文件收编为独立 pi 包: - extensions/commandcode-go.ts:provider commandcode-go,只走 CLI 内部端点 POST /alpha/generate;启动拉 /provider/v1/models 并过滤出 Go 套餐可用的 DeepSeek 模型;凭据按 env → ~/.commandcode/auth.json → pi auth.json 顺序验活 - extensions/commandcode-transient-retry.ts:把网关签名明确的偶发 400 改写为 可重试,复用 pi 内置退避重试 - 文档:README(安装/凭据/模型/风险/排查)、docs/protocol.md(线协议)、 LICENSE(MIT + 上游署名)
57 lines
2.6 KiB
TypeScript
57 lines
2.6 KiB
TypeScript
/**
|
|
* Auto-retry Command Code's transient HTTP 400s.
|
|
*
|
|
* Command Code's gateway occasionally rejects an otherwise valid request with
|
|
* a generic, server-side 400 — on the native Provider API
|
|
* (`/provider/v1/responses`) and on the `/alpha/generate` fallback alike:
|
|
*
|
|
* commandcode API error (400): {"message":"{\"type\":\"invalid_request_error\",
|
|
* \"code\":\"\",\"message\":\"invalid request error trace_id: <hex>\"}\n",
|
|
* "type":"invalid_request_error"}
|
|
*
|
|
* The request is well formed and the identical payload succeeds on replay, so
|
|
* this is a transient upstream failure, not a client error. Pi only retries
|
|
* assistant errors whose message matches its transient-error classifier
|
|
* (`isRetryableAssistantError`), and a bare 400 does not match. Rewriting the
|
|
* finalized message to carry the classifier's "server error" marker lets Pi's
|
|
* built-in auto-retry (settings.retry: 3 attempts with backoff) handle it
|
|
* transparently.
|
|
*
|
|
* Scope is deliberately narrow — provider `commandcode` + HTTP 400 + the
|
|
* gateway's `invalid_request_error` signature — so genuine client-side 400s
|
|
* are not blanket-retried.
|
|
*/
|
|
|
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"
|
|
|
|
const COMMAND_CODE_ERROR = /\bcommand[ -]?code API error\b/i
|
|
const COMMAND_CODE_PROVIDERS = new Set(["commandcode", "command-code", "commandcode-go"])
|
|
const HTTP_400 = /\b400\b/
|
|
const TRANSIENT_SIGNATURE = /invalid request error trace_id|"type"\s*:\s*"invalid_request_error"/i
|
|
// Already matches one of Pi's retryable patterns: leave the message untouched.
|
|
const ALREADY_RETRYABLE =
|
|
/server.?error|internal.?error|overloaded|rate.?limit|too many requests|\b(?:408|409|429|5\d\d)\b/i
|
|
const RETRY_MARKER =
|
|
" [transient Command Code gateway error; treated as server error, auto-retrying]"
|
|
|
|
export default function (pi: ExtensionAPI) {
|
|
pi.on("message_end", async (event, ctx) => {
|
|
const message = event.message
|
|
if (!message || message.role !== "assistant") return
|
|
if (message.stopReason !== "error") return
|
|
if (
|
|
!COMMAND_CODE_PROVIDERS.has(message.provider ?? "") &&
|
|
!COMMAND_CODE_PROVIDERS.has(ctx.model?.provider ?? "")
|
|
)
|
|
return
|
|
|
|
const errorMessage = message.errorMessage
|
|
if (typeof errorMessage !== "string" || errorMessage.length === 0) return
|
|
if (!COMMAND_CODE_ERROR.test(errorMessage) || !HTTP_400.test(errorMessage)) return
|
|
if (!TRANSIENT_SIGNATURE.test(errorMessage)) return
|
|
if (ALREADY_RETRYABLE.test(errorMessage) || errorMessage.includes(RETRY_MARKER)) return
|
|
|
|
return { message: { ...message, errorMessage: errorMessage + RETRY_MARKER } }
|
|
})
|
|
}
|