import { z } from "zod";
import { AddMemorySchema } from "../schemas/index.js";
import { makeInfraNodusRequest } from "../api/client.js";
import { transformToStructuredOutput } from "../utils/transformers.js";
export const addMemoryTool = {
name: "memory_add_relations",
definition: {
title: "Add Relations to the InfraNodus Memory",
description:
"Add relations to the InfraNodus memory from text, save it, and provide its name and a link to it for future use. ",
inputSchema: AddMemorySchema.shape,
annotations: {
readOnlyHint: false,
idempotentHint: false,
destructiveHint: false,
},
},
handler: async (params: z.infer<typeof AddMemorySchema>) => {
try {
const includeNodesAndEdges = params.addNodesAndEdges;
const includeGraph = params.includeGraph;
const buildingEntitiesGraph =
params.modifyAnalyzedText == "extractEntitiesOnly" ? true : false;
// Build query parameters
const queryParams = new URLSearchParams({
doNotSave: "false",
addStats: "true",
includeStatements: params.includeStatements ? "true" : "false",
includeGraphSummary: "false",
extendedGraphSummary: "true",
includeGraph: includeGraph || buildingEntitiesGraph ? "true" : "false",
compactGraph: includeGraph || buildingEntitiesGraph ? "true" : "false",
compactStatements: params.includeStatements ? "true" : "false",
aiTopics: "true",
optimize: "develop",
});
const endpoint = `/graphAndStatements?${queryParams.toString()}`;
const requestBody: any = {
name: params.graphName,
text: params.text,
aiTopics: "true",
};
if (params.modifyAnalyzedText && params.modifyAnalyzedText !== "none") {
requestBody.modifyAnalyzedText = params.modifyAnalyzedText;
}
const response = await makeInfraNodusRequest(endpoint, requestBody);
if (response.error) {
return {
content: [
{
type: "text" as const,
text: `Error: ${response.error}`,
},
],
isError: true,
};
}
const structuredOutput = transformToStructuredOutput(
response,
includeGraph,
includeNodesAndEdges,
buildingEntitiesGraph
);
return {
content: [
{
type: "text" as const,
text: JSON.stringify(structuredOutput, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: "text" as const,
text: JSON.stringify({
error: error instanceof Error ? error.message : String(error),
}),
},
],
isError: true,
};
}
},
};