Skip to content

Commit c1451ec

Browse files
committed
refactor: extract extractFactText helper with JSON.stringify fallback
Address Copilot review comments: - Extract duplicated fact-to-text logic into reusable helper - Use JSON.stringify instead of String() for unknown object shapes to avoid [object Object] fallback
1 parent 823ff42 commit c1451ec

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/services/context.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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,15 +34,15 @@ export function formatContextForPrompt(
2434
if (staticFacts.length > 0) {
2535
parts.push("\nUser Profile:");
2636
staticFacts.slice(0, CONFIG.maxProfileItems).forEach((fact) => {
27-
const text = typeof fact === "string" ? fact : (fact as { content?: string }).content ?? String(fact);
37+
const text = extractFactText(fact);
2838
parts.push(`- ${text}`);
2939
});
3040
}
3141

3242
if (dynamicFacts.length > 0) {
3343
parts.push("\nRecent Context:");
3444
dynamicFacts.slice(0, CONFIG.maxProfileItems).forEach((fact) => {
35-
const text = typeof fact === "string" ? fact : (fact as { content?: string }).content ?? String(fact);
45+
const text = extractFactText(fact);
3646
parts.push(`- ${text}`);
3747
});
3848
}

0 commit comments

Comments
 (0)