Skip to content

Instantly share code, notes, and snippets.

@proofoftom
Created February 1, 2025 10:42
Show Gist options
  • Select an option

  • Save proofoftom/ccdc0fd70bdf202bd7477d4272d31eac to your computer and use it in GitHub Desktop.

Select an option

Save proofoftom/ccdc0fd70bdf202bd7477d4272d31eac to your computer and use it in GitHub Desktop.
// Base Agent class that handles core functionality
class AIAgent {
constructor(userId, config = {}) {
this.userId = userId;
this.context = new Map();
this.config = {
modelName: config.modelName || 'default-model',
maxTokens: config.maxTokens || 1000,
temperature: config.temperature || 0.7
};
this.conversationHistory = [];
}
async processMessage(message) {
// Add message to conversation history
this.conversationHistory.push({
role: 'user',
content: message,
timestamp: new Date()
});
// Get relevant context
const relevantContext = this.getRelevantContext(message);
// Prepare the prompt with context and history
const prompt = this.preparePrompt(message, relevantContext);
// Call AI model (implementation depends on your chosen provider)
const response = await this.callAIModel(prompt);
// Update context based on interaction
this.updateContext(message, response);
return response;
}
getRelevantContext(message) {
// Implement context retrieval logic
return Array.from(this.context.entries())
.filter(([key, value]) => this.isRelevant(message, key, value));
}
updateContext(message, response) {
// Update context based on the interaction
const contextKey = this.generateContextKey(message);
this.context.set(contextKey, {
message,
response,
timestamp: new Date()
});
}
}
// Agent Manager to handle multiple instances
class AgentManager {
constructor() {
this.agents = new Map();
this.configs = new Map();
}
getAgent(userId) {
if (!this.agents.has(userId)) {
const config = this.configs.get(userId) || {};
const agent = new AIAgent(userId, config);
this.agents.set(userId, agent);
}
return this.agents.get(userId);
}
setUserConfig(userId, config) {
this.configs.set(userId, config);
if (this.agents.has(userId)) {
const agent = this.agents.get(userId);
agent.config = { ...agent.config, ...config };
}
}
}
// Example usage with N8N
async function setupN8NWorkflow(agentManager) {
// This would be your N8N workflow entry point
return async function(workflowData) {
const { userId, message } = workflowData;
const agent = agentManager.getAgent(userId);
const response = await agent.processMessage(message);
return {
response,
userId,
timestamp: new Date()
};
};
}
// Example Express.js implementation
import express from 'express';
const app = express();
const agentManager = new AgentManager();
app.post('/chat/:userId', async (req, res) => {
const { userId } = req.params;
const { message } = req.body;
try {
const agent = agentManager.getAgent(userId);
const response = await agent.processMessage(message);
res.json({ success: true, response });
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment