@frockbot/plugin-memory 0.3.14 → 0.3.16
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/package.json +6 -5
- package/src/agent.ts +17 -0
- package/src/chunk-index.test.ts +32 -0
- package/src/chunk-index.ts +62 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frockbot/plugin-memory",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.16",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": "./src/index.ts",
|
|
8
8
|
"./agent": "./src/agent.ts",
|
|
9
|
+
"./chunk-index": "./src/chunk-index.ts",
|
|
9
10
|
"./manifest": "./src/manifest.ts",
|
|
10
11
|
"./projects": "./src/projects.ts",
|
|
11
12
|
"./roots": "./src/roots.ts",
|
|
@@ -22,10 +23,10 @@
|
|
|
22
23
|
"typecheck": "tsc --noEmit -p tsconfig.json"
|
|
23
24
|
},
|
|
24
25
|
"dependencies": {
|
|
25
|
-
"@frockbot/kernel-agent-loop": "0.3.
|
|
26
|
-
"@frockbot/kernel-contracts": "0.3.
|
|
27
|
-
"@frockbot/secret-shapes": "0.3.
|
|
28
|
-
"@frockbot/workspace-store": "0.3.
|
|
26
|
+
"@frockbot/kernel-agent-loop": "0.3.16",
|
|
27
|
+
"@frockbot/kernel-contracts": "0.3.16",
|
|
28
|
+
"@frockbot/secret-shapes": "0.3.16",
|
|
29
|
+
"@frockbot/workspace-store": "0.3.16",
|
|
29
30
|
"cordis": "4.0.0-rc.8"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
package/src/agent.ts
CHANGED
|
@@ -39,9 +39,11 @@ import {
|
|
|
39
39
|
buildMemoryIndexV1,
|
|
40
40
|
emptyMemoryIndexV1,
|
|
41
41
|
embedMemoryIndexV1,
|
|
42
|
+
memoryChunkVectorIdV1,
|
|
42
43
|
updateMemoryIndexV1,
|
|
43
44
|
type MemoryIndexV1,
|
|
44
45
|
} from "./indexer.js";
|
|
46
|
+
import type { MemoryChunkIndexWriterV1 } from "./chunk-index.js";
|
|
45
47
|
import {
|
|
46
48
|
parseProjectDocumentV1,
|
|
47
49
|
projectDocumentPathV1,
|
|
@@ -99,6 +101,8 @@ export interface MemoryRuntimeHostV1 {
|
|
|
99
101
|
projects?: MemoryProjectsV1;
|
|
100
102
|
/** Optional derived-index bindings; Memory is complete without them. */
|
|
101
103
|
vectorize?: MemoryVectorIndex;
|
|
104
|
+
/** Durable ledger for vectors derived from this Bot's own Memory root. */
|
|
105
|
+
chunkIndex?: MemoryChunkIndexWriterV1;
|
|
102
106
|
embed?: EmbedMemory;
|
|
103
107
|
ai?: MemoryAiBinding;
|
|
104
108
|
embeddingModel?: string;
|
|
@@ -387,6 +391,19 @@ export class MemoryProjection {
|
|
|
387
391
|
const embed = memoryEmbedderV1(this.#host);
|
|
388
392
|
if (!embed || !this.#host.vectorize) return;
|
|
389
393
|
try {
|
|
394
|
+
if (this.#host.chunkIndex) {
|
|
395
|
+
const ownVectorIds = await Promise.all(
|
|
396
|
+
this.#index.chunks
|
|
397
|
+
.filter(
|
|
398
|
+
(chunk) =>
|
|
399
|
+
chunk.scope === "bot" && chunk.botId === this.#host.owner.botId,
|
|
400
|
+
)
|
|
401
|
+
.map(memoryChunkVectorIdV1),
|
|
402
|
+
);
|
|
403
|
+
// Intent before effect: a crash after this write and before/during the
|
|
404
|
+
// upsert leaves at worst an id whose delete is a harmless no-op.
|
|
405
|
+
await this.#host.chunkIndex.record(ownVectorIds);
|
|
406
|
+
}
|
|
390
407
|
await embedMemoryIndexV1(this.#index, embed, this.#host.vectorize);
|
|
391
408
|
} catch (error) {
|
|
392
409
|
// Embeddings are derived from the files and rebuildable; losing them
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
decodeMemoryChunkIndexEntryV1,
|
|
4
|
+
memoryChunkIndexEntriesV1,
|
|
5
|
+
memoryChunkIndexKeyV1,
|
|
6
|
+
} from "./chunk-index.js";
|
|
7
|
+
|
|
8
|
+
describe("the durable Memory chunk index", () => {
|
|
9
|
+
test("deduplicates ids and round-trips each exact stored entry", () => {
|
|
10
|
+
const entries = memoryChunkIndexEntriesV1([
|
|
11
|
+
"chunk/a",
|
|
12
|
+
"chunk/a",
|
|
13
|
+
"chunk b",
|
|
14
|
+
]);
|
|
15
|
+
expect(Object.keys(entries)).toEqual([
|
|
16
|
+
memoryChunkIndexKeyV1("chunk/a"),
|
|
17
|
+
memoryChunkIndexKeyV1("chunk b"),
|
|
18
|
+
]);
|
|
19
|
+
for (const [key, value] of Object.entries(entries)) {
|
|
20
|
+
expect(decodeMemoryChunkIndexEntryV1(key, value)).toEqual(value);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("refuses a record whose key names a different vector", () => {
|
|
25
|
+
expect(() =>
|
|
26
|
+
decodeMemoryChunkIndexEntryV1(memoryChunkIndexKeyV1("one"), {
|
|
27
|
+
schemaVersion: 1,
|
|
28
|
+
vectorId: "two",
|
|
29
|
+
}),
|
|
30
|
+
).toThrow("does not match");
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Bot-scoped ledger of Vectorize ids produced from its own Memory root.
|
|
3
|
+
*
|
|
4
|
+
* One id per key makes the ledger its own durable deletion cursor: a purge
|
|
5
|
+
* removes a key only after Vectorize accepted the matching delete. A retry
|
|
6
|
+
* after eviction therefore either advances to the next key or harmlessly
|
|
7
|
+
* repeats an id whose external delete succeeded before the local commit.
|
|
8
|
+
*/
|
|
9
|
+
export const MEMORY_CHUNK_INDEX_PREFIX_V1 = "memory:chunk-index:v1:";
|
|
10
|
+
|
|
11
|
+
export interface MemoryChunkIndexEntryV1 {
|
|
12
|
+
schemaVersion: 1;
|
|
13
|
+
vectorId: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The narrow durable seam used before Bot-Memory vectors are upserted. */
|
|
17
|
+
export interface MemoryChunkIndexWriterV1 {
|
|
18
|
+
record(vectorIds: readonly string[]): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function memoryChunkIndexKeyV1(vectorId: string): string {
|
|
22
|
+
if (!vectorId || new TextEncoder().encode(vectorId).byteLength > 64) {
|
|
23
|
+
throw new Error("Memory vector id must contain between 1 and 64 bytes");
|
|
24
|
+
}
|
|
25
|
+
return `${MEMORY_CHUNK_INDEX_PREFIX_V1}${encodeURIComponent(vectorId)}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function decodeMemoryChunkIndexEntryV1(
|
|
29
|
+
key: string,
|
|
30
|
+
input: unknown,
|
|
31
|
+
): MemoryChunkIndexEntryV1 {
|
|
32
|
+
if (
|
|
33
|
+
typeof input !== "object" ||
|
|
34
|
+
input === null ||
|
|
35
|
+
Array.isArray(input) ||
|
|
36
|
+
Reflect.get(input, "schemaVersion") !== 1 ||
|
|
37
|
+
typeof Reflect.get(input, "vectorId") !== "string" ||
|
|
38
|
+
Object.keys(input).some(
|
|
39
|
+
(field) => field !== "schemaVersion" && field !== "vectorId",
|
|
40
|
+
)
|
|
41
|
+
) {
|
|
42
|
+
throw new Error("Stored Memory chunk index entry is invalid");
|
|
43
|
+
}
|
|
44
|
+
const vectorId = Reflect.get(input, "vectorId") as string;
|
|
45
|
+
if (memoryChunkIndexKeyV1(vectorId) !== key) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
"Stored Memory chunk index key does not match its vector id",
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
return { schemaVersion: 1, vectorId };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function memoryChunkIndexEntriesV1(
|
|
54
|
+
vectorIds: readonly string[],
|
|
55
|
+
): Record<string, MemoryChunkIndexEntryV1> {
|
|
56
|
+
return Object.fromEntries(
|
|
57
|
+
[...new Set(vectorIds)].map((vectorId) => [
|
|
58
|
+
memoryChunkIndexKeyV1(vectorId),
|
|
59
|
+
{ schemaVersion: 1, vectorId },
|
|
60
|
+
]),
|
|
61
|
+
);
|
|
62
|
+
}
|