@juspay/neurolink 6.0.0 → 6.2.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.
Files changed (283) hide show
  1. package/CHANGELOG.md +14 -3
  2. package/dist/cli/commands/config.d.ts +13 -13
  3. package/dist/cli/index.js +3 -9
  4. package/dist/core/analytics.d.ts +11 -1
  5. package/dist/core/analytics.js +2 -2
  6. package/dist/core/base-provider.js +4 -18
  7. package/dist/core/dynamic-models.d.ts +8 -8
  8. package/dist/core/factory.js +3 -11
  9. package/dist/index.d.ts +2 -4
  10. package/dist/index.js +2 -11
  11. package/dist/lib/core/analytics.d.ts +11 -1
  12. package/dist/lib/core/analytics.js +2 -2
  13. package/dist/lib/core/base-provider.js +4 -18
  14. package/dist/lib/core/dynamic-models.d.ts +8 -8
  15. package/dist/lib/core/factory.js +3 -11
  16. package/dist/lib/index.d.ts +2 -4
  17. package/dist/lib/index.js +2 -11
  18. package/dist/lib/mcp/factory.d.ts +1 -1
  19. package/dist/lib/mcp/index.d.ts +7 -19
  20. package/dist/lib/mcp/index.js +15 -56
  21. package/dist/lib/neurolink.d.ts +1 -9
  22. package/dist/lib/neurolink.js +21 -70
  23. package/dist/lib/providers/amazon-bedrock.js +5 -16
  24. package/dist/lib/providers/anthropic-baseprovider.js +3 -21
  25. package/dist/lib/providers/anthropic.js +4 -7
  26. package/dist/lib/providers/azure-openai.js +4 -2
  27. package/dist/lib/providers/google-vertex.js +5 -9
  28. package/dist/lib/providers/huggingFace.js +5 -10
  29. package/dist/lib/providers/mistral.js +5 -8
  30. package/dist/lib/providers/openAI.js +4 -7
  31. package/dist/lib/utils/providerConfig.d.ts +117 -0
  32. package/dist/lib/utils/providerConfig.js +353 -0
  33. package/dist/lib/utils/providerUtils.d.ts +2 -1
  34. package/dist/lib/utils/providerUtils.js +53 -36
  35. package/dist/lib/utils/timeout.d.ts +72 -1
  36. package/dist/lib/utils/timeout.js +203 -2
  37. package/dist/mcp/factory.d.ts +1 -1
  38. package/dist/mcp/index.d.ts +7 -19
  39. package/dist/mcp/index.js +15 -56
  40. package/dist/neurolink.d.ts +1 -9
  41. package/dist/neurolink.js +21 -70
  42. package/dist/providers/amazon-bedrock.js +5 -16
  43. package/dist/providers/anthropic-baseprovider.js +3 -21
  44. package/dist/providers/anthropic.js +4 -7
  45. package/dist/providers/azure-openai.js +4 -2
  46. package/dist/providers/google-vertex.js +5 -9
  47. package/dist/providers/huggingFace.js +5 -10
  48. package/dist/providers/mistral.js +5 -8
  49. package/dist/providers/openAI.js +4 -7
  50. package/dist/utils/providerConfig.d.ts +117 -0
  51. package/dist/utils/providerConfig.js +353 -0
  52. package/dist/utils/providerUtils.d.ts +2 -1
  53. package/dist/utils/providerUtils.js +53 -36
  54. package/dist/utils/timeout.d.ts +72 -1
  55. package/dist/utils/timeout.js +203 -2
  56. package/package.json +2 -1
  57. package/dist/chat/client-utils.d.ts +0 -95
  58. package/dist/chat/client-utils.js +0 -315
  59. package/dist/chat/index.d.ts +0 -24
  60. package/dist/chat/index.js +0 -33
  61. package/dist/chat/session-storage.d.ts +0 -77
  62. package/dist/chat/session-storage.js +0 -233
  63. package/dist/chat/session.d.ts +0 -96
  64. package/dist/chat/session.js +0 -257
  65. package/dist/chat/sse-handler.d.ts +0 -49
  66. package/dist/chat/sse-handler.js +0 -259
  67. package/dist/chat/types.d.ts +0 -74
  68. package/dist/chat/types.js +0 -5
  69. package/dist/chat/websocket-chat-handler.d.ts +0 -37
  70. package/dist/chat/websocket-chat-handler.js +0 -262
  71. package/dist/cli/commands/mcp.d.ts +0 -20
  72. package/dist/cli/commands/mcp.js +0 -1272
  73. package/dist/core/defaults.d.ts +0 -19
  74. package/dist/core/defaults.js +0 -29
  75. package/dist/core/evaluation-config.d.ts +0 -29
  76. package/dist/core/evaluation-config.js +0 -144
  77. package/dist/factories/compatibility-factory.d.ts +0 -35
  78. package/dist/factories/compatibility-factory.js +0 -71
  79. package/dist/factories/provider-generate-factory.d.ts +0 -20
  80. package/dist/factories/provider-generate-factory.js +0 -93
  81. package/dist/lib/chat/client-utils.d.ts +0 -95
  82. package/dist/lib/chat/client-utils.js +0 -315
  83. package/dist/lib/chat/index.d.ts +0 -24
  84. package/dist/lib/chat/index.js +0 -33
  85. package/dist/lib/chat/session-storage.d.ts +0 -77
  86. package/dist/lib/chat/session-storage.js +0 -233
  87. package/dist/lib/chat/session.d.ts +0 -96
  88. package/dist/lib/chat/session.js +0 -257
  89. package/dist/lib/chat/sse-handler.d.ts +0 -49
  90. package/dist/lib/chat/sse-handler.js +0 -259
  91. package/dist/lib/chat/types.d.ts +0 -74
  92. package/dist/lib/chat/types.js +0 -5
  93. package/dist/lib/chat/websocket-chat-handler.d.ts +0 -37
  94. package/dist/lib/chat/websocket-chat-handler.js +0 -262
  95. package/dist/lib/core/defaults.d.ts +0 -19
  96. package/dist/lib/core/defaults.js +0 -29
  97. package/dist/lib/core/evaluation-config.d.ts +0 -29
  98. package/dist/lib/core/evaluation-config.js +0 -144
  99. package/dist/lib/factories/compatibility-factory.d.ts +0 -35
  100. package/dist/lib/factories/compatibility-factory.js +0 -71
  101. package/dist/lib/factories/provider-generate-factory.d.ts +0 -20
  102. package/dist/lib/factories/provider-generate-factory.js +0 -93
  103. package/dist/lib/mcp/adapters/plugin-bridge.d.ts +0 -40
  104. package/dist/lib/mcp/adapters/plugin-bridge.js +0 -89
  105. package/dist/lib/mcp/auto-discovery.d.ts +0 -62
  106. package/dist/lib/mcp/auto-discovery.js +0 -149
  107. package/dist/lib/mcp/client.d.ts +0 -68
  108. package/dist/lib/mcp/client.js +0 -248
  109. package/dist/lib/mcp/config.d.ts +0 -31
  110. package/dist/lib/mcp/config.js +0 -99
  111. package/dist/lib/mcp/context-manager.d.ts +0 -171
  112. package/dist/lib/mcp/context-manager.js +0 -362
  113. package/dist/lib/mcp/contracts/mcp-contract.d.ts +0 -169
  114. package/dist/lib/mcp/contracts/mcp-contract.js +0 -58
  115. package/dist/lib/mcp/core/plugin-manager.d.ts +0 -46
  116. package/dist/lib/mcp/core/plugin-manager.js +0 -110
  117. package/dist/lib/mcp/demo/plugin-demo.d.ts +0 -20
  118. package/dist/lib/mcp/demo/plugin-demo.js +0 -118
  119. package/dist/lib/mcp/dynamic-chain-executor.d.ts +0 -225
  120. package/dist/lib/mcp/dynamic-chain-executor.js +0 -489
  121. package/dist/lib/mcp/dynamic-orchestrator.d.ts +0 -115
  122. package/dist/lib/mcp/dynamic-orchestrator.js +0 -351
  123. package/dist/lib/mcp/ecosystem.d.ts +0 -75
  124. package/dist/lib/mcp/ecosystem.js +0 -161
  125. package/dist/lib/mcp/error-manager.d.ts +0 -254
  126. package/dist/lib/mcp/error-manager.js +0 -501
  127. package/dist/lib/mcp/error-recovery.d.ts +0 -159
  128. package/dist/lib/mcp/error-recovery.js +0 -405
  129. package/dist/lib/mcp/external-client.d.ts +0 -88
  130. package/dist/lib/mcp/external-client.js +0 -331
  131. package/dist/lib/mcp/external-manager.d.ts +0 -112
  132. package/dist/lib/mcp/external-manager.js +0 -308
  133. package/dist/lib/mcp/function-calling.d.ts +0 -65
  134. package/dist/lib/mcp/function-calling.js +0 -642
  135. package/dist/lib/mcp/health-monitor.d.ts +0 -257
  136. package/dist/lib/mcp/health-monitor.js +0 -630
  137. package/dist/lib/mcp/initialize-tools.d.ts +0 -29
  138. package/dist/lib/mcp/initialize-tools.js +0 -261
  139. package/dist/lib/mcp/initialize.d.ts +0 -18
  140. package/dist/lib/mcp/initialize.js +0 -62
  141. package/dist/lib/mcp/manager.d.ts +0 -68
  142. package/dist/lib/mcp/manager.js +0 -176
  143. package/dist/lib/mcp/neurolink-mcp-client.d.ts +0 -97
  144. package/dist/lib/mcp/neurolink-mcp-client.js +0 -462
  145. package/dist/lib/mcp/orchestrator.d.ts +0 -302
  146. package/dist/lib/mcp/orchestrator.js +0 -703
  147. package/dist/lib/mcp/plugin-manager.d.ts +0 -98
  148. package/dist/lib/mcp/plugin-manager.js +0 -296
  149. package/dist/lib/mcp/plugins/core/filesystem-mcp.d.ts +0 -36
  150. package/dist/lib/mcp/plugins/core/filesystem-mcp.js +0 -142
  151. package/dist/lib/mcp/plugins/filesystem-mcp.d.ts +0 -37
  152. package/dist/lib/mcp/plugins/filesystem-mcp.js +0 -54
  153. package/dist/lib/mcp/security-manager.d.ts +0 -87
  154. package/dist/lib/mcp/security-manager.js +0 -344
  155. package/dist/lib/mcp/semaphore-manager.d.ts +0 -137
  156. package/dist/lib/mcp/semaphore-manager.js +0 -329
  157. package/dist/lib/mcp/session-manager.d.ts +0 -187
  158. package/dist/lib/mcp/session-manager.js +0 -400
  159. package/dist/lib/mcp/session-persistence.d.ts +0 -93
  160. package/dist/lib/mcp/session-persistence.js +0 -301
  161. package/dist/lib/mcp/tool-integration.d.ts +0 -58
  162. package/dist/lib/mcp/tool-integration.js +0 -203
  163. package/dist/lib/mcp/transport-manager.d.ts +0 -154
  164. package/dist/lib/mcp/transport-manager.js +0 -334
  165. package/dist/lib/mcp/unified-mcp.d.ts +0 -133
  166. package/dist/lib/mcp/unified-mcp.js +0 -251
  167. package/dist/lib/mcp/unified-registry.d.ts +0 -165
  168. package/dist/lib/mcp/unified-registry.js +0 -538
  169. package/dist/lib/providers/analytics-helper.d.ts +0 -38
  170. package/dist/lib/providers/analytics-helper.js +0 -216
  171. package/dist/lib/providers/function-calling-provider.d.ts +0 -142
  172. package/dist/lib/providers/function-calling-provider.js +0 -630
  173. package/dist/lib/providers/mcp-provider.d.ts +0 -75
  174. package/dist/lib/providers/mcp-provider.js +0 -283
  175. package/dist/lib/providers/timeout-wrapper.d.ts +0 -40
  176. package/dist/lib/providers/timeout-wrapper.js +0 -100
  177. package/dist/lib/sdk/tool-extension.d.ts +0 -181
  178. package/dist/lib/sdk/tool-extension.js +0 -284
  179. package/dist/lib/services/streaming/streaming-manager.d.ts +0 -29
  180. package/dist/lib/services/streaming/streaming-manager.js +0 -245
  181. package/dist/lib/services/types.d.ts +0 -156
  182. package/dist/lib/services/types.js +0 -2
  183. package/dist/lib/services/websocket/websocket-server.d.ts +0 -34
  184. package/dist/lib/services/websocket/websocket-server.js +0 -305
  185. package/dist/lib/utils/provider-validation.d.ts +0 -36
  186. package/dist/lib/utils/provider-validation.js +0 -625
  187. package/dist/lib/utils/providerUtils-fixed.d.ts +0 -8
  188. package/dist/lib/utils/providerUtils-fixed.js +0 -94
  189. package/dist/lib/utils/streaming-utils.d.ts +0 -79
  190. package/dist/lib/utils/streaming-utils.js +0 -198
  191. package/dist/lib/utils/timeout-manager.d.ts +0 -75
  192. package/dist/lib/utils/timeout-manager.js +0 -244
  193. package/dist/mcp/adapters/plugin-bridge.d.ts +0 -40
  194. package/dist/mcp/adapters/plugin-bridge.js +0 -89
  195. package/dist/mcp/auto-discovery.d.ts +0 -62
  196. package/dist/mcp/auto-discovery.js +0 -149
  197. package/dist/mcp/client.d.ts +0 -68
  198. package/dist/mcp/client.js +0 -248
  199. package/dist/mcp/config.d.ts +0 -31
  200. package/dist/mcp/config.js +0 -99
  201. package/dist/mcp/context-manager.d.ts +0 -171
  202. package/dist/mcp/context-manager.js +0 -362
  203. package/dist/mcp/contracts/mcp-contract.d.ts +0 -169
  204. package/dist/mcp/contracts/mcp-contract.js +0 -58
  205. package/dist/mcp/core/plugin-manager.d.ts +0 -46
  206. package/dist/mcp/core/plugin-manager.js +0 -110
  207. package/dist/mcp/demo/plugin-demo.d.ts +0 -20
  208. package/dist/mcp/demo/plugin-demo.js +0 -118
  209. package/dist/mcp/dynamic-chain-executor.d.ts +0 -225
  210. package/dist/mcp/dynamic-chain-executor.js +0 -489
  211. package/dist/mcp/dynamic-orchestrator.d.ts +0 -115
  212. package/dist/mcp/dynamic-orchestrator.js +0 -351
  213. package/dist/mcp/ecosystem.d.ts +0 -75
  214. package/dist/mcp/ecosystem.js +0 -162
  215. package/dist/mcp/error-manager.d.ts +0 -254
  216. package/dist/mcp/error-manager.js +0 -501
  217. package/dist/mcp/error-recovery.d.ts +0 -159
  218. package/dist/mcp/error-recovery.js +0 -405
  219. package/dist/mcp/external-client.d.ts +0 -88
  220. package/dist/mcp/external-client.js +0 -331
  221. package/dist/mcp/external-manager.d.ts +0 -112
  222. package/dist/mcp/external-manager.js +0 -308
  223. package/dist/mcp/function-calling.d.ts +0 -65
  224. package/dist/mcp/function-calling.js +0 -642
  225. package/dist/mcp/health-monitor.d.ts +0 -257
  226. package/dist/mcp/health-monitor.js +0 -630
  227. package/dist/mcp/initialize-tools.d.ts +0 -29
  228. package/dist/mcp/initialize-tools.js +0 -262
  229. package/dist/mcp/initialize.d.ts +0 -18
  230. package/dist/mcp/initialize.js +0 -62
  231. package/dist/mcp/manager.d.ts +0 -68
  232. package/dist/mcp/manager.js +0 -176
  233. package/dist/mcp/neurolink-mcp-client.d.ts +0 -97
  234. package/dist/mcp/neurolink-mcp-client.js +0 -462
  235. package/dist/mcp/orchestrator.d.ts +0 -302
  236. package/dist/mcp/orchestrator.js +0 -703
  237. package/dist/mcp/plugin-manager.d.ts +0 -98
  238. package/dist/mcp/plugin-manager.js +0 -297
  239. package/dist/mcp/plugins/core/filesystem-mcp.d.ts +0 -36
  240. package/dist/mcp/plugins/core/filesystem-mcp.js +0 -142
  241. package/dist/mcp/plugins/core/neurolink-mcp.json +0 -17
  242. package/dist/mcp/plugins/filesystem-mcp.d.ts +0 -37
  243. package/dist/mcp/plugins/filesystem-mcp.js +0 -54
  244. package/dist/mcp/security-manager.d.ts +0 -87
  245. package/dist/mcp/security-manager.js +0 -344
  246. package/dist/mcp/semaphore-manager.d.ts +0 -137
  247. package/dist/mcp/semaphore-manager.js +0 -329
  248. package/dist/mcp/session-manager.d.ts +0 -187
  249. package/dist/mcp/session-manager.js +0 -400
  250. package/dist/mcp/session-persistence.d.ts +0 -93
  251. package/dist/mcp/session-persistence.js +0 -302
  252. package/dist/mcp/tool-integration.d.ts +0 -58
  253. package/dist/mcp/tool-integration.js +0 -203
  254. package/dist/mcp/transport-manager.d.ts +0 -154
  255. package/dist/mcp/transport-manager.js +0 -335
  256. package/dist/mcp/unified-mcp.d.ts +0 -133
  257. package/dist/mcp/unified-mcp.js +0 -251
  258. package/dist/mcp/unified-registry.d.ts +0 -165
  259. package/dist/mcp/unified-registry.js +0 -539
  260. package/dist/providers/analytics-helper.d.ts +0 -38
  261. package/dist/providers/analytics-helper.js +0 -216
  262. package/dist/providers/function-calling-provider.d.ts +0 -142
  263. package/dist/providers/function-calling-provider.js +0 -630
  264. package/dist/providers/mcp-provider.d.ts +0 -75
  265. package/dist/providers/mcp-provider.js +0 -283
  266. package/dist/providers/timeout-wrapper.d.ts +0 -40
  267. package/dist/providers/timeout-wrapper.js +0 -100
  268. package/dist/sdk/tool-extension.d.ts +0 -181
  269. package/dist/sdk/tool-extension.js +0 -284
  270. package/dist/services/streaming/streaming-manager.d.ts +0 -29
  271. package/dist/services/streaming/streaming-manager.js +0 -245
  272. package/dist/services/types.d.ts +0 -156
  273. package/dist/services/types.js +0 -2
  274. package/dist/services/websocket/websocket-server.d.ts +0 -34
  275. package/dist/services/websocket/websocket-server.js +0 -306
  276. package/dist/utils/provider-validation.d.ts +0 -36
  277. package/dist/utils/provider-validation.js +0 -625
  278. package/dist/utils/providerUtils-fixed.d.ts +0 -8
  279. package/dist/utils/providerUtils-fixed.js +0 -94
  280. package/dist/utils/streaming-utils.d.ts +0 -79
  281. package/dist/utils/streaming-utils.js +0 -198
  282. package/dist/utils/timeout-manager.d.ts +0 -75
  283. package/dist/utils/timeout-manager.js +0 -244
