@juspay/neurolink 10.8.5 → 10.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/browser/neurolink.min.js +403 -403
  3. package/dist/core/baseProvider.js +5 -1
  4. package/dist/lib/core/baseProvider.js +5 -1
  5. package/dist/lib/providers/googleVertex/client.d.ts +20 -0
  6. package/dist/lib/providers/googleVertex/client.js +39 -15
  7. package/dist/lib/proxy/modelRouter.d.ts +10 -0
  8. package/dist/lib/proxy/modelRouter.js +17 -0
  9. package/dist/lib/proxy/proxyConfig.js +46 -0
  10. package/dist/lib/proxy/rollingWorkerSupervisor.d.ts +1 -0
  11. package/dist/lib/proxy/rollingWorkerSupervisor.js +41 -15
  12. package/dist/lib/proxy/routingPolicy.d.ts +4 -2
  13. package/dist/lib/proxy/routingPolicy.js +8 -5
  14. package/dist/lib/proxy/runtimeConfig.js +2 -0
  15. package/dist/lib/server/routes/claudeProxyRoutes.d.ts +52 -1
  16. package/dist/lib/server/routes/claudeProxyRoutes.js +467 -221
  17. package/dist/lib/server/routes/openaiProxyRoutes.js +1 -1
  18. package/dist/lib/types/proxy.d.ts +26 -0
  19. package/dist/lib/types/subscription.d.ts +4 -0
  20. package/dist/lib/utils/messageBuilder.d.ts +25 -0
  21. package/dist/lib/utils/messageBuilder.js +33 -12
  22. package/dist/lib/utils/multimodalOptionsBuilder.d.ts +5 -1
  23. package/dist/lib/utils/multimodalOptionsBuilder.js +8 -1
  24. package/dist/providers/googleVertex/client.d.ts +20 -0
  25. package/dist/providers/googleVertex/client.js +39 -15
  26. package/dist/proxy/modelRouter.d.ts +10 -0
  27. package/dist/proxy/modelRouter.js +17 -0
  28. package/dist/proxy/proxyConfig.js +46 -0
  29. package/dist/proxy/rollingWorkerSupervisor.d.ts +1 -0
  30. package/dist/proxy/rollingWorkerSupervisor.js +41 -15
  31. package/dist/proxy/routingPolicy.d.ts +4 -2
  32. package/dist/proxy/routingPolicy.js +8 -5
  33. package/dist/proxy/runtimeConfig.js +2 -0
  34. package/dist/server/routes/claudeProxyRoutes.d.ts +52 -1
  35. package/dist/server/routes/claudeProxyRoutes.js +467 -221
  36. package/dist/server/routes/openaiProxyRoutes.js +1 -1
  37. package/dist/types/proxy.d.ts +26 -0
  38. package/dist/types/subscription.d.ts +4 -0
  39. package/dist/utils/messageBuilder.d.ts +25 -0
  40. package/dist/utils/messageBuilder.js +33 -12
  41. package/dist/utils/multimodalOptionsBuilder.d.ts +5 -1
  42. package/dist/utils/multimodalOptionsBuilder.js +8 -1
  43. package/package.json +1 -1
@@ -321,7 +321,7 @@ export function createOpenAIProxyRoutes(modelRouter, basePath = "", loopbackPort
321
321
  model: targetModel,
322
322
  }, requestModelRouter?.getFallbackChain() ?? [], body.model,
323
323
  // The classifier only reads fields present on both types.
324
- adapted);
324
+ adapted, requestModelRouter?.isAutoFallbackEnabled?.() ?? false);
325
325
  const attempts = plan.attempts;
326
326
  // --- Optional tracing ---
327
327
  let tracer;
@@ -29,6 +29,8 @@ export type ModelRouterInterface = {
29
29
  resolve(requestedModel: string): RouteResult;
30
30
  isClaudeTarget(requestedModel: string): boolean;
31
31
  getFallbackChain(): FallbackEntry[];
32
+ isAutoFallbackEnabled?(): boolean;
33
+ getMaxInflightPerAccount?(): number;
32
34
  getModelMappings?: () => ModelMapping[];
33
35
  getPassthroughModels?: () => string[];
34
36
  };
@@ -639,6 +641,27 @@ export type AnthropicSuccessResult = {
639
641
  };
