fix: implement missing memory and skills api routes, wire MCP tools, fix migration numbers

This commit is contained in:
diegosouzapw
2026-04-01 00:34:06 -03:00
parent 5899b0f1e4
commit 60968a926f
10 changed files with 171 additions and 9 deletions

View File

@@ -55,6 +55,8 @@ import {
handleGetSessionSnapshot,
handleSyncPricing,
} from "./tools/advancedTools.ts";
import { memoryTools } from "./tools/memoryTools.ts";
import { skillTools } from "./tools/skillTools.ts";
import { normalizeQuotaResponse } from "../../src/shared/contracts/quota.ts";
// ============ Configuration ============
@@ -717,6 +719,48 @@ export function createMcpServer(): McpServer {
)
);
// ── Memory Tools ──────────────────────────────
Object.values(memoryTools).forEach((toolDef) => {
server.registerTool(
toolDef.name,
{
description: toolDef.description,
inputSchema: toolDef.inputSchema as any,
},
withScopeEnforcement(toolDef.name, async (args) => {
try {
const parsedArgs = toolDef.inputSchema.parse(args ?? {});
const result = await toolDef.handler(parsedArgs as any);
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
}
})
);
});
// ── Skill Tools ──────────────────────────────
Object.values(skillTools).forEach((toolDef) => {
server.registerTool(
toolDef.name,
{
description: toolDef.description,
inputSchema: toolDef.inputSchema as any,
},
withScopeEnforcement(toolDef.name, async (args) => {
try {
const parsedArgs = toolDef.inputSchema.parse(args ?? {});
const result = await toolDef.handler(parsedArgs as any);
return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] };
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
return { content: [{ type: "text" as const, text: `Error: ${msg}` }], isError: true };
}
})
);
});
return server;
}

View File

@@ -0,0 +1,36 @@
import { NextResponse } from "next/server";
import { deleteMemory, getMemory } from "@/lib/memory/store";
export async function DELETE(
request: Request,
props: { params: Promise<{ id: string }> }
) {
try {
const { id } = await props.params;
const success = await deleteMemory(id);
if (!success) {
return NextResponse.json({ error: "Memory not found" }, { status: 404 });
}
return NextResponse.json({ success: true });
} catch (err: unknown) {
const error = err instanceof Error ? err.message : String(err);
return NextResponse.json({ error }, { status: 500 });
}
}
export async function GET(
request: Request,
props: { params: Promise<{ id: string }> }
) {
try {
const { id } = await props.params;
const memory = await getMemory(id);
if (!memory) {
return NextResponse.json({ error: "Not found" }, { status: 404 });
}
return NextResponse.json({ memory });
} catch (err: unknown) {
const error = err instanceof Error ? err.message : String(err);
return NextResponse.json({ error }, { status: 500 });
}
}

View File

@@ -0,0 +1,36 @@
import { NextResponse } from "next/server";
import { listMemories, createMemory } from "@/lib/memory/store";
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const apiKeyId = searchParams.get("apiKeyId") || undefined;
const type = searchParams.get("type") as any || undefined;
const sessionId = searchParams.get("sessionId") || undefined;
const limitParams = searchParams.get("limit");
const offsetParams = searchParams.get("offset");
const memories = await listMemories({
apiKeyId,
type,
sessionId,
limit: limitParams ? parseInt(limitParams, 10) : undefined,
offset: offsetParams ? parseInt(offsetParams, 10) : undefined,
});
return NextResponse.json({ memories });
} catch (err: unknown) {
const error = err instanceof Error ? err.message : String(err);
return NextResponse.json({ error }, { status: 500 });
}
}
export async function POST(request: Request) {
try {
const body = await request.json();
const memoryId = await createMemory(body);
return NextResponse.json({ success: true, id: memoryId });
} catch (err: unknown) {
const error = err instanceof Error ? err.message : String(err);
return NextResponse.json({ error }, { status: 400 });
}
}

View File

@@ -0,0 +1,30 @@
import { NextResponse } from "next/server";
import { getDbInstance } from "@/lib/db/core";
import { skillRegistry } from "@/lib/skills/registry";
export async function PUT(
request: Request,
props: { params: Promise<{ id: string }> }
) {
try {
const { id } = await props.params;
const body = await request.json();
if (typeof body.enabled !== "boolean") {
return NextResponse.json({ error: "Invalid payload, missing enabled boolean" }, { status: 400 });
}
const db = getDbInstance();
db.prepare("UPDATE skills SET enabled = ? WHERE id = ?").run(
body.enabled ? 1 : 0,
id
);
await skillRegistry.loadFromDatabase();
return NextResponse.json({ success: true, enabled: body.enabled });
} catch (err: unknown) {
const error = err instanceof Error ? err.message : String(err);
return NextResponse.json({ error }, { status: 500 });
}
}

View File

@@ -0,0 +1,12 @@
import { NextResponse } from "next/server";
import { skillExecutor } from "@/lib/skills/executor";
export async function GET() {
try {
const executions = skillExecutor.listExecutions();
return NextResponse.json({ executions });
} catch (err: unknown) {
const error = err instanceof Error ? err.message : String(err);
return NextResponse.json({ error }, { status: 500 });
}
}

View File

@@ -0,0 +1,13 @@
import { NextResponse } from "next/server";
import { skillRegistry } from "@/lib/skills/registry";
export async function GET() {
try {
await skillRegistry.loadFromDatabase();
const skills = skillRegistry.list();
return NextResponse.json({ skills });
} catch (err: unknown) {
const error = err instanceof Error ? err.message : String(err);
return NextResponse.json({ error }, { status: 500 });
}
}

View File

@@ -1,4 +0,0 @@
-- 014_create_memories_down.sql
-- DOWN Migration: Remove memories table (Rollback)
DROP TABLE IF EXISTS memories;

View File

@@ -1,5 +0,0 @@
-- 015_create_skills_down.sql
-- Rollback skills and skill_executions tables
DROP TABLE IF EXISTS skill_executions;
DROP TABLE IF EXISTS skills;