@mcowger/pi-plexus 1.14.0 → 1.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -41
- package/dist/extension.js +966 -1007
- package/package.json +2 -7
- package/context-policy.md +0 -64
- package/models-control.md +0 -51
- package/policy-control.md +0 -97
- package/service-tiers.md +0 -57
- package/short-names.md +0 -59
package/dist/extension.js
CHANGED
|
@@ -558,8 +558,148 @@ function getSuppressedModels() {
|
|
|
558
558
|
return [...envSuppressed, ...configSuppressed];
|
|
559
559
|
}
|
|
560
560
|
|
|
561
|
-
// src/
|
|
562
|
-
import {
|
|
561
|
+
// src/log.ts
|
|
562
|
+
import { appendFile, mkdir as mkdir2 } from "fs/promises";
|
|
563
|
+
import { join as join3 } from "path";
|
|
564
|
+
import { getAgentDir as getAgentDir3 } from "@earendil-works/pi-coding-agent";
|
|
565
|
+
var getCacheDir = () => join3(getAgentDir3(), "extensions", "plexus");
|
|
566
|
+
var getLogPath = () => join3(getCacheDir(), "plexus.log");
|
|
567
|
+
var dirEnsured = false;
|
|
568
|
+
function log(message, data) {
|
|
569
|
+
writeLogLine(message, data);
|
|
570
|
+
}
|
|
571
|
+
async function writeLogLine(message, data) {
|
|
572
|
+
try {
|
|
573
|
+
if (!dirEnsured) {
|
|
574
|
+
await mkdir2(getCacheDir(), { recursive: true });
|
|
575
|
+
dirEnsured = true;
|
|
576
|
+
}
|
|
577
|
+
const ts = new Date().toISOString();
|
|
578
|
+
const line = data !== undefined ? `${ts} ${message} ${JSON.stringify(data)}
|
|
579
|
+
` : `${ts} ${message}
|
|
580
|
+
`;
|
|
581
|
+
await appendFile(getLogPath(), line, "utf8");
|
|
582
|
+
} catch {}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// src/gemini-malformed-retry.ts
|
|
586
|
+
var MALFORMED_LEAK_PATTERN = /(?:print\()?call:\s*default_api[.:]|default_api\.\w+\s*\(/;
|
|
587
|
+
var MALFORMED_DIAGNOSTIC_PATTERN = /\bmalformed[\s_-]?function[\s_-]?call\b/i;
|
|
588
|
+
var TRUNCATED_JSON_PATTERN = /\bUnexpected end of JSON input\b/i;
|
|
589
|
+
var PROVIDER_CONNECTION_CLOSED_PATTERN = /\bprovider connection closed\b/i;
|
|
590
|
+
var NORMALIZED_PREFIX = "MALFORMED_FUNCTION_CALL:";
|
|
591
|
+
var TRUNCATED_JSON_PREFIX = "TRUNCATED_JSON_RESPONSE:";
|
|
592
|
+
var PROVIDER_CONNECTION_CLOSED_PREFIX = "PROVIDER_CONNECTION_CLOSED:";
|
|
593
|
+
var NORMALIZED_MESSAGE = `${NORMALIZED_PREFIX} Gemini emitted a malformed tool call (its internal ` + `function-call syntax leaked as text). This is a transient model failure \u2014 ` + `please retry your request.`;
|
|
594
|
+
var TRUNCATED_JSON_MESSAGE = `${TRUNCATED_JSON_PREFIX} Unexpected end of JSON input. The upstream provider ` + `returned a truncated JSON response. This is a transient provider failure \u2014 ` + `please retry your request.`;
|
|
595
|
+
var PROVIDER_CONNECTION_CLOSED_MESSAGE = `${PROVIDER_CONNECTION_CLOSED_PREFIX} Provider connection closed. The upstream provider ` + `dropped the request. This is a transient provider failure \u2014 please retry your request.`;
|
|
596
|
+
function hasLeakedFunctionCall(content) {
|
|
597
|
+
if (!Array.isArray(content))
|
|
598
|
+
return false;
|
|
599
|
+
for (const block of content) {
|
|
600
|
+
if (block?.type === "text" && typeof block.text === "string" && MALFORMED_LEAK_PATTERN.test(block.text)) {
|
|
601
|
+
return true;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return false;
|
|
605
|
+
}
|
|
606
|
+
function hasToolCall(content) {
|
|
607
|
+
return Array.isArray(content) && content.some((block) => block?.type === "toolCall");
|
|
608
|
+
}
|
|
609
|
+
function normalizeMalformedFunctionCall(message, providerName) {
|
|
610
|
+
if (message?.role !== "assistant" || message.provider !== providerName || message.stopReason !== "error") {
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
if (typeof message.errorMessage === "string" && (message.errorMessage.startsWith(NORMALIZED_PREFIX) || message.errorMessage.startsWith(TRUNCATED_JSON_PREFIX) || message.errorMessage.startsWith(PROVIDER_CONNECTION_CLOSED_PREFIX))) {
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
if (hasToolCall(message.content))
|
|
617
|
+
return;
|
|
618
|
+
if (typeof message.errorMessage === "string" && TRUNCATED_JSON_PATTERN.test(message.errorMessage)) {
|
|
619
|
+
log("retryable-error: retagged truncated JSON response for retry", {
|
|
620
|
+
model: message.model
|
|
621
|
+
});
|
|
622
|
+
return { message: { ...message, errorMessage: TRUNCATED_JSON_MESSAGE } };
|
|
623
|
+
}
|
|
624
|
+
if (typeof message.errorMessage === "string" && PROVIDER_CONNECTION_CLOSED_PATTERN.test(message.errorMessage)) {
|
|
625
|
+
log("retryable-error: retagged closed provider connection for retry", {
|
|
626
|
+
model: message.model
|
|
627
|
+
});
|
|
628
|
+
return {
|
|
629
|
+
message: { ...message, errorMessage: PROVIDER_CONNECTION_CLOSED_MESSAGE }
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
const via = hasLeakedFunctionCall(message.content) ? "leak" : typeof message.errorMessage === "string" && MALFORMED_DIAGNOSTIC_PATTERN.test(message.errorMessage) ? "diagnostic" : undefined;
|
|
633
|
+
if (!via)
|
|
634
|
+
return;
|
|
635
|
+
log("gemini-malformed-retry: retagged MALFORMED_FUNCTION_CALL for retry", {
|
|
636
|
+
model: message.model,
|
|
637
|
+
via
|
|
638
|
+
});
|
|
639
|
+
return { message: { ...message, errorMessage: NORMALIZED_MESSAGE } };
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// src/mapper.ts
|
|
643
|
+
import {
|
|
644
|
+
getModel
|
|
645
|
+
} from "@earendil-works/pi-ai/compat";
|
|
646
|
+
var MINIMUM_OUTPUT_TOKENS = 8192;
|
|
647
|
+
function descriptorToPiModel(descriptor) {
|
|
648
|
+
let builtinModel;
|
|
649
|
+
if (descriptor.piProvider && descriptor.piModel) {
|
|
650
|
+
try {
|
|
651
|
+
builtinModel = getModel(descriptor.piProvider, descriptor.piModel);
|
|
652
|
+
} catch {
|
|
653
|
+
builtinModel = undefined;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
const cost = {
|
|
657
|
+
input: descriptor.cost.input * 1e6,
|
|
658
|
+
output: descriptor.cost.output * 1e6,
|
|
659
|
+
cacheRead: descriptor.cost.cacheRead * 1e6,
|
|
660
|
+
cacheWrite: descriptor.cost.cacheWrite * 1e6,
|
|
661
|
+
...descriptor.cost.tiers ? {
|
|
662
|
+
tiers: descriptor.cost.tiers.map((tier) => ({
|
|
663
|
+
inputTokensAbove: tier.inputTokensAbove,
|
|
664
|
+
input: tier.input * 1e6,
|
|
665
|
+
output: tier.output * 1e6,
|
|
666
|
+
cacheRead: tier.cacheRead * 1e6,
|
|
667
|
+
cacheWrite: tier.cacheWrite * 1e6
|
|
668
|
+
}))
|
|
669
|
+
} : {}
|
|
670
|
+
};
|
|
671
|
+
let compat;
|
|
672
|
+
if (descriptor.preferredApi === "openai-completions") {
|
|
673
|
+
const heuristic = detectOpenAICompletionsCompat(descriptor.piProvider ?? descriptor.provider, descriptor.baseUrl);
|
|
674
|
+
const builtinCompat = builtinModel?.compat;
|
|
675
|
+
const merged = {
|
|
676
|
+
...heuristic,
|
|
677
|
+
...builtinCompat ?? {},
|
|
678
|
+
...descriptor.piOptions ?? {}
|
|
679
|
+
};
|
|
680
|
+
compat = merged;
|
|
681
|
+
} else if (descriptor.piOptions) {
|
|
682
|
+
compat = descriptor.piOptions;
|
|
683
|
+
} else if (builtinModel?.compat) {
|
|
684
|
+
compat = builtinModel.compat;
|
|
685
|
+
}
|
|
686
|
+
return {
|
|
687
|
+
id: descriptor.id,
|
|
688
|
+
name: descriptor.name,
|
|
689
|
+
...descriptor.shortName !== undefined ? { plexusShortName: descriptor.shortName } : {},
|
|
690
|
+
api: descriptor.preferredApi,
|
|
691
|
+
provider: descriptor.provider,
|
|
692
|
+
baseUrl: descriptor.baseUrl,
|
|
693
|
+
reasoning: descriptor.reasoning,
|
|
694
|
+
input: descriptor.input,
|
|
695
|
+
cost,
|
|
696
|
+
contextWindow: descriptor.contextWindow,
|
|
697
|
+
maxTokens: Math.max(descriptor.maxTokens, MINIMUM_OUTPUT_TOKENS),
|
|
698
|
+
...builtinModel?.thinkingLevelMap !== undefined ? { thinkingLevelMap: builtinModel.thinkingLevelMap } : {},
|
|
699
|
+
...builtinModel?.headers !== undefined ? { headers: builtinModel.headers } : {},
|
|
700
|
+
...compat !== undefined ? { compat } : {}
|
|
701
|
+
};
|
|
702
|
+
}
|
|
563
703
|
|
|
564
704
|
// ../../node_modules/.bun/zod@4.6.5/node_modules/zod/v4/core/util.js
|
|
565
705
|
function getEnumValues(entries) {
|
|
@@ -5769,6 +5909,14 @@ function object(shape, params) {
|
|
|
5769
5909
|
};
|
|
5770
5910
|
return new ZodObject(def);
|
|
5771
5911
|
}
|
|
5912
|
+
function strictObject(shape, params) {
|
|
5913
|
+
return new ZodObject({
|
|
5914
|
+
type: "object",
|
|
5915
|
+
shape,
|
|
5916
|
+
catchall: never(),
|
|
5917
|
+
...normalizeParams(params)
|
|
5918
|
+
});
|
|
5919
|
+
}
|
|
5772
5920
|
var ZodUnion = /* @__PURE__ */ $constructor("ZodUnion", (inst, def) => {
|
|
5773
5921
|
$ZodUnion.init(inst, def);
|
|
5774
5922
|
ZodType.init(inst, def);
|
|
@@ -6032,435 +6180,797 @@ function refine(fn, _params = {}) {
|
|
|
6032
6180
|
function superRefine(fn, params) {
|
|
6033
6181
|
return _superRefine(fn, params);
|
|
6034
6182
|
}
|
|
6035
|
-
// src/
|
|
6036
|
-
var
|
|
6037
|
-
var
|
|
6038
|
-
var
|
|
6039
|
-
|
|
6040
|
-
|
|
6041
|
-
|
|
6042
|
-
|
|
6043
|
-
|
|
6044
|
-
|
|
6045
|
-
var
|
|
6046
|
-
var
|
|
6047
|
-
|
|
6048
|
-
|
|
6049
|
-
|
|
6050
|
-
|
|
6051
|
-
|
|
6052
|
-
|
|
6053
|
-
|
|
6054
|
-
|
|
6055
|
-
|
|
6056
|
-
|
|
6057
|
-
|
|
6058
|
-
|
|
6059
|
-
|
|
6060
|
-
|
|
6061
|
-
|
|
6062
|
-
|
|
6063
|
-
|
|
6064
|
-
|
|
6065
|
-
|
|
6183
|
+
// src/paseo-control/protocol.ts
|
|
6184
|
+
var PROTOCOL_VERSION = 1;
|
|
6185
|
+
var NOTIFY_PREFIX = "plexuspi:v1:";
|
|
6186
|
+
var STATE_NOTIFY_PREFIX = "plexuspi:state:v1:";
|
|
6187
|
+
var CHILD_NOTIFY_PREFIX = "plexuspi:child:v1:";
|
|
6188
|
+
var COMMAND_NAME = "plexuspi-control";
|
|
6189
|
+
var CUSTOM_ENTRY_TYPE = "plexuspi-controls";
|
|
6190
|
+
var REWIND_ENTRY_TYPE = "plexuspi-rewind";
|
|
6191
|
+
var SUBAGENT_BRIDGE_CHANNEL = "plexuspi:subagent:v1";
|
|
6192
|
+
var SUBAGENT_BRIDGE_VERSION = 1;
|
|
6193
|
+
var ROOT_SIGNAL_CHANNEL = "plexuspi:root:v1";
|
|
6194
|
+
var ROOT_SIGNAL_VERSION = 1;
|
|
6195
|
+
var SESSION_KEY_ENV = "PLEXUSPI_SESSION_KEY";
|
|
6196
|
+
var TierSchema = string2().min(1).max(100);
|
|
6197
|
+
var NO_TIER = "default";
|
|
6198
|
+
var SettingsSchema = strictObject({
|
|
6199
|
+
tier: TierSchema,
|
|
6200
|
+
longContext: boolean2()
|
|
6201
|
+
});
|
|
6202
|
+
var OperationSchema = _enum([
|
|
6203
|
+
"hello",
|
|
6204
|
+
"get-state",
|
|
6205
|
+
"configure",
|
|
6206
|
+
"rewind",
|
|
6207
|
+
"reload"
|
|
6208
|
+
]);
|
|
6209
|
+
var ConfigureDataSchema = strictObject({
|
|
6210
|
+
tier: TierSchema.optional(),
|
|
6211
|
+
longContext: boolean2().optional()
|
|
6212
|
+
});
|
|
6213
|
+
var RewindDataSchema = strictObject({
|
|
6214
|
+
targetEntryId: string2().min(1)
|
|
6215
|
+
});
|
|
6216
|
+
var RewindResultSchema = strictObject({
|
|
6217
|
+
cancelled: boolean2(),
|
|
6218
|
+
targetEntryId: string2().min(1),
|
|
6219
|
+
leafId: string2().optional(),
|
|
6220
|
+
settings: SettingsSchema,
|
|
6221
|
+
activeChildren: number2().int().nonnegative()
|
|
6222
|
+
});
|
|
6223
|
+
var RewindEntrySchema = strictObject({
|
|
6224
|
+
version: literal(PROTOCOL_VERSION),
|
|
6225
|
+
targetEntryId: string2().min(1),
|
|
6226
|
+
leafId: string2().nullable().optional(),
|
|
6227
|
+
timestamp: number2().int().nonnegative()
|
|
6228
|
+
});
|
|
6229
|
+
var SubagentBridgeRecordSchema = strictObject({
|
|
6230
|
+
version: literal(SUBAGENT_BRIDGE_VERSION),
|
|
6231
|
+
runId: string2().min(1),
|
|
6232
|
+
sequence: number2().int().positive(),
|
|
6233
|
+
type: _enum(["created", "activity", "terminal"]),
|
|
6234
|
+
parentRunId: string2().optional(),
|
|
6235
|
+
parentSessionId: string2().optional(),
|
|
6236
|
+
childSessionId: string2().optional(),
|
|
6237
|
+
toolCallId: string2().optional(),
|
|
6238
|
+
title: string2().optional(),
|
|
6239
|
+
timestamp: number2().optional(),
|
|
6240
|
+
transcriptPath: string2().optional(),
|
|
6241
|
+
cwd: string2().optional(),
|
|
6242
|
+
record: unknown().optional(),
|
|
6243
|
+
outcome: _enum(["completed", "steered", "canceled", "failed"]).optional(),
|
|
6244
|
+
result: unknown().optional(),
|
|
6245
|
+
reason: string2().optional(),
|
|
6246
|
+
omitted: boolean2().optional(),
|
|
6247
|
+
omittedBytes: number2().int().nonnegative().optional()
|
|
6248
|
+
});
|
|
6249
|
+
var ChildForwardSchema = strictObject({
|
|
6250
|
+
version: literal(SUBAGENT_BRIDGE_VERSION),
|
|
6251
|
+
sessionKey: string2(),
|
|
6252
|
+
record: SubagentBridgeRecordSchema
|
|
6253
|
+
});
|
|
6254
|
+
var PlexuspiRequestSchema = object({
|
|
6255
|
+
version: literal(PROTOCOL_VERSION),
|
|
6256
|
+
sessionKey: string2().min(1),
|
|
6257
|
+
requestId: string2().min(1),
|
|
6258
|
+
operation: OperationSchema,
|
|
6259
|
+
data: unknown().optional()
|
|
6260
|
+
});
|
|
6261
|
+
var ConflictSchema = strictObject({
|
|
6262
|
+
owner: string2().min(1),
|
|
6263
|
+
command: string2().min(1),
|
|
6264
|
+
resolution: string2().min(1)
|
|
6265
|
+
});
|
|
6266
|
+
var PlexuspiStateSchema = strictObject({
|
|
6267
|
+
capabilities: array(string2()),
|
|
6268
|
+
settings: SettingsSchema,
|
|
6269
|
+
contextWindow: number2().int().nonnegative().optional(),
|
|
6270
|
+
activeChildren: number2().int().nonnegative().optional(),
|
|
6271
|
+
sessionId: string2().optional(),
|
|
6272
|
+
origin: literal("root"),
|
|
6273
|
+
tiers: array(TierSchema),
|
|
6274
|
+
longContextTarget: number2().int().nonnegative().optional(),
|
|
6275
|
+
longContextAvailable: boolean2().optional(),
|
|
6276
|
+
shortContextBudgetTokens: number2().int().positive().optional(),
|
|
6277
|
+
pricingThresholdInputTokens: number2().int().positive().optional(),
|
|
6278
|
+
contextPolicyError: string2().optional(),
|
|
6279
|
+
modelBaselineContextWindow: number2().int().nonnegative().optional(),
|
|
6280
|
+
tierApplicable: boolean2(),
|
|
6281
|
+
conflicts: array(ConflictSchema),
|
|
6282
|
+
limitations: array(string2())
|
|
6283
|
+
});
|
|
6284
|
+
var PlexuspiReplySchema = strictObject({
|
|
6285
|
+
version: literal(PROTOCOL_VERSION),
|
|
6286
|
+
sessionKey: string2(),
|
|
6287
|
+
requestId: string2(),
|
|
6288
|
+
operation: OperationSchema,
|
|
6289
|
+
ok: boolean2(),
|
|
6290
|
+
data: unknown().optional(),
|
|
6291
|
+
error: string2().optional()
|
|
6292
|
+
});
|
|
6293
|
+
var ControlEntrySchema = strictObject({
|
|
6294
|
+
version: literal(PROTOCOL_VERSION),
|
|
6295
|
+
tier: TierSchema,
|
|
6296
|
+
longContext: boolean2(),
|
|
6297
|
+
contextWindow: number2().int().nonnegative().optional(),
|
|
6298
|
+
timestamp: number2().int().nonnegative()
|
|
6299
|
+
});
|
|
6300
|
+
function formatIssues(error) {
|
|
6301
|
+
return error.issues.map((issue) => `${issue.path.length > 0 ? `${issue.path.join(".")}: ` : ""}${issue.message}`).join("; ");
|
|
6302
|
+
}
|
|
6303
|
+
function decodeBase64Url(value) {
|
|
6304
|
+
return Buffer.from(value, "base64url").toString("utf8");
|
|
6305
|
+
}
|
|
6306
|
+
function parseRequestArg(args) {
|
|
6307
|
+
const fail = (error) => ({
|
|
6308
|
+
ok: false,
|
|
6309
|
+
error,
|
|
6310
|
+
sessionKey: "",
|
|
6311
|
+
requestId: "",
|
|
6312
|
+
operation: "hello"
|
|
6313
|
+
});
|
|
6314
|
+
const trimmed = args.trim();
|
|
6315
|
+
if (!trimmed)
|
|
6316
|
+
return fail("missing base64url-encoded request");
|
|
6317
|
+
let json;
|
|
6318
|
+
try {
|
|
6319
|
+
json = decodeBase64Url(trimmed);
|
|
6320
|
+
} catch {
|
|
6321
|
+
return fail("invalid base64url request");
|
|
6066
6322
|
}
|
|
6067
|
-
if (
|
|
6068
|
-
|
|
6069
|
-
|
|
6070
|
-
|
|
6071
|
-
|
|
6323
|
+
if (json.trim().length === 0)
|
|
6324
|
+
return fail("empty request payload");
|
|
6325
|
+
let parsed;
|
|
6326
|
+
try {
|
|
6327
|
+
parsed = JSON.parse(json);
|
|
6328
|
+
} catch {
|
|
6329
|
+
return fail("request payload is not valid JSON");
|
|
6072
6330
|
}
|
|
6073
|
-
|
|
6074
|
-
|
|
6075
|
-
|
|
6331
|
+
const result = PlexuspiRequestSchema.safeParse(parsed);
|
|
6332
|
+
if (result.success)
|
|
6333
|
+
return { ok: true, request: result.data };
|
|
6334
|
+
const loose = parsed ?? {};
|
|
6335
|
+
const operation = OperationSchema.safeParse(loose.operation);
|
|
6336
|
+
return {
|
|
6337
|
+
ok: false,
|
|
6338
|
+
error: `invalid request: ${formatIssues(result.error)}`,
|
|
6339
|
+
sessionKey: typeof loose.sessionKey === "string" ? loose.sessionKey : "",
|
|
6340
|
+
requestId: typeof loose.requestId === "string" ? loose.requestId : "",
|
|
6341
|
+
operation: operation.success ? operation.data : "hello"
|
|
6342
|
+
};
|
|
6343
|
+
}
|
|
6076
6344
|
|
|
6077
|
-
|
|
6078
|
-
|
|
6079
|
-
|
|
6080
|
-
|
|
6081
|
-
|
|
6082
|
-
|
|
6083
|
-
|
|
6084
|
-
|
|
6085
|
-
|
|
6086
|
-
|
|
6087
|
-
|
|
6088
|
-
|
|
6089
|
-
|
|
6090
|
-
|
|
6091
|
-
|
|
6092
|
-
|
|
6093
|
-
|
|
6094
|
-
|
|
6095
|
-
|
|
6096
|
-
|
|
6097
|
-
|
|
6098
|
-
|
|
6099
|
-
|
|
6100
|
-
|
|
6101
|
-
|
|
6102
|
-
|
|
6103
|
-
|
|
6104
|
-
|
|
6105
|
-
|
|
6106
|
-
|
|
6107
|
-
|
|
6108
|
-
|
|
6109
|
-
|
|
6110
|
-
|
|
6111
|
-
|
|
6112
|
-
|
|
6113
|
-
|
|
6114
|
-
|
|
6115
|
-
|
|
6116
|
-
|
|
6117
|
-
|
|
6118
|
-
};
|
|
6119
|
-
if (oversized)
|
|
6120
|
-
next = {
|
|
6121
|
-
...next,
|
|
6122
|
-
status: "unavailable",
|
|
6123
|
-
policies: [],
|
|
6124
|
-
reason: "Context policy snapshot exceeds the 1 MiB publication limit."
|
|
6125
|
-
};
|
|
6126
|
-
if (status !== "ready" && policies.length > 0)
|
|
6127
|
-
next = {
|
|
6128
|
-
...next,
|
|
6129
|
-
status: "unavailable",
|
|
6130
|
-
policies: [],
|
|
6131
|
-
reason: "Context policy metadata is unavailable."
|
|
6132
|
-
};
|
|
6133
|
-
const { revision: _revision, ...stateWithoutRevision } = this.state;
|
|
6134
|
-
const { revision: _candidateRevision, ...nextWithoutRevision } = next;
|
|
6135
|
-
if (JSON.stringify(stateWithoutRevision) === JSON.stringify(nextWithoutRevision))
|
|
6136
|
-
return false;
|
|
6137
|
-
this.revision++;
|
|
6138
|
-
this.state = Object.freeze({
|
|
6139
|
-
...next,
|
|
6140
|
-
revision: this.revision,
|
|
6141
|
-
policies: Object.freeze([...next.policies])
|
|
6142
|
-
});
|
|
6143
|
-
this.send();
|
|
6144
|
-
return true;
|
|
6345
|
+
// src/paseo-control/bridge.ts
|
|
6346
|
+
function createSubagentBridgeTracker(sessionKey) {
|
|
6347
|
+
const runs = new Map;
|
|
6348
|
+
return {
|
|
6349
|
+
accept(record) {
|
|
6350
|
+
const existing = runs.get(record.runId);
|
|
6351
|
+
if (existing) {
|
|
6352
|
+
if (existing.terminal)
|
|
6353
|
+
return;
|
|
6354
|
+
if (record.sequence <= existing.sequence)
|
|
6355
|
+
return;
|
|
6356
|
+
existing.sequence = record.sequence;
|
|
6357
|
+
if (record.type === "terminal")
|
|
6358
|
+
existing.terminal = true;
|
|
6359
|
+
} else {
|
|
6360
|
+
if (record.type !== "created")
|
|
6361
|
+
return;
|
|
6362
|
+
runs.set(record.runId, { sequence: record.sequence, terminal: false });
|
|
6363
|
+
}
|
|
6364
|
+
return { version: SUBAGENT_BRIDGE_VERSION, sessionKey, record };
|
|
6365
|
+
},
|
|
6366
|
+
activeChildren() {
|
|
6367
|
+
let count = 0;
|
|
6368
|
+
for (const run of runs.values()) {
|
|
6369
|
+
if (!run.terminal)
|
|
6370
|
+
count += 1;
|
|
6371
|
+
}
|
|
6372
|
+
return count;
|
|
6373
|
+
}
|
|
6374
|
+
};
|
|
6375
|
+
}
|
|
6376
|
+
function formatChildForward(forward) {
|
|
6377
|
+
return `${CHILD_NOTIFY_PREFIX}${JSON.stringify(forward)}`;
|
|
6378
|
+
}
|
|
6379
|
+
|
|
6380
|
+
// src/paseo-control/conflicts.ts
|
|
6381
|
+
var KNOWN_CONTROL_OWNERS = [
|
|
6382
|
+
{
|
|
6383
|
+
owner: "pi-microgpt",
|
|
6384
|
+
commands: ["fast", "flex", "long-context"],
|
|
6385
|
+
resolution: "Disable pi-microgpt's /fast, /flex, and /long-context commands for this Plexus root so plexus-pi owns tier and context selection."
|
|
6145
6386
|
}
|
|
6146
|
-
|
|
6147
|
-
|
|
6148
|
-
|
|
6149
|
-
|
|
6150
|
-
|
|
6151
|
-
|
|
6152
|
-
if (
|
|
6153
|
-
|
|
6154
|
-
|
|
6155
|
-
|
|
6156
|
-
|
|
6157
|
-
status: "unavailable",
|
|
6158
|
-
policies: [],
|
|
6159
|
-
reason: "Context policy snapshot exceeds publication limits or is invalid.",
|
|
6160
|
-
...requestId === undefined ? {} : { requestId }
|
|
6387
|
+
];
|
|
6388
|
+
function detectControlConflicts(commands) {
|
|
6389
|
+
const declared = new Set(commands.filter((command) => command.source === undefined || command.source === "extension").map((command) => command.name));
|
|
6390
|
+
const conflicts = [];
|
|
6391
|
+
for (const known of KNOWN_CONTROL_OWNERS) {
|
|
6392
|
+
const matched = known.commands.filter((command) => declared.has(command));
|
|
6393
|
+
if (matched.length > 0) {
|
|
6394
|
+
conflicts.push({
|
|
6395
|
+
owner: known.owner,
|
|
6396
|
+
command: matched.join(", "),
|
|
6397
|
+
resolution: known.resolution
|
|
6161
6398
|
});
|
|
6162
6399
|
}
|
|
6163
|
-
if (valid.success)
|
|
6164
|
-
this.emit(CONTEXT_POLICY_SNAPSHOT_CHANNEL, Object.freeze(valid.data));
|
|
6165
|
-
}
|
|
6166
|
-
dispose() {
|
|
6167
|
-
this.unsubscribe();
|
|
6168
6400
|
}
|
|
6401
|
+
return conflicts;
|
|
6169
6402
|
}
|
|
6170
|
-
var CONTEXT_POLICY_METADATA_UNAVAILABLE_REASON = SAFE_REASON;
|
|
6171
6403
|
|
|
6172
|
-
// src/
|
|
6173
|
-
|
|
6174
|
-
|
|
6175
|
-
|
|
6176
|
-
|
|
6177
|
-
|
|
6178
|
-
|
|
6179
|
-
|
|
6180
|
-
|
|
6404
|
+
// src/paseo-control/rewind.ts
|
|
6405
|
+
function findActiveBranchUserEntry(branch, targetEntryId) {
|
|
6406
|
+
const entry = branch.find((candidate) => candidate.id === targetEntryId);
|
|
6407
|
+
if (!entry)
|
|
6408
|
+
return { ok: false, error: "rewind target is not on the active branch" };
|
|
6409
|
+
if (entry.type !== "message" || entry.message?.role !== "user") {
|
|
6410
|
+
return {
|
|
6411
|
+
ok: false,
|
|
6412
|
+
error: "rewind target must be an active-branch user message"
|
|
6413
|
+
};
|
|
6414
|
+
}
|
|
6415
|
+
return { ok: true, entry };
|
|
6181
6416
|
}
|
|
6182
|
-
|
|
6183
|
-
|
|
6184
|
-
|
|
6185
|
-
|
|
6186
|
-
|
|
6187
|
-
|
|
6188
|
-
|
|
6189
|
-
const line = data !== undefined ? `${ts} ${message} ${JSON.stringify(data)}
|
|
6190
|
-
` : `${ts} ${message}
|
|
6191
|
-
`;
|
|
6192
|
-
await appendFile(getLogPath(), line, "utf8");
|
|
6193
|
-
} catch {}
|
|
6417
|
+
function buildRewindEntry(targetEntryId, leafId, timestamp) {
|
|
6418
|
+
return {
|
|
6419
|
+
version: PROTOCOL_VERSION,
|
|
6420
|
+
targetEntryId,
|
|
6421
|
+
...leafId !== null ? { leafId } : {},
|
|
6422
|
+
timestamp
|
|
6423
|
+
};
|
|
6194
6424
|
}
|
|
6195
|
-
|
|
6196
|
-
|
|
6197
|
-
var MALFORMED_LEAK_PATTERN = /(?:print\()?call:\s*default_api[.:]|default_api\.\w+\s*\(/;
|
|
6198
|
-
var MALFORMED_DIAGNOSTIC_PATTERN = /\bmalformed[\s_-]?function[\s_-]?call\b/i;
|
|
6199
|
-
var TRUNCATED_JSON_PATTERN = /\bUnexpected end of JSON input\b/i;
|
|
6200
|
-
var PROVIDER_CONNECTION_CLOSED_PATTERN = /\bprovider connection closed\b/i;
|
|
6201
|
-
var NORMALIZED_PREFIX = "MALFORMED_FUNCTION_CALL:";
|
|
6202
|
-
var TRUNCATED_JSON_PREFIX = "TRUNCATED_JSON_RESPONSE:";
|
|
6203
|
-
var PROVIDER_CONNECTION_CLOSED_PREFIX = "PROVIDER_CONNECTION_CLOSED:";
|
|
6204
|
-
var NORMALIZED_MESSAGE = `${NORMALIZED_PREFIX} Gemini emitted a malformed tool call (its internal ` + `function-call syntax leaked as text). This is a transient model failure \u2014 ` + `please retry your request.`;
|
|
6205
|
-
var TRUNCATED_JSON_MESSAGE = `${TRUNCATED_JSON_PREFIX} Unexpected end of JSON input. The upstream provider ` + `returned a truncated JSON response. This is a transient provider failure \u2014 ` + `please retry your request.`;
|
|
6206
|
-
var PROVIDER_CONNECTION_CLOSED_MESSAGE = `${PROVIDER_CONNECTION_CLOSED_PREFIX} Provider connection closed. The upstream provider ` + `dropped the request. This is a transient provider failure \u2014 please retry your request.`;
|
|
6207
|
-
function hasLeakedFunctionCall(content) {
|
|
6208
|
-
if (!Array.isArray(content))
|
|
6209
|
-
return false;
|
|
6210
|
-
for (const block of content) {
|
|
6211
|
-
if (block?.type === "text" && typeof block.text === "string" && MALFORMED_LEAK_PATTERN.test(block.text)) {
|
|
6212
|
-
return true;
|
|
6213
|
-
}
|
|
6214
|
-
}
|
|
6215
|
-
return false;
|
|
6425
|
+
function navigationCancelled(result) {
|
|
6426
|
+
return Boolean(result && typeof result === "object" && result.cancelled === true);
|
|
6216
6427
|
}
|
|
6217
|
-
|
|
6218
|
-
|
|
6428
|
+
|
|
6429
|
+
// src/paseo-control/tier.ts
|
|
6430
|
+
function resolveSavedTier(tier, choices) {
|
|
6431
|
+
if (choices.includes(tier))
|
|
6432
|
+
return tier;
|
|
6433
|
+
const migrated = tier === "fast" ? "priority" : tier === "default" ? "auto" : tier;
|
|
6434
|
+
if (choices.includes(migrated))
|
|
6435
|
+
return migrated;
|
|
6436
|
+
if (choices.includes("auto"))
|
|
6437
|
+
return "auto";
|
|
6438
|
+
if (choices.includes("standard"))
|
|
6439
|
+
return "standard";
|
|
6440
|
+
return NO_TIER;
|
|
6441
|
+
}
|
|
6442
|
+
function isTierApplicable(model, tier, policy) {
|
|
6443
|
+
return model?.provider === "plexus" && policy?.provider === model.provider && policy.modelId === model.id && policy.serviceTiers.includes(tier);
|
|
6219
6444
|
}
|
|
6220
|
-
|
|
6221
|
-
|
|
6445
|
+
|
|
6446
|
+
// src/paseo-control/control.ts
|
|
6447
|
+
var CAPABILITIES = [
|
|
6448
|
+
"hello",
|
|
6449
|
+
"get-state",
|
|
6450
|
+
"configure",
|
|
6451
|
+
"rewind",
|
|
6452
|
+
"reload"
|
|
6453
|
+
];
|
|
6454
|
+
var LIMITATIONS = [
|
|
6455
|
+
"rewind restores Pi's active branch and appends a branch pin; it never writes files and never summarizes the abandoned future",
|
|
6456
|
+
"rewind is rejected while the root agent is busy, while a compaction is in progress, or while owned children are active",
|
|
6457
|
+
"the provider must clear its own queued input before asking the module to navigate",
|
|
6458
|
+
"reload is accepted while idle and the ok reply is emitted before reloading, so ok means started rather than finished; the provider confirms completion by re-reading state and commands",
|
|
6459
|
+
"reload re-reads settings, extensions, skills, prompts, themes, and context files without touching the session history; files that arrive after reload starts are not picked up",
|
|
6460
|
+
"host history replacement after rewind is the provider's supported-adapter concern, not a module-invented reset event",
|
|
6461
|
+
"active owned children are counted from the pi-subagents bridge; children not bridged are not visible",
|
|
6462
|
+
"advertised service tiers describe Plexus capabilities, not a routing or backend acceptance guarantee",
|
|
6463
|
+
"context budgets come from Plexus policy metadata; models without distinct short/max budgets have no long-context control"
|
|
6464
|
+
];
|
|
6465
|
+
var APPLY_ERROR = "Pi could not apply the context policy budget.";
|
|
6466
|
+
function normalizeKey(value) {
|
|
6467
|
+
if (typeof value !== "string")
|
|
6222
6468
|
return;
|
|
6223
|
-
|
|
6224
|
-
|
|
6469
|
+
const trimmed = value.trim();
|
|
6470
|
+
return trimmed.length > 0 ? trimmed : undefined;
|
|
6471
|
+
}
|
|
6472
|
+
function readSessionId(ctx) {
|
|
6473
|
+
try {
|
|
6474
|
+
const id = ctx.sessionManager.getSessionId();
|
|
6475
|
+
return typeof id === "string" && id.length > 0 ? id : undefined;
|
|
6476
|
+
} catch {
|
|
6225
6477
|
return;
|
|
6226
6478
|
}
|
|
6227
|
-
|
|
6479
|
+
}
|
|
6480
|
+
function readLeafId(ctx) {
|
|
6481
|
+
try {
|
|
6482
|
+
const id = ctx.sessionManager.getLeafId();
|
|
6483
|
+
return typeof id === "string" && id.length > 0 ? id : undefined;
|
|
6484
|
+
} catch {
|
|
6228
6485
|
return;
|
|
6229
|
-
if (typeof message.errorMessage === "string" && TRUNCATED_JSON_PATTERN.test(message.errorMessage)) {
|
|
6230
|
-
log("retryable-error: retagged truncated JSON response for retry", {
|
|
6231
|
-
model: message.model
|
|
6232
|
-
});
|
|
6233
|
-
return { message: { ...message, errorMessage: TRUNCATED_JSON_MESSAGE } };
|
|
6234
6486
|
}
|
|
6235
|
-
|
|
6236
|
-
|
|
6237
|
-
|
|
6238
|
-
|
|
6487
|
+
}
|
|
6488
|
+
function readBranch(ctx) {
|
|
6489
|
+
try {
|
|
6490
|
+
return ctx.sessionManager.getBranch();
|
|
6491
|
+
} catch {
|
|
6492
|
+
return [];
|
|
6493
|
+
}
|
|
6494
|
+
}
|
|
6495
|
+
function createPlexuspiControl(pi, deps, options = {}) {
|
|
6496
|
+
const { policy, host } = deps;
|
|
6497
|
+
const now = options.now ?? (() => Date.now());
|
|
6498
|
+
const expectedSessionKey = normalizeKey(options.sessionKey ?? process.env[SESSION_KEY_ENV]);
|
|
6499
|
+
const bridgeTracker = createSubagentBridgeTracker(expectedSessionKey ?? "");
|
|
6500
|
+
let active = false;
|
|
6501
|
+
let closed = false;
|
|
6502
|
+
let latestCtx;
|
|
6503
|
+
let compactionActive = false;
|
|
6504
|
+
let desiredTier = NO_TIER;
|
|
6505
|
+
let desiredLongContext = false;
|
|
6506
|
+
let resolvedTier = NO_TIER;
|
|
6507
|
+
let suspendPersist = false;
|
|
6508
|
+
let persistedKey = "";
|
|
6509
|
+
let unsubscribeBridge;
|
|
6510
|
+
let unsubscribeView;
|
|
6511
|
+
const resolveIntent = (_model, advertisement) => {
|
|
6512
|
+
const choices = advertisement?.serviceTier?.serviceTiers ?? [];
|
|
6513
|
+
resolvedTier = resolveSavedTier(desiredTier, choices);
|
|
6514
|
+
if (choices.includes(desiredTier) || desiredTier === "fast" && resolvedTier === "priority" || desiredTier === NO_TIER && (resolvedTier === "auto" || resolvedTier === "standard")) {
|
|
6515
|
+
desiredTier = resolvedTier;
|
|
6516
|
+
}
|
|
6517
|
+
const context = advertisement?.context;
|
|
6518
|
+
const distinct = context !== undefined && context.shortContextBudgetTokens < context.maxContextTokens;
|
|
6519
|
+
if (!distinct && host.hasCatalogContextPolicy())
|
|
6520
|
+
desiredLongContext = false;
|
|
6521
|
+
return { longContext: desiredLongContext, serviceTier: resolvedTier };
|
|
6522
|
+
};
|
|
6523
|
+
function reportedSettings() {
|
|
6239
6524
|
return {
|
|
6240
|
-
|
|
6525
|
+
tier: resolvedTier,
|
|
6526
|
+
longContext: policy.getView().selection.longContext
|
|
6241
6527
|
};
|
|
6242
6528
|
}
|
|
6243
|
-
|
|
6244
|
-
|
|
6245
|
-
|
|
6246
|
-
|
|
6247
|
-
|
|
6248
|
-
|
|
6249
|
-
}
|
|
6250
|
-
|
|
6251
|
-
}
|
|
6252
|
-
|
|
6253
|
-
// src/mapper.ts
|
|
6254
|
-
import {
|
|
6255
|
-
getModel
|
|
6256
|
-
} from "@earendil-works/pi-ai/compat";
|
|
6257
|
-
var MINIMUM_OUTPUT_TOKENS = 8192;
|
|
6258
|
-
function descriptorToPiModel(descriptor) {
|
|
6259
|
-
let builtinModel;
|
|
6260
|
-
if (descriptor.piProvider && descriptor.piModel) {
|
|
6529
|
+
function plexusModel(model) {
|
|
6530
|
+
return model?.provider === "plexus" && typeof model.id === "string" ? { provider: "plexus", id: model.id } : undefined;
|
|
6531
|
+
}
|
|
6532
|
+
function advertisementFor(model) {
|
|
6533
|
+
const identity = plexusModel(model);
|
|
6534
|
+
return identity ? host.getAdvertisement(identity) : undefined;
|
|
6535
|
+
}
|
|
6536
|
+
function conflicts() {
|
|
6261
6537
|
try {
|
|
6262
|
-
|
|
6538
|
+
return detectControlConflicts(pi.getCommands());
|
|
6263
6539
|
} catch {
|
|
6264
|
-
|
|
6540
|
+
return [];
|
|
6265
6541
|
}
|
|
6266
6542
|
}
|
|
6267
|
-
|
|
6268
|
-
|
|
6269
|
-
|
|
6270
|
-
|
|
6271
|
-
|
|
6272
|
-
|
|
6273
|
-
|
|
6274
|
-
|
|
6275
|
-
|
|
6276
|
-
|
|
6277
|
-
|
|
6278
|
-
|
|
6279
|
-
|
|
6280
|
-
|
|
6281
|
-
|
|
6282
|
-
|
|
6283
|
-
if (descriptor.preferredApi === "openai-completions") {
|
|
6284
|
-
const heuristic = detectOpenAICompletionsCompat(descriptor.piProvider ?? descriptor.provider, descriptor.baseUrl);
|
|
6285
|
-
const builtinCompat = builtinModel?.compat;
|
|
6286
|
-
const merged = {
|
|
6287
|
-
...heuristic,
|
|
6288
|
-
...builtinCompat ?? {},
|
|
6289
|
-
...descriptor.piOptions ?? {}
|
|
6543
|
+
function buildState(ctx) {
|
|
6544
|
+
const model = ctx.model;
|
|
6545
|
+
const identity = plexusModel(model);
|
|
6546
|
+
const advertisement = advertisementFor(model);
|
|
6547
|
+
const context = advertisement?.context;
|
|
6548
|
+
const settings = reportedSettings();
|
|
6549
|
+
const state = {
|
|
6550
|
+
capabilities: [...CAPABILITIES],
|
|
6551
|
+
settings,
|
|
6552
|
+
origin: "root",
|
|
6553
|
+
tiers: [...advertisement?.serviceTier?.serviceTiers ?? []],
|
|
6554
|
+
longContextAvailable: context !== undefined && context.shortContextBudgetTokens < context.maxContextTokens,
|
|
6555
|
+
tierApplicable: isTierApplicable(model, settings.tier, advertisement?.serviceTier),
|
|
6556
|
+
conflicts: conflicts(),
|
|
6557
|
+
limitations: [...LIMITATIONS],
|
|
6558
|
+
activeChildren: bridgeTracker.activeChildren()
|
|
6290
6559
|
};
|
|
6291
|
-
|
|
6292
|
-
|
|
6293
|
-
|
|
6294
|
-
|
|
6295
|
-
|
|
6560
|
+
if (typeof model?.contextWindow === "number")
|
|
6561
|
+
state.contextWindow = model.contextWindow;
|
|
6562
|
+
const sessionId = readSessionId(ctx);
|
|
6563
|
+
if (sessionId !== undefined)
|
|
6564
|
+
state.sessionId = sessionId;
|
|
6565
|
+
if (context) {
|
|
6566
|
+
state.longContextTarget = context.maxContextTokens;
|
|
6567
|
+
state.shortContextBudgetTokens = context.shortContextBudgetTokens;
|
|
6568
|
+
if (context.pricingThresholdInputTokens !== undefined)
|
|
6569
|
+
state.pricingThresholdInputTokens = context.pricingThresholdInputTokens;
|
|
6570
|
+
}
|
|
6571
|
+
const baseline = identity ? host.getCatalogContextWindow(identity) : undefined;
|
|
6572
|
+
if (baseline !== undefined)
|
|
6573
|
+
state.modelBaselineContextWindow = baseline;
|
|
6574
|
+
if (policy.getView().applyFailed)
|
|
6575
|
+
state.contextPolicyError = APPLY_ERROR;
|
|
6576
|
+
return PlexuspiStateSchema.parse(state);
|
|
6577
|
+
}
|
|
6578
|
+
function publishState(ctx) {
|
|
6579
|
+
if (!expectedSessionKey)
|
|
6580
|
+
return;
|
|
6581
|
+
ctx.ui.notify(`${STATE_NOTIFY_PREFIX}${JSON.stringify({ version: PROTOCOL_VERSION, sessionKey: expectedSessionKey, state: buildState(ctx) })}`, "info");
|
|
6296
6582
|
}
|
|
6297
|
-
|
|
6298
|
-
|
|
6299
|
-
|
|
6300
|
-
|
|
6301
|
-
|
|
6302
|
-
|
|
6303
|
-
|
|
6304
|
-
|
|
6305
|
-
|
|
6306
|
-
|
|
6307
|
-
|
|
6308
|
-
|
|
6309
|
-
|
|
6310
|
-
|
|
6311
|
-
|
|
6312
|
-
|
|
6313
|
-
}
|
|
6314
|
-
|
|
6315
|
-
// src/models-control.ts
|
|
6316
|
-
import { randomUUID as randomUUID2 } from "crypto";
|
|
6317
|
-
var MODELS_REFRESH_CHANNEL = "plexus:models:refresh:v1";
|
|
6318
|
-
var MODELS_STATE_CHANNEL = "plexus:models:state:v1";
|
|
6319
|
-
var MAX_STATE_BYTES = 1024 * 1024;
|
|
6320
|
-
var ModelsRefreshSchema = object({
|
|
6321
|
-
version: literal(1),
|
|
6322
|
-
requestId: string2().min(1).max(1024)
|
|
6323
|
-
}).strict();
|
|
6324
|
-
var ModelsStateSchema = object({
|
|
6325
|
-
version: literal(1),
|
|
6326
|
-
publisherId: string2().uuid(),
|
|
6327
|
-
revision: number2().int().positive().safe(),
|
|
6328
|
-
requestId: string2().min(1).max(1024).optional(),
|
|
6329
|
-
status: _enum(["idle", "refreshing", "ready", "error"]),
|
|
6330
|
-
modelCount: number2().int().nonnegative().safe().optional(),
|
|
6331
|
-
reason: string2().max(500).optional()
|
|
6332
|
-
}).strict();
|
|
6333
|
-
var MODELS_REFRESH_FAILED_REASON = "The Plexus catalog refresh failed.";
|
|
6334
|
-
var encoder = new TextEncoder;
|
|
6335
|
-
function byteLength(value) {
|
|
6336
|
-
return encoder.encode(JSON.stringify(value)).byteLength;
|
|
6337
|
-
}
|
|
6338
|
-
function freezeState(state) {
|
|
6339
|
-
return Object.freeze({ ...state });
|
|
6340
|
-
}
|
|
6341
|
-
function freezeParsed(state) {
|
|
6342
|
-
return Object.freeze({ ...state });
|
|
6343
|
-
}
|
|
6344
|
-
function statesEqual(current, next) {
|
|
6345
|
-
return current.status === next.status && current.modelCount === next.modelCount && current.reason === next.reason;
|
|
6346
|
-
}
|
|
6347
|
-
|
|
6348
|
-
class ModelsControl {
|
|
6349
|
-
host;
|
|
6350
|
-
publisherId = randomUUID2();
|
|
6351
|
-
revision = 1;
|
|
6352
|
-
state;
|
|
6353
|
-
inFlight;
|
|
6354
|
-
emit;
|
|
6355
|
-
unsubscribe;
|
|
6356
|
-
constructor(events, host) {
|
|
6357
|
-
this.host = host;
|
|
6358
|
-
this.emit = events.emit.bind(events);
|
|
6359
|
-
this.state = freezeState({
|
|
6360
|
-
version: 1,
|
|
6361
|
-
publisherId: this.publisherId,
|
|
6362
|
-
revision: 1,
|
|
6363
|
-
status: "idle"
|
|
6583
|
+
function emit(ctx, reply) {
|
|
6584
|
+
const validated = PlexuspiReplySchema.parse(reply);
|
|
6585
|
+
ctx.ui.notify(`${NOTIFY_PREFIX}${JSON.stringify(validated)}`, validated.ok ? "info" : "warning");
|
|
6586
|
+
}
|
|
6587
|
+
function base(request) {
|
|
6588
|
+
return {
|
|
6589
|
+
version: PROTOCOL_VERSION,
|
|
6590
|
+
sessionKey: request.sessionKey,
|
|
6591
|
+
requestId: request.requestId,
|
|
6592
|
+
operation: request.operation
|
|
6593
|
+
};
|
|
6594
|
+
}
|
|
6595
|
+
function controlKey() {
|
|
6596
|
+
return JSON.stringify({
|
|
6597
|
+
tier: desiredTier,
|
|
6598
|
+
longContext: reportedSettings().longContext
|
|
6364
6599
|
});
|
|
6365
|
-
this.unsubscribe = events.on(MODELS_REFRESH_CHANNEL, (data) => this.handleCommand(data));
|
|
6366
6600
|
}
|
|
6367
|
-
|
|
6368
|
-
|
|
6601
|
+
function persistIfChanged() {
|
|
6602
|
+
if (suspendPersist)
|
|
6603
|
+
return;
|
|
6604
|
+
const key = controlKey();
|
|
6605
|
+
if (key === persistedKey)
|
|
6606
|
+
return;
|
|
6607
|
+
persistedKey = key;
|
|
6608
|
+
const model = policy.getView().model;
|
|
6609
|
+
const baseline = model ? host.getCatalogContextWindow(model) : undefined;
|
|
6610
|
+
pi.appendEntry(CUSTOM_ENTRY_TYPE, {
|
|
6611
|
+
version: PROTOCOL_VERSION,
|
|
6612
|
+
tier: desiredTier,
|
|
6613
|
+
longContext: reportedSettings().longContext,
|
|
6614
|
+
...baseline !== undefined ? { contextWindow: baseline } : {},
|
|
6615
|
+
timestamp: now()
|
|
6616
|
+
});
|
|
6617
|
+
}
|
|
6618
|
+
function onPolicyCommit() {
|
|
6619
|
+
if (!active || closed)
|
|
6620
|
+
return;
|
|
6621
|
+
persistIfChanged();
|
|
6622
|
+
if (latestCtx)
|
|
6623
|
+
publishState(latestCtx);
|
|
6624
|
+
}
|
|
6625
|
+
function latestControlEntry(ctx) {
|
|
6626
|
+
let latest;
|
|
6627
|
+
for (const entry of readBranch(ctx)) {
|
|
6628
|
+
if (entry.type !== "custom" || entry.customType !== CUSTOM_ENTRY_TYPE)
|
|
6629
|
+
continue;
|
|
6630
|
+
const parsed = ControlEntrySchema.safeParse(entry.data);
|
|
6631
|
+
if (parsed.success)
|
|
6632
|
+
latest = parsed.data;
|
|
6633
|
+
}
|
|
6634
|
+
return latest;
|
|
6369
6635
|
}
|
|
6370
|
-
|
|
6371
|
-
|
|
6636
|
+
async function restore(ctx) {
|
|
6637
|
+
const entry = latestControlEntry(ctx);
|
|
6638
|
+
desiredTier = entry?.tier ?? NO_TIER;
|
|
6639
|
+
desiredLongContext = entry?.longContext ?? false;
|
|
6640
|
+
suspendPersist = true;
|
|
6641
|
+
try {
|
|
6642
|
+
await policy.reconcile();
|
|
6643
|
+
} finally {
|
|
6644
|
+
suspendPersist = false;
|
|
6645
|
+
}
|
|
6646
|
+
persistedKey = controlKey();
|
|
6372
6647
|
}
|
|
6373
|
-
|
|
6374
|
-
|
|
6648
|
+
function forwardBridgeRecord(data) {
|
|
6649
|
+
if (!active || closed)
|
|
6650
|
+
return;
|
|
6651
|
+
const parsed = SubagentBridgeRecordSchema.safeParse(data);
|
|
6375
6652
|
if (!parsed.success)
|
|
6376
6653
|
return;
|
|
6377
|
-
|
|
6378
|
-
|
|
6379
|
-
|
|
6380
|
-
|
|
6654
|
+
const forward = bridgeTracker.accept(parsed.data);
|
|
6655
|
+
if (!forward || !latestCtx)
|
|
6656
|
+
return;
|
|
6657
|
+
latestCtx.ui.notify(formatChildForward(forward), "info");
|
|
6658
|
+
}
|
|
6659
|
+
function emitRootSignal(ctx) {
|
|
6660
|
+
const sessionId = readSessionId(ctx);
|
|
6661
|
+
if (sessionId === undefined)
|
|
6662
|
+
return;
|
|
6663
|
+
pi.events.emit(ROOT_SIGNAL_CHANNEL, {
|
|
6664
|
+
version: ROOT_SIGNAL_VERSION,
|
|
6665
|
+
sessionId
|
|
6666
|
+
});
|
|
6667
|
+
}
|
|
6668
|
+
async function activate(ctx) {
|
|
6669
|
+
latestCtx = ctx;
|
|
6670
|
+
if (active)
|
|
6671
|
+
return;
|
|
6672
|
+
active = true;
|
|
6673
|
+
policy.setResolver(resolveIntent);
|
|
6674
|
+
unsubscribeView = policy.subscribe(onPolicyCommit);
|
|
6675
|
+
unsubscribeBridge = pi.events.on(SUBAGENT_BRIDGE_CHANNEL, forwardBridgeRecord);
|
|
6676
|
+
await restore(ctx);
|
|
6677
|
+
emitRootSignal(ctx);
|
|
6678
|
+
}
|
|
6679
|
+
function rewindResult(cancelled, targetEntryId, ctx) {
|
|
6680
|
+
const leafId = readLeafId(ctx);
|
|
6681
|
+
return {
|
|
6682
|
+
cancelled,
|
|
6683
|
+
targetEntryId,
|
|
6684
|
+
...leafId !== undefined ? { leafId } : {},
|
|
6685
|
+
settings: reportedSettings(),
|
|
6686
|
+
activeChildren: bridgeTracker.activeChildren()
|
|
6687
|
+
};
|
|
6688
|
+
}
|
|
6689
|
+
function handleHello(request, ctx) {
|
|
6690
|
+
const data = buildState(ctx);
|
|
6691
|
+
if (data.conflicts.length > 0) {
|
|
6692
|
+
const detail = data.conflicts.map((conflict) => `${conflict.owner} owns ${conflict.command}`).join("; ");
|
|
6693
|
+
const resolutions = data.conflicts.map((conflict) => conflict.resolution).join(" ");
|
|
6694
|
+
emit(ctx, {
|
|
6695
|
+
...base(request),
|
|
6696
|
+
ok: false,
|
|
6697
|
+
data,
|
|
6698
|
+
error: `known control owner conflict: ${detail}. ${resolutions}`
|
|
6699
|
+
});
|
|
6700
|
+
return;
|
|
6701
|
+
}
|
|
6702
|
+
emit(ctx, { ...base(request), ok: true, data });
|
|
6703
|
+
}
|
|
6704
|
+
async function handleConfigure(request, ctx) {
|
|
6705
|
+
const parsed = ConfigureDataSchema.safeParse(request.data ?? {});
|
|
6706
|
+
if (!parsed.success) {
|
|
6707
|
+
emit(ctx, {
|
|
6708
|
+
...base(request),
|
|
6709
|
+
ok: false,
|
|
6710
|
+
error: `invalid configure data: ${formatIssues(parsed.error)}`
|
|
6711
|
+
});
|
|
6712
|
+
return;
|
|
6713
|
+
}
|
|
6714
|
+
const advertisement = advertisementFor(ctx.model);
|
|
6715
|
+
const choices = advertisement?.serviceTier?.serviceTiers ?? [];
|
|
6716
|
+
const requestedTier = parsed.data.tier;
|
|
6717
|
+
const nextTier = requestedTier === undefined ? resolvedTier : choices.includes(requestedTier) ? requestedTier : requestedTier === "fast" ? "priority" : requestedTier === NO_TIER ? resolveSavedTier(requestedTier, choices) : requestedTier;
|
|
6718
|
+
if (requestedTier !== undefined && !choices.includes(nextTier)) {
|
|
6719
|
+
emit(ctx, {
|
|
6720
|
+
...base(request),
|
|
6721
|
+
ok: false,
|
|
6722
|
+
data: buildState(ctx),
|
|
6723
|
+
error: `Service tier '${requestedTier}' is not advertised for the current model.`
|
|
6724
|
+
});
|
|
6725
|
+
return;
|
|
6726
|
+
}
|
|
6727
|
+
const nextLongContext = parsed.data.longContext ?? reportedSettings().longContext;
|
|
6728
|
+
const context = advertisement?.context;
|
|
6729
|
+
if (nextLongContext && (!context || context.shortContextBudgetTokens >= context.maxContextTokens)) {
|
|
6730
|
+
emit(ctx, {
|
|
6731
|
+
...base(request),
|
|
6732
|
+
ok: false,
|
|
6733
|
+
data: buildState(ctx),
|
|
6734
|
+
error: "The current model has no distinct short and long context budgets."
|
|
6381
6735
|
});
|
|
6736
|
+
return;
|
|
6737
|
+
}
|
|
6738
|
+
if (parsed.data.longContext !== undefined) {
|
|
6739
|
+
const previousLongContext = desiredLongContext;
|
|
6740
|
+
desiredLongContext = nextLongContext;
|
|
6741
|
+
await policy.reconcile();
|
|
6742
|
+
if (policy.getView().applyFailed) {
|
|
6743
|
+
desiredLongContext = previousLongContext;
|
|
6744
|
+
emit(ctx, {
|
|
6745
|
+
...base(request),
|
|
6746
|
+
ok: false,
|
|
6747
|
+
data: buildState(ctx),
|
|
6748
|
+
error: APPLY_ERROR
|
|
6749
|
+
});
|
|
6750
|
+
return;
|
|
6751
|
+
}
|
|
6382
6752
|
}
|
|
6383
|
-
|
|
6384
|
-
|
|
6753
|
+
if (requestedTier !== undefined) {
|
|
6754
|
+
desiredTier = nextTier;
|
|
6755
|
+
await policy.reconcile();
|
|
6756
|
+
}
|
|
6757
|
+
emit(ctx, { ...base(request), ok: true, data: buildState(ctx) });
|
|
6385
6758
|
}
|
|
6386
|
-
async
|
|
6387
|
-
|
|
6759
|
+
async function handleRewind(request, ctx) {
|
|
6760
|
+
const parsed = RewindDataSchema.safeParse(request.data ?? {});
|
|
6761
|
+
if (!parsed.success) {
|
|
6762
|
+
emit(ctx, {
|
|
6763
|
+
...base(request),
|
|
6764
|
+
ok: false,
|
|
6765
|
+
error: `invalid rewind data: ${formatIssues(parsed.error)}`
|
|
6766
|
+
});
|
|
6767
|
+
return;
|
|
6768
|
+
}
|
|
6769
|
+
const { targetEntryId } = parsed.data;
|
|
6770
|
+
if (!ctx.isIdle()) {
|
|
6771
|
+
emit(ctx, {
|
|
6772
|
+
...base(request),
|
|
6773
|
+
ok: false,
|
|
6774
|
+
data: buildState(ctx),
|
|
6775
|
+
error: "rewind requires an idle agent"
|
|
6776
|
+
});
|
|
6777
|
+
return;
|
|
6778
|
+
}
|
|
6779
|
+
if (compactionActive) {
|
|
6780
|
+
emit(ctx, {
|
|
6781
|
+
...base(request),
|
|
6782
|
+
ok: false,
|
|
6783
|
+
data: buildState(ctx),
|
|
6784
|
+
error: "rewind is unavailable while a compaction is in progress"
|
|
6785
|
+
});
|
|
6786
|
+
return;
|
|
6787
|
+
}
|
|
6788
|
+
const activeChildren = bridgeTracker.activeChildren();
|
|
6789
|
+
if (activeChildren > 0) {
|
|
6790
|
+
emit(ctx, {
|
|
6791
|
+
...base(request),
|
|
6792
|
+
ok: false,
|
|
6793
|
+
data: buildState(ctx),
|
|
6794
|
+
error: `rewind is unavailable while ${activeChildren} owned child session(s) are active`
|
|
6795
|
+
});
|
|
6796
|
+
return;
|
|
6797
|
+
}
|
|
6798
|
+
const target = findActiveBranchUserEntry(readBranch(ctx), targetEntryId);
|
|
6799
|
+
if (!target.ok) {
|
|
6800
|
+
emit(ctx, {
|
|
6801
|
+
...base(request),
|
|
6802
|
+
ok: false,
|
|
6803
|
+
data: buildState(ctx),
|
|
6804
|
+
error: `invalid rewind target: ${target.error}`
|
|
6805
|
+
});
|
|
6806
|
+
return;
|
|
6807
|
+
}
|
|
6808
|
+
let result;
|
|
6388
6809
|
try {
|
|
6389
|
-
|
|
6390
|
-
this.commit({ status: "ready", modelCount: result.modelCount });
|
|
6810
|
+
result = await ctx.navigateTree(targetEntryId, { summarize: false });
|
|
6391
6811
|
} catch (error) {
|
|
6392
|
-
|
|
6393
|
-
|
|
6812
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
6813
|
+
emit(ctx, {
|
|
6814
|
+
...base(request),
|
|
6815
|
+
ok: false,
|
|
6816
|
+
data: buildState(ctx),
|
|
6817
|
+
error: `rewind navigation failed: ${message}`
|
|
6818
|
+
});
|
|
6819
|
+
return;
|
|
6394
6820
|
}
|
|
6395
|
-
|
|
6396
|
-
|
|
6397
|
-
|
|
6821
|
+
if (navigationCancelled(result)) {
|
|
6822
|
+
emit(ctx, {
|
|
6823
|
+
...base(request),
|
|
6824
|
+
ok: false,
|
|
6825
|
+
data: rewindResult(true, targetEntryId, ctx),
|
|
6826
|
+
error: "rewind was cancelled before navigation completed."
|
|
6827
|
+
});
|
|
6398
6828
|
return;
|
|
6399
|
-
|
|
6400
|
-
|
|
6401
|
-
|
|
6402
|
-
|
|
6403
|
-
|
|
6404
|
-
|
|
6405
|
-
...
|
|
6406
|
-
|
|
6829
|
+
}
|
|
6830
|
+
const leafId = readLeafId(ctx) ?? null;
|
|
6831
|
+
pi.appendEntry(REWIND_ENTRY_TYPE, buildRewindEntry(targetEntryId, leafId, now()));
|
|
6832
|
+
latestCtx = ctx;
|
|
6833
|
+
await restore(ctx);
|
|
6834
|
+
emit(ctx, {
|
|
6835
|
+
...base(request),
|
|
6836
|
+
ok: true,
|
|
6837
|
+
data: rewindResult(false, targetEntryId, ctx)
|
|
6407
6838
|
});
|
|
6408
|
-
this.emitState(this.state);
|
|
6409
6839
|
}
|
|
6410
|
-
|
|
6411
|
-
|
|
6412
|
-
|
|
6413
|
-
|
|
6414
|
-
|
|
6840
|
+
async function handleReload(request, ctx) {
|
|
6841
|
+
if (!ctx.isIdle()) {
|
|
6842
|
+
emit(ctx, {
|
|
6843
|
+
...base(request),
|
|
6844
|
+
ok: false,
|
|
6845
|
+
data: buildState(ctx),
|
|
6846
|
+
error: "reload is unavailable while the agent is busy"
|
|
6847
|
+
});
|
|
6415
6848
|
return;
|
|
6416
6849
|
}
|
|
6417
|
-
|
|
6418
|
-
|
|
6419
|
-
|
|
6420
|
-
|
|
6421
|
-
|
|
6422
|
-
|
|
6423
|
-
|
|
6424
|
-
|
|
6425
|
-
|
|
6426
|
-
|
|
6850
|
+
if (compactionActive) {
|
|
6851
|
+
emit(ctx, {
|
|
6852
|
+
...base(request),
|
|
6853
|
+
ok: false,
|
|
6854
|
+
data: buildState(ctx),
|
|
6855
|
+
error: "reload is unavailable while a compaction is in progress"
|
|
6856
|
+
});
|
|
6857
|
+
return;
|
|
6858
|
+
}
|
|
6859
|
+
const activeChildren = bridgeTracker.activeChildren();
|
|
6860
|
+
if (activeChildren > 0) {
|
|
6861
|
+
emit(ctx, {
|
|
6862
|
+
...base(request),
|
|
6863
|
+
ok: false,
|
|
6864
|
+
data: buildState(ctx),
|
|
6865
|
+
error: `reload is unavailable while ${activeChildren} owned child session(s) are active`
|
|
6866
|
+
});
|
|
6867
|
+
return;
|
|
6868
|
+
}
|
|
6869
|
+
emit(ctx, { ...base(request), ok: true, data: buildState(ctx) });
|
|
6870
|
+
await ctx.reload();
|
|
6871
|
+
}
|
|
6872
|
+
async function handleArgs(args, ctx) {
|
|
6873
|
+
const parsed = parseRequestArg(args);
|
|
6874
|
+
if (!parsed.ok) {
|
|
6875
|
+
emit(ctx, {
|
|
6876
|
+
version: PROTOCOL_VERSION,
|
|
6877
|
+
sessionKey: parsed.sessionKey,
|
|
6878
|
+
requestId: parsed.requestId,
|
|
6879
|
+
operation: parsed.operation,
|
|
6880
|
+
ok: false,
|
|
6881
|
+
error: parsed.error
|
|
6882
|
+
});
|
|
6883
|
+
return;
|
|
6884
|
+
}
|
|
6885
|
+
const request = parsed.request;
|
|
6886
|
+
const refuse = (error) => emit(ctx, { ...base(request), ok: false, error });
|
|
6887
|
+
if (!expectedSessionKey) {
|
|
6888
|
+
refuse(`${SESSION_KEY_ENV} is not set for this process; refusing control request.`);
|
|
6889
|
+
return;
|
|
6890
|
+
}
|
|
6891
|
+
if (request.sessionKey !== expectedSessionKey) {
|
|
6892
|
+
refuse("session key mismatch; refusing control request.");
|
|
6893
|
+
return;
|
|
6894
|
+
}
|
|
6895
|
+
await activate(ctx);
|
|
6896
|
+
switch (request.operation) {
|
|
6897
|
+
case "hello":
|
|
6898
|
+
handleHello(request, ctx);
|
|
6899
|
+
return;
|
|
6900
|
+
case "get-state":
|
|
6901
|
+
emit(ctx, { ...base(request), ok: true, data: buildState(ctx) });
|
|
6902
|
+
return;
|
|
6903
|
+
case "configure":
|
|
6904
|
+
await handleConfigure(request, ctx);
|
|
6905
|
+
return;
|
|
6906
|
+
case "rewind":
|
|
6907
|
+
await handleRewind(request, ctx);
|
|
6908
|
+
return;
|
|
6909
|
+
case "reload":
|
|
6910
|
+
await handleReload(request, ctx);
|
|
6911
|
+
return;
|
|
6912
|
+
}
|
|
6427
6913
|
}
|
|
6914
|
+
pi.registerCommand(COMMAND_NAME, {
|
|
6915
|
+
description: "Plexus control protocol (base64url-encoded JSON).",
|
|
6916
|
+
handler: (args, ctx) => handleArgs(args, ctx)
|
|
6917
|
+
});
|
|
6918
|
+
pi.on("session_start", (_event, ctx) => {
|
|
6919
|
+
if (!active || closed)
|
|
6920
|
+
return;
|
|
6921
|
+
latestCtx = ctx;
|
|
6922
|
+
return restore(ctx);
|
|
6923
|
+
});
|
|
6924
|
+
pi.on("session_tree", (_event, ctx) => {
|
|
6925
|
+
if (!active || closed)
|
|
6926
|
+
return;
|
|
6927
|
+
latestCtx = ctx;
|
|
6928
|
+
return restore(ctx);
|
|
6929
|
+
});
|
|
6930
|
+
pi.on("model_select", (_event, ctx) => {
|
|
6931
|
+
if (!active || closed)
|
|
6932
|
+
return;
|
|
6933
|
+
latestCtx = ctx;
|
|
6934
|
+
});
|
|
6935
|
+
pi.on("before_agent_start", async (_event, ctx) => {
|
|
6936
|
+
if (!active || closed)
|
|
6937
|
+
return;
|
|
6938
|
+
latestCtx = ctx;
|
|
6939
|
+
await policy.reconcile();
|
|
6940
|
+
if (policy.getView().applyFailed)
|
|
6941
|
+
throw new Error(APPLY_ERROR);
|
|
6942
|
+
});
|
|
6943
|
+
pi.on("session_before_compact", () => {
|
|
6944
|
+
compactionActive = true;
|
|
6945
|
+
});
|
|
6946
|
+
pi.on("session_compact", () => {
|
|
6947
|
+
compactionActive = false;
|
|
6948
|
+
});
|
|
6949
|
+
pi.on("session_compact_failed", () => {
|
|
6950
|
+
compactionActive = false;
|
|
6951
|
+
});
|
|
6952
|
+
pi.on("session_shutdown", () => {
|
|
6953
|
+
closed = true;
|
|
6954
|
+
compactionActive = false;
|
|
6955
|
+
unsubscribeBridge?.();
|
|
6956
|
+
unsubscribeBridge = undefined;
|
|
6957
|
+
unsubscribeView?.();
|
|
6958
|
+
unsubscribeView = undefined;
|
|
6959
|
+
if (active)
|
|
6960
|
+
policy.setResolver(undefined);
|
|
6961
|
+
latestCtx = undefined;
|
|
6962
|
+
});
|
|
6963
|
+
return {
|
|
6964
|
+
get active() {
|
|
6965
|
+
return active;
|
|
6966
|
+
},
|
|
6967
|
+
handleArgs,
|
|
6968
|
+
buildState,
|
|
6969
|
+
activeChildren: () => bridgeTracker.activeChildren()
|
|
6970
|
+
};
|
|
6428
6971
|
}
|
|
6429
6972
|
|
|
6430
6973
|
// src/policy.ts
|
|
6431
|
-
import { randomUUID as randomUUID3 } from "crypto";
|
|
6432
|
-
var POLICY_SET_CHANNEL = "plexus:policy:set:v1";
|
|
6433
|
-
var POLICY_STATE_CHANNEL = "plexus:policy:state:v1";
|
|
6434
|
-
var MAX_STATE_BYTES2 = 1024 * 1024;
|
|
6435
|
-
var PolicySetSchema = object({
|
|
6436
|
-
version: literal(1),
|
|
6437
|
-
requestId: string2().min(1).max(1024),
|
|
6438
|
-
longContext: boolean2().optional(),
|
|
6439
|
-
serviceTier: string2().min(1).max(100).nullable().optional()
|
|
6440
|
-
}).strict().refine((command) => command.longContext !== undefined || command.serviceTier !== undefined, { message: "At least one of longContext or serviceTier is required." });
|
|
6441
|
-
var PolicyAppliedSchema = object({
|
|
6442
|
-
longContext: boolean2(),
|
|
6443
|
-
serviceTier: string2().min(1).max(100).nullable(),
|
|
6444
|
-
contextWindow: number2().int().positive().safe().optional()
|
|
6445
|
-
}).strict();
|
|
6446
|
-
var PolicyAvailabilitySchema = object({
|
|
6447
|
-
longContext: boolean2(),
|
|
6448
|
-
serviceTier: boolean2()
|
|
6449
|
-
}).strict();
|
|
6450
|
-
var PolicyStateSchema = object({
|
|
6451
|
-
version: literal(1),
|
|
6452
|
-
publisherId: string2().uuid(),
|
|
6453
|
-
revision: number2().int().positive().safe(),
|
|
6454
|
-
requestId: string2().min(1).max(1024).optional(),
|
|
6455
|
-
applied: PolicyAppliedSchema,
|
|
6456
|
-
available: PolicyAvailabilitySchema,
|
|
6457
|
-
reason: string2().max(500).optional()
|
|
6458
|
-
}).strict();
|
|
6459
|
-
var POLICY_UNAVAILABLE_REASON = "No Plexus context or service-tier policy is advertised for the active model.";
|
|
6460
|
-
var DEFAULT_SELECTION = Object.freeze({
|
|
6461
|
-
longContext: true,
|
|
6462
|
-
serviceTier: null
|
|
6463
|
-
});
|
|
6464
6974
|
function policyAvailability(advertisement) {
|
|
6465
6975
|
const context = advertisement?.context;
|
|
6466
6976
|
return {
|
|
@@ -6484,99 +6994,50 @@ function injectServiceTier(payload, tier) {
|
|
|
6484
6994
|
return payload;
|
|
6485
6995
|
return { ...payload, service_tier: tier };
|
|
6486
6996
|
}
|
|
6487
|
-
var
|
|
6488
|
-
|
|
6489
|
-
|
|
6490
|
-
}
|
|
6491
|
-
function
|
|
6492
|
-
return
|
|
6493
|
-
|
|
6494
|
-
|
|
6495
|
-
|
|
6496
|
-
})
|
|
6497
|
-
}
|
|
6498
|
-
|
|
6499
|
-
|
|
6500
|
-
|
|
6501
|
-
applied: Object.freeze({ ...state.applied }),
|
|
6502
|
-
available: Object.freeze({ ...state.available })
|
|
6503
|
-
});
|
|
6504
|
-
}
|
|
6505
|
-
function selectionStatesEqual(current, next) {
|
|
6506
|
-
const shape = (state) => JSON.stringify({
|
|
6507
|
-
applied: state.applied,
|
|
6508
|
-
available: state.available,
|
|
6509
|
-
reason: state.reason ?? null
|
|
6510
|
-
});
|
|
6511
|
-
return shape(current) === shape(next);
|
|
6512
|
-
}
|
|
6513
|
-
function validateSelection(model, advertisement, command) {
|
|
6514
|
-
if (model === undefined || advertisement === undefined || advertisement.context === undefined && advertisement.serviceTier === undefined) {
|
|
6515
|
-
return POLICY_UNAVAILABLE_REASON;
|
|
6516
|
-
}
|
|
6517
|
-
if (command.longContext !== undefined) {
|
|
6518
|
-
const context = advertisement.context;
|
|
6519
|
-
if (context === undefined)
|
|
6520
|
-
return "No context budget is advertised for the active model.";
|
|
6521
|
-
if (context.shortContextBudgetTokens >= context.maxContextTokens) {
|
|
6522
|
-
return "The active model has no distinct short and maximum context budget.";
|
|
6523
|
-
}
|
|
6524
|
-
}
|
|
6525
|
-
if (command.serviceTier !== undefined && command.serviceTier !== null) {
|
|
6526
|
-
const tiers = advertisement.serviceTier?.serviceTiers;
|
|
6527
|
-
if (!tiers?.includes(command.serviceTier)) {
|
|
6528
|
-
return "The requested service tier is not advertised for the active model.";
|
|
6529
|
-
}
|
|
6530
|
-
}
|
|
6531
|
-
return;
|
|
6532
|
-
}
|
|
6997
|
+
var TERMINAL_RESOLVER = () => ({
|
|
6998
|
+
longContext: true,
|
|
6999
|
+
serviceTier: null
|
|
7000
|
+
});
|
|
7001
|
+
function sameModel(a, b) {
|
|
7002
|
+
return a !== undefined && b !== undefined && a.provider === b.provider && a.id === b.id;
|
|
7003
|
+
}
|
|
7004
|
+
var EMPTY_VIEW = Object.freeze({
|
|
7005
|
+
model: undefined,
|
|
7006
|
+
selection: Object.freeze({ longContext: false, serviceTier: null }),
|
|
7007
|
+
available: Object.freeze({ longContext: false, serviceTier: false }),
|
|
7008
|
+
contextWindow: undefined,
|
|
7009
|
+
applyFailed: false
|
|
7010
|
+
});
|
|
6533
7011
|
|
|
6534
7012
|
class PolicyController {
|
|
6535
7013
|
host;
|
|
6536
|
-
|
|
6537
|
-
|
|
6538
|
-
|
|
6539
|
-
boundModel;
|
|
6540
|
-
available = {
|
|
6541
|
-
longContext: false,
|
|
6542
|
-
serviceTier: false
|
|
6543
|
-
};
|
|
6544
|
-
contextWindow;
|
|
6545
|
-
reason;
|
|
6546
|
-
state;
|
|
6547
|
-
emit;
|
|
6548
|
-
unsubscribe;
|
|
7014
|
+
resolver;
|
|
7015
|
+
view = EMPTY_VIEW;
|
|
7016
|
+
listeners = new Set;
|
|
6549
7017
|
chain = Promise.resolve();
|
|
6550
|
-
constructor(
|
|
7018
|
+
constructor(host) {
|
|
6551
7019
|
this.host = host;
|
|
6552
|
-
this.emit = events.emit.bind(events);
|
|
6553
|
-
this.state = freezePublished({
|
|
6554
|
-
version: 1,
|
|
6555
|
-
publisherId: this.publisherId,
|
|
6556
|
-
revision: 1,
|
|
6557
|
-
applied: { longContext: true, serviceTier: null },
|
|
6558
|
-
available: { longContext: false, serviceTier: false }
|
|
6559
|
-
});
|
|
6560
|
-
this.unsubscribe = events.on(POLICY_SET_CHANNEL, (data) => this.enqueue(() => this.handleSet(data)));
|
|
6561
7020
|
}
|
|
6562
|
-
|
|
6563
|
-
return this.
|
|
7021
|
+
getView() {
|
|
7022
|
+
return this.view;
|
|
7023
|
+
}
|
|
7024
|
+
setResolver(resolver) {
|
|
7025
|
+
this.resolver = resolver;
|
|
7026
|
+
}
|
|
7027
|
+
subscribe(listener) {
|
|
7028
|
+
this.listeners.add(listener);
|
|
7029
|
+
return () => {
|
|
7030
|
+
this.listeners.delete(listener);
|
|
7031
|
+
};
|
|
6564
7032
|
}
|
|
6565
7033
|
serviceTierFor(model) {
|
|
6566
|
-
if (!model || !this.
|
|
6567
|
-
return null;
|
|
6568
|
-
if (this.boundModel.provider !== model.provider || this.boundModel.id !== model.id)
|
|
6569
|
-
return null;
|
|
6570
|
-
if (!this.available.serviceTier)
|
|
7034
|
+
if (!model || !sameModel(this.view.model, model))
|
|
6571
7035
|
return null;
|
|
6572
|
-
return this.selection.serviceTier;
|
|
7036
|
+
return this.view.selection.serviceTier;
|
|
6573
7037
|
}
|
|
6574
7038
|
reconcile() {
|
|
6575
7039
|
return this.enqueue(() => this.recompute());
|
|
6576
7040
|
}
|
|
6577
|
-
dispose() {
|
|
6578
|
-
this.unsubscribe();
|
|
6579
|
-
}
|
|
6580
7041
|
enqueue(task) {
|
|
6581
7042
|
const run = this.chain.then(task, task).catch(() => {
|
|
6582
7043
|
return;
|
|
@@ -6586,84 +7047,41 @@ class PolicyController {
|
|
|
6586
7047
|
}
|
|
6587
7048
|
async recompute() {
|
|
6588
7049
|
const model = this.host.getActiveModel();
|
|
6589
|
-
|
|
6590
|
-
|
|
6591
|
-
selection: { ...DEFAULT_SELECTION },
|
|
6592
|
-
boundModel: undefined,
|
|
6593
|
-
available: { longContext: false, serviceTier: false },
|
|
6594
|
-
contextWindow: undefined,
|
|
6595
|
-
reason: POLICY_UNAVAILABLE_REASON
|
|
6596
|
-
});
|
|
6597
|
-
return;
|
|
6598
|
-
}
|
|
6599
|
-
const advertisement = this.host.getAdvertisement(model);
|
|
6600
|
-
const sameModel = this.boundModel?.provider === model.provider && this.boundModel.id === model.id;
|
|
6601
|
-
let selection = sameModel ? { ...this.selection } : { ...DEFAULT_SELECTION };
|
|
6602
|
-
let reason;
|
|
7050
|
+
const advertisement = model === undefined ? undefined : this.host.getAdvertisement(model);
|
|
7051
|
+
const intent = (this.resolver ?? TERMINAL_RESOLVER)(model, advertisement);
|
|
6603
7052
|
const available = policyAvailability(advertisement);
|
|
6604
|
-
|
|
6605
|
-
|
|
6606
|
-
|
|
6607
|
-
|
|
6608
|
-
|
|
6609
|
-
}
|
|
6610
|
-
|
|
6611
|
-
|
|
6612
|
-
|
|
6613
|
-
|
|
6614
|
-
|
|
6615
|
-
|
|
6616
|
-
|
|
6617
|
-
|
|
7053
|
+
const tiers = advertisement?.serviceTier?.serviceTiers ?? [];
|
|
7054
|
+
const same = sameModel(this.view.model, model);
|
|
7055
|
+
let selection = {
|
|
7056
|
+
longContext: intent.longContext && available.longContext,
|
|
7057
|
+
serviceTier: intent.serviceTier !== null && tiers.includes(intent.serviceTier) ? intent.serviceTier : null
|
|
7058
|
+
};
|
|
7059
|
+
let contextWindow;
|
|
7060
|
+
let applyFailed = false;
|
|
7061
|
+
if (model !== undefined) {
|
|
7062
|
+
const applied = await this.applyContext(model, effectiveContextWindow(advertisement, selection));
|
|
7063
|
+
if (advertisement?.context !== undefined) {
|
|
7064
|
+
if (applied === undefined) {
|
|
7065
|
+
applyFailed = true;
|
|
7066
|
+
selection = {
|
|
7067
|
+
...selection,
|
|
7068
|
+
longContext: same ? this.view.selection.longContext : false
|
|
7069
|
+
};
|
|
7070
|
+
contextWindow = same ? this.view.contextWindow : undefined;
|
|
7071
|
+
} else {
|
|
7072
|
+
contextWindow = applied;
|
|
7073
|
+
}
|
|
6618
7074
|
}
|
|
6619
7075
|
}
|
|
6620
|
-
|
|
6621
|
-
|
|
6622
|
-
|
|
6623
|
-
|
|
6624
|
-
|
|
6625
|
-
|
|
6626
|
-
reason = "The session context window could not be updated.";
|
|
6627
|
-
}
|
|
6628
|
-
this.commit({
|
|
6629
|
-
selection,
|
|
6630
|
-
boundModel: model,
|
|
6631
|
-
available,
|
|
6632
|
-
contextWindow: advertisement?.context !== undefined ? appliedWindow : undefined,
|
|
6633
|
-
reason
|
|
7076
|
+
this.view = Object.freeze({
|
|
7077
|
+
model,
|
|
7078
|
+
selection: Object.freeze(selection),
|
|
7079
|
+
available: Object.freeze(available),
|
|
7080
|
+
contextWindow,
|
|
7081
|
+
applyFailed
|
|
6634
7082
|
});
|
|
6635
|
-
|
|
6636
|
-
|
|
6637
|
-
const parsed = PolicySetSchema.safeParse(data);
|
|
6638
|
-
if (!parsed.success)
|
|
6639
|
-
return;
|
|
6640
|
-
const command = parsed.data;
|
|
6641
|
-
const model = this.host.getActiveModel();
|
|
6642
|
-
const advertisement = model === undefined ? undefined : this.host.getAdvertisement(model);
|
|
6643
|
-
const sameModel = model !== undefined && this.boundModel?.provider === model.provider && this.boundModel.id === model.id;
|
|
6644
|
-
const current = sameModel ? { ...this.selection } : { ...DEFAULT_SELECTION };
|
|
6645
|
-
const nextSelection = {
|
|
6646
|
-
longContext: command.longContext ?? current.longContext,
|
|
6647
|
-
serviceTier: command.serviceTier === undefined ? current.serviceTier : command.serviceTier
|
|
6648
|
-
};
|
|
6649
|
-
const rejection = validateSelection(model, advertisement, command);
|
|
6650
|
-
if (rejection !== undefined) {
|
|
6651
|
-
this.emitReply(command.requestId, rejection);
|
|
6652
|
-
return;
|
|
6653
|
-
}
|
|
6654
|
-
const desired = effectiveContextWindow(advertisement, nextSelection);
|
|
6655
|
-
const appliedWindow = model === undefined ? undefined : await this.applyContext(model, desired);
|
|
6656
|
-
if (advertisement?.context !== undefined && appliedWindow === undefined) {
|
|
6657
|
-
this.emitReply(command.requestId, "The session context window could not be updated.");
|
|
6658
|
-
return;
|
|
6659
|
-
}
|
|
6660
|
-
this.commit({
|
|
6661
|
-
selection: nextSelection,
|
|
6662
|
-
boundModel: model,
|
|
6663
|
-
available: policyAvailability(advertisement),
|
|
6664
|
-
contextWindow: advertisement?.context !== undefined ? appliedWindow : undefined,
|
|
6665
|
-
reason: undefined
|
|
6666
|
-
}, command.requestId);
|
|
7083
|
+
for (const listener of [...this.listeners])
|
|
7084
|
+
listener();
|
|
6667
7085
|
}
|
|
6668
7086
|
async applyContext(model, contextWindow) {
|
|
6669
7087
|
try {
|
|
@@ -6672,342 +7090,21 @@ class PolicyController {
|
|
|
6672
7090
|
return;
|
|
6673
7091
|
}
|
|
6674
7092
|
}
|
|
6675
|
-
commit(next, requestId) {
|
|
6676
|
-
this.selection = next.selection;
|
|
6677
|
-
this.boundModel = next.boundModel;
|
|
6678
|
-
this.available = next.available;
|
|
6679
|
-
this.contextWindow = next.contextWindow;
|
|
6680
|
-
this.reason = next.reason;
|
|
6681
|
-
const candidate = {
|
|
6682
|
-
applied: {
|
|
6683
|
-
longContext: next.selection.longContext,
|
|
6684
|
-
serviceTier: next.selection.serviceTier,
|
|
6685
|
-
...next.contextWindow === undefined ? {} : { contextWindow: next.contextWindow }
|
|
6686
|
-
},
|
|
6687
|
-
available: { ...next.available },
|
|
6688
|
-
...next.reason === undefined ? {} : { reason: next.reason }
|
|
6689
|
-
};
|
|
6690
|
-
const changed = !selectionStatesEqual(this.state, candidate);
|
|
6691
|
-
if (changed) {
|
|
6692
|
-
this.revision++;
|
|
6693
|
-
this.state = freezePublished({
|
|
6694
|
-
version: 1,
|
|
6695
|
-
publisherId: this.publisherId,
|
|
6696
|
-
revision: this.revision,
|
|
6697
|
-
...candidate
|
|
6698
|
-
});
|
|
6699
|
-
this.emitState(this.state);
|
|
6700
|
-
}
|
|
6701
|
-
if (requestId !== undefined) {
|
|
6702
|
-
this.emitState(this.state, requestId);
|
|
6703
|
-
}
|
|
6704
|
-
}
|
|
6705
|
-
emitReply(requestId, reason) {
|
|
6706
|
-
const parsed = PolicyStateSchema.safeParse({
|
|
6707
|
-
...this.state,
|
|
6708
|
-
...reason === undefined ? {} : { reason },
|
|
6709
|
-
requestId
|
|
6710
|
-
});
|
|
6711
|
-
if (parsed.success && byteLength2(parsed.data) <= MAX_STATE_BYTES2) {
|
|
6712
|
-
this.emit(POLICY_STATE_CHANNEL, freezeParsed2(parsed.data));
|
|
6713
|
-
}
|
|
6714
|
-
}
|
|
6715
|
-
emitState(state, requestId) {
|
|
6716
|
-
const candidate = requestId === undefined ? state : { ...state, requestId };
|
|
6717
|
-
const parsed = PolicyStateSchema.safeParse(candidate);
|
|
6718
|
-
if (parsed.success && byteLength2(parsed.data) <= MAX_STATE_BYTES2) {
|
|
6719
|
-
this.emit(POLICY_STATE_CHANNEL, freezeParsed2(parsed.data));
|
|
6720
|
-
return;
|
|
6721
|
-
}
|
|
6722
|
-
const fallback = PolicyStateSchema.safeParse({
|
|
6723
|
-
version: 1,
|
|
6724
|
-
publisherId: this.publisherId,
|
|
6725
|
-
revision: this.revision,
|
|
6726
|
-
...requestId === undefined ? {} : { requestId },
|
|
6727
|
-
applied: { longContext: true, serviceTier: null },
|
|
6728
|
-
available: { longContext: false, serviceTier: false },
|
|
6729
|
-
reason: "Plexus policy state is unavailable."
|
|
6730
|
-
});
|
|
6731
|
-
if (fallback.success)
|
|
6732
|
-
this.emit(POLICY_STATE_CHANNEL, freezeParsed2(fallback.data));
|
|
6733
|
-
}
|
|
6734
7093
|
}
|
|
6735
7094
|
|
|
6736
7095
|
// src/service-tiers.ts
|
|
6737
|
-
import { randomUUID as randomUUID4 } from "crypto";
|
|
6738
|
-
var SERVICE_TIERS_REQUEST_CHANNEL = "plexus:service-tiers:request:v1";
|
|
6739
|
-
var SERVICE_TIERS_SNAPSHOT_CHANNEL = "plexus:service-tiers:snapshot:v1";
|
|
6740
7096
|
var ServiceTierPolicySchema = object({
|
|
6741
7097
|
provider: string2().min(1),
|
|
6742
7098
|
modelId: string2().min(1),
|
|
6743
7099
|
serviceTiers: array(string2().min(1).max(100)).min(1).max(64)
|
|
6744
7100
|
}).strict().refine((policy) => new Set(policy.serviceTiers).size === policy.serviceTiers.length);
|
|
6745
|
-
var ServiceTiersRequestSchema = object({
|
|
6746
|
-
version: literal(1),
|
|
6747
|
-
requestId: string2().min(1).max(1024)
|
|
6748
|
-
}).strict();
|
|
6749
|
-
var ServiceTiersSnapshotSchema = object({
|
|
6750
|
-
version: literal(1),
|
|
6751
|
-
publisherId: string2().uuid(),
|
|
6752
|
-
revision: number2().int().positive().safe(),
|
|
6753
|
-
requestId: string2().min(1).max(1024).optional(),
|
|
6754
|
-
status: _enum(["ready", "loading", "unavailable"]),
|
|
6755
|
-
policies: array(ServiceTierPolicySchema),
|
|
6756
|
-
fetchedAt: number2().int().nonnegative().safe().optional(),
|
|
6757
|
-
cached: boolean2().optional(),
|
|
6758
|
-
reason: string2().max(500).optional()
|
|
6759
|
-
}).strict().superRefine((snapshot, ctx) => {
|
|
6760
|
-
const pairs = new Set;
|
|
6761
|
-
for (const policy of snapshot.policies) {
|
|
6762
|
-
const pair = `${policy.provider}\x00${policy.modelId}`;
|
|
6763
|
-
if (pairs.has(pair))
|
|
6764
|
-
ctx.addIssue({
|
|
6765
|
-
code: "custom",
|
|
6766
|
-
message: "Duplicate provider/modelId service-tier policy"
|
|
6767
|
-
});
|
|
6768
|
-
pairs.add(pair);
|
|
6769
|
-
}
|
|
6770
|
-
if (snapshot.status !== "ready" && snapshot.policies.length !== 0) {
|
|
6771
|
-
ctx.addIssue({
|
|
6772
|
-
code: "custom",
|
|
6773
|
-
message: "Non-ready snapshots must have no policies"
|
|
6774
|
-
});
|
|
6775
|
-
}
|
|
6776
|
-
});
|
|
6777
|
-
var MAX_SNAPSHOT_BYTES2 = 1024 * 1024;
|
|
6778
|
-
var SERVICE_TIERS_METADATA_UNAVAILABLE_REASON = "Service-tier metadata is not available from the committed Plexus catalog.";
|
|
6779
|
-
|
|
6780
|
-
class ServiceTiersPublisher {
|
|
6781
|
-
publisherId = randomUUID4();
|
|
6782
|
-
revision = 1;
|
|
6783
|
-
state = Object.freeze({
|
|
6784
|
-
version: 1,
|
|
6785
|
-
publisherId: this.publisherId,
|
|
6786
|
-
revision: 1,
|
|
6787
|
-
status: "loading",
|
|
6788
|
-
policies: Object.freeze([])
|
|
6789
|
-
});
|
|
6790
|
-
emit;
|
|
6791
|
-
unsubscribe;
|
|
6792
|
-
constructor(events) {
|
|
6793
|
-
this.emit = events.emit.bind(events);
|
|
6794
|
-
this.unsubscribe = events.on(SERVICE_TIERS_REQUEST_CHANNEL, (data) => {
|
|
6795
|
-
const request = ServiceTiersRequestSchema.safeParse(data);
|
|
6796
|
-
if (request.success)
|
|
6797
|
-
this.send(request.data.requestId);
|
|
6798
|
-
});
|
|
6799
|
-
}
|
|
6800
|
-
getSnapshot() {
|
|
6801
|
-
return this.state;
|
|
6802
|
-
}
|
|
6803
|
-
setCatalog(status, policies, metadata) {
|
|
6804
|
-
const candidate = ServiceTiersSnapshotSchema.safeParse({
|
|
6805
|
-
version: 1,
|
|
6806
|
-
publisherId: this.publisherId,
|
|
6807
|
-
revision: this.revision,
|
|
6808
|
-
status,
|
|
6809
|
-
policies: [...policies],
|
|
6810
|
-
...metadata
|
|
6811
|
-
});
|
|
6812
|
-
const oversized = candidate.success && new TextEncoder().encode(JSON.stringify(candidate.data)).byteLength > MAX_SNAPSHOT_BYTES2;
|
|
6813
|
-
let next = candidate.success && !oversized ? candidate.data : {
|
|
6814
|
-
version: 1,
|
|
6815
|
-
publisherId: this.publisherId,
|
|
6816
|
-
revision: this.revision,
|
|
6817
|
-
status: "unavailable",
|
|
6818
|
-
policies: [],
|
|
6819
|
-
reason: oversized ? "Service-tier snapshot exceeds the 1 MiB publication limit." : "Service-tier snapshot could not be validated."
|
|
6820
|
-
};
|
|
6821
|
-
if (status !== "ready" && policies.length > 0) {
|
|
6822
|
-
next = {
|
|
6823
|
-
...next,
|
|
6824
|
-
status: "unavailable",
|
|
6825
|
-
policies: [],
|
|
6826
|
-
reason: "Service-tier metadata is unavailable."
|
|
6827
|
-
};
|
|
6828
|
-
}
|
|
6829
|
-
const { revision: _revision, ...stateWithoutRevision } = this.state;
|
|
6830
|
-
const { revision: _candidateRevision, ...nextWithoutRevision } = next;
|
|
6831
|
-
if (JSON.stringify(stateWithoutRevision) === JSON.stringify(nextWithoutRevision))
|
|
6832
|
-
return false;
|
|
6833
|
-
this.revision++;
|
|
6834
|
-
this.state = Object.freeze({
|
|
6835
|
-
...next,
|
|
6836
|
-
revision: this.revision,
|
|
6837
|
-
policies: Object.freeze(next.policies.map((policy) => Object.freeze({
|
|
6838
|
-
...policy,
|
|
6839
|
-
serviceTiers: Object.freeze([...policy.serviceTiers])
|
|
6840
|
-
})))
|
|
6841
|
-
});
|
|
6842
|
-
this.send();
|
|
6843
|
-
return true;
|
|
6844
|
-
}
|
|
6845
|
-
send(requestId) {
|
|
6846
|
-
const snapshot = {
|
|
6847
|
-
...this.state,
|
|
6848
|
-
...requestId === undefined ? {} : { requestId }
|
|
6849
|
-
};
|
|
6850
|
-
let valid = ServiceTiersSnapshotSchema.safeParse(snapshot);
|
|
6851
|
-
if (!valid.success || new TextEncoder().encode(JSON.stringify(snapshot)).byteLength > MAX_SNAPSHOT_BYTES2) {
|
|
6852
|
-
valid = ServiceTiersSnapshotSchema.safeParse({
|
|
6853
|
-
version: 1,
|
|
6854
|
-
publisherId: this.publisherId,
|
|
6855
|
-
revision: this.revision,
|
|
6856
|
-
status: "unavailable",
|
|
6857
|
-
policies: [],
|
|
6858
|
-
reason: "Service-tier snapshot exceeds publication limits or is invalid.",
|
|
6859
|
-
...requestId === undefined ? {} : { requestId }
|
|
6860
|
-
});
|
|
6861
|
-
}
|
|
6862
|
-
if (valid.success) {
|
|
6863
|
-
const immutable = Object.freeze({
|
|
6864
|
-
...valid.data,
|
|
6865
|
-
policies: Object.freeze(valid.data.policies.map((policy) => Object.freeze({
|
|
6866
|
-
...policy,
|
|
6867
|
-
serviceTiers: Object.freeze([...policy.serviceTiers])
|
|
6868
|
-
})))
|
|
6869
|
-
});
|
|
6870
|
-
this.emit(SERVICE_TIERS_SNAPSHOT_CHANNEL, immutable);
|
|
6871
|
-
}
|
|
6872
|
-
}
|
|
6873
|
-
dispose() {
|
|
6874
|
-
this.unsubscribe();
|
|
6875
|
-
}
|
|
6876
|
-
}
|
|
6877
7101
|
|
|
6878
7102
|
// src/short-names.ts
|
|
6879
|
-
import { randomUUID as randomUUID5 } from "crypto";
|
|
6880
|
-
var SHORT_NAMES_REQUEST_CHANNEL = "plexus:short-names:request:v1";
|
|
6881
|
-
var SHORT_NAMES_SNAPSHOT_CHANNEL = "plexus:short-names:snapshot:v1";
|
|
6882
7103
|
var ShortNameEntrySchema = object({
|
|
6883
7104
|
provider: string2().min(1),
|
|
6884
7105
|
modelId: string2().min(1),
|
|
6885
7106
|
shortName: string2().min(1).max(200)
|
|
6886
7107
|
}).strict();
|
|
6887
|
-
var ShortNamesRequestSchema = object({
|
|
6888
|
-
version: literal(1),
|
|
6889
|
-
requestId: string2().min(1).max(1024)
|
|
6890
|
-
}).strict();
|
|
6891
|
-
var ShortNamesSnapshotSchema = object({
|
|
6892
|
-
version: literal(1),
|
|
6893
|
-
publisherId: string2().uuid(),
|
|
6894
|
-
revision: number2().int().positive().safe(),
|
|
6895
|
-
requestId: string2().min(1).max(1024).optional(),
|
|
6896
|
-
status: _enum(["ready", "loading", "unavailable"]),
|
|
6897
|
-
names: array(ShortNameEntrySchema),
|
|
6898
|
-
fetchedAt: number2().int().nonnegative().safe().optional(),
|
|
6899
|
-
cached: boolean2().optional(),
|
|
6900
|
-
reason: string2().max(500).optional()
|
|
6901
|
-
}).strict().superRefine((snapshot, ctx) => {
|
|
6902
|
-
const pairs = new Set;
|
|
6903
|
-
for (const entry of snapshot.names) {
|
|
6904
|
-
const pair = `${entry.provider}\x00${entry.modelId}`;
|
|
6905
|
-
if (pairs.has(pair))
|
|
6906
|
-
ctx.addIssue({
|
|
6907
|
-
code: "custom",
|
|
6908
|
-
message: "Duplicate provider/modelId short-name entry"
|
|
6909
|
-
});
|
|
6910
|
-
pairs.add(pair);
|
|
6911
|
-
}
|
|
6912
|
-
if (snapshot.status !== "ready" && snapshot.names.length !== 0) {
|
|
6913
|
-
ctx.addIssue({
|
|
6914
|
-
code: "custom",
|
|
6915
|
-
message: "Non-ready snapshots must have no names"
|
|
6916
|
-
});
|
|
6917
|
-
}
|
|
6918
|
-
});
|
|
6919
|
-
var MAX_SNAPSHOT_BYTES3 = 1024 * 1024;
|
|
6920
|
-
class ShortNamesPublisher {
|
|
6921
|
-
publisherId = randomUUID5();
|
|
6922
|
-
revision = 1;
|
|
6923
|
-
state = Object.freeze({
|
|
6924
|
-
version: 1,
|
|
6925
|
-
publisherId: this.publisherId,
|
|
6926
|
-
revision: 1,
|
|
6927
|
-
status: "loading",
|
|
6928
|
-
names: Object.freeze([])
|
|
6929
|
-
});
|
|
6930
|
-
emit;
|
|
6931
|
-
unsubscribe;
|
|
6932
|
-
constructor(events) {
|
|
6933
|
-
this.emit = events.emit.bind(events);
|
|
6934
|
-
this.unsubscribe = events.on(SHORT_NAMES_REQUEST_CHANNEL, (data) => {
|
|
6935
|
-
const request = ShortNamesRequestSchema.safeParse(data);
|
|
6936
|
-
if (request.success)
|
|
6937
|
-
this.send(request.data.requestId);
|
|
6938
|
-
});
|
|
6939
|
-
}
|
|
6940
|
-
getSnapshot() {
|
|
6941
|
-
return this.state;
|
|
6942
|
-
}
|
|
6943
|
-
setCatalog(status, names, metadata) {
|
|
6944
|
-
const candidate = ShortNamesSnapshotSchema.safeParse({
|
|
6945
|
-
version: 1,
|
|
6946
|
-
publisherId: this.publisherId,
|
|
6947
|
-
revision: this.revision,
|
|
6948
|
-
status,
|
|
6949
|
-
names: [...names],
|
|
6950
|
-
...metadata
|
|
6951
|
-
});
|
|
6952
|
-
const oversized = candidate.success && new TextEncoder().encode(JSON.stringify(candidate.data)).byteLength > MAX_SNAPSHOT_BYTES3;
|
|
6953
|
-
let next = candidate.success && !oversized ? candidate.data : {
|
|
6954
|
-
version: 1,
|
|
6955
|
-
publisherId: this.publisherId,
|
|
6956
|
-
revision: this.revision,
|
|
6957
|
-
status: "unavailable",
|
|
6958
|
-
names: [],
|
|
6959
|
-
reason: oversized ? "Short-name snapshot exceeds the 1 MiB publication limit." : "Short-name snapshot could not be validated."
|
|
6960
|
-
};
|
|
6961
|
-
if (status !== "ready" && names.length > 0) {
|
|
6962
|
-
next = {
|
|
6963
|
-
...next,
|
|
6964
|
-
status: "unavailable",
|
|
6965
|
-
names: [],
|
|
6966
|
-
reason: "Short-name metadata is unavailable."
|
|
6967
|
-
};
|
|
6968
|
-
}
|
|
6969
|
-
const { revision: _revision, ...stateWithoutRevision } = this.state;
|
|
6970
|
-
const { revision: _candidateRevision, ...nextWithoutRevision } = next;
|
|
6971
|
-
if (JSON.stringify(stateWithoutRevision) === JSON.stringify(nextWithoutRevision))
|
|
6972
|
-
return false;
|
|
6973
|
-
this.revision++;
|
|
6974
|
-
this.state = Object.freeze({
|
|
6975
|
-
...next,
|
|
6976
|
-
revision: this.revision,
|
|
6977
|
-
names: Object.freeze(next.names.map((entry) => Object.freeze({ ...entry })))
|
|
6978
|
-
});
|
|
6979
|
-
this.send();
|
|
6980
|
-
return true;
|
|
6981
|
-
}
|
|
6982
|
-
send(requestId) {
|
|
6983
|
-
const snapshot = {
|
|
6984
|
-
...this.state,
|
|
6985
|
-
...requestId === undefined ? {} : { requestId }
|
|
6986
|
-
};
|
|
6987
|
-
let valid = ShortNamesSnapshotSchema.safeParse(snapshot);
|
|
6988
|
-
if (!valid.success || new TextEncoder().encode(JSON.stringify(snapshot)).byteLength > MAX_SNAPSHOT_BYTES3) {
|
|
6989
|
-
valid = ShortNamesSnapshotSchema.safeParse({
|
|
6990
|
-
version: 1,
|
|
6991
|
-
publisherId: this.publisherId,
|
|
6992
|
-
revision: this.revision,
|
|
6993
|
-
status: "unavailable",
|
|
6994
|
-
names: [],
|
|
6995
|
-
reason: "Short-name snapshot exceeds publication limits or is invalid.",
|
|
6996
|
-
...requestId === undefined ? {} : { requestId }
|
|
6997
|
-
});
|
|
6998
|
-
}
|
|
6999
|
-
if (valid.success) {
|
|
7000
|
-
const immutable = Object.freeze({
|
|
7001
|
-
...valid.data,
|
|
7002
|
-
names: Object.freeze(valid.data.names.map((entry) => Object.freeze({ ...entry })))
|
|
7003
|
-
});
|
|
7004
|
-
this.emit(SHORT_NAMES_SNAPSHOT_CHANNEL, immutable);
|
|
7005
|
-
}
|
|
7006
|
-
}
|
|
7007
|
-
dispose() {
|
|
7008
|
-
this.unsubscribe();
|
|
7009
|
-
}
|
|
7010
|
-
}
|
|
7011
7108
|
|
|
7012
7109
|
// src/extension.ts
|
|
7013
7110
|
var PROVIDER_NAME = "plexus";
|
|
@@ -7240,8 +7337,11 @@ function createPolicyHost(deps) {
|
|
|
7240
7337
|
...serviceTier ? { serviceTier } : {}
|
|
7241
7338
|
};
|
|
7242
7339
|
},
|
|
7243
|
-
|
|
7244
|
-
return deps.
|
|
7340
|
+
getCatalogContextWindow(model) {
|
|
7341
|
+
return deps.getRegistry()?.find(model.provider, model.id)?.contextWindow;
|
|
7342
|
+
},
|
|
7343
|
+
hasCatalogContextPolicy() {
|
|
7344
|
+
return deps.getModels().some((entry) => entry.plexusContextPolicy !== undefined);
|
|
7245
7345
|
},
|
|
7246
7346
|
async applyContextWindow(model, contextWindow) {
|
|
7247
7347
|
const registry = deps.getRegistry();
|
|
@@ -7264,21 +7364,14 @@ function createPolicyHost(deps) {
|
|
|
7264
7364
|
};
|
|
7265
7365
|
}
|
|
7266
7366
|
function plexusExtension(pi) {
|
|
7267
|
-
const contextPolicies = new ContextPolicyPublisher(pi.events);
|
|
7268
|
-
const serviceTiers = new ServiceTiersPublisher(pi.events);
|
|
7269
|
-
const shortNames = new ShortNamesPublisher(pi.events);
|
|
7270
7367
|
const state = {
|
|
7271
7368
|
models: [],
|
|
7272
7369
|
catalogSource: "none",
|
|
7273
|
-
refreshSequence: 0,
|
|
7274
|
-
contextPolicies,
|
|
7275
|
-
serviceTiers,
|
|
7276
|
-
shortNames,
|
|
7277
7370
|
policy: undefined,
|
|
7278
7371
|
activeModel: undefined,
|
|
7279
7372
|
registry: undefined
|
|
7280
7373
|
};
|
|
7281
|
-
const
|
|
7374
|
+
const host = createPolicyHost({
|
|
7282
7375
|
getActiveModel: () => state.activeModel,
|
|
7283
7376
|
getModels: () => state.models,
|
|
7284
7377
|
getRegistry: () => state.registry,
|
|
@@ -7286,42 +7379,11 @@ function plexusExtension(pi) {
|
|
|
7286
7379
|
setActiveModel: (model) => {
|
|
7287
7380
|
state.activeModel = model;
|
|
7288
7381
|
}
|
|
7289
|
-
}));
|
|
7290
|
-
state.policy = policy;
|
|
7291
|
-
const modelsControl = new ModelsControl(pi.events, {
|
|
7292
|
-
refresh: async () => {
|
|
7293
|
-
const registry = state.registry;
|
|
7294
|
-
if (!registry) {
|
|
7295
|
-
const message = "Plexus model registry is unavailable before the session starts.";
|
|
7296
|
-
log("models:refresh unavailable", { reason: message });
|
|
7297
|
-
throw new Error(message);
|
|
7298
|
-
}
|
|
7299
|
-
const result = await registry.refresh({
|
|
7300
|
-
providers: [PROVIDER_NAME],
|
|
7301
|
-
force: true
|
|
7302
|
-
});
|
|
7303
|
-
if (result.aborted) {
|
|
7304
|
-
log("models:refresh aborted", {});
|
|
7305
|
-
throw new Error("Plexus model refresh was cancelled.");
|
|
7306
|
-
}
|
|
7307
|
-
const refreshError = result.errors.get(PROVIDER_NAME);
|
|
7308
|
-
if (refreshError)
|
|
7309
|
-
throw refreshError;
|
|
7310
|
-
return { modelCount: state.models.length };
|
|
7311
|
-
}
|
|
7312
7382
|
});
|
|
7383
|
+
const policy = new PolicyController(host);
|
|
7384
|
+
state.policy = policy;
|
|
7385
|
+
createPlexuspiControl(pi, { policy, host });
|
|
7313
7386
|
pi.on("session_shutdown", () => {
|
|
7314
|
-
contextPolicies.dispose();
|
|
7315
|
-
serviceTiers.dispose();
|
|
7316
|
-
shortNames.dispose();
|
|
7317
|
-
policy.dispose();
|
|
7318
|
-
modelsControl.dispose();
|
|
7319
|
-
if (state.contextPolicies === contextPolicies)
|
|
7320
|
-
state.contextPolicies = undefined;
|
|
7321
|
-
if (state.serviceTiers === serviceTiers)
|
|
7322
|
-
state.serviceTiers = undefined;
|
|
7323
|
-
if (state.shortNames === shortNames)
|
|
7324
|
-
state.shortNames = undefined;
|
|
7325
7387
|
if (state.policy === policy)
|
|
7326
7388
|
state.policy = undefined;
|
|
7327
7389
|
});
|
|
@@ -7335,19 +7397,6 @@ function plexusExtension(pi) {
|
|
|
7335
7397
|
const startupModels = (storedCatalog?.models ?? []).filter((model) => !isModelSuppressed({ id: model.id, name: model.name }, suppressPatterns));
|
|
7336
7398
|
state.models = startupModels;
|
|
7337
7399
|
state.catalogSource = startupModels.length > 0 ? "host store" : "none";
|
|
7338
|
-
const startupPolicies = collectStoredContextPolicies(startupModels);
|
|
7339
|
-
contextPolicies.setCatalog(startupPolicies.length > 0 ? "ready" : "unavailable", startupPolicies.map((entry) => entry.policy), startupPolicies.length > 0 ? { cached: true, fetchedAt: startupPolicies[0]?.fetchedAt } : { cached: true, reason: CONTEXT_POLICY_METADATA_UNAVAILABLE_REASON });
|
|
7340
|
-
if (storedCatalog) {
|
|
7341
|
-
const cachedTiers = collectStoredServiceTiers(startupModels);
|
|
7342
|
-
serviceTiers.setCatalog(cachedTiers.complete ? "ready" : "unavailable", cachedTiers.policies, cachedTiers.complete ? {
|
|
7343
|
-
cached: true,
|
|
7344
|
-
...cachedTiers.fetchedAt === undefined ? {} : { fetchedAt: cachedTiers.fetchedAt }
|
|
7345
|
-
} : { cached: true, reason: SERVICE_TIERS_METADATA_UNAVAILABLE_REASON });
|
|
7346
|
-
shortNames.setCatalog("ready", collectStoredShortNames(startupModels), {
|
|
7347
|
-
cached: true,
|
|
7348
|
-
...storedCatalog.checkedAt ? { fetchedAt: storedCatalog.checkedAt } : {}
|
|
7349
|
-
});
|
|
7350
|
-
}
|
|
7351
7400
|
policy.reconcile();
|
|
7352
7401
|
pi.on("session_start", (_event, ctx) => {
|
|
7353
7402
|
state.registry = ctx.modelRegistry;
|
|
@@ -7427,7 +7476,6 @@ function plexusExtension(pi) {
|
|
|
7427
7476
|
});
|
|
7428
7477
|
}
|
|
7429
7478
|
async function refreshPlexusModels(state, context) {
|
|
7430
|
-
const refreshId = ++state.refreshSequence;
|
|
7431
7479
|
const baseUrl = getBaseUrl();
|
|
7432
7480
|
const modelsUrl = getModelsUrl();
|
|
7433
7481
|
const apiKey = resolveApiKey(credentialApiKey(context.credential));
|
|
@@ -7442,20 +7490,6 @@ async function refreshPlexusModels(state, context) {
|
|
|
7442
7490
|
},
|
|
7443
7491
|
update: () => {
|
|
7444
7492
|
state.models = filteredCurrent;
|
|
7445
|
-
const cachedTiers = collectStoredServiceTiers(filteredCurrent);
|
|
7446
|
-
state.serviceTiers?.setCatalog(cachedTiers.complete ? "ready" : "unavailable", cachedTiers.policies, cachedTiers.complete ? {
|
|
7447
|
-
cached: true,
|
|
7448
|
-
...cachedTiers.fetchedAt === undefined ? {} : { fetchedAt: cachedTiers.fetchedAt }
|
|
7449
|
-
} : {
|
|
7450
|
-
cached: true,
|
|
7451
|
-
reason: SERVICE_TIERS_METADATA_UNAVAILABLE_REASON
|
|
7452
|
-
});
|
|
7453
|
-
const cachedPolicies = collectStoredContextPolicies(filteredCurrent);
|
|
7454
|
-
state.contextPolicies?.setCatalog(cachedPolicies.length > 0 ? "ready" : "unavailable", cachedPolicies.map((entry) => entry.policy), cachedPolicies.length > 0 ? { cached: true, fetchedAt: cachedPolicies[0]?.fetchedAt } : {
|
|
7455
|
-
cached: true,
|
|
7456
|
-
reason: CONTEXT_POLICY_METADATA_UNAVAILABLE_REASON
|
|
7457
|
-
});
|
|
7458
|
-
state.shortNames?.setCatalog("ready", collectStoredShortNames(filteredCurrent), { cached: true });
|
|
7459
7493
|
state.policy?.reconcile();
|
|
7460
7494
|
}
|
|
7461
7495
|
});
|
|
@@ -7464,16 +7498,8 @@ async function refreshPlexusModels(state, context) {
|
|
|
7464
7498
|
const restored = await restoreStoredModels(state, context);
|
|
7465
7499
|
if (restored)
|
|
7466
7500
|
return restored;
|
|
7467
|
-
if (
|
|
7468
|
-
|
|
7469
|
-
reason: "Plexus model metadata is not configured."
|
|
7470
|
-
});
|
|
7471
|
-
}
|
|
7472
|
-
if (refreshId === state.refreshSequence && state.shortNames?.getSnapshot().status === "loading") {
|
|
7473
|
-
state.shortNames.setCatalog("unavailable", [], {
|
|
7474
|
-
reason: "Plexus model metadata is not configured."
|
|
7475
|
-
});
|
|
7476
|
-
}
|
|
7501
|
+
if (!context.allowNetwork)
|
|
7502
|
+
return [];
|
|
7477
7503
|
throw new Error(!modelsUrl || !baseUrl ? "Plexus base URL not configured. Run /login plexus first." : "No Plexus API key configured. Run /login plexus first.");
|
|
7478
7504
|
}
|
|
7479
7505
|
try {
|
|
@@ -7501,9 +7527,6 @@ async function refreshPlexusModels(state, context) {
|
|
|
7501
7527
|
else
|
|
7502
7528
|
delete piModel.plexusShortName;
|
|
7503
7529
|
}
|
|
7504
|
-
const committedPolicies = collectStoredContextPolicies(piModels);
|
|
7505
|
-
const committedServiceTiers = collectStoredServiceTiers(piModels).policies;
|
|
7506
|
-
const committedShortNames = collectStoredShortNames(piModels);
|
|
7507
7530
|
const published = await context.publish({
|
|
7508
7531
|
persist: {
|
|
7509
7532
|
models: piModels,
|
|
@@ -7512,13 +7535,6 @@ async function refreshPlexusModels(state, context) {
|
|
|
7512
7535
|
update: () => {
|
|
7513
7536
|
state.models = piModels;
|
|
7514
7537
|
state.catalogSource = "live refresh";
|
|
7515
|
-
state.serviceTiers?.setCatalog("ready", committedServiceTiers, {
|
|
7516
|
-
fetchedAt
|
|
7517
|
-
});
|
|
7518
|
-
state.shortNames?.setCatalog("ready", committedShortNames, {
|
|
7519
|
-
fetchedAt
|
|
7520
|
-
});
|
|
7521
|
-
state.contextPolicies?.setCatalog(committedPolicies.length > 0 ? "ready" : "unavailable", committedPolicies.map((entry) => entry.policy), committedPolicies.length > 0 ? { fetchedAt } : { fetchedAt, reason: CONTEXT_POLICY_METADATA_UNAVAILABLE_REASON });
|
|
7522
7538
|
state.policy?.reconcile();
|
|
7523
7539
|
}
|
|
7524
7540
|
});
|
|
@@ -7528,21 +7544,6 @@ async function refreshPlexusModels(state, context) {
|
|
|
7528
7544
|
log("refreshModels: fetched", { count: piModels.length });
|
|
7529
7545
|
return piModels;
|
|
7530
7546
|
} catch (error) {
|
|
7531
|
-
if (refreshId === state.refreshSequence && state.contextPolicies?.getSnapshot().status === "loading") {
|
|
7532
|
-
state.contextPolicies.setCatalog("unavailable", [], {
|
|
7533
|
-
reason: "Plexus model metadata could not be loaded."
|
|
7534
|
-
});
|
|
7535
|
-
}
|
|
7536
|
-
if (refreshId === state.refreshSequence && state.serviceTiers?.getSnapshot().status === "loading") {
|
|
7537
|
-
state.serviceTiers.setCatalog("unavailable", [], {
|
|
7538
|
-
reason: "Plexus model metadata could not be loaded."
|
|
7539
|
-
});
|
|
7540
|
-
}
|
|
7541
|
-
if (refreshId === state.refreshSequence && state.shortNames?.getSnapshot().status === "loading") {
|
|
7542
|
-
state.shortNames.setCatalog("unavailable", [], {
|
|
7543
|
-
reason: "Plexus model metadata could not be loaded."
|
|
7544
|
-
});
|
|
7545
|
-
}
|
|
7546
7547
|
log("refreshModels: fetch failed", { error: String(error) });
|
|
7547
7548
|
throw error;
|
|
7548
7549
|
}
|
|
@@ -7557,19 +7558,6 @@ async function restoreStoredModels(state, context) {
|
|
|
7557
7558
|
update: () => {
|
|
7558
7559
|
state.models = models;
|
|
7559
7560
|
state.catalogSource = "host store";
|
|
7560
|
-
const cachedTiers = collectStoredServiceTiers(models);
|
|
7561
|
-
state.serviceTiers?.setCatalog(cachedTiers.complete ? "ready" : "unavailable", cachedTiers.policies, cachedTiers.complete ? {
|
|
7562
|
-
cached: true,
|
|
7563
|
-
...cachedTiers.fetchedAt === undefined ? {} : { fetchedAt: cachedTiers.fetchedAt }
|
|
7564
|
-
} : { cached: true, reason: SERVICE_TIERS_METADATA_UNAVAILABLE_REASON });
|
|
7565
|
-
const cachedPolicies = collectStoredContextPolicies(models);
|
|
7566
|
-
state.contextPolicies?.setCatalog(cachedPolicies.length > 0 ? "ready" : "unavailable", cachedPolicies.map((entry) => entry.policy), cachedPolicies.length > 0 ? { cached: true, fetchedAt: cachedPolicies[0]?.fetchedAt } : {
|
|
7567
|
-
cached: true,
|
|
7568
|
-
reason: CONTEXT_POLICY_METADATA_UNAVAILABLE_REASON
|
|
7569
|
-
});
|
|
7570
|
-
state.shortNames?.setCatalog("ready", collectStoredShortNames(models), {
|
|
7571
|
-
cached: true
|
|
7572
|
-
});
|
|
7573
7561
|
state.policy?.reconcile();
|
|
7574
7562
|
}
|
|
7575
7563
|
});
|
|
@@ -7589,12 +7577,6 @@ function contextPolicyFromApiModel(model) {
|
|
|
7589
7577
|
pricingThresholdInputTokens
|
|
7590
7578
|
};
|
|
7591
7579
|
}
|
|
7592
|
-
function collectStoredContextPolicies(models) {
|
|
7593
|
-
return models.flatMap((model) => {
|
|
7594
|
-
const metadata = model.plexusContextPolicy;
|
|
7595
|
-
return metadata ? [metadata] : [];
|
|
7596
|
-
});
|
|
7597
|
-
}
|
|
7598
7580
|
function serviceTierPolicyFromApiModel(model) {
|
|
7599
7581
|
const serviceTiers = model.service_tiers;
|
|
7600
7582
|
if (!Array.isArray(serviceTiers) || serviceTiers.length === 0)
|
|
@@ -7618,29 +7600,6 @@ function shortNameEntryFromApiModel(model) {
|
|
|
7618
7600
|
});
|
|
7619
7601
|
return parsed.success ? parsed.data : undefined;
|
|
7620
7602
|
}
|
|
7621
|
-
function collectStoredShortNames(models) {
|
|
7622
|
-
return models.flatMap((model) => {
|
|
7623
|
-
const shortName = model.plexusShortName;
|
|
7624
|
-
if (typeof shortName !== "string" || !shortName.trim())
|
|
7625
|
-
return [];
|
|
7626
|
-
const parsed = ShortNameEntrySchema.safeParse({
|
|
7627
|
-
provider: PROVIDER_NAME,
|
|
7628
|
-
modelId: model.id,
|
|
7629
|
-
shortName: shortName.trim()
|
|
7630
|
-
});
|
|
7631
|
-
return parsed.success ? [parsed.data] : [];
|
|
7632
|
-
});
|
|
7633
|
-
}
|
|
7634
|
-
function collectStoredServiceTiers(models) {
|
|
7635
|
-
const metadata = models.map((model) => model.plexusServiceTiers);
|
|
7636
|
-
const complete = models.length === 0 || metadata.every((entry) => entry !== undefined);
|
|
7637
|
-
const stored = metadata.filter((entry) => entry !== undefined);
|
|
7638
|
-
return {
|
|
7639
|
-
policies: stored.flatMap((entry) => entry.policy ? [entry.policy] : []),
|
|
7640
|
-
complete,
|
|
7641
|
-
...stored[0] ? { fetchedAt: stored[0].fetchedAt } : {}
|
|
7642
|
-
};
|
|
7643
|
-
}
|
|
7644
7603
|
function credentialApiKey(credential) {
|
|
7645
7604
|
if (!credential)
|
|
7646
7605
|
return;
|