@narumitw/pi-usage 0.60.7 → 0.60.9

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/src/codex-fast.ts CHANGED
@@ -5,120 +5,110 @@ export const CODEX_FAST_SERVICE_TIER = "priority";
5
5
  export const CODEX_STANDARD_SERVICE_TIER = "default";
6
6
 
7
7
  export const CODEX_FAST_MODEL_IDS: ReadonlySet<string> = new Set([
8
- "gpt-5.4",
9
- "gpt-5.5",
10
- "gpt-5.6-luna",
11
- "gpt-5.6-sol",
12
- "gpt-5.6-terra",
8
+ "gpt-5.5",
9
+ "gpt-5.6-luna",
10
+ "gpt-5.6-sol",
11
+ "gpt-5.6-terra",
13
12
  ]);
14
13
 
15
14
  export type CodexFastAvailability =
16
- | { kind: "available"; enabled: boolean }
17
- | { kind: "not-codex" }
18
- | { kind: "unavailable"; reason: string };
15
+ | { kind: "available"; enabled: boolean }
16
+ | { kind: "not-codex" }
17
+ | { kind: "unavailable"; reason: string };
19
18
 
20
- export function codexFastAvailability(
21
- model: PiModel | undefined,
22
- enabled: boolean,
23
- ): CodexFastAvailability {
24
- if (model?.provider !== "openai-codex") return { kind: "not-codex" };
25
- if (!isOfficialCodexModel(model)) {
26
- return {
27
- kind: "unavailable",
28
- reason: "Fast mode requires the official OpenAI Codex Responses endpoint.",
29
- };
30
- }
31
- if (!CODEX_FAST_MODEL_IDS.has(model.id)) {
32
- return {
33
- kind: "unavailable",
34
- reason: `${model.id} does not advertise Codex Fast support.`,
35
- };
36
- }
37
- return { kind: "available", enabled };
19
+ export function codexFastAvailability(model: PiModel | undefined, enabled: boolean): CodexFastAvailability {
20
+ if (model?.provider !== "openai-codex") return { kind: "not-codex" };
21
+ if (!isOfficialCodexModel(model)) {
22
+ return {
23
+ kind: "unavailable",
24
+ reason: "Fast mode requires the official OpenAI Codex Responses endpoint.",
25
+ };
26
+ }
27
+ if (!CODEX_FAST_MODEL_IDS.has(model.id)) {
28
+ return {
29
+ kind: "unavailable",
30
+ reason: `${model.id} does not advertise Codex Fast support.`,
31
+ };
32
+ }
33
+ return { kind: "available", enabled };
38
34
  }
39
35
 
40
36
  export function codexFastIsEffective(model: PiModel | undefined, enabled: boolean): boolean {
41
- return codexFastAvailability(model, enabled).kind === "available" && enabled;
37
+ return codexFastAvailability(model, enabled).kind === "available" && enabled;
42
38
  }
43
39
 
44
40
  export function codexFastRequestTier(
45
- model: PiModel | undefined,
46
- enabled: boolean,
41
+ model: PiModel | undefined,
42
+ enabled: boolean,
47
43
  ): typeof CODEX_FAST_SERVICE_TIER | typeof CODEX_STANDARD_SERVICE_TIER | undefined {
48
- if (!isOfficialCodexModel(model)) return undefined;
49
- return enabled && CODEX_FAST_MODEL_IDS.has(model.id)
50
- ? CODEX_FAST_SERVICE_TIER
51
- : CODEX_STANDARD_SERVICE_TIER;
44
+ if (!isOfficialCodexModel(model)) return undefined;
45
+ return enabled && CODEX_FAST_MODEL_IDS.has(model.id) ? CODEX_FAST_SERVICE_TIER : CODEX_STANDARD_SERVICE_TIER;
52
46
  }
53
47
 
54
48
  export function rewriteCodexFastPayload(
55
- payload: unknown,
56
- model: PiModel | undefined,
57
- enabled: boolean,
49
+ payload: unknown,
50
+ model: PiModel | undefined,
51
+ enabled: boolean,
58
52
  ): unknown | undefined {
59
- const serviceTier = codexFastRequestTier(model, enabled);
60
- if (!serviceTier || !isRecord(payload)) return undefined;
61
- return { ...payload, service_tier: serviceTier };
53
+ const serviceTier = codexFastRequestTier(model, enabled);
54
+ if (!serviceTier || !isRecord(payload)) return undefined;
55
+ return { ...payload, service_tier: serviceTier };
62
56
  }
63
57
 
64
58
  export function correctCodexFastMessageCost(
65
- message: unknown,
66
- model: PiModel | undefined,
67
- fastRequested: boolean,
59
+ message: unknown,
60
+ model: PiModel | undefined,
61
+ fastRequested: boolean,
68
62
  ): unknown | undefined {
69
- if (
70
- !codexFastIsEffective(model, fastRequested) ||
71
- !isRecord(message) ||
72
- message.role !== "assistant" ||
73
- message.provider !== model?.provider ||
74
- message.model !== model?.id
75
- ) {
76
- return undefined;
77
- }
78
- const usage = isRecord(message.usage) ? message.usage : undefined;
79
- const cost = usage && isRecord(usage.cost) ? usage.cost : undefined;
80
- if (!usage || !cost || !hasCompleteUsage(usage) || !isOfficialCodexModel(model)) return undefined;
81
- const correctedUsage = structuredClone(usage) as typeof usage;
82
- calculateCost(model, correctedUsage as never);
83
- const multiplier = model.id === "gpt-5.5" ? 2.5 : 2;
84
- const correctedCost = correctedUsage.cost as Record<string, number>;
85
- for (const key of ["input", "output", "cacheRead", "cacheWrite", "total"] as const) {
86
- correctedCost[key] *= multiplier;
87
- }
88
- if (costsEqual(cost, correctedCost)) return undefined;
89
- return { ...message, usage: correctedUsage };
63
+ if (
64
+ !codexFastIsEffective(model, fastRequested) ||
65
+ !isRecord(message) ||
66
+ message.role !== "assistant" ||
67
+ message.provider !== model?.provider ||
68
+ message.model !== model?.id
69
+ ) {
70
+ return undefined;
71
+ }
72
+ const usage = isRecord(message.usage) ? message.usage : undefined;
73
+ const cost = usage && isRecord(usage.cost) ? usage.cost : undefined;
74
+ if (!usage || !cost || !hasCompleteUsage(usage) || !isOfficialCodexModel(model)) return undefined;
75
+ const correctedUsage = structuredClone(usage) as typeof usage;
76
+ calculateCost(model, correctedUsage as never);
77
+ const multiplier = model.id === "gpt-5.5" ? 2.5 : 2;
78
+ const correctedCost = correctedUsage.cost as Record<string, number>;
79
+ for (const key of ["input", "output", "cacheRead", "cacheWrite", "total"] as const) {
80
+ correctedCost[key] *= multiplier;
81
+ }
82
+ if (costsEqual(cost, correctedCost)) return undefined;
83
+ return { ...message, usage: correctedUsage };
90
84
  }
91
85
 
92
86
  export function codexFastStatusLabel(status: string, enabled: boolean): string {
93
- if (!enabled || !/^codex(?:\s|$)/u.test(status)) return status;
94
- return status === "codex" ? "codex fast" : `codex fast${status.slice("codex".length)}`;
87
+ if (!enabled || !/^codex(?:\s|$)/u.test(status)) return status;
88
+ return status === "codex" ? "codex fast" : `codex fast${status.slice("codex".length)}`;
95
89
  }
96
90
 
97
- function isOfficialCodexModel(
98
- model: PiModel | undefined,
99
- ): model is PiModel & { api: "openai-codex-responses" } {
100
- if (model?.provider !== "openai-codex" || !hasApi(model, "openai-codex-responses")) {
101
- return false;
102
- }
103
- try {
104
- return new URL(model.baseUrl).origin === "https://chatgpt.com";
105
- } catch {
106
- return false;
107
- }
91
+ function isOfficialCodexModel(model: PiModel | undefined): model is PiModel & { api: "openai-codex-responses" } {
92
+ if (model?.provider !== "openai-codex" || !hasApi(model, "openai-codex-responses")) {
93
+ return false;
94
+ }
95
+ try {
96
+ return new URL(model.baseUrl).origin === "https://chatgpt.com";
97
+ } catch {
98
+ return false;
99
+ }
108
100
  }
109
101
 
110
102
  function isRecord(value: unknown): value is Record<string, unknown> {
111
- return typeof value === "object" && value !== null && !Array.isArray(value);
103
+ return typeof value === "object" && value !== null && !Array.isArray(value);
112
104
  }
113
105
 
114
106
  function hasCompleteUsage(value: Record<string, unknown>): boolean {
115
- return ["input", "output", "cacheRead", "cacheWrite"].every(
116
- (key) => typeof value[key] === "number" && Number.isFinite(value[key]),
117
- );
107
+ return ["input", "output", "cacheRead", "cacheWrite"].every(
108
+ (key) => typeof value[key] === "number" && Number.isFinite(value[key]),
109
+ );
118
110
  }
119
111
 
120
112
  function costsEqual(left: Record<string, unknown>, right: Record<string, number>): boolean {
121
- return ["input", "output", "cacheRead", "cacheWrite", "total"].every(
122
- (key) => left[key] === right[key],
123
- );
113
+ return ["input", "output", "cacheRead", "cacheWrite", "total"].every((key) => left[key] === right[key]);
124
114
  }