@juspay/neurolink 12.56.0 → 12.56.1
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/CHANGELOG.md +3 -3
- package/dist/browser/neurolink.min.js +409 -409
- package/dist/neurolink.d.ts +12 -8
- package/dist/neurolink.js +89 -25
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/ollamaDiagnostics.d.ts +31 -0
- package/dist/types/ollamaDiagnostics.js +1 -0
- package/dist/utils/ollamaDiagnostics.d.ts +69 -0
- package/dist/utils/ollamaDiagnostics.js +192 -0
- package/dist/utils/providerHealth.d.ts +33 -19
- package/dist/utils/providerHealth.js +224 -85
- package/package.json +3 -1
package/dist/neurolink.d.ts
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* NeuroLink - Unified AI Interface with Real MCP Tool Integration
|
|
3
|
-
*
|
|
4
|
-
* REDESIGNED FALLBACK CHAIN - NO CIRCULAR DEPENDENCIES
|
|
5
|
-
* Enhanced AI provider system with natural MCP tool access.
|
|
6
|
-
* Uses real MCP infrastructure for tool discovery and execution.
|
|
7
|
-
*/
|
|
8
1
|
import type { DecisionCallerOptions, DecisionLimitsQuery, DecisionLimitsReading, DecisionOptions, DecisionResult, AgentDefinition, AgentNetworkConfig, AgentRunOptions, AgentRunOutcome, AgentToolRegistrationOptions, IsolatedAgentDefinition, NetworkExecutionInput, NetworkExecutionOptions, NetworkExecutionResult, NetworkStreamChunk, WorkerInstanceOptions } from "./types/index.js";
|
|
9
2
|
import type { CompactionConfig, CompactionResult, ArtifactStore, SpanData, ObservabilityConfig, MetricsSummary, MCPToolAnnotations, TraceView, AuthenticatedContext, AuthProvider, JsonObject, NeuroLinkEvents, TypedEventEmitter, MCPEnhancementsConfig, NeuroLinkAuthConfig, NeurolinkConstructorConfig, ChatMessage, ConversationSeedMessage, ConversationSeedOptions, RecordConversationTurnOptions, RecordConversationTurnResult, ExternalMCPOperationResult, ExternalMCPServerInstance, ExternalMCPServerHealth, ExternalMCPToolInfo, GenerateOptions, GenerateResult, ProviderStatus, TextGenerationOptions, TextGenerationResult, MCPExecutableTool, MCPServerInfo, MCPStatus, StreamOptions, StreamResult, ToolExecutionContext, ToolExecutionSummary, ToolInfo, ToolRegistrationOptions, BatchOperationResult, StreamGenerationEndContext, ToolRoutingServerDescriptor, ToolDedupConfig, ToolConfig, KnowledgeEngineStatus, ChecklistState, ArtifactPageRequest, BankArtifactOptions, BankedArtifactRef, DelegateCollectRequest, DelegateCollectResult, DelegateHandle, DelegateRegistrationOptions, DelegateSpawnOptions, BackgroundCommandHandle, BackgroundCommandOptions, BackgroundCommandOutputPage, BackgroundCommandPageRequest, BackgroundCommandPolicy, BackgroundCommandStatus, GitToolResult, GitToolsetOptions } from "./types/index.js";
|
|
10
3
|
import { ConversationMemoryManager } from "./core/conversationMemoryManager.js";
|
|
@@ -612,6 +605,12 @@ export declare class NeuroLink {
|
|
|
612
605
|
* @returns Modified options with orchestrated provider marked in context, or empty object if validation fails
|
|
613
606
|
*/
|
|
614
607
|
private applyStreamOrchestration;
|
|
608
|
+
/**
|
|
609
|
+
* Read a nonempty orchestration preference only when provider is absent/auto.
|
|
610
|
+
* An explicit provider always takes precedence over incoming or generated
|
|
611
|
+
* context; empty, non-string and auto preferences are ignored.
|
|
612
|
+
*/
|
|
613
|
+
private getOrchestratedPreferredProvider;
|
|
615
614
|
/**
|
|
616
615
|
* MAIN ENTRY POINT: Enhanced generate method with new function signature
|
|
617
616
|
* Replaces both generateText and legacy methods
|
|
@@ -937,6 +936,7 @@ export declare class NeuroLink {
|
|
|
937
936
|
*
|
|
938
937
|
* The router is skipped when:
|
|
939
938
|
* - no `requestRouter` is configured on this instance, or
|
|
939
|
+
* - a valid orchestration preference already owns the provider/model pair, or
|
|
940
940
|
* - the caller explicitly set both `options.provider` AND `options.model`
|
|
941
941
|
* (we only skip the router if BOTH are set; a caller setting only one
|
|
942
942
|
* still lets the router fill in the other field).
|
|
@@ -1118,6 +1118,8 @@ export declare class NeuroLink {
|
|
|
1118
1118
|
private generateWithMCPProvider;
|
|
1119
1119
|
/**
|
|
1120
1120
|
* Direct provider generation (no MCP, no recursion)
|
|
1121
|
+
* An explicit provider precedes orchestration context; otherwise a valid
|
|
1122
|
+
* orchestration preference is tried before the existing provider fallback list.
|
|
1121
1123
|
*/
|
|
1122
1124
|
private directProviderGeneration;
|
|
1123
1125
|
/**
|
|
@@ -1324,7 +1326,9 @@ export declare class NeuroLink {
|
|
|
1324
1326
|
*/
|
|
1325
1327
|
private emitStreamStartEvents;
|
|
1326
1328
|
/**
|
|
1327
|
-
* Create MCP stream
|
|
1329
|
+
* Create the provider-backed MCP stream with the existing tool lifecycle.
|
|
1330
|
+
* An explicit provider precedes a valid orchestration context preference;
|
|
1331
|
+
* absent either, getBestProvider retains its normal automatic selection.
|
|
1328
1332
|
*/
|
|
1329
1333
|
private createMCPStream;
|
|
1330
1334
|
/**
|
package/dist/neurolink.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { beginOllamaDiagnostic, captureOllamaDiagnosticContext, completeOllamaDiagnostic, getOllamaDiagnosticErrorMessage, } from "./utils/ollamaDiagnostics.js";
|
|
1
2
|
/**
|
|
2
3
|
* NeuroLink - Unified AI Interface with Real MCP Tool Integration
|
|
3
4
|
*
|
|
@@ -203,13 +204,6 @@ const isNonRetryableForPool = sharedIsNonRetryableForPool;
|
|
|
203
204
|
*/
|
|
204
205
|
const MEMORY_WRITE_TIMEOUT_MS = 30_000;
|
|
205
206
|
/** Native tags share the health check's endpoint precedence and origin. */
|
|
206
|
-
function getOllamaTagsUrl() {
|
|
207
|
-
const baseUrl = process.env.OLLAMA_BASE_URL ||
|
|
208
|
-
process.env.OLLAMA_API_BASE ||
|
|
209
|
-
"http://localhost:11434";
|
|
210
|
-
// Native /api/tags is at the origin even if the compatible base ends /v1.
|
|
211
|
-
return new URL("/api/tags", baseUrl).toString();
|
|
212
|
-
}
|
|
213
207
|
/**
|
|
214
208
|
* NeuroLink - Universal AI Development Platform
|
|
215
209
|
*
|
|
@@ -2737,8 +2731,17 @@ Current user's request: ${currentInput}`;
|
|
|
2737
2731
|
}
|
|
2738
2732
|
// For Ollama, check if service is running and model is available
|
|
2739
2733
|
if (route.provider === "ollama") {
|
|
2734
|
+
let ollamaAttempt;
|
|
2735
|
+
let ollamaOutcome = "not-run";
|
|
2740
2736
|
try {
|
|
2741
|
-
const
|
|
2737
|
+
const ollamaContext = captureOllamaDiagnosticContext();
|
|
2738
|
+
const gate = beginOllamaDiagnostic(ollamaContext);
|
|
2739
|
+
if (gate.kind === "suppressed") {
|
|
2740
|
+
return {};
|
|
2741
|
+
}
|
|
2742
|
+
ollamaAttempt = gate;
|
|
2743
|
+
ollamaOutcome = "transport-failure";
|
|
2744
|
+
const response = await fetch(ollamaContext.url, {
|
|
2742
2745
|
method: "GET",
|
|
2743
2746
|
signal: AbortSignal.timeout(2000),
|
|
2744
2747
|
});
|
|
@@ -2752,6 +2755,7 @@ Current user's request: ${currentInput}`;
|
|
|
2752
2755
|
});
|
|
2753
2756
|
return {}; // Return empty object to preserve existing fallback behavior
|
|
2754
2757
|
}
|
|
2758
|
+
ollamaOutcome = "protocol-failure";
|
|
2755
2759
|
const responseData = await response.json();
|
|
2756
2760
|
const models = responseData?.models;
|
|
2757
2761
|
// Runtime-safe guard: ensure models is an array with valid objects
|
|
@@ -2762,6 +2766,7 @@ Current user's request: ${currentInput}`;
|
|
|
2762
2766
|
});
|
|
2763
2767
|
return {}; // Return empty object for fallback behavior
|
|
2764
2768
|
}
|
|
2769
|
+
ollamaOutcome = "success";
|
|
2765
2770
|
// Filter and validate models before comparison
|
|
2766
2771
|
const validModels = models.filter((m) => m && typeof m === "object" && typeof m.name === "string");
|
|
2767
2772
|
const targetModel = route.model;
|
|
@@ -2807,12 +2812,17 @@ Current user's request: ${currentInput}`;
|
|
|
2807
2812
|
routedProvider: route.provider,
|
|
2808
2813
|
routedModel: route.model,
|
|
2809
2814
|
reason: error instanceof Error
|
|
2810
|
-
? error
|
|
2815
|
+
? getOllamaDiagnosticErrorMessage(error)
|
|
2811
2816
|
: "Ollama service check failed",
|
|
2812
2817
|
orchestrationTime: `${Date.now() - startTime}ms`,
|
|
2813
2818
|
});
|
|
2814
2819
|
return {}; // Return empty object to preserve existing fallback behavior
|
|
2815
2820
|
}
|
|
2821
|
+
finally {
|
|
2822
|
+
if (ollamaAttempt) {
|
|
2823
|
+
completeOllamaDiagnostic(ollamaAttempt, ollamaOutcome);
|
|
2824
|
+
}
|
|
2825
|
+
}
|
|
2816
2826
|
}
|
|
2817
2827
|
logger.debug("Orchestration route determined", {
|
|
2818
2828
|
taskType: classification.type,
|
|
@@ -2875,8 +2885,17 @@ Current user's request: ${currentInput}`;
|
|
|
2875
2885
|
}
|
|
2876
2886
|
// For Ollama, check if service is running and model is available
|
|
2877
2887
|
if (route.provider === "ollama") {
|
|
2888
|
+
let ollamaAttempt;
|
|
2889
|
+
let ollamaOutcome = "not-run";
|
|
2878
2890
|
try {
|
|
2879
|
-
const
|
|
2891
|
+
const ollamaContext = captureOllamaDiagnosticContext();
|
|
2892
|
+
const gate = beginOllamaDiagnostic(ollamaContext);
|
|
2893
|
+
if (gate.kind === "suppressed") {
|
|
2894
|
+
return {};
|
|
2895
|
+
}
|
|
2896
|
+
ollamaAttempt = gate;
|
|
2897
|
+
ollamaOutcome = "transport-failure";
|
|
2898
|
+
const response = await fetch(ollamaContext.url, {
|
|
2880
2899
|
method: "GET",
|
|
2881
2900
|
signal: AbortSignal.timeout(2000),
|
|
2882
2901
|
});
|
|
@@ -2890,6 +2909,7 @@ Current user's request: ${currentInput}`;
|
|
|
2890
2909
|
});
|
|
2891
2910
|
return {}; // Return empty object to preserve existing fallback behavior
|
|
2892
2911
|
}
|
|
2912
|
+
ollamaOutcome = "protocol-failure";
|
|
2893
2913
|
const responseData = await response.json();
|
|
2894
2914
|
const models = responseData?.models;
|
|
2895
2915
|
// Runtime-safe guard: ensure models is an array with valid objects
|
|
@@ -2900,6 +2920,7 @@ Current user's request: ${currentInput}`;
|
|
|
2900
2920
|
});
|
|
2901
2921
|
return {}; // Return empty object for fallback behavior
|
|
2902
2922
|
}
|
|
2923
|
+
ollamaOutcome = "success";
|
|
2903
2924
|
// Filter and validate models before comparison
|
|
2904
2925
|
const validModels = models.filter((m) => m && typeof m === "object" && typeof m.name === "string");
|
|
2905
2926
|
const targetModel = route.model;
|
|
@@ -2945,12 +2966,17 @@ Current user's request: ${currentInput}`;
|
|
|
2945
2966
|
routedProvider: route.provider,
|
|
2946
2967
|
routedModel: route.model,
|
|
2947
2968
|
reason: error instanceof Error
|
|
2948
|
-
? error
|
|
2969
|
+
? getOllamaDiagnosticErrorMessage(error)
|
|
2949
2970
|
: "Ollama service check failed",
|
|
2950
2971
|
orchestrationTime: `${Date.now() - startTime}ms`,
|
|
2951
2972
|
});
|
|
2952
2973
|
return {}; // Return empty object to preserve existing fallback behavior
|
|
2953
2974
|
}
|
|
2975
|
+
finally {
|
|
2976
|
+
if (ollamaAttempt) {
|
|
2977
|
+
completeOllamaDiagnostic(ollamaAttempt, ollamaOutcome);
|
|
2978
|
+
}
|
|
2979
|
+
}
|
|
2954
2980
|
}
|
|
2955
2981
|
logger.debug("Stream orchestration route determined", {
|
|
2956
2982
|
taskType: classification.type,
|
|
@@ -2978,6 +3004,22 @@ Current user's request: ${currentInput}`;
|
|
|
2978
3004
|
throw error;
|
|
2979
3005
|
}
|
|
2980
3006
|
}
|
|
3007
|
+
/**
|
|
3008
|
+
* Read a nonempty orchestration preference only when provider is absent/auto.
|
|
3009
|
+
* An explicit provider always takes precedence over incoming or generated
|
|
3010
|
+
* context; empty, non-string and auto preferences are ignored.
|
|
3011
|
+
*/
|
|
3012
|
+
getOrchestratedPreferredProvider(options) {
|
|
3013
|
+
if (options.provider && options.provider !== "auto") {
|
|
3014
|
+
return undefined;
|
|
3015
|
+
}
|
|
3016
|
+
const preferred = options.context?.__orchestratedPreferredProvider;
|
|
3017
|
+
return typeof preferred === "string" &&
|
|
3018
|
+
preferred.length > 0 &&
|
|
3019
|
+
preferred !== "auto"
|
|
3020
|
+
? preferred
|
|
3021
|
+
: undefined;
|
|
3022
|
+
}
|
|
2981
3023
|
/**
|
|
2982
3024
|
* MAIN ENTRY POINT: Enhanced generate method with new function signature
|
|
2983
3025
|
* Replaces both generateText and legacy methods
|
|
@@ -4339,6 +4381,7 @@ Current user's request: ${currentInput}`;
|
|
|
4339
4381
|
*
|
|
4340
4382
|
* The router is skipped when:
|
|
4341
4383
|
* - no `requestRouter` is configured on this instance, or
|
|
4384
|
+
* - a valid orchestration preference already owns the provider/model pair, or
|
|
4342
4385
|
* - the caller explicitly set both `options.provider` AND `options.model`
|
|
4343
4386
|
* (we only skip the router if BOTH are set; a caller setting only one
|
|
4344
4387
|
* still lets the router fill in the other field).
|
|
@@ -4358,8 +4401,11 @@ Current user's request: ${currentInput}`;
|
|
|
4358
4401
|
}
|
|
4359
4402
|
// Stand down if the classifier router already selected a provider/model on
|
|
4360
4403
|
// this turn — one explicit precedence order: classifierRouter wins.
|
|
4361
|
-
if (options.context
|
|
4362
|
-
|
|
4404
|
+
if (options.context?.__classifierRouted) {
|
|
4405
|
+
return;
|
|
4406
|
+
}
|
|
4407
|
+
// Keep an orchestration-selected model with the provider that owns it.
|
|
4408
|
+
if (this.getOrchestratedPreferredProvider(options)) {
|
|
4363
4409
|
return;
|
|
4364
4410
|
}
|
|
4365
4411
|
// Skip if the caller explicitly set both provider AND model — they know
|
|
@@ -6577,6 +6623,8 @@ Current user's request: ${currentInput}`;
|
|
|
6577
6623
|
}
|
|
6578
6624
|
/**
|
|
6579
6625
|
* Direct provider generation (no MCP, no recursion)
|
|
6626
|
+
* An explicit provider precedes orchestration context; otherwise a valid
|
|
6627
|
+
* orchestration preference is tried before the existing provider fallback list.
|
|
6580
6628
|
*/
|
|
6581
6629
|
async directProviderGeneration(options) {
|
|
6582
6630
|
const startTime = Date.now();
|
|
@@ -6593,13 +6641,8 @@ Current user's request: ${currentInput}`;
|
|
|
6593
6641
|
"ollama",
|
|
6594
6642
|
];
|
|
6595
6643
|
const requestedProvider = options.provider === "auto" ? undefined : options.provider;
|
|
6596
|
-
//
|
|
6597
|
-
const preferredOrchestrated = options
|
|
6598
|
-
typeof options.context === "object" &&
|
|
6599
|
-
"__orchestratedPreferredProvider" in options.context
|
|
6600
|
-
? options.context
|
|
6601
|
-
.__orchestratedPreferredProvider
|
|
6602
|
-
: undefined;
|
|
6644
|
+
// Read the shared preference while keeping an explicit provider first.
|
|
6645
|
+
const preferredOrchestrated = this.getOrchestratedPreferredProvider(options);
|
|
6603
6646
|
// Build provider list with orchestrated preference first, then fallback to full list
|
|
6604
6647
|
const tryProviders = preferredOrchestrated
|
|
6605
6648
|
? [
|
|
@@ -9425,11 +9468,16 @@ Current user's request: ${currentInput}`;
|
|
|
9425
9468
|
this.emitter.emit("message", `Starting ${options.provider || "auto"} stream...`);
|
|
9426
9469
|
}
|
|
9427
9470
|
/**
|
|
9428
|
-
* Create MCP stream
|
|
9471
|
+
* Create the provider-backed MCP stream with the existing tool lifecycle.
|
|
9472
|
+
* An explicit provider precedes a valid orchestration context preference;
|
|
9473
|
+
* absent either, getBestProvider retains its normal automatic selection.
|
|
9429
9474
|
*/
|
|
9430
9475
|
async createMCPStream(options) {
|
|
9431
|
-
//
|
|
9432
|
-
const
|
|
9476
|
+
// Consume the validated orchestration choice after an explicit provider.
|
|
9477
|
+
const requestedProvider = options.provider && options.provider !== "auto"
|
|
9478
|
+
? options.provider
|
|
9479
|
+
: this.getOrchestratedPreferredProvider(options);
|
|
9480
|
+
const providerName = await getBestProvider(requestedProvider);
|
|
9433
9481
|
const provider = await AIProviderFactory.createProvider(providerName, options.model, !options.disableTools, // Pass disableTools as inverse of enableMCP
|
|
9434
9482
|
this, // Pass SDK instance
|
|
9435
9483
|
options.region, // Pass region parameter
|
|
@@ -11900,14 +11948,24 @@ Current user's request: ${currentInput}`;
|
|
|
11900
11948
|
}
|
|
11901
11949
|
// Special handling for Ollama
|
|
11902
11950
|
if (providerName === "ollama") {
|
|
11951
|
+
let ollamaAttempt;
|
|
11952
|
+
let ollamaOutcome = "not-run";
|
|
11903
11953
|
try {
|
|
11904
|
-
const
|
|
11954
|
+
const ollamaContext = captureOllamaDiagnosticContext();
|
|
11955
|
+
const gate = beginOllamaDiagnostic(ollamaContext);
|
|
11956
|
+
if (gate.kind === "suppressed") {
|
|
11957
|
+
throw new Error(gate.reason);
|
|
11958
|
+
}
|
|
11959
|
+
ollamaAttempt = gate;
|
|
11960
|
+
ollamaOutcome = "transport-failure";
|
|
11961
|
+
const response = await fetch(ollamaContext.url, {
|
|
11905
11962
|
method: "GET",
|
|
11906
11963
|
signal: AbortSignal.timeout(PROVIDER_TIMEOUTS.AUTH_MS),
|
|
11907
11964
|
});
|
|
11908
11965
|
if (!response.ok) {
|
|
11909
11966
|
throw new Error("Ollama service not responding");
|
|
11910
11967
|
}
|
|
11968
|
+
ollamaOutcome = "protocol-failure";
|
|
11911
11969
|
const responseData = await response.json();
|
|
11912
11970
|
const models = responseData?.models;
|
|
11913
11971
|
// Runtime-safe guard: ensure models is an array with valid objects
|
|
@@ -11918,6 +11976,7 @@ Current user's request: ${currentInput}`;
|
|
|
11918
11976
|
});
|
|
11919
11977
|
throw new Error("Invalid models format from Ollama API");
|
|
11920
11978
|
}
|
|
11979
|
+
ollamaOutcome = "success";
|
|
11921
11980
|
// Filter and validate models before comparison
|
|
11922
11981
|
const validModels = models.filter((m) => m && typeof m === "object" && typeof m.name === "string");
|
|
11923
11982
|
if (validModels.length > 0) {
|
|
@@ -11948,11 +12007,16 @@ Current user's request: ${currentInput}`;
|
|
|
11948
12007
|
configured: false,
|
|
11949
12008
|
authenticated: false,
|
|
11950
12009
|
error: error instanceof Error
|
|
11951
|
-
? error
|
|
12010
|
+
? getOllamaDiagnosticErrorMessage(error)
|
|
11952
12011
|
: "Ollama service not running",
|
|
11953
12012
|
responseTime: Date.now() - startTime,
|
|
11954
12013
|
};
|
|
11955
12014
|
}
|
|
12015
|
+
finally {
|
|
12016
|
+
if (ollamaAttempt) {
|
|
12017
|
+
completeOllamaDiagnostic(ollamaAttempt, ollamaOutcome);
|
|
12018
|
+
}
|
|
12019
|
+
}
|
|
11956
12020
|
}
|
|
11957
12021
|
// Test other providers with actual generation call. The losing
|
|
11958
12022
|
// side of the race is aborted in `finally` below — previously the
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/index.js
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Native Ollama diagnostic identity, outcome and captured policy state. */
|
|
2
|
+
export type OllamaDiagnosticContext = Readonly<{
|
|
3
|
+
/** Private resolved URL: never include it, its key, or credentials in logs. */
|
|
4
|
+
url: string;
|
|
5
|
+
key: string;
|
|
6
|
+
model: string;
|
|
7
|
+
}>;
|
|
8
|
+
export type OllamaDiagnosticSnapshot = Readonly<{
|
|
9
|
+
count: number;
|
|
10
|
+
lastFailureAt: number | undefined;
|
|
11
|
+
revision: number;
|
|
12
|
+
generation: number;
|
|
13
|
+
}>;
|
|
14
|
+
export type OllamaDiagnosticState = {
|
|
15
|
+
count: number;
|
|
16
|
+
lastFailureAt: number | undefined;
|
|
17
|
+
revision: number;
|
|
18
|
+
generation: number;
|
|
19
|
+
};
|
|
20
|
+
export type OllamaDiagnosticPermit = Readonly<{
|
|
21
|
+
kind: "allowed";
|
|
22
|
+
context: OllamaDiagnosticContext;
|
|
23
|
+
generation: number;
|
|
24
|
+
}>;
|
|
25
|
+
export type OllamaDiagnosticGate = OllamaDiagnosticPermit | Readonly<{
|
|
26
|
+
kind: "suppressed";
|
|
27
|
+
count: number;
|
|
28
|
+
reason: string;
|
|
29
|
+
generation: number;
|
|
30
|
+
}>;
|
|
31
|
+
export type OllamaDiagnosticOutcome = "success" | "transport-failure" | "protocol-failure" | "not-run";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { OllamaDiagnosticContext, OllamaDiagnosticGate, OllamaDiagnosticOutcome, OllamaDiagnosticPermit, OllamaDiagnosticSnapshot } from "../types/index.js";
|
|
2
|
+
export declare const OLLAMA_DIAGNOSTIC_RESET_MS = 300000;
|
|
3
|
+
/**
|
|
4
|
+
* Validate the shared failure threshold once and reuse that first-use value.
|
|
5
|
+
* Missing or invalid PROVIDER_FAILURE_THRESHOLD values use the existing default
|
|
6
|
+
* of three; ProviderHealthChecker retains its original initialization point.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getValidatedProviderFailureThreshold(): number;
|
|
9
|
+
/** Return diagnostic error text with URL secrets redacted for logs and status. */
|
|
10
|
+
export declare function getOllamaDiagnosticErrorMessage(error: unknown): string;
|
|
11
|
+
/**
|
|
12
|
+
* Normalize an endpoint identity without its fragment, retaining path and query.
|
|
13
|
+
* The key can contain credentials and must remain private like the source URL.
|
|
14
|
+
*/
|
|
15
|
+
export declare function getOllamaDiagnosticEndpointKey(url: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Resolve native /api/tags at the configured Ollama origin, independently of a
|
|
18
|
+
* compatible /v1 base path. OLLAMA_BASE_URL precedes the legacy OLLAMA_API_BASE
|
|
19
|
+
* alias, then localhost; malformed URLs and non-HTTP(S) schemes are rejected.
|
|
20
|
+
*/
|
|
21
|
+
export declare function resolveOllamaNativeTagsUrl(): string;
|
|
22
|
+
/**
|
|
23
|
+
* Capture endpoint identity and model before asynchronous diagnostic work.
|
|
24
|
+
* Later environment changes cannot retarget this context or its health-cache
|
|
25
|
+
* identity; failure accounting remains shared by endpoint rather than model.
|
|
26
|
+
*/
|
|
27
|
+
export declare function captureOllamaDiagnosticContext(model?: string): OllamaDiagnosticContext;
|
|
28
|
+
/**
|
|
29
|
+
* Copy the current policy state after applying initialization or timed expiry.
|
|
30
|
+
* This observation issues no request and records neither failure nor recovery.
|
|
31
|
+
*/
|
|
32
|
+
export declare function getOllamaDiagnosticSnapshot(context: OllamaDiagnosticContext): OllamaDiagnosticSnapshot;
|
|
33
|
+
/**
|
|
34
|
+
* Admit a native probe or report backoff at the current failure threshold.
|
|
35
|
+
* Timed expiry is applied before this decision. Admission captures the clear
|
|
36
|
+
* generation without reserving a request or recording an outcome; callers must
|
|
37
|
+
* record the actual outcome on completion.
|
|
38
|
+
*/
|
|
39
|
+
export declare function beginOllamaDiagnostic(context: OllamaDiagnosticContext): OllamaDiagnosticGate;
|
|
40
|
+
/**
|
|
41
|
+
* Apply timed expiry, then an admitted outcome once to the latest endpoint state.
|
|
42
|
+
* Transport/protocol failures increment its current count; success clears a
|
|
43
|
+
* nonzero count; not-run records no outcome. Timed expiry preserves permits,
|
|
44
|
+
* including those admitted before the boundary and completed after it.
|
|
45
|
+
* Duplicate permits and permits invalidated by explicit clearing are ignored.
|
|
46
|
+
*
|
|
47
|
+
* @returns A copy of the resulting state, or undefined for an ignored permit.
|
|
48
|
+
*/
|
|
49
|
+
export declare function completeOllamaDiagnostic(attempt: OllamaDiagnosticPermit, outcome: OllamaDiagnosticOutcome): OllamaDiagnosticSnapshot | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Clear every endpoint and advance generation, invalidating in-flight permits.
|
|
52
|
+
* Unlike timed expiry, this prevents an older completion from repopulating state.
|
|
53
|
+
*/
|
|
54
|
+
export declare function clearOllamaDiagnostics(): void;
|
|
55
|
+
/**
|
|
56
|
+
* Separate cached health by captured endpoint and model without splitting the
|
|
57
|
+
* endpoint-wide diagnostic counter. This private key can contain URL secrets.
|
|
58
|
+
*/
|
|
59
|
+
export declare function getOllamaHealthCacheKey(context: OllamaDiagnosticContext): string;
|
|
60
|
+
export declare class OllamaProtocolError extends Error {
|
|
61
|
+
/** Identify an answered tags response whose models envelope is malformed. */
|
|
62
|
+
constructor();
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Require an object containing a models array, including an empty array.
|
|
66
|
+
* Individual model entries are left to existing normalization; a malformed
|
|
67
|
+
* envelope throws OllamaProtocolError rather than appearing as no models.
|
|
68
|
+
*/
|
|
69
|
+
export declare function readOllamaTagsModels(payload: unknown): unknown[];
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/** Internal native-diagnostic policy; inference and provider selection are separate. */
|
|
2
|
+
import { logger } from "./logger.js";
|
|
3
|
+
import { DEFAULT_OLLAMA_MODEL } from "../providers/ollama/constants.js";
|
|
4
|
+
import { redactUrlsInText } from "./logSanitize.js";
|
|
5
|
+
export const OLLAMA_DIAGNOSTIC_RESET_MS = 300000;
|
|
6
|
+
let validatedFailureThreshold;
|
|
7
|
+
/**
|
|
8
|
+
* Validate the shared failure threshold once and reuse that first-use value.
|
|
9
|
+
* Missing or invalid PROVIDER_FAILURE_THRESHOLD values use the existing default
|
|
10
|
+
* of three; ProviderHealthChecker retains its original initialization point.
|
|
11
|
+
*/
|
|
12
|
+
export function getValidatedProviderFailureThreshold() {
|
|
13
|
+
if (validatedFailureThreshold !== undefined) {
|
|
14
|
+
return validatedFailureThreshold;
|
|
15
|
+
}
|
|
16
|
+
const envValue = process.env.PROVIDER_FAILURE_THRESHOLD;
|
|
17
|
+
if (!envValue) {
|
|
18
|
+
validatedFailureThreshold = 3;
|
|
19
|
+
return validatedFailureThreshold;
|
|
20
|
+
}
|
|
21
|
+
const parsed = Number(envValue);
|
|
22
|
+
if (isNaN(parsed) || parsed <= 0 || parsed > 10) {
|
|
23
|
+
logger.warn(`Invalid PROVIDER_FAILURE_THRESHOLD: ${envValue} (must be between 1 and 10), using default: 3`);
|
|
24
|
+
validatedFailureThreshold = 3;
|
|
25
|
+
return validatedFailureThreshold;
|
|
26
|
+
}
|
|
27
|
+
validatedFailureThreshold = parsed;
|
|
28
|
+
return validatedFailureThreshold;
|
|
29
|
+
}
|
|
30
|
+
/** Return diagnostic error text with URL secrets redacted for logs and status. */
|
|
31
|
+
export function getOllamaDiagnosticErrorMessage(error) {
|
|
32
|
+
return redactUrlsInText(error instanceof Error ? error.message : String(error));
|
|
33
|
+
}
|
|
34
|
+
const states = new Map();
|
|
35
|
+
const completed = new WeakSet();
|
|
36
|
+
let clearGeneration = 0;
|
|
37
|
+
/**
|
|
38
|
+
* Normalize an endpoint identity without its fragment, retaining path and query.
|
|
39
|
+
* The key can contain credentials and must remain private like the source URL.
|
|
40
|
+
*/
|
|
41
|
+
export function getOllamaDiagnosticEndpointKey(url) {
|
|
42
|
+
const parsed = new URL(url);
|
|
43
|
+
parsed.hash = "";
|
|
44
|
+
return parsed.toString();
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Resolve native /api/tags at the configured Ollama origin, independently of a
|
|
48
|
+
* compatible /v1 base path. OLLAMA_BASE_URL precedes the legacy OLLAMA_API_BASE
|
|
49
|
+
* alias, then localhost; malformed URLs and non-HTTP(S) schemes are rejected.
|
|
50
|
+
*/
|
|
51
|
+
export function resolveOllamaNativeTagsUrl() {
|
|
52
|
+
const base = process.env.OLLAMA_BASE_URL ||
|
|
53
|
+
process.env.OLLAMA_API_BASE ||
|
|
54
|
+
"http://localhost:11434";
|
|
55
|
+
const resolved = new URL("/api/tags", base);
|
|
56
|
+
if (resolved.protocol !== "http:" && resolved.protocol !== "https:") {
|
|
57
|
+
throw new Error("Invalid OLLAMA_BASE_URL format (OLLAMA_API_BASE is still accepted as a legacy alias)");
|
|
58
|
+
}
|
|
59
|
+
resolved.hash = "";
|
|
60
|
+
return resolved.toString();
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Capture endpoint identity and model before asynchronous diagnostic work.
|
|
64
|
+
* Later environment changes cannot retarget this context or its health-cache
|
|
65
|
+
* identity; failure accounting remains shared by endpoint rather than model.
|
|
66
|
+
*/
|
|
67
|
+
export function captureOllamaDiagnosticContext(model = process.env.OLLAMA_MODEL || DEFAULT_OLLAMA_MODEL) {
|
|
68
|
+
const url = resolveOllamaNativeTagsUrl();
|
|
69
|
+
return { url, key: getOllamaDiagnosticEndpointKey(url), model };
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Initialize endpoint state or expire failures five minutes after the latest one.
|
|
73
|
+
* Expiry clears the count/timestamp and advances revision, retaining generation
|
|
74
|
+
* so existing in-flight permits remain valid. Only an explicit clear changes
|
|
75
|
+
* generation and invalidates those permits.
|
|
76
|
+
*/
|
|
77
|
+
function getState(context) {
|
|
78
|
+
let state = states.get(context.key);
|
|
79
|
+
if (!state) {
|
|
80
|
+
state = {
|
|
81
|
+
count: 0,
|
|
82
|
+
lastFailureAt: undefined,
|
|
83
|
+
revision: 0,
|
|
84
|
+
generation: clearGeneration,
|
|
85
|
+
};
|
|
86
|
+
states.set(context.key, state);
|
|
87
|
+
}
|
|
88
|
+
if (state.lastFailureAt !== undefined &&
|
|
89
|
+
Date.now() - state.lastFailureAt >= OLLAMA_DIAGNOSTIC_RESET_MS) {
|
|
90
|
+
// This is a fixed-window expiry, not an outcome from a cache hit/probe.
|
|
91
|
+
state.count = 0;
|
|
92
|
+
state.lastFailureAt = undefined;
|
|
93
|
+
state.revision++;
|
|
94
|
+
}
|
|
95
|
+
return state;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Copy the current policy state after applying initialization or timed expiry.
|
|
99
|
+
* This observation issues no request and records neither failure nor recovery.
|
|
100
|
+
*/
|
|
101
|
+
export function getOllamaDiagnosticSnapshot(context) {
|
|
102
|
+
return { ...getState(context) };
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Admit a native probe or report backoff at the current failure threshold.
|
|
106
|
+
* Timed expiry is applied before this decision. Admission captures the clear
|
|
107
|
+
* generation without reserving a request or recording an outcome; callers must
|
|
108
|
+
* record the actual outcome on completion.
|
|
109
|
+
*/
|
|
110
|
+
export function beginOllamaDiagnostic(context) {
|
|
111
|
+
const state = getState(context);
|
|
112
|
+
if (state.count >= getValidatedProviderFailureThreshold()) {
|
|
113
|
+
return {
|
|
114
|
+
kind: "suppressed",
|
|
115
|
+
count: state.count,
|
|
116
|
+
reason: `Ollama native diagnostic backed off after ${state.count} consecutive transport failures`,
|
|
117
|
+
generation: state.generation,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return { kind: "allowed", context, generation: state.generation };
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Apply timed expiry, then an admitted outcome once to the latest endpoint state.
|
|
124
|
+
* Transport/protocol failures increment its current count; success clears a
|
|
125
|
+
* nonzero count; not-run records no outcome. Timed expiry preserves permits,
|
|
126
|
+
* including those admitted before the boundary and completed after it.
|
|
127
|
+
* Duplicate permits and permits invalidated by explicit clearing are ignored.
|
|
128
|
+
*
|
|
129
|
+
* @returns A copy of the resulting state, or undefined for an ignored permit.
|
|
130
|
+
*/
|
|
131
|
+
export function completeOllamaDiagnostic(attempt, outcome) {
|
|
132
|
+
if (completed.has(attempt)) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
completed.add(attempt);
|
|
136
|
+
const storedState = states.get(attempt.context.key);
|
|
137
|
+
if (!storedState || storedState.generation !== attempt.generation) {
|
|
138
|
+
return undefined; // Explicit clearing invalidated this in-flight token.
|
|
139
|
+
}
|
|
140
|
+
const state = getState(attempt.context);
|
|
141
|
+
if (outcome === "transport-failure" || outcome === "protocol-failure") {
|
|
142
|
+
// Read and increment the latest completed state, never the starting count.
|
|
143
|
+
state.count++;
|
|
144
|
+
state.lastFailureAt = Date.now();
|
|
145
|
+
state.revision++;
|
|
146
|
+
}
|
|
147
|
+
else if (outcome === "success" && state.count !== 0) {
|
|
148
|
+
state.count = 0;
|
|
149
|
+
state.lastFailureAt = undefined;
|
|
150
|
+
state.revision++;
|
|
151
|
+
}
|
|
152
|
+
// Only admitted probes may reach this function. not-run cannot claim failure
|
|
153
|
+
// or recovery; genuine success while already clear retains the cache revision.
|
|
154
|
+
return { ...state };
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Clear every endpoint and advance generation, invalidating in-flight permits.
|
|
158
|
+
* Unlike timed expiry, this prevents an older completion from repopulating state.
|
|
159
|
+
*/
|
|
160
|
+
export function clearOllamaDiagnostics() {
|
|
161
|
+
clearGeneration++;
|
|
162
|
+
states.clear();
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Separate cached health by captured endpoint and model without splitting the
|
|
166
|
+
* endpoint-wide diagnostic counter. This private key can contain URL secrets.
|
|
167
|
+
*/
|
|
168
|
+
export function getOllamaHealthCacheKey(context) {
|
|
169
|
+
return JSON.stringify(["ollama", context.key, context.model]);
|
|
170
|
+
}
|
|
171
|
+
export class OllamaProtocolError extends Error {
|
|
172
|
+
/** Identify an answered tags response whose models envelope is malformed. */
|
|
173
|
+
constructor() {
|
|
174
|
+
super("Invalid models format from Ollama API");
|
|
175
|
+
this.name = "OllamaProtocolError";
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Require an object containing a models array, including an empty array.
|
|
180
|
+
* Individual model entries are left to existing normalization; a malformed
|
|
181
|
+
* envelope throws OllamaProtocolError rather than appearing as no models.
|
|
182
|
+
*/
|
|
183
|
+
export function readOllamaTagsModels(payload) {
|
|
184
|
+
if (payload === null ||
|
|
185
|
+
typeof payload !== "object" ||
|
|
186
|
+
Array.isArray(payload) ||
|
|
187
|
+
!("models" in payload) ||
|
|
188
|
+
!Array.isArray(payload.models)) {
|
|
189
|
+
throw new OllamaProtocolError();
|
|
190
|
+
}
|
|
191
|
+
return payload.models;
|
|
192
|
+
}
|