@redonvn/redai-openapi-router 0.1.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/dist/index.js ADDED
@@ -0,0 +1,795 @@
1
+ // index.ts
2
+ import { mkdir, readFile, writeFile } from "fs/promises";
3
+ import path from "path";
4
+ import { fileURLToPath } from "url";
5
+
6
+ // ../redai-agent-sdk/dist/index.js
7
+ var DEFAULT_BASE_URL = "https://api.redai.vn/api/v1";
8
+ var encodeQueryValue = (value) => {
9
+ if (value === void 0) {
10
+ return [];
11
+ }
12
+ if (Array.isArray(value)) {
13
+ return value.map((item) => String(item));
14
+ }
15
+ return [String(value)];
16
+ };
17
+ var headersToObject = (headers) => {
18
+ const result = {};
19
+ headers.forEach((value, key) => {
20
+ result[key] = value;
21
+ });
22
+ return result;
23
+ };
24
+ var applyPathParams = (pathTemplate, pathParams) => pathTemplate.replace(/\{([^}]+)\}/g, (_, key) => {
25
+ const raw = pathParams?.[key];
26
+ if (raw === void 0 || raw === null) {
27
+ throw new RedaiAgentError(`Missing required path parameter: ${key}`);
28
+ }
29
+ return encodeURIComponent(String(raw));
30
+ });
31
+ var buildUrl = (baseUrl, path2, query) => {
32
+ const url = new URL(baseUrl);
33
+ const normalizedBasePath = url.pathname.replace(/\/+$/, "");
34
+ const normalizedRequestPath = path2.startsWith("/") ? path2 : `/${path2}`;
35
+ url.pathname = `${normalizedBasePath}${normalizedRequestPath}`.replace(/\/{2,}/g, "/");
36
+ if (query) {
37
+ for (const [key, value] of Object.entries(query)) {
38
+ for (const item of encodeQueryValue(value)) {
39
+ url.searchParams.append(key, item);
40
+ }
41
+ }
42
+ }
43
+ return url;
44
+ };
45
+ var parseResponse = async (response) => {
46
+ const contentType = response.headers.get("content-type") ?? "";
47
+ if (contentType.includes("application/json")) {
48
+ return response.json();
49
+ }
50
+ return response.text();
51
+ };
52
+ var RedaiAgentError = class extends Error {
53
+ constructor(message, options) {
54
+ super(message);
55
+ this.name = "RedaiAgentError";
56
+ this.status = options?.status;
57
+ this.body = options?.body;
58
+ }
59
+ };
60
+ var RedaiAgentClient = class {
61
+ constructor(options = {}) {
62
+ this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
63
+ this.bearerToken = options.bearerToken;
64
+ this.apiKey = options.apiKey;
65
+ this.apiKeyHeader = options.apiKeyHeader ?? "x-api-key";
66
+ this.defaultHeaders = options.defaultHeaders;
67
+ this.timeoutMs = options.timeoutMs;
68
+ this.fetchImpl = options.fetch ?? globalThis.fetch;
69
+ this.tenantContext = {
70
+ workspaceId: options.workspaceId,
71
+ baseId: options.baseId
72
+ };
73
+ }
74
+ createTenantHeaders(overrides) {
75
+ const workspaceId = overrides?.workspaceId ?? this.tenantContext.workspaceId;
76
+ const baseId = overrides?.baseId ?? this.tenantContext.baseId;
77
+ return {
78
+ "x-workspace-id": workspaceId,
79
+ "x-base-id": baseId
80
+ };
81
+ }
82
+ async executeTool(tool, input = {}) {
83
+ return this.request({
84
+ method: tool.method,
85
+ path: tool.path,
86
+ pathParams: input.path,
87
+ query: input.query,
88
+ headers: input.headers,
89
+ body: input.body
90
+ });
91
+ }
92
+ async request(request) {
93
+ const url = buildUrl(this.baseUrl, applyPathParams(request.path, request.pathParams), request.query);
94
+ const controller = new AbortController();
95
+ const timeout = this.timeoutMs ? setTimeout(() => controller.abort(), this.timeoutMs) : void 0;
96
+ const headers = this.buildHeaders(request.method, request.headers, request.body !== void 0);
97
+ try {
98
+ const response = await this.fetchImpl(url, {
99
+ method: request.method,
100
+ headers,
101
+ body: request.body === void 0 ? void 0 : JSON.stringify(request.body),
102
+ signal: controller.signal
103
+ });
104
+ const data = await parseResponse(response);
105
+ const result = {
106
+ ok: response.ok,
107
+ status: response.status,
108
+ statusText: response.statusText,
109
+ headers: headersToObject(response.headers),
110
+ data
111
+ };
112
+ if (!response.ok) {
113
+ throw new RedaiAgentError(`Request failed with status ${response.status}`, {
114
+ status: response.status,
115
+ body: data
116
+ });
117
+ }
118
+ return result;
119
+ } catch (error) {
120
+ if (error instanceof RedaiAgentError) {
121
+ throw error;
122
+ }
123
+ throw new RedaiAgentError("Request failed", { body: error });
124
+ } finally {
125
+ if (timeout) {
126
+ clearTimeout(timeout);
127
+ }
128
+ }
129
+ }
130
+ buildHeaders(method, requestHeaders, hasBody) {
131
+ const headers = new Headers();
132
+ for (const [key, value] of Object.entries(this.defaultHeaders ?? {})) {
133
+ headers.set(key, value);
134
+ }
135
+ for (const [key, value] of Object.entries(this.createTenantHeaders())) {
136
+ if (value !== void 0 && value !== null && value !== "") {
137
+ headers.set(key, value);
138
+ }
139
+ }
140
+ if (this.bearerToken) {
141
+ headers.set("authorization", `Bearer ${this.bearerToken}`);
142
+ } else if (this.apiKey) {
143
+ headers.set(this.apiKeyHeader ?? "x-api-key", this.apiKey);
144
+ }
145
+ for (const [key, value] of Object.entries(requestHeaders ?? {})) {
146
+ if (value !== void 0 && value !== null && value !== "") {
147
+ headers.set(key, value);
148
+ }
149
+ }
150
+ if (!headers.has("accept")) {
151
+ headers.set("accept", "application/json");
152
+ }
153
+ if (hasBody && method !== "GET" && !headers.has("content-type")) {
154
+ headers.set("content-type", "application/json");
155
+ }
156
+ return headers;
157
+ }
158
+ };
159
+
160
+ // tool-catalog.json
161
+ var tool_catalog_default = {
162
+ pluginId: "redai-openapi-router",
163
+ pluginName: "RedAI OpenAPI Router",
164
+ version: "0.1.0",
165
+ description: "Router plugin de tim module, operation va execute lazy cho RedAI OpenAPI.",
166
+ optional: true,
167
+ tools: [
168
+ {
169
+ name: "redai_router_list_modules",
170
+ description: "Liet ke cac module RedAI co san trong router.",
171
+ parameters: { type: "object", properties: {}, additionalProperties: false }
172
+ },
173
+ {
174
+ name: "redai_router_search",
175
+ description: "Tim operation phu hop theo y nghia nghiep vu, module va tu khoa.",
176
+ parameters: {
177
+ type: "object",
178
+ properties: {
179
+ query: { type: "string", description: "Yeu cau nghiep vu can tim operation." },
180
+ moduleId: { type: "string", description: "Loc theo module cu the neu da biet." },
181
+ limit: { type: "number", description: "So ket qua toi da tra ve.", default: 8 }
182
+ },
183
+ required: ["query"],
184
+ additionalProperties: false
185
+ }
186
+ },
187
+ {
188
+ name: "redai_router_get_module",
189
+ description: "Lay metadata module, feature groups, common tasks va operations shortlist.",
190
+ parameters: {
191
+ type: "object",
192
+ properties: {
193
+ moduleId: { type: "string", description: "ID module can xem." },
194
+ includeOperations: { type: "boolean", description: "Co tra ve danh sach operation rut gon hay khong.", default: true },
195
+ operationLimit: { type: "number", description: "So operation toi da tra ve khi includeOperations=true.", default: 12 }
196
+ },
197
+ required: ["moduleId"],
198
+ additionalProperties: false
199
+ }
200
+ },
201
+ {
202
+ name: "redai_router_get_operation",
203
+ description: "Lay metadata chi tiet va schema tham so cua mot operation cu the.",
204
+ parameters: {
205
+ type: "object",
206
+ properties: {
207
+ operationId: { type: "string", description: "ID operation can xem." }
208
+ },
209
+ required: ["operationId"],
210
+ additionalProperties: false
211
+ }
212
+ },
213
+ {
214
+ name: "redai_router_execute",
215
+ description: "Execute operation thong qua router va auth RedAI da login trong DM.",
216
+ parameters: {
217
+ type: "object",
218
+ properties: {
219
+ operationId: { type: "string", description: "ID operation can goi." },
220
+ telegramUserId: { type: "string", description: "Telegram sender ID tu DM hien tai." },
221
+ args: { type: "object", description: "Tham so operation theo shape path/query/body/headers.", additionalProperties: true },
222
+ confirm: { type: "boolean", description: "Xac nhan cho operation co risk cao.", default: false }
223
+ },
224
+ required: ["operationId", "telegramUserId"],
225
+ additionalProperties: false
226
+ }
227
+ }
228
+ ]
229
+ };
230
+
231
+ // index.ts
232
+ var typedToolCatalog = tool_catalog_default;
233
+ var DEFAULT_AUTH_STORE_PATH = "C:\\Users\\Acer\\.openclaw\\workspace-nhan-vien-redai\\auth\\redai-auth.json";
234
+ var currentDir = path.dirname(fileURLToPath(import.meta.url));
235
+ var dataRoot = path.resolve(currentDir, "..", "data");
236
+ var sitemapPath = path.join(dataRoot, "sitemap.json");
237
+ var moduleCache = /* @__PURE__ */ new Map();
238
+ var sitemapCache = null;
239
+ var formatToolResult = (payload) => ({
240
+ content: [
241
+ {
242
+ type: "text",
243
+ text: typeof payload === "string" ? payload : JSON.stringify(payload, null, 2)
244
+ }
245
+ ]
246
+ });
247
+ var resolvePluginConfig = (api) => api.config?.plugins?.entries?.[typedToolCatalog.pluginId]?.config ?? {};
248
+ var ensureStoreDirectory = async (storePath) => {
249
+ await mkdir(path.dirname(storePath), { recursive: true });
250
+ };
251
+ var getAuthStorePath = (runtimeConfig) => runtimeConfig.authStorePath?.trim() || DEFAULT_AUTH_STORE_PATH;
252
+ var loadAuthStore = async (runtimeConfig) => {
253
+ const storePath = getAuthStorePath(runtimeConfig);
254
+ try {
255
+ const raw = await readFile(storePath, "utf8");
256
+ const parsed = JSON.parse(raw);
257
+ return { version: 1, users: parsed?.users ?? {} };
258
+ } catch (error) {
259
+ const maybeNodeError = error;
260
+ if (maybeNodeError?.code === "ENOENT") {
261
+ await ensureStoreDirectory(storePath);
262
+ await writeFile(storePath, JSON.stringify({ version: 1, users: {} }, null, 2), "utf8");
263
+ return { version: 1, users: {} };
264
+ }
265
+ throw error;
266
+ }
267
+ };
268
+ var getAuthStoreEntry = async (runtimeConfig, telegramUserId) => {
269
+ const store = await loadAuthStore(runtimeConfig);
270
+ return store.users[telegramUserId] ?? null;
271
+ };
272
+ var withRedaiAuth = (runtimeConfig, auth) => {
273
+ const defaultHeaders = {
274
+ ...runtimeConfig.defaultHeaders ?? {},
275
+ "x-workspace-id": auth.workspaceId
276
+ };
277
+ if (auth.baseId) {
278
+ defaultHeaders["x-base-id"] = auth.baseId;
279
+ } else {
280
+ delete defaultHeaders["x-base-id"];
281
+ }
282
+ return {
283
+ ...runtimeConfig,
284
+ bearerToken: auth.bearerToken,
285
+ apiKey: void 0,
286
+ apiKeyHeader: void 0,
287
+ defaultHeaders,
288
+ workspaceId: auth.workspaceId,
289
+ baseId: auth.baseId ?? void 0
290
+ };
291
+ };
292
+ var resolveUserRuntimeConfig = async (api, telegramUserId) => {
293
+ const runtimeConfig = resolvePluginConfig(api);
294
+ const authEntry = await getAuthStoreEntry(runtimeConfig, telegramUserId);
295
+ if (!authEntry || authEntry.status !== "active") {
296
+ return null;
297
+ }
298
+ return {
299
+ runtimeConfig,
300
+ scopedConfig: withRedaiAuth(runtimeConfig, authEntry)
301
+ };
302
+ };
303
+ var authRequiredResult = (telegramUserId) => formatToolResult({
304
+ ok: false,
305
+ status: 401,
306
+ statusText: "AUTH_REQUIRED",
307
+ data: {
308
+ message: "Ban chua dang nhap RedAI auth hop le. Hay dung /login <bearerToken> <workspaceId> [baseId] trong DM truoc khi thao tac nghiep vu.",
309
+ telegramUserId
310
+ }
311
+ });
312
+ var normalizeText = (value) => value.normalize("NFKD").replace(/[^\w\s-]/g, " ").toLowerCase();
313
+ var tokenize = (value) => normalizeText(value).split(/\s+/).map((item) => item.trim()).filter(Boolean);
314
+ var READ_HINTS = [
315
+ "xem",
316
+ "liet",
317
+ "lietke",
318
+ "list",
319
+ "get",
320
+ "chi",
321
+ "tiet",
322
+ "detail",
323
+ "thong",
324
+ "tin",
325
+ "tim",
326
+ "kiem",
327
+ "search",
328
+ "find",
329
+ "tra",
330
+ "cuu",
331
+ "kiemtra",
332
+ "check",
333
+ "doc",
334
+ "read",
335
+ "preview"
336
+ ];
337
+ var MUTATION_HINTS = [
338
+ "tao",
339
+ "them",
340
+ "cap",
341
+ "nhat",
342
+ "update",
343
+ "create",
344
+ "edit",
345
+ "sua",
346
+ "run",
347
+ "execute",
348
+ "gui",
349
+ "send",
350
+ "pause",
351
+ "resume",
352
+ "sync",
353
+ "train",
354
+ "fine",
355
+ "tuning"
356
+ ];
357
+ var DESTRUCTIVE_HINTS = [
358
+ "xoa",
359
+ "delete",
360
+ "remove",
361
+ "huy",
362
+ "cancel",
363
+ "reset",
364
+ "clear",
365
+ "drop"
366
+ ];
367
+ var BULK_HINTS = ["hangloat", "bulk", "batch", "nhieu", "tatca", "all"];
368
+ var EXPORT_HINTS = ["export", "tai", "file", "csv", "excel", "download"];
369
+ var inferQueryIntent = (query, queryTokens) => {
370
+ const normalizedQuery = normalizeText(query);
371
+ const hasAnyHint = (hints) => hints.some((hint) => normalizedQuery.includes(normalizeText(hint)) || queryTokens.includes(normalizeText(hint)));
372
+ return {
373
+ wantsRead: hasAnyHint(READ_HINTS),
374
+ wantsMutation: hasAnyHint(MUTATION_HINTS),
375
+ wantsDestructive: hasAnyHint(DESTRUCTIVE_HINTS),
376
+ wantsBulk: hasAnyHint(BULK_HINTS),
377
+ wantsExport: hasAnyHint(EXPORT_HINTS),
378
+ mentionsExactOperation: normalizedQuery.includes(":") || normalizedQuery.includes("_v1")
379
+ };
380
+ };
381
+ var getSitemap = async () => {
382
+ if (!sitemapCache) {
383
+ sitemapCache = JSON.parse(await readFile(sitemapPath, "utf8"));
384
+ }
385
+ return sitemapCache;
386
+ };
387
+ var getModuleDoc = async (moduleId) => {
388
+ if (moduleCache.has(moduleId)) {
389
+ return moduleCache.get(moduleId) ?? null;
390
+ }
391
+ const sitemap = await getSitemap();
392
+ const entry = sitemap.modules.find((item) => item.id === moduleId);
393
+ if (!entry) {
394
+ return null;
395
+ }
396
+ const parsed = JSON.parse(await readFile(path.join(dataRoot, entry.moduleFile), "utf8"));
397
+ moduleCache.set(moduleId, parsed);
398
+ return parsed;
399
+ };
400
+ var findOperation = async (operationId) => {
401
+ const moduleId = operationId.split(":")[0];
402
+ const moduleDoc = await getModuleDoc(moduleId);
403
+ return moduleDoc?.operations.find((item) => item.id === operationId) ?? null;
404
+ };
405
+ var scoreOperation = (queryTokens, intent, requestedModuleId, moduleDoc, operation) => {
406
+ let score = 0;
407
+ const exactQuery = queryTokens.join(" ");
408
+ const opJoined = normalizeText(
409
+ [
410
+ operation.id,
411
+ operation.operationId,
412
+ operation.title,
413
+ operation.summary,
414
+ operation.businessMeaning,
415
+ ...operation.tags,
416
+ ...operation.keywords
417
+ ].join(" ")
418
+ );
419
+ const moduleJoined = normalizeText(
420
+ [moduleDoc.id, moduleDoc.title, moduleDoc.summary, moduleDoc.purpose, ...moduleDoc.keywords].join(" ")
421
+ );
422
+ if (requestedModuleId && moduleDoc.id === requestedModuleId) {
423
+ score += 20;
424
+ }
425
+ if (normalizeText(operation.id) === exactQuery || normalizeText(operation.operationId) === exactQuery) {
426
+ score += 100;
427
+ }
428
+ for (const token of queryTokens) {
429
+ if (normalizeText(operation.operationId).includes(token)) {
430
+ score += 20;
431
+ }
432
+ if (opJoined.includes(token)) {
433
+ score += 12;
434
+ }
435
+ if (moduleJoined.includes(token)) {
436
+ score += 6;
437
+ }
438
+ }
439
+ if (operation.method === "GET") {
440
+ score += 4;
441
+ } else {
442
+ score -= 2;
443
+ }
444
+ if (operation.riskLevel === "safe") {
445
+ score += 8;
446
+ }
447
+ if (operation.riskLevel === "confirm_required") {
448
+ score -= 8;
449
+ }
450
+ if (operation.riskLevel === "costly") {
451
+ score -= intent.wantsMutation ? 6 : 18;
452
+ }
453
+ if (operation.riskLevel === "destructive") {
454
+ score -= intent.wantsDestructive ? 10 : 32;
455
+ }
456
+ const opText = normalizeText(
457
+ [
458
+ operation.id,
459
+ operation.operationId,
460
+ operation.title,
461
+ operation.summary,
462
+ operation.businessMeaning,
463
+ ...operation.whenToUse,
464
+ ...operation.whenNotToUse,
465
+ ...operation.keywords
466
+ ].join(" ")
467
+ );
468
+ if (intent.wantsRead) {
469
+ if (operation.method === "GET") {
470
+ score += 10;
471
+ }
472
+ if (operation.riskLevel === "safe") {
473
+ score += 6;
474
+ }
475
+ }
476
+ if (intent.wantsMutation && operation.method !== "GET") {
477
+ score += 10;
478
+ }
479
+ if (intent.wantsDestructive && /(delete|remove|cancel|reset|bulkremove|bulkdelete|destroy|xoa|huy)/.test(opText)) {
480
+ score += 18;
481
+ }
482
+ if (intent.wantsBulk && /(bulk|batch|hang loat|tat ca|all)/.test(opText)) {
483
+ score += 12;
484
+ }
485
+ if (intent.wantsExport && /(export|download|csv|excel|file)/.test(opText)) {
486
+ score += 16;
487
+ }
488
+ if (!intent.wantsMutation && !intent.wantsDestructive && operation.method !== "GET") {
489
+ score -= 10;
490
+ }
491
+ if (!intent.wantsBulk && /(bulk|batch)/.test(opText)) {
492
+ score -= 10;
493
+ }
494
+ if (!intent.wantsExport && /(export|download)/.test(opText)) {
495
+ score -= 10;
496
+ }
497
+ if (!intent.mentionsExactOperation && operation.confirmationRequired) {
498
+ score -= 8;
499
+ }
500
+ return score;
501
+ };
502
+ var summarizeOperation = (operation) => ({
503
+ id: operation.id,
504
+ title: operation.title,
505
+ summary: operation.summary,
506
+ method: operation.method,
507
+ path: operation.path,
508
+ riskLevel: operation.riskLevel,
509
+ confirmationRequired: operation.confirmationRequired,
510
+ featureGroupId: operation.featureGroupId
511
+ });
512
+ var buildMatchReasons = (queryTokens, intent, moduleDoc, operation) => {
513
+ const reasons = [];
514
+ const normalizedOperation = normalizeText(
515
+ [operation.operationId, operation.title, ...operation.tags, ...operation.keywords].join(" ")
516
+ );
517
+ const normalizedModule = normalizeText([moduleDoc.title, ...moduleDoc.keywords].join(" "));
518
+ if (queryTokens.some((token) => normalizeText(operation.operationId).includes(token))) {
519
+ reasons.push("operation-id");
520
+ }
521
+ if (queryTokens.some((token) => normalizedOperation.includes(token))) {
522
+ reasons.push("keyword");
523
+ }
524
+ if (queryTokens.some((token) => normalizedModule.includes(token))) {
525
+ reasons.push("module");
526
+ }
527
+ if (intent.wantsRead && operation.method === "GET") {
528
+ reasons.push("read-safe");
529
+ }
530
+ if (intent.wantsMutation && operation.method !== "GET") {
531
+ reasons.push("write-intent");
532
+ }
533
+ if (intent.wantsDestructive && operation.riskLevel === "destructive") {
534
+ reasons.push("destructive-intent");
535
+ }
536
+ if (intent.wantsExport && /(export|download)/.test(normalizeText(operation.id))) {
537
+ reasons.push("export-intent");
538
+ }
539
+ return reasons.length > 0 ? reasons : [operation.tags[0] ?? moduleDoc.title];
540
+ };
541
+ var hasHeadersSection = (schema) => {
542
+ const objectSchema = schema;
543
+ return Boolean(objectSchema?.properties?.headers);
544
+ };
545
+ var mergeTenantHeaders = (schema, inputHeaders, runtimeConfig) => {
546
+ const entries = Object.entries(inputHeaders ?? {}).filter(([, value]) => value !== void 0 && value !== null && value !== "").map(([key, value]) => [key, String(value)]);
547
+ if (hasHeadersSection(schema)) {
548
+ if (runtimeConfig.workspaceId && !entries.some(([key]) => key.toLowerCase() === "x-workspace-id")) {
549
+ entries.push(["x-workspace-id", runtimeConfig.workspaceId]);
550
+ }
551
+ if (runtimeConfig.baseId && !entries.some(([key]) => key.toLowerCase() === "x-base-id")) {
552
+ entries.push(["x-base-id", runtimeConfig.baseId]);
553
+ }
554
+ }
555
+ return entries.length > 0 ? Object.fromEntries(entries) : void 0;
556
+ };
557
+ var executeOperation = async (runtimeConfig, operation, rawArgs) => {
558
+ const client = new RedaiAgentClient({
559
+ ...runtimeConfig,
560
+ baseUrl: runtimeConfig.baseUrl
561
+ });
562
+ const args = rawArgs ?? {};
563
+ return client.executeTool(
564
+ { method: operation.method, path: operation.path },
565
+ {
566
+ path: args.path,
567
+ query: args.query,
568
+ body: args.body,
569
+ headers: mergeTenantHeaders(operation.parameterSchema, args.headers, runtimeConfig)
570
+ }
571
+ );
572
+ };
573
+ function register(api) {
574
+ api.registerTool(
575
+ {
576
+ name: "redai_router_list_modules",
577
+ description: "Liet ke cac module RedAI co san trong router.",
578
+ parameters: { type: "object", properties: {}, additionalProperties: false },
579
+ execute: async () => {
580
+ const sitemap = await getSitemap();
581
+ return formatToolResult({
582
+ catalogVersion: sitemap.catalogVersion,
583
+ generatedAt: sitemap.generatedAt,
584
+ modules: sitemap.modules.map((item) => ({
585
+ id: item.id,
586
+ title: item.title,
587
+ summary: item.summary,
588
+ whenToUse: item.whenToUse,
589
+ whenNotToUse: item.whenNotToUse,
590
+ operationCount: item.operationCount,
591
+ topOperations: item.topOperations
592
+ }))
593
+ });
594
+ }
595
+ },
596
+ { optional: typedToolCatalog.optional ?? true }
597
+ );
598
+ api.registerTool(
599
+ {
600
+ name: "redai_router_search",
601
+ description: "Tim operation phu hop theo y nghia nghiep vu, module va tu khoa.",
602
+ parameters: {
603
+ type: "object",
604
+ properties: {
605
+ query: { type: "string" },
606
+ moduleId: { type: "string" },
607
+ limit: { type: "number", default: 8 }
608
+ },
609
+ required: ["query"],
610
+ additionalProperties: false
611
+ },
612
+ execute: async (_id, rawParams) => {
613
+ const params = rawParams ?? {};
614
+ const sitemap = await getSitemap();
615
+ const moduleEntries = params.moduleId ? sitemap.modules.filter((item) => item.id === params.moduleId) : sitemap.modules;
616
+ const queryTokens = tokenize(params.query);
617
+ const intent = inferQueryIntent(params.query, queryTokens);
618
+ const results = [];
619
+ for (const entry of moduleEntries) {
620
+ const moduleDoc = await getModuleDoc(entry.id);
621
+ if (!moduleDoc) {
622
+ continue;
623
+ }
624
+ for (const operation of moduleDoc.operations) {
625
+ const score = scoreOperation(queryTokens, intent, params.moduleId, moduleDoc, operation);
626
+ if (score < 12) {
627
+ continue;
628
+ }
629
+ results.push({
630
+ moduleId: moduleDoc.id,
631
+ moduleTitle: moduleDoc.title,
632
+ operationId: operation.id,
633
+ title: operation.title,
634
+ summary: operation.summary,
635
+ riskLevel: operation.riskLevel,
636
+ reasonMatched: buildMatchReasons(queryTokens, intent, moduleDoc, operation),
637
+ score
638
+ });
639
+ }
640
+ }
641
+ results.sort((left, right) => right.score - left.score);
642
+ return formatToolResult({
643
+ query: params.query,
644
+ moduleId: params.moduleId ?? null,
645
+ inferredIntent: intent,
646
+ matches: results.slice(0, Math.max(1, Math.min(params.limit ?? 8, 20)))
647
+ });
648
+ }
649
+ },
650
+ { optional: typedToolCatalog.optional ?? true }
651
+ );
652
+ api.registerTool(
653
+ {
654
+ name: "redai_router_get_module",
655
+ description: "Lay metadata module, feature groups, common tasks va operations shortlist.",
656
+ parameters: {
657
+ type: "object",
658
+ properties: {
659
+ moduleId: { type: "string" },
660
+ includeOperations: { type: "boolean", default: true },
661
+ operationLimit: { type: "number", default: 12 }
662
+ },
663
+ required: ["moduleId"],
664
+ additionalProperties: false
665
+ },
666
+ execute: async (_id, rawParams) => {
667
+ const params = rawParams ?? {};
668
+ const moduleDoc = await getModuleDoc(params.moduleId);
669
+ if (!moduleDoc) {
670
+ return formatToolResult({ ok: false, status: 404, statusText: "MODULE_NOT_FOUND", data: { moduleId: params.moduleId } });
671
+ }
672
+ return formatToolResult({
673
+ id: moduleDoc.id,
674
+ title: moduleDoc.title,
675
+ summary: moduleDoc.summary,
676
+ purpose: moduleDoc.purpose,
677
+ whenToUse: moduleDoc.whenToUse,
678
+ whenNotToUse: moduleDoc.whenNotToUse,
679
+ keywords: moduleDoc.keywords,
680
+ commonTasks: moduleDoc.commonTasks,
681
+ featureGroups: moduleDoc.featureGroups,
682
+ operations: params.includeOperations === false ? void 0 : moduleDoc.operations.slice(0, Math.max(1, Math.min(params.operationLimit ?? 12, 40))).map(summarizeOperation)
683
+ });
684
+ }
685
+ },
686
+ { optional: typedToolCatalog.optional ?? true }
687
+ );
688
+ api.registerTool(
689
+ {
690
+ name: "redai_router_get_operation",
691
+ description: "Lay metadata chi tiet va schema tham so cua mot operation cu the.",
692
+ parameters: {
693
+ type: "object",
694
+ properties: {
695
+ operationId: { type: "string" }
696
+ },
697
+ required: ["operationId"],
698
+ additionalProperties: false
699
+ },
700
+ execute: async (_id, rawParams) => {
701
+ const params = rawParams ?? {};
702
+ const operation = await findOperation(params.operationId);
703
+ if (!operation) {
704
+ return formatToolResult({ ok: false, status: 404, statusText: "OPERATION_NOT_FOUND", data: { operationId: params.operationId } });
705
+ }
706
+ return formatToolResult(operation);
707
+ }
708
+ },
709
+ { optional: typedToolCatalog.optional ?? true }
710
+ );
711
+ api.registerTool(
712
+ {
713
+ name: "redai_router_execute",
714
+ description: "Execute operation thong qua router va auth RedAI da login trong DM.",
715
+ parameters: {
716
+ type: "object",
717
+ properties: {
718
+ operationId: { type: "string" },
719
+ telegramUserId: { type: "string" },
720
+ args: { type: "object", additionalProperties: true },
721
+ confirm: { type: "boolean", default: false }
722
+ },
723
+ required: ["operationId", "telegramUserId"],
724
+ additionalProperties: false
725
+ },
726
+ execute: async (_id, rawParams) => {
727
+ const params = rawParams ?? {};
728
+ const operation = await findOperation(params.operationId);
729
+ if (!operation) {
730
+ return formatToolResult({ ok: false, status: 404, statusText: "OPERATION_NOT_FOUND", data: { operationId: params.operationId } });
731
+ }
732
+ if (operation.confirmationRequired && !params.confirm) {
733
+ return formatToolResult({
734
+ ok: false,
735
+ status: 412,
736
+ statusText: "CONFIRMATION_REQUIRED",
737
+ data: {
738
+ operationId: operation.id,
739
+ title: operation.title,
740
+ riskLevel: operation.riskLevel,
741
+ message: "Operation nay co risk cao hoac co tac dong that. Hay goi lai voi confirm=true sau khi da xac nhan voi user."
742
+ }
743
+ });
744
+ }
745
+ if (operation.riskLevel === "destructive" && !params.confirm) {
746
+ return formatToolResult({
747
+ ok: false,
748
+ status: 412,
749
+ statusText: "DESTRUCTIVE_CONFIRMATION_REQUIRED",
750
+ data: {
751
+ operationId: operation.id,
752
+ title: operation.title,
753
+ riskLevel: operation.riskLevel,
754
+ message: "Operation destructive bi chan mac dinh. Chi duoc goi khi user xac nhan ro va confirm=true."
755
+ }
756
+ });
757
+ }
758
+ const resolved = await resolveUserRuntimeConfig(api, params.telegramUserId);
759
+ if (!resolved) {
760
+ return authRequiredResult(params.telegramUserId);
761
+ }
762
+ try {
763
+ const response = await executeOperation(resolved.scopedConfig, operation, params.args);
764
+ return formatToolResult({
765
+ operationId: operation.id,
766
+ ok: response.ok,
767
+ status: response.status,
768
+ statusText: response.statusText,
769
+ data: response.data
770
+ });
771
+ } catch (error) {
772
+ if (error instanceof RedaiAgentError) {
773
+ return formatToolResult({
774
+ ok: false,
775
+ status: error.status ?? 500,
776
+ statusText: error.message,
777
+ data: error.body
778
+ });
779
+ }
780
+ return formatToolResult({
781
+ ok: false,
782
+ status: 500,
783
+ statusText: "ROUTER_EXECUTION_FAILED",
784
+ data: String(error)
785
+ });
786
+ }
787
+ }
788
+ },
789
+ { optional: typedToolCatalog.optional ?? true }
790
+ );
791
+ }
792
+ export {
793
+ register as default
794
+ };
795
+ //# sourceMappingURL=index.js.map