@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/usage-settings-ui.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { stripVTControlCharacters } from "node:util";
|
|
2
|
-
import {
|
|
3
|
-
type ExtensionCommandContext,
|
|
4
|
-
getSettingsListTheme,
|
|
5
|
-
} from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { type ExtensionCommandContext, getSettingsListTheme } from "@earendil-works/pi-coding-agent";
|
|
6
3
|
import { Key, matchesKey, type SettingItem, SettingsList, Text } from "@earendil-works/pi-tui";
|
|
7
4
|
import { errorMessage } from "./core.js";
|
|
8
5
|
import type { UsageSettingsRuntime } from "./settings.js";
|
|
@@ -13,129 +10,123 @@ const ON = "On";
|
|
|
13
10
|
type UsageSettingId = "codexFastMode" | "codexStatusResetCountdown";
|
|
14
11
|
|
|
15
12
|
export async function showUsageSettings(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
ctx: ExtensionCommandContext,
|
|
14
|
+
settingsRuntime: UsageSettingsRuntime,
|
|
15
|
+
parentSignal: AbortSignal,
|
|
16
|
+
isCurrent: () => boolean,
|
|
17
|
+
onApplied: (id: UsageSettingId) => void,
|
|
21
18
|
): Promise<boolean> {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
19
|
+
if (ctx.mode !== "tui") {
|
|
20
|
+
if (ctx.hasUI) ctx.ui.notify(`Edit settings manually: ${settingsRuntime.get().path}`, "info");
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
const { HorizontalRule, renderBoundedFrame } = await import("@narumitw/pi-tui-kit");
|
|
24
|
+
if (parentSignal.aborted || !isCurrent()) return false;
|
|
25
|
+
return (
|
|
26
|
+
(await ctx.ui.custom<boolean>((tui, theme, _keybindings, done) => {
|
|
27
|
+
const localController = new AbortController();
|
|
28
|
+
const signal = AbortSignal.any([parentSignal, localController.signal]);
|
|
29
|
+
let changed = false;
|
|
30
|
+
let closing = false;
|
|
31
|
+
let saveQueue = Promise.resolve();
|
|
32
|
+
const state = settingsRuntime.get();
|
|
33
|
+
const items: SettingItem[] = [
|
|
34
|
+
{
|
|
35
|
+
id: "codexFastMode",
|
|
36
|
+
label: "Codex Fast mode",
|
|
37
|
+
description: "Use faster Codex routing at increased plan allowance consumption.",
|
|
38
|
+
currentValue: state.settings.codexFastMode ? ON : OFF,
|
|
39
|
+
values: [OFF, ON],
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
id: "codexStatusResetCountdown",
|
|
43
|
+
label: "Codex reset countdown",
|
|
44
|
+
description: "Show time remaining until each Codex usage limit resets.",
|
|
45
|
+
currentValue: state.settings.codexStatusResetCountdown ? ON : OFF,
|
|
46
|
+
values: [OFF, ON],
|
|
47
|
+
},
|
|
48
|
+
];
|
|
49
|
+
const rule = new HorizontalRule({ ruleStyle: (text) => theme.fg("border", text) });
|
|
53
50
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
51
|
+
let settingsList: SettingsList;
|
|
52
|
+
const cancel = () => {
|
|
53
|
+
if (closing) return;
|
|
54
|
+
closing = true;
|
|
55
|
+
localController.abort();
|
|
56
|
+
done(changed);
|
|
57
|
+
};
|
|
58
|
+
const queueUpdate = (id: UsageSettingId, requested: boolean, display: string) => {
|
|
59
|
+
saveQueue = saveQueue.then(async () => {
|
|
60
|
+
const previous = settingsRuntime.get().settings[id];
|
|
61
|
+
if (settingsRuntime.get().kind === "invalid") {
|
|
62
|
+
settingsList.updateValue(id, previous ? ON : OFF);
|
|
63
|
+
if (!signal.aborted && isCurrent()) {
|
|
64
|
+
ctx.ui.notify("Repair pi-usage.json and reload before changing settings.", "error");
|
|
65
|
+
tui.requestRender();
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
await settingsRuntime.update({ [id]: requested }, signal);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
if (signal.aborted || !isCurrent()) return;
|
|
73
|
+
settingsList.updateValue(id, previous ? ON : OFF);
|
|
74
|
+
ctx.ui.notify(`Could not save pi-usage.json: ${errorMessage(error)}`, "error");
|
|
75
|
+
tui.requestRender();
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (previous !== requested) {
|
|
79
|
+
changed = true;
|
|
80
|
+
onApplied(id);
|
|
81
|
+
}
|
|
82
|
+
if (signal.aborted || !isCurrent()) return;
|
|
83
|
+
settingsList.updateValue(id, display);
|
|
84
|
+
tui.requestRender();
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
settingsList = new SettingsList(
|
|
88
|
+
items,
|
|
89
|
+
items.length + 2,
|
|
90
|
+
getSettingsListTheme(),
|
|
91
|
+
(id, value) => {
|
|
92
|
+
if (closing || signal.aborted || !isCurrent()) return;
|
|
93
|
+
queueUpdate(id as UsageSettingId, value !== OFF, value);
|
|
94
|
+
},
|
|
95
|
+
cancel,
|
|
96
|
+
);
|
|
100
97
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
localController.abort();
|
|
136
|
-
parentSignal.removeEventListener("abort", cancel);
|
|
137
|
-
},
|
|
138
|
-
};
|
|
139
|
-
})) ?? false
|
|
140
|
-
);
|
|
98
|
+
parentSignal.addEventListener("abort", cancel, { once: true });
|
|
99
|
+
return {
|
|
100
|
+
render(width: number) {
|
|
101
|
+
// Compatibility: Kit puts the title directly below the top rule and keeps compact
|
|
102
|
+
// rules when at least five rows fit; both replace the legacy wrapper layout.
|
|
103
|
+
const title = new Text(theme.fg("accent", theme.bold("pi-usage Settings")), 1, 0).render(width);
|
|
104
|
+
const content = settingsList.render(width);
|
|
105
|
+
const focusedRow = content.findIndex((line) => /^[→›]\s/u.test(stripVTControlCharacters(line)));
|
|
106
|
+
const terminalRows = Number.isFinite(tui.terminal?.rows) ? Math.floor(tui.terminal.rows) : 24;
|
|
107
|
+
const [ruleLine = ""] = rule.render(width);
|
|
108
|
+
return renderBoundedFrame({
|
|
109
|
+
width,
|
|
110
|
+
maxRows: Math.max(1, terminalRows - 3),
|
|
111
|
+
rule: ruleLine,
|
|
112
|
+
title,
|
|
113
|
+
content,
|
|
114
|
+
priorityRows: focusedRow < 0 ? [] : [focusedRow],
|
|
115
|
+
focusedRow,
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
invalidate: () => settingsList.invalidate(),
|
|
119
|
+
handleInput(data: string) {
|
|
120
|
+
if (closing) return;
|
|
121
|
+
if (matchesKey(data, Key.ctrl("c"))) cancel();
|
|
122
|
+
else settingsList.handleInput(data);
|
|
123
|
+
tui.requestRender();
|
|
124
|
+
},
|
|
125
|
+
dispose() {
|
|
126
|
+
localController.abort();
|
|
127
|
+
parentSignal.removeEventListener("abort", cancel);
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
})) ?? false
|
|
131
|
+
);
|
|
141
132
|
}
|
package/src/usage-targets.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import { sanitizeDisplayText } from "./core.js";
|
|
2
|
-
import type {
|
|
3
|
-
ResolvedUsageAuth,
|
|
4
|
-
UsageProviderAdapter,
|
|
5
|
-
UsageProviderTarget,
|
|
6
|
-
UsageRequestGuard,
|
|
7
|
-
} from "./types.js";
|
|
2
|
+
import type { ResolvedUsageAuth, UsageProviderAdapter, UsageProviderTarget, UsageRequestGuard } from "./types.js";
|
|
8
3
|
|
|
9
4
|
const MAX_TARGETS = 1_000;
|
|
10
5
|
const MAX_TARGET_ID_CHARS = 256;
|
|
@@ -12,132 +7,121 @@ const MAX_TARGET_LABEL_CHARS = 120;
|
|
|
12
7
|
const MAX_TARGET_DESCRIPTION_CHARS = 180;
|
|
13
8
|
|
|
14
9
|
export type UsageTargetResolution =
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
| { kind: "selected"; targetId?: string }
|
|
11
|
+
| {
|
|
12
|
+
kind: "selection-required";
|
|
13
|
+
choices: readonly UsageProviderTarget[];
|
|
14
|
+
};
|
|
20
15
|
|
|
21
16
|
export interface UsageTargetSelectOptions {
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
options: readonly string[];
|
|
18
|
+
targetIdFor(option: string): string | undefined;
|
|
24
19
|
}
|
|
25
20
|
|
|
26
21
|
export async function resolveUsageTarget(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
22
|
+
adapter: UsageProviderAdapter,
|
|
23
|
+
auth: ResolvedUsageAuth,
|
|
24
|
+
rememberedTargetId: string | undefined,
|
|
25
|
+
signal: AbortSignal,
|
|
26
|
+
timeoutMs: number,
|
|
27
|
+
guard: UsageRequestGuard,
|
|
33
28
|
): Promise<UsageTargetResolution> {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
29
|
+
if (!adapter.targets) return { kind: "selected" };
|
|
30
|
+
if (rememberedTargetId !== undefined && !isBoundedTargetId(rememberedTargetId)) {
|
|
31
|
+
throw new Error(`The remembered ${adapter.targets.singularLabel} identifier was invalid.`);
|
|
32
|
+
}
|
|
33
|
+
const choices = await listUsageTargets(adapter, auth, signal, timeoutMs, guard);
|
|
34
|
+
if (rememberedTargetId) {
|
|
35
|
+
return choices.some((choice) => choice.id === rememberedTargetId)
|
|
36
|
+
? { kind: "selected", targetId: rememberedTargetId }
|
|
37
|
+
: { kind: "selection-required", choices };
|
|
38
|
+
}
|
|
39
|
+
if (choices.length === 1) return { kind: "selected", targetId: choices[0]?.id };
|
|
40
|
+
return { kind: "selection-required", choices };
|
|
46
41
|
}
|
|
47
42
|
|
|
48
43
|
export async function listUsageTargets(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
44
|
+
adapter: UsageProviderAdapter,
|
|
45
|
+
auth: ResolvedUsageAuth,
|
|
46
|
+
signal: AbortSignal,
|
|
47
|
+
timeoutMs: number,
|
|
48
|
+
guard: UsageRequestGuard,
|
|
54
49
|
): Promise<readonly UsageProviderTarget[]> {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const choices = normalizeUsageTargets(listed);
|
|
67
|
-
if (choices.length === 0) {
|
|
68
|
-
throw new Error(`${adapter.targets.pluralLabel} discovery returned no choices.`);
|
|
69
|
-
}
|
|
70
|
-
return choices;
|
|
50
|
+
if (!adapter.targets) return [];
|
|
51
|
+
const startedAt = Date.now();
|
|
52
|
+
await guard();
|
|
53
|
+
const listed = await adapter.targets.list(auth, signal, remainingTargetTimeout(timeoutMs, startedAt), guard);
|
|
54
|
+
await guard();
|
|
55
|
+
remainingTargetTimeout(timeoutMs, startedAt);
|
|
56
|
+
const choices = normalizeUsageTargets(listed);
|
|
57
|
+
if (choices.length === 0) {
|
|
58
|
+
throw new Error(`${adapter.targets.pluralLabel} discovery returned no choices.`);
|
|
59
|
+
}
|
|
60
|
+
return choices;
|
|
71
61
|
}
|
|
72
62
|
|
|
73
|
-
export function normalizeUsageTargets(
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
? sanitizeDisplayText(description, MAX_TARGET_DESCRIPTION_CHARS)
|
|
99
|
-
: undefined;
|
|
100
|
-
return { id, label: safeLabel, ...(safeDescription ? { description: safeDescription } : {}) };
|
|
101
|
-
});
|
|
63
|
+
export function normalizeUsageTargets(targets: readonly UsageProviderTarget[]): readonly UsageProviderTarget[] {
|
|
64
|
+
if (!Array.isArray(targets)) throw new Error("Target discovery did not return a choices array.");
|
|
65
|
+
if (targets.length > MAX_TARGETS) {
|
|
66
|
+
throw new Error(`Target discovery exceeded ${MAX_TARGETS} choices.`);
|
|
67
|
+
}
|
|
68
|
+
const seen = new Set<string>();
|
|
69
|
+
return targets.map((target) => {
|
|
70
|
+
if (!target || typeof target !== "object" || Array.isArray(target)) {
|
|
71
|
+
throw new Error("Target discovery returned an invalid choice.");
|
|
72
|
+
}
|
|
73
|
+
const { id, label, description } = target as Partial<UsageProviderTarget>;
|
|
74
|
+
if (!isBoundedTargetId(id)) throw new Error("Target discovery returned an invalid ID.");
|
|
75
|
+
if (seen.has(id)) throw new Error(`Target discovery repeated ${id}.`);
|
|
76
|
+
seen.add(id);
|
|
77
|
+
if (typeof label !== "string") {
|
|
78
|
+
throw new Error("Target discovery returned an invalid display label.");
|
|
79
|
+
}
|
|
80
|
+
if (description !== undefined && typeof description !== "string") {
|
|
81
|
+
throw new Error("Target discovery returned an invalid description.");
|
|
82
|
+
}
|
|
83
|
+
const safeLabel = sanitizeDisplayText(label, MAX_TARGET_LABEL_CHARS);
|
|
84
|
+
if (!safeLabel) throw new Error("Target discovery returned an empty display label.");
|
|
85
|
+
const safeDescription = description ? sanitizeDisplayText(description, MAX_TARGET_DESCRIPTION_CHARS) : undefined;
|
|
86
|
+
return { id, label: safeLabel, ...(safeDescription ? { description: safeDescription } : {}) };
|
|
87
|
+
});
|
|
102
88
|
}
|
|
103
89
|
|
|
104
|
-
export function createUsageTargetSelectOptions(
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
});
|
|
124
|
-
return { options, targetIdFor: (option) => ids.get(option) };
|
|
90
|
+
export function createUsageTargetSelectOptions(targets: readonly UsageProviderTarget[]): UsageTargetSelectOptions {
|
|
91
|
+
const normalized = normalizeUsageTargets(targets);
|
|
92
|
+
const ids = new Map<string, string>();
|
|
93
|
+
const options = normalized.map((target) => {
|
|
94
|
+
const base = target.description ? `${target.label} — ${target.description}` : target.label;
|
|
95
|
+
let option = base;
|
|
96
|
+
if (ids.has(option)) {
|
|
97
|
+
const safeId = sanitizeDisplayText(target.id, 80) || "target";
|
|
98
|
+
option = `${base} · ${safeId}`;
|
|
99
|
+
let duplicate = 2;
|
|
100
|
+
while (ids.has(option)) {
|
|
101
|
+
option = `${base} · ${safeId} (${duplicate})`;
|
|
102
|
+
duplicate += 1;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
ids.set(option, target.id);
|
|
106
|
+
return option;
|
|
107
|
+
});
|
|
108
|
+
return { options, targetIdFor: (option) => ids.get(option) };
|
|
125
109
|
}
|
|
126
110
|
|
|
127
111
|
function remainingTargetTimeout(timeoutMs: number, startedAt: number): number {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
112
|
+
const remaining = timeoutMs - (Date.now() - startedAt);
|
|
113
|
+
if (remaining <= 0) throw new Error("Timed out while discovering provider targets.");
|
|
114
|
+
return remaining;
|
|
131
115
|
}
|
|
132
116
|
|
|
133
117
|
export function isBoundedTargetId(value: unknown): value is string {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
118
|
+
return (
|
|
119
|
+
typeof value === "string" &&
|
|
120
|
+
value.length > 0 &&
|
|
121
|
+
value.length <= MAX_TARGET_ID_CHARS &&
|
|
122
|
+
![...value].some((character) => {
|
|
123
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
124
|
+
return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f);
|
|
125
|
+
})
|
|
126
|
+
);
|
|
143
127
|
}
|