@hiper2d/ai-agents 0.28.1 → 0.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -0
- package/dist/index.d.mts +61 -12
- package/dist/index.d.ts +61 -12
- package/dist/index.js +268 -164
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +265 -164
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -589,6 +589,77 @@ function needsPromptBasedSchema(provider) {
|
|
|
589
589
|
return provider === "anthropic";
|
|
590
590
|
}
|
|
591
591
|
|
|
592
|
+
// src/errors.ts
|
|
593
|
+
var ModelError = class extends Error {
|
|
594
|
+
modelType;
|
|
595
|
+
constructor(message, modelType) {
|
|
596
|
+
super(message);
|
|
597
|
+
this.modelType = modelType;
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
var ModelOverloadError = class extends ModelError {
|
|
601
|
+
retryable;
|
|
602
|
+
constructor(message, modelType, retryable = true) {
|
|
603
|
+
super(message, modelType);
|
|
604
|
+
this.name = "ModelOverloadError";
|
|
605
|
+
this.retryable = retryable;
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
var ModelRateLimitError = class extends ModelError {
|
|
609
|
+
retryAfter;
|
|
610
|
+
// seconds to wait before retrying
|
|
611
|
+
constructor(message, modelType, retryAfter) {
|
|
612
|
+
super(message, modelType);
|
|
613
|
+
this.name = "ModelRateLimitError";
|
|
614
|
+
this.retryAfter = retryAfter;
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
var ModelUnavailableError = class extends ModelError {
|
|
618
|
+
reason;
|
|
619
|
+
constructor(message, modelType, reason = "unknown") {
|
|
620
|
+
super(message, modelType);
|
|
621
|
+
this.name = "ModelUnavailableError";
|
|
622
|
+
this.reason = reason;
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
var ModelAuthenticationError = class extends ModelError {
|
|
626
|
+
constructor(message, modelType) {
|
|
627
|
+
super(message, modelType);
|
|
628
|
+
this.name = "ModelAuthenticationError";
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
var ModelQuotaExceededError = class extends ModelError {
|
|
632
|
+
constructor(message, modelType) {
|
|
633
|
+
super(message, modelType);
|
|
634
|
+
this.name = "ModelQuotaExceededError";
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
var ModelInvalidResponseError = class extends ModelError {
|
|
638
|
+
truncated;
|
|
639
|
+
retries;
|
|
640
|
+
constructor(modelType, detail, truncated = false) {
|
|
641
|
+
super(`${modelType} failed to produce a valid response: ${detail}`, modelType);
|
|
642
|
+
this.name = "ModelInvalidResponseError";
|
|
643
|
+
this.truncated = truncated;
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
var ModelTimeoutError = class extends ModelError {
|
|
647
|
+
timeoutMs;
|
|
648
|
+
constructor(modelType, timeoutMs) {
|
|
649
|
+
super(`${modelType} did not respond within ${Math.round(timeoutMs / 1e3)}s`, modelType);
|
|
650
|
+
this.name = "ModelTimeoutError";
|
|
651
|
+
this.timeoutMs = timeoutMs;
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
var ModelRefusalError = class extends ModelError {
|
|
655
|
+
reason;
|
|
656
|
+
constructor(modelType, message = `${modelType} refused to answer (stop_reason: refusal)`, reason) {
|
|
657
|
+
super(message, modelType);
|
|
658
|
+
this.name = "ModelRefusalError";
|
|
659
|
+
this.reason = reason;
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
|
|
592
663
|
// src/json-response-parser.ts
|
|
593
664
|
function extractFirstJsonObject(text) {
|
|
594
665
|
let searchFrom = 0;
|
|
@@ -633,7 +704,7 @@ function normalizeNestedReply(value, log) {
|
|
|
633
704
|
return value;
|
|
634
705
|
}
|
|
635
706
|
function parseAndValidateLlmJson(rawReply, zodSchema, log = () => {
|
|
636
|
-
}) {
|
|
707
|
+
}, model = "The model") {
|
|
637
708
|
const cleaned = cleanResponse(rawReply);
|
|
638
709
|
const candidates = [cleaned];
|
|
639
710
|
if (cleaned.startsWith('"') && cleaned.endsWith('"')) {
|
|
@@ -690,81 +761,11 @@ function parseAndValidateLlmJson(rawReply, zodSchema, log = () => {
|
|
|
690
761
|
}
|
|
691
762
|
if (zodError !== null) {
|
|
692
763
|
log(`Zod validation failed: ${JSON.stringify(zodError.errors)}`);
|
|
693
|
-
throw new
|
|
764
|
+
throw new ModelInvalidResponseError(model, `Response validation failed: ${zodError.message}`);
|
|
694
765
|
}
|
|
695
|
-
throw new
|
|
766
|
+
throw new ModelInvalidResponseError(model, `Failed to parse JSON response: ${parseError}. First 200 chars: ${cleaned.slice(0, 200)}`);
|
|
696
767
|
}
|
|
697
768
|
|
|
698
|
-
// src/errors.ts
|
|
699
|
-
var ModelError = class extends Error {
|
|
700
|
-
modelType;
|
|
701
|
-
constructor(message, modelType) {
|
|
702
|
-
super(message);
|
|
703
|
-
this.modelType = modelType;
|
|
704
|
-
}
|
|
705
|
-
};
|
|
706
|
-
var ModelOverloadError = class extends ModelError {
|
|
707
|
-
retryable;
|
|
708
|
-
constructor(message, modelType, retryable = true) {
|
|
709
|
-
super(message, modelType);
|
|
710
|
-
this.name = "ModelOverloadError";
|
|
711
|
-
this.retryable = retryable;
|
|
712
|
-
}
|
|
713
|
-
};
|
|
714
|
-
var ModelRateLimitError = class extends ModelError {
|
|
715
|
-
retryAfter;
|
|
716
|
-
// seconds to wait before retrying
|
|
717
|
-
constructor(message, modelType, retryAfter) {
|
|
718
|
-
super(message, modelType);
|
|
719
|
-
this.name = "ModelRateLimitError";
|
|
720
|
-
this.retryAfter = retryAfter;
|
|
721
|
-
}
|
|
722
|
-
};
|
|
723
|
-
var ModelUnavailableError = class extends ModelError {
|
|
724
|
-
reason;
|
|
725
|
-
constructor(message, modelType, reason = "unknown") {
|
|
726
|
-
super(message, modelType);
|
|
727
|
-
this.name = "ModelUnavailableError";
|
|
728
|
-
this.reason = reason;
|
|
729
|
-
}
|
|
730
|
-
};
|
|
731
|
-
var ModelAuthenticationError = class extends ModelError {
|
|
732
|
-
constructor(message, modelType) {
|
|
733
|
-
super(message, modelType);
|
|
734
|
-
this.name = "ModelAuthenticationError";
|
|
735
|
-
}
|
|
736
|
-
};
|
|
737
|
-
var ModelQuotaExceededError = class extends ModelError {
|
|
738
|
-
constructor(message, modelType) {
|
|
739
|
-
super(message, modelType);
|
|
740
|
-
this.name = "ModelQuotaExceededError";
|
|
741
|
-
}
|
|
742
|
-
};
|
|
743
|
-
var ModelInvalidResponseError = class extends ModelError {
|
|
744
|
-
truncated;
|
|
745
|
-
constructor(modelType, detail, truncated = false) {
|
|
746
|
-
super(`${modelType} failed to produce a valid response: ${detail}`, modelType);
|
|
747
|
-
this.name = "ModelInvalidResponseError";
|
|
748
|
-
this.truncated = truncated;
|
|
749
|
-
}
|
|
750
|
-
};
|
|
751
|
-
var ModelTimeoutError = class extends ModelError {
|
|
752
|
-
timeoutMs;
|
|
753
|
-
constructor(modelType, timeoutMs) {
|
|
754
|
-
super(`${modelType} did not respond within ${Math.round(timeoutMs / 1e3)}s`, modelType);
|
|
755
|
-
this.name = "ModelTimeoutError";
|
|
756
|
-
this.timeoutMs = timeoutMs;
|
|
757
|
-
}
|
|
758
|
-
};
|
|
759
|
-
var ModelRefusalError = class extends ModelError {
|
|
760
|
-
reason;
|
|
761
|
-
constructor(modelType, message = `${modelType} refused to answer (stop_reason: refusal)`, reason) {
|
|
762
|
-
super(message, modelType);
|
|
763
|
-
this.name = "ModelRefusalError";
|
|
764
|
-
this.reason = reason;
|
|
765
|
-
}
|
|
766
|
-
};
|
|
767
|
-
|
|
768
769
|
// src/thinking-utils.ts
|
|
769
770
|
function stripInlineThinking(raw) {
|
|
770
771
|
let thinking = "";
|
|
@@ -1267,20 +1268,29 @@ var SupportedAiModels = {
|
|
|
1267
1268
|
thinkingBudgetTokens: 1024,
|
|
1268
1269
|
tags: ["slow", "cheap"]
|
|
1269
1270
|
},
|
|
1270
|
-
// MiniMax M3 (OpenAI-compatible endpoint, 1M context).
|
|
1271
|
-
//
|
|
1272
|
-
//
|
|
1273
|
-
//
|
|
1274
|
-
//
|
|
1275
|
-
//
|
|
1276
|
-
// Temperature
|
|
1271
|
+
// MiniMax M3.1 Flash (OpenAI-compatible endpoint, 1M context). `minimax` repointed 2026-10-27
|
|
1272
|
+
// from MiniMax-M3 ($0.30/$1.20 after the permanent 50% off, 25s single-sample turn, 60s
|
|
1273
|
+
// timeouts in prod on 2026-10-25 and 10-27) to MiniMax-M3.1-Flash: half the price
|
|
1274
|
+
// ($0.15/$0.60), and the first MiniMax model with an effort dial — low|medium|high|xhigh|max,
|
|
1275
|
+
// provider default max, so it is pinned. Thinking is always on (`thinking: disabled` → 400),
|
|
1276
|
+
// thinking arrives in `reasoning_content` because the agent sends `reasoning_split: true`
|
|
1277
|
+
// (M3.1 rejects false). Temperature and top_p are documented as ignored ("not supported
|
|
1278
|
+
// yet", always 1 / 0.95), so the entry sets none and the agent sends none.
|
|
1279
|
+
// Live 2026-10-27 at medium: the all-providers sweep ran the schema ask in 3.1s, prose in
|
|
1280
|
+
// 1.7s and the 8-character scene (363 output tokens) in 2.4s — ~150 tok/s, the fastest
|
|
1281
|
+
// provider in the sweep that day (Kimi Highspeed's scene takes 8s); a 3.2k-token game
|
|
1282
|
+
// prompt with a two-sentence JSON reply took 1.9-2.1s at every effort level vs 3.5s for M3.
|
|
1283
|
+
// Tagged `fast` rather than `very-fast`: the sweep straddles the 3s line and production
|
|
1284
|
+
// turns carry 10k+ tokens of context. Launched 2026-09-27 as Token-Plan-only (the
|
|
1285
|
+
// 2026-10-02 probe got `unknown model` on the pay-as-you-go key); on the key since at
|
|
1286
|
+
// least 2026-10-27.
|
|
1277
1287
|
[LLM_CONSTANTS.MINIMAX]: {
|
|
1278
|
-
displayName: "MiniMax M3",
|
|
1279
|
-
modelApiName: "MiniMax-M3",
|
|
1288
|
+
displayName: "MiniMax M3.1 Flash",
|
|
1289
|
+
modelApiName: "MiniMax-M3.1-Flash",
|
|
1280
1290
|
apiKeyName: API_KEY_CONSTANTS.MINIMAX,
|
|
1281
1291
|
hasThinking: true,
|
|
1282
|
-
|
|
1283
|
-
tags: ["
|
|
1292
|
+
reasoningEffort: "medium",
|
|
1293
|
+
tags: ["fast", "cheap"]
|
|
1284
1294
|
},
|
|
1285
1295
|
// Meta Muse Spark 1.3 (added 2026-09-12) on Meta's own Model API — Standard tier, i.e.
|
|
1286
1296
|
// the private model id (the `-contributor` id is a quarter of the price but Meta trains
|
|
@@ -1597,18 +1607,14 @@ var MODEL_PRICING = {
|
|
|
1597
1607
|
outputPrice: 0.47,
|
|
1598
1608
|
cacheHitPrice: 0.016
|
|
1599
1609
|
},
|
|
1600
|
-
// MiniMax M3. Rates from platform.minimax.io/docs/guides/pricing-paygo (2026-
|
|
1601
|
-
//
|
|
1602
|
-
// (≥512 input tokens), hits reported in
|
|
1603
|
-
//
|
|
1610
|
+
// MiniMax M3.1 Flash. Rates from platform.minimax.io/docs/guides/pricing-paygo (2026-10-27,
|
|
1611
|
+
// USD): one tier across the 1M window (M3 had ≤512k / >512k tiers; the page lists none for
|
|
1612
|
+
// M3.1 Flash). Caching is automatic (≥512 input tokens), hits reported in
|
|
1613
|
+
// prompt_tokens_details.cached_tokens; no write fee listed.
|
|
1604
1614
|
[SupportedAiModels[LLM_CONSTANTS.MINIMAX].modelApiName]: {
|
|
1605
|
-
inputPrice: 0.
|
|
1606
|
-
outputPrice:
|
|
1607
|
-
cacheHitPrice: 0.
|
|
1608
|
-
extendedContextInputPrice: 0.6,
|
|
1609
|
-
extendedContextOutputPrice: 2.4,
|
|
1610
|
-
extendedContextCacheHitPrice: 0.12,
|
|
1611
|
-
extendedContextThresholdTokens: 512e3
|
|
1615
|
+
inputPrice: 0.15,
|
|
1616
|
+
outputPrice: 0.6,
|
|
1617
|
+
cacheHitPrice: 0.015
|
|
1612
1618
|
},
|
|
1613
1619
|
// Meta Muse Spark 1.3, Standard tier. Rates from ai.developer.meta.com/docs/pricing-rate-limits
|
|
1614
1620
|
// (2026-09-12): no long-context premium at any point of the 1M window; reasoning tokens bill
|
|
@@ -1638,7 +1644,7 @@ var HYBRID_THINKING_API_NAMES = /* @__PURE__ */ new Set([
|
|
|
1638
1644
|
// hybrid by the same definition: we force reasoning on, so they carry the multiplier.
|
|
1639
1645
|
SupportedAiModels[LLM_CONSTANTS.QWEN_MAX].modelApiName,
|
|
1640
1646
|
SupportedAiModels[LLM_CONSTANTS.QWEN_FLASH].modelApiName,
|
|
1641
|
-
|
|
1647
|
+
// MiniMax left the set 2026-10-27: M3.1 Flash cannot turn thinking off (M3 could).
|
|
1642
1648
|
// Mistral reasons only when asked (`reasoning_effort`), and we always ask — hybrid by the
|
|
1643
1649
|
// same definition as Qwen. Added 2026-09-18 when the Mistral entries went thinking-on.
|
|
1644
1650
|
SupportedAiModels[LLM_CONSTANTS.MISTRAL_SMALL].modelApiName
|
|
@@ -1711,6 +1717,7 @@ var META_REASONING_EFFORTS = ["minimal", "low", "medium", "high", "xhigh", "max"
|
|
|
1711
1717
|
var MISTRAL_REASONING_EFFORTS = ["high"];
|
|
1712
1718
|
var XAI_REASONING_EFFORTS = ["low", "medium", "high", "xhigh"];
|
|
1713
1719
|
var KIMI_REASONING_EFFORTS = ["low", "high", "max"];
|
|
1720
|
+
var MINIMAX_REASONING_EFFORTS = ["low", "medium", "high", "xhigh", "max"];
|
|
1714
1721
|
function clampReasoningEffort(effort, allowed) {
|
|
1715
1722
|
const rank = REASONING_EFFORT_SCALE.indexOf(effort);
|
|
1716
1723
|
let best = allowed[0];
|
|
@@ -1734,6 +1741,7 @@ var toFuguEffort = (effort) => clampReasoningEffort(effort, FUGU_REASONING_EFFOR
|
|
|
1734
1741
|
var toMistralEffort = (effort) => clampReasoningEffort(effort, MISTRAL_REASONING_EFFORTS);
|
|
1735
1742
|
var toXaiEffort = (effort) => clampReasoningEffort(effort, XAI_REASONING_EFFORTS);
|
|
1736
1743
|
var toKimiEffort = (effort) => clampReasoningEffort(effort, KIMI_REASONING_EFFORTS);
|
|
1744
|
+
var toMiniMaxEffort = (effort) => clampReasoningEffort(effort, MINIMAX_REASONING_EFFORTS);
|
|
1737
1745
|
|
|
1738
1746
|
// src/pricing/token-usage-utils.ts
|
|
1739
1747
|
function extractTokenUsage(response) {
|
|
@@ -2356,6 +2364,10 @@ var beforeAskHook;
|
|
|
2356
2364
|
function setBeforeAskHook(hook) {
|
|
2357
2365
|
beforeAskHook = hook;
|
|
2358
2366
|
}
|
|
2367
|
+
var retryHook;
|
|
2368
|
+
function setRetryHook(hook) {
|
|
2369
|
+
retryHook = hook;
|
|
2370
|
+
}
|
|
2359
2371
|
var AbstractAgent = class {
|
|
2360
2372
|
name;
|
|
2361
2373
|
gameId;
|
|
@@ -2382,6 +2394,17 @@ var AbstractAgent = class {
|
|
|
2382
2394
|
* instance for calls that legitimately run long, like maxOutputTokens.
|
|
2383
2395
|
*/
|
|
2384
2396
|
requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT_MS;
|
|
2397
|
+
/**
|
|
2398
|
+
* How many times one ask is re-sent after a ModelInvalidResponseError that is not a
|
|
2399
|
+
* cap hit: the model answered with nothing, or with text that is not the JSON the
|
|
2400
|
+
* schema asked for. Those are one-off generation failures the same prompt usually
|
|
2401
|
+
* gets right on the next try, unlike timeouts, refusals and transport errors, which
|
|
2402
|
+
* are never retried here. 0 (the default) hands every failure to the caller. A
|
|
2403
|
+
* truncated response is excluded because re-running it re-buys the whole output at
|
|
2404
|
+
* the cap. Each re-send runs the before-ask hook again (it is a new paid call) and
|
|
2405
|
+
* fires the retry hook; the error finally thrown carries `retries`.
|
|
2406
|
+
*/
|
|
2407
|
+
invalidResponseRetries = 0;
|
|
2385
2408
|
instruction;
|
|
2386
2409
|
/**
|
|
2387
2410
|
* The instruction split on CACHE_TIER_MARKER: [shared static tier, per-bot tier].
|
|
@@ -2408,26 +2431,83 @@ var AbstractAgent = class {
|
|
|
2408
2431
|
this.thinkingBudgetTokens = modelConfig?.thinkingBudgetTokens;
|
|
2409
2432
|
}
|
|
2410
2433
|
/**
|
|
2411
|
-
* Public ask API — template methods that
|
|
2412
|
-
* into the returned TokenUsage
|
|
2434
|
+
* Public ask API — template methods that run the before-ask hook, time the provider
|
|
2435
|
+
* call, stamp `durationMs` into the returned TokenUsage and apply the
|
|
2436
|
+
* invalidResponseRetries policy. Subclasses implement doAskWithZodSchema/doAskText and
|
|
2413
2437
|
* must NOT override these.
|
|
2414
2438
|
*/
|
|
2415
2439
|
async askWithZodSchema(zodSchema, messages) {
|
|
2416
|
-
|
|
2417
|
-
|
|
2440
|
+
return this.askWithRetries(() => this.doAskWithZodSchema(zodSchema, messages));
|
|
2441
|
+
}
|
|
2442
|
+
async askText(messages) {
|
|
2443
|
+
return this.askWithRetries(() => this.doAskText(messages));
|
|
2444
|
+
}
|
|
2445
|
+
/**
|
|
2446
|
+
* One attempt per loop; an empty or malformed answer (ModelInvalidResponseError, not
|
|
2447
|
+
* truncated) is re-sent up to invalidResponseRetries times, with the retry hook told
|
|
2448
|
+
* before each re-send. Anything else — a timeout, a refusal, a transport error, or the
|
|
2449
|
+
* last allowed attempt failing — is thrown as-is, stamped with the retry count. Every
|
|
2450
|
+
* settle function the hook returned runs once the ask is over, whichever way it ended.
|
|
2451
|
+
*/
|
|
2452
|
+
async askWithRetries(call) {
|
|
2453
|
+
const settled = [];
|
|
2454
|
+
let retries = 0;
|
|
2418
2455
|
try {
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2456
|
+
while (true) {
|
|
2457
|
+
try {
|
|
2458
|
+
return await this.attempt(call);
|
|
2459
|
+
} catch (error) {
|
|
2460
|
+
if (retries > 0 && error && typeof error === "object") {
|
|
2461
|
+
error.retries = retries;
|
|
2462
|
+
}
|
|
2463
|
+
if (!(error instanceof ModelInvalidResponseError) || error.truncated || retries >= this.invalidResponseRetries) {
|
|
2464
|
+
throw error;
|
|
2465
|
+
}
|
|
2466
|
+
retries++;
|
|
2467
|
+
logger.warn(`${this.name} (${this.model}): ${error.message} \u2014 asking again (retry ${retries} of ${this.invalidResponseRetries})`, {
|
|
2468
|
+
gameId: this.gameId,
|
|
2469
|
+
userId: this.userId,
|
|
2470
|
+
agentName: this.name,
|
|
2471
|
+
model: this.model,
|
|
2472
|
+
activity: "llm_retry",
|
|
2473
|
+
attempt: retries,
|
|
2474
|
+
error: error.message
|
|
2475
|
+
});
|
|
2476
|
+
if (retryHook) {
|
|
2477
|
+
try {
|
|
2478
|
+
const onSettled = await retryHook({ agent: this, model: this.model, error, attempt: retries });
|
|
2479
|
+
if (typeof onSettled === "function") settled.push(onSettled);
|
|
2480
|
+
} catch (hookError) {
|
|
2481
|
+
logger.warn(`${this.name} (${this.model}): retry hook failed, retrying anyway`, {
|
|
2482
|
+
gameId: this.gameId,
|
|
2483
|
+
agentName: this.name,
|
|
2484
|
+
model: this.model,
|
|
2485
|
+
error: hookError instanceof Error ? hookError.message : String(hookError)
|
|
2486
|
+
});
|
|
2487
|
+
}
|
|
2488
|
+
}
|
|
2489
|
+
}
|
|
2490
|
+
}
|
|
2491
|
+
} finally {
|
|
2492
|
+
for (const onSettled of settled) {
|
|
2493
|
+
try {
|
|
2494
|
+
await onSettled();
|
|
2495
|
+
} catch (hookError) {
|
|
2496
|
+
logger.warn(`${this.name} (${this.model}): retry settle hook failed`, {
|
|
2497
|
+
gameId: this.gameId,
|
|
2498
|
+
agentName: this.name,
|
|
2499
|
+
model: this.model,
|
|
2500
|
+
error: hookError instanceof Error ? hookError.message : String(hookError)
|
|
2501
|
+
});
|
|
2502
|
+
}
|
|
2503
|
+
}
|
|
2424
2504
|
}
|
|
2425
2505
|
}
|
|
2426
|
-
async
|
|
2506
|
+
async attempt(call) {
|
|
2427
2507
|
if (beforeAskHook) await beforeAskHook(this);
|
|
2428
2508
|
const startedAt = Date.now();
|
|
2429
2509
|
try {
|
|
2430
|
-
const [content, thinking, usage, signature] = await this.withTimeout(
|
|
2510
|
+
const [content, thinking, usage, signature] = await this.withTimeout(call());
|
|
2431
2511
|
return [content, thinking, this.stampDuration(usage, startedAt), signature];
|
|
2432
2512
|
} catch (error) {
|
|
2433
2513
|
this.stampErrorDuration(error, startedAt);
|
|
@@ -2727,7 +2807,7 @@ ${instruction}`);
|
|
|
2727
2807
|
reason === "max_output_tokens"
|
|
2728
2808
|
);
|
|
2729
2809
|
}
|
|
2730
|
-
throw new
|
|
2810
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
2731
2811
|
}
|
|
2732
2812
|
let tokenUsage;
|
|
2733
2813
|
if (response.usage) {
|
|
@@ -3008,7 +3088,7 @@ var ClaudeAgent = class extends AbstractAgent {
|
|
|
3008
3088
|
throw new ModelRefusalError(this.model, void 0, "refusal");
|
|
3009
3089
|
}
|
|
3010
3090
|
if (!("content" in response) || !Array.isArray(response.content) || response.content.length === 0) {
|
|
3011
|
-
throw new
|
|
3091
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
3012
3092
|
}
|
|
3013
3093
|
let textContent = null;
|
|
3014
3094
|
let thinkingContent = "";
|
|
@@ -3025,9 +3105,9 @@ var ClaudeAgent = class extends AbstractAgent {
|
|
|
3025
3105
|
}
|
|
3026
3106
|
}
|
|
3027
3107
|
if (!textContent) {
|
|
3028
|
-
throw new
|
|
3108
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.invalidFormat);
|
|
3029
3109
|
}
|
|
3030
|
-
const parsedData = parseAndValidateLlmJson(textContent, zodSchema, (m) => this.logger(m));
|
|
3110
|
+
const parsedData = parseAndValidateLlmJson(textContent, zodSchema, (m) => this.logger(m), this.model);
|
|
3031
3111
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
3032
3112
|
let tokenUsage;
|
|
3033
3113
|
if (response.usage) {
|
|
@@ -3106,7 +3186,7 @@ var ClaudeAgent = class extends AbstractAgent {
|
|
|
3106
3186
|
throw new ModelRefusalError(this.model, void 0, "refusal");
|
|
3107
3187
|
}
|
|
3108
3188
|
if (!("content" in response) || !Array.isArray(response.content) || response.content.length === 0) {
|
|
3109
|
-
throw new
|
|
3189
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
3110
3190
|
}
|
|
3111
3191
|
const textParts = [];
|
|
3112
3192
|
let thinkingContent = "";
|
|
@@ -3124,7 +3204,7 @@ var ClaudeAgent = class extends AbstractAgent {
|
|
|
3124
3204
|
}
|
|
3125
3205
|
const textContent = textParts.join("");
|
|
3126
3206
|
if (!textContent) {
|
|
3127
|
-
throw new
|
|
3207
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
3128
3208
|
}
|
|
3129
3209
|
let tokenUsage;
|
|
3130
3210
|
if (response.usage) {
|
|
@@ -3196,7 +3276,7 @@ function throwForEmptyGeminiResponse(model, response, fallback) {
|
|
|
3196
3276
|
const bits = [`finishReason=${finishReason ?? "none"}`];
|
|
3197
3277
|
if (!response?.candidates?.length) bits.push("no candidates");
|
|
3198
3278
|
if (finishMessage) bits.push(`finishMessage="${finishMessage}"`);
|
|
3199
|
-
throw new
|
|
3279
|
+
throw new ModelInvalidResponseError(model, `${fallback} (${bits.join(", ")})`);
|
|
3200
3280
|
}
|
|
3201
3281
|
var GoogleAgent = class extends AbstractAgent {
|
|
3202
3282
|
client;
|
|
@@ -3392,7 +3472,7 @@ var GoogleAgent = class extends AbstractAgent {
|
|
|
3392
3472
|
if (!response.text) {
|
|
3393
3473
|
throwForEmptyGeminiResponse(this.model, response, this.errorMessages.emptyResponse);
|
|
3394
3474
|
}
|
|
3395
|
-
const parsedData = parseAndValidateLlmJson(response.text, zodSchema, (m) => this.logger(m));
|
|
3475
|
+
const parsedData = parseAndValidateLlmJson(response.text, zodSchema, (m) => this.logger(m), this.model);
|
|
3396
3476
|
if (parsedData) {
|
|
3397
3477
|
this.logReply(parsedData, thinkingContent || void 0, tokenUsage);
|
|
3398
3478
|
}
|
|
@@ -3628,7 +3708,7 @@ ${instruction}`);
|
|
|
3628
3708
|
processReply(response) {
|
|
3629
3709
|
const message = response?.choices?.[0]?.message;
|
|
3630
3710
|
if (!message || !message.content) {
|
|
3631
|
-
throw new
|
|
3711
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
3632
3712
|
}
|
|
3633
3713
|
let reply = message.content;
|
|
3634
3714
|
if (Array.isArray(reply)) {
|
|
@@ -3733,12 +3813,12 @@ ${schemaDescription}`
|
|
|
3733
3813
|
throw new Error(this.errorMessages.apiError(apiError));
|
|
3734
3814
|
}
|
|
3735
3815
|
if (!response || !response.choices || response.choices.length === 0) {
|
|
3736
|
-
throw new
|
|
3816
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
3737
3817
|
}
|
|
3738
3818
|
const choice = response.choices[0];
|
|
3739
3819
|
const content = choice.message?.content;
|
|
3740
3820
|
if (!content) {
|
|
3741
|
-
throw new
|
|
3821
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.invalidFormat);
|
|
3742
3822
|
}
|
|
3743
3823
|
let responseText;
|
|
3744
3824
|
let thinkingContent = "";
|
|
@@ -3751,7 +3831,7 @@ ${schemaDescription}`
|
|
|
3751
3831
|
} else {
|
|
3752
3832
|
responseText = JSON.stringify(content);
|
|
3753
3833
|
}
|
|
3754
|
-
const parsedData = parseAndValidateLlmJson(responseText, zodSchema, (m) => this.logger(m));
|
|
3834
|
+
const parsedData = parseAndValidateLlmJson(responseText, zodSchema, (m) => this.logger(m), this.model);
|
|
3755
3835
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
3756
3836
|
const tokenUsage = this.extractTokenUsage(response);
|
|
3757
3837
|
if (parsedData) {
|
|
@@ -3760,6 +3840,7 @@ ${schemaDescription}`
|
|
|
3760
3840
|
return [parsedData, thinkingContent, tokenUsage];
|
|
3761
3841
|
} catch (error) {
|
|
3762
3842
|
this.logger(this.logTemplates.error(this.name, error));
|
|
3843
|
+
if (error instanceof ModelError) throw error;
|
|
3763
3844
|
throw new Error(this.errorMessages.apiError(error));
|
|
3764
3845
|
}
|
|
3765
3846
|
}
|
|
@@ -3789,12 +3870,13 @@ ${schemaDescription}`
|
|
|
3789
3870
|
}
|
|
3790
3871
|
const [content, thinkingContent, tokenUsage] = this.processReply(response);
|
|
3791
3872
|
if (!content) {
|
|
3792
|
-
throw new
|
|
3873
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
3793
3874
|
}
|
|
3794
3875
|
this.logReply(content, thinkingContent || void 0, tokenUsage);
|
|
3795
3876
|
return [content, thinkingContent, tokenUsage];
|
|
3796
3877
|
} catch (error) {
|
|
3797
3878
|
this.logger(this.logTemplates.error(this.name, error));
|
|
3879
|
+
if (error instanceof ModelError) throw error;
|
|
3798
3880
|
throw new Error(this.errorMessages.apiError(error));
|
|
3799
3881
|
}
|
|
3800
3882
|
}
|
|
@@ -3811,7 +3893,7 @@ var DeepSeekV2Agent = class extends AbstractAgent {
|
|
|
3811
3893
|
};
|
|
3812
3894
|
// Error message templates
|
|
3813
3895
|
errorMessages = {
|
|
3814
|
-
emptyResponse:
|
|
3896
|
+
emptyResponse: (finishReason) => `Empty or undefined response from DeepSeek API (finish_reason: ${finishReason ?? "unknown"})`,
|
|
3815
3897
|
invalidFormat: "Invalid response format from DeepSeek API",
|
|
3816
3898
|
apiError: (error) => `Failed to get response from DeepSeek API: ${error instanceof Error ? error.message : String(error)}`
|
|
3817
3899
|
};
|
|
@@ -3916,14 +3998,14 @@ ${schemaDescription}`
|
|
|
3916
3998
|
}
|
|
3917
3999
|
const rawContent = response.choices[0]?.message?.content;
|
|
3918
4000
|
if (!rawContent) {
|
|
3919
|
-
throw new
|
|
4001
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(response.choices[0]?.finish_reason));
|
|
3920
4002
|
}
|
|
3921
4003
|
const { text: content, thinking: inlineThinking } = stripInlineThinking(rawContent);
|
|
3922
4004
|
thinkingContent = mergeThinking(thinkingContent, inlineThinking);
|
|
3923
4005
|
if (!content) {
|
|
3924
|
-
throw new
|
|
4006
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(response.choices[0]?.finish_reason));
|
|
3925
4007
|
}
|
|
3926
|
-
const parsedData = parseAndValidateLlmJson(content, zodSchema, (m) => this.logger(m));
|
|
4008
|
+
const parsedData = parseAndValidateLlmJson(content, zodSchema, (m) => this.logger(m), this.model);
|
|
3927
4009
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
3928
4010
|
const usageResult = extractUsageAndCalculateCost(this.model, response);
|
|
3929
4011
|
let tokenUsage;
|
|
@@ -3944,6 +4026,7 @@ ${schemaDescription}`
|
|
|
3944
4026
|
return [parsedData, thinkingContent, tokenUsage];
|
|
3945
4027
|
} catch (error) {
|
|
3946
4028
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4029
|
+
if (error instanceof ModelError) throw error;
|
|
3947
4030
|
throw new Error(this.errorMessages.apiError(error));
|
|
3948
4031
|
}
|
|
3949
4032
|
}
|
|
@@ -3979,12 +4062,12 @@ ${schemaDescription}`
|
|
|
3979
4062
|
}
|
|
3980
4063
|
const rawContent = response.choices[0]?.message?.content;
|
|
3981
4064
|
if (!rawContent) {
|
|
3982
|
-
throw new
|
|
4065
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(response.choices[0]?.finish_reason));
|
|
3983
4066
|
}
|
|
3984
4067
|
const { text: content, thinking: inlineThinking } = stripInlineThinking(rawContent);
|
|
3985
4068
|
thinkingContent = mergeThinking(thinkingContent, inlineThinking);
|
|
3986
4069
|
if (!content) {
|
|
3987
|
-
throw new
|
|
4070
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(response.choices[0]?.finish_reason));
|
|
3988
4071
|
}
|
|
3989
4072
|
const usageResult = extractUsageAndCalculateCost(this.model, response);
|
|
3990
4073
|
let tokenUsage;
|
|
@@ -4003,6 +4086,7 @@ ${schemaDescription}`
|
|
|
4003
4086
|
return [content, thinkingContent, tokenUsage];
|
|
4004
4087
|
} catch (error) {
|
|
4005
4088
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4089
|
+
if (error instanceof ModelError) throw error;
|
|
4006
4090
|
throw new Error(this.errorMessages.apiError(error));
|
|
4007
4091
|
}
|
|
4008
4092
|
}
|
|
@@ -4053,10 +4137,10 @@ ${schemaDescription}`;
|
|
|
4053
4137
|
const response = await this.createResponse(input, true);
|
|
4054
4138
|
const { text, reasoningSummary, encryptedReasoning } = this.extractResponseParts(response);
|
|
4055
4139
|
if (!text) {
|
|
4056
|
-
throw new
|
|
4140
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4057
4141
|
}
|
|
4058
4142
|
this.logger(`Grok Agent - Found reasoning summary: ${!!reasoningSummary}, encrypted reasoning: ${!!encryptedReasoning}`);
|
|
4059
|
-
const parsedData = parseAndValidateLlmJson(text, zodSchema, (m) => this.logger(m));
|
|
4143
|
+
const parsedData = parseAndValidateLlmJson(text, zodSchema, (m) => this.logger(m), this.model);
|
|
4060
4144
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
4061
4145
|
const tokenUsage = this.extractTokenUsage(response);
|
|
4062
4146
|
if (parsedData) {
|
|
@@ -4065,6 +4149,7 @@ ${schemaDescription}`;
|
|
|
4065
4149
|
return [parsedData, reasoningSummary, tokenUsage, encryptedReasoning];
|
|
4066
4150
|
} catch (error) {
|
|
4067
4151
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4152
|
+
if (error instanceof ModelError) throw error;
|
|
4068
4153
|
throw new Error(this.errorMessages.apiError(error));
|
|
4069
4154
|
}
|
|
4070
4155
|
}
|
|
@@ -4080,13 +4165,14 @@ ${schemaDescription}`;
|
|
|
4080
4165
|
const response = await this.createResponse(input, false);
|
|
4081
4166
|
const { text, reasoningSummary, encryptedReasoning } = this.extractResponseParts(response);
|
|
4082
4167
|
if (!text) {
|
|
4083
|
-
throw new
|
|
4168
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4084
4169
|
}
|
|
4085
4170
|
const tokenUsage = this.extractTokenUsage(response);
|
|
4086
4171
|
this.logReply(text, reasoningSummary, tokenUsage);
|
|
4087
4172
|
return [text, reasoningSummary, tokenUsage, encryptedReasoning];
|
|
4088
4173
|
} catch (error) {
|
|
4089
4174
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4175
|
+
if (error instanceof ModelError) throw error;
|
|
4090
4176
|
throw new Error(this.errorMessages.apiError(error));
|
|
4091
4177
|
}
|
|
4092
4178
|
}
|
|
@@ -4326,13 +4412,13 @@ ${openAIMessages[0].content}`;
|
|
|
4326
4412
|
}
|
|
4327
4413
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4328
4414
|
if (!rawReply) {
|
|
4329
|
-
throw new
|
|
4415
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4330
4416
|
}
|
|
4331
4417
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4332
4418
|
if (!reply) {
|
|
4333
|
-
throw new
|
|
4419
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4334
4420
|
}
|
|
4335
|
-
const parsedData = parseAndValidateLlmJson(reply, zodSchema, (m) => this.logger(m));
|
|
4421
|
+
const parsedData = parseAndValidateLlmJson(reply, zodSchema, (m) => this.logger(m), this.model);
|
|
4336
4422
|
this.logger(`\u2705 Response validated successfully with Zod schema (JSON mode)`);
|
|
4337
4423
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
4338
4424
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -4363,13 +4449,13 @@ ${schemaDescription}`;
|
|
|
4363
4449
|
}
|
|
4364
4450
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4365
4451
|
if (!rawReply) {
|
|
4366
|
-
throw new
|
|
4452
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4367
4453
|
}
|
|
4368
4454
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4369
4455
|
if (!reply) {
|
|
4370
|
-
throw new
|
|
4456
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4371
4457
|
}
|
|
4372
|
-
const parsedData = parseAndValidateLlmJson(reply, zodSchema, (m) => this.logger(m));
|
|
4458
|
+
const parsedData = parseAndValidateLlmJson(reply, zodSchema, (m) => this.logger(m), this.model);
|
|
4373
4459
|
this.logger(`\u2705 Response validated successfully with Zod schema (prompt mode)`);
|
|
4374
4460
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
4375
4461
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -4380,6 +4466,7 @@ ${schemaDescription}`;
|
|
|
4380
4466
|
}
|
|
4381
4467
|
} catch (error) {
|
|
4382
4468
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4469
|
+
if (error instanceof ModelError) throw error;
|
|
4383
4470
|
throw new Error(this.errorMessages.apiError(error));
|
|
4384
4471
|
}
|
|
4385
4472
|
}
|
|
@@ -4416,11 +4503,11 @@ ${openAIMessages[0].content}`;
|
|
|
4416
4503
|
}
|
|
4417
4504
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4418
4505
|
if (!rawReply) {
|
|
4419
|
-
throw new
|
|
4506
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4420
4507
|
}
|
|
4421
4508
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4422
4509
|
if (!reply) {
|
|
4423
|
-
throw new
|
|
4510
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4424
4511
|
}
|
|
4425
4512
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
4426
4513
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -4428,6 +4515,7 @@ ${openAIMessages[0].content}`;
|
|
|
4428
4515
|
return [reply, thinkingContent, tokenUsage];
|
|
4429
4516
|
} catch (error) {
|
|
4430
4517
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4518
|
+
if (error instanceof ModelError) throw error;
|
|
4431
4519
|
throw new Error(this.errorMessages.apiError(error));
|
|
4432
4520
|
}
|
|
4433
4521
|
}
|
|
@@ -4504,7 +4592,7 @@ var GlmAgent = class extends AbstractAgent {
|
|
|
4504
4592
|
* Returns the validated value or throws.
|
|
4505
4593
|
*/
|
|
4506
4594
|
parseAndValidate(rawReply, zodSchema) {
|
|
4507
|
-
return parseAndValidateLlmJson(rawReply, zodSchema, (m) => this.logger(m));
|
|
4595
|
+
return parseAndValidateLlmJson(rawReply, zodSchema, (m) => this.logger(m), this.model);
|
|
4508
4596
|
}
|
|
4509
4597
|
async doAskWithZodSchema(zodSchema, messages) {
|
|
4510
4598
|
try {
|
|
@@ -4544,11 +4632,11 @@ ${schemaDescription}`;
|
|
|
4544
4632
|
}
|
|
4545
4633
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4546
4634
|
if (!rawReply) {
|
|
4547
|
-
throw new
|
|
4635
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(completion.choices[0]?.finish_reason));
|
|
4548
4636
|
}
|
|
4549
4637
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4550
4638
|
if (!reply) {
|
|
4551
|
-
throw new
|
|
4639
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(completion.choices[0]?.finish_reason));
|
|
4552
4640
|
}
|
|
4553
4641
|
const validated = this.parseAndValidate(reply, zodSchema);
|
|
4554
4642
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
@@ -4560,6 +4648,7 @@ ${schemaDescription}`;
|
|
|
4560
4648
|
return [validated, thinkingContent, tokenUsage];
|
|
4561
4649
|
} catch (error) {
|
|
4562
4650
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4651
|
+
if (error instanceof ModelError) throw error;
|
|
4563
4652
|
throw new Error(this.errorMessages.apiError(error));
|
|
4564
4653
|
}
|
|
4565
4654
|
}
|
|
@@ -4596,11 +4685,11 @@ ${openAIMessages[0].content}`;
|
|
|
4596
4685
|
}
|
|
4597
4686
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4598
4687
|
if (!rawReply) {
|
|
4599
|
-
throw new
|
|
4688
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(completion.choices[0]?.finish_reason));
|
|
4600
4689
|
}
|
|
4601
4690
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4602
4691
|
if (!reply) {
|
|
4603
|
-
throw new
|
|
4692
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(completion.choices[0]?.finish_reason));
|
|
4604
4693
|
}
|
|
4605
4694
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
4606
4695
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -4608,6 +4697,7 @@ ${openAIMessages[0].content}`;
|
|
|
4608
4697
|
return [reply, thinkingContent, tokenUsage];
|
|
4609
4698
|
} catch (error) {
|
|
4610
4699
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4700
|
+
if (error instanceof ModelError) throw error;
|
|
4611
4701
|
throw new Error(this.errorMessages.apiError(error));
|
|
4612
4702
|
}
|
|
4613
4703
|
}
|
|
@@ -4710,13 +4800,13 @@ ${schemaDescription}`;
|
|
|
4710
4800
|
}
|
|
4711
4801
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4712
4802
|
if (!rawReply) {
|
|
4713
|
-
throw new
|
|
4803
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4714
4804
|
}
|
|
4715
4805
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4716
4806
|
if (!reply) {
|
|
4717
|
-
throw new
|
|
4807
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4718
4808
|
}
|
|
4719
|
-
const validated = parseAndValidateLlmJson(reply, zodSchema, (m) => this.logger(m));
|
|
4809
|
+
const validated = parseAndValidateLlmJson(reply, zodSchema, (m) => this.logger(m), this.model);
|
|
4720
4810
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
4721
4811
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
4722
4812
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -4726,6 +4816,7 @@ ${schemaDescription}`;
|
|
|
4726
4816
|
return [validated, thinkingContent, tokenUsage];
|
|
4727
4817
|
} catch (error) {
|
|
4728
4818
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4819
|
+
if (error instanceof ModelError) throw error;
|
|
4729
4820
|
throw new Error(this.errorMessages.apiError(error));
|
|
4730
4821
|
}
|
|
4731
4822
|
}
|
|
@@ -4753,11 +4844,11 @@ ${schemaDescription}`;
|
|
|
4753
4844
|
}
|
|
4754
4845
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4755
4846
|
if (!rawReply) {
|
|
4756
|
-
throw new
|
|
4847
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4757
4848
|
}
|
|
4758
4849
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4759
4850
|
if (!reply) {
|
|
4760
|
-
throw new
|
|
4851
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4761
4852
|
}
|
|
4762
4853
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
4763
4854
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -4765,6 +4856,7 @@ ${schemaDescription}`;
|
|
|
4765
4856
|
return [reply, thinkingContent, tokenUsage];
|
|
4766
4857
|
} catch (error) {
|
|
4767
4858
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4859
|
+
if (error instanceof ModelError) throw error;
|
|
4768
4860
|
throw new Error(this.errorMessages.apiError(error));
|
|
4769
4861
|
}
|
|
4770
4862
|
}
|
|
@@ -4922,7 +5014,7 @@ var QwenAgent = class extends AbstractAgent {
|
|
|
4922
5014
|
* Returns the validated value or throws.
|
|
4923
5015
|
*/
|
|
4924
5016
|
parseAndValidate(rawReply, zodSchema) {
|
|
4925
|
-
return parseAndValidateLlmJson(rawReply, zodSchema, (m) => this.logger(m));
|
|
5017
|
+
return parseAndValidateLlmJson(rawReply, zodSchema, (m) => this.logger(m), this.model);
|
|
4926
5018
|
}
|
|
4927
5019
|
async doAskWithZodSchema(zodSchema, messages) {
|
|
4928
5020
|
try {
|
|
@@ -4966,11 +5058,11 @@ ${schemaDescription}`;
|
|
|
4966
5058
|
}
|
|
4967
5059
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4968
5060
|
if (!rawReply) {
|
|
4969
|
-
throw new
|
|
5061
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4970
5062
|
}
|
|
4971
5063
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4972
5064
|
if (!reply) {
|
|
4973
|
-
throw new
|
|
5065
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4974
5066
|
}
|
|
4975
5067
|
const validated = this.parseAndValidate(reply, zodSchema);
|
|
4976
5068
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
@@ -5022,11 +5114,11 @@ ${openAIMessages[0].content}`;
|
|
|
5022
5114
|
}
|
|
5023
5115
|
const rawReply = completion.choices[0]?.message?.content;
|
|
5024
5116
|
if (!rawReply) {
|
|
5025
|
-
throw new
|
|
5117
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5026
5118
|
}
|
|
5027
5119
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
5028
5120
|
if (!reply) {
|
|
5029
|
-
throw new
|
|
5121
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5030
5122
|
}
|
|
5031
5123
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
5032
5124
|
const thinkingContent = [reasoningContent, inlineThinking].filter(Boolean).join("\n");
|
|
@@ -5051,10 +5143,10 @@ var MiniMaxAgent = class extends AbstractAgent {
|
|
|
5051
5143
|
get defaultParams() {
|
|
5052
5144
|
return {
|
|
5053
5145
|
model: this.model,
|
|
5054
|
-
temperature: this.temperature,
|
|
5146
|
+
...this.temperature !== void 0 ? { temperature: this.temperature } : {},
|
|
5055
5147
|
stream: false,
|
|
5056
|
-
// MiniMax deprecates max_tokens in favor of max_completion_tokens (
|
|
5057
|
-
// far above anything a turn needs).
|
|
5148
|
+
// MiniMax deprecates max_tokens in favor of max_completion_tokens (the M-series max is
|
|
5149
|
+
// 512K, far above anything a turn needs).
|
|
5058
5150
|
max_completion_tokens: this.maxOutputTokens
|
|
5059
5151
|
};
|
|
5060
5152
|
}
|
|
@@ -5076,7 +5168,9 @@ var MiniMaxAgent = class extends AbstractAgent {
|
|
|
5076
5168
|
thinkingParams() {
|
|
5077
5169
|
return {
|
|
5078
5170
|
thinking: { type: this.enableThinking ? "adaptive" : "disabled" },
|
|
5079
|
-
reasoning_split: true
|
|
5171
|
+
reasoning_split: true,
|
|
5172
|
+
// Provider default is max; the catalog pins M3.1 Flash to medium. Older models ignore it.
|
|
5173
|
+
...this.enableThinking && this.reasoningEffort ? { reasoning_effort: toMiniMaxEffort(this.reasoningEffort) } : {}
|
|
5080
5174
|
};
|
|
5081
5175
|
}
|
|
5082
5176
|
convertToOpenAIMessages(messages) {
|
|
@@ -5118,7 +5212,7 @@ var MiniMaxAgent = class extends AbstractAgent {
|
|
|
5118
5212
|
* Returns the validated value or throws.
|
|
5119
5213
|
*/
|
|
5120
5214
|
parseAndValidate(rawReply, zodSchema) {
|
|
5121
|
-
return parseAndValidateLlmJson(rawReply, zodSchema, (m) => this.logger(m));
|
|
5215
|
+
return parseAndValidateLlmJson(rawReply, zodSchema, (m) => this.logger(m), this.model);
|
|
5122
5216
|
}
|
|
5123
5217
|
async doAskWithZodSchema(zodSchema, messages) {
|
|
5124
5218
|
try {
|
|
@@ -5158,7 +5252,7 @@ ${schemaDescription}`;
|
|
|
5158
5252
|
}
|
|
5159
5253
|
const reply = completion.choices[0]?.message?.content;
|
|
5160
5254
|
if (!reply) {
|
|
5161
|
-
throw new
|
|
5255
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5162
5256
|
}
|
|
5163
5257
|
const { text: cleanReply, thinking: inlineThinking } = stripInlineThinking(reply);
|
|
5164
5258
|
const validated = this.parseAndValidate(cleanReply, zodSchema);
|
|
@@ -5171,6 +5265,7 @@ ${schemaDescription}`;
|
|
|
5171
5265
|
return [validated, thinkingContent, tokenUsage];
|
|
5172
5266
|
} catch (error) {
|
|
5173
5267
|
this.logger(this.logTemplates.error(this.name, error));
|
|
5268
|
+
if (error instanceof ModelError) throw error;
|
|
5174
5269
|
throw new Error(this.errorMessages.apiError(error));
|
|
5175
5270
|
}
|
|
5176
5271
|
}
|
|
@@ -5208,11 +5303,11 @@ ${openAIMessages[0].content}`;
|
|
|
5208
5303
|
}
|
|
5209
5304
|
const reply = completion.choices[0]?.message?.content;
|
|
5210
5305
|
if (!reply) {
|
|
5211
|
-
throw new
|
|
5306
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5212
5307
|
}
|
|
5213
5308
|
const { text: cleanReply, thinking: inlineThinking } = stripInlineThinking(reply);
|
|
5214
5309
|
if (!cleanReply) {
|
|
5215
|
-
throw new
|
|
5310
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5216
5311
|
}
|
|
5217
5312
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
5218
5313
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -5220,6 +5315,7 @@ ${openAIMessages[0].content}`;
|
|
|
5220
5315
|
return [cleanReply, thinkingContent, tokenUsage];
|
|
5221
5316
|
} catch (error) {
|
|
5222
5317
|
this.logger(this.logTemplates.error(this.name, error));
|
|
5318
|
+
if (error instanceof ModelError) throw error;
|
|
5223
5319
|
throw new Error(this.errorMessages.apiError(error));
|
|
5224
5320
|
}
|
|
5225
5321
|
}
|
|
@@ -5270,10 +5366,10 @@ ${schemaDescription}`;
|
|
|
5270
5366
|
const response = await this.createResponse(input, zodSchema);
|
|
5271
5367
|
const { text, reasoningSummary, encryptedReasoning } = this.extractResponseParts(response);
|
|
5272
5368
|
if (!text) {
|
|
5273
|
-
throw new
|
|
5369
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5274
5370
|
}
|
|
5275
5371
|
this.logger(`Meta Agent - Found reasoning summary: ${!!reasoningSummary}, encrypted reasoning: ${!!encryptedReasoning}`);
|
|
5276
|
-
const parsedData = parseAndValidateLlmJson(text, zodSchema, (m) => this.logger(m));
|
|
5372
|
+
const parsedData = parseAndValidateLlmJson(text, zodSchema, (m) => this.logger(m), this.model);
|
|
5277
5373
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
5278
5374
|
const tokenUsage = this.extractTokenUsage(response);
|
|
5279
5375
|
if (parsedData) {
|
|
@@ -5282,6 +5378,7 @@ ${schemaDescription}`;
|
|
|
5282
5378
|
return [parsedData, reasoningSummary, tokenUsage, encryptedReasoning];
|
|
5283
5379
|
} catch (error) {
|
|
5284
5380
|
this.logger(this.logTemplates.error(this.name, error));
|
|
5381
|
+
if (error instanceof ModelError) throw error;
|
|
5285
5382
|
throw new Error(this.errorMessages.apiError(error));
|
|
5286
5383
|
}
|
|
5287
5384
|
}
|
|
@@ -5297,13 +5394,14 @@ ${schemaDescription}`;
|
|
|
5297
5394
|
const response = await this.createResponse(input);
|
|
5298
5395
|
const { text, reasoningSummary, encryptedReasoning } = this.extractResponseParts(response);
|
|
5299
5396
|
if (!text) {
|
|
5300
|
-
throw new
|
|
5397
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5301
5398
|
}
|
|
5302
5399
|
const tokenUsage = this.extractTokenUsage(response);
|
|
5303
5400
|
this.logReply(text, reasoningSummary, tokenUsage);
|
|
5304
5401
|
return [text, reasoningSummary, tokenUsage, encryptedReasoning];
|
|
5305
5402
|
} catch (error) {
|
|
5306
5403
|
this.logger(this.logTemplates.error(this.name, error));
|
|
5404
|
+
if (error instanceof ModelError) throw error;
|
|
5307
5405
|
throw new Error(this.errorMessages.apiError(error));
|
|
5308
5406
|
}
|
|
5309
5407
|
}
|
|
@@ -5489,7 +5587,7 @@ var AgentFactory = class {
|
|
|
5489
5587
|
case LLM_CONSTANTS.QWEN_MAX:
|
|
5490
5588
|
case LLM_CONSTANTS.QWEN_FLASH:
|
|
5491
5589
|
return new QwenAgent(name, instruction, model.modelApiName, key, model.temperature, shouldEnableThinking);
|
|
5492
|
-
// MiniMax M3 — adaptive thinking
|
|
5590
|
+
// MiniMax M3.1 Flash — always-on adaptive thinking, effort from the catalog
|
|
5493
5591
|
case LLM_CONSTANTS.MINIMAX:
|
|
5494
5592
|
return new MiniMaxAgent(name, instruction, model.modelApiName, key, model.temperature, shouldEnableThinking);
|
|
5495
5593
|
// Meta Muse Spark — Responses API with encrypted reasoning replay, effort from the catalog
|
|
@@ -5540,6 +5638,7 @@ export {
|
|
|
5540
5638
|
LLM_CONSTANTS,
|
|
5541
5639
|
MESSAGE_ROLE,
|
|
5542
5640
|
META_REASONING_EFFORTS,
|
|
5641
|
+
MINIMAX_REASONING_EFFORTS,
|
|
5543
5642
|
MISTRAL_REASONING_EFFORTS,
|
|
5544
5643
|
MODEL_PRICING,
|
|
5545
5644
|
MetaAgent,
|
|
@@ -5636,6 +5735,7 @@ export {
|
|
|
5636
5735
|
safeValidateResponse,
|
|
5637
5736
|
setBeforeAskHook,
|
|
5638
5737
|
setLlmLogger,
|
|
5738
|
+
setRetryHook,
|
|
5639
5739
|
stableHashHex,
|
|
5640
5740
|
stripInlineThinking,
|
|
5641
5741
|
supportsNativeJsonSchema,
|
|
@@ -5646,6 +5746,7 @@ export {
|
|
|
5646
5746
|
toGlmEffort,
|
|
5647
5747
|
toKimiEffort,
|
|
5648
5748
|
toMetaEffort,
|
|
5749
|
+
toMiniMaxEffort,
|
|
5649
5750
|
toMistralEffort,
|
|
5650
5751
|
toOpenAIEffort,
|
|
5651
5752
|
toXaiEffort,
|