Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions packages/junior-memory/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import { z } from "zod";
import { MEMORY_KINDS, MEMORY_SCOPES } from "./types";
import type { MemoryRecord } from "./store";

function currentScope(
scope: "personal" | "conversation" | "private" | "public",
): "private" | "public" {
if (scope === "personal") return "private";
if (scope === "conversation") return "public";
return scope;
}

const capturedMemoryFields = {
content: z.string().min(1),
id: z.string().min(1),
Expand All @@ -20,7 +28,17 @@ const legacyCapturedMemorySchema = z
const capturedMemorySchema = z
.object({
...capturedMemoryFields,
scope: z.enum(MEMORY_SCOPES),
scope: z.preprocess((val) => {
if (
val === "personal" ||
val === "conversation" ||
val === "private" ||
val === "public"
) {
return currentScope(val);
}
return val;
}, z.enum(MEMORY_SCOPES)),
})
.strict();

Expand All @@ -39,14 +57,6 @@ const recalledMemoriesSchema = z
})
.strict();

function currentScope(
scope: "personal" | "conversation" | "private" | "public",
) {
if (scope === "personal") return "private";
if (scope === "conversation") return "public";
return scope;
}

function renderCapturedMemories(event: {
memories: Array<
| z.output<typeof legacyCapturedMemorySchema>
Expand Down
45 changes: 31 additions & 14 deletions packages/junior-memory/tests/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,37 @@ describe("memory conversation events", () => {
memoriesCapturedEventV1.renderEvent(legacy)?.details?.[0]?.metadata,
).toEqual(["preference", "private"]);

expect(() =>
memoriesCapturedEvent.parse({
costUsd: 0.0042,
memories: [
{
content: "Release notes live in Notion.",
id: "memory-v2",
kind: "knowledge",
observedAtMs: 2,
scope: "conversation",
},
],
}),
).toThrow(/scope/);
const v2Legacy = memoriesCapturedEvent.parse({
costUsd: 0.0042,
memories: [
{
content: "Release notes live in Notion.",
id: "memory-v2",
kind: "knowledge",
observedAtMs: 2,
scope: "conversation",
},
{
content: "Prefer short replies.",
id: "memory-v2-personal",
kind: "preference",
observedAtMs: 3,
scope: "personal",
},
],
});
expect(v2Legacy.memories.map((memory) => memory.scope)).toEqual([
"public",
"private",
]);
expect(
memoriesCapturedEvent.renderEvent(v2Legacy)?.details?.map(
(detail) => detail.metadata,
),
).toEqual([
["knowledge", "public"],
["preference", "private"],
]);
});

it("omits empty extraction results from transcript presentation", () => {
Expand Down
Loading