-
Notifications
You must be signed in to change notification settings - Fork 9
feat: Add static provider for listing available documents #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import type { IAgentRuntime, Memory, Provider } from '@elizaos/core'; | ||
| import { addHeader, logger, MemoryType } from '@elizaos/core'; | ||
| import { KnowledgeService } from './service.ts'; | ||
|
|
||
| /** | ||
| * Represents a static provider that lists available documents in the knowledge base. | ||
| * This provider helps the agent understand which documents are available for retrieval. | ||
| * @type {Provider} | ||
| * @property {string} name - The name of the documents provider. | ||
| * @property {string} description - The description of the documents provider. | ||
| * @property {boolean} dynamic - Indicates if the provider is static (false). | ||
| * @property {Function} get - Asynchronously retrieves the list of available documents. | ||
| * @param {IAgentRuntime} runtime - The agent runtime object. | ||
| * @returns {Object} An object containing the available documents list. | ||
| */ | ||
| export const documentsProvider: Provider = { | ||
| name: 'AVAILABLE_DOCUMENTS', | ||
| description: | ||
| 'List of documents available in the knowledge base. Shows which documents the agent can reference and retrieve information from.', | ||
| dynamic: false, // Static provider - doesn't change based on the message | ||
| get: async (runtime: IAgentRuntime) => { | ||
| try { | ||
| const knowledgeService = runtime.getService('knowledge') as KnowledgeService; | ||
|
|
||
| if (!knowledgeService) { | ||
| logger.warn('Knowledge service not available for documents provider'); | ||
| return { | ||
| data: { documents: [] }, | ||
| values: { documents: '' }, | ||
| text: '', | ||
| }; | ||
| } | ||
|
Comment on lines
+25
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent error handling in return values. When the knowledge service is unavailable (lines 25-32), the return object doesn't include an Consider adding an error field when the service is unavailable: if (!knowledgeService) {
logger.warn('Knowledge service not available for documents provider');
return {
- data: { documents: [] },
+ data: { documents: [], error: 'Knowledge service not available' },
values: { documents: '' },
text: '',
};
}Also applies to: 112-119 🤖 Prompt for AI Agents |
||
|
|
||
| // Retrieve all documents for the agent | ||
| const allMemories = await knowledgeService.getMemories({ | ||
| tableName: 'documents', | ||
| roomId: runtime.agentId, | ||
| count: 100, // Limit to 100 documents to avoid context overflow | ||
| }); | ||
|
|
||
| // Filter to only documents (not fragments) | ||
| const documents = allMemories.filter( | ||
| (memory) => memory.metadata?.type === MemoryType.DOCUMENT | ||
| ); | ||
|
|
||
| if (!documents || documents.length === 0) { | ||
| return { | ||
| data: { documents: [] }, | ||
| values: { documents: '' }, | ||
| text: '', | ||
| }; | ||
| } | ||
|
|
||
| // Format documents concisely | ||
| const documentsList = documents | ||
| .map((doc, index) => { | ||
| const metadata = doc.metadata as any; | ||
| const filename = metadata?.filename || metadata?.title || `Document ${index + 1}`; | ||
| const fileType = metadata?.fileExt || metadata?.fileType || 'unknown'; | ||
| const source = metadata?.source || 'upload'; | ||
| const fileSize = metadata?.fileSize; | ||
|
|
||
| // Build description from metadata | ||
| const parts = [filename]; | ||
|
|
||
| // Add file type info | ||
| if (fileType && fileType !== 'unknown') { | ||
| parts.push(fileType); | ||
| } | ||
|
|
||
| // Add file size if available | ||
| if (fileSize) { | ||
| const sizeKB = Math.round(fileSize / 1024); | ||
| if (sizeKB > 1024) { | ||
| parts.push(`${Math.round(sizeKB / 1024)}MB`); | ||
| } else { | ||
| parts.push(`${sizeKB}KB`); | ||
| } | ||
| } | ||
|
|
||
| // Add source if meaningful | ||
| if (source && source !== 'upload') { | ||
| parts.push(`from ${source}`); | ||
| } | ||
|
|
||
| // Format: "filename (type, size, source)" | ||
| return parts.join(' - '); | ||
| }) | ||
| .join('\n'); | ||
|
|
||
| const documentsText = addHeader( | ||
| '# Available Documents', | ||
| `${documents.length} document(s) in knowledge base:\n${documentsList}` | ||
| ); | ||
|
|
||
| return { | ||
| data: { | ||
| documents: documents.map((doc) => ({ | ||
| id: doc.id, | ||
| filename: (doc.metadata as any)?.filename || (doc.metadata as any)?.title, | ||
| fileType: (doc.metadata as any)?.fileType || (doc.metadata as any)?.fileExt, | ||
| source: (doc.metadata as any)?.source, | ||
| })), | ||
| count: documents.length, | ||
| }, | ||
| values: { | ||
| documentsCount: documents.length, | ||
| documents: documentsList, | ||
| }, | ||
| text: documentsText, | ||
| }; | ||
| } catch (error: any) { | ||
| logger.error('Error in documents provider:', error.message); | ||
| return { | ||
| data: { documents: [], error: error.message }, | ||
| values: { documents: '' }, | ||
| text: '', | ||
| }; | ||
| } | ||
| }, | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove file extension from import statement.
The
.tsextension in the import path may cause module resolution issues in certain build configurations. TypeScript imports should typically omit the file extension.Apply this diff:
📝 Committable suggestion
🤖 Prompt for AI Agents