pi-advisor-flow 0.13.1 → 0.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.js +14 -7
- package/extensions/index.ts +6 -0
- package/package.json +9 -9
- package/scripts/check-bun-version.mjs +9 -0
- package/src/attachments.ts +36 -0
- package/src/child-session.ts +1 -0
- package/src/commands/activation-preparation.ts +12 -0
- package/src/commands/activation.ts +43 -0
- package/src/commands/advisor-tools.ts +3 -0
- package/src/commands/lifecycle.ts +15 -0
- package/src/commands/manual-command.ts +19 -0
- package/src/commands/manual-consultation.ts +20 -0
- package/src/commands/manual-progress.ts +13 -0
- package/src/commands/model-commands.ts +4 -0
- package/src/commands/model-options.ts +18 -0
- package/src/commands/model-picker.ts +16 -0
- package/src/commands/outcome-stats.ts +9 -0
- package/src/commands/registration.ts +1 -0
- package/src/commands/renderers.ts +14 -0
- package/src/commands/runtime.ts +5 -0
- package/src/commands/settings-commands.ts +18 -0
- package/src/commands/settings-persistence.ts +2 -0
- package/src/commands.ts +1 -0
- package/src/config/args.ts +7 -0
- package/src/config/defaults.ts +1 -0
- package/src/config/schema.ts +1 -0
- package/src/config/state.ts +64 -1
- package/src/config/storage.ts +37 -0
- package/src/config/types.ts +9 -0
- package/src/config/validation.ts +7 -0
- package/src/config.ts +6 -0
- package/src/content-utils.ts +5 -0
- package/src/conversation.ts +39 -0
- package/src/follow-up.ts +8 -0
- package/src/git.ts +8 -0
- package/src/herdr-block.ts +7 -0
- package/src/herdr-shared.ts +23 -0
- package/src/herdr.ts +18 -0
- package/src/image-attachments.ts +24 -0
- package/src/image-validation.ts +58 -0
- package/src/images.ts +24 -0
- package/src/jev/base-url.ts +14 -0
- package/src/jev/client.ts +49 -0
- package/src/jev/decisions-client.ts +14 -0
- package/src/jev/decisions-response.ts +38 -0
- package/src/jev/endpoint-key-store.ts +18 -0
- package/src/jev/key-store.ts +28 -0
- package/src/jev/ledger.ts +30 -12
- package/src/jev/openai-key-store.ts +17 -0
- package/src/jev/questions.ts +15 -0
- package/src/jev/state.ts +5 -0
- package/src/jev/transport.ts +35 -0
- package/src/model-stream.ts +32 -0
- package/src/outcome-stats.ts +16 -0
- package/src/outcomes.ts +31 -0
- package/src/pi-settings.ts +9 -0
- package/src/preferences.ts +26 -0
- package/src/redaction.ts +8 -0
- package/src/scout-context.ts +17 -0
- package/src/scout-curation.ts +12 -0
- package/src/scout-groups.ts +47 -0
- package/src/scout-protocol.ts +17 -0
- package/src/scout-reconstruct.ts +12 -0
- package/src/scout.ts +47 -0
- package/src/session-context.ts +11 -0
- package/src/session-state.ts +73 -13
- package/src/tool-result-cap.ts +11 -0
- package/src/tools/consult-context.ts +24 -0
- package/src/tools/consultation.ts +43 -0
- package/src/tools/gate-policy.ts +18 -0
- package/src/tools/gate-protocol.ts +22 -0
- package/src/tools/jev-filter.ts +26 -0
- package/src/tools/jev-turn-gate.ts +17 -0
- package/src/tools/loop-gate.ts +25 -0
- package/src/tools/model-access.ts +12 -0
- package/src/tools/outage-notifier.ts +4 -0
- package/src/tools/prompts.ts +17 -0
- package/src/tools/register-ask-advisor.ts +44 -0
- package/src/tools/register-lifecycle.ts +32 -0
- package/src/tools/register-outcome.ts +7 -0
- package/src/tools/register-renderers.ts +14 -0
- package/src/tools/registration.ts +3 -0
- package/src/tools/render-advisor-result.ts +44 -0
- package/src/tools/render-common.ts +10 -0
- package/src/tools/scout-status.ts +34 -0
- package/src/tools/session.ts +3 -0
- package/src/tools.ts +8 -0
- package/src/ui/jev-filter-submenu.ts +21 -0
- package/src/ui/jev-provider-submenu-view.ts +22 -0
- package/src/ui/jev-provider-submenu.ts +81 -0
- package/src/ui/jev-setup-support.ts +26 -0
- package/src/ui/manual-dialog-render.ts +20 -0
- package/src/ui/manual-dialog.ts +38 -0
- package/src/ui/masked-input.ts +2 -0
- package/src/ui/searchable-model-list.ts +28 -0
- package/src/ui/settings-formatting.ts +26 -0
- package/src/ui/settings-items.ts +11 -0
- package/src/ui/settings-list-adapter.ts +13 -0
- package/src/ui/settings-mutations.ts +60 -0
- package/src/ui/settings-selector.ts +30 -0
- package/src/ui/text-setting-submenu.ts +9 -0
- package/src/ui-guard.ts +1 -0
- package/src/ui.ts +3 -0
- package/src/usage.ts +31 -0
package/src/jev/base-url.ts
CHANGED
|
@@ -11,6 +11,7 @@ const IPV4_LOOPBACK_HOST = /^127(?:\.\d{1,3}){3}$/u;
|
|
|
11
11
|
|
|
12
12
|
const isLoopbackHost = (hostname: string): boolean => {
|
|
13
13
|
const host = hostname.toLowerCase();
|
|
14
|
+
|
|
14
15
|
return (
|
|
15
16
|
host === "localhost" ||
|
|
16
17
|
host === "[::1]" ||
|
|
@@ -21,46 +22,59 @@ const isLoopbackHost = (hostname: string): boolean => {
|
|
|
21
22
|
|
|
22
23
|
const stripContractPath = (pathname: string): string => {
|
|
23
24
|
let path = pathname.replace(/\/+$/u, "");
|
|
25
|
+
|
|
24
26
|
if (path.endsWith(JEV_SYSTEM_ONE_PATH)) {
|
|
25
27
|
path = path.slice(0, -JEV_SYSTEM_ONE_PATH.length);
|
|
26
28
|
}
|
|
29
|
+
|
|
27
30
|
path = path.replace(/\/+$/u, "");
|
|
31
|
+
|
|
28
32
|
if (path.endsWith(VERSION_PATH)) {
|
|
29
33
|
path = path.slice(0, -VERSION_PATH.length);
|
|
30
34
|
}
|
|
35
|
+
|
|
31
36
|
return path.replace(/\/+$/u, "");
|
|
32
37
|
};
|
|
33
38
|
|
|
34
39
|
/** Returns the origin plus path prefix of any accepted Base URL spelling. */
|
|
35
40
|
export const normalizeJevBaseUrl = (input: string): JevBaseUrlResult => {
|
|
36
41
|
const trimmed = input.trim();
|
|
42
|
+
|
|
37
43
|
if (!trimmed) {
|
|
38
44
|
return { error: "Enter the Base URL of a System One–compatible endpoint." };
|
|
39
45
|
}
|
|
46
|
+
|
|
40
47
|
let parsed: URL;
|
|
48
|
+
|
|
41
49
|
try {
|
|
42
50
|
parsed = new URL(trimmed);
|
|
43
51
|
} catch {
|
|
44
52
|
return { error: "That is not a valid URL." };
|
|
45
53
|
}
|
|
54
|
+
|
|
46
55
|
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
|
|
47
56
|
return { error: "The Base URL must use https." };
|
|
48
57
|
}
|
|
58
|
+
|
|
49
59
|
if (parsed.protocol === "http:" && !isLoopbackHost(parsed.hostname)) {
|
|
50
60
|
return {
|
|
51
61
|
error:
|
|
52
62
|
"Plain http is allowed only for localhost, ::1, and 127.x addresses.",
|
|
53
63
|
};
|
|
54
64
|
}
|
|
65
|
+
|
|
55
66
|
if (parsed.username || parsed.password) {
|
|
56
67
|
return { error: "Remove the credentials embedded in the URL." };
|
|
57
68
|
}
|
|
69
|
+
|
|
58
70
|
if (parsed.search) {
|
|
59
71
|
return { error: "Remove the query string from the Base URL." };
|
|
60
72
|
}
|
|
73
|
+
|
|
61
74
|
if (parsed.hash) {
|
|
62
75
|
return { error: "Remove the fragment from the Base URL." };
|
|
63
76
|
}
|
|
77
|
+
|
|
64
78
|
return { baseUrl: `${parsed.origin}${stripContractPath(parsed.pathname)}` };
|
|
65
79
|
};
|
|
66
80
|
|
package/src/jev/client.ts
CHANGED
|
@@ -24,6 +24,7 @@ import type { JevErrorCategory } from "./failure.ts";
|
|
|
24
24
|
import type { JevCredentials, JevTransportKind } from "./transport.ts";
|
|
25
25
|
|
|
26
26
|
export type { JevErrorCategory } from "./failure.ts";
|
|
27
|
+
|
|
27
28
|
export { JevFailureError as JevFailure } from "./failure.ts";
|
|
28
29
|
|
|
29
30
|
export interface JevUsage {
|
|
@@ -71,9 +72,11 @@ const errorDetail = (error: JsonValue | undefined): string => {
|
|
|
71
72
|
if (isString(error)) {
|
|
72
73
|
return error;
|
|
73
74
|
}
|
|
75
|
+
|
|
74
76
|
if (isRecordOf(error) && "message" in error) {
|
|
75
77
|
return String(error.message);
|
|
76
78
|
}
|
|
79
|
+
|
|
77
80
|
return "";
|
|
78
81
|
};
|
|
79
82
|
|
|
@@ -81,9 +84,11 @@ const statusCategory = (status: number): JevErrorCategory => {
|
|
|
81
84
|
if (status === 401 || status === 403) {
|
|
82
85
|
return "auth";
|
|
83
86
|
}
|
|
87
|
+
|
|
84
88
|
if (status === 400 || status === 422) {
|
|
85
89
|
return "malformed";
|
|
86
90
|
}
|
|
91
|
+
|
|
87
92
|
return "error";
|
|
88
93
|
};
|
|
89
94
|
|
|
@@ -103,11 +108,13 @@ const buildClientRequest = (
|
|
|
103
108
|
): ClientRequest => {
|
|
104
109
|
if (transport === "openai-decisions") {
|
|
105
110
|
const request = buildDecisionsRequest(state, questions);
|
|
111
|
+
|
|
106
112
|
return {
|
|
107
113
|
body: JSON.stringify(request),
|
|
108
114
|
decisionQuestions: request.questions,
|
|
109
115
|
};
|
|
110
116
|
}
|
|
117
|
+
|
|
111
118
|
return {
|
|
112
119
|
body: JSON.stringify({ model, questions, state }),
|
|
113
120
|
};
|
|
@@ -125,6 +132,7 @@ const connectionFailure = <E>(error: E) => {
|
|
|
125
132
|
const message = redactSecrets(
|
|
126
133
|
error instanceof Error ? error.message : String(error)
|
|
127
134
|
);
|
|
135
|
+
|
|
128
136
|
return {
|
|
129
137
|
failure: new JevFailureError(
|
|
130
138
|
"network",
|
|
@@ -137,6 +145,7 @@ const sleepWithAbort = (signal: AbortSignal, ms: number): Promise<void> => {
|
|
|
137
145
|
if (signal.aborted) {
|
|
138
146
|
return Promise.resolve();
|
|
139
147
|
}
|
|
148
|
+
|
|
140
149
|
// oxlint-disable-next-line promise/avoid-new -- unref-ed abortable retry timer cannot be expressed with async/await.
|
|
141
150
|
return new Promise((resolve) => {
|
|
142
151
|
const timer = setTimeout(resolve, ms);
|
|
@@ -172,6 +181,7 @@ export class JevClient {
|
|
|
172
181
|
transport,
|
|
173
182
|
}: JevClientOptions) {
|
|
174
183
|
this.#apiKey = apiKey;
|
|
184
|
+
|
|
175
185
|
if (transport === "typesafe-compatible") {
|
|
176
186
|
if (!baseUrl) {
|
|
177
187
|
throw new JevFailureError(
|
|
@@ -179,12 +189,15 @@ export class JevClient {
|
|
|
179
189
|
"The System One–compatible endpoint has no Base URL."
|
|
180
190
|
);
|
|
181
191
|
}
|
|
192
|
+
|
|
182
193
|
this.#endpoint = jevSystemOneEndpoint(baseUrl);
|
|
183
194
|
} else {
|
|
184
195
|
this.#endpoint = FIXED_ENDPOINTS[transport];
|
|
185
196
|
}
|
|
197
|
+
|
|
186
198
|
// SAFETY: the bound global fetch satisfies the SDK Fetch signature; binding keeps the receiver correct.
|
|
187
199
|
this.#fetch = fetch ?? (globalThis.fetch.bind(globalThis) as Fetch);
|
|
200
|
+
|
|
188
201
|
if (transport === "openai-decisions") {
|
|
189
202
|
this.#model = OPENAI_DECISIONS_MODEL;
|
|
190
203
|
} else if (transport === "openrouter") {
|
|
@@ -192,6 +205,7 @@ export class JevClient {
|
|
|
192
205
|
} else {
|
|
193
206
|
this.#model = model;
|
|
194
207
|
}
|
|
208
|
+
|
|
195
209
|
this.#pricePerMtok = pricePerMtok;
|
|
196
210
|
this.#timeoutMs = timeoutMs;
|
|
197
211
|
this.#transport = transport;
|
|
@@ -208,23 +222,31 @@ export class JevClient {
|
|
|
208
222
|
state,
|
|
209
223
|
questions
|
|
210
224
|
);
|
|
225
|
+
|
|
211
226
|
const deadline = new AbortController();
|
|
212
227
|
const abortFromCaller = () => deadline.abort(signal?.reason);
|
|
213
228
|
signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
229
|
+
|
|
214
230
|
if (signal?.aborted) {
|
|
215
231
|
abortFromCaller();
|
|
216
232
|
}
|
|
233
|
+
|
|
217
234
|
let deadlineHit = false;
|
|
235
|
+
|
|
218
236
|
const timer = setTimeout(() => {
|
|
219
237
|
deadlineHit = true;
|
|
220
238
|
deadline.abort(new Error("Jev wall-time budget elapsed"));
|
|
221
239
|
}, this.#timeoutMs);
|
|
240
|
+
|
|
222
241
|
timer.unref?.();
|
|
242
|
+
|
|
223
243
|
try {
|
|
224
244
|
let outcome: AttemptOutcome = {};
|
|
245
|
+
|
|
225
246
|
for (let attempt = 1; ; attempt += 1) {
|
|
226
247
|
// The retry loop is bounded to one backoff by the deadline controller.
|
|
227
248
|
outcome = await this.#attempt(body, deadline.signal, decisionQuestions);
|
|
249
|
+
|
|
228
250
|
if (
|
|
229
251
|
!outcome.retryable ||
|
|
230
252
|
attempt >= MAX_ATTEMPTS ||
|
|
@@ -232,22 +254,28 @@ export class JevClient {
|
|
|
232
254
|
) {
|
|
233
255
|
break;
|
|
234
256
|
}
|
|
257
|
+
|
|
235
258
|
await sleepWithAbort(deadline.signal, RETRY_BACKOFF_MS);
|
|
259
|
+
|
|
236
260
|
if (deadline.signal.aborted) {
|
|
237
261
|
break;
|
|
238
262
|
}
|
|
239
263
|
}
|
|
264
|
+
|
|
240
265
|
return this.#settle(outcome, deadlineHit, signal);
|
|
241
266
|
} catch (error) {
|
|
242
267
|
if (signal?.aborted && !deadlineHit) {
|
|
243
268
|
throw error;
|
|
244
269
|
}
|
|
270
|
+
|
|
245
271
|
if (error instanceof JevFailureError) {
|
|
246
272
|
throw error;
|
|
247
273
|
}
|
|
274
|
+
|
|
248
275
|
const message = redactSecrets(
|
|
249
276
|
error instanceof Error ? error.message : String(error)
|
|
250
277
|
);
|
|
278
|
+
|
|
251
279
|
throw deadlineHit
|
|
252
280
|
? new JevFailureError(
|
|
253
281
|
"timeout",
|
|
@@ -268,18 +296,22 @@ export class JevClient {
|
|
|
268
296
|
if (outcome.answers) {
|
|
269
297
|
return this.#result(outcome);
|
|
270
298
|
}
|
|
299
|
+
|
|
271
300
|
if (deadlineHit && outcome.failure?.category !== "auth") {
|
|
272
301
|
throw new JevFailureError(
|
|
273
302
|
"timeout",
|
|
274
303
|
`Jev call exceeded its ${this.#timeoutMs} ms wall-time budget.`
|
|
275
304
|
);
|
|
276
305
|
}
|
|
306
|
+
|
|
277
307
|
if (signal?.aborted) {
|
|
278
308
|
throw new Error("Jev call aborted by the caller.");
|
|
279
309
|
}
|
|
310
|
+
|
|
280
311
|
if (outcome.failure) {
|
|
281
312
|
throw outcome.failure;
|
|
282
313
|
}
|
|
314
|
+
|
|
283
315
|
throw new JevFailureError("error", "Jev call failed.");
|
|
284
316
|
}
|
|
285
317
|
|
|
@@ -291,7 +323,9 @@ export class JevClient {
|
|
|
291
323
|
if (signal.aborted) {
|
|
292
324
|
return { retryable: false };
|
|
293
325
|
}
|
|
326
|
+
|
|
294
327
|
let response: Response;
|
|
328
|
+
|
|
295
329
|
try {
|
|
296
330
|
response = await this.#fetch(this.#endpoint, {
|
|
297
331
|
body,
|
|
@@ -306,15 +340,19 @@ export class JevClient {
|
|
|
306
340
|
if (signal.aborted) {
|
|
307
341
|
return { retryable: false };
|
|
308
342
|
}
|
|
343
|
+
|
|
309
344
|
return {
|
|
310
345
|
retryable: true,
|
|
311
346
|
...connectionFailure(error),
|
|
312
347
|
};
|
|
313
348
|
}
|
|
349
|
+
|
|
314
350
|
if (response.ok) {
|
|
315
351
|
return this.#parseSuccess(response, decisionQuestions);
|
|
316
352
|
}
|
|
353
|
+
|
|
317
354
|
const failure = await this.#failureFromStatus(response);
|
|
355
|
+
|
|
318
356
|
return {
|
|
319
357
|
failure,
|
|
320
358
|
retryable: RETRYABLE_STATUSES.has(response.status),
|
|
@@ -323,6 +361,7 @@ export class JevClient {
|
|
|
323
361
|
|
|
324
362
|
async #failureFromStatus(response: Response): Promise<JevFailureError> {
|
|
325
363
|
let detail = "";
|
|
364
|
+
|
|
326
365
|
try {
|
|
327
366
|
const parsed: unknown = await response.json();
|
|
328
367
|
const error = isRecord(parsed) ? parsed.error : undefined;
|
|
@@ -330,9 +369,11 @@ export class JevClient {
|
|
|
330
369
|
} catch {
|
|
331
370
|
detail = "";
|
|
332
371
|
}
|
|
372
|
+
|
|
333
373
|
const message = redactSecrets(
|
|
334
374
|
`Jev ${this.#transportLabel()} request failed with HTTP ${response.status}${detail ? `: ${detail}` : ""}.`
|
|
335
375
|
);
|
|
376
|
+
|
|
336
377
|
return new JevFailureError(statusCategory(response.status), message);
|
|
337
378
|
}
|
|
338
379
|
|
|
@@ -340,9 +381,11 @@ export class JevClient {
|
|
|
340
381
|
if (this.#transport === "openrouter") {
|
|
341
382
|
return "OpenRouter";
|
|
342
383
|
}
|
|
384
|
+
|
|
343
385
|
if (this.#transport === "openai-decisions") {
|
|
344
386
|
return "OpenAI Decisions";
|
|
345
387
|
}
|
|
388
|
+
|
|
346
389
|
return this.#transport === "typesafe" ? "TypeSafe" : "endpoint";
|
|
347
390
|
}
|
|
348
391
|
|
|
@@ -355,6 +398,7 @@ export class JevClient {
|
|
|
355
398
|
await parseDecisionsApiResponse(response),
|
|
356
399
|
decisionQuestions ?? []
|
|
357
400
|
);
|
|
401
|
+
|
|
358
402
|
return {
|
|
359
403
|
answers: normalized.answers,
|
|
360
404
|
model: normalized.model,
|
|
@@ -364,7 +408,9 @@ export class JevClient {
|
|
|
364
408
|
},
|
|
365
409
|
};
|
|
366
410
|
}
|
|
411
|
+
|
|
367
412
|
let parsed: unknown;
|
|
413
|
+
|
|
368
414
|
try {
|
|
369
415
|
parsed = await response.json();
|
|
370
416
|
} catch (error) {
|
|
@@ -375,6 +421,7 @@ export class JevClient {
|
|
|
375
421
|
),
|
|
376
422
|
};
|
|
377
423
|
}
|
|
424
|
+
|
|
378
425
|
if (!isRecord(parsed) || !isRecord(parsed.answers)) {
|
|
379
426
|
return {
|
|
380
427
|
failure: new JevFailureError(
|
|
@@ -383,6 +430,7 @@ export class JevClient {
|
|
|
383
430
|
),
|
|
384
431
|
};
|
|
385
432
|
}
|
|
433
|
+
|
|
386
434
|
return {
|
|
387
435
|
answers: parsed.answers,
|
|
388
436
|
model: isString(parsed.model) ? parsed.model : this.#model,
|
|
@@ -400,6 +448,7 @@ export class JevClient {
|
|
|
400
448
|
const inputTokens = finiteTokens(usage?.input_tokens);
|
|
401
449
|
const outputTokens = finiteTokens(usage?.output_tokens);
|
|
402
450
|
const price = this.#pricePerMtok ?? advisorJevPricePerMtokRef;
|
|
451
|
+
|
|
403
452
|
return {
|
|
404
453
|
answers: outcome.answers ?? {},
|
|
405
454
|
model: outcome.model ?? this.#model,
|
|
@@ -38,21 +38,25 @@ const formatEntry = (entry: EntryType | undefined): string => {
|
|
|
38
38
|
if (isString(entry)) {
|
|
39
39
|
return entry;
|
|
40
40
|
}
|
|
41
|
+
|
|
41
42
|
return entry === undefined ? "" : JSON.stringify(entry);
|
|
42
43
|
};
|
|
43
44
|
|
|
44
45
|
const predicateInstructions = (question: Questions[string]): string => {
|
|
45
46
|
const parts = [formatEntry(question.instructions)];
|
|
47
|
+
|
|
46
48
|
if (question.type === "noul") {
|
|
47
49
|
if (question.criteria?.true !== undefined) {
|
|
48
50
|
parts.push(`A true answer means: ${formatEntry(question.criteria.true)}`);
|
|
49
51
|
}
|
|
52
|
+
|
|
50
53
|
if (question.criteria?.false !== undefined) {
|
|
51
54
|
parts.push(
|
|
52
55
|
`A false answer means: ${formatEntry(question.criteria.false)}`
|
|
53
56
|
);
|
|
54
57
|
}
|
|
55
58
|
}
|
|
59
|
+
|
|
56
60
|
return parts.filter(Boolean).join("\n\n");
|
|
57
61
|
};
|
|
58
62
|
|
|
@@ -60,6 +64,7 @@ const scoreLevelLabel = (description: EntryType, index: number): string => {
|
|
|
60
64
|
const text = formatEntry(description);
|
|
61
65
|
const colon = text.indexOf(":");
|
|
62
66
|
const label = (colon === -1 ? text : text.slice(0, colon)).trim();
|
|
67
|
+
|
|
63
68
|
return label || `Level ${index}`;
|
|
64
69
|
};
|
|
65
70
|
|
|
@@ -74,19 +79,25 @@ const decisionQuestion = (
|
|
|
74
79
|
type: "predicate",
|
|
75
80
|
};
|
|
76
81
|
}
|
|
82
|
+
|
|
77
83
|
if (question.type === "score") {
|
|
78
84
|
const labels = new Set<string>();
|
|
85
|
+
|
|
79
86
|
const levels = question.criteria.map((description, index) => {
|
|
80
87
|
const label = scoreLevelLabel(description, index);
|
|
88
|
+
|
|
81
89
|
if (labels.has(label)) {
|
|
82
90
|
throw new JevFailureError(
|
|
83
91
|
"malformed",
|
|
84
92
|
`Jev score question ${name} has duplicate level labels.`
|
|
85
93
|
);
|
|
86
94
|
}
|
|
95
|
+
|
|
87
96
|
labels.add(label);
|
|
97
|
+
|
|
88
98
|
return { description: formatEntry(description), label };
|
|
89
99
|
});
|
|
100
|
+
|
|
90
101
|
return {
|
|
91
102
|
instructions: formatEntry(question.instructions),
|
|
92
103
|
levels,
|
|
@@ -94,6 +105,7 @@ const decisionQuestion = (
|
|
|
94
105
|
type: "score",
|
|
95
106
|
};
|
|
96
107
|
}
|
|
108
|
+
|
|
97
109
|
throw new JevFailureError(
|
|
98
110
|
"malformed",
|
|
99
111
|
`Jev question ${name} has an unsupported type.`
|
|
@@ -104,6 +116,7 @@ export const formatDecisionsInput = (state: EntryType): string => {
|
|
|
104
116
|
if (isString(state)) {
|
|
105
117
|
return state;
|
|
106
118
|
}
|
|
119
|
+
|
|
107
120
|
return JSON.stringify(state, null, 2);
|
|
108
121
|
};
|
|
109
122
|
|
|
@@ -114,6 +127,7 @@ export const buildDecisionsRequest = (
|
|
|
114
127
|
if (!isRecordOf(questions) || Object.keys(questions).length === 0) {
|
|
115
128
|
throw new JevFailureError("malformed", "Jev questions are empty.");
|
|
116
129
|
}
|
|
130
|
+
|
|
117
131
|
return {
|
|
118
132
|
input: formatDecisionsInput(state),
|
|
119
133
|
model: OPENAI_DECISIONS_MODEL,
|
|
@@ -36,9 +36,11 @@ const boundedProbability = (
|
|
|
36
36
|
|
|
37
37
|
const normalizePredicateAnswer = (answer: RecordValue): RecordValue => {
|
|
38
38
|
const probability = boundedProbability(answer.probability);
|
|
39
|
+
|
|
39
40
|
if (probability === undefined) {
|
|
40
41
|
throw malformedResponse("has an invalid predicate probability");
|
|
41
42
|
}
|
|
43
|
+
|
|
42
44
|
return { noul: probability, type: "noul" };
|
|
43
45
|
};
|
|
44
46
|
|
|
@@ -49,20 +51,25 @@ const normalizeScoreAnswer = (
|
|
|
49
51
|
if (!Array.isArray(answer.probabilities)) {
|
|
50
52
|
throw malformedResponse("has no score probability array");
|
|
51
53
|
}
|
|
54
|
+
|
|
52
55
|
if (answer.probabilities.length !== question.levels.length) {
|
|
53
56
|
throw malformedResponse("has an incomplete score probability array");
|
|
54
57
|
}
|
|
58
|
+
|
|
55
59
|
const probabilities: Record<string, number> = {};
|
|
56
60
|
const legend: Record<string, string> = {};
|
|
57
61
|
const seenLabels = new Set<string>();
|
|
58
62
|
const seenValues = new Set<number>();
|
|
59
63
|
let probabilityTotal = 0;
|
|
64
|
+
|
|
60
65
|
for (const entry of answer.probabilities) {
|
|
61
66
|
if (!isRecord(entry)) {
|
|
62
67
|
throw malformedResponse("has an invalid score probability entry");
|
|
63
68
|
}
|
|
69
|
+
|
|
64
70
|
const { label, value } = entry;
|
|
65
71
|
const probability = boundedProbability(entry.probability);
|
|
72
|
+
|
|
66
73
|
if (
|
|
67
74
|
!isString(label) ||
|
|
68
75
|
!isNumber(value) ||
|
|
@@ -75,16 +82,20 @@ const normalizeScoreAnswer = (
|
|
|
75
82
|
) {
|
|
76
83
|
throw malformedResponse("has an invalid or duplicate score level");
|
|
77
84
|
}
|
|
85
|
+
|
|
78
86
|
const level = question.levels[value];
|
|
87
|
+
|
|
79
88
|
if (label !== level.label) {
|
|
80
89
|
throw malformedResponse("has an ambiguous score label/value pair");
|
|
81
90
|
}
|
|
91
|
+
|
|
82
92
|
seenLabels.add(label);
|
|
83
93
|
seenValues.add(value);
|
|
84
94
|
probabilities[String(value)] = probability;
|
|
85
95
|
legend[String(value)] = level.description;
|
|
86
96
|
probabilityTotal += probability;
|
|
87
97
|
}
|
|
98
|
+
|
|
88
99
|
if (
|
|
89
100
|
Math.abs(probabilityTotal - 1) > 0.02 ||
|
|
90
101
|
question.levels.some(
|
|
@@ -95,6 +106,7 @@ const normalizeScoreAnswer = (
|
|
|
95
106
|
"has an incomplete or ambiguous score distribution"
|
|
96
107
|
);
|
|
97
108
|
}
|
|
109
|
+
|
|
98
110
|
return { legend, probabilities, type: "score" };
|
|
99
111
|
};
|
|
100
112
|
|
|
@@ -107,31 +119,41 @@ export const parseDecisionsApiResponse = async (
|
|
|
107
119
|
response: Response
|
|
108
120
|
): Promise<DecisionsApiResponse> => {
|
|
109
121
|
let value: unknown;
|
|
122
|
+
|
|
110
123
|
try {
|
|
111
124
|
value = await response.json();
|
|
112
125
|
} catch {
|
|
113
126
|
throw malformedResponse("was not valid JSON");
|
|
114
127
|
}
|
|
128
|
+
|
|
115
129
|
if (!isRecord(value)) {
|
|
116
130
|
throw malformedResponse("is not an object");
|
|
117
131
|
}
|
|
132
|
+
|
|
118
133
|
const { answers: rawAnswers, model, usage } = value;
|
|
134
|
+
|
|
119
135
|
if (!Array.isArray(rawAnswers)) {
|
|
120
136
|
throw malformedResponse("did not include an answers array");
|
|
121
137
|
}
|
|
138
|
+
|
|
122
139
|
if (!isRecord(usage)) {
|
|
123
140
|
throw malformedResponse("has malformed usage");
|
|
124
141
|
}
|
|
142
|
+
|
|
125
143
|
if (model !== undefined && !isString(model)) {
|
|
126
144
|
throw malformedResponse("has an invalid model name");
|
|
127
145
|
}
|
|
146
|
+
|
|
128
147
|
const answers: RecordValue[] = [];
|
|
148
|
+
|
|
129
149
|
for (const answer of rawAnswers) {
|
|
130
150
|
if (!isRecord(answer)) {
|
|
131
151
|
throw malformedResponse("contains an invalid answer");
|
|
132
152
|
}
|
|
153
|
+
|
|
133
154
|
answers.push(answer);
|
|
134
155
|
}
|
|
156
|
+
|
|
135
157
|
return { answers, model: isString(model) ? model : undefined, usage };
|
|
136
158
|
};
|
|
137
159
|
|
|
@@ -142,42 +164,56 @@ const normalizeAnswers = (
|
|
|
142
164
|
const expected = new Map(
|
|
143
165
|
questions.map((question) => [question.name, question])
|
|
144
166
|
);
|
|
167
|
+
|
|
145
168
|
if (expected.size !== questions.length || expected.size === 0) {
|
|
146
169
|
throw malformedResponse(
|
|
147
170
|
"has duplicate or missing requested question names"
|
|
148
171
|
);
|
|
149
172
|
}
|
|
173
|
+
|
|
150
174
|
const received = new Map<string, RecordValue>();
|
|
175
|
+
|
|
151
176
|
for (const answer of responseAnswers) {
|
|
152
177
|
const { name } = answer;
|
|
178
|
+
|
|
153
179
|
if (!isString(name) || !name) {
|
|
154
180
|
throw malformedResponse("contains an unnamed answer");
|
|
155
181
|
}
|
|
182
|
+
|
|
156
183
|
if (!expected.has(name)) {
|
|
157
184
|
throw malformedResponse("contains an unexpected answer");
|
|
158
185
|
}
|
|
186
|
+
|
|
159
187
|
if (received.has(name)) {
|
|
160
188
|
throw malformedResponse("contains duplicate answer names");
|
|
161
189
|
}
|
|
190
|
+
|
|
162
191
|
if (answer.type === "refusal") {
|
|
163
192
|
throw malformedResponse("contains a refusal");
|
|
164
193
|
}
|
|
194
|
+
|
|
165
195
|
received.set(name, answer);
|
|
166
196
|
}
|
|
197
|
+
|
|
167
198
|
if (received.size !== expected.size) {
|
|
168
199
|
throw malformedResponse("is missing one or more named answers");
|
|
169
200
|
}
|
|
201
|
+
|
|
170
202
|
const answers: RecordValue = {};
|
|
203
|
+
|
|
171
204
|
for (const question of questions) {
|
|
172
205
|
const answer = received.get(question.name);
|
|
206
|
+
|
|
173
207
|
if (!answer || answer.type !== question.type) {
|
|
174
208
|
throw malformedResponse("has an answer with the wrong type");
|
|
175
209
|
}
|
|
210
|
+
|
|
176
211
|
answers[question.name] =
|
|
177
212
|
question.type === "predicate"
|
|
178
213
|
? normalizePredicateAnswer(answer)
|
|
179
214
|
: normalizeScoreAnswer(answer, question);
|
|
180
215
|
}
|
|
216
|
+
|
|
181
217
|
return answers;
|
|
182
218
|
};
|
|
183
219
|
|
|
@@ -187,9 +223,11 @@ export const normalizeDecisionsResponse = (
|
|
|
187
223
|
): NormalizedDecisionsResponse => {
|
|
188
224
|
const inputTokens = usageTokenCount(response.usage.input_tokens);
|
|
189
225
|
const outputTokens = usageTokenCount(response.usage.output_tokens);
|
|
226
|
+
|
|
190
227
|
if (inputTokens === undefined || outputTokens === undefined) {
|
|
191
228
|
throw malformedResponse("has malformed usage");
|
|
192
229
|
}
|
|
230
|
+
|
|
193
231
|
return {
|
|
194
232
|
answers: normalizeAnswers(response.answers, questions),
|
|
195
233
|
model: response.model || OPENAI_DECISIONS_MODEL,
|
|
@@ -40,6 +40,7 @@ interface BunGlobal {
|
|
|
40
40
|
const runtimeSecrets = (): JevSecretsLike | undefined => {
|
|
41
41
|
// SAFETY: only Bun runtimes expose Bun.secrets; other hosts leave it absent.
|
|
42
42
|
const bun = globalThis as BunGlobal;
|
|
43
|
+
|
|
43
44
|
return bun.Bun?.secrets;
|
|
44
45
|
};
|
|
45
46
|
|
|
@@ -72,6 +73,7 @@ export const resolveJevEndpointKey = async (
|
|
|
72
73
|
deps: JevEndpointKeyStoreDeps = {}
|
|
73
74
|
): Promise<JevEndpointKeyResolution> => {
|
|
74
75
|
const secrets = deps.secrets === undefined ? runtimeSecrets() : deps.secrets;
|
|
76
|
+
|
|
75
77
|
if (secrets) {
|
|
76
78
|
try {
|
|
77
79
|
const stored = normalizeKey(
|
|
@@ -80,6 +82,7 @@ export const resolveJevEndpointKey = async (
|
|
|
80
82
|
service: JEV_ENDPOINT_KEY_SERVICE,
|
|
81
83
|
})
|
|
82
84
|
);
|
|
85
|
+
|
|
83
86
|
if (stored) {
|
|
84
87
|
return { key: stored, source: "jev-bun-secrets" };
|
|
85
88
|
}
|
|
@@ -87,12 +90,16 @@ export const resolveJevEndpointKey = async (
|
|
|
87
90
|
// An unavailable secret store falls through to the next source.
|
|
88
91
|
}
|
|
89
92
|
}
|
|
93
|
+
|
|
90
94
|
const env = deps.env ?? process.env;
|
|
91
95
|
const fromEnv = normalizeKey(env[JEV_ENDPOINT_KEY_ENV_VAR]);
|
|
96
|
+
|
|
92
97
|
if (fromEnv) {
|
|
93
98
|
return { key: fromEnv, source: "jev-env" };
|
|
94
99
|
}
|
|
100
|
+
|
|
95
101
|
const fromFile = normalizeKey((deps.readFileStore ?? readDefaultFileStore)());
|
|
102
|
+
|
|
96
103
|
return fromFile ? { key: fromFile, source: "jev-file" } : {};
|
|
97
104
|
};
|
|
98
105
|
|
|
@@ -101,10 +108,13 @@ export const writeJevEndpointKey = async (
|
|
|
101
108
|
deps: JevEndpointKeyStoreDeps = {}
|
|
102
109
|
): Promise<JevKeyStoreResult> => {
|
|
103
110
|
const normalized = normalizeKey(key);
|
|
111
|
+
|
|
104
112
|
if (!normalized) {
|
|
105
113
|
return { message: "The endpoint API key is empty.", ok: false };
|
|
106
114
|
}
|
|
115
|
+
|
|
107
116
|
const secrets = deps.secrets === undefined ? runtimeSecrets() : deps.secrets;
|
|
117
|
+
|
|
108
118
|
if (secrets) {
|
|
109
119
|
try {
|
|
110
120
|
await secrets.set({
|
|
@@ -112,6 +122,7 @@ export const writeJevEndpointKey = async (
|
|
|
112
122
|
service: JEV_ENDPOINT_KEY_SERVICE,
|
|
113
123
|
value: normalized,
|
|
114
124
|
});
|
|
125
|
+
|
|
115
126
|
return {
|
|
116
127
|
message: "Endpoint API key stored in Bun.secrets.",
|
|
117
128
|
ok: true,
|
|
@@ -123,8 +134,10 @@ export const writeJevEndpointKey = async (
|
|
|
123
134
|
};
|
|
124
135
|
}
|
|
125
136
|
}
|
|
137
|
+
|
|
126
138
|
try {
|
|
127
139
|
(deps.writeFileStore ?? writeDefaultFileStore)(normalized);
|
|
140
|
+
|
|
128
141
|
return {
|
|
129
142
|
message: "Endpoint API key stored in the Pi agent directory (mode 0600).",
|
|
130
143
|
ok: true,
|
|
@@ -143,6 +156,7 @@ export const clearJevEndpointKey = async (
|
|
|
143
156
|
let clearedSomething = false;
|
|
144
157
|
let firstError: string | undefined;
|
|
145
158
|
const secrets = deps.secrets === undefined ? runtimeSecrets() : deps.secrets;
|
|
159
|
+
|
|
146
160
|
if (secrets) {
|
|
147
161
|
try {
|
|
148
162
|
await secrets.delete({
|
|
@@ -154,22 +168,26 @@ export const clearJevEndpointKey = async (
|
|
|
154
168
|
firstError = messageOf(error);
|
|
155
169
|
}
|
|
156
170
|
}
|
|
171
|
+
|
|
157
172
|
try {
|
|
158
173
|
if (deps.deleteFileStore) {
|
|
159
174
|
deps.deleteFileStore();
|
|
160
175
|
} else if (existsSync(keyFilePath())) {
|
|
161
176
|
deleteDefaultFileStore();
|
|
162
177
|
}
|
|
178
|
+
|
|
163
179
|
clearedSomething = true;
|
|
164
180
|
} catch (error) {
|
|
165
181
|
firstError ??= messageOf(error);
|
|
166
182
|
}
|
|
183
|
+
|
|
167
184
|
if (firstError) {
|
|
168
185
|
return {
|
|
169
186
|
message: `Clearing the stored endpoint API key failed: ${firstError}.`,
|
|
170
187
|
ok: false,
|
|
171
188
|
};
|
|
172
189
|
}
|
|
190
|
+
|
|
173
191
|
return {
|
|
174
192
|
message: `Stored endpoint API key cleared.${clearedSomething ? "" : ` Nothing was stored; unset ${JEV_ENDPOINT_KEY_ENV_VAR} yourself if you use it.`}`,
|
|
175
193
|
ok: true,
|