@@ -1,96 +0,0 @@
1
- /**
2
- * Phase 3: SSE Chat Utilities
3
- * Chat session management with persistence
4
- */
5
- import type { ChatMessage, SessionOptions, ChatSessionState } from "./types.js";
6
- import type { JsonValue } from "../types/common.js";
7
- export declare class ChatSession {
8
- private sessionId;
9
- private messages;
10
- private options;
11
- private storage;
12
- private createdAt;
13
- private lastActivity;
14
- private metadata;
15
- constructor(sessionId: string, options?: SessionOptions);
16
- /**
17
- * Add message to session
18
- */
19
- addMessage(role: "user" | "assistant" | "system", content: string, metadata?: unknown): ChatMessage;
20
- /**
21
- * Get conversation history
22
- */
23
- getHistory(limit?: number): ChatMessage[];
24
- /**
25
- * Get messages for a specific role
26
- */
27
- getMessagesByRole(role: "user" | "assistant" | "system"): ChatMessage[];
28
- /**
29
- * Get session metadata
30
- */
31
- getMetadata(): Record<string, JsonValue>;
32
- /**
33
- * Set session metadata
34
- */
35
- setMetadata(key: string, value: JsonValue): void;
36
- /**
37
- * Clear session history
38
- */
39
- clearHistory(): void;
40
- /**
41
- * Get session statistics
42
- */
43
- getStats(): {
44
- messageCount: number;
45
- userMessages: number;
46
- assistantMessages: number;
47
- systemMessages: number;
48
- totalTokens: number;
49
- sessionAge: number;
50
- lastActivity: number;
51
- };
52
- /**
53
- * Trim context to fit within token limits
54
- */
55
- trimContext(maxTokens: number): number;
56
- /**
57
- * Estimate total tokens in session
58
- */
59
- estimateTokens(): number;
60
- /**
61
- * Get session context for AI prompts
62
- */
63
- getContextForPrompt(includeSystem?: boolean): string;
64
- /**
65
- * Persist session to storage
66
- */
67
- persist(): Promise<void>;
68
- /**
69
- * Load session from storage
70
- */
71
- private loadSession;
72
- /**
73
- * Delete session from storage
74
- */
75
- delete(): Promise<void>;
76
- /**
77
- * Create storage adapter based on options
78
- */
79
- private createStorageAdapter;
80
- /**
81
- * Export session data
82
- */
83
- export(): ChatSessionState;
84
- /**
85
- * Import session data
86
- */
87
- import(state: ChatSessionState): void;
88
- /**
89
- * Get session ID
90
- */
91
- getId(): string;
92
- /**
93
- * Check if session is active (not expired)
94
- */
95
- isActive(): boolean;
96
- }
@@ -1,257 +0,0 @@
1
- /**
2
- * Phase 3: SSE Chat Utilities
3
- * Chat session management with persistence
4
- */
5
- import { MemorySessionStorage } from "./session-storage.js";
6
- export class ChatSession {
7
- sessionId;
8
- messages = [];
9
- options;
10
- storage;
11
- createdAt;
12
- lastActivity;
13
- metadata = {};
14
- constructor(sessionId, options = {}) {
15
- this.sessionId = sessionId;
16
- this.createdAt = Date.now();
17
- this.lastActivity = Date.now();
18
- this.options = {
19
- maxHistory: options.maxHistory ?? 100,
20
- persistenceAdapter: options.persistenceAdapter ?? "memory",
21
- contextWindowSize: options.contextWindowSize ?? 4000,
22
- autoSave: options.autoSave ?? true,
23
- ttl: options.ttl ?? 3600, // 1 hour default
24
- };
25
- // Initialize storage adapter
26
- this.storage = this.createStorageAdapter();
27
- // Load existing session if available
28
- this.loadSession().catch(() => { });
29
- }
30
- /**
31
- * Add message to session
32
- */
33
- addMessage(role, content, metadata) {
34
- const message = {
35
- id: `${this.sessionId}_${Date.now()}_${role}`,
36
- role,
37
- content,
38
- timestamp: Date.now(),
39
- metadata: metadata,
40
- };
41
- this.messages.push(message);
42
- this.lastActivity = Date.now();
43
- // Trim history if needed
44
- if (this.messages.length > this.options.maxHistory) {
45
- this.messages = this.messages.slice(-this.options.maxHistory);
46
- }
47
- // Auto-save if enabled
48
- if (this.options.autoSave) {
49
- this.persist().catch((error) => {
50
- console.error("Auto-save failed:", error);
51
- });
52
- }
53
- return message;
54
- }
55
- /**
56
- * Get conversation history
57
- */
58
- getHistory(limit) {
59
- const messages = limit ? this.messages.slice(-limit) : this.messages;
60
- return [...messages]; // Return copy to prevent mutations
61
- }
62
- /**
63
- * Get messages for a specific role
64
- */
65
- getMessagesByRole(role) {
66
- return this.messages.filter((msg) => msg.role === role);
67
- }
68
- /**
69
- * Get session metadata
70
- */
71
- getMetadata() {
72
- return { ...this.metadata };
73
- }
74
- /**
75
- * Set session metadata
76
- */
77
- setMetadata(key, value) {
78
- this.metadata[key] = value;
79
- this.lastActivity = Date.now();
80
- }
81
- /**
82
- * Clear session history
83
- */
84
- clearHistory() {
85
- this.messages = [];
86
- this.lastActivity = Date.now();
87
- if (this.options.autoSave) {
88
- this.persist().catch((error) => {
89
- console.error("Auto-save after clear failed:", error);
90
- });
91
- }
92
- }
93
- /**
94
- * Get session statistics
95
- */
96
- getStats() {
97
- const userMessages = this.getMessagesByRole("user").length;
98
- const assistantMessages = this.getMessagesByRole("assistant").length;
99
- const systemMessages = this.getMessagesByRole("system").length;
100
- // Rough token estimation (characters / 4)
101
- const totalTokens = this.messages.reduce((sum, msg) => {
102
- return sum + Math.ceil(msg.content.length / 4);
103
- }, 0);
104
- return {
105
- messageCount: this.messages.length,
106
- userMessages,
107
- assistantMessages,
108
- systemMessages,
109
- totalTokens,
110
- sessionAge: Date.now() - this.createdAt,
111
- lastActivity: this.lastActivity,
112
- };
113
- }
114
- /**
115
- * Trim context to fit within token limits
116
- */
117
- trimContext(maxTokens) {
118
- let currentTokens = this.estimateTokens();
119
- let removedMessages = 0;
120
- // Keep the most recent messages within token limit
121
- while (currentTokens > maxTokens && this.messages.length > 1) {
122
- // Remove oldest message (but keep system messages if possible)
123
- const oldestNonSystem = this.messages.findIndex((msg) => msg.role !== "system");
124
- const indexToRemove = oldestNonSystem >= 0 ? oldestNonSystem : 0;
125
- const removedMessage = this.messages.splice(indexToRemove, 1)[0];
126
- currentTokens -= Math.ceil(removedMessage.content.length / 4);
127
- removedMessages++;
128
- }
129
- this.lastActivity = Date.now();
130
- if (this.options.autoSave && removedMessages > 0) {
131
- this.persist().catch((error) => {
132
- console.error("Auto-save after trim failed:", error);
133
- });
134
- }
135
- return removedMessages;
136
- }
137
- /**
138
- * Estimate total tokens in session
139
- */
140
- estimateTokens() {
141
- return this.messages.reduce((sum, msg) => {
142
- return sum + Math.ceil(msg.content.length / 4);
143
- }, 0);
144
- }
145
- /**
146
- * Get session context for AI prompts
147
- */
148
- getContextForPrompt(includeSystem = true) {
149
- const relevantMessages = includeSystem
150
- ? this.messages
151
- : this.messages.filter((msg) => msg.role !== "system");
152
- return relevantMessages
153
- .map((msg) => `${msg.role}: ${msg.content}`)
154
- .join("\n\n");
155
- }
156
- /**
157
- * Persist session to storage
158
- */
159
- async persist() {
160
- const state = {
161
- id: this.sessionId,
162
- messages: this.messages,
163
- createdAt: this.createdAt,
164
- lastActivity: this.lastActivity,
165
- metadata: this.metadata,
166
- };
167
- await this.storage.set(this.sessionId, state);
168
- }
169
- /**
170
- * Load session from storage
171
- */
172
- async loadSession() {
173
- try {
174
- const state = await this.storage.get(this.sessionId);
175
- if (state) {
176
- this.messages = state.messages || [];
177
- this.createdAt = state.createdAt || Date.now();
178
- this.lastActivity = state.lastActivity || Date.now();
179
- this.metadata = state.metadata || {};
180
- // Check if session has expired
181
- const age = Date.now() - this.lastActivity;
182
- if (age > this.options.ttl * 1000) {
183
- // Session expired, start fresh
184
- this.messages = [];
185
- this.metadata = {};
186
- this.createdAt = Date.now();
187
- this.lastActivity = Date.now();
188
- }
189
- }
190
- }
191
- catch (error) {
192
- console.error("Failed to load session:", error);
193
- // Continue with fresh session
194
- }
195
- }
196
- /**
197
- * Delete session from storage
198
- */
199
- async delete() {
200
- await this.storage.delete(this.sessionId);
201
- this.messages = [];
202
- this.metadata = {};
203
- }
204
- /**
205
- * Create storage adapter based on options
206
- */
207
- createStorageAdapter() {
208
- switch (this.options.persistenceAdapter) {
209
- case "memory":
210
- return new MemorySessionStorage();
211
- case "redis":
212
- // TODO: Implement Redis adapter
213
- console.warn("Redis adapter not yet implemented, falling back to memory");
214
- return new MemorySessionStorage();
215
- case "file":
216
- // TODO: Implement File adapter
217
- console.warn("File adapter not yet implemented, falling back to memory");
218
- return new MemorySessionStorage();
219
- default:
220
- return new MemorySessionStorage();
221
- }
222
- }
223
- /**
224
- * Export session data
225
- */
226
- export() {
227
- return {
228
- id: this.sessionId,
229
- messages: [...this.messages],
230
- createdAt: this.createdAt,
231
- lastActivity: this.lastActivity,
232
- metadata: { ...this.metadata },
233
- };
234
- }
235
- /**
236
- * Import session data
237
- */
238
- import(state) {
239
- this.messages = state.messages || [];
240
- this.createdAt = state.createdAt || Date.now();
241
- this.lastActivity = state.lastActivity || Date.now();
242
- this.metadata = state.metadata || {};
243
- }
244
- /**
245
- * Get session ID
246
- */
247
- getId() {
248
- return this.sessionId;
249
- }
250
- /**
251
- * Check if session is active (not expired)
252
- */
253
- isActive() {
254
- const age = Date.now() - this.lastActivity;
255
- return age <= this.options.ttl * 1000;
256
- }
257
- }
@@ -1,49 +0,0 @@
1
- /**
2
- * Phase 3: SSE Chat Utilities
3
- * Server-Sent Events handler for real-time chat
4
- */
5
- import type { AIProvider } from "../core/types.js";
6
- import type { ChatRequest, SSEOptions } from "./types.js";
7
- export declare class SSEChatHandler {
8
- private connections;
9
- protected provider: AIProvider;
10
- protected options: Required<SSEOptions>;
11
- private heartbeatInterval?;
12
- constructor(provider: AIProvider, options?: SSEOptions);
13
- /**
14
- * Handle incoming chat request and return SSE response
15
- */
16
- handleChatRequest(request: ChatRequest): Promise<Response>;
17
- /**
18
- * Create event stream for a session
19
- */
20
- createEventStream(sessionId: string, handler?: (writer: WritableStreamDefaultWriter) => Promise<void>): ReadableStream;
21
- /**
22
- * Send SSE event to client
23
- */
24
- private sendEvent;
25
- /**
26
- * Get SSE response headers
27
- */
28
- private getSSEHeaders;
29
- /**
30
- * Close connection and cleanup
31
- */
32
- private closeConnection;
33
- /**
34
- * Start heartbeat to keep connections alive
35
- */
36
- private startHeartbeat;
37
- /**
38
- * Cleanup and stop handler
39
- */
40
- destroy(): void;
41
- /**
42
- * Get handler statistics
43
- */
44
- getStats(): {
45
- activeConnections: number;
46
- maxConnections: number;
47
- uptime: number;
48
- };
49
- }
@@ -1,259 +0,0 @@
1
- /**
2
- * Phase 3: SSE Chat Utilities
3
- * Server-Sent Events handler for real-time chat
4
- */
5
- import { ChatSession } from "./session.js";
6
- export class SSEChatHandler {
7
- connections = new Map();
8
- provider;
9
- options;
10
- heartbeatInterval;
11
- constructor(provider, options = {}) {
12
- this.provider = provider;
13
- this.options = {
14
- maxConnections: options.maxConnections ?? 100,
15
- heartbeatInterval: options.heartbeatInterval ?? 30000,
16
- connectionTimeout: options.connectionTimeout ?? 300000,
17
- enableCors: options.enableCors ?? true,
18
- corsOrigins: options.corsOrigins ?? ["*"],
19
- };
20
- this.startHeartbeat();
21
- }
22
- /**
23
- * Handle incoming chat request and return SSE response
24
- */
25
- async handleChatRequest(request) {
26
- const { sessionId, message, options = {} } = request;
27
- const session = new ChatSession(sessionId);
28
- // Add user message to session
29
- const userMessage = {
30
- id: `msg_${Date.now()}_user`,
31
- role: "user",
32
- content: message,
33
- timestamp: Date.now(),
34
- };
35
- session.addMessage(userMessage.role, userMessage.content);
36
- // Create SSE stream
37
- const stream = this.createEventStream(sessionId, async (writer) => {
38
- try {
39
- // Send initial event
40
- await this.sendEvent(writer, {
41
- type: "data",
42
- data: { type: "start", sessionId, messageId: userMessage.id },
43
- });
44
- // Generate AI response with streaming
45
- const aiResponse = await this.provider.stream({
46
- input: { text: message },
47
- temperature: options.temperature,
48
- maxTokens: options.maxTokens,
49
- systemPrompt: options.systemPrompt,
50
- });
51
- if (aiResponse?.stream) {
52
- // Iterate over the async iterable stream
53
- const reader = aiResponse.stream;
54
- let fullResponse = "";
55
- for await (const chunk of reader) {
56
- const content = chunk.content;
57
- fullResponse += content;
58
- // Send chunk to client
59
- await this.sendEvent(writer, {
60
- type: "data",
61
- data: {
62
- type: "chunk",
63
- content: content,
64
- sessionId,
65
- },
66
- });
67
- }
68
- // Add AI response to session
69
- const assistantMessage = {
70
- id: `msg_${Date.now()}_assistant`,
71
- role: "assistant",
72
- content: fullResponse,
73
- timestamp: Date.now(),
74
- metadata: {
75
- provider: this.provider.constructor.name || "unknown",
76
- model: "default",
77
- },
78
- };
79
- session.addMessage(assistantMessage.role, assistantMessage.content);
80
- // Send completion event
81
- await this.sendEvent(writer, {
82
- type: "complete",
83
- data: {
84
- type: "complete",
85
- sessionId,
86
- messageId: assistantMessage.id,
87
- totalTokens: fullResponse.length, // Rough estimate
88
- },
89
- });
90
- }
91
- // Persist session
92
- await session.persist();
93
- }
94
- catch (error) {
95
- await this.sendEvent(writer, {
96
- type: "error",
97
- data: {
98
- type: "error",
99
- message: error instanceof Error ? error.message : "Unknown error",
100
- sessionId,
101
- },
102
- });
103
- }
104
- });
105
- return new Response(stream, {
106
- headers: this.getSSEHeaders(),
107
- });
108
- }
109
- /**
110
- * Create event stream for a session
111
- */
112
- createEventStream(sessionId, handler) {
113
- const encoder = new TextEncoder();
114
- return new ReadableStream({
115
- start: async (controller) => {
116
- // Check connection limits
117
- if (this.connections.size >= this.options.maxConnections) {
118
- controller.error(new Error("Maximum connections exceeded"));
119
- return;
120
- }
121
- const { writable, readable } = new TransformStream();
122
- const writer = writable.getWriter();
123
- // Store connection
124
- this.connections.set(sessionId, writer);
125
- // Setup connection timeout
126
- const timeout = setTimeout(() => {
127
- this.closeConnection(sessionId);
128
- }, this.options.connectionTimeout);
129
- try {
130
- if (handler) {
131
- await handler(writer);
132
- }
133
- }
134
- catch (error) {
135
- console.error("SSE Handler error:", error);
136
- }
137
- finally {
138
- clearTimeout(timeout);
139
- this.closeConnection(sessionId);
140
- }
141
- // Pipe the readable side to controller
142
- const reader = readable.getReader();
143
- const pump = async () => {
144
- try {
145
- while (true) {
146
- const { done, value } = await reader.read();
147
- if (done) {
148
- controller.close();
149
- break;
150
- }
151
- controller.enqueue(value);
152
- }
153
- }
154
- catch (error) {
155
- controller.error(error);
156
- }
157
- finally {
158
- reader.releaseLock();
159
- }
160
- };
161
- pump();
162
- },
163
- cancel: () => {
164
- this.closeConnection(sessionId);
165
- },
166
- });
167
- }
168
- /**
169
- * Send SSE event to client
170
- */
171
- async sendEvent(writer, event) {
172
- const encoder = new TextEncoder();
173
- let eventText = "";
174
- if (event.id) {
175
- eventText += `id: ${event.id}\n`;
176
- }
177
- if (event.retry) {
178
- eventText += `retry: ${event.retry}\n`;
179
- }
180
- eventText += `event: ${event.type}\n`;
181
- eventText += `data: ${JSON.stringify(event.data)}\n\n`;
182
- await writer.write(encoder.encode(eventText));
183
- }
184
- /**
185
- * Get SSE response headers
186
- */
187
- getSSEHeaders() {
188
- const headers = {
189
- "Content-Type": "text/event-stream",
190
- "Cache-Control": "no-cache",
191
- Connection: "keep-alive",
192
- };
193
- if (this.options.enableCors) {
194
- headers["Access-Control-Allow-Origin"] =
195
- this.options.corsOrigins.join(", ");
196
- headers["Access-Control-Allow-Headers"] = "Cache-Control";
197
- }
198
- return headers;
199
- }
200
- /**
201
- * Close connection and cleanup
202
- */
203
- async closeConnection(sessionId) {
204
- const writer = this.connections.get(sessionId);
205
- if (writer) {
206
- try {
207
- await writer.close();
208
- }
209
- catch (error) {
210
- // Ignore close errors
211
- }
212
- this.connections.delete(sessionId);
213
- }
214
- }
215
- /**
216
- * Start heartbeat to keep connections alive
217
- */
218
- startHeartbeat() {
219
- this.heartbeatInterval = setInterval(async () => {
220
- const heartbeatEvent = {
221
- type: "heartbeat",
222
- data: { timestamp: Date.now() },
223
- };
224
- // Send heartbeat to all connections
225
- for (const [sessionId, writer] of this.connections) {
226
- try {
227
- await this.sendEvent(writer, heartbeatEvent);
228
- }
229
- catch (error) {
230
- // Remove failed connections
231
- this.closeConnection(sessionId);
232
- }
233
- }
234
- }, this.options.heartbeatInterval);
235
- }
236
- /**
237
- * Cleanup and stop handler
238
- */
239
- destroy() {
240
- if (this.heartbeatInterval) {
241
- clearInterval(this.heartbeatInterval);
242
- }
243
- // Close all connections
244
- for (const sessionId of this.connections.keys()) {
245
- this.closeConnection(sessionId);
246
- }
247
- this.connections.clear();
248
- }
249
- /**
250
- * Get handler statistics
251
- */
252
- getStats() {
253
- return {
254
- activeConnections: this.connections.size,
255
- maxConnections: this.options.maxConnections,
256
- uptime: process.uptime() * 1000,
257
- };
258
- }
259
- }