Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
})),
});
Expand Down Expand Up @@ -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 || [];
Expand All @@ -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),
})),
});
}
1 change: 1 addition & 0 deletions src/services/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export class SupermemoryClient {
limit,
order: "desc",
sort: "createdAt",
includeContent: true,
}),
TIMEOUT_MS
);
Expand Down
22 changes: 17 additions & 5 deletions src/services/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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,
Expand All @@ -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}`);
});
}
}
Expand All @@ -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}`);
});
Expand All @@ -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}`);
});
Expand Down