Skip to content

Commit 6bca5df

Browse files
authored
Merge pull request #26 from kui123456789/fix/context-display-bugs
fix: handle missing similarity and object profile items in context display
2 parents 1c0bb9a + c1451ec commit 6bca5df

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export const SupermemoryPlugin: Plugin = async (ctx: PluginInput) => {
140140
const projectMemories = {
141141
results: (projectMemoriesList.memories || []).map((m: any) => ({
142142
id: m.id,
143-
memory: m.summary,
143+
memory: m.summary || m.content || m.title || "",
144144
similarity: 1,
145145
title: m.title,
146146
metadata: m.metadata,
@@ -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/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export class SupermemoryClient {
151151
limit,
152152
order: "desc",
153153
sort: "createdAt",
154+
includeContent: true,
154155
}),
155156
TIMEOUT_MS
156157
);

src/services/context.ts

Lines changed: 17 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
}
@@ -11,6 +11,16 @@ interface MemoriesResponseMinimal {
1111
results?: MemoryResultMinimal[];
1212
}
1313

14+
function extractFactText(fact: unknown): string {
15+
if (typeof fact === "string") return fact;
16+
if (fact != null && typeof fact === "object") {
17+
const content = (fact as { content?: string }).content;
18+
if (typeof content === "string") return content;
19+
return JSON.stringify(fact);
20+
}
21+
return String(fact ?? "");
22+
}
23+
1424
export function formatContextForPrompt(
1525
profile: ProfileResponse | null,
1626
userMemories: MemoriesResponseMinimal,
@@ -24,14 +34,16 @@ export function formatContextForPrompt(
2434
if (staticFacts.length > 0) {
2535
parts.push("\nUser Profile:");
2636
staticFacts.slice(0, CONFIG.maxProfileItems).forEach((fact) => {
27-
parts.push(`- ${fact}`);
37+
const text = extractFactText(fact);
38+
parts.push(`- ${text}`);
2839
});
2940
}
3041

3142
if (dynamicFacts.length > 0) {
3243
parts.push("\nRecent Context:");
3344
dynamicFacts.slice(0, CONFIG.maxProfileItems).forEach((fact) => {
34-
parts.push(`- ${fact}`);
45+
const text = extractFactText(fact);
46+
parts.push(`- ${text}`);
3547
});
3648
}
3749
}
@@ -40,7 +52,7 @@ export function formatContextForPrompt(
4052
if (projectResults.length > 0) {
4153
parts.push("\nProject Knowledge:");
4254
projectResults.forEach((mem) => {
43-
const similarity = Math.round(mem.similarity * 100);
55+
const similarity = Math.round((mem.similarity ?? 0) * 100);
4456
const content = mem.memory || mem.chunk || "";
4557
parts.push(`- [${similarity}%] ${content}`);
4658
});
@@ -50,7 +62,7 @@ export function formatContextForPrompt(
5062
if (userResults.length > 0) {
5163
parts.push("\nRelevant Memories:");
5264
userResults.forEach((mem) => {
53-
const similarity = Math.round(mem.similarity * 100);
65+
const similarity = Math.round((mem.similarity ?? 0) * 100);
5466
const content = mem.memory || mem.chunk || "";
5567
parts.push(`- [${similarity}%] ${content}`);
5668
});

0 commit comments

Comments
 (0)