vidspotai-shared 1.0.142 → 1.0.144
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/services/agent/chatAgent.d.ts +3 -0
- package/lib/services/agent/chatAgent.d.ts.map +1 -1
- package/lib/services/agent/chatAgent.js +13 -0
- package/lib/services/agent/llmCallerOpenai.d.ts +5 -0
- package/lib/services/agent/llmCallerOpenai.d.ts.map +1 -1
- package/lib/services/agent/llmCallerOpenai.js +58 -10
- package/lib/services/agent/llmCallerRouting.d.ts +9 -0
- package/lib/services/agent/llmCallerRouting.d.ts.map +1 -1
- package/lib/services/agent/llmCallerRouting.js +66 -6
- package/lib/services/agent/llmRetry.d.ts +13 -0
- package/lib/services/agent/llmRetry.d.ts.map +1 -1
- package/lib/services/agent/llmRetry.js +29 -0
- package/lib/services/agent/planner/Planner.d.ts.map +1 -1
- package/lib/services/agent/planner/Planner.js +4 -1
- package/lib/services/providerHealth.service.d.ts +5 -0
- package/lib/services/providerHealth.service.d.ts.map +1 -1
- package/lib/services/providerHealth.service.js +30 -3
- package/package.json +1 -1
|
@@ -42,6 +42,9 @@ export type ChatTurnEvent = {
|
|
|
42
42
|
} | {
|
|
43
43
|
kind: "error";
|
|
44
44
|
message: string;
|
|
45
|
+
/** Set when the LLM provider ACCOUNT is unusable (spend cap / no credit):
|
|
46
|
+
* an operator problem, not a per-turn bug — hosts warn instead of paging. */
|
|
47
|
+
code?: "PROVIDER_ACCOUNT_UNAVAILABLE";
|
|
45
48
|
};
|
|
46
49
|
export interface ChatTurnTool {
|
|
47
50
|
name: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chatAgent.d.ts","sourceRoot":"","sources":["../../../src/services/agent/chatAgent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"chatAgent.d.ts","sourceRoot":"","sources":["../../../src/services/agent/chatAgent.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,CAAC,EAAE,MA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CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;CACxD;AAaD,wBAAgB,UAAU,CAAC,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,YAAY,CAQ/E;AAED,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,qEAAqE;IACrE,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC7D,4CAA4C;IAC5C,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,YAAY,EAAE,CAAC;IACtB,kEAAkE;IAClE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,kFAAkF;IAClF,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAqGD;;;;;;GAMG;AACH,wBAAuB,WAAW,CAChC,GAAG,EAAE,eAAe,GACnB,cAAc,CAAC,aAAa,EAAE,IAAI,EAAE,IAAI,CAAC,CAyL3C"}
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.defineTool = defineTool;
|
|
7
7
|
exports.runChatTurn = runChatTurn;
|
|
8
8
|
const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
|
|
9
|
+
const llmRetry_1 = require("./llmRetry");
|
|
9
10
|
const zod_1 = require("zod");
|
|
10
11
|
const logger_1 = require("../../utils/logger");
|
|
11
12
|
/** Strip JSON-Schema keys Anthropic's tool `input_schema` doesn't want. */
|
|
@@ -187,6 +188,18 @@ async function* runChatTurn(req) {
|
|
|
187
188
|
yield { kind: "error", message: "aborted" };
|
|
188
189
|
return;
|
|
189
190
|
}
|
|
191
|
+
// The chat loop is Anthropic-only (tool use), so there's no cross-
|
|
192
|
+
// provider fallback here. When the account itself is capped (prod
|
|
193
|
+
// 2026-11-20 workspace usage limit), tell the user plainly instead of
|
|
194
|
+
// surfacing the raw API JSON, and tag it so the host doesn't page per turn.
|
|
195
|
+
if ((0, llmRetry_1.isProviderAccountUnavailableError)(err)) {
|
|
196
|
+
yield {
|
|
197
|
+
kind: "error",
|
|
198
|
+
code: "PROVIDER_ACCOUNT_UNAVAILABLE",
|
|
199
|
+
message: "The chat assistant is temporarily unavailable. You can still create and generate videos — please try chat again later.",
|
|
200
|
+
};
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
190
203
|
yield {
|
|
191
204
|
kind: "error",
|
|
192
205
|
message: `chat turn failed: ${err.message}`,
|
|
@@ -53,4 +53,9 @@ export declare class OpenAiLlmCaller implements LlmCaller {
|
|
|
53
53
|
*/
|
|
54
54
|
private withRetry;
|
|
55
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* OpenAI reasoning models (gpt-5 family incl. gpt-5.x, o1/o3/o4…) reject any
|
|
58
|
+
* temperature other than the default 1. Exported for tests.
|
|
59
|
+
*/
|
|
60
|
+
export declare function supportsCustomTemperature(modelId: string): boolean;
|
|
56
61
|
//# sourceMappingURL=llmCallerOpenai.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llmCallerOpenai.d.ts","sourceRoot":"","sources":["../../../src/services/agent/llmCallerOpenai.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"llmCallerOpenai.d.ts","sourceRoot":"","sources":["../../../src/services/agent/llmCallerOpenai.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAEL,WAAW,EACX,YAAY,EACZ,SAAS,EACT,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,aAAa,CAAC;AAKrB;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,uEAAuE;IACvE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4EAA4E;IAC5E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,2DAA2D;IAC3D,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAID,qBAAa,eAAgB,YAAW,SAAS;IAC/C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAExB,GAAG,EAAE,kBAAkB;IAU7B,IAAI,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IAoB7C,UAAU,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,EACrC,GAAG,EAAE,iBAAiB,CAAC,CAAC,CAAC,GACxB,OAAO,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC;IA8BjC,gBAAgB,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,EACrC,GAAG,EAAE,iBAAiB,CAAC,CAAC,CAAC,GACxB;QACD,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;QAC9B,MAAM,EAAE,OAAO,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,CAAC;KACxC;YAgBc,cAAc;IAqE7B,+EAA+E;IAC/E,OAAO,CAAC,UAAU;IA6BlB,OAAO,CAAC,aAAa;IAkBrB,iFAAiF;IACjF,OAAO,CAAC,eAAe;IA0BvB,OAAO,CAAC,cAAc;IAStB,OAAO,CAAC,KAAK;IAUb;;;;OAIG;IACH,OAAO,CAAC,SAAS;CAyBlB;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAElE"}
|
|
@@ -4,6 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.OpenAiLlmCaller = void 0;
|
|
7
|
+
exports.supportsCustomTemperature = supportsCustomTemperature;
|
|
8
|
+
const logger_1 = require("../../utils/logger");
|
|
7
9
|
const openai_1 = __importDefault(require("openai"));
|
|
8
10
|
const zod_1 = require("zod");
|
|
9
11
|
const llmModelRegistry_1 = require("./llmModelRegistry");
|
|
@@ -36,7 +38,7 @@ class OpenAiLlmCaller {
|
|
|
36
38
|
}
|
|
37
39
|
async structured(req) {
|
|
38
40
|
const jsonSchema = zod_1.z.toJSONSchema(req.schema, { target: "draft-7" });
|
|
39
|
-
const completion = await this.withRetry(() => this.client.chat.completions.create({
|
|
41
|
+
const completion = await withStrictSchemaFallback(req.schemaName, (strict) => this.withRetry(() => this.client.chat.completions.create({
|
|
40
42
|
...this.baseParams(req.model, req.messages, {
|
|
41
43
|
temperature: req.temperature,
|
|
42
44
|
maxTokens: req.maxTokens,
|
|
@@ -46,10 +48,10 @@ class OpenAiLlmCaller {
|
|
|
46
48
|
json_schema: {
|
|
47
49
|
name: req.schemaName,
|
|
48
50
|
schema: jsonSchema,
|
|
49
|
-
strict
|
|
51
|
+
strict,
|
|
50
52
|
},
|
|
51
53
|
},
|
|
52
|
-
}));
|
|
54
|
+
})));
|
|
53
55
|
const txt = completion.choices?.[0]?.message?.content ?? "";
|
|
54
56
|
return {
|
|
55
57
|
...this.parseStructured(req, txt),
|
|
@@ -83,7 +85,7 @@ class OpenAiLlmCaller {
|
|
|
83
85
|
// Connect with transient retry; once bytes flow a mid-stream drop
|
|
84
86
|
// propagates and the planner's own retry loop handles it (mirrors the
|
|
85
87
|
// gateway's streaming contract).
|
|
86
|
-
const stream = await this.withRetry(() => this.client.chat.completions.create({
|
|
88
|
+
const stream = await withStrictSchemaFallback(req.schemaName, (strict) => this.withRetry(() => this.client.chat.completions.create({
|
|
87
89
|
...this.baseParams(req.model, req.messages, {
|
|
88
90
|
temperature: req.temperature,
|
|
89
91
|
maxTokens: req.maxTokens,
|
|
@@ -93,12 +95,12 @@ class OpenAiLlmCaller {
|
|
|
93
95
|
json_schema: {
|
|
94
96
|
name: req.schemaName,
|
|
95
97
|
schema: jsonSchema,
|
|
96
|
-
strict
|
|
98
|
+
strict,
|
|
97
99
|
},
|
|
98
100
|
},
|
|
99
101
|
stream: true,
|
|
100
102
|
stream_options: { include_usage: true },
|
|
101
|
-
}));
|
|
103
|
+
})));
|
|
102
104
|
for await (const chunk of stream) {
|
|
103
105
|
if (chunk.id)
|
|
104
106
|
requestId = chunk.id;
|
|
@@ -140,12 +142,20 @@ class OpenAiLlmCaller {
|
|
|
140
142
|
// ---------- helpers ----------
|
|
141
143
|
/** Shared non-streaming params: model id, encoded messages, sampling knobs. */
|
|
142
144
|
baseParams(model, messages, opts) {
|
|
145
|
+
const modelId = this.resolveModelId(model);
|
|
146
|
+
// REMOVED FOR v1 — kept for reference: "most agent calls leave it unset"
|
|
147
|
+
// was false — the Planner always sets 0.4, so when Anthropic hit its spend
|
|
148
|
+
// cap and planning fell back to gpt-5.5, every plan 400'd ("'temperature'
|
|
149
|
+
// does not support 0.4 with this model") in prod 2026-11-19/20.
|
|
150
|
+
// ...(opts.temperature !== undefined ? { temperature: opts.temperature } : {}),
|
|
151
|
+
const sendTemperature = opts.temperature !== undefined &&
|
|
152
|
+
(supportsCustomTemperature(modelId) || opts.temperature === 1);
|
|
143
153
|
return {
|
|
144
|
-
model:
|
|
154
|
+
model: modelId,
|
|
145
155
|
messages: messages.map((m) => this.encodeMessage(m)),
|
|
146
|
-
//
|
|
147
|
-
//
|
|
148
|
-
...(
|
|
156
|
+
// Reasoning models (gpt-5 family, o-series) accept only the default
|
|
157
|
+
// temperature; drop it for them rather than 400 the whole call.
|
|
158
|
+
...(sendTemperature ? { temperature: opts.temperature } : {}),
|
|
149
159
|
// gpt-5 family requires max_completion_tokens (max_tokens is rejected on
|
|
150
160
|
// reasoning models); it's also accepted by non-reasoning models.
|
|
151
161
|
...(opts.maxTokens !== undefined
|
|
@@ -228,3 +238,41 @@ class OpenAiLlmCaller {
|
|
|
228
238
|
}
|
|
229
239
|
}
|
|
230
240
|
exports.OpenAiLlmCaller = OpenAiLlmCaller;
|
|
241
|
+
/**
|
|
242
|
+
* OpenAI reasoning models (gpt-5 family incl. gpt-5.x, o1/o3/o4…) reject any
|
|
243
|
+
* temperature other than the default 1. Exported for tests.
|
|
244
|
+
*/
|
|
245
|
+
function supportsCustomTemperature(modelId) {
|
|
246
|
+
return !/^(gpt-5|o\d)/i.test(modelId);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Schemas OpenAI rejected for strict mode. Strict `json_schema` demands every
|
|
250
|
+
* property be `required`; our Zod schemas use `.optional()` freely (VideoPlan's
|
|
251
|
+
* captionStyle etc.), so strict 400s before generating ("Invalid schema for
|
|
252
|
+
* response_format"). That stayed hidden while OpenAI was only a fallback, and
|
|
253
|
+
* surfaced when the Anthropic spend cap pushed all planning onto gpt-5.5
|
|
254
|
+
* (prod 2026-11-20). Non-strict json_schema still steers the model; the Zod
|
|
255
|
+
* parse + the planner's correction loop remain the real validators.
|
|
256
|
+
*/
|
|
257
|
+
const nonStrictSchemas = new Set();
|
|
258
|
+
function isStrictSchemaRejection(err) {
|
|
259
|
+
const e = err;
|
|
260
|
+
return !!e && e.status === 400 && /Invalid schema for response_format/i.test(String(e.message ?? ""));
|
|
261
|
+
}
|
|
262
|
+
async function withStrictSchemaFallback(schemaName, run) {
|
|
263
|
+
if (nonStrictSchemas.has(schemaName))
|
|
264
|
+
return run(false);
|
|
265
|
+
try {
|
|
266
|
+
return await run(true);
|
|
267
|
+
}
|
|
268
|
+
catch (err) {
|
|
269
|
+
if (!isStrictSchemaRejection(err))
|
|
270
|
+
throw err;
|
|
271
|
+
nonStrictSchemas.add(schemaName);
|
|
272
|
+
logger_1.logger.warn("OpenAI rejected schema for strict mode — using non-strict json_schema", {
|
|
273
|
+
schemaName,
|
|
274
|
+
err: String(err.message ?? err).slice(0, 300),
|
|
275
|
+
});
|
|
276
|
+
return run(false);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { ChatRequest, ChatResponse, LlmCaller, StructuredRequest, StructuredResponse, StructuredWithToolsRequest } from "./llmCaller";
|
|
3
|
+
/**
|
|
4
|
+
* Per-process cooldown for a provider whose ACCOUNT is unusable (spend cap /
|
|
5
|
+
* no credit / revoked key). While cooling, calls that carry a fallbackModel go
|
|
6
|
+
* straight to it instead of paying a failed round-trip (and a Slack page) on
|
|
7
|
+
* the dead provider first. Short so a top-up / limit raise recovers on its own.
|
|
8
|
+
*/
|
|
9
|
+
export declare const PROVIDER_ACCOUNT_COOLDOWN_MS: number;
|
|
10
|
+
/** Test hook. */
|
|
11
|
+
export declare function resetProviderAccountCooldowns(): void;
|
|
3
12
|
/**
|
|
4
13
|
* Provider-dispatching LlmCaller — the native replacement for GatewayLlmCaller
|
|
5
14
|
* as the wired singleton (LLM_NATIVE_MIGRATION_PLAN Phase 5).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llmCallerRouting.d.ts","sourceRoot":"","sources":["../../../src/services/agent/llmCallerRouting.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EACL,WAAW,EACX,YAAY,EACZ,SAAS,EACT,iBAAiB,EACjB,kBAAkB,EAClB,0BAA0B,EAC3B,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"llmCallerRouting.d.ts","sourceRoot":"","sources":["../../../src/services/agent/llmCallerRouting.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EACL,WAAW,EACX,YAAY,EACZ,SAAS,EACT,iBAAiB,EACjB,kBAAkB,EAClB,0BAA0B,EAC3B,MAAM,aAAa,CAAC;AAQrB;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,QAAc,CAAC;AAsBxD,iBAAiB;AACjB,wBAAgB,6BAA6B,IAAI,IAAI,CAEpD;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,WAAW,eAAe;IAC9B,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,MAAM,CAAC,EAAE,SAAS,CAAC;IACnB,MAAM,CAAC,EAAE,SAAS,CAAC;CACpB;AAED,qBAAa,wBAAyB,YAAW,SAAS;IAC5C,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,eAAe;IAErD,OAAO,CAAC,IAAI;IAYN,IAAI,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IAyB7C,UAAU,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,EACrC,GAAG,EAAE,iBAAiB,CAAC,CAAC,CAAC,GACxB,OAAO,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC;IA2BjC;;;;;;OAMG;IACH,gBAAgB,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,EACrC,GAAG,EAAE,iBAAiB,CAAC,CAAC,CAAC,GACxB;QACD,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;QAC9B,MAAM,EAAE,OAAO,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,CAAC;KACxC;IAYD;;;;;;OAMG;IACH,OAAO,CAAC,iBAAiB;IAgCzB;;;;;OAKG;IACG,mBAAmB,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,EAC9C,GAAG,EAAE,0BAA0B,CAAC,CAAC,CAAC,GACjC,OAAO,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC;CAuBlC"}
|
|
@@ -1,8 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ProviderRoutingLlmCaller = void 0;
|
|
3
|
+
exports.ProviderRoutingLlmCaller = exports.PROVIDER_ACCOUNT_COOLDOWN_MS = void 0;
|
|
4
|
+
exports.resetProviderAccountCooldowns = resetProviderAccountCooldowns;
|
|
4
5
|
const llmModelRegistry_1 = require("./llmModelRegistry");
|
|
6
|
+
// REMOVED FOR v1 — kept for reference: fallback was transient-only.
|
|
7
|
+
// import { isTransientLlmError } from "./llmRetry";
|
|
5
8
|
const llmRetry_1 = require("./llmRetry");
|
|
9
|
+
const logger_1 = require("../../utils/logger");
|
|
10
|
+
/**
|
|
11
|
+
* Per-process cooldown for a provider whose ACCOUNT is unusable (spend cap /
|
|
12
|
+
* no credit / revoked key). While cooling, calls that carry a fallbackModel go
|
|
13
|
+
* straight to it instead of paying a failed round-trip (and a Slack page) on
|
|
14
|
+
* the dead provider first. Short so a top-up / limit raise recovers on its own.
|
|
15
|
+
*/
|
|
16
|
+
exports.PROVIDER_ACCOUNT_COOLDOWN_MS = 10 * 60000;
|
|
17
|
+
const accountCooldownUntil = new Map();
|
|
18
|
+
function isCoolingDown(provider) {
|
|
19
|
+
const until = accountCooldownUntil.get(provider);
|
|
20
|
+
return until !== undefined && until > Date.now();
|
|
21
|
+
}
|
|
22
|
+
function noteAccountFailure(model, err) {
|
|
23
|
+
if (!(0, llmRetry_1.isProviderAccountUnavailableError)(err))
|
|
24
|
+
return;
|
|
25
|
+
const provider = (0, llmModelRegistry_1.providerFor)(model);
|
|
26
|
+
if (!isCoolingDown(provider)) {
|
|
27
|
+
logger_1.logger.warn("LLM provider account unavailable — routing to fallback models", {
|
|
28
|
+
provider,
|
|
29
|
+
modelId: model.modelId,
|
|
30
|
+
cooldownMs: exports.PROVIDER_ACCOUNT_COOLDOWN_MS,
|
|
31
|
+
err: err instanceof Error ? err.message.slice(0, 300) : String(err).slice(0, 300),
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
accountCooldownUntil.set(provider, Date.now() + exports.PROVIDER_ACCOUNT_COOLDOWN_MS);
|
|
35
|
+
}
|
|
36
|
+
/** Test hook. */
|
|
37
|
+
function resetProviderAccountCooldowns() {
|
|
38
|
+
accountCooldownUntil.clear();
|
|
39
|
+
}
|
|
6
40
|
class ProviderRoutingLlmCaller {
|
|
7
41
|
constructor(callers) {
|
|
8
42
|
this.callers = callers;
|
|
@@ -24,12 +58,17 @@ class ProviderRoutingLlmCaller {
|
|
|
24
58
|
// should not attempt its own (it can't reach other providers anyway).
|
|
25
59
|
return this.pick(model).chat({ ...req, model, fallbackModel: undefined });
|
|
26
60
|
};
|
|
61
|
+
if (req.fallbackModel && isCoolingDown((0, llmModelRegistry_1.providerFor)(req.model))) {
|
|
62
|
+
const out = await run(req.fallbackModel);
|
|
63
|
+
return { ...out, attemptedModels: [...attempted], usedFallback: true };
|
|
64
|
+
}
|
|
27
65
|
try {
|
|
28
66
|
const out = await run(req.model);
|
|
29
67
|
return { ...out, attemptedModels: [...attempted], usedFallback: false };
|
|
30
68
|
}
|
|
31
69
|
catch (err) {
|
|
32
|
-
|
|
70
|
+
noteAccountFailure(req.model, err);
|
|
71
|
+
if (req.fallbackModel && (0, llmRetry_1.isFallbackEligibleLlmError)(err)) {
|
|
33
72
|
const out = await run(req.fallbackModel);
|
|
34
73
|
return { ...out, attemptedModels: [...attempted], usedFallback: true };
|
|
35
74
|
}
|
|
@@ -46,12 +85,17 @@ class ProviderRoutingLlmCaller {
|
|
|
46
85
|
fallbackModel: undefined,
|
|
47
86
|
});
|
|
48
87
|
};
|
|
88
|
+
if (req.fallbackModel && isCoolingDown((0, llmModelRegistry_1.providerFor)(req.model))) {
|
|
89
|
+
const out = await run(req.fallbackModel);
|
|
90
|
+
return { ...out, attemptedModels: [...attempted], usedFallback: true };
|
|
91
|
+
}
|
|
49
92
|
try {
|
|
50
93
|
const out = await run(req.model);
|
|
51
94
|
return { ...out, attemptedModels: [...attempted], usedFallback: false };
|
|
52
95
|
}
|
|
53
96
|
catch (err) {
|
|
54
|
-
|
|
97
|
+
noteAccountFailure(req.model, err);
|
|
98
|
+
if (req.fallbackModel && (0, llmRetry_1.isFallbackEligibleLlmError)(err)) {
|
|
55
99
|
const out = await run(req.fallbackModel);
|
|
56
100
|
return { ...out, attemptedModels: [...attempted], usedFallback: true };
|
|
57
101
|
}
|
|
@@ -66,8 +110,10 @@ class ProviderRoutingLlmCaller {
|
|
|
66
110
|
* handles it.
|
|
67
111
|
*/
|
|
68
112
|
structuredStream(req) {
|
|
69
|
-
|
|
70
|
-
const
|
|
113
|
+
// Primary provider's account is known-dead → stream from the fallback.
|
|
114
|
+
const model = req.fallbackModel && isCoolingDown((0, llmModelRegistry_1.providerFor)(req.model)) ? req.fallbackModel : req.model;
|
|
115
|
+
const caller = this.pick(model);
|
|
116
|
+
const streamReq = { ...req, model, fallbackModel: undefined };
|
|
71
117
|
if (typeof caller.structuredStream === "function") {
|
|
72
118
|
return caller.structuredStream(streamReq);
|
|
73
119
|
}
|
|
@@ -114,11 +160,25 @@ class ProviderRoutingLlmCaller {
|
|
|
114
160
|
*/
|
|
115
161
|
async structuredWithTools(req) {
|
|
116
162
|
const caller = this.pick(req.model);
|
|
163
|
+
if (isCoolingDown((0, llmModelRegistry_1.providerFor)(req.model))) {
|
|
164
|
+
// Server tools are provider-specific, so there's no cross-provider
|
|
165
|
+
// fallback here — fail fast with an account error the caller (Planner
|
|
166
|
+
// research path) downgrades to the plain dispatcher on.
|
|
167
|
+
throw Object.assign(new Error("provider account unavailable (usage limits) — cooling down"), {
|
|
168
|
+
status: 400,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
117
171
|
if (typeof caller.structuredWithTools !== "function") {
|
|
118
172
|
throw new Error(`ProviderRoutingLlmCaller: provider for model=${req.model.modelId} ` +
|
|
119
173
|
`does not support server-side tools (structuredWithTools).`);
|
|
120
174
|
}
|
|
121
|
-
|
|
175
|
+
try {
|
|
176
|
+
return await caller.structuredWithTools(req);
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
noteAccountFailure(req.model, err);
|
|
180
|
+
throw err;
|
|
181
|
+
}
|
|
122
182
|
}
|
|
123
183
|
}
|
|
124
184
|
exports.ProviderRoutingLlmCaller = ProviderRoutingLlmCaller;
|
|
@@ -54,4 +54,17 @@ export interface RetryOptions {
|
|
|
54
54
|
* wrapped on the final attempt; pre-wrapped `TransientLlmError`s pass through.
|
|
55
55
|
*/
|
|
56
56
|
export declare function withRetry<T>(fn: () => Promise<T>, opts?: RetryOptions): Promise<T>;
|
|
57
|
+
/**
|
|
58
|
+
* Provider ACCOUNT is unusable (spend cap, no credit, quota, revoked key) —
|
|
59
|
+
* deterministic for THIS provider, but another provider will succeed.
|
|
60
|
+
*
|
|
61
|
+
* Prod 2026-11-18/19: Anthropic returned 400 "You have reached your specified
|
|
62
|
+
* workspace API usage limits. You will regain access on 2026-12-01" for every
|
|
63
|
+
* planner/persona call. A 400 is not transient, so the dispatcher never tried
|
|
64
|
+
* the OpenAI `fallbackModel` and agent planning was down for every user.
|
|
65
|
+
* These errors must fall back cross-provider (and never be retried in place).
|
|
66
|
+
*/
|
|
67
|
+
export declare function isProviderAccountUnavailableError(err: unknown): boolean;
|
|
68
|
+
/** True when the dispatcher should try `fallbackModel` after `err`. */
|
|
69
|
+
export declare function isFallbackEligibleLlmError(err: unknown): boolean;
|
|
57
70
|
//# sourceMappingURL=llmRetry.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llmRetry.d.ts","sourceRoot":"","sources":["../../../src/services/agent/llmRetry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH;;;;GAIG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;IAGxC,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO;gBADxB,OAAO,EAAE,MAAM,EACN,KAAK,CAAC,EAAE,OAAO,YAAA;CAK3B;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,iBAAiB,CAE1E;AAED,sEAAsE;AACtE,eAAO,MAAM,uBAAuB,aAElC,CAAC;AAEH;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,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AAC,CAAC,CAAC,EACpB,IAAI,GAAE,YAAiB,GACtB,OAAO,CAAC,CAAC,CAAC,CA6BZ;AAED;;;;;;;;;GASG;AACH,wBAAgB,iCAAiC,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAWvE;AAED,uEAAuE;AACvE,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAEhE"}
|
|
@@ -21,6 +21,8 @@ exports.TRANSIENT_HTTP_STATUSES = exports.TransientLlmError = void 0;
|
|
|
21
21
|
exports.isTransientLlmError = isTransientLlmError;
|
|
22
22
|
exports.isTransientError = isTransientError;
|
|
23
23
|
exports.withRetry = withRetry;
|
|
24
|
+
exports.isProviderAccountUnavailableError = isProviderAccountUnavailableError;
|
|
25
|
+
exports.isFallbackEligibleLlmError = isFallbackEligibleLlmError;
|
|
24
26
|
/**
|
|
25
27
|
* Marks an error worth retrying / falling back on. The provider-dispatching caller
|
|
26
28
|
* catches this specifically to attempt `fallbackModel`; deterministic errors
|
|
@@ -100,3 +102,30 @@ async function withRetry(fn, opts = {}) {
|
|
|
100
102
|
}
|
|
101
103
|
throw lastErr;
|
|
102
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Provider ACCOUNT is unusable (spend cap, no credit, quota, revoked key) —
|
|
107
|
+
* deterministic for THIS provider, but another provider will succeed.
|
|
108
|
+
*
|
|
109
|
+
* Prod 2026-11-18/19: Anthropic returned 400 "You have reached your specified
|
|
110
|
+
* workspace API usage limits. You will regain access on 2026-12-01" for every
|
|
111
|
+
* planner/persona call. A 400 is not transient, so the dispatcher never tried
|
|
112
|
+
* the OpenAI `fallbackModel` and agent planning was down for every user.
|
|
113
|
+
* These errors must fall back cross-provider (and never be retried in place).
|
|
114
|
+
*/
|
|
115
|
+
function isProviderAccountUnavailableError(err) {
|
|
116
|
+
if (!err || typeof err !== "object")
|
|
117
|
+
return false;
|
|
118
|
+
const e = err;
|
|
119
|
+
// TransientLlmError wraps the original — look through it.
|
|
120
|
+
if (e.cause && e.cause !== err && isProviderAccountUnavailableError(e.cause))
|
|
121
|
+
return true;
|
|
122
|
+
const status = typeof e.status === "number" ? e.status : undefined;
|
|
123
|
+
if (status !== undefined && ![400, 401, 402, 403, 429].includes(status))
|
|
124
|
+
return false;
|
|
125
|
+
const msg = String(e.message ?? "");
|
|
126
|
+
return /usage limits?|credit balance is too low|insufficient[_ ]quota|exceeded your current quota|billing|spend(ing)? limit|payment required|invalid[_ ]api[_ ]key|invalid x-api-key|api key not valid|permission[_ ]denied|account (is )?(disabled|deactivated|suspended)/i.test(msg);
|
|
127
|
+
}
|
|
128
|
+
/** True when the dispatcher should try `fallbackModel` after `err`. */
|
|
129
|
+
function isFallbackEligibleLlmError(err) {
|
|
130
|
+
return isTransientLlmError(err) || isProviderAccountUnavailableError(err);
|
|
131
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Planner.d.ts","sourceRoot":"","sources":["../../../../src/services/agent/planner/Planner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sCAAsC,CAAC;AACpE,OAAO,EAAE,UAAU,EAAoB,MAAM,+BAA+B,CAAC;AAC7E,OAAO,EACL,gBAAgB,EAChB,YAAY,EACZ,WAAW,EACX,SAAS,EAEV,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAChF,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"Planner.d.ts","sourceRoot":"","sources":["../../../../src/services/agent/planner/Planner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sCAAsC,CAAC;AACpE,OAAO,EAAE,UAAU,EAAoB,MAAM,+BAA+B,CAAC;AAC7E,OAAO,EACL,gBAAgB,EAChB,YAAY,EACZ,WAAW,EACX,SAAS,EAEV,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE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G,0BAA0B,CAC9B,KAAK,EAAE,YAAY,EACnB,KAAK,EAAE,UAAU,EACjB,IAAI,GAAE;QACJ,OAAO,CAAC,EAAE,YAAY,CAAC;QACvB,QAAQ,CAAC,EAAE,cAAc,GAAG,IAAI,CAAC;QACjC,KAAK,CAAC,EAAE,gBAAgB,GAAG,IAAI,CAAC;QAChC,cAAc,CAAC,EAAE,OAAO,CAAC;QACzB,0EAA0E;QAC1E,WAAW,CAAC,EAAE,MAAM,CAAC;KACjB,GACL,OAAO,CAAC,WAAW,EAAE,CAAC;CAQ1B"}
|
|
@@ -4,6 +4,7 @@ exports.Planner = void 0;
|
|
|
4
4
|
const brief_schema_1 = require("../../../schemas/brief.schema");
|
|
5
5
|
const videoPlan_schema_1 = require("../../../schemas/videoPlan.schema");
|
|
6
6
|
const llmCallerAnthropic_1 = require("../llmCallerAnthropic");
|
|
7
|
+
const llmRetry_1 = require("../llmRetry");
|
|
7
8
|
const llmCallerRegistry_1 = require("../llmCallerRegistry");
|
|
8
9
|
const modelRouter_1 = require("../modelRouter");
|
|
9
10
|
const scriptFirstPlanner_1 = require("./scriptFirstPlanner");
|
|
@@ -81,7 +82,9 @@ class Planner {
|
|
|
81
82
|
});
|
|
82
83
|
}
|
|
83
84
|
catch (anthErr) {
|
|
84
|
-
|
|
85
|
+
// Account errors (spend cap / no credit) downgrade too: the
|
|
86
|
+
// dispatcher below then falls back cross-provider (prod 2026-11-19).
|
|
87
|
+
if ((0, llmCallerAnthropic_1.isTransientLlmError)(anthErr) || (0, llmRetry_1.isProviderAccountUnavailableError)(anthErr)) {
|
|
85
88
|
// Downgrade: disable research for the rest of this plan call
|
|
86
89
|
// so we don't keep retrying the failing Anthropic path on
|
|
87
90
|
// subsequent attempts. The plan we produce won't be grounded,
|
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
* the model comes back on its own within this window. */
|
|
3
3
|
export declare const PROVIDER_DEPLETION_WINDOW_MS: number;
|
|
4
4
|
/** Worker side: record that `modelKey`'s provider account is out of quota. */
|
|
5
|
+
export declare const PROVIDER_DEPLETION_MAX_WINDOW_MS: number;
|
|
6
|
+
export declare function nextDepletionWindowMs(prev: {
|
|
7
|
+
depletedUntilMs: number;
|
|
8
|
+
windowMs?: number;
|
|
9
|
+
} | null, now: number): number;
|
|
5
10
|
export declare function markModelAccountDepleted(modelKey: string, code: string): Promise<void>;
|
|
6
11
|
/**
|
|
7
12
|
* API side: true while `modelKey` is inside a recorded depletion window.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"providerHealth.service.d.ts","sourceRoot":"","sources":["../../src/services/providerHealth.service.ts"],"names":[],"mappings":"AAkBA;0DAC0D;AAC1D,eAAO,MAAM,4BAA4B,QAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"providerHealth.service.d.ts","sourceRoot":"","sources":["../../src/services/providerHealth.service.ts"],"names":[],"mappings":"AAkBA;0DAC0D;AAC1D,eAAO,MAAM,4BAA4B,QAAc,CAAC;AAcxD,8EAA8E;AAO9E,eAAO,MAAM,gCAAgC,QAAkB,CAAC;AAGhE,wBAAgB,qBAAqB,CACnC,IAAI,EAAE;IAAE,eAAe,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,EAC3D,GAAG,EAAE,MAAM,GACV,MAAM,CAOR;AAED,wBAAsB,wBAAwB,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CA6B5F;AAED;;;;GAIG;AACH,wBAAsB,sBAAsB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAqB/E"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PROVIDER_DEPLETION_WINDOW_MS = void 0;
|
|
3
|
+
exports.PROVIDER_DEPLETION_MAX_WINDOW_MS = exports.PROVIDER_DEPLETION_WINDOW_MS = void 0;
|
|
4
|
+
exports.nextDepletionWindowMs = nextDepletionWindowMs;
|
|
4
5
|
exports.markModelAccountDepleted = markModelAccountDepleted;
|
|
5
6
|
exports.isModelAccountDepleted = isModelAccountDepleted;
|
|
6
7
|
const firestore_1 = require("firebase-admin/firestore");
|
|
@@ -25,16 +26,42 @@ exports.PROVIDER_DEPLETION_WINDOW_MS = 10 * 60000;
|
|
|
25
26
|
const READ_CACHE_MS = 30000;
|
|
26
27
|
const readCache = new Map();
|
|
27
28
|
/** Worker side: record that `modelKey`'s provider account is out of quota. */
|
|
29
|
+
// Escalation (2026-11-19): with a fixed 10-min window a still-dry account
|
|
30
|
+
// failed (and paged) once per window for days — kling-v4.0 paged ~10x across
|
|
31
|
+
// 2026-11-17/19. If the previous window ended recently, double it, capped, so a
|
|
32
|
+
// long outage settles to ~1 page / 2 h. A fresh depletion (or one after a quiet
|
|
33
|
+
// gap) starts at the base window again, so recovery after a top-up stays fast.
|
|
34
|
+
// Delete providerHealth/{modelKey} to clear early.
|
|
35
|
+
exports.PROVIDER_DEPLETION_MAX_WINDOW_MS = 2 * 60 * 60000;
|
|
36
|
+
const ESCALATION_LOOKBACK_MS = 6 * 60 * 60000;
|
|
37
|
+
function nextDepletionWindowMs(prev, now) {
|
|
38
|
+
if (!prev || now - prev.depletedUntilMs > ESCALATION_LOOKBACK_MS)
|
|
39
|
+
return exports.PROVIDER_DEPLETION_WINDOW_MS;
|
|
40
|
+
const prevWindow = prev.windowMs ?? exports.PROVIDER_DEPLETION_WINDOW_MS;
|
|
41
|
+
// Still inside the window (sibling scenes of the same job, a BullMQ retry):
|
|
42
|
+
// don't compound — only a failure AFTER the window expired escalates.
|
|
43
|
+
if (prev.depletedUntilMs > now)
|
|
44
|
+
return prevWindow;
|
|
45
|
+
return Math.min(prevWindow * 2, exports.PROVIDER_DEPLETION_MAX_WINDOW_MS);
|
|
46
|
+
}
|
|
28
47
|
async function markModelAccountDepleted(modelKey, code) {
|
|
29
|
-
|
|
48
|
+
// REMOVED FOR v1 — kept for reference: fixed window.
|
|
49
|
+
// const until = Date.now() + PROVIDER_DEPLETION_WINDOW_MS;
|
|
30
50
|
try {
|
|
51
|
+
const ref = firebase_1.firestore.collection(COLLECTION).doc(modelKey);
|
|
52
|
+
const prevSnap = await ref.get();
|
|
53
|
+
const prev = prevSnap.exists ? prevSnap.data() : undefined;
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
const windowMs = nextDepletionWindowMs(prev?.depletedUntil ? { depletedUntilMs: prev.depletedUntil.toMillis(), windowMs: prev.windowMs } : null, now);
|
|
56
|
+
const until = now + windowMs;
|
|
31
57
|
const doc = {
|
|
32
58
|
modelKey,
|
|
33
59
|
depletedUntil: firestore_1.Timestamp.fromMillis(until),
|
|
60
|
+
windowMs,
|
|
34
61
|
code,
|
|
35
62
|
updatedAt: firestore_1.Timestamp.now(),
|
|
36
63
|
};
|
|
37
|
-
await
|
|
64
|
+
await ref.set(doc);
|
|
38
65
|
readCache.set(modelKey, { at: Date.now(), depletedUntilMs: until });
|
|
39
66
|
}
|
|
40
67
|
catch (err) {
|