@elizaos/plugin-knowledge 1.5.10 → 1.5.12

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 logger10 } from "@elizaos/core";
3
+
1
4
  // src/service.ts
2
5
  import {
3
6
  createUniqueUuid,
@@ -2898,10 +2901,7 @@ var KnowledgeService = class _KnowledgeService extends Service {
2898
2901
  const chunks = await splitChunks2(text, targetTokens, overlap);
2899
2902
  return chunks.map((chunk, index) => {
2900
2903
  const fragmentIdContent = `${document.id}-fragment-${index}-${Date.now()}`;
2901
- const fragmentId = createUniqueUuid(
2902
- this.runtime,
2903
- fragmentIdContent
2904
- );
2904
+ const fragmentId = createUniqueUuid(this.runtime, fragmentIdContent);
2905
2905
  return {
2906
2906
  id: fragmentId,
2907
2907
  entityId: scope.entityId,
@@ -2936,10 +2936,20 @@ var KnowledgeService = class _KnowledgeService extends Service {
2936
2936
  async getMemories(params) {
2937
2937
  return this.runtime.getMemories({
2938
2938
  ...params,
2939
- // includes tableName, roomId, count, end
2939
+ // includes tableName, roomId, count, offset, end
2940
2940
  agentId: this.runtime.agentId
2941
2941
  });
2942
2942
  }
2943
+ /**
2944
+ * Counts memories for pagination.
2945
+ * Corresponds to counting documents or fragments.
2946
+ */
2947
+ async countMemories(params) {
2948
+ const roomId = params.roomId || this.runtime.agentId;
2949
+ const unique = params.unique ?? false;
2950
+ const tableName = params.tableName;
2951
+ return this.runtime.countMemories(roomId, unique, tableName);
2952
+ }
2943
2953
  /**
2944
2954
  * Deletes a specific memory item (knowledge document) by its ID.
2945
2955
  * Corresponds to DELETE /plugins/knowledge/documents/:knowledgeId
@@ -4150,7 +4160,7 @@ ${formattedResults}`
4150
4160
  var knowledgeActions = [processKnowledgeAction, searchKnowledgeAction];
4151
4161
 
4152
4162
  // src/routes.ts
4153
- import { createUniqueUuid as createUniqueUuid2, logger as logger9, ModelType as ModelType4 } from "@elizaos/core";
4163
+ import { MemoryType as MemoryType4, createUniqueUuid as createUniqueUuid2, logger as logger9, ModelType as ModelType4 } from "@elizaos/core";
4154
4164
  import fs4 from "fs";
4155
4165
  import path4 from "path";
4156
4166
  import multer from "multer";
@@ -4687,8 +4697,8 @@ async function getKnowledgeChunksHandler(req, res, runtime) {
4687
4697
  if (documentId) {
4688
4698
  const allFragments = await service.getMemories({
4689
4699
  tableName: "knowledge",
4690
- count: 1e5
4691
- // Very high limit to get all fragments
4700
+ count: 5e4
4701
+ // Reduced from 100000 - still high enough for large documents
4692
4702
  });
4693
4703
  const documentFragments = allFragments.filter((fragment) => {
4694
4704
  const metadata = fragment.metadata;
@@ -4801,6 +4811,258 @@ async function searchKnowledgeHandler(req, res, runtime) {
4801
4811
  sendError(res, 500, "SEARCH_ERROR", "Failed to search knowledge", error.message);
4802
4812
  }
4803
4813
  }
4814
+ async function getGraphNodesHandler(req, res, runtime) {
4815
+ const service = runtime.getService(KnowledgeService.serviceType);
4816
+ if (!service) {
4817
+ return sendError(res, 500, "SERVICE_NOT_FOUND", "KnowledgeService not found");
4818
+ }
4819
+ try {
4820
+ const parsedPage = req.query.page ? Number.parseInt(req.query.page, 10) : 1;
4821
+ const parsedLimit = req.query.limit ? Number.parseInt(req.query.limit, 10) : 20;
4822
+ const type = req.query.type;
4823
+ const agentId = req.query.agentId || runtime.agentId;
4824
+ const page = Number.isNaN(parsedPage) || parsedPage < 1 ? 1 : parsedPage;
4825
+ const limit = Number.isNaN(parsedLimit) || parsedLimit < 1 ? 20 : Math.min(parsedLimit, 50);
4826
+ const offset = (page - 1) * limit;
4827
+ logger9.debug(
4828
+ `[Graph API] \u{1F4CA} Fetching graph nodes: page=${page}, limit=${limit}, type=${type || "all"}, agent=${agentId}`
4829
+ );
4830
+ const totalDocuments = await service.countMemories({
4831
+ tableName: "documents",
4832
+ roomId: agentId,
4833
+ unique: false
4834
+ // Count all documents, not just unique ones
4835
+ });
4836
+ const totalPages = Math.ceil(totalDocuments / limit);
4837
+ const hasMore = page < totalPages;
4838
+ const paginatedDocuments = await service.getMemories({
4839
+ tableName: "documents",
4840
+ roomId: agentId,
4841
+ count: limit,
4842
+ offset
4843
+ });
4844
+ const nodes = [];
4845
+ const links = [];
4846
+ paginatedDocuments.forEach((doc) => {
4847
+ if (!doc.id) {
4848
+ logger9.warn(`[Graph API] \u26A0\uFE0F Skipping document without ID`);
4849
+ return;
4850
+ }
4851
+ nodes.push({ id: doc.id, type: "document" });
4852
+ });
4853
+ if (type !== "document") {
4854
+ const allFragments = await service.getMemories({
4855
+ tableName: "knowledge",
4856
+ roomId: agentId,
4857
+ count: 5e4
4858
+ // Reduced from 100000 - still high enough for large documents
4859
+ });
4860
+ logger9.debug(`[Graph API] \u{1F4CA} Total fragments found: ${allFragments.length}`);
4861
+ if (allFragments.length > 0) {
4862
+ logger9.debug(
4863
+ `[Graph API] \u{1F4CA} Sample fragment metadata: ${JSON.stringify(
4864
+ allFragments.slice(0, 3).map((f) => ({
4865
+ id: f.id,
4866
+ metadata: f.metadata
4867
+ }))
4868
+ )}`
4869
+ );
4870
+ }
4871
+ paginatedDocuments.forEach((doc) => {
4872
+ if (!doc.id) {
4873
+ return;
4874
+ }
4875
+ const docFragments = allFragments.filter((fragment) => {
4876
+ const metadata = fragment.metadata;
4877
+ const typeString = typeof metadata?.type === "string" ? metadata.type : null;
4878
+ const isFragment = typeString && typeString.toLowerCase() === "fragment" || metadata?.type === MemoryType4.FRAGMENT || // If no type but has documentId, assume it's a fragment
4879
+ !metadata?.type && metadata?.documentId;
4880
+ return metadata?.documentId === doc.id && isFragment;
4881
+ });
4882
+ if (docFragments.length > 0) {
4883
+ logger9.debug(`[Graph API] \u{1F4CA} Document ${doc.id} has ${docFragments.length} fragments`);
4884
+ }
4885
+ docFragments.forEach((frag) => {
4886
+ const docId = doc.id;
4887
+ if (!frag.id || !docId) {
4888
+ logger9.warn(
4889
+ `[Graph API] \u26A0\uFE0F Skipping fragment without ID for document ${docId || "unknown"}`
4890
+ );
4891
+ return;
4892
+ }
4893
+ nodes.push({ id: frag.id, type: "fragment" });
4894
+ links.push({ source: docId, target: frag.id });
4895
+ });
4896
+ });
4897
+ logger9.info(
4898
+ `[Graph API] \u{1F4CA} Final graph: ${nodes.length} nodes (${paginatedDocuments.length} documents), ${links.length} links`
4899
+ );
4900
+ }
4901
+ sendSuccess(res, {
4902
+ nodes,
4903
+ links,
4904
+ pagination: {
4905
+ currentPage: page,
4906
+ totalPages,
4907
+ hasMore,
4908
+ totalDocuments
4909
+ }
4910
+ });
4911
+ } catch (error) {
4912
+ logger9.error("[Graph API] \u274C Error fetching graph nodes:", error);
4913
+ sendError(res, 500, "GRAPH_ERROR", "Failed to fetch graph nodes", error.message);
4914
+ }
4915
+ }
4916
+ async function getGraphNodeDetailsHandler(req, res, runtime) {
4917
+ const service = runtime.getService(KnowledgeService.serviceType);
4918
+ if (!service) {
4919
+ return sendError(res, 500, "SERVICE_NOT_FOUND", "KnowledgeService not found");
4920
+ }
4921
+ const nodeId = req.params.nodeId;
4922
+ const agentId = req.query.agentId || runtime.agentId;
4923
+ if (!nodeId || nodeId.length < 36) {
4924
+ return sendError(res, 400, "INVALID_ID", "Invalid node ID format");
4925
+ }
4926
+ try {
4927
+ logger9.info(`[Graph API] \u{1F4CA} Fetching node details for: ${nodeId}, agent: ${agentId}`);
4928
+ const allDocuments = await service.getMemories({
4929
+ tableName: "documents",
4930
+ count: 1e4
4931
+ });
4932
+ logger9.debug(`[Graph API] \u{1F4CA} Total documents in DB: ${allDocuments.length}`);
4933
+ let document = allDocuments.find((doc) => doc.id === nodeId && doc.roomId === agentId);
4934
+ if (!document) {
4935
+ logger9.debug(`[Graph API] \u{1F4CA} Document not found with roomId filter, trying without filter`);
4936
+ document = allDocuments.find((doc) => doc.id === nodeId);
4937
+ if (document) {
4938
+ logger9.warn(
4939
+ `[Graph API] \u26A0\uFE0F Document ${nodeId} found but has different roomId: ${document.roomId} vs ${agentId}`
4940
+ );
4941
+ }
4942
+ }
4943
+ if (document) {
4944
+ logger9.info(`[Graph API] \u2705 Found document: ${nodeId}`);
4945
+ sendSuccess(res, {
4946
+ id: document.id,
4947
+ type: "document",
4948
+ content: document.content,
4949
+ metadata: document.metadata,
4950
+ createdAt: document.createdAt,
4951
+ entityId: document.entityId,
4952
+ roomId: document.roomId,
4953
+ agentId: document.agentId,
4954
+ worldId: document.worldId
4955
+ });
4956
+ return;
4957
+ }
4958
+ logger9.debug(`[Graph API] \u{1F4CA} Document not found, searching in fragments`);
4959
+ const allFragments = await service.getMemories({
4960
+ tableName: "knowledge",
4961
+ count: 5e4
4962
+ // Reduced from 100000 - still high enough for large documents
4963
+ });
4964
+ logger9.debug(`[Graph API] \u{1F4CA} Total fragments in DB: ${allFragments.length}`);
4965
+ let fragment = allFragments.find((frag) => frag.id === nodeId && frag.roomId === agentId);
4966
+ if (!fragment) {
4967
+ logger9.debug(`[Graph API] \u{1F4CA} Fragment not found with roomId filter, trying without filter`);
4968
+ fragment = allFragments.find((frag) => frag.id === nodeId);
4969
+ if (fragment) {
4970
+ logger9.warn(
4971
+ `[Graph API] \u26A0\uFE0F Fragment ${nodeId} found but has different roomId: ${fragment.roomId} vs ${agentId}`
4972
+ );
4973
+ }
4974
+ }
4975
+ if (fragment) {
4976
+ logger9.info(`[Graph API] \u2705 Found fragment: ${nodeId}`);
4977
+ sendSuccess(res, {
4978
+ id: fragment.id,
4979
+ type: "fragment",
4980
+ content: fragment.content,
4981
+ metadata: fragment.metadata,
4982
+ createdAt: fragment.createdAt,
4983
+ entityId: fragment.entityId,
4984
+ roomId: fragment.roomId,
4985
+ agentId: fragment.agentId,
4986
+ worldId: fragment.worldId
4987
+ });
4988
+ return;
4989
+ }
4990
+ logger9.error(`[Graph API] \u274C Node ${nodeId} not found in documents or fragments`);
4991
+ sendError(res, 404, "NOT_FOUND", `Node with ID ${nodeId} not found`);
4992
+ } catch (error) {
4993
+ logger9.error(`[Graph API] \u274C Error fetching node details for ${nodeId}:`, error);
4994
+ sendError(res, 500, "GRAPH_ERROR", "Failed to fetch node details", error.message);
4995
+ }
4996
+ }
4997
+ async function expandDocumentGraphHandler(req, res, runtime) {
4998
+ const service = runtime.getService(KnowledgeService.serviceType);
4999
+ if (!service) {
5000
+ return sendError(res, 500, "SERVICE_NOT_FOUND", "KnowledgeService not found");
5001
+ }
5002
+ const documentId = req.params.documentId;
5003
+ const agentId = req.query.agentId || runtime.agentId;
5004
+ if (!documentId || documentId.length < 36) {
5005
+ return sendError(res, 400, "INVALID_ID", "Invalid document ID format");
5006
+ }
5007
+ try {
5008
+ logger9.debug(`[Graph API] \u{1F4CA} Expanding document: ${documentId}, agent: ${agentId}`);
5009
+ const allFragments = await service.getMemories({
5010
+ tableName: "knowledge",
5011
+ roomId: agentId,
5012
+ // Filter by agent
5013
+ count: 5e4
5014
+ // Reduced from 100000 - still high enough for large documents
5015
+ });
5016
+ logger9.debug(`[Graph API] \u{1F4CA} Total fragments in knowledge table: ${allFragments.length}`);
5017
+ if (allFragments.length > 0 && process.env.NODE_ENV !== "production") {
5018
+ logger9.debug(
5019
+ `[Graph API] \u{1F4CA} Sample fragment metadata: ${JSON.stringify(allFragments[0].metadata)}`
5020
+ );
5021
+ const uniqueTypes = new Set(allFragments.map((f) => f.metadata?.type));
5022
+ logger9.debug(
5023
+ `[Graph API] \u{1F4CA} Unique metadata types found in knowledge table: ${JSON.stringify(Array.from(uniqueTypes))}`
5024
+ );
5025
+ const relevantFragments = allFragments.filter((fragment) => {
5026
+ const metadata = fragment.metadata;
5027
+ const hasDocumentId = metadata?.documentId === documentId;
5028
+ if (hasDocumentId) {
5029
+ logger9.debug(
5030
+ `[Graph API] \u{1F4CA} Fragment ${fragment.id} metadata: ${JSON.stringify(metadata)}`
5031
+ );
5032
+ }
5033
+ return hasDocumentId;
5034
+ });
5035
+ logger9.debug(
5036
+ `[Graph API] \u{1F4CA} Fragments with matching documentId: ${relevantFragments.length}`
5037
+ );
5038
+ }
5039
+ const documentFragments = allFragments.filter((fragment) => {
5040
+ const metadata = fragment.metadata;
5041
+ const typeString = typeof metadata?.type === "string" ? metadata.type : null;
5042
+ const isFragment = typeString && typeString.toLowerCase() === "fragment" || metadata?.type === MemoryType4.FRAGMENT || // If no type but has documentId, assume it's a fragment
5043
+ !metadata?.type && metadata?.documentId;
5044
+ return metadata?.documentId === documentId && isFragment;
5045
+ });
5046
+ const nodes = documentFragments.filter((frag) => frag.id !== void 0).map((frag) => ({
5047
+ id: frag.id,
5048
+ type: "fragment"
5049
+ }));
5050
+ const links = documentFragments.filter((frag) => frag.id !== void 0).map((frag) => ({
5051
+ source: documentId,
5052
+ target: frag.id
5053
+ }));
5054
+ logger9.info(`[Graph API] \u{1F4CA} Found ${nodes.length} fragments for document ${documentId}`);
5055
+ sendSuccess(res, {
5056
+ documentId,
5057
+ nodes,
5058
+ links,
5059
+ fragmentCount: nodes.length
5060
+ });
5061
+ } catch (error) {
5062
+ logger9.error(`[Graph API] \u274C Error expanding document ${documentId}:`, error);
5063
+ sendError(res, 500, "GRAPH_ERROR", "Failed to expand document", error.message);
5064
+ }
5065
+ }
4804
5066
  async function uploadKnowledgeWithMulter(req, res, runtime) {
4805
5067
  const upload = createUploadMiddleware(runtime);
4806
5068
  const uploadArray = upload.array(
@@ -4857,24 +5119,76 @@ var knowledgeRoutes = [
4857
5119
  type: "GET",
4858
5120
  path: "/search",
4859
5121
  handler: searchKnowledgeHandler
5122
+ },
5123
+ // New graph routes
5124
+ {
5125
+ type: "GET",
5126
+ path: "/graph/nodes",
5127
+ handler: getGraphNodesHandler
5128
+ },
5129
+ {
5130
+ type: "GET",
5131
+ path: "/graph/node/:nodeId",
5132
+ handler: getGraphNodeDetailsHandler
5133
+ },
5134
+ {
5135
+ type: "GET",
5136
+ path: "/graph/expand/:documentId",
5137
+ handler: expandDocumentGraphHandler
4860
5138
  }
4861
5139
  ];
4862
5140
 
4863
5141
  // src/index.ts
4864
- var knowledgePlugin = {
4865
- name: "knowledge",
4866
- description: "Plugin for Retrieval Augmented Generation, including knowledge management and embedding.",
4867
- services: [KnowledgeService],
4868
- providers: [knowledgeProvider],
4869
- routes: knowledgeRoutes,
4870
- actions: knowledgeActions,
4871
- tests: [tests_default]
4872
- };
5142
+ function createKnowledgePlugin(config = {}) {
5143
+ const { enableUI = true, enableRoutes = true, enableActions = true, enableTests = true } = config;
5144
+ const plugin = {
5145
+ name: "knowledge",
5146
+ description: "Plugin for Retrieval Augmented Generation, including knowledge management and embedding.",
5147
+ services: [KnowledgeService],
5148
+ providers: [knowledgeProvider]
5149
+ };
5150
+ if (enableUI || enableRoutes) {
5151
+ plugin.routes = knowledgeRoutes;
5152
+ logger10.debug("[Knowledge Plugin] Routes enabled");
5153
+ } else {
5154
+ logger10.info("[Knowledge Plugin] Running in headless mode (no routes or UI)");
5155
+ }
5156
+ if (enableActions) {
5157
+ plugin.actions = knowledgeActions;
5158
+ }
5159
+ if (enableTests) {
5160
+ plugin.tests = [tests_default];
5161
+ }
5162
+ return plugin;
5163
+ }
5164
+ var knowledgePluginCore = createKnowledgePlugin({
5165
+ enableUI: false,
5166
+ enableRoutes: false,
5167
+ enableActions: false,
5168
+ enableTests: false
5169
+ });
5170
+ var knowledgePluginHeadless = createKnowledgePlugin({
5171
+ enableUI: false,
5172
+ enableRoutes: false,
5173
+ enableActions: true,
5174
+ enableTests: false
5175
+ });
5176
+ var knowledgePlugin = createKnowledgePlugin({
5177
+ enableUI: true,
5178
+ enableRoutes: true,
5179
+ enableActions: true,
5180
+ enableTests: true
5181
+ });
4873
5182
  var index_default = knowledgePlugin;
4874
5183
  export {
5184
+ KnowledgeService,
4875
5185
  KnowledgeServiceType,
4876
5186
  ModelConfigSchema,
5187
+ createKnowledgePlugin,
4877
5188
  index_default as default,
4878
- knowledgePlugin
5189
+ knowledgePlugin,
5190
+ knowledgePluginCore,
5191
+ knowledgePluginHeadless,
5192
+ knowledgeProvider
4879
5193
  };
4880
5194
  //# sourceMappingURL=index.js.map