Installation
Copy
npm install praisonai
Creating Tools
Copy
import { tool, FunctionTool } from 'praisonai';
// Simple tool
const greetTool = tool({
name: 'greet',
description: 'Greet a person by name',
parameters: {
type: 'object',
properties: {
name: { type: 'string', description: 'Name to greet' }
},
required: ['name']
},
execute: async ({ name }) => `Hello, ${name}!`
});
// Execute the tool
const result = await greetTool.execute({ name: 'Alice' });
console.log(result); // "Hello, Alice!"
Tool with Categories
Copy
const searchTool = tool({
name: 'web_search',
description: 'Search the web',
category: 'web',
parameters: {
type: 'object',
properties: {
query: { type: 'string' }
},
required: ['query']
},
execute: async ({ query }) => {
// Perform search
return `Results for: ${query}`;
}
});
Tool Registry
Copy
import { ToolRegistry, tool } from 'praisonai';
const registry = new ToolRegistry();
// Register tools
registry.register(tool({
name: 'calculator',
execute: async ({ a, b }) => a + b
}));
registry.register(tool({
name: 'translator',
category: 'language',
execute: async ({ text }) => `Translated: ${text}`
}));
// Get tool by name
const calc = registry.get('calculator');
// Get tools by category
const langTools = registry.getByCategory('language');
// List all tools
const allTools = registry.list();
// Get OpenAI-compatible format
const openaiTools = registry.toOpenAITools();
Global Registry
Copy
import { getRegistry, registerTool, getTool, tool } from 'praisonai';
// Register globally
registerTool(tool({
name: 'global_tool',
execute: async () => 'result'
}));
// Retrieve from global registry
const myTool = getTool('global_tool');
// Get the global registry instance
const registry = getRegistry();
Tool Context
Copy
const contextAwareTool = tool({
name: 'context_tool',
execute: async (params, context) => {
console.log('Agent:', context?.agentName);
console.log('Session:', context?.sessionId);
console.log('Run:', context?.runId);
return 'done';
}
});
OpenAI Tool Format
Copy
const myTool = tool({
name: 'example',
description: 'An example tool',
parameters: {
type: 'object',
properties: {
input: { type: 'string' }
}
},
execute: async ({ input }) => input.toUpperCase()
});
// Get OpenAI-compatible format
const openaiFormat = myTool.toOpenAITool();
console.log(openaiFormat);
// {
// type: 'function',
// function: {
// name: 'example',
// description: 'An example tool',
// parameters: { ... }
// }
// }
Tool Definitions
Copy
const registry = new ToolRegistry();
registry.register(tool1);
registry.register(tool2);
// Get definitions for documentation
const definitions = registry.getDefinitions();
for (const def of definitions) {
console.log(`${def.name}: ${def.description}`);
}

