@lyogvce/dsh-codex-subscription 0.2.11 → 0.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,8 +19,8 @@
19
19
  - 在插件配置中通过 `disabledModels` 屏蔽指定模型 `slug`,保持模型选择器干净清爽。
20
20
  - 通过 `contextWindows` 按模型 `slug` 自定义向 DSH 会话与自动压缩器暴露的预算窗口(例如设为 `400000`);未配置时默认使用模型物理上限 `max_context_window`(`872000`)。同时底层 `PiCodexModel.contextWindow` 始终保持物理上限 `872000`,彻底防止 `dsh-llm-pi-ai` 在预算窗口与物理上限之间误判 `CONTEXT_WINDOW_EXCEEDED`。
21
21
  - 通过 `retryPolicy` 暴露 DSH 原生 `RetryPolicySchema`(支持 `normal` / `always` 模式、`maxRetries`、`retryableCodes` 与 `backoff`,并随 `registration.replace` 热更新生效)。未配置时保持宿主默认策略(`mode: "normal"`、`maxRetries: 5`、默认 5 类错误码 `EMPTY_RESPONSE / RATE_LIMIT / SERVER / TIMEOUT / TRANSPORT` 及 `500ms..10000ms` 退避)。**注意:`retryableCodes` 为完整覆盖集合而非追加列表**,显式配置时需同时列出默认错误码与新增错误码。
22
- 4. **原生多轮思考链回放与工具调用**
23
- - 底层复用 DSH 内置的 `PiAiAdapter` 与 `@earendil-works/pi-ai` 的 `openaiCodexProvider()`,固定使用 `sse` 传输(含 `zstd` 请求体压缩、规范化纯 UUID `session-id` / `x-client-request-id` 缓存头、`reasoning.encrypted_content` 签名回放与图片自动缩放卸载)。
22
+ 4. **原生多轮思考链回放、工具调用与出站指纹净化**
23
+ - 底层复用 DSH 内置的 `PiAiAdapter` 与 `@earendil-works/pi-ai` 的 `openaiCodexProvider()`,固定使用 `sse` 传输(含 `zstd` 请求体压缩、规范化 36 位 RFC 4122 UUID `session-id` 缓存头与 `prompt_cache_key`、`reasoning.encrypted_content` 签名回放与图片自动缩放卸载),并通过自定义 `fetch` 与 `onPayload` 剥除 `OpenAI-Beta: responses=experimental`、静态 `x-client-request-id`、`msg_pi_*` 等非官方指纹。
24
24
  5. **Codex 订阅限额内联展示与 ⚡ Fast 极速模式一键切换(`conversation.input.right`)**
25
25
  - 当会话选中 `openai-codex` 模型时,在输入框右下角紧邻模型选择器左侧展示限额胶囊(根据 `wham/usage` 返回的 `limit_window_seconds` 自适应展示 Plus 的 `5h X% · 7d Y%` 双窗口或 Pro 的 `7d X%` 单窗口,内置 60 秒缓存、单飞合并与每轮回复结束自动刷新),点击可展开查看对应窗口进度条、重置倒计时与手动刷新按钮;切换至其他 Provider 模型时自动隐藏。
26
26
  - **⚡ 会话树级 Fast 极速模式(`service_tier: "priority"`,最高 2x 推理速度,主会话与 Subagent / 智能体团队同开同关)**:每个主会话可在限额弹窗底部独立开关 Fast 模式(不同主会话互不影响,新会话默认继承插件设置中的 `fastMode`,未配置时默认 `false` 关闭),该主会话派生的所有 `subagent`、`subagent_fork`、`workflow` 子任务以及智能体团队成员(`spawn_teammate`)均自动绑定至同一主会话开关,实时联动、同开同关;状态自动持久化至 `~/.codex/cache/dsh-codex-subscription/runtime-state.json`。开启后胶囊显示闪电标识 `⚡`,并在请求体中自动注入 `"service_tier": "priority"`。
@@ -59,7 +59,7 @@ dsh plugin --profile desktop add @lyogvce/dsh-codex-subscription
59
59
  retryPolicy:
60
60
  mode: normal
61
61
  maxRetries: 5
