@lykling/pi-supermemory 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -6
- package/package.json +7 -3
- package/src/config.ts +31 -8
- package/src/context.ts +91 -26
- package/src/index.ts +125 -27
package/README.md
CHANGED
|
@@ -12,9 +12,18 @@ repository, and vice versa.
|
|
|
12
12
|
|
|
13
13
|
- Registers a `supermemory` tool with modes: `add`, `search`, `list`,
|
|
14
14
|
`forget`, `profile`, `help` — the same interface as the opencode plugin.
|
|
15
|
-
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
- **Hybrid recall** (v0.2.0, the default `autoRecall: "hybrid"`):
|
|
16
|
+
- At the first prompt of a session it injects a *small deterministic
|
|
17
|
+
preload*: user profile + a recent-project snapshot (latest 3, with age
|
|
18
|
+
labels) + semantic search hits for the opening prompt, within a hard
|
|
19
|
+
character budget.
|
|
20
|
+
- For the rest of the session, per-turn reasoned recall: the tool's prompt
|
|
21
|
+
guidelines tell the model when to `search` (topic shift, references to
|
|
22
|
+
past decisions) and when to skip (self-contained or trivial messages).
|
|
23
|
+
- After context compaction, profile + snapshot are re-injected, because
|
|
24
|
+
the session-start block does not survive summarization.
|
|
25
|
+
- `add` refuses once on near-duplicates (similarity ≥ 0.9) and suggests
|
|
26
|
+
`forget`-then-`add`, keeping the store from stacking stale copies.
|
|
18
27
|
- Redacts `<private>...</private>` blocks before sending anything to the cloud.
|
|
19
28
|
- Reuses your existing Supermemory credentials — zero extra setup if you
|
|
20
29
|
already use opencode-supermemory.
|
|
@@ -43,14 +52,18 @@ Other options:
|
|
|
43
52
|
- `SUPERMEMORY_REPO_TAG` — force a fixed container tag
|
|
44
53
|
- `SUPERMEMORY_ISOLATE_WORKTREES=true` — give each git worktree its own tag
|
|
45
54
|
- `autoRecall` in `~/.pi/agent/supermemory.json`:
|
|
46
|
-
`"
|
|
47
|
-
`"profile"` (profile only), or `"off"`
|
|
55
|
+
`"hybrid"` (default; profile + recent snapshot + prompt search on both
|
|
56
|
+
scopes), `"full"` (legacy eager dump), `"profile"` (profile only), or `"off"`
|
|
57
|
+
- Tunables in the same file: `snapshotCount` (3), `maxContextChars` (4000),
|
|
58
|
+
`compactReinject` (true), `dedupeOnAdd` (true), `dedupeThreshold` (0.9),
|
|
59
|
+
`similarityThreshold` (0.6), `maxMemories` (5), `maxProjectMemories` (10),
|
|
60
|
+
`maxProfileItems` (5)
|
|
48
61
|
|
|
49
62
|
## The tool
|
|
50
63
|
|
|
51
64
|
| mode | args | description |
|
|
52
65
|
|---|---|---|
|
|
53
|
-
| `add` | `content`, `type?`, `scope?` | Store a memory (`scope`: `user`/`project`, default `project`) |
|
|
66
|
+
| `add` | `content`, `type?`, `scope?`, `force?` | Store a memory (`scope`: `user`/`project`, default `project`; `force: true` skips the near-duplicate check) |
|
|
54
67
|
| `search` | `query`, `scope?`, `limit?` | Hybrid search over memories |
|
|
55
68
|
| `list` | `scope?`, `limit?` | List recent memories |
|
|
56
69
|
| `profile` | `query?` | User profile extracted by Supermemory |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lykling/pi-supermemory",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Persistent Supermemory cloud memory for the Pi coding agent. Shares the same memory store as your opencode supermemory plugin.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"author": "lykling <lykling.lyk@gmail.com>",
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
|
-
"url": "git+
|
|
16
|
+
"url": "git+https://github.com/lykling/pi-supermemory.git"
|
|
17
17
|
},
|
|
18
18
|
"type": "module",
|
|
19
19
|
"main": "src/index.ts",
|
|
@@ -44,5 +44,9 @@
|
|
|
44
44
|
"src",
|
|
45
45
|
"README.md",
|
|
46
46
|
"LICENSE"
|
|
47
|
-
]
|
|
47
|
+
],
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public",
|
|
50
|
+
"provenance": true
|
|
51
|
+
}
|
|
48
52
|
}
|
package/src/config.ts
CHANGED
|
@@ -6,12 +6,22 @@ import { join } from "node:path";
|
|
|
6
6
|
export interface SupermemorySettings {
|
|
7
7
|
apiKey: string | undefined;
|
|
8
8
|
baseUrl: string;
|
|
9
|
-
/**
|
|
10
|
-
autoRecall: "full" | "profile" | "off";
|
|
9
|
+
/** Recall strategy for the first prompt of each session. */
|
|
10
|
+
autoRecall: "hybrid" | "full" | "profile" | "off";
|
|
11
11
|
similarityThreshold: number;
|
|
12
12
|
maxMemories: number;
|
|
13
13
|
maxProjectMemories: number;
|
|
14
14
|
maxProfileItems: number;
|
|
15
|
+
/** Size of the "recent work snapshot" injected at session start and after compaction. */
|
|
16
|
+
snapshotCount: number;
|
|
17
|
+
/** Hard budget for the injected memory context block, in characters. */
|
|
18
|
+
maxContextChars: number;
|
|
19
|
+
/** Re-inject profile + snapshot after context compaction. */
|
|
20
|
+
compactReinject: boolean;
|
|
21
|
+
/** Similarity above which add-mode refuses once and suggests replacing the old memory. */
|
|
22
|
+
dedupeThreshold: number;
|
|
23
|
+
/** Run the near-duplicate check in add mode. */
|
|
24
|
+
dedupeOnAdd: boolean;
|
|
15
25
|
}
|
|
16
26
|
|
|
17
27
|
const DEFAULT_BASE_URL = "https://api.supermemory.ai";
|
|
@@ -140,17 +150,30 @@ export function resolveSettings(): SupermemorySettings {
|
|
|
140
150
|
|
|
141
151
|
const autoRecallRaw = firstString(piConfig?.autoRecall);
|
|
142
152
|
const autoRecall: SupermemorySettings["autoRecall"] =
|
|
143
|
-
autoRecallRaw === "
|
|
153
|
+
autoRecallRaw === "hybrid" ||
|
|
154
|
+
autoRecallRaw === "full" ||
|
|
155
|
+
autoRecallRaw === "profile" ||
|
|
156
|
+
autoRecallRaw === "off"
|
|
144
157
|
? autoRecallRaw
|
|
145
|
-
: "
|
|
158
|
+
: "hybrid";
|
|
159
|
+
|
|
160
|
+
const num = (value: unknown, fallback: number): number =>
|
|
161
|
+
typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
162
|
+
const bool = (value: unknown, fallback: boolean): boolean =>
|
|
163
|
+
typeof value === "boolean" ? value : fallback;
|
|
146
164
|
|
|
147
165
|
return {
|
|
148
166
|
apiKey,
|
|
149
167
|
baseUrl,
|
|
150
168
|
autoRecall,
|
|
151
|
-
similarityThreshold: 0.6,
|
|
152
|
-
maxMemories: 5,
|
|
153
|
-
maxProjectMemories: 10,
|
|
154
|
-
maxProfileItems: 5,
|
|
169
|
+
similarityThreshold: num(piConfig?.similarityThreshold, 0.6),
|
|
170
|
+
maxMemories: num(piConfig?.maxMemories, 5),
|
|
171
|
+
maxProjectMemories: num(piConfig?.maxProjectMemories, 10),
|
|
172
|
+
maxProfileItems: num(piConfig?.maxProfileItems, 5),
|
|
173
|
+
snapshotCount: num(piConfig?.snapshotCount, 3),
|
|
174
|
+
maxContextChars: num(piConfig?.maxContextChars, 4000),
|
|
175
|
+
compactReinject: bool(piConfig?.compactReinject, true),
|
|
176
|
+
dedupeThreshold: num(piConfig?.dedupeThreshold, 0.9),
|
|
177
|
+
dedupeOnAdd: bool(piConfig?.dedupeOnAdd, true),
|
|
155
178
|
};
|
|
156
179
|
}
|
package/src/context.ts
CHANGED
|
@@ -1,42 +1,107 @@
|
|
|
1
1
|
import type { MemoryResult, ProfileResult } from "./api.ts";
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
export function
|
|
3
|
+
/** Extract the deletable memory id from a search/list result. */
|
|
4
|
+
export function memoryId(memory: MemoryResult): string {
|
|
5
|
+
return memory.documents?.[0]?.id ?? memory.id ?? "";
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** Extract the human-readable content from a search/list result. */
|
|
9
|
+
export function memoryContent(memory: MemoryResult): string {
|
|
10
|
+
return memory.memory || memory.chunk || memory.content || memory.summary || "";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Short "how old" label from an ISO createdAt, for snapshot lines. */
|
|
14
|
+
export function relativeAge(createdAt?: string, now = Date.now()): string {
|
|
15
|
+
if (!createdAt) return "";
|
|
16
|
+
const then = Date.parse(createdAt);
|
|
17
|
+
if (!Number.isFinite(then) || then > now) return "";
|
|
18
|
+
const days = Math.floor((now - then) / 86_400_000);
|
|
19
|
+
if (days <= 0) return "today";
|
|
20
|
+
if (days === 1) return "yesterday";
|
|
21
|
+
if (days < 30) return `${days}d ago`;
|
|
22
|
+
if (days < 365) return `${Math.floor(days / 30)}mo ago`;
|
|
23
|
+
return `${Math.floor(days / 365)}y ago`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface MemoryContextOptions {
|
|
5
27
|
profile: ProfileResult | null;
|
|
6
|
-
|
|
7
|
-
|
|
28
|
+
/** Recent project memories (recency-based snapshot). */
|
|
29
|
+
snapshot: MemoryResult[];
|
|
30
|
+
/** Semantically matched memories, project and personal combined. */
|
|
31
|
+
relevant: Array<{ memory: MemoryResult; scope: "project" | "personal" }>;
|
|
8
32
|
maxProfileItems: number;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
33
|
+
maxContextChars: number;
|
|
34
|
+
/** Clock override for deterministic age labels in tests. */
|
|
35
|
+
now?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Format recalled memories into a compact context block for the LLM. */
|
|
39
|
+
export function formatMemoryContext(options: MemoryContextOptions): string {
|
|
40
|
+
const { profile, snapshot, relevant, maxProfileItems, maxContextChars, now } = options;
|
|
41
|
+
const header = "[Supermemory — persistent memory recalled for this project]";
|
|
42
|
+
const budget = Math.max(0, maxContextChars - header.length);
|
|
43
|
+
|
|
44
|
+
const lines: string[] = [];
|
|
45
|
+
let used = 0;
|
|
46
|
+
const push = (line: string): boolean => {
|
|
47
|
+
if (used + line.length + 1 > budget) return false;
|
|
48
|
+
lines.push(line);
|
|
49
|
+
used += line.length + 1;
|
|
50
|
+
return true;
|
|
51
|
+
};
|
|
12
52
|
|
|
13
53
|
if (profile && (profile.static.length > 0 || profile.dynamic.length > 0)) {
|
|
14
|
-
|
|
15
|
-
for (const line of profile.static.slice(0, maxProfileItems)) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
for (const line of profile.dynamic.slice(0, maxProfileItems)) {
|
|
19
|
-
parts.push(`- ${line}`);
|
|
20
|
-
}
|
|
54
|
+
const section = ["User Profile:"];
|
|
55
|
+
for (const line of profile.static.slice(0, maxProfileItems)) section.push(`- ${line}`);
|
|
56
|
+
for (const line of profile.dynamic.slice(0, maxProfileItems)) section.push(`- ${line}`);
|
|
57
|
+
if (!push(section.join("\n"))) return block(header, lines);
|
|
21
58
|
}
|
|
22
59
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
60
|
+
const snapshotIds = new Set<string>();
|
|
61
|
+
if (snapshot.length > 0) {
|
|
62
|
+
const sectionHeader = "Recent Project Snapshot (latest first):";
|
|
63
|
+
let opened = false;
|
|
64
|
+
for (const memory of snapshot) {
|
|
65
|
+
const age = relativeAge(memory.createdAt, now);
|
|
66
|
+
const content = memoryContent(memory);
|
|
67
|
+
if (!content) continue;
|
|
68
|
+
const id = memoryId(memory);
|
|
69
|
+
if (id) snapshotIds.add(id);
|
|
70
|
+
const line = `- ${age ? `[${age}] ` : ""}${content}`;
|
|
71
|
+
// First entry travels with its section header so we never emit an empty header.
|
|
72
|
+
if (!opened) {
|
|
73
|
+
if (!push(`${sectionHeader}\n${line}`)) return block(header, lines);
|
|
74
|
+
opened = true;
|
|
75
|
+
} else if (!push(line)) {
|
|
76
|
+
return block(header, lines);
|
|
77
|
+
}
|
|
28
78
|
}
|
|
29
79
|
}
|
|
30
80
|
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
81
|
+
if (relevant.length > 0) {
|
|
82
|
+
// Highest similarity first; skip memories already shown in the snapshot.
|
|
83
|
+
const ordered = [...relevant].sort(
|
|
84
|
+
(a, b) => (b.memory.similarity ?? 0) - (a.memory.similarity ?? 0),
|
|
85
|
+
);
|
|
86
|
+
let opened = false;
|
|
87
|
+
for (const { memory, scope } of ordered) {
|
|
88
|
+
const content = memoryContent(memory);
|
|
89
|
+
if (!content) continue;
|
|
90
|
+
if (snapshotIds.has(memoryId(memory))) continue;
|
|
34
91
|
const similarity = Math.round((memory.similarity ?? 0) * 100);
|
|
35
|
-
const
|
|
36
|
-
if (
|
|
92
|
+
const line = `- [${scope}, ${similarity}%] ${content}`;
|
|
93
|
+
if (!opened) {
|
|
94
|
+
if (push(`\nRelevant Memories:\n${line}`)) opened = true;
|
|
95
|
+
} else {
|
|
96
|
+
push(line);
|
|
97
|
+
}
|
|
37
98
|
}
|
|
38
99
|
}
|
|
39
100
|
|
|
40
|
-
|
|
41
|
-
|
|
101
|
+
return block(header, lines);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function block(header: string, lines: string[]): string {
|
|
105
|
+
if (lines.length === 0) return "";
|
|
106
|
+
return `${header}\n${lines.join("\n")}`;
|
|
42
107
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,19 +3,26 @@ import { Type } from "typebox";
|
|
|
3
3
|
import { StringEnum } from "@earendil-works/pi-ai";
|
|
4
4
|
import { SupermemoryClient } from "./api.ts";
|
|
5
5
|
import { resolveSettings } from "./config.ts";
|
|
6
|
-
import { formatMemoryContext } from "./context.ts";
|
|
6
|
+
import { formatMemoryContext, memoryId } from "./context.ts";
|
|
7
7
|
import { isFullyPrivate, stripPrivateContent } from "./privacy.ts";
|
|
8
8
|
import { getProjectName, getProjectIdentity, getProjectTag, getScopeFilters } from "./tags.ts";
|
|
9
9
|
|
|
10
|
-
const VERSION = "0.
|
|
10
|
+
const VERSION = "0.2.0";
|
|
11
11
|
const PLUGIN_NAME = "@lykling/pi-supermemory";
|
|
12
12
|
|
|
13
|
+
/** Prompts shorter than this make poor semantic search queries; snapshot covers them. */
|
|
14
|
+
const MIN_SEARCH_PROMPT_LENGTH = 16;
|
|
15
|
+
|
|
13
16
|
/**
|
|
14
17
|
* Persistent Supermemory memory for pi.
|
|
15
18
|
*
|
|
16
19
|
* Reuses the Supermemory cloud account already configured for opencode
|
|
17
20
|
* (same API key, same per-repo container tags), so memories are shared
|
|
18
21
|
* across both agents.
|
|
22
|
+
*
|
|
23
|
+
* Recall strategy (hybrid): small deterministic preload (profile + recent
|
|
24
|
+
* snapshot) at session start, per-turn reasoned recall via the tool
|
|
25
|
+
* thereafter, and re-injection after context compaction.
|
|
19
26
|
*/
|
|
20
27
|
export default function supermemoryExtension(pi: ExtensionAPI) {
|
|
21
28
|
const settings = resolveSettings();
|
|
@@ -42,8 +49,8 @@ export default function supermemoryExtension(pi: ExtensionAPI) {
|
|
|
42
49
|
"'profile' to view the user profile, 'forget' to remove a memory.",
|
|
43
50
|
promptSnippet: "Search, add, list, or forget persistent memories shared across sessions and agents",
|
|
44
51
|
promptGuidelines: [
|
|
45
|
-
"Use supermemory
|
|
46
|
-
"
|
|
52
|
+
"Recall discipline: before answering, silently decide whether saved memory would materially improve THIS reply. Use supermemory mode=search when starting a task that may build on earlier sessions, when the user references past decisions or preferences, or when the topic shifts to something previously discussed. Skip recall when the message is self-contained, trivial, or you already recalled this topic and it has not shifted. Fold results into your answer without narrating the search.",
|
|
53
|
+
"Before storing with mode=add, search first for near-duplicates: if the tool warns about similar existing memories, forget the outdated one (mode=forget with its memoryId) and then add, instead of stacking similar entries.",
|
|
47
54
|
],
|
|
48
55
|
parameters: Type.Object({
|
|
49
56
|
mode: StringEnum(["add", "search", "profile", "list", "forget", "help"], {
|
|
@@ -52,6 +59,11 @@ export default function supermemoryExtension(pi: ExtensionAPI) {
|
|
|
52
59
|
content: Type.Optional(
|
|
53
60
|
Type.String({ description: "Memory content to store (mode=add)" }),
|
|
54
61
|
),
|
|
62
|
+
force: Type.Optional(
|
|
63
|
+
Type.Boolean({
|
|
64
|
+
description: "Skip the near-duplicate check (mode=add)",
|
|
65
|
+
}),
|
|
66
|
+
),
|
|
55
67
|
query: Type.Optional(Type.String({ description: "Search query (mode=search, mode=profile)" })),
|
|
56
68
|
type: Type.Optional(
|
|
57
69
|
StringEnum(
|
|
@@ -80,6 +92,28 @@ export default function supermemoryExtension(pi: ExtensionAPI) {
|
|
|
80
92
|
if (isFullyPrivate(params.content)) {
|
|
81
93
|
return text({ success: false, error: "Cannot store fully private content" });
|
|
82
94
|
}
|
|
95
|
+
if (settings.dedupeOnAdd && !params.force) {
|
|
96
|
+
const dup = await client.searchMemories(params.content, tags.containerTag, {
|
|
97
|
+
threshold: settings.dedupeThreshold,
|
|
98
|
+
limit: 3,
|
|
99
|
+
signal,
|
|
100
|
+
});
|
|
101
|
+
if (dup.success && dup.results.length > 0) {
|
|
102
|
+
return text({
|
|
103
|
+
success: false,
|
|
104
|
+
duplicate: true,
|
|
105
|
+
message:
|
|
106
|
+
"Highly similar memories already exist. If the new content supersedes them, " +
|
|
107
|
+
"call forget with each memoryId below and then add again; to store anyway, " +
|
|
108
|
+
"retry add with force=true.",
|
|
109
|
+
similar: dup.results.map((m) => ({
|
|
110
|
+
id: memoryId(m),
|
|
111
|
+
similarity: m.similarity,
|
|
112
|
+
content: (m.memory || m.chunk || m.content || m.summary || "").slice(0, 200),
|
|
113
|
+
})),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
83
117
|
const scope = params.scope ?? "project";
|
|
84
118
|
const internalScope = scope === "user" ? "personal" : "project";
|
|
85
119
|
const result = await client.addMemory(
|
|
@@ -199,7 +233,7 @@ export default function supermemoryExtension(pi: ExtensionAPI) {
|
|
|
199
233
|
success: true,
|
|
200
234
|
message: "Supermemory Usage Guide (shared with opencode)",
|
|
201
235
|
commands: [
|
|
202
|
-
{ command: "add", description: "Store a new memory", args: ["content", "type?", "scope?"] },
|
|
236
|
+
{ command: "add", description: "Store a new memory", args: ["content", "type?", "scope?", "force?"] },
|
|
203
237
|
{ command: "search", description: "Search memories", args: ["query", "scope?", "limit?"] },
|
|
204
238
|
{ command: "profile", description: "View user profile", args: ["query?"] },
|
|
205
239
|
{ command: "list", description: "List recent memories", args: ["scope?", "limit?"] },
|
|
@@ -234,36 +268,71 @@ export default function supermemoryExtension(pi: ExtensionAPI) {
|
|
|
234
268
|
const tag = getProjectTag(ctx.cwd);
|
|
235
269
|
const prompt =
|
|
236
270
|
event.messageType === "user" ? (event as { prompt: string }).prompt : "";
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
271
|
+
// Short openers ("continue") are poor queries; the snapshot covers them.
|
|
272
|
+
const searchable = prompt.trim().length >= MIN_SEARCH_PROMPT_LENGTH;
|
|
273
|
+
|
|
274
|
+
let profileResult: Awaited<ReturnType<typeof client.getProfile>>;
|
|
275
|
+
let snapshotResult: Awaited<ReturnType<typeof client.listMemories>> | null = null;
|
|
276
|
+
let projectSearchResult: Awaited<ReturnType<typeof client.searchMemories>> | null = null;
|
|
277
|
+
let personalSearchResult: Awaited<ReturnType<typeof client.searchMemories>> | null = null;
|
|
278
|
+
|
|
279
|
+
if (settings.autoRecall === "profile") {
|
|
280
|
+
profileResult = await client.getProfile(tag);
|
|
281
|
+
} else if (settings.autoRecall === "full") {
|
|
282
|
+
[profileResult, snapshotResult, personalSearchResult] = await Promise.all([
|
|
283
|
+
client.getProfile(tag),
|
|
284
|
+
client.listMemories(tag, settings.maxProjectMemories, getScopeFilters("project")),
|
|
285
|
+
searchable
|
|
286
|
+
? client.searchMemories(prompt, tag, {
|
|
287
|
+
threshold: settings.similarityThreshold,
|
|
288
|
+
limit: settings.maxMemories,
|
|
289
|
+
filters: getScopeFilters("personal"),
|
|
290
|
+
})
|
|
291
|
+
: Promise.resolve(null),
|
|
292
|
+
]);
|
|
293
|
+
} else {
|
|
294
|
+
// hybrid (default): profile + small recent snapshot + semantic search on both scopes.
|
|
295
|
+
[profileResult, snapshotResult, projectSearchResult, personalSearchResult] = await Promise.all([
|
|
296
|
+
client.getProfile(tag, undefined, getScopeFilters("personal")),
|
|
297
|
+
client.listMemories(tag, settings.snapshotCount, getScopeFilters("project")),
|
|
298
|
+
searchable
|
|
299
|
+
? client.searchMemories(prompt, tag, {
|
|
300
|
+
threshold: settings.similarityThreshold,
|
|
301
|
+
limit: settings.maxMemories,
|
|
302
|
+
filters: getScopeFilters("project"),
|
|
303
|
+
})
|
|
304
|
+
: Promise.resolve(null),
|
|
305
|
+
searchable
|
|
306
|
+
? client.searchMemories(prompt, tag, {
|
|
307
|
+
threshold: settings.similarityThreshold,
|
|
308
|
+
limit: 3,
|
|
309
|
+
filters: getScopeFilters("personal"),
|
|
310
|
+
})
|
|
311
|
+
: Promise.resolve(null),
|
|
312
|
+
]);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const relevant: Array<{ memory: NonNullable<typeof projectSearchResult>["results"][number]; scope: "project" | "personal" }> = [];
|
|
316
|
+
for (const memory of projectSearchResult?.results ?? []) {
|
|
317
|
+
relevant.push({ memory, scope: "project" });
|
|
318
|
+
}
|
|
319
|
+
for (const memory of personalSearchResult?.results ?? []) {
|
|
320
|
+
relevant.push({ memory, scope: "personal" });
|
|
321
|
+
}
|
|
252
322
|
|
|
253
323
|
const content = formatMemoryContext({
|
|
254
324
|
profile: profileResult.success ? profileResult.profile : null,
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
projectListResult?.memories.map((m) => ({
|
|
258
|
-
...m,
|
|
259
|
-
similarity: 1,
|
|
260
|
-
})) ?? [],
|
|
325
|
+
snapshot: snapshotResult?.memories ?? [],
|
|
326
|
+
relevant,
|
|
261
327
|
maxProfileItems: settings.maxProfileItems,
|
|
328
|
+
maxContextChars: settings.maxContextChars,
|
|
262
329
|
});
|
|
263
330
|
if (!content) return;
|
|
264
331
|
|
|
332
|
+
const snapshotCount = snapshotResult?.memories.length ?? 0;
|
|
333
|
+
const relevantCount = relevant.length;
|
|
265
334
|
ctx.ui.notify(
|
|
266
|
-
`Supermemory: recalled ${
|
|
335
|
+
`Supermemory: recalled ${snapshotCount} snapshot / ${relevantCount} relevant memories`,
|
|
267
336
|
"info",
|
|
268
337
|
);
|
|
269
338
|
return {
|
|
@@ -277,6 +346,35 @@ export default function supermemoryExtension(pi: ExtensionAPI) {
|
|
|
277
346
|
// Recall must never break the turn.
|
|
278
347
|
}
|
|
279
348
|
});
|
|
349
|
+
|
|
350
|
+
// Compaction washes out the session-start injection; re-inject the
|
|
351
|
+
// deterministic parts (profile + recent snapshot) so memory survives.
|
|
352
|
+
pi.on("session_compact", async (event, ctx) => {
|
|
353
|
+
if (!settings.compactReinject || settings.autoRecall === "off") return;
|
|
354
|
+
void event;
|
|
355
|
+
try {
|
|
356
|
+
const tag = getProjectTag(ctx.cwd);
|
|
357
|
+
const [profileResult, snapshotResult] = await Promise.all([
|
|
358
|
+
client.getProfile(tag, undefined, getScopeFilters("personal")),
|
|
359
|
+
client.listMemories(tag, settings.snapshotCount, getScopeFilters("project")),
|
|
360
|
+
]);
|
|
361
|
+
const content = formatMemoryContext({
|
|
362
|
+
profile: profileResult.success ? profileResult.profile : null,
|
|
363
|
+
snapshot: snapshotResult?.memories ?? [],
|
|
364
|
+
relevant: [],
|
|
365
|
+
maxProfileItems: settings.maxProfileItems,
|
|
366
|
+
maxContextChars: settings.maxContextChars,
|
|
367
|
+
});
|
|
368
|
+
if (!content) return;
|
|
369
|
+
pi.sendMessage({
|
|
370
|
+
customType: "supermemory-recall",
|
|
371
|
+
content,
|
|
372
|
+
display: true,
|
|
373
|
+
});
|
|
374
|
+
} catch {
|
|
375
|
+
// Re-injection must never break the session.
|
|
376
|
+
}
|
|
377
|
+
});
|
|
280
378
|
}
|
|
281
379
|
|
|
282
380
|
function text(value: unknown) {
|