@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.js
CHANGED
|
@@ -61,6 +61,7 @@ __export(src_exports, {
|
|
|
61
61
|
LLM_CONSTANTS: () => LLM_CONSTANTS,
|
|
62
62
|
MESSAGE_ROLE: () => MESSAGE_ROLE,
|
|
63
63
|
META_REASONING_EFFORTS: () => META_REASONING_EFFORTS,
|
|
64
|
+
MINIMAX_REASONING_EFFORTS: () => MINIMAX_REASONING_EFFORTS,
|
|
64
65
|
MISTRAL_REASONING_EFFORTS: () => MISTRAL_REASONING_EFFORTS,
|
|
65
66
|
MODEL_PRICING: () => MODEL_PRICING,
|
|
66
67
|
MetaAgent: () => MetaAgent,
|
|
@@ -157,6 +158,7 @@ __export(src_exports, {
|
|
|
157
158
|
safeValidateResponse: () => safeValidateResponse,
|
|
158
159
|
setBeforeAskHook: () => setBeforeAskHook,
|
|
159
160
|
setLlmLogger: () => setLlmLogger,
|
|
161
|
+
setRetryHook: () => setRetryHook,
|
|
160
162
|
stableHashHex: () => stableHashHex,
|
|
161
163
|
stripInlineThinking: () => stripInlineThinking,
|
|
162
164
|
supportsNativeJsonSchema: () => supportsNativeJsonSchema,
|
|
@@ -167,6 +169,7 @@ __export(src_exports, {
|
|
|
167
169
|
toGlmEffort: () => toGlmEffort,
|
|
168
170
|
toKimiEffort: () => toKimiEffort,
|
|
169
171
|
toMetaEffort: () => toMetaEffort,
|
|
172
|
+
toMiniMaxEffort: () => toMiniMaxEffort,
|
|
170
173
|
toMistralEffort: () => toMistralEffort,
|
|
171
174
|
toOpenAIEffort: () => toOpenAIEffort,
|
|
172
175
|
toXaiEffort: () => toXaiEffort,
|
|
@@ -767,6 +770,77 @@ function needsPromptBasedSchema(provider) {
|
|
|
767
770
|
return provider === "anthropic";
|
|
768
771
|
}
|
|
769
772
|
|
|
773
|
+
// src/errors.ts
|
|
774
|
+
var ModelError = class extends Error {
|
|
775
|
+
modelType;
|
|
776
|
+
constructor(message, modelType) {
|
|
777
|
+
super(message);
|
|
778
|
+
this.modelType = modelType;
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
var ModelOverloadError = class extends ModelError {
|
|
782
|
+
retryable;
|
|
783
|
+
constructor(message, modelType, retryable = true) {
|
|
784
|
+
super(message, modelType);
|
|
785
|
+
this.name = "ModelOverloadError";
|
|
786
|
+
this.retryable = retryable;
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
var ModelRateLimitError = class extends ModelError {
|
|
790
|
+
retryAfter;
|
|
791
|
+
// seconds to wait before retrying
|
|
792
|
+
constructor(message, modelType, retryAfter) {
|
|
793
|
+
super(message, modelType);
|
|
794
|
+
this.name = "ModelRateLimitError";
|
|
795
|
+
this.retryAfter = retryAfter;
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
var ModelUnavailableError = class extends ModelError {
|
|
799
|
+
reason;
|
|
800
|
+
constructor(message, modelType, reason = "unknown") {
|
|
801
|
+
super(message, modelType);
|
|
802
|
+
this.name = "ModelUnavailableError";
|
|
803
|
+
this.reason = reason;
|
|
804
|
+
}
|
|
805
|
+
};
|
|
806
|
+
var ModelAuthenticationError = class extends ModelError {
|
|
807
|
+
constructor(message, modelType) {
|
|
808
|
+
super(message, modelType);
|
|
809
|
+
this.name = "ModelAuthenticationError";
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
var ModelQuotaExceededError = class extends ModelError {
|
|
813
|
+
constructor(message, modelType) {
|
|
814
|
+
super(message, modelType);
|
|
815
|
+
this.name = "ModelQuotaExceededError";
|
|
816
|
+
}
|
|
817
|
+
};
|
|
818
|
+
var ModelInvalidResponseError = class extends ModelError {
|
|
819
|
+
truncated;
|
|
820
|
+
retries;
|
|
821
|
+
constructor(modelType, detail, truncated = false) {
|
|
822
|
+
super(`${modelType} failed to produce a valid response: ${detail}`, modelType);
|
|
823
|
+
this.name = "ModelInvalidResponseError";
|
|
824
|
+
this.truncated = truncated;
|
|
825
|
+
}
|
|
826
|
+
};
|
|
827
|
+
var ModelTimeoutError = class extends ModelError {
|
|
828
|
+
timeoutMs;
|
|
829
|
+
constructor(modelType, timeoutMs) {
|
|
830
|
+
super(`${modelType} did not respond within ${Math.round(timeoutMs / 1e3)}s`, modelType);
|
|
831
|
+
this.name = "ModelTimeoutError";
|
|
832
|
+
this.timeoutMs = timeoutMs;
|
|
833
|
+
}
|
|
834
|
+
};
|
|
835
|
+
var ModelRefusalError = class extends ModelError {
|
|
836
|
+
reason;
|
|
837
|
+
constructor(modelType, message = `${modelType} refused to answer (stop_reason: refusal)`, reason) {
|
|
838
|
+
super(message, modelType);
|
|
839
|
+
this.name = "ModelRefusalError";
|
|
840
|
+
this.reason = reason;
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
|
|
770
844
|
// src/json-response-parser.ts
|
|
771
845
|
function extractFirstJsonObject(text) {
|
|
772
846
|
let searchFrom = 0;
|
|
@@ -811,7 +885,7 @@ function normalizeNestedReply(value, log) {
|
|
|
811
885
|
return value;
|
|
812
886
|
}
|
|
813
887
|
function parseAndValidateLlmJson(rawReply, zodSchema, log = () => {
|
|
814
|
-
}) {
|
|
888
|
+
}, model = "The model") {
|
|
815
889
|
const cleaned = cleanResponse(rawReply);
|
|
816
890
|
const candidates = [cleaned];
|
|
817
891
|
if (cleaned.startsWith('"') && cleaned.endsWith('"')) {
|
|
@@ -868,81 +942,11 @@ function parseAndValidateLlmJson(rawReply, zodSchema, log = () => {
|
|
|
868
942
|
}
|
|
869
943
|
if (zodError !== null) {
|
|
870
944
|
log(`Zod validation failed: ${JSON.stringify(zodError.errors)}`);
|
|
871
|
-
throw new
|
|
945
|
+
throw new ModelInvalidResponseError(model, `Response validation failed: ${zodError.message}`);
|
|
872
946
|
}
|
|
873
|
-
throw new
|
|
947
|
+
throw new ModelInvalidResponseError(model, `Failed to parse JSON response: ${parseError}. First 200 chars: ${cleaned.slice(0, 200)}`);
|
|
874
948
|
}
|
|
875
949
|
|
|
876
|
-
// src/errors.ts
|
|
877
|
-
var ModelError = class extends Error {
|
|
878
|
-
modelType;
|
|
879
|
-
constructor(message, modelType) {
|
|
880
|
-
super(message);
|
|
881
|
-
this.modelType = modelType;
|
|
882
|
-
}
|
|
883
|
-
};
|
|
884
|
-
var ModelOverloadError = class extends ModelError {
|
|
885
|
-
retryable;
|
|
886
|
-
constructor(message, modelType, retryable = true) {
|
|
887
|
-
super(message, modelType);
|
|
888
|
-
this.name = "ModelOverloadError";
|
|
889
|
-
this.retryable = retryable;
|
|
890
|
-
}
|
|
891
|
-
};
|
|
892
|
-
var ModelRateLimitError = class extends ModelError {
|
|
893
|
-
retryAfter;
|
|
894
|
-
// seconds to wait before retrying
|
|
895
|
-
constructor(message, modelType, retryAfter) {
|
|
896
|
-
super(message, modelType);
|
|
897
|
-
this.name = "ModelRateLimitError";
|
|
898
|
-
this.retryAfter = retryAfter;
|
|
899
|
-
}
|
|
900
|
-
};
|
|
901
|
-
var ModelUnavailableError = class extends ModelError {
|
|
902
|
-
reason;
|
|
903
|
-
constructor(message, modelType, reason = "unknown") {
|
|
904
|
-
super(message, modelType);
|
|
905
|
-
this.name = "ModelUnavailableError";
|
|
906
|
-
this.reason = reason;
|
|
907
|
-
}
|
|
908
|
-
};
|
|
909
|
-
var ModelAuthenticationError = class extends ModelError {
|
|
910
|
-
constructor(message, modelType) {
|
|
911
|
-
super(message, modelType);
|
|
912
|
-
this.name = "ModelAuthenticationError";
|
|
913
|
-
}
|
|
914
|
-
};
|
|
915
|
-
var ModelQuotaExceededError = class extends ModelError {
|
|
916
|
-
constructor(message, modelType) {
|
|
917
|
-
super(message, modelType);
|
|
918
|
-
this.name = "ModelQuotaExceededError";
|
|
919
|
-
}
|
|
920
|
-
};
|
|
921
|
-
var ModelInvalidResponseError = class extends ModelError {
|
|
922
|
-
truncated;
|
|
923
|
-
constructor(modelType, detail, truncated = false) {
|
|
924
|
-
super(`${modelType} failed to produce a valid response: ${detail}`, modelType);
|
|
925
|
-
this.name = "ModelInvalidResponseError";
|
|
926
|
-
this.truncated = truncated;
|
|
927
|
-
}
|
|
928
|
-
};
|
|
929
|
-
var ModelTimeoutError = class extends ModelError {
|
|
930
|
-
timeoutMs;
|
|
931
|
-
constructor(modelType, timeoutMs) {
|
|
932
|
-
super(`${modelType} did not respond within ${Math.round(timeoutMs / 1e3)}s`, modelType);
|
|
933
|
-
this.name = "ModelTimeoutError";
|
|
934
|
-
this.timeoutMs = timeoutMs;
|
|
935
|
-
}
|
|
936
|
-
};
|
|
937
|
-
var ModelRefusalError = class extends ModelError {
|
|
938
|
-
reason;
|
|
939
|
-
constructor(modelType, message = `${modelType} refused to answer (stop_reason: refusal)`, reason) {
|
|
940
|
-
super(message, modelType);
|
|
941
|
-
this.name = "ModelRefusalError";
|
|
942
|
-
this.reason = reason;
|
|
943
|
-
}
|
|
944
|
-
};
|
|
945
|
-
|
|
946
950
|
// src/thinking-utils.ts
|
|
947
951
|
function stripInlineThinking(raw) {
|
|
948
952
|
let thinking = "";
|
|
@@ -1445,20 +1449,29 @@ var SupportedAiModels = {
|
|
|
1445
1449
|
thinkingBudgetTokens: 1024,
|
|
1446
1450
|
tags: ["slow", "cheap"]
|
|
1447
1451
|
},
|
|
1448
|
-
// MiniMax M3 (OpenAI-compatible endpoint, 1M context).
|
|
1449
|
-
//
|
|
1450
|
-
//
|
|
1451
|
-
//
|
|
1452
|
-
//
|
|
1453
|
-
//
|
|
1454
|
-
// Temperature
|
|
1452
|
+
// MiniMax M3.1 Flash (OpenAI-compatible endpoint, 1M context). `minimax` repointed 2026-10-27
|
|
1453
|
+
// from MiniMax-M3 ($0.30/$1.20 after the permanent 50% off, 25s single-sample turn, 60s
|
|
1454
|
+
// timeouts in prod on 2026-10-25 and 10-27) to MiniMax-M3.1-Flash: half the price
|
|
1455
|
+
// ($0.15/$0.60), and the first MiniMax model with an effort dial — low|medium|high|xhigh|max,
|
|
1456
|
+
// provider default max, so it is pinned. Thinking is always on (`thinking: disabled` → 400),
|
|
1457
|
+
// thinking arrives in `reasoning_content` because the agent sends `reasoning_split: true`
|
|
1458
|
+
// (M3.1 rejects false). Temperature and top_p are documented as ignored ("not supported
|
|
1459
|
+
// yet", always 1 / 0.95), so the entry sets none and the agent sends none.
|
|
1460
|
+
// Live 2026-10-27 at medium: the all-providers sweep ran the schema ask in 3.1s, prose in
|
|
1461
|
+
// 1.7s and the 8-character scene (363 output tokens) in 2.4s — ~150 tok/s, the fastest
|
|
1462
|
+
// provider in the sweep that day (Kimi Highspeed's scene takes 8s); a 3.2k-token game
|
|
1463
|
+
// prompt with a two-sentence JSON reply took 1.9-2.1s at every effort level vs 3.5s for M3.
|
|
1464
|
+
// Tagged `fast` rather than `very-fast`: the sweep straddles the 3s line and production
|
|
1465
|
+
// turns carry 10k+ tokens of context. Launched 2026-09-27 as Token-Plan-only (the
|
|
1466
|
+
// 2026-10-02 probe got `unknown model` on the pay-as-you-go key); on the key since at
|
|
1467
|
+
// least 2026-10-27.
|
|
1455
1468
|
[LLM_CONSTANTS.MINIMAX]: {
|
|
1456
|
-
displayName: "MiniMax M3",
|
|
1457
|
-
modelApiName: "MiniMax-M3",
|
|
1469
|
+
displayName: "MiniMax M3.1 Flash",
|
|
1470
|
+
modelApiName: "MiniMax-M3.1-Flash",
|
|
1458
1471
|
apiKeyName: API_KEY_CONSTANTS.MINIMAX,
|
|
1459
1472
|
hasThinking: true,
|
|
1460
|
-
|
|
1461
|
-
tags: ["
|
|
1473
|
+
reasoningEffort: "medium",
|
|
1474
|
+
tags: ["fast", "cheap"]
|
|
1462
1475
|
},
|
|
1463
1476
|
// Meta Muse Spark 1.3 (added 2026-09-12) on Meta's own Model API — Standard tier, i.e.
|
|
1464
1477
|
// the private model id (the `-contributor` id is a quarter of the price but Meta trains
|
|
@@ -1775,18 +1788,14 @@ var MODEL_PRICING = {
|
|
|
1775
1788
|
outputPrice: 0.47,
|
|
1776
1789
|
cacheHitPrice: 0.016
|
|
1777
1790
|
},
|
|
1778
|
-
// MiniMax M3. Rates from platform.minimax.io/docs/guides/pricing-paygo (2026-
|
|
1779
|
-
//
|
|
1780
|
-
// (≥512 input tokens), hits reported in
|
|
1781
|
-
//
|
|
1791
|
+
// MiniMax M3.1 Flash. Rates from platform.minimax.io/docs/guides/pricing-paygo (2026-10-27,
|
|
1792
|
+
// USD): one tier across the 1M window (M3 had ≤512k / >512k tiers; the page lists none for
|
|
1793
|
+
// M3.1 Flash). Caching is automatic (≥512 input tokens), hits reported in
|
|
1794
|
+
// prompt_tokens_details.cached_tokens; no write fee listed.
|
|
1782
1795
|
[SupportedAiModels[LLM_CONSTANTS.MINIMAX].modelApiName]: {
|
|
1783
|
-
inputPrice: 0.
|
|
1784
|
-
outputPrice:
|
|
1785
|
-
cacheHitPrice: 0.
|
|
1786
|
-
extendedContextInputPrice: 0.6,
|
|
1787
|
-
extendedContextOutputPrice: 2.4,
|
|
1788
|
-
extendedContextCacheHitPrice: 0.12,
|
|
1789
|
-
extendedContextThresholdTokens: 512e3
|
|
1796
|
+
inputPrice: 0.15,
|
|
1797
|
+
outputPrice: 0.6,
|
|
1798
|
+
cacheHitPrice: 0.015
|
|
1790
1799
|
},
|
|
1791
1800
|
// Meta Muse Spark 1.3, Standard tier. Rates from ai.developer.meta.com/docs/pricing-rate-limits
|
|
1792
1801
|
// (2026-09-12): no long-context premium at any point of the 1M window; reasoning tokens bill
|
|
@@ -1816,7 +1825,7 @@ var HYBRID_THINKING_API_NAMES = /* @__PURE__ */ new Set([
|
|
|
1816
1825
|
// hybrid by the same definition: we force reasoning on, so they carry the multiplier.
|
|
1817
1826
|
SupportedAiModels[LLM_CONSTANTS.QWEN_MAX].modelApiName,
|
|
1818
1827
|
SupportedAiModels[LLM_CONSTANTS.QWEN_FLASH].modelApiName,
|
|
1819
|
-
|
|
1828
|
+
// MiniMax left the set 2026-10-27: M3.1 Flash cannot turn thinking off (M3 could).
|
|
1820
1829
|
// Mistral reasons only when asked (`reasoning_effort`), and we always ask — hybrid by the
|
|
1821
1830
|
// same definition as Qwen. Added 2026-09-18 when the Mistral entries went thinking-on.
|
|
1822
1831
|
SupportedAiModels[LLM_CONSTANTS.MISTRAL_SMALL].modelApiName
|
|
@@ -1889,6 +1898,7 @@ var META_REASONING_EFFORTS = ["minimal", "low", "medium", "high", "xhigh", "max"
|
|
|
1889
1898
|
var MISTRAL_REASONING_EFFORTS = ["high"];
|
|
1890
1899
|
var XAI_REASONING_EFFORTS = ["low", "medium", "high", "xhigh"];
|
|
1891
1900
|
var KIMI_REASONING_EFFORTS = ["low", "high", "max"];
|
|
1901
|
+
var MINIMAX_REASONING_EFFORTS = ["low", "medium", "high", "xhigh", "max"];
|
|
1892
1902
|
function clampReasoningEffort(effort, allowed) {
|
|
1893
1903
|
const rank = REASONING_EFFORT_SCALE.indexOf(effort);
|
|
1894
1904
|
let best = allowed[0];
|
|
@@ -1912,6 +1922,7 @@ var toFuguEffort = (effort) => clampReasoningEffort(effort, FUGU_REASONING_EFFOR
|
|
|
1912
1922
|
var toMistralEffort = (effort) => clampReasoningEffort(effort, MISTRAL_REASONING_EFFORTS);
|
|
1913
1923
|
var toXaiEffort = (effort) => clampReasoningEffort(effort, XAI_REASONING_EFFORTS);
|
|
1914
1924
|
var toKimiEffort = (effort) => clampReasoningEffort(effort, KIMI_REASONING_EFFORTS);
|
|
1925
|
+
var toMiniMaxEffort = (effort) => clampReasoningEffort(effort, MINIMAX_REASONING_EFFORTS);
|
|
1915
1926
|
|
|
1916
1927
|
// src/pricing/token-usage-utils.ts
|
|
1917
1928
|
function extractTokenUsage(response) {
|
|
@@ -2534,6 +2545,10 @@ var beforeAskHook;
|
|
|
2534
2545
|
function setBeforeAskHook(hook) {
|
|
2535
2546
|
beforeAskHook = hook;
|
|
2536
2547
|
}
|
|
2548
|
+
var retryHook;
|
|
2549
|
+
function setRetryHook(hook) {
|
|
2550
|
+
retryHook = hook;
|
|
2551
|
+
}
|
|
2537
2552
|
var AbstractAgent = class {
|
|
2538
2553
|
name;
|
|
2539
2554
|
gameId;
|
|
@@ -2560,6 +2575,17 @@ var AbstractAgent = class {
|
|
|
2560
2575
|
* instance for calls that legitimately run long, like maxOutputTokens.
|
|
2561
2576
|
*/
|
|
2562
2577
|
requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT_MS;
|
|
2578
|
+
/**
|
|
2579
|
+
* How many times one ask is re-sent after a ModelInvalidResponseError that is not a
|
|
2580
|
+
* cap hit: the model answered with nothing, or with text that is not the JSON the
|
|
2581
|
+
* schema asked for. Those are one-off generation failures the same prompt usually
|
|
2582
|
+
* gets right on the next try, unlike timeouts, refusals and transport errors, which
|
|
2583
|
+
* are never retried here. 0 (the default) hands every failure to the caller. A
|
|
2584
|
+
* truncated response is excluded because re-running it re-buys the whole output at
|
|
2585
|
+
* the cap. Each re-send runs the before-ask hook again (it is a new paid call) and
|
|
2586
|
+
* fires the retry hook; the error finally thrown carries `retries`.
|
|
2587
|
+
*/
|
|
2588
|
+
invalidResponseRetries = 0;
|
|
2563
2589
|
instruction;
|
|
2564
2590
|
/**
|
|
2565
2591
|
* The instruction split on CACHE_TIER_MARKER: [shared static tier, per-bot tier].
|
|
@@ -2586,26 +2612,83 @@ var AbstractAgent = class {
|
|
|
2586
2612
|
this.thinkingBudgetTokens = modelConfig?.thinkingBudgetTokens;
|
|
2587
2613
|
}
|
|
2588
2614
|
/**
|
|
2589
|
-
* Public ask API — template methods that
|
|
2590
|
-
* into the returned TokenUsage
|
|
2615
|
+
* Public ask API — template methods that run the before-ask hook, time the provider
|
|
2616
|
+
* call, stamp `durationMs` into the returned TokenUsage and apply the
|
|
2617
|
+
* invalidResponseRetries policy. Subclasses implement doAskWithZodSchema/doAskText and
|
|
2591
2618
|
* must NOT override these.
|
|
2592
2619
|
*/
|
|
2593
2620
|
async askWithZodSchema(zodSchema, messages) {
|
|
2594
|
-
|
|
2595
|
-
|
|
2621
|
+
return this.askWithRetries(() => this.doAskWithZodSchema(zodSchema, messages));
|
|
2622
|
+
}
|
|
2623
|
+
async askText(messages) {
|
|
2624
|
+
return this.askWithRetries(() => this.doAskText(messages));
|
|
2625
|
+
}
|
|
2626
|
+
/**
|
|
2627
|
+
* One attempt per loop; an empty or malformed answer (ModelInvalidResponseError, not
|
|
2628
|
+
* truncated) is re-sent up to invalidResponseRetries times, with the retry hook told
|
|
2629
|
+
* before each re-send. Anything else — a timeout, a refusal, a transport error, or the
|
|
2630
|
+
* last allowed attempt failing — is thrown as-is, stamped with the retry count. Every
|
|
2631
|
+
* settle function the hook returned runs once the ask is over, whichever way it ended.
|
|
2632
|
+
*/
|
|
2633
|
+
async askWithRetries(call) {
|
|
2634
|
+
const settled = [];
|
|
2635
|
+
let retries = 0;
|
|
2596
2636
|
try {
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2637
|
+
while (true) {
|
|
2638
|
+
try {
|
|
2639
|
+
return await this.attempt(call);
|
|
2640
|
+
} catch (error) {
|
|
2641
|
+
if (retries > 0 && error && typeof error === "object") {
|
|
2642
|
+
error.retries = retries;
|
|
2643
|
+
}
|
|
2644
|
+
if (!(error instanceof ModelInvalidResponseError) || error.truncated || retries >= this.invalidResponseRetries) {
|
|
2645
|
+
throw error;
|
|
2646
|
+
}
|
|
2647
|
+
retries++;
|
|
2648
|
+
logger.warn(`${this.name} (${this.model}): ${error.message} \u2014 asking again (retry ${retries} of ${this.invalidResponseRetries})`, {
|
|
2649
|
+
gameId: this.gameId,
|
|
2650
|
+
userId: this.userId,
|
|
2651
|
+
agentName: this.name,
|
|
2652
|
+
model: this.model,
|
|
2653
|
+
activity: "llm_retry",
|
|
2654
|
+
attempt: retries,
|
|
2655
|
+
error: error.message
|
|
2656
|
+
});
|
|
2657
|
+
if (retryHook) {
|
|
2658
|
+
try {
|
|
2659
|
+
const onSettled = await retryHook({ agent: this, model: this.model, error, attempt: retries });
|
|
2660
|
+
if (typeof onSettled === "function") settled.push(onSettled);
|
|
2661
|
+
} catch (hookError) {
|
|
2662
|
+
logger.warn(`${this.name} (${this.model}): retry hook failed, retrying anyway`, {
|
|
2663
|
+
gameId: this.gameId,
|
|
2664
|
+
agentName: this.name,
|
|
2665
|
+
model: this.model,
|
|
2666
|
+
error: hookError instanceof Error ? hookError.message : String(hookError)
|
|
2667
|
+
});
|
|
2668
|
+
}
|
|
2669
|
+
}
|
|
2670
|
+
}
|
|
2671
|
+
}
|
|
2672
|
+
} finally {
|
|
2673
|
+
for (const onSettled of settled) {
|
|
2674
|
+
try {
|
|
2675
|
+
await onSettled();
|
|
2676
|
+
} catch (hookError) {
|
|
2677
|
+
logger.warn(`${this.name} (${this.model}): retry settle hook failed`, {
|
|
2678
|
+
gameId: this.gameId,
|
|
2679
|
+
agentName: this.name,
|
|
2680
|
+
model: this.model,
|
|
2681
|
+
error: hookError instanceof Error ? hookError.message : String(hookError)
|
|
2682
|
+
});
|
|
2683
|
+
}
|
|
2684
|
+
}
|
|
2602
2685
|
}
|
|
2603
2686
|
}
|
|
2604
|
-
async
|
|
2687
|
+
async attempt(call) {
|
|
2605
2688
|
if (beforeAskHook) await beforeAskHook(this);
|
|
2606
2689
|
const startedAt = Date.now();
|
|
2607
2690
|
try {
|
|
2608
|
-
const [content, thinking, usage, signature] = await this.withTimeout(
|
|
2691
|
+
const [content, thinking, usage, signature] = await this.withTimeout(call());
|
|
2609
2692
|
return [content, thinking, this.stampDuration(usage, startedAt), signature];
|
|
2610
2693
|
} catch (error) {
|
|
2611
2694
|
this.stampErrorDuration(error, startedAt);
|
|
@@ -2905,7 +2988,7 @@ ${instruction}`);
|
|
|
2905
2988
|
reason === "max_output_tokens"
|
|
2906
2989
|
);
|
|
2907
2990
|
}
|
|
2908
|
-
throw new
|
|
2991
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
2909
2992
|
}
|
|
2910
2993
|
let tokenUsage;
|
|
2911
2994
|
if (response.usage) {
|
|
@@ -3186,7 +3269,7 @@ var ClaudeAgent = class extends AbstractAgent {
|
|
|
3186
3269
|
throw new ModelRefusalError(this.model, void 0, "refusal");
|
|
3187
3270
|
}
|
|
3188
3271
|
if (!("content" in response) || !Array.isArray(response.content) || response.content.length === 0) {
|
|
3189
|
-
throw new
|
|
3272
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
3190
3273
|
}
|
|
3191
3274
|
let textContent = null;
|
|
3192
3275
|
let thinkingContent = "";
|
|
@@ -3203,9 +3286,9 @@ var ClaudeAgent = class extends AbstractAgent {
|
|
|
3203
3286
|
}
|
|
3204
3287
|
}
|
|
3205
3288
|
if (!textContent) {
|
|
3206
|
-
throw new
|
|
3289
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.invalidFormat);
|
|
3207
3290
|
}
|
|
3208
|
-
const parsedData = parseAndValidateLlmJson(textContent, zodSchema, (m) => this.logger(m));
|
|
3291
|
+
const parsedData = parseAndValidateLlmJson(textContent, zodSchema, (m) => this.logger(m), this.model);
|
|
3209
3292
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
3210
3293
|
let tokenUsage;
|
|
3211
3294
|
if (response.usage) {
|
|
@@ -3284,7 +3367,7 @@ var ClaudeAgent = class extends AbstractAgent {
|
|
|
3284
3367
|
throw new ModelRefusalError(this.model, void 0, "refusal");
|
|
3285
3368
|
}
|
|
3286
3369
|
if (!("content" in response) || !Array.isArray(response.content) || response.content.length === 0) {
|
|
3287
|
-
throw new
|
|
3370
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
3288
3371
|
}
|
|
3289
3372
|
const textParts = [];
|
|
3290
3373
|
let thinkingContent = "";
|
|
@@ -3302,7 +3385,7 @@ var ClaudeAgent = class extends AbstractAgent {
|
|
|
3302
3385
|
}
|
|
3303
3386
|
const textContent = textParts.join("");
|
|
3304
3387
|
if (!textContent) {
|
|
3305
|
-
throw new
|
|
3388
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
3306
3389
|
}
|
|
3307
3390
|
let tokenUsage;
|
|
3308
3391
|
if (response.usage) {
|
|
@@ -3374,7 +3457,7 @@ function throwForEmptyGeminiResponse(model, response, fallback) {
|
|
|
3374
3457
|
const bits = [`finishReason=${finishReason ?? "none"}`];
|
|
3375
3458
|
if (!response?.candidates?.length) bits.push("no candidates");
|
|
3376
3459
|
if (finishMessage) bits.push(`finishMessage="${finishMessage}"`);
|
|
3377
|
-
throw new
|
|
3460
|
+
throw new ModelInvalidResponseError(model, `${fallback} (${bits.join(", ")})`);
|
|
3378
3461
|
}
|
|
3379
3462
|
var GoogleAgent = class extends AbstractAgent {
|
|
3380
3463
|
client;
|
|
@@ -3570,7 +3653,7 @@ var GoogleAgent = class extends AbstractAgent {
|
|
|
3570
3653
|
if (!response.text) {
|
|
3571
3654
|
throwForEmptyGeminiResponse(this.model, response, this.errorMessages.emptyResponse);
|
|
3572
3655
|
}
|
|
3573
|
-
const parsedData = parseAndValidateLlmJson(response.text, zodSchema, (m) => this.logger(m));
|
|
3656
|
+
const parsedData = parseAndValidateLlmJson(response.text, zodSchema, (m) => this.logger(m), this.model);
|
|
3574
3657
|
if (parsedData) {
|
|
3575
3658
|
this.logReply(parsedData, thinkingContent || void 0, tokenUsage);
|
|
3576
3659
|
}
|
|
@@ -3806,7 +3889,7 @@ ${instruction}`);
|
|
|
3806
3889
|
processReply(response) {
|
|
3807
3890
|
const message = response?.choices?.[0]?.message;
|
|
3808
3891
|
if (!message || !message.content) {
|
|
3809
|
-
throw new
|
|
3892
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
3810
3893
|
}
|
|
3811
3894
|
let reply = message.content;
|
|
3812
3895
|
if (Array.isArray(reply)) {
|
|
@@ -3911,12 +3994,12 @@ ${schemaDescription}`
|
|
|
3911
3994
|
throw new Error(this.errorMessages.apiError(apiError));
|
|
3912
3995
|
}
|
|
3913
3996
|
if (!response || !response.choices || response.choices.length === 0) {
|
|
3914
|
-
throw new
|
|
3997
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
3915
3998
|
}
|
|
3916
3999
|
const choice = response.choices[0];
|
|
3917
4000
|
const content = choice.message?.content;
|
|
3918
4001
|
if (!content) {
|
|
3919
|
-
throw new
|
|
4002
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.invalidFormat);
|
|
3920
4003
|
}
|
|
3921
4004
|
let responseText;
|
|
3922
4005
|
let thinkingContent = "";
|
|
@@ -3929,7 +4012,7 @@ ${schemaDescription}`
|
|
|
3929
4012
|
} else {
|
|
3930
4013
|
responseText = JSON.stringify(content);
|
|
3931
4014
|
}
|
|
3932
|
-
const parsedData = parseAndValidateLlmJson(responseText, zodSchema, (m) => this.logger(m));
|
|
4015
|
+
const parsedData = parseAndValidateLlmJson(responseText, zodSchema, (m) => this.logger(m), this.model);
|
|
3933
4016
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
3934
4017
|
const tokenUsage = this.extractTokenUsage(response);
|
|
3935
4018
|
if (parsedData) {
|
|
@@ -3938,6 +4021,7 @@ ${schemaDescription}`
|
|
|
3938
4021
|
return [parsedData, thinkingContent, tokenUsage];
|
|
3939
4022
|
} catch (error) {
|
|
3940
4023
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4024
|
+
if (error instanceof ModelError) throw error;
|
|
3941
4025
|
throw new Error(this.errorMessages.apiError(error));
|
|
3942
4026
|
}
|
|
3943
4027
|
}
|
|
@@ -3967,12 +4051,13 @@ ${schemaDescription}`
|
|
|
3967
4051
|
}
|
|
3968
4052
|
const [content, thinkingContent, tokenUsage] = this.processReply(response);
|
|
3969
4053
|
if (!content) {
|
|
3970
|
-
throw new
|
|
4054
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
3971
4055
|
}
|
|
3972
4056
|
this.logReply(content, thinkingContent || void 0, tokenUsage);
|
|
3973
4057
|
return [content, thinkingContent, tokenUsage];
|
|
3974
4058
|
} catch (error) {
|
|
3975
4059
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4060
|
+
if (error instanceof ModelError) throw error;
|
|
3976
4061
|
throw new Error(this.errorMessages.apiError(error));
|
|
3977
4062
|
}
|
|
3978
4063
|
}
|
|
@@ -3989,7 +4074,7 @@ var DeepSeekV2Agent = class extends AbstractAgent {
|
|
|
3989
4074
|
};
|
|
3990
4075
|
// Error message templates
|
|
3991
4076
|
errorMessages = {
|
|
3992
|
-
emptyResponse:
|
|
4077
|
+
emptyResponse: (finishReason) => `Empty or undefined response from DeepSeek API (finish_reason: ${finishReason ?? "unknown"})`,
|
|
3993
4078
|
invalidFormat: "Invalid response format from DeepSeek API",
|
|
3994
4079
|
apiError: (error) => `Failed to get response from DeepSeek API: ${error instanceof Error ? error.message : String(error)}`
|
|
3995
4080
|
};
|
|
@@ -4094,14 +4179,14 @@ ${schemaDescription}`
|
|
|
4094
4179
|
}
|
|
4095
4180
|
const rawContent = response.choices[0]?.message?.content;
|
|
4096
4181
|
if (!rawContent) {
|
|
4097
|
-
throw new
|
|
4182
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(response.choices[0]?.finish_reason));
|
|
4098
4183
|
}
|
|
4099
4184
|
const { text: content, thinking: inlineThinking } = stripInlineThinking(rawContent);
|
|
4100
4185
|
thinkingContent = mergeThinking(thinkingContent, inlineThinking);
|
|
4101
4186
|
if (!content) {
|
|
4102
|
-
throw new
|
|
4187
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(response.choices[0]?.finish_reason));
|
|
4103
4188
|
}
|
|
4104
|
-
const parsedData = parseAndValidateLlmJson(content, zodSchema, (m) => this.logger(m));
|
|
4189
|
+
const parsedData = parseAndValidateLlmJson(content, zodSchema, (m) => this.logger(m), this.model);
|
|
4105
4190
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
4106
4191
|
const usageResult = extractUsageAndCalculateCost(this.model, response);
|
|
4107
4192
|
let tokenUsage;
|
|
@@ -4122,6 +4207,7 @@ ${schemaDescription}`
|
|
|
4122
4207
|
return [parsedData, thinkingContent, tokenUsage];
|
|
4123
4208
|
} catch (error) {
|
|
4124
4209
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4210
|
+
if (error instanceof ModelError) throw error;
|
|
4125
4211
|
throw new Error(this.errorMessages.apiError(error));
|
|
4126
4212
|
}
|
|
4127
4213
|
}
|
|
@@ -4157,12 +4243,12 @@ ${schemaDescription}`
|
|
|
4157
4243
|
}
|
|
4158
4244
|
const rawContent = response.choices[0]?.message?.content;
|
|
4159
4245
|
if (!rawContent) {
|
|
4160
|
-
throw new
|
|
4246
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(response.choices[0]?.finish_reason));
|
|
4161
4247
|
}
|
|
4162
4248
|
const { text: content, thinking: inlineThinking } = stripInlineThinking(rawContent);
|
|
4163
4249
|
thinkingContent = mergeThinking(thinkingContent, inlineThinking);
|
|
4164
4250
|
if (!content) {
|
|
4165
|
-
throw new
|
|
4251
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(response.choices[0]?.finish_reason));
|
|
4166
4252
|
}
|
|
4167
4253
|
const usageResult = extractUsageAndCalculateCost(this.model, response);
|
|
4168
4254
|
let tokenUsage;
|
|
@@ -4181,6 +4267,7 @@ ${schemaDescription}`
|
|
|
4181
4267
|
return [content, thinkingContent, tokenUsage];
|
|
4182
4268
|
} catch (error) {
|
|
4183
4269
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4270
|
+
if (error instanceof ModelError) throw error;
|
|
4184
4271
|
throw new Error(this.errorMessages.apiError(error));
|
|
4185
4272
|
}
|
|
4186
4273
|
}
|
|
@@ -4231,10 +4318,10 @@ ${schemaDescription}`;
|
|
|
4231
4318
|
const response = await this.createResponse(input, true);
|
|
4232
4319
|
const { text, reasoningSummary, encryptedReasoning } = this.extractResponseParts(response);
|
|
4233
4320
|
if (!text) {
|
|
4234
|
-
throw new
|
|
4321
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4235
4322
|
}
|
|
4236
4323
|
this.logger(`Grok Agent - Found reasoning summary: ${!!reasoningSummary}, encrypted reasoning: ${!!encryptedReasoning}`);
|
|
4237
|
-
const parsedData = parseAndValidateLlmJson(text, zodSchema, (m) => this.logger(m));
|
|
4324
|
+
const parsedData = parseAndValidateLlmJson(text, zodSchema, (m) => this.logger(m), this.model);
|
|
4238
4325
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
4239
4326
|
const tokenUsage = this.extractTokenUsage(response);
|
|
4240
4327
|
if (parsedData) {
|
|
@@ -4243,6 +4330,7 @@ ${schemaDescription}`;
|
|
|
4243
4330
|
return [parsedData, reasoningSummary, tokenUsage, encryptedReasoning];
|
|
4244
4331
|
} catch (error) {
|
|
4245
4332
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4333
|
+
if (error instanceof ModelError) throw error;
|
|
4246
4334
|
throw new Error(this.errorMessages.apiError(error));
|
|
4247
4335
|
}
|
|
4248
4336
|
}
|
|
@@ -4258,13 +4346,14 @@ ${schemaDescription}`;
|
|
|
4258
4346
|
const response = await this.createResponse(input, false);
|
|
4259
4347
|
const { text, reasoningSummary, encryptedReasoning } = this.extractResponseParts(response);
|
|
4260
4348
|
if (!text) {
|
|
4261
|
-
throw new
|
|
4349
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4262
4350
|
}
|
|
4263
4351
|
const tokenUsage = this.extractTokenUsage(response);
|
|
4264
4352
|
this.logReply(text, reasoningSummary, tokenUsage);
|
|
4265
4353
|
return [text, reasoningSummary, tokenUsage, encryptedReasoning];
|
|
4266
4354
|
} catch (error) {
|
|
4267
4355
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4356
|
+
if (error instanceof ModelError) throw error;
|
|
4268
4357
|
throw new Error(this.errorMessages.apiError(error));
|
|
4269
4358
|
}
|
|
4270
4359
|
}
|
|
@@ -4504,13 +4593,13 @@ ${openAIMessages[0].content}`;
|
|
|
4504
4593
|
}
|
|
4505
4594
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4506
4595
|
if (!rawReply) {
|
|
4507
|
-
throw new
|
|
4596
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4508
4597
|
}
|
|
4509
4598
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4510
4599
|
if (!reply) {
|
|
4511
|
-
throw new
|
|
4600
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4512
4601
|
}
|
|
4513
|
-
const parsedData = parseAndValidateLlmJson(reply, zodSchema, (m) => this.logger(m));
|
|
4602
|
+
const parsedData = parseAndValidateLlmJson(reply, zodSchema, (m) => this.logger(m), this.model);
|
|
4514
4603
|
this.logger(`\u2705 Response validated successfully with Zod schema (JSON mode)`);
|
|
4515
4604
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
4516
4605
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -4541,13 +4630,13 @@ ${schemaDescription}`;
|
|
|
4541
4630
|
}
|
|
4542
4631
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4543
4632
|
if (!rawReply) {
|
|
4544
|
-
throw new
|
|
4633
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4545
4634
|
}
|
|
4546
4635
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4547
4636
|
if (!reply) {
|
|
4548
|
-
throw new
|
|
4637
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4549
4638
|
}
|
|
4550
|
-
const parsedData = parseAndValidateLlmJson(reply, zodSchema, (m) => this.logger(m));
|
|
4639
|
+
const parsedData = parseAndValidateLlmJson(reply, zodSchema, (m) => this.logger(m), this.model);
|
|
4551
4640
|
this.logger(`\u2705 Response validated successfully with Zod schema (prompt mode)`);
|
|
4552
4641
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
4553
4642
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -4558,6 +4647,7 @@ ${schemaDescription}`;
|
|
|
4558
4647
|
}
|
|
4559
4648
|
} catch (error) {
|
|
4560
4649
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4650
|
+
if (error instanceof ModelError) throw error;
|
|
4561
4651
|
throw new Error(this.errorMessages.apiError(error));
|
|
4562
4652
|
}
|
|
4563
4653
|
}
|
|
@@ -4594,11 +4684,11 @@ ${openAIMessages[0].content}`;
|
|
|
4594
4684
|
}
|
|
4595
4685
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4596
4686
|
if (!rawReply) {
|
|
4597
|
-
throw new
|
|
4687
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4598
4688
|
}
|
|
4599
4689
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4600
4690
|
if (!reply) {
|
|
4601
|
-
throw new
|
|
4691
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4602
4692
|
}
|
|
4603
4693
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
4604
4694
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -4606,6 +4696,7 @@ ${openAIMessages[0].content}`;
|
|
|
4606
4696
|
return [reply, thinkingContent, tokenUsage];
|
|
4607
4697
|
} catch (error) {
|
|
4608
4698
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4699
|
+
if (error instanceof ModelError) throw error;
|
|
4609
4700
|
throw new Error(this.errorMessages.apiError(error));
|
|
4610
4701
|
}
|
|
4611
4702
|
}
|
|
@@ -4682,7 +4773,7 @@ var GlmAgent = class extends AbstractAgent {
|
|
|
4682
4773
|
* Returns the validated value or throws.
|
|
4683
4774
|
*/
|
|
4684
4775
|
parseAndValidate(rawReply, zodSchema) {
|
|
4685
|
-
return parseAndValidateLlmJson(rawReply, zodSchema, (m) => this.logger(m));
|
|
4776
|
+
return parseAndValidateLlmJson(rawReply, zodSchema, (m) => this.logger(m), this.model);
|
|
4686
4777
|
}
|
|
4687
4778
|
async doAskWithZodSchema(zodSchema, messages) {
|
|
4688
4779
|
try {
|
|
@@ -4722,11 +4813,11 @@ ${schemaDescription}`;
|
|
|
4722
4813
|
}
|
|
4723
4814
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4724
4815
|
if (!rawReply) {
|
|
4725
|
-
throw new
|
|
4816
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(completion.choices[0]?.finish_reason));
|
|
4726
4817
|
}
|
|
4727
4818
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4728
4819
|
if (!reply) {
|
|
4729
|
-
throw new
|
|
4820
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(completion.choices[0]?.finish_reason));
|
|
4730
4821
|
}
|
|
4731
4822
|
const validated = this.parseAndValidate(reply, zodSchema);
|
|
4732
4823
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
@@ -4738,6 +4829,7 @@ ${schemaDescription}`;
|
|
|
4738
4829
|
return [validated, thinkingContent, tokenUsage];
|
|
4739
4830
|
} catch (error) {
|
|
4740
4831
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4832
|
+
if (error instanceof ModelError) throw error;
|
|
4741
4833
|
throw new Error(this.errorMessages.apiError(error));
|
|
4742
4834
|
}
|
|
4743
4835
|
}
|
|
@@ -4774,11 +4866,11 @@ ${openAIMessages[0].content}`;
|
|
|
4774
4866
|
}
|
|
4775
4867
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4776
4868
|
if (!rawReply) {
|
|
4777
|
-
throw new
|
|
4869
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(completion.choices[0]?.finish_reason));
|
|
4778
4870
|
}
|
|
4779
4871
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4780
4872
|
if (!reply) {
|
|
4781
|
-
throw new
|
|
4873
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse(completion.choices[0]?.finish_reason));
|
|
4782
4874
|
}
|
|
4783
4875
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
4784
4876
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -4786,6 +4878,7 @@ ${openAIMessages[0].content}`;
|
|
|
4786
4878
|
return [reply, thinkingContent, tokenUsage];
|
|
4787
4879
|
} catch (error) {
|
|
4788
4880
|
this.logger(this.logTemplates.error(this.name, error));
|
|
4881
|
+
if (error instanceof ModelError) throw error;
|
|
4789
4882
|
throw new Error(this.errorMessages.apiError(error));
|
|
4790
4883
|
}
|
|
4791
4884
|
}
|
|
@@ -4888,13 +4981,13 @@ ${schemaDescription}`;
|
|
|
4888
4981
|
}
|
|
4889
4982
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4890
4983
|
if (!rawReply) {
|
|
4891
|
-
throw new
|
|
4984
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4892
4985
|
}
|
|
4893
4986
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4894
4987
|
if (!reply) {
|
|
4895
|
-
throw new
|
|
4988
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4896
4989
|
}
|
|
4897
|
-
const validated = parseAndValidateLlmJson(reply, zodSchema, (m) => this.logger(m));
|
|
4990
|
+
const validated = parseAndValidateLlmJson(reply, zodSchema, (m) => this.logger(m), this.model);
|
|
4898
4991
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
4899
4992
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
4900
4993
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -4904,6 +4997,7 @@ ${schemaDescription}`;
|
|
|
4904
4997
|
return [validated, thinkingContent, tokenUsage];
|
|
4905
4998
|
} catch (error) {
|
|
4906
4999
|
this.logger(this.logTemplates.error(this.name, error));
|
|
5000
|
+
if (error instanceof ModelError) throw error;
|
|
4907
5001
|
throw new Error(this.errorMessages.apiError(error));
|
|
4908
5002
|
}
|
|
4909
5003
|
}
|
|
@@ -4931,11 +5025,11 @@ ${schemaDescription}`;
|
|
|
4931
5025
|
}
|
|
4932
5026
|
const rawReply = completion.choices[0]?.message?.content;
|
|
4933
5027
|
if (!rawReply) {
|
|
4934
|
-
throw new
|
|
5028
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4935
5029
|
}
|
|
4936
5030
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
4937
5031
|
if (!reply) {
|
|
4938
|
-
throw new
|
|
5032
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
4939
5033
|
}
|
|
4940
5034
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
4941
5035
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -4943,6 +5037,7 @@ ${schemaDescription}`;
|
|
|
4943
5037
|
return [reply, thinkingContent, tokenUsage];
|
|
4944
5038
|
} catch (error) {
|
|
4945
5039
|
this.logger(this.logTemplates.error(this.name, error));
|
|
5040
|
+
if (error instanceof ModelError) throw error;
|
|
4946
5041
|
throw new Error(this.errorMessages.apiError(error));
|
|
4947
5042
|
}
|
|
4948
5043
|
}
|
|
@@ -5100,7 +5195,7 @@ var QwenAgent = class extends AbstractAgent {
|
|
|
5100
5195
|
* Returns the validated value or throws.
|
|
5101
5196
|
*/
|
|
5102
5197
|
parseAndValidate(rawReply, zodSchema) {
|
|
5103
|
-
return parseAndValidateLlmJson(rawReply, zodSchema, (m) => this.logger(m));
|
|
5198
|
+
return parseAndValidateLlmJson(rawReply, zodSchema, (m) => this.logger(m), this.model);
|
|
5104
5199
|
}
|
|
5105
5200
|
async doAskWithZodSchema(zodSchema, messages) {
|
|
5106
5201
|
try {
|
|
@@ -5144,11 +5239,11 @@ ${schemaDescription}`;
|
|
|
5144
5239
|
}
|
|
5145
5240
|
const rawReply = completion.choices[0]?.message?.content;
|
|
5146
5241
|
if (!rawReply) {
|
|
5147
|
-
throw new
|
|
5242
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5148
5243
|
}
|
|
5149
5244
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
5150
5245
|
if (!reply) {
|
|
5151
|
-
throw new
|
|
5246
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5152
5247
|
}
|
|
5153
5248
|
const validated = this.parseAndValidate(reply, zodSchema);
|
|
5154
5249
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
@@ -5200,11 +5295,11 @@ ${openAIMessages[0].content}`;
|
|
|
5200
5295
|
}
|
|
5201
5296
|
const rawReply = completion.choices[0]?.message?.content;
|
|
5202
5297
|
if (!rawReply) {
|
|
5203
|
-
throw new
|
|
5298
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5204
5299
|
}
|
|
5205
5300
|
const { text: reply, thinking: inlineThinking } = stripInlineThinking(rawReply);
|
|
5206
5301
|
if (!reply) {
|
|
5207
|
-
throw new
|
|
5302
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5208
5303
|
}
|
|
5209
5304
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
5210
5305
|
const thinkingContent = [reasoningContent, inlineThinking].filter(Boolean).join("\n");
|
|
@@ -5229,10 +5324,10 @@ var MiniMaxAgent = class extends AbstractAgent {
|
|
|
5229
5324
|
get defaultParams() {
|
|
5230
5325
|
return {
|
|
5231
5326
|
model: this.model,
|
|
5232
|
-
temperature: this.temperature,
|
|
5327
|
+
...this.temperature !== void 0 ? { temperature: this.temperature } : {},
|
|
5233
5328
|
stream: false,
|
|
5234
|
-
// MiniMax deprecates max_tokens in favor of max_completion_tokens (
|
|
5235
|
-
// far above anything a turn needs).
|
|
5329
|
+
// MiniMax deprecates max_tokens in favor of max_completion_tokens (the M-series max is
|
|
5330
|
+
// 512K, far above anything a turn needs).
|
|
5236
5331
|
max_completion_tokens: this.maxOutputTokens
|
|
5237
5332
|
};
|
|
5238
5333
|
}
|
|
@@ -5254,7 +5349,9 @@ var MiniMaxAgent = class extends AbstractAgent {
|
|
|
5254
5349
|
thinkingParams() {
|
|
5255
5350
|
return {
|
|
5256
5351
|
thinking: { type: this.enableThinking ? "adaptive" : "disabled" },
|
|
5257
|
-
reasoning_split: true
|
|
5352
|
+
reasoning_split: true,
|
|
5353
|
+
// Provider default is max; the catalog pins M3.1 Flash to medium. Older models ignore it.
|
|
5354
|
+
...this.enableThinking && this.reasoningEffort ? { reasoning_effort: toMiniMaxEffort(this.reasoningEffort) } : {}
|
|
5258
5355
|
};
|
|
5259
5356
|
}
|
|
5260
5357
|
convertToOpenAIMessages(messages) {
|
|
@@ -5296,7 +5393,7 @@ var MiniMaxAgent = class extends AbstractAgent {
|
|
|
5296
5393
|
* Returns the validated value or throws.
|
|
5297
5394
|
*/
|
|
5298
5395
|
parseAndValidate(rawReply, zodSchema) {
|
|
5299
|
-
return parseAndValidateLlmJson(rawReply, zodSchema, (m) => this.logger(m));
|
|
5396
|
+
return parseAndValidateLlmJson(rawReply, zodSchema, (m) => this.logger(m), this.model);
|
|
5300
5397
|
}
|
|
5301
5398
|
async doAskWithZodSchema(zodSchema, messages) {
|
|
5302
5399
|
try {
|
|
@@ -5336,7 +5433,7 @@ ${schemaDescription}`;
|
|
|
5336
5433
|
}
|
|
5337
5434
|
const reply = completion.choices[0]?.message?.content;
|
|
5338
5435
|
if (!reply) {
|
|
5339
|
-
throw new
|
|
5436
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5340
5437
|
}
|
|
5341
5438
|
const { text: cleanReply, thinking: inlineThinking } = stripInlineThinking(reply);
|
|
5342
5439
|
const validated = this.parseAndValidate(cleanReply, zodSchema);
|
|
@@ -5349,6 +5446,7 @@ ${schemaDescription}`;
|
|
|
5349
5446
|
return [validated, thinkingContent, tokenUsage];
|
|
5350
5447
|
} catch (error) {
|
|
5351
5448
|
this.logger(this.logTemplates.error(this.name, error));
|
|
5449
|
+
if (error instanceof ModelError) throw error;
|
|
5352
5450
|
throw new Error(this.errorMessages.apiError(error));
|
|
5353
5451
|
}
|
|
5354
5452
|
}
|
|
@@ -5386,11 +5484,11 @@ ${openAIMessages[0].content}`;
|
|
|
5386
5484
|
}
|
|
5387
5485
|
const reply = completion.choices[0]?.message?.content;
|
|
5388
5486
|
if (!reply) {
|
|
5389
|
-
throw new
|
|
5487
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5390
5488
|
}
|
|
5391
5489
|
const { text: cleanReply, thinking: inlineThinking } = stripInlineThinking(reply);
|
|
5392
5490
|
if (!cleanReply) {
|
|
5393
|
-
throw new
|
|
5491
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5394
5492
|
}
|
|
5395
5493
|
const { thinkingContent: reasoningContent, tokenUsage } = this.extractThinkingAndUsage(completion);
|
|
5396
5494
|
const thinkingContent = mergeThinking(reasoningContent, inlineThinking);
|
|
@@ -5398,6 +5496,7 @@ ${openAIMessages[0].content}`;
|
|
|
5398
5496
|
return [cleanReply, thinkingContent, tokenUsage];
|
|
5399
5497
|
} catch (error) {
|
|
5400
5498
|
this.logger(this.logTemplates.error(this.name, error));
|
|
5499
|
+
if (error instanceof ModelError) throw error;
|
|
5401
5500
|
throw new Error(this.errorMessages.apiError(error));
|
|
5402
5501
|
}
|
|
5403
5502
|
}
|
|
@@ -5448,10 +5547,10 @@ ${schemaDescription}`;
|
|
|
5448
5547
|
const response = await this.createResponse(input, zodSchema);
|
|
5449
5548
|
const { text, reasoningSummary, encryptedReasoning } = this.extractResponseParts(response);
|
|
5450
5549
|
if (!text) {
|
|
5451
|
-
throw new
|
|
5550
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5452
5551
|
}
|
|
5453
5552
|
this.logger(`Meta Agent - Found reasoning summary: ${!!reasoningSummary}, encrypted reasoning: ${!!encryptedReasoning}`);
|
|
5454
|
-
const parsedData = parseAndValidateLlmJson(text, zodSchema, (m) => this.logger(m));
|
|
5553
|
+
const parsedData = parseAndValidateLlmJson(text, zodSchema, (m) => this.logger(m), this.model);
|
|
5455
5554
|
this.logger(`\u2705 Response validated successfully with Zod schema`);
|
|
5456
5555
|
const tokenUsage = this.extractTokenUsage(response);
|
|
5457
5556
|
if (parsedData) {
|
|
@@ -5460,6 +5559,7 @@ ${schemaDescription}`;
|
|
|
5460
5559
|
return [parsedData, reasoningSummary, tokenUsage, encryptedReasoning];
|
|
5461
5560
|
} catch (error) {
|
|
5462
5561
|
this.logger(this.logTemplates.error(this.name, error));
|
|
5562
|
+
if (error instanceof ModelError) throw error;
|
|
5463
5563
|
throw new Error(this.errorMessages.apiError(error));
|
|
5464
5564
|
}
|
|
5465
5565
|
}
|
|
@@ -5475,13 +5575,14 @@ ${schemaDescription}`;
|
|
|
5475
5575
|
const response = await this.createResponse(input);
|
|
5476
5576
|
const { text, reasoningSummary, encryptedReasoning } = this.extractResponseParts(response);
|
|
5477
5577
|
if (!text) {
|
|
5478
|
-
throw new
|
|
5578
|
+
throw new ModelInvalidResponseError(this.model, this.errorMessages.emptyResponse);
|
|
5479
5579
|
}
|
|
5480
5580
|
const tokenUsage = this.extractTokenUsage(response);
|
|
5481
5581
|
this.logReply(text, reasoningSummary, tokenUsage);
|
|
5482
5582
|
return [text, reasoningSummary, tokenUsage, encryptedReasoning];
|
|
5483
5583
|
} catch (error) {
|
|
5484
5584
|
this.logger(this.logTemplates.error(this.name, error));
|
|
5585
|
+
if (error instanceof ModelError) throw error;
|
|
5485
5586
|
throw new Error(this.errorMessages.apiError(error));
|
|
5486
5587
|
}
|
|
5487
5588
|
}
|
|
@@ -5667,7 +5768,7 @@ var AgentFactory = class {
|
|
|
5667
5768
|
case LLM_CONSTANTS.QWEN_MAX:
|
|
5668
5769
|
case LLM_CONSTANTS.QWEN_FLASH:
|
|
5669
5770
|
return new QwenAgent(name, instruction, model.modelApiName, key, model.temperature, shouldEnableThinking);
|
|
5670
|
-
// MiniMax M3 — adaptive thinking
|
|
5771
|
+
// MiniMax M3.1 Flash — always-on adaptive thinking, effort from the catalog
|
|
5671
5772
|
case LLM_CONSTANTS.MINIMAX:
|
|
5672
5773
|
return new MiniMaxAgent(name, instruction, model.modelApiName, key, model.temperature, shouldEnableThinking);
|
|
5673
5774
|
// Meta Muse Spark — Responses API with encrypted reasoning replay, effort from the catalog
|
|
@@ -5719,6 +5820,7 @@ var AgentFactory = class {
|
|
|
5719
5820
|
LLM_CONSTANTS,
|
|
5720
5821
|
MESSAGE_ROLE,
|
|
5721
5822
|
META_REASONING_EFFORTS,
|
|
5823
|
+
MINIMAX_REASONING_EFFORTS,
|
|
5722
5824
|
MISTRAL_REASONING_EFFORTS,
|
|
5723
5825
|
MODEL_PRICING,
|
|
5724
5826
|
MetaAgent,
|
|
@@ -5815,6 +5917,7 @@ var AgentFactory = class {
|
|
|
5815
5917
|
safeValidateResponse,
|
|
5816
5918
|
setBeforeAskHook,
|
|
5817
5919
|
setLlmLogger,
|
|
5920
|
+
setRetryHook,
|
|
5818
5921
|
stableHashHex,
|
|
5819
5922
|
stripInlineThinking,
|
|
5820
5923
|
supportsNativeJsonSchema,
|
|
@@ -5825,6 +5928,7 @@ var AgentFactory = class {
|
|
|
5825
5928
|
toGlmEffort,
|
|
5826
5929
|
toKimiEffort,
|
|
5827
5930
|
toMetaEffort,
|
|
5931
|
+
toMiniMaxEffort,
|
|
5828
5932
|
toMistralEffort,
|
|
5829
5933
|
toOpenAIEffort,
|
|
5830
5934
|
toXaiEffort,
|