Skip to content

Commit d7cf2b9

Browse files
committed
fix: handle missing similarity and object profile items in context display
1 parent 092816a commit d7cf2b9

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
372372
...r,
373373
scope: "project" as const,
374374
})),
375-
].sort((a, b) => b.similarity - a.similarity);
375+
].sort((a, b) => (b.similarity ?? 0) - (a.similarity ?? 0));
376376

377377
return JSON.stringify({
378378
success: true,
@@ -381,7 +381,7 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
381381
results: combined.slice(0, args.limit || 10).map((r) => ({
382382
id: r.id,
383383
content: r.memory || r.chunk,
384-
similarity: Math.round(r.similarity * 100),
384+
similarity: Math.round((r.similarity ?? 0) * 100),
385385
scope: r.scope,
386386
})),
387387
});
@@ -495,7 +495,7 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
495495
function formatSearchResults(
496496
query: string,
497497
scope: string | undefined,
498-
results: { results?: Array<{ id: string; memory?: string; chunk?: string; similarity: number }> },
498+
results: { results?: Array<{ id: string; memory?: string; chunk?: string; similarity?: number }> },
499499
limit?: number
500500
): string {
501501
const memoryResults = results.results || [];
@@ -507,7 +507,7 @@ function formatSearchResults(
507507
results: memoryResults.slice(0, limit || 10).map((r) => ({
508508
id: r.id,
509509
content: r.memory || r.chunk,
510-
similarity: Math.round(r.similarity * 100),
510+
similarity: Math.round((r.similarity ?? 0) * 100),
511511
})),
512512
});
513513
}

src/services/context.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ProfileResponse } from "supermemory/resources";
22
import { CONFIG } from "../config.js";
33

44
interface MemoryResultMinimal {
5-
similarity: number;
5+
similarity?: number;
66
memory?: string;
77
chunk?: string;
88
}
@@ -24,14 +24,16 @@ export function formatContextForPrompt(
2424
if (staticFacts.length > 0) {
2525
parts.push("\nUser Profile:");
2626
staticFacts.slice(0, CONFIG.maxProfileItems).forEach((fact) => {
27-
parts.push(`- ${fact}`);
27+
const text = typeof fact === "string" ? fact : (fact as { content?: string }).content ?? String(fact);
28+
parts.push(`- ${text}`);
2829
});
2930
}
3031

3132
if (dynamicFacts.length > 0) {
3233
parts.push("\nRecent Context:");
3334
dynamicFacts.slice(0, CONFIG.maxProfileItems).forEach((fact) => {
34-
parts.push(`- ${fact}`);
35+
const text = typeof fact === "string" ? fact : (fact as { content?: string }).content ?? String(fact);
36+
parts.push(`- ${text}`);
3537
});
3638
}
3739
}
@@ -40,7 +42,7 @@ export function formatContextForPrompt(
4042
if (projectResults.length > 0) {
4143
parts.push("\nProject Knowledge:");
4244
projectResults.forEach((mem) => {
43-
const similarity = Math.round(mem.similarity * 100);
45+
const similarity = Math.round((mem.similarity ?? 0) * 100);
4446
const content = mem.memory || mem.chunk || "";
4547
parts.push(`- [${similarity}%] ${content}`);
4648
});
@@ -50,7 +52,7 @@ export function formatContextForPrompt(
5052
if (userResults.length > 0) {
5153
parts.push("\nRelevant Memories:");
5254
userResults.forEach((mem) => {
53-
const similarity = Math.round(mem.similarity * 100);
55+
const similarity = Math.round((mem.similarity ?? 0) * 100);
5456
const content = mem.memory || mem.chunk || "";
5557
parts.push(`- [${similarity}%] ${content}`);
5658
});

0 commit comments

Comments
 (0)