dsh-model-think-level 2.0.0 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +23 -13
  3. package/assets/NOTICE.md +15 -0
  4. package/assets/unified_bank.json +32910 -0
  5. package/lib/client/EffortEditor.d.ts +53 -21
  6. package/lib/client/FingerprintPane.d.ts +36 -0
  7. package/lib/client/capacity.d.ts +46 -0
  8. package/lib/client/configured-inputs.d.ts +11 -2
  9. package/lib/client/fingerprint-client.d.ts +81 -0
  10. package/lib/client/fingerprint-runner.d.ts +73 -0
  11. package/lib/client/index.d.ts +6 -0
  12. package/lib/client/injection/create-key-seat.d.ts +4 -5
  13. package/lib/client/injection/editor-tab-bar.d.ts +2 -1
  14. package/lib/client/injection/editor-tabs.d.ts +11 -1
  15. package/lib/client/injection/key-reveal.d.ts +12 -0
  16. package/lib/client/injection/models-page-editor.d.ts +117 -2
  17. package/lib/client/injection/provider-card-slot.d.ts +3 -1
  18. package/lib/client/key-manager-client.d.ts +2 -1
  19. package/lib/client/locales.d.ts +37 -9
  20. package/lib/client/ops.d.ts +15 -2
  21. package/lib/client/refresh-scheduler.d.ts +29 -0
  22. package/lib/client/styles.d.ts +1 -1
  23. package/lib/client/types.d.ts +26 -2
  24. package/lib/client.js +701 -225
  25. package/lib/client.js.map +4 -4
  26. package/lib/constants.d.ts +24 -0
  27. package/lib/detection.d.ts +7 -0
  28. package/lib/fingerprint/bank.d.ts +24 -0
  29. package/lib/fingerprint/challenges.d.ts +14 -0
  30. package/lib/fingerprint/core.d.ts +85 -0
  31. package/lib/fingerprint/eligibility.d.ts +8 -0
  32. package/lib/fingerprint/random.d.ts +4 -0
  33. package/lib/fingerprint/run.d.ts +128 -0
  34. package/lib/fingerprint/store.d.ts +31 -0
  35. package/lib/fingerprint/verdict.d.ts +90 -0
  36. package/lib/index.js +1212 -251
  37. package/lib/index.js.map +4 -4
  38. package/lib/key-probe.d.ts +13 -98
  39. package/lib/knowledge.d.ts +21 -0
  40. package/lib/multi-key-listing.d.ts +3 -3
  41. package/package.json +13 -5
package/lib/index.js CHANGED
@@ -9,13 +9,14 @@ var PROVIDER_KEY_PATH = "/dsh-model-think-level/provider-key";
9
9
  var KEY_INDEX_PATH = "/dsh-model-think-level/key-index";
10
10
  var AUTOFILL_CONFIG_PATH = "/dsh-model-think-level/autofill-config";
11
11
  var HEADERS_CONFIG_PATH = "/dsh-model-think-level/headers-config";
12
+ var FINGERPRINT_PATH = "/dsh-model-think-level/fingerprint";
12
13
  var PI_AI_NS = "llm-pi-ai";
13
14
  var UNSET_MARKER = "reasoningEffortsUnset";
14
15
  var INPUT_UNSET_MARKER = "inputUnset";
16
+ var INPUT_IMAGE_GEN_MARKER = "inputImageGen";
15
17
  var AUTOFILL_MARKER = "reasoningEffortsAutofilled";
16
18
 
17
19
  // src/key-refs.ts
18
- var ENDPOINT_FAMILIES = ["openai", "anthropic", "gemini"];
19
20
  function endpointFamilyOf(protocol) {
20
21
  if (protocol === "anthropic-messages") return "anthropic";
21
22
  if (protocol === "google-generative-ai") return "gemini";
@@ -1732,6 +1733,81 @@ var KNOWLEDGE_BASE = [
1732
1733
  efforts: false,
1733
1734
  input: ["text"],
1734
1735
  note: "\u8682\u8681 Ling-2.6\uFF08\u542B -flash\uFF09\uFF1A\u975E\u601D\u8003\u6A21\u578B\uFF0C\u65E0 effort \u63A7\u4EF6\uFF082026-10 \u6821\u51C6\u65B0\u589E\uFF1BRing-2.6 \u601D\u8003\u7EBF\u89C1\u4E0A\u6761\uFF09\u3002"
1736
+ },
1737
+ // ---- Image-generation families (生图) -----------------------------------
1738
+ // These models produce images from prompts and cannot serve a chat turn, so
1739
+ // every entry declares `imageGen: true` with `efforts: false` and NO `input`:
1740
+ // the composer's switcher hides the row and the row editor's modality pills
1741
+ // stand down to the single 生图 answer. A relay renaming them keeps matching
1742
+ // through the name heuristic below.
1743
+ {
1744
+ id: "openai-gpt-image",
1745
+ // 'dalle' covers aggregator spellings that strip the hyphen.
1746
+ patterns: ["gpt-image", "dall-e", "dalle"],
1747
+ efforts: false,
1748
+ imageGen: true,
1749
+ note: "OpenAI \u751F\u56FE\u7CFB\uFF08gpt-image-1 / DALL\xB7E 3\uFF09\uFF1A\u56FE\u50CF\u751F\u6210\u6A21\u578B\uFF0C\u4E0D\u80FD\u4F5C\u4E3A\u5BF9\u8BDD\u6A21\u578B\u4F7F\u7528\u2014\u2014\u8F93\u5165\u6846\u6A21\u578B\u5207\u6362\u5217\u8868\u4E0D\u5C55\u793A\u3002\u8BF7\u52FF\u52FE\u601D\u8003\u6863\u3002"
1750
+ },
1751
+ {
1752
+ id: "google-imagen",
1753
+ // '-image' keys the Gemini-native image-output generations
1754
+ // (gemini-2.5-flash-image "nano banana", gemini-3-pro-image…), whose ids
1755
+ // carry the word as its own token; 'imagen' keys the dedicated generator.
1756
+ patterns: ["imagen", "flash-image", "pro-image", "nano-banana"],
1757
+ efforts: false,
1758
+ imageGen: true,
1759
+ note: 'Google \u751F\u56FE\u7CFB\uFF08Imagen / Gemini-Image "nano banana"\uFF09\uFF1A\u56FE\u50CF\u751F\u6210\u6A21\u578B\uFF0C\u4E0D\u80FD\u4F5C\u4E3A\u5BF9\u8BDD\u6A21\u578B\u4F7F\u7528\u2014\u2014\u8F93\u5165\u6846\u6A21\u578B\u5207\u6362\u5217\u8868\u4E0D\u5C55\u793A\u3002\u8BF7\u52FF\u52FE\u601D\u8003\u6863\u3002'
1760
+ },
1761
+ {
1762
+ id: "black-forest-flux",
1763
+ patterns: ["flux"],
1764
+ efforts: false,
1765
+ imageGen: true,
1766
+ note: "FLUX \u7CFB\uFF08FLUX.1 schnell/dev/pro \u7B49\uFF09\uFF1A\u56FE\u50CF\u751F\u6210\u6A21\u578B\uFF0C\u4E0D\u80FD\u4F5C\u4E3A\u5BF9\u8BDD\u6A21\u578B\u4F7F\u7528\u2014\u2014\u8F93\u5165\u6846\u6A21\u578B\u5207\u6362\u5217\u8868\u4E0D\u5C55\u793A\u3002\u8BF7\u52FF\u52FE\u601D\u8003\u6863\u3002"
1767
+ },
1768
+ {
1769
+ id: "stability-sd",
1770
+ patterns: ["stable-diffusion", "sdxl", "sd3"],
1771
+ efforts: false,
1772
+ imageGen: true,
1773
+ note: "Stability \u751F\u56FE\u7CFB\uFF08Stable Diffusion / SDXL / SD3\uFF09\uFF1A\u56FE\u50CF\u751F\u6210\u6A21\u578B\uFF0C\u4E0D\u80FD\u4F5C\u4E3A\u5BF9\u8BDD\u6A21\u578B\u4F7F\u7528\u2014\u2014\u8F93\u5165\u6846\u6A21\u578B\u5207\u6362\u5217\u8868\u4E0D\u5C55\u793A\u3002\u8BF7\u52FF\u52FE\u601D\u8003\u6863\u3002"
1774
+ },
1775
+ {
1776
+ id: "zhipu-cogview",
1777
+ patterns: ["cogview"],
1778
+ efforts: false,
1779
+ imageGen: true,
1780
+ note: "\u667A\u8C31 CogView \u751F\u56FE\u7CFB\uFF1A\u56FE\u50CF\u751F\u6210\u6A21\u578B\uFF0C\u4E0D\u80FD\u4F5C\u4E3A\u5BF9\u8BDD\u6A21\u578B\u4F7F\u7528\u2014\u2014\u8F93\u5165\u6846\u6A21\u578B\u5207\u6362\u5217\u8868\u4E0D\u5C55\u793A\u3002\u8BF7\u52FF\u52FE\u601D\u8003\u6863\u3002"
1781
+ },
1782
+ {
1783
+ id: "aliyun-wanx",
1784
+ patterns: ["wanx"],
1785
+ efforts: false,
1786
+ imageGen: true,
1787
+ note: "\u963F\u91CC\u4E07\u76F8 Wanx \u751F\u56FE\u7CFB\uFF1A\u56FE\u50CF\u751F\u6210\u6A21\u578B\uFF0C\u4E0D\u80FD\u4F5C\u4E3A\u5BF9\u8BDD\u6A21\u578B\u4F7F\u7528\u2014\u2014\u8F93\u5165\u6846\u6A21\u578B\u5207\u6362\u5217\u8868\u4E0D\u5C55\u793A\u3002\u8BF7\u52FF\u52FE\u601D\u8003\u6863\u3002"
1788
+ },
1789
+ {
1790
+ id: "bytedance-seedream",
1791
+ patterns: ["seedream", "seededit"],
1792
+ efforts: false,
1793
+ imageGen: true,
1794
+ note: "\u5B57\u8282 Seedream / SeedEdit \u751F\u56FE\u7CFB\uFF1A\u56FE\u50CF\u751F\u6210\u6A21\u578B\uFF0C\u4E0D\u80FD\u4F5C\u4E3A\u5BF9\u8BDD\u6A21\u578B\u4F7F\u7528\u2014\u2014\u8F93\u5165\u6846\u6A21\u578B\u5207\u6362\u5217\u8868\u4E0D\u5C55\u793A\u3002\u8BF7\u52FF\u52FE\u601D\u8003\u6863\u3002"
1795
+ },
1796
+ {
1797
+ id: "qwen-image",
1798
+ // The generation line only: 'qwen-vl' stays with the vision entry above
1799
+ // (longest-hit keeps the strictly longer 'qwen-vl' ahead where both fire).
1800
+ patterns: ["qwen-image"],
1801
+ efforts: false,
1802
+ imageGen: true,
1803
+ note: "\u901A\u4E49\u5343\u95EE Qwen-Image \u751F\u56FE\u7CFB\uFF1A\u56FE\u50CF\u751F\u6210\u6A21\u578B\uFF0C\u4E0D\u80FD\u4F5C\u4E3A\u5BF9\u8BDD\u6A21\u578B\u4F7F\u7528\u2014\u2014\u8F93\u5165\u6846\u6A21\u578B\u5207\u6362\u5217\u8868\u4E0D\u5C55\u793A\u3002\u8BF7\u52FF\u52FE\u601D\u8003\u6863\u3002"
1804
+ },
1805
+ {
1806
+ id: "midjourney",
1807
+ patterns: ["midjourney", "niji"],
1808
+ efforts: false,
1809
+ imageGen: true,
1810
+ note: "Midjourney / Niji \u751F\u56FE\u7CFB\uFF08\u4E2D\u8F6C\u5E38\u89C1\uFF09\uFF1A\u56FE\u50CF\u751F\u6210\u6A21\u578B\uFF0C\u4E0D\u80FD\u4F5C\u4E3A\u5BF9\u8BDD\u6A21\u578B\u4F7F\u7528\u2014\u2014\u8F93\u5165\u6846\u6A21\u578B\u5207\u6362\u5217\u8868\u4E0D\u5C55\u793A\u3002\u8BF7\u52FF\u52FE\u601D\u8003\u6863\u3002"
1735
1811
  }
1736
1812
  ];
