@answerai/answeragent-mcp
Version:
A lightweight Model Context Protocol (MCP) server for Answer AI chatflow and document store management
95 lines (94 loc) • 3.96 kB
JavaScript
import { apiClient } from "./api-client.js";
export class ChatflowsService {
constructor() { }
static getInstance() {
if (!ChatflowsService.instance) {
ChatflowsService.instance = new ChatflowsService();
}
return ChatflowsService.instance;
}
async listChatflows() {
const { data } = await apiClient.get("/chatflows");
return data.map((chatflow) => ({
id: chatflow.id,
name: chatflow.name,
description: chatflow.description,
visibility: chatflow.visibility,
isPublic: chatflow.isPublic,
category: chatflow.category,
type: chatflow.type,
userId: chatflow.userId,
organizationId: chatflow.organizationId,
createdDate: chatflow.createdDate,
updatedDate: chatflow.updatedDate,
deleteDate: chatflow.deleteDate,
badge: chatflow.badge,
isOwner: chatflow.isOwner,
canEdit: chatflow.canEdit,
}));
}
async getChatflow(id, includeFullFlowData = false) {
const { data } = await apiClient.get(`/chatflows/${id}`);
// Parse flowData if it exists
if (data.flowData) {
try {
const flowDataObj = JSON.parse(data.flowData);
// Extract relevant node information and credentials
const nodesInfo = flowDataObj.nodes.map((node) => {
const nodeInfo = {
id: node.id,
type: node.data.type,
label: node.data.label,
description: node.data.description,
category: node.data.category,
credential: node.data.credential || node.data.inputs?.credential,
};
// Extract prompt templates and agent instructions if they exist
const systemPrompt = node.data.inputs?.systemMessagePrompt;
const humanPrompt = node.data.inputs?.humanMessagePrompt;
const agentInstructions = node.data.inputs?.agentInstructions;
if (systemPrompt || humanPrompt || agentInstructions) {
nodeInfo.prompts = {
systemMessagePrompt: systemPrompt,
humanMessagePrompt: humanPrompt,
agentInstructions: agentInstructions,
};
}
return nodeInfo;
});
// Extract all credential IDs
const credentialIds = flowDataObj.nodes
.map((node) => node.data.credential || node.data.inputs?.credential)
.filter(Boolean);
// Add parsed data to the response
data.parsedFlowData = {
nodes: nodesInfo,
credentialIds: [...new Set(credentialIds)], // Remove duplicates
};
// Create a new object without flowData if not requested
if (!includeFullFlowData) {
const { flowData: _, ...dataWithoutFlowData } = data;
return {
...dataWithoutFlowData,
parsedFlowData: data.parsedFlowData,
};
}
}
catch (error) {
console.error("Error parsing flowData:", error);
}
}
return data;
}
async createChatflow(chatflow) {
const { data } = await apiClient.post("/chatflows", chatflow);
return data;
}
async updateChatflow(id, chatflow) {
const { data } = await apiClient.put(`/chatflows/${id}`, chatflow);
return data;
}
async deleteChatflow(id) {
await apiClient.delete(`/chatflows/${id}`);
}
}