obsidian-mcp-server 1.5.8 → 2.0.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/README.md +248 -104
- package/dist/config/index.d.ts +41 -0
- package/dist/config/index.js +191 -0
- package/dist/index.d.ts +1 -5
- package/dist/index.js +296 -18
- package/dist/mcp-server/server.d.ts +33 -0
- package/dist/mcp-server/server.js +211 -0
- package/dist/mcp-server/tools/obsidianDeleteFileTool/index.d.ts +12 -0
- package/dist/mcp-server/tools/obsidianDeleteFileTool/index.js +12 -0
- package/dist/mcp-server/tools/obsidianDeleteFileTool/logic.d.ts +51 -0
- package/dist/mcp-server/tools/obsidianDeleteFileTool/logic.js +168 -0
- package/dist/mcp-server/tools/obsidianDeleteFileTool/registration.d.ts +19 -0
- package/dist/mcp-server/tools/obsidianDeleteFileTool/registration.js +91 -0
- package/dist/mcp-server/tools/obsidianGlobalSearchTool/index.d.ts +12 -0
- package/dist/mcp-server/tools/obsidianGlobalSearchTool/index.js +12 -0
- package/dist/mcp-server/tools/obsidianGlobalSearchTool/logic.d.ts +77 -0
- package/dist/mcp-server/tools/obsidianGlobalSearchTool/logic.js +341 -0
- package/dist/mcp-server/tools/obsidianGlobalSearchTool/registration.d.ts +18 -0
- package/dist/mcp-server/tools/obsidianGlobalSearchTool/registration.js +69 -0
- package/dist/mcp-server/tools/obsidianListFilesTool/index.d.ts +12 -0
- package/dist/mcp-server/tools/obsidianListFilesTool/index.js +12 -0
- package/dist/mcp-server/tools/obsidianListFilesTool/logic.d.ts +64 -0
- package/dist/mcp-server/tools/obsidianListFilesTool/logic.js +179 -0
- package/dist/mcp-server/tools/obsidianListFilesTool/registration.d.ts +19 -0
- package/dist/mcp-server/tools/obsidianListFilesTool/registration.js +96 -0
- package/dist/mcp-server/tools/obsidianManageFrontmatterTool/index.d.ts +3 -0
- package/dist/mcp-server/tools/obsidianManageFrontmatterTool/index.js +2 -0
- package/dist/mcp-server/tools/obsidianManageFrontmatterTool/logic.d.ts +42 -0
- package/dist/mcp-server/tools/obsidianManageFrontmatterTool/logic.js +152 -0
- package/dist/mcp-server/tools/obsidianManageFrontmatterTool/registration.d.ts +3 -0
- package/dist/mcp-server/tools/obsidianManageFrontmatterTool/registration.js +52 -0
- package/dist/mcp-server/tools/obsidianManageTagsTool/index.d.ts +3 -0
- package/dist/mcp-server/tools/obsidianManageTagsTool/index.js +2 -0
- package/dist/mcp-server/tools/obsidianManageTagsTool/logic.d.ts +28 -0
- package/dist/mcp-server/tools/obsidianManageTagsTool/logic.js +161 -0
- package/dist/mcp-server/tools/obsidianManageTagsTool/registration.d.ts +3 -0
- package/dist/mcp-server/tools/obsidianManageTagsTool/registration.js +52 -0
- package/dist/mcp-server/tools/obsidianReadFileTool/index.d.ts +12 -0
- package/dist/mcp-server/tools/obsidianReadFileTool/index.js +12 -0
- package/dist/mcp-server/tools/obsidianReadFileTool/logic.d.ts +87 -0
- package/dist/mcp-server/tools/obsidianReadFileTool/logic.js +216 -0
- package/dist/mcp-server/tools/obsidianReadFileTool/registration.d.ts +20 -0
- package/dist/mcp-server/tools/obsidianReadFileTool/registration.js +101 -0
- package/dist/mcp-server/tools/obsidianSearchReplaceTool/index.d.ts +12 -0
- package/dist/mcp-server/tools/obsidianSearchReplaceTool/index.js +12 -0
- package/dist/mcp-server/tools/obsidianSearchReplaceTool/logic.d.ts +255 -0
- package/dist/mcp-server/tools/obsidianSearchReplaceTool/logic.js +583 -0
- package/dist/mcp-server/tools/obsidianSearchReplaceTool/registration.d.ts +22 -0
- package/dist/mcp-server/tools/obsidianSearchReplaceTool/registration.js +111 -0
- package/dist/mcp-server/tools/obsidianUpdateFileTool/index.d.ts +12 -0
- package/dist/mcp-server/tools/obsidianUpdateFileTool/index.js +12 -0
- package/dist/mcp-server/tools/obsidianUpdateFileTool/logic.d.ts +183 -0
- package/dist/mcp-server/tools/obsidianUpdateFileTool/logic.js +490 -0
- package/dist/mcp-server/tools/obsidianUpdateFileTool/registration.d.ts +21 -0
- package/dist/mcp-server/tools/obsidianUpdateFileTool/registration.js +108 -0
- package/dist/mcp-server/transports/authentication/authContext.d.ts +33 -0
- package/dist/mcp-server/transports/authentication/authContext.js +24 -0
- package/dist/mcp-server/transports/authentication/authMiddleware.d.ts +30 -0
- package/dist/mcp-server/transports/authentication/authMiddleware.js +145 -0
- package/dist/mcp-server/transports/authentication/authUtils.d.ts +18 -0
- package/dist/mcp-server/transports/authentication/authUtils.js +45 -0
- package/dist/mcp-server/transports/authentication/oauthMiddleware.d.ts +24 -0
- package/dist/mcp-server/transports/authentication/oauthMiddleware.js +109 -0
- package/dist/mcp-server/transports/authentication/types.d.ts +17 -0
- package/dist/mcp-server/transports/authentication/types.js +5 -0
- package/dist/mcp-server/transports/httpTransport.d.ts +24 -0
- package/dist/mcp-server/transports/httpTransport.js +496 -0
- package/dist/mcp-server/transports/stdioTransport.d.ts +42 -0
- package/dist/mcp-server/transports/stdioTransport.js +63 -0
- package/dist/services/obsidianRestAPI/index.d.ts +15 -0
- package/dist/services/obsidianRestAPI/index.js +17 -0
- package/dist/services/obsidianRestAPI/methods/activeFileMethods.d.ts +38 -0
- package/dist/services/obsidianRestAPI/methods/activeFileMethods.js +62 -0
- package/dist/services/obsidianRestAPI/methods/commandMethods.d.ts +22 -0
- package/dist/services/obsidianRestAPI/methods/commandMethods.js +31 -0
- package/dist/services/obsidianRestAPI/methods/openMethods.d.ts +16 -0
- package/dist/services/obsidianRestAPI/methods/openMethods.js +21 -0
- package/dist/services/obsidianRestAPI/methods/patchMethods.d.ts +37 -0
- package/dist/services/obsidianRestAPI/methods/patchMethods.js +94 -0
- package/dist/services/obsidianRestAPI/methods/periodicNoteMethods.d.ts +42 -0
- package/dist/services/obsidianRestAPI/methods/periodicNoteMethods.js +66 -0
- package/dist/services/obsidianRestAPI/methods/searchMethods.d.ts +25 -0
- package/dist/services/obsidianRestAPI/methods/searchMethods.js +36 -0
- package/dist/services/obsidianRestAPI/methods/vaultMethods.d.ts +58 -0
- package/dist/services/obsidianRestAPI/methods/vaultMethods.js +144 -0
- package/dist/services/obsidianRestAPI/service.d.ts +195 -0
- package/dist/services/obsidianRestAPI/service.js +379 -0
- package/dist/services/obsidianRestAPI/types.d.ts +127 -0
- package/dist/services/obsidianRestAPI/types.js +7 -0
- package/dist/services/obsidianRestAPI/vaultCache/index.d.ts +4 -0
- package/dist/services/obsidianRestAPI/vaultCache/index.js +4 -0
- package/dist/services/obsidianRestAPI/vaultCache/service.d.ts +88 -0
- package/dist/services/obsidianRestAPI/vaultCache/service.js +299 -0
- package/dist/types-global/errors.d.ts +73 -0
- package/dist/types-global/errors.js +71 -0
- package/dist/utils/index.d.ts +5 -8
- package/dist/utils/index.js +13 -9
- package/dist/utils/internal/asyncUtils.d.ts +54 -0
- package/dist/utils/internal/asyncUtils.js +101 -0
- package/dist/utils/internal/errorHandler.d.ts +176 -0
- package/dist/utils/internal/errorHandler.js +351 -0
- package/dist/utils/internal/index.d.ts +4 -0
- package/dist/utils/internal/index.js +4 -0
- package/dist/utils/internal/logger.d.ts +141 -0
- package/dist/utils/internal/logger.js +406 -0
- package/dist/utils/internal/requestContext.d.ts +83 -0
- package/dist/utils/internal/requestContext.js +72 -0
- package/dist/utils/metrics/index.d.ts +1 -0
- package/dist/utils/metrics/index.js +1 -0
- package/dist/utils/metrics/tokenCounter.d.ts +27 -0
- package/dist/utils/metrics/tokenCounter.js +128 -0
- package/dist/utils/obsidian/index.d.ts +5 -0
- package/dist/utils/obsidian/index.js +5 -0
- package/dist/utils/obsidian/obsidianApiUtils.d.ts +14 -0
- package/dist/utils/obsidian/obsidianApiUtils.js +29 -0
- package/dist/utils/obsidian/obsidianStatUtils.d.ts +68 -0
- package/dist/utils/obsidian/obsidianStatUtils.js +143 -0
- package/dist/utils/parsing/dateParser.d.ts +56 -0
- package/dist/utils/parsing/dateParser.js +104 -0
- package/dist/utils/parsing/index.d.ts +2 -0
- package/dist/utils/parsing/index.js +3 -0
- package/dist/utils/parsing/jsonParser.d.ts +80 -0
- package/dist/utils/parsing/jsonParser.js +133 -0
- package/dist/utils/security/idGenerator.d.ts +140 -0
- package/dist/utils/security/idGenerator.js +194 -0
- package/dist/utils/security/index.d.ts +3 -0
- package/dist/utils/security/index.js +3 -0
- package/dist/utils/security/rateLimiter.d.ts +156 -0
- package/dist/utils/security/rateLimiter.js +235 -0
- package/dist/utils/security/sanitization.d.ts +244 -0
- package/dist/utils/security/sanitization.js +599 -0
- package/package.json +58 -37
- package/dist/index.js.map +0 -1
- package/dist/mcp/handlers.d.ts +0 -29
- package/dist/mcp/handlers.js +0 -305
- package/dist/mcp/handlers.js.map +0 -1
- package/dist/mcp/index.d.ts +0 -6
- package/dist/mcp/index.js +0 -7
- package/dist/mcp/index.js.map +0 -1
- package/dist/mcp/server.d.ts +0 -18
- package/dist/mcp/server.js +0 -240
- package/dist/mcp/server.js.map +0 -1
- package/dist/mcp/types.d.ts +0 -70
- package/dist/mcp/types.js +0 -49
- package/dist/mcp/types.js.map +0 -1
- package/dist/obsidian/client.d.ts +0 -109
- package/dist/obsidian/client.js +0 -403
- package/dist/obsidian/client.js.map +0 -1
- package/dist/obsidian/errors.d.ts +0 -28
- package/dist/obsidian/errors.js +0 -75
- package/dist/obsidian/errors.js.map +0 -1
- package/dist/obsidian/index.d.ts +0 -6
- package/dist/obsidian/index.js +0 -7
- package/dist/obsidian/index.js.map +0 -1
- package/dist/obsidian/types.d.ts +0 -107
- package/dist/obsidian/types.js +0 -12
- package/dist/obsidian/types.js.map +0 -1
- package/dist/resources/index.d.ts +0 -13
- package/dist/resources/index.js +0 -15
- package/dist/resources/index.js.map +0 -1
- package/dist/resources/tags.d.ts +0 -39
- package/dist/resources/tags.js +0 -257
- package/dist/resources/tags.js.map +0 -1
- package/dist/resources/types.d.ts +0 -27
- package/dist/resources/types.js +0 -5
- package/dist/resources/types.js.map +0 -1
- package/dist/tools/base.d.ts +0 -46
- package/dist/tools/base.js +0 -88
- package/dist/tools/base.js.map +0 -1
- package/dist/tools/files/content.d.ts +0 -58
- package/dist/tools/files/content.js +0 -171
- package/dist/tools/files/content.js.map +0 -1
- package/dist/tools/files/index.d.ts +0 -14
- package/dist/tools/files/index.js +0 -22
- package/dist/tools/files/index.js.map +0 -1
- package/dist/tools/files/list.d.ts +0 -35
- package/dist/tools/files/list.js +0 -133
- package/dist/tools/files/list.js.map +0 -1
- package/dist/tools/index.d.ts +0 -21
- package/dist/tools/index.js +0 -31
- package/dist/tools/index.js.map +0 -1
- package/dist/tools/properties/index.d.ts +0 -14
- package/dist/tools/properties/index.js +0 -19
- package/dist/tools/properties/index.js.map +0 -1
- package/dist/tools/properties/manager.d.ts +0 -62
- package/dist/tools/properties/manager.js +0 -302
- package/dist/tools/properties/manager.js.map +0 -1
- package/dist/tools/properties/tools.d.ts +0 -47
- package/dist/tools/properties/tools.js +0 -239
- package/dist/tools/properties/tools.js.map +0 -1
- package/dist/tools/properties/types.d.ts +0 -141
- package/dist/tools/properties/types.js +0 -70
- package/dist/tools/properties/types.js.map +0 -1
- package/dist/tools/search/complex.d.ts +0 -38
- package/dist/tools/search/complex.js +0 -270
- package/dist/tools/search/complex.js.map +0 -1
- package/dist/tools/search/index.d.ts +0 -14
- package/dist/tools/search/index.js +0 -20
- package/dist/tools/search/index.js.map +0 -1
- package/dist/tools/search/simple.d.ts +0 -25
- package/dist/tools/search/simple.js +0 -127
- package/dist/tools/search/simple.js.map +0 -1
- package/dist/utils/errors.d.ts +0 -24
- package/dist/utils/errors.js +0 -59
- package/dist/utils/errors.js.map +0 -1
- package/dist/utils/idGenerator.d.ts +0 -15
- package/dist/utils/idGenerator.js +0 -21
- package/dist/utils/idGenerator.js.map +0 -1
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/logging.d.ts +0 -245
- package/dist/utils/logging.js +0 -417
- package/dist/utils/logging.js.map +0 -1
- package/dist/utils/rate-limiting.d.ts +0 -50
- package/dist/utils/rate-limiting.js +0 -94
- package/dist/utils/rate-limiting.js.map +0 -1
- package/dist/utils/tokenization.d.ts +0 -28
- package/dist/utils/tokenization.js +0 -75
- package/dist/utils/tokenization.js.map +0 -1
- package/dist/utils/validation.d.ts +0 -22
- package/dist/utils/validation.js +0 -92
- package/dist/utils/validation.js.map +0 -1
package/dist/utils/logging.js
DELETED
|
@@ -1,417 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Standardized Logging System for the Obsidian MCP Server
|
|
3
|
-
*
|
|
4
|
-
* This module provides structured logging capabilities aligned with MCP best practices.
|
|
5
|
-
* It supports categorized errors, severity levels, context tracking, performance
|
|
6
|
-
* monitoring, and secure logging patterns.
|
|
7
|
-
*
|
|
8
|
-
* @module utils/logging
|
|
9
|
-
*/
|
|
10
|
-
import fs from "fs";
|
|
11
|
-
import path from "path";
|
|
12
|
-
import process from "process";
|
|
13
|
-
import winston from "winston";
|
|
14
|
-
/**
|
|
15
|
-
* Error categories following MCP standards
|
|
16
|
-
*/
|
|
17
|
-
export var ErrorCategoryType;
|
|
18
|
-
(function (ErrorCategoryType) {
|
|
19
|
-
ErrorCategoryType["CATEGORY_VALIDATION"] = "VALIDATION";
|
|
20
|
-
ErrorCategoryType["CATEGORY_AUTHENTICATION"] = "AUTHENTICATION";
|
|
21
|
-
ErrorCategoryType["CATEGORY_AUTHORIZATION"] = "AUTHORIZATION";
|
|
22
|
-
ErrorCategoryType["CATEGORY_BUSINESS_LOGIC"] = "BUSINESS_LOGIC";
|
|
23
|
-
ErrorCategoryType["CATEGORY_DATA_ACCESS"] = "DATA_ACCESS";
|
|
24
|
-
ErrorCategoryType["CATEGORY_EXTERNAL_SERVICE"] = "EXTERNAL_SERVICE";
|
|
25
|
-
ErrorCategoryType["CATEGORY_SYSTEM"] = "SYSTEM";
|
|
26
|
-
ErrorCategoryType["CATEGORY_UNKNOWN"] = "UNKNOWN";
|
|
27
|
-
})(ErrorCategoryType || (ErrorCategoryType = {}));
|
|
28
|
-
/**
|
|
29
|
-
* Log levels aligned with MCP's ErrorSeverityLevel
|
|
30
|
-
*/
|
|
31
|
-
export var LogLevel;
|
|
32
|
-
(function (LogLevel) {
|
|
33
|
-
/** Critical errors that require immediate attention */
|
|
34
|
-
LogLevel[LogLevel["ERROR"] = 0] = "ERROR";
|
|
35
|
-
/** Potentially harmful situations that should be reviewed */
|
|
36
|
-
LogLevel[LogLevel["WARN"] = 1] = "WARN";
|
|
37
|
-
/** General informational messages about system operation */
|
|
38
|
-
LogLevel[LogLevel["INFO"] = 2] = "INFO";
|
|
39
|
-
/** Detailed information for debugging purposes */
|
|
40
|
-
LogLevel[LogLevel["DEBUG"] = 3] = "DEBUG";
|
|
41
|
-
/** Highly detailed tracing information */
|
|
42
|
-
LogLevel[LogLevel["TRACE"] = 4] = "TRACE";
|
|
43
|
-
})(LogLevel || (LogLevel = {}));
|
|
44
|
-
/**
|
|
45
|
-
* Maps LogLevel to MCP severity level for integration with MCP error handling
|
|
46
|
-
*/
|
|
47
|
-
export const logLevelToMcpSeverity = {
|
|
48
|
-
[LogLevel.ERROR]: 3, // SEVERITY_ERROR
|
|
49
|
-
[LogLevel.WARN]: 2, // SEVERITY_WARN
|
|
50
|
-
[LogLevel.INFO]: 1, // SEVERITY_INFO
|
|
51
|
-
[LogLevel.DEBUG]: 0, // SEVERITY_DEBUG
|
|
52
|
-
[LogLevel.TRACE]: 0 // Also maps to SEVERITY_DEBUG
|
|
53
|
-
};
|
|
54
|
-
/**
|
|
55
|
-
* Winston-compatible log levels
|
|
56
|
-
*/
|
|
57
|
-
const winstonLogLevels = {
|
|
58
|
-
error: 0,
|
|
59
|
-
warn: 1,
|
|
60
|
-
info: 2,
|
|
61
|
-
debug: 3,
|
|
62
|
-
trace: 4
|
|
63
|
-
};
|
|
64
|
-
/**
|
|
65
|
-
* Default logger configuration
|
|
66
|
-
*/
|
|
67
|
-
const DEFAULT_CONFIG = {
|
|
68
|
-
level: process.env.NODE_ENV === 'production'
|
|
69
|
-
? LogLevel.INFO
|
|
70
|
-
: LogLevel.DEBUG,
|
|
71
|
-
includeTimestamps: true,
|
|
72
|
-
includeLevel: true,
|
|
73
|
-
maskSensitiveData: true,
|
|
74
|
-
sensitiveFields: ['password', 'token', 'secret', 'key', 'auth', 'credential'],
|
|
75
|
-
logDir: "logs",
|
|
76
|
-
files: true,
|
|
77
|
-
fileNames: {
|
|
78
|
-
combined: "combined.log",
|
|
79
|
-
error: "error.log",
|
|
80
|
-
warn: "warn.log",
|
|
81
|
-
info: "info.log",
|
|
82
|
-
debug: "debug.log"
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
/**
|
|
86
|
-
* Enhanced logger for MCP server operations with structured logging support
|
|
87
|
-
* Implements file-based logging with zero console output
|
|
88
|
-
*/
|
|
89
|
-
export class Logger {
|
|
90
|
-
name;
|
|
91
|
-
config;
|
|
92
|
-
timers = new Map();
|
|
93
|
-
logger;
|
|
94
|
-
/**
|
|
95
|
-
* Creates a new logger instance
|
|
96
|
-
*
|
|
97
|
-
* @param name - Component name for this logger
|
|
98
|
-
* @param config - Optional configuration overrides
|
|
99
|
-
*/
|
|
100
|
-
constructor(name, config = {}) {
|
|
101
|
-
this.name = name;
|
|
102
|
-
this.config = {
|
|
103
|
-
...DEFAULT_CONFIG,
|
|
104
|
-
...config,
|
|
105
|
-
fileNames: {
|
|
106
|
-
...DEFAULT_CONFIG.fileNames,
|
|
107
|
-
...config.fileNames
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
// Initialize logger with silent transport as fallback
|
|
111
|
-
this.logger = winston.createLogger({
|
|
112
|
-
levels: winstonLogLevels,
|
|
113
|
-
defaultMeta: { component: this.name },
|
|
114
|
-
transports: [
|
|
115
|
-
// Silent transport to prevent "no transports" warning
|
|
116
|
-
new winston.transports.Console({
|
|
117
|
-
silent: true
|
|
118
|
-
})
|
|
119
|
-
]
|
|
120
|
-
});
|
|
121
|
-
this.initializeLogger();
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Initialize or reinitialize the Winston logger
|
|
125
|
-
*/
|
|
126
|
-
initializeLogger() {
|
|
127
|
-
if (this.config.files && this.config.logDir) {
|
|
128
|
-
if (!fs.existsSync(this.config.logDir)) {
|
|
129
|
-
try {
|
|
130
|
-
fs.mkdirSync(this.config.logDir, { recursive: true });
|
|
131
|
-
}
|
|
132
|
-
catch (error) {
|
|
133
|
-
// Silently continue - we have a silent transport as fallback
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
// Create log format
|
|
138
|
-
const logFormat = winston.format.combine(winston.format.timestamp(), winston.format.errors({ stack: true }), winston.format.json());
|
|
139
|
-
// Initialize transports array with a silent transport to avoid "no transports" warning
|
|
140
|
-
const transports = [
|
|
141
|
-
new winston.transports.Console({
|
|
142
|
-
silent: true, // Silent transport that doesn't output logs
|
|
143
|
-
format: logFormat
|
|
144
|
-
})
|
|
145
|
-
];
|
|
146
|
-
// Add file transports if enabled
|
|
147
|
-
if (this.config.files && this.config.logDir) {
|
|
148
|
-
try {
|
|
149
|
-
const fileNames = this.config.fileNames || DEFAULT_CONFIG.fileNames;
|
|
150
|
-
// Combined log file
|
|
151
|
-
if (fileNames?.combined) {
|
|
152
|
-
transports.push(new winston.transports.File({
|
|
153
|
-
filename: path.join(this.config.logDir, fileNames.combined),
|
|
154
|
-
format: logFormat
|
|
155
|
-
}));
|
|
156
|
-
}
|
|
157
|
-
// Level-specific log files
|
|
158
|
-
if (fileNames?.error) {
|
|
159
|
-
transports.push(new winston.transports.File({
|
|
160
|
-
filename: path.join(this.config.logDir, fileNames.error),
|
|
161
|
-
level: 'error',
|
|
162
|
-
format: logFormat
|
|
163
|
-
}));
|
|
164
|
-
}
|
|
165
|
-
if (fileNames?.warn) {
|
|
166
|
-
transports.push(new winston.transports.File({
|
|
167
|
-
filename: path.join(this.config.logDir, fileNames.warn),
|
|
168
|
-
level: 'warn',
|
|
169
|
-
format: logFormat
|
|
170
|
-
}));
|
|
171
|
-
}
|
|
172
|
-
if (fileNames?.info) {
|
|
173
|
-
transports.push(new winston.transports.File({
|
|
174
|
-
filename: path.join(this.config.logDir, fileNames.info),
|
|
175
|
-
level: 'info',
|
|
176
|
-
format: logFormat
|
|
177
|
-
}));
|
|
178
|
-
}
|
|
179
|
-
if (fileNames?.debug) {
|
|
180
|
-
transports.push(new winston.transports.File({
|
|
181
|
-
filename: path.join(this.config.logDir, fileNames.debug),
|
|
182
|
-
level: 'debug',
|
|
183
|
-
format: logFormat
|
|
184
|
-
}));
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
catch (error) {
|
|
188
|
-
// Silently continue with just the silent transport if file transports fail
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
// Replace logger configuration
|
|
192
|
-
this.logger.configure({
|
|
193
|
-
levels: winstonLogLevels,
|
|
194
|
-
level: LogLevel[this.config.level].toLowerCase(),
|
|
195
|
-
defaultMeta: { component: this.name },
|
|
196
|
-
transports
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Maps internal LogLevel to Winston log level
|
|
201
|
-
*/
|
|
202
|
-
levelToWinstonLevel(level) {
|
|
203
|
-
const levelName = LogLevel[level].toLowerCase();
|
|
204
|
-
return levelName === 'trace' ? 'debug' : levelName;
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* Creates a standardized error object for MCP compatibility
|
|
208
|
-
*
|
|
209
|
-
* @param message - Human-readable error description
|
|
210
|
-
* @param code - Machine-readable error identifier
|
|
211
|
-
* @param category - Type of error
|
|
212
|
-
* @param context - Additional error context
|
|
213
|
-
* @returns Standardized error object
|
|
214
|
-
*/
|
|
215
|
-
createStandardizedError(message, code, category = ErrorCategoryType.CATEGORY_UNKNOWN, context) {
|
|
216
|
-
return {
|
|
217
|
-
errorMessage: message,
|
|
218
|
-
errorCode: code,
|
|
219
|
-
errorCategory: category,
|
|
220
|
-
errorSeverity: LogLevel.ERROR,
|
|
221
|
-
errorTimestamp: new Date().toISOString(),
|
|
222
|
-
errorContext: context ? this.processSensitiveData(context) : undefined,
|
|
223
|
-
errorStack: new Error().stack
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* Wraps an exception as a standardized error
|
|
228
|
-
*
|
|
229
|
-
* @param error - Original error object
|
|
230
|
-
* @param message - Optional override message
|
|
231
|
-
* @param category - Error category
|
|
232
|
-
* @returns Standardized error object
|
|
233
|
-
*/
|
|
234
|
-
wrapExceptionAsStandardizedError(error, message, category = ErrorCategoryType.CATEGORY_UNKNOWN) {
|
|
235
|
-
const errorObject = error instanceof Error ? error : new Error(String(error));
|
|
236
|
-
return {
|
|
237
|
-
errorMessage: message || errorObject.message,
|
|
238
|
-
errorCode: errorObject.name || 'UNKNOWN_ERROR',
|
|
239
|
-
errorCategory: category,
|
|
240
|
-
errorSeverity: LogLevel.ERROR,
|
|
241
|
-
errorTimestamp: new Date().toISOString(),
|
|
242
|
-
errorStack: errorObject.stack
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Masks sensitive data in objects before logging
|
|
247
|
-
*
|
|
248
|
-
* @param data - Data object to process
|
|
249
|
-
* @returns Processed data with sensitive fields masked
|
|
250
|
-
*/
|
|
251
|
-
processSensitiveData(data) {
|
|
252
|
-
if (!this.config.maskSensitiveData)
|
|
253
|
-
return data;
|
|
254
|
-
const result = {};
|
|
255
|
-
const sensitiveFields = this.config.sensitiveFields;
|
|
256
|
-
for (const [key, value] of Object.entries(data)) {
|
|
257
|
-
// Check if key contains any sensitive field name
|
|
258
|
-
const isSensitive = sensitiveFields.some(field => key.toLowerCase().includes(field.toLowerCase()));
|
|
259
|
-
if (isSensitive) {
|
|
260
|
-
// Mask sensitive values
|
|
261
|
-
result[key] = '********';
|
|
262
|
-
}
|
|
263
|
-
else if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
264
|
-
// Recursively process nested objects
|
|
265
|
-
result[key] = this.processSensitiveData(value);
|
|
266
|
-
}
|
|
267
|
-
else {
|
|
268
|
-
// Pass through non-sensitive values
|
|
269
|
-
result[key] = value;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
return result;
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Internal method to log a message using Winston
|
|
276
|
-
*
|
|
277
|
-
* @param level - Log level
|
|
278
|
-
* @param message - Log message
|
|
279
|
-
* @param context - Optional context data
|
|
280
|
-
*/
|
|
281
|
-
log(level, message, context) {
|
|
282
|
-
if (level > this.config.level)
|
|
283
|
-
return;
|
|
284
|
-
const winstonLevel = this.levelToWinstonLevel(level);
|
|
285
|
-
const processedContext = context ? this.processSensitiveData(context) : undefined;
|
|
286
|
-
this.logger.log(winstonLevel, message, { context: processedContext });
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Log an error message with optional error object and context
|
|
290
|
-
*
|
|
291
|
-
* @param message - Error message
|
|
292
|
-
* @param errorOrContext - Error object or context
|
|
293
|
-
* @param context - Additional context
|
|
294
|
-
*/
|
|
295
|
-
error(message, errorOrContext, context) {
|
|
296
|
-
if (errorOrContext instanceof Error) {
|
|
297
|
-
const errorObj = this.wrapExceptionAsStandardizedError(errorOrContext);
|
|
298
|
-
const combinedContext = {
|
|
299
|
-
...(context || {}),
|
|
300
|
-
error: errorObj
|
|
301
|
-
};
|
|
302
|
-
this.log(LogLevel.ERROR, message, combinedContext);
|
|
303
|
-
}
|
|
304
|
-
else {
|
|
305
|
-
this.log(LogLevel.ERROR, message, errorOrContext || context);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Log a warning message
|
|
310
|
-
*
|
|
311
|
-
* @param message - Warning message
|
|
312
|
-
* @param context - Optional context data
|
|
313
|
-
*/
|
|
314
|
-
warn(message, context) {
|
|
315
|
-
this.log(LogLevel.WARN, message, context);
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Log an info message
|
|
319
|
-
*
|
|
320
|
-
* @param message - Info message
|
|
321
|
-
* @param context - Optional context data
|
|
322
|
-
*/
|
|
323
|
-
info(message, context) {
|
|
324
|
-
this.log(LogLevel.INFO, message, context);
|
|
325
|
-
}
|
|
326
|
-
/**
|
|
327
|
-
* Log a debug message
|
|
328
|
-
*
|
|
329
|
-
* @param message - Debug message
|
|
330
|
-
* @param context - Optional context data
|
|
331
|
-
*/
|
|
332
|
-
debug(message, context) {
|
|
333
|
-
this.log(LogLevel.DEBUG, message, context);
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Log a trace message
|
|
337
|
-
*
|
|
338
|
-
* @param message - Trace message
|
|
339
|
-
* @param context - Optional context data
|
|
340
|
-
*/
|
|
341
|
-
trace(message, context) {
|
|
342
|
-
this.log(LogLevel.TRACE, message, context);
|
|
343
|
-
}
|
|
344
|
-
/**
|
|
345
|
-
* Set the log level
|
|
346
|
-
*
|
|
347
|
-
* @param level - New log level
|
|
348
|
-
*/
|
|
349
|
-
setLevel(level) {
|
|
350
|
-
this.config.level = level;
|
|
351
|
-
this.logger.level = this.levelToWinstonLevel(level);
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* Start a timer for performance tracking
|
|
355
|
-
*
|
|
356
|
-
* @param id - Timer identifier
|
|
357
|
-
*/
|
|
358
|
-
startTimer(id) {
|
|
359
|
-
this.timers.set(id, performance.now());
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* End a timer and log the elapsed time
|
|
363
|
-
*
|
|
364
|
-
* @param id - Timer identifier
|
|
365
|
-
* @param message - Log message prefix
|
|
366
|
-
* @param level - Log level for the timing message
|
|
367
|
-
*/
|
|
368
|
-
endTimer(id, message = 'Operation completed', level = LogLevel.DEBUG) {
|
|
369
|
-
const startTime = this.timers.get(id);
|
|
370
|
-
if (startTime === undefined) {
|
|
371
|
-
this.warn(`Timer "${id}" does not exist`, { action: 'endTimer' });
|
|
372
|
-
return 0;
|
|
373
|
-
}
|
|
374
|
-
const endTime = performance.now();
|
|
375
|
-
const elapsedMs = endTime - startTime;
|
|
376
|
-
this.log(level, `${message} in ${elapsedMs.toFixed(2)}ms`, {
|
|
377
|
-
timerId: id,
|
|
378
|
-
processingTimeMs: elapsedMs
|
|
379
|
-
});
|
|
380
|
-
this.timers.delete(id);
|
|
381
|
-
return elapsedMs;
|
|
382
|
-
}
|
|
383
|
-
/**
|
|
384
|
-
* Log the result of an operation with timing information
|
|
385
|
-
*
|
|
386
|
-
* @param success - Whether the operation was successful
|
|
387
|
-
* @param operation - Operation name
|
|
388
|
-
* @param elapsedMs - Processing time in milliseconds
|
|
389
|
-
* @param context - Optional operation context
|
|
390
|
-
*/
|
|
391
|
-
logOperationResult(success, operation, elapsedMs, context) {
|
|
392
|
-
const level = success ? LogLevel.INFO : LogLevel.ERROR;
|
|
393
|
-
const status = success ? 'succeeded' : 'failed';
|
|
394
|
-
this.log(level, `Operation ${operation} ${status}`, {
|
|
395
|
-
...(context || {}),
|
|
396
|
-
operation,
|
|
397
|
-
success,
|
|
398
|
-
processingTimeMs: elapsedMs
|
|
399
|
-
});
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
|
-
* Create and return a namespaced logger
|
|
404
|
-
*
|
|
405
|
-
* @param name - Component name for the logger
|
|
406
|
-
* @param config - Optional configuration overrides
|
|
407
|
-
* @returns Configured logger instance
|
|
408
|
-
*/
|
|
409
|
-
export function createLogger(name, config) {
|
|
410
|
-
return new Logger(name, config);
|
|
411
|
-
}
|
|
412
|
-
// Create a global root logger
|
|
413
|
-
export const rootLogger = createLogger('obsidian-mcp-server', {
|
|
414
|
-
files: true,
|
|
415
|
-
logDir: "logs"
|
|
416
|
-
});
|
|
417
|
-
//# sourceMappingURL=logging.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"logging.js","sourceRoot":"","sources":["../../src/utils/logging.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,OAAO,MAA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
|
8
|
-
/**
|
|
9
|
-
* Default rate limit configuration
|
|
10
|
-
*/
|
|
11
|
-
export declare const DEFAULT_RATE_LIMIT_CONFIG: RateLimitConfig;
|
|
12
|
-
/**
|
|
13
|
-
* RateLimit manages request rate limiting for API endpoints
|
|
14
|
-
*/
|
|
15
|
-
export declare class RateLimiter {
|
|
16
|
-
private config;
|
|
17
|
-
private requestCounts;
|
|
18
|
-
private cleanupInterval;
|
|
19
|
-
constructor(config?: RateLimitConfig);
|
|
20
|
-
/**
|
|
21
|
-
* Check if a request is within rate limits
|
|
22
|
-
* @param key Identifier for the rate limit bucket (e.g., toolName)
|
|
23
|
-
* @returns Whether the request is allowed
|
|
24
|
-
*/
|
|
25
|
-
checkRateLimit(key: string): boolean;
|
|
26
|
-
/**
|
|
27
|
-
* Check rate limit and throw an error if exceeded
|
|
28
|
-
* @param key Identifier for the rate limit bucket
|
|
29
|
-
* @throws ObsidianError if rate limit is exceeded
|
|
30
|
-
*/
|
|
31
|
-
enforceRateLimit(key: string): void;
|
|
32
|
-
/**
|
|
33
|
-
* Get information about current rate limit status
|
|
34
|
-
* @param key Identifier for the rate limit bucket
|
|
35
|
-
* @returns Rate limit information or null if no requests have been made
|
|
36
|
-
*/
|
|
37
|
-
getRateLimitInfo(key: string): {
|
|
38
|
-
remaining: number;
|
|
39
|
-
resetTime: number;
|
|
40
|
-
} | null;
|
|
41
|
-
/**
|
|
42
|
-
* Clean up expired rate limit entries
|
|
43
|
-
*/
|
|
44
|
-
private cleanup;
|
|
45
|
-
/**
|
|
46
|
-
* Clean up resources (e.g., when shutting down)
|
|
47
|
-
*/
|
|
48
|
-
dispose(): void;
|
|
49
|
-
}
|
|
50
|
-
export declare const rateLimiter: RateLimiter;
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Rate limiting utilities for the Obsidian MCP Server
|
|
3
|
-
*/
|
|
4
|
-
import { ObsidianError } from './errors.js';
|
|
5
|
-
/**
|
|
6
|
-
* Default rate limit configuration
|
|
7
|
-
*/
|
|
8
|
-
export const DEFAULT_RATE_LIMIT_CONFIG = {
|
|
9
|
-
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
10
|
-
maxRequests: 200
|
|
11
|
-
};
|
|
12
|
-
/**
|
|
13
|
-
* RateLimit manages request rate limiting for API endpoints
|
|
14
|
-
*/
|
|
15
|
-
export class RateLimiter {
|
|
16
|
-
config;
|
|
17
|
-
requestCounts = new Map();
|
|
18
|
-
cleanupInterval;
|
|
19
|
-
constructor(config = DEFAULT_RATE_LIMIT_CONFIG) {
|
|
20
|
-
this.config = config;
|
|
21
|
-
// Clean up expired rate limit entries periodically
|
|
22
|
-
this.cleanupInterval = setInterval(() => this.cleanup(), 60000); // Clean up every minute
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Check if a request is within rate limits
|
|
26
|
-
* @param key Identifier for the rate limit bucket (e.g., toolName)
|
|
27
|
-
* @returns Whether the request is allowed
|
|
28
|
-
*/
|
|
29
|
-
checkRateLimit(key) {
|
|
30
|
-
const now = Date.now();
|
|
31
|
-
const requestInfo = this.requestCounts.get(key);
|
|
32
|
-
if (!requestInfo || now > requestInfo.resetTime) {
|
|
33
|
-
// Reset counter for new window
|
|
34
|
-
this.requestCounts.set(key, {
|
|
35
|
-
count: 1,
|
|
36
|
-
resetTime: now + this.config.windowMs
|
|
37
|
-
});
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
if (requestInfo.count >= this.config.maxRequests) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
requestInfo.count++;
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Check rate limit and throw an error if exceeded
|
|
48
|
-
* @param key Identifier for the rate limit bucket
|
|
49
|
-
* @throws ObsidianError if rate limit is exceeded
|
|
50
|
-
*/
|
|
51
|
-
enforceRateLimit(key) {
|
|
52
|
-
if (!this.checkRateLimit(key)) {
|
|
53
|
-
throw new ObsidianError(`Rate limit exceeded for ${key}. Please try again later.`, 42900 // 42900 = Rate limit exceeded
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Get information about current rate limit status
|
|
59
|
-
* @param key Identifier for the rate limit bucket
|
|
60
|
-
* @returns Rate limit information or null if no requests have been made
|
|
61
|
-
*/
|
|
62
|
-
getRateLimitInfo(key) {
|
|
63
|
-
const requestInfo = this.requestCounts.get(key);
|
|
64
|
-
if (!requestInfo) {
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
return {
|
|
68
|
-
remaining: Math.max(0, this.config.maxRequests - requestInfo.count),
|
|
69
|
-
resetTime: requestInfo.resetTime
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Clean up expired rate limit entries
|
|
74
|
-
*/
|
|
75
|
-
cleanup() {
|
|
76
|
-
const now = Date.now();
|
|
77
|
-
for (const [key, info] of this.requestCounts.entries()) {
|
|
78
|
-
if (now > info.resetTime) {
|
|
79
|
-
this.requestCounts.delete(key);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Clean up resources (e.g., when shutting down)
|
|
85
|
-
*/
|
|
86
|
-
dispose() {
|
|
87
|
-
if (this.cleanupInterval) {
|
|
88
|
-
clearInterval(this.cleanupInterval);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
// Export a singleton instance with default configuration
|
|
93
|
-
export const rateLimiter = new RateLimiter();
|
|
94
|
-
//# sourceMappingURL=rate-limiting.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rate-limiting.js","sourceRoot":"","sources":["../../src/utils/rate-limiting.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAU5C;;GAEG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAoB;I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B;aACrC,CAAC;QACJ,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,gBAAgB,CAAC,GAAW;QAC1B,MAAM,WAAW,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAChD,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,OAAO;YACL,SAAS,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,WAAW,GAAG,WAAW,CAAC,KAAK,CAAC;YACnE,SAAS,EAAE,WAAW,CAAC,SAAS;SACjC,CAAC;IACJ,CAAC;IAED;;OAEG;IACK,OAAO;QACb,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,KAAK,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,EAAE,CAAC;YACvD,IAAI,GAAG,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;gBACzB,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YACjC,CAAC;QACH,CAAC;IACH,CAAC;IAED;;OAEG;IACH,OAAO;QACL,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;YACzB,aAAa,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;CACF;AAED,yDAAyD;AACzD,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC"}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export declare const MAX_TOKENS: number;
|
|
2
|
-
export declare const TRUNCATION_MESSAGE = "\n\n[Response truncated due to length]";
|
|
3
|
-
/**
|
|
4
|
-
* Handles token counting and text truncation to stay within token limits
|
|
5
|
-
*/
|
|
6
|
-
export declare class TokenCounter {
|
|
7
|
-
private tokenizer;
|
|
8
|
-
private isShuttingDown;
|
|
9
|
-
private cleanupListener;
|
|
10
|
-
constructor();
|
|
11
|
-
/**
|
|
12
|
-
* Count the number of tokens in a string
|
|
13
|
-
*/
|
|
14
|
-
countTokens(text: string): number;
|
|
15
|
-
/**
|
|
16
|
-
* Truncate text to stay within token limit
|
|
17
|
-
*/
|
|
18
|
-
truncateToTokenLimit(text: string, limit?: number): string;
|
|
19
|
-
/**
|
|
20
|
-
* Clean up resources and remove listeners
|
|
21
|
-
*/
|
|
22
|
-
cleanup(): void;
|
|
23
|
-
/**
|
|
24
|
-
* Remove process event listeners
|
|
25
|
-
*/
|
|
26
|
-
private removeListeners;
|
|
27
|
-
}
|
|
28
|
-
export declare const tokenCounter: TokenCounter;
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Token counting utilities for the Obsidian MCP Server
|
|
3
|
-
*/
|
|
4
|
-
import { encoding_for_model } from "tiktoken";
|
|
5
|
-
// Load token limits from environment or use defaults
|
|
6
|
-
export const MAX_TOKENS = parseInt(process.env.MAX_TOKENS ?? '20000');
|
|
7
|
-
export const TRUNCATION_MESSAGE = "\n\n[Response truncated due to length]";
|
|
8
|
-
/**
|
|
9
|
-
* Handles token counting and text truncation to stay within token limits
|
|
10
|
-
*/
|
|
11
|
-
export class TokenCounter {
|
|
12
|
-
tokenizer = encoding_for_model("gpt-4"); // This is strictly for token counting, not for LLM inference
|
|
13
|
-
isShuttingDown = false;
|
|
14
|
-
cleanupListener;
|
|
15
|
-
constructor() {
|
|
16
|
-
// Define the cleanup logic
|
|
17
|
-
this.cleanupListener = () => {
|
|
18
|
-
if (!this.isShuttingDown) {
|
|
19
|
-
this.isShuttingDown = true;
|
|
20
|
-
if (this.tokenizer) {
|
|
21
|
-
this.tokenizer.free();
|
|
22
|
-
// No need to explicitly set this.tokenizer to null,
|
|
23
|
-
// but ensure it's not used after free()
|
|
24
|
-
}
|
|
25
|
-
// Remove listeners after execution to prevent multiple calls
|
|
26
|
-
// and potential leaks if cleanup is called manually before exit
|
|
27
|
-
this.removeListeners();
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
// Attach listeners
|
|
31
|
-
process.on('exit', this.cleanupListener);
|
|
32
|
-
process.on('SIGINT', this.cleanupListener);
|
|
33
|
-
process.on('SIGTERM', this.cleanupListener);
|
|
34
|
-
process.on('uncaughtException', this.cleanupListener);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Count the number of tokens in a string
|
|
38
|
-
*/
|
|
39
|
-
countTokens(text) {
|
|
40
|
-
return this.tokenizer.encode(text).length;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Truncate text to stay within token limit
|
|
44
|
-
*/
|
|
45
|
-
truncateToTokenLimit(text, limit = MAX_TOKENS) {
|
|
46
|
-
const tokens = this.tokenizer.encode(text);
|
|
47
|
-
if (tokens.length <= limit) {
|
|
48
|
-
return text;
|
|
49
|
-
}
|
|
50
|
-
// Reserve tokens for truncation message
|
|
51
|
-
const messageTokens = this.tokenizer.encode(TRUNCATION_MESSAGE);
|
|
52
|
-
const availableTokens = limit - messageTokens.length;
|
|
53
|
-
// Decode truncated tokens back to text
|
|
54
|
-
const truncatedText = this.tokenizer.decode(tokens.slice(0, availableTokens));
|
|
55
|
-
return truncatedText + TRUNCATION_MESSAGE;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Clean up resources and remove listeners
|
|
59
|
-
*/
|
|
60
|
-
cleanup() {
|
|
61
|
-
this.cleanupListener(); // Call the main cleanup logic
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Remove process event listeners
|
|
65
|
-
*/
|
|
66
|
-
removeListeners() {
|
|
67
|
-
process.off('exit', this.cleanupListener);
|
|
68
|
-
process.off('SIGINT', this.cleanupListener);
|
|
69
|
-
process.off('SIGTERM', this.cleanupListener);
|
|
70
|
-
process.off('uncaughtException', this.cleanupListener);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
// Export a singleton instance
|
|
74
|
-
export const tokenCounter = new TokenCounter();
|
|
75
|
-
//# sourceMappingURL=tokenization.js.map
|