@narumitw/pi-usage 0.60.7 → 0.60.8

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