@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/dist/index.ts +91 -339
- package/dist/index.ts.map +2 -2
- package/docs/providers.md +2 -1
- package/package.json +73 -74
- package/src/codex-fast-runtime.ts +148 -184
- package/src/codex-fast.ts +74 -83
- package/src/codex-resets.ts +249 -278
- package/src/core.ts +204 -206
- package/src/format.ts +519 -574
- package/src/index.ts +77 -77
- package/src/oauth-credential-source.ts +57 -65
- package/src/providers/baseten.ts +32 -36
- package/src/providers/codex.ts +109 -120
- package/src/providers/deepseek.ts +59 -64
- package/src/providers/fireworks.ts +219 -238
- package/src/providers/github-copilot.ts +89 -95
- package/src/providers/kimi-coding.ts +202 -213
- package/src/providers/minimax.ts +214 -219
- package/src/providers/moonshot.ts +41 -46
- package/src/providers/opencode-zen.ts +56 -59
- package/src/providers/openrouter.ts +50 -53
- package/src/providers/vercel-ai-gateway.ts +33 -33
- package/src/providers/xai.ts +141 -148
- package/src/providers/zai-errors.ts +75 -75
- package/src/providers/zai.ts +173 -189
- package/src/query.ts +887 -967
- package/src/settings.ts +275 -304
- package/src/types.ts +145 -145
- package/src/usage-helpers.ts +18 -18
- package/src/usage-settings-ui.ts +117 -126
- package/src/usage-targets.ts +98 -114
- package/src/usage.ts +1264 -1426
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
| { kind: "available"; enabled: boolean }
|
|
17
|
+
| { kind: "not-codex" }
|
|
18
|
+
| { kind: "unavailable"; reason: string };
|
|
19
19
|
|
|
20
|
-
export function codexFastAvailability(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
38
|
+
return codexFastAvailability(model, enabled).kind === "available" && enabled;
|
|
42
39
|
}
|
|
43
40
|
|
|
44
41
|
export function codexFastRequestTier(
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
model: PiModel | undefined,
|
|
43
|
+
enabled: boolean,
|
|
47
44
|
): typeof CODEX_FAST_SERVICE_TIER | typeof CODEX_STANDARD_SERVICE_TIER | undefined {
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
payload: unknown,
|
|
51
|
+
model: PiModel | undefined,
|
|
52
|
+
enabled: boolean,
|
|
58
53
|
): unknown | undefined {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
60
|
+
message: unknown,
|
|
61
|
+
model: PiModel | undefined,
|
|
62
|
+
fastRequested: boolean,
|
|
68
63
|
): unknown | undefined {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
94
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
104
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
112
105
|
}
|
|
113
106
|
|
|
114
107
|
function hasCompleteUsage(value: Record<string, unknown>): boolean {
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
122
|
-
(key) => left[key] === right[key],
|
|
123
|
-
);
|
|
114
|
+
return ["input", "output", "cacheRead", "cacheWrite", "total"].every((key) => left[key] === right[key]);
|
|
124
115
|
}
|