-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionStore.ts
More file actions
250 lines (221 loc) · 9.32 KB
/
Copy pathsessionStore.ts
File metadata and controls
250 lines (221 loc) · 9.32 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
import type { AdminUser, IAdminForth } from "adminforth";
import { Filters, Sorts } from "adminforth";
import { randomUUID } from "crypto";
import type { ChatSurfaceIncomingMessage } from "adminforth";
import type { PreviousUserMessage } from "../domain/languageDetect.js";
import type { PluginOptions } from "../types.js";
export const AGENT_SYSTEM_TURN_PROMPT = "__adminforth_system_message__";
export const STEER_PROMPT_MARKER = "__adminforth_steer__:";
export class AgentSessionStore {
constructor(
private getAdminforth: () => IAdminForth,
private options: PluginOptions,
) {}
async createNewTurn(sessionId: string, prompt: string, response?: string) {
const turnId = randomUUID();
const turnRecord = {
[this.options.turnResource.idField]: turnId,
[this.options.turnResource.sessionIdField]: sessionId,
[this.options.turnResource.promptField]: prompt,
[this.options.turnResource.responseField]: response ?? "not_finished",
};
const newTurn = await this.getAdminforth().resource(this.options.turnResource.resourceId).create(turnRecord);
return newTurn.createdRecord[this.options.turnResource.idField];
}
async createSystemTurn(sessionId: string, systemMessage: string) {
const turnId = randomUUID();
const turnRecord = {
[this.options.turnResource.idField]: turnId,
[this.options.turnResource.sessionIdField]: sessionId,
[this.options.turnResource.promptField]: AGENT_SYSTEM_TURN_PROMPT,
[this.options.turnResource.responseField]: systemMessage,
};
const newTurn = await this.getAdminforth().resource(this.options.turnResource.resourceId).create(turnRecord);
return newTurn.createdRecord[this.options.turnResource.idField];
}
/**
* Append a mid-turn steer to the current (running) turn's prompt instead of creating a
* new record. Targets the latest non-system turn so a steer attaches to the real
* conversation turn, never an audio/system note.
*/
async appendSteerToCurrentTurn(sessionId: string, steerText: string) {
const turns = await this.getAdminforth().resource(this.options.turnResource.resourceId).list(
[Filters.EQ(this.options.turnResource.sessionIdField, sessionId)],
undefined,
undefined,
[Sorts.DESC(this.options.turnResource.createdAtField)]
);
const turn = turns.find(
(candidate) => candidate[this.options.turnResource.promptField] !== AGENT_SYSTEM_TURN_PROMPT
);
if (!turn) {
return;
}
const currentPrompt = turn[this.options.turnResource.promptField] ?? "";
await this.getAdminforth().resource(this.options.turnResource.resourceId).update(
turn[this.options.turnResource.idField],
{
[this.options.turnResource.promptField]: `${currentPrompt}${STEER_PROMPT_MARKER}${steerText}`,
},
);
}
async getSessionTurns(sessionId: string) {
const turns = await this.getAdminforth().resource(this.options.turnResource.resourceId).list(
[Filters.EQ(this.options.turnResource.sessionIdField, sessionId)],
undefined,
undefined,
[Sorts.ASC(this.options.turnResource.createdAtField), Sorts.ASC(this.options.turnResource.idField)]
);
return turns.map(turn => ({
id: turn[this.options.turnResource.idField],
prompt: turn[this.options.turnResource.promptField],
response: turn[this.options.turnResource.responseField],
}));
}
/**
* Ordered non-system (real conversation) turns with their stored checkpoint id.
* Used by message editing to locate the target turn and its previous turn's
* fork point. Stable order (`createdAt ASC, id ASC`) so positions never drift.
*/
async getAgentTurns(sessionId: string) {
const checkpointIdField = this.options.turnResource.checkpointIdField;
const turns = await this.getAdminforth().resource(this.options.turnResource.resourceId).list(
[Filters.EQ(this.options.turnResource.sessionIdField, sessionId)],
undefined,
undefined,
[Sorts.ASC(this.options.turnResource.createdAtField), Sorts.ASC(this.options.turnResource.idField)]
);
return turns
.filter((turn) => turn[this.options.turnResource.promptField] !== AGENT_SYSTEM_TURN_PROMPT)
.map((turn) => ({
id: turn[this.options.turnResource.idField],
prompt: turn[this.options.turnResource.promptField],
response: turn[this.options.turnResource.responseField],
checkpointId: checkpointIdField
? (turn[checkpointIdField] ? String(turn[checkpointIdField]) : null)
: null,
}));
}
/**
* Apply a message edit and branch: replace the edited turn's prompt and truncate
* everything after it. Deletes later turns FIRST, then updates the edited turn, so
* an interruption can only leave a consistent "truncated, edit-not-yet-applied"
* state (recoverable by retry) — never "edited but stale later turns remain".
* (AdminForth's resource API has no transactions, hence the deliberate ordering.)
*/
async editTurnAndTruncateAfter(input: {
sessionId: string;
turnId: string;
newPrompt: string;
}): Promise<void> {
const r = this.options.turnResource;
const resource = this.getAdminforth().resource(r.resourceId);
const turns = await resource.list(
[Filters.EQ(r.sessionIdField, input.sessionId)],
undefined,
undefined,
[Sorts.ASC(r.createdAtField), Sorts.ASC(r.idField)],
);
const targetIndex = turns.findIndex((turn) => turn[r.idField] === input.turnId);
if (targetIndex === -1) {
throw new Error(`Turn "${input.turnId}" not found in session "${input.sessionId}".`);
}
const laterTurns = turns.slice(targetIndex + 1);
for (const turn of laterTurns) {
await resource.delete(turn[r.idField]);
}
const updates: Record<string, unknown> = {
[r.promptField]: input.newPrompt,
[r.responseField]: "not_finished",
};
if (r.checkpointIdField) {
updates[r.checkpointIdField] = null;
}
await resource.update(input.turnId, updates);
}
async getPreviousUserMessages(sessionId: string) {
const turns = await this.getAdminforth().resource(this.options.turnResource.resourceId).list(
[Filters.EQ(this.options.turnResource.sessionIdField, sessionId)],
2,
undefined,
[Sorts.DESC(this.options.turnResource.createdAtField)]
);
return turns
.reverse()
.filter((turn) => turn[this.options.turnResource.promptField] !== AGENT_SYSTEM_TURN_PROMPT)
.map((turn): PreviousUserMessage => ({
text: turn[this.options.turnResource.promptField],
}));
}
async getLatestTurn(sessionId: string) {
const turns = await this.getAdminforth().resource(this.options.turnResource.resourceId).list(
[Filters.EQ(this.options.turnResource.sessionIdField, sessionId)],
1,
undefined,
[Sorts.DESC(this.options.turnResource.createdAtField)]
);
return turns[0];
}
getChatSurfaceSessionId(incoming: ChatSurfaceIncomingMessage) {
return `${incoming.surface}:${incoming.externalConversationId}`;
}
async getOrCreateChatSurfaceSession(
incoming: ChatSurfaceIncomingMessage,
adminUser: AdminUser,
) {
const sessionId = this.getChatSurfaceSessionId(incoming);
const sessionResource = this.getAdminforth().resource(this.options.sessionResource.resourceId);
const session = await sessionResource.get(
[Filters.EQ(this.options.sessionResource.idField, sessionId)]
);
if (session) {
return sessionId;
}
await sessionResource.create({
[this.options.sessionResource.idField]: sessionId,
[this.options.sessionResource.titleField]: incoming.prompt.slice(0, 40) || "New Session",
[this.options.sessionResource.askerIdField]: adminUser.pk,
});
return sessionId;
}
async touchSession(sessionId: string) {
await this.getAdminforth().resource(this.options.sessionResource.resourceId).update(sessionId, {
[this.options.sessionResource.createdAtField]: new Date().toISOString(),
});
}
async saveTurnResponse(input: {
turnId: string;
responseText: string;
debugHistory?: unknown;
/**
* Tip checkpoint id for this turn. Persisted only when provided AND the field is
* configured — so aborted/failed turns (which pass undefined) never overwrite it
* with a stale value.
*/
checkpointId?: string | null;
}) {
const turnUpdates: Record<string, unknown> = {
[this.options.turnResource.responseField]: input.responseText,
};
if (this.options.turnResource.debugField) {
turnUpdates[this.options.turnResource.debugField] = input.debugHistory;
}
if (this.options.turnResource.checkpointIdField && input.checkpointId !== undefined) {
turnUpdates[this.options.turnResource.checkpointIdField] = input.checkpointId;
}
await this.getAdminforth()
.resource(this.options.turnResource.resourceId)
.update(input.turnId, turnUpdates);
}
async getResumeState(sessionId: string): Promise<{ turnId: string; initialResponse: string }> {
const latestTurn = await this.getLatestTurn(sessionId);
if (!latestTurn) {
throw new Error(`No agent turn found for session "${sessionId}".`);
}
const response = latestTurn[this.options.turnResource.responseField];
return {
turnId: latestTurn[this.options.turnResource.idField],
initialResponse: response === "not_finished" ? "" : String(response),
};
}
}