@elizaos/plugin-knowledge 1.5.11 → 1.5.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ // src/index.ts
2
+ import { logger as logger11 } from "@elizaos/core";
3
+
1
4
  // src/service.ts
2
5
  import {
3
6
  createUniqueUuid,
@@ -3030,8 +3033,97 @@ var knowledgeProvider = {
3030
3033
  }
3031
3034
  };
3032
3035
 
3036
+ // src/documents-provider.ts
3037
+ import { addHeader as addHeader2, logger as logger8, MemoryType as MemoryType3 } from "@elizaos/core";
3038
+ var documentsProvider = {
3039
+ name: "AVAILABLE_DOCUMENTS",
3040
+ description: "List of documents available in the knowledge base. Shows which documents the agent can reference and retrieve information from.",
3041
+ dynamic: false,
3042
+ // Static provider - doesn't change based on the message
3043
+ get: async (runtime) => {
3044
+ try {
3045
+ const knowledgeService = runtime.getService("knowledge");
3046
+ if (!knowledgeService) {
3047
+ logger8.warn("Knowledge service not available for documents provider");
3048
+ return {
3049
+ data: { documents: [] },
3050
+ values: { documents: "" },
3051
+ text: ""
3052
+ };
3053
+ }
3054
+ const allMemories = await knowledgeService.getMemories({
3055
+ tableName: "documents",
3056
+ roomId: runtime.agentId,
3057
+ count: 100
3058
+ // Limit to 100 documents to avoid context overflow
3059
+ });
3060
+ const documents = allMemories.filter(
3061
+ (memory) => memory.metadata?.type === MemoryType3.DOCUMENT
3062
+ );
3063
+ if (!documents || documents.length === 0) {
3064
+ return {
3065
+ data: { documents: [] },
3066
+ values: { documents: "" },
3067
+ text: ""
3068
+ };
3069
+ }
3070
+ const documentsList = documents.map((doc, index) => {
3071
+ const metadata = doc.metadata;
3072
+ const filename = metadata?.filename || metadata?.title || `Document ${index + 1}`;
3073
+ const fileType = metadata?.fileExt || metadata?.fileType || "unknown";
3074
+ const source = metadata?.source || "upload";
3075
+ const fileSize = metadata?.fileSize;
3076
+ const parts = [filename];
3077
+ if (fileType && fileType !== "unknown") {
3078
+ parts.push(fileType);
3079
+ }
3080
+ if (fileSize) {
3081
+ const sizeKB = Math.round(fileSize / 1024);
3082
+ if (sizeKB > 1024) {
3083
+ parts.push(`${Math.round(sizeKB / 1024)}MB`);
3084
+ } else {
3085
+ parts.push(`${sizeKB}KB`);
3086
+ }
3087
+ }
3088
+ if (source && source !== "upload") {
3089
+ parts.push(`from ${source}`);
3090
+ }
3091
+ return parts.join(" - ");
3092
+ }).join("\n");
3093
+ const documentsText = addHeader2(
3094
+ "# Available Documents",
3095
+ `${documents.length} document(s) in knowledge base:
3096
+ ${documentsList}`
3097
+ );
3098
+ return {
3099
+ data: {
3100
+ documents: documents.map((doc) => ({
3101
+ id: doc.id,
3102
+ filename: doc.metadata?.filename || doc.metadata?.title,
3103
+ fileType: doc.metadata?.fileType || doc.metadata?.fileExt,
3104
+ source: doc.metadata?.source
3105
+ })),
3106
+ count: documents.length
3107
+ },
3108
+ values: {
3109
+ documentsCount: documents.length,
3110
+ documents: documentsList
3111
+ },
3112
+ text: documentsText
3113
+ };
3114
+ } catch (error) {
3115
+ logger8.error("Error in documents provider:", error.message);
3116
+ return {
3117
+ data: { documents: [], error: error.message },
3118
+ values: { documents: "" },
3119
+ text: ""
3120
+ };
3121
+ }
3122
+ }
3123
+ };
3124
+
3033
3125
  // src/tests.ts
3034
- import { MemoryType as MemoryType3, ModelType as ModelType3 } from "@elizaos/core";
3126
+ import { MemoryType as MemoryType4, ModelType as ModelType3 } from "@elizaos/core";
3035
3127
  import { Buffer as Buffer3 } from "buffer";
3036
3128
  import * as fs2 from "fs";
3037
3129
  import * as path2 from "path";
