@elizaos/plugin-knowledge 1.5.11 → 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/README.md CHANGED
@@ -2,6 +2,67 @@
2
2
 
3
3
  Give your AI agent the ability to learn from documents and answer questions based on that knowledge. Works out of the box with zero configuration!
4
4
 
5
+ ## 📦 Installation Modes
6
+
7
+ The Knowledge plugin supports multiple deployment modes to fit your use case:
8
+
9
+ ### **Full Mode** (Default - With UI & Routes)
10
+
11
+ Perfect for standard deployments with the full web interface:
12
+
13
+ ```typescript
14
+ import { knowledgePlugin } from '@elizaos/plugin-knowledge';
15
+ // or
16
+ import knowledgePlugin from '@elizaos/plugin-knowledge';
17
+
18
+ export const character = {
19
+ plugins: [knowledgePlugin],
20
+ };
21
+ ```
22
+
23
+ ### **Headless Mode** (Service + Provider + Actions, No UI)
24
+
25
+ For server deployments without frontend:
26
+
27
+ ```typescript
28
+ import { knowledgePluginHeadless } from '@elizaos/plugin-knowledge';
29
+
30
+ export const character = {
31
+ plugins: [knowledgePluginHeadless],
32
+ };
33
+ ```
34
+
35
+ ### **Core Mode** (Service + Provider Only)
36
+
37
+ For cloud runtimes or minimal deployments (no routes, no UI, no actions):
38
+
39
+ ```typescript
40
+ import { knowledgePluginCore } from '@elizaos/plugin-knowledge';
41
+
42
+ export const character = {
43
+ plugins: [knowledgePluginCore],
44
+ };
45
+ ```
46
+
47
+ ### **Custom Configuration**
48
+
49
+ Create your own configuration:
50
+
51
+ ```typescript
52
+ import { createKnowledgePlugin } from '@elizaos/plugin-knowledge';
53
+
54
+ const customPlugin = createKnowledgePlugin({
55
+ enableUI: false, // Disable frontend UI
56
+ enableRoutes: false, // Disable HTTP routes
57
+ enableActions: true, // Keep actions enabled
58
+ enableTests: false, // Disable tests
59
+ });
60
+
61
+ export const character = {
62
+ plugins: [customPlugin],
63
+ };
64
+ ```
65
+
5
66
  ## 🚀 Getting Started (Beginner-Friendly)
6
67
 
7
68
  ### Step 1: Add the Plugin
