modality-kit 0.14.4 → 0.14.6

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
@@ -16,6 +16,100 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
16
16
  throw Error('Dynamic require of "' + x + '" is not supported');
17
17
  });
18
18
 
19
+ // src/ErrorCode.ts
20
+ class ErrorCode extends Error {
21
+ cause;
22
+ constructor(message, originalError) {
23
+ super(message);
24
+ this.name = this.constructor.name;
25
+ if (originalError) {
26
+ this.cause = originalError;
27
+ }
28
+ }
29
+ }
30
+
31
+ // src/util_response.ts
32
+ var ContentType = [
33
+ "text",
34
+ "image",
35
+ "audio",
36
+ "resource_link",
37
+ "resource"
38
+ ];
39
+ function formatSuccessResponse(successData, meta) {
40
+ const data = structuredClone(successData);
41
+ const { instructions, content: dataContent, ...restData } = data;
42
+ const result = {
43
+ isError: false,
44
+ structuredContent: {
45
+ instructions,
46
+ meta,
47
+ ...restData
48
+ },
49
+ content: []
50
+ };
51
+ if (Array.isArray(dataContent)) {
52
+ result.content = dataContent.map((item) => {
53
+ if (typeof item === "string") {
54
+ return { type: "text", text: item };
55
+ } else if (item.type && ContentType.includes(item.type)) {
56
+ return item;
57
+ } else {
58
+ return { type: "text", text: JSON.stringify(item) };
59
+ }
60
+ });
61
+ }
62
+ return result;
63
+ }
64
+ function formatErrorResponse(errorData, operation, meta) {
65
+ let errorResponse;
66
+ let isError = true;
67
+ if (typeof errorData === "string") {
68
+ errorResponse = {
69
+ error: errorData,
70
+ operation,
71
+ meta
72
+ };
73
+ } else if (errorData instanceof ErrorCode) {
74
+ errorResponse = {
75
+ error: errorData.message,
76
+ operation,
77
+ meta,
78
+ code: errorData.code,
79
+ reason: errorData.cause?.message
80
+ };
81
+ } else if (errorData instanceof Error) {
82
+ errorResponse = {
83
+ error: errorData.message,
84
+ operation,
85
+ meta
86
+ };
87
+ } else if (typeof errorData === "object" && errorData !== null && typeof errorData.message === "string") {
88
+ const errObj = errorData;
89
+ isError = !errObj.success;
90
+ errorResponse = {
91
+ error: errObj.message,
92
+ code: errObj.code,
93
+ operation: errObj.operation || operation,
94
+ meta
95
+ };
96
+ } else {
97
+ errorResponse = {
98
+ error: "Unknown error",
99
+ operation,
100
+ meta
101
+ };
102
+ }
103
+ return {
104
+ isError,
105
+ content: [
106
+ {
107
+ type: "text",
108
+ text: JSON.stringify(errorResponse)
109
+ }
110
+ ]
111
+ };
112
+ }
19
113
  // src/util_logger.ts
20
114
  var levels = [null, "debug", "info", "warn", "error", "success"];
21
115
 
@@ -170,20 +264,8 @@ class ModalityLogger {
170
264
  }
171
265
  }
172
266
  var getLoggerInstance = ModalityLogger.getInstance.bind(ModalityLogger);
173
-
174
267
  // src/util_error.ts
175
268
  var logger = getLoggerInstance("Safe to Handle");
