Resources
Resources in A2 are a powerful system for managing and accessing structured content that your agents can utilize. The Resource system provides a consistent way to store, retrieve, and manipulate different types of content, from prompt templates to files.
Overview
The A2 Resource system is composed of:
- Resources: Individual content items with metadata
- ResourceManager: A service for managing resources and prompts
- Prompt Templates: Parameterized text templates that can be rendered with variables
- Resource Tools: A set of tools for working with resources within agent workflows
Getting Started
Import the Resource system from the core package:
import { DefaultResourceManager, ResourceManager } from '@a2/core';
Create a resource manager instance:
const resourceManager = new DefaultResourceManager({ resourceDirectory: './resources', // Optional: path to store/load resources});
Working with Resources
Resource Structure
Resources have the following structure:
interface Resource { id: string; // Unique identifier content: string; // The actual content type: string; // Type of resource (e.g., 'text', 'json', 'prompt') metadata?: Record<string, any>; // Optional additional metadata}
Managing Resources
// Add a resourceresourceManager.addResource({ id: 'welcome-message', content: 'Welcome to the A2 framework!', type: 'text',});
// Get a resourceconst welcomeMsg = resourceManager.getResource('welcome-message');
// Update a resourceresourceManager.updateResource('welcome-message', { content: 'Welcome to the new A2 framework!',});
// List all resourcesconst resourceIds = resourceManager.listResources();
// Get resources of a specific typeconst textResources = resourceManager.getResourcesByType('text');
// Remove a resourceresourceManager.removeResource('welcome-message');
File Operations
The Resource Manager provides methods for working with files:
// Load a resource from a fileconst jsonResource = resourceManager.loadResourceFromFile('./data.json', 'json', { description: 'Configuration data',});
// Save a resource to a fileresourceManager.saveResourceToFile('config-data', './saved-config.json');
// Load all resources from a directoryconst resources = resourceManager.loadResourcesFromDirectory('./resources', 'json');
// Export resources to filesresourceManager.exportResourcesToDirectory( ['welcome-message', 'config-data'], './exported-resources');
Working with Prompts
The Resource Manager also provides a prompt management system, allowing for reusable, parametrized prompt templates.
Prompt Types
Prompts can be either static strings or template functions:
// Static promptresourceManager.addPrompt('greeting', 'Hello, how can I help you today?');
// Template functionresourceManager.addPrompt( 'personalized-greeting', params => `Hello ${params.name}, how can I help you today?`);
Using Prompts
// Get a promptconst greeting = resourceManager.getPrompt('greeting');
// Render a prompt with parametersconst personalGreeting = resourceManager.renderPrompt('personalized-greeting', { name: 'Alice',});
// Compose multiple promptsconst fullPrompt = resourceManager.composePrompt(['greeting', 'instructions', 'examples'], { userName: 'Bob',});
Resource Tools
A2 provides tools that agents can use to work with resources:
get-resource
: Retrieve a resource by IDlist-resources
: List available resources, optionally filtered by typeget-prompt
: Get a prompt by namerender-prompt
: Render a prompt with parameters
These tools can be integrated into your agent’s toolset to allow manipulation of resources during execution.
Best Practices
- Organization: Group related resources by type for easier management
- Naming Conventions: Use clear, consistent naming for resource IDs
- Parameterization: Create flexible prompt templates using parameters
- Persistence: Use the file operations to persist important resources
Example: Building a Knowledge Base
// Create a resource manager with a dedicated directoryconst knowledgeBase = new DefaultResourceManager({ resourceDirectory: './knowledge-base',});
// Load existing resources from the directoryknowledgeBase.loadResourcesFromDirectory('./knowledge-base');
// Add a new knowledge articleknowledgeBase.addResource({ id: 'api-usage', content: '# API Usage Guide\n\nThis document explains...', type: 'markdown', metadata: { author: 'A2 Team', lastUpdated: new Date().toISOString(), },});
// Create a prompt that references knowledgeknowledgeBase.addPrompt( 'answer-with-knowledge', params => `Use the following information to answer the question:
${params.resourceContent}
Question: ${params.question}Answer:`);
// Use the knowledge in an agent interactionconst apiGuide = knowledgeBase.getResource('api-usage');const prompt = knowledgeBase.renderPrompt('answer-with-knowledge', { resourceContent: apiGuide.content, question: 'How do I authenticate to the API?',});