@@ -3156,9 +3248,9 @@ function createMockRuntime(overrides) {
3156
3248
  const results = Array.from(memories.values()).filter((m) => {
3157
3249
  if (params.roomId && m.roomId !== params.roomId) return false;
3158
3250
  if (params.entityId && m.entityId !== params.entityId) return false;
3159
- if (params.tableName === "knowledge" && m.metadata?.type !== MemoryType3.FRAGMENT)
3251
+ if (params.tableName === "knowledge" && m.metadata?.type !== MemoryType4.FRAGMENT)
3160
3252
  return false;
3161
- if (params.tableName === "documents" && m.metadata?.type !== MemoryType3.DOCUMENT)
3253
+ if (params.tableName === "documents" && m.metadata?.type !== MemoryType4.DOCUMENT)
3162
3254
  return false;
3163
3255
  return true;
3164
3256
  });
@@ -3172,7 +3264,7 @@ function createMockRuntime(overrides) {
3172
3264
  },
3173
3265
  async searchMemories(params) {
3174
3266
  const fragments = Array.from(memories.values()).filter(
3175
- (m) => m.metadata?.type === MemoryType3.FRAGMENT
3267
+ (m) => m.metadata?.type === MemoryType4.FRAGMENT
3176
3268
  );
3177
3269
  return fragments.map((f) => ({
3178
3270
  ...f,
@@ -3544,7 +3636,7 @@ var KnowledgeTestSuite = class {
3544
3636
  if (!memory.id) {
3545
3637
  throw new Error("Document memory should have an ID");
3546
3638
  }
3547
- if (memory.metadata?.type !== MemoryType3.DOCUMENT) {
3639
+ if (memory.metadata?.type !== MemoryType4.DOCUMENT) {
3548
3640
  throw new Error("Document memory should have DOCUMENT type");
3549
3641
  }
3550
3642
  if (memory.content.text !== params.text) {
@@ -3917,7 +4009,7 @@ var KnowledgeTestSuite = class {
3917
4009
  var tests_default = new KnowledgeTestSuite();
3918
4010
 
3919
4011
  // src/actions.ts
3920
- import { logger as logger8, stringToUuid } from "@elizaos/core";
4012
+ import { logger as logger9, stringToUuid } from "@elizaos/core";
3921
4013
  import * as fs3 from "fs";
3922
4014
  import * as path3 from "path";
3923
4015
  var processKnowledgeAction = {
@@ -3975,7 +4067,7 @@ var processKnowledgeAction = {
3975
4067
  const hasPath = pathPattern.test(text);
3976
4068
  const service = runtime.getService(KnowledgeService.serviceType);
3977
4069
  if (!service) {
3978
- logger8.warn("Knowledge service not available for PROCESS_KNOWLEDGE action");
4070
+ logger9.warn("Knowledge service not available for PROCESS_KNOWLEDGE action");
3979
4071
  return false;
3980
4072
  }
3981
4073
  return hasKeyword || hasPath;
@@ -4053,7 +4145,7 @@ var processKnowledgeAction = {
4053
4145
  await callback(response);
4054
4146
  }
4055
4147
  } catch (error) {
4056
- logger8.error({ error }, "Error in PROCESS_KNOWLEDGE action");
4148
+ logger9.error({ error }, "Error in PROCESS_KNOWLEDGE action");
4057
4149
  const errorResponse = {
4058
4150
  text: `I encountered an error while processing the knowledge: ${error instanceof Error ? error.message : "Unknown error"}`
4059
4151
  };
@@ -4144,7 +4236,7 @@ ${formattedResults}`
4144
4236
  await callback(response);
4145
4237
  }
4146
4238
  } catch (error) {
4147
- logger8.error({ error }, "Error in SEARCH_KNOWLEDGE action");
4239
+ logger9.error({ error }, "Error in SEARCH_KNOWLEDGE action");
4148
4240
  const errorResponse = {
4149
4241
  text: `I encountered an error while searching the knowledge base: ${error instanceof Error ? error.message : "Unknown error"}`
4150
4242
  };
@@ -4157,7 +4249,7 @@ ${formattedResults}`
4157
4249
  var knowledgeActions = [processKnowledgeAction, searchKnowledgeAction];
4158
4250
 
4159
4251
  // src/routes.ts
4160
- import { MemoryType as MemoryType4, createUniqueUuid as createUniqueUuid2, logger as logger9, ModelType as ModelType4 } from "@elizaos/core";
4252
+ import { MemoryType as MemoryType5, createUniqueUuid as createUniqueUuid2, logger as logger10, ModelType as ModelType4 } from "@elizaos/core";
4161
4253
  import fs4 from "fs";
4162
4254
  import path4 from "path";
4163
4255
  import multer from "multer";
@@ -4208,7 +4300,7 @@ var cleanupFile = (filePath) => {
4208
4300
  try {
4209
4301
  fs4.unlinkSync(filePath);
4210
4302
  } catch (error) {
4211
- logger9.error({ error }, `Error cleaning up file ${filePath}`);
4303
+ logger10.error({ error }, `Error cleaning up file ${filePath}`);
4212
4304
  }
4213
4305
  }
4214
4306
  };
@@ -4235,15 +4327,15 @@ async function uploadKnowledgeHandler(req, res, runtime) {
4235
4327
  }
4236
4328
  const invalidFiles = files.filter((file) => {
4237
4329
  if (file.size === 0) {
4238
- logger9.warn(`File ${file.originalname} is empty`);
4330
+ logger10.warn(`File ${file.originalname} is empty`);
4239
4331
  return true;
4240
4332
  }
4241
4333
  if (!file.originalname || file.originalname.trim() === "") {
4242
- logger9.warn(`File has no name`);
4334
+ logger10.warn(`File has no name`);
4243
4335
  return true;
4244
4336
  }
4245
4337
  if (!file.path) {
4246
- logger9.warn(`File ${file.originalname} has no path`);
4338
+ logger10.warn(`File ${file.originalname} has no path`);
4247
4339
  return true;
4248
4340
  }
4249
4341
  return false;
@@ -4260,7 +4352,7 @@ async function uploadKnowledgeHandler(req, res, runtime) {
4260
4352
  }
4261
4353
  const agentId = req.body.agentId || req.query.agentId;
4262
4354
  if (!agentId) {
4263
- logger9.error("[Document Processor] \u274C No agent ID provided in upload request");
4355
+ logger10.error("[Document Processor] \u274C No agent ID provided in upload request");
4264
4356
  return sendError(
4265
4357
  res,
4266
4358
  400,
@@ -4269,11 +4361,11 @@ async function uploadKnowledgeHandler(req, res, runtime) {
4269
4361
  );
4270
4362
  }
4271
4363
  const worldId = req.body.worldId || agentId;
4272
- logger9.info(`[Document Processor] \u{1F4E4} Processing file upload for agent: ${agentId}`);
4364
+ logger10.info(`[Document Processor] \u{1F4E4} Processing file upload for agent: ${agentId}`);
4273
4365
  const processingPromises = files.map(async (file, index) => {
4274
4366
  const originalFilename = file.originalname;
4275
4367
  const filePath = file.path;
4276
- logger9.debug(
4368
+ logger10.debug(
4277
4369
  `[Document Processor] \u{1F4C4} Processing file: ${originalFilename} (agent: ${agentId})`
4278
4370
  );
4279
4371
  try {
@@ -4308,7 +4400,7 @@ async function uploadKnowledgeHandler(req, res, runtime) {
4308
4400
  status: "success"
4309
4401
  };
4310
4402
  } catch (fileError) {
4311
- logger9.error(
4403
+ logger10.error(
4312
4404
  `[Document Processor] \u274C Error processing file ${file.originalname}:`,
4313
4405
  fileError
4314
4406
  );
@@ -4331,7 +4423,7 @@ async function uploadKnowledgeHandler(req, res, runtime) {
4331
4423
  }
4332
4424
  const agentId = req.body.agentId || req.query.agentId;
4333
4425
  if (!agentId) {
4334
- logger9.error("[Document Processor] \u274C No agent ID provided in URL request");
4426
+ logger10.error("[Document Processor] \u274C No agent ID provided in URL request");
4335
4427
  return sendError(
4336
4428
  res,
4337
4429
  400,
@@ -4339,7 +4431,7 @@ async function uploadKnowledgeHandler(req, res, runtime) {
4339
4431
  "Agent ID is required for uploading knowledge from URLs"
4340
4432
  );
4341
4433
  }
4342
- logger9.info(`[Document Processor] \u{1F4E4} Processing URL upload for agent: ${agentId}`);
4434
+ logger10.info(`[Document Processor] \u{1F4E4} Processing URL upload for agent: ${agentId}`);
4343
4435
  const processingPromises = fileUrls.map(async (fileUrl) => {
4344
4436
  try {
4345
4437
  const normalizedUrl = normalizeS3Url(fileUrl);
@@ -4347,7 +4439,7 @@ async function uploadKnowledgeHandler(req, res, runtime) {
4347
4439
  const pathSegments = urlObject.pathname.split("/");
4348
4440
  const encodedFilename = pathSegments[pathSegments.length - 1] || "document.pdf";
4349
4441
  const originalFilename = decodeURIComponent(encodedFilename);
4350
- logger9.debug(`[Document Processor] \u{1F310} Fetching content from URL: ${fileUrl}`);
4442
+ logger10.debug(`[Document Processor] \u{1F310} Fetching content from URL: ${fileUrl}`);
4351
4443
  const { content, contentType: fetchedContentType } = await fetchUrlContent(fileUrl);
4352
4444
  let contentType = fetchedContentType;
4353
4445
  if (contentType === "application/octet-stream") {
@@ -4387,7 +4479,7 @@ async function uploadKnowledgeHandler(req, res, runtime) {
4387
4479
  url: normalizedUrl
4388
4480
  }
4389
4481
  };
4390
- logger9.debug(
4482
+ logger10.debug(
4391
4483
  `[Document Processor] \u{1F4C4} Processing knowledge from URL: ${originalFilename} (type: ${contentType})`
4392
4484
  );
4393
4485
  const result = await service.addKnowledge(addKnowledgeOpts);
@@ -4402,7 +4494,7 @@ async function uploadKnowledgeHandler(req, res, runtime) {
4402
4494
  status: "success"
4403
4495
  };
4404
4496
  } catch (urlError) {
4405
- logger9.error(`[Document Processor] \u274C Error processing URL ${fileUrl}:`, urlError);
4497
+ logger10.error(`[Document Processor] \u274C Error processing URL ${fileUrl}:`, urlError);
4406
4498
  return {
4407
4499
  fileUrl,
4408
4500
  status: "error_processing",
@@ -4414,7 +4506,7 @@ async function uploadKnowledgeHandler(req, res, runtime) {
4414
4506
  sendSuccess(res, results);
4415
4507
  }
4416
4508
  } catch (error) {
4417
- logger9.error({ error }, "[Document Processor] \u274C Error processing knowledge");
4509
+ logger10.error({ error }, "[Document Processor] \u274C Error processing knowledge");
4418
4510
  if (hasUploadedFiles) {
4419
4511
  cleanupFiles(req.files);
4420
4512
  }
@@ -4453,7 +4545,7 @@ async function getKnowledgeDocumentsHandler(req, res, runtime) {
4453
4545
  // Or if the URL is stored in the metadata (check if it exists)
4454
4546
  memory.metadata && "url" in memory.metadata && typeof memory.metadata.url === "string" && normalizedRequestUrls.includes(normalizeS3Url(memory.metadata.url))
4455
4547
  );
4456
- logger9.debug(
4548
+ logger10.debug(
4457
4549
  `[Document Processor] \u{1F50D} Filtered documents by URLs: ${fileUrls.length} URLs, found ${filteredMemories.length} matching documents`
4458
4550
  );
4459
4551
  }
@@ -4468,12 +4560,12 @@ async function getKnowledgeDocumentsHandler(req, res, runtime) {
4468
4560
  totalRequested: fileUrls ? fileUrls.length : 0
4469
4561
  });
4470
4562
  } catch (error) {
4471
- logger9.error({ error }, "[Document Processor] \u274C Error retrieving documents");
4563
+ logger10.error({ error }, "[Document Processor] \u274C Error retrieving documents");
4472
4564
  sendError(res, 500, "RETRIEVAL_ERROR", "Failed to retrieve documents", error.message);
4473
4565
  }
4474
4566
  }
4475
4567
  async function deleteKnowledgeDocumentHandler(req, res, runtime) {
4476
- logger9.debug(`[Document Processor] \u{1F5D1}\uFE0F DELETE request for document: ${req.params.knowledgeId}`);
4568
+ logger10.debug(`[Document Processor] \u{1F5D1}\uFE0F DELETE request for document: ${req.params.knowledgeId}`);
4477
4569
  const service = runtime.getService(KnowledgeService.serviceType);
4478
4570
  if (!service) {
4479
4571
  return sendError(
@@ -4485,22 +4577,22 @@ async function deleteKnowledgeDocumentHandler(req, res, runtime) {
4485
4577
  }
4486
4578
  const knowledgeId = req.params.knowledgeId;
4487
4579
  if (!knowledgeId || knowledgeId.length < 36) {
4488
- logger9.error(`[Document Processor] \u274C Invalid knowledge ID format: ${knowledgeId}`);
4580
+ logger10.error(`[Document Processor] \u274C Invalid knowledge ID format: ${knowledgeId}`);
4489
4581
  return sendError(res, 400, "INVALID_ID", "Invalid Knowledge ID format");
4490
4582
  }
4491
4583
  try {
4492
4584
  const typedKnowledgeId = knowledgeId;
4493
- logger9.debug(`[Document Processor] \u{1F5D1}\uFE0F Deleting document: ${typedKnowledgeId}`);
4585
+ logger10.debug(`[Document Processor] \u{1F5D1}\uFE0F Deleting document: ${typedKnowledgeId}`);
4494
4586
  await service.deleteMemory(typedKnowledgeId);
4495
- logger9.info(`[Document Processor] \u2705 Successfully deleted document: ${typedKnowledgeId}`);
4587
+ logger10.info(`[Document Processor] \u2705 Successfully deleted document: ${typedKnowledgeId}`);
4496
4588
  sendSuccess(res, null, 204);
4497
4589
  } catch (error) {
4498
- logger9.error({ error }, `[Document Processor] \u274C Error deleting document ${knowledgeId}`);
4590
+ logger10.error({ error }, `[Document Processor] \u274C Error deleting document ${knowledgeId}`);
4499
4591
  sendError(res, 500, "DELETE_ERROR", "Failed to delete document", error.message);
4500
4592
  }
4501
4593
  }
4502
4594
  async function getKnowledgeByIdHandler(req, res, runtime) {
4503
- logger9.debug(`[Document Processor] \u{1F50D} GET request for document: ${req.params.knowledgeId}`);
4595
+ logger10.debug(`[Document Processor] \u{1F50D} GET request for document: ${req.params.knowledgeId}`);
4504
4596
  const service = runtime.getService(KnowledgeService.serviceType);
4505
4597
  if (!service) {
4506
4598
  return sendError(
@@ -4512,11 +4604,11 @@ async function getKnowledgeByIdHandler(req, res, runtime) {
4512
4604
  }
4513
4605
  const knowledgeId = req.params.knowledgeId;
4514
4606
  if (!knowledgeId || knowledgeId.length < 36) {
4515
- logger9.error(`[Document Processor] \u274C Invalid knowledge ID format: ${knowledgeId}`);
4607
+ logger10.error(`[Document Processor] \u274C Invalid knowledge ID format: ${knowledgeId}`);
4516
4608
  return sendError(res, 400, "INVALID_ID", "Invalid Knowledge ID format");
4517
4609
  }
4518
4610
  try {
4519
- logger9.debug(`[Document Processor] \u{1F50D} Retrieving document: ${knowledgeId}`);
4611
+ logger10.debug(`[Document Processor] \u{1F50D} Retrieving document: ${knowledgeId}`);
4520
4612
  const agentId = req.query.agentId;
4521
4613
  const memories = await service.getMemories({
4522
4614
  tableName: "documents",
@@ -4533,20 +4625,20 @@ async function getKnowledgeByIdHandler(req, res, runtime) {
4533
4625
  };
4534
4626
  sendSuccess(res, { document: cleanDocument });
4535
4627
  } catch (error) {
4536
- logger9.error({ error }, `[Document Processor] \u274C Error retrieving document ${knowledgeId}`);
4628
+ logger10.error({ error }, `[Document Processor] \u274C Error retrieving document ${knowledgeId}`);
4537
4629
  sendError(res, 500, "RETRIEVAL_ERROR", "Failed to retrieve document", error.message);
4538
4630
  }
4539
4631
  }
4540
4632
  async function knowledgePanelHandler(req, res, runtime) {
4541
4633
  const agentId = runtime.agentId;
4542
- logger9.debug(`[Document Processor] \u{1F310} Serving knowledge panel for agent ${agentId}`);
4634
+ logger10.debug(`[Document Processor] \u{1F310} Serving knowledge panel for agent ${agentId}`);
4543
4635
  const requestPath = req.originalUrl || req.url || req.path;
4544
4636
  const pluginBasePath = requestPath.replace(/\/display.*$/, "");
4545
- logger9.debug(`[Document Processor] \u{1F310} Plugin base path: ${pluginBasePath}`);
4637
+ logger10.debug(`[Document Processor] \u{1F310} Plugin base path: ${pluginBasePath}`);
4546
4638
  try {
4547
4639
  const currentDir = path4.dirname(new URL(import.meta.url).pathname);
4548
4640
  const frontendPath = path4.join(currentDir, "../dist/index.html");
4549
- logger9.debug(`[Document Processor] \u{1F310} Looking for frontend at: ${frontendPath}`);
4641
+ logger10.debug(`[Document Processor] \u{1F310} Looking for frontend at: ${frontendPath}`);
4550
4642
  if (fs4.existsSync(frontendPath)) {
4551
4643
  const html = await fs4.promises.readFile(frontendPath, "utf8");
4552
4644
  let injectedHtml = html.replace(
@@ -4582,10 +4674,10 @@ async function knowledgePanelHandler(req, res, runtime) {
4582
4674
  }
4583
4675
  }
4584
4676
  } catch (manifestError) {
4585
- logger9.error({ error: manifestError }, "[Document Processor] \u274C Error reading manifest");
4677
+ logger10.error({ error: manifestError }, "[Document Processor] \u274C Error reading manifest");
4586
4678
  }
4587
4679
  }
4588
- logger9.debug(`[Document Processor] \u{1F310} Using fallback with CSS: ${cssFile}, JS: ${jsFile}`);
4680
+ logger10.debug(`[Document Processor] \u{1F310} Using fallback with CSS: ${cssFile}, JS: ${jsFile}`);
4589
4681
  const html = `
4590
4682
  <!DOCTYPE html>
4591
4683
  <html lang="en">
@@ -4619,14 +4711,14 @@ async function knowledgePanelHandler(req, res, runtime) {
4619
4711
  res.end(html);
4620
4712
  }
4621
4713
  } catch (error) {
4622
- logger9.error({ error }, "[Document Processor] \u274C Error serving frontend");
4714
+ logger10.error({ error }, "[Document Processor] \u274C Error serving frontend");
4623
4715
  sendError(res, 500, "FRONTEND_ERROR", "Failed to load knowledge panel", error.message);
4624
4716
  }
4625
4717
  }
4626
4718
  async function frontendAssetHandler(req, res, runtime) {
4627
4719
  try {
4628
4720
  const fullPath = req.originalUrl || req.url || req.path;
4629
- logger9.debug(`[Document Processor] \u{1F310} Asset request: ${fullPath}`);
4721
+ logger10.debug(`[Document Processor] \u{1F310} Asset request: ${fullPath}`);
4630
4722
  const currentDir = path4.dirname(new URL(import.meta.url).pathname);
4631
4723
  const assetsMarker = "/assets/";
4632
4724
  const assetsStartIndex = fullPath.lastIndexOf(assetsMarker);
@@ -4647,7 +4739,7 @@ async function frontendAssetHandler(req, res, runtime) {
4647
4739
  );
4648
4740
  }
4649
4741
  const assetPath = path4.join(currentDir, "../dist/assets", assetName);
4650
- logger9.debug(`[Document Processor] \u{1F310} Serving asset: ${assetPath}`);
4742
+ logger10.debug(`[Document Processor] \u{1F310} Serving asset: ${assetPath}`);
4651
4743
  if (fs4.existsSync(assetPath)) {
4652
4744
  const fileStream = fs4.createReadStream(assetPath);
4653
4745
  let contentType = "application/octet-stream";
@@ -4662,7 +4754,7 @@ async function frontendAssetHandler(req, res, runtime) {
4662
4754
  sendError(res, 404, "NOT_FOUND", `Asset not found: ${req.url}`);
4663
4755
  }
4664
4756
  } catch (error) {
4665
- logger9.error({ error }, `[Document Processor] \u274C Error serving asset ${req.url}`);
4757
+ logger10.error({ error }, `[Document Processor] \u274C Error serving asset ${req.url}`);
4666
4758
  sendError(res, 500, "ASSET_ERROR", `Failed to load asset ${req.url}`, error.message);
4667
4759
  }
4668
4760
  }
@@ -4723,7 +4815,7 @@ async function getKnowledgeChunksHandler(req, res, runtime) {
4723
4815
  }
4724
4816
  });
4725
4817
  } catch (error) {
4726
- logger9.error({ error }, "[Document Processor] \u274C Error retrieving chunks");
4818
+ logger10.error({ error }, "[Document Processor] \u274C Error retrieving chunks");
4727
4819
  sendError(res, 500, "RETRIEVAL_ERROR", "Failed to retrieve knowledge chunks", error.message);
4728
4820
  }
4729
4821
  }
@@ -4745,14 +4837,14 @@ async function searchKnowledgeHandler(req, res, runtime) {
4745
4837
  return sendError(res, 400, "INVALID_QUERY", "Search query cannot be empty");
4746
4838
  }
4747
4839
  if (req.query.threshold && (parsedThreshold < 0 || parsedThreshold > 1)) {
4748
- logger9.debug(
4840
+ logger10.debug(
4749
4841
  `[Document Processor] \u{1F50D} Threshold value ${parsedThreshold} was clamped to ${matchThreshold}`
4750
4842
  );
4751
4843
  }
4752
4844
  if (req.query.limit && (parsedLimit < 1 || parsedLimit > 100)) {
4753
- logger9.debug(`[Document Processor] \u{1F50D} Limit value ${parsedLimit} was clamped to ${limit}`);
4845
+ logger10.debug(`[Document Processor] \u{1F50D} Limit value ${parsedLimit} was clamped to ${limit}`);
4754
4846
  }
4755
- logger9.debug(
4847
+ logger10.debug(
4756
4848
  `[Document Processor] \u{1F50D} Searching: "${searchText}" (threshold: ${matchThreshold}, limit: ${limit})`
4757
4849
  );
4758
4850
  const embedding = await runtime.useModel(ModelType4.TEXT_EMBEDDING, {
@@ -4779,7 +4871,7 @@ async function searchKnowledgeHandler(req, res, runtime) {
4779
4871
  documentFilename = document.metadata.filename || documentFilename;
4780
4872
  }
4781
4873
  } catch (e) {
4782
- logger9.debug(`Could not fetch document ${documentId} for fragment`);
4874
+ logger10.debug(`Could not fetch document ${documentId} for fragment`);
4783
4875
  }
4784
4876
  }
4785
4877
  return {
@@ -4794,7 +4886,7 @@ async function searchKnowledgeHandler(req, res, runtime) {
4794
4886
  };
4795
4887
  })
4796
4888
  );
4797
- logger9.info(
4889
+ logger10.info(
4798
4890
  `[Document Processor] \u{1F50D} Found ${enhancedResults.length} results for: "${searchText}"`
4799
4891
  );
4800
4892
  sendSuccess(res, {
@@ -4804,7 +4896,7 @@ async function searchKnowledgeHandler(req, res, runtime) {
4804
4896
  count: enhancedResults.length
4805
4897
  });
4806
4898
  } catch (error) {
4807
- logger9.error({ error }, "[Document Processor] \u274C Error searching knowledge");
4899
+ logger10.error({ error }, "[Document Processor] \u274C Error searching knowledge");
4808
4900
  sendError(res, 500, "SEARCH_ERROR", "Failed to search knowledge", error.message);
4809
4901
  }
4810
4902
  }
@@ -4821,7 +4913,7 @@ async function getGraphNodesHandler(req, res, runtime) {
4821
4913
  const page = Number.isNaN(parsedPage) || parsedPage < 1 ? 1 : parsedPage;
4822
4914
  const limit = Number.isNaN(parsedLimit) || parsedLimit < 1 ? 20 : Math.min(parsedLimit, 50);
4823
4915
  const offset = (page - 1) * limit;
4824
- logger9.debug(
4916
+ logger10.debug(
4825
4917
  `[Graph API] \u{1F4CA} Fetching graph nodes: page=${page}, limit=${limit}, type=${type || "all"}, agent=${agentId}`
4826
4918
  );
4827
4919
  const totalDocuments = await service.countMemories({
@@ -4842,7 +4934,7 @@ async function getGraphNodesHandler(req, res, runtime) {
4842
4934
  const links = [];
4843
4935
  paginatedDocuments.forEach((doc) => {
4844
4936
  if (!doc.id) {
4845
- logger9.warn(`[Graph API] \u26A0\uFE0F Skipping document without ID`);
4937
+ logger10.warn(`[Graph API] \u26A0\uFE0F Skipping document without ID`);
4846
4938
  return;
4847
4939
  }
4848
4940
  nodes.push({ id: doc.id, type: "document" });
@@ -4854,9 +4946,9 @@ async function getGraphNodesHandler(req, res, runtime) {
4854
4946
  count: 5e4
4855
4947
  // Reduced from 100000 - still high enough for large documents
4856
4948
  });
4857
- logger9.debug(`[Graph API] \u{1F4CA} Total fragments found: ${allFragments.length}`);
4949
+ logger10.debug(`[Graph API] \u{1F4CA} Total fragments found: ${allFragments.length}`);
4858
4950
  if (allFragments.length > 0) {
4859
- logger9.debug(
4951
+ logger10.debug(
4860
4952
  `[Graph API] \u{1F4CA} Sample fragment metadata: ${JSON.stringify(
4861
4953
  allFragments.slice(0, 3).map((f) => ({
4862
4954
  id: f.id,
@@ -4872,17 +4964,17 @@ async function getGraphNodesHandler(req, res, runtime) {
4872
4964
  const docFragments = allFragments.filter((fragment) => {
4873
4965
  const metadata = fragment.metadata;
4874
4966
  const typeString = typeof metadata?.type === "string" ? metadata.type : null;
4875
- const isFragment = typeString && typeString.toLowerCase() === "fragment" || metadata?.type === MemoryType4.FRAGMENT || // If no type but has documentId, assume it's a fragment
4967
+ const isFragment = typeString && typeString.toLowerCase() === "fragment" || metadata?.type === MemoryType5.FRAGMENT || // If no type but has documentId, assume it's a fragment
4876
4968
  !metadata?.type && metadata?.documentId;
4877
4969
  return metadata?.documentId === doc.id && isFragment;
4878
4970
  });
4879
4971
  if (docFragments.length > 0) {
4880
- logger9.debug(`[Graph API] \u{1F4CA} Document ${doc.id} has ${docFragments.length} fragments`);
4972
+ logger10.debug(`[Graph API] \u{1F4CA} Document ${doc.id} has ${docFragments.length} fragments`);
4881
4973
  }
4882
4974
  docFragments.forEach((frag) => {
4883
4975
  const docId = doc.id;
4884
4976
  if (!frag.id || !docId) {
4885
- logger9.warn(
4977
+ logger10.warn(
4886
4978
  `[Graph API] \u26A0\uFE0F Skipping fragment without ID for document ${docId || "unknown"}`
4887
4979
  );
4888
4980
  return;
@@ -4891,7 +4983,7 @@ async function getGraphNodesHandler(req, res, runtime) {
4891
4983
  links.push({ source: docId, target: frag.id });
4892
4984
  });
4893
4985
  });
4894
- logger9.info(
4986
+ logger10.info(
4895
4987
  `[Graph API] \u{1F4CA} Final graph: ${nodes.length} nodes (${paginatedDocuments.length} documents), ${links.length} links`
4896
4988
  );
4897
4989
  }
@@ -4906,7 +4998,7 @@ async function getGraphNodesHandler(req, res, runtime) {
4906
4998
  }
4907
4999
  });
4908
5000
  } catch (error) {
4909
- logger9.error("[Graph API] \u274C Error fetching graph nodes:", error);
5001
+ logger10.error("[Graph API] \u274C Error fetching graph nodes:", error);
4910
5002
  sendError(res, 500, "GRAPH_ERROR", "Failed to fetch graph nodes", error.message);
4911
5003
  }
4912
5004
  }
@@ -4921,24 +5013,24 @@ async function getGraphNodeDetailsHandler(req, res, runtime) {
4921
5013
  return sendError(res, 400, "INVALID_ID", "Invalid node ID format");
4922
5014
  }
4923
5015
  try {
4924
- logger9.info(`[Graph API] \u{1F4CA} Fetching node details for: ${nodeId}, agent: ${agentId}`);
5016
+ logger10.info(`[Graph API] \u{1F4CA} Fetching node details for: ${nodeId}, agent: ${agentId}`);
4925
5017
  const allDocuments = await service.getMemories({
4926
5018
  tableName: "documents",
4927
5019
  count: 1e4
4928
5020
  });
4929
- logger9.debug(`[Graph API] \u{1F4CA} Total documents in DB: ${allDocuments.length}`);
5021
+ logger10.debug(`[Graph API] \u{1F4CA} Total documents in DB: ${allDocuments.length}`);
4930
5022
  let document = allDocuments.find((doc) => doc.id === nodeId && doc.roomId === agentId);
4931
5023
  if (!document) {
4932
- logger9.debug(`[Graph API] \u{1F4CA} Document not found with roomId filter, trying without filter`);
5024
+ logger10.debug(`[Graph API] \u{1F4CA} Document not found with roomId filter, trying without filter`);
4933
5025
  document = allDocuments.find((doc) => doc.id === nodeId);
4934
5026
  if (document) {
4935
- logger9.warn(
5027
+ logger10.warn(
4936
5028
  `[Graph API] \u26A0\uFE0F Document ${nodeId} found but has different roomId: ${document.roomId} vs ${agentId}`
4937
5029
  );
4938
5030
  }
4939
5031
  }
4940
5032
  if (document) {
4941
- logger9.info(`[Graph API] \u2705 Found document: ${nodeId}`);
5033
+ logger10.info(`[Graph API] \u2705 Found document: ${nodeId}`);
4942
5034
  sendSuccess(res, {
4943
5035
  id: document.id,
4944
5036
  type: "document",
@@ -4952,25 +5044,25 @@ async function getGraphNodeDetailsHandler(req, res, runtime) {
4952
5044
  });
4953
5045
  return;
4954
5046
  }
4955
- logger9.debug(`[Graph API] \u{1F4CA} Document not found, searching in fragments`);
5047
+ logger10.debug(`[Graph API] \u{1F4CA} Document not found, searching in fragments`);
4956
5048
  const allFragments = await service.getMemories({
4957
5049
  tableName: "knowledge",
4958
5050
  count: 5e4
4959
5051
  // Reduced from 100000 - still high enough for large documents
4960
5052
  });
4961
- logger9.debug(`[Graph API] \u{1F4CA} Total fragments in DB: ${allFragments.length}`);
5053
+ logger10.debug(`[Graph API] \u{1F4CA} Total fragments in DB: ${allFragments.length}`);
4962
5054
  let fragment = allFragments.find((frag) => frag.id === nodeId && frag.roomId === agentId);
4963
5055
  if (!fragment) {
4964
- logger9.debug(`[Graph API] \u{1F4CA} Fragment not found with roomId filter, trying without filter`);
5056
+ logger10.debug(`[Graph API] \u{1F4CA} Fragment not found with roomId filter, trying without filter`);
4965
5057
  fragment = allFragments.find((frag) => frag.id === nodeId);
4966
5058
  if (fragment) {
4967
- logger9.warn(
5059
+ logger10.warn(
4968
5060
  `[Graph API] \u26A0\uFE0F Fragment ${nodeId} found but has different roomId: ${fragment.roomId} vs ${agentId}`
4969
5061
  );
4970
5062
  }
4971
5063
  }
4972
5064
  if (fragment) {
4973
- logger9.info(`[Graph API] \u2705 Found fragment: ${nodeId}`);
5065
+ logger10.info(`[Graph API] \u2705 Found fragment: ${nodeId}`);
4974
5066
  sendSuccess(res, {
4975
5067
  id: fragment.id,
4976
5068
  type: "fragment",
@@ -4984,10 +5076,10 @@ async function getGraphNodeDetailsHandler(req, res, runtime) {
4984
5076
  });
4985
5077
  return;
4986
5078
  }
4987
- logger9.error(`[Graph API] \u274C Node ${nodeId} not found in documents or fragments`);
5079
+ logger10.error(`[Graph API] \u274C Node ${nodeId} not found in documents or fragments`);
4988
5080
  sendError(res, 404, "NOT_FOUND", `Node with ID ${nodeId} not found`);
4989
5081
  } catch (error) {
4990
- logger9.error(`[Graph API] \u274C Error fetching node details for ${nodeId}:`, error);
5082
+ logger10.error(`[Graph API] \u274C Error fetching node details for ${nodeId}:`, error);
4991
5083
  sendError(res, 500, "GRAPH_ERROR", "Failed to fetch node details", error.message);
4992
5084
  }
4993
5085
  }
@@ -5002,7 +5094,7 @@ async function expandDocumentGraphHandler(req, res, runtime) {
5002
5094
  return sendError(res, 400, "INVALID_ID", "Invalid document ID format");
5003
5095
  }
5004
5096
  try {
5005
- logger9.debug(`[Graph API] \u{1F4CA} Expanding document: ${documentId}, agent: ${agentId}`);
5097
+ logger10.debug(`[Graph API] \u{1F4CA} Expanding document: ${documentId}, agent: ${agentId}`);
5006
5098
  const allFragments = await service.getMemories({
5007
5099
  tableName: "knowledge",
5008
5100
  roomId: agentId,
@@ -5010,33 +5102,33 @@ async function expandDocumentGraphHandler(req, res, runtime) {
5010
5102
  count: 5e4
5011
5103
  // Reduced from 100000 - still high enough for large documents
5012
5104
  });
5013
- logger9.debug(`[Graph API] \u{1F4CA} Total fragments in knowledge table: ${allFragments.length}`);
5105
+ logger10.debug(`[Graph API] \u{1F4CA} Total fragments in knowledge table: ${allFragments.length}`);
5014
5106
  if (allFragments.length > 0 && process.env.NODE_ENV !== "production") {
5015
- logger9.debug(
5107
+ logger10.debug(
5016
5108
  `[Graph API] \u{1F4CA} Sample fragment metadata: ${JSON.stringify(allFragments[0].metadata)}`
5017
5109
  );
5018
5110
  const uniqueTypes = new Set(allFragments.map((f) => f.metadata?.type));
5019
- logger9.debug(
5111
+ logger10.debug(
5020
5112
  `[Graph API] \u{1F4CA} Unique metadata types found in knowledge table: ${JSON.stringify(Array.from(uniqueTypes))}`
5021
5113
  );
5022
5114
  const relevantFragments = allFragments.filter((fragment) => {
5023
5115
  const metadata = fragment.metadata;
5024
5116
  const hasDocumentId = metadata?.documentId === documentId;
5025
5117
  if (hasDocumentId) {
5026
- logger9.debug(
5118
+ logger10.debug(
5027
5119
  `[Graph API] \u{1F4CA} Fragment ${fragment.id} metadata: ${JSON.stringify(metadata)}`
5028
5120
  );
5029
5121
  }
5030
5122
  return hasDocumentId;
5031
5123
  });
5032
- logger9.debug(
5124
+ logger10.debug(
5033
5125
  `[Graph API] \u{1F4CA} Fragments with matching documentId: ${relevantFragments.length}`
5034
5126
  );
5035
5127
  }
5036
5128
  const documentFragments = allFragments.filter((fragment) => {
5037
5129
  const metadata = fragment.metadata;
5038
5130
  const typeString = typeof metadata?.type === "string" ? metadata.type : null;
5039
- const isFragment = typeString && typeString.toLowerCase() === "fragment" || metadata?.type === MemoryType4.FRAGMENT || // If no type but has documentId, assume it's a fragment
5131
+ const isFragment = typeString && typeString.toLowerCase() === "fragment" || metadata?.type === MemoryType5.FRAGMENT || // If no type but has documentId, assume it's a fragment
5040
5132
  !metadata?.type && metadata?.documentId;
5041
5133
  return metadata?.documentId === documentId && isFragment;
5042
5134
  });
@@ -5048,7 +5140,7 @@ async function expandDocumentGraphHandler(req, res, runtime) {
5048
5140
  source: documentId,
5049
5141
  target: frag.id
5050
5142
  }));
5051
- logger9.info(`[Graph API] \u{1F4CA} Found ${nodes.length} fragments for document ${documentId}`);
5143
+ logger10.info(`[Graph API] \u{1F4CA} Found ${nodes.length} fragments for document ${documentId}`);
5052
5144
  sendSuccess(res, {
5053
5145
  documentId,
5054
5146
  nodes,
@@ -5056,7 +5148,7 @@ async function expandDocumentGraphHandler(req, res, runtime) {
5056
5148
  fragmentCount: nodes.length
5057
5149
  });
5058
5150
  } catch (error) {
5059
- logger9.error(`[Graph API] \u274C Error expanding document ${documentId}:`, error);
5151
+ logger10.error(`[Graph API] \u274C Error expanding document ${documentId}:`, error);
5060
5152
  sendError(res, 500, "GRAPH_ERROR", "Failed to expand document", error.message);
5061
5153
  }
5062
5154
  }
@@ -5068,7 +5160,7 @@ async function uploadKnowledgeWithMulter(req, res, runtime) {
5068
5160
  );
5069
5161
  uploadArray(req, res, (err) => {
5070
5162
  if (err) {
5071
- logger9.error({ error: err }, "[Document Processor] \u274C File upload error");
5163
+ logger10.error({ error: err }, "[Document Processor] \u274C File upload error");
5072
5164
  return sendError(res, 400, "UPLOAD_ERROR", err.message);
5073
5165
  }
5074
5166
  uploadKnowledgeHandler(req, res, runtime);
@@ -5136,20 +5228,57 @@ var knowledgeRoutes = [
5136
5228
  ];
5137
5229
 
5138
5230
  // src/index.ts
5139
- var knowledgePlugin = {
5140
- name: "knowledge",
5141
- description: "Plugin for Retrieval Augmented Generation, including knowledge management and embedding.",
5142
- services: [KnowledgeService],
5143
- providers: [knowledgeProvider],
5144
- routes: knowledgeRoutes,
5145
- actions: knowledgeActions,
5146
- tests: [tests_default]
5147
- };
5231
+ function createKnowledgePlugin(config = {}) {
5232
+ const { enableUI = true, enableRoutes = true, enableActions = true, enableTests = true } = config;
5233
+ const plugin = {
5234
+ name: "knowledge",
5235
+ description: "Plugin for Retrieval Augmented Generation, including knowledge management and embedding.",
5236
+ services: [KnowledgeService],
5237
+ providers: [knowledgeProvider, documentsProvider]
5238
+ };
5239
+ if (enableUI || enableRoutes) {
5240
+ plugin.routes = knowledgeRoutes;
5241
+ logger11.debug("[Knowledge Plugin] Routes enabled");
5242
+ } else {
5243
+ logger11.info("[Knowledge Plugin] Running in headless mode (no routes or UI)");
5244
+ }
5245
+ if (enableActions) {
5246
+ plugin.actions = knowledgeActions;
5247
+ }
5248
+ if (enableTests) {
5249
+ plugin.tests = [tests_default];
5250
+ }
5251
+ return plugin;
5252
+ }
5253
+ var knowledgePluginCore = createKnowledgePlugin({
5254
+ enableUI: false,
5255
+ enableRoutes: false,
5256
+ enableActions: false,
5257
+ enableTests: false
5258
+ });
5259
+ var knowledgePluginHeadless = createKnowledgePlugin({
5260
+ enableUI: false,
5261
+ enableRoutes: false,
5262
+ enableActions: true,
5263
+ enableTests: false
5264
+ });
5265
+ var knowledgePlugin = createKnowledgePlugin({
5266
+ enableUI: true,
5267
+ enableRoutes: true,
5268
+ enableActions: true,
5269
+ enableTests: true
5270
+ });
5148
5271
  var index_default = knowledgePlugin;
5149
5272
  export {
5273
+ KnowledgeService,
5150
5274
  KnowledgeServiceType,
5151
5275
  ModelConfigSchema,
5276
+ createKnowledgePlugin,
5152
5277
  index_default as default,
5153
- knowledgePlugin
5278
+ documentsProvider,
5279
+ knowledgePlugin,
5280
+ knowledgePluginCore,
5281
+ knowledgePluginHeadless,
5282
+ knowledgeProvider
5154
5283
  };
5155
5284
  //# sourceMappingURL=index.js.map