dsh-model-think-level 2.0.2 → 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.
package/lib/index.js CHANGED
@@ -13,10 +13,10 @@ var FINGERPRINT_PATH = "/dsh-model-think-level/fingerprint";
13
13
  var PI_AI_NS = "llm-pi-ai";
14
14
  var UNSET_MARKER = "reasoningEffortsUnset";
15
15
  var INPUT_UNSET_MARKER = "inputUnset";
16
+ var INPUT_IMAGE_GEN_MARKER = "inputImageGen";
16
17
  var AUTOFILL_MARKER = "reasoningEffortsAutofilled";
17
18
 
18
19
  // src/key-refs.ts
19
- var ENDPOINT_FAMILIES = ["openai", "anthropic", "gemini"];
20
20
  function endpointFamilyOf(protocol) {
21
21
  if (protocol === "anthropic-messages") return "anthropic";
22
22
  if (protocol === "google-generative-ai") return "gemini";
@@ -1733,6 +1733,81 @@ var KNOWLEDGE_BASE = [
1733
1733
  efforts: false,
1734
1734
  input: ["text"],
1735
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"
1736
1811
  }
1737
1812
  ];
1738
1813
  var GENERIC_OPENAI_EFFORTS = {
@@ -1918,6 +1993,39 @@ function inferModalitiesFromName(modelId) {
1918
1993
  }
1919
1994
  return void 0;
1920
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
+ }
1921
2029
  function declaredCoreInput(raw) {
1922
2030
  if (raw === void 0) return void 0;
1923
2031
  const lowered = raw.map((value) => value.toLowerCase());
@@ -1928,8 +2036,10 @@ function suggestEfforts(modelId, route, endpoint) {
1928
2036
  const entry = matchKnowledgeBase(modelId, route.displayName);
1929
2037
  let input;
1930
2038
  let inputSource;
2039
+ let imageGen;
1931
2040
  let contextWindow;
1932
2041
  let maxTokens;
2042
+ if (entry?.imageGen === true) imageGen = true;
1933
2043
  if (entry?.input !== void 0) {
1934
2044
  input = entry.input;
1935
2045
  inputSource = "knowledge";
@@ -1941,6 +2051,10 @@ function suggestEfforts(modelId, route, endpoint) {
1941
2051
  input = listedInput;
1942
2052
  inputSource = "endpoint";
1943
2053
  }
2054
+ const listedOutput = endpoint?.output;
2055
+ if (listedOutput !== void 0) {
2056
+ imageGen = listedOutput.includes("image") && !listedOutput.includes("text");
2057
+ }
1944
2058
  if (endpoint?.contextLength !== void 0 && Number.isFinite(endpoint.contextLength) && endpoint.contextLength > 0) {
1945
2059
  contextWindow = endpoint.contextLength;
1946
2060
  }
@@ -1951,9 +2065,15 @@ function suggestEfforts(modelId, route, endpoint) {
1951
2065
  inputSource = "heuristic";
1952
2066
  }
1953
2067
  }
2068
+ if (imageGen === void 0 && inferImageGenFromName(modelId)) imageGen = true;
2069
+ if (imageGen === true) {
2070
+ input = void 0;
2071
+ inputSource = void 0;
2072
+ }
1954
2073
  const modalityParts = {
1955
2074
  ...input === void 0 ? {} : { input },
1956
2075
  ...inputSource === void 0 ? {} : { inputSource },
2076
+ ...imageGen === void 0 ? {} : { imageGen },
1957
2077
  ...contextWindow === void 0 ? {} : { contextWindow },
1958
2078
  ...maxTokens === void 0 ? {} : { maxTokens }
1959
2079
  };
@@ -2252,152 +2372,6 @@ var FAMILY_PROTOCOL = {
2252
2372
  anthropic: "anthropic-messages",
2253
2373
  gemini: "google-generative-ai"
2254
2374
  };
2255
- function knownWire(api) {
2256
- return api === "anthropic-messages" || api === "openai-completions" || api === "openai-responses" || api === "google-generative-ai" ? api : void 0;
2257
- }
2258
- function address(value) {
2259
- if (typeof value !== "string") return void 0;
2260
- const trimmed = value.trim();
2261
- return trimmed.length === 0 ? void 0 : trimmed;
2262
- }
2263
- function probePlanFor(intent) {
2264
- const wire = knownWire(intent.api);
2265
- const family = wire === void 0 ? void 0 : endpointFamilyOf(wire);
2266
- const baseURL = address(intent.baseURL);
2267
- const targets = [];
2268
- const seen = /* @__PURE__ */ new Set();
2269
- const push = (target) => {
2270
- if (!LISTABLE_PROTOCOLS.has(target.api)) return;
2271
- const key = `${target.api}\0${probeListingUrl(target.baseURL, target.api)}`;
2272
- if (seen.has(key)) return;
2273
- seen.add(key);
2274
- targets.push(target);
2275
- };
2276
- if (intent.pinned !== void 0) {
2277
- const pinFamily = endpointFamilyOf(intent.pinned);
2278
- const pinnedBase = pinFamily === family ? baseURL ?? address(intent.endpoints?.[pinFamily]) : address(intent.endpoints?.[pinFamily]);
2279
- if (pinnedBase === void 0) {
2280
- return {
2281
- targets: [],
2282
- skipped: `the key pins ${intent.pinned} but this provider has no ${pinFamily} address`
2283
- };
2284
- }
2285
- push({ api: intent.pinned, baseURL: pinnedBase, family: pinFamily });
2286
- return { targets };
2287
- }
2288
- if (wire !== void 0 && family !== void 0) {
2289
- if (baseURL !== void 0) push({ api: wire, baseURL, family });
2290
- for (const other of ENDPOINT_FAMILIES) {
2291
- if (other === family) continue;
2292
- const otherBase = address(intent.endpoints?.[other]);
2293
- if (otherBase !== void 0) push({ api: FAMILY_PROTOCOL[other], baseURL: otherBase, family: other });
2294
- }
2295
- return { targets };
2296
- }
2297
- for (const configured of ENDPOINT_FAMILIES) {
2298
- const configuredBase = address(intent.endpoints?.[configured]);
2299
- if (configuredBase !== void 0) push({ api: FAMILY_PROTOCOL[configured], baseURL: configuredBase, family: configured });
2300
- }
2301
- return {
2302
- targets,
2303
- ...targets.length === 0 ? { skipped: `this provider cannot be interrogated on ${String(intent.api)}; set each key's models by hand` } : {}
2304
- };
2305
- }
2306
- async function probeKeyModels(options) {
2307
- const plan = probePlanFor(options.intent);
2308
- const answers = await Promise.all(plan.targets.map(async (target) => {
2309
- const url = probeListingUrl(target.baseURL, target.api);
2310
- const reported = displayUrl(url);
2311
- let response;
2312
- try {
2313
- response = await options.send(url, {
2314
- method: "GET",
2315
- headers: options.headersFor(options.profileHeaders, options.apiKey, target.api),
2316
- redirect: "error",
2317
- signal: AbortSignal.timeout(options.timeoutMs)
2318
- });
2319
- } catch (error) {
2320
- const outcome2 = {
2321
- api: target.api,
2322
- family: target.family,
2323
- url: reported,
2324
- ok: false,
2325
- error: error instanceof Error ? error.message : String(error)
2326
- };
2327
- return { outcome: outcome2, ids: [] };
2328
- }
2329
- if (!response.ok) {
2330
- await response.body?.cancel().catch(() => {
2331
- });
2332
- const outcome2 = {
2333
- api: target.api,
2334
- family: target.family,
2335
- url: reported,
2336
- ok: false,
2337
- status: response.status
2338
- };
2339
- return { outcome: outcome2, ids: [] };
2340
- }
2341
- let entries;
2342
- try {
2343
- entries = options.entries(JSON.parse(await options.read(response, url)));
2344
- } catch (error) {
2345
- const outcome2 = {
2346
- api: target.api,
2347
- family: target.family,
2348
- url: reported,
2349
- ok: false,
2350
- status: response.status,
2351
- error: error instanceof Error ? error.message : String(error)
2352
- };
2353
- return { outcome: outcome2, ids: [] };
2354
- }
2355
- if (entries === void 0) {
2356
- const outcome2 = {
2357
- api: target.api,
2358
- family: target.family,
2359
- url: reported,
2360
- ok: false,
2361
- status: response.status,
2362
- error: "the reply names no model listing"
2363
- };
2364
- return { outcome: outcome2, ids: [] };
2365
- }
2366
- const outcome = {
2367
- api: target.api,
2368
- family: target.family,
2369
- url: reported,
2370
- ok: true,
2371
- status: response.status,
2372
- models: entries.length
2373
- };
2374
- const found = [];
2375
- for (const entry of entries) {
2376
- const id = entry["id"];
2377
- if (typeof id === "string" && id.length > 0) found.push(id);
2378
- }
2379
- return { outcome, ids: found };
2380
- }));
2381
- const outcomes = answers.map((answer) => answer.outcome);
2382
- const ids = [];
2383
- const seen = /* @__PURE__ */ new Set();
2384
- let answered = 0;
2385
- for (const answer of answers) {
2386
- if (!answer.outcome.ok) continue;
2387
- answered += 1;
2388
- for (const id of answer.ids) {
2389
- if (seen.has(id)) continue;
2390
- seen.add(id);
2391
- ids.push(id);
2392
- }
2393
- }
2394
- return {
2395
- ref: options.ref,
2396
- models: answered === 0 ? void 0 : ids,
2397
- targets: outcomes,
2398
- ...plan.skipped === void 0 ? {} : { skipped: plan.skipped }
2399
- };
2400
- }
2401
2375
 
2402
2376
  // src/multi-key-listing.ts
2403
2377
  var DEFAULT_PROBE_CONCURRENCY = 4;
@@ -2471,7 +2445,7 @@ async function collectKeyListings(options) {
2471
2445
  results[index] = await options.probe(key);
2472
2446
  } catch {
2473
2447
  const ref = options.refOf?.(key);
2474
- if (ref !== void 0) results[index] = { ref, models: void 0, targets: [] };
2448
+ if (ref !== void 0) results[index] = { ref, models: void 0 };
2475
2449
  }
2476
2450
  }
2477
2451
  };