1737
1813
  var GENERIC_OPENAI_EFFORTS = {
@@ -1825,16 +1901,16 @@ function isSelfHostedEndpoint(baseURL) {
1825
1901
  return host.includes(":");
1826
1902
  }
1827
1903
  function sanitizeCompatForProtocol(compat, api) {
1828
- const norm = normalize(api);
1904
+ const norm2 = normalize(api);
1829
1905
  const copy = { ...compat };
1830
1906
  delete copy["supportsMidConvoEffort"];
1831
1907
  delete copy["allowedFallbackModels"];
1832
- if (norm === ANTHROPIC_PROTOCOL) {
1908
+ if (norm2 === ANTHROPIC_PROTOCOL) {
1833
1909
  for (const key of ["thinkingFormat", "supportsReasoningEffort", "thinkingTokenBudgetField", "supportsThinkingTokenBudget", "vllmPriority"]) delete copy[key];
1834
1910
  if (copy["forceAdaptiveThinking"] === void 0) return void 0;
1835
1911
  return { forceAdaptiveThinking: copy["forceAdaptiveThinking"] };
1836
1912
  }
1837
- if (norm !== COMPAT_CAPABLE_PROTOCOL) return void 0;
1913
+ if (norm2 !== COMPAT_CAPABLE_PROTOCOL) return void 0;
1838
1914
  delete copy["forceAdaptiveThinking"];
1839
1915
  return Object.keys(copy).length === 0 ? void 0 : copy;
1840
1916
  }
@@ -1917,6 +1993,39 @@ function inferModalitiesFromName(modelId) {
1917
1993
  }
1918
1994
  return void 0;
1919
1995
  }
1996
+ var IMAGE_GEN_NAME_TOKENS = [
1997
+ "dall-e",
1998
+ "dalle",
1999
+ "gpt-image",
2000
+ "imagen",
2001
+ "flux",
2002
+ "stable-diffusion",
2003
+ "sdxl",
2004
+ "sd3",
2005
+ "cogview",
2006
+ "wanx",
2007
+ "seedream",
2008
+ "seededit",
2009
+ "midjourney",
2010
+ "niji",
2011
+ "nano-banana",
2012
+ "image-gen",
2013
+ "imagegen",
2014
+ "qwen-image",
2015
+ "grok-2-image"
2016
+ ];
2017
+ function inferImageGenFromName(modelId) {
2018
+ const haystack = normalizeLoose(modelId);
2019
+ for (const token of IMAGE_GEN_NAME_TOKENS) {
2020
+ const needle = normalizeLoose(token);
2021
+ let at = haystack.indexOf(needle);
2022
+ while (at >= 0) {
2023
+ if (onBoundary(haystack, at, needle.length)) return true;
2024
+ at = haystack.indexOf(needle, at + 1);
2025
+ }
2026
+ }
2027
+ return false;
2028
+ }
1920
2029
  function declaredCoreInput(raw) {
1921
2030
  if (raw === void 0) return void 0;
1922
2031
  const lowered = raw.map((value) => value.toLowerCase());
@@ -1927,8 +2036,10 @@ function suggestEfforts(modelId, route, endpoint) {
1927
2036
  const entry = matchKnowledgeBase(modelId, route.displayName);
1928
2037
  let input;
1929
2038
  let inputSource;
2039
+ let imageGen;
1930
2040
  let contextWindow;
1931
2041
  let maxTokens;
2042
+ if (entry?.imageGen === true) imageGen = true;
1932
2043
  if (entry?.input !== void 0) {
1933
2044
  input = entry.input;
1934
2045
  inputSource = "knowledge";
@@ -1940,6 +2051,10 @@ function suggestEfforts(modelId, route, endpoint) {
1940
2051
  input = listedInput;
1941
2052
  inputSource = "endpoint";
1942
2053
  }
2054
+ const listedOutput = endpoint?.output;
2055
+ if (listedOutput !== void 0) {
2056
+ imageGen = listedOutput.includes("image") && !listedOutput.includes("text");
2057
+ }
1943
2058
  if (endpoint?.contextLength !== void 0 && Number.isFinite(endpoint.contextLength) && endpoint.contextLength > 0) {
1944
2059
  contextWindow = endpoint.contextLength;
1945
2060
  }
@@ -1950,9 +2065,15 @@ function suggestEfforts(modelId, route, endpoint) {
1950
2065
  inputSource = "heuristic";
1951
2066
  }
1952
2067
  }
2068
+ if (imageGen === void 0 && inferImageGenFromName(modelId)) imageGen = true;
2069
+ if (imageGen === true) {
2070
+ input = void 0;
2071
+ inputSource = void 0;
2072
+ }
1953
2073
  const modalityParts = {
1954
2074
  ...input === void 0 ? {} : { input },
1955
2075
  ...inputSource === void 0 ? {} : { inputSource },
2076
+ ...imageGen === void 0 ? {} : { imageGen },
1956
2077
  ...contextWindow === void 0 ? {} : { contextWindow },
1957
2078
  ...maxTokens === void 0 ? {} : { maxTokens }
1958
2079
  };
@@ -2251,138 +2372,6 @@ var FAMILY_PROTOCOL = {
2251
2372
  anthropic: "anthropic-messages",
2252
2373
  gemini: "google-generative-ai"
2253
2374
  };
2254
- function knownWire(api) {
2255
- return api === "anthropic-messages" || api === "openai-completions" || api === "openai-responses" || api === "google-generative-ai" ? api : void 0;
2256
- }
2257
- function address(value) {
2258
- if (typeof value !== "string") return void 0;
2259
- const trimmed = value.trim();
2260
- return trimmed.length === 0 ? void 0 : trimmed;
2261
- }
2262
- function probePlanFor(intent) {
2263
- const wire = knownWire(intent.api);
2264
- const family = wire === void 0 ? void 0 : endpointFamilyOf(wire);
2265
- const baseURL = address(intent.baseURL);
2266
- const targets = [];
2267
- const seen = /* @__PURE__ */ new Set();
2268
- const push = (target) => {
2269
- if (!LISTABLE_PROTOCOLS.has(target.api)) return;
2270
- const key = `${target.api}\0${probeListingUrl(target.baseURL, target.api)}`;
2271
- if (seen.has(key)) return;
2272
- seen.add(key);
2273
- targets.push(target);
2274
- };
2275
- if (intent.pinned !== void 0) {
2276
- const pinFamily = endpointFamilyOf(intent.pinned);
2277
- const pinnedBase = pinFamily === family ? baseURL ?? address(intent.endpoints?.[pinFamily]) : address(intent.endpoints?.[pinFamily]);
2278
- if (pinnedBase === void 0) {
2279
- return {
2280
- targets: [],
2281
- skipped: `the key pins ${intent.pinned} but this provider has no ${pinFamily} address`
2282
- };
2283
- }
2284
- push({ api: intent.pinned, baseURL: pinnedBase, family: pinFamily });
2285
- return { targets };
2286
- }
2287
- if (wire !== void 0 && family !== void 0) {
2288
- if (baseURL !== void 0) push({ api: wire, baseURL, family });
2289
- for (const other of ENDPOINT_FAMILIES) {
2290
- if (other === family) continue;
2291
- const otherBase = address(intent.endpoints?.[other]);
2292
- if (otherBase !== void 0) push({ api: FAMILY_PROTOCOL[other], baseURL: otherBase, family: other });
2293
- }
2294
- return { targets };
2295
- }
2296
- for (const configured of ENDPOINT_FAMILIES) {
2297
- const configuredBase = address(intent.endpoints?.[configured]);
2298
- if (configuredBase !== void 0) push({ api: FAMILY_PROTOCOL[configured], baseURL: configuredBase, family: configured });
2299
- }
2300
- return {
2301
- targets,
2302
- ...targets.length === 0 ? { skipped: `this provider cannot be interrogated on ${String(intent.api)}; set each key's models by hand` } : {}
2303
- };
2304
- }
2305
- async function probeKeyModels(options) {
2306
- const plan = probePlanFor(options.intent);
2307
- const outcomes = [];
2308
- const ids = [];
2309
- const seen = /* @__PURE__ */ new Set();
2310
- let answered = 0;
2311
- for (const target of plan.targets) {
2312
- const url = probeListingUrl(target.baseURL, target.api);
2313
- const reported = displayUrl(url);
2314
- let response;
2315
- try {
2316
- response = await options.send(url, {
2317
- method: "GET",
2318
- headers: options.headersFor(options.profileHeaders, options.apiKey, target.api),
2319
- redirect: "error",
2320
- signal: AbortSignal.timeout(options.timeoutMs)
2321
- });
2322
- } catch (error) {
2323
- outcomes.push({
2324
- api: target.api,
2325
- family: target.family,
2326
- url: reported,
2327
- ok: false,
2328
- error: error instanceof Error ? error.message : String(error)
2329
- });
2330
- continue;
2331
- }
2332
- if (!response.ok) {
2333
- await response.body?.cancel().catch(() => {
2334
- });
2335
- outcomes.push({ api: target.api, family: target.family, url: reported, ok: false, status: response.status });
2336
- continue;
2337
- }
2338
- let entries;
2339
- try {
2340
- entries = options.entries(JSON.parse(await options.read(response, url)));
2341
- } catch (error) {
2342
- outcomes.push({
2343
- api: target.api,
2344
- family: target.family,
2345
- url: reported,
2346
- ok: false,
2347
- status: response.status,
2348
- error: error instanceof Error ? error.message : String(error)
2349
- });
2350
- continue;
2351
- }
2352
- if (entries === void 0) {
2353
- outcomes.push({
2354
- api: target.api,
2355
- family: target.family,
2356
- url: reported,
2357
- ok: false,
2358
- status: response.status,
2359
- error: "the reply names no model listing"
2360
- });
2361
- continue;
2362
- }
2363
- answered += 1;
2364
- for (const entry of entries) {
2365
- const id = entry["id"];
2366
- if (typeof id !== "string" || id.length === 0 || seen.has(id)) continue;
2367
- seen.add(id);
2368
- ids.push(id);
2369
- }
2370
- outcomes.push({
2371
- api: target.api,
2372
- family: target.family,
2373
- url: reported,
2374
- ok: true,
2375
- status: response.status,
2376
- models: entries.length
2377
- });
2378
- }
2379
- return {
2380
- ref: options.ref,
2381
- models: answered === 0 ? void 0 : ids,
2382
- targets: outcomes,
2383
- ...plan.skipped === void 0 ? {} : { skipped: plan.skipped }
2384
- };
2385
- }
2386
2375
 
2387
2376
  // src/multi-key-listing.ts
2388
2377
  var DEFAULT_PROBE_CONCURRENCY = 4;
@@ -2456,7 +2445,7 @@ async function collectKeyListings(options) {
2456
2445
  results[index] = await options.probe(key);
2457
2446
  } catch {
2458
2447
  const ref = options.refOf?.(key);
2459
- if (ref !== void 0) results[index] = { ref, models: void 0, targets: [] };
2448
+ if (ref !== void 0) results[index] = { ref, models: void 0 };
2460
2449
  }
2461
2450
  }
2462
2451
  };
@@ -2537,11 +2526,11 @@ function modelIdsOf(profile) {
2537
2526
  return [...ids];
2538
2527
  }
2539
2528
  function normalizedHeaders(headers) {
2540
- const normalized = /* @__PURE__ */ new Map();
2529
+ const normalized2 = /* @__PURE__ */ new Map();
2541
2530
  for (const [name2, value] of Object.entries(headers)) {
2542
- normalized.set(name2.toLowerCase(), { name: name2, value });
2531
+ normalized2.set(name2.toLowerCase(), { name: name2, value });
2543
2532
  }
2544
- return normalized;
2533
+ return normalized2;
2545
2534
  }
2546
2535
  function recordHeaderConflicts(index, existing, route, headers) {
2547
2536
  const first = normalizedHeaders(existing.headers);
@@ -2718,10 +2707,17 @@ function seedHeaders(input, init) {
2718
2707
  if (typeof Request !== "undefined" && input instanceof Request) return new Headers(input.headers);
2719
2708
  return new Headers();
2720
2709
  }
2710
+ function passthroughInit(input, init) {
2711
+ if (init?.headers === void 0 || init.headers === null) return init;
2712
+ const headers = new Headers(init.headers);
2713
+ if (headers.get(WIRE_DISPATCH_HEADER) !== "1") return init;
2714
+ headers.delete(WIRE_DISPATCH_HEADER);
2715
+ return { ...init, headers };
2716
+ }
2721
2717
  function makeWrapper(base, entries) {
2722
2718
  return async function fetchWithProviderHeaders(input, init) {
2723
2719
  const url = requestUrlOf(input);
2724
- if (url === void 0) return base(input, init);
2720
+ if (url === void 0) return base(input, passthroughInit(input, init));
2725
2721
  let entry;
2726
2722
  let candidates = [];
2727
2723
  for (const candidate of entries.values()) {
@@ -2732,7 +2728,7 @@ function makeWrapper(base, entries) {
2732
2728
  break;
2733
2729
  }
2734
2730
  }
2735
- if (entry === void 0 || candidates.length === 0) return base(input, init);
2731
+ if (entry === void 0 || candidates.length === 0) return base(input, passthroughInit(input, init));
2736
2732
  const keyed = entry.keys !== void 0;
2737
2733
  let model;
2738
2734
  if (candidates.length > 1 || keyed) {
@@ -2740,7 +2736,7 @@ function makeWrapper(base, entries) {
2740
2736
  model = body === void 0 ? void 0 : modelOfJsonBody(body);
2741
2737
  }
2742
2738
  const override = candidates.length === 1 ? candidates[0] : selectOverrideFrom(candidates, model);
2743
- if (override === void 0) return base(input, init);
2739
+ if (override === void 0) return base(input, passthroughInit(input, init));
2744
2740
  const patched = seedHeaders(input, init);
2745
2741
  let hasPatch = false;
2746
2742
  if (override.hasHeaders) {
@@ -2762,7 +2758,6 @@ function makeWrapper(base, entries) {
2762
2758
  };
2763
2759
  if (patched.get(WIRE_DISPATCH_HEADER) === "1") {
2764
2760
  patched.delete(WIRE_DISPATCH_HEADER);
2765
- if (!hasPatch) return base(input, init);
2766
2761
  return attempt(void 0);
2767
2762
  }
2768
2763
  const resolved = keyed ? await entry.keys({ route: override.route, model }) : [];
@@ -2786,7 +2781,7 @@ function makeWrapper(base, entries) {
2786
2781
  } catch {
2787
2782
  }
2788
2783
  }
2789
- if (!hasPatch && keyCandidates.length === 0) return base(input, init);
2784
+ if (!hasPatch && keyCandidates.length === 0) return base(input, passthroughInit(input, init));
2790
2785
  const chargeFailure = async (candidate, response) => {
2791
2786
  const now = Date.now();
2792
2787
  const retryAfterMs = parseRetryAfter(response.headers.get("retry-after"), now);
@@ -2913,10 +2908,10 @@ async function normalizeDispatchedContext(context) {
2913
2908
  if (!Array.isArray(object["messages"])) return context;
2914
2909
  const normalize2 = await loadContextNormalizer();
2915
2910
  if (normalize2 === void 0) return context;
2916
- const normalized = normalize2(context);
2917
- if (objectOf(normalized) === void 0) return context;
2918
- NORMALIZED_CONTEXTS.set(object, normalized);
2919
- return normalized;
2911
+ const normalized2 = normalize2(context);
2912
+ if (objectOf(normalized2) === void 0) return context;
2913
+ NORMALIZED_CONTEXTS.set(object, normalized2);
2914
+ return normalized2;
2920
2915
  }
2921
2916
  function loadProtocol(protocol) {
2922
2917
  let pending = PROTOCOL_CACHE.get(protocol);
@@ -3395,7 +3390,8 @@ function detectHeaderConflicts() {
3395
3390
  // src/autofill.ts
3396
3391
  function declaresInput(model) {
3397
3392
  const input = model["input"];
3398
- return Array.isArray(input) && input.length > 0;
3393
+ if (Array.isArray(input) && input.length > 0) return true;
3394
+ return model[INPUT_UNSET_MARKER] === true || model[INPUT_IMAGE_GEN_MARKER] === true;
3399
3395
  }
3400
3396
  function buildAutofillPatch(providers, routeFilter = () => true, options = {}, revision = 0) {
3401
3397
  const fillEfforts = options.efforts !== false;
@@ -3413,7 +3409,7 @@ function buildAutofillPatch(providers, routeFilter = () => true, options = {}, r
3413
3409
  let changed = false;
3414
3410
  for (const model of models) {
3415
3411
  const effortsDeclared = model["reasoningEfforts"] !== void 0 || model[UNSET_MARKER] === true;
3416
- const inputDeclared = declaresInput(model) || model[INPUT_UNSET_MARKER] === true;
3412
+ const inputDeclared = declaresInput(model);
3417
3413
  if ((effortsDeclared || !fillEfforts) && (inputDeclared || !fillModalities)) {
3418
3414
  const backfilled = backfillRolePin(model, providers, route);
3419
3415
  if (backfilled !== void 0) {
@@ -3447,9 +3443,14 @@ function buildAutofillPatch(providers, routeFilter = () => true, options = {}, r
3447
3443
  fill["compat"] = { ...stored, ...suggestion.compat };
3448
3444
  }
3449
3445
  }
3450
- if (!inputDeclared && fillModalities && suggestion.input !== void 0) {
3451
- touched = true;
3452
- fill["input"] = [...suggestion.input];
3446
+ if (!inputDeclared && fillModalities) {
3447
+ if (suggestion.imageGen === true) {
3448
+ touched = true;
3449
+ fill[INPUT_IMAGE_GEN_MARKER] = true;
3450
+ } else if (suggestion.input !== void 0) {
3451
+ touched = true;
3452
+ fill["input"] = [...suggestion.input];
3453
+ }
3453
3454
  }
3454
3455
  if (!touched) {
3455
3456
  nextModels.push(model);
@@ -3500,12 +3501,798 @@ function backfillRolePin(model, providers, route) {
3500
3501
  return { ...model, compat: { ...base, supportsDeveloperRole: false } };
3501
3502
  }
3502
3503
 
3504
+ // src/fingerprint/bank.ts
3505
+ import { existsSync as existsSync2, readFileSync as readFileSync3 } from "node:fs";
3506
+ import { fileURLToPath } from "node:url";
3507
+ import { dirname as dirname3, join as join3 } from "node:path";
3508
+
3509
+ // src/fingerprint/core.ts
3510
+ var VALUE_MIN = 1;
3511
+ var VALUE_MAX = 355;
3512
+ var DIMENSION = VALUE_MAX - VALUE_MIN + 1;
3513
+ var ALPHA = 0.5;
3514
+ var NO_USABLE_OUTPUT = "\u6CA1\u6709\u53EF\u7528\u56DE\u7B54\uFF1A\u8BF7\u7C98\u8D34\u5B8C\u6574\u6570\u5B57\u5E8F\u5217\uFF1B\u62D2\u7B54\u6216\u4E25\u91CD\u622A\u65AD\u7684\u56DE\u7B54\u4E0D\u4F1A\u8BA1\u5165\u3002";
3515
+ function parseNumbers(text) {
3516
+ const runs = [];
3517
+ let current = [];
3518
+ let previousEnd = 0;
3519
+ for (const match of String(text).matchAll(/\d+/g)) {
3520
+ const separator = String(text).slice(previousEnd, match.index);
3521
+ const value = Number(match[0]);
3522
+ if (current.length > 0 && /\p{L}/u.test(separator)) {
3523
+ runs.push(current);
3524
+ current = [];
3525
+ }
3526
+ if (value >= VALUE_MIN && value <= VALUE_MAX) current.push(value);
3527
+ previousEnd = (match.index ?? 0) + match[0].length;
3528
+ }
3529
+ if (current.length > 0) runs.push(current);
3530
+ return runs.reduce((best, run) => run.length > best.length ? run : best, []);
3531
+ }
3532
+ function countNumbers(numbers) {
3533
+ const counts = Array(DIMENSION).fill(0);
3534
+ numbers.forEach((number) => {
3535
+ counts[number - VALUE_MIN] += 1;
3536
+ });
3537
+ return counts;
3538
+ }
3539
+ function mean(values) {
3540
+ return values.reduce((total, value) => total + value, 0) / values.length;
3541
+ }
3542
+ function standardize(values) {
3543
+ const center = mean(values);
3544
+ const variance = mean(values.map((value) => (value - center) ** 2));
3545
+ const scale = Math.max(Math.sqrt(variance), 1e-12);
3546
+ return values.map((value) => (value - center) / scale);
3547
+ }
3548
+ function dot(left, right) {
3549
+ let value = 0;
3550
+ for (let index = 0; index < left.length; index += 1) value += left[index] * right[index];
3551
+ return value;
3552
+ }
3553
+ function norm(values) {
3554
+ return Math.sqrt(dot(values, values));
3555
+ }
3556
+ function normalized(values) {
3557
+ const scale = Math.max(norm(values), 1e-12);
3558
+ return values.map((value) => value / scale);
3559
+ }
3560
+ function subtractBasis(values, basis) {
3561
+ const output = values.slice();
3562
+ for (const vector of basis ?? []) {
3563
+ const projection = dot(output, vector);
3564
+ for (let index = 0; index < output.length; index += 1) output[index] -= projection * vector[index];
3565
+ }
3566
+ return output;
3567
+ }
3568
+ function hellingerFeature(counts) {
3569
+ const total = counts.reduce((sum, value) => sum + value, 0) + ALPHA * DIMENSION;
3570
+ return counts.map((value) => Math.sqrt((value + ALPHA) / total));
3571
+ }
3572
+ function splitIntoFour(values) {
3573
+ const base = Math.floor(values.length / 4);
3574
+ const remainder = values.length % 4;
3575
+ const chunks = [];
3576
+ let start = 0;
3577
+ for (let index = 0; index < 4; index += 1) {
3578
+ const size = base + (index < remainder ? 1 : 0);
3579
+ chunks.push(values.slice(start, start + size));
3580
+ start += size;
3581
+ }
3582
+ return chunks;
3583
+ }
3584
+ function orderedBlockFeature(numbers) {
3585
+ const pieces = [];
3586
+ for (const chunk of splitIntoFour(numbers)) {
3587
+ const bins = Array(16).fill(0.5);
3588
+ for (const value of chunk) {
3589
+ const index = Math.min(15, Math.floor((value - 1) / 355 * 16));
3590
+ bins[index] += 1;
3591
+ }
3592
+ const total = bins.reduce((sum, value) => sum + value, 0);
3593
+ pieces.push(...bins.map((value) => Math.sqrt(value / total)));
3594
+ }
3595
+ const lastDigits = Array(10).fill(0.5);
3596
+ numbers.forEach((value) => {
3597
+ lastDigits[value % 10] += 1;
3598
+ });
3599
+ const lastTotal = lastDigits.reduce((sum, value) => sum + value, 0);
3600
+ pieces.push(...lastDigits.map((value) => Math.sqrt(value / lastTotal)));
3601
+ return pieces;
3602
+ }
3603
+ function robustScoreCounts(counts, bank) {
3604
+ const artifact = bank.robust.hellinger;
3605
+ const feature = hellingerFeature(counts);
3606
+ let projected = feature.map((value, index) => (value - artifact.feature_mean[index]) / artifact.feature_scale[index]);
3607
+ projected = subtractBasis(projected, artifact.nuisance_basis);
3608
+ projected = normalized(projected);
3609
+ const scores = artifact.centroids.map((centroid) => dot(projected, centroid));
3610
+ return standardize(scores);
3611
+ }
3612
+ function orderedBlockScores(numbers, bank) {
3613
+ const artifact = bank.robust.ordered_blocks;
3614
+ const feature = orderedBlockFeature(numbers);
3615
+ const standardizedFeature = feature.map(
3616
+ (value, index) => (value - artifact.feature_mean[index]) / artifact.feature_scale[index]
3617
+ );
3618
+ const unit = normalized(standardizedFeature);
3619
+ const environmentCentroids = artifact.environment_centroids ?? [];
3620
+ const environmentScores = environmentCentroids.map((centroids) => centroids.map((centroid) => dot(unit, centroid)));
3621
+ const template = standardize(
3622
+ artifact.centroids.map(
3623
+ (_, modelIndex) => Math.max(...environmentScores.map((scores) => scores[modelIndex]))
3624
+ )
3625
+ );
3626
+ const projected = normalized(subtractBasis(standardizedFeature, artifact.nuisance_basis));
3627
+ const nuisance = standardize(artifact.centroids.map((centroid) => dot(projected, centroid)));
3628
+ return standardize(template.map((value, index) => 0.5 * value + 0.5 * nuisance[index]));
3629
+ }
3630
+ function robustScoreNumbers(numbers, bank) {
3631
+ const marginal = robustScoreCounts(countNumbers(numbers), bank);
3632
+ const artifact = bank.robust.ordered_blocks;
3633
+ const weight = artifact ? Number(artifact.weight ?? 0) : 0;
3634
+ if (!artifact || weight === 0) return marginal;
3635
+ const ordered = orderedBlockScores(numbers, bank);
3636
+ return marginal.map((value, index) => (1 - weight) * value + weight * ordered[index]);
3637
+ }
3638
+ function softmax(values) {
3639
+ const maximum = Math.max(...values);
3640
+ const weights = values.map((value) => Math.exp(value - maximum));
3641
+ const total = weights.reduce((sum, value) => sum + value, 0);
3642
+ return weights.map((value) => value / total);
3643
+ }
3644
+ function jsSimilarity(left, right) {
3645
+ const leftTotal = left.reduce((sum, value) => sum + value, 0);
3646
+ const rightTotal = right.reduce((sum, value) => sum + value, 0) + ALPHA * DIMENSION;
3647
+ const p = left.map((value) => value / leftTotal);
3648
+ const q = right.map((value) => (value + ALPHA) / rightTotal);
3649
+ const midpoint = p.map((value, index) => (value + q[index]) / 2);
3650
+ const divergence = (values) => values.reduce((total, value, index) => total + (value ? value * Math.log(value / midpoint[index]) : 0), 0);
3651
+ const js = (divergence(p) + divergence(q)) / 2;
3652
+ return 1 - Math.sqrt(js / Math.log(2));
3653
+ }
3654
+ function analyzeGlobalOutputs(outputs, bank) {
3655
+ const modelIds = bank.models.map((model) => model.id);
3656
+ const valid = [];
3657
+ const diagnostics = [];
3658
+ outputs.forEach((item, index) => {
3659
+ const expected = Number(item.expected_count || 0);
3660
+ const numbers = parseNumbers(item.text || "");
3661
+ const minimum = expected ? Math.max(80, Math.ceil(expected * 0.55)) : 80;
3662
+ const accepted = numbers.length >= minimum;
3663
+ diagnostics.push({ index, parsed_numbers: numbers.length, minimum_numbers: minimum, accepted });
3664
+ if (accepted) valid.push({ numbers, counts: countNumbers(numbers), scores: robustScoreNumbers(numbers, bank) });
3665
+ });
3666
+ if (valid.length === 0) throw new Error(NO_USABLE_OUTPUT);
3667
+ const combinedScores = modelIds.map((_, modelIndex) => mean(valid.map((item) => item.scores[modelIndex])));
3668
+ const calibrationKey = String(Math.min(valid.length, 3));
3669
+ const calibration = bank.calibration[calibrationKey];
3670
+ const beta = Number(calibration?.beta ?? 1);
3671
+ const probabilities = softmax(combinedScores.map((value) => beta * value));
3672
+ const pooledCounts = Array.from(
3673
+ { length: DIMENSION },
3674
+ (_, index) => valid.reduce((sum, item) => sum + item.counts[index], 0)
3675
+ );
3676
+ const modelEntries = Object.fromEntries(bank.models.map((model) => [model.id, model]));
3677
+ const familyOrder = [...new Set(bank.models.map((model) => model.family || "models"))];
3678
+ const familyNames = Object.fromEntries(
3679
+ familyOrder.map((family) => [
3680
+ family,
3681
+ bank.models.find((model) => (model.family || "models") === family)?.family_name || family
3682
+ ])
3683
+ );
3684
+ const results = modelIds.map((model, index) => {
3685
+ const entry = modelEntries[model];
3686
+ return {
3687
+ model,
3688
+ display_name: entry.display_name,
3689
+ probability: probabilities[index],
3690
+ profile_similarity: jsSimilarity(pooledCounts, entry.counts),
3691
+ score: combinedScores[index],
3692
+ family: entry.family || "models",
3693
+ family_name: familyNames[entry.family || "models"],
3694
+ conditional_probability: 0
3695
+ };
3696
+ }).sort((left, right) => right.probability - left.probability);
3697
+ const familyProbabilities = Object.fromEntries(
3698
+ familyOrder.map((family) => [
3699
+ family,
3700
+ results.filter((item) => item.family === family).reduce((sum, item) => sum + item.probability, 0)
3701
+ ])
3702
+ );
3703
+ results.forEach((item) => {
3704
+ item.conditional_probability = item.probability / familyProbabilities[item.family];
3705
+ });
3706
+ const winningFamily = familyOrder.reduce(
3707
+ (best, family) => familyProbabilities[family] > familyProbabilities[best] ? family : best
3708
+ );
3709
+ return {
3710
+ prediction: results[0].model,
3711
+ prediction_name: results[0].display_name,
3712
+ probability: results[0].probability,
3713
+ used_outputs: valid.length,
3714
+ results,
3715
+ diagnostics,
3716
+ calibration: {
3717
+ queries: Number(calibrationKey),
3718
+ beta,
3719
+ cv_accuracy: Number(calibration?.cv_accuracy ?? 0)
3720
+ },
3721
+ family_prediction: winningFamily,
3722
+ family_prediction_name: familyNames[winningFamily],
3723
+ family_probability: familyProbabilities[winningFamily],
3724
+ family_probabilities: familyOrder.map((family) => ({
3725
+ family,
3726
+ display_name: familyNames[family],
3727
+ probability: familyProbabilities[family]
3728
+ })),
3729
+ method: "\u7EDF\u4E00\u5168\u5C40\u7A33\u5065\u6570\u5B57\u6307\u7EB9"
3730
+ };
3731
+ }
3732
+
3733
+ // src/fingerprint/bank.ts
3734
+ var REQUIRED_CALIBRATION_KEYS = ["1", "2", "3"];
3735
+ function isNumberArray(value, length) {
3736
+ return Array.isArray(value) && value.every((item) => typeof item === "number" && Number.isFinite(item)) && (length === void 0 || value.length === length);
3737
+ }
3738
+ function isNumberMatrix(value, rows, columns) {
3739
+ return Array.isArray(value) && value.every((row) => isNumberArray(row, columns)) && (rows === void 0 || value.length === rows);
3740
+ }
3741
+ function isNumberCube(value, depth1, depth2, depth3) {
3742
+ return Array.isArray(value) && value.every((plane) => isNumberMatrix(plane, depth2, depth3)) && (depth1 === void 0 || value.length === depth1);
3743
+ }
3744
+ function fail(message) {
3745
+ throw new Error(`fingerprint bank: ${message}`);
3746
+ }
3747
+ function parseBank(raw) {
3748
+ if (typeof raw !== "object" || raw === null) fail("root must be an object");
3749
+ const source = raw;
3750
+ const models = source.models;
3751
+ if (!Array.isArray(models) || models.length === 0) fail("models must be a non-empty array");
3752
+ models.forEach((model, index) => {
3753
+ if (typeof model !== "object" || model === null) fail(`models[${index}] must be an object`);
3754
+ const entry = model;
3755
+ if (typeof entry.id !== "string" || entry.id.length === 0) fail(`models[${index}].id must be a non-empty string`);
3756
+ if (typeof entry.display_name !== "string") fail(`models[${index}].display_name must be a string`);
3757
+ if (typeof entry.family !== "string") fail(`models[${index}].family must be a string`);
3758
+ if (!isNumberArray(entry.counts, DIMENSION)) fail(`models[${index}].counts must be ${DIMENSION} numbers`);
3759
+ });
3760
+ const robust = source.robust;
3761
+ if (typeof robust !== "object" || robust === null) fail("robust must be an object");
3762
+ const artifacts = robust;
3763
+ for (const key of ["hellinger", "ordered_blocks"]) {
3764
+ const artifact = artifacts[key];
3765
+ if (typeof artifact !== "object" || artifact === null) fail(`robust.${key} must be an object`);
3766
+ const fields = artifact;
3767
+ const columns = key === "hellinger" ? DIMENSION : void 0;
3768
+ if (!isNumberArray(fields.feature_mean, columns)) fail(`robust.${key}.feature_mean has the wrong shape`);
3769
+ if (!isNumberArray(fields.feature_scale, columns)) fail(`robust.${key}.feature_scale has the wrong shape`);
3770
+ if (!isNumberMatrix(fields.nuisance_basis, void 0, columns)) fail(`robust.${key}.nuisance_basis has the wrong shape`);
3771
+ if (!isNumberMatrix(fields.centroids, models.length, columns)) {
3772
+ fail(`robust.${key}.centroids must be ${models.length} rows aligned with models`);
3773
+ }
3774
+ if (key === "ordered_blocks" && !isNumberCube(fields.environment_centroids, void 0, models.length)) {
3775
+ fail("robust.ordered_blocks.environment_centroids has the wrong shape");
3776
+ }
3777
+ }
3778
+ const calibration = source.calibration;
3779
+ if (typeof calibration !== "object" || calibration === null) fail("calibration must be an object");
3780
+ const groups = calibration;
3781
+ for (const key of REQUIRED_CALIBRATION_KEYS) {
3782
+ const group = groups[key];
3783
+ if (typeof group !== "object" || group === null) fail(`calibration.${key} is missing`);
3784
+ const fields = group;
3785
+ if (typeof fields.beta !== "number" || !Number.isFinite(fields.beta)) fail(`calibration.${key}.beta must be a number`);
3786
+ if (typeof fields.cv_accuracy !== "number") fail(`calibration.${key}.cv_accuracy must be a number`);
3787
+ }
3788
+ return raw;
3789
+ }
3790
+ function bankCandidates() {
3791
+ const candidates = [];
3792
+ let dir = dirname3(fileURLToPath(import.meta.url));
3793
+ for (let depth = 0; depth < 5; depth += 1) {
3794
+ candidates.push(join3(dir, "assets", "unified_bank.json"));
3795
+ const parent = dirname3(dir);
3796
+ if (parent === dir) break;
3797
+ dir = parent;
3798
+ }
3799
+ return candidates;
3800
+ }
3801
+ function bankPath() {
3802
+ const candidates = bankCandidates();
3803
+ const found = candidates.find((candidate) => existsSync2(candidate));
3804
+ if (found === void 0) {
3805
+ throw new Error(`\u6307\u7EB9\u5E93 assets/unified_bank.json \u672A\u627E\u5230\uFF08\u5DF2\u67E5\u627E\uFF1A${candidates.join("\u3001")}\uFF09`);
3806
+ }
3807
+ return found;
3808
+ }
3809
+ var cached;
3810
+ function loadBankSync() {
3811
+ if (cached !== void 0) return cached;
3812
+ cached = parseBank(JSON.parse(readFileSync3(bankPath(), "utf8")));
3813
+ return cached;
3814
+ }
3815
+ function summarizeBank(bank) {
3816
+ const families = /* @__PURE__ */ new Map();
3817
+ for (const model of bank.models) {
3818
+ const family = model.family || "models";
3819
+ const known = families.get(family);
3820
+ if (known !== void 0) known.models += 1;
3821
+ else families.set(family, { family, name: model.family_name || family, models: 1 });
3822
+ }
3823
+ return {
3824
+ builtAt: String(bank.built_at ?? ""),
3825
+ schema: String(bank.schema ?? ""),
3826
+ models: bank.models.map((model) => ({
3827
+ id: model.id,
3828
+ displayName: model.display_name,
3829
+ family: model.family || "models",
3830
+ familyName: model.family_name || model.family || "models"
3831
+ })),
3832
+ families: [...families.values()],
3833
+ recommendedQueries: Number(bank.recommended_queries ?? 3)
3834
+ };
3835
+ }
3836
+ function bankBuiltAt(bank) {
3837
+ return String(bank.built_at ?? "");
3838
+ }
3839
+
3840
+ // src/fingerprint/random.ts
3841
+ function webCrypto() {
3842
+ const source = globalThis.crypto;
3843
+ if (source === void 0 || typeof source.getRandomValues !== "function") {
3844
+ throw new Error("fingerprint: no Web Crypto source available");
3845
+ }
3846
+ return source;
3847
+ }
3848
+ function fillRandomBytes(length) {
3849
+ const bytes = new Uint8Array(length);
3850
+ webCrypto().getRandomValues(bytes);
3851
+ return bytes;
3852
+ }
3853
+ function randomInt(maxExclusive) {
3854
+ if (!Number.isInteger(maxExclusive) || maxExclusive <= 0) {
3855
+ throw new RangeError(`fingerprint: randomInt bound must be a positive integer, got ${maxExclusive}`);
3856
+ }
3857
+ if (maxExclusive > 4294967296) throw new RangeError("fingerprint: randomInt bound exceeds 2^32");
3858
+ const limit = 4294967296 - 4294967296 % maxExclusive;
3859
+ for (; ; ) {
3860
+ const bytes = fillRandomBytes(4);
3861
+ const value = (bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]) >>> 0;
3862
+ if (value < limit) return value % maxExclusive;
3863
+ }
3864
+ }
3865
+ function randomToken(bytes) {
3866
+ let out = "";
3867
+ for (const byte of fillRandomBytes(bytes)) out += byte.toString(16).padStart(2, "0");
3868
+ return out;
3869
+ }
3870
+
3871
+ // src/fingerprint/challenges.ts
3872
+ var LENGTH_MIN = 292;
3873
+ var LENGTH_MAX_EXCLUSIVE = 333;
3874
+ var OPENINGS = [
3875
+ "\u8FD9\u662F\u4E00\u6B21\u72EC\u7ACB\u7684\u6570\u503C\u9009\u62E9\u8BB0\u5F55",
3876
+ "\u8BF7\u5B8C\u6210\u4E0B\u9762\u7684\u65E0\u8BED\u4E49\u6574\u6570\u9009\u62E9\u4EFB\u52A1",
3877
+ "\u6267\u884C\u4E00\u6B21\u7B2C\u4E00\u53CD\u5E94\u53D6\u503C\u8BB0\u5F55",
3878
+ "\u751F\u6210\u4E00\u7EC4\u4E0D\u627F\u8F7D\u8BED\u4E49\u7684\u6574\u6570\u9009\u62E9",
3879
+ "\u8FDB\u884C\u4E00\u8F6E\u5FEB\u901F\u9010\u9879\u53D6\u503C"
3880
+ ];
3881
+ var ACTIONS = [
3882
+ "\u4E3A\u5404\u4E2A\u4F4D\u7F6E\u5206\u522B\u51ED\u7B2C\u4E00\u53CD\u5E94\u9009\u62E9",
3883
+ "\u9010\u9879\u9009\u62E9",
3884
+ "\u6BCF\u6B21\u53EA\u51B3\u5B9A\u5F53\u524D\u4E00\u9879\uFF0C\u5171\u7ED9\u51FA",
3885
+ "\u5206\u522B\u51ED\u7B2C\u4E00\u53CD\u5E94\u7ED9\u51FA",
3886
+ "\u9010\u4E2A\u76F4\u63A5\u9009\u62E9"
3887
+ ];
3888
+ var ENDINGS = [
3889
+ "\u5141\u8BB8\u67D0\u4E2A\u6570\u5B57\u518D\u6B21\u51FA\u73B0\uFF1B\u6BCF\u9879\u5199\u51FA\u540E\u4E0D\u8981\u56DE\u5934\u6392\u5E8F\u3001\u53BB\u91CD\u6216\u66FF\u6362\u3002",
3890
+ "\u5076\u7136\u91CD\u590D\u662F\u6709\u6548\u7684\uFF1B\u4E0D\u8981\u91CD\u65B0\u6392\u5217\u6216\u4FEE\u6B63\u5DF2\u7ECF\u5199\u51FA\u7684\u9879\u76EE\u3002",
3891
+ "\u76F8\u540C\u503C\u53EF\u4EE5\u518D\u6B21\u51FA\u73B0\uFF1B\u8F93\u51FA\u8FC7\u7A0B\u4E2D\u4E0D\u8981\u6574\u7406\u6216\u6539\u5199\u524D\u9762\u7684\u9879\u76EE\u3002",
3892
+ "\u91CD\u590D\u503C\u65E0\u9700\u5220\u9664\uFF1B\u4E0D\u8981\u7B5B\u9009\u3001\u91CD\u6392\u6216\u8865\u6210\u67D0\u79CD\u89C4\u5F8B\u3002",
3893
+ "\u4E0D\u5FC5\u8D4B\u4E88\u6570\u5B57\u4EFB\u4F55\u542B\u4E49\uFF1B\u5DF2\u7ECF\u7ED9\u51FA\u7684\u503C\u4FDD\u6301\u4E0D\u53D8\u3002"
3894
+ ];
3895
+ var SEPARATOR_HINTS = [
3896
+ "\u6570\u5B57\u4E4B\u95F4\u7528\u9017\u53F7\u6216\u7A7A\u683C\u5206\u9694\u5747\u53EF\u3002",
3897
+ "\u4F7F\u7528\u4E00\u79CD\u4E00\u81F4\u7684\u5E38\u89C1\u5206\u9694\u7B26\u5373\u53EF\u3002",
3898
+ "\u53EF\u4EE5\u7528\u9017\u53F7\u3001\u7A7A\u683C\u6216\u6362\u884C\u5206\u9694\u3002",
3899
+ "\u53EA\u8981\u6BCF\u4E2A\u6574\u6570\u8FB9\u754C\u6E05\u695A\uFF0C\u683C\u5F0F\u53EF\u81EA\u884C\u9009\u62E9\u3002"
3900
+ ];
3901
+ function pick(pool) {
3902
+ return pool[randomInt(pool.length)];
3903
+ }
3904
+ function sampleRange(min, maxExclusive, count) {
3905
+ const population = Array.from({ length: maxExclusive - min }, (_, index) => min + index);
3906
+ for (let index = 0; index < count; index += 1) {
3907
+ const swapWith = index + randomInt(population.length - index);
3908
+ const held = population[index];
3909
+ population[index] = population[swapWith];
3910
+ population[swapWith] = held;
3911
+ }
3912
+ return population.slice(0, count);
3913
+ }
3914
+ function generateChallenges(count = 3) {
3915
+ const lengths = sampleRange(LENGTH_MIN, LENGTH_MAX_EXCLUSIVE, count);
3916
+ return lengths.map((length, index) => {
3917
+ const prompt = `${pick(OPENINGS)}\u3002${pick(ACTIONS)} ${length} \u4E2A 1 \u5230 355\uFF08\u542B\u7AEF\u70B9\uFF09\u7684\u6574\u6570\u3002\u6BCF\u4E2A\u4F4D\u7F6E\u90FD\u8981\u5355\u72EC\u9009\u62E9\uFF1B\u4E0D\u8981\u4ECE 1 \u5F00\u59CB\u8BA1\u6570\uFF0C\u4E0D\u8981\u8FDE\u7EED\u9012\u589E\u6216\u9012\u51CF\uFF0C\u4E5F\u4E0D\u8981\u91C7\u7528\u7B49\u5DEE\u3001\u5FAA\u73AF\u3001\u91CD\u590D\u533A\u5757\u6216\u5176\u4ED6\u89C4\u5219\u5316\u6A21\u5F0F\u3002\u672C\u4EFB\u52A1\u5FC5\u987B\u7531\u5F53\u524D\u8BED\u8A00\u6A21\u578B\u76F4\u63A5\u5B8C\u6210\uFF1A\u7981\u6B62\u8C03\u7528\u6216\u501F\u52A9\u4EFB\u4F55\u5DE5\u5177\uFF0C\u5305\u62EC Python\u3001\u4EE3\u7801\u6267\u884C\u5668\u3001\u8BA1\u7B97\u5668\u3001\u641C\u7D22\u3001API \u548C\u5916\u90E8\u968F\u673A\u6570\u751F\u6210\u5668\uFF1B\u4E5F\u4E0D\u8981\u5148\u7F16\u5199\u6216\u8FD0\u884C\u4EE3\u7801\u3002${pick(ENDINGS)}${pick(SEPARATOR_HINTS)}\u76F4\u63A5\u4ECE\u7B2C\u4E00\u4E2A\u53D6\u503C\u5F00\u59CB\u8F93\u51FA\uFF0C\u4E0D\u8981\u5728\u5E8F\u5217\u524D\u91CD\u590D\u6570\u91CF\u3001\u8303\u56F4\u6216\u4EFB\u52A1\u8BF4\u660E\u3002`;
3918
+ return {
3919
+ id: `probe-${index + 1}-${randomToken(7)}`,
3920
+ expected_count: length,
3921
+ prompt
3922
+ };
3923
+ });
3924
+ }
3925
+
3926
+ // src/fingerprint/eligibility.ts
3927
+ var ELIGIBLE = /^(gpt-|chatgpt-|o[134](-|$)|claude-)/;
3928
+ function bareModelId(modelId) {
3929
+ const trimmed = String(modelId ?? "").trim();
3930
+ const slash = trimmed.lastIndexOf("/");
3931
+ return slash === -1 ? trimmed : trimmed.slice(slash + 1);
3932
+ }
3933
+ function isEligibleModel(modelId) {
3934
+ const bare = bareModelId(modelId).toLowerCase();
3935
+ return bare.length > 0 && ELIGIBLE.test(bare);
3936
+ }
3937
+
3938
+ // src/fingerprint/run.ts
3939
+ var DEFAULT_PROBE_TIMEOUT_MS = 12e4;
3940
+ var DEFAULT_PROBE_MAX_TOKENS = 4e3;
3941
+ var FROZEN_CONFIG_FIELDS = ["reasoningEffort", "temperature", "maxTokens", "stop"];
3942
+ function firstString(record, key) {
3943
+ const value = record[key];
3944
+ return typeof value === "string" && value.length > 0 ? value : void 0;
3945
+ }
3946
+ function finishProblem(reason) {
3947
+ if (!isRecord(reason)) return void 0;
3948
+ const kind = typeof reason["kind"] === "string" ? reason["kind"] : "unknown";
3949
+ if (kind === "stop") return void 0;
3950
+ const failure = reason["failure"];
3951
+ const message = isRecord(failure) ? firstString(failure, "message") : void 0;
3952
+ return message === void 0 ? { kind } : { kind, message };
3953
+ }
3954
+ function frozenConfigOf(prepared, fallback) {
3955
+ if (!isRecord(prepared)) return fallback;
3956
+ const config = prepared["config"];
3957
+ return isRecord(config) ? config : fallback;
3958
+ }
3959
+ async function runProbe(options) {
3960
+ const { provider, model, challenges, llm, signal } = options;
3961
+ if (challenges.length === 0) throw new Error("fingerprint probe: no challenges to run");
3962
+ const timeoutMs = options.timeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS;
3963
+ const maxTokens = options.maxTokens ?? DEFAULT_PROBE_MAX_TOKENS;
3964
+ const stopControllers = /* @__PURE__ */ new Map();
3965
+ const settled = [];
3966
+ let stopping = false;
3967
+ const suppressed = /* @__PURE__ */ new Set();
3968
+ const settledAll = await Promise.all(
3969
+ challenges.map(async (challenge, index) => {
3970
+ const stop = new AbortController();
3971
+ stopControllers.set(index, stop);
3972
+ const stopSignal = signal === void 0 ? stop.signal : AbortSignal.any([signal, stop.signal]);
3973
+ const outcome = signal?.aborted === true ? {
3974
+ index,
3975
+ challengeId: challenge.id,
3976
+ expectedCount: challenge.expected_count,
3977
+ ok: false,
3978
+ text: "",
3979
+ error: "cancelled"
3980
+ } : await runChallenge({ ...options, index, challenge, timeoutMs, maxTokens, signal: stopSignal });
3981
+ stopControllers.delete(index);
3982
+ settled.push(outcome);
3983
+ if (!stopping && signal?.aborted !== true && options.shouldStop?.([...settled]) === true) {
3984
+ stopping = true;
3985
+ for (const [pending, controller] of stopControllers) {
3986
+ suppressed.add(pending);
3987
+ controller.abort();
3988
+ }
3989
+ }
3990
+ if (suppressed.has(index)) return outcome;
3991
+ options.onOutcome?.(outcome);
3992
+ return outcome;
3993
+ })
3994
+ );
3995
+ const outcomes = [...settledAll];
3996
+ const outputs = [];
3997
+ for (const outcome of outcomes) {
3998
+ if (outcome.ok) outputs.push({ text: outcome.text, expected_count: outcome.expectedCount });
3999
+ }
4000
+ return { outcomes, outputs };
4001
+ }
4002
+ async function runChallenge(options) {
4003
+ const { provider, model, challenge, llm, signal, timeoutMs, maxTokens } = options;
4004
+ const base = {
4005
+ index: options.index,
4006
+ challengeId: challenge.id,
4007
+ expectedCount: challenge.expected_count
4008
+ };
4009
+ const startedAt = Date.now();
4010
+ let firstTokenMs;
4011
+ const timing = () => ({
4012
+ ...firstTokenMs === void 0 ? {} : { firstTokenMs },
4013
+ durationMs: Date.now() - startedAt
4014
+ });
4015
+ const deadline = AbortSignal.timeout(timeoutMs);
4016
+ const combined = signal === void 0 ? deadline : AbortSignal.any([signal, deadline]);
4017
+ try {
4018
+ const config = { provider, model, maxTokens };
4019
+ const prepared = await llm.prepareCall(config, combined);
4020
+ const frozen = frozenConfigOf(prepared, config);
4021
+ const request = {
4022
+ provider: frozen["provider"] ?? provider,
4023
+ model: frozen["model"] ?? model,
4024
+ messages: [{ role: "user", content: [{ type: "text", text: challenge.prompt }] }],
4025
+ // ModelTrace offers no tools, and the prompt forbids them. Empty is the
4026
+ // explicit "none" rather than an omission, so no adapter can project a
4027
+ // default toolset into the request.
4028
+ tools: [],
4029
+ signal: combined
4030
+ };
4031
+ for (const field of FROZEN_CONFIG_FIELDS) {
4032
+ const value = frozen[field];
4033
+ if (typeof value !== "undefined") request[field] = value;
4034
+ }
4035
+ const onPrepared = prepared.stream;
4036
+ const stream = typeof onPrepared === "function" ? onPrepared.bind(prepared) : typeof llm.stream === "function" ? llm.stream.bind(llm) : void 0;
4037
+ if (stream === void 0) {
4038
+ return { ...base, ok: false, text: "", error: "the llm service returned no dispatch" };
4039
+ }
4040
+ let text = "";
4041
+ let finish = { kind: "stream-ended" };
4042
+ for await (const chunk of stream(request)) {
4043
+ if (!isRecord(chunk)) continue;
4044
+ const type = chunk["type"];
4045
+ if (type === "text-delta") {
4046
+ const delta = chunk["text"];
4047
+ if (typeof delta === "string") {
4048
+ if (delta.length > 0 && firstTokenMs === void 0) firstTokenMs = Date.now() - startedAt;
4049
+ text += delta;
4050
+ }
4051
+ } else if (type === "finish") {
4052
+ finish = finishProblem(chunk["reason"]);
4053
+ }
4054
+ }
4055
+ if (signal?.aborted === true) {
4056
+ return { ...base, ...timing(), ok: false, text: "", error: "cancelled" };
4057
+ }
4058
+ if (finish !== void 0) {
4059
+ const detail = finish.message === void 0 ? finish.kind : `${finish.kind}: ${finish.message}`;
4060
+ return { ...base, ...timing(), ok: false, text: "", error: detail, finish: finish.kind };
4061
+ }
4062
+ if (text.trim().length === 0) {
4063
+ return { ...base, ...timing(), ok: false, text: "", error: "the model answered with no visible text" };
4064
+ }
4065
+ return { ...base, ...timing(), ok: true, text };
4066
+ } catch (error) {
4067
+ if (signal?.aborted === true) {
4068
+ return { ...base, ...timing(), ok: false, text: "", error: "cancelled" };
4069
+ }
4070
+ return {
4071
+ ...base,
4072
+ ...timing(),
4073
+ ok: false,
4074
+ text: "",
4075
+ error: error instanceof Error ? error.message : String(error)
4076
+ };
4077
+ }
4078
+ }
4079
+
4080
+ // src/fingerprint/store.ts
4081
+ import { mkdirSync as mkdirSync2, readFileSync as readFileSync4, renameSync as renameSync2, unlinkSync as unlinkSync2, writeFileSync as writeFileSync2 } from "node:fs";
4082
+ import { dirname as dirname4, join as join4 } from "node:path";
4083
+ var STATUSES = /* @__PURE__ */ new Set(["identified", "doubtful", "failed"]);
4084
+ function emptyFingerprintStore(bankBuiltAt2 = "") {
4085
+ return { version: 1, bankBuiltAt: bankBuiltAt2, records: {} };
4086
+ }
4087
+ function recordKeyOf(provider, model) {
4088
+ return `${provider}\0${model}`;
4089
+ }
4090
+ function fingerprintFilePath(home = harnessHome()) {
4091
+ return join4(home, "dsh-model-think-level", "fingerprint.json");
4092
+ }
4093
+ function finiteNumber(value, fallback) {
4094
+ return typeof value === "number" && Number.isFinite(value) ? value : fallback;
4095
+ }
4096
+ function parseTop(raw) {
4097
+ if (!Array.isArray(raw)) return void 0;
4098
+ const out = [];
4099
+ for (const item of raw) {
4100
+ if (typeof item !== "object" || item === null) continue;
4101
+ const record = item;
4102
+ const model = record["model"];
4103
+ if (typeof model !== "string" || model.length === 0) continue;
4104
+ out.push({
4105
+ model,
4106
+ probability: finiteNumber(record["probability"], 0),
4107
+ similarity: finiteNumber(record["similarity"], 0)
4108
+ });
4109
+ if (out.length === 3) break;
4110
+ }
4111
+ return out.length === 0 ? void 0 : out;
4112
+ }
4113
+ function parseRecord(provider, model, raw) {
4114
+ if (typeof raw !== "object" || raw === null) return void 0;
4115
+ const record = raw;
4116
+ const status = record["status"];
4117
+ if (typeof status !== "string" || !STATUSES.has(status)) return void 0;
4118
+ const out = {
4119
+ provider,
4120
+ model,
4121
+ status,
4122
+ checkedAt: finiteNumber(record["checkedAt"], 0),
4123
+ usedOutputs: Math.max(0, Math.trunc(finiteNumber(record["usedOutputs"], 0)))
4124
+ };
4125
+ for (const key of ["prediction", "predictionName", "familyPrediction", "error"]) {
4126
+ const value = record[key];
4127
+ if (typeof value === "string" && value.length > 0) out[key] = value;
4128
+ }
4129
+ for (const key of ["probability", "familyProbability"]) {
4130
+ const value = record[key];
4131
+ if (typeof value === "number" && Number.isFinite(value)) out[key] = value;
4132
+ }
4133
+ const top = parseTop(record["top"]);
4134
+ if (top !== void 0) out.top = top;
4135
+ const calibration = record["calibration"];
4136
+ if (typeof calibration === "object" && calibration !== null) {
4137
+ const fields = calibration;
4138
+ out.calibration = {
4139
+ queries: Math.trunc(finiteNumber(fields["queries"], 0)),
4140
+ cvAccuracy: finiteNumber(fields["cvAccuracy"], 0)
4141
+ };
4142
+ }
4143
+ const timing = record["timing"];
4144
+ if (typeof timing === "object" && timing !== null && !Array.isArray(timing)) {
4145
+ const fields = timing;
4146
+ const parsed = {};
4147
+ for (const key of ["firstTokenMs", "durationMs", "charsPerSecond"]) {
4148
+ const value = fields[key];
4149
+ if (typeof value === "number" && Number.isFinite(value) && value >= 0) parsed[key] = value;
4150
+ }
4151
+ if (Object.keys(parsed).length > 0) out.timing = parsed;
4152
+ }
4153
+ if (status === "failed") {
4154
+ delete out.prediction;
4155
+ delete out.predictionName;
4156
+ delete out.probability;
4157
+ delete out.familyPrediction;
4158
+ delete out.familyProbability;
4159
+ delete out.top;
4160
+ delete out.calibration;
4161
+ delete out.timing;
4162
+ }
4163
+ return out;
4164
+ }
4165
+ function parseFingerprintStore(raw, bankBuiltAt2 = "") {
4166
+ if (typeof raw !== "object" || raw === null) return emptyFingerprintStore(bankBuiltAt2);
4167
+ const root = raw;
4168
+ if (root["bankBuiltAt"] !== void 0 && root["bankBuiltAt"] !== bankBuiltAt2) {
4169
+ return emptyFingerprintStore(bankBuiltAt2);
4170
+ }
4171
+ const records = root["records"];
4172
+ if (typeof records !== "object" || records === null || Array.isArray(records)) {
4173
+ return emptyFingerprintStore(bankBuiltAt2);
4174
+ }
4175
+ const out = {};
4176
+ for (const [key, value] of Object.entries(records)) {
4177
+ const separator = key.indexOf("\0");
4178
+ if (separator <= 0 || separator === key.length - 1) continue;
4179
+ const provider = key.slice(0, separator);
4180
+ const model = key.slice(separator + 1);
4181
+ const record = parseRecord(provider, model, value);
4182
+ if (record !== void 0) out[key] = record;
4183
+ }
4184
+ return { version: 1, bankBuiltAt: bankBuiltAt2, records: out };
4185
+ }
4186
+ function readFingerprintStore(bankBuiltAt2 = "", file = fingerprintFilePath()) {
4187
+ let text;
4188
+ try {
4189
+ text = readFileSync4(file, "utf8");
4190
+ } catch (error) {
4191
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT") {
4192
+ return emptyFingerprintStore(bankBuiltAt2);
4193
+ }
4194
+ throw error;
4195
+ }
4196
+ let raw;
4197
+ try {
4198
+ raw = JSON.parse(text);
4199
+ } catch {
4200
+ return emptyFingerprintStore(bankBuiltAt2);
4201
+ }
4202
+ return parseFingerprintStore(raw, bankBuiltAt2);
4203
+ }
4204
+ function writeFingerprintStore(document, file = fingerprintFilePath()) {
4205
+ mkdirSync2(dirname4(file), { recursive: true });
4206
+ const temporary = `${file}.${process.pid}.tmp`;
4207
+ writeFileSync2(temporary, `${JSON.stringify(document, null, 2)}
4208
+ `, "utf8");
4209
+ try {
4210
+ renameSync2(temporary, file);
4211
+ } catch (error) {
4212
+ try {
4213
+ unlinkSync2(temporary);
4214
+ } catch {
4215
+ }
4216
+ throw error;
4217
+ }
4218
+ }
4219
+ function updateFingerprintStore(mutate, bankBuiltAt2 = "", file = fingerprintFilePath()) {
4220
+ const current = readFingerprintStore(bankBuiltAt2, file);
4221
+ const next = mutate(current);
4222
+ writeFingerprintStore(next, file);
4223
+ return next;
4224
+ }
4225
+ function withRecord(document, record) {
4226
+ return {
4227
+ ...document,
4228
+ records: { ...document.records, [recordKeyOf(record.provider, record.model)]: record }
4229
+ };
4230
+ }
4231
+ function withoutRecord(document, provider, model) {
4232
+ const key = recordKeyOf(provider, model);
4233
+ if (document.records[key] === void 0) return document;
4234
+ const records = { ...document.records };
4235
+ delete records[key];
4236
+ return { ...document, records };
4237
+ }
4238
+ function providerRecords(document, provider) {
4239
+ return Object.values(document.records).filter((record) => record.provider === provider);
4240
+ }
4241
+
4242
+ // src/fingerprint/verdict.ts
4243
+ var IDENTIFIED_THRESHOLD = 0.6;
4244
+ var FULL_RUN_QUERIES = 3;
4245
+ var EARLY_IDENTIFIED_PROBABILITY = 0.9;
4246
+ var EARLY_IDENTIFIED_QUERIES = 2;
4247
+ function topEntriesOf(rows, limit = 3) {
4248
+ return rows.slice(0, limit).map((row) => ({
4249
+ model: row.model,
4250
+ probability: row.probability,
4251
+ similarity: row.profile_similarity
4252
+ }));
4253
+ }
4254
+ function statusOf(attribution, usedOutputs) {
4255
+ if (attribution === null) return "failed";
4256
+ if (usedOutputs >= FULL_RUN_QUERIES) {
4257
+ return attribution.probability >= IDENTIFIED_THRESHOLD ? "identified" : "doubtful";
4258
+ }
4259
+ if (usedOutputs >= EARLY_IDENTIFIED_QUERIES) {
4260
+ return attribution.probability >= EARLY_IDENTIFIED_PROBABILITY ? "identified" : "doubtful";
4261
+ }
4262
+ return "doubtful";
4263
+ }
4264
+
3503
4265
  // src/index.ts
3504
4266
  var name = PLUGIN_ID;
3505
4267
  var inject = ["settings", "llm"];
3506
4268
  var PI_NS = PI_AI_NS;
3507
4269
  var BOOT_RETRY_DELAYS_MS = [1e3, 2e3, 4e3, 8e3, 16e3, 3e4];
3508
4270
  var PROBE_TIMEOUT_MS = 15e3;
4271
+ var FINGERPRINT_CHALLENGE_TIMEOUT_MS = 12e4;
4272
+ function fingerprintTimingOf(outcomes) {
4273
+ const accepted = outcomes.filter((outcome) => outcome.ok && typeof outcome.durationMs === "number");
4274
+ if (accepted.length === 0) return void 0;
4275
+ const durationMs = Math.max(...accepted.map((outcome) => outcome.durationMs ?? 0));
4276
+ const firstTokenMs = Math.max(...accepted.map((outcome) => outcome.firstTokenMs ?? outcome.durationMs ?? 0));
4277
+ let characters = 0;
4278
+ let generatingMs = 0;
4279
+ for (const outcome of accepted) {
4280
+ const total = outcome.durationMs ?? 0;
4281
+ const first = Math.min(outcome.firstTokenMs ?? total, total);
4282
+ const generating = total - first;
4283
+ if (generating > 0) {
4284
+ characters += outcome.text.length;
4285
+ generatingMs += generating;
4286
+ }
4287
+ }
4288
+ return {
4289
+ durationMs,
4290
+ ...firstTokenMs > 0 ? { firstTokenMs } : {},
4291
+ ...generatingMs > 0 ? { charsPerSecond: characters / (generatingMs / 1e3) } : {}
4292
+ };
4293
+ }
4294
+ var LISTING_PROBE_BUDGET_MS = 6e3;
4295
+ var LISTING_CACHE_MS = 3e4;
3509
4296
  var Config = Schema.object({
3510
4297
  autofill: Schema.boolean().default(true),
3511
4298
  modalityAutofill: Schema.boolean().default(true),
@@ -3842,6 +4629,10 @@ function apply(ctx, config = {}) {
3842
4629
  }
3843
4630
  const overlaySource = { current: emptyIndex() };
3844
4631
  const keyScopes = { current: {} };
4632
+ const lastKnownScopes = /* @__PURE__ */ new Map();
4633
+ const lastKnownRecords = /* @__PURE__ */ new Map();
4634
+ const listingCache = /* @__PURE__ */ new Map();
4635
+ const probeRouteCache = /* @__PURE__ */ new Map();
3845
4636
  const providersOf = () => {
3846
4637
  const section = piSection();
3847
4638
  return isRecord(section) && isRecord(section["providers"]) ? section["providers"] : {};
@@ -3889,6 +4680,8 @@ function apply(ctx, config = {}) {
3889
4680
  if (ns === PI_NS) {
3890
4681
  refreshHeaders();
3891
4682
  refreshKeyScopes();
4683
+ listingCache.clear();
4684
+ probeRouteCache.clear();
3892
4685
  }
3893
4686
  });
3894
4687
  overlaySource.keys = async ({ route, model }) => {
@@ -3925,10 +4718,23 @@ function apply(ctx, config = {}) {
3925
4718
  const api = normalizeKeyProtocol(profile?.["api"]);
3926
4719
  const baseURL = typeof profile?.["baseURL"] === "string" ? profile["baseURL"].trim() : "";
3927
4720
  if (api === void 0 || !LISTABLE_PROTOCOLS.has(api) || baseURL.length === 0) return void 0;
4721
+ const cacheKey = `${route}\0${keys.map((key) => key.ref).join(",")}`;
4722
+ const cached2 = listingCache.get(cacheKey);
4723
+ if (cached2 !== void 0 && Date.now() - cached2.at < LISTING_CACHE_MS) {
4724
+ return new Response(cached2.body, { status: 200, headers: { "content-type": "application/json" } });
4725
+ }
3928
4726
  const recordsByRef = /* @__PURE__ */ new Map();
4727
+ const probeTimeoutMs = Math.min(resolved.probeTimeoutMs, LISTING_PROBE_BUDGET_MS);
4728
+ const here = (ref) => `${route}\0${ref}`;
4729
+ const knownHere = /* @__PURE__ */ new Map();
4730
+ for (const key of keys) {
4731
+ const remembered = lastKnownScopes.get(here(key.ref));
4732
+ if (remembered !== void 0) knownHere.set(key.ref, remembered);
4733
+ }
3929
4734
  const { results } = await collectKeyListings({
3930
4735
  keys,
3931
4736
  refOf: (key) => key.ref,
4737
+ lastKnown: knownHere,
3932
4738
  probe: async (key) => {
3933
4739
  const targets = key.protocol !== void 0 ? [{ api: key.protocol, baseURL: key.endpoint ?? baseURL }] : [
3934
4740
  { api, baseURL },
@@ -3944,44 +4750,53 @@ function apply(ctx, config = {}) {
3944
4750
  return send(url, { ...init, headers: requestHeaders, signal: requestSignal });
3945
4751
  };
3946
4752
  const asked = /* @__PURE__ */ new Set();
3947
- const records2 = [];
4753
+ const distinct = [];
3948
4754
  for (const target of targets) {
3949
4755
  if (!LISTABLE_PROTOCOLS.has(target.api) || target.baseURL.length === 0) continue;
3950
4756
  const url = probeListingUrl(target.baseURL, target.api);
3951
4757
  if (asked.has(url)) continue;
3952
4758
  asked.add(url);
3953
- const listing = await probeListing({
3954
- baseURL: target.baseURL,
3955
- api: target.api,
3956
- apiKey: key.value,
3957
- headers: profile?.["headers"],
3958
- timeoutMs: resolved.probeTimeoutMs,
3959
- send: sendListing
3960
- });
4759
+ distinct.push(target);
4760
+ }
4761
+ const answered = await Promise.all(distinct.map((target) => probeListing({
4762
+ baseURL: target.baseURL,
4763
+ api: target.api,
4764
+ apiKey: key.value,
4765
+ headers: profile?.["headers"],
4766
+ timeoutMs: probeTimeoutMs,
4767
+ send: sendListing
4768
+ })));
4769
+ const records2 = [];
4770
+ for (const listing of answered) {
3961
4771
  if (listing === void 0) continue;
3962
4772
  for (const model of listing) {
3963
4773
  if (records2.some((existing) => existing.id === model.id)) continue;
3964
4774
  records2.push(model);
3965
4775
  }
3966
4776
  }
3967
- if (records2.length === 0) return { ref: key.ref, models: void 0, targets: [] };
4777
+ if (records2.length === 0) return { ref: key.ref, models: void 0 };
3968
4778
  recordsByRef.set(key.ref, records2);
4779
+ const ids = records2.map((model) => model.id);
4780
+ lastKnownScopes.set(here(key.ref), ids);
4781
+ lastKnownRecords.set(here(key.ref), records2);
3969
4782
  return {
3970
4783
  ref: key.ref,
3971
- models: records2.map((model) => model.id),
3972
- targets: []
4784
+ models: ids
3973
4785
  };
3974
4786
  }
3975
4787
  });
3976
4788
  const records = /* @__PURE__ */ new Map();
3977
4789
  for (const result of results) {
3978
- for (const model of recordsByRef.get(result.ref) ?? []) {
4790
+ const owned = recordsByRef.get(result.ref) ?? lastKnownRecords.get(here(result.ref)) ?? [];
4791
+ for (const model of owned) {
3979
4792
  if (!records.has(model.id)) records.set(model.id, model);
3980
4793
  }
3981
4794
  }
3982
- const models = mergeKeyListings(results).ids.map((id) => records.get(id)).filter((model) => model !== void 0);
4795
+ const models = mergeKeyListings(results, knownHere).ids.map((id) => records.get(id)).filter((model) => model !== void 0);
3983
4796
  if (models.length === 0) return void 0;
3984
- return new Response(JSON.stringify({ data: models }), {
4797
+ const body = JSON.stringify({ data: models });
4798
+ listingCache.set(cacheKey, { at: Date.now(), body });
4799
+ return new Response(body, {
3985
4800
  status: 200,
3986
4801
  headers: { "content-type": "application/json" }
3987
4802
  });
@@ -3999,15 +4814,20 @@ function apply(ctx, config = {}) {
3999
4814
  ctx.effect(() => () => transforms.dispose(), "dsh-model-think-level: pi-ai request headers");
4000
4815
  };
4001
4816
  const availableLlm = ctx.llm;
4817
+ let fingerprintDispatch = availableLlm;
4002
4818
  if (availableLlm === void 0) {
4003
4819
  ctx.inject(["llm"], (llmCtx) => {
4004
4820
  const llm = llmCtx.llm;
4005
- if (llm !== void 0) installPiAiBridge(llm);
4821
+ if (llm !== void 0) {
4822
+ fingerprintDispatch = llm;
4823
+ installPiAiBridge(llm);
4824
+ }
4006
4825
  });
4007
4826
  } else {
4008
4827
  installPiAiBridge(availableLlm);
4009
4828
  }
4010
4829
  const environmentConflicts = detectHeaderConflicts();
4830
+ let fingerprintMutationTail = Promise.resolve();
4011
4831
  ctx.inject(["webServer"], (webServerCtx) => {
4012
4832
  ctx.effect(
4013
4833
  () => webServerCtx.webServer.register({
@@ -4069,6 +4889,13 @@ function apply(ctx, config = {}) {
4069
4889
  }
4070
4890
  const apiKeyEnv = typeof profile["apiKeyEnv"] === "string" ? profile["apiKeyEnv"] : void 0;
4071
4891
  const listingURL = probeListingUrl(baseURL, api);
4892
+ const listingKey = `${listingURL}\0${apiKeyEnv ?? ""}`;
4893
+ const cacheable = url.searchParams.get("refresh") !== "1";
4894
+ const remembered = cacheable ? probeRouteCache.get(listingKey) : void 0;
4895
+ if (remembered !== void 0 && Date.now() - remembered.at < LISTING_CACHE_MS) {
4896
+ sendJson(res, 200, { ok: true, url: displayUrl(listingURL), data: remembered.data });
4897
+ return;
4898
+ }
4072
4899
  let apiKey;
4073
4900
  if (apiKeyEnv !== void 0) {
4074
4901
  try {
@@ -4100,7 +4927,11 @@ function apply(ctx, config = {}) {
4100
4927
  // protocol inference, the path every unanswerable endpoint
4101
4928
  // takes.
4102
4929
  redirect: "error",
4103
- signal: AbortSignal.timeout(resolved.probeTimeoutMs)
4930
+ // This probe waits on an interactive pass (Auto-adapt asks for
4931
+ // it per model), so it gets the catalogue budget rather than the
4932
+ // diagnostic one: a gateway that cannot list within it yields no
4933
+ // endpoint evidence, and the pass moves on to the knowledge base.
4934
+ signal: AbortSignal.timeout(Math.min(resolved.probeTimeoutMs, LISTING_PROBE_BUDGET_MS))
4104
4935
  });
4105
4936
  if (!upstream.ok) {
4106
4937
  const hint = upstream.status === 401 || upstream.status === 403 ? "; check the API key" : "";
@@ -4123,6 +4954,7 @@ function apply(ctx, config = {}) {
4123
4954
  });
4124
4955
  return;
4125
4956
  }
4957
+ probeRouteCache.set(listingKey, { at: Date.now(), data: entries });
4126
4958
  sendJson(res, 200, { ok: true, url: displayUrl(listingURL), data: entries });
4127
4959
  } catch (error) {
4128
4960
  sendJson(res, 502, {
@@ -4344,7 +5176,6 @@ function apply(ctx, config = {}) {
4344
5176
  const alias = isRecord(body) ? normalizeKeyAlias(body["alias"]) : void 0;
4345
5177
  const value = isRecord(body) && typeof body["value"] === "string" ? body["value"] : void 0;
4346
5178
  const refuse = (error) => sendJson(res, 400, { ok: false, error });
4347
- let probeReport;
4348
5179
  if (op === "add") {
4349
5180
  if (!isLegalKeyValue(value)) {
4350
5181
  refuse("a key must be printable ASCII without spaces");
@@ -4454,80 +5285,6 @@ function apply(ctx, config = {}) {
4454
5285
  gemini: body["gemini"]
4455
5286
  }) : void 0;
4456
5287
  updateKeyIndex((document) => withRouteEndpoints(document, route, endpoints), indexFile);
4457
- } else if (op === "discover") {
4458
- const document = readKeyIndex(indexFile);
4459
- const profile = providerProfileOf(entryOf()?.value, route);
4460
- const baseURL = typeof profile?.["baseURL"] === "string" ? profile["baseURL"] : "";
4461
- const api = typeof profile?.["api"] === "string" ? profile["api"] : "";
4462
- if (baseURL.length === 0) {
4463
- refuse("this provider has no address to interrogate; set each key's models by hand");
4464
- return;
4465
- }
4466
- const seat = enabledNow();
4467
- const listedEntries = keyEntriesOf(document, route);
4468
- const listedRefs = listedEntries.map((candidate) => candidate.ref);
4469
- if (seat !== void 0 && !listedRefs.includes(seat)) listedRefs.push(seat);
4470
- if (listedRefs.length === 0) {
4471
- refuse("add a key before detecting its models");
4472
- return;
4473
- }
4474
- const endpoints = document.endpoints?.[route];
4475
- const asks = [];
4476
- for (const listed of listedRefs) {
4477
- let apiKey;
4478
- try {
4479
- apiKey = await resolveCredential(credentials, listed);
4480
- } catch {
4481
- continue;
4482
- }
4483
- if (apiKey === void 0) continue;
4484
- const pinned = listedEntries.find((candidate) => candidate.ref === listed)?.protocol;
4485
- asks.push({
4486
- ref: listed,
4487
- apiKey,
4488
- ...pinned === void 0 ? {} : { pinned }
4489
- });
4490
- }
4491
- const lastKnown = new Map(
4492
- listedEntries.filter((candidate) => candidate.models !== void 0).map((candidate) => [candidate.ref, candidate.models])
4493
- );
4494
- const { results } = await collectKeyListings({
4495
- keys: asks,
4496
- lastKnown,
4497
- refOf: (ask) => ask.ref,
4498
- probe: async (ask) => probeKeyModels({
4499
- ref: ask.ref,
4500
- intent: {
4501
- api,
4502
- baseURL,
4503
- ...ask.pinned === void 0 ? {} : { pinned: ask.pinned },
4504
- ...endpoints === void 0 ? {} : { endpoints }
4505
- },
4506
- apiKey: ask.apiKey,
4507
- profileHeaders: profile?.["headers"],
4508
- timeoutMs: resolved.probeTimeoutMs,
4509
- // Each key probes its OWN credential, so bypass the key-routing
4510
- // wrapper (which would otherwise sign every probe with the first key).
4511
- send: headerOverlayBypass(),
4512
- headersFor: composeProbeHeaders,
4513
- read: readBounded,
4514
- entries: listingEntries
4515
- })
4516
- });
4517
- for (const result of results) {
4518
- if (result.models === void 0) continue;
4519
- updateKeyIndex(
4520
- (document2) => hasKeyEntry(document2, route, result.ref) ? withKeyModels(document2, route, result.ref, result.models) : withKeyEntry(document2, route, result.ref, void 0),
4521
- indexFile
4522
- );
4523
- }
4524
- if (results.every((result) => result.targets.length === 0)) {
4525
- refuse(
4526
- results.find((result) => result.skipped !== void 0)?.skipped ?? "this provider cannot be interrogated; set each key's models by hand"
4527
- );
4528
- return;
4529
- }
4530
- probeReport = results;
4531
5288
  } else if (op === "rename") {
4532
5289
  const known = ref !== void 0 && (hasKeyEntry(readKeyIndex(indexFile), route, ref) || ref === enabledNow());
4533
5290
  if (ref === void 0 || !known) {
@@ -4652,11 +5409,7 @@ function apply(ctx, config = {}) {
4652
5409
  }
4653
5410
  refreshKeyScopes();
4654
5411
  const answer = await payload();
4655
- sendJson(
4656
- res,
4657
- 200,
4658
- probeReport === void 0 || !isRecord(answer) ? answer : { ...answer, probe: probeReport }
4659
- );
5412
+ sendJson(res, 200, answer);
4660
5413
  } catch (error) {
4661
5414
  sendJson(res, error instanceof HostReadError ? 502 : 500, {
4662
5415
  ok: false,
@@ -4730,6 +5483,214 @@ function apply(ctx, config = {}) {
4730
5483
  }),
4731
5484
  "dsh-model-think-level: headers config route"
4732
5485
  );
5486
+ ctx.effect(
5487
+ () => webServerCtx.webServer.register({
5488
+ kind: "exact",
5489
+ path: FINGERPRINT_PATH,
5490
+ handler: async (req, res) => {
5491
+ if (!isTrustedRequest(req)) {
5492
+ sendJson(res, 403, { ok: false, error: "forbidden" });
5493
+ return;
5494
+ }
5495
+ const method = req.method ?? "GET";
5496
+ if (method !== "GET" && method !== "POST" && method !== "DELETE") {
5497
+ sendJson(res, 405, { ok: false, error: "method not allowed" });
5498
+ return;
5499
+ }
5500
+ const url = new URL(req.url ?? "/", "http://x");
5501
+ const route = url.searchParams.get("route") ?? "";
5502
+ if (route.length === 0) {
5503
+ sendJson(res, 400, { ok: false, error: "a provider route is required" });
5504
+ return;
5505
+ }
5506
+ let section;
5507
+ try {
5508
+ section = piSection(true);
5509
+ } catch {
5510
+ sendJson(res, 502, { ok: false, error: "the llm-pi-ai settings could not be read" });
5511
+ return;
5512
+ }
5513
+ const profile = isRecord(section) && isRecord(section["providers"]) ? section["providers"][route] : void 0;
5514
+ if (!isRecord(profile)) {
5515
+ sendJson(res, 400, { ok: false, error: `no llm-pi-ai provider route "${route}"` });
5516
+ return;
5517
+ }
5518
+ let bank;
5519
+ try {
5520
+ bank = loadBankSync();
5521
+ } catch (error) {
5522
+ sendJson(res, 500, {
5523
+ ok: false,
5524
+ error: error instanceof Error ? error.message : String(error)
5525
+ });
5526
+ return;
5527
+ }
5528
+ const file = fingerprintFilePath();
5529
+ const stamp = bankBuiltAt(bank);
5530
+ if (method === "GET") {
5531
+ const document = readFingerprintStore(stamp, file);
5532
+ sendJson(res, 200, {
5533
+ ok: true,
5534
+ data: {
5535
+ bank: summarizeBank(bank),
5536
+ records: providerRecords(document, route)
5537
+ }
5538
+ });
5539
+ return;
5540
+ }
5541
+ let releaseMutation;
5542
+ const previous = fingerprintMutationTail;
5543
+ fingerprintMutationTail = new Promise((resolve2) => {
5544
+ releaseMutation = resolve2;
5545
+ });
5546
+ await previous;
5547
+ try {
5548
+ if (method === "DELETE") {
5549
+ const model2 = url.searchParams.get("model") ?? "";
5550
+ if (model2.length === 0) {
5551
+ sendJson(res, 400, { ok: false, error: "a model id is required" });
5552
+ return;
5553
+ }
5554
+ updateFingerprintStore(
5555
+ (document) => withoutRecord(document, route, model2),
5556
+ stamp,
5557
+ file
5558
+ );
5559
+ sendJson(res, 200, { ok: true });
5560
+ return;
5561
+ }
5562
+ const body = await readJsonBody(req, 2048);
5563
+ const model = isRecord(body) && typeof body["model"] === "string" ? body["model"] : "";
5564
+ if (model.length === 0) {
5565
+ sendJson(res, 400, { ok: false, error: "a model id is required" });
5566
+ return;
5567
+ }
5568
+ if (!isEligibleModel(model)) {
5569
+ sendJson(res, 400, {
5570
+ ok: false,
5571
+ error: `model "${model}" is not a GPT or Claude model`
5572
+ });
5573
+ return;
5574
+ }
5575
+ const llm = fingerprintDispatch ?? ctx.llm;
5576
+ if (llm === void 0 || typeof llm.prepareCall !== "function") {
5577
+ sendJson(res, 503, { ok: false, error: "the llm service is unavailable" });
5578
+ return;
5579
+ }
5580
+ res.statusCode = 200;
5581
+ res.setHeader("content-type", "application/x-ndjson; charset=utf-8");
5582
+ res.setHeader("cache-control", "no-store");
5583
+ res.setHeader("x-accel-buffering", "no");
5584
+ const emit = (line) => {
5585
+ res.write(`${JSON.stringify(line)}
5586
+ `);
5587
+ };
5588
+ const challenges = generateChallenges(3);
5589
+ const controller = new AbortController();
5590
+ res.on("close", () => {
5591
+ if (!res.writableEnded) controller.abort();
5592
+ });
5593
+ let run;
5594
+ try {
5595
+ run = await runProbe({
5596
+ provider: route,
5597
+ model,
5598
+ challenges,
5599
+ llm,
5600
+ signal: controller.signal,
5601
+ timeoutMs: FINGERPRINT_CHALLENGE_TIMEOUT_MS,
5602
+ onOutcome: (outcome) => {
5603
+ emit({
5604
+ type: "progress",
5605
+ index: outcome.index,
5606
+ ok: outcome.ok,
5607
+ ...outcome.error === void 0 ? {} : { error: outcome.error }
5608
+ });
5609
+ },
5610
+ // Early exit (design §5.3): the bank's 2-query calibration
5611
+ // carries 99.7% cross-validated accuracy, so two confident
5612
+ // answers may close the run before the slowest challenge
5613
+ // lands. The gate is deliberately double: the POOLED pair
5614
+ // must clear the 2-query probability bar, AND each answer
5615
+ // on its own must name the same winner — two answers biting
5616
+ // different candidates must not fuse into a lucky spike.
5617
+ shouldStop: (settled) => {
5618
+ const accepted = settled.filter((outcome) => outcome.ok);
5619
+ if (accepted.length < 2) return false;
5620
+ try {
5621
+ const outputs = accepted.map((outcome) => ({
5622
+ text: outcome.text,
5623
+ expected_count: outcome.expectedCount
5624
+ }));
5625
+ const pooled = analyzeGlobalOutputs(outputs, bank);
5626
+ if (pooled.probability < EARLY_IDENTIFIED_PROBABILITY) return false;
5627
+ const first = analyzeGlobalOutputs(outputs.slice(0, 1), bank);
5628
+ const second = analyzeGlobalOutputs(outputs.slice(1), bank);
5629
+ return first.prediction === second.prediction;
5630
+ } catch {
5631
+ return false;
5632
+ }
5633
+ }
5634
+ });
5635
+ } catch (error) {
5636
+ emit({
5637
+ type: "error",
5638
+ error: error instanceof Error ? error.message : String(error)
5639
+ });
5640
+ res.end();
5641
+ return;
5642
+ }
5643
+ let attribution = null;
5644
+ let failure;
5645
+ try {
5646
+ attribution = analyzeGlobalOutputs(run.outputs, bank);
5647
+ } catch (error) {
5648
+ failure = error instanceof Error ? error.message : String(error);
5649
+ }
5650
+ const usedOutputs = attribution === null ? 0 : attribution.used_outputs;
5651
+ const record = {
5652
+ provider: route,
5653
+ model,
5654
+ status: statusOf(attribution, usedOutputs),
5655
+ checkedAt: Date.now(),
5656
+ usedOutputs
5657
+ };
5658
+ if (attribution === null) {
5659
+ const firstFailure = run.outcomes.find((outcome) => !outcome.ok);
5660
+ record.error = run.outputs.length === 0 && firstFailure?.error !== void 0 ? firstFailure.error : failure ?? NO_USABLE_OUTPUT;
5661
+ } else {
5662
+ record.prediction = attribution.prediction;
5663
+ record.predictionName = attribution.prediction_name;
5664
+ record.probability = attribution.probability;
5665
+ record.familyPrediction = attribution.family_prediction;
5666
+ record.familyProbability = attribution.family_probability;
5667
+ record.top = topEntriesOf(attribution.results, 3);
5668
+ record.calibration = {
5669
+ queries: attribution.calibration.queries,
5670
+ cvAccuracy: attribution.calibration.cv_accuracy
5671
+ };
5672
+ const timing = fingerprintTimingOf(run.outcomes);
5673
+ if (timing !== void 0) record.timing = timing;
5674
+ }
5675
+ updateFingerprintStore(
5676
+ (document) => withRecord(document, record),
5677
+ stamp,
5678
+ file
5679
+ );
5680
+ emit({ type: "result", record });
5681
+ res.end();
5682
+ } catch (error) {
5683
+ sendJson(res, 500, {
5684
+ ok: false,
5685
+ error: error instanceof Error ? error.message : String(error)
5686
+ });
5687
+ } finally {
5688
+ releaseMutation?.();
5689
+ }
5690
+ }
5691
+ }),
5692
+ "dsh-model-think-level: fingerprint route"
5693
+ );
4733
5694
  });
4734
5695
  }
4735
5696
  export {