bn-telegram-mcp-server 0.0.1
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 +1031 -0
- package/dist/debug-middleware.d.ts +12 -0
- package/dist/debug-middleware.d.ts.map +1 -0
- package/dist/debug-middleware.js +36 -0
- package/dist/debug-middleware.js.map +1 -0
- package/dist/encryption.d.ts +11 -0
- package/dist/encryption.d.ts.map +1 -0
- package/dist/encryption.js +26 -0
- package/dist/encryption.js.map +1 -0
- package/dist/helpers.d.ts +27 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +103 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +176 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +29 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +114 -0
- package/dist/logger.js.map +1 -0
- package/dist/request-context.d.ts +24 -0
- package/dist/request-context.d.ts.map +1 -0
- package/dist/request-context.js +32 -0
- package/dist/request-context.js.map +1 -0
- package/dist/resolvers.d.ts +36 -0
- package/dist/resolvers.d.ts.map +1 -0
- package/dist/resolvers.js +252 -0
- package/dist/resolvers.js.map +1 -0
- package/dist/schemas.d.ts +150 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/schemas.js +216 -0
- package/dist/schemas.js.map +1 -0
- package/dist/telegram-client.d.ts +19 -0
- package/dist/telegram-client.d.ts.map +1 -0
- package/dist/telegram-client.js +112 -0
- package/dist/telegram-client.js.map +1 -0
- package/dist/tool-loader.d.ts +31 -0
- package/dist/tool-loader.d.ts.map +1 -0
- package/dist/tool-loader.js +121 -0
- package/dist/tool-loader.js.map +1 -0
- package/dist/tool-registry.d.ts +47 -0
- package/dist/tool-registry.d.ts.map +1 -0
- package/dist/tool-registry.js +58 -0
- package/dist/tool-registry.js.map +1 -0
- package/dist/tools/getChat.d.ts +28 -0
- package/dist/tools/getChat.d.ts.map +1 -0
- package/dist/tools/getChat.js +69 -0
- package/dist/tools/getChat.js.map +1 -0
- package/dist/tools/getChatMembers.d.ts +34 -0
- package/dist/tools/getChatMembers.d.ts.map +1 -0
- package/dist/tools/getChatMembers.js +150 -0
- package/dist/tools/getChatMembers.js.map +1 -0
- package/dist/tools/getContacts.d.ts +28 -0
- package/dist/tools/getContacts.d.ts.map +1 -0
- package/dist/tools/getContacts.js +73 -0
- package/dist/tools/getContacts.js.map +1 -0
- package/dist/tools/getFile.d.ts +21 -0
- package/dist/tools/getFile.d.ts.map +1 -0
- package/dist/tools/getFile.js +33 -0
- package/dist/tools/getFile.js.map +1 -0
- package/dist/tools/getGroup.d.ts +27 -0
- package/dist/tools/getGroup.d.ts.map +1 -0
- package/dist/tools/getGroup.js +100 -0
- package/dist/tools/getGroup.js.map +1 -0
- package/dist/tools/getMe.d.ts +17 -0
- package/dist/tools/getMe.d.ts.map +1 -0
- package/dist/tools/getMe.js +20 -0
- package/dist/tools/getMe.js.map +1 -0
- package/dist/tools/getMessageContext.d.ts +21 -0
- package/dist/tools/getMessageContext.d.ts.map +1 -0
- package/dist/tools/getMessageContext.js +164 -0
- package/dist/tools/getMessageContext.js.map +1 -0
- package/dist/tools/getMessages.d.ts +38 -0
- package/dist/tools/getMessages.d.ts.map +1 -0
- package/dist/tools/getMessages.js +123 -0
- package/dist/tools/getMessages.js.map +1 -0
- package/dist/tools/getUser.d.ts +22 -0
- package/dist/tools/getUser.d.ts.map +1 -0
- package/dist/tools/getUser.js +38 -0
- package/dist/tools/getUser.js.map +1 -0
- package/dist/tools/index.d.ts +11 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +12 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/sendMessage.d.ts +39 -0
- package/dist/tools/sendMessage.d.ts.map +1 -0
- package/dist/tools/sendMessage.js +116 -0
- package/dist/tools/sendMessage.js.map +1 -0
- package/dist/transformers.d.ts +133 -0
- package/dist/transformers.d.ts.map +1 -0
- package/dist/transformers.js +335 -0
- package/dist/transformers.js.map +1 -0
- package/dist/types.d.ts +55 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +45 -0
- package/tools.json +252 -0
package/dist/schemas.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Flexible identifier that accepts either:
|
|
4
|
+
* - A numeric ID (number or numeric string)
|
|
5
|
+
* - A name/title to search for (string)
|
|
6
|
+
*
|
|
7
|
+
* Examples:
|
|
8
|
+
* - 8572674464 (numeric ID)
|
|
9
|
+
* - "8572674464" (numeric string, treated as ID)
|
|
10
|
+
* - "Gong Zhe" (name, will be resolved)
|
|
11
|
+
* - "@username" (username, will be resolved)
|
|
12
|
+
*/
|
|
13
|
+
const FlexibleChatId = z.union([z.number(), z.string()]).describe("Chat identifier: either a numeric ID (e.g., -1001234567890) or chat title/name to search (e.g., \"Gong Zhe\", \"BlueNexus Team\")");
|
|
14
|
+
const FlexibleUserId = z.union([z.number(), z.string()]).describe("User identifier: either a numeric ID (e.g., 8572674464), username (e.g., \"@johndoe\"), or name to search (e.g., \"John Doe\")");
|
|
15
|
+
const FlexibleGroupId = z.union([z.number(), z.string()]).describe("Group identifier: either a numeric ID or group name to search");
|
|
16
|
+
// ===== Tool 1: getMe =====
|
|
17
|
+
export const GetMeSchema = z.object({});
|
|
18
|
+
// ===== Tool 2: getChat (consolidated from getChats + getChat) =====
|
|
19
|
+
export const GetChatSchema = z.object({
|
|
20
|
+
chat: FlexibleChatId.optional().describe("Chat to get info for. If not provided, lists all chats. Can be a numeric ID or chat title/name"),
|
|
21
|
+
chat_list: z
|
|
22
|
+
.enum(["main", "archive"])
|
|
23
|
+
.optional()
|
|
24
|
+
.default("main")
|
|
25
|
+
.describe("Which chat list to retrieve when listing chats (default: main)"),
|
|
26
|
+
limit: z
|
|
27
|
+
.number()
|
|
28
|
+
.int()
|
|
29
|
+
.positive()
|
|
30
|
+
.max(4000)
|
|
31
|
+
.optional()
|
|
32
|
+
.default(100)
|
|
33
|
+
.describe("Maximum number of chats to return when listing (default: 100, max: 4000)"),
|
|
34
|
+
});
|
|
35
|
+
// ===== Tool 3: getMessages (consolidated from getChatHistory + getMessages + searchMessages) =====
|
|
36
|
+
export const GetMessagesSchema = z.object({
|
|
37
|
+
chat: FlexibleChatId.optional().describe("Chat to get messages from. Required for 'history' and 'specific' modes. Optional for 'search' mode (global search if omitted)"),
|
|
38
|
+
mode: z
|
|
39
|
+
.enum(["history", "specific", "search"])
|
|
40
|
+
.optional()
|
|
41
|
+
.default("history")
|
|
42
|
+
.describe("Mode: 'history' for paginated history, 'specific' for exact message IDs, 'search' for text search"),
|
|
43
|
+
// For mode="history"
|
|
44
|
+
from_message_id: z
|
|
45
|
+
.number()
|
|
46
|
+
.optional()
|
|
47
|
+
.default(0)
|
|
48
|
+
.describe("(history mode) Message ID to start from (0 for latest)"),
|
|
49
|
+
offset: z
|
|
50
|
+
.number()
|
|
51
|
+
.optional()
|
|
52
|
+
.default(0)
|
|
53
|
+
.describe("(history mode) Numeric offset for pagination"),
|
|
54
|
+
limit: z
|
|
55
|
+
.number()
|
|
56
|
+
.int()
|
|
57
|
+
.positive()
|
|
58
|
+
.max(100)
|
|
59
|
+
.optional()
|
|
60
|
+
.default(50)
|
|
61
|
+
.describe("Maximum messages to return (default: 50, max: 100)"),
|
|
62
|
+
only_local: z
|
|
63
|
+
.boolean()
|
|
64
|
+
.optional()
|
|
65
|
+
.default(false)
|
|
66
|
+
.describe("(history mode) Only return locally cached messages"),
|
|
67
|
+
// For mode="specific"
|
|
68
|
+
message_ids: z
|
|
69
|
+
.array(z.number())
|
|
70
|
+
.optional()
|
|
71
|
+
.describe("(specific mode) Array of message IDs to retrieve"),
|
|
72
|
+
// For mode="search"
|
|
73
|
+
query: z
|
|
74
|
+
.string()
|
|
75
|
+
.optional()
|
|
76
|
+
.describe("(search mode) Search query text"),
|
|
77
|
+
next_offset: z
|
|
78
|
+
.string()
|
|
79
|
+
.optional()
|
|
80
|
+
.default("")
|
|
81
|
+
.describe("(search mode) Cursor for pagination - pass the next_offset from previous response"),
|
|
82
|
+
});
|
|
83
|
+
// ===== Tool 4: sendMessage (consolidated from sendMessage + forwardMessages) =====
|
|
84
|
+
export const SendMessageSchema = z.object({
|
|
85
|
+
chat: FlexibleChatId.describe("Target chat to send/forward to. Can be a numeric ID or chat title/name"),
|
|
86
|
+
action: z
|
|
87
|
+
.enum(["send", "forward"])
|
|
88
|
+
.optional()
|
|
89
|
+
.default("send")
|
|
90
|
+
.describe("Action: 'send' to compose new message, 'forward' to forward existing messages"),
|
|
91
|
+
// For action="send"
|
|
92
|
+
text: z
|
|
93
|
+
.string()
|
|
94
|
+
.optional()
|
|
95
|
+
.describe("(send action) Text content of the message"),
|
|
96
|
+
reply_to_message_id: z
|
|
97
|
+
.number()
|
|
98
|
+
.optional()
|
|
99
|
+
.describe("(send action) ID of message to reply to"),
|
|
100
|
+
// For action="forward"
|
|
101
|
+
from_chat: FlexibleChatId.optional().describe("(forward action) Source chat to forward from. Can be a numeric ID or chat title"),
|
|
102
|
+
message_ids: z
|
|
103
|
+
.array(z.number())
|
|
104
|
+
.optional()
|
|
105
|
+
.describe("(forward action) Array of message IDs to forward"),
|
|
106
|
+
send_copy: z
|
|
107
|
+
.boolean()
|
|
108
|
+
.optional()
|
|
109
|
+
.default(false)
|
|
110
|
+
.describe("(forward action) Send copies without forwarding header"),
|
|
111
|
+
remove_caption: z
|
|
112
|
+
.boolean()
|
|
113
|
+
.optional()
|
|
114
|
+
.default(false)
|
|
115
|
+
.describe("(forward action) Remove media captions when forwarding"),
|
|
116
|
+
});
|
|
117
|
+
// ===== Tool 5: getUser =====
|
|
118
|
+
export const GetUserSchema = z.object({
|
|
119
|
+
user: FlexibleUserId.describe("User to get info for. Can be a numeric ID, @username, or name (e.g., \"John Doe\")"),
|
|
120
|
+
});
|
|
121
|
+
// ===== Tool 6: getContacts (consolidated from getContacts + searchContacts) =====
|
|
122
|
+
export const GetContactsSchema = z.object({
|
|
123
|
+
query: z
|
|
124
|
+
.string()
|
|
125
|
+
.optional()
|
|
126
|
+
.describe("Search query to filter contacts. If not provided, returns all contacts"),
|
|
127
|
+
limit: z
|
|
128
|
+
.number()
|
|
129
|
+
.int()
|
|
130
|
+
.positive()
|
|
131
|
+
.max(200)
|
|
132
|
+
.optional()
|
|
133
|
+
.default(50)
|
|
134
|
+
.describe("Maximum results when searching (default: 50, max: 200)"),
|
|
135
|
+
});
|
|
136
|
+
// ===== Tool 7: getFile =====
|
|
137
|
+
export const GetFileSchema = z.object({
|
|
138
|
+
file_id: z
|
|
139
|
+
.number()
|
|
140
|
+
.describe("Identifier of the file to get metadata for"),
|
|
141
|
+
});
|
|
142
|
+
// ===== Tool 8: getGroup (consolidated from getSupergroup + getBasicGroup) =====
|
|
143
|
+
export const GetGroupSchema = z.object({
|
|
144
|
+
group: FlexibleGroupId.describe("Group to get info for. Can be a numeric ID or group name"),
|
|
145
|
+
type: z
|
|
146
|
+
.enum(["supergroup", "basic_group", "auto"])
|
|
147
|
+
.optional()
|
|
148
|
+
.default("auto")
|
|
149
|
+
.describe("Group type: 'supergroup', 'basic_group', or 'auto' to detect automatically"),
|
|
150
|
+
});
|
|
151
|
+
// ===== Tool 9: getChatMembers (consolidated from getSupergroupMembers + getChatAdministrators) =====
|
|
152
|
+
export const GetChatMembersSchema = z.object({
|
|
153
|
+
chat: FlexibleChatId.describe("Chat to get members from. Can be a numeric ID or chat title"),
|
|
154
|
+
filter: z
|
|
155
|
+
.enum(["recent", "administrators", "banned", "bots", "search"])
|
|
156
|
+
.optional()
|
|
157
|
+
.default("recent")
|
|
158
|
+
.describe("Member filter type (default: recent)"),
|
|
159
|
+
query: z
|
|
160
|
+
.string()
|
|
161
|
+
.optional()
|
|
162
|
+
.default("")
|
|
163
|
+
.describe("Search query (only used with 'search' filter)"),
|
|
164
|
+
offset: z
|
|
165
|
+
.number()
|
|
166
|
+
.int()
|
|
167
|
+
.optional()
|
|
168
|
+
.default(0)
|
|
169
|
+
.describe("Offset for pagination"),
|
|
170
|
+
limit: z
|
|
171
|
+
.number()
|
|
172
|
+
.int()
|
|
173
|
+
.positive()
|
|
174
|
+
.max(200)
|
|
175
|
+
.optional()
|
|
176
|
+
.default(200)
|
|
177
|
+
.describe("Maximum members to return (default: 200, max: 200)"),
|
|
178
|
+
});
|
|
179
|
+
// ===== Tool 10: getMessageContext =====
|
|
180
|
+
// Retrieves surrounding messages to provide context around a specific message or search result
|
|
181
|
+
export const GetMessageContextSchema = z.object({
|
|
182
|
+
chat: FlexibleChatId.optional().describe("Chat to get message context from. Required for 'message_id' mode, optional for 'search' mode (searches globally if omitted)"),
|
|
183
|
+
mode: z
|
|
184
|
+
.enum(["message_id", "search"])
|
|
185
|
+
.optional()
|
|
186
|
+
.default("message_id")
|
|
187
|
+
.describe("Mode: 'message_id' to get context around a specific message, 'search' to find messages and get context around each match"),
|
|
188
|
+
// For mode="message_id"
|
|
189
|
+
message_id: z
|
|
190
|
+
.number()
|
|
191
|
+
.optional()
|
|
192
|
+
.describe("(message_id mode) The message ID to get context around"),
|
|
193
|
+
// For mode="search"
|
|
194
|
+
query: z
|
|
195
|
+
.string()
|
|
196
|
+
.optional()
|
|
197
|
+
.describe("(search mode) Search query to find messages"),
|
|
198
|
+
search_limit: z
|
|
199
|
+
.number()
|
|
200
|
+
.int()
|
|
201
|
+
.positive()
|
|
202
|
+
.max(20)
|
|
203
|
+
.optional()
|
|
204
|
+
.default(5)
|
|
205
|
+
.describe("(search mode) Maximum number of search results to get context for (default: 5, max: 20)"),
|
|
206
|
+
// Shared parameters
|
|
207
|
+
context_count: z
|
|
208
|
+
.number()
|
|
209
|
+
.int()
|
|
210
|
+
.positive()
|
|
211
|
+
.max(50)
|
|
212
|
+
.optional()
|
|
213
|
+
.default(5)
|
|
214
|
+
.describe("Number of messages to fetch before AND after the target message (default: 5, max: 50)"),
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,GAAG,EAAE;SACL,QAAQ,EAAE;SACV,GAAG,CAAC,EAAE,CAAC;SACP,QAAQ,EAAE;SACV,OAAO,CAAC,CAAC,CAAC;SACV,QAAQ,CAAC,uFAAuF,CAAC;CACrG,CAAC,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import tdl from "tdl";
|
|
2
|
+
export type { Client } from "tdl";
|
|
3
|
+
/**
|
|
4
|
+
* Create a TDLib client with a restored session from binlog
|
|
5
|
+
*
|
|
6
|
+
* @param sessionId - Unique session identifier (used for temp directory)
|
|
7
|
+
* @param binlog - TDLib binlog buffer (decrypted session data)
|
|
8
|
+
* @param apiId - Telegram API ID
|
|
9
|
+
* @param apiHash - Telegram API Hash
|
|
10
|
+
* @returns Configured and authorized TDLib client
|
|
11
|
+
*/
|
|
12
|
+
export declare function createTelegramClient(sessionId: string, binlog: Buffer, apiId: number, apiHash: string): Promise<tdl.Client>;
|
|
13
|
+
/**
|
|
14
|
+
* Clean up session files after use
|
|
15
|
+
*
|
|
16
|
+
* @param sessionId - Session identifier
|
|
17
|
+
*/
|
|
18
|
+
export declare function cleanupSession(sessionId: string): Promise<void>;
|
|
19
|
+
//# sourceMappingURL=telegram-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"telegram-client.d.ts","sourceRoot":"","sources":["../src/telegram-client.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AAatB,YAAY,EAAE,MAAM,EAAE,MAAM,KAAK,CAAC;AAElC;;;;;;;;GAQG;AACH,wBAAsB,oBAAoB,CACxC,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAmCrB;AAwDD;;;;GAIG;AACH,wBAAsB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAYrE"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import tdl from "tdl";
|
|
2
|
+
import { getTdjson } from "prebuilt-tdlib";
|
|
3
|
+
import fs from "fs/promises";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import os from "os";
|
|
6
|
+
import { createLogger } from "./logger.js";
|
|
7
|
+
// Configure TDLib once at startup
|
|
8
|
+
tdl.configure({ tdjson: getTdjson() });
|
|
9
|
+
const logger = createLogger("TelegramClient");
|
|
10
|
+
/**
|
|
11
|
+
* Create a TDLib client with a restored session from binlog
|
|
12
|
+
*
|
|
13
|
+
* @param sessionId - Unique session identifier (used for temp directory)
|
|
14
|
+
* @param binlog - TDLib binlog buffer (decrypted session data)
|
|
15
|
+
* @param apiId - Telegram API ID
|
|
16
|
+
* @param apiHash - Telegram API Hash
|
|
17
|
+
* @returns Configured and authorized TDLib client
|
|
18
|
+
*/
|
|
19
|
+
export async function createTelegramClient(sessionId, binlog, apiId, apiHash) {
|
|
20
|
+
// Create temp directory for this session
|
|
21
|
+
const dbPath = path.join(os.tmpdir(), "tdlib-sessions", sessionId);
|
|
22
|
+
await fs.mkdir(dbPath, { recursive: true });
|
|
23
|
+
// Write binlog file to restore session
|
|
24
|
+
const binlogPath = path.join(dbPath, "td.binlog");
|
|
25
|
+
await fs.writeFile(binlogPath, binlog);
|
|
26
|
+
// Check if we should use test DC (must match how the session was created)
|
|
27
|
+
const useTestDc = process.env.TELEGRAM_USE_TEST_DC === "true";
|
|
28
|
+
// Create TDLib client with parameters matching the backend
|
|
29
|
+
const client = tdl.createClient({
|
|
30
|
+
apiId,
|
|
31
|
+
apiHash,
|
|
32
|
+
databaseDirectory: dbPath,
|
|
33
|
+
filesDirectory: path.join(dbPath, "files"),
|
|
34
|
+
useTestDc,
|
|
35
|
+
tdlibParameters: {
|
|
36
|
+
// Enable databases for chat/message operations
|
|
37
|
+
use_message_database: true,
|
|
38
|
+
use_chat_info_database: true,
|
|
39
|
+
use_file_database: true,
|
|
40
|
+
use_secret_chats: false,
|
|
41
|
+
device_model: "BlueNexus MCP Server",
|
|
42
|
+
application_version: "1.0.0",
|
|
43
|
+
system_language_code: "en",
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
// Wait for authorization to complete
|
|
47
|
+
await waitForAuthorization(client);
|
|
48
|
+
return client;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Wait for TDLib client to be authorized
|
|
52
|
+
*
|
|
53
|
+
* @param client - TDLib client
|
|
54
|
+
* @returns Promise that resolves when client is authorized
|
|
55
|
+
*/
|
|
56
|
+
function waitForAuthorization(client) {
|
|
57
|
+
return new Promise((resolve, reject) => {
|
|
58
|
+
const timeout = setTimeout(() => {
|
|
59
|
+
cleanup();
|
|
60
|
+
reject(new Error("Authorization timeout: Session may be expired or invalid"));
|
|
61
|
+
}, 30000);
|
|
62
|
+
const onUpdate = (update) => {
|
|
63
|
+
if (update._ === "updateAuthorizationState") {
|
|
64
|
+
const state = update.authorization_state?._;
|
|
65
|
+
if (state === "authorizationStateReady") {
|
|
66
|
+
cleanup();
|
|
67
|
+
resolve();
|
|
68
|
+
}
|
|
69
|
+
else if (state === "authorizationStateClosed") {
|
|
70
|
+
cleanup();
|
|
71
|
+
reject(new Error("Session closed: The TDLib session has been terminated"));
|
|
72
|
+
}
|
|
73
|
+
else if (state === "authorizationStateWaitPhoneNumber" ||
|
|
74
|
+
state === "authorizationStateWaitCode" ||
|
|
75
|
+
state === "authorizationStateWaitPassword") {
|
|
76
|
+
cleanup();
|
|
77
|
+
reject(new Error(`Session requires re-authentication: ${state}. The stored session may be expired or invalid.`));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
const onError = (error) => {
|
|
82
|
+
cleanup();
|
|
83
|
+
reject(error);
|
|
84
|
+
};
|
|
85
|
+
const cleanup = () => {
|
|
86
|
+
clearTimeout(timeout);
|
|
87
|
+
client.off("update", onUpdate);
|
|
88
|
+
client.off("error", onError);
|
|
89
|
+
};
|
|
90
|
+
client.on("update", onUpdate);
|
|
91
|
+
client.on("error", onError);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Clean up session files after use
|
|
96
|
+
*
|
|
97
|
+
* @param sessionId - Session identifier
|
|
98
|
+
*/
|
|
99
|
+
export async function cleanupSession(sessionId) {
|
|
100
|
+
const dbPath = path.join(os.tmpdir(), "tdlib-sessions", sessionId);
|
|
101
|
+
try {
|
|
102
|
+
await fs.rm(dbPath, { recursive: true, force: true });
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
// Log but don't throw - cleanup failures shouldn't break the flow
|
|
106
|
+
logger.warn("Failed to cleanup session", {
|
|
107
|
+
sessionId,
|
|
108
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=telegram-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"telegram-client.js","sourceRoot":"","sources":["../src/telegram-client.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,kCAAkC;AAClC,GAAG,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,CAAC,CAAC;AAEvC,MAAM,MAAM,GAAG,YAAY,CAAC,gBAAgB,CAAC,CAAC;AAK9C;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,SAAiB,EACjB,MAAc,EACd,KAAa,EACb,OAAe;IAEf,yCAAyC;IACzC,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI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AAiB;IACpD,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,EAAE,gBAAgB,EAAE,SAAS,CAAC,CAAC;IAEnE,IAAI,CAAC;QACH,MAAM,EAAE,CAAC,EAAE,CAAC,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IACxD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,kEAAkE;QAClE,MAAM,CAAC,IAAI,CAAC,2BAA2B,EAAE;YACvC,SAAS;YACT,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;SACjE,CAAC,CAAC;IACL,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool configuration loader
|
|
3
|
+
* Loads tool definitions from tools.json and validates them
|
|
4
|
+
*
|
|
5
|
+
* Supports loading from (in priority order):
|
|
6
|
+
* 1. Custom path via TOOLS_CONFIG_PATH environment variable
|
|
7
|
+
* 2. tools.json in current working directory (for development)
|
|
8
|
+
* 3. tools.json in package installation directory (for npx/installed usage)
|
|
9
|
+
*/
|
|
10
|
+
export interface ToolDefinition {
|
|
11
|
+
name: string;
|
|
12
|
+
description: string;
|
|
13
|
+
inputSchema: {
|
|
14
|
+
type: string;
|
|
15
|
+
properties: Record<string, unknown>;
|
|
16
|
+
required?: string[];
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export interface ToolsConfig {
|
|
20
|
+
[toolName: string]: ToolDefinition;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Load tool definitions from tools.json
|
|
24
|
+
* Uses caching in production, always reloads in development
|
|
25
|
+
*/
|
|
26
|
+
export declare function loadToolDefinitions(forceReload?: boolean): ToolDefinition[];
|
|
27
|
+
/**
|
|
28
|
+
* Clear the tool cache (useful for testing or forced reloads)
|
|
29
|
+
*/
|
|
30
|
+
export declare function clearToolCache(): void;
|
|
31
|
+
//# sourceMappingURL=tool-loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-loader.d.ts","sourceRoot":"","sources":["../src/tool-loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAUH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE;QACX,IAAI,EAAE,MAAM,CAAC;QACb,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QACpC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;KACrB,CAAC;CACH;AAED,MAAM,WAAW,WAAW;IAC1B,CAAC,QAAQ,EAAE,MAAM,GAAG,cAAc,CAAC;CACpC;AAmDD;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,WAAW,UAAQ,GAAG,cAAc,EAAE,CAkEzE;AAED;;GAEG;AACH,wBAAgB,cAAc,IAAI,IAAI,CAIrC"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool configuration loader
|
|
3
|
+
* Loads tool definitions from tools.json and validates them
|
|
4
|
+
*
|
|
5
|
+
* Supports loading from (in priority order):
|
|
6
|
+
* 1. Custom path via TOOLS_CONFIG_PATH environment variable
|
|
7
|
+
* 2. tools.json in current working directory (for development)
|
|
8
|
+
* 3. tools.json in package installation directory (for npx/installed usage)
|
|
9
|
+
*/
|
|
10
|
+
import { readFileSync, existsSync } from "fs";
|
|
11
|
+
import { resolve, isAbsolute, dirname } from "path";
|
|
12
|
+
import { homedir } from "os";
|
|
13
|
+
import { fileURLToPath } from "url";
|
|
14
|
+
import { createLogger } from "./logger.js";
|
|
15
|
+
const logger = createLogger("ToolLoader");
|
|
16
|
+
let cachedTools = null;
|
|
17
|
+
let lastLoadTime = 0;
|
|
18
|
+
let lastLoadPath = "";
|
|
19
|
+
/**
|
|
20
|
+
* Resolve the tools configuration file path
|
|
21
|
+
* Supports:
|
|
22
|
+
* - Environment variable: TOOLS_CONFIG_PATH
|
|
23
|
+
* - Absolute paths: /Users/username/Desktop/tools.json
|
|
24
|
+
* - Relative paths: ./tools.json, ../config/tools.json
|
|
25
|
+
* - Home directory: ~/Desktop/tools.json
|
|
26
|
+
* - Default: tools.json in current working directory
|
|
27
|
+
* - Fallback: tools.json in package installation directory
|
|
28
|
+
*/
|
|
29
|
+
function resolveToolsPath() {
|
|
30
|
+
// Check environment variable first
|
|
31
|
+
const envPath = process.env.TOOLS_CONFIG_PATH;
|
|
32
|
+
if (envPath) {
|
|
33
|
+
// Expand ~ to home directory
|
|
34
|
+
const expandedPath = envPath.startsWith("~/")
|
|
35
|
+
? resolve(homedir(), envPath.slice(2))
|
|
36
|
+
: envPath;
|
|
37
|
+
// If absolute path, use as-is
|
|
38
|
+
if (isAbsolute(expandedPath)) {
|
|
39
|
+
return expandedPath;
|
|
40
|
+
}
|
|
41
|
+
// Otherwise resolve relative to current working directory
|
|
42
|
+
return resolve(process.cwd(), expandedPath);
|
|
43
|
+
}
|
|
44
|
+
// Try current working directory first (for dev mode)
|
|
45
|
+
const cwdPath = resolve(process.cwd(), "tools.json");
|
|
46
|
+
if (existsSync(cwdPath)) {
|
|
47
|
+
return cwdPath;
|
|
48
|
+
}
|
|
49
|
+
// Fallback: tools.json in package installation directory (for npx/installed usage)
|
|
50
|
+
// Get the directory of this module file
|
|
51
|
+
const moduleDir = dirname(fileURLToPath(import.meta.url));
|
|
52
|
+
// Go up from dist/tool-loader.js to package root
|
|
53
|
+
const packageRoot = resolve(moduleDir, "..");
|
|
54
|
+
const packagePath = resolve(packageRoot, "tools.json");
|
|
55
|
+
return packagePath;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Load tool definitions from tools.json
|
|
59
|
+
* Uses caching in production, always reloads in development
|
|
60
|
+
*/
|
|
61
|
+
export function loadToolDefinitions(forceReload = false) {
|
|
62
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
63
|
+
const now = Date.now();
|
|
64
|
+
// Get the tools config path
|
|
65
|
+
const toolsPath = resolveToolsPath();
|
|
66
|
+
// In production, cache tools and only reload if forced or path changed
|
|
67
|
+
// In development, reload every time (for nodemon compatibility)
|
|
68
|
+
if (!isDev &&
|
|
69
|
+
!forceReload &&
|
|
70
|
+
cachedTools &&
|
|
71
|
+
now - lastLoadTime < 60000 &&
|
|
72
|
+
lastLoadPath === toolsPath) {
|
|
73
|
+
return cachedTools;
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
// Check if file exists
|
|
77
|
+
if (!existsSync(toolsPath)) {
|
|
78
|
+
throw new Error(`Tools configuration file not found: ${toolsPath}`);
|
|
79
|
+
}
|
|
80
|
+
const toolsJson = readFileSync(toolsPath, "utf-8");
|
|
81
|
+
const toolsConfig = JSON.parse(toolsJson);
|
|
82
|
+
// Convert config object to array of tool definitions
|
|
83
|
+
const tools = Object.values(toolsConfig);
|
|
84
|
+
// Validate tool definitions
|
|
85
|
+
for (const tool of tools) {
|
|
86
|
+
if (!tool.name || !tool.description || !tool.inputSchema) {
|
|
87
|
+
throw new Error(`Invalid tool definition: ${JSON.stringify(tool)}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
cachedTools = tools;
|
|
91
|
+
lastLoadTime = now;
|
|
92
|
+
lastLoadPath = toolsPath;
|
|
93
|
+
logger.info("Tool definitions loaded", {
|
|
94
|
+
count: tools.length,
|
|
95
|
+
path: toolsPath,
|
|
96
|
+
mode: isDev ? "development" : "production",
|
|
97
|
+
customPath: !!process.env.TOOLS_CONFIG_PATH,
|
|
98
|
+
});
|
|
99
|
+
return tools;
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
logger.error("Error loading tools.json", {
|
|
103
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
104
|
+
});
|
|
105
|
+
// In case of error, return cached tools if available
|
|
106
|
+
if (cachedTools) {
|
|
107
|
+
logger.warn("Using cached tool definitions due to load error");
|
|
108
|
+
return cachedTools;
|
|
109
|
+
}
|
|
110
|
+
throw new Error(`Failed to load tool definitions: ${error instanceof Error ? error.message : "Unknown error"}`, { cause: error instanceof Error ? error : undefined });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Clear the tool cache (useful for testing or forced reloads)
|
|
115
|
+
*/
|
|
116
|
+
export function clearToolCache() {
|
|
117
|
+
cachedTools = null;
|
|
118
|
+
lastLoadTime = 0;
|
|
119
|
+
logger.info("Tool cache cleared");
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=tool-loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-loader.js","sourceRoot":"","sources":["../src/tool-loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,IAAI,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,MAAM,MAAM,GAAG,YAAY,CAAC,YAAY,CAAC,CAAC;AAgB1C,IAAI,WAAW,GAA4B,IAAI,CAAC;AAChD,IAAI,YAAY,GAAG,CAAC,CAAC;AACrB,IAAI,YAAY,GAAG,EAAE,CAAC;AAEtB;;;;;;;;;GASG;AACH,SAAS,gBAAgB;IACvB,mCAAmC;IACnC,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;IAE9C,IAAI,OAAO,EAAE,CAAC;QACZ,6BAA6B;QAC7B,MAAM,YAAY,GAAG,OAA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KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;SACjE,CAAC,CAAC;QAEH,qDAAqD;QACrD,IAAI,WAAW,EAAE,CAAC;YAChB,MAAM,CAAC,IAAI,CAAC,iDAAiD,CAAC,CAAC;YAC/D,OAAO,WAAW,CAAC;QACrB,CAAC;QAED,MAAM,IAAI,KAAK,CACb,oCAAoC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,EAAE,EAC9F,EAAE,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,EAAE,CACtD,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc;IAC5B,WAAW,GAAG,IAAI,CAAC;IACnB,YAAY,GAAG,CAAC,CAAC;IACjB,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;AACpC,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Request } from "express";
|
|
2
|
+
import type { z } from "zod";
|
|
3
|
+
import type { ToolResponse } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Type for tool handler functions
|
|
6
|
+
* Uses z.infer<TSchema> to get the output type of the schema (after defaults are applied)
|
|
7
|
+
*/
|
|
8
|
+
type ToolHandler<TSchema extends z.ZodTypeAny, TOutput = unknown> = (req: Request, args: z.infer<TSchema>) => Promise<TOutput>;
|
|
9
|
+
/**
|
|
10
|
+
* Registry for managing and executing MCP tools with automatic debug wrapping
|
|
11
|
+
*/
|
|
12
|
+
export declare class ToolRegistry {
|
|
13
|
+
private tools;
|
|
14
|
+
/**
|
|
15
|
+
* Register a new tool in the registry
|
|
16
|
+
*
|
|
17
|
+
* @param name - Unique name of the tool
|
|
18
|
+
* @param schema - Zod schema for validating tool input
|
|
19
|
+
* @param handler - Async function that executes the tool logic
|
|
20
|
+
*/
|
|
21
|
+
register<TSchema extends z.ZodTypeAny, TOutput>(name: string, schema: TSchema, handler: ToolHandler<TSchema, TOutput>): void;
|
|
22
|
+
/**
|
|
23
|
+
* Execute a tool by name with automatic validation and debug wrapping
|
|
24
|
+
*
|
|
25
|
+
* @param toolName - Name of the tool to execute
|
|
26
|
+
* @param rawInput - Raw input arguments (will be validated against schema)
|
|
27
|
+
* @param req - Express request object for accessing headers/context
|
|
28
|
+
* @returns ToolResponse with data and optional debug metadata
|
|
29
|
+
* @throws Error if tool is not found or validation fails
|
|
30
|
+
*/
|
|
31
|
+
execute(toolName: string, rawInput: unknown, req: Request): Promise<ToolResponse<unknown>>;
|
|
32
|
+
/**
|
|
33
|
+
* Check if a tool exists in the registry
|
|
34
|
+
*
|
|
35
|
+
* @param toolName - Name of the tool to check
|
|
36
|
+
* @returns True if the tool is registered
|
|
37
|
+
*/
|
|
38
|
+
has(toolName: string): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Get all registered tool names
|
|
41
|
+
*
|
|
42
|
+
* @returns Array of registered tool names
|
|
43
|
+
*/
|
|
44
|
+
getToolNames(): string[];
|
|
45
|
+
}
|
|
46
|
+
export {};
|
|
47
|
+
//# sourceMappingURL=tool-registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-registry.d.ts","sourceRoot":"","sources":["../src/tool-registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAG/C;;;GAGG;AACH,KAAK,WAAW,CAAC,OAAO,SAAS,CAAC,CAAC,UAAU,EAAE,OAAO,GAAG,OAAO,IAAI,CAClE,GAAG,EAAE,OAAO,EACZ,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,KACnB,OAAO,CAAC,OAAO,CAAC,CAAC;AAWtB;;GAEG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,KAAK,CAAqC;IAElD;;;;;;OAMG;IACH,QAAQ,CAAC,OAAO,SAAS,CAAC,CAAC,UAAU,EAAE,OAAO,EAC5C,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,OAAO,EACf,OAAO,EAAE,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC,GACrC,IAAI;IAQP;;;;;;;;OAQG;IACG,OAAO,CACX,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,OAAO,EACjB,GAAG,EAAE,OAAO,GACX,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;IAcjC;;;;;OAKG;IACH,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAI9B;;;;OAIG;IACH,YAAY,IAAI,MAAM,EAAE;CAGzB"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { wrapWithDebug } from "./debug-middleware.js";
|
|
2
|
+
/**
|
|
3
|
+
* Registry for managing and executing MCP tools with automatic debug wrapping
|
|
4
|
+
*/
|
|
5
|
+
export class ToolRegistry {
|
|
6
|
+
tools = new Map();
|
|
7
|
+
/**
|
|
8
|
+
* Register a new tool in the registry
|
|
9
|
+
*
|
|
10
|
+
* @param name - Unique name of the tool
|
|
11
|
+
* @param schema - Zod schema for validating tool input
|
|
12
|
+
* @param handler - Async function that executes the tool logic
|
|
13
|
+
*/
|
|
14
|
+
register(name, schema, handler) {
|
|
15
|
+
this.tools.set(name, {
|
|
16
|
+
name,
|
|
17
|
+
schema,
|
|
18
|
+
handler: handler,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Execute a tool by name with automatic validation and debug wrapping
|
|
23
|
+
*
|
|
24
|
+
* @param toolName - Name of the tool to execute
|
|
25
|
+
* @param rawInput - Raw input arguments (will be validated against schema)
|
|
26
|
+
* @param req - Express request object for accessing headers/context
|
|
27
|
+
* @returns ToolResponse with data and optional debug metadata
|
|
28
|
+
* @throws Error if tool is not found or validation fails
|
|
29
|
+
*/
|
|
30
|
+
async execute(toolName, rawInput, req) {
|
|
31
|
+
const tool = this.tools.get(toolName);
|
|
32
|
+
if (!tool) {
|
|
33
|
+
throw new Error(`Unknown tool: ${toolName}`);
|
|
34
|
+
}
|
|
35
|
+
// Validate input against schema
|
|
36
|
+
const args = tool.schema.parse(rawInput);
|
|
37
|
+
// Execute with debug wrapping
|
|
38
|
+
return wrapWithDebug(toolName, args, () => tool.handler(req, args));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Check if a tool exists in the registry
|
|
42
|
+
*
|
|
43
|
+
* @param toolName - Name of the tool to check
|
|
44
|
+
* @returns True if the tool is registered
|
|
45
|
+
*/
|
|
46
|
+
has(toolName) {
|
|
47
|
+
return this.tools.has(toolName);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Get all registered tool names
|
|
51
|
+
*
|
|
52
|
+
* @returns Array of registered tool names
|
|
53
|
+
*/
|
|
54
|
+
getToolNames() {
|
|
55
|
+
return Array.from(this.tools.keys());
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=tool-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-registry.js","sourceRoot":"","sources":["../src/tool-registry.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAoBtD;;GAEG;AACH,MAAM,OAAO,YAAY;IACf,KAAK,GAAG,IAAI,GAAG,EAA0B,CAAC;IAElD;;;;;;OAMG;IACH,QAAQ,CACN,IAAY,EACZ,MAAe,EACf,OAAsC;QAEtC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE;YACnB,IAAI;YACJ,MAAM;YACN,OAAO,EAAE,OAA4D;SACtE,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,OAAO,CACX,QAAgB,EAChB,QAAiB,EACjB,GAAY;QAEZ,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAEtC,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,MAAM,IAAI,KAAK,CAAC,iBAAiB,QAAQ,EAAE,CAAC,CAAC;QAC/C,CAAC;QAED,gCAAgC;QAChC,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAEzC,8BAA8B;QAC9B,OAAO,aAAa,CAAC,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC;IACtE,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,QAAgB;QAClB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACH,YAAY;QACV,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IACvC,CAAC;CACF"}
|