@@ -2733,10 +2707,17 @@ function seedHeaders(input, init) {
2733
2707
  if (typeof Request !== "undefined" && input instanceof Request) return new Headers(input.headers);
2734
2708
  return new Headers();
2735
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
+ }
2736
2717
  function makeWrapper(base, entries) {
2737
2718
  return async function fetchWithProviderHeaders(input, init) {
2738
2719
  const url = requestUrlOf(input);
2739
- if (url === void 0) return base(input, init);
2720
+ if (url === void 0) return base(input, passthroughInit(input, init));
2740
2721
  let entry;
2741
2722
  let candidates = [];
2742
2723
  for (const candidate of entries.values()) {
@@ -2747,7 +2728,7 @@ function makeWrapper(base, entries) {
2747
2728
  break;
2748
2729
  }
2749
2730
  }
2750
- 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));
2751
2732
  const keyed = entry.keys !== void 0;
2752
2733
  let model;
2753
2734
  if (candidates.length > 1 || keyed) {
@@ -2755,7 +2736,7 @@ function makeWrapper(base, entries) {
2755
2736
  model = body === void 0 ? void 0 : modelOfJsonBody(body);
2756
2737
  }
2757
2738
  const override = candidates.length === 1 ? candidates[0] : selectOverrideFrom(candidates, model);
2758
- if (override === void 0) return base(input, init);
2739
+ if (override === void 0) return base(input, passthroughInit(input, init));
2759
2740
  const patched = seedHeaders(input, init);
2760
2741
  let hasPatch = false;
2761
2742
  if (override.hasHeaders) {
@@ -2777,7 +2758,6 @@ function makeWrapper(base, entries) {
2777
2758
  };
2778
2759
  if (patched.get(WIRE_DISPATCH_HEADER) === "1") {
2779
2760
  patched.delete(WIRE_DISPATCH_HEADER);
2780
- if (!hasPatch) return base(input, init);
2781
2761
  return attempt(void 0);
2782
2762
  }
2783
2763
  const resolved = keyed ? await entry.keys({ route: override.route, model }) : [];
@@ -2801,7 +2781,7 @@ function makeWrapper(base, entries) {
2801
2781
  } catch {
2802
2782
  }
2803
2783
  }
2804
- if (!hasPatch && keyCandidates.length === 0) return base(input, init);
2784
+ if (!hasPatch && keyCandidates.length === 0) return base(input, passthroughInit(input, init));
2805
2785
  const chargeFailure = async (candidate, response) => {
2806
2786
  const now = Date.now();
2807
2787
  const retryAfterMs = parseRetryAfter(response.headers.get("retry-after"), now);
@@ -3410,7 +3390,8 @@ function detectHeaderConflicts() {
3410
3390
  // src/autofill.ts
3411
3391
  function declaresInput(model) {
3412
3392
  const input = model["input"];
3413
- 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;
3414
3395
  }
3415
3396
  function buildAutofillPatch(providers, routeFilter = () => true, options = {}, revision = 0) {
3416
3397
  const fillEfforts = options.efforts !== false;
@@ -3428,7 +3409,7 @@ function buildAutofillPatch(providers, routeFilter = () => true, options = {}, r
3428
3409
  let changed = false;
3429
3410
  for (const model of models) {
3430
3411
  const effortsDeclared = model["reasoningEfforts"] !== void 0 || model[UNSET_MARKER] === true;
3431
- const inputDeclared = declaresInput(model) || model[INPUT_UNSET_MARKER] === true;
3412
+ const inputDeclared = declaresInput(model);
3432
3413
  if ((effortsDeclared || !fillEfforts) && (inputDeclared || !fillModalities)) {
3433
3414
  const backfilled = backfillRolePin(model, providers, route);
3434
3415
  if (backfilled !== void 0) {
@@ -3462,9 +3443,14 @@ function buildAutofillPatch(providers, routeFilter = () => true, options = {}, r
3462
3443
  fill["compat"] = { ...stored, ...suggestion.compat };
3463
3444
  }
3464
3445
  }
3465
- if (!inputDeclared && fillModalities && suggestion.input !== void 0) {
3466
- touched = true;
3467
- 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
+ }
3468
3454
  }
3469
3455
  if (!touched) {
3470
3456
  nextModels.push(model);
@@ -3975,8 +3961,15 @@ async function runProbe(options) {
3975
3961
  if (challenges.length === 0) throw new Error("fingerprint probe: no challenges to run");
3976
3962
  const timeoutMs = options.timeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS;
3977
3963
  const maxTokens = options.maxTokens ?? DEFAULT_PROBE_MAX_TOKENS;
3978
- const settled = await Promise.all(
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(
3979
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]);
3980
3973
  const outcome = signal?.aborted === true ? {
3981
3974
  index,
3982
3975
  challengeId: challenge.id,
@@ -3984,12 +3977,22 @@ async function runProbe(options) {
3984
3977
  ok: false,
3985
3978
  text: "",
3986
3979
  error: "cancelled"
3987
- } : await runChallenge({ ...options, index, challenge, timeoutMs, maxTokens });
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;
3988
3991
  options.onOutcome?.(outcome);
3989
3992
  return outcome;
3990
3993
  })
3991
3994
  );
3992
- const outcomes = [...settled];
3995
+ const outcomes = [...settledAll];
3993
3996
  const outputs = [];
3994
3997
  for (const outcome of outcomes) {
3995
3998
  if (outcome.ok) outputs.push({ text: outcome.text, expected_count: outcome.expectedCount });
@@ -4003,6 +4006,12 @@ async function runChallenge(options) {
4003
4006
  challengeId: challenge.id,
4004
4007
  expectedCount: challenge.expected_count
4005
4008
  };
4009
+ const startedAt = Date.now();
4010
+ let firstTokenMs;
4011
+ const timing = () => ({
4012
+ ...firstTokenMs === void 0 ? {} : { firstTokenMs },
4013
+ durationMs: Date.now() - startedAt
4014
+ });
4006
4015
  const deadline = AbortSignal.timeout(timeoutMs);
4007
4016
  const combined = signal === void 0 ? deadline : AbortSignal.any([signal, deadline]);
4008
4017
  try {
@@ -4035,28 +4044,32 @@ async function runChallenge(options) {
4035
4044
  const type = chunk["type"];
4036
4045
  if (type === "text-delta") {
4037
4046
  const delta = chunk["text"];
4038
- if (typeof delta === "string") text += delta;
4047
+ if (typeof delta === "string") {
4048
+ if (delta.length > 0 && firstTokenMs === void 0) firstTokenMs = Date.now() - startedAt;
4049
+ text += delta;
4050
+ }
4039
4051
  } else if (type === "finish") {
4040
4052
  finish = finishProblem(chunk["reason"]);
4041
4053
  }
4042
4054
  }
4043
4055
  if (signal?.aborted === true) {
4044
- return { ...base, ok: false, text: "", error: "cancelled" };
4056
+ return { ...base, ...timing(), ok: false, text: "", error: "cancelled" };
4045
4057
  }
4046
4058
  if (finish !== void 0) {
4047
4059
  const detail = finish.message === void 0 ? finish.kind : `${finish.kind}: ${finish.message}`;
4048
- return { ...base, ok: false, text: "", error: detail, finish: finish.kind };
4060
+ return { ...base, ...timing(), ok: false, text: "", error: detail, finish: finish.kind };
4049
4061
  }
4050
4062
  if (text.trim().length === 0) {
4051
- return { ...base, ok: false, text: "", error: "the model answered with no visible text" };
4063
+ return { ...base, ...timing(), ok: false, text: "", error: "the model answered with no visible text" };
4052
4064
  }
4053
- return { ...base, ok: true, text };
4065
+ return { ...base, ...timing(), ok: true, text };
4054
4066
  } catch (error) {
4055
4067
  if (signal?.aborted === true) {
4056
- return { ...base, ok: false, text: "", error: "cancelled" };
4068
+ return { ...base, ...timing(), ok: false, text: "", error: "cancelled" };
4057
4069
  }
4058
4070
  return {
4059
4071
  ...base,
4072
+ ...timing(),
4060
4073
  ok: false,
4061
4074
  text: "",
4062
4075
  error: error instanceof Error ? error.message : String(error)
@@ -4127,6 +4140,16 @@ function parseRecord(provider, model, raw) {
4127
4140
  cvAccuracy: finiteNumber(fields["cvAccuracy"], 0)
4128
4141
  };
4129
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
+ }
4130
4153
  if (status === "failed") {
4131
4154
  delete out.prediction;
4132
4155
  delete out.predictionName;
@@ -4135,6 +4158,7 @@ function parseRecord(provider, model, raw) {
4135
4158
  delete out.familyProbability;
4136
4159
  delete out.top;
4137
4160
  delete out.calibration;
4161
+ delete out.timing;
4138
4162
  }
4139
4163
  return out;
4140
4164
  }
@@ -4218,6 +4242,8 @@ function providerRecords(document, provider) {
4218
4242
  // src/fingerprint/verdict.ts
4219
4243
  var IDENTIFIED_THRESHOLD = 0.6;
4220
4244
  var FULL_RUN_QUERIES = 3;
4245
+ var EARLY_IDENTIFIED_PROBABILITY = 0.9;
4246
+ var EARLY_IDENTIFIED_QUERIES = 2;
4221
4247
  function topEntriesOf(rows, limit = 3) {
4222
4248
  return rows.slice(0, limit).map((row) => ({
4223
4249
  model: row.model,
@@ -4227,8 +4253,13 @@ function topEntriesOf(rows, limit = 3) {
4227
4253
  }
4228
4254
  function statusOf(attribution, usedOutputs) {
4229
4255
  if (attribution === null) return "failed";
4230
- if (usedOutputs < FULL_RUN_QUERIES) return "doubtful";
4231
- return attribution.probability >= IDENTIFIED_THRESHOLD ? "identified" : "doubtful";
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";
4232
4263
  }
4233
4264
 
4234
4265
  // src/index.ts
@@ -4238,6 +4269,28 @@ var PI_NS = PI_AI_NS;
4238
4269
  var BOOT_RETRY_DELAYS_MS = [1e3, 2e3, 4e3, 8e3, 16e3, 3e4];
4239
4270
  var PROBE_TIMEOUT_MS = 15e3;
4240
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
+ }
4241
4294
  var LISTING_PROBE_BUDGET_MS = 6e3;
4242
4295
  var LISTING_CACHE_MS = 3e4;
4243
4296
  var Config = Schema.object({
@@ -4721,15 +4774,14 @@ function apply(ctx, config = {}) {
4721
4774
  records2.push(model);
4722
4775
  }
4723
4776
  }
4724
- if (records2.length === 0) return { ref: key.ref, models: void 0, targets: [] };
4777
+ if (records2.length === 0) return { ref: key.ref, models: void 0 };
4725
4778
  recordsByRef.set(key.ref, records2);
4726
4779
  const ids = records2.map((model) => model.id);
4727
4780
  lastKnownScopes.set(here(key.ref), ids);
4728
4781
  lastKnownRecords.set(here(key.ref), records2);
4729
4782
  return {
4730
4783
  ref: key.ref,
4731
- models: ids,
4732
- targets: []
4784
+ models: ids
4733
4785
  };
4734
4786
  }
4735
4787
  });
@@ -5124,7 +5176,6 @@ function apply(ctx, config = {}) {
5124
5176
  const alias = isRecord(body) ? normalizeKeyAlias(body["alias"]) : void 0;
5125
5177
  const value = isRecord(body) && typeof body["value"] === "string" ? body["value"] : void 0;
5126
5178
  const refuse = (error) => sendJson(res, 400, { ok: false, error });
5127
- let probeReport;
5128
5179
  if (op === "add") {
5129
5180
  if (!isLegalKeyValue(value)) {
5130
5181
  refuse("a key must be printable ASCII without spaces");
@@ -5234,80 +5285,6 @@ function apply(ctx, config = {}) {
5234
5285
  gemini: body["gemini"]
5235
5286
  }) : void 0;
5236
5287
  updateKeyIndex((document) => withRouteEndpoints(document, route, endpoints), indexFile);
5237
- } else if (op === "discover") {
5238
- const document = readKeyIndex(indexFile);
5239
- const profile = providerProfileOf(entryOf()?.value, route);
5240
- const baseURL = typeof profile?.["baseURL"] === "string" ? profile["baseURL"] : "";
5241
- const api = typeof profile?.["api"] === "string" ? profile["api"] : "";
5242
- if (baseURL.length === 0) {
5243
- refuse("this provider has no address to interrogate; set each key's models by hand");
5244
- return;
5245
- }
5246
- const seat = enabledNow();
5247
- const listedEntries = keyEntriesOf(document, route);
5248
- const listedRefs = listedEntries.map((candidate) => candidate.ref);
5249
- if (seat !== void 0 && !listedRefs.includes(seat)) listedRefs.push(seat);
5250
- if (listedRefs.length === 0) {
5251
- refuse("add a key before detecting its models");
5252
- return;
5253
- }
5254
- const endpoints = document.endpoints?.[route];
5255
- const asks = [];
5256
- for (const listed of listedRefs) {
5257
- let apiKey;
5258
- try {
5259
- apiKey = await resolveCredential(credentials, listed);
5260
- } catch {
5261
- continue;
5262
- }
5263
- if (apiKey === void 0) continue;
5264
- const pinned = listedEntries.find((candidate) => candidate.ref === listed)?.protocol;
5265
- asks.push({
5266
- ref: listed,
5267
- apiKey,
5268
- ...pinned === void 0 ? {} : { pinned }
5269
- });
5270
- }
5271
- const lastKnown = new Map(
5272
- listedEntries.filter((candidate) => candidate.models !== void 0).map((candidate) => [candidate.ref, candidate.models])
5273
- );
5274
- const { results } = await collectKeyListings({
5275
- keys: asks,
5276
- lastKnown,
5277
- refOf: (ask) => ask.ref,
5278
- probe: async (ask) => probeKeyModels({
5279
- ref: ask.ref,
5280
- intent: {
5281
- api,
5282
- baseURL,
5283
- ...ask.pinned === void 0 ? {} : { pinned: ask.pinned },
5284
- ...endpoints === void 0 ? {} : { endpoints }
5285
- },
5286
- apiKey: ask.apiKey,
5287
- profileHeaders: profile?.["headers"],
5288
- timeoutMs: resolved.probeTimeoutMs,
5289
- // Each key probes its OWN credential, so bypass the key-routing
5290
- // wrapper (which would otherwise sign every probe with the first key).
5291
- send: headerOverlayBypass(),
5292
- headersFor: composeProbeHeaders,
5293
- read: readBounded,
5294
- entries: listingEntries
5295
- })
5296
- });
5297
- for (const result of results) {
5298
- if (result.models === void 0) continue;
5299
- updateKeyIndex(
5300
- (document2) => hasKeyEntry(document2, route, result.ref) ? withKeyModels(document2, route, result.ref, result.models) : withKeyEntry(document2, route, result.ref, void 0),
5301
- indexFile
5302
- );
5303
- }
5304
- if (results.every((result) => result.targets.length === 0)) {
5305
- refuse(
5306
- results.find((result) => result.skipped !== void 0)?.skipped ?? "this provider cannot be interrogated; set each key's models by hand"
5307
- );
5308
- return;
5309
- }
5310
- probeReport = results;
5311
5288
  } else if (op === "rename") {
5312
5289
  const known = ref !== void 0 && (hasKeyEntry(readKeyIndex(indexFile), route, ref) || ref === enabledNow());
5313
5290
  if (ref === void 0 || !known) {
@@ -5432,11 +5409,7 @@ function apply(ctx, config = {}) {
5432
5409
  }
5433
5410
  refreshKeyScopes();
5434
5411
  const answer = await payload();
5435
- sendJson(
5436
- res,
5437
- 200,
5438
- probeReport === void 0 || !isRecord(answer) ? answer : { ...answer, probe: probeReport }
5439
- );
5412
+ sendJson(res, 200, answer);
5440
5413
  } catch (error) {
5441
5414
  sendJson(res, error instanceof HostReadError ? 502 : 500, {
5442
5415
  ok: false,
@@ -5633,6 +5606,30 @@ function apply(ctx, config = {}) {
5633
5606
  ok: outcome.ok,
5634
5607
  ...outcome.error === void 0 ? {} : { error: outcome.error }
5635
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
+ }
5636
5633
  }
5637
5634
  });
5638
5635
  } catch (error) {
@@ -5672,6 +5669,8 @@ function apply(ctx, config = {}) {
5672
5669
  queries: attribution.calibration.queries,
5673
5670
  cvAccuracy: attribution.calibration.cv_accuracy
5674
5671
  };
5672
+ const timing = fingerprintTimingOf(run.outcomes);
5673
+ if (timing !== void 0) record.timing = timing;
5675
5674
  }
5676
5675
  updateFingerprintStore(
5677
5676
  (document) => withRecord(document, record),