62
- retryableCodes: [ EMPTY_RESPONSE, RATE_LIMIT, SERVER, TIMEOUT, TRANSPORT, QUOTA, ACCOUNT_QUOTA, AUTH, INVALID_REQUEST, PI_AI_ERROR, STREAM_CLOSED, MALFORMED_RESPONSE ]
62
+ retryableCodes: [ EMPTY_RESPONSE, RATE_LIMIT, SERVER, TIMEOUT, TRANSPORT, STREAM_CLOSED ]
63
63
  ```
64
64
 
65
65
  可在 DSH 的 **设置 → 插件 → Codex 订阅直连** 页面或 `cordis.patch.yml` 中随时修改 `disabledModels`、`contextWindows` 与 `retryPolicy`,实时热更新生效无需重启。
package/lib/index.d.ts CHANGED
@@ -59,6 +59,7 @@ interface CodexResolvedCredentials {
59
59
  declare function createAuthManager(options: AuthManagerOptions): {
60
60
  resolveCredentials: () => Promise<CodexResolvedCredentials>;
61
61
  resolveAccessToken: () => Promise<string>;
62
+ getLastKnownAccountId: () => string | undefined;
62
63
  };
63
64
  //#endregion
64
65
  //#region src/catalog.d.ts
@@ -114,8 +115,14 @@ declare function buildThinkingLevelMap(supportedLevels: unknown): Record<string,
114
115
  declare function parseModelsCache(rawCache: RawModelsCache | null | undefined, baseModels: readonly PiCodexModel[], disabledModels?: readonly string[], contextWindows?: Readonly<Record<string, number>>): ParsedModelsCatalog;
115
116
  //#endregion
116
117
  //#region src/request.d.ts
118
+ declare function toCodexWireSessionId(sessionId: string | undefined): string | undefined;
117
119
  declare function buildCodexUserAgent(version?: string): string;
118
120
  declare function buildCodexHeaders(existingHeaders?: Record<string, string> | undefined, clientVersion?: string): Record<string, string>;
121
+ interface CodexResponsesFetchOptions {
122
+ readonly baseFetch?: typeof fetch | undefined;
123
+ readonly getAccountId?: (() => string | undefined) | undefined;
124
+ }
125
+ declare function createCodexResponsesFetch(options?: CodexResponsesFetchOptions): typeof fetch;
119
126
  declare function sanitizeCodexPayload(payload: Record<string, unknown> | null | undefined, modelMeta?: CodexModelMetadata | undefined, fastMode?: boolean): Record<string, unknown> | null | undefined;
120
127
  //#endregion
121
128
  //#region src/usage.d.ts
@@ -152,6 +159,7 @@ interface UsageFetcherOptions {
152
159
  readonly fetchImpl?: typeof fetch | undefined;
153
160
  readonly now?: (() => number) | undefined;
154
161
  readonly ttlMs?: number | undefined;
162
+ readonly minForceIntervalMs?: number | undefined;
155
163
  }
156
164
  declare function parseUsagePayload(raw: unknown, fetchedAt: number): CodexQuotaSnapshot | null;
157
165
  declare function createUsageFetcher(options: UsageFetcherOptions): {
@@ -185,7 +193,9 @@ export declare const __test: Readonly<{
185
193
  sanitizeCodexPayload: typeof sanitizeCodexPayload;
186
194
  buildCodexHeaders: typeof buildCodexHeaders;
187
195
  buildCodexUserAgent: typeof buildCodexUserAgent;
196
+ createCodexResponsesFetch: typeof createCodexResponsesFetch;
188
197
  normalizeCodexSessionId: typeof normalizeCodexSessionId;
198
+ toCodexWireSessionId: typeof toCodexWireSessionId;
189
199
  parseUsagePayload: typeof parseUsagePayload;
190
200
  createUsageFetcher: typeof createUsageFetcher;
191
201
  }>;
package/lib/index.js CHANGED
@@ -4,7 +4,7 @@ import z from "@deepseek-ai/schemastery";
4
4
  import { LlmError, RetryPolicySchema, resolveImageAttachmentAccess, resolveRetryPolicy } from "@deepseek-ai/dsh-llm";
5
5
  import { PiAiAdapter } from "@deepseek-ai/dsh-llm-pi-ai";
6
6
  import { openaiCodexProvider } from "@earendil-works/pi-ai/providers/openai-codex";
7
- import { randomUUID } from "node:crypto";
7
+ import { createHash, randomUUID } from "node:crypto";
8
8
  import { chmod, rename, writeFile } from "node:fs/promises";
9
9
  import { arch, homedir, platform, release } from "node:os";
10
10
  import { execFileSync } from "node:child_process";
@@ -155,8 +155,15 @@ function parseModelsCache(rawCache, baseModels, disabledModels = [], contextWind
155
155
  defaultReasoningLevel,
156
156
  fastServiceTier
157
157
  });
158
+ const rawCompat = base?.compat;
159
+ let sanitizedCompat;
160
+ if (rawCompat && typeof rawCompat === "object" && !Array.isArray(rawCompat)) {
161
+ const { supportsAdditionalTools: _supportsAdditionalTools, supportsToolSearch: _supportsToolSearch, ...restCompat } = rawCompat;
162
+ sanitizedCompat = restCompat;
163
+ }
158
164
  models.push({
159
165
  ...base ?? {},
166
+ ...sanitizedCompat !== void 0 ? { compat: sanitizedCompat } : {},
160
167
  id,
161
168
  name: typeof entry.display_name === "string" && entry.display_name.trim() !== "" ? entry.display_name.trim() : id,
162
169
  ...typeof entry.description === "string" && entry.description.trim() !== "" ? { description: entry.description.trim() } : {},
@@ -184,6 +191,33 @@ function parseModelsCache(rawCache, baseModels, disabledModels = [], contextWind
184
191
  }
185
192
  //#endregion
186
193
  //#region src/request.ts
194
+ const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu;
195
+ const FORBIDDEN_HEADER_NAMES = new Set([
196
+ "user-agent",
197
+ "openai-beta",
198
+ "version",
199
+ "x-client-request-id",
200
+ "http-referer",
201
+ "x-title"
202
+ ]);
203
+ function isForbiddenHeaderName(name) {
204
+ const lower = name.toLowerCase();
205
+ return FORBIDDEN_HEADER_NAMES.has(lower) || lower.startsWith("x-stainless-");
206
+ }
207
+ function formatDeterministicUuid(seed) {
208
+ const digest = createHash("sha256").update(seed).digest();
209
+ const bytes = Uint8Array.from(digest.subarray(0, 16));
210
+ bytes[6] = bytes[6] & 15 | 80;
211
+ bytes[8] = bytes[8] & 63 | 128;
212
+ const hex = Buffer.from(bytes).toString("hex");
213
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
214
+ }
215
+ function toCodexWireSessionId(sessionId) {
216
+ const normalized = normalizeCodexSessionId(sessionId);
217
+ if (normalized === void 0) return void 0;
218
+ if (UUID_REGEX.test(normalized)) return normalized.toLowerCase();
219
+ return formatDeterministicUuid(`codex-session:${normalized}`);
220
+ }
187
221
  const FORBIDDEN_PAYLOAD_FIELDS = Object.freeze([
188
222
  "max_output_tokens",
189
223
  "max_completion_tokens",
@@ -225,18 +259,62 @@ function buildCodexUserAgent(version = DEFAULT_CLIENT_VERSION) {
225
259
  function buildCodexHeaders(existingHeaders, clientVersion = DEFAULT_CLIENT_VERSION) {
226
260
  const headers = {};
227
261
  for (const [key, value] of Object.entries(existingHeaders ?? {})) {
228
- if (key.toLowerCase() === "user-agent") continue;
262
+ if (isForbiddenHeaderName(key)) continue;
229
263
  headers[key] = value;
230
264
  }
231
265
  headers.originator = "codex_cli_rs";
232
- headers.version = clientVersion;
233
266
  headers["User-Agent"] = buildCodexUserAgent(clientVersion);
234
267
  return headers;
235
268
  }
269
+ function createCodexResponsesFetch(options = {}) {
270
+ const fetchFn = options.baseFetch ?? globalThis.fetch;
271
+ return async (input, init) => {
272
+ const headers = new Headers(init?.headers);
273
+ for (const key of [...headers.keys()]) {
274
+ if (key.toLowerCase() === "user-agent") continue;
275
+ if (isForbiddenHeaderName(key)) headers.delete(key);
276
+ }
277
+ const accountId = options.getAccountId?.();
278
+ if (typeof accountId === "string" && accountId.trim() !== "") headers.set("chatgpt-account-id", accountId.trim());
279
+ return fetchFn(input, {
280
+ ...init,
281
+ headers
282
+ });
283
+ };
284
+ }
285
+ function sanitizeInputItem(item) {
286
+ if (!item || typeof item !== "object" || Array.isArray(item)) return item;
287
+ const record = item;
288
+ let patched;
289
+ if (typeof record.id === "string" && /^msg_pi_/iu.test(record.id)) patched = {
290
+ ...patched ?? record,
291
+ id: `msg_${createHash("sha256").update(record.id).digest("hex").slice(0, 48)}`
292
+ };
293
+ if (typeof record.call_id === "string" && /^pi_tool_load_/iu.test(record.call_id)) patched = {
294
+ ...patched ?? record,
295
+ call_id: `call_${createHash("sha256").update(record.call_id).digest("hex").slice(0, 24)}`
296
+ };
297
+ return patched ?? item;
298
+ }
236
299
  function sanitizeCodexPayload(payload, modelMeta, fastMode = false) {
237
300
  if (!payload || typeof payload !== "object") return payload;
238
301
  const cleaned = { ...payload };
239
302
  for (const field of FORBIDDEN_PAYLOAD_FIELDS) delete cleaned[field];
303
+ if (typeof cleaned.prompt_cache_key === "string") {
304
+ const wireKey = toCodexWireSessionId(cleaned.prompt_cache_key);
305
+ if (wireKey !== void 0) cleaned.prompt_cache_key = wireKey;
306
+ else delete cleaned.prompt_cache_key;
307
+ }
308
+ if (!Array.isArray(cleaned.tools) || cleaned.tools.length === 0) {
309
+ delete cleaned.tools;
310
+ delete cleaned.parallel_tool_calls;
311
+ delete cleaned.tool_choice;
312
+ }
313
+ if (Array.isArray(cleaned.input)) cleaned.input = cleaned.input.filter((item) => {
314
+ if (!item || typeof item !== "object" || Array.isArray(item)) return true;
315
+ const t = item.type;
316
+ return t !== "additional_tools" && t !== "tool_search_call" && t !== "tool_search_output";
317
+ }).map(sanitizeInputItem);
240
318
  if (fastMode) {
241
319
  const tier = modelMeta === void 0 ? "priority" : modelMeta.fastServiceTier;
242
320
  if (typeof tier === "string" && tier !== "") cleaned.service_tier = tier;
@@ -328,6 +406,7 @@ function createUsageFetcher(options) {
328
406
  const fetchFn = options.fetchImpl ?? globalThis.fetch;
329
407
  const nowFn = options.now ?? Date.now;
330
408
  const ttlMs = options.ttlMs ?? 6e4;
409
+ const minForceIntervalMs = options.minForceIntervalMs ?? 5e3;
331
410
  let cached;
332
411
  let stale = false;
333
412
  let inflight;
@@ -374,10 +453,17 @@ function createUsageFetcher(options) {
374
453
  stale = true;
375
454
  },
376
455
  async getUsage(force = false) {
377
- if (!force && !stale && cached !== void 0 && nowFn() - cached.fetchedAt < ttlMs) return {
378
- ok: true,
379
- quota: cached
380
- };
456
+ if (!stale && cached !== void 0) {
457
+ const ageMs = nowFn() - cached.fetchedAt;
458
+ if (!force && ageMs < ttlMs) return {
459
+ ok: true,
460
+ quota: cached
461
+ };
462
+ if (force && ageMs >= 0 && ageMs < minForceIntervalMs) return {
463
+ ok: true,
464
+ quota: cached
465
+ };
466
+ }
381
467
  if (!inflight) inflight = fetchFresh().finally(() => {
382
468
  inflight = void 0;
383
469
  });
@@ -462,6 +548,7 @@ function createAuthManager(options) {
462
548
  const fetchFn = options.fetchImpl ?? globalThis.fetch;
463
549
  const nowFn = options.now ?? Date.now;
464
550
  let inflightRefresh;
551
+ let lastKnownAccountId;
465
552
  let memoryFallback;
466
553
  function readAuthState() {
467
554
  const authPath = join(getCodexHome(), "auth.json");
@@ -525,8 +612,7 @@ function createAuthManager(options) {
525
612
  body: JSON.stringify({
526
613
  client_id: CODEX_CLIENT_ID,
527
614
  grant_type: "refresh_token",
528
- refresh_token: refreshTok,
529
- scope: "openid profile email"
615
+ refresh_token: refreshTok
530
616
  }),
531
617
  signal: AbortSignal.timeout(TOKEN_REFRESH_TIMEOUT_MS)
532
618
  });
@@ -555,11 +641,13 @@ function createAuthManager(options) {
555
641
  parsed: nextAuth,
556
642
  accessToken: fresh.access_token
557
643
  };
644
+ const refreshedAccountId = extractAccountId(nextAuth, fresh.access_token);
645
+ lastKnownAccountId = refreshedAccountId;
558
646
  try {
559
647
  await writeJsonAtomic(authPath, nextAuth);
560
648
  options.onLog?.("INFO", "auth.refreshed", {
561
649
  authPath,
562
- accountId: extractAccountId(nextAuth, fresh.access_token) ?? null
650
+ accountId: refreshedAccountId ?? null
563
651
  });
564
652
  } catch (error) {
565
653
  const errMsg = safeErrorMessage(error);
@@ -571,13 +659,15 @@ function createAuthManager(options) {
571
659
  }
572
660
  return {
573
661
  accessToken: fresh.access_token,
574
- accountId: extractAccountId(nextAuth, fresh.access_token)
662
+ accountId: refreshedAccountId
575
663
  };
576
664
  }
577
665
  function toCredentials(state) {
666
+ const accountId = extractAccountId(state.parsed, state.accessToken);
667
+ lastKnownAccountId = accountId;
578
668
  return {
579
669
  accessToken: state.accessToken,
580
- accountId: extractAccountId(state.parsed, state.accessToken)
670
+ accountId
581
671
  };
582
672
  }
583
673
  async function resolveCredentials() {
@@ -596,6 +686,16 @@ function createAuthManager(options) {
596
686
  resolveCredentials,
597
687
  async resolveAccessToken() {
598
688
  return (await resolveCredentials()).accessToken;
689
+ },
690
+ getLastKnownAccountId() {
691
+ if (lastKnownAccountId !== void 0) return lastKnownAccountId;
692
+ try {
693
+ const current = readAuthState();
694
+ lastKnownAccountId = extractAccountId(current.parsed, current.accessToken);
695
+ return lastKnownAccountId;
696
+ } catch {
697
+ return;
698
+ }
599
699
  }
600
700
  };
601
701
  }
@@ -910,12 +1010,17 @@ function apply(ctx, config = {}) {
910
1010
  const { temperature: _temperature, sessionId, onPayload, ...rest } = options;
911
1011
  const initiatorId = (ctx.get?.("agents"))?.currentInitiator?.()?.id;
912
1012
  const normalizedSessionId = normalizeCodexSessionId(sessionId) ?? normalizeCodexSessionId(initiatorId);
1013
+ const wireSessionId = toCodexWireSessionId(normalizedSessionId);
913
1014
  resolveRootSessionKey(normalizedSessionId);
914
1015
  return {
915
1016
  ...rest,
916
- ...normalizedSessionId !== void 0 ? { sessionId: normalizedSessionId } : {},
1017
+ ...wireSessionId !== void 0 ? { sessionId: wireSessionId } : {},
917
1018
  transport: "sse",
918
1019
  headers: buildCodexHeaders(rest.headers, catalog.clientVersion),
1020
+ fetch: createCodexResponsesFetch({
1021
+ baseFetch: rest.fetch,
1022
+ getAccountId: () => authManager.getLastKnownAccountId()
1023
+ }),
919
1024
  async onPayload(payload, requestModel) {
920
1025
  return sanitizeCodexPayload(await onPayload?.(payload, requestModel) ?? payload, modelMeta, getEffectiveFastMode(normalizedSessionId));
921
1026
  }
@@ -1214,7 +1319,9 @@ const __test = Object.freeze({
1214
1319
  sanitizeCodexPayload,
1215
1320
  buildCodexHeaders,
1216
1321
  buildCodexUserAgent,
1322
+ createCodexResponsesFetch,
1217
1323
  normalizeCodexSessionId,
1324
+ toCodexWireSessionId,
1218
1325
  parseUsagePayload,
1219
1326
  createUsageFetcher
1220
1327
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lyogvce/dsh-codex-subscription",
3
- "version": "0.2.11",
3
+ "version": "0.2.12",
4
4
  "description": "复用本机 ~/.codex 订阅凭证与 models_cache.json 模型缓存,在 DSH 中通过 SSE 直连调用 Codex 模型。",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",