pi-advisor-flow 0.13.3-dev.1 → 0.13.3-dev.3
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 +8 -0
- package/README.md +3 -3
- package/dist/index.js +827 -1828
- package/package.json +6 -6
- 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
package/dist/index.js
CHANGED
|
@@ -13,6 +13,7 @@ var DEFAULT_ADVISOR_TOOL_RESULT_MAX_LINES = PI_DEFAULT_MAX_LINES;
|
|
|
13
13
|
var DEFAULT_ADVISOR_TOOL_RESULT_MAX_BYTES = PI_DEFAULT_MAX_BYTES;
|
|
14
14
|
var DEFAULT_ADVISOR_GIT_CONTEXT_MAX_CHARS = 20000;
|
|
15
15
|
var DEFAULT_JEV_MODEL = "jev-latest";
|
|
16
|
+
var JEV_API_KEY_CLASSIFIER = "jev-api-key";
|
|
16
17
|
var DEFAULT_JEV_TIMEOUT_MS = 8000;
|
|
17
18
|
var DEFAULT_SCOUT_TIMEOUT_MS = 30000;
|
|
18
19
|
var MAX_SCOUT_TIMEOUT_MS = 2147483647;
|
|
@@ -74,6 +75,8 @@ var advisorJevFilterSkipConfidenceRef = DEFAULT_JEV_FILTER_SKIP_CONFIDENCE;
|
|
|
74
75
|
var advisorJevFilterNoulMarginRef = DEFAULT_JEV_FILTER_NOUL_MARGIN;
|
|
75
76
|
var advisorJevFilterOverrideWindowRef = DEFAULT_JEV_FILTER_OVERRIDE_WINDOW;
|
|
76
77
|
var advisorJevModelRef = DEFAULT_JEV_MODEL;
|
|
78
|
+
var advisorJevClassifierRef;
|
|
79
|
+
var advisorJevClassifierModeRef = "pi";
|
|
77
80
|
var advisorJevTimeoutMsRef = DEFAULT_JEV_TIMEOUT_MS;
|
|
78
81
|
var advisorJevDigestMaxCharsRef = DEFAULT_JEV_DIGEST_MAX_CHARS;
|
|
79
82
|
var advisorJevPricePerMtokRef = DEFAULT_JEV_PRICE_PER_MTOK;
|
|
@@ -181,6 +184,10 @@ var setAdvisorFailureModeRef = (value) => {
|
|
|
181
184
|
var setAdvisorHerdrIntegrationRef = (enabled) => {
|
|
182
185
|
advisorHerdrIntegrationRef = enabled;
|
|
183
186
|
};
|
|
187
|
+
var setAdvisorJevClassifierRef = (ref) => {
|
|
188
|
+
advisorJevClassifierRef = ref?.trim() || undefined;
|
|
189
|
+
advisorJevClassifierModeRef = advisorJevClassifierRef === "jev-api-key" ? "api-key" : "pi";
|
|
190
|
+
};
|
|
184
191
|
var setAdvisorJevFilterEnabledRef = (enabled) => {
|
|
185
192
|
advisorJevFilterEnabledRef = enabled;
|
|
186
193
|
};
|
|
@@ -278,6 +285,7 @@ var getAdvisorSettings = () => ({
|
|
|
278
285
|
gitContextMaxChars: advisorGitContextMaxCharsRef,
|
|
279
286
|
herdrIntegration: advisorHerdrIntegrationRef,
|
|
280
287
|
jevBaseUrl: advisorJevBaseUrlRef,
|
|
288
|
+
jevClassifier: advisorJevClassifierRef,
|
|
281
289
|
jevDigestMaxChars: advisorJevDigestMaxCharsRef,
|
|
282
290
|
jevFilterEnabled: advisorJevFilterEnabledRef,
|
|
283
291
|
jevFilterNoulMargin: advisorJevFilterNoulMarginRef,
|
|
@@ -508,7 +516,6 @@ var normalizeJevBaseUrl = (input) => {
|
|
|
508
516
|
}
|
|
509
517
|
return { baseUrl: `${parsed.origin}${stripContractPath(parsed.pathname)}` };
|
|
510
518
|
};
|
|
511
|
-
var jevSystemOneEndpoint = (baseUrl) => `${baseUrl}${JEV_SYSTEM_ONE_PATH}`;
|
|
512
519
|
|
|
513
520
|
// src/config/schema.ts
|
|
514
521
|
var configuredModelRef = (value) => value?.trim() || undefined;
|
|
@@ -633,6 +640,13 @@ var CONFIG_SCHEMA = {
|
|
|
633
640
|
type: "string",
|
|
634
641
|
validate: isValidJevBaseUrl
|
|
635
642
|
},
|
|
643
|
+
advisorJevClassifier: {
|
|
644
|
+
accepted: "a concrete provider/model classifier reference or jev-api-key",
|
|
645
|
+
current: () => advisorJevClassifierRef,
|
|
646
|
+
persisted: true,
|
|
647
|
+
type: "string",
|
|
648
|
+
validate: (value) => isString(value) && (value === "jev-api-key" || /^[^/]+\/.+$/u.test(value.trim()))
|
|
649
|
+
},
|
|
636
650
|
advisorJevDigestMaxChars: {
|
|
637
651
|
accepted: "a non-negative safe integer",
|
|
638
652
|
current: () => advisorJevDigestMaxCharsRef,
|
|
@@ -967,6 +981,7 @@ var resetDefaults = () => {
|
|
|
967
981
|
setAdvisorFailureModeRef("block-session");
|
|
968
982
|
setAdvisorHerdrIntegrationRef(true);
|
|
969
983
|
setAdvisorJevModelRef(DEFAULT_JEV_MODEL);
|
|
984
|
+
setAdvisorJevClassifierRef(undefined);
|
|
970
985
|
setAdvisorJevBaseUrlRef(undefined);
|
|
971
986
|
setAdvisorJevKeyProviderRef(undefined);
|
|
972
987
|
setAdvisorJevFilterEnabledRef(false);
|
|
@@ -1038,6 +1053,7 @@ var applyConfig = (config) => {
|
|
|
1038
1053
|
applyOptionalConfig(config, "advisorJevFilterNoulMargin", setAdvisorJevFilterNoulMarginRef);
|
|
1039
1054
|
applyOptionalConfig(config, "advisorJevFilterOverrideWindow", setAdvisorJevFilterOverrideWindowRef);
|
|
1040
1055
|
applyNonEmptyStringConfig(config.advisorJevModel, setAdvisorJevModelRef);
|
|
1056
|
+
applyOptionalConfig(config, "advisorJevClassifier", setAdvisorJevClassifierRef);
|
|
1041
1057
|
applyOptionalConfig(config, "advisorJevBaseUrl", setAdvisorJevBaseUrlRef);
|
|
1042
1058
|
applyOptionalConfig(config, "advisorJevKeyProvider", setAdvisorJevKeyProviderRef);
|
|
1043
1059
|
applyOptionalConfig(config, "advisorJevTimeoutMs", setAdvisorJevTimeoutMsRef);
|
|
@@ -1061,14 +1077,6 @@ var applyConfig = (config) => {
|
|
|
1061
1077
|
var currentConfigState = () => Object.fromEntries(SAVED_CONFIG_KEYS.map((key) => [key, CONFIG_SCHEMA[key].current()]));
|
|
1062
1078
|
var sameConfigValue = (left, right) => JSON.stringify(left) === JSON.stringify(right);
|
|
1063
1079
|
var RESERVED_ADVISOR_JSON_KEYS = new Set(["typesafe_api_key"]);
|
|
1064
|
-
var readExistingConfig = (path) => {
|
|
1065
|
-
try {
|
|
1066
|
-
const parsed = JSON.parse(readFileSync(path, "utf-8"));
|
|
1067
|
-
return isRecord(parsed) ? parsed : {};
|
|
1068
|
-
} catch {
|
|
1069
|
-
return {};
|
|
1070
|
-
}
|
|
1071
|
-
};
|
|
1072
1080
|
var readConfigForSave = (path) => {
|
|
1073
1081
|
if (!existsSync(path)) {
|
|
1074
1082
|
return {};
|
|
@@ -1120,9 +1128,11 @@ var readConfig = (path) => {
|
|
|
1120
1128
|
};
|
|
1121
1129
|
var configCache = new Map;
|
|
1122
1130
|
var warnedUnknownConfigIdentities = new Set;
|
|
1131
|
+
var warnedFilterWithoutClassifierIdentities = new Set;
|
|
1123
1132
|
var resetConfigCache = () => {
|
|
1124
1133
|
configCache.clear();
|
|
1125
1134
|
warnedUnknownConfigIdentities.clear();
|
|
1135
|
+
warnedFilterWithoutClassifierIdentities.clear();
|
|
1126
1136
|
};
|
|
1127
1137
|
var configIdentity = (path) => {
|
|
1128
1138
|
try {
|
|
@@ -1149,8 +1159,12 @@ var loadConfig = (_ctx) => {
|
|
|
1149
1159
|
setPersistedModelRefs(configuredModelRef(globalConfig?.advisor), configuredModelRef(globalConfig?.executor));
|
|
1150
1160
|
if (globalConfig) {
|
|
1151
1161
|
applyConfig(globalConfig);
|
|
1152
|
-
const unknownKeys = unknownConfigKeys(globalConfig).filter((key) => !RESERVED_ADVISOR_JSON_KEYS.has(key));
|
|
1153
1162
|
const warningIdentity = `${global}:${configIdentity(global)}`;
|
|
1163
|
+
if (advisorJevFilterEnabledRef && !advisorJevClassifierRef && _ctx.hasUI && !warnedFilterWithoutClassifierIdentities.has(warningIdentity)) {
|
|
1164
|
+
warnedFilterWithoutClassifierIdentities.add(warningIdentity);
|
|
1165
|
+
_ctx.ui.notify("The Jev consultation filter is enabled but no Jev classifier is selected; consultations are not screened until you pick one in /advisor-settings.", "warning");
|
|
1166
|
+
}
|
|
1167
|
+
const unknownKeys = unknownConfigKeys(globalConfig).filter((key) => !RESERVED_ADVISOR_JSON_KEYS.has(key));
|
|
1154
1168
|
if (unknownKeys.length > 0 && _ctx.hasUI && !warnedUnknownConfigIdentities.has(warningIdentity)) {
|
|
1155
1169
|
_ctx.ui.notify(`Advisor configuration at ${global} contains unrecognized key(s) ${unknownKeys.map((key) => JSON.stringify(key)).join(", ")}. They were preserved but ignored; check for typos or upgrade pi-advisor.`, "warning");
|
|
1156
1170
|
warnedUnknownConfigIdentities.add(warningIdentity);
|
|
@@ -3490,6 +3504,51 @@ var completeUsageSnapshot = (usage) => {
|
|
|
3490
3504
|
}
|
|
3491
3505
|
return { snapshot, usage };
|
|
3492
3506
|
};
|
|
3507
|
+
var cleanUsage = (usage) => {
|
|
3508
|
+
const { cost } = usage;
|
|
3509
|
+
const totalTokens = usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
|
|
3510
|
+
return {
|
|
3511
|
+
cacheRead: usage.cacheRead,
|
|
3512
|
+
cacheWrite: usage.cacheWrite,
|
|
3513
|
+
cost: {
|
|
3514
|
+
cacheRead: cost.cacheRead,
|
|
3515
|
+
cacheWrite: cost.cacheWrite,
|
|
3516
|
+
input: cost.input,
|
|
3517
|
+
output: cost.output,
|
|
3518
|
+
total: cost.total
|
|
3519
|
+
},
|
|
3520
|
+
input: usage.input,
|
|
3521
|
+
output: usage.output,
|
|
3522
|
+
totalTokens
|
|
3523
|
+
};
|
|
3524
|
+
};
|
|
3525
|
+
var combineAdvisorAndClassifierUsage = (advisor, classifier) => {
|
|
3526
|
+
if (!(advisor || classifier)) {
|
|
3527
|
+
return;
|
|
3528
|
+
}
|
|
3529
|
+
const left = advisor ? cleanUsage(advisor) : undefined;
|
|
3530
|
+
const right = classifier ? cleanUsage(classifier) : undefined;
|
|
3531
|
+
if (!left) {
|
|
3532
|
+
return right;
|
|
3533
|
+
}
|
|
3534
|
+
if (!right) {
|
|
3535
|
+
return left;
|
|
3536
|
+
}
|
|
3537
|
+
return {
|
|
3538
|
+
cacheRead: left.cacheRead + right.cacheRead,
|
|
3539
|
+
cacheWrite: left.cacheWrite + right.cacheWrite,
|
|
3540
|
+
cost: {
|
|
3541
|
+
cacheRead: left.cost.cacheRead + right.cost.cacheRead,
|
|
3542
|
+
cacheWrite: left.cost.cacheWrite + right.cost.cacheWrite,
|
|
3543
|
+
input: left.cost.input + right.cost.input,
|
|
3544
|
+
output: left.cost.output,
|
|
3545
|
+
total: left.cost.total + right.cost.input + right.cost.cacheRead + right.cost.cacheWrite
|
|
3546
|
+
},
|
|
3547
|
+
input: left.input + right.input,
|
|
3548
|
+
output: left.output + right.output,
|
|
3549
|
+
totalTokens: left.totalTokens + right.totalTokens
|
|
3550
|
+
};
|
|
3551
|
+
};
|
|
3493
3552
|
var advisorUsageForPi = (usage) => {
|
|
3494
3553
|
const complete = completeUsageSnapshot(usage);
|
|
3495
3554
|
if (!complete) {
|
|
@@ -3501,8 +3560,6 @@ var advisorUsageForPi = (usage) => {
|
|
|
3501
3560
|
const output = snapshot.output ?? 0;
|
|
3502
3561
|
const cacheRead = snapshot.cacheRead ?? 0;
|
|
3503
3562
|
const cacheWrite = snapshot.cacheWrite ?? 0;
|
|
3504
|
-
const cacheWrite1h = finite(complete.usage.cacheWrite1h);
|
|
3505
|
-
const reasoning = finite(complete.usage.reasoning);
|
|
3506
3563
|
const piUsage = {
|
|
3507
3564
|
cacheRead,
|
|
3508
3565
|
cacheWrite,
|
|
@@ -3517,6 +3574,8 @@ var advisorUsageForPi = (usage) => {
|
|
|
3517
3574
|
output,
|
|
3518
3575
|
totalTokens: snapshot.totalTokens ?? input + output + cacheRead + cacheWrite
|
|
3519
3576
|
};
|
|
3577
|
+
const cacheWrite1h = finite(complete.usage.cacheWrite1h);
|
|
3578
|
+
const reasoning = finite(complete.usage.reasoning);
|
|
3520
3579
|
if (cacheWrite1h !== undefined) {
|
|
3521
3580
|
piUsage.cacheWrite1h = cacheWrite1h;
|
|
3522
3581
|
}
|
|
@@ -3562,17 +3621,16 @@ var formatTokenCount = (value) => {
|
|
|
3562
3621
|
}
|
|
3563
3622
|
return `${Math.round(value / 1e6)}M`;
|
|
3564
3623
|
};
|
|
3565
|
-
var formatTokens = formatTokenCount;
|
|
3566
3624
|
var formatCost = (value) => `$${value.toFixed(4)}`;
|
|
3567
3625
|
var formatUsageFields = (usage) => {
|
|
3568
3626
|
const tokens = [
|
|
3569
|
-
usage.input === undefined ? undefined : `↑${
|
|
3570
|
-
usage.output === undefined ? undefined : `↓${
|
|
3571
|
-
usage.cacheRead === undefined ? undefined : `cr:${
|
|
3572
|
-
usage.cacheWrite === undefined ? undefined : `cw:${
|
|
3627
|
+
usage.input === undefined ? undefined : `↑${formatTokenCount(usage.input)}`,
|
|
3628
|
+
usage.output === undefined ? undefined : `↓${formatTokenCount(usage.output)}`,
|
|
3629
|
+
usage.cacheRead === undefined ? undefined : `cr:${formatTokenCount(usage.cacheRead)}`,
|
|
3630
|
+
usage.cacheWrite === undefined ? undefined : `cw:${formatTokenCount(usage.cacheWrite)}`
|
|
3573
3631
|
].filter((value) => value !== undefined);
|
|
3574
3632
|
if (tokens.length === 0 && usage.totalTokens !== undefined) {
|
|
3575
|
-
tokens.push(`tokens:${
|
|
3633
|
+
tokens.push(`tokens:${formatTokenCount(usage.totalTokens)}`);
|
|
3576
3634
|
}
|
|
3577
3635
|
if (usage.cost !== undefined) {
|
|
3578
3636
|
tokens.push(formatCost(usage.cost));
|
|
@@ -3586,18 +3644,13 @@ var formatAdvisorUsage = (usage) => {
|
|
|
3586
3644
|
var formatAdvisorUsageTotals = (totals) => {
|
|
3587
3645
|
const usage = formatUsageFields(totals);
|
|
3588
3646
|
const missing = totals.calls - totals.knownCalls;
|
|
3589
|
-
|
|
3590
|
-
usage,
|
|
3591
|
-
missing > 0 ? `${missing} without usage data` : undefined
|
|
3592
|
-
];
|
|
3593
|
-
return parts.filter((value) => value !== undefined).join(" · ") || "unavailable";
|
|
3647
|
+
return [usage, missing > 0 ? `${missing} without usage data` : undefined].filter((value) => value !== undefined).join(" · ") || "unavailable";
|
|
3594
3648
|
};
|
|
3595
3649
|
var formatAdvisorUsageStatus = (totals) => {
|
|
3596
3650
|
if (totals.calls === 0) {
|
|
3597
3651
|
return;
|
|
3598
3652
|
}
|
|
3599
|
-
|
|
3600
|
-
return `${label} · ${formatAdvisorUsageTotals(totals)}`;
|
|
3653
|
+
return `Advisor: ${totals.calls} call${totals.calls === 1 ? "" : "s"} · ${formatAdvisorUsageTotals(totals)}`;
|
|
3601
3654
|
};
|
|
3602
3655
|
|
|
3603
3656
|
// src/scout.ts
|
|
@@ -4832,7 +4885,7 @@ var savingsLine = (costs, skipped) => {
|
|
|
4832
4885
|
var formatJevTokens = (usage) => `↑${formatTokenCount(usage.inputTokens + usage.outputTokens)}`;
|
|
4833
4886
|
var jevCostText = (usage) => {
|
|
4834
4887
|
if (usage.unavailableCostCalls > 0) {
|
|
4835
|
-
return usage.knownCostCalls > 0 ? `cost unavailable (known-
|
|
4888
|
+
return usage.knownCostCalls > 0 ? `cost unavailable (known catalog-priced usage $${usage.cost.toFixed(4)})` : "cost unavailable (classifier usage was not reported)";
|
|
4836
4889
|
}
|
|
4837
4890
|
return `$${usage.cost.toFixed(4)}`;
|
|
4838
4891
|
};
|
|
@@ -4917,7 +4970,7 @@ class AdvisorJevLedgerState {
|
|
|
4917
4970
|
lines.push(filterLine(filter));
|
|
4918
4971
|
const jevTokens = usage.inputTokens + usage.outputTokens;
|
|
4919
4972
|
if (jevTokens > 0 || usage.unavailableCostCalls > 0) {
|
|
4920
|
-
const cost = usage.unavailableCostCalls > 0 ? jevCostText(usage) : `$${usage.cost.toFixed(4)} (input
|
|
4973
|
+
const cost = usage.unavailableCostCalls > 0 ? jevCostText(usage) : `$${usage.cost.toFixed(4)} (catalog-priced input/cache; output free)`;
|
|
4921
4974
|
lines.push(`Jev/Decisions cost: ${formatJevTokens(usage)} tokens · ${cost}`);
|
|
4922
4975
|
}
|
|
4923
4976
|
if (filter.skipped > 0) {
|
|
@@ -5961,556 +6014,59 @@ class ManualAdvisorDialog {
|
|
|
5961
6014
|
}
|
|
5962
6015
|
}
|
|
5963
6016
|
|
|
5964
|
-
// src/jev/
|
|
5965
|
-
|
|
5966
|
-
category;
|
|
5967
|
-
constructor(category, message) {
|
|
5968
|
-
super(message);
|
|
5969
|
-
this.name = "JevFailureError";
|
|
5970
|
-
this.category = category;
|
|
5971
|
-
}
|
|
5972
|
-
}
|
|
5973
|
-
|
|
5974
|
-
// src/jev/decisions-client.ts
|
|
5975
|
-
var OPENAI_DECISIONS_ENDPOINT = "https://api.openai.com/v1/decisions";
|
|
5976
|
-
var OPENAI_DECISIONS_MODEL = "gpt-6-luna";
|
|
5977
|
-
var formatEntry = (entry) => {
|
|
5978
|
-
if (isString(entry)) {
|
|
5979
|
-
return entry;
|
|
5980
|
-
}
|
|
5981
|
-
return entry === undefined ? "" : JSON.stringify(entry);
|
|
5982
|
-
};
|
|
5983
|
-
var predicateInstructions = (question) => {
|
|
5984
|
-
const parts = [formatEntry(question.instructions)];
|
|
5985
|
-
if (question.type === "noul") {
|
|
5986
|
-
if (question.criteria?.true !== undefined) {
|
|
5987
|
-
parts.push(`A true answer means: ${formatEntry(question.criteria.true)}`);
|
|
5988
|
-
}
|
|
5989
|
-
if (question.criteria?.false !== undefined) {
|
|
5990
|
-
parts.push(`A false answer means: ${formatEntry(question.criteria.false)}`);
|
|
5991
|
-
}
|
|
5992
|
-
}
|
|
5993
|
-
return parts.filter(Boolean).join(`
|
|
5994
|
-
|
|
5995
|
-
`);
|
|
5996
|
-
};
|
|
5997
|
-
var scoreLevelLabel = (description, index) => {
|
|
5998
|
-
const text = formatEntry(description);
|
|
5999
|
-
const colon = text.indexOf(":");
|
|
6000
|
-
const label = (colon === -1 ? text : text.slice(0, colon)).trim();
|
|
6001
|
-
return label || `Level ${index}`;
|
|
6002
|
-
};
|
|
6003
|
-
var decisionQuestion = (name, question) => {
|
|
6004
|
-
if (question.type === "noul") {
|
|
6005
|
-
return {
|
|
6006
|
-
instructions: predicateInstructions(question),
|
|
6007
|
-
name,
|
|
6008
|
-
type: "predicate"
|
|
6009
|
-
};
|
|
6010
|
-
}
|
|
6011
|
-
if (question.type === "score") {
|
|
6012
|
-
const labels = new Set;
|
|
6013
|
-
const levels = question.criteria.map((description, index) => {
|
|
6014
|
-
const label = scoreLevelLabel(description, index);
|
|
6015
|
-
if (labels.has(label)) {
|
|
6016
|
-
throw new JevFailureError("malformed", `Jev score question ${name} has duplicate level labels.`);
|
|
6017
|
-
}
|
|
6018
|
-
labels.add(label);
|
|
6019
|
-
return { description: formatEntry(description), label };
|
|
6020
|
-
});
|
|
6021
|
-
return {
|
|
6022
|
-
instructions: formatEntry(question.instructions),
|
|
6023
|
-
levels,
|
|
6024
|
-
name,
|
|
6025
|
-
type: "score"
|
|
6026
|
-
};
|
|
6027
|
-
}
|
|
6028
|
-
throw new JevFailureError("malformed", `Jev question ${name} has an unsupported type.`);
|
|
6029
|
-
};
|
|
6030
|
-
var formatDecisionsInput = (state) => {
|
|
6031
|
-
if (isString(state)) {
|
|
6032
|
-
return state;
|
|
6033
|
-
}
|
|
6034
|
-
return JSON.stringify(state, null, 2);
|
|
6035
|
-
};
|
|
6036
|
-
var buildDecisionsRequest = (state, questions) => {
|
|
6037
|
-
if (!isRecordOf(questions) || Object.keys(questions).length === 0) {
|
|
6038
|
-
throw new JevFailureError("malformed", "Jev questions are empty.");
|
|
6039
|
-
}
|
|
6040
|
-
return {
|
|
6041
|
-
input: formatDecisionsInput(state),
|
|
6042
|
-
model: OPENAI_DECISIONS_MODEL,
|
|
6043
|
-
questions: Object.entries(questions).map(([name, question]) => decisionQuestion(name, question))
|
|
6044
|
-
};
|
|
6045
|
-
};
|
|
6046
|
-
|
|
6047
|
-
// src/jev/decisions-response.ts
|
|
6048
|
-
var malformedResponse = (detail) => new JevFailureError("malformed", `OpenAI Decisions response ${detail}.`);
|
|
6049
|
-
var boundedProbability = (value) => isNumber(value) && Number.isFinite(value) && value >= 0 && value <= 1 ? value : undefined;
|
|
6050
|
-
var normalizePredicateAnswer = (answer) => {
|
|
6051
|
-
const probability = boundedProbability(answer.probability);
|
|
6052
|
-
if (probability === undefined) {
|
|
6053
|
-
throw malformedResponse("has an invalid predicate probability");
|
|
6054
|
-
}
|
|
6055
|
-
return { noul: probability, type: "noul" };
|
|
6056
|
-
};
|
|
6057
|
-
var normalizeScoreAnswer = (answer, question) => {
|
|
6058
|
-
if (!Array.isArray(answer.probabilities)) {
|
|
6059
|
-
throw malformedResponse("has no score probability array");
|
|
6060
|
-
}
|
|
6061
|
-
if (answer.probabilities.length !== question.levels.length) {
|
|
6062
|
-
throw malformedResponse("has an incomplete score probability array");
|
|
6063
|
-
}
|
|
6064
|
-
const probabilities = {};
|
|
6065
|
-
const legend = {};
|
|
6066
|
-
const seenLabels = new Set;
|
|
6067
|
-
const seenValues = new Set;
|
|
6068
|
-
let probabilityTotal = 0;
|
|
6069
|
-
for (const entry of answer.probabilities) {
|
|
6070
|
-
if (!isRecord(entry)) {
|
|
6071
|
-
throw malformedResponse("has an invalid score probability entry");
|
|
6072
|
-
}
|
|
6073
|
-
const { label, value } = entry;
|
|
6074
|
-
const probability = boundedProbability(entry.probability);
|
|
6075
|
-
if (!isString(label) || !isNumber(value) || !Number.isSafeInteger(value) || value < 0 || value >= question.levels.length || probability === undefined || seenLabels.has(label) || seenValues.has(value)) {
|
|
6076
|
-
throw malformedResponse("has an invalid or duplicate score level");
|
|
6077
|
-
}
|
|
6078
|
-
const level = question.levels[value];
|
|
6079
|
-
if (label !== level.label) {
|
|
6080
|
-
throw malformedResponse("has an ambiguous score label/value pair");
|
|
6081
|
-
}
|
|
6082
|
-
seenLabels.add(label);
|
|
6083
|
-
seenValues.add(value);
|
|
6084
|
-
probabilities[String(value)] = probability;
|
|
6085
|
-
legend[String(value)] = level.description;
|
|
6086
|
-
probabilityTotal += probability;
|
|
6087
|
-
}
|
|
6088
|
-
if (Math.abs(probabilityTotal - 1) > 0.02 || question.levels.some((level, index) => !seenValues.has(index) || !seenLabels.has(level.label))) {
|
|
6089
|
-
throw malformedResponse("has an incomplete or ambiguous score distribution");
|
|
6090
|
-
}
|
|
6091
|
-
return { legend, probabilities, type: "score" };
|
|
6092
|
-
};
|
|
6093
|
-
var usageTokenCount = (value) => isNumber(value) && Number.isSafeInteger(value) && value >= 0 ? value : undefined;
|
|
6094
|
-
var parseDecisionsApiResponse = async (response) => {
|
|
6095
|
-
let value;
|
|
6096
|
-
try {
|
|
6097
|
-
value = await response.json();
|
|
6098
|
-
} catch {
|
|
6099
|
-
throw malformedResponse("was not valid JSON");
|
|
6100
|
-
}
|
|
6101
|
-
if (!isRecord(value)) {
|
|
6102
|
-
throw malformedResponse("is not an object");
|
|
6103
|
-
}
|
|
6104
|
-
const { answers: rawAnswers, model, usage } = value;
|
|
6105
|
-
if (!Array.isArray(rawAnswers)) {
|
|
6106
|
-
throw malformedResponse("did not include an answers array");
|
|
6107
|
-
}
|
|
6108
|
-
if (!isRecord(usage)) {
|
|
6109
|
-
throw malformedResponse("has malformed usage");
|
|
6110
|
-
}
|
|
6111
|
-
if (model !== undefined && !isString(model)) {
|
|
6112
|
-
throw malformedResponse("has an invalid model name");
|
|
6113
|
-
}
|
|
6114
|
-
const answers = [];
|
|
6115
|
-
for (const answer of rawAnswers) {
|
|
6116
|
-
if (!isRecord(answer)) {
|
|
6117
|
-
throw malformedResponse("contains an invalid answer");
|
|
6118
|
-
}
|
|
6119
|
-
answers.push(answer);
|
|
6120
|
-
}
|
|
6121
|
-
return { answers, model: isString(model) ? model : undefined, usage };
|
|
6122
|
-
};
|
|
6123
|
-
var normalizeAnswers = (responseAnswers, questions) => {
|
|
6124
|
-
const expected = new Map(questions.map((question) => [question.name, question]));
|
|
6125
|
-
if (expected.size !== questions.length || expected.size === 0) {
|
|
6126
|
-
throw malformedResponse("has duplicate or missing requested question names");
|
|
6127
|
-
}
|
|
6128
|
-
const received = new Map;
|
|
6129
|
-
for (const answer of responseAnswers) {
|
|
6130
|
-
const { name } = answer;
|
|
6131
|
-
if (!isString(name) || !name) {
|
|
6132
|
-
throw malformedResponse("contains an unnamed answer");
|
|
6133
|
-
}
|
|
6134
|
-
if (!expected.has(name)) {
|
|
6135
|
-
throw malformedResponse("contains an unexpected answer");
|
|
6136
|
-
}
|
|
6137
|
-
if (received.has(name)) {
|
|
6138
|
-
throw malformedResponse("contains duplicate answer names");
|
|
6139
|
-
}
|
|
6140
|
-
if (answer.type === "refusal") {
|
|
6141
|
-
throw malformedResponse("contains a refusal");
|
|
6142
|
-
}
|
|
6143
|
-
received.set(name, answer);
|
|
6144
|
-
}
|
|
6145
|
-
if (received.size !== expected.size) {
|
|
6146
|
-
throw malformedResponse("is missing one or more named answers");
|
|
6147
|
-
}
|
|
6148
|
-
const answers = {};
|
|
6149
|
-
for (const question of questions) {
|
|
6150
|
-
const answer = received.get(question.name);
|
|
6151
|
-
if (!answer || answer.type !== question.type) {
|
|
6152
|
-
throw malformedResponse("has an answer with the wrong type");
|
|
6153
|
-
}
|
|
6154
|
-
answers[question.name] = question.type === "predicate" ? normalizePredicateAnswer(answer) : normalizeScoreAnswer(answer, question);
|
|
6155
|
-
}
|
|
6156
|
-
return answers;
|
|
6157
|
-
};
|
|
6158
|
-
var normalizeDecisionsResponse = (response, questions) => {
|
|
6159
|
-
const inputTokens = usageTokenCount(response.usage.input_tokens);
|
|
6160
|
-
const outputTokens = usageTokenCount(response.usage.output_tokens);
|
|
6161
|
-
if (inputTokens === undefined || outputTokens === undefined) {
|
|
6162
|
-
throw malformedResponse("has malformed usage");
|
|
6163
|
-
}
|
|
6164
|
-
return {
|
|
6165
|
-
answers: normalizeAnswers(response.answers, questions),
|
|
6166
|
-
model: response.model || OPENAI_DECISIONS_MODEL,
|
|
6167
|
-
usage: { inputTokens, outputTokens }
|
|
6168
|
-
};
|
|
6169
|
-
};
|
|
6170
|
-
|
|
6171
|
-
// src/jev/client.ts
|
|
6172
|
-
var FIXED_ENDPOINTS = {
|
|
6173
|
-
"openai-decisions": OPENAI_DECISIONS_ENDPOINT,
|
|
6174
|
-
openrouter: "https://openrouter.ai/api/alpha/decisions",
|
|
6175
|
-
typesafe: "https://api.typesafe.ai/v1/systemone"
|
|
6176
|
-
};
|
|
6177
|
-
var range = (from, to) => Array.from({ length: to - from + 1 }, (_, index) => from + index);
|
|
6178
|
-
var RETRYABLE_STATUSES = new Set([408, 429, ...range(500, 599)]);
|
|
6179
|
-
var RETRY_BACKOFF_MS = 250;
|
|
6180
|
-
var MAX_ATTEMPTS = 2;
|
|
6181
|
-
var finiteTokens = (value) => isNumber(value) && Number.isFinite(value) && value >= 0 ? value : 0;
|
|
6182
|
-
var errorDetail = (error) => {
|
|
6183
|
-
if (isString(error)) {
|
|
6184
|
-
return error;
|
|
6185
|
-
}
|
|
6186
|
-
if (isRecordOf(error) && "message" in error) {
|
|
6187
|
-
return String(error.message);
|
|
6188
|
-
}
|
|
6189
|
-
return "";
|
|
6190
|
-
};
|
|
6191
|
-
var statusCategory = (status) => {
|
|
6192
|
-
if (status === 401 || status === 403) {
|
|
6193
|
-
return "auth";
|
|
6194
|
-
}
|
|
6195
|
-
if (status === 400 || status === 422) {
|
|
6196
|
-
return "malformed";
|
|
6197
|
-
}
|
|
6198
|
-
return "error";
|
|
6199
|
-
};
|
|
6200
|
-
var openRouterModelId = (model) => model.includes("/") ? model : `~typesafe/${model}`;
|
|
6201
|
-
var buildClientRequest = (transport, model, state, questions) => {
|
|
6202
|
-
if (transport === "openai-decisions") {
|
|
6203
|
-
const request = buildDecisionsRequest(state, questions);
|
|
6204
|
-
return {
|
|
6205
|
-
body: JSON.stringify(request),
|
|
6206
|
-
decisionQuestions: request.questions
|
|
6207
|
-
};
|
|
6208
|
-
}
|
|
6209
|
-
return {
|
|
6210
|
-
body: JSON.stringify({ model, questions, state })
|
|
6211
|
-
};
|
|
6212
|
-
};
|
|
6213
|
-
var connectionFailure = (error) => {
|
|
6214
|
-
const message = redactSecrets(error instanceof Error ? error.message : String(error));
|
|
6215
|
-
return {
|
|
6216
|
-
failure: new JevFailureError("network", `Jev connection failed: ${message}`)
|
|
6217
|
-
};
|
|
6218
|
-
};
|
|
6219
|
-
var sleepWithAbort = (signal, ms) => {
|
|
6220
|
-
if (signal.aborted) {
|
|
6221
|
-
return Promise.resolve();
|
|
6222
|
-
}
|
|
6223
|
-
return new Promise((resolve) => {
|
|
6224
|
-
const timer = setTimeout(resolve, ms);
|
|
6225
|
-
timer.unref?.();
|
|
6226
|
-
signal.addEventListener("abort", () => {
|
|
6227
|
-
clearTimeout(timer);
|
|
6228
|
-
resolve();
|
|
6229
|
-
}, { once: true });
|
|
6230
|
-
});
|
|
6231
|
-
};
|
|
6232
|
-
|
|
6233
|
-
class JevClient {
|
|
6234
|
-
#apiKey;
|
|
6235
|
-
#endpoint;
|
|
6236
|
-
#fetch;
|
|
6237
|
-
#model;
|
|
6238
|
-
#pricePerMtok;
|
|
6239
|
-
#timeoutMs;
|
|
6240
|
-
#transport;
|
|
6241
|
-
constructor({
|
|
6242
|
-
apiKey,
|
|
6243
|
-
baseUrl,
|
|
6244
|
-
fetch,
|
|
6245
|
-
model,
|
|
6246
|
-
pricePerMtok,
|
|
6247
|
-
timeoutMs,
|
|
6248
|
-
transport
|
|
6249
|
-
}) {
|
|
6250
|
-
this.#apiKey = apiKey;
|
|
6251
|
-
if (transport === "typesafe-compatible") {
|
|
6252
|
-
if (!baseUrl) {
|
|
6253
|
-
throw new JevFailureError("malformed", "The System One–compatible endpoint has no Base URL.");
|
|
6254
|
-
}
|
|
6255
|
-
this.#endpoint = jevSystemOneEndpoint(baseUrl);
|
|
6256
|
-
} else {
|
|
6257
|
-
this.#endpoint = FIXED_ENDPOINTS[transport];
|
|
6258
|
-
}
|
|
6259
|
-
this.#fetch = fetch ?? globalThis.fetch.bind(globalThis);
|
|
6260
|
-
if (transport === "openai-decisions") {
|
|
6261
|
-
this.#model = OPENAI_DECISIONS_MODEL;
|
|
6262
|
-
} else if (transport === "openrouter") {
|
|
6263
|
-
this.#model = openRouterModelId(model);
|
|
6264
|
-
} else {
|
|
6265
|
-
this.#model = model;
|
|
6266
|
-
}
|
|
6267
|
-
this.#pricePerMtok = pricePerMtok;
|
|
6268
|
-
this.#timeoutMs = timeoutMs;
|
|
6269
|
-
this.#transport = transport;
|
|
6270
|
-
}
|
|
6271
|
-
async ask(state, questions, signal) {
|
|
6272
|
-
const { body, decisionQuestions } = buildClientRequest(this.#transport, this.#model, state, questions);
|
|
6273
|
-
const deadline = new AbortController;
|
|
6274
|
-
const abortFromCaller = () => deadline.abort(signal?.reason);
|
|
6275
|
-
signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
6276
|
-
if (signal?.aborted) {
|
|
6277
|
-
abortFromCaller();
|
|
6278
|
-
}
|
|
6279
|
-
let deadlineHit = false;
|
|
6280
|
-
const timer = setTimeout(() => {
|
|
6281
|
-
deadlineHit = true;
|
|
6282
|
-
deadline.abort(new Error("Jev wall-time budget elapsed"));
|
|
6283
|
-
}, this.#timeoutMs);
|
|
6284
|
-
timer.unref?.();
|
|
6285
|
-
try {
|
|
6286
|
-
let outcome = {};
|
|
6287
|
-
for (let attempt = 1;; attempt += 1) {
|
|
6288
|
-
outcome = await this.#attempt(body, deadline.signal, decisionQuestions);
|
|
6289
|
-
if (!outcome.retryable || attempt >= MAX_ATTEMPTS || deadline.signal.aborted) {
|
|
6290
|
-
break;
|
|
6291
|
-
}
|
|
6292
|
-
await sleepWithAbort(deadline.signal, RETRY_BACKOFF_MS);
|
|
6293
|
-
if (deadline.signal.aborted) {
|
|
6294
|
-
break;
|
|
6295
|
-
}
|
|
6296
|
-
}
|
|
6297
|
-
return this.#settle(outcome, deadlineHit, signal);
|
|
6298
|
-
} catch (error) {
|
|
6299
|
-
if (signal?.aborted && !deadlineHit) {
|
|
6300
|
-
throw error;
|
|
6301
|
-
}
|
|
6302
|
-
if (error instanceof JevFailureError) {
|
|
6303
|
-
throw error;
|
|
6304
|
-
}
|
|
6305
|
-
const message = redactSecrets(error instanceof Error ? error.message : String(error));
|
|
6306
|
-
throw deadlineHit ? new JevFailureError("timeout", `Jev call exceeded its ${this.#timeoutMs} ms wall-time budget.`) : new JevFailureError("error", message);
|
|
6307
|
-
} finally {
|
|
6308
|
-
clearTimeout(timer);
|
|
6309
|
-
signal?.removeEventListener("abort", abortFromCaller);
|
|
6310
|
-
}
|
|
6311
|
-
}
|
|
6312
|
-
#settle(outcome, deadlineHit, signal) {
|
|
6313
|
-
if (outcome.answers) {
|
|
6314
|
-
return this.#result(outcome);
|
|
6315
|
-
}
|
|
6316
|
-
if (deadlineHit && outcome.failure?.category !== "auth") {
|
|
6317
|
-
throw new JevFailureError("timeout", `Jev call exceeded its ${this.#timeoutMs} ms wall-time budget.`);
|
|
6318
|
-
}
|
|
6319
|
-
if (signal?.aborted) {
|
|
6320
|
-
throw new Error("Jev call aborted by the caller.");
|
|
6321
|
-
}
|
|
6322
|
-
if (outcome.failure) {
|
|
6323
|
-
throw outcome.failure;
|
|
6324
|
-
}
|
|
6325
|
-
throw new JevFailureError("error", "Jev call failed.");
|
|
6326
|
-
}
|
|
6327
|
-
async#attempt(body, signal, decisionQuestions) {
|
|
6328
|
-
if (signal.aborted) {
|
|
6329
|
-
return { retryable: false };
|
|
6330
|
-
}
|
|
6331
|
-
let response;
|
|
6332
|
-
try {
|
|
6333
|
-
response = await this.#fetch(this.#endpoint, {
|
|
6334
|
-
body,
|
|
6335
|
-
headers: {
|
|
6336
|
-
authorization: `Bearer ${this.#apiKey}`,
|
|
6337
|
-
"content-type": "application/json"
|
|
6338
|
-
},
|
|
6339
|
-
method: "POST",
|
|
6340
|
-
signal
|
|
6341
|
-
});
|
|
6342
|
-
} catch (error) {
|
|
6343
|
-
if (signal.aborted) {
|
|
6344
|
-
return { retryable: false };
|
|
6345
|
-
}
|
|
6346
|
-
return {
|
|
6347
|
-
retryable: true,
|
|
6348
|
-
...connectionFailure(error)
|
|
6349
|
-
};
|
|
6350
|
-
}
|
|
6351
|
-
if (response.ok) {
|
|
6352
|
-
return this.#parseSuccess(response, decisionQuestions);
|
|
6353
|
-
}
|
|
6354
|
-
const failure = await this.#failureFromStatus(response);
|
|
6355
|
-
return {
|
|
6356
|
-
failure,
|
|
6357
|
-
retryable: RETRYABLE_STATUSES.has(response.status)
|
|
6358
|
-
};
|
|
6359
|
-
}
|
|
6360
|
-
async#failureFromStatus(response) {
|
|
6361
|
-
let detail = "";
|
|
6362
|
-
try {
|
|
6363
|
-
const parsed = await response.json();
|
|
6364
|
-
const error = isRecord(parsed) ? parsed.error : undefined;
|
|
6365
|
-
detail = errorDetail(error);
|
|
6366
|
-
} catch {
|
|
6367
|
-
detail = "";
|
|
6368
|
-
}
|
|
6369
|
-
const message = redactSecrets(`Jev ${this.#transportLabel()} request failed with HTTP ${response.status}${detail ? `: ${detail}` : ""}.`);
|
|
6370
|
-
return new JevFailureError(statusCategory(response.status), message);
|
|
6371
|
-
}
|
|
6372
|
-
#transportLabel() {
|
|
6373
|
-
if (this.#transport === "openrouter") {
|
|
6374
|
-
return "OpenRouter";
|
|
6375
|
-
}
|
|
6376
|
-
if (this.#transport === "openai-decisions") {
|
|
6377
|
-
return "OpenAI Decisions";
|
|
6378
|
-
}
|
|
6379
|
-
return this.#transport === "typesafe" ? "TypeSafe" : "endpoint";
|
|
6380
|
-
}
|
|
6381
|
-
async#parseSuccess(response, decisionQuestions) {
|
|
6382
|
-
if (this.#transport === "openai-decisions") {
|
|
6383
|
-
const normalized = normalizeDecisionsResponse(await parseDecisionsApiResponse(response), decisionQuestions ?? []);
|
|
6384
|
-
return {
|
|
6385
|
-
answers: normalized.answers,
|
|
6386
|
-
model: normalized.model,
|
|
6387
|
-
usage: {
|
|
6388
|
-
input_tokens: normalized.usage.inputTokens,
|
|
6389
|
-
output_tokens: normalized.usage.outputTokens
|
|
6390
|
-
}
|
|
6391
|
-
};
|
|
6392
|
-
}
|
|
6393
|
-
let parsed;
|
|
6394
|
-
try {
|
|
6395
|
-
parsed = await response.json();
|
|
6396
|
-
} catch (error) {
|
|
6397
|
-
return {
|
|
6398
|
-
failure: new JevFailureError("malformed", `Jev response was not JSON: ${redactSecrets(error instanceof Error ? error.message : String(error))}`)
|
|
6399
|
-
};
|
|
6400
|
-
}
|
|
6401
|
-
if (!isRecord(parsed) || !isRecord(parsed.answers)) {
|
|
6402
|
-
return {
|
|
6403
|
-
failure: new JevFailureError("malformed", "Jev response did not include an answers object.")
|
|
6404
|
-
};
|
|
6405
|
-
}
|
|
6406
|
-
return {
|
|
6407
|
-
answers: parsed.answers,
|
|
6408
|
-
model: isString(parsed.model) ? parsed.model : this.#model,
|
|
6409
|
-
usage: isRecordOf(parsed.usage) ? {
|
|
6410
|
-
input_tokens: parsed.usage.input_tokens,
|
|
6411
|
-
output_tokens: parsed.usage.output_tokens
|
|
6412
|
-
} : undefined
|
|
6413
|
-
};
|
|
6414
|
-
}
|
|
6415
|
-
#result(outcome) {
|
|
6416
|
-
const { usage } = outcome;
|
|
6417
|
-
const inputTokens = finiteTokens(usage?.input_tokens);
|
|
6418
|
-
const outputTokens = finiteTokens(usage?.output_tokens);
|
|
6419
|
-
const price = this.#pricePerMtok ?? advisorJevPricePerMtokRef;
|
|
6420
|
-
return {
|
|
6421
|
-
answers: outcome.answers ?? {},
|
|
6422
|
-
model: outcome.model ?? this.#model,
|
|
6423
|
-
usage: {
|
|
6424
|
-
cost: this.#transport === "openai-decisions" ? undefined : inputTokens / 1e6 * price,
|
|
6425
|
-
inputTokens,
|
|
6426
|
-
outputTokens
|
|
6427
|
-
}
|
|
6428
|
-
};
|
|
6429
|
-
}
|
|
6430
|
-
}
|
|
6431
|
-
var jevClientFromCredentials = (credentials, fetch) => new JevClient({
|
|
6432
|
-
apiKey: credentials.apiKey,
|
|
6433
|
-
baseUrl: credentials.baseUrl,
|
|
6434
|
-
fetch,
|
|
6435
|
-
model: advisorJevModelRef,
|
|
6436
|
-
timeoutMs: advisorJevTimeoutMsRef,
|
|
6437
|
-
transport: credentials.transport
|
|
6438
|
-
});
|
|
6439
|
-
|
|
6440
|
-
// src/jev/key-store.ts
|
|
6441
|
-
import {
|
|
6442
|
-
chmodSync,
|
|
6443
|
-
existsSync as existsSync3,
|
|
6444
|
-
readFileSync as readFileSync3,
|
|
6445
|
-
rmSync,
|
|
6446
|
-
writeFileSync as writeFileSync2
|
|
6447
|
-
} from "node:fs";
|
|
6017
|
+
// src/jev/classifier-key-store.ts
|
|
6018
|
+
import { chmodSync, readFileSync as readFileSync3, rmSync, writeFileSync as writeFileSync2 } from "node:fs";
|
|
6448
6019
|
import { join as join4 } from "node:path";
|
|
6449
6020
|
import { getAgentDir as getAgentDir4 } from "@earendil-works/pi-coding-agent";
|
|
6450
|
-
var
|
|
6451
|
-
var
|
|
6452
|
-
var TYPESAFE_KEY_NAME = "typesafe-api-key";
|
|
6453
|
-
var TYPESAFE_KEY_CONFIG_FIELD = "typesafe_api_key";
|
|
6021
|
+
var JEV_CLASSIFIER_KEY_SERVICE = "pi-advisor-jev-classifier";
|
|
6022
|
+
var JEV_CLASSIFIER_KEY_NAME = "classifier-api-key";
|
|
6454
6023
|
var KEY_FILE_MODE = 384;
|
|
6455
|
-
var keyFilePath = () => join4(getAgentDir4(), "
|
|
6024
|
+
var keyFilePath = () => join4(getAgentDir4(), "jev_classifier_api_key");
|
|
6025
|
+
var classifierKeyFailClosed = false;
|
|
6026
|
+
var disableJevClassifierKeyForRuntime = () => {
|
|
6027
|
+
classifierKeyFailClosed = true;
|
|
6028
|
+
};
|
|
6029
|
+
var enableJevClassifierKeyForRuntime = () => {
|
|
6030
|
+
classifierKeyFailClosed = false;
|
|
6031
|
+
};
|
|
6456
6032
|
var runtimeSecrets = () => {
|
|
6457
6033
|
const bun = globalThis;
|
|
6458
6034
|
return bun.Bun?.secrets;
|
|
6459
6035
|
};
|
|
6460
|
-
var
|
|
6461
|
-
var readAdvisorJsonConfig = () => readExistingConfig(join4(getAgentDir4(), "advisor.json"));
|
|
6462
|
-
var defaultReadFileStore = () => {
|
|
6036
|
+
var defaultReadFile = () => {
|
|
6463
6037
|
try {
|
|
6464
|
-
return
|
|
6038
|
+
return readFileSync3(keyFilePath(), "utf-8").trim() || undefined;
|
|
6465
6039
|
} catch {
|
|
6466
6040
|
return;
|
|
6467
6041
|
}
|
|
6468
6042
|
};
|
|
6469
|
-
var
|
|
6043
|
+
var defaultWriteFile = (key) => {
|
|
6470
6044
|
const path = keyFilePath();
|
|
6471
6045
|
writeFileSync2(path, `${key}
|
|
6472
6046
|
`, { mode: KEY_FILE_MODE });
|
|
6473
6047
|
chmodSync(path, KEY_FILE_MODE);
|
|
6474
6048
|
};
|
|
6475
|
-
var
|
|
6476
|
-
rmSync(keyFilePath(), { force: true });
|
|
6477
|
-
};
|
|
6478
|
-
var messageOf = (error) => redactSecrets(error instanceof Error ? error.message : String(error));
|
|
6479
|
-
var resolveTypeSafeKey = async (deps = {}) => {
|
|
6049
|
+
var readJevClassifierKey = async (deps = {}) => {
|
|
6480
6050
|
const secrets = deps.secrets === undefined ? runtimeSecrets() : deps.secrets;
|
|
6481
6051
|
if (secrets) {
|
|
6482
6052
|
try {
|
|
6483
|
-
const
|
|
6484
|
-
name:
|
|
6485
|
-
service:
|
|
6486
|
-
})
|
|
6487
|
-
|
|
6488
|
-
|
|
6053
|
+
const storedValue = await secrets.get({
|
|
6054
|
+
name: JEV_CLASSIFIER_KEY_NAME,
|
|
6055
|
+
service: JEV_CLASSIFIER_KEY_SERVICE
|
|
6056
|
+
});
|
|
6057
|
+
const key = storedValue?.trim();
|
|
6058
|
+
if (key) {
|
|
6059
|
+
return { key, source: "bun-secrets" };
|
|
6489
6060
|
}
|
|
6490
|
-
} catch {
|
|
6491
|
-
|
|
6492
|
-
const env = deps.env ?? process.env;
|
|
6493
|
-
const fromEnv = normalizeKey(env[TYPESAFE_KEY_ENV_VAR]);
|
|
6494
|
-
if (fromEnv) {
|
|
6495
|
-
return { key: fromEnv, source: "env" };
|
|
6496
|
-
}
|
|
6497
|
-
const readFileStore = deps.readFileStore ?? defaultReadFileStore;
|
|
6498
|
-
const fromFile = normalizeKey(readFileStore());
|
|
6499
|
-
if (fromFile) {
|
|
6500
|
-
return { key: fromFile, source: "file" };
|
|
6501
|
-
}
|
|
6502
|
-
const config = (deps.readAdvisorJson ?? readAdvisorJsonConfig)();
|
|
6503
|
-
const staged = config[TYPESAFE_KEY_CONFIG_FIELD];
|
|
6504
|
-
if (isString(staged)) {
|
|
6505
|
-
const fromConfig = normalizeKey(staged);
|
|
6506
|
-
if (fromConfig) {
|
|
6507
|
-
return { key: fromConfig, source: "advisor-json" };
|
|
6061
|
+
} catch {
|
|
6062
|
+
return { failed: true };
|
|
6508
6063
|
}
|
|
6509
6064
|
}
|
|
6510
|
-
|
|
6065
|
+
const key = (deps.readFile ?? defaultReadFile)();
|
|
6066
|
+
return key ? { key, source: "file" } : {};
|
|
6511
6067
|
};
|
|
6512
|
-
var
|
|
6513
|
-
const normalized =
|
|
6068
|
+
var writeJevClassifierKey = async (key, deps = {}) => {
|
|
6069
|
+
const normalized = key.trim();
|
|
6514
6070
|
if (!normalized) {
|
|
6515
6071
|
return { message: "The key is empty.", ok: false };
|
|
6516
6072
|
}
|
|
@@ -6518,95 +6074,81 @@ var writeKeyTypeSafeKey = async (key, deps = {}) => {
|
|
|
6518
6074
|
if (secrets) {
|
|
6519
6075
|
try {
|
|
6520
6076
|
await secrets.set({
|
|
6521
|
-
name:
|
|
6522
|
-
service:
|
|
6077
|
+
name: JEV_CLASSIFIER_KEY_NAME,
|
|
6078
|
+
service: JEV_CLASSIFIER_KEY_SERVICE,
|
|
6523
6079
|
value: normalized
|
|
6524
6080
|
});
|
|
6525
|
-
return { message: "
|
|
6526
|
-
} catch
|
|
6081
|
+
return { message: "Jev classifier key stored securely.", ok: true };
|
|
6082
|
+
} catch {
|
|
6527
6083
|
return {
|
|
6528
|
-
message:
|
|
6084
|
+
message: "Could not store the Jev classifier key securely.",
|
|
6529
6085
|
ok: false
|
|
6530
6086
|
};
|
|
6531
6087
|
}
|
|
6532
6088
|
}
|
|
6533
6089
|
try {
|
|
6534
|
-
(deps.
|
|
6090
|
+
(deps.writeFile ?? defaultWriteFile)(normalized);
|
|
6535
6091
|
return {
|
|
6536
|
-
message: "
|
|
6092
|
+
message: "Jev classifier key stored in a 0600-mode file.",
|
|
6537
6093
|
ok: true
|
|
6538
6094
|
};
|
|
6539
|
-
} catch
|
|
6095
|
+
} catch {
|
|
6540
6096
|
return {
|
|
6541
|
-
message:
|
|
6097
|
+
message: "Could not store the Jev classifier key securely.",
|
|
6542
6098
|
ok: false
|
|
6543
6099
|
};
|
|
6544
6100
|
}
|
|
6545
6101
|
};
|
|
6546
|
-
var
|
|
6547
|
-
|
|
6548
|
-
|
|
6102
|
+
var restoreJevClassifierKey = async (previous, deps = {}) => {
|
|
6103
|
+
if (previous.failed) {
|
|
6104
|
+
return false;
|
|
6105
|
+
}
|
|
6106
|
+
if (previous.key) {
|
|
6107
|
+
const result = await writeJevClassifierKey(previous.key, deps);
|
|
6108
|
+
return result.ok;
|
|
6109
|
+
}
|
|
6110
|
+
if (deps.secrets === null && deps.writeFile) {
|
|
6111
|
+
return true;
|
|
6112
|
+
}
|
|
6549
6113
|
const secrets = deps.secrets === undefined ? runtimeSecrets() : deps.secrets;
|
|
6114
|
+
let secretCleared = true;
|
|
6550
6115
|
if (secrets) {
|
|
6551
6116
|
try {
|
|
6552
6117
|
await secrets.delete({
|
|
6553
|
-
name:
|
|
6554
|
-
service:
|
|
6118
|
+
name: JEV_CLASSIFIER_KEY_NAME,
|
|
6119
|
+
service: JEV_CLASSIFIER_KEY_SERVICE
|
|
6555
6120
|
});
|
|
6556
|
-
|
|
6557
|
-
|
|
6558
|
-
firstError = messageOf(error);
|
|
6559
|
-
}
|
|
6560
|
-
}
|
|
6561
|
-
try {
|
|
6562
|
-
if (deps.deleteFileStore) {
|
|
6563
|
-
deps.deleteFileStore();
|
|
6564
|
-
} else if (existsSync3(keyFilePath())) {
|
|
6565
|
-
defaultDeleteFileStore();
|
|
6121
|
+
} catch {
|
|
6122
|
+
secretCleared = false;
|
|
6566
6123
|
}
|
|
6567
|
-
clearedSomething = true;
|
|
6568
|
-
} catch (error) {
|
|
6569
|
-
firstError ??= messageOf(error);
|
|
6570
|
-
}
|
|
6571
|
-
if (firstError) {
|
|
6572
|
-
return {
|
|
6573
|
-
message: `Clearing the stored key failed: ${firstError}.`,
|
|
6574
|
-
ok: false
|
|
6575
|
-
};
|
|
6576
6124
|
}
|
|
6577
|
-
return {
|
|
6578
|
-
message: `Stored key cleared.${clearedSomething ? "" : ` Nothing was stored; unset ${TYPESAFE_KEY_ENV_VAR} and remove ${TYPESAFE_KEY_CONFIG_FIELD} from advisor.json yourself if you use them.`}`,
|
|
6579
|
-
ok: true
|
|
6580
|
-
};
|
|
6581
|
-
};
|
|
6582
|
-
var removeTypeSafeKeyFromAdvisorJson = () => {
|
|
6583
6125
|
try {
|
|
6584
|
-
|
|
6585
|
-
|
|
6586
|
-
if (!(TYPESAFE_KEY_CONFIG_FIELD in existing)) {
|
|
6587
|
-
return { message: "No plaintext key in advisor.json.", ok: true };
|
|
6126
|
+
if (deps.secrets === undefined && !secrets) {
|
|
6127
|
+
rmSync(keyFilePath(), { force: true });
|
|
6588
6128
|
}
|
|
6589
|
-
|
|
6590
|
-
|
|
6591
|
-
`);
|
|
6592
|
-
resetConfigCache();
|
|
6593
|
-
return { message: "Plaintext key removed from advisor.json.", ok: true };
|
|
6594
|
-
} catch (error) {
|
|
6595
|
-
return {
|
|
6596
|
-
message: `Removing the plaintext key failed: ${messageOf(error)}.`,
|
|
6597
|
-
ok: false
|
|
6598
|
-
};
|
|
6129
|
+
} catch {
|
|
6130
|
+
return false;
|
|
6599
6131
|
}
|
|
6132
|
+
return secretCleared;
|
|
6600
6133
|
};
|
|
6601
|
-
var
|
|
6602
|
-
|
|
6603
|
-
if (warnedPlaintextKey) {
|
|
6134
|
+
var consumeJevClassifierKey = async () => {
|
|
6135
|
+
if (classifierKeyFailClosed) {
|
|
6604
6136
|
return;
|
|
6605
6137
|
}
|
|
6606
|
-
|
|
6607
|
-
return
|
|
6138
|
+
const stored = await readJevClassifierKey();
|
|
6139
|
+
return stored.key;
|
|
6608
6140
|
};
|
|
6609
6141
|
|
|
6142
|
+
// src/jev/failure.ts
|
|
6143
|
+
class JevFailureError extends Error {
|
|
6144
|
+
category;
|
|
6145
|
+
constructor(category, message) {
|
|
6146
|
+
super(message);
|
|
6147
|
+
this.name = "JevFailureError";
|
|
6148
|
+
this.category = category;
|
|
6149
|
+
}
|
|
6150
|
+
}
|
|
6151
|
+
|
|
6610
6152
|
// src/jev/questions.ts
|
|
6611
6153
|
var STAKES_RUBRIC = [
|
|
6612
6154
|
"Negligible: routine, low-risk, mechanical, or reversible; a wrong call costs little and is easy to undo.",
|
|
@@ -6629,10 +6171,29 @@ var screeningQuestions = {
|
|
|
6629
6171
|
type: "score"
|
|
6630
6172
|
}
|
|
6631
6173
|
};
|
|
6632
|
-
var
|
|
6633
|
-
|
|
6634
|
-
|
|
6635
|
-
|
|
6174
|
+
var nativeScreeningQuestions = {
|
|
6175
|
+
self_answerable: {
|
|
6176
|
+
criteria: {
|
|
6177
|
+
false: "The executor needs the Advisor's second opinion.",
|
|
6178
|
+
true: "The executor can resolve this alone with available tools and context."
|
|
6179
|
+
},
|
|
6180
|
+
instructions: `Can the executor confidently resolve this request alone, using available tools and context? ${EVIDENCE_RULE}`,
|
|
6181
|
+
type: "bool"
|
|
6182
|
+
},
|
|
6183
|
+
stakes: {
|
|
6184
|
+
criteria: {
|
|
6185
|
+
high: STAKES_RUBRIC[2],
|
|
6186
|
+
moderate: STAKES_RUBRIC[1],
|
|
6187
|
+
negligible: STAKES_RUBRIC[0]
|
|
6188
|
+
},
|
|
6189
|
+
instructions: `How material are the stakes of the decision behind this consultation request? ${EVIDENCE_RULE}`,
|
|
6190
|
+
type: "choice"
|
|
6191
|
+
}
|
|
6192
|
+
};
|
|
6193
|
+
var NUMERIC_KEY_PATTERN = /^\d+$/u;
|
|
6194
|
+
var finiteProbability = (value) => isNumber(value) && Number.isFinite(value) && value >= 0 && value <= 1 ? value : undefined;
|
|
6195
|
+
var lowestStakesProbability = (answer) => {
|
|
6196
|
+
if (!isRecordOf(answer)) {
|
|
6636
6197
|
return;
|
|
6637
6198
|
}
|
|
6638
6199
|
const probabilities = isRecordOf(answer.probabilities) ? answer.probabilities : {};
|
|
@@ -6640,9 +6201,12 @@ var lowestStakesProbability = (answer) => {
|
|
|
6640
6201
|
if (legend) {
|
|
6641
6202
|
const exact = Object.keys(legend).find((key) => legend[key] === STAKES_RUBRIC[0]);
|
|
6642
6203
|
if (exact) {
|
|
6643
|
-
return
|
|
6204
|
+
return finiteProbability(probabilities[exact]);
|
|
6644
6205
|
}
|
|
6645
6206
|
}
|
|
6207
|
+
if (Object.hasOwn(probabilities, "negligible")) {
|
|
6208
|
+
return finiteProbability(probabilities.negligible);
|
|
6209
|
+
}
|
|
6646
6210
|
const numericKeys = Object.keys(probabilities).filter((key) => NUMERIC_KEY_PATTERN.test(key));
|
|
6647
6211
|
if (numericKeys.length === 0) {
|
|
6648
6212
|
return;
|
|
@@ -6653,418 +6217,218 @@ var lowestStakesProbability = (answer) => {
|
|
|
6653
6217
|
lowest = key;
|
|
6654
6218
|
}
|
|
6655
6219
|
}
|
|
6656
|
-
return
|
|
6220
|
+
return finiteProbability(probabilities[lowest]);
|
|
6221
|
+
};
|
|
6222
|
+
var selfAnswerableProbability = (answer) => {
|
|
6223
|
+
if (!isRecordOf(answer)) {
|
|
6224
|
+
return;
|
|
6225
|
+
}
|
|
6226
|
+
const value = answer.type === "bool" ? answer.probability : answer.noul;
|
|
6227
|
+
return finiteProbability(value);
|
|
6657
6228
|
};
|
|
6658
|
-
var selfAnswerableNoul = (answer) => isRecordOf(answer) ? finiteNumber(answer.noul) : undefined;
|
|
6659
6229
|
var composeScreeningVerdict = (answers, { noulMargin, skipConfidence }) => {
|
|
6660
6230
|
if (!isRecordOf(answers)) {
|
|
6661
6231
|
return { skip: false };
|
|
6662
6232
|
}
|
|
6663
6233
|
const negligibleMass = lowestStakesProbability(answers.stakes);
|
|
6664
|
-
const
|
|
6665
|
-
if (negligibleMass === undefined ||
|
|
6234
|
+
const selfAnswerable = selfAnswerableProbability(answers.self_answerable);
|
|
6235
|
+
if (negligibleMass === undefined || selfAnswerable === undefined) {
|
|
6666
6236
|
return { skip: false };
|
|
6667
6237
|
}
|
|
6668
|
-
const confidentlySelfAnswerable = noul >= 0.5 + noulMargin;
|
|
6669
6238
|
return {
|
|
6670
|
-
skip: negligibleMass >= skipConfidence &&
|
|
6239
|
+
skip: negligibleMass >= skipConfidence && selfAnswerable >= 0.5 + noulMargin
|
|
6671
6240
|
};
|
|
6672
6241
|
};
|
|
6673
6242
|
var composeTurnGateVerdict = (answers, threshold) => {
|
|
6674
6243
|
if (!isRecordOf(answers)) {
|
|
6675
6244
|
return false;
|
|
6676
6245
|
}
|
|
6677
|
-
const
|
|
6678
|
-
|
|
6679
|
-
return noul !== undefined && noul >= threshold;
|
|
6246
|
+
const probability = selfAnswerableProbability(answers.should_consult);
|
|
6247
|
+
return probability !== undefined && probability >= threshold;
|
|
6680
6248
|
};
|
|
6681
6249
|
|
|
6682
|
-
// src/jev/
|
|
6683
|
-
var
|
|
6684
|
-
var
|
|
6685
|
-
const
|
|
6686
|
-
|
|
6687
|
-
|
|
6688
|
-
}
|
|
6689
|
-
if (input.draft) {
|
|
6690
|
-
fields.set("executor_draft", redactAndCapText(input.draft, JEV_TEXT_CAP_BYTES, advisorRedactSecretsRef));
|
|
6691
|
-
}
|
|
6692
|
-
const digest = recentConversation(ctx, advisorJevDigestMaxCharsRef);
|
|
6693
|
-
if (digest) {
|
|
6694
|
-
fields.set("recent_conversation", digest);
|
|
6695
|
-
}
|
|
6696
|
-
return Object.fromEntries(fields);
|
|
6250
|
+
// src/jev/classifier.ts
|
|
6251
|
+
var REF_PATTERN = /^(?<provider>[^/]+)\/(?<modelId>.+)$/u;
|
|
6252
|
+
var parseJevClassifierRef = (ref) => {
|
|
6253
|
+
const match = REF_PATTERN.exec(ref.trim());
|
|
6254
|
+
const provider = match?.groups?.provider;
|
|
6255
|
+
const modelId = match?.groups?.modelId;
|
|
6256
|
+
return provider && modelId ? { modelId, provider } : undefined;
|
|
6697
6257
|
};
|
|
6698
|
-
|
|
6699
|
-
|
|
6700
|
-
|
|
6701
|
-
|
|
6702
|
-
existsSync as existsSync4,
|
|
6703
|
-
readFileSync as readFileSync4,
|
|
6704
|
-
rmSync as rmSync2,
|
|
6705
|
-
writeFileSync as writeFileSync3
|
|
6706
|
-
} from "node:fs";
|
|
6707
|
-
import { join as join5 } from "node:path";
|
|
6708
|
-
import { getAgentDir as getAgentDir5 } from "@earendil-works/pi-coding-agent";
|
|
6709
|
-
var JEV_ENDPOINT_KEY_ENV_VAR = "JEV_API_KEY";
|
|
6710
|
-
var JEV_ENDPOINT_KEY_SERVICE = "pi-advisor";
|
|
6711
|
-
var JEV_ENDPOINT_KEY_NAME = "jev-api-key";
|
|
6712
|
-
var KEY_FILE_MODE2 = 384;
|
|
6713
|
-
var runtimeSecrets2 = () => {
|
|
6714
|
-
const bun = globalThis;
|
|
6715
|
-
return bun.Bun?.secrets;
|
|
6716
|
-
};
|
|
6717
|
-
var keyFilePath2 = () => join5(getAgentDir5(), "jev_api_key");
|
|
6718
|
-
var normalizeKey2 = (value) => value?.trim() || undefined;
|
|
6719
|
-
var readDefaultFileStore = () => {
|
|
6720
|
-
try {
|
|
6721
|
-
return normalizeKey2(readFileSync4(keyFilePath2(), "utf-8"));
|
|
6722
|
-
} catch {
|
|
6258
|
+
var isClassifierModel = (model) => isRecordOf(model) && model.type === "classifier" && typeof model.provider === "string" && typeof model.id === "string" && typeof model.api === "string" && isRecordOf(model.cost);
|
|
6259
|
+
var isProbability = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 1;
|
|
6260
|
+
var normalizedUsage = (usage) => {
|
|
6261
|
+
if (!usage) {
|
|
6723
6262
|
return;
|
|
6724
6263
|
}
|
|
6725
|
-
|
|
6726
|
-
|
|
6727
|
-
const
|
|
6728
|
-
|
|
6729
|
-
|
|
6730
|
-
|
|
6731
|
-
|
|
6732
|
-
var deleteDefaultFileStore = () => rmSync2(keyFilePath2(), { force: true });
|
|
6733
|
-
var messageOf2 = (error) => redactSecrets(error instanceof Error ? error.message : String(error));
|
|
6734
|
-
var resolveJevEndpointKey = async (deps = {}) => {
|
|
6735
|
-
const secrets = deps.secrets === undefined ? runtimeSecrets2() : deps.secrets;
|
|
6736
|
-
if (secrets) {
|
|
6737
|
-
try {
|
|
6738
|
-
const stored = normalizeKey2(await secrets.get({
|
|
6739
|
-
name: JEV_ENDPOINT_KEY_NAME,
|
|
6740
|
-
service: JEV_ENDPOINT_KEY_SERVICE
|
|
6741
|
-
}));
|
|
6742
|
-
if (stored) {
|
|
6743
|
-
return { key: stored, source: "jev-bun-secrets" };
|
|
6744
|
-
}
|
|
6745
|
-
} catch {}
|
|
6746
|
-
}
|
|
6747
|
-
const env = deps.env ?? process.env;
|
|
6748
|
-
const fromEnv = normalizeKey2(env[JEV_ENDPOINT_KEY_ENV_VAR]);
|
|
6749
|
-
if (fromEnv) {
|
|
6750
|
-
return { key: fromEnv, source: "jev-env" };
|
|
6751
|
-
}
|
|
6752
|
-
const fromFile = normalizeKey2((deps.readFileStore ?? readDefaultFileStore)());
|
|
6753
|
-
return fromFile ? { key: fromFile, source: "jev-file" } : {};
|
|
6754
|
-
};
|
|
6755
|
-
var writeJevEndpointKey = async (key, deps = {}) => {
|
|
6756
|
-
const normalized = normalizeKey2(key);
|
|
6757
|
-
if (!normalized) {
|
|
6758
|
-
return { message: "The endpoint API key is empty.", ok: false };
|
|
6759
|
-
}
|
|
6760
|
-
const secrets = deps.secrets === undefined ? runtimeSecrets2() : deps.secrets;
|
|
6761
|
-
if (secrets) {
|
|
6762
|
-
try {
|
|
6763
|
-
await secrets.set({
|
|
6764
|
-
name: JEV_ENDPOINT_KEY_NAME,
|
|
6765
|
-
service: JEV_ENDPOINT_KEY_SERVICE,
|
|
6766
|
-
value: normalized
|
|
6767
|
-
});
|
|
6768
|
-
return {
|
|
6769
|
-
message: "Endpoint API key stored in Bun.secrets.",
|
|
6770
|
-
ok: true
|
|
6771
|
-
};
|
|
6772
|
-
} catch (error) {
|
|
6773
|
-
return {
|
|
6774
|
-
message: `Storing the endpoint API key in Bun.secrets failed: ${messageOf2(error)}. Alternatively set ${JEV_ENDPOINT_KEY_ENV_VAR} in your shell profile.`,
|
|
6775
|
-
ok: false
|
|
6776
|
-
};
|
|
6777
|
-
}
|
|
6778
|
-
}
|
|
6779
|
-
try {
|
|
6780
|
-
(deps.writeFileStore ?? writeDefaultFileStore)(normalized);
|
|
6781
|
-
return {
|
|
6782
|
-
message: "Endpoint API key stored in the Pi agent directory (mode 0600).",
|
|
6783
|
-
ok: true
|
|
6784
|
-
};
|
|
6785
|
-
} catch (error) {
|
|
6786
|
-
return {
|
|
6787
|
-
message: `Storing the endpoint API key failed: ${messageOf2(error)}. Alternatively set ${JEV_ENDPOINT_KEY_ENV_VAR} in your shell profile.`,
|
|
6788
|
-
ok: false
|
|
6789
|
-
};
|
|
6790
|
-
}
|
|
6791
|
-
};
|
|
6792
|
-
var clearJevEndpointKey = async (deps = {}) => {
|
|
6793
|
-
let clearedSomething = false;
|
|
6794
|
-
let firstError;
|
|
6795
|
-
const secrets = deps.secrets === undefined ? runtimeSecrets2() : deps.secrets;
|
|
6796
|
-
if (secrets) {
|
|
6797
|
-
try {
|
|
6798
|
-
await secrets.delete({
|
|
6799
|
-
name: JEV_ENDPOINT_KEY_NAME,
|
|
6800
|
-
service: JEV_ENDPOINT_KEY_SERVICE
|
|
6801
|
-
});
|
|
6802
|
-
clearedSomething = true;
|
|
6803
|
-
} catch (error) {
|
|
6804
|
-
firstError = messageOf2(error);
|
|
6805
|
-
}
|
|
6264
|
+
const inputTokens = usage.input;
|
|
6265
|
+
const outputTokens = usage.output;
|
|
6266
|
+
const inputCost = usage.cost?.input;
|
|
6267
|
+
const cacheReadCost = usage.cost?.cacheRead;
|
|
6268
|
+
const cacheWriteCost = usage.cost?.cacheWrite;
|
|
6269
|
+
if (!Number.isFinite(inputTokens) || inputTokens < 0 || !Number.isFinite(outputTokens) || outputTokens < 0) {
|
|
6270
|
+
return;
|
|
6806
6271
|
}
|
|
6807
|
-
|
|
6808
|
-
|
|
6809
|
-
|
|
6810
|
-
|
|
6811
|
-
|
|
6272
|
+
const completeCost = Number.isFinite(inputCost) && inputCost >= 0 && Number.isFinite(cacheReadCost) && cacheReadCost >= 0 && Number.isFinite(cacheWriteCost) && cacheWriteCost >= 0;
|
|
6273
|
+
const cost = completeCost ? inputCost + cacheReadCost + cacheWriteCost : undefined;
|
|
6274
|
+
const piUsage = completeCost && cost !== undefined ? {
|
|
6275
|
+
...usage,
|
|
6276
|
+
cost: {
|
|
6277
|
+
...usage.cost,
|
|
6278
|
+
input: inputCost,
|
|
6279
|
+
output: 0,
|
|
6280
|
+
total: cost
|
|
6812
6281
|
}
|
|
6813
|
-
|
|
6814
|
-
|
|
6815
|
-
firstError ??= messageOf2(error);
|
|
6816
|
-
}
|
|
6817
|
-
if (firstError) {
|
|
6818
|
-
return {
|
|
6819
|
-
message: `Clearing the stored endpoint API key failed: ${firstError}.`,
|
|
6820
|
-
ok: false
|
|
6821
|
-
};
|
|
6822
|
-
}
|
|
6823
|
-
return {
|
|
6824
|
-
message: `Stored endpoint API key cleared.${clearedSomething ? "" : ` Nothing was stored; unset ${JEV_ENDPOINT_KEY_ENV_VAR} yourself if you use it.`}`,
|
|
6825
|
-
ok: true
|
|
6826
|
-
};
|
|
6827
|
-
};
|
|
6828
|
-
|
|
6829
|
-
// src/jev/openai-key-store.ts
|
|
6830
|
-
import {
|
|
6831
|
-
chmodSync as chmodSync3,
|
|
6832
|
-
existsSync as existsSync5,
|
|
6833
|
-
readFileSync as readFileSync5,
|
|
6834
|
-
rmSync as rmSync3,
|
|
6835
|
-
writeFileSync as writeFileSync4
|
|
6836
|
-
} from "node:fs";
|
|
6837
|
-
import { join as join6 } from "node:path";
|
|
6838
|
-
import { getAgentDir as getAgentDir6 } from "@earendil-works/pi-coding-agent";
|
|
6839
|
-
var OPENAI_DECISIONS_KEY_SERVICE = "pi-advisor";
|
|
6840
|
-
var OPENAI_DECISIONS_KEY_NAME = "openai-decisions-api-key";
|
|
6841
|
-
var KEY_FILE_MODE3 = 384;
|
|
6842
|
-
var runtimeSecrets3 = () => {
|
|
6843
|
-
const bun = globalThis;
|
|
6844
|
-
return bun.Bun?.secrets;
|
|
6282
|
+
} : undefined;
|
|
6283
|
+
return { cost, inputTokens, outputTokens, piUsage };
|
|
6845
6284
|
};
|
|
6846
|
-
var
|
|
6847
|
-
var
|
|
6848
|
-
|
|
6849
|
-
|
|
6850
|
-
return normalizeKey3(readFileSync5(keyFilePath3(), "utf-8"));
|
|
6851
|
-
} catch {
|
|
6852
|
-
return;
|
|
6285
|
+
var abortError = () => new DOMException("Jev classifier call aborted by the caller.", "AbortError");
|
|
6286
|
+
var normalizeClassifierAnswers = (result, required) => {
|
|
6287
|
+
if (!isRecordOf(result.answers)) {
|
|
6288
|
+
throw new JevFailureError("malformed", "Classifier returned malformed answers.");
|
|
6853
6289
|
}
|
|
6854
|
-
};
|
|
6855
|
-
|
|
6856
|
-
|
|
6857
|
-
|
|
6858
|
-
|
|
6859
|
-
|
|
6860
|
-
}
|
|
6861
|
-
|
|
6862
|
-
|
|
6863
|
-
|
|
6864
|
-
const secrets = deps.secrets === undefined ? runtimeSecrets3() : deps.secrets;
|
|
6865
|
-
if (secrets) {
|
|
6866
|
-
let secretKey;
|
|
6867
|
-
try {
|
|
6868
|
-
secretKey = normalizeKey3(await secrets.get({
|
|
6869
|
-
name: OPENAI_DECISIONS_KEY_NAME,
|
|
6870
|
-
service: OPENAI_DECISIONS_KEY_SERVICE
|
|
6871
|
-
}));
|
|
6872
|
-
} catch {
|
|
6873
|
-
secretKey = undefined;
|
|
6290
|
+
const answers = {};
|
|
6291
|
+
for (const name of required) {
|
|
6292
|
+
const answer = result.answers[name];
|
|
6293
|
+
if (isRecordOf(answer) && answer.type === "bool" && isProbability(answer.probability)) {
|
|
6294
|
+
if (name === "self_answerable" || name === "should_consult") {
|
|
6295
|
+
answers[name] = { noul: answer.probability, type: "noul" };
|
|
6296
|
+
} else {
|
|
6297
|
+
throw new JevFailureError("malformed", `Classifier returned an unexpected bool answer for ${name}.`);
|
|
6298
|
+
}
|
|
6299
|
+
continue;
|
|
6874
6300
|
}
|
|
6875
|
-
if (
|
|
6876
|
-
|
|
6301
|
+
if (isRecordOf(answer) && answer.type === "choice") {
|
|
6302
|
+
if (!isRecordOf(answer.probabilities) || !isString(answer.choice)) {
|
|
6303
|
+
throw new JevFailureError("malformed", `Classifier returned malformed ${name} choice.`);
|
|
6304
|
+
}
|
|
6305
|
+
const probabilities = {};
|
|
6306
|
+
for (const [label, probability] of Object.entries(answer.probabilities)) {
|
|
6307
|
+
if (!isProbability(probability)) {
|
|
6308
|
+
throw new JevFailureError("malformed", `Classifier returned an invalid ${name} probability.`);
|
|
6309
|
+
}
|
|
6310
|
+
probabilities[label] = probability;
|
|
6311
|
+
}
|
|
6312
|
+
if (name === "stakes") {
|
|
6313
|
+
const expected = ["high", "moderate", "negligible"];
|
|
6314
|
+
const distributionTotal = expected.reduce((sum, label) => sum + (probabilities[label] ?? 0), 0);
|
|
6315
|
+
if (!expected.includes(answer.choice) || expected.some((label) => !Object.hasOwn(probabilities, label)) || distributionTotal <= 0 || Math.abs(distributionTotal - 1) > 0.05) {
|
|
6316
|
+
throw new JevFailureError("malformed", "Classifier returned an incomplete stakes distribution.");
|
|
6317
|
+
}
|
|
6318
|
+
if (!isProbability(answer.confidence)) {
|
|
6319
|
+
throw new JevFailureError("malformed", "Classifier returned invalid stakes confidence.");
|
|
6320
|
+
}
|
|
6321
|
+
answers.stakes = {
|
|
6322
|
+
choice: answer.choice,
|
|
6323
|
+
confidence: answer.confidence,
|
|
6324
|
+
probabilities,
|
|
6325
|
+
type: "choice"
|
|
6326
|
+
};
|
|
6327
|
+
} else {
|
|
6328
|
+
throw new JevFailureError("malformed", `Classifier returned unsupported choice answer for ${name}.`);
|
|
6329
|
+
}
|
|
6330
|
+
continue;
|
|
6877
6331
|
}
|
|
6332
|
+
throw new JevFailureError("malformed", `Classifier omitted or invalidated ${name}.`);
|
|
6878
6333
|
}
|
|
6879
|
-
|
|
6880
|
-
return key ? { key, source: "openai-file" } : {};
|
|
6334
|
+
return answers;
|
|
6881
6335
|
};
|
|
6882
|
-
var
|
|
6883
|
-
const
|
|
6884
|
-
|
|
6885
|
-
|
|
6886
|
-
|
|
6887
|
-
|
|
6888
|
-
|
|
6889
|
-
|
|
6890
|
-
|
|
6891
|
-
|
|
6892
|
-
|
|
6893
|
-
|
|
6894
|
-
|
|
6895
|
-
|
|
6896
|
-
|
|
6897
|
-
|
|
6898
|
-
|
|
6899
|
-
|
|
6900
|
-
};
|
|
6901
|
-
}
|
|
6336
|
+
var classifyWithinDeadline = async (ctx, model, state, questions, options, timeoutMs) => {
|
|
6337
|
+
const controller = new AbortController;
|
|
6338
|
+
const callerSignal = options.signal ?? ctx.signal;
|
|
6339
|
+
const abortFromCaller = () => controller.abort(callerSignal?.reason);
|
|
6340
|
+
if (callerSignal?.aborted) {
|
|
6341
|
+
throw abortError();
|
|
6342
|
+
}
|
|
6343
|
+
callerSignal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
6344
|
+
const timeout = setTimeout(() => {
|
|
6345
|
+
controller.abort(new DOMException("Timed out", "TimeoutError"));
|
|
6346
|
+
}, timeoutMs);
|
|
6347
|
+
const apiOptions = {
|
|
6348
|
+
maxRetries: 0,
|
|
6349
|
+
signal: controller.signal,
|
|
6350
|
+
timeoutMs
|
|
6351
|
+
};
|
|
6352
|
+
if (options.apiKey !== undefined) {
|
|
6353
|
+
apiOptions.apiKey = options.apiKey;
|
|
6902
6354
|
}
|
|
6355
|
+
let abortListener;
|
|
6903
6356
|
try {
|
|
6904
|
-
(
|
|
6905
|
-
|
|
6906
|
-
|
|
6907
|
-
|
|
6357
|
+
const classification = ctx.modelRegistry.classify(model, { questions, state }, apiOptions);
|
|
6358
|
+
const aborted = Promise.withResolvers();
|
|
6359
|
+
abortListener = () => {
|
|
6360
|
+
aborted.reject(controller.signal.reason ?? abortError());
|
|
6908
6361
|
};
|
|
6362
|
+
controller.signal.addEventListener("abort", abortListener, { once: true });
|
|
6363
|
+
const request = Promise.race([classification, aborted.promise]);
|
|
6364
|
+
return await request;
|
|
6909
6365
|
} catch (error) {
|
|
6910
|
-
|
|
6911
|
-
|
|
6912
|
-
ok: false
|
|
6913
|
-
};
|
|
6914
|
-
}
|
|
6915
|
-
};
|
|
6916
|
-
var clearOpenAIDecisionsKey = async (deps = {}) => {
|
|
6917
|
-
let firstError;
|
|
6918
|
-
const secrets = deps.secrets === undefined ? runtimeSecrets3() : deps.secrets;
|
|
6919
|
-
if (secrets) {
|
|
6920
|
-
try {
|
|
6921
|
-
await secrets.delete({
|
|
6922
|
-
name: OPENAI_DECISIONS_KEY_NAME,
|
|
6923
|
-
service: OPENAI_DECISIONS_KEY_SERVICE
|
|
6924
|
-
});
|
|
6925
|
-
} catch (error) {
|
|
6926
|
-
firstError = messageOf3(error);
|
|
6366
|
+
if (callerSignal?.aborted) {
|
|
6367
|
+
throw error;
|
|
6927
6368
|
}
|
|
6928
|
-
|
|
6929
|
-
|
|
6930
|
-
if (deps.deleteFileStore) {
|
|
6931
|
-
deps.deleteFileStore();
|
|
6932
|
-
} else if (existsSync5(keyFilePath3())) {
|
|
6933
|
-
deleteDefaultFileStore2();
|
|
6369
|
+
if (controller.signal.aborted) {
|
|
6370
|
+
throw new JevFailureError("timeout", `Jev classifier timed out after ${timeoutMs}ms.`);
|
|
6934
6371
|
}
|
|
6935
|
-
|
|
6936
|
-
|
|
6937
|
-
}
|
|
6938
|
-
if (firstError) {
|
|
6939
|
-
return {
|
|
6940
|
-
message: `Clearing the OpenAI API key failed: ${firstError}.`,
|
|
6941
|
-
ok: false
|
|
6942
|
-
};
|
|
6943
|
-
}
|
|
6944
|
-
return { message: "Stored OpenAI API key cleared.", ok: true };
|
|
6945
|
-
};
|
|
6946
|
-
|
|
6947
|
-
// src/jev/transport.ts
|
|
6948
|
-
var OPENROUTER_PROVIDER = "openrouter";
|
|
6949
|
-
var OPENAI_PROVIDER = "openai";
|
|
6950
|
-
var OPENAI_API_KEY_SOURCES = new Map([
|
|
6951
|
-
["OPENAI_API_KEY", "openai-env"],
|
|
6952
|
-
["stored credential", "openai-provider-credential"]
|
|
6953
|
-
]);
|
|
6954
|
-
var openAiPlatformCredentials = async (ctx, deps) => {
|
|
6955
|
-
let result;
|
|
6956
|
-
try {
|
|
6957
|
-
result = deps.getProviderAuth ? await deps.getProviderAuth(OPENAI_PROVIDER) : await ctx?.modelRegistry?.getProviderAuth(OPENAI_PROVIDER);
|
|
6958
|
-
} catch {
|
|
6959
|
-
result = undefined;
|
|
6960
|
-
}
|
|
6961
|
-
const source = result?.source;
|
|
6962
|
-
const credentialSource = source ? OPENAI_API_KEY_SOURCES.get(source) : undefined;
|
|
6963
|
-
const apiKey = result?.auth.apiKey?.trim();
|
|
6964
|
-
if (credentialSource && apiKey) {
|
|
6965
|
-
return {
|
|
6966
|
-
apiKey,
|
|
6967
|
-
source: credentialSource,
|
|
6968
|
-
transport: "openai-decisions"
|
|
6969
|
-
};
|
|
6970
|
-
}
|
|
6971
|
-
try {
|
|
6972
|
-
const stored = await (deps.resolveOpenAIKey ?? resolveOpenAIDecisionsKey)();
|
|
6973
|
-
const storedKey = stored.key?.trim();
|
|
6974
|
-
return storedKey && stored.source ? {
|
|
6975
|
-
apiKey: storedKey,
|
|
6976
|
-
source: stored.source,
|
|
6977
|
-
transport: "openai-decisions"
|
|
6978
|
-
} : undefined;
|
|
6979
|
-
} catch {
|
|
6980
|
-
return;
|
|
6981
|
-
}
|
|
6982
|
-
};
|
|
6983
|
-
var openRouterKey = async (ctx, deps) => {
|
|
6984
|
-
const key = deps.getProviderKey ? await deps.getProviderKey(OPENROUTER_PROVIDER) : await ctx?.modelRegistry?.getApiKeyForProvider(OPENROUTER_PROVIDER);
|
|
6985
|
-
return key?.trim() || undefined;
|
|
6986
|
-
};
|
|
6987
|
-
var typeSafeCredentials = async (deps) => {
|
|
6988
|
-
const resolution = await (deps.resolveTypesafe ?? resolveTypeSafeKey)();
|
|
6989
|
-
if (!resolution.key) {
|
|
6990
|
-
return;
|
|
6991
|
-
}
|
|
6992
|
-
const credentials = {
|
|
6993
|
-
apiKey: resolution.key,
|
|
6994
|
-
transport: "typesafe"
|
|
6995
|
-
};
|
|
6996
|
-
if (resolution.source) {
|
|
6997
|
-
credentials.source = resolution.source;
|
|
6998
|
-
}
|
|
6999
|
-
return credentials;
|
|
7000
|
-
};
|
|
7001
|
-
var resolveTypesafeCompatibleCredentials = async (target, ctx, deps = {}) => {
|
|
7002
|
-
const { baseUrl } = target;
|
|
7003
|
-
const provider = target.keyProvider;
|
|
7004
|
-
if (provider) {
|
|
7005
|
-
let providerKey;
|
|
7006
|
-
try {
|
|
7007
|
-
providerKey = deps.getProviderKey ? await deps.getProviderKey(provider) : await ctx?.modelRegistry?.getApiKeyForProvider(provider);
|
|
7008
|
-
} catch {
|
|
7009
|
-
providerKey = undefined;
|
|
6372
|
+
if (error instanceof JevFailureError) {
|
|
6373
|
+
throw error;
|
|
7010
6374
|
}
|
|
7011
|
-
|
|
7012
|
-
|
|
7013
|
-
|
|
6375
|
+
throw new JevFailureError("error", "Jev classifier request failed.");
|
|
6376
|
+
} finally {
|
|
6377
|
+
clearTimeout(timeout);
|
|
6378
|
+
callerSignal?.removeEventListener("abort", abortFromCaller);
|
|
6379
|
+
if (abortListener) {
|
|
6380
|
+
controller.signal.removeEventListener("abort", abortListener);
|
|
7014
6381
|
}
|
|
7015
|
-
return {
|
|
7016
|
-
apiKey: trimmed,
|
|
7017
|
-
baseUrl,
|
|
7018
|
-
source: "provider-credential",
|
|
7019
|
-
transport: "typesafe-compatible"
|
|
7020
|
-
};
|
|
7021
6382
|
}
|
|
7022
|
-
const resolution = await (deps.resolveEndpointKey ?? resolveJevEndpointKey)();
|
|
7023
|
-
if (!resolution.key) {
|
|
7024
|
-
return;
|
|
7025
|
-
}
|
|
7026
|
-
const credentials = {
|
|
7027
|
-
apiKey: resolution.key,
|
|
7028
|
-
baseUrl,
|
|
7029
|
-
transport: "typesafe-compatible"
|
|
7030
|
-
};
|
|
7031
|
-
if (resolution.source) {
|
|
7032
|
-
credentials.source = resolution.source;
|
|
7033
|
-
}
|
|
7034
|
-
return credentials;
|
|
7035
6383
|
};
|
|
7036
|
-
var
|
|
7037
|
-
const
|
|
7038
|
-
if (!
|
|
7039
|
-
|
|
6384
|
+
var classifyWithJev = async (ctx, ref, state, questions, options = {}) => {
|
|
6385
|
+
const parsed = parseJevClassifierRef(ref);
|
|
6386
|
+
if (!parsed) {
|
|
6387
|
+
throw new JevFailureError("malformed", "No Jev classifier is selected.");
|
|
7040
6388
|
}
|
|
7041
|
-
const
|
|
7042
|
-
if (!
|
|
7043
|
-
|
|
6389
|
+
const model = ctx.modelRegistry.findOfType("classifier", parsed.provider, parsed.modelId);
|
|
6390
|
+
if (!isClassifierModel(model)) {
|
|
6391
|
+
throw new JevFailureError("error", `Selected classifier ${ref} is unavailable.`);
|
|
6392
|
+
}
|
|
6393
|
+
const timeoutMs = Math.max(1, advisorJevTimeoutMsRef);
|
|
6394
|
+
const result = await classifyWithinDeadline(ctx, model, state, questions, options, timeoutMs);
|
|
6395
|
+
const stopped = result.stopReason !== "stop";
|
|
6396
|
+
if (stopped) {
|
|
6397
|
+
throw new JevFailureError("error", `Jev classifier stopped: ${result.stopReason}.`);
|
|
7044
6398
|
}
|
|
7045
|
-
|
|
7046
|
-
|
|
6399
|
+
return {
|
|
6400
|
+
answers: normalizeClassifierAnswers(result, Object.keys(questions)),
|
|
6401
|
+
model: `${result.provider}/${result.model}`,
|
|
6402
|
+
usage: normalizedUsage(result.usage)
|
|
6403
|
+
};
|
|
7047
6404
|
};
|
|
7048
|
-
var
|
|
7049
|
-
|
|
7050
|
-
|
|
6405
|
+
var classifyScreening = (ctx, ref, state, options) => classifyWithJev(ctx, ref, state, nativeScreeningQuestions, options);
|
|
6406
|
+
var classifyTurnGate = (ctx, ref, state, options) => classifyWithJev(ctx, ref, state, {
|
|
6407
|
+
should_consult: {
|
|
6408
|
+
criteria: {
|
|
6409
|
+
false: "The executor is progressing soundly on work that matches the user's intent; interrupting would add nothing material.",
|
|
6410
|
+
true: "The executor is approaching a material decision, repeating a failure, about to claim success without validation, or drifting from the user's intent; a second opinion now would change what happens next."
|
|
6411
|
+
},
|
|
6412
|
+
instructions: "Should a senior engineering advisor be consulted right now, before the executor continues? Judge from `recent_conversation`.",
|
|
6413
|
+
type: "bool"
|
|
7051
6414
|
}
|
|
7052
|
-
|
|
7053
|
-
|
|
6415
|
+
}, options);
|
|
6416
|
+
|
|
6417
|
+
// src/jev/state.ts
|
|
6418
|
+
var JEV_TEXT_CAP_BYTES = 8 * 1024;
|
|
6419
|
+
var buildJevState = (ctx, input = {}) => {
|
|
6420
|
+
const fields = new Map([["role", "executor"]]);
|
|
6421
|
+
if (input.question) {
|
|
6422
|
+
fields.set("executor_question", redactAndCapText(input.question, JEV_TEXT_CAP_BYTES, advisorRedactSecretsRef));
|
|
7054
6423
|
}
|
|
7055
|
-
if (
|
|
7056
|
-
|
|
7057
|
-
return key ? { apiKey: key, transport } : undefined;
|
|
6424
|
+
if (input.draft) {
|
|
6425
|
+
fields.set("executor_draft", redactAndCapText(input.draft, JEV_TEXT_CAP_BYTES, advisorRedactSecretsRef));
|
|
7058
6426
|
}
|
|
7059
|
-
|
|
7060
|
-
|
|
7061
|
-
|
|
7062
|
-
const preference = advisorJevTransportRef;
|
|
7063
|
-
if (preference !== "auto") {
|
|
7064
|
-
return resolveJevTransportFor(preference, ctx, deps);
|
|
6427
|
+
const digest = recentConversation(ctx, advisorJevDigestMaxCharsRef);
|
|
6428
|
+
if (digest) {
|
|
6429
|
+
fields.set("recent_conversation", digest);
|
|
7065
6430
|
}
|
|
7066
|
-
|
|
7067
|
-
return typesafe ?? resolveJevTransportFor("openrouter", ctx, deps);
|
|
6431
|
+
return Object.fromEntries(fields);
|
|
7068
6432
|
};
|
|
7069
6433
|
|
|
7070
6434
|
// src/tools/outage-notifier.ts
|
|
@@ -7094,7 +6458,7 @@ var SCREENED_SKIP_TEXT = "Advisor consultation skipped (screened out): the stake
|
|
|
7094
6458
|
var repeatSkipText = (advice) => `Advisor consultation skipped (already answered): this question was answered earlier in this session; the earlier advice is reattached below. Consult again only if the situation has materially changed.
|
|
7095
6459
|
|
|
7096
6460
|
${advice}`;
|
|
7097
|
-
var outageNotifier = createOutageNotifier((category, message) => `Advisor Jev
|
|
6461
|
+
var outageNotifier = createOutageNotifier((category, message) => `Advisor Jev classifier failed (${category}); allowing consultations. ${message}`);
|
|
7098
6462
|
var notifyOutageOnce = outageNotifier.notify;
|
|
7099
6463
|
var resetJevOutageNotification = outageNotifier.reset;
|
|
7100
6464
|
var allow = () => ({ decision: "allow" });
|
|
@@ -7112,23 +6476,29 @@ var bypassOutcome = (session, options, normalizedQuestion) => {
|
|
|
7112
6476
|
}
|
|
7113
6477
|
return;
|
|
7114
6478
|
};
|
|
6479
|
+
var classifierRef = (deps) => advisorJevClassifierModeRef === "api-key" ? "typesafe/jev-latest" : deps.selectedRef?.() ?? advisorJevClassifierRef;
|
|
6480
|
+
var apiKeyFor = (deps) => advisorJevClassifierModeRef === "api-key" ? (deps.resolveApiKey ?? consumeJevClassifierKey)() : Promise.resolve(undefined);
|
|
7115
6481
|
var screenWithJev = async (ctx, session, options, deps, normalizedQuestion) => {
|
|
7116
|
-
const
|
|
7117
|
-
|
|
6482
|
+
const selectedRef = classifierRef(deps);
|
|
6483
|
+
const apiKey = await apiKeyFor(deps);
|
|
6484
|
+
if (!selectedRef || advisorJevClassifierModeRef === "api-key" && !apiKey) {
|
|
7118
6485
|
session.recordJevFilterFailure();
|
|
7119
|
-
notifyOutageOnce(ctx, "missing-key", "No credentials resolved for the selected Jev
|
|
6486
|
+
notifyOutageOnce(ctx, "missing-key", "No credentials resolved for the selected Jev classifier.");
|
|
7120
6487
|
return allow();
|
|
7121
6488
|
}
|
|
7122
|
-
if (credentials.source === "advisor-json") {
|
|
7123
|
-
const warning = consumePlaintextKeyWarning();
|
|
7124
|
-
if (warning && ctx.hasUI) {
|
|
7125
|
-
ctx.ui.notify(warning, "warning");
|
|
7126
|
-
}
|
|
7127
|
-
}
|
|
7128
|
-
const client = jevClientFromCredentials(credentials, deps.fetch);
|
|
7129
6489
|
try {
|
|
7130
|
-
const
|
|
7131
|
-
|
|
6490
|
+
const state = buildJevState(ctx, options);
|
|
6491
|
+
const result = deps.classifyResult ? await deps.classifyResult(state, options.signal) : await (deps.classify ?? classifyScreening)(ctx, selectedRef, state, {
|
|
6492
|
+
apiKey: advisorJevClassifierModeRef === "api-key" ? apiKey : undefined,
|
|
6493
|
+
signal: options.signal
|
|
6494
|
+
});
|
|
6495
|
+
if (result.usage) {
|
|
6496
|
+
session.recordJevFilterUsage({
|
|
6497
|
+
cost: result.usage.cost,
|
|
6498
|
+
inputTokens: result.usage.inputTokens,
|
|
6499
|
+
outputTokens: result.usage.outputTokens
|
|
6500
|
+
});
|
|
6501
|
+
}
|
|
7132
6502
|
const verdict = composeScreeningVerdict(result.answers, {
|
|
7133
6503
|
noulMargin: advisorJevFilterNoulMarginRef,
|
|
7134
6504
|
skipConfidence: advisorJevFilterSkipConfidenceRef
|
|
@@ -7138,11 +6508,12 @@ var screenWithJev = async (ctx, session, options, deps, normalizedQuestion) => {
|
|
|
7138
6508
|
return {
|
|
7139
6509
|
decision: "skip",
|
|
7140
6510
|
kind: "screened",
|
|
7141
|
-
reason: "low stakes and resolvable without a consultation"
|
|
6511
|
+
reason: "low stakes and resolvable without a consultation",
|
|
6512
|
+
usage: result.usage?.piUsage
|
|
7142
6513
|
};
|
|
7143
6514
|
}
|
|
7144
6515
|
session.recordJevFilterAllowed();
|
|
7145
|
-
return allow
|
|
6516
|
+
return { decision: "allow", usage: result.usage?.piUsage };
|
|
7146
6517
|
} catch (error) {
|
|
7147
6518
|
if (options.signal?.aborted) {
|
|
7148
6519
|
throw error;
|
|
@@ -7454,9 +6825,9 @@ import {
|
|
|
7454
6825
|
unlink,
|
|
7455
6826
|
writeFile
|
|
7456
6827
|
} from "node:fs/promises";
|
|
7457
|
-
import { join as
|
|
6828
|
+
import { join as join5 } from "node:path";
|
|
7458
6829
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
7459
|
-
import { getAgentDir as
|
|
6830
|
+
import { getAgentDir as getAgentDir5 } from "@earendil-works/pi-coding-agent";
|
|
7460
6831
|
var ADOPTIONS = [
|
|
7461
6832
|
"followed",
|
|
7462
6833
|
"not-followed",
|
|
@@ -7476,12 +6847,12 @@ var OUTCOME_TRIGGERS = [
|
|
|
7476
6847
|
];
|
|
7477
6848
|
var MAX_LOG_BYTES = 1024 * 1024;
|
|
7478
6849
|
var OUTCOME_LOG_MAX_BYTES = MAX_LOG_BYTES;
|
|
7479
|
-
var statePath = () =>
|
|
7480
|
-
var outcomeLogPath = () =>
|
|
6850
|
+
var statePath = () => join5(getAgentDir5(), "advisor-outcomes-salt");
|
|
6851
|
+
var outcomeLogPath = () => join5(getAgentDir5(), "advisor-outcomes.jsonl");
|
|
7481
6852
|
var isErrnoException = (error) => error instanceof Error && ("code" in error);
|
|
7482
6853
|
var salt = async () => {
|
|
7483
6854
|
const path = statePath();
|
|
7484
|
-
await mkdir(
|
|
6855
|
+
await mkdir(getAgentDir5(), { mode: 448, recursive: true });
|
|
7485
6856
|
for (let attempt = 0;attempt < 20; attempt += 1) {
|
|
7486
6857
|
try {
|
|
7487
6858
|
const existing = await readFile(path);
|
|
@@ -7558,7 +6929,7 @@ var withOutcomeLock = async (run) => {
|
|
|
7558
6929
|
var adviceDigest = (advice, key) => createHmac("sha256", key).update(advice).digest("hex").slice(0, 16);
|
|
7559
6930
|
var appendOutcome = async (record) => {
|
|
7560
6931
|
const path = outcomeLogPath();
|
|
7561
|
-
await mkdir(
|
|
6932
|
+
await mkdir(getAgentDir5(), { mode: 448, recursive: true });
|
|
7562
6933
|
return withOutcomeLock(async () => {
|
|
7563
6934
|
const next = {
|
|
7564
6935
|
adoption: record.adoption,
|
|
@@ -7853,7 +7224,7 @@ var registerCommandRenderers = (runtime) => {
|
|
|
7853
7224
|
|
|
7854
7225
|
// src/ui/settings-selector.ts
|
|
7855
7226
|
import { getSettingsListTheme } from "@earendil-works/pi-coding-agent";
|
|
7856
|
-
import { SettingsList, truncateToWidth as
|
|
7227
|
+
import { SettingsList, truncateToWidth as truncateToWidth7 } from "@earendil-works/pi-tui";
|
|
7857
7228
|
|
|
7858
7229
|
// src/ui/settings-formatting.ts
|
|
7859
7230
|
import { visibleWidth as visibleWidth3 } from "@earendil-works/pi-tui";
|
|
@@ -7945,407 +7316,11 @@ var rainbowGradient = (text, startedAt) => {
|
|
|
7945
7316
|
// src/ui/settings-items.ts
|
|
7946
7317
|
import { getKeybindings } from "@earendil-works/pi-tui";
|
|
7947
7318
|
|
|
7948
|
-
// src/ui/jev-
|
|
7949
|
-
import {
|
|
7950
|
-
|
|
7951
|
-
// node_modules/@typesafe-ai/sdk/dist/index.mjs
|
|
7952
|
-
var range2 = (from, to) => Array.from({ length: to - from }, (_, i) => from + i);
|
|
7953
|
-
var DEFAULT_RETRY_POLICY = {
|
|
7954
|
-
maxRetries: 2,
|
|
7955
|
-
backoffInitialMs: 500,
|
|
7956
|
-
backoffMaxMs: 5000,
|
|
7957
|
-
backoffJitter: 0.25,
|
|
7958
|
-
httpStatuses: /* @__PURE__ */ new Set([
|
|
7959
|
-
408,
|
|
7960
|
-
429,
|
|
7961
|
-
...range2(500, 600)
|
|
7962
|
-
]),
|
|
7963
|
-
respectRetryAfter: true,
|
|
7964
|
-
maxRetryAfterMs: 60000,
|
|
7965
|
-
apiConnectionError: true,
|
|
7966
|
-
apiTimeoutError: true
|
|
7967
|
-
};
|
|
7968
|
-
DEFAULT_RETRY_POLICY.maxRetries;
|
|
7969
|
-
var noul = (instructions = null, criteria) => ({
|
|
7970
|
-
type: "noul",
|
|
7971
|
-
instructions,
|
|
7972
|
-
criteria
|
|
7973
|
-
});
|
|
7974
|
-
var g = globalThis;
|
|
7975
|
-
var isBrowser = () => typeof g.window !== "undefined" && typeof g.window.document !== "undefined" && typeof g.navigator !== "undefined";
|
|
7976
|
-
var describeRuntime = () => {
|
|
7977
|
-
const platform = g.process?.platform && g.process?.arch ? ` (${g.process.platform}; ${g.process.arch})` : "";
|
|
7978
|
-
if (g.Bun?.version)
|
|
7979
|
-
return `bun/${g.Bun.version}${platform}`;
|
|
7980
|
-
if (g.Deno?.version?.deno)
|
|
7981
|
-
return `deno/${g.Deno.version.deno}${platform}`;
|
|
7982
|
-
if (g.EdgeRuntime !== undefined)
|
|
7983
|
-
return "vercel-edge";
|
|
7984
|
-
if (g.navigator?.userAgent === "Cloudflare-Workers")
|
|
7985
|
-
return "cloudflare-workers";
|
|
7986
|
-
if (g.process?.versions?.node)
|
|
7987
|
-
return `node/${g.process.versions.node}${platform}`;
|
|
7988
|
-
if (isBrowser())
|
|
7989
|
-
return "browser";
|
|
7990
|
-
return "unknown";
|
|
7991
|
-
};
|
|
7992
|
-
var RUNTIME = describeRuntime();
|
|
7993
|
-
|
|
7994
|
-
// src/ui/jev-setup-support.ts
|
|
7995
|
-
var fireAndForget = async (action, onError) => {
|
|
7996
|
-
try {
|
|
7997
|
-
await action;
|
|
7998
|
-
} catch (error) {
|
|
7999
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
8000
|
-
onError(redactSecrets(message));
|
|
8001
|
-
}
|
|
8002
|
-
};
|
|
8003
|
-
var providerName = (transport) => {
|
|
8004
|
-
if (transport === "typesafe") {
|
|
8005
|
-
return "TypeSafe Jev";
|
|
8006
|
-
}
|
|
8007
|
-
if (transport === "openrouter") {
|
|
8008
|
-
return "OpenRouter (Pi login)";
|
|
8009
|
-
}
|
|
8010
|
-
if (transport === "typesafe-compatible") {
|
|
8011
|
-
return "System One–compatible endpoint";
|
|
8012
|
-
}
|
|
8013
|
-
return `OpenAI Decisions (${OPENAI_DECISIONS_MODEL})`;
|
|
8014
|
-
};
|
|
8015
|
-
var endpointSourceLabel = (source) => {
|
|
8016
|
-
switch (source) {
|
|
8017
|
-
case "provider-credential": {
|
|
8018
|
-
return "Pi provider login";
|
|
8019
|
-
}
|
|
8020
|
-
case "jev-bun-secrets": {
|
|
8021
|
-
return "Bun.secrets";
|
|
8022
|
-
}
|
|
8023
|
-
case "jev-file": {
|
|
8024
|
-
return "stored file, mode 0600";
|
|
8025
|
-
}
|
|
8026
|
-
default: {
|
|
8027
|
-
return "JEV_API_KEY";
|
|
8028
|
-
}
|
|
8029
|
-
}
|
|
8030
|
-
};
|
|
8031
|
-
var transportLabel = (credentials) => {
|
|
8032
|
-
if (credentials.transport === "openrouter") {
|
|
8033
|
-
return "OpenRouter (reusing Pi login)";
|
|
8034
|
-
}
|
|
8035
|
-
if (credentials.transport === "typesafe-compatible") {
|
|
8036
|
-
return `System One–compatible (${credentials.baseUrl ?? "no Base URL"}; key: ${endpointSourceLabel(credentials.source)})`;
|
|
8037
|
-
}
|
|
8038
|
-
if (credentials.transport === "openai-decisions") {
|
|
8039
|
-
let source = "verified API key";
|
|
8040
|
-
switch (credentials.source) {
|
|
8041
|
-
case "openai-bun-secrets": {
|
|
8042
|
-
source = "Bun.secrets";
|
|
8043
|
-
break;
|
|
8044
|
-
}
|
|
8045
|
-
case "openai-env": {
|
|
8046
|
-
source = "OPENAI_API_KEY";
|
|
8047
|
-
break;
|
|
8048
|
-
}
|
|
8049
|
-
case "openai-file": {
|
|
8050
|
-
source = "stored file, mode 0600";
|
|
8051
|
-
break;
|
|
8052
|
-
}
|
|
8053
|
-
case "openai-provider-credential": {
|
|
8054
|
-
source = "Pi stored API key";
|
|
8055
|
-
break;
|
|
8056
|
-
}
|
|
8057
|
-
default: {
|
|
8058
|
-
break;
|
|
8059
|
-
}
|
|
8060
|
-
}
|
|
8061
|
-
return `OpenAI Decisions (${OPENAI_DECISIONS_MODEL}; key: ${source})`;
|
|
8062
|
-
}
|
|
8063
|
-
switch (credentials.source) {
|
|
8064
|
-
case "advisor-json": {
|
|
8065
|
-
return "TypeSafe (key: advisor.json — plaintext, not recommended)";
|
|
8066
|
-
}
|
|
8067
|
-
case "bun-secrets": {
|
|
8068
|
-
return "TypeSafe (key: Bun.secrets)";
|
|
8069
|
-
}
|
|
8070
|
-
case "file": {
|
|
8071
|
-
return "TypeSafe (key: stored file, mode 0600)";
|
|
8072
|
-
}
|
|
8073
|
-
default: {
|
|
8074
|
-
return "TypeSafe (key: TYPESAFE_API_KEY)";
|
|
8075
|
-
}
|
|
8076
|
-
}
|
|
8077
|
-
};
|
|
8078
|
-
var canClearCredential = (credentials) => credentials.source === "bun-secrets" || credentials.source === "file" || credentials.source === "openai-bun-secrets" || credentials.source === "openai-file" || credentials.source === "jev-bun-secrets" || credentials.source === "jev-file";
|
|
8079
|
-
var canReplaceWithEnteredKey = (credentials) => credentials.transport === "typesafe" || credentials.transport === "typesafe-compatible" || credentials.transport === "openai-decisions" && (credentials.source === "openai-bun-secrets" || credentials.source === "openai-file");
|
|
8080
|
-
var keyEntryPrompt = (transport) => {
|
|
8081
|
-
if (transport === "openai-decisions") {
|
|
8082
|
-
return "Paste an OpenAI Platform API key";
|
|
8083
|
-
}
|
|
8084
|
-
if (transport === "typesafe-compatible") {
|
|
8085
|
-
return "Paste the endpoint API key";
|
|
8086
|
-
}
|
|
8087
|
-
return "Paste a TypeSafe API key";
|
|
8088
|
-
};
|
|
8089
|
-
var defaultVerify = async (credentials) => {
|
|
8090
|
-
const client = jevClientFromCredentials(credentials);
|
|
8091
|
-
try {
|
|
8092
|
-
await client.ask({ purpose: "pi-advisor setup verification" }, { verified: noul("Answer yes.") });
|
|
8093
|
-
return { ok: true };
|
|
8094
|
-
} catch (error) {
|
|
8095
|
-
return {
|
|
8096
|
-
message: error instanceof Error ? error.message : String(error),
|
|
8097
|
-
ok: false
|
|
8098
|
-
};
|
|
8099
|
-
}
|
|
8100
|
-
};
|
|
8101
|
-
var writeKeyFor = (key, transport) => {
|
|
8102
|
-
if (transport === "openai-decisions") {
|
|
8103
|
-
return writeOpenAIDecisionsKey(key);
|
|
8104
|
-
}
|
|
8105
|
-
if (transport === "typesafe-compatible") {
|
|
8106
|
-
return writeJevEndpointKey(key);
|
|
8107
|
-
}
|
|
8108
|
-
return writeKeyTypeSafeKey(key);
|
|
8109
|
-
};
|
|
8110
|
-
var clearKeyFor = (transport) => {
|
|
8111
|
-
if (transport === "openai-decisions") {
|
|
8112
|
-
return clearOpenAIDecisionsKey();
|
|
8113
|
-
}
|
|
8114
|
-
if (transport === "typesafe-compatible") {
|
|
8115
|
-
return clearJevEndpointKey();
|
|
8116
|
-
}
|
|
8117
|
-
return clearKeyTypeSafeKey();
|
|
8118
|
-
};
|
|
8119
|
-
var defaultSetupDeps = {
|
|
8120
|
-
clearStoredKey: (transport) => clearKeyFor(transport),
|
|
8121
|
-
removePlaintextKey: removeTypeSafeKeyFromAdvisorJson,
|
|
8122
|
-
resolveEndpoint: (options) => resolveTypesafeCompatibleCredentials(options),
|
|
8123
|
-
resolveTransport: (transport) => transport ? resolveJevTransportFor(transport) : resolveJevTransport(),
|
|
8124
|
-
verify: defaultVerify,
|
|
8125
|
-
writeKey: (key, transport) => writeKeyFor(key, transport)
|
|
8126
|
-
};
|
|
8127
|
-
|
|
8128
|
-
// src/ui/jev-filter-submenu.ts
|
|
8129
|
-
var actionsFor = (enabled) => enabled ? ["disable", "done"] : ["enable", "done"];
|
|
8130
|
-
var labelFor2 = (action) => {
|
|
8131
|
-
if (action === "enable") {
|
|
8132
|
-
return "Enable the filter";
|
|
8133
|
-
}
|
|
8134
|
-
return action === "disable" ? "Disable the filter" : "Done";
|
|
8135
|
-
};
|
|
8136
|
-
|
|
8137
|
-
class JevFilterSubmenu {
|
|
8138
|
-
options;
|
|
8139
|
-
deps;
|
|
8140
|
-
credentials;
|
|
8141
|
-
mode = "resolving";
|
|
8142
|
-
notice;
|
|
8143
|
-
selectedIndex = 0;
|
|
8144
|
-
enabled;
|
|
8145
|
-
_focused = true;
|
|
8146
|
-
constructor(options, deps = {}) {
|
|
8147
|
-
this.options = options;
|
|
8148
|
-
this.enabled = options.currentValue === "On";
|
|
8149
|
-
this.deps = { ...defaultSetupDeps, ...options.setupDeps, ...deps };
|
|
8150
|
-
fireAndForget(this.refresh(), this.reportBackgroundError);
|
|
8151
|
-
}
|
|
8152
|
-
get focused() {
|
|
8153
|
-
return this._focused;
|
|
8154
|
-
}
|
|
8155
|
-
set focused(value) {
|
|
8156
|
-
this._focused = value;
|
|
8157
|
-
}
|
|
8158
|
-
invalidate() {}
|
|
8159
|
-
render(width) {
|
|
8160
|
-
const { theme } = this.options;
|
|
8161
|
-
const lines = [
|
|
8162
|
-
theme.fg("accent", theme.bold(" Jev/Decisions consultation filter")),
|
|
8163
|
-
"",
|
|
8164
|
-
` Filter: ${this.enabled ? "On" : "Off"}`,
|
|
8165
|
-
` Provider: ${this.options.currentTransport}`
|
|
8166
|
-
];
|
|
8167
|
-
if (this.mode === "resolving") {
|
|
8168
|
-
lines.push("", " Checking the selected provider credentials…");
|
|
8169
|
-
} else if (this.notice) {
|
|
8170
|
-
lines.push("", theme.fg("warning", ` ${this.notice}`));
|
|
8171
|
-
}
|
|
8172
|
-
if (this.mode === "menu") {
|
|
8173
|
-
lines.push("");
|
|
8174
|
-
for (const [index, action] of actionsFor(this.enabled).entries()) {
|
|
8175
|
-
const prefix = index === this.selectedIndex ? "→ " : " ";
|
|
8176
|
-
lines.push(`${prefix}${labelFor2(action)}`);
|
|
8177
|
-
}
|
|
8178
|
-
}
|
|
8179
|
-
return lines.map((line) => truncateToWidth4(line, width));
|
|
8180
|
-
}
|
|
8181
|
-
handleInput(keyData) {
|
|
8182
|
-
if (this.mode !== "menu") {
|
|
8183
|
-
return;
|
|
8184
|
-
}
|
|
8185
|
-
const actions = actionsFor(this.enabled);
|
|
8186
|
-
if (matchesKey2(keyData, Key2.down) || keyData === "\x1B[B" || keyData === "\x1BOB") {
|
|
8187
|
-
this.selectedIndex = (this.selectedIndex + 1) % actions.length;
|
|
8188
|
-
} else if (matchesKey2(keyData, Key2.up) || keyData === "\x1B[A" || keyData === "\x1BOA") {
|
|
8189
|
-
this.selectedIndex = (this.selectedIndex - 1 + actions.length) % actions.length;
|
|
8190
|
-
} else if (matchesKey2(keyData, Key2.enter) || keyData === "\r") {
|
|
8191
|
-
this.activate(actions[this.selectedIndex]);
|
|
8192
|
-
} else {
|
|
8193
|
-
return;
|
|
8194
|
-
}
|
|
8195
|
-
this.options.tui.requestRender();
|
|
8196
|
-
}
|
|
8197
|
-
async refresh() {
|
|
8198
|
-
const transport = this.options.currentTransport;
|
|
8199
|
-
try {
|
|
8200
|
-
this.credentials = await this.deps.resolveTransport(transport === "auto" ? undefined : transport);
|
|
8201
|
-
} catch (error) {
|
|
8202
|
-
this.credentials = undefined;
|
|
8203
|
-
this.notice = `Credential lookup failed: ${error instanceof Error ? error.message : String(error)}`;
|
|
8204
|
-
}
|
|
8205
|
-
if (!this.credentials && !this.notice) {
|
|
8206
|
-
this.notice = `No Jev provider resolves for "${transport}". Open Jev provider to configure one.`;
|
|
8207
|
-
}
|
|
8208
|
-
this.mode = "menu";
|
|
8209
|
-
this.options.tui.requestRender();
|
|
8210
|
-
}
|
|
8211
|
-
reportBackgroundError = (message) => {
|
|
8212
|
-
this.mode = "menu";
|
|
8213
|
-
this.notice = `Setup failed: ${message}`;
|
|
8214
|
-
this.options.tui.requestRender();
|
|
8215
|
-
};
|
|
8216
|
-
activate(action) {
|
|
8217
|
-
if (action === "done") {
|
|
8218
|
-
this.options.done();
|
|
8219
|
-
return;
|
|
8220
|
-
}
|
|
8221
|
-
const enabled = action === "enable";
|
|
8222
|
-
if (enabled && !this.credentials) {
|
|
8223
|
-
this.options.tui.requestRender();
|
|
8224
|
-
return;
|
|
8225
|
-
}
|
|
8226
|
-
const saved = this.commit({ enabled });
|
|
8227
|
-
if (!saved) {
|
|
8228
|
-
this.notice = "The filter state could not be saved.";
|
|
8229
|
-
this.options.tui.requestRender();
|
|
8230
|
-
return;
|
|
8231
|
-
}
|
|
8232
|
-
this.enabled = enabled;
|
|
8233
|
-
this.notice = undefined;
|
|
8234
|
-
this.options.done(enabled ? "On" : "Off");
|
|
8235
|
-
}
|
|
8236
|
-
commit(selection) {
|
|
8237
|
-
if (!this.options.onSelection) {
|
|
8238
|
-
return true;
|
|
8239
|
-
}
|
|
8240
|
-
return this.options.onSelection(selection);
|
|
8241
|
-
}
|
|
8242
|
-
}
|
|
8243
|
-
|
|
8244
|
-
// src/ui/jev-provider-submenu.ts
|
|
8245
|
-
import { Input as Input3, Key as Key3, matchesKey as matchesKey3 } from "@earendil-works/pi-tui";
|
|
8246
|
-
|
|
8247
|
-
// src/ui/jev-provider-submenu-view.ts
|
|
8248
|
-
import { truncateToWidth as truncateToWidth5 } from "@earendil-works/pi-tui";
|
|
8249
|
-
var providerActions = () => [
|
|
8250
|
-
"typesafe",
|
|
8251
|
-
"openrouter",
|
|
8252
|
-
"openai-decisions",
|
|
8253
|
-
"typesafe-compatible"
|
|
8254
|
-
];
|
|
8255
|
-
var setupActions = (state) => {
|
|
8256
|
-
const actions = providerActions();
|
|
8257
|
-
if (state.canEnterKey) {
|
|
8258
|
-
actions.push("enter-key");
|
|
8259
|
-
}
|
|
8260
|
-
if (state.canReuseProviderLogin) {
|
|
8261
|
-
actions.push("reuse-provider-login");
|
|
8262
|
-
}
|
|
8263
|
-
if (state.credentials && canClearCredential(state.credentials)) {
|
|
8264
|
-
actions.push("clear-stored-key");
|
|
8265
|
-
}
|
|
8266
|
-
actions.push("done");
|
|
8267
|
-
return actions;
|
|
8268
|
-
};
|
|
8269
|
-
var setupLabels = (state) => setupActions(state).map((action) => {
|
|
8270
|
-
if (action === "clear-stored-key") {
|
|
8271
|
-
return "Clear stored key";
|
|
8272
|
-
}
|
|
8273
|
-
if (action === "done") {
|
|
8274
|
-
return "Done";
|
|
8275
|
-
}
|
|
8276
|
-
if (action === "enter-key") {
|
|
8277
|
-
return `Enter an API key for ${providerName(state.selectedTransport ?? "typesafe")}`;
|
|
8278
|
-
}
|
|
8279
|
-
if (action === "reuse-provider-login") {
|
|
8280
|
-
return "Reuse a Pi provider login";
|
|
8281
|
-
}
|
|
8282
|
-
if (action === "typesafe-compatible") {
|
|
8283
|
-
return `${providerName(action)}…`;
|
|
8284
|
-
}
|
|
8285
|
-
return providerName(action);
|
|
8286
|
-
});
|
|
8287
|
-
var inputLines = (key, state, input, width) => {
|
|
8288
|
-
const rendered = input.render(Math.max(10, width - 4))[0] ?? "";
|
|
8289
|
-
const titles = {
|
|
8290
|
-
"base-url": [
|
|
8291
|
-
"Base URL of a System One–compatible endpoint",
|
|
8292
|
-
"Origin, for example https://api.codiv.ai. /v1/systemone is appended; plain http works on localhost."
|
|
8293
|
-
],
|
|
8294
|
-
"key-entry": [
|
|
8295
|
-
providerName(state.selectedTransport ?? "typesafe"),
|
|
8296
|
-
state.selectedTransport === "openai-decisions" ? "No OpenAI Platform API key found. ChatGPT subscription OAuth is not accepted." : `No API key found for ${providerName(state.selectedTransport ?? "typesafe")}.`
|
|
8297
|
-
],
|
|
8298
|
-
"provider-id": [
|
|
8299
|
-
"Pi provider id",
|
|
8300
|
-
"The Pi provider whose stored login supplies this endpoint's key, for example openrouter."
|
|
8301
|
-
]
|
|
8302
|
-
};
|
|
8303
|
-
const [title, description] = titles[key];
|
|
8304
|
-
return [` ${title}`, ` ${description}`, ` ${rendered}`];
|
|
8305
|
-
};
|
|
8306
|
-
var renderJevProvider = (options, state, input, width) => {
|
|
8307
|
-
const { theme } = options;
|
|
8308
|
-
const lines = [theme.fg("accent", theme.bold(" Jev provider")), ""];
|
|
8309
|
-
if (state.credentials) {
|
|
8310
|
-
lines.push(` Credential: ${transportLabel(state.credentials)}`);
|
|
8311
|
-
} else if (state.selectedTransport) {
|
|
8312
|
-
lines.push(` Selected provider: ${providerName(state.selectedTransport)}`);
|
|
8313
|
-
} else {
|
|
8314
|
-
lines.push(` Saved transport: ${options.currentTransport}`);
|
|
8315
|
-
}
|
|
8316
|
-
if (state.baseUrl) {
|
|
8317
|
-
lines.push(` Base URL: ${state.baseUrl}`);
|
|
8318
|
-
}
|
|
8319
|
-
if (state.providerId) {
|
|
8320
|
-
lines.push(` Pi provider: ${state.providerId}`);
|
|
8321
|
-
}
|
|
8322
|
-
if (state.notice) {
|
|
8323
|
-
lines.push("", theme.fg("warning", ` ${state.notice}`));
|
|
8324
|
-
}
|
|
8325
|
-
lines.push("");
|
|
8326
|
-
if (state.mode === "resolving") {
|
|
8327
|
-
lines.push(" Checking the selected provider credentials…");
|
|
8328
|
-
} else if (state.mode === "verifying") {
|
|
8329
|
-
lines.push(" Verifying with a live Jev call…");
|
|
8330
|
-
} else if (state.mode === "clearing") {
|
|
8331
|
-
lines.push(" Clearing the selected provider's stored key…");
|
|
8332
|
-
} else if (state.mode === "base-url" || state.mode === "key-entry" || state.mode === "provider-id") {
|
|
8333
|
-
lines.push(...inputLines(state.mode, state, input, width));
|
|
8334
|
-
if (state.inputError) {
|
|
8335
|
-
lines.push(theme.fg("error", ` ${state.inputError}`));
|
|
8336
|
-
}
|
|
8337
|
-
lines.push(theme.fg("dim", " Enter: continue · Esc: return"));
|
|
8338
|
-
} else {
|
|
8339
|
-
for (const [index, label] of setupLabels(state).entries()) {
|
|
8340
|
-
const prefix = index === state.selectedIndex ? "→ " : " ";
|
|
8341
|
-
lines.push(`${prefix}${label}`);
|
|
8342
|
-
}
|
|
8343
|
-
}
|
|
8344
|
-
return lines.map((line) => truncateToWidth5(line, width));
|
|
8345
|
-
};
|
|
7319
|
+
// src/ui/jev-classifier-submenu.ts
|
|
7320
|
+
import { Input as Input3, Key as Key2, matchesKey as matchesKey2 } from "@earendil-works/pi-tui";
|
|
8346
7321
|
|
|
8347
7322
|
// src/ui/masked-input.ts
|
|
8348
|
-
import { Input as Input2, truncateToWidth as
|
|
7323
|
+
import { Input as Input2, truncateToWidth as truncateToWidth4 } from "@earendil-works/pi-tui";
|
|
8349
7324
|
|
|
8350
7325
|
class MaskedInput {
|
|
8351
7326
|
input;
|
|
@@ -8382,373 +7357,416 @@ class MaskedInput {
|
|
|
8382
7357
|
const { cursor } = this.input;
|
|
8383
7358
|
const masked = `${"•".repeat(Math.min(cursor, value.length))}█${"•".repeat(Math.max(0, value.length - cursor))}`;
|
|
8384
7359
|
const placeholder = value.length === 0 && this.options.placeholder ? this.options.placeholder : masked;
|
|
8385
|
-
return [
|
|
7360
|
+
return [truncateToWidth4(placeholder, Math.max(1, width))];
|
|
8386
7361
|
}
|
|
8387
7362
|
}
|
|
8388
7363
|
|
|
8389
|
-
// src/ui/jev-
|
|
8390
|
-
class
|
|
7364
|
+
// src/ui/jev-classifier-submenu.ts
|
|
7365
|
+
class JevClassifierSubmenu {
|
|
8391
7366
|
options;
|
|
8392
|
-
|
|
8393
|
-
|
|
8394
|
-
|
|
8395
|
-
|
|
8396
|
-
|
|
8397
|
-
mode = "menu";
|
|
8398
|
-
notice;
|
|
8399
|
-
inputError;
|
|
7367
|
+
search = new Input3;
|
|
7368
|
+
loaded = Promise.withResolvers();
|
|
7369
|
+
keyInput;
|
|
7370
|
+
choices = [];
|
|
7371
|
+
filtered = [];
|
|
8400
7372
|
selectedIndex = 0;
|
|
8401
|
-
|
|
8402
|
-
|
|
8403
|
-
|
|
8404
|
-
pendingSelectionRefresh = false;
|
|
7373
|
+
mode = "loading";
|
|
7374
|
+
notice;
|
|
7375
|
+
failClosedKeyMode = false;
|
|
8405
7376
|
_focused = true;
|
|
8406
|
-
constructor(options
|
|
7377
|
+
constructor(options) {
|
|
8407
7378
|
this.options = options;
|
|
8408
|
-
this.
|
|
8409
|
-
this.
|
|
8410
|
-
this.providerId = options.currentKeyProvider;
|
|
8411
|
-
this.maskedInput = this.createMaskedInput(keyEntryPrompt("typesafe"));
|
|
8412
|
-
this.textInput.focused = true;
|
|
8413
|
-
this.textInput.onSubmit = (value) => this.submitText(value);
|
|
8414
|
-
this.textInput.onEscape = () => this.returnToMenu();
|
|
8415
|
-
fireAndForget(this.refresh(), this.reportBackgroundError);
|
|
7379
|
+
this.search.focused = true;
|
|
7380
|
+
this.loadChoices();
|
|
8416
7381
|
}
|
|
8417
7382
|
get focused() {
|
|
8418
7383
|
return this._focused;
|
|
8419
7384
|
}
|
|
8420
7385
|
set focused(value) {
|
|
8421
7386
|
this._focused = value;
|
|
8422
|
-
this.
|
|
8423
|
-
this.textInput.focused = value;
|
|
7387
|
+
this.search.focused = value;
|
|
8424
7388
|
}
|
|
8425
7389
|
invalidate() {
|
|
8426
|
-
this.
|
|
8427
|
-
this.textInput.invalidate();
|
|
7390
|
+
this.search.invalidate();
|
|
8428
7391
|
}
|
|
8429
|
-
|
|
8430
|
-
|
|
8431
|
-
|
|
8432
|
-
|
|
8433
|
-
return
|
|
7392
|
+
render(width) {
|
|
7393
|
+
const { theme } = this.options;
|
|
7394
|
+
const border = theme.fg("border", "─".repeat(Math.max(1, width)));
|
|
7395
|
+
if (this.mode === "key") {
|
|
7396
|
+
return [
|
|
7397
|
+
border,
|
|
7398
|
+
theme.fg("accent", theme.bold("Jev API Key")),
|
|
7399
|
+
"",
|
|
7400
|
+
this.notice ?? "Enter a new TypeSafe Jev API key:",
|
|
7401
|
+
` ${this.keyInput?.render(Math.max(1, width - 4))[0] ?? ""}`,
|
|
7402
|
+
"",
|
|
7403
|
+
this.failClosedKeyMode ? "Key restore failed. Re-enter and verify a valid Jev key." : "Enter to verify · Esc to cancel",
|
|
7404
|
+
border
|
|
7405
|
+
];
|
|
8434
7406
|
}
|
|
8435
|
-
|
|
8436
|
-
|
|
8437
|
-
|
|
8438
|
-
|
|
8439
|
-
|
|
8440
|
-
|
|
7407
|
+
const query = this.search.getValue().trim().toLowerCase();
|
|
7408
|
+
const filteredChoices = this.choices.filter((choice) => choice.label.toLowerCase().includes(query));
|
|
7409
|
+
this.filtered = filteredChoices;
|
|
7410
|
+
const rows = this.filtered.map((choice, index) => `${index === this.selectedIndex ? "›" : " "} ${choice.label}`);
|
|
7411
|
+
return [
|
|
7412
|
+
border,
|
|
7413
|
+
theme.fg("accent", theme.bold("Jev classifier")),
|
|
7414
|
+
"",
|
|
7415
|
+
this.mode === "loading" ? "Loading Pi classifier catalog…" : `Search: ${this.search.render(Math.max(1, width - 10))[0]}`,
|
|
7416
|
+
...this.notice ? [this.notice] : [],
|
|
7417
|
+
...rows.slice(0, 10),
|
|
7418
|
+
"",
|
|
7419
|
+
"↑/↓ move · Enter select · Esc cancel",
|
|
7420
|
+
border
|
|
7421
|
+
];
|
|
7422
|
+
}
|
|
7423
|
+
handleInput(data) {
|
|
7424
|
+
if (this.mode === "key") {
|
|
7425
|
+
this.keyInput?.handleInput(data);
|
|
8441
7426
|
this.options.tui.requestRender();
|
|
8442
7427
|
return;
|
|
8443
7428
|
}
|
|
8444
|
-
if (this.mode !== "
|
|
7429
|
+
if (this.mode !== "list") {
|
|
8445
7430
|
return;
|
|
8446
7431
|
}
|
|
8447
|
-
|
|
8448
|
-
|
|
8449
|
-
|
|
8450
|
-
|
|
8451
|
-
|
|
8452
|
-
|
|
8453
|
-
|
|
8454
|
-
|
|
7432
|
+
if (data === "\x1B[B" || matchesKey2(data, Key2.down)) {
|
|
7433
|
+
if (this.selectedIndex + 1 < this.filtered.length) {
|
|
7434
|
+
this.selectedIndex += 1;
|
|
7435
|
+
}
|
|
7436
|
+
} else if (data === "\x1B[A" || matchesKey2(data, Key2.up)) {
|
|
7437
|
+
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
|
7438
|
+
} else if (matchesKey2(data, Key2.escape) || data === "\x1B") {
|
|
7439
|
+
this.options.done();
|
|
8455
7440
|
return;
|
|
7441
|
+
} else if (matchesKey2(data, Key2.enter) || data === "\r") {
|
|
7442
|
+
const selected = this.filtered[this.selectedIndex];
|
|
7443
|
+
if (selected) {
|
|
7444
|
+
this.choose(selected);
|
|
7445
|
+
}
|
|
7446
|
+
} else {
|
|
7447
|
+
this.search.handleInput(data);
|
|
7448
|
+
this.selectedIndex = 0;
|
|
8456
7449
|
}
|
|
8457
7450
|
this.options.tui.requestRender();
|
|
8458
7451
|
}
|
|
8459
|
-
|
|
8460
|
-
const input = this.mode === "key-entry" ? this.maskedInput : this.textInput;
|
|
8461
|
-
return renderJevProvider(this.options, this.viewState(), input, width);
|
|
8462
|
-
}
|
|
8463
|
-
viewState() {
|
|
8464
|
-
return {
|
|
8465
|
-
baseUrl: this.baseUrl,
|
|
8466
|
-
canEnterKey: this.canEnterKey,
|
|
8467
|
-
canReuseProviderLogin: Boolean(this.baseUrl),
|
|
8468
|
-
credentials: this.credentials,
|
|
8469
|
-
inputError: this.inputError,
|
|
8470
|
-
mode: this.mode,
|
|
8471
|
-
notice: this.notice,
|
|
8472
|
-
providerId: this.providerId,
|
|
8473
|
-
selectedIndex: this.selectedIndex,
|
|
8474
|
-
selectedTransport: this.selectedTransport
|
|
8475
|
-
};
|
|
8476
|
-
}
|
|
8477
|
-
createMaskedInput(placeholder) {
|
|
8478
|
-
return new MaskedInput({
|
|
8479
|
-
onEscape: () => this.returnToMenu(),
|
|
8480
|
-
onSubmit: (value) => this.submitEnteredKey(value),
|
|
8481
|
-
placeholder
|
|
8482
|
-
});
|
|
8483
|
-
}
|
|
8484
|
-
async refresh() {
|
|
8485
|
-
this.mode = "resolving";
|
|
8486
|
-
this.options.tui.requestRender();
|
|
7452
|
+
async loadChoices() {
|
|
8487
7453
|
try {
|
|
8488
|
-
|
|
8489
|
-
|
|
8490
|
-
this.selectedTransport = credentials?.transport;
|
|
8491
|
-
if (credentials?.transport === "typesafe-compatible") {
|
|
8492
|
-
this.baseUrl = credentials.baseUrl ?? this.baseUrl;
|
|
7454
|
+
if (!this.options.modelRegistry) {
|
|
7455
|
+
throw new Error("Pi classifier registry is unavailable.");
|
|
8493
7456
|
}
|
|
8494
|
-
|
|
8495
|
-
this.
|
|
8496
|
-
|
|
8497
|
-
this.
|
|
7457
|
+
const models = await this.options.modelRegistry.getAvailableOfType("classifier");
|
|
7458
|
+
this.choices = (models ?? []).map((model) => ({
|
|
7459
|
+
action: "classifier",
|
|
7460
|
+
label: `${this.options.modelRegistry?.getProviderDisplayName(model.provider) ?? model.provider} / ${model.id} (${JevClassifierSubmenu.rates(model)})`,
|
|
7461
|
+
model,
|
|
7462
|
+
ref: `${model.provider}/${model.id}`
|
|
7463
|
+
}));
|
|
7464
|
+
const stored = await (this.options.resolveStoredKey ?? readJevClassifierKey)();
|
|
7465
|
+
if (stored.key) {
|
|
7466
|
+
this.choices.push({
|
|
7467
|
+
action: "use-key",
|
|
7468
|
+
label: "Jev API Key (use stored key)",
|
|
7469
|
+
ref: JEV_API_KEY_CLASSIFIER
|
|
7470
|
+
}, {
|
|
7471
|
+
action: "replace-key",
|
|
7472
|
+
label: "Replace Jev API Key…",
|
|
7473
|
+
ref: JEV_API_KEY_CLASSIFIER
|
|
7474
|
+
});
|
|
7475
|
+
} else {
|
|
7476
|
+
this.choices.push({
|
|
7477
|
+
action: "replace-key",
|
|
7478
|
+
label: "Jev API Key… (enter TypeSafe key)",
|
|
7479
|
+
ref: JEV_API_KEY_CLASSIFIER
|
|
7480
|
+
});
|
|
8498
7481
|
}
|
|
8499
|
-
this.
|
|
7482
|
+
this.mode = "list";
|
|
7483
|
+
this.loaded.resolve(true);
|
|
7484
|
+
this.notice = this.options.currentValue && this.options.currentValue !== "Choose classifier" ? `Current: ${this.options.currentValue}` : undefined;
|
|
8500
7485
|
} catch (error) {
|
|
8501
|
-
this.
|
|
7486
|
+
this.mode = "list";
|
|
7487
|
+
this.loaded.resolve(false);
|
|
7488
|
+
this.notice = `Could not load classifiers: ${error instanceof Error ? error.message : "unknown error"}. Retry /advisor-settings.`;
|
|
8502
7489
|
}
|
|
8503
|
-
}
|
|
8504
|
-
reportBackgroundError = (message) => {
|
|
8505
|
-
this.mode = "menu";
|
|
8506
|
-
this.notice = `Setup failed: ${message}`;
|
|
8507
|
-
this.options.tui.requestRender();
|
|
8508
|
-
};
|
|
8509
|
-
returnToMenu() {
|
|
8510
|
-
this.mode = "menu";
|
|
8511
|
-
this.inputError = undefined;
|
|
8512
|
-
this.maskedInput.setValue("");
|
|
8513
7490
|
this.options.tui.requestRender();
|
|
8514
7491
|
}
|
|
8515
|
-
|
|
8516
|
-
|
|
8517
|
-
|
|
8518
|
-
|
|
8519
|
-
|
|
8520
|
-
|
|
8521
|
-
|
|
8522
|
-
|
|
8523
|
-
|
|
8524
|
-
|
|
8525
|
-
if (
|
|
8526
|
-
this.
|
|
7492
|
+
whenLoaded() {
|
|
7493
|
+
return this.loaded.promise;
|
|
7494
|
+
}
|
|
7495
|
+
static rates(model) {
|
|
7496
|
+
const rates = model.cost;
|
|
7497
|
+
return rates.input === 0 && rates.output === 0 ? "$0/M tokens" : `$${rates.input}/$${rates.output} per Mtok`;
|
|
7498
|
+
}
|
|
7499
|
+
async choose(choice) {
|
|
7500
|
+
if (choice.action === "use-key") {
|
|
7501
|
+
const stored = await (this.options.resolveStoredKey ?? readJevClassifierKey)();
|
|
7502
|
+
if (stored.key) {
|
|
7503
|
+
await this.verifyStoredKey(stored.key);
|
|
7504
|
+
} else {
|
|
7505
|
+
this.openKeyEntry();
|
|
8527
7506
|
}
|
|
8528
7507
|
return;
|
|
8529
7508
|
}
|
|
8530
|
-
if (action === "
|
|
8531
|
-
this.
|
|
8532
|
-
return;
|
|
8533
|
-
}
|
|
8534
|
-
if (action === "typesafe-compatible") {
|
|
8535
|
-
this.openBaseUrlEntry();
|
|
7509
|
+
if (choice.action === "replace-key") {
|
|
7510
|
+
this.openKeyEntry();
|
|
8536
7511
|
return;
|
|
8537
7512
|
}
|
|
8538
|
-
if (
|
|
7513
|
+
if (!choice.model) {
|
|
8539
7514
|
return;
|
|
8540
7515
|
}
|
|
8541
|
-
|
|
8542
|
-
|
|
8543
|
-
openBaseUrlEntry() {
|
|
8544
|
-
this.mode = "base-url";
|
|
8545
|
-
this.inputError = undefined;
|
|
8546
|
-
this.notice = undefined;
|
|
8547
|
-
this.textInput.setValue(this.baseUrl ?? "");
|
|
8548
|
-
this.textInput.focused = this._focused;
|
|
8549
|
-
this.options.tui.requestRender();
|
|
8550
|
-
}
|
|
8551
|
-
openProviderIdEntry() {
|
|
8552
|
-
this.mode = "provider-id";
|
|
8553
|
-
this.inputError = undefined;
|
|
8554
|
-
this.notice = undefined;
|
|
8555
|
-
this.textInput.setValue(this.providerId ?? "");
|
|
8556
|
-
this.textInput.focused = this._focused;
|
|
7516
|
+
this.mode = "verifying";
|
|
7517
|
+
this.notice = `Verifying ${choice.ref}…`;
|
|
8557
7518
|
this.options.tui.requestRender();
|
|
8558
|
-
|
|
8559
|
-
|
|
8560
|
-
|
|
8561
|
-
|
|
8562
|
-
|
|
8563
|
-
|
|
8564
|
-
|
|
7519
|
+
try {
|
|
7520
|
+
await this.verify(choice.model);
|
|
7521
|
+
const saved = this.save({ classifier: choice.ref, transport: "auto" });
|
|
7522
|
+
if (!saved) {
|
|
7523
|
+
this.mode = "list";
|
|
7524
|
+
this.notice = "Verified, but settings could not be saved.";
|
|
7525
|
+
}
|
|
7526
|
+
} catch (error) {
|
|
7527
|
+
this.notice = `Verification failed for ${choice.ref}: ${error instanceof Error ? error.message : "provider error"}`;
|
|
7528
|
+
this.mode = "list";
|
|
7529
|
+
}
|
|
8565
7530
|
this.options.tui.requestRender();
|
|
8566
7531
|
}
|
|
8567
|
-
|
|
8568
|
-
if (this.
|
|
8569
|
-
|
|
8570
|
-
return;
|
|
7532
|
+
async verify(model, apiKey) {
|
|
7533
|
+
if (!this.options.modelRegistry) {
|
|
7534
|
+
throw new Error("Pi classifier registry is unavailable.");
|
|
8571
7535
|
}
|
|
8572
|
-
|
|
8573
|
-
|
|
7536
|
+
const state = {
|
|
7537
|
+
recent_conversation: "Classifier verification check.",
|
|
7538
|
+
role: "executor"
|
|
7539
|
+
};
|
|
7540
|
+
const result = await classifyWithJev({ modelRegistry: this.options.modelRegistry, signal: undefined }, `${model.provider}/${model.id}`, state, nativeScreeningQuestions, { apiKey });
|
|
7541
|
+
if (!result.answers.self_answerable || !result.answers.stakes) {
|
|
7542
|
+
throw new Error("Classifier returned an incomplete verification result.");
|
|
8574
7543
|
}
|
|
8575
7544
|
}
|
|
8576
|
-
async
|
|
8577
|
-
const
|
|
8578
|
-
|
|
8579
|
-
|
|
7545
|
+
async verifyKey() {
|
|
7546
|
+
const candidate = this.keyInput?.getValue().trim() ?? "";
|
|
7547
|
+
this.keyInput?.setValue("");
|
|
7548
|
+
if (!candidate) {
|
|
7549
|
+
this.notice = "Enter a non-empty Jev API key.";
|
|
8580
7550
|
this.options.tui.requestRender();
|
|
8581
7551
|
return;
|
|
8582
7552
|
}
|
|
8583
|
-
this.
|
|
8584
|
-
|
|
8585
|
-
|
|
8586
|
-
this.inputError = undefined;
|
|
8587
|
-
this.options.tui.requestRender();
|
|
8588
|
-
const options = this.providerId ? { baseUrl, keyProvider: this.providerId } : { baseUrl };
|
|
8589
|
-
const credentials = await this.deps.resolveEndpoint(options);
|
|
8590
|
-
if (!credentials) {
|
|
8591
|
-
this.openKeyEntry("typesafe-compatible");
|
|
8592
|
-
return;
|
|
8593
|
-
}
|
|
8594
|
-
await this.verifyAndSave(credentials);
|
|
8595
|
-
}
|
|
8596
|
-
async submitProviderId(value) {
|
|
8597
|
-
const providerId = value.trim();
|
|
8598
|
-
if (!providerId || /\s/u.test(providerId)) {
|
|
8599
|
-
this.inputError = "Enter a single Pi provider id, for example openrouter.";
|
|
7553
|
+
const model = this.options.modelRegistry?.findOfType("classifier", "typesafe", "jev-latest");
|
|
7554
|
+
if (!model) {
|
|
7555
|
+
this.notice = "TypeSafe Jev classifier is not present in Pi's catalog.";
|
|
8600
7556
|
this.options.tui.requestRender();
|
|
8601
7557
|
return;
|
|
8602
7558
|
}
|
|
8603
|
-
|
|
8604
|
-
|
|
7559
|
+
const previous = await (this.options.resolveStoredKey ?? readJevClassifierKey)();
|
|
7560
|
+
if (previous.failed) {
|
|
7561
|
+
this.mode = "list";
|
|
7562
|
+
this.notice = "Could not read the stored Jev API Key state; nothing was changed. Try again later.";
|
|
8605
7563
|
this.options.tui.requestRender();
|
|
8606
7564
|
return;
|
|
8607
7565
|
}
|
|
8608
|
-
this.
|
|
8609
|
-
this.
|
|
8610
|
-
this.
|
|
8611
|
-
|
|
7566
|
+
this.mode = "verifying";
|
|
7567
|
+
this.notice = "Verifying Jev API Key…";
|
|
7568
|
+
this.options.tui.requestRender();
|
|
7569
|
+
try {
|
|
7570
|
+
await this.verify(model, candidate);
|
|
7571
|
+
const stored = await (this.options.persistKey ?? writeJevClassifierKey)(candidate);
|
|
7572
|
+
if (!stored.ok) {
|
|
7573
|
+
throw new Error(stored.message);
|
|
7574
|
+
}
|
|
7575
|
+
enableJevClassifierKeyForRuntime();
|
|
7576
|
+
if (!this.save({ classifier: JEV_API_KEY_CLASSIFIER, transport: "auto" })) {
|
|
7577
|
+
if (!await this.restorePreviousKey(previous)) {
|
|
7578
|
+
this.failClosedKeyMode = true;
|
|
7579
|
+
disableJevClassifierKeyForRuntime();
|
|
7580
|
+
throw new Error("Settings save failed and the previous key could not be restored. Re-enter and verify a valid Jev key before continuing.");
|
|
7581
|
+
}
|
|
7582
|
+
throw new Error("Could not save Jev classifier settings; existing key was restored.");
|
|
7583
|
+
}
|
|
7584
|
+
this.mode = "list";
|
|
7585
|
+
this.notice = "Jev API Key verified and saved securely.";
|
|
7586
|
+
} catch (error) {
|
|
7587
|
+
this.mode = "key";
|
|
7588
|
+
this.notice = `Verification failed: ${error instanceof Error ? error.message : "provider error"}`;
|
|
7589
|
+
}
|
|
8612
7590
|
this.options.tui.requestRender();
|
|
8613
|
-
|
|
8614
|
-
|
|
8615
|
-
|
|
7591
|
+
}
|
|
7592
|
+
openKeyEntry() {
|
|
7593
|
+
this.mode = "key";
|
|
7594
|
+
this.keyInput = new MaskedInput({
|
|
7595
|
+
onEscape: () => {
|
|
7596
|
+
this.mode = "list";
|
|
7597
|
+
this.keyInput?.setValue("");
|
|
7598
|
+
this.options.tui.requestRender();
|
|
7599
|
+
},
|
|
7600
|
+
onSubmit: () => this.handleKeySubmit(),
|
|
7601
|
+
placeholder: "TypeSafe Jev API key"
|
|
8616
7602
|
});
|
|
8617
|
-
|
|
8618
|
-
|
|
8619
|
-
|
|
7603
|
+
}
|
|
7604
|
+
restorePreviousKey(previous) {
|
|
7605
|
+
if (this.options.restoreKey) {
|
|
7606
|
+
return this.options.restoreKey(previous);
|
|
7607
|
+
}
|
|
7608
|
+
return restoreJevClassifierKey(previous);
|
|
7609
|
+
}
|
|
7610
|
+
async verifyStoredKey(key) {
|
|
7611
|
+
const model = this.options.modelRegistry?.findOfType("classifier", "typesafe", "jev-latest");
|
|
7612
|
+
if (!model) {
|
|
7613
|
+
this.notice = "TypeSafe Jev classifier is not present in Pi's catalog.";
|
|
8620
7614
|
this.options.tui.requestRender();
|
|
8621
7615
|
return;
|
|
8622
7616
|
}
|
|
8623
|
-
|
|
8624
|
-
|
|
8625
|
-
async selectProvider(transport) {
|
|
8626
|
-
this.selectedTransport = transport;
|
|
8627
|
-
this.mode = "resolving";
|
|
8628
|
-
this.notice = undefined;
|
|
8629
|
-
this.canEnterKey = false;
|
|
7617
|
+
this.mode = "verifying";
|
|
7618
|
+
this.notice = "Verifying stored Jev API Key…";
|
|
8630
7619
|
this.options.tui.requestRender();
|
|
8631
|
-
let credentials;
|
|
8632
7620
|
try {
|
|
8633
|
-
|
|
7621
|
+
await this.verify(model, key);
|
|
7622
|
+
if (!this.save({ classifier: JEV_API_KEY_CLASSIFIER, transport: "auto" })) {
|
|
7623
|
+
throw new Error("Could not save the Jev classifier selection.");
|
|
7624
|
+
}
|
|
7625
|
+
this.notice = "Stored Jev API Key verified.";
|
|
8634
7626
|
} catch (error) {
|
|
8635
|
-
this.mode = "
|
|
8636
|
-
this.notice = `
|
|
8637
|
-
this.options.tui.requestRender();
|
|
8638
|
-
return;
|
|
7627
|
+
this.mode = "list";
|
|
7628
|
+
this.notice = `Verification failed: ${error instanceof Error ? error.message : "provider error"}`;
|
|
8639
7629
|
}
|
|
8640
|
-
this.
|
|
8641
|
-
|
|
8642
|
-
|
|
8643
|
-
|
|
8644
|
-
|
|
8645
|
-
|
|
8646
|
-
|
|
8647
|
-
|
|
8648
|
-
}
|
|
7630
|
+
this.options.tui.requestRender();
|
|
7631
|
+
}
|
|
7632
|
+
async handleKeySubmit() {
|
|
7633
|
+
try {
|
|
7634
|
+
await this.verifyKey();
|
|
7635
|
+
} catch (error) {
|
|
7636
|
+
this.notice = `Verification failed: ${error instanceof Error ? error.message : "provider error"}`;
|
|
7637
|
+
this.mode = "key";
|
|
8649
7638
|
this.options.tui.requestRender();
|
|
8650
|
-
return;
|
|
8651
7639
|
}
|
|
8652
|
-
|
|
8653
|
-
|
|
8654
|
-
|
|
8655
|
-
|
|
7640
|
+
}
|
|
7641
|
+
save(selection) {
|
|
7642
|
+
const saved = this.options.onSelection?.(selection) ?? false;
|
|
7643
|
+
if (saved) {
|
|
7644
|
+
this.options.done(selection.classifier);
|
|
7645
|
+
} else {
|
|
7646
|
+
this.mode = "list";
|
|
7647
|
+
this.notice = "Verified, but settings could not be saved.";
|
|
8656
7648
|
}
|
|
8657
|
-
|
|
7649
|
+
return saved;
|
|
8658
7650
|
}
|
|
8659
|
-
|
|
8660
|
-
|
|
8661
|
-
|
|
8662
|
-
|
|
8663
|
-
|
|
8664
|
-
|
|
7651
|
+
}
|
|
7652
|
+
|
|
7653
|
+
// src/ui/jev-filter-submenu.ts
|
|
7654
|
+
import { Key as Key3, matchesKey as matchesKey3, truncateToWidth as truncateToWidth5 } from "@earendil-works/pi-tui";
|
|
7655
|
+
var actionsFor = (enabled) => enabled ? ["disable", "done"] : ["enable", "done"];
|
|
7656
|
+
var labelFor2 = (action) => {
|
|
7657
|
+
if (action === "enable") {
|
|
7658
|
+
return "Enable the filter";
|
|
7659
|
+
}
|
|
7660
|
+
return action === "disable" ? "Disable the filter" : "Done";
|
|
7661
|
+
};
|
|
7662
|
+
|
|
7663
|
+
class JevFilterSubmenu {
|
|
7664
|
+
options;
|
|
7665
|
+
mode = "resolving";
|
|
7666
|
+
notice;
|
|
7667
|
+
selectedIndex = 0;
|
|
7668
|
+
enabled;
|
|
7669
|
+
classifierAvailable = false;
|
|
7670
|
+
modelRegistry;
|
|
7671
|
+
resolveClassifierKey;
|
|
7672
|
+
_focused = true;
|
|
7673
|
+
constructor(options) {
|
|
7674
|
+
this.options = options;
|
|
7675
|
+
this.modelRegistry = options.modelRegistry;
|
|
7676
|
+
this.resolveClassifierKey = options.resolveClassifierKey;
|
|
7677
|
+
this.enabled = options.currentValue.startsWith("On");
|
|
7678
|
+
this.refresh();
|
|
7679
|
+
}
|
|
7680
|
+
get focused() {
|
|
7681
|
+
return this._focused;
|
|
7682
|
+
}
|
|
7683
|
+
set focused(value) {
|
|
7684
|
+
this._focused = value;
|
|
7685
|
+
}
|
|
7686
|
+
invalidate() {}
|
|
7687
|
+
render(width) {
|
|
7688
|
+
const { theme } = this.options;
|
|
7689
|
+
const lines = [
|
|
7690
|
+
theme.fg("accent", theme.bold(" Jev classifier consultation filter")),
|
|
7691
|
+
"",
|
|
7692
|
+
` Filter: ${this.enabled ? "On" : "Off"}`,
|
|
7693
|
+
` Classifier: ${this.options.currentClassifier ?? "not selected"}`
|
|
7694
|
+
];
|
|
7695
|
+
if (this.mode === "resolving") {
|
|
7696
|
+
lines.push("", " Checking selected classifier availability…");
|
|
7697
|
+
} else if (this.notice) {
|
|
7698
|
+
lines.push("", theme.fg("warning", ` ${this.notice}`));
|
|
8665
7699
|
}
|
|
8666
|
-
|
|
8667
|
-
|
|
8668
|
-
|
|
7700
|
+
if (this.mode === "menu") {
|
|
7701
|
+
lines.push("");
|
|
7702
|
+
for (const [index, action] of actionsFor(this.enabled).entries()) {
|
|
7703
|
+
lines.push(`${index === this.selectedIndex ? "→ " : " "}${labelFor2(action)}`);
|
|
7704
|
+
}
|
|
8669
7705
|
}
|
|
8670
|
-
|
|
7706
|
+
return lines.map((line) => truncateToWidth5(line, width));
|
|
8671
7707
|
}
|
|
8672
|
-
|
|
8673
|
-
this.mode
|
|
8674
|
-
this.notice = undefined;
|
|
8675
|
-
this.options.tui.requestRender();
|
|
8676
|
-
const outcome = await this.deps.verify(credentials);
|
|
8677
|
-
if (!outcome.ok) {
|
|
8678
|
-
this.notice = `Verification failed: ${outcome.message ?? "unknown error"}`;
|
|
8679
|
-
this.mode = enteredKey ? "key-entry" : "menu";
|
|
8680
|
-
this.canEnterKey = !enteredKey && canReplaceWithEnteredKey(credentials);
|
|
8681
|
-
this.options.tui.requestRender();
|
|
7708
|
+
handleInput(keyData) {
|
|
7709
|
+
if (this.mode !== "menu") {
|
|
8682
7710
|
return;
|
|
8683
7711
|
}
|
|
8684
|
-
|
|
8685
|
-
|
|
8686
|
-
|
|
8687
|
-
|
|
8688
|
-
|
|
8689
|
-
|
|
8690
|
-
|
|
8691
|
-
|
|
8692
|
-
}
|
|
8693
|
-
this.notice = `${stored.message} Verification succeeded.`;
|
|
8694
|
-
if (credentials.transport === "typesafe-compatible") {
|
|
8695
|
-
this.providerId = undefined;
|
|
8696
|
-
}
|
|
8697
|
-
}
|
|
8698
|
-
this.credentials = credentials;
|
|
8699
|
-
if (this.commitSelection(credentials)) {
|
|
8700
|
-
this.closeSetup(true);
|
|
7712
|
+
const actions = actionsFor(this.enabled);
|
|
7713
|
+
if (matchesKey3(keyData, Key3.down) || keyData === "\x1B[B" || keyData === "\x1BOB") {
|
|
7714
|
+
this.selectedIndex = (this.selectedIndex + 1) % actions.length;
|
|
7715
|
+
} else if (matchesKey3(keyData, Key3.up) || keyData === "\x1B[A" || keyData === "\x1BOA") {
|
|
7716
|
+
this.selectedIndex = (this.selectedIndex - 1 + actions.length) % actions.length;
|
|
7717
|
+
} else if (matchesKey3(keyData, Key3.enter) || keyData === "\r") {
|
|
7718
|
+
this.activate(actions[this.selectedIndex]);
|
|
7719
|
+
} else {
|
|
8701
7720
|
return;
|
|
8702
7721
|
}
|
|
8703
|
-
this.mode = "menu";
|
|
8704
|
-
this.canEnterKey = false;
|
|
8705
|
-
this.notice = "Provider verified, but settings could not be saved.";
|
|
8706
7722
|
this.options.tui.requestRender();
|
|
8707
7723
|
}
|
|
8708
|
-
|
|
8709
|
-
|
|
8710
|
-
|
|
7724
|
+
async refresh() {
|
|
7725
|
+
const selected = this.options.currentClassifier;
|
|
7726
|
+
try {
|
|
7727
|
+
if (selected === JEV_API_KEY_CLASSIFIER) {
|
|
7728
|
+
const stored = await (this.resolveClassifierKey ?? readJevClassifierKey)();
|
|
7729
|
+
this.classifierAvailable = Boolean(stored.key);
|
|
7730
|
+
if (stored.failed) {
|
|
7731
|
+
this.notice = "Could not read the stored Jev API Key state; try again later.";
|
|
7732
|
+
} else if (!this.classifierAvailable) {
|
|
7733
|
+
this.notice = "No Jev API Key is stored. Choose Jev API Key in Jev classifier settings.";
|
|
7734
|
+
}
|
|
7735
|
+
} else {
|
|
7736
|
+
const parsed = selected ? parseJevClassifierRef(selected) : undefined;
|
|
7737
|
+
const registry = this.modelRegistry;
|
|
7738
|
+
if (parsed && registry) {
|
|
7739
|
+
const available = await registry.getAvailableOfType("classifier", parsed.provider);
|
|
7740
|
+
this.classifierAvailable = available.some((model) => model.id === parsed.modelId);
|
|
7741
|
+
}
|
|
7742
|
+
if (!this.classifierAvailable) {
|
|
7743
|
+
this.notice = "Select an available authenticated classifier in Jev classifier settings first.";
|
|
7744
|
+
}
|
|
7745
|
+
}
|
|
7746
|
+
} catch (error) {
|
|
7747
|
+
this.notice = `Classifier lookup failed: ${error instanceof Error ? error.message : "Pi error"}`;
|
|
8711
7748
|
}
|
|
8712
|
-
|
|
7749
|
+
this.mode = "menu";
|
|
7750
|
+
this.options.tui.requestRender();
|
|
8713
7751
|
}
|
|
8714
|
-
|
|
8715
|
-
if (
|
|
8716
|
-
|
|
8717
|
-
|
|
8718
|
-
const selection = {
|
|
8719
|
-
transport: credentials.transport
|
|
8720
|
-
};
|
|
8721
|
-
if (this.baseUrl) {
|
|
8722
|
-
selection.baseUrl = this.baseUrl;
|
|
7752
|
+
activate(action) {
|
|
7753
|
+
if (action === "done") {
|
|
7754
|
+
this.options.done();
|
|
7755
|
+
return;
|
|
8723
7756
|
}
|
|
8724
|
-
|
|
8725
|
-
|
|
7757
|
+
const enabled = action === "enable";
|
|
7758
|
+
if (enabled && !this.classifierAvailable) {
|
|
7759
|
+
this.options.tui.requestRender();
|
|
7760
|
+
return;
|
|
8726
7761
|
}
|
|
8727
|
-
|
|
8728
|
-
|
|
8729
|
-
|
|
8730
|
-
const { credentials } = this;
|
|
8731
|
-
if (!(credentials && credentials.source)) {
|
|
7762
|
+
if (this.options.onSelection?.({ enabled }) === false) {
|
|
7763
|
+
this.notice = "The filter state could not be saved.";
|
|
7764
|
+
this.options.tui.requestRender();
|
|
8732
7765
|
return;
|
|
8733
7766
|
}
|
|
8734
|
-
this.
|
|
7767
|
+
this.enabled = enabled;
|
|
8735
7768
|
this.notice = undefined;
|
|
8736
|
-
this.options.
|
|
8737
|
-
const result = await this.deps.clearStoredKey(credentials.transport);
|
|
8738
|
-
this.mode = "menu";
|
|
8739
|
-
this.notice = result.message;
|
|
8740
|
-
if (result.ok) {
|
|
8741
|
-
this.credentials = undefined;
|
|
8742
|
-
this.pendingSelectionRefresh = true;
|
|
8743
|
-
}
|
|
8744
|
-
this.options.tui.requestRender();
|
|
8745
|
-
}
|
|
8746
|
-
closeSetup(refreshSettings = false) {
|
|
8747
|
-
this.options.done();
|
|
8748
|
-
if (refreshSettings || this.pendingSelectionRefresh) {
|
|
8749
|
-
this.options.afterSelection?.();
|
|
8750
|
-
}
|
|
8751
|
-
this.pendingSelectionRefresh = false;
|
|
7769
|
+
this.options.done(enabled ? "On" : "Off");
|
|
8752
7770
|
}
|
|
8753
7771
|
}
|
|
8754
7772
|
|
|
@@ -8760,7 +7778,7 @@ class SearchableModelMultiSelector extends ModelSelectorAdapter {
|
|
|
8760
7778
|
}
|
|
8761
7779
|
|
|
8762
7780
|
// src/ui/text-setting-submenu.ts
|
|
8763
|
-
import { Input as Input4, truncateToWidth as
|
|
7781
|
+
import { Input as Input4, truncateToWidth as truncateToWidth6 } from "@earendil-works/pi-tui";
|
|
8764
7782
|
|
|
8765
7783
|
class TextSettingSubmenu {
|
|
8766
7784
|
input = new Input4;
|
|
@@ -8807,7 +7825,7 @@ class TextSettingSubmenu {
|
|
|
8807
7825
|
lines.push(theme.fg("error", ` ${this.error}`));
|
|
8808
7826
|
}
|
|
8809
7827
|
lines.push("", theme.fg("dim", " Enter: apply · Esc: cancel"));
|
|
8810
|
-
return lines.map((line) =>
|
|
7828
|
+
return lines.map((line) => truncateToWidth6(line, width));
|
|
8811
7829
|
}
|
|
8812
7830
|
handleInput(keyData) {
|
|
8813
7831
|
const before = this.input.getValue();
|
|
@@ -8836,47 +7854,43 @@ var scoutTimeoutItem = (settings) => {
|
|
|
8836
7854
|
values: numericValues(timeoutMs, [5000, 1e4, 15000, 30000, 45000, 60000, 90000, 120000])
|
|
8837
7855
|
};
|
|
8838
7856
|
};
|
|
8839
|
-
var
|
|
8840
|
-
|
|
8841
|
-
|
|
8842
|
-
return "Auto (TypeSafe → OpenRouter)";
|
|
7857
|
+
var jevFilterRowValue = (settings) => {
|
|
7858
|
+
if (!settings.jevFilterEnabled) {
|
|
7859
|
+
return "Off";
|
|
8843
7860
|
}
|
|
8844
|
-
|
|
8845
|
-
const baseUrl = settings.jevBaseUrl ?? "no Base URL";
|
|
8846
|
-
return settings.jevKeyProvider ? `System One–compatible (${baseUrl}; Pi login "${settings.jevKeyProvider}")` : `System One–compatible (${baseUrl})`;
|
|
8847
|
-
}
|
|
8848
|
-
return providerName(transport);
|
|
7861
|
+
return settings.jevClassifier ? "On" : "On (unavailable: no classifier)";
|
|
8849
7862
|
};
|
|
8850
|
-
var jevItems = (settings, theme, tui,
|
|
7863
|
+
var jevItems = (settings, theme, tui, jevClassifierRegistry, jevKeyDeps, onJevFilter, onJevProvider) => [
|
|
8851
7864
|
{
|
|
8852
|
-
currentValue:
|
|
8853
|
-
description: "
|
|
7865
|
+
currentValue: settings.jevClassifier ?? "Choose classifier",
|
|
7866
|
+
description: "Select a Pi-authenticated classifier or explicitly configure the separate Jev API Key option.",
|
|
8854
7867
|
id: "jevProvider",
|
|
8855
|
-
label: "Jev
|
|
8856
|
-
submenu: (currentValue, done) => new
|
|
8857
|
-
afterSelection: afterJevSetup,
|
|
8858
|
-
currentBaseUrl: settings.jevBaseUrl,
|
|
8859
|
-
currentKeyProvider: settings.jevKeyProvider,
|
|
8860
|
-
currentTransport: settings.jevTransport ?? DEFAULT_JEV_TRANSPORT,
|
|
7868
|
+
label: "Jev classifier",
|
|
7869
|
+
submenu: (currentValue, done) => new JevClassifierSubmenu({
|
|
8861
7870
|
currentValue,
|
|
8862
7871
|
done,
|
|
7872
|
+
keybindings: getKeybindings(),
|
|
7873
|
+
modelRegistry: jevClassifierRegistry,
|
|
8863
7874
|
onSelection: onJevProvider,
|
|
8864
|
-
|
|
7875
|
+
persistKey: jevKeyDeps?.persistClassifierKey,
|
|
7876
|
+
resolveStoredKey: jevKeyDeps?.resolveClassifierKey,
|
|
7877
|
+
restoreKey: jevKeyDeps?.restoreClassifierKey,
|
|
8865
7878
|
theme,
|
|
8866
7879
|
tui
|
|
8867
7880
|
})
|
|
8868
7881
|
},
|
|
8869
7882
|
{
|
|
8870
|
-
currentValue:
|
|
7883
|
+
currentValue: jevFilterRowValue(settings),
|
|
8871
7884
|
description: "Screen low-stakes ask_advisor consultations with the selected Jev provider.",
|
|
8872
7885
|
id: "jevFilter",
|
|
8873
7886
|
label: "Jev/Decisions consultation filter",
|
|
8874
7887
|
submenu: (currentValue, done) => new JevFilterSubmenu({
|
|
8875
|
-
|
|
7888
|
+
currentClassifier: settings.jevClassifier,
|
|
8876
7889
|
currentValue,
|
|
8877
7890
|
done,
|
|
7891
|
+
modelRegistry: jevClassifierRegistry,
|
|
8878
7892
|
onSelection: onJevFilter,
|
|
8879
|
-
|
|
7893
|
+
resolveClassifierKey: jevKeyDeps?.resolveClassifierKey,
|
|
8880
7894
|
theme,
|
|
8881
7895
|
tui
|
|
8882
7896
|
})
|
|
@@ -8902,21 +7916,6 @@ var jevItems = (settings, theme, tui, jevSetupDeps, onJevFilter, onJevProvider,
|
|
|
8902
7916
|
label: "Jev override window",
|
|
8903
7917
|
values: numericValues(settings.jevFilterOverrideWindow ?? DEFAULT_JEV_FILTER_OVERRIDE_WINDOW, [0, 3, 5, 10, 20, 50]).map((value) => `${value} turns`)
|
|
8904
7918
|
},
|
|
8905
|
-
{
|
|
8906
|
-
currentValue: settings.jevModel ?? DEFAULT_JEV_MODEL,
|
|
8907
|
-
description: "TypeSafe Jev model used for screening and turn-gate checks.",
|
|
8908
|
-
id: "jevModel",
|
|
8909
|
-
label: "Jev model",
|
|
8910
|
-
submenu: (_currentValue, done) => new TextSettingSubmenu({
|
|
8911
|
-
description: "Enter a TypeSafe model name (for example jev-latest or jev-1.13.0).",
|
|
8912
|
-
initial: settings.jevModel ?? DEFAULT_JEV_MODEL,
|
|
8913
|
-
onCancel: done,
|
|
8914
|
-
onSubmit: (value) => ({ value: value.trim() || DEFAULT_JEV_MODEL }),
|
|
8915
|
-
theme,
|
|
8916
|
-
title: "Jev model",
|
|
8917
|
-
tui
|
|
8918
|
-
})
|
|
8919
|
-
},
|
|
8920
7919
|
{
|
|
8921
7920
|
currentValue: String(settings.jevTimeoutMs ?? DEFAULT_JEV_TIMEOUT_MS),
|
|
8922
7921
|
description: "Total wall-time budget for one Jev call, including retries.",
|
|
@@ -8931,13 +7930,6 @@ var jevItems = (settings, theme, tui, jevSetupDeps, onJevFilter, onJevProvider,
|
|
|
8931
7930
|
label: "Jev digest chars",
|
|
8932
7931
|
values: numericValues(settings.jevDigestMaxChars ?? DEFAULT_JEV_DIGEST_MAX_CHARS, [0, 1000, 2000, 4000, 8000, 15000])
|
|
8933
7932
|
},
|
|
8934
|
-
{
|
|
8935
|
-
currentValue: String(settings.jevPricePerMtok ?? DEFAULT_JEV_PRICE_PER_MTOK),
|
|
8936
|
-
description: "TypeSafe/OpenRouter estimated input price per million tokens; ignored for OpenAI Decisions, whose cost is unavailable.",
|
|
8937
|
-
id: "jevPricePerMtok",
|
|
8938
|
-
label: "Jev price/Mtok",
|
|
8939
|
-
values: numericValues(settings.jevPricePerMtok ?? DEFAULT_JEV_PRICE_PER_MTOK, [0.01, 0.02, 0.042, 0.05, 0.1])
|
|
8940
|
-
},
|
|
8941
7933
|
{
|
|
8942
7934
|
currentValue: settings.jevTurnGateEveryTurns === 0 || settings.jevTurnGateEveryTurns === undefined ? "Off" : `every ${settings.jevTurnGateEveryTurns} turns`,
|
|
8943
7935
|
description: "Proactively consult the Advisor every N turns without a consultation (0 = off).",
|
|
@@ -9003,8 +7995,8 @@ var createSettingsItems = ({
|
|
|
9003
7995
|
modelWhitelist,
|
|
9004
7996
|
presets,
|
|
9005
7997
|
settings,
|
|
9006
|
-
|
|
9007
|
-
|
|
7998
|
+
jevClassifierRegistry,
|
|
7999
|
+
jevKeyDeps,
|
|
9008
8000
|
onJevFilter,
|
|
9009
8001
|
onJevProvider,
|
|
9010
8002
|
theme,
|
|
@@ -9148,7 +8140,7 @@ var createSettingsItems = ({
|
|
|
9148
8140
|
title: "Tool disclosure policies",
|
|
9149
8141
|
tui
|
|
9150
8142
|
})
|
|
9151
|
-
}, toggle("trackedFileContent", "Tracked file content", "Allow tracked file contents to be sent with Advisor context.", settings.trackedFileContent, false), toggle("untrackedContent", "Untracked file content", "Allow untracked file contents to be sent with Advisor context.", settings.untrackedContent, false), toggle("outcomeLogging", "Outcome logging (global)", "Allow anonymized Advisor outcomes to be logged globally.", settings.outcomeLogging, false), ...jevItems(settings, theme, tui,
|
|
8143
|
+
}, toggle("trackedFileContent", "Tracked file content", "Allow tracked file contents to be sent with Advisor context.", settings.trackedFileContent, false), toggle("untrackedContent", "Untracked file content", "Allow untracked file contents to be sent with Advisor context.", settings.untrackedContent, false), toggle("outcomeLogging", "Outcome logging (global)", "Allow anonymized Advisor outcomes to be logged globally.", settings.outcomeLogging, false), ...jevItems(settings, theme, tui, jevClassifierRegistry, jevKeyDeps, onJevFilter, onJevProvider));
|
|
9152
8144
|
return items;
|
|
9153
8145
|
};
|
|
9154
8146
|
|
|
@@ -9364,10 +8356,6 @@ var applyJevMutation = (settings, id, value) => {
|
|
|
9364
8356
|
settings.jevTurnGateNoulThreshold = Number(value);
|
|
9365
8357
|
return true;
|
|
9366
8358
|
}
|
|
9367
|
-
case "jevModel": {
|
|
9368
|
-
settings.jevModel = value.trim() || DEFAULT_JEV_MODEL;
|
|
9369
|
-
return true;
|
|
9370
|
-
}
|
|
9371
8359
|
case "jevTimeoutMs": {
|
|
9372
8360
|
settings.jevTimeoutMs = Number(value);
|
|
9373
8361
|
return true;
|
|
@@ -9380,10 +8368,6 @@ var applyJevMutation = (settings, id, value) => {
|
|
|
9380
8368
|
settings.jevDigestMaxChars = Number(value);
|
|
9381
8369
|
return true;
|
|
9382
8370
|
}
|
|
9383
|
-
case "jevPricePerMtok": {
|
|
9384
|
-
settings.jevPricePerMtok = Number(value);
|
|
9385
|
-
return true;
|
|
9386
|
-
}
|
|
9387
8371
|
default: {
|
|
9388
8372
|
return false;
|
|
9389
8373
|
}
|
|
@@ -9448,7 +8432,7 @@ class AdvisorSettingsSelector {
|
|
|
9448
8432
|
}
|
|
9449
8433
|
render(width) {
|
|
9450
8434
|
const border = this.options.theme.fg("border", "─".repeat(Math.max(1, width)));
|
|
9451
|
-
return [border, ...this.settingsList.render(width), border].map((line) =>
|
|
8435
|
+
return [border, ...this.settingsList.render(width), border].map((line) => truncateToWidth7(line, width));
|
|
9452
8436
|
}
|
|
9453
8437
|
handleInput(keyData) {
|
|
9454
8438
|
if (!this.settingsList.changeWithArrow(keyData, (id, value) => this.change(id, value))) {
|
|
@@ -9468,13 +8452,14 @@ class AdvisorSettingsSelector {
|
|
|
9468
8452
|
const fallbackModel = advisorFallbackModelItem(this.settings, this.options.modelRefs, this.options.keybindings, this.options.theme, this.options.tui);
|
|
9469
8453
|
const modelWhitelist = advisorModelWhitelistItem(this.settings, this.options.modelRefs, this.options.keybindings, this.options.theme, this.options.tui);
|
|
9470
8454
|
const items = createSettingsItems({
|
|
9471
|
-
afterJevSetup: () => {
|
|
9472
|
-
this.settingsList = this.createSettingsList("jevFilter");
|
|
9473
|
-
this.options.tui.requestRender();
|
|
9474
|
-
},
|
|
9475
8455
|
effortLevels: this.options.effortLevels,
|
|
9476
8456
|
fallbackModel,
|
|
9477
|
-
|
|
8457
|
+
jevClassifierRegistry: this.options.jevSetupDeps?.modelRegistry,
|
|
8458
|
+
jevKeyDeps: {
|
|
8459
|
+
persistClassifierKey: this.options.jevSetupDeps?.persistClassifierKey,
|
|
8460
|
+
resolveClassifierKey: this.options.jevSetupDeps?.resolveClassifierKey,
|
|
8461
|
+
restoreClassifierKey: this.options.jevSetupDeps?.restoreClassifierKey
|
|
8462
|
+
},
|
|
9478
8463
|
modelWhitelist,
|
|
9479
8464
|
onJevFilter: (selection) => this.applyJevFilter(selection),
|
|
9480
8465
|
onJevProvider: (selection) => this.applyJevProvider(selection),
|
|
@@ -9518,24 +8503,30 @@ class AdvisorSettingsSelector {
|
|
|
9518
8503
|
if (legacy) {
|
|
9519
8504
|
return this.applyJevSelection(patch, (settings) => legacy({
|
|
9520
8505
|
enabled: selection.enabled,
|
|
9521
|
-
transport: settings.jevTransport ??
|
|
8506
|
+
transport: settings.jevTransport ?? "auto"
|
|
9522
8507
|
}, settings));
|
|
9523
8508
|
}
|
|
9524
8509
|
return this.applyJevSelection(patch);
|
|
9525
8510
|
}
|
|
9526
8511
|
applyJevProvider(selection) {
|
|
9527
|
-
|
|
9528
|
-
|
|
9529
|
-
|
|
9530
|
-
|
|
9531
|
-
|
|
9532
|
-
|
|
8512
|
+
let patch;
|
|
8513
|
+
if (selection.classifier) {
|
|
8514
|
+
patch = { jevClassifier: selection.classifier };
|
|
8515
|
+
} else if (selection.transport) {
|
|
8516
|
+
patch = {
|
|
8517
|
+
jevBaseUrl: selection.baseUrl,
|
|
8518
|
+
jevKeyProvider: selection.keyProvider,
|
|
8519
|
+
jevTransport: selection.transport
|
|
8520
|
+
};
|
|
8521
|
+
} else {
|
|
8522
|
+
patch = {};
|
|
8523
|
+
}
|
|
9533
8524
|
const handler = this.options.onJevProvider;
|
|
9534
8525
|
if (handler) {
|
|
9535
8526
|
return this.applyJevSelection(patch, (settings) => handler(selection, settings));
|
|
9536
8527
|
}
|
|
9537
8528
|
const legacy = this.options.onJevSetup;
|
|
9538
|
-
if (legacy) {
|
|
8529
|
+
if (legacy && selection.transport) {
|
|
9539
8530
|
return this.applyJevSelection(patch, (settings) => legacy({
|
|
9540
8531
|
enabled: settings.jevFilterEnabled ?? false,
|
|
9541
8532
|
transport: selection.transport
|
|
@@ -9607,16 +8598,12 @@ var applySessionSettings = (settings) => {
|
|
|
9607
8598
|
};
|
|
9608
8599
|
var applyJevSettings = (settings) => {
|
|
9609
8600
|
setAdvisorJevFilterEnabledRef(settings.jevFilterEnabled ?? false);
|
|
9610
|
-
|
|
9611
|
-
setAdvisorJevKeyProviderRef(settings.jevKeyProvider);
|
|
8601
|
+
setAdvisorJevClassifierRef(settings.jevClassifier);
|
|
9612
8602
|
setAdvisorJevFilterSkipConfidenceRef(settings.jevFilterSkipConfidence ?? DEFAULT_JEV_FILTER_SKIP_CONFIDENCE);
|
|
9613
8603
|
setAdvisorJevFilterNoulMarginRef(settings.jevFilterNoulMargin ?? DEFAULT_JEV_FILTER_NOUL_MARGIN);
|
|
9614
8604
|
setAdvisorJevFilterOverrideWindowRef(settings.jevFilterOverrideWindow ?? DEFAULT_JEV_FILTER_OVERRIDE_WINDOW);
|
|
9615
|
-
setAdvisorJevModelRef(settings.jevModel ?? DEFAULT_JEV_MODEL);
|
|
9616
8605
|
setAdvisorJevTimeoutMsRef(settings.jevTimeoutMs ?? DEFAULT_JEV_TIMEOUT_MS);
|
|
9617
8606
|
setAdvisorJevDigestMaxCharsRef(settings.jevDigestMaxChars ?? DEFAULT_JEV_DIGEST_MAX_CHARS);
|
|
9618
|
-
setAdvisorJevPricePerMtokRef(settings.jevPricePerMtok ?? DEFAULT_JEV_PRICE_PER_MTOK);
|
|
9619
|
-
setAdvisorJevTransportRef(settings.jevTransport ?? DEFAULT_JEV_TRANSPORT);
|
|
9620
8607
|
setAdvisorJevTurnGateEveryTurnsRef(settings.jevTurnGateEveryTurns ?? DEFAULT_JEV_TURN_GATE_EVERY_TURNS);
|
|
9621
8608
|
setAdvisorJevTurnGateNoulThresholdRef(settings.jevTurnGateNoulThreshold ?? DEFAULT_JEV_TURN_GATE_NOUL_THRESHOLD);
|
|
9622
8609
|
};
|
|
@@ -9686,8 +8673,10 @@ var registerSettingsCommands = (runtime) => {
|
|
|
9686
8673
|
effortLevels: EFFORT_LEVELS,
|
|
9687
8674
|
initial,
|
|
9688
8675
|
jevSetupDeps: {
|
|
9689
|
-
|
|
9690
|
-
|
|
8676
|
+
modelRegistry: ctx.modelRegistry,
|
|
8677
|
+
persistClassifierKey: writeJevClassifierKey,
|
|
8678
|
+
resolveClassifierKey: readJevClassifierKey,
|
|
8679
|
+
restoreClassifierKey: restoreJevClassifierKey
|
|
9691
8680
|
},
|
|
9692
8681
|
keybindings,
|
|
9693
8682
|
modelRefs: getConfiguredModelRefs(ctx),
|
|
@@ -9847,15 +8836,7 @@ var reserveAdvisorCall = (event, ctx, session, reservedCalls) => {
|
|
|
9847
8836
|
};
|
|
9848
8837
|
|
|
9849
8838
|
// src/tools/jev-turn-gate.ts
|
|
9850
|
-
var
|
|
9851
|
-
criteria: {
|
|
9852
|
-
false: "The executor is progressing soundly on work that matches the user's intent; interrupting would add nothing material.",
|
|
9853
|
-
true: "The executor is approaching a material decision, repeating a failure, about to claim success without validation, or drifting from the user's intent; a second opinion now would change what happens next."
|
|
9854
|
-
},
|
|
9855
|
-
instructions: "Should a senior engineering advisor be consulted right now, before the executor continues? Judge from `recent_conversation`.",
|
|
9856
|
-
type: "noul"
|
|
9857
|
-
};
|
|
9858
|
-
var outageNotifier2 = createOutageNotifier((category, message) => `Advisor Jev/Decisions turn gate failed (${category}); continuing without a proactive consultation. ${message}`);
|
|
8839
|
+
var outageNotifier2 = createOutageNotifier((category, message) => `Advisor Jev classifier turn gate failed (${category}); continuing without a proactive consultation. ${message}`);
|
|
9859
8840
|
var notifyFailureOnce = outageNotifier2.notify;
|
|
9860
8841
|
var shouldRunTurnGate = (registration, ctx, session) => {
|
|
9861
8842
|
const interval = advisorJevTurnGateEveryTurnsRef;
|
|
@@ -9869,6 +8850,61 @@ var consumeTurnGateBudget = (session, shouldConsult) => {
|
|
|
9869
8850
|
return true;
|
|
9870
8851
|
};
|
|
9871
8852
|
var resetJevTurnGateNotification = outageNotifier2.reset;
|
|
8853
|
+
var classifyGate = async (ctx, deps) => {
|
|
8854
|
+
if (deps.classifyResult) {
|
|
8855
|
+
return deps.classifyResult();
|
|
8856
|
+
}
|
|
8857
|
+
const selectedRef = advisorJevClassifierModeRef === "api-key" ? "typesafe/jev-latest" : deps.selectedRef?.() ?? advisorJevClassifierRef;
|
|
8858
|
+
const apiKey = advisorJevClassifierModeRef === "api-key" ? await (deps.resolveApiKey ?? consumeJevClassifierKey)() : undefined;
|
|
8859
|
+
if (!selectedRef || advisorJevClassifierModeRef === "api-key" && !apiKey) {
|
|
8860
|
+
throw new JevFailureError("auth", "No credentials resolved for the selected Jev classifier.");
|
|
8861
|
+
}
|
|
8862
|
+
return classifyTurnGate(ctx, selectedRef, buildJevState(ctx, {}), {
|
|
8863
|
+
apiKey,
|
|
8864
|
+
signal: ctx.signal
|
|
8865
|
+
});
|
|
8866
|
+
};
|
|
8867
|
+
var sendProactiveConsultation = async (registration, ctx, consult) => {
|
|
8868
|
+
const { session } = registration;
|
|
8869
|
+
const activity = registration.herdrActivity ?? herdrAdvisorActivity;
|
|
8870
|
+
const finishHerdrActivity = activity.start();
|
|
8871
|
+
try {
|
|
8872
|
+
registration.send({
|
|
8873
|
+
content: "Proactive Advisor turn review",
|
|
8874
|
+
customType: "advisor-turn-gate-call",
|
|
8875
|
+
details: {
|
|
8876
|
+
question: `Turn gate: ${session.turnsSinceConsultation} turns without a consultation`,
|
|
8877
|
+
turn: session.sessionTurnOrdinal
|
|
8878
|
+
},
|
|
8879
|
+
display: true
|
|
8880
|
+
});
|
|
8881
|
+
const consulted = await consult(ctx, undefined, ctx.signal, undefined, "turn-gate");
|
|
8882
|
+
session.recordJevGateConsultation();
|
|
8883
|
+
session.resetTurnsSinceConsultation();
|
|
8884
|
+
session.recordInvocation({
|
|
8885
|
+
cost: advisorUsageCost(consulted.usage),
|
|
8886
|
+
executionEffect: "continued",
|
|
8887
|
+
kind: "markdown",
|
|
8888
|
+
model: consulted.model,
|
|
8889
|
+
trigger: "turn-gate",
|
|
8890
|
+
usage: consulted.usage
|
|
8891
|
+
});
|
|
8892
|
+
updateAdvisorUsageStatus(ctx, session);
|
|
8893
|
+
registration.send({
|
|
8894
|
+
content: consulted.markdown,
|
|
8895
|
+
customType: "advisor-turn-gate-result",
|
|
8896
|
+
details: {
|
|
8897
|
+
advisor: consulted.model,
|
|
8898
|
+
agentRulesBytes: consulted.agentRulesBytes,
|
|
8899
|
+
text: consulted.markdown,
|
|
8900
|
+
usage: consulted.usage
|
|
8901
|
+
},
|
|
8902
|
+
display: true
|
|
8903
|
+
});
|
|
8904
|
+
} finally {
|
|
8905
|
+
finishHerdrActivity();
|
|
8906
|
+
}
|
|
8907
|
+
};
|
|
9872
8908
|
var handleJevTurnEnd = async (registration, ctx) => {
|
|
9873
8909
|
const { session } = registration;
|
|
9874
8910
|
session.recordCompletedTurn();
|
|
@@ -9876,60 +8912,16 @@ var handleJevTurnEnd = async (registration, ctx) => {
|
|
|
9876
8912
|
return;
|
|
9877
8913
|
}
|
|
9878
8914
|
const consult = registration.deps?.consult ?? registration.consult;
|
|
9879
|
-
const deps = registration.deps ?? {};
|
|
9880
8915
|
try {
|
|
9881
|
-
const
|
|
9882
|
-
|
|
9883
|
-
|
|
9884
|
-
|
|
9885
|
-
|
|
9886
|
-
}
|
|
9887
|
-
const client = jevClientFromCredentials(credentials, deps.fetch);
|
|
9888
|
-
const result = await client.ask(buildJevState(ctx, {}), {
|
|
9889
|
-
should_consult: turnGateQuestion
|
|
9890
|
-
});
|
|
9891
|
-
session.recordJevGateCheck(result.usage);
|
|
8916
|
+
const result = await classifyGate(ctx, registration.deps ?? {});
|
|
8917
|
+
session.recordJevGateCheck(result.usage ? {
|
|
8918
|
+
cost: result.usage.cost,
|
|
8919
|
+
inputTokens: result.usage.inputTokens,
|
|
8920
|
+
outputTokens: result.usage.outputTokens
|
|
8921
|
+
} : undefined);
|
|
9892
8922
|
const shouldConsult = composeTurnGateVerdict(result.answers, advisorJevTurnGateNoulThresholdRef);
|
|
9893
|
-
if (
|
|
9894
|
-
|
|
9895
|
-
}
|
|
9896
|
-
const herdrActivity = registration.herdrActivity ?? herdrAdvisorActivity;
|
|
9897
|
-
const finishHerdrActivity = herdrActivity.start();
|
|
9898
|
-
try {
|
|
9899
|
-
registration.send({
|
|
9900
|
-
content: "Proactive Advisor turn review",
|
|
9901
|
-
customType: "advisor-turn-gate-call",
|
|
9902
|
-
details: {
|
|
9903
|
-
question: `Turn gate: ${session.turnsSinceConsultation} turns without a consultation`,
|
|
9904
|
-
turn: session.sessionTurnOrdinal
|
|
9905
|
-
},
|
|
9906
|
-
display: true
|
|
9907
|
-
});
|
|
9908
|
-
const consulted = await consult(ctx, undefined, ctx.signal, undefined, "turn-gate");
|
|
9909
|
-
session.recordJevGateConsultation();
|
|
9910
|
-
session.resetTurnsSinceConsultation();
|
|
9911
|
-
session.recordInvocation({
|
|
9912
|
-
cost: advisorUsageCost(consulted.usage),
|
|
9913
|
-
executionEffect: "continued",
|
|
9914
|
-
kind: "markdown",
|
|
9915
|
-
model: consulted.model,
|
|
9916
|
-
trigger: "turn-gate",
|
|
9917
|
-
usage: consulted.usage
|
|
9918
|
-
});
|
|
9919
|
-
updateAdvisorUsageStatus(ctx, session);
|
|
9920
|
-
registration.send({
|
|
9921
|
-
content: consulted.markdown,
|
|
9922
|
-
customType: "advisor-turn-gate-result",
|
|
9923
|
-
details: {
|
|
9924
|
-
advisor: consulted.model,
|
|
9925
|
-
agentRulesBytes: consulted.agentRulesBytes,
|
|
9926
|
-
text: consulted.markdown,
|
|
9927
|
-
usage: consulted.usage
|
|
9928
|
-
},
|
|
9929
|
-
display: true
|
|
9930
|
-
});
|
|
9931
|
-
} finally {
|
|
9932
|
-
finishHerdrActivity();
|
|
8923
|
+
if (advisorModelIsAllowed(ctx) && consumeTurnGateBudget(session, shouldConsult)) {
|
|
8924
|
+
await sendProactiveConsultation(registration, ctx, consult);
|
|
9933
8925
|
}
|
|
9934
8926
|
} catch (error) {
|
|
9935
8927
|
session.recordJevGateFailure();
|
|
@@ -10194,6 +9186,7 @@ var registerAskAdvisorTool = ({
|
|
|
10194
9186
|
reservedCalls.delete(_id);
|
|
10195
9187
|
throw error;
|
|
10196
9188
|
}
|
|
9189
|
+
let classifierUsage;
|
|
10197
9190
|
const simpleMode = isSimpleMode();
|
|
10198
9191
|
if (!simpleMode && !session.reserveCall(_id, getAdvisorMaxCallsPerSession())) {
|
|
10199
9192
|
throw new Error("Advisor call budget exhausted for this session.");
|
|
@@ -10216,6 +9209,7 @@ var registerAskAdvisorTool = ({
|
|
|
10216
9209
|
question: resolveAdvisorRequest(params.question),
|
|
10217
9210
|
signal
|
|
10218
9211
|
});
|
|
9212
|
+
classifierUsage = screening.usage;
|
|
10219
9213
|
if (screening.decision === "skip") {
|
|
10220
9214
|
const skipText = screeningSkipText(screening);
|
|
10221
9215
|
session.releaseCall(_id);
|
|
@@ -10228,11 +9222,15 @@ var registerAskAdvisorTool = ({
|
|
|
10228
9222
|
},
|
|
10229
9223
|
text: skipText
|
|
10230
9224
|
};
|
|
10231
|
-
|
|
9225
|
+
const skipResult = {
|
|
10232
9226
|
content: [{ text: skipText, type: "text" }],
|
|
10233
9227
|
details: skipped,
|
|
10234
9228
|
structuredContent: skipped
|
|
10235
9229
|
};
|
|
9230
|
+
if (classifierUsage) {
|
|
9231
|
+
skipResult.usage = classifierUsage;
|
|
9232
|
+
}
|
|
9233
|
+
return skipResult;
|
|
10236
9234
|
}
|
|
10237
9235
|
if (sameModelAdvisorDisabled(ctx, ctx.model, followUp?.payload.model)) {
|
|
10238
9236
|
return skippedSameModelResult(_id, sameModelNoticeFor(followUp?.payload.model), reservedCalls, session);
|
|
@@ -10296,7 +9294,8 @@ var registerAskAdvisorTool = ({
|
|
|
10296
9294
|
usage: result.usage
|
|
10297
9295
|
});
|
|
10298
9296
|
const usage = snapshotAdvisorUsage(result.usage);
|
|
10299
|
-
const
|
|
9297
|
+
const advisorPiUsage = advisorUsageForPi(result.usage);
|
|
9298
|
+
const piUsage = combineAdvisorAndClassifierUsage(advisorPiUsage, classifierUsage);
|
|
10300
9299
|
updateAdvisorUsageStatus(ctx, session);
|
|
10301
9300
|
const details = {
|
|
10302
9301
|
adviceId: result.adviceId,
|