@@ -14,7 +75,7 @@ export const character = {
14
75
  name: 'MyAgent',
15
76
  plugins: [
16
77
  '@elizaos/plugin-openai', // ← Make sure you have this
17
- '@elizaos/plugin-knowledge', // ← Add this line
78
+ '@elizaos/plugin-knowledge', // ← Add this line (full mode)
18
79
  // ... your other plugins
19
80
  ],
20
81
  // ... rest of your character config
@@ -192,28 +253,68 @@ MAX_OUTPUT_TOKENS=4096 # Response size limit
192
253
  ```typescript
193
254
  import { KnowledgeService } from '@elizaos/plugin-knowledge';
194
255
 
256
+ // Get the service from runtime
257
+ const knowledgeService = runtime.getService<KnowledgeService>(KnowledgeService.serviceType);
258
+
195
259
  // Add knowledge programmatically
196
260
  const result = await knowledgeService.addKnowledge({
197
- clientDocumentId: 'unique-doc-id',
261
+ agentId: runtime.agentId,
262
+ clientDocumentId: '' as UUID, // Auto-generated based on content
198
263
  content: documentContent, // Base64 for PDFs, plain text for others
199
264
  contentType: 'application/pdf',
200
265
  originalFilename: 'document.pdf',
201
- worldId: runtime.worldId,
202
- roomId: runtime.roomId,
203
- entityId: runtime.entityId,
266
+ worldId: runtime.agentId,
267
+ roomId: runtime.agentId,
268
+ entityId: runtime.agentId,
204
269
  metadata: {
205
- // Optional
270
+ // Optional custom metadata
206
271
  source: 'upload',
207
272
  author: 'John Doe',
208
273
  },
209
274
  });
210
275
 
211
- // Search knowledge
212
- const searchResults = await knowledgeService.searchKnowledge({
213
- query: 'quantum computing',
276
+ // The provider automatically retrieves relevant knowledge during conversations
277
+ // But you can also search directly:
278
+ const knowledgeItems = await knowledgeService.getKnowledge(
279
+ message, // The message/query
280
+ {
281
+ roomId: runtime.agentId,
282
+ worldId: runtime.agentId,
283
+ entityId: runtime.agentId,
284
+ }
285
+ );
286
+ ```
287
+
288
+ ### Cloud/Custom Runtime Usage
289
+
290
+ For cloud deployments or custom runtimes, use the core mode and access the service directly:
291
+
292
+ ```typescript
293
+ import { knowledgePluginCore, KnowledgeService } from '@elizaos/plugin-knowledge';
294
+
295
+ // In your cloud runtime setup
296
+ const runtime = await createRuntime({
297
+ // ... your runtime config
298
+ plugins: [knowledgePluginCore], // Core mode: no routes, no UI
299
+ });
300
+
301
+ // Access the service
302
+ const knowledgeService = runtime.getService<KnowledgeService>(KnowledgeService.serviceType);
303
+
304
+ // Add documents
305
+ await knowledgeService.addKnowledge({
214
306
  agentId: runtime.agentId,
215
- limit: 10,
307
+ clientDocumentId: '' as UUID,
308
+ content: base64Content,
309
+ contentType: 'application/pdf',
310
+ originalFilename: 'company-docs.pdf',
311
+ worldId: runtime.agentId,
312
+ roomId: runtime.agentId,
313
+ entityId: runtime.agentId,
216
314
  });
315
+
316
+ // The knowledge provider will automatically inject relevant context
317
+ // into the agent's conversations based on the query
217
318
  ```
218
319
 
219
320
  </details>
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { UUID, Plugin } from '@elizaos/core';
1
+ import { UUID, Service, Metadata, IAgentRuntime, Memory, KnowledgeItem, Provider, Plugin } from '@elizaos/core';
2
2
  import z from 'zod';
3
3
 
4
4
  declare const ModelConfigSchema: z.ZodObject<{
@@ -179,6 +179,146 @@ interface ExtendedMemoryMetadata extends Record<string, any> {
179
179
  url?: string;
180
180
  }
181
181
 
182
+ /**
183
+ * Knowledge Service - Provides retrieval augmented generation capabilities
184
+ */
185
+ declare class KnowledgeService extends Service {
186
+ static readonly serviceType = "knowledge";
187
+ config: Metadata;
188
+ private knowledgeConfig;
189
+ capabilityDescription: string;
190
+ private knowledgeProcessingSemaphore;
191
+ /**
192
+ * Create a new Knowledge service
193
+ * @param runtime Agent runtime
194
+ */
195
+ constructor(runtime: IAgentRuntime, config?: Partial<KnowledgeConfig>);
196
+ private loadInitialDocuments;
197
+ /**
198
+ * Start the Knowledge service
199
+ * @param runtime Agent runtime
200
+ * @returns Initialized Knowledge service
201
+ */
202
+ static start(runtime: IAgentRuntime): Promise<KnowledgeService>;
203
+ /**
204
+ * Stop the Knowledge service
205
+ * @param runtime Agent runtime
206
+ */
207
+ static stop(runtime: IAgentRuntime): Promise<void>;
208
+ /**
209
+ * Stop the service
210
+ */
211
+ stop(): Promise<void>;
212
+ /**
213
+ * Add knowledge to the system
214
+ * @param options Knowledge options
215
+ * @returns Promise with document processing result
216
+ */
217
+ addKnowledge(options: AddKnowledgeOptions): Promise<{
218
+ clientDocumentId: string;
219
+ storedDocumentMemoryId: UUID;
220
+ fragmentCount: number;
221
+ }>;
222
+ /**
223
+ * Process a document regardless of type - Called by public addKnowledge
224
+ * @param options Document options
225
+ * @returns Promise with document processing result
226
+ */
227
+ private processDocument;
228
+ private handleProcessingError;
229
+ checkExistingKnowledge(knowledgeId: UUID): Promise<boolean>;
230
+ getKnowledge(message: Memory, scope?: {
231
+ roomId?: UUID;
232
+ worldId?: UUID;
233
+ entityId?: UUID;
234
+ }): Promise<KnowledgeItem[]>;
235
+ /**
236
+ * Enrich a conversation memory with RAG metadata
237
+ * This can be called after response generation to add RAG tracking data
238
+ * @param memoryId The ID of the conversation memory to enrich
239
+ * @param ragMetadata The RAG metadata to add
240
+ */
241
+ enrichConversationMemoryWithRAG(memoryId: UUID, ragMetadata: {
242
+ retrievedFragments: Array<{
243
+ fragmentId: UUID;
244
+ documentTitle: string;
245
+ similarityScore?: number;
246
+ contentPreview: string;
247
+ }>;
248
+ queryText: string;
249
+ totalFragments: number;
250
+ retrievalTimestamp: number;
251
+ }): Promise<void>;
252
+ /**
253
+ * Set the current response memory ID for RAG tracking
254
+ * This is called by the knowledge provider to track which response memory to enrich
255
+ */
256
+ private pendingRAGEnrichment;
257
+ /**
258
+ * Store RAG metadata for the next conversation memory that gets created
259
+ * @param ragMetadata The RAG metadata to associate with the next memory
260
+ */
261
+ setPendingRAGMetadata(ragMetadata: any): void;
262
+ /**
263
+ * Try to enrich recent conversation memories with pending RAG metadata
264
+ * This is called periodically to catch memories that were created after RAG retrieval
265
+ */
266
+ enrichRecentMemoriesWithPendingRAG(): Promise<void>;
267
+ processCharacterKnowledge(items: string[]): Promise<void>;
268
+ _internalAddKnowledge(item: KnowledgeItem, // item.id here is expected to be the ID of the "document"
269
+ options?: {
270
+ targetTokens: number;
271
+ overlap: number;
272
+ modelContextSize: number;
273
+ }, scope?: {
274
+ roomId: `${string}-${string}-${string}-${string}-${string}`;
275
+ entityId: `${string}-${string}-${string}-${string}-${string}`;
276
+ worldId: `${string}-${string}-${string}-${string}-${string}`;
277
+ }): Promise<void>;
278
+ private processDocumentFragment;
279
+ private splitAndCreateFragments;
280
+ /**
281
+ * Retrieves memories, typically documents, for the agent.
282
+ * Corresponds to GET /plugins/knowledge/documents
283
+ */
284
+ getMemories(params: {
285
+ tableName: string;
286
+ roomId?: UUID;
287
+ count?: number;
288
+ offset?: number;
289
+ end?: number;
290
+ }): Promise<Memory[]>;
291
+ /**
292
+ * Counts memories for pagination.
293
+ * Corresponds to counting documents or fragments.
294
+ */
295
+ countMemories(params: {
296
+ tableName: string;
297
+ roomId?: UUID;
298
+ unique?: boolean;
299
+ }): Promise<number>;
300
+ /**
301
+ * Deletes a specific memory item (knowledge document) by its ID.
302
+ * Corresponds to DELETE /plugins/knowledge/documents/:knowledgeId
303
+ * Assumes the memoryId corresponds to an item in the 'documents' table or that
304
+ * runtime.deleteMemory can correctly identify it.
305
+ */
306
+ deleteMemory(memoryId: UUID): Promise<void>;
307
+ }
308
+
309
+ /**
310
+ * Represents a knowledge provider that retrieves knowledge from the knowledge base.
311
+ * @type {Provider}
312
+ * @property {string} name - The name of the knowledge provider.
313
+ * @property {string} description - The description of the knowledge provider.
314
+ * @property {boolean} dynamic - Indicates if the knowledge provider is dynamic or static.
315
+ * @property {Function} get - Asynchronously retrieves knowledge from the knowledge base.
316
+ * @param {IAgentRuntime} runtime - The agent runtime object.
317
+ * @param {Memory} message - The message containing the query for knowledge retrieval.
318
+ * @returns {Object} An object containing the retrieved knowledge data, values, and text.
319
+ */
320
+ declare const knowledgeProvider: Provider;
321
+
182
322
  /**
183
323
  * Knowledge Plugin - Main Entry Point
184
324
  *
@@ -186,8 +326,69 @@ interface ExtendedMemoryMetadata extends Record<string, any> {
186
326
  */
187
327
 
188
328
  /**
189
- * Knowledge Plugin - Provides Retrieval Augmented Generation capabilities
329
+ * Configuration options for the Knowledge Plugin
330
+ */
331
+ interface KnowledgePluginConfig {
332
+ /**
333
+ * Enable frontend UI and routes
334
+ * Set to false for cloud/server-only deployments
335
+ * @default true
336
+ */
337
+ enableUI?: boolean;
338
+ /**
339
+ * Enable HTTP routes for document management
340
+ * Set to false for browser-only or minimal deployments
341
+ * @default true
342
+ */
343
+ enableRoutes?: boolean;
344
+ /**
345
+ * Enable actions (PROCESS_KNOWLEDGE, SEARCH_KNOWLEDGE)
346
+ * @default true
347
+ */
348
+ enableActions?: boolean;
349
+ /**
350
+ * Enable tests
351
+ * @default true
352
+ */
353
+ enableTests?: boolean;
354
+ }
355
+ /**
356
+ * Create a Knowledge Plugin with custom configuration
357
+ * @param config Plugin configuration options
358
+ * @returns Configured Plugin instance
359
+ *
360
+ * @example
361
+ * // Cloud runtime mode (service + provider only)
362
+ * const plugin = createKnowledgePlugin({
363
+ * enableUI: false,
364
+ * enableRoutes: false,
365
+ * });
366
+ *
367
+ * @example
368
+ * // Browser-only mode (no routes)
369
+ * const plugin = createKnowledgePlugin({
370
+ * enableRoutes: false,
371
+ * });
372
+ *
373
+ * @example
374
+ * // Full mode (default)
375
+ * const plugin = createKnowledgePlugin();
376
+ */
377
+ declare function createKnowledgePlugin(config?: KnowledgePluginConfig): Plugin;
378
+ /**
379
+ * Knowledge Plugin - Core mode (Service + Provider only)
380
+ * Use this for cloud runtimes or minimal deployments
381
+ */
382
+ declare const knowledgePluginCore: Plugin;
383
+ /**
384
+ * Knowledge Plugin - Headless mode (Service + Provider + Actions, no UI)
385
+ * Use this for server deployments without frontend
386
+ */
387
+ declare const knowledgePluginHeadless: Plugin;
388
+ /**
389
+ * Knowledge Plugin - Full mode (default)
390
+ * Includes everything: Service, Provider, Actions, Routes, UI, Tests
190
391
  */
191
392
  declare const knowledgePlugin: Plugin;
192
393
 
193
- export { type AddKnowledgeOptions, type ExtendedMemoryMetadata, type KnowledgeConfig, type KnowledgeDocumentMetadata, KnowledgeServiceType, type LoadResult, type ModelConfig, ModelConfigSchema, type ProviderRateLimits, type TextGenerationOptions, knowledgePlugin as default, knowledgePlugin };
394
+ export { type AddKnowledgeOptions, type ExtendedMemoryMetadata, type KnowledgeConfig, type KnowledgeDocumentMetadata, type KnowledgePluginConfig, KnowledgeService, KnowledgeServiceType, type LoadResult, type ModelConfig, ModelConfigSchema, type ProviderRateLimits, type TextGenerationOptions, createKnowledgePlugin, knowledgePlugin as default, knowledgePlugin, knowledgePluginCore, knowledgePluginHeadless, knowledgeProvider };
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,
@@ -5136,20 +5139,56 @@ var knowledgeRoutes = [
5136
5139
  ];
5137
5140
 
5138
5141
  // 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
- };
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
+ });
5148
5182
  var index_default = knowledgePlugin;
5149
5183
  export {
5184
+ KnowledgeService,
5150
5185
  KnowledgeServiceType,
5151
5186
  ModelConfigSchema,
5187
+ createKnowledgePlugin,
5152
5188
  index_default as default,
5153
- knowledgePlugin
5189
+ knowledgePlugin,
5190
+ knowledgePluginCore,
5191
+ knowledgePluginHeadless,
5192
+ knowledgeProvider
5154
5193
  };
5155
5194
  //# sourceMappingURL=index.js.map