mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-02 13:22:11 +03:00
- Remove `new Request('http://localhost/api/keys')` default arg from GET handler in src/app/api/keys/route.ts (line 26)
- Fix api-key-reveal-route.test.mjs to pass explicit Request instead of calling GET() with no args
- Add --output-dir coverage to all c8 scripts in package.json
- Add coverage.reportsDirectory: 'coverage' to vitest.config.ts and vitest.mcp.config.ts
- Fix CHANGELOG.md structure (# Changelog + [Unreleased] to top)
- Remove 30+ stale coverage-* directories from project root
- Coverage: Statements 78.76% | Branches 72.75% | Functions 80.93% | Lines 78.76% (all thresholds passed)
120 lines
3.5 KiB
TypeScript
120 lines
3.5 KiB
TypeScript
import { z } from "zod";
|
|
import { retrieveMemories } from "@/lib/memory/retrieval";
|
|
import { createMemory, deleteMemory, listMemories } from "@/lib/memory/store";
|
|
import { MemoryType } from "@/lib/memory/types";
|
|
|
|
export const MemorySearchSchema = z.object({
|
|
apiKeyId: z.string(),
|
|
query: z.string().optional(),
|
|
type: z.enum(["factual", "episodic", "procedural", "semantic"]).optional(),
|
|
maxTokens: z.number().int().positive().max(8000).optional(),
|
|
limit: z.number().int().positive().max(100).optional(),
|
|
});
|
|
|
|
export const MemoryAddSchema = z.object({
|
|
apiKeyId: z.string(),
|
|
sessionId: z.string().optional(),
|
|
type: z.enum(["factual", "episodic", "procedural", "semantic"]),
|
|
key: z.string().min(1),
|
|
content: z.string().min(1),
|
|
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
});
|
|
|
|
export const MemoryClearSchema = z.object({
|
|
apiKeyId: z.string(),
|
|
type: z.enum(["factual", "episodic", "procedural", "semantic"]).optional(),
|
|
olderThan: z.string().optional(),
|
|
});
|
|
|
|
export const memoryTools = {
|
|
omniroute_memory_search: {
|
|
name: "omniroute_memory_search",
|
|
description: "Search memories by query, type, or API key with token budget enforcement",
|
|
inputSchema: MemorySearchSchema,
|
|
handler: async (args: z.infer<typeof MemorySearchSchema>) => {
|
|
const config = {
|
|
enabled: true,
|
|
maxTokens: args.maxTokens || 2000,
|
|
retrievalStrategy: "exact" as const,
|
|
autoSummarize: false,
|
|
persistAcrossModels: false,
|
|
retentionDays: 30,
|
|
scope: "apiKey" as const,
|
|
query: args.query,
|
|
};
|
|
|
|
const memories = await retrieveMemories(args.apiKeyId, config);
|
|
|
|
const filtered = args.type ? memories.filter((m) => m.type === args.type) : memories;
|
|
|
|
const limited = args.limit ? filtered.slice(0, args.limit) : filtered;
|
|
|
|
return {
|
|
success: true,
|
|
data: {
|
|
memories: limited,
|
|
count: limited.length,
|
|
totalTokens: limited.reduce((sum, m) => sum + Math.ceil(m.content.length / 4), 0),
|
|
},
|
|
};
|
|
},
|
|
},
|
|
|
|
omniroute_memory_add: {
|
|
name: "omniroute_memory_add",
|
|
description: "Add a new memory entry",
|
|
inputSchema: MemoryAddSchema,
|
|
handler: async (args: z.infer<typeof MemoryAddSchema>) => {
|
|
const memory = await createMemory({
|
|
apiKeyId: args.apiKeyId,
|
|
sessionId: args.sessionId || "",
|
|
type: args.type as MemoryType,
|
|
key: args.key,
|
|
content: args.content,
|
|
metadata: args.metadata || {},
|
|
expiresAt: null,
|
|
});
|
|
|
|
return {
|
|
success: true,
|
|
data: {
|
|
memory,
|
|
message: "Memory created successfully",
|
|
},
|
|
};
|
|
},
|
|
},
|
|
|
|
omniroute_memory_clear: {
|
|
name: "omniroute_memory_clear",
|
|
description: "Clear memories for an API key, optionally filtered by type or age",
|
|
inputSchema: MemoryClearSchema,
|
|
handler: async (args: z.infer<typeof MemoryClearSchema>) => {
|
|
const memories = await listMemories({
|
|
apiKeyId: args.apiKeyId,
|
|
type: args.type as MemoryType | undefined,
|
|
});
|
|
|
|
let toDelete = memories;
|
|
if (args.olderThan) {
|
|
const cutoff = new Date(args.olderThan);
|
|
toDelete = memories.filter((m) => new Date(m.createdAt) < cutoff);
|
|
}
|
|
|
|
let deletedCount = 0;
|
|
for (const memory of toDelete) {
|
|
await deleteMemory(memory.id);
|
|
deletedCount++;
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
data: {
|
|
deletedCount,
|
|
message: `Cleared ${deletedCount} memories`,
|
|
},
|
|
};
|
|
},
|
|
},
|
|
};
|