pi-advisor-flow 0.13.3-dev.1 → 0.13.3-dev.2
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/CHANGELOG.md +7 -0
- package/README.md +3 -3
- package/dist/index.js +827 -1828
- package/package.json +2 -2
- package/src/commands/settings-commands.ts +8 -10
- package/src/commands/settings-persistence.ts +2 -15
- package/src/config/defaults.ts +7 -0
- package/src/config/schema.ts +10 -0
- package/src/config/state.ts +9 -0
- package/src/config/storage.ts +23 -3
- package/src/config/types.ts +2 -0
- package/src/jev/classifier-key-store.ts +203 -0
- package/src/jev/classifier.ts +421 -0
- package/src/jev/ledger.ts +3 -3
- package/src/jev/questions.ts +61 -23
- package/src/tools/jev-filter.ts +54 -34
- package/src/tools/jev-turn-gate.ts +105 -97
- package/src/tools/register-ask-advisor.ts +19 -2
- package/src/ui/jev-classifier-submenu.ts +459 -0
- package/src/ui/jev-filter-submenu.ts +67 -53
- package/src/ui/jev-provider-submenu.ts +16 -1
- package/src/ui/jev-setup-support.ts +13 -1
- package/src/ui/settings-items.ts +39 -72
- package/src/ui/settings-mutations.ts +0 -13
- package/src/ui/settings-selector.ts +21 -17
- package/src/ui/types.ts +36 -2
- package/src/usage.ts +82 -42
|
@@ -249,7 +249,19 @@ const clearKeyFor = (transport: JevTransportKind) => {
|
|
|
249
249
|
return clearKeyTypeSafeKey();
|
|
250
250
|
};
|
|
251
251
|
|
|
252
|
-
export const defaultSetupDeps: Required<
|
|
252
|
+
export const defaultSetupDeps: Required<
|
|
253
|
+
Omit<
|
|
254
|
+
JevSetupDeps,
|
|
255
|
+
| "modelRegistry"
|
|
256
|
+
| "resolveClassifierKey"
|
|
257
|
+
| "persistClassifierKey"
|
|
258
|
+
| "restoreClassifierKey"
|
|
259
|
+
>
|
|
260
|
+
> &
|
|
261
|
+
Pick<
|
|
262
|
+
JevSetupDeps,
|
|
263
|
+
"resolveClassifierKey" | "persistClassifierKey" | "restoreClassifierKey"
|
|
264
|
+
> = {
|
|
253
265
|
clearStoredKey: (transport) => clearKeyFor(transport),
|
|
254
266
|
removePlaintextKey: removeTypeSafeKeyFromAdvisorJson,
|
|
255
267
|
resolveEndpoint: (options) => resolveTypesafeCompatibleCredentials(options),
|
package/src/ui/settings-items.ts
CHANGED
|
@@ -7,17 +7,14 @@ import {
|
|
|
7
7
|
DEFAULT_JEV_FILTER_NOUL_MARGIN,
|
|
8
8
|
DEFAULT_JEV_FILTER_OVERRIDE_WINDOW,
|
|
9
9
|
DEFAULT_JEV_FILTER_SKIP_CONFIDENCE,
|
|
10
|
-
DEFAULT_JEV_MODEL,
|
|
11
|
-
DEFAULT_JEV_PRICE_PER_MTOK,
|
|
12
10
|
DEFAULT_JEV_TIMEOUT_MS,
|
|
13
|
-
DEFAULT_JEV_TRANSPORT,
|
|
14
11
|
DEFAULT_JEV_TURN_GATE_NOUL_THRESHOLD,
|
|
15
12
|
DEFAULT_SCOUT_TIMEOUT_MS,
|
|
16
13
|
} from "../config/types.ts";
|
|
17
14
|
import { isValidAdvisorToolPolicies } from "../config/validation.ts";
|
|
15
|
+
import type { JevClassifierKeySnapshot } from "../jev/classifier-key-store.ts";
|
|
16
|
+
import { JevClassifierSubmenu } from "./jev-classifier-submenu.ts";
|
|
18
17
|
import { JevFilterSubmenu } from "./jev-filter-submenu.ts";
|
|
19
|
-
import { JevProviderSubmenu } from "./jev-provider-submenu.ts";
|
|
20
|
-
import { providerName } from "./jev-setup-support.ts";
|
|
21
18
|
import { SearchableModelMultiSelector } from "./model-multi-selector.ts";
|
|
22
19
|
import { SearchableModelSelector } from "./model-selector.ts";
|
|
23
20
|
import {
|
|
@@ -35,9 +32,9 @@ import { FALLBACK_ADVISOR_MODEL_DISABLED } from "./types.ts";
|
|
|
35
32
|
import type {
|
|
36
33
|
AdvisorSettings,
|
|
37
34
|
ContextPreset,
|
|
35
|
+
JevClassifierRegistryAccess,
|
|
38
36
|
JevFilterSelection,
|
|
39
37
|
JevProviderSelection,
|
|
40
|
-
JevSetupDeps,
|
|
41
38
|
RenderRequester,
|
|
42
39
|
} from "./types.ts";
|
|
43
40
|
|
|
@@ -47,8 +44,16 @@ export interface SettingsItemsOptions {
|
|
|
47
44
|
modelWhitelist: SettingItem;
|
|
48
45
|
presets: ContextPreset[];
|
|
49
46
|
settings: AdvisorSettings;
|
|
50
|
-
|
|
51
|
-
|
|
47
|
+
jevClassifierRegistry?: JevClassifierRegistryAccess;
|
|
48
|
+
jevKeyDeps?: {
|
|
49
|
+
resolveClassifierKey?: () => Promise<JevClassifierKeySnapshot>;
|
|
50
|
+
persistClassifierKey?: (
|
|
51
|
+
key: string
|
|
52
|
+
) => Promise<{ ok: boolean; message: string }>;
|
|
53
|
+
restoreClassifierKey?: (
|
|
54
|
+
previous: JevClassifierKeySnapshot
|
|
55
|
+
) => Promise<boolean>;
|
|
56
|
+
};
|
|
52
57
|
onJevFilter?: (selection: JevFilterSelection) => boolean;
|
|
53
58
|
onJevProvider?: (selection: JevProviderSelection) => boolean;
|
|
54
59
|
theme: Theme;
|
|
@@ -85,66 +90,57 @@ const scoutTimeoutItem = (settings: AdvisorSettings): SettingItem => {
|
|
|
85
90
|
};
|
|
86
91
|
};
|
|
87
92
|
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (transport === "auto") {
|
|
92
|
-
return "Auto (TypeSafe → OpenRouter)";
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (transport === "typesafe-compatible") {
|
|
96
|
-
const baseUrl = settings.jevBaseUrl ?? "no Base URL";
|
|
97
|
-
|
|
98
|
-
return settings.jevKeyProvider
|
|
99
|
-
? `System One–compatible (${baseUrl}; Pi login "${settings.jevKeyProvider}")`
|
|
100
|
-
: `System One–compatible (${baseUrl})`;
|
|
93
|
+
const jevFilterRowValue = (settings: AdvisorSettings) => {
|
|
94
|
+
if (!settings.jevFilterEnabled) {
|
|
95
|
+
return "Off";
|
|
101
96
|
}
|
|
102
97
|
|
|
103
|
-
return
|
|
98
|
+
return settings.jevClassifier ? "On" : "On (unavailable: no classifier)";
|
|
104
99
|
};
|
|
105
100
|
|
|
106
101
|
const jevItems = (
|
|
107
102
|
settings: AdvisorSettings,
|
|
108
103
|
theme: Theme,
|
|
109
104
|
tui: RenderRequester,
|
|
110
|
-
|
|
105
|
+
jevClassifierRegistry?: JevClassifierRegistryAccess,
|
|
106
|
+
jevKeyDeps?: SettingsItemsOptions["jevKeyDeps"],
|
|
111
107
|
onJevFilter?: (selection: JevFilterSelection) => boolean,
|
|
112
|
-
onJevProvider?: (selection: JevProviderSelection) => boolean
|
|
113
|
-
afterJevSetup?: () => void
|
|
108
|
+
onJevProvider?: (selection: JevProviderSelection) => boolean
|
|
114
109
|
): SettingItem[] => [
|
|
115
110
|
{
|
|
116
|
-
currentValue:
|
|
111
|
+
currentValue: settings.jevClassifier ?? "Choose classifier",
|
|
117
112
|
description:
|
|
118
|
-
"
|
|
113
|
+
"Select a Pi-authenticated classifier or explicitly configure the separate Jev API Key option.",
|
|
119
114
|
id: "jevProvider",
|
|
120
|
-
label: "Jev
|
|
115
|
+
label: "Jev classifier",
|
|
121
116
|
submenu: (currentValue, done) =>
|
|
122
|
-
new
|
|
123
|
-
afterSelection: afterJevSetup,
|
|
124
|
-
currentBaseUrl: settings.jevBaseUrl,
|
|
125
|
-
currentKeyProvider: settings.jevKeyProvider,
|
|
126
|
-
currentTransport: settings.jevTransport ?? DEFAULT_JEV_TRANSPORT,
|
|
117
|
+
new JevClassifierSubmenu({
|
|
127
118
|
currentValue,
|
|
128
119
|
done,
|
|
120
|
+
keybindings: getKeybindings(),
|
|
121
|
+
modelRegistry: jevClassifierRegistry,
|
|
129
122
|
onSelection: onJevProvider,
|
|
130
|
-
|
|
123
|
+
persistKey: jevKeyDeps?.persistClassifierKey,
|
|
124
|
+
resolveStoredKey: jevKeyDeps?.resolveClassifierKey,
|
|
125
|
+
restoreKey: jevKeyDeps?.restoreClassifierKey,
|
|
131
126
|
theme,
|
|
132
127
|
tui,
|
|
133
128
|
}),
|
|
134
129
|
},
|
|
135
130
|
{
|
|
136
|
-
currentValue:
|
|
131
|
+
currentValue: jevFilterRowValue(settings),
|
|
137
132
|
description:
|
|
138
133
|
"Screen low-stakes ask_advisor consultations with the selected Jev provider.",
|
|
139
134
|
id: "jevFilter",
|
|
140
135
|
label: "Jev/Decisions consultation filter",
|
|
141
136
|
submenu: (currentValue, done) =>
|
|
142
137
|
new JevFilterSubmenu({
|
|
143
|
-
|
|
138
|
+
currentClassifier: settings.jevClassifier,
|
|
144
139
|
currentValue,
|
|
145
140
|
done,
|
|
141
|
+
modelRegistry: jevClassifierRegistry,
|
|
146
142
|
onSelection: onJevFilter,
|
|
147
|
-
|
|
143
|
+
resolveClassifierKey: jevKeyDeps?.resolveClassifierKey,
|
|
148
144
|
theme,
|
|
149
145
|
tui,
|
|
150
146
|
}),
|
|
@@ -186,23 +182,6 @@ const jevItems = (
|
|
|
186
182
|
[0, 3, 5, 10, 20, 50]
|
|
187
183
|
).map((value) => `${value} turns`),
|
|
188
184
|
},
|
|
189
|
-
{
|
|
190
|
-
currentValue: settings.jevModel ?? DEFAULT_JEV_MODEL,
|
|
191
|
-
description: "TypeSafe Jev model used for screening and turn-gate checks.",
|
|
192
|
-
id: "jevModel",
|
|
193
|
-
label: "Jev model",
|
|
194
|
-
submenu: (_currentValue, done) =>
|
|
195
|
-
new TextSettingSubmenu({
|
|
196
|
-
description:
|
|
197
|
-
"Enter a TypeSafe model name (for example jev-latest or jev-1.13.0).",
|
|
198
|
-
initial: settings.jevModel ?? DEFAULT_JEV_MODEL,
|
|
199
|
-
onCancel: done,
|
|
200
|
-
onSubmit: (value) => ({ value: value.trim() || DEFAULT_JEV_MODEL }),
|
|
201
|
-
theme,
|
|
202
|
-
title: "Jev model",
|
|
203
|
-
tui,
|
|
204
|
-
}),
|
|
205
|
-
},
|
|
206
185
|
{
|
|
207
186
|
currentValue: String(settings.jevTimeoutMs ?? DEFAULT_JEV_TIMEOUT_MS),
|
|
208
187
|
description: "Total wall-time budget for one Jev call, including retries.",
|
|
@@ -225,19 +204,7 @@ const jevItems = (
|
|
|
225
204
|
[0, 1000, 2000, 4000, 8000, 15_000]
|
|
226
205
|
),
|
|
227
206
|
},
|
|
228
|
-
|
|
229
|
-
currentValue: String(
|
|
230
|
-
settings.jevPricePerMtok ?? DEFAULT_JEV_PRICE_PER_MTOK
|
|
231
|
-
),
|
|
232
|
-
description:
|
|
233
|
-
"TypeSafe/OpenRouter estimated input price per million tokens; ignored for OpenAI Decisions, whose cost is unavailable.",
|
|
234
|
-
id: "jevPricePerMtok",
|
|
235
|
-
label: "Jev price/Mtok",
|
|
236
|
-
values: numericValues(
|
|
237
|
-
settings.jevPricePerMtok ?? DEFAULT_JEV_PRICE_PER_MTOK,
|
|
238
|
-
[0.01, 0.02, 0.042, 0.05, 0.1]
|
|
239
|
-
),
|
|
240
|
-
},
|
|
207
|
+
|
|
241
208
|
{
|
|
242
209
|
currentValue:
|
|
243
210
|
settings.jevTurnGateEveryTurns === 0 ||
|
|
@@ -338,8 +305,8 @@ export const createSettingsItems = ({
|
|
|
338
305
|
modelWhitelist,
|
|
339
306
|
presets,
|
|
340
307
|
settings,
|
|
341
|
-
|
|
342
|
-
|
|
308
|
+
jevClassifierRegistry,
|
|
309
|
+
jevKeyDeps,
|
|
343
310
|
onJevFilter,
|
|
344
311
|
onJevProvider,
|
|
345
312
|
theme,
|
|
@@ -660,10 +627,10 @@ export const createSettingsItems = ({
|
|
|
660
627
|
settings,
|
|
661
628
|
theme,
|
|
662
629
|
tui,
|
|
663
|
-
|
|
630
|
+
jevClassifierRegistry,
|
|
631
|
+
jevKeyDeps,
|
|
664
632
|
onJevFilter,
|
|
665
|
-
onJevProvider
|
|
666
|
-
afterJevSetup
|
|
633
|
+
onJevProvider
|
|
667
634
|
)
|
|
668
635
|
);
|
|
669
636
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { DEFAULT_JEV_MODEL } from "../config/types.ts";
|
|
2
1
|
import { FALLBACK_ADVISOR_MODEL_DISABLED } from "./types.ts";
|
|
3
2
|
import type { AdvisorSettings, ContextPreset } from "./types.ts";
|
|
4
3
|
|
|
@@ -231,12 +230,6 @@ const applyJevMutation = (
|
|
|
231
230
|
return true;
|
|
232
231
|
}
|
|
233
232
|
|
|
234
|
-
case "jevModel": {
|
|
235
|
-
settings.jevModel = value.trim() || DEFAULT_JEV_MODEL;
|
|
236
|
-
|
|
237
|
-
return true;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
233
|
case "jevTimeoutMs": {
|
|
241
234
|
settings.jevTimeoutMs = Number(value);
|
|
242
235
|
|
|
@@ -255,12 +248,6 @@ const applyJevMutation = (
|
|
|
255
248
|
return true;
|
|
256
249
|
}
|
|
257
250
|
|
|
258
|
-
case "jevPricePerMtok": {
|
|
259
|
-
settings.jevPricePerMtok = Number(value);
|
|
260
|
-
|
|
261
|
-
return true;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
251
|
default: {
|
|
265
252
|
return false;
|
|
266
253
|
}
|
|
@@ -2,7 +2,6 @@ import { getSettingsListTheme } from "@earendil-works/pi-coding-agent";
|
|
|
2
2
|
import { SettingsList, truncateToWidth } from "@earendil-works/pi-tui";
|
|
3
3
|
import type { Component, Focusable } from "@earendil-works/pi-tui";
|
|
4
4
|
|
|
5
|
-
import { DEFAULT_JEV_TRANSPORT } from "../config/types.ts";
|
|
6
5
|
import {
|
|
7
6
|
rainbowGradient,
|
|
8
7
|
SIMPLE_MODE_GRADIENT_INTERVAL_MS,
|
|
@@ -123,13 +122,14 @@ export class AdvisorSettingsSelector implements Component, Focusable {
|
|
|
123
122
|
);
|
|
124
123
|
|
|
125
124
|
const items = createSettingsItems({
|
|
126
|
-
afterJevSetup: () => {
|
|
127
|
-
this.settingsList = this.createSettingsList("jevFilter");
|
|
128
|
-
this.options.tui.requestRender();
|
|
129
|
-
},
|
|
130
125
|
effortLevels: this.options.effortLevels,
|
|
131
126
|
fallbackModel,
|
|
132
|
-
|
|
127
|
+
jevClassifierRegistry: this.options.jevSetupDeps?.modelRegistry,
|
|
128
|
+
jevKeyDeps: {
|
|
129
|
+
persistClassifierKey: this.options.jevSetupDeps?.persistClassifierKey,
|
|
130
|
+
resolveClassifierKey: this.options.jevSetupDeps?.resolveClassifierKey,
|
|
131
|
+
restoreClassifierKey: this.options.jevSetupDeps?.restoreClassifierKey,
|
|
132
|
+
},
|
|
133
133
|
modelWhitelist,
|
|
134
134
|
onJevFilter: (selection) => this.applyJevFilter(selection),
|
|
135
135
|
onJevProvider: (selection) => this.applyJevProvider(selection),
|
|
@@ -202,7 +202,7 @@ export class AdvisorSettingsSelector implements Component, Focusable {
|
|
|
202
202
|
legacy(
|
|
203
203
|
{
|
|
204
204
|
enabled: selection.enabled,
|
|
205
|
-
transport: settings.jevTransport ??
|
|
205
|
+
transport: settings.jevTransport ?? "auto",
|
|
206
206
|
},
|
|
207
207
|
settings
|
|
208
208
|
)
|
|
@@ -213,15 +213,19 @@ export class AdvisorSettingsSelector implements Component, Focusable {
|
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
private applyJevProvider(selection: JevProviderSelection): boolean {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
:
|
|
223
|
-
|
|
224
|
-
|
|
216
|
+
let patch: Partial<AdvisorSettings>;
|
|
217
|
+
|
|
218
|
+
if (selection.classifier) {
|
|
219
|
+
patch = { jevClassifier: selection.classifier };
|
|
220
|
+
} else if (selection.transport) {
|
|
221
|
+
patch = {
|
|
222
|
+
jevBaseUrl: selection.baseUrl,
|
|
223
|
+
jevKeyProvider: selection.keyProvider,
|
|
224
|
+
jevTransport: selection.transport,
|
|
225
|
+
};
|
|
226
|
+
} else {
|
|
227
|
+
patch = {};
|
|
228
|
+
}
|
|
225
229
|
|
|
226
230
|
const handler = this.options.onJevProvider;
|
|
227
231
|
|
|
@@ -233,7 +237,7 @@ export class AdvisorSettingsSelector implements Component, Focusable {
|
|
|
233
237
|
|
|
234
238
|
const legacy = this.options.onJevSetup;
|
|
235
239
|
|
|
236
|
-
if (legacy) {
|
|
240
|
+
if (legacy && selection.transport) {
|
|
237
241
|
return this.applyJevSelection(patch, (settings) =>
|
|
238
242
|
legacy(
|
|
239
243
|
{
|
package/src/ui/types.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ClassifierApi,
|
|
3
|
+
ClassifierContext,
|
|
4
|
+
ClassifierModel,
|
|
5
|
+
ClassifierResult,
|
|
6
|
+
ModelsClassifierOptions,
|
|
7
|
+
} from "@earendil-works/pi-ai";
|
|
1
8
|
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
9
|
import type { KeybindingsManager, TUI } from "@earendil-works/pi-tui";
|
|
3
10
|
|
|
4
11
|
import type { GateFailureMode, JevTransport } from "../config/types.ts";
|
|
5
12
|
import type { GitContextLevel } from "../git.ts";
|
|
13
|
+
import type { JevClassifierKeySnapshot } from "../jev/classifier-key-store.ts";
|
|
6
14
|
import type { JevKeyStoreResult } from "../jev/key-store.ts";
|
|
7
15
|
import type {
|
|
8
16
|
JevCredentials,
|
|
@@ -36,9 +44,7 @@ export interface SearchableModelMultiSelectorOptions extends Omit<
|
|
|
36
44
|
}
|
|
37
45
|
|
|
38
46
|
export interface ManualAdvisorRequest {
|
|
39
|
-
/** The repository disclosure level selected for this consultation. */
|
|
40
47
|
gitContext: GitContextLevel;
|
|
41
|
-
/** The unnormalized message entered in the dialog, when one was supplied. */
|
|
42
48
|
message?: string;
|
|
43
49
|
}
|
|
44
50
|
|
|
@@ -96,6 +102,7 @@ export interface AdvisorSettings {
|
|
|
96
102
|
gitContextMaxChars?: number;
|
|
97
103
|
herdrIntegration?: boolean;
|
|
98
104
|
jevBaseUrl?: string;
|
|
105
|
+
jevClassifier?: string;
|
|
99
106
|
jevDigestMaxChars?: number;
|
|
100
107
|
jevFilterEnabled?: boolean;
|
|
101
108
|
jevFilterNoulMargin?: number;
|
|
@@ -139,6 +146,7 @@ export interface JevFilterSelection {
|
|
|
139
146
|
|
|
140
147
|
export interface JevProviderSelection {
|
|
141
148
|
baseUrl?: string;
|
|
149
|
+
classifier?: string;
|
|
142
150
|
keyProvider?: string;
|
|
143
151
|
transport: JevTransport;
|
|
144
152
|
}
|
|
@@ -148,7 +156,33 @@ export interface JevSetupResult {
|
|
|
148
156
|
ok: boolean;
|
|
149
157
|
}
|
|
150
158
|
|
|
159
|
+
export interface JevClassifierRegistryAccess {
|
|
160
|
+
classify: (
|
|
161
|
+
model: ClassifierModel<ClassifierApi>,
|
|
162
|
+
context: ClassifierContext,
|
|
163
|
+
options?: ModelsClassifierOptions
|
|
164
|
+
) => Promise<ClassifierResult>;
|
|
165
|
+
findOfType: (
|
|
166
|
+
type: "classifier",
|
|
167
|
+
provider: string,
|
|
168
|
+
modelId: string
|
|
169
|
+
) => ClassifierModel<ClassifierApi> | undefined;
|
|
170
|
+
getAvailableOfType: (
|
|
171
|
+
type: "classifier",
|
|
172
|
+
provider?: string
|
|
173
|
+
) => Promise<readonly ClassifierModel<ClassifierApi>[]>;
|
|
174
|
+
getProviderDisplayName: (provider: string) => string;
|
|
175
|
+
}
|
|
176
|
+
|
|
151
177
|
export interface JevSetupDeps {
|
|
178
|
+
modelRegistry?: JevClassifierRegistryAccess;
|
|
179
|
+
resolveClassifierKey?: () => Promise<JevClassifierKeySnapshot>;
|
|
180
|
+
persistClassifierKey?: (
|
|
181
|
+
key: string
|
|
182
|
+
) => Promise<{ ok: boolean; message: string }>;
|
|
183
|
+
restoreClassifierKey?: (
|
|
184
|
+
previous: JevClassifierKeySnapshot
|
|
185
|
+
) => Promise<boolean>;
|
|
152
186
|
clearStoredKey?: (transport: JevTransportKind) => Promise<JevKeyStoreResult>;
|
|
153
187
|
removePlaintextKey?: () => JevKeyStoreResult;
|
|
154
188
|
resolveEndpoint?: (
|
package/src/usage.ts
CHANGED
|
@@ -3,7 +3,6 @@ import type { Usage } from "@earendil-works/pi-ai/compat";
|
|
|
3
3
|
import { isNumber, isRecordOf } from "./content-utils.ts";
|
|
4
4
|
import type { JsonValue, RecordValue } from "./content-utils.ts";
|
|
5
5
|
|
|
6
|
-
/** Normalized usage returned by an Advisor or Scout provider response. */
|
|
7
6
|
export interface AdvisorUsageSnapshot {
|
|
8
7
|
cacheRead?: number;
|
|
9
8
|
cacheWrite?: number;
|
|
@@ -46,7 +45,6 @@ const hasPartialProviderCost = (
|
|
|
46
45
|
);
|
|
47
46
|
};
|
|
48
47
|
|
|
49
|
-
/** Extracts provider-agnostic usage fields without trusting provider metadata. */
|
|
50
48
|
export const snapshotAdvisorUsage = <Input>(
|
|
51
49
|
usage: Input
|
|
52
50
|
): AdvisorUsageSnapshot | undefined => {
|
|
@@ -75,31 +73,84 @@ export const snapshotAdvisorUsage = <Input>(
|
|
|
75
73
|
: undefined;
|
|
76
74
|
};
|
|
77
75
|
|
|
78
|
-
|
|
79
|
-
export const advisorUsageCost = <Input>(usage: Input): number | undefined =>
|
|
76
|
+
export const advisorUsageCost = <Input>(usage: Input) =>
|
|
80
77
|
snapshotAdvisorUsage(usage)?.cost;
|
|
81
78
|
|
|
82
|
-
const completeUsageSnapshot = <Input>(
|
|
83
|
-
usage: Input
|
|
84
|
-
): { snapshot: AdvisorUsageSnapshot; usage: RecordValue } | undefined => {
|
|
79
|
+
const completeUsageSnapshot = <Input>(usage: Input) => {
|
|
85
80
|
const snapshot = snapshotAdvisorUsage(usage);
|
|
86
81
|
|
|
87
82
|
if (!(snapshot && isRecordOf(usage))) {
|
|
88
|
-
return;
|
|
83
|
+
return undefined;
|
|
89
84
|
}
|
|
90
85
|
|
|
91
86
|
if (hasPartialProviderCost(snapshot, usage)) {
|
|
92
|
-
return;
|
|
87
|
+
return undefined;
|
|
93
88
|
}
|
|
94
89
|
|
|
95
90
|
return { snapshot, usage };
|
|
96
91
|
};
|
|
97
92
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
93
|
+
const cleanUsage = (usage: Usage): Usage => {
|
|
94
|
+
const { cost } = usage;
|
|
95
|
+
|
|
96
|
+
const totalTokens =
|
|
97
|
+
usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
cacheRead: usage.cacheRead,
|
|
101
|
+
cacheWrite: usage.cacheWrite,
|
|
102
|
+
cost: {
|
|
103
|
+
cacheRead: cost.cacheRead,
|
|
104
|
+
cacheWrite: cost.cacheWrite,
|
|
105
|
+
input: cost.input,
|
|
106
|
+
output: cost.output,
|
|
107
|
+
total: cost.total,
|
|
108
|
+
},
|
|
109
|
+
input: usage.input,
|
|
110
|
+
output: usage.output,
|
|
111
|
+
totalTokens,
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export const combineAdvisorAndClassifierUsage = (
|
|
116
|
+
advisor: Usage | undefined,
|
|
117
|
+
classifier: Usage | undefined
|
|
118
|
+
): Usage | undefined => {
|
|
119
|
+
if (!(advisor || classifier)) {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const left = advisor ? cleanUsage(advisor) : undefined;
|
|
124
|
+
const right = classifier ? cleanUsage(classifier) : undefined;
|
|
125
|
+
|
|
126
|
+
if (!left) {
|
|
127
|
+
return right;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (!right) {
|
|
131
|
+
return left;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
cacheRead: left.cacheRead + right.cacheRead,
|
|
136
|
+
cacheWrite: left.cacheWrite + right.cacheWrite,
|
|
137
|
+
cost: {
|
|
138
|
+
cacheRead: left.cost.cacheRead + right.cost.cacheRead,
|
|
139
|
+
cacheWrite: left.cost.cacheWrite + right.cost.cacheWrite,
|
|
140
|
+
input: left.cost.input + right.cost.input,
|
|
141
|
+
output: left.cost.output,
|
|
142
|
+
total:
|
|
143
|
+
left.cost.total +
|
|
144
|
+
right.cost.input +
|
|
145
|
+
right.cost.cacheRead +
|
|
146
|
+
right.cost.cacheWrite,
|
|
147
|
+
},
|
|
148
|
+
input: left.input + right.input,
|
|
149
|
+
output: left.output + right.output,
|
|
150
|
+
totalTokens: left.totalTokens + right.totalTokens,
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
|
|
103
154
|
export const advisorUsageForPi = <Input>(usage: Input): Usage | undefined => {
|
|
104
155
|
const complete = completeUsageSnapshot(usage);
|
|
105
156
|
|
|
@@ -117,8 +168,6 @@ export const advisorUsageForPi = <Input>(usage: Input): Usage | undefined => {
|
|
|
117
168
|
const output = snapshot.output ?? 0;
|
|
118
169
|
const cacheRead = snapshot.cacheRead ?? 0;
|
|
119
170
|
const cacheWrite = snapshot.cacheWrite ?? 0;
|
|
120
|
-
const cacheWrite1h = finite(complete.usage.cacheWrite1h);
|
|
121
|
-
const reasoning = finite(complete.usage.reasoning);
|
|
122
171
|
|
|
123
172
|
const piUsage: Usage = {
|
|
124
173
|
cacheRead,
|
|
@@ -136,6 +185,9 @@ export const advisorUsageForPi = <Input>(usage: Input): Usage | undefined => {
|
|
|
136
185
|
snapshot.totalTokens ?? input + output + cacheRead + cacheWrite,
|
|
137
186
|
};
|
|
138
187
|
|
|
188
|
+
const cacheWrite1h = finite(complete.usage.cacheWrite1h);
|
|
189
|
+
const reasoning = finite(complete.usage.reasoning);
|
|
190
|
+
|
|
139
191
|
if (cacheWrite1h !== undefined) {
|
|
140
192
|
piUsage.cacheWrite1h = cacheWrite1h;
|
|
141
193
|
}
|
|
@@ -147,14 +199,12 @@ export const advisorUsageForPi = <Input>(usage: Input): Usage | undefined => {
|
|
|
147
199
|
return piUsage;
|
|
148
200
|
};
|
|
149
201
|
|
|
150
|
-
/** Creates empty totals without treating absent usage as zero usage. */
|
|
151
202
|
export const emptyAdvisorUsageTotals = (): AdvisorUsageTotals => ({
|
|
152
203
|
calls: 0,
|
|
153
204
|
costCalls: 0,
|
|
154
205
|
knownCalls: 0,
|
|
155
206
|
});
|
|
156
207
|
|
|
157
|
-
/** Adds one direct Advisor response to session-local usage totals. */
|
|
158
208
|
export const addAdvisorUsage = <Input>(
|
|
159
209
|
totals: AdvisorUsageTotals,
|
|
160
210
|
usage: Input
|
|
@@ -199,24 +249,24 @@ export const formatTokenCount = (value: number) => {
|
|
|
199
249
|
return `${Math.round(value / 1_000_000)}M`;
|
|
200
250
|
};
|
|
201
251
|
|
|
202
|
-
const formatTokens = formatTokenCount;
|
|
203
|
-
|
|
204
252
|
const formatCost = (value: number) => `$${value.toFixed(4)}`;
|
|
205
253
|
|
|
206
254
|
const formatUsageFields = (usage: AdvisorUsageSnapshot): string | undefined => {
|
|
207
255
|
const tokens = [
|
|
208
|
-
usage.input === undefined ? undefined : `↑${
|
|
209
|
-
usage.output === undefined
|
|
256
|
+
usage.input === undefined ? undefined : `↑${formatTokenCount(usage.input)}`,
|
|
257
|
+
usage.output === undefined
|
|
258
|
+
? undefined
|
|
259
|
+
: `↓${formatTokenCount(usage.output)}`,
|
|
210
260
|
usage.cacheRead === undefined
|
|
211
261
|
? undefined
|
|
212
|
-
: `cr:${
|
|
262
|
+
: `cr:${formatTokenCount(usage.cacheRead)}`,
|
|
213
263
|
usage.cacheWrite === undefined
|
|
214
264
|
? undefined
|
|
215
|
-
: `cw:${
|
|
265
|
+
: `cw:${formatTokenCount(usage.cacheWrite)}`,
|
|
216
266
|
].filter((value): value is string => value !== undefined);
|
|
217
267
|
|
|
218
268
|
if (tokens.length === 0 && usage.totalTokens !== undefined) {
|
|
219
|
-
tokens.push(`tokens:${
|
|
269
|
+
tokens.push(`tokens:${formatTokenCount(usage.totalTokens)}`);
|
|
220
270
|
}
|
|
221
271
|
|
|
222
272
|
if (usage.cost !== undefined) {
|
|
@@ -226,37 +276,27 @@ const formatUsageFields = (usage: AdvisorUsageSnapshot): string | undefined => {
|
|
|
226
276
|
return tokens.join(" · ") || undefined;
|
|
227
277
|
};
|
|
228
278
|
|
|
229
|
-
export const formatAdvisorUsage = <Input>(usage: Input)
|
|
279
|
+
export const formatAdvisorUsage = <Input>(usage: Input) => {
|
|
230
280
|
const snapshot = snapshotAdvisorUsage(usage);
|
|
231
281
|
|
|
232
282
|
return snapshot ? formatUsageFields(snapshot) : undefined;
|
|
233
283
|
};
|
|
234
284
|
|
|
235
|
-
export const formatAdvisorUsageTotals = (
|
|
236
|
-
totals: AdvisorUsageTotals
|
|
237
|
-
): string => {
|
|
285
|
+
export const formatAdvisorUsageTotals = (totals: AdvisorUsageTotals) => {
|
|
238
286
|
const usage = formatUsageFields(totals);
|
|
239
287
|
const missing = totals.calls - totals.knownCalls;
|
|
240
288
|
|
|
241
|
-
const parts = [
|
|
242
|
-
usage,
|
|
243
|
-
missing > 0 ? `${missing} without usage data` : undefined,
|
|
244
|
-
];
|
|
245
|
-
|
|
246
289
|
return (
|
|
247
|
-
|
|
248
|
-
|
|
290
|
+
[usage, missing > 0 ? `${missing} without usage data` : undefined]
|
|
291
|
+
.filter((value): value is string => value !== undefined)
|
|
292
|
+
.join(" · ") || "unavailable"
|
|
249
293
|
);
|
|
250
294
|
};
|
|
251
295
|
|
|
252
|
-
export const formatAdvisorUsageStatus = (
|
|
253
|
-
totals: AdvisorUsageTotals
|
|
254
|
-
): string | undefined => {
|
|
296
|
+
export const formatAdvisorUsageStatus = (totals: AdvisorUsageTotals) => {
|
|
255
297
|
if (totals.calls === 0) {
|
|
256
298
|
return undefined;
|
|
257
299
|
}
|
|
258
300
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
return `${label} · ${formatAdvisorUsageTotals(totals)}`;
|
|
301
|
+
return `Advisor: ${totals.calls} call${totals.calls === 1 ? "" : "s"} · ${formatAdvisorUsageTotals(totals)}`;
|
|
262
302
|
};
|