Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | 18x 71x 71x 4x 1x 4x 35x 30x 3x 27x 30x 30x 27x 27x 1x 26x 18x 24x 12x 12x 17x 14x 12x 12x 12x 2x 7x 7x 11x 11x 5x 5x 5x 5x 5x | import { AgentMemory } from "@/objects/Agent";
import { parseServerDate } from "@/util/time";
/**
* How one of an agent's memories reads on a page.
*
* svc-ai keeps two sorts (`app/memory/kinds.py`): an **episode** — one turn,
* `Task: …\nResponse: …` — and **curated** memories kept from the person's
* own words: a fact, a preference, ongoing work. A correction replaces what
* it corrects, and a temporary memory ends on the date the person gave;
* both stay listed, for the record, and neither is recalled again.
*/
/** svc-ai's `MemoryKind`, held equal by svc-ai's `tests/memory/test_kinds.py`. */
export const MEMORY_KIND_LABELS = {
episode: "Conversation",
fact: "Fact",
preference: "Preference",
goal: "Ongoing work",
} as const;
export type MemoryKind = keyof typeof MEMORY_KIND_LABELS;
/** A row's kind; anything unknown — a row from before kinds — is an episode. */
export function memoryKind(memory: Pick<AgentMemory, "kind">): MemoryKind {
const kind = memory.kind ?? "episode";
return kind in MEMORY_KIND_LABELS ? (kind as MemoryKind) : "episode";
}
/** What a memory says, in a line: an episode's task, a curated memory's
* statement. */
export function memoryHeadline(memory: Pick<AgentMemory, "kind" | "content">): string {
if (memoryKind(memory) !== "episode") return memory.content.trim();
const first = memory.content.split("\n")[0] ?? "";
return first.replace(/^Task:\s*/i, "").trim() || memory.content.slice(0, 120);
}
/** `MEMORY_STATE`'s vocabulary. */
export type MemoryState = "current" | "temporary" | "replaced" | "expired";
export function memoryState(
memory: Pick<AgentMemory, "superseded_at" | "expires_at">,
now: Date = new Date(),
): MemoryState {
if (memory.superseded_at) return "replaced";
if (memory.expires_at) {
return parseServerDate(memory.expires_at).getTime() <= now.getTime()
? "expired"
: "temporary";
}
return "current";
}
/** The badge text for a state, or null for a memory simply in force. */
export function memoryStateLabel(
memory: Pick<AgentMemory, "superseded_at" | "expires_at">,
now: Date = new Date(),
): string | null {
const state = memoryState(memory, now);
if (state === "replaced") return "Replaced";
Iif (state === "expired") return "Ended";
if (state === "temporary" && memory.expires_at)
return `Until ${parseServerDate(memory.expires_at).toLocaleDateString()}`;
return null;
}
/**
* Why recall chose a memory (svc-ai `recall.Recalled.reason`): a preference
* comes on every turn, anything else because it bears on the request. An
* unknown reason is shown as it was sent.
*/
export const RECALL_REASON_LABELS: Record<string, string> = {
always: "a preference, recalled every turn",
relevant: "matched the request",
};
/** Why recall chose a memory and how it scored, from svc-ai's trace only
* (never shown to a model). */
export interface RecallBasis {
/** `always` or `relevant` (`RECALL_REASON_LABELS`); empty when not sent. */
reason: string;
/** How much of the request's wording it covers (`lexical.coverage`, 0–1). */
lexical: number | null;
/** Its embedding's cosine similarity to the request's; null when either
* had no embedding. */
dense: number | null;
}
/** A memory recall sent to the model for one turn, as a trace shows it. */
export interface RecalledMemoryView {
kind: MemoryKind;
text: string;
/** The day it was recorded or last restated (ISO date), when known. */
recorded: string | null;
/** Absent on traces stored before svc-ai sent it. */
recall?: RecallBasis;
}
function score(raw: unknown): number | null {
return typeof raw === "number" && Number.isFinite(raw) ? raw : null;
}
function recallBasis(entry: {
reason?: unknown;
lexical?: unknown;
dense?: unknown;
}): RecallBasis | null {
const basis: RecallBasis = {
reason: typeof entry.reason === "string" ? entry.reason.trim() : "",
lexical: score(entry.lexical),
dense: score(entry.dense),
};
return basis.reason || basis.lexical !== null || basis.dense !== null ? basis : null;
}
/**
* One entry of `__memory_read__`'s trace frame (`result.memories`).
*
* svc-ai sends an object — `{kind, text, recorded, id, reason, lexical,
* dense}` — since memory gained kinds (svc-ai migration 28; the last three
* since recall reported its scores), and sent a bare string before; stored
* traces hold all three shapes. Rendering the object as text was React error
* #31 on every chat turn that recalled something.
*/
export function recalledMemory(raw: unknown): RecalledMemoryView {
if (typeof raw === "string") return { kind: "episode", text: raw, recorded: null };
if (raw && typeof raw === "object") {
const entry = raw as {
kind?: unknown;
text?: unknown;
recorded?: unknown;
reason?: unknown;
lexical?: unknown;
dense?: unknown;
};
const recall = recallBasis(entry);
return {
kind: memoryKind({ kind: typeof entry.kind === "string" ? entry.kind : undefined }),
text: typeof entry.text === "string" ? entry.text : "",
recorded:
typeof entry.recorded === "string" && entry.recorded ? entry.recorded : null,
...(recall ? { recall } : {}),
};
}
return { kind: "episode", text: String(raw ?? ""), recorded: null };
}
/** The label a recalled memory is shown under: its kind, and its date. */
export function recalledMemoryLabel(memory: RecalledMemoryView): string {
const kind = MEMORY_KIND_LABELS[memory.kind];
return memory.recorded ? `${kind} · ${memory.recorded}` : kind;
}
/**
* Why a recalled memory was chosen, in a line: `matched the request · words
* 0.62 · meaning 0.48`. Null for a trace stored before recall reported it.
*/
export function recallBasisLabel(memory: RecalledMemoryView): string | null {
const basis = memory.recall;
if (!basis) return null;
const parts: string[] = [];
Eif (basis.reason) parts.push(RECALL_REASON_LABELS[basis.reason] ?? basis.reason);
Eif (basis.lexical !== null) parts.push(`words ${basis.lexical.toFixed(2)}`);
if (basis.dense !== null) parts.push(`meaning ${basis.dense.toFixed(2)}`);
return parts.join(" · ");
}
|