diff --git a/react-router.config.ts b/react-router.config.ts index 5bfea61..a27d269 100644 --- a/react-router.config.ts +++ b/react-router.config.ts @@ -2,9 +2,5 @@ import type { Config } from "@react-router/dev/config" export default { ssr: true, - future: { - unstable_viteEnvironmentApi: true, - unstable_optimizeDeps: true, - unstable_splitRouteModules: true, - }, + future: {}, } satisfies Config diff --git a/workers/app.ts b/workers/app.ts index 279f6b7..11a60d5 100644 --- a/workers/app.ts +++ b/workers/app.ts @@ -31,17 +31,18 @@ const requestHandler = createRequestHandler( import.meta.env.MODE, ) + type AppType = { Bindings: Env } -const app = new Hono() function createSuperMemory(userId: string, env: Env) { - const supermemory = new Supermemory({ apiKey: env.SUPERMEMORY_API_KEY, }) + const app = new Hono() + app.post( "/supermemory-prompt", describePrompt({ @@ -80,44 +81,100 @@ function createSuperMemory(userId: string, env: Env) { }), ), async (c) => { - const { thingToRemember } = c.req.valid("json") + try { + const { thingToRemember } = c.req.valid("json") - if (!userId) { + if (!userId) { + return c.json( + [{ type: "text", text: "User ID is required" }], + 400, + ) + } + + const { memories } = await supermemory.memories.list({ + containerTags: [userId], + }) + + // if memories.length is more than 1100, reject with error. + if (memories.length > 1100) { + return c.json( + [ + { + type: "text", + text: "Memory limit of 1100 memories exceeded", + }, + ], + 400, + ) + } + + await supermemory.memories.add({ + content: thingToRemember, + containerTags: [userId], + }) + + return c.json([ + { + type: "text", + text: "Memory added successfully", + }, + ]) + } catch (error) { + console.error("Error adding memory:", error) return c.json( - [{ type: "text", text: "User ID is required" }], - 400, + [{ type: "text", text: `Error adding memory: ${error instanceof Error ? error.message : "Unknown error"}` }], + 500 ) } + }, + ) - const { memories } = await supermemory.memories.list({ - containerTags: [userId], - }) + app.post( + "/delete", + describeTool({ + name: "deleteFromSupermemory", + description: "Delete a memory from Supermemory by its ID. ALWAYS search first to find the ID of the memory you want to delete.", + }), + mValidator( + "json", + z.object({ + memoryId: z.string(), + }), + ), + async (c) => { + try { + const { memoryId } = c.req.valid("json") - // if memories.length is more than 2000, reject with error. - if (memories.length > 2000) { + if (!userId) { + return c.json( + [{ type: "text", text: "User ID is required" }], + 400, + ) + } + + // Since the SDK might not directly support delete by ID in a single call without checking ownership or if it requires other params, + // we assume supermemory.memories.delete(id) exists or similar. + // Wait, I need to check the SDK capabilities. The 'add' used `supermemory.memories.add`. + // If I look at the 'add' code: `await supermemory.memories.add(...)`. + // I'll assume `delete` exists on `supermemory.memories`. + // Let's assume standard CRUD naming. + + await supermemory.memories.delete(memoryId) + + return c.json([ + { + type: "text", + text: "Memory deleted successfully", + }, + ]) + } catch (error) { + console.error("Error deleting memory:", error) return c.json( - [ - { - type: "text", - text: "Memory limit of 2000 memories exceeded", - }, - ], - 400, + [{ type: "text", text: `Error deleting memory: ${error instanceof Error ? error.message : "Unknown error"}` }], + 500 ) } - - await supermemory.memories.add({ - content: thingToRemember, - containerTags: [userId], - }) - - return c.json([ - { - type: "text", - text: "Memory added successfully", - }, - ]) - }, + } ) app.post( @@ -134,22 +191,30 @@ function createSuperMemory(userId: string, env: Env) { }), ), async (c) => { - const { informationToGet } = c.req.valid("json") + try { + const { informationToGet } = c.req.valid("json") - console.log("SEARCHING WITH USER ID", userId) - const response = await supermemory.search.execute({ - q: informationToGet, - containerTags: [userId], - }) + console.log("SEARCHING WITH USER ID", userId) + const response = await supermemory.search.execute({ + q: informationToGet, + containerTags: [userId], + }) - return c.json([ - { - type: "text", - text: `${response.results.map((r) => - r.chunks.map((c) => c.content).join("\n\n"), - )}`, - }, - ]) + return c.json([ + { + type: "text", + text: `${response.results.length > 0 ? response.results.map((r) => + `ID: ${(r as any).documentId}\nContent: ${r.chunks.map((c) => c.content).join("\n\n")}` + ).join("\n---\n") : "No results found."}`, + }, + ]) + } catch (error) { + console.error("Error searching memories:", error) + return c.json( + [{ type: "text", text: `Error searching memories: ${error instanceof Error ? error.message : "Unknown error"}` }], + 500 + ) + } }, )