640
642
  } | {
641
643
  response: Response | unknown;
644
+ holdsAccountAdmission?: boolean;
645
+ };
646
+ /** A release handle for one in-flight request admitted to an OAuth account. */
647
+ export type AccountAdmissionLease = {
648
+ release(): void;
649
+ };
650
+ /** A cancellable queued request for per-account admission capacity. */
651
+ export type QueuedAccountAdmission = {
652
+ accountKey: string;
653
+ promise: Promise<AccountAdmissionLease>;
654
+ cancel(): void;
655
+ };
656
+ /** One queued request waiting for per-account admission capacity. */
657
+ export type AccountAdmissionWaiter = {
658
+ capacity: number;
659
+ resolve: (lease: AccountAdmissionLease) => void;
660
+ };
661
+ /** In-process request admission state for an OAuth account. */
662
+ export type AccountAdmissionState = {
663
+ active: number;
664
+ waiters: AccountAdmissionWaiter[];
642
665
  };
643
666
  /** Result of buffering only enough upstream SSE to make a retry-safe decision. */
644
667
  export type AnthropicStreamPreflightResult = {
@@ -669,6 +692,7 @@ export type ParsedSSEBuffer = {
669
692
  };
670
693
  export type AnthropicAuthRetryResult = {
671
694
  response?: Response | unknown;
695
+ holdsAccountAdmission?: boolean;
672
696
  continueLoop: boolean;
673
697
  lastError: unknown;
674
698
  authFailureMessage: string | null;
@@ -1925,6 +1949,8 @@ export type RollingManagedWorker = {
1925
1949
  generation: number;
1926
1950
  version: string;
1927
1951
  dispose: () => void;
1952
+ pendingTransfers: number;
1953
+ drainRequested: boolean;
1928
1954
  };
1929
1955
  export type RollingCandidateWorker = RollingManagedWorker & {
1930
1956
  expectedVersion: string;
@@ -908,6 +908,10 @@ export type ProxyRoutingConfig = {
908
908
  strategy: "round-robin" | "fill-first";
909
909
  modelMappings: ModelMapping[];
910
910
  fallbackChain: FallbackEntry[];
911
+ /** Permit a last-resort provider chosen by the translation layer. Disabled by default. */
912
+ autoFallback?: boolean;
913
+ /** Maximum in-flight upstream requests per OAuth account. Defaults to two. */
914
+ maxInflightPerAccount?: number;
911
915
  passthroughModels?: string[];
912
916
  /** Enable quota-aware fill-first account ordering. Defaults to true. */
913
917
  quotaRouting?: boolean;
@@ -12,6 +12,31 @@ export declare function convertToModelMessages(messages: MultimodalChatMessage[]
12
12
  * Enhanced with CSV file processing support
13
13
  */
14
14
  export declare function buildMessagesArray(options: TextGenerationOptions | StreamOptions): Promise<ModelMessage[]>;
15
+ /**
16
+ * Fold the `audioFiles` / `videoFiles` aliases into the unified `files` array.
17
+ *
18
+ * #284 gave audio and video their own input fields, but neither has a
19
+ * dedicated processor — both are meant to travel through the same
20
+ * auto-detecting `files` pipeline that already understands "audio"/"video"
21
+ * FileDetector results (see `appendDetectedFileResult`). The fold used to live
22
+ * inline in `buildMultimodalMessagesArray`, which meant any path that bypassed
23
+ * that builder never performed it and dropped the files silently: the model
24
+ * received the prompt alone and answered as though nothing were attached
25
+ * (#1259). GoogleVertex's native SDK path and `buildMultimodalOptions`
26
+ * (Bedrock) are both such paths, so the fold has to be callable from them.
27
+ *
28
+ * The aliases are cleared once merged, which makes the call idempotent: a
29
+ * provider override and the shared builder can both call this on the same
30
+ * options object without attaching every file twice.
31
+ *
32
+ * Mutates in place, matching `processUnifiedFilesArray` below — downstream
33
+ * stages all read `options.input.files`.
34
+ */
35
+ export declare function mergeMediaFileAliases<TFile>(input: {
36
+ files?: Array<TFile | Buffer | string>;
37
+ audioFiles?: Array<Buffer | string>;
38
+ videoFiles?: Array<Buffer | string>;
39
+ }): void;
15
40
  /**
16
41
  * Process the unified files array with auto-detection.
17
42
  * Handles lazy file registration, full processing, and preview injection.
@@ -777,6 +777,38 @@ function appendDetectedFileResult(result, file, options) {
777
777
  logger.info(`[FileDetector] ⚠️ Unknown format (metadata extracted): ${filename}`);
778
778
  }
779
779
  }
780
+ /**
781
+ * Fold the `audioFiles` / `videoFiles` aliases into the unified `files` array.
782
+ *
783
+ * #284 gave audio and video their own input fields, but neither has a
784
+ * dedicated processor — both are meant to travel through the same
785
+ * auto-detecting `files` pipeline that already understands "audio"/"video"
786
+ * FileDetector results (see `appendDetectedFileResult`). The fold used to live
787
+ * inline in `buildMultimodalMessagesArray`, which meant any path that bypassed
788
+ * that builder never performed it and dropped the files silently: the model
789
+ * received the prompt alone and answered as though nothing were attached
790
+ * (#1259). GoogleVertex's native SDK path and `buildMultimodalOptions`
791
+ * (Bedrock) are both such paths, so the fold has to be callable from them.
792
+ *
793
+ * The aliases are cleared once merged, which makes the call idempotent: a
794
+ * provider override and the shared builder can both call this on the same
795
+ * options object without attaching every file twice.
796
+ *
797
+ * Mutates in place, matching `processUnifiedFilesArray` below — downstream
798
+ * stages all read `options.input.files`.
799
+ */
800
+ export function mergeMediaFileAliases(input) {
801
+ if (!input.audioFiles?.length && !input.videoFiles?.length) {
802
+ return;
803
+ }
804
+ input.files = [
805
+ ...(input.files ?? []),
806
+ ...(input.audioFiles ?? []),
807
+ ...(input.videoFiles ?? []),
808
+ ];
809
+ input.audioFiles = undefined;
810
+ input.videoFiles = undefined;
811
+ }
780
812
  /**
781
813
  * Process the unified files array with auto-detection.
782
814
  * Handles lazy file registration, full processing, and preview injection.
@@ -1115,18 +1147,7 @@ export async function buildMultimodalMessagesArray(options, provider, model) {
1115
1147
  // local const so TypeScript sees the definite (non-optional) type in the
1116
1148
  // rest of this function, avoiding 60+ "possibly undefined" errors.
1117
1149
  const inp = options.input;
1118
- // #284: audioFiles/videoFiles have no dedicated processor — route them
1119
- // through the same auto-detecting `files` pipeline that already
1120
- // understands "audio"/"video" FileDetector results (see
1121
- // appendDetectedFileResult), instead of silently dropping them once
1122
- // detectMultimodal() routes an audio/video-only request here.
1123
- if (inp.audioFiles?.length || inp.videoFiles?.length) {
1124
- inp.files = [
1125
- ...(inp.files || []),
1126
- ...(inp.audioFiles || []),
1127
- ...(inp.videoFiles || []),
1128
- ];
1129
- }
1150
+ mergeMediaFileAliases(inp);
1130
1151
  // Compute provider-specific max PDF size once for consistent validation
1131
1152
  const pdfConfig = PDFProcessor.getProviderConfig(provider);
1132
1153
  const maxSize = pdfConfig
@@ -12,6 +12,8 @@ import type { StreamOptions } from "../types/index.js";
12
12
  * - input.files: Auto-detected file types
13
13
  * - input.csvFiles: CSV files for tabular data
14
14
  * - input.pdfFiles: PDF documents (Buffer | string paths)
15
+ * - input.audioFiles: Audio files (Buffer | string paths)
16
+ * - input.videoFiles: Video files (Buffer | string paths)
15
17
  * - csvOptions: CSV parsing options
16
18
  * - systemPrompt: System-level instructions
17
19
  * - conversationMessages: Chat history
@@ -23,7 +25,7 @@ import type { StreamOptions } from "../types/index.js";
23
25
  * @param {string} providerName - Provider identifier (e.g., "vertex", "openai", "anthropic")
24
26
  * @param {string} modelName - Model identifier (e.g., "gemini-2.5-flash", "gpt-4o")
25
27
  * @returns {object} Normalized options object with:
26
- * - input: { text, images, content, files, csvFiles, pdfFiles }
28
+ * - input: { text, images, content, files, csvFiles, pdfFiles, audioFiles, videoFiles }
27
29
  * - csvOptions: CSV processing options
28
30
  * - systemPrompt: System prompt string
29
31
  * - conversationHistory: Message history array
@@ -49,6 +51,8 @@ export declare function buildMultimodalOptions(options: StreamOptions, providerN
49
51
  files: (string | Buffer<ArrayBufferLike> | import("../types/file.js").FileWithMetadata)[] | undefined;
50
52
  csvFiles: (string | Buffer<ArrayBufferLike>)[] | undefined;
51
53
  pdfFiles: (string | Buffer<ArrayBufferLike>)[] | undefined;
54
+ audioFiles: (string | Buffer<ArrayBufferLike>)[] | undefined;
55
+ videoFiles: (string | Buffer<ArrayBufferLike>)[] | undefined;
52
56
  };
53
57
  csvOptions: import("../types/file.js").CSVProcessorOptions | undefined;
54
58
  pdfOptions: {
@@ -11,6 +11,8 @@
11
11
  * - input.files: Auto-detected file types
12
12
  * - input.csvFiles: CSV files for tabular data
13
13
  * - input.pdfFiles: PDF documents (Buffer | string paths)
14
+ * - input.audioFiles: Audio files (Buffer | string paths)
15
+ * - input.videoFiles: Video files (Buffer | string paths)
14
16
  * - csvOptions: CSV parsing options
15
17
  * - systemPrompt: System-level instructions
16
18
  * - conversationMessages: Chat history
@@ -22,7 +24,7 @@
22
24
  * @param {string} providerName - Provider identifier (e.g., "vertex", "openai", "anthropic")
23
25
  * @param {string} modelName - Model identifier (e.g., "gemini-2.5-flash", "gpt-4o")
24
26
  * @returns {object} Normalized options object with:
25
- * - input: { text, images, content, files, csvFiles, pdfFiles }
27
+ * - input: { text, images, content, files, csvFiles, pdfFiles, audioFiles, videoFiles }
26
28
  * - csvOptions: CSV processing options
27
29
  * - systemPrompt: System prompt string
28
30
  * - conversationHistory: Message history array
@@ -49,6 +51,11 @@ export function buildMultimodalOptions(options, providerName, modelName) {
49
51
  files: options.input?.files,
50
52
  csvFiles: options.input?.csvFiles,
51
53
  pdfFiles: options.input?.pdfFiles,
54
+ // #1259: this is a whitelist — a field omitted here is dropped
55
+ // silently, and the model answers as though nothing were attached.
56
+ // audioFiles/videoFiles were missing, so Bedrock received neither.
57
+ audioFiles: options.input?.audioFiles,
58
+ videoFiles: options.input?.videoFiles,
52
59
  },
53
60
  csvOptions: options.csvOptions,
54
61
  pdfOptions: options.pdfOptions,
@@ -141,6 +141,26 @@ export declare class GoogleVertexProvider extends BaseProvider {
141
141
  * Validate stream options
142
142
  */
143
143
  private validateStreamOptionsOnly;
144
+ /**
145
+ * Preprocess file input before routing to the native SDKs.
146
+ *
147
+ * BaseProvider runs this via `buildMultimodalMessagesArray`, but Vertex
148
+ * overrides both `generate()` and `executeStream()` to reach the native
149
+ * @google/genai / @anthropic-ai/vertex-sdk clients directly, so neither
150
+ * inherits it. Without this the file content never reaches the model and
151
+ * the reply is an entirely plausible "no document is attached" — a silent
152
+ * wrong answer rather than an error.
153
+ *
154
+ * #1258: only `generate()` used to call this, so the same document that
155
+ * `generate()` read back correctly came back as "no documents attached"
156
+ * through `stream()`. Sharing one method is what keeps the two paths from
157
+ * drifting apart again.
158
+ *
159
+ * #1259: the alias fold has to happen *before* the `files` check, or
160
+ * requests carrying only `audioFiles`/`videoFiles` look empty here and skip
161
+ * preprocessing entirely.
162
+ */
163
+ private preprocessNativeFileInput;
144
164
  protected executeStream(options: StreamOptions, _analysisSchema?: ZodType<unknown> | Schema<unknown>): Promise<StreamResult>;
145
165
  /**
146
166
  * Emit `stream:end` so the Pipeline B observability listener creates a
@@ -14,7 +14,7 @@ import { AuthenticationError, InvalidModelError, NetworkError, ProviderError, Ra
14
14
  import { ERROR_CODES, NeuroLinkError } from "../../utils/errorHandling.js";
15
15
  import { applyVertexAnthropicCacheBreakpoints } from "../../utils/anthropicCacheBreakpoints.js";
16
16
  import { FileDetector } from "../../utils/fileDetector.js";
17
- import { processUnifiedFilesArray } from "../../utils/messageBuilder.js";
17
+ import { mergeMediaFileAliases, processUnifiedFilesArray, } from "../../utils/messageBuilder.js";
18
18
  import { logger } from "../../utils/logger.js";
19
19
  import { hasRestrictedOutputLimit, RESTRICTED_OUTPUT_TOKEN_LIMIT, toVertexAnthropicModelId, } from "../../utils/modelDetection.js";
20
20
  import { detectImageMimeType } from "../../utils/imageDetection.js";
@@ -868,6 +868,40 @@ export class GoogleVertexProvider extends BaseProvider {
868
868
  validateStreamOptionsOnly(options) {
869
869
  this.validateStreamOptions(options);
870
870
  }
871
+ /**
872
+ * Preprocess file input before routing to the native SDKs.
873
+ *
874
+ * BaseProvider runs this via `buildMultimodalMessagesArray`, but Vertex
875
+ * overrides both `generate()` and `executeStream()` to reach the native
876
+ * @google/genai / @anthropic-ai/vertex-sdk clients directly, so neither
877
+ * inherits it. Without this the file content never reaches the model and
878
+ * the reply is an entirely plausible "no document is attached" — a silent
879
+ * wrong answer rather than an error.
880
+ *
881
+ * #1258: only `generate()` used to call this, so the same document that
882
+ * `generate()` read back correctly came back as "no documents attached"
883
+ * through `stream()`. Sharing one method is what keeps the two paths from
884
+ * drifting apart again.
885
+ *
886
+ * #1259: the alias fold has to happen *before* the `files` check, or
887
+ * requests carrying only `audioFiles`/`videoFiles` look empty here and skip
888
+ * preprocessing entirely.
889
+ */
890
+ async preprocessNativeFileInput(options) {
891
+ if (options.input) {
892
+ mergeMediaFileAliases(options.input);
893
+ }
894
+ if (!options.input?.files?.length) {
895
+ return;
896
+ }
897
+ try {
898
+ // Mutates options.input.text / .images / .pdfFiles in place.
899
+ await processUnifiedFilesArray(options, 100 * 1024 * 1024, this.providerName);
900
+ }
901
+ catch (fileError) {
902
+ logger.warn(`[GoogleVertex] processUnifiedFilesArray threw, continuing without file content: ${fileError instanceof Error ? fileError.message : String(fileError)}`);
903
+ }
904
+ }
871
905
  async executeStream(options, _analysisSchema) {
872
906
  // ALL models now use native SDKs - no more @ai-sdk/google-vertex dependency
873
907
  const modelName = options.model || this.modelName || getDefaultVertexModel();
@@ -889,6 +923,9 @@ export class GoogleVertexProvider extends BaseProvider {
889
923
  // Tool filter (a0269210): trust options.tools — caller (BaseProvider.stream)
890
924
  // already merged MCP/built-in tools and applied any enabledToolNames filter.
891
925
  const optionTools = options.tools || {};
926
+ // #1258: stream() must run the same file preprocessing generate()
927
+ // does, or attached files are dropped on this path alone.
928
+ await this.preprocessNativeFileInput(options);
892
929
  // Emit a `neurolink.message.build` span for the native stream path
893
930
  // so observability tooling sees the same hierarchy it sees on
894
931
  // Pipeline A. Without this, test:tracing's "Message Build Span"
@@ -5742,20 +5779,7 @@ export class GoogleVertexProvider extends BaseProvider {
5742
5779
  const baseTools = !options.disableTools
5743
5780
  ? await this.getToolsForStream(options)
5744
5781
  : {};
5745
- // Process the unified `input.files` array before routing to the
5746
- // native SDK. BaseProvider.generate() runs this preprocessing via
5747
- // buildMultimodalMessagesArray, but Vertex's override skips it,
5748
- // which would otherwise drop text-file content (and the
5749
- // mimetype-hint contract) on the floor. Mutates options.input.text /
5750
- // options.input.images / options.input.pdfFiles in place.
5751
- if (options.input?.files && options.input.files.length > 0) {
5752
- try {
5753
- await processUnifiedFilesArray(options, 100 * 1024 * 1024, this.providerName);
5754
- }
5755
- catch (fileError) {
5756
- logger.warn(`[GoogleVertex] processUnifiedFilesArray threw, continuing without file content: ${fileError instanceof Error ? fileError.message : String(fileError)}`);
5757
- }
5758
- }
5782
+ await this.preprocessNativeFileInput(options);
5759
5783
  // Emit a `neurolink.message.build` span so observability tooling
5760
5784
  // sees the message-construction phase even on the native (Pipeline B)
5761
5785
  // Vertex path. Pipeline A normally produces this via MessageBuilder;
@@ -1,12 +1,22 @@
1
1
  import type { FallbackEntry, ModelMapping, ProxyRoutingConfig, RouteResult } from "../types/index.js";
2
+ /** Default and accepted range for concurrent upstream requests per OAuth account. */
3
+ export declare const MIN_MAX_INFLIGHT_PER_ACCOUNT = 1;
4
+ export declare const MAX_MAX_INFLIGHT_PER_ACCOUNT = 20;
5
+ export declare const DEFAULT_MAX_INFLIGHT_PER_ACCOUNT = 2;
2
6
  export declare class ModelRouter {
3
7
  private readonly mappings;
4
8
  private readonly passthrough;
5
9
  private readonly fallback;
10
+ private readonly autoFallback;
11
+ private readonly maxInflightPerAccount;
6
12
  constructor(config: ProxyRoutingConfig);
7
13
  resolve(requestedModel: string): RouteResult;
8
14
  isClaudeTarget(requestedModel: string): boolean;
9
15
  getFallbackChain(): FallbackEntry[];
16
+ /** Whether translation-layer auto-provider fallback is explicitly enabled. */
17
+ isAutoFallbackEnabled(): boolean;
18
+ /** Maximum concurrent upstream requests admitted for each OAuth account. */
19
+ getMaxInflightPerAccount(): number;
10
20
  /** Return the raw model mapping entries (used by /v1/models). */
11
21
  getModelMappings(): ModelMapping[];
12
22
  /** Return models configured for passthrough (used by /v1/models). */
@@ -1,11 +1,20 @@
1
+ /** Default and accepted range for concurrent upstream requests per OAuth account. */
2
+ export const MIN_MAX_INFLIGHT_PER_ACCOUNT = 1;
3
+ export const MAX_MAX_INFLIGHT_PER_ACCOUNT = 20;
4
+ export const DEFAULT_MAX_INFLIGHT_PER_ACCOUNT = 2;
1
5
  export class ModelRouter {
2
6
  mappings;
3
7
  passthrough;
4
8
  fallback;
9
+ autoFallback;
10
+ maxInflightPerAccount;
5
11
  constructor(config) {
6
12
  this.mappings = new Map(config.modelMappings.map((m) => [m.from, m]));
7
13
  this.passthrough = new Set(config.passthroughModels ?? []);
8
14
  this.fallback = config.fallbackChain;
15
+ this.autoFallback = config.autoFallback === true;
16
+ this.maxInflightPerAccount =
17
+ config.maxInflightPerAccount ?? DEFAULT_MAX_INFLIGHT_PER_ACCOUNT;
9
18
  }
10
19
  resolve(requestedModel) {
11
20
  const mapping = this.mappings.get(requestedModel);
@@ -29,6 +38,14 @@ export class ModelRouter {
29
38
  getFallbackChain() {
30
39
  return this.fallback;
31
40
  }
41
+ /** Whether translation-layer auto-provider fallback is explicitly enabled. */
42
+ isAutoFallbackEnabled() {
43
+ return this.autoFallback;
44
+ }
45
+ /** Maximum concurrent upstream requests admitted for each OAuth account. */
46
+ getMaxInflightPerAccount() {
47
+ return this.maxInflightPerAccount;
48
+ }
32
49
  /** Return the raw model mapping entries (used by /v1/models). */
33
50
  getModelMappings() {
34
51
  return Array.from(this.mappings.values());
@@ -12,6 +12,7 @@
12
12
  */
13
13
  import { readFile } from "node:fs/promises";
14
14
  import { extname } from "node:path";
15
+ import { MAX_MAX_INFLIGHT_PER_ACCOUNT, MIN_MAX_INFLIGHT_PER_ACCOUNT, } from "./modelRouter.js";
15
16
  import { logger } from "../utils/logger.js";
16
17
  // ---------------------------------------------------------------------------
17
18
  // Environment variable resolution
@@ -237,6 +238,24 @@ export function validateProxyConfig(config) {
237
238
  normalizedQuotaRouting !== "false") {
238
239
  errors.push("routing.quota-routing must be a boolean");
239
240
  }
241
+ const rawAutoFallback = routing["auto-fallback"] ?? routing.autoFallback;
242
+ const normalizedAutoFallback = typeof rawAutoFallback === "string"
243
+ ? rawAutoFallback.trim().toLowerCase()
244
+ : undefined;
245
+ if (rawAutoFallback !== undefined &&
246
+ typeof rawAutoFallback !== "boolean" &&
247
+ normalizedAutoFallback !== "true" &&
248
+ normalizedAutoFallback !== "false") {
249
+ errors.push("routing.auto-fallback must be a boolean");
250
+ }
251
+ const rawMaxInflight = routing["max-inflight-per-account"] ?? routing.maxInflightPerAccount;
252
+ if (rawMaxInflight !== undefined &&
253
+ (typeof rawMaxInflight !== "number" ||
254
+ !Number.isInteger(rawMaxInflight) ||
255
+ rawMaxInflight < MIN_MAX_INFLIGHT_PER_ACCOUNT ||
256
+ rawMaxInflight > MAX_MAX_INFLIGHT_PER_ACCOUNT)) {
257
+ errors.push("routing.max-inflight-per-account must be an integer between 1 and 20");
258
+ }
240
259
  const rawSessionSoftLimit = routing["session-soft-limit"] ?? routing.sessionSoftLimit;
241
260
  if (rawSessionSoftLimit !== undefined) {
242
261
  const sessionSoftLimit = Number(rawSessionSoftLimit);
@@ -322,6 +341,8 @@ function warnPlaintextApiKeys(accounts) {
322
341
  * - `strategy` ("round-robin" | "fill-first")
323
342
  * - `model-mappings` / `modelMappings` — array of {from, to, provider}
324
343
  * - `fallback-chain` / `fallbackChain` — array of {provider, model}
344
+ * - `auto-fallback` / `autoFallback` — opt in to an unspecified provider
345
+ * - `max-inflight-per-account` / `maxInflightPerAccount` — concurrency cap
325
346
  * - `passthroughModels` / `passthrough-models` — array of model IDs
326
347
  * - `quota-routing` / `quotaRouting` — quota-aware fill-first ordering
327
348
  * - `session-soft-limit` / `sessionSoftLimit` — proactive handoff threshold
@@ -396,6 +417,31 @@ function parseRoutingConfig(raw) {
396
417
  logger.warn(`[proxy-config] Ignoring routing.quotaRouting: expected boolean, got ${typeof rawQuotaRouting}`);
397
418
  }
398
419
  }
420
+ const rawAutoFallback = raw["auto-fallback"] ?? raw.autoFallback;
421
+ if (rawAutoFallback !== undefined) {
422
+ if (typeof rawAutoFallback === "boolean") {
423
+ result.autoFallback = rawAutoFallback;
424
+ }
425
+ else if (typeof rawAutoFallback === "string" &&
426
+ ["true", "false"].includes(rawAutoFallback.trim().toLowerCase())) {
427
+ result.autoFallback = rawAutoFallback.trim().toLowerCase() === "true";
428
+ }
429
+ else {
430
+ logger.warn(`[proxy-config] Ignoring routing.autoFallback: expected boolean, got ${typeof rawAutoFallback}`);
431
+ }
432
+ }
433
+ const rawMaxInflight = raw["max-inflight-per-account"] ?? raw.maxInflightPerAccount;
434
+ if (rawMaxInflight !== undefined) {
435
+ if (typeof rawMaxInflight === "number" &&
436
+ Number.isInteger(rawMaxInflight) &&
437
+ rawMaxInflight >= MIN_MAX_INFLIGHT_PER_ACCOUNT &&
438
+ rawMaxInflight <= MAX_MAX_INFLIGHT_PER_ACCOUNT) {
439
+ result.maxInflightPerAccount = rawMaxInflight;
440
+ }
441
+ else {
442
+ logger.warn(`[proxy-config] Ignoring routing.maxInflightPerAccount: expected integer between 1 and 20, got ${String(rawMaxInflight)}`);
443
+ }
444
+ }
399
445
  const rawSessionSoftLimit = raw["session-soft-limit"] ?? raw.sessionSoftLimit;
400
446
  if (rawSessionSoftLimit !== undefined) {
401
447
  const sessionSoftLimit = Number(rawSessionSoftLimit);
@@ -32,6 +32,7 @@ export declare class RollingWorkerSupervisor {
32
32
  private spawnCandidate;
33
33
  private flushQueuedSockets;
34
34
  private transferSocket;
35
+ private maybeDrainWorker;
35
36
  private handleTransferFailure;
36
37
  private rejectSocket;
37
38
  private describeTransferError;
@@ -287,21 +287,19 @@ export class RollingWorkerSupervisor {
287
287
  generation,
288
288
  version: expectedVersion,
289
289
  dispose,
290
+ pendingTransfers: 0,
291
+ drainRequested: false,
290
292
  };
291
293
  this.active = activated;
292
294
  this.candidate = null;
293
295
  this.flushQueuedSockets();
294
296
  if (previous) {
295
297
  this.draining.set(previous.generation, previous);
296
- try {
297
- previous.handle.sendControl({
298
- type: "proxy-worker:drain",
299
- generation: previous.generation,
300
- });
301
- }
302
- catch {
303
- previous.handle.terminate("SIGTERM");
304
- }
298
+ // A worker can still own socket transfers accepted before this
299
+ // activation. Draining it before their IPC commits makes those
300
+ // clients fail even though the replacement is healthy.
301
+ previous.drainRequested = true;
302
+ this.maybeDrainWorker(previous);
305
303
  }
306
304
  this.options.log?.(`[proxy-supervisor] activated generation=${generation} pid=${handle.pid} version=${expectedVersion}`);
307
305
  this.publishState();
@@ -340,6 +338,8 @@ export class RollingWorkerSupervisor {
340
338
  handle,
341
339
  generation,
342
340
  version: expectedVersion,
341
+ pendingTransfers: 0,
342
+ drainRequested: false,
343
343
  expectedVersion,
344
344
  activationRequested: false,
345
345
  dispose,
@@ -362,15 +362,41 @@ export class RollingWorkerSupervisor {
362
362
  }
363
363
  }
364
364
  transferSocket(worker, socket) {
365
+ worker.pendingTransfers += 1;
366
+ let settled = false;
367
+ const complete = (error) => {
368
+ if (settled) {
369
+ return;
370
+ }
371
+ settled = true;
372
+ worker.pendingTransfers = Math.max(0, worker.pendingTransfers - 1);
373
+ if (error) {
374
+ this.handleTransferFailure(worker, socket, error);
375
+ }
376
+ this.maybeDrainWorker(worker);
377
+ };
365
378
  try {
366
- worker.handle.sendSocket(worker.generation, socket, (error) => {
367
- if (error) {
368
- this.handleTransferFailure(worker, socket, error);
369
- }
370
- });
379
+ worker.handle.sendSocket(worker.generation, socket, complete);
371
380
  }
372
381
  catch (error) {
373
- this.handleTransferFailure(worker, socket, error);
382
+ complete(error instanceof Error ? error : new Error(String(error)));
383
+ }
384
+ }
385
+ maybeDrainWorker(worker) {
386
+ if (!worker.drainRequested ||
387
+ worker.pendingTransfers > 0 ||
388
+ !this.draining.has(worker.generation)) {
389
+ return;
390
+ }
391
+ worker.drainRequested = false;
392
+ try {
393
+ worker.handle.sendControl({
394
+ type: "proxy-worker:drain",
395
+ generation: worker.generation,
396
+ });
397
+ }
398
+ catch {
399
+ worker.handle.terminate("SIGTERM");
374
400
  }
375
401
  }
376
402
  handleTransferFailure(worker, socket, error) {
@@ -4,12 +4,14 @@ export declare function inferClaudeProxyModelTier(modelName: string): ClaudeProx
4
4
  * Build a translation plan for a Claude-compatible proxy request.
5
5
  * The plan lists the primary provider followed by eligible fallback targets.
6
6
  * All configured fallback entries are always eligible — no contract-based gating.
7
- * When no fallback chain is configured, an "auto-provider" entry is appended.
7
+ * An "auto-provider" entry is appended only when explicitly enabled by the
8
+ * caller. This keeps an empty fallback chain from silently escaping to an
9
+ * unrelated provider.
8
10
  */
9
11
  export declare function buildProxyTranslationPlan(primary: {
10
12
  provider: string;
11
13
  model?: string;
12
- }, fallbackChain: FallbackEntry[], requestedModel: string, _parsed: ParsedClaudeRequest): ProxyTranslationPlan;
14
+ }, fallbackChain: FallbackEntry[], requestedModel: string, _parsed: ParsedClaudeRequest, allowAutoFallback?: boolean): ProxyTranslationPlan;
13
15
  /**
14
16
  * Parse the retry-after header from an upstream 429 response.
15
17
  * Returns milliseconds to wait, or 0 if no valid header present.
@@ -15,9 +15,11 @@ export function inferClaudeProxyModelTier(modelName) {
15
15
  * Build a translation plan for a Claude-compatible proxy request.
16
16
  * The plan lists the primary provider followed by eligible fallback targets.
17
17
  * All configured fallback entries are always eligible — no contract-based gating.
18
- * When no fallback chain is configured, an "auto-provider" entry is appended.
18
+ * An "auto-provider" entry is appended only when explicitly enabled by the
19
+ * caller. This keeps an empty fallback chain from silently escaping to an
20
+ * unrelated provider.
19
21
  */
20
- export function buildProxyTranslationPlan(primary, fallbackChain, requestedModel, _parsed) {
22
+ export function buildProxyTranslationPlan(primary, fallbackChain, requestedModel, _parsed, allowAutoFallback = false) {
21
23
  const attempts = [
22
24
  {
23
25
  provider: primary.provider,
@@ -36,9 +38,10 @@ export function buildProxyTranslationPlan(primary, fallbackChain, requestedModel
36
38
  label: `${fallback.provider}/${fallback.model}`,
37
39
  });
38
40
  }
39
- // Append auto-provider when no configured fallback chain exists,
40
- // or when all configured entries were deduped (same as primary).
41
- if (fallbackChain.length === 0 || attempts.length === 1) {
41
+ // A provider chosen by the translation layer is an explicit opt-in. It is
42
+ // intentionally not a default when configured entries are absent or deduped.
43
+ if (allowAutoFallback &&
44
+ (fallbackChain.length === 0 || attempts.length === 1)) {
42
45
  attempts.push({ label: "auto-provider" });
43
46
  }
44
47
  return {
@@ -212,6 +212,8 @@ async function buildCandidate(options, generation, allowMissingConfig, allowMiss
212
212
  strategy,
213
213
  modelMappings: routing.modelMappings ?? [],
214
214
  fallbackChain: routing.fallbackChain ?? [],
215
+ autoFallback: routing.autoFallback,
216
+ maxInflightPerAccount: routing.maxInflightPerAccount,
215
217
  passthroughModels: routing.passthroughModels,
216
218
  quotaRouting: routing.quotaRouting,
217
219
  sessionSoftLimit: routing.sessionSoftLimit,