diff --git a/src/index.ts b/src/index.ts index 8f0a442..7a5a441 100644 --- a/src/index.ts +++ b/src/index.ts @@ -140,7 +140,7 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => { const projectMemories = { results: (projectMemoriesList.memories || []).map((m: any) => ({ id: m.id, - memory: m.summary, + memory: m.summary || m.content || m.title || "", similarity: 1, title: m.title, metadata: m.metadata, @@ -372,7 +372,7 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => { ...r, scope: "project" as const, })), - ].sort((a, b) => b.similarity - a.similarity); + ].sort((a, b) => (b.similarity ?? 0) - (a.similarity ?? 0)); return JSON.stringify({ success: true, @@ -381,7 +381,7 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => { results: combined.slice(0, args.limit || 10).map((r) => ({ id: r.id, content: r.memory || r.chunk, - similarity: Math.round(r.similarity * 100), + similarity: Math.round((r.similarity ?? 0) * 100), scope: r.scope, })), }); @@ -495,7 +495,7 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => { function formatSearchResults( query: string, scope: string | undefined, - results: { results?: Array<{ id: string; memory?: string; chunk?: string; similarity: number }> }, + results: { results?: Array<{ id: string; memory?: string; chunk?: string; similarity?: number }> }, limit?: number ): string { const memoryResults = results.results || []; @@ -507,7 +507,7 @@ function formatSearchResults( results: memoryResults.slice(0, limit || 10).map((r) => ({ id: r.id, content: r.memory || r.chunk, - similarity: Math.round(r.similarity * 100), + similarity: Math.round((r.similarity ?? 0) * 100), })), }); } diff --git a/src/services/client.ts b/src/services/client.ts index 2bff479..6fb3c3c 100644 --- a/src/services/client.ts +++ b/src/services/client.ts @@ -151,6 +151,7 @@ export class SupermemoryClient { limit, order: "desc", sort: "createdAt", + includeContent: true, }), TIMEOUT_MS ); diff --git a/src/services/context.ts b/src/services/context.ts index ae9f76a..3d6a18e 100644 --- a/src/services/context.ts +++ b/src/services/context.ts @@ -2,7 +2,7 @@ import type { ProfileResponse } from "supermemory/resources"; import { CONFIG } from "../config.js"; interface MemoryResultMinimal { - similarity: number; + similarity?: number; memory?: string; chunk?: string; } @@ -11,6 +11,16 @@ interface MemoriesResponseMinimal { results?: MemoryResultMinimal[]; } +function extractFactText(fact: unknown): string { + if (typeof fact === "string") return fact; + if (fact != null && typeof fact === "object") { + const content = (fact as { content?: string }).content; + if (typeof content === "string") return content; + return JSON.stringify(fact); + } + return String(fact ?? ""); +} + export function formatContextForPrompt( profile: ProfileResponse | null, userMemories: MemoriesResponseMinimal, @@ -24,14 +34,16 @@ export function formatContextForPrompt( if (staticFacts.length > 0) { parts.push("\nUser Profile:"); staticFacts.slice(0, CONFIG.maxProfileItems).forEach((fact) => { - parts.push(`- ${fact}`); + const text = extractFactText(fact); + parts.push(`- ${text}`); }); } if (dynamicFacts.length > 0) { parts.push("\nRecent Context:"); dynamicFacts.slice(0, CONFIG.maxProfileItems).forEach((fact) => { - parts.push(`- ${fact}`); + const text = extractFactText(fact); + parts.push(`- ${text}`); }); } } @@ -40,7 +52,7 @@ export function formatContextForPrompt( if (projectResults.length > 0) { parts.push("\nProject Knowledge:"); projectResults.forEach((mem) => { - const similarity = Math.round(mem.similarity * 100); + const similarity = Math.round((mem.similarity ?? 0) * 100); const content = mem.memory || mem.chunk || ""; parts.push(`- [${similarity}%] ${content}`); }); @@ -50,7 +62,7 @@ export function formatContextForPrompt( if (userResults.length > 0) { parts.push("\nRelevant Memories:"); userResults.forEach((mem) => { - const similarity = Math.round(mem.similarity * 100); + const similarity = Math.round((mem.similarity ?? 0) * 100); const content = mem.memory || mem.chunk || ""; parts.push(`- [${similarity}%] ${content}`); });