@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/README.md +1 -1
- package/dist/index.ts +91 -340
- 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 +73 -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,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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
| { kind: "available"; enabled: boolean }
|
|
16
|
+
| { kind: "not-codex" }
|
|
17
|
+
| { kind: "unavailable"; reason: string };
|
|
19
18
|
|
|
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 };
|
|
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
|
-
|
|
37
|
+
return codexFastAvailability(model, enabled).kind === "available" && enabled;
|
|
42
38
|
}
|
|
43
39
|
|
|
44
40
|
export function codexFastRequestTier(
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
model: PiModel | undefined,
|
|
42
|
+
enabled: boolean,
|
|
47
43
|
): typeof CODEX_FAST_SERVICE_TIER | typeof CODEX_STANDARD_SERVICE_TIER | undefined {
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
49
|
+
payload: unknown,
|
|
50
|
+
model: PiModel | undefined,
|
|
51
|
+
enabled: boolean,
|
|
58
52
|
): unknown | undefined {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
59
|
+
message: unknown,
|
|
60
|
+
model: PiModel | undefined,
|
|
61
|
+
fastRequested: boolean,
|
|
68
62
|
): unknown | undefined {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
94
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
103
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
112
104
|
}
|
|
113
105
|
|
|
114
106
|
function hasCompleteUsage(value: Record<string, unknown>): boolean {
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
122
|
-
(key) => left[key] === right[key],
|
|
123
|
-
);
|
|
113
|
+
return ["input", "output", "cacheRead", "cacheWrite", "total"].every((key) => left[key] === right[key]);
|
|
124
114
|
}
|