Autonomous research agent with web search, source analysis, and report synthesis. Built on Pi agent core. Available as CLI, programmatic API, and MCP server. Features: - 4 built-in tools: web_search, read_url, save_note, synthesize_report - CLI with TUI and print modes - Programmatic API via ResearchAgent class - MCP server with 3 tools: research, quick_search, read_url - Skills system for custom research behavior - Automatic .env file loading for API keys - Configurable max turns, output formats (markdown/JSON/bullet) - Automatic report saving to research-output/
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
/**
|
|
* Basic Research Agent Example
|
|
*
|
|
* Demonstrates how to use the ResearchAgent programmatically.
|
|
*
|
|
* Run with:
|
|
* TAVILY_API_KEY=*** \
|
|
* OPENROUTER_API_KEY=*** \
|
|
* npx tsx examples/basic.ts
|
|
*/
|
|
|
|
import { ResearchAgent } from "../src/index.ts";
|
|
|
|
const agent = new ResearchAgent({
|
|
maxTurns: 10,
|
|
});
|
|
|
|
agent.subscribe((event) => {
|
|
switch (event.type) {
|
|
case "tool_execution_start": {
|
|
const toolName = event.toolName;
|
|
const args = event.args as Record<string, unknown>;
|
|
if (toolName === "web_search") {
|
|
console.error(`[Searching: ${args.query}]`);
|
|
} else if (toolName === "read_url") {
|
|
console.error(`[Reading: ${args.url}]`);
|
|
} else if (toolName === "save_note") {
|
|
console.error(`[Saving note: ${args.topic}]`);
|
|
} else if (toolName === "synthesize_report") {
|
|
console.error("[Synthesizing report...]");
|
|
}
|
|
break;
|
|
}
|
|
case "message_update": {
|
|
if (event.assistantMessageEvent.type === "text_delta") {
|
|
process.stdout.write(event.assistantMessageEvent.delta);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
|
|
const topic = "Latest developments in quantum error correction 2026";
|
|
console.log(`Researching: ${topic}\n`);
|
|
|
|
const result = await agent.research(topic);
|
|
|
|
console.log("\n\n" + "=".repeat(60));
|
|
console.log(`Research complete!`);
|
|
console.log(`Turns used: ${result.turnsUsed}`);
|
|
console.log(`Notes collected: ${result.notes.length}`);
|
|
console.log("=".repeat(60) + "\n");
|
|
|
|
console.log(result.report);
|