@juspay/neurolink 3.0.0 → 4.0.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 +62 -4
- package/README.md +235 -2
- package/dist/agent/direct-tools.d.ts +6 -6
- package/dist/chat/client-utils.d.ts +92 -0
- package/dist/chat/client-utils.js +298 -0
- package/dist/chat/index.d.ts +27 -0
- package/dist/chat/index.js +41 -0
- package/dist/chat/session-storage.d.ts +77 -0
- package/dist/chat/session-storage.js +233 -0
- package/dist/chat/session.d.ts +95 -0
- package/dist/chat/session.js +257 -0
- package/dist/chat/sse-handler.d.ts +49 -0
- package/dist/chat/sse-handler.js +266 -0
- package/dist/chat/types.d.ts +73 -0
- package/dist/chat/types.js +5 -0
- package/dist/chat/websocket-chat-handler.d.ts +36 -0
- package/dist/chat/websocket-chat-handler.js +262 -0
- package/dist/cli/commands/config.js +12 -12
- package/dist/cli/commands/mcp.js +3 -4
- package/dist/cli/index.d.ts +0 -7
- package/dist/cli/index.js +256 -27
- package/dist/config/configManager.d.ts +60 -0
- package/dist/config/configManager.js +300 -0
- package/dist/config/types.d.ts +136 -0
- package/dist/config/types.js +43 -0
- package/dist/core/analytics.d.ts +23 -0
- package/dist/core/analytics.js +131 -0
- package/dist/core/constants.d.ts +41 -0
- package/dist/core/constants.js +50 -0
- package/dist/core/defaults.d.ts +18 -0
- package/dist/core/defaults.js +29 -0
- package/dist/core/evaluation-config.d.ts +29 -0
- package/dist/core/evaluation-config.js +144 -0
- package/dist/core/evaluation-providers.d.ts +30 -0
- package/dist/core/evaluation-providers.js +187 -0
- package/dist/core/evaluation.d.ts +117 -0
- package/dist/core/evaluation.js +528 -0
- package/dist/core/factory.js +33 -25
- package/dist/core/types.d.ts +165 -6
- package/dist/core/types.js +3 -4
- package/dist/index.d.ts +9 -4
- package/dist/index.js +25 -4
- package/dist/lib/agent/direct-tools.d.ts +6 -6
- package/dist/lib/chat/client-utils.d.ts +92 -0
- package/dist/lib/chat/client-utils.js +298 -0
- package/dist/lib/chat/index.d.ts +27 -0
- package/dist/lib/chat/index.js +41 -0
- package/dist/lib/chat/session-storage.d.ts +77 -0
- package/dist/lib/chat/session-storage.js +233 -0
- package/dist/lib/chat/session.d.ts +95 -0
- package/dist/lib/chat/session.js +257 -0
- package/dist/lib/chat/sse-handler.d.ts +49 -0
- package/dist/lib/chat/sse-handler.js +266 -0
- package/dist/lib/chat/types.d.ts +73 -0
- package/dist/lib/chat/types.js +5 -0
- package/dist/lib/chat/websocket-chat-handler.d.ts +36 -0
- package/dist/lib/chat/websocket-chat-handler.js +262 -0
- package/dist/lib/config/configManager.d.ts +60 -0
- package/dist/lib/config/configManager.js +300 -0
- package/dist/lib/config/types.d.ts +136 -0
- package/dist/lib/config/types.js +43 -0
- package/dist/lib/core/analytics.d.ts +23 -0
- package/dist/lib/core/analytics.js +131 -0
- package/dist/lib/core/constants.d.ts +41 -0
- package/dist/lib/core/constants.js +50 -0
- package/dist/lib/core/defaults.d.ts +18 -0
- package/dist/lib/core/defaults.js +29 -0
- package/dist/lib/core/evaluation-config.d.ts +29 -0
- package/dist/lib/core/evaluation-config.js +144 -0
- package/dist/lib/core/evaluation-providers.d.ts +30 -0
- package/dist/lib/core/evaluation-providers.js +187 -0
- package/dist/lib/core/evaluation.d.ts +117 -0
- package/dist/lib/core/evaluation.js +528 -0
- package/dist/lib/core/factory.js +33 -26
- package/dist/lib/core/types.d.ts +165 -6
- package/dist/lib/core/types.js +3 -4
- package/dist/lib/index.d.ts +9 -4
- package/dist/lib/index.js +25 -4
- package/dist/lib/mcp/contracts/mcpContract.d.ts +118 -0
- package/dist/lib/mcp/contracts/mcpContract.js +5 -0
- package/dist/lib/mcp/function-calling.js +11 -3
- package/dist/lib/mcp/logging.js +5 -0
- package/dist/lib/mcp/neurolink-mcp-client.js +2 -1
- package/dist/lib/mcp/orchestrator.js +18 -9
- package/dist/lib/mcp/registry.d.ts +49 -16
- package/dist/lib/mcp/registry.js +80 -6
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.js +5 -4
- package/dist/lib/mcp/tool-integration.js +1 -1
- package/dist/lib/mcp/tool-registry.d.ts +55 -34
- package/dist/lib/mcp/tool-registry.js +111 -97
- package/dist/lib/mcp/unified-mcp.js +6 -1
- package/dist/lib/mcp/unified-registry.d.ts +12 -4
- package/dist/lib/mcp/unified-registry.js +17 -4
- package/dist/lib/neurolink.d.ts +28 -0
- package/dist/lib/neurolink.js +48 -4
- package/dist/lib/providers/agent-enhanced-provider.d.ts +11 -2
- package/dist/lib/providers/agent-enhanced-provider.js +86 -15
- package/dist/lib/providers/amazonBedrock.d.ts +9 -1
- package/dist/lib/providers/amazonBedrock.js +26 -2
- package/dist/lib/providers/analytics-helper.d.ts +53 -0
- package/dist/lib/providers/analytics-helper.js +151 -0
- package/dist/lib/providers/anthropic.d.ts +11 -1
- package/dist/lib/providers/anthropic.js +29 -4
- package/dist/lib/providers/azureOpenAI.d.ts +3 -1
- package/dist/lib/providers/azureOpenAI.js +28 -4
- package/dist/lib/providers/function-calling-provider.d.ts +9 -1
- package/dist/lib/providers/function-calling-provider.js +14 -1
- package/dist/lib/providers/googleAIStudio.d.ts +15 -1
- package/dist/lib/providers/googleAIStudio.js +32 -2
- package/dist/lib/providers/googleVertexAI.d.ts +9 -1
- package/dist/lib/providers/googleVertexAI.js +31 -2
- package/dist/lib/providers/huggingFace.d.ts +3 -1
- package/dist/lib/providers/huggingFace.js +26 -3
- package/dist/lib/providers/mcp-provider.d.ts +9 -1
- package/dist/lib/providers/mcp-provider.js +12 -0
- package/dist/lib/providers/mistralAI.d.ts +3 -1
- package/dist/lib/providers/mistralAI.js +25 -2
- package/dist/lib/providers/ollama.d.ts +3 -1
- package/dist/lib/providers/ollama.js +27 -4
- package/dist/lib/providers/openAI.d.ts +15 -1
- package/dist/lib/providers/openAI.js +32 -2
- package/dist/lib/proxy/proxy-fetch.js +8 -7
- package/dist/lib/services/streaming/streaming-manager.d.ts +29 -0
- package/dist/lib/services/streaming/streaming-manager.js +244 -0
- package/dist/lib/services/types.d.ts +155 -0
- package/dist/lib/services/types.js +2 -0
- package/dist/lib/services/websocket/websocket-server.d.ts +34 -0
- package/dist/lib/services/websocket/websocket-server.js +304 -0
- package/dist/lib/telemetry/index.d.ts +15 -0
- package/dist/lib/telemetry/index.js +22 -0
- package/dist/lib/telemetry/telemetry-service.d.ts +47 -0
- package/dist/lib/telemetry/telemetry-service.js +259 -0
- package/dist/lib/utils/streaming-utils.d.ts +67 -0
- package/dist/lib/utils/streaming-utils.js +201 -0
- package/dist/mcp/contracts/mcpContract.d.ts +118 -0
- package/dist/mcp/contracts/mcpContract.js +5 -0
- package/dist/mcp/function-calling.js +11 -3
- package/dist/mcp/logging.js +5 -0
- package/dist/mcp/neurolink-mcp-client.js +2 -1
- package/dist/mcp/orchestrator.js +18 -9
- package/dist/mcp/registry.d.ts +49 -16
- package/dist/mcp/registry.js +80 -6
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.js +5 -4
- package/dist/mcp/tool-integration.js +1 -1
- package/dist/mcp/tool-registry.d.ts +55 -34
- package/dist/mcp/tool-registry.js +111 -97
- package/dist/mcp/unified-mcp.js +6 -1
- package/dist/mcp/unified-registry.d.ts +12 -4
- package/dist/mcp/unified-registry.js +17 -4
- package/dist/neurolink.d.ts +28 -0
- package/dist/neurolink.js +48 -4
- package/dist/providers/agent-enhanced-provider.d.ts +11 -2
- package/dist/providers/agent-enhanced-provider.js +86 -15
- package/dist/providers/amazonBedrock.d.ts +9 -1
- package/dist/providers/amazonBedrock.js +26 -2
- package/dist/providers/analytics-helper.d.ts +53 -0
- package/dist/providers/analytics-helper.js +151 -0
- package/dist/providers/anthropic.d.ts +11 -1
- package/dist/providers/anthropic.js +29 -4
- package/dist/providers/azureOpenAI.d.ts +3 -1
- package/dist/providers/azureOpenAI.js +29 -4
- package/dist/providers/function-calling-provider.d.ts +9 -1
- package/dist/providers/function-calling-provider.js +14 -1
- package/dist/providers/googleAIStudio.d.ts +15 -1
- package/dist/providers/googleAIStudio.js +32 -2
- package/dist/providers/googleVertexAI.d.ts +9 -1
- package/dist/providers/googleVertexAI.js +31 -2
- package/dist/providers/huggingFace.d.ts +3 -1
- package/dist/providers/huggingFace.js +26 -3
- package/dist/providers/mcp-provider.d.ts +9 -1
- package/dist/providers/mcp-provider.js +12 -0
- package/dist/providers/mistralAI.d.ts +3 -1
- package/dist/providers/mistralAI.js +25 -2
- package/dist/providers/ollama.d.ts +3 -1
- package/dist/providers/ollama.js +27 -4
- package/dist/providers/openAI.d.ts +15 -1
- package/dist/providers/openAI.js +33 -2
- package/dist/proxy/proxy-fetch.js +8 -7
- package/dist/services/streaming/streaming-manager.d.ts +29 -0
- package/dist/services/streaming/streaming-manager.js +244 -0
- package/dist/services/types.d.ts +155 -0
- package/dist/services/types.js +2 -0
- package/dist/services/websocket/websocket-server.d.ts +34 -0
- package/dist/services/websocket/websocket-server.js +304 -0
- package/dist/telemetry/index.d.ts +15 -0
- package/dist/telemetry/index.js +22 -0
- package/dist/telemetry/telemetry-service.d.ts +47 -0
- package/dist/telemetry/telemetry-service.js +261 -0
- package/dist/utils/streaming-utils.d.ts +67 -0
- package/dist/utils/streaming-utils.js +201 -0
- package/package.json +18 -2
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 3: SSE Chat Utilities
|
|
3
|
+
* Type definitions for chat infrastructure
|
|
4
|
+
*/
|
|
5
|
+
export interface ChatMessage {
|
|
6
|
+
id: string;
|
|
7
|
+
role: "user" | "assistant" | "system";
|
|
8
|
+
content: string;
|
|
9
|
+
timestamp: number;
|
|
10
|
+
metadata?: {
|
|
11
|
+
provider?: string;
|
|
12
|
+
model?: string;
|
|
13
|
+
tokens?: number;
|
|
14
|
+
duration?: number;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export interface ChatRequest {
|
|
18
|
+
sessionId: string;
|
|
19
|
+
message: string;
|
|
20
|
+
role?: "user";
|
|
21
|
+
options?: {
|
|
22
|
+
temperature?: number;
|
|
23
|
+
maxTokens?: number;
|
|
24
|
+
systemPrompt?: string;
|
|
25
|
+
streamResponse?: boolean;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export interface SSEOptions {
|
|
29
|
+
maxConnections?: number;
|
|
30
|
+
heartbeatInterval?: number;
|
|
31
|
+
connectionTimeout?: number;
|
|
32
|
+
enableCors?: boolean;
|
|
33
|
+
corsOrigins?: string[];
|
|
34
|
+
}
|
|
35
|
+
export interface SessionOptions {
|
|
36
|
+
maxHistory?: number;
|
|
37
|
+
persistenceAdapter?: "memory" | "redis" | "file";
|
|
38
|
+
contextWindowSize?: number;
|
|
39
|
+
autoSave?: boolean;
|
|
40
|
+
ttl?: number;
|
|
41
|
+
}
|
|
42
|
+
export interface SSEEvent {
|
|
43
|
+
type: "data" | "error" | "progress" | "complete" | "heartbeat";
|
|
44
|
+
data: any;
|
|
45
|
+
id?: string;
|
|
46
|
+
retry?: number;
|
|
47
|
+
}
|
|
48
|
+
export interface ChatSessionState {
|
|
49
|
+
id: string;
|
|
50
|
+
messages: ChatMessage[];
|
|
51
|
+
createdAt: number;
|
|
52
|
+
lastActivity: number;
|
|
53
|
+
metadata?: Record<string, any>;
|
|
54
|
+
}
|
|
55
|
+
export interface StreamingChatResponse {
|
|
56
|
+
sessionId: string;
|
|
57
|
+
messageId: string;
|
|
58
|
+
stream: ReadableStream;
|
|
59
|
+
response: Response;
|
|
60
|
+
}
|
|
61
|
+
export interface SessionStorage {
|
|
62
|
+
get(sessionId: string): Promise<ChatSessionState | null>;
|
|
63
|
+
set(sessionId: string, state: ChatSessionState): Promise<void>;
|
|
64
|
+
delete(sessionId: string): Promise<void>;
|
|
65
|
+
list(): Promise<string[]>;
|
|
66
|
+
cleanup(maxAge: number): Promise<number>;
|
|
67
|
+
}
|
|
68
|
+
export interface ContextManager {
|
|
69
|
+
addMessage(sessionId: string, message: ChatMessage): Promise<void>;
|
|
70
|
+
getHistory(sessionId: string, limit?: number): Promise<ChatMessage[]>;
|
|
71
|
+
trimContext(sessionId: string, maxTokens: number): Promise<number>;
|
|
72
|
+
estimateTokens(messages: ChatMessage[]): number;
|
|
73
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { SSEChatHandler } from "./sse-handler.js";
|
|
2
|
+
import type { AIProvider } from "../core/types.js";
|
|
3
|
+
import type { WebSocketOptions, ChatRequest, GroupChatRequest, StreamingChatRequest } from "../services/types.js";
|
|
4
|
+
import type { SSEOptions } from "./types.js";
|
|
5
|
+
export interface WebSocketChatOptions {
|
|
6
|
+
sseOptions?: any;
|
|
7
|
+
wsOptions?: WebSocketOptions;
|
|
8
|
+
enableTypingIndicators?: boolean;
|
|
9
|
+
enablePresenceTracking?: boolean;
|
|
10
|
+
enableMessageSync?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare class WebSocketChatHandler extends SSEChatHandler {
|
|
13
|
+
private wsServer;
|
|
14
|
+
private streamingManager;
|
|
15
|
+
protected options: WebSocketChatOptions & Required<SSEOptions>;
|
|
16
|
+
private typingIndicators;
|
|
17
|
+
private presenceTracking;
|
|
18
|
+
constructor(provider: AIProvider, options?: WebSocketChatOptions);
|
|
19
|
+
private setupWebSocketHandlers;
|
|
20
|
+
handleWebSocketChatRequest(connectionId: string, request: ChatRequest): Promise<void>;
|
|
21
|
+
handleGroupChat(roomId: string, request: GroupChatRequest): Promise<void>;
|
|
22
|
+
handleStreamingChat(connectionId: string, request: StreamingChatRequest): Promise<void>;
|
|
23
|
+
enableTypingIndicators(roomId: string): Promise<void>;
|
|
24
|
+
enablePresenceTracking(roomId: string): Promise<void>;
|
|
25
|
+
enableMessageSynchronization(roomId: string): Promise<void>;
|
|
26
|
+
private sendToConnection;
|
|
27
|
+
private sendError;
|
|
28
|
+
private showTypingIndicator;
|
|
29
|
+
private clearTypingIndicator;
|
|
30
|
+
private updatePresence;
|
|
31
|
+
private handleWebSocketChatMessage;
|
|
32
|
+
getConnectionCount(): number;
|
|
33
|
+
getRoomCount(): number;
|
|
34
|
+
getStreamingMetrics(): import("../services/types.js").StreamingMetrics;
|
|
35
|
+
close(): void;
|
|
36
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { SSEChatHandler } from "./sse-handler.js";
|
|
2
|
+
import { NeuroLinkWebSocketServer } from "../services/websocket/websocket-server.js";
|
|
3
|
+
import { StreamingManager } from "../services/streaming/streaming-manager.js";
|
|
4
|
+
import { randomUUID } from "crypto";
|
|
5
|
+
export class WebSocketChatHandler extends SSEChatHandler {
|
|
6
|
+
wsServer;
|
|
7
|
+
streamingManager;
|
|
8
|
+
options;
|
|
9
|
+
typingIndicators = new Map();
|
|
10
|
+
presenceTracking = new Map();
|
|
11
|
+
constructor(provider, options = {}) {
|
|
12
|
+
super(provider, options.sseOptions);
|
|
13
|
+
this.options = {
|
|
14
|
+
enableTypingIndicators: true,
|
|
15
|
+
enablePresenceTracking: true,
|
|
16
|
+
enableMessageSync: true,
|
|
17
|
+
maxConnections: 100,
|
|
18
|
+
heartbeatInterval: 30000,
|
|
19
|
+
connectionTimeout: 300000,
|
|
20
|
+
enableCors: true,
|
|
21
|
+
corsOrigins: ["*"],
|
|
22
|
+
...options,
|
|
23
|
+
};
|
|
24
|
+
this.wsServer = new NeuroLinkWebSocketServer(options.wsOptions);
|
|
25
|
+
this.streamingManager = new StreamingManager();
|
|
26
|
+
this.setupWebSocketHandlers();
|
|
27
|
+
}
|
|
28
|
+
setupWebSocketHandlers() {
|
|
29
|
+
this.wsServer.on("connection", ({ connectionId }) => {
|
|
30
|
+
console.log(`[WebSocket Chat] New connection: ${connectionId}`);
|
|
31
|
+
if (this.options.enablePresenceTracking) {
|
|
32
|
+
this.updatePresence(connectionId, "online");
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
this.wsServer.on("disconnection", ({ connectionId }) => {
|
|
36
|
+
console.log(`[WebSocket Chat] Disconnection: ${connectionId}`);
|
|
37
|
+
if (this.options.enablePresenceTracking) {
|
|
38
|
+
this.updatePresence(connectionId, "offline");
|
|
39
|
+
}
|
|
40
|
+
this.clearTypingIndicator(connectionId);
|
|
41
|
+
});
|
|
42
|
+
this.wsServer.on("chat-message", ({ connectionId, message }) => {
|
|
43
|
+
this.handleWebSocketChatMessage(connectionId, message);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
// Enhanced Chat Capabilities
|
|
47
|
+
async handleWebSocketChatRequest(connectionId, request) {
|
|
48
|
+
try {
|
|
49
|
+
if (this.options.enableTypingIndicators) {
|
|
50
|
+
this.showTypingIndicator(connectionId, "assistant");
|
|
51
|
+
}
|
|
52
|
+
// Create streaming session for this chat
|
|
53
|
+
const streamingSession = await this.streamingManager.createStreamingSession({
|
|
54
|
+
provider: this.provider.constructor.name,
|
|
55
|
+
model: "default", // Should get from provider
|
|
56
|
+
streamingMode: "real-time",
|
|
57
|
+
compressionEnabled: true,
|
|
58
|
+
maxChunkSize: 1024,
|
|
59
|
+
bufferSize: 4096,
|
|
60
|
+
latencyTarget: 200,
|
|
61
|
+
});
|
|
62
|
+
// Generate AI response
|
|
63
|
+
const result = await this.provider.generateText({
|
|
64
|
+
prompt: request.prompt,
|
|
65
|
+
temperature: request.options?.temperature,
|
|
66
|
+
maxTokens: request.options?.maxTokens,
|
|
67
|
+
});
|
|
68
|
+
if (!result || !result.text) {
|
|
69
|
+
throw new Error("Invalid AI response");
|
|
70
|
+
}
|
|
71
|
+
// Send response via WebSocket
|
|
72
|
+
const responseMessage = {
|
|
73
|
+
id: randomUUID(),
|
|
74
|
+
type: "ai-response",
|
|
75
|
+
connectionId,
|
|
76
|
+
timestamp: Date.now(),
|
|
77
|
+
data: {
|
|
78
|
+
text: result.text,
|
|
79
|
+
sessionId: request.sessionId,
|
|
80
|
+
metadata: {
|
|
81
|
+
provider: this.provider.constructor.name,
|
|
82
|
+
tokens: result.usage?.totalTokens,
|
|
83
|
+
latency: Date.now() - Date.now(), // Should track actual timing
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
this.sendToConnection(connectionId, responseMessage);
|
|
88
|
+
if (this.options.enableTypingIndicators) {
|
|
89
|
+
this.clearTypingIndicator(connectionId);
|
|
90
|
+
}
|
|
91
|
+
await this.streamingManager.terminateStreamingSession(streamingSession.id);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
console.error("[WebSocket Chat] Error handling chat request:", error);
|
|
95
|
+
this.sendError(connectionId, "Failed to process chat request");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async handleGroupChat(roomId, request) {
|
|
99
|
+
try {
|
|
100
|
+
// Process AI request
|
|
101
|
+
const result = await this.provider.generateText({
|
|
102
|
+
prompt: request.prompt,
|
|
103
|
+
temperature: request.options?.temperature,
|
|
104
|
+
maxTokens: request.options?.maxTokens,
|
|
105
|
+
});
|
|
106
|
+
if (!result || !result.text) {
|
|
107
|
+
throw new Error("Invalid AI response");
|
|
108
|
+
}
|
|
109
|
+
// Broadcast to room
|
|
110
|
+
const broadcastMessage = {
|
|
111
|
+
id: randomUUID(),
|
|
112
|
+
type: "ai-response",
|
|
113
|
+
connectionId: "system",
|
|
114
|
+
timestamp: Date.now(),
|
|
115
|
+
data: {
|
|
116
|
+
text: result.text,
|
|
117
|
+
sessionId: request.sessionId,
|
|
118
|
+
userId: request.userId,
|
|
119
|
+
isGroupMessage: true,
|
|
120
|
+
metadata: {
|
|
121
|
+
provider: this.provider.constructor.name,
|
|
122
|
+
tokens: result.usage?.totalTokens,
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
this.wsServer.broadcastToRoom(roomId, broadcastMessage);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
console.error("[WebSocket Chat] Error handling group chat:", error);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async handleStreamingChat(connectionId, request) {
|
|
133
|
+
try {
|
|
134
|
+
const streamingOptions = request.streamingOptions || {};
|
|
135
|
+
// Create streaming channel
|
|
136
|
+
const channelId = randomUUID();
|
|
137
|
+
const channel = this.wsServer.createStreamingChannel(connectionId, channelId);
|
|
138
|
+
// Generate response
|
|
139
|
+
const result = await this.provider.generateText({
|
|
140
|
+
prompt: request.prompt,
|
|
141
|
+
...request.options,
|
|
142
|
+
});
|
|
143
|
+
if (!result || !result.text) {
|
|
144
|
+
throw new Error("Invalid AI response");
|
|
145
|
+
}
|
|
146
|
+
// Send complete response
|
|
147
|
+
const responseMessage = {
|
|
148
|
+
id: randomUUID(),
|
|
149
|
+
type: "ai-response",
|
|
150
|
+
connectionId,
|
|
151
|
+
timestamp: Date.now(),
|
|
152
|
+
data: {
|
|
153
|
+
text: result.text,
|
|
154
|
+
isStreamingComplete: true,
|
|
155
|
+
channelId,
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
this.sendToConnection(connectionId, responseMessage);
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
console.error("[WebSocket Chat] Error handling streaming chat:", error);
|
|
162
|
+
this.sendError(connectionId, "Failed to process streaming chat");
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// Real-time Features
|
|
166
|
+
async enableTypingIndicators(roomId) {
|
|
167
|
+
this.options.enableTypingIndicators = true;
|
|
168
|
+
console.log(`[WebSocket Chat] Enabled typing indicators for room ${roomId}`);
|
|
169
|
+
}
|
|
170
|
+
async enablePresenceTracking(roomId) {
|
|
171
|
+
this.options.enablePresenceTracking = true;
|
|
172
|
+
console.log(`[WebSocket Chat] Enabled presence tracking for room ${roomId}`);
|
|
173
|
+
}
|
|
174
|
+
async enableMessageSynchronization(roomId) {
|
|
175
|
+
this.options.enableMessageSync = true;
|
|
176
|
+
console.log(`[WebSocket Chat] Enabled message sync for room ${roomId}`);
|
|
177
|
+
}
|
|
178
|
+
// Helper methods
|
|
179
|
+
sendToConnection(connectionId, message) {
|
|
180
|
+
// Use the WebSocket server's send method
|
|
181
|
+
const ws = this.wsServer.connections?.get(connectionId);
|
|
182
|
+
if (ws && ws.readyState === 1) {
|
|
183
|
+
// WebSocket.OPEN
|
|
184
|
+
ws.send(JSON.stringify(message));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
sendError(connectionId, errorMessage) {
|
|
188
|
+
const errorMsg = {
|
|
189
|
+
id: randomUUID(),
|
|
190
|
+
type: "error",
|
|
191
|
+
connectionId,
|
|
192
|
+
timestamp: Date.now(),
|
|
193
|
+
data: { error: errorMessage },
|
|
194
|
+
};
|
|
195
|
+
this.sendToConnection(connectionId, errorMsg);
|
|
196
|
+
}
|
|
197
|
+
showTypingIndicator(connectionId, sender) {
|
|
198
|
+
if (!this.options.enableTypingIndicators) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
const message = {
|
|
202
|
+
id: randomUUID(),
|
|
203
|
+
type: "system",
|
|
204
|
+
connectionId,
|
|
205
|
+
timestamp: Date.now(),
|
|
206
|
+
data: {
|
|
207
|
+
event: "typing_start",
|
|
208
|
+
sender,
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
this.sendToConnection(connectionId, message);
|
|
212
|
+
}
|
|
213
|
+
clearTypingIndicator(connectionId) {
|
|
214
|
+
const timeout = this.typingIndicators.get(connectionId);
|
|
215
|
+
if (timeout) {
|
|
216
|
+
clearTimeout(timeout);
|
|
217
|
+
this.typingIndicators.delete(connectionId);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
updatePresence(connectionId, status) {
|
|
221
|
+
if (!this.options.enablePresenceTracking) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
this.presenceTracking.set(connectionId, {
|
|
225
|
+
lastSeen: Date.now(),
|
|
226
|
+
status,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
handleWebSocketChatMessage(connectionId, message) {
|
|
230
|
+
switch (message.data?.event) {
|
|
231
|
+
case "chat_request":
|
|
232
|
+
this.handleWebSocketChatRequest(connectionId, message.data.request);
|
|
233
|
+
break;
|
|
234
|
+
case "join_room":
|
|
235
|
+
this.wsServer.joinRoom(connectionId, message.data.roomId);
|
|
236
|
+
break;
|
|
237
|
+
case "leave_room":
|
|
238
|
+
this.wsServer.leaveRoom(connectionId, message.data.roomId);
|
|
239
|
+
break;
|
|
240
|
+
case "typing_start":
|
|
241
|
+
this.showTypingIndicator(connectionId, message.data.sender || "user");
|
|
242
|
+
break;
|
|
243
|
+
case "typing_stop":
|
|
244
|
+
this.clearTypingIndicator(connectionId);
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// Public API
|
|
249
|
+
getConnectionCount() {
|
|
250
|
+
return this.wsServer.getConnectionCount();
|
|
251
|
+
}
|
|
252
|
+
getRoomCount() {
|
|
253
|
+
return this.wsServer.getRoomCount();
|
|
254
|
+
}
|
|
255
|
+
getStreamingMetrics() {
|
|
256
|
+
return this.streamingManager.getStreamingMetrics();
|
|
257
|
+
}
|
|
258
|
+
close() {
|
|
259
|
+
this.wsServer.close();
|
|
260
|
+
this.streamingManager.destroy();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NeuroLink Config Manager with Backup/Restore System
|
|
3
|
+
* Industry standard configuration management with safety mechanisms
|
|
4
|
+
*/
|
|
5
|
+
import type { NeuroLinkConfig, ProviderConfig, BackupInfo, ConfigValidationResult, ConfigUpdateOptions } from "./types.js";
|
|
6
|
+
/**
|
|
7
|
+
* Enhanced Config Manager with automatic backup/restore capabilities
|
|
8
|
+
*/
|
|
9
|
+
export declare class NeuroLinkConfigManager {
|
|
10
|
+
private configPath;
|
|
11
|
+
private backupDir;
|
|
12
|
+
private config;
|
|
13
|
+
private configCache;
|
|
14
|
+
/**
|
|
15
|
+
* Load configuration with caching
|
|
16
|
+
*/
|
|
17
|
+
loadConfig(): Promise<NeuroLinkConfig>;
|
|
18
|
+
/**
|
|
19
|
+
* Update configuration with automatic backup
|
|
20
|
+
*/
|
|
21
|
+
updateConfig(updates: Partial<NeuroLinkConfig>, options?: ConfigUpdateOptions): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Create a backup with metadata
|
|
24
|
+
*/
|
|
25
|
+
createBackup(reason?: string): Promise<string>;
|
|
26
|
+
/**
|
|
27
|
+
* List all available backups
|
|
28
|
+
*/
|
|
29
|
+
listBackups(): Promise<BackupInfo[]>;
|
|
30
|
+
/**
|
|
31
|
+
* Restore from specific backup
|
|
32
|
+
*/
|
|
33
|
+
restoreFromBackup(backupFilename: string): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Restore from latest backup
|
|
36
|
+
*/
|
|
37
|
+
restoreLatestBackup(): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Clean up old backups
|
|
40
|
+
*/
|
|
41
|
+
cleanupOldBackups(keepCount?: number): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Update provider status
|
|
44
|
+
*/
|
|
45
|
+
updateProviderStatus(providerId: string, status: Partial<ProviderConfig>): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Validate configuration
|
|
48
|
+
*/
|
|
49
|
+
validateConfig(config: NeuroLinkConfig): Promise<ConfigValidationResult>;
|
|
50
|
+
/**
|
|
51
|
+
* Generate default configuration
|
|
52
|
+
*/
|
|
53
|
+
generateDefaultConfig(): Promise<NeuroLinkConfig>;
|
|
54
|
+
private ensureBackupDirectory;
|
|
55
|
+
private readConfigFile;
|
|
56
|
+
private persistConfig;
|
|
57
|
+
private generateConfigHash;
|
|
58
|
+
private extractMetadataFromBackup;
|
|
59
|
+
private extractConfigFromBackup;
|
|
60
|
+
}
|