Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

memory-capture.ts

52 lines1.6 KB

1/**
2 * Per-testId capture for the `memory` mode of `/middleware-test`. A recorder
3 * middleware placed AFTER `memoryMiddleware` records the config the model
4 * actually sees (post-injection system prompts + tool names) plus a flag for
5 * each deferred `save`. The page fetches it via
6 * `GET /api/middleware-test?testId=...&kind=memory` and surfaces it in the DOM
7 * for the Playwright spec. Mirrors `phase-capture.ts`.
8 */
9​
10export interface MemoryConfigRecord {
11 /** System-prompt strings present in the config at `init` (post memory injection). */
12 systemPrompts: Array<string>
13 /** Tool names present in the config at `init` (post memory injection). */
14 toolNames: Array<string>
16​
17export interface MemoryCapture {
18 /** One entry per `onConfig(init)` observed by the recorder. */
19 configs: Array<MemoryConfigRecord>
20 /** Count of `save` calls the fake adapter observed. */
21 saveCount: number
23​
24const captures: Map<string, MemoryCapture> = new Map()
25​
26function bucketFor(captureId: string): MemoryCapture {
27 let bucket = captures.get(captureId)
28 if (!bucket) {
29 bucket = { configs: [], saveCount: 0 }
30 captures.set(captureId, bucket)
31 }
32 return bucket
34​
35export function resetMemoryCapture(captureId: string): void {
36 captures.set(captureId, { configs: [], saveCount: 0 })
38​
39export function getMemoryCapture(captureId: string): MemoryCapture {
40 return bucketFor(captureId)
42​
43export function recordMemoryConfig(
44 captureId: string,
45 record: MemoryConfigRecord,
46): void {
47 bucketFor(captureId).configs.push(record)
49​
50export function recordMemorySave(captureId: string): void {
51 bucketFor(captureId).saveCount += 1