@lynn123411/dsh-subagent-judge-dispatch 0.6.15 → 0.6.16

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/lib/index.js CHANGED
@@ -406,6 +406,9 @@ function createJudgeCredentials(service, initialEndpoint) {
406
406
  };
407
407
  }
408
408
 
409
+ // src/server/judge-policy.ts
410
+ import { symbols } from "@deepseek-ai/cordis";
411
+
409
412
  // src/server/config.ts
410
413
  import z from "@deepseek-ai/schemastery";
411
414
 
@@ -483,11 +486,15 @@ function outcomeReason(outcome, decision) {
483
486
  }
484
487
 
485
488
  // src/server/judge-policy.ts
489
+ function serviceIdentity(llm) {
490
+ return llm?.[symbols.original] ?? llm;
491
+ }
486
492
  function createJudgePolicy(options) {
487
493
  let previousRows;
488
494
  let previousSignature = "";
489
495
  let rules = [];
490
496
  const settings = () => readConfigState(options.refs);
497
+ const isCurrentLlm = (llm) => serviceIdentity(options.llm()) === serviceIdentity(llm);
491
498
  function currentRules() {
492
499
  const stored = options.refs.rows.get();
493
500
  if (stored !== previousRows) {
@@ -500,31 +507,36 @@ function createJudgePolicy(options) {
500
507
  }
501
508
  return rules;
502
509
  }
503
- async function isInCatalog(route, budget) {
510
+ async function readCatalog(provider, llm, budget) {
511
+ if (llm === void 0) return void 0;
504
512
  try {
505
- return await budget.run(async () => {
506
- const llm = options.llm();
507
- if (llm === void 0) return false;
508
- const listed = await llm.isModelInCatalog(route.provider, route.model);
509
- return listed === true && options.llm() === llm;
510
- });
511
- } catch {
513
+ const models = await budget.run(() => llm.listModels(provider));
514
+ return new Set(models.map(({ id }) => id));
515
+ } catch (error) {
512
516
  budget.check();
513
- return false;
517
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "NO_ADAPTER") {
518
+ return /* @__PURE__ */ new Set();
519
+ }
520
+ return void 0;
514
521
  }
515
522
  }
516
523
  async function eligibleRows(rows, budget) {
517
- const lookups = /* @__PURE__ */ new Map();
518
- const available = await Promise.all(rows.map((row) => {
519
- const route = { provider: row.provider.trim(), model: row.model.trim() };
520
- const key = JSON.stringify([route.provider, route.model]);
521
- const existing = lookups.get(key);
522
- if (existing !== void 0) return existing;
523
- const lookup = isInCatalog(route, budget);
524
- lookups.set(key, lookup);
525
- return lookup;
526
- }));
527
- return rows.filter((_row, index) => available[index]);
524
+ const llm = options.llm();
525
+ const providers = new Set(rows.map((row) => row.provider.trim()));
526
+ const catalogs = new Map(await Promise.all(Array.from(
527
+ providers,
528
+ async (provider) => [provider, await readCatalog(provider, llm, budget)]
529
+ )));
530
+ if (llm === void 0 || !isCurrentLlm(llm)) return { rows: [], failed: providers.size > 0 };
531
+ return {
532
+ rows: rows.filter((row) => catalogs.get(row.provider.trim())?.has(row.model.trim())),
533
+ failed: Array.from(catalogs.values()).some((catalog) => catalog === void 0)
534
+ };
535
+ }
536
+ async function checkCatalog(route, llm, budget) {
537
+ const catalog = await readCatalog(route.provider, llm, budget);
538
+ if (llm === void 0 || catalog === void 0 || !isCurrentLlm(llm)) return "catalog-unavailable";
539
+ return catalog.has(route.model) ? void 0 : "unavailable-route";
528
540
  }
529
541
  function prepare(snapshot) {
530
542
  if (!snapshot.endpointUrl || !snapshot.judgeModel) return "unconfigured";
@@ -542,18 +554,19 @@ function createJudgePolicy(options) {
542
554
  }
543
555
  async function previewRows(snapshot, budget) {
544
556
  const plan = { url: snapshot.endpointUrl, model: snapshot.judgeModel, rules: currentRules() };
545
- const rows = await eligibleRows(plan.rules, budget);
557
+ const candidates = await eligibleRows(plan.rules, budget);
546
558
  budget.check();
547
559
  if (!planIsCurrent(plan)) throw new Error("Judgement settings changed while preparing the preview");
548
- return rows;
560
+ if (candidates.failed && candidates.rows.length === 0) throw new Error("Could not read the DSH model catalog");
561
+ return candidates.rows;
549
562
  }
550
563
  async function checkFinalRoute(plan, route, budget, checkTask = () => void 0) {
551
564
  const stale = () => planIsCurrent(plan) ? checkTask() : "settings-changed";
552
565
  try {
553
566
  const changed = stale();
554
567
  if (changed !== void 0) return changed;
555
- const listed = await isInCatalog(route, budget);
556
- return stale() ?? (listed ? void 0 : "unavailable-route");
568
+ const reason = await checkCatalog(route, options.llm(), budget);
569
+ return stale() ?? reason;
557
570
  } catch {
558
571
  return budget.expired() ? "timeout" : "unavailable-route";
559
572
  }
@@ -574,12 +587,12 @@ function createJudgePolicy(options) {
574
587
  if (changed !== void 0) return { kind: "keep", stage: "judge", reason: changed };
575
588
  const candidates = await eligibleRows(plan.rules, budget);
576
589
  const changedAfterLookup = stale();
577
- if (changedAfterLookup !== void 0 || candidates.length === 0) {
578
- return { kind: "keep", stage: "judge", reason: changedAfterLookup ?? "no-authorized-route" };
590
+ if (changedAfterLookup !== void 0 || candidates.rows.length === 0) {
591
+ return { kind: "keep", stage: "judge", reason: changedAfterLookup ?? (candidates.failed ? "catalog-unavailable" : "no-authorized-route") };
579
592
  }
580
593
  outcome = await judgeTaskType(
581
594
  { url: plan.url, model: plan.model, apiKey },
582
- candidates,
595
+ candidates.rows,
583
596
  composeJudgeSubject(subject.label, subject.text),
584
597
  (url, init) => {
585
598
  rejectedBeforeSend = stale();
@@ -590,7 +603,7 @@ function createJudgePolicy(options) {
590
603
  budget
591
604
  );
592
605
  } catch {
593
- return { kind: "keep", stage: "judge", reason: budget.expired() ? "timeout" : "no-authorized-route" };
606
+ return { kind: "keep", stage: "judge", reason: budget.expired() ? "timeout" : "catalog-unavailable" };
594
607
  } finally {
595
608
  timings.judge = Math.round(performance.now() - judgeStarted);
596
609
  }
@@ -605,20 +618,22 @@ function createJudgePolicy(options) {
605
618
  const changed = stale();
606
619
  if (changed !== void 0) return { kind: "keep", stage: "preflight", reason: changed, match, metrics };
607
620
  const llm = options.llm();
608
- if (llm === void 0 || !await isInCatalog(match.route, budget)) {
609
- return { kind: "keep", stage: "preflight", reason: "unavailable-route", match, metrics };
621
+ if (llm === void 0) return { kind: "keep", stage: "preflight", reason: "catalog-unavailable", match, metrics };
622
+ const catalogReason = await checkCatalog(match.route, llm, budget);
623
+ if (catalogReason !== void 0) {
624
+ return { kind: "keep", stage: "preflight", reason: catalogReason, match, metrics };
610
625
  }
611
626
  const changedAfterLookup = stale();
612
627
  if (changedAfterLookup !== void 0) {
613
628
  return { kind: "keep", stage: "preflight", reason: changedAfterLookup, match, metrics };
614
629
  }
615
- if (options.llm() !== llm) return { kind: "keep", stage: "preflight", reason: "unavailable-route", match, metrics };
630
+ if (!isCurrentLlm(llm)) return { kind: "keep", stage: "preflight", reason: "unavailable-route", match, metrics };
616
631
  const resolved = await budget.run((signal) => llm.resolveCallConfig(match.route, signal));
617
632
  const changedAfterValidation = stale();
618
633
  if (changedAfterValidation !== void 0) {
619
634
  return { kind: "keep", stage: "preflight", reason: changedAfterValidation, match, metrics };
620
635
  }
621
- if (options.llm() !== llm) return { kind: "keep", stage: "preflight", reason: "unavailable-route", match, metrics };
636
+ if (!isCurrentLlm(llm)) return { kind: "keep", stage: "preflight", reason: "unavailable-route", match, metrics };
622
637
  const effort = resolved?.reasoningEffort;
623
638
  return { kind: "ready", match: { ...match, resolvedEffort: typeof effort === "string" ? effort : match.route.reasoningEffort }, metrics };
624
639
  } catch {
@@ -842,6 +857,7 @@ var REASONS = [
842
857
  "unavailable-route",
843
858
  "unconfigured",
844
859
  "no-authorized-route",
860
+ "catalog-unavailable",
845
861
  "unidentified-task",
846
862
  "explicit-route",
847
863
  "recorded-route",