176
-
177
- class ErrorCode extends Error {
178
- cause;
179
- constructor(message, originalError) {
180
- super(message);
181
- this.name = this.constructor.name;
182
- if (originalError) {
183
- this.cause = originalError;
184
- }
185
- }
186
- }
187
269
  function withErrorHandling(fn, operation) {
188
270
  return async (...args) => {
189
271
  try {
@@ -204,68 +286,6 @@ function withErrorHandling(fn, operation) {
204
286
  }
205
287
  };
206
288
  }
207
-
208
- // src/util_response.ts
209
- function formatSuccessResponse(content, meta) {
210
- let otherContent;
211
- const instructions = content.instructions;
212
- if (instructions != null) {
213
- const { instructions: instructions2, ...restContent } = content;
214
- otherContent = JSON.parse(JSON.stringify(restContent || {}));
215
- } else {
216
- otherContent = JSON.parse(JSON.stringify(content || {}));
217
- }
218
- return JSON.stringify({
219
- success: true,
220
- instructions,
221
- content: Object.keys(otherContent || {}).length ? otherContent : undefined,
222
- meta
223
- });
224
- }
225
- function formatErrorResponse(errorData, operation, meta) {
226
- let errorResponse;
227
- if (typeof errorData === "string") {
228
- errorResponse = {
229
- success: false,
230
- error: errorData,
231
- operation,
232
- meta
233
- };
234
- } else if (errorData instanceof ErrorCode) {
235
- errorResponse = {
236
- success: false,
237
- error: errorData.message,
238
- code: errorData.code,
239
- reason: errorData.cause?.message,
240
- operation,
241
- meta
242
- };
243
- } else if (errorData instanceof Error) {
244
- errorResponse = {
245
- success: false,
246
- error: errorData.message,
247
- operation,
248
- meta
249
- };
250
- } else if (typeof errorData === "object" && errorData !== null && typeof errorData.message === "string") {
251
- const errObj = errorData;
252
- errorResponse = {
253
- success: errObj.success || false,
254
- error: errObj.message,
255
- code: errObj.code,
256
- operation: errObj.operation || operation,
257
- meta
258
- };
259
- } else {
260
- errorResponse = {
261
- success: false,
262
- error: "Unknown error",
263
- operation,
264
- meta
265
- };
266
- }
267
- return JSON.stringify(errorResponse);
268
- }
269
289
  // src/schemas/schemas_symbol.ts
270
290
  var exports_schemas_symbol = {};
271
291
  __export(exports_schemas_symbol, {
@@ -5947,6 +5967,48 @@ class LruCache {
5947
5967
  return [...this._values.keys()];
5948
5968
  }
5949
5969
  }
5970
+ // src/simple-cache.ts
5971
+ class SimpleCache {
5972
+ lruCache;
5973
+ ttlMs;
5974
+ maxSize;
5975
+ constructor(options = {}) {
5976
+ this.ttlMs = options.ttlMs ?? 300000;
5977
+ this.maxSize = options.maxSize ?? 100;
5978
+ this.lruCache = new LruCache(this.maxSize);
5979
+ }
5980
+ set(key, data, ttlMs) {
5981
+ const entry = {
5982
+ data,
5983
+ timestamp: Date.now(),
5984
+ ttl: ttlMs ?? this.ttlMs
5985
+ };
5986
+ this.lruCache.set(key, entry);
5987
+ }
5988
+ get(key, ignoreTTL) {
5989
+ const entry = this.lruCache.get(key);
5990
+ if (!entry)
5991
+ return null;
5992
+ if (entry.ttl !== undefined && !ignoreTTL) {
5993
+ const age = Date.now() - entry.timestamp;
5994
+ if (age > entry.ttl) {
5995
+ this.lruCache.delete(key);
5996
+ return null;
5997
+ }
5998
+ }
5999
+ return entry.data;
6000
+ }
6001
+ has(key) {
6002
+ return this.lruCache.has(key);
6003
+ }
6004
+ delete(key) {
6005
+ const existed = this.lruCache.delete(key);
6006
+ return existed;
6007
+ }
6008
+ keys() {
6009
+ return this.lruCache.keys();
6010
+ }
6011
+ }
5950
6012
  export {
5951
6013
  withErrorHandling,
5952
6014
  loadVersion,
@@ -5959,6 +6021,7 @@ export {
5959
6021
  compressWithLanguageDetection as compressText,
5960
6022
  WebSocketClient,
5961
6023
  exports_schemas_symbol as SymbolTypes,
6024
+ SimpleCache,
5962
6025
  LruCache,
5963
6026
  JSONRPCUtils,
5964
6027
  JSONRPCManager,
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Base class for task-related errors
3
+ */
4
+ export declare abstract class ErrorCode extends Error {
5
+ abstract readonly code: string | number;
6
+ cause?: Error;
7
+ constructor(message: string, originalError?: unknown);
8
+ }
@@ -1,6 +1,7 @@
1
1
  export { formatErrorResponse, formatSuccessResponse } from "./util_response";
2
2
  export { getLoggerInstance, type ModalityLogger } from "./util_logger";
3
- export { withErrorHandling, ErrorCode } from "./util_error";
3
+ export { withErrorHandling } from "./util_error";
4
+ export { ErrorCode } from "./ErrorCode";
4
5
  export * as SymbolTypes from "./schemas/schemas_symbol";
5
6
  export type { EmptyType } from "./schemas/schemas_empty";
6
7
  export { emptySchema } from "./schemas/schemas_empty";
@@ -23,3 +24,5 @@ export { ERROR_METHOD_NOT_FOUND } from "./jsonrpc-manager";
23
24
  export { isTestEnvironment } from "./util_tests/isTestEnvironment";
24
25
  export { WebSocketClient } from "./websocket-client";
25
26
  export { LruCache } from "./lruCache";
27
+ export { SimpleCache } from "./simple-cache";
28
+ export type { SimpleCacheOptions } from "./simple-cache";
@@ -4,7 +4,7 @@
4
4
  * Clean JSON-RPC 2.0 implementation for communication.
5
5
  * Provides method registration, routing, and lifecycle management.
6
6
  */
7
- import { ErrorCode } from "./util_error";
7
+ import { ErrorCode } from "./ErrorCode";
8
8
  import { JSONRPCCall } from "./util_pending";
9
9
  import { JSONRPCErrorCode } from "./schemas/jsonrpc";
10
10
  import type { JSONRPCRequest, JSONRPCMessage, JSONRPCError, JSONRPCParams } from "./schemas/jsonrpc";
@@ -91,12 +91,12 @@ declare const fileEntrySchema: z.ZodObject<{
91
91
  type: z.ZodEnum<["file", "directory"]>;
92
92
  lastModified: z.ZodNullable<z.ZodOptional<z.ZodNumber>>;
93
93
  }, "strip", z.ZodTypeAny, {
94
- path: string;
95
94
  type: "file" | "directory";
95
+ path: string;
96
96
  lastModified?: number | null | undefined;
97
97
  }, {
98
- path: string;
99
98
  type: "file" | "directory";
99
+ path: string;
100
100
  lastModified?: number | null | undefined;
101
101
  }>;
102
102
  export type FileEntryType = z.infer<typeof fileEntrySchema>;
@@ -0,0 +1,48 @@
1
+ /**
2
+ * SimpleCache - Unified caching utility with TTL and LRU support
3
+ *
4
+ * Supports two caching strategies:
5
+ * - TTL-based: Entries expire after specified time
6
+ * - LRU: Least Recently Used eviction when size limit reached
7
+ * - Hybrid: Both TTL and LRU can be enabled together
8
+ */
9
+ /**
10
+ * Cache options
11
+ */
12
+ export interface SimpleCacheOptions {
13
+ /** TTL in milliseconds. Set to undefined to disable TTL. Default: 300000 (5 minutes) */
14
+ ttlMs?: number;
15
+ /** Enable LRU eviction. Default: false */
16
+ enableLru?: boolean;
17
+ /** Max cache size for LRU. Default: 100 */
18
+ maxSize?: number;
19
+ }
20
+ /**
21
+ * SimpleCache - Generic cache supporting TTL and/or LRU eviction
22
+ */
23
+ export declare class SimpleCache<T> {
24
+ private lruCache;
25
+ private readonly ttlMs;
26
+ private readonly maxSize;
27
+ constructor(options?: SimpleCacheOptions);
28
+ /**
29
+ * Set cache entry
30
+ * @param key Cache key
31
+ * @param data Data to cache
32
+ * @param ttlMs Optional override for TTL (if undefined, uses instance TTL)
33
+ */
34
+ set(key: string, data: T, ttlMs?: number): void;
35
+ /**
36
+ * Get cache entry if still valid (not expired and not evicted by LRU)
37
+ */
38
+ get(key: string, ignoreTTL?: boolean): T | null;
39
+ /**
40
+ * Check if key exists and is valid
41
+ */
42
+ has(key: string): boolean;
43
+ /**
44
+ * Delete a specific key
45
+ */
46
+ delete(key: string): boolean;
47
+ keys(): string[];
48
+ }
@@ -1,11 +1,3 @@
1
- /**
2
- * Base class for task-related errors
3
- */
4
- export declare abstract class ErrorCode extends Error {
5
- abstract readonly code: string | number;
6
- cause?: Error;
7
- constructor(message: string, originalError?: unknown);
8
- }
9
1
  /**
10
2
  * Wrapper for any functions that adds consistent error handling
11
3
  */
@@ -1,16 +1,11 @@
1
+ import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
1
2
  /**
2
3
  * Utility functions for formatting MCP responses
3
4
  *
4
5
  * This is a global utility module that should not import domain-specific classes.
5
6
  * It provides generic response formatting for any MCP tool.
6
7
  */
7
- export interface McpSuccessResponse {
8
- success: true;
9
- content: any;
10
- meta?: any;
11
- }
12
8
  export interface McpErrorResponse {
13
- success: boolean;
14
9
  code?: string | number;
15
10
  error: string;
16
11
  operation?: string;
@@ -30,12 +25,9 @@ interface ErrorData extends Record<string, any> {
30
25
  message: string;
31
26
  operation?: string;
32
27
  }
33
- /**
34
- * Format a successful response for MCP tools
35
- */
36
- export declare function formatSuccessResponse(content: SuccessData, meta?: any): string;
28
+ export declare function formatSuccessResponse(successData: SuccessData, meta?: any): CallToolResult;
37
29
  /**
38
30
  * Format an error response for MCP tools using generic error data
39
31
  */
40
- export declare function formatErrorResponse(errorData: ErrorData | Error | string | unknown, operation?: string, meta?: Record<string, any>): string;
32
+ export declare function formatErrorResponse(errorData: ErrorData | Error | string | unknown, operation?: string, meta?: Record<string, any>): CallToolResult;
41
33
  export {};
@@ -1,5 +1,5 @@
1
1
  import { ModalityLogger } from './util_logger.js';
2
- import { ErrorCode } from './util_error.js';
2
+ import { ErrorCode } from './ErrorCode';
3
3
  export interface CompressionConfig {
4
4
  maxTokens: number;
5
5
  compressionLevel: "light" | "moderate" | "aggressive";
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.14.4",
2
+ "version": "0.14.6",
3
3
  "name": "modality-kit",
4
4
  "repository": {
5
5
  "type": "git",
@@ -11,6 +11,7 @@
11
11
  "author": "Hill <hill@kimo.com>",
12
12
  "license": "ISC",
13
13
  "devDependencies": {
14
+ "@modelcontextprotocol/sdk": "^1.24.2",
14
15
  "@types/bun": "^1.2.23",
15
16
  "typescript": "^5.9.2",
16
17
  "zod": "^3.25.76"