contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
293 lines (263 loc) ⢠10.2 kB
JavaScript
import { ChatService } from '../services/chatService.js';
import fs from 'fs/promises';
import path from 'path';
// Mock LLM that simulates the complete workflow from your example
class CompleteWorkflowMockLLM {
constructor() {
this.step = 0;
}
async executePrompt(prompt, _options) {
console.log(`š¤ Mock LLM Step ${this.step + 1} - Prompt length: ${prompt.length}`);
if (this.step === 0) {
// Initial request - agent makes multiple tool calls
this.step++;
return {
content: `I'll help you understand the project setup and update the README accordingly. Based on the provided file structure, I can see some files that might be useful to understand the project setup. I'll start by reading the README, package.json, and src/index.ts files to get a comprehensive understanding of the project. This will help me provide accurate information and update the README accordingly.
<tool_call name="read_file" id="1">
<file_path>README.md</file_path>
</tool_call>
<tool_call name="read_file" id="2">
<file_path>package.json</file_path>
</tool_call>
<tool_call name="read_file" id="3">
<file_path>src/index.ts</file_path>
</tool_call>`
};
}
else if (this.step === 1) {
// Follow-up after first set of tool calls
this.step++;
return {
content: `I can see the README.md has some reference to other md files, let me read those files to understand the project better.
<tool_call name="read_file" id="4">
<file_path>content/ai-tools-research.md</file_path>
</tool_call>
<tool_call name="read_file" id="5">
<file_path>templates/blog-post-template.md</file_path>
</tool_call>`
};
}
else if (this.step === 2) {
// Final step - update the README
this.step++;
return {
content: `Based on the project files I've analyzed, I can see that the project is a CLI tool for managing content. Let me update the README.md file accordingly.
<tool_call name="write_file" id="6">
<operation_type>rewrite</operation_type>
<file_path>README.md</file_path>
<content><![CDATA[
# Contaigents CLI
This is a CLI tool for managing content. It is built using TypeScript and uses the following dependencies:
## Dependencies
- ts-node: TypeScript execution environment
- typescript: TypeScript compiler
## Getting Started
The main entry point for the CLI is the \`src/index.ts\` file.
### Running the CLI
\`\`\`bash
npm start
\`\`\`
## Project Structure
The CLI provides the following resources:
- \`content/ai-tools-research.md\`: Research on AI tools
- \`templates/blog-post-template.md\`: Template for writing blog posts
## Features
This CLI tool helps manage content creation and organization with AI-powered assistance.
]]></content>
</tool_call>`
};
}
else {
// Final response
return {
content: `I've updated the README.md file with the information I've gathered from the project files. The README now includes:
- Project description and purpose
- Dependencies information
- Getting started instructions
- Project structure overview
- Available resources and templates
The updated README provides a comprehensive overview of the Contaigents CLI tool and its capabilities. Please let me know if you need any additional information or modifications.`
};
}
}
}
async function demonstrateCompleteWorkflow() {
console.log('š Complete Workflow Demonstration');
console.log('='.repeat(60));
// Create test directory and files
const testDir = path.join(process.cwd(), 'test-complete-workflow');
await fs.mkdir(testDir, { recursive: true });
await fs.mkdir(path.join(testDir, 'src'), { recursive: true });
await fs.mkdir(path.join(testDir, 'content'), { recursive: true });
await fs.mkdir(path.join(testDir, 'templates'), { recursive: true });
// Create test files
await fs.writeFile(path.join(testDir, 'README.md'), `# Contaigents CLI
This is a CLI tool for managing content.`);
await fs.writeFile(path.join(testDir, 'package.json'), JSON.stringify({
"name": "contaigents",
"version": "1.0.0",
"description": "CLI tool for managing content",
"main": "src/index.ts",
"scripts": {
"start": "ts-node src/index.ts"
},
"dependencies": {
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}, null, 2));
await fs.writeFile(path.join(testDir, 'src/index.ts'), `import { ContaigentsCLI } from './contaigents-cli';
const cli = new ContaigentsCLI();
cli.run();`);
await fs.writeFile(path.join(testDir, 'content/ai-tools-research.md'), `# AI Tools Research
This file contains research on AI tools.`);
await fs.writeFile(path.join(testDir, 'templates/blog-post-template.md'), `# Blog Post Template
This is a template for writing blog posts.`);
// Create chat service with mock LLM
const mockLLM = new CompleteWorkflowMockLLM();
const chatService = new ChatService(testDir);
chatService.llmFactory = {
createLLM: () => mockLLM
};
// Create session
const session = await chatService.createSession();
console.log(`š Created chat session: ${session.id}`);
// Simulate the complete workflow
console.log('\nš¤ User: "learn project setup in detail and update the readme"');
console.log('\nš¤ Agent Response:');
console.log('-'.repeat(50));
const response1 = await chatService.sendMessage(session.id, 'learn project setup in detail and update the readme');
console.log(response1.content);
console.log('\nš¤ User: (Tool responses provided)');
console.log('\nš¤ Agent Response:');
console.log('-'.repeat(50));
// Simulate user providing tool responses (this would normally be automatic)
const toolResponsesMessage = `<tool_response id="1">
<tool_call name="read_file">
<file_path>README.md</file_path>
</tool_call>
<result>
<content>
# Contaigents CLI
This is a CLI tool for managing content.
</content>
</result>
</tool_response>
<tool_response id="2">
<tool_call name="read_file">
<file_path>package.json</file_path>
</tool_call>
<result>
<content>
{
"name": "contaigents",
"version": "1.0.0",
"description": "CLI tool for managing content",
"main": "src/index.ts",
"scripts": {
"start": "ts-node src/index.ts"
},
"dependencies": {
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
</content>
</result>
</tool_response>
<tool_response id="3">
<tool_call name="read_file">
<file_path>src/index.ts</file_path>
</tool_call>
<result>
<content>
import { ContaigentsCLI } from './contaigents-cli';
const cli = new ContaigentsCLI();
cli.run();
</content>
</result>
</tool_response>`;
const response2 = await chatService.sendMessage(session.id, toolResponsesMessage);
console.log(response2.content);
// Continue the workflow...
console.log('\nš¤ User: (Additional tool responses provided)');
console.log('\nš¤ Agent Response:');
console.log('-'.repeat(50));
const additionalToolResponses = `<tool_response id="4">
<tool_call name="read_file">
<file_path>content/ai-tools-research.md</file_path>
</tool_call>
<result>
<content>
# AI Tools Research
This file contains research on AI tools.
</content>
</result>
</tool_response>
<tool_response id="5">
<tool_call name="read_file">
<file_path>templates/blog-post-template.md</file_path>
</tool_call>
<result>
<content>
# Blog Post Template
This is a template for writing blog posts.
</content>
</result>
</tool_response>`;
const response3 = await chatService.sendMessage(session.id, additionalToolResponses);
console.log(response3.content);
// Final update
console.log('\nš¤ User: (Final tool response provided)');
console.log('\nš¤ Agent Response:');
console.log('-'.repeat(50));
const finalToolResponse = `<tool_response id="6">
<tool_call name="write_file">
<operation_type>rewrite</operation_type>
<file_path>README.md</file_path>
<content><![CDATA[
# Contaigents CLI
This is a CLI tool for managing content. It is built using TypeScript and uses the following dependencies:
## Dependencies
- ts-node: TypeScript execution environment
- typescript: TypeScript compiler
## Getting Started
The main entry point for the CLI is the \`src/index.ts\` file.
### Running the CLI
\`\`\`bash
npm start
\`\`\`
## Project Structure
The CLI provides the following resources:
- \`content/ai-tools-research.md\`: Research on AI tools
- \`templates/blog-post-template.md\`: Template for writing blog posts
## Features
This CLI tool helps manage content creation and organization with AI-powered assistance.
]]></content>
</tool_call>
<result>
<content>
README.md updated successfully!
</content>
</result>
</tool_response>`;
const response4 = await chatService.sendMessage(session.id, finalToolResponse);
console.log(response4.content);
// Show the updated README
console.log('\nš Updated README.md:');
console.log('-'.repeat(50));
const updatedReadme = await fs.readFile(path.join(testDir, 'README.md'), 'utf-8');
console.log(updatedReadme);
// Cleanup
await fs.rm(testDir, { recursive: true });
console.log('\nš Complete Workflow Demonstration Finished!');
console.log('\nšÆ Workflow Features Demonstrated:');
console.log('- ā
Multi-step tool call sequences');
console.log('- ā
XML tool call format with IDs');
console.log('- ā
Structured XML tool responses');
console.log('- ā
Complex content handling with CDATA');
console.log('- ā
File reading and writing operations');
console.log('- ā
Conversational flow with tool integration');
}
// Run the demonstration
demonstrateCompleteWorkflow().catch(console.error);