-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_store.test.ts
More file actions
293 lines (256 loc) · 9.97 KB
/
Copy pathsession_store.test.ts
File metadata and controls
293 lines (256 loc) · 9.97 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import {
AgentSessionStore,
AGENT_SYSTEM_TURN_PROMPT,
} from '../persistence/sessionStore.js';
// Characterization tests for session/turn persistence. Uses a fake AdminForth resource
// so we freeze the field mapping and the read/transform logic (previous-message windowing,
// system-turn filtering, chat-surface session id derivation) independent of a real DB.
const OPTIONS = {
sessionResource: {
resourceId: 'sessions',
idField: 'id',
titleField: 'title',
turnsField: 'turns',
askerIdField: 'asker_id',
createdAtField: 'created_at',
},
turnResource: {
resourceId: 'turns',
idField: 'id',
sessionIdField: 'session_id',
createdAtField: 'created_at',
promptField: 'prompt',
responseField: 'response',
debugField: 'debug',
},
} as any;
function buildStore(resourceImpl: (resourceId: string) => any) {
const adminforth = { resource: (resourceId: string) => resourceImpl(resourceId) };
return new AgentSessionStore(() => adminforth as any, OPTIONS);
}
describe('adminforth-agent session store', () => {
it('createNewTurn writes the field mapping and defaults response to "not_finished"', async () => {
let created: any;
const store = buildStore(() => ({
async create(record: any) {
created = record;
return { createdRecord: record };
},
}));
const id = await store.createNewTurn('s1', 'hello');
expect(created).toMatchObject({ session_id: 's1', prompt: 'hello', response: 'not_finished' });
expect(typeof created.id).toBe('string');
expect(id).toBe(created.id);
});
it('createSystemTurn stores the sentinel prompt and the system message as response', async () => {
let created: any;
const store = buildStore(() => ({
async create(record: any) {
created = record;
return { createdRecord: record };
},
}));
await store.createSystemTurn('s1', 'a system note');
expect(created.prompt).toBe(AGENT_SYSTEM_TURN_PROMPT);
expect(created.response).toBe('a system note');
expect(created.session_id).toBe('s1');
});
it('getSessionTurns maps rows to { id, prompt, response }', async () => {
const store = buildStore(() => ({
async list() {
return [
{ id: '1', prompt: 'p1', response: 'r1' },
{ id: '2', prompt: 'p2', response: 'r2' },
];
},
}));
expect(await store.getSessionTurns('s1')).toEqual([
{ id: '1', prompt: 'p1', response: 'r1' },
{ id: '2', prompt: 'p2', response: 'r2' },
]);
});
it('getPreviousUserMessages reverses the DESC window into chronological order', async () => {
const store = buildStore(() => ({
// resource is queried DESC + limit 2; return newest-first.
async list() {
return [
{ prompt: 'newest', response: 'x' },
{ prompt: 'older', response: 'y' },
];
},
}));
expect(await store.getPreviousUserMessages('s1')).toEqual([{ text: 'older' }, { text: 'newest' }]);
});
it('getPreviousUserMessages drops system turns', async () => {
const store = buildStore(() => ({
async list() {
return [
{ prompt: AGENT_SYSTEM_TURN_PROMPT, response: 'sys' },
{ prompt: 'hello', response: 'x' },
];
},
}));
// reversed -> [hello, sys], system filtered out
expect(await store.getPreviousUserMessages('s1')).toEqual([{ text: 'hello' }]);
});
it('appendSteerToCurrentTurn appends onto the latest non-system turn, skipping system notes', async () => {
// newest-first (DESC): a system note (e.g. audio) sits on top of the real running turn.
const rows: any[] = [
{ id: 'sys', prompt: AGENT_SYSTEM_TURN_PROMPT, response: 'END_AUDIO_CHAT' },
{ id: 'main', prompt: 'buy a car', response: 'not_finished' },
];
const store = buildStore(() => ({
async list() {
return rows;
},
async update(id: string, fields: any) {
Object.assign(rows.find((r: any) => r.id === id), fields);
},
}));
await store.appendSteerToCurrentTurn('s1', 'and the cheapest one');
expect(rows.find((r: any) => r.id === 'main').prompt).toBe('buy a car__adminforth_steer__:and the cheapest one');
expect(rows.find((r: any) => r.id === 'sys').prompt).toBe(AGENT_SYSTEM_TURN_PROMPT);
});
it('getAgentTurns excludes system turns and surfaces the stored checkpoint id', async () => {
const optionsWithCheckpoint = {
...OPTIONS,
turnResource: { ...OPTIONS.turnResource, checkpointIdField: 'checkpoint_id' },
} as any;
const adminforth = {
resource: () => ({
async list() {
return [
{ id: 't1', prompt: 'hello', response: 'hi', checkpoint_id: 'cp1' },
{ id: 'sys', prompt: AGENT_SYSTEM_TURN_PROMPT, response: 'note', checkpoint_id: null },
{ id: 't2', prompt: 'again', response: 'ok', checkpoint_id: 'cp2' },
];
},
}),
};
const store = new AgentSessionStore(() => adminforth as any, optionsWithCheckpoint);
expect(await store.getAgentTurns('s1')).toEqual([
{ id: 't1', prompt: 'hello', response: 'hi', checkpointId: 'cp1' },
{ id: 't2', prompt: 'again', response: 'ok', checkpointId: 'cp2' },
]);
});
it('getAgentTurns yields null checkpointId when the field is not configured', async () => {
const store = buildStore(() => ({
async list() {
return [{ id: 't1', prompt: 'hello', response: 'hi', checkpoint_id: 'cp1' }];
},
}));
expect(await store.getAgentTurns('s1')).toEqual([
{ id: 't1', prompt: 'hello', response: 'hi', checkpointId: null },
]);
});
it('editTurnAndTruncateAfter deletes later turns first, then rewrites the edited turn', async () => {
const optionsWithCheckpoint = {
...OPTIONS,
turnResource: { ...OPTIONS.turnResource, checkpointIdField: 'checkpoint_id' },
} as any;
const rows: any[] = [
{ id: 't1', prompt: 'p1', response: 'r1', checkpoint_id: 'cp1' },
{ id: 't2', prompt: 'p2', response: 'r2', checkpoint_id: 'cp2' },
{ id: 't3', prompt: 'p3', response: 'r3', checkpoint_id: 'cp3' },
];
const calls: string[] = [];
const adminforth = {
resource: () => ({
async list() {
return [...rows];
},
async delete(id: string) {
calls.push(`delete:${id}`);
const idx = rows.findIndex((r) => r.id === id);
if (idx !== -1) rows.splice(idx, 1);
},
async update(id: string, fields: any) {
calls.push(`update:${id}`);
Object.assign(rows.find((r) => r.id === id), fields);
},
}),
};
const store = new AgentSessionStore(() => adminforth as any, optionsWithCheckpoint);
await store.editTurnAndTruncateAfter({ sessionId: 's1', turnId: 't2', newPrompt: 'edited' });
// t3 (later) deleted before the edited turn is updated; t1 untouched.
expect(calls).toEqual(['delete:t3', 'update:t2']);
expect(rows.map((r) => r.id)).toEqual(['t1', 't2']);
expect(rows.find((r) => r.id === 't2')).toMatchObject({
prompt: 'edited',
response: 'not_finished',
checkpoint_id: null,
});
});
it('editTurnAndTruncateAfter throws when the turn is not in the session', async () => {
const store = buildStore(() => ({
async list() {
return [{ id: 't1', prompt: 'p1', response: 'r1' }];
},
}));
await expect(
store.editTurnAndTruncateAfter({ sessionId: 's1', turnId: 'missing', newPrompt: 'x' }),
).rejects.toThrow(/not found/);
});
it('saveTurnResponse persists checkpointId only when the field is configured and a value is provided', async () => {
const optionsWithCheckpoint = {
...OPTIONS,
turnResource: { ...OPTIONS.turnResource, checkpointIdField: 'checkpoint_id' },
} as any;
const updates: any[] = [];
const adminforth = {
resource: () => ({
async update(id: string, fields: any) {
updates.push({ id, fields });
},
}),
};
const store = new AgentSessionStore(() => adminforth as any, optionsWithCheckpoint);
await store.saveTurnResponse({ turnId: 't1', responseText: 'done', checkpointId: 'cpX' });
await store.saveTurnResponse({ turnId: 't2', responseText: 'partial' }); // undefined -> not written
expect(updates[0].fields).toMatchObject({ response: 'done', checkpoint_id: 'cpX' });
expect(updates[1].fields).toEqual({ response: 'partial' });
expect(updates[1].fields).not.toHaveProperty('checkpoint_id');
});
it('derives a deterministic chat-surface session id', () => {
const store = buildStore(() => ({}));
expect(
store.getChatSurfaceSessionId({ surface: 'telegram', externalConversationId: 'conv1' } as any),
).toBe('telegram:conv1');
});
it('getOrCreateChatSurfaceSession returns the existing session without creating', async () => {
let createCalled = false;
const store = buildStore(() => ({
async get() {
return { id: 'telegram:conv1' };
},
async create() {
createCalled = true;
},
}));
const sessionId = await store.getOrCreateChatSurfaceSession(
{ surface: 'telegram', externalConversationId: 'conv1', prompt: 'hi', externalUserId: 'u1' } as any,
{ pk: 'admin1' } as any,
);
expect(sessionId).toBe('telegram:conv1');
expect(createCalled).toBe(false);
});
it('getOrCreateChatSurfaceSession creates a new session with a truncated title', async () => {
let created: any;
const longPrompt = 'x'.repeat(120);
const store = buildStore(() => ({
async get() {
return null;
},
async create(record: any) {
created = record;
},
}));
const sessionId = await store.getOrCreateChatSurfaceSession(
{ surface: 'telegram', externalConversationId: 'conv2', prompt: longPrompt, externalUserId: 'u1' } as any,
{ pk: 'admin1' } as any,
);
expect(sessionId).toBe('telegram:conv2');
expect(created).toMatchObject({ id: 'telegram:conv2', asker_id: 'admin1' });
expect(created.title.length).toBe(40);
});
});