modality-kit 0.8.0 → 0.8.2

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/dist/index.js CHANGED
@@ -4991,14 +4991,6 @@ async function compressWithLanguageDetection(text, maxTokens = DEFAULT_CONFIG.ma
4991
4991
  });
4992
4992
  }
4993
4993
  // src/schemas/jsonrpc.ts
4994
- var exports_jsonrpc = {};
4995
- __export(exports_jsonrpc, {
4996
- generateId: () => generateId,
4997
- STANDARD_ERROR_MESSAGES: () => STANDARD_ERROR_MESSAGES,
4998
- JSONRPC_VERSION: () => JSONRPC_VERSION,
4999
- JSONRPCUtils: () => JSONRPCUtils,
5000
- JSONRPCErrorCode: () => JSONRPCErrorCode
5001
- });
5002
4994
  var JSONRPC_VERSION = "2.0";
5003
4995
  var JSONRPCErrorCode;
5004
4996
  ((JSONRPCErrorCode2) => {
@@ -5463,6 +5455,196 @@ class JSONRPCCall {
5463
5455
  this.pendingRequests.destroy("JSONRPCManager destroyed");
5464
5456
  }
5465
5457
  }
5458
+ // src/jsonrpc-manager.ts
5459
+ var logger2 = getLoggerInstance("JSON-RPC-Manager");
5460
+
5461
+ class JSONRPCManager extends JSONRPCCall {
5462
+ methods = new Map;
5463
+ config;
5464
+ eventHandlers;
5465
+ constructor(config = {}, eventHandlers = {}) {
5466
+ super();
5467
+ this.config = {
5468
+ strictValidation: true,
5469
+ defaultTimeout: 30000,
5470
+ maxBatchSize: 10,
5471
+ errorHandler: this.defaultErrorHandler.bind(this),
5472
+ ...config
5473
+ };
5474
+ this.eventHandlers = eventHandlers;
5475
+ }
5476
+ registerMethod(methodName, config) {
5477
+ if (this.methods.has(methodName)) {
5478
+ throw new Error(`Method '${methodName}' is already registered`);
5479
+ }
5480
+ this.methods.set(methodName, config);
5481
+ console.log(`Registered JSON-RPC method: ${methodName}`);
5482
+ }
5483
+ unregisterMethod(methodName) {
5484
+ const removed = this.methods.delete(methodName);
5485
+ if (removed) {
5486
+ console.log(`Unregistered JSON-RPC method: ${methodName}`);
5487
+ }
5488
+ return removed;
5489
+ }
5490
+ getRegisteredMethods() {
5491
+ return Array.from(this.methods.keys());
5492
+ }
5493
+ handleRequest(method, params, options = {}) {
5494
+ const { promise, request } = super.handleRequest(method, params, options);
5495
+ this.sendMessage(request, options);
5496
+ return { promise, request };
5497
+ }
5498
+ sendNotification(method, params, options) {
5499
+ const notification = JSONRPCUtils.createNotification(method, params);
5500
+ this.sendMessage(notification, options);
5501
+ }
5502
+ sendMessage(message, options) {
5503
+ console.warn("JSONRPCManager.sendMessage not implemented - message not sent:", message, options);
5504
+ }
5505
+ async validateMessage(data, options = {}) {
5506
+ try {
5507
+ const messageStr = typeof data === "string" ? data : data.toString();
5508
+ const message = JSONRPCUtils.deserialize(messageStr);
5509
+ if (!message) {
5510
+ console.error("Failed to parse JSON-RPC message:", messageStr);
5511
+ const errorResponse = JSONRPCUtils.createErrorResponse(JSONRPCUtils.createError(-32700 /* PARSE_ERROR */, "Parse error"), null);
5512
+ this.sendMessage(errorResponse, options);
5513
+ return;
5514
+ }
5515
+ const validation = JSONRPCUtils.validateMessage(message);
5516
+ if (!validation.valid) {
5517
+ const errorResponse = JSONRPCUtils.createErrorResponse(validation.error, message.id || null);
5518
+ this.sendMessage(errorResponse, options);
5519
+ return;
5520
+ }
5521
+ const response = await this.processMessage(validation, options);
5522
+ if (response) {
5523
+ this.sendMessage(response, options);
5524
+ }
5525
+ } catch (err) {
5526
+ const error = err;
5527
+ console.error("Error handling WebSocket message:", error);
5528
+ const errorResponse = JSONRPCUtils.createErrorResponse(JSONRPCUtils.createError(-32603 /* INTERNAL_ERROR */, "Internal error"), null);
5529
+ this.sendMessage(errorResponse, options);
5530
+ }
5531
+ }
5532
+ async processMessage(validation, options = {}) {
5533
+ try {
5534
+ switch (validation.messageType) {
5535
+ case "request":
5536
+ return await this.processRequest(validation.message, options);
5537
+ case "notification":
5538
+ this.processNotification(validation.message, options);
5539
+ return;
5540
+ case "response":
5541
+ const response = validation.message;
5542
+ logger2.info("Received response message, handling internally", [
5543
+ response,
5544
+ response.id
5545
+ ]);
5546
+ this.handleResponse(response);
5547
+ return;
5548
+ case "batch":
5549
+ return await this.processBatchRequest(validation.message, options);
5550
+ default:
5551
+ return JSONRPCUtils.createErrorResponse(JSONRPCUtils.createError(-32600 /* INVALID_REQUEST */, "Unknown message type"), validation.message.id || null);
5552
+ }
5553
+ } catch (error) {
5554
+ console.error("Error processing JSON-RPC message:", error);
5555
+ return JSONRPCUtils.createErrorResponse(this.config.errorHandler(error), validation.message.id || null);
5556
+ }
5557
+ }
5558
+ async processRequest(request, options = {}) {
5559
+ const context = {
5560
+ ...options,
5561
+ request,
5562
+ metadata: {}
5563
+ };
5564
+ try {
5565
+ const methodConfig = this.methods.get(request.method);
5566
+ if (!methodConfig) {
5567
+ const error = JSONRPCUtils.createError(-32601 /* METHOD_NOT_FOUND */, `Method '${request.method}' not found`);
5568
+ if (this.eventHandlers.onMethodError) {
5569
+ this.eventHandlers.onMethodError(request.method, error, context);
5570
+ }
5571
+ return JSONRPCUtils.createErrorResponse(error, request.id);
5572
+ }
5573
+ if (this.eventHandlers.onMethodCall) {
5574
+ this.eventHandlers.onMethodCall(request.method, request.params, context);
5575
+ }
5576
+ const result = await methodConfig.handler(request.params, context);
5577
+ if (this.eventHandlers.onMethodResponse) {
5578
+ this.eventHandlers.onMethodResponse(request.method, result, context);
5579
+ }
5580
+ return JSONRPCUtils.createSuccessResponse(result, request.id);
5581
+ } catch (error) {
5582
+ console.error(`Error executing method '${request.method}':`, error);
5583
+ const jsonrpcError = this.config.errorHandler(error, context);
5584
+ if (this.eventHandlers.onMethodError) {
5585
+ this.eventHandlers.onMethodError(request.method, jsonrpcError, context);
5586
+ }
5587
+ return JSONRPCUtils.createErrorResponse(jsonrpcError, request.id);
5588
+ }
5589
+ }
5590
+ async processNotification(notification, options = {}) {
5591
+ const context = {
5592
+ ...options,
5593
+ request: notification,
5594
+ metadata: {}
5595
+ };
5596
+ try {
5597
+ const methodConfig = this.methods.get(notification.method);
5598
+ if (!methodConfig) {
5599
+ console.warn(`Notification method '${notification.method}' not found`);
5600
+ return;
5601
+ }
5602
+ if (this.eventHandlers.onMethodCall) {
5603
+ this.eventHandlers.onMethodCall(notification.method, notification.params, context);
5604
+ }
5605
+ await methodConfig.handler(notification.params, context);
5606
+ } catch (error) {
5607
+ console.error(`Error executing notification '${notification.method}':`, error);
5608
+ }
5609
+ }
5610
+ async processBatchRequest(batchRequest, options = {}) {
5611
+ if (batchRequest.length > this.config.maxBatchSize) {
5612
+ const error = JSONRPCUtils.createError(-32600 /* INVALID_REQUEST */, `Batch size ${batchRequest.length} exceeds maximum allowed size ${this.config.maxBatchSize}`);
5613
+ return [JSONRPCUtils.createErrorResponse(error, null)];
5614
+ }
5615
+ const promises = batchRequest.map((item) => {
5616
+ if (JSONRPCUtils.isRequest(item)) {
5617
+ return this.processRequest(item, options);
5618
+ } else if (JSONRPCUtils.isNotification(item)) {
5619
+ return this.processNotification(item, options);
5620
+ }
5621
+ return Promise.resolve();
5622
+ });
5623
+ const results = await Promise.all(promises);
5624
+ return results.filter((result) => !!result);
5625
+ }
5626
+ defaultErrorHandler(error) {
5627
+ if (error.message.includes("timeout")) {
5628
+ return JSONRPCUtils.createError(-32001 /* TIMEOUT_ERROR */, STANDARD_ERROR_MESSAGES[-32001 /* TIMEOUT_ERROR */]);
5629
+ }
5630
+ if (error.message.includes("connection")) {
5631
+ return JSONRPCUtils.createError(-32002 /* CONNECTION_ERROR */, STANDARD_ERROR_MESSAGES[-32002 /* CONNECTION_ERROR */]);
5632
+ }
5633
+ return JSONRPCUtils.createError(-32603 /* INTERNAL_ERROR */, STANDARD_ERROR_MESSAGES[-32603 /* INTERNAL_ERROR */], { originalError: error.message });
5634
+ }
5635
+ getStats() {
5636
+ const parentStats = super.getStats();
5637
+ return {
5638
+ ...parentStats,
5639
+ registeredMethods: this.methods.size,
5640
+ methodNames: this.getRegisteredMethods()
5641
+ };
5642
+ }
5643
+ destroy() {
5644
+ super.destroy();
5645
+ this.methods.clear();
5646
+ }
5647
+ }
5466
5648
  export {
5467
5649
  withErrorHandling,
5468
5650
  setupAITools,
@@ -5474,7 +5656,9 @@ export {
5474
5656
  createDataPendingOperations,
5475
5657
  compressWithLanguageDetection as compressText,
5476
5658
  exports_schemas_symbol as SymbolTypes,
5477
- exports_jsonrpc as JSONRPCTypes,
5659
+ JSONRPCUtils,
5660
+ JSONRPCManager,
5661
+ JSONRPCErrorCode,
5478
5662
  JSONRPCCall,
5479
5663
  ErrorCode
5480
5664
  };
@@ -10,6 +10,7 @@ export { loadVersion } from "./util_version";
10
10
  export { compressWithLanguageDetection as compressText } from "./util_text_compression";
11
11
  export { JSONRPCCall, createDataPendingOperations } from "./util_pending";
12
12
  export type { DataPendingOperation, PendingOperation } from "./util_pending";
13
- export * as JSONRPCTypes from "./schemas/jsonrpc";
14
- export type { JSONRPCMessage, JSONRPCRequest, JSONRPCNotification, JSONRPCResponse, JSONRPCBatchRequest, JSONRPCBatchResponse, JSONRPCErrorResponse, JSONRPCValidationResult, CommandExecuteParams, NotificationSendParams, JSONRPCError, JSONRPCParams, } from "./schemas/jsonrpc";
15
- export type { JSONRPCManagerEvents } from "./jsonrpc-manager";
13
+ export { JSONRPCUtils, JSONRPCErrorCode } from "./schemas/jsonrpc";
14
+ export type { JSONRPCMessage, JSONRPCRequest, JSONRPCNotification, JSONRPCResponse, JSONRPCBatchRequest, JSONRPCBatchResponse, JSONRPCErrorResponse, JSONRPCValidationResult, JSONRPCError, JSONRPCParams, CommandExecuteParams, NotificationSendParams, } from "./schemas/jsonrpc";
15
+ export { JSONRPCManager } from "./jsonrpc-manager";
16
+ export type { JSONRPCManagerEvents, JSONRPCManagerConfig, } from "./jsonrpc-manager";
@@ -31,7 +31,7 @@ interface JSONRPCMethodConfig<TContext, T = JSONRPCParams> {
31
31
  /**
32
32
  * JSON-RPC Manager configuration
33
33
  */
34
- interface JSONRPCManagerConfig<TContext> {
34
+ export interface JSONRPCManagerConfig<TContext> {
35
35
  /** Enable strict JSON-RPC 2.0 validation */
36
36
  strictValidation?: boolean;
37
37
  /** Default timeout for requests */
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.8.0",
2
+ "version": "0.8.2",
3
3
  "name": "modality-kit",
4
4
  "repository": {
5
5
  "type": "git",