@juspay/neurolink 7.48.0 → 7.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +177 -784
- package/dist/agent/directTools.d.ts +55 -0
- package/dist/agent/directTools.js +266 -0
- package/dist/cli/factories/commandFactory.d.ts +2 -0
- package/dist/cli/factories/commandFactory.js +130 -16
- package/dist/cli/index.js +0 -0
- package/dist/cli/loop/conversationSelector.d.ts +45 -0
- package/dist/cli/loop/conversationSelector.js +222 -0
- package/dist/cli/loop/optionsSchema.d.ts +1 -1
- package/dist/cli/loop/session.d.ts +36 -8
- package/dist/cli/loop/session.js +257 -61
- package/dist/core/baseProvider.js +9 -2
- package/dist/core/evaluation.js +5 -2
- package/dist/factories/providerRegistry.js +2 -2
- package/dist/lib/agent/directTools.d.ts +55 -0
- package/dist/lib/agent/directTools.js +266 -0
- package/dist/lib/core/baseProvider.js +9 -2
- package/dist/lib/core/evaluation.js +5 -2
- package/dist/lib/factories/providerRegistry.js +2 -2
- package/dist/lib/mcp/factory.d.ts +2 -157
- package/dist/lib/mcp/flexibleToolValidator.d.ts +1 -5
- package/dist/lib/mcp/index.d.ts +3 -2
- package/dist/lib/mcp/mcpCircuitBreaker.d.ts +1 -75
- package/dist/lib/mcp/mcpClientFactory.d.ts +1 -20
- package/dist/lib/mcp/mcpClientFactory.js +1 -0
- package/dist/lib/mcp/registry.d.ts +3 -10
- package/dist/lib/mcp/servers/agent/directToolsServer.d.ts +1 -1
- package/dist/lib/mcp/servers/aiProviders/aiCoreServer.d.ts +1 -1
- package/dist/lib/mcp/servers/utilities/utilityServer.d.ts +1 -1
- package/dist/lib/mcp/toolDiscoveryService.d.ts +3 -84
- package/dist/lib/mcp/toolRegistry.d.ts +2 -24
- package/dist/lib/middleware/builtin/guardrails.d.ts +5 -16
- package/dist/lib/middleware/builtin/guardrails.js +44 -39
- package/dist/lib/middleware/utils/guardrailsUtils.d.ts +64 -0
- package/dist/lib/middleware/utils/guardrailsUtils.js +387 -0
- package/dist/lib/neurolink.d.ts +1 -1
- package/dist/lib/providers/anthropic.js +46 -3
- package/dist/lib/providers/azureOpenai.js +8 -2
- package/dist/lib/providers/googleAiStudio.js +8 -2
- package/dist/lib/providers/googleVertex.js +11 -2
- package/dist/lib/providers/huggingFace.js +1 -1
- package/dist/lib/providers/litellm.js +1 -1
- package/dist/lib/providers/mistral.js +1 -1
- package/dist/lib/providers/openAI.js +46 -3
- package/dist/lib/providers/sagemaker/adaptive-semaphore.d.ts +1 -13
- package/dist/lib/providers/sagemaker/client.d.ts +1 -1
- package/dist/lib/providers/sagemaker/config.d.ts +1 -1
- package/dist/lib/providers/sagemaker/detection.d.ts +1 -1
- package/dist/lib/providers/sagemaker/errors.d.ts +1 -1
- package/dist/lib/providers/sagemaker/index.d.ts +1 -1
- package/dist/lib/providers/sagemaker/language-model.d.ts +1 -1
- package/dist/lib/providers/sagemaker/parsers.d.ts +1 -1
- package/dist/lib/providers/sagemaker/streaming.d.ts +1 -1
- package/dist/lib/providers/sagemaker/structured-parser.d.ts +1 -1
- package/dist/lib/session/globalSessionState.d.ts +26 -0
- package/dist/lib/session/globalSessionState.js +49 -0
- package/dist/lib/types/cli.d.ts +28 -0
- package/dist/lib/types/content.d.ts +18 -5
- package/dist/lib/types/contextTypes.d.ts +1 -1
- package/dist/lib/types/conversation.d.ts +55 -4
- package/dist/lib/types/fileTypes.d.ts +65 -0
- package/dist/lib/types/fileTypes.js +4 -0
- package/dist/lib/types/generateTypes.d.ts +12 -0
- package/dist/lib/types/guardrails.d.ts +103 -0
- package/dist/lib/types/guardrails.js +1 -0
- package/dist/lib/types/index.d.ts +4 -2
- package/dist/lib/types/index.js +4 -0
- package/dist/lib/types/mcpTypes.d.ts +407 -14
- package/dist/lib/types/providers.d.ts +469 -0
- package/dist/lib/types/streamTypes.d.ts +7 -0
- package/dist/lib/types/tools.d.ts +132 -35
- package/dist/lib/utils/csvProcessor.d.ts +68 -0
- package/dist/lib/utils/csvProcessor.js +277 -0
- package/dist/lib/utils/fileDetector.d.ts +57 -0
- package/dist/lib/utils/fileDetector.js +457 -0
- package/dist/lib/utils/imageProcessor.d.ts +10 -0
- package/dist/lib/utils/imageProcessor.js +22 -0
- package/dist/lib/utils/loopUtils.d.ts +71 -0
- package/dist/lib/utils/loopUtils.js +262 -0
- package/dist/lib/utils/messageBuilder.d.ts +2 -1
- package/dist/lib/utils/messageBuilder.js +197 -2
- package/dist/lib/utils/optionsUtils.d.ts +1 -1
- package/dist/mcp/factory.d.ts +2 -157
- package/dist/mcp/flexibleToolValidator.d.ts +1 -5
- package/dist/mcp/index.d.ts +3 -2
- package/dist/mcp/mcpCircuitBreaker.d.ts +1 -75
- package/dist/mcp/mcpClientFactory.d.ts +1 -20
- package/dist/mcp/mcpClientFactory.js +1 -0
- package/dist/mcp/registry.d.ts +3 -10
- package/dist/mcp/servers/agent/directToolsServer.d.ts +1 -1
- package/dist/mcp/servers/aiProviders/aiCoreServer.d.ts +1 -1
- package/dist/mcp/servers/utilities/utilityServer.d.ts +1 -1
- package/dist/mcp/toolDiscoveryService.d.ts +3 -84
- package/dist/mcp/toolRegistry.d.ts +2 -24
- package/dist/middleware/builtin/guardrails.d.ts +5 -16
- package/dist/middleware/builtin/guardrails.js +44 -39
- package/dist/middleware/utils/guardrailsUtils.d.ts +64 -0
- package/dist/middleware/utils/guardrailsUtils.js +387 -0
- package/dist/neurolink.d.ts +1 -1
- package/dist/providers/anthropic.js +46 -3
- package/dist/providers/azureOpenai.js +8 -2
- package/dist/providers/googleAiStudio.js +8 -2
- package/dist/providers/googleVertex.js +11 -2
- package/dist/providers/huggingFace.js +1 -1
- package/dist/providers/litellm.js +1 -1
- package/dist/providers/mistral.js +1 -1
- package/dist/providers/openAI.js +46 -3
- package/dist/providers/sagemaker/adaptive-semaphore.d.ts +1 -13
- package/dist/providers/sagemaker/client.d.ts +1 -1
- package/dist/providers/sagemaker/config.d.ts +1 -1
- package/dist/providers/sagemaker/detection.d.ts +1 -1
- package/dist/providers/sagemaker/errors.d.ts +1 -1
- package/dist/providers/sagemaker/index.d.ts +1 -1
- package/dist/providers/sagemaker/language-model.d.ts +3 -3
- package/dist/providers/sagemaker/parsers.d.ts +1 -1
- package/dist/providers/sagemaker/streaming.d.ts +1 -1
- package/dist/providers/sagemaker/structured-parser.d.ts +1 -1
- package/dist/session/globalSessionState.d.ts +26 -0
- package/dist/session/globalSessionState.js +49 -0
- package/dist/types/cli.d.ts +28 -0
- package/dist/types/content.d.ts +18 -5
- package/dist/types/contextTypes.d.ts +1 -1
- package/dist/types/conversation.d.ts +55 -4
- package/dist/types/fileTypes.d.ts +65 -0
- package/dist/types/fileTypes.js +4 -0
- package/dist/types/generateTypes.d.ts +12 -0
- package/dist/types/guardrails.d.ts +103 -0
- package/dist/types/guardrails.js +1 -0
- package/dist/types/index.d.ts +4 -2
- package/dist/types/index.js +4 -0
- package/dist/types/mcpTypes.d.ts +407 -14
- package/dist/types/modelTypes.d.ts +6 -6
- package/dist/types/providers.d.ts +469 -0
- package/dist/types/streamTypes.d.ts +7 -0
- package/dist/types/tools.d.ts +132 -35
- package/dist/utils/csvProcessor.d.ts +68 -0
- package/dist/utils/csvProcessor.js +277 -0
- package/dist/utils/fileDetector.d.ts +57 -0
- package/dist/utils/fileDetector.js +457 -0
- package/dist/utils/imageProcessor.d.ts +10 -0
- package/dist/utils/imageProcessor.js +22 -0
- package/dist/utils/loopUtils.d.ts +71 -0
- package/dist/utils/loopUtils.js +262 -0
- package/dist/utils/messageBuilder.d.ts +2 -1
- package/dist/utils/messageBuilder.js +197 -2
- package/dist/utils/optionsUtils.d.ts +1 -1
- package/package.json +9 -3
- package/dist/lib/mcp/contracts/mcpContract.d.ts +0 -106
- package/dist/lib/mcp/contracts/mcpContract.js +0 -5
- package/dist/lib/providers/sagemaker/types.d.ts +0 -456
- package/dist/lib/providers/sagemaker/types.js +0 -7
- package/dist/mcp/contracts/mcpContract.d.ts +0 -106
- package/dist/mcp/contracts/mcpContract.js +0 -5
- package/dist/providers/sagemaker/types.d.ts +0 -456
- package/dist/providers/sagemaker/types.js +0 -7
|
@@ -1,22 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MCP Registry - Industry Standard Interface with camelCase
|
|
3
3
|
*/
|
|
4
|
-
import type { DiscoveredMcp,
|
|
4
|
+
import type { DiscoveredMcp, McpRegistry } from "../types/mcpTypes.js";
|
|
5
|
+
import type { ToolInfo, ExecutionContext } from "../types/tools.js";
|
|
5
6
|
import type { UnknownRecord } from "../types/common.js";
|
|
6
|
-
/**
|
|
7
|
-
* MCP Registry interface with optional methods for maximum flexibility
|
|
8
|
-
*/
|
|
9
|
-
export interface McpRegistry {
|
|
10
|
-
registerServer?(serverId: string, serverConfig?: unknown, context?: ExecutionContext): Promise<void>;
|
|
11
|
-
executeTool?<T = unknown>(toolName: string, args?: unknown, context?: ExecutionContext): Promise<T>;
|
|
12
|
-
listTools?(context?: ExecutionContext): Promise<ToolInfo[]>;
|
|
13
|
-
}
|
|
14
7
|
/**
|
|
15
8
|
* Simple MCP registry for plugin management
|
|
16
9
|
* Maintains backward compatibility with existing code
|
|
17
10
|
*/
|
|
18
11
|
export declare class MCPRegistry implements McpRegistry {
|
|
19
|
-
plugins: Map<string, DiscoveredMcp
|
|
12
|
+
plugins: Map<string, DiscoveredMcp>;
|
|
20
13
|
/**
|
|
21
14
|
* Register a plugin
|
|
22
15
|
*/
|
|
@@ -7,4 +7,4 @@
|
|
|
7
7
|
* AI Core Server - Central hub for AI provider management
|
|
8
8
|
* Provides provider selection and status checking functionality
|
|
9
9
|
*/
|
|
10
|
-
export declare const aiCoreServer: import("
|
|
10
|
+
export declare const aiCoreServer: import("../../../types/mcpTypes.js").NeuroLinkMCPServer;
|
|
@@ -5,90 +5,9 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { EventEmitter } from "events";
|
|
7
7
|
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
8
|
-
import type { ExternalMCPToolInfo, ExternalMCPToolResult
|
|
8
|
+
import type { ExternalMCPToolInfo, ExternalMCPToolResult } from "../types/externalMcp.js";
|
|
9
|
+
import type { ToolDiscoveryResult, ExternalToolExecutionOptions } from "../types/mcpTypes.js";
|
|
9
10
|
import type { JsonObject } from "../types/common.js";
|
|
10
|
-
/**
|
|
11
|
-
* Tool discovery result
|
|
12
|
-
*/
|
|
13
|
-
export interface ToolDiscoveryResult {
|
|
14
|
-
/** Whether discovery was successful */
|
|
15
|
-
success: boolean;
|
|
16
|
-
/** Number of tools discovered */
|
|
17
|
-
toolCount: number;
|
|
18
|
-
/** Discovered tools */
|
|
19
|
-
tools: ExternalMCPToolInfo[];
|
|
20
|
-
/** Error message if failed */
|
|
21
|
-
error?: string;
|
|
22
|
-
/** Discovery duration in milliseconds */
|
|
23
|
-
duration: number;
|
|
24
|
-
/** Server ID */
|
|
25
|
-
serverId: string;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Tool execution options
|
|
29
|
-
*/
|
|
30
|
-
export interface ToolExecutionOptions {
|
|
31
|
-
/** Execution timeout in milliseconds */
|
|
32
|
-
timeout?: number;
|
|
33
|
-
/** Additional context for execution */
|
|
34
|
-
context?: Partial<ExternalMCPToolContext>;
|
|
35
|
-
/** Whether to validate input parameters */
|
|
36
|
-
validateInput?: boolean;
|
|
37
|
-
/** Whether to validate output */
|
|
38
|
-
validateOutput?: boolean;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Tool validation result
|
|
42
|
-
*/
|
|
43
|
-
export interface ToolValidationResult {
|
|
44
|
-
/** Whether the tool is valid */
|
|
45
|
-
isValid: boolean;
|
|
46
|
-
/** Validation errors */
|
|
47
|
-
errors: string[];
|
|
48
|
-
/** Validation warnings */
|
|
49
|
-
warnings: string[];
|
|
50
|
-
/** Tool metadata */
|
|
51
|
-
metadata?: {
|
|
52
|
-
category?: string;
|
|
53
|
-
complexity?: "simple" | "moderate" | "complex";
|
|
54
|
-
requiresAuth?: boolean;
|
|
55
|
-
isDeprecated?: boolean;
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Tool registry events
|
|
60
|
-
*/
|
|
61
|
-
export interface ToolRegistryEvents {
|
|
62
|
-
toolRegistered: {
|
|
63
|
-
serverId: string;
|
|
64
|
-
toolName: string;
|
|
65
|
-
toolInfo: ExternalMCPToolInfo;
|
|
66
|
-
timestamp: Date;
|
|
67
|
-
};
|
|
68
|
-
toolUnregistered: {
|
|
69
|
-
serverId: string;
|
|
70
|
-
toolName: string;
|
|
71
|
-
timestamp: Date;
|
|
72
|
-
};
|
|
73
|
-
toolUpdated: {
|
|
74
|
-
serverId: string;
|
|
75
|
-
toolName: string;
|
|
76
|
-
oldInfo: ExternalMCPToolInfo;
|
|
77
|
-
newInfo: ExternalMCPToolInfo;
|
|
78
|
-
timestamp: Date;
|
|
79
|
-
};
|
|
80
|
-
discoveryCompleted: {
|
|
81
|
-
serverId: string;
|
|
82
|
-
toolCount: number;
|
|
83
|
-
duration: number;
|
|
84
|
-
timestamp: Date;
|
|
85
|
-
};
|
|
86
|
-
discoveryFailed: {
|
|
87
|
-
serverId: string;
|
|
88
|
-
error: string;
|
|
89
|
-
timestamp: Date;
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
11
|
/**
|
|
93
12
|
* ToolDiscoveryService
|
|
94
13
|
* Handles automatic tool discovery and registration from external MCP servers
|
|
@@ -134,7 +53,7 @@ export declare class ToolDiscoveryService extends EventEmitter {
|
|
|
134
53
|
/**
|
|
135
54
|
* Execute a tool
|
|
136
55
|
*/
|
|
137
|
-
executeTool(toolName: string, serverId: string, client: Client, parameters: JsonObject, options?:
|
|
56
|
+
executeTool(toolName: string, serverId: string, client: Client, parameters: JsonObject, options?: ExternalToolExecutionOptions): Promise<ExternalMCPToolResult>;
|
|
138
57
|
/**
|
|
139
58
|
* Validate tool parameters
|
|
140
59
|
*/
|
|
@@ -2,32 +2,10 @@
|
|
|
2
2
|
* MCP Tool Registry - Extended Registry with Tool Management
|
|
3
3
|
* Updated to match industry standard camelCase interfaces
|
|
4
4
|
*/
|
|
5
|
-
import type { ExecutionContext, ToolInfo } from "./contracts/mcpContract.js";
|
|
6
|
-
import type { ToolResult } from "./factory.js";
|
|
7
5
|
import type { MCPServerInfo } from "../types/mcpTypes.js";
|
|
6
|
+
import type { ToolImplementation, ToolInfo, ExecutionContext } from "../types/tools.js";
|
|
8
7
|
import { MCPRegistry } from "./registry.js";
|
|
9
8
|
import type { HITLManager } from "../hitl/hitlManager.js";
|
|
10
|
-
interface ToolImplementation {
|
|
11
|
-
execute: (params: unknown, context?: ExecutionContext) => Promise<unknown> | unknown;
|
|
12
|
-
description?: string;
|
|
13
|
-
inputSchema?: unknown;
|
|
14
|
-
outputSchema?: unknown;
|
|
15
|
-
category?: string;
|
|
16
|
-
permissions?: string[];
|
|
17
|
-
}
|
|
18
|
-
export type ToolExecutionResult = ToolResult;
|
|
19
|
-
/**
|
|
20
|
-
* Tool execution options
|
|
21
|
-
*/
|
|
22
|
-
export interface ToolExecutionOptions {
|
|
23
|
-
timeout?: number;
|
|
24
|
-
retries?: number;
|
|
25
|
-
context?: ExecutionContext;
|
|
26
|
-
preferredSource?: string;
|
|
27
|
-
fallbackEnabled?: boolean;
|
|
28
|
-
validateBeforeExecution?: boolean;
|
|
29
|
-
timeoutMs?: number;
|
|
30
|
-
}
|
|
31
9
|
export declare class MCPToolRegistry extends MCPRegistry {
|
|
32
10
|
private tools;
|
|
33
11
|
private toolImplementations;
|
|
@@ -163,4 +141,4 @@ export declare class MCPToolRegistry extends MCPRegistry {
|
|
|
163
141
|
}
|
|
164
142
|
export declare const toolRegistry: MCPToolRegistry;
|
|
165
143
|
export declare const defaultToolRegistry: MCPToolRegistry;
|
|
166
|
-
export type { ToolInfo } from "
|
|
144
|
+
export type { ToolInfo } from "../types/tools.js";
|
|
@@ -1,20 +1,9 @@
|
|
|
1
|
-
import type { LanguageModelV1 } from "ai";
|
|
2
1
|
import type { NeuroLinkMiddleware } from "../../types/middlewareTypes.js";
|
|
2
|
+
import type { GuardrailsMiddlewareConfig } from "../../types/guardrails.js";
|
|
3
|
+
export type { GuardrailsMiddlewareConfig } from "../../types/guardrails.js";
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
badWords?: {
|
|
8
|
-
enabled?: boolean;
|
|
9
|
-
list?: string[];
|
|
10
|
-
};
|
|
11
|
-
modelFilter?: {
|
|
12
|
-
enabled?: boolean;
|
|
13
|
-
filterModel?: LanguageModelV1;
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Create Guardrails AI middleware for content filtering and policy enforcement.
|
|
18
|
-
* @param config - Configuration for the guardrails middleware.
|
|
5
|
+
* Create Guardrails AI middleware for content filtering and policy enforcement
|
|
6
|
+
* @param config Configuration for the guardrails middleware
|
|
7
|
+
* @returns NeuroLink middleware instance
|
|
19
8
|
*/
|
|
20
9
|
export declare function createGuardrailsMiddleware(config?: GuardrailsMiddlewareConfig): NeuroLinkMiddleware;
|
|
@@ -1,43 +1,48 @@
|
|
|
1
1
|
import { generateText } from "ai";
|
|
2
|
+
import { createBlockedResponse, createBlockedStream, applyContentFiltering, handlePrecallGuardrails, } from "../utils/guardrailsUtils.js";
|
|
2
3
|
import { logger } from "../../utils/logger.js";
|
|
3
4
|
/**
|
|
4
|
-
* Create Guardrails AI middleware for content filtering and policy enforcement
|
|
5
|
-
* @param config
|
|
5
|
+
* Create Guardrails AI middleware for content filtering and policy enforcement
|
|
6
|
+
* @param config Configuration for the guardrails middleware
|
|
7
|
+
* @returns NeuroLink middleware instance
|
|
6
8
|
*/
|
|
7
9
|
export function createGuardrailsMiddleware(config = {}) {
|
|
8
10
|
const metadata = {
|
|
9
11
|
id: "guardrails",
|
|
10
12
|
name: "Guardrails AI",
|
|
11
|
-
description: "Provides content filtering and policy enforcement using custom rules and
|
|
13
|
+
description: "Provides comprehensive content filtering and policy enforcement using custom rules, AI models, and precall evaluation to filter inappropriate content before it reaches the LLM.",
|
|
12
14
|
priority: 90,
|
|
13
15
|
defaultEnabled: true,
|
|
14
16
|
};
|
|
17
|
+
// WeakMap to store blocking state from transformParams to wrap methods
|
|
18
|
+
const blockingState = new WeakMap();
|
|
15
19
|
const middleware = {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
transformParams: async ({ params }) => {
|
|
21
|
+
if (config.precallEvaluation?.enabled) {
|
|
22
|
+
const { shouldBlock, transformedParams } = await handlePrecallGuardrails(params, config.precallEvaluation);
|
|
23
|
+
// Store the blocking state for use in wrap methods
|
|
24
|
+
blockingState.set(transformedParams, shouldBlock);
|
|
25
|
+
return transformedParams;
|
|
26
|
+
}
|
|
27
|
+
return params;
|
|
28
|
+
},
|
|
29
|
+
wrapGenerate: async ({ doGenerate, params }) => {
|
|
30
|
+
logger.debug(`[GuardrailsMiddleware] Applying to generate call.`);
|
|
31
|
+
// Check if this request should be blocked (set by transformParams)
|
|
32
|
+
if (config.precallEvaluation?.enabled && blockingState.get(params)) {
|
|
33
|
+
return createBlockedResponse();
|
|
34
|
+
}
|
|
21
35
|
let result = await doGenerate();
|
|
22
|
-
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
for (const term of config.badWords.list) {
|
|
26
|
-
const regex = new RegExp(term, "gi");
|
|
27
|
-
filteredText = filteredText?.replace(regex, "*".repeat(term.length));
|
|
28
|
-
}
|
|
29
|
-
if (result.text !== filteredText) {
|
|
30
|
-
logger.debug(`[GuardrailsMiddleware] Redacted bad words from text.`);
|
|
31
|
-
result = { ...result, text: filteredText ?? "" };
|
|
32
|
-
}
|
|
36
|
+
const filterResult = applyContentFiltering(result.text || "", config.badWords, "generate");
|
|
37
|
+
if (filterResult.hasChanges) {
|
|
38
|
+
result = { ...result, text: filterResult.filteredText };
|
|
33
39
|
}
|
|
34
|
-
// 2. Model-based Filtering
|
|
35
40
|
if (config.modelFilter?.enabled && config.modelFilter.filterModel) {
|
|
36
41
|
logger.debug(`[GuardrailsMiddleware] Invoking model-based filter.`);
|
|
37
42
|
try {
|
|
38
43
|
const filterPrompt = `Is the following text safe? Respond with only "safe" or "unsafe".\n\nText: "${result.text}"`;
|
|
39
44
|
const { text: filterResponse } = await generateText({
|
|
40
|
-
model: config.modelFilter.filterModel,
|
|
45
|
+
model: config.modelFilter.filterModel,
|
|
41
46
|
prompt: filterPrompt,
|
|
42
47
|
});
|
|
43
48
|
if (filterResponse.toLowerCase().trim() === "unsafe") {
|
|
@@ -53,28 +58,28 @@ export function createGuardrailsMiddleware(config = {}) {
|
|
|
53
58
|
}
|
|
54
59
|
return result;
|
|
55
60
|
},
|
|
56
|
-
wrapStream: async ({ doStream, params
|
|
57
|
-
logger.debug(`[GuardrailsMiddleware] Applying to stream call
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
wrapStream: async ({ doStream, params }) => {
|
|
62
|
+
logger.debug(`[GuardrailsMiddleware] Applying to stream call.`);
|
|
63
|
+
// Check if this request should be blocked (set by transformParams)
|
|
64
|
+
if (config.precallEvaluation?.enabled && blockingState.get(params)) {
|
|
65
|
+
return {
|
|
66
|
+
stream: createBlockedStream(),
|
|
67
|
+
rawCall: { rawPrompt: null, rawSettings: {} },
|
|
68
|
+
warnings: [],
|
|
69
|
+
};
|
|
64
70
|
}
|
|
71
|
+
const { stream, ...rest } = await doStream();
|
|
65
72
|
const transformStream = new TransformStream({
|
|
66
73
|
transform(chunk, controller) {
|
|
67
74
|
let filteredChunk = chunk;
|
|
68
|
-
if (
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
filteredChunk
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
};
|
|
77
|
-
}
|
|
75
|
+
if (typeof filteredChunk === "object" &&
|
|
76
|
+
"textDelta" in filteredChunk) {
|
|
77
|
+
const filterResult = applyContentFiltering(filteredChunk.textDelta, config.badWords, "stream");
|
|
78
|
+
if (filterResult.hasChanges) {
|
|
79
|
+
filteredChunk = {
|
|
80
|
+
...filteredChunk,
|
|
81
|
+
textDelta: filterResult.filteredText,
|
|
82
|
+
};
|
|
78
83
|
}
|
|
79
84
|
}
|
|
80
85
|
controller.enqueue(filteredChunk);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { PrecallEvaluationResult, PrecallEvaluationConfig, EvaluationActionResult, BadWordsConfig } from "../../types/guardrails.js";
|
|
2
|
+
import type { LanguageModelV1CallOptions } from "ai";
|
|
3
|
+
export declare const DEFAULT_EVALUATION_PROMPT = "\nEvaluate the following user input for safety and appropriateness. Respond ONLY with valid JSON:\n\n{\n \"overall\": \"safe|unsafe|suspicious|inappropriate\",\n \"safetyScore\": <1-10>,\n \"appropriatenessScore\": <1-10>,\n \"confidenceLevel\": <1-10>,\n \"issues\": [\n {\n \"category\": \"explicit_content|hate_speech|violence|personal_info|spam|other\",\n \"severity\": \"low|medium|high|critical\",\n \"description\": \"Brief description\"\n }\n ],\n \"suggestedAction\": \"allow|block|sanitize|warn\",\n \"reasoning\": \"Brief explanation\"\n}\n\nUser Input: \"{USER_INPUT}\"\n";
|
|
4
|
+
/**
|
|
5
|
+
* Extract user input from LanguageModelV1CallOptions
|
|
6
|
+
*/
|
|
7
|
+
export declare function extractUserInput(params: LanguageModelV1CallOptions): string;
|
|
8
|
+
export declare function parseEvaluationResponse(rawResponse: string): PrecallEvaluationResult;
|
|
9
|
+
/**
|
|
10
|
+
* Handles the precall guardrails logic, including evaluation and sanitization.
|
|
11
|
+
* @param params - The language model call options.
|
|
12
|
+
* @param config - The precall evaluation configuration.
|
|
13
|
+
* @returns An object indicating if the request should be blocked and the (potentially transformed) params.
|
|
14
|
+
*/
|
|
15
|
+
export declare function handlePrecallGuardrails(params: LanguageModelV1CallOptions, config: PrecallEvaluationConfig): Promise<{
|
|
16
|
+
shouldBlock: boolean;
|
|
17
|
+
transformedParams: LanguageModelV1CallOptions;
|
|
18
|
+
}>;
|
|
19
|
+
/**
|
|
20
|
+
* Perform precall evaluation of user input using AI models
|
|
21
|
+
*/
|
|
22
|
+
export declare function performPrecallEvaluation(config: PrecallEvaluationConfig, userInput: string): Promise<PrecallEvaluationResult>;
|
|
23
|
+
export declare function applyEvaluationActions(evaluation: PrecallEvaluationResult, config: PrecallEvaluationConfig, userInput: string): EvaluationActionResult;
|
|
24
|
+
/**
|
|
25
|
+
* Apply parameter sanitization to request parameters
|
|
26
|
+
*/
|
|
27
|
+
export declare function applySanitization(params: LanguageModelV1CallOptions, sanitizedInput: string): LanguageModelV1CallOptions;
|
|
28
|
+
export declare function escapeRegExp(string: string): string;
|
|
29
|
+
export declare function createBlockedResponse(): {
|
|
30
|
+
text: string;
|
|
31
|
+
usage: {
|
|
32
|
+
promptTokens: number;
|
|
33
|
+
completionTokens: number;
|
|
34
|
+
};
|
|
35
|
+
finishReason: "stop";
|
|
36
|
+
warnings: never[];
|
|
37
|
+
rawCall: {
|
|
38
|
+
rawPrompt: null;
|
|
39
|
+
rawSettings: {};
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export declare function createBlockedStream(): ReadableStream<any>;
|
|
43
|
+
/**
|
|
44
|
+
* Result from content filtering operation
|
|
45
|
+
*/
|
|
46
|
+
export interface ContentFilteringResult {
|
|
47
|
+
filteredText: string;
|
|
48
|
+
hasChanges: boolean;
|
|
49
|
+
appliedFilters: string[];
|
|
50
|
+
filteringStats: {
|
|
51
|
+
regexPatternsApplied: number;
|
|
52
|
+
stringFiltersApplied: number;
|
|
53
|
+
totalMatches: number;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Apply content filtering using bad words configuration
|
|
58
|
+
* Handles both regex patterns and string lists with proper priority
|
|
59
|
+
* @param text The text to filter
|
|
60
|
+
* @param badWordsConfig Bad words configuration
|
|
61
|
+
* @param context Optional context for logging (e.g., "generate", "stream")
|
|
62
|
+
* @returns Filtering result with filtered text and metadata
|
|
63
|
+
*/
|
|
64
|
+
export declare function applyContentFiltering(text: string, badWordsConfig?: BadWordsConfig, context?: string): ContentFilteringResult;
|