@node-llm/orm 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +155 -0
- package/LICENSE +21 -0
- package/README.md +38 -14
- package/dist/BaseChat.d.ts +124 -0
- package/dist/BaseChat.d.ts.map +1 -0
- package/dist/BaseChat.js +182 -0
- package/dist/Store.d.ts +109 -0
- package/dist/Store.d.ts.map +1 -0
- package/dist/Store.js +14 -0
- package/dist/adapters/prisma/AgentSession.d.ts +178 -0
- package/dist/adapters/prisma/AgentSession.d.ts.map +1 -0
- package/dist/adapters/prisma/AgentSession.js +328 -0
- package/dist/adapters/prisma/Chat.d.ts +70 -0
- package/dist/adapters/prisma/Chat.d.ts.map +1 -0
- package/dist/adapters/prisma/Chat.js +301 -0
- package/dist/adapters/prisma/PrismaStore.d.ts +54 -0
- package/dist/adapters/prisma/PrismaStore.d.ts.map +1 -0
- package/dist/adapters/prisma/PrismaStore.js +97 -0
- package/dist/adapters/prisma/index.d.ts +51 -0
- package/dist/adapters/prisma/index.d.ts.map +1 -0
- package/dist/adapters/prisma/index.js +51 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +61 -0
- package/migrations/README.md +5 -4
- package/migrations/add_cached_tokens.sql +5 -0
- package/package.json +14 -12
- package/schema.prisma +1 -0
- package/src/Store.ts +135 -0
- package/src/adapters/prisma/AgentSession.ts +89 -185
- package/src/adapters/prisma/Chat.ts +95 -162
- package/src/adapters/prisma/PrismaStore.ts +156 -0
- package/src/adapters/prisma/index.ts +3 -0
- package/src/index.ts +14 -0
- package/test/CachedTokens.test.ts +40 -0
- package/test/Store.test.ts +214 -0
- package/test/docs/readme-exports.test.ts +8 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import type { PrismaClient } from "@prisma/client";
|
|
3
2
|
import type { NodeLLMCore, ChatChunk, AskOptions, Usage } from "@node-llm/core";
|
|
4
3
|
import { BaseChat, type ChatRecord, type ChatOptions } from "../../BaseChat.js";
|
|
4
|
+
import type { ChatStore } from "../../Store.js";
|
|
5
|
+
import { toStore } from "./PrismaStore.js";
|
|
5
6
|
|
|
6
7
|
export { type ChatRecord, type ChatOptions };
|
|
7
8
|
|
|
@@ -30,26 +31,22 @@ export interface TableNames {
|
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
/**
|
|
33
|
-
* Prisma
|
|
34
|
+
* A chat whose messages, tool calls and requests are recorded in a store: a Prisma client's
|
|
35
|
+
* database by default, or any ChatStore.
|
|
34
36
|
*/
|
|
35
37
|
export class Chat extends BaseChat {
|
|
36
|
-
private
|
|
38
|
+
private store: ChatStore;
|
|
37
39
|
private persistenceConfig: Required<NonNullable<ChatOptions["persistence"]>>;
|
|
38
40
|
|
|
39
41
|
constructor(
|
|
40
|
-
|
|
42
|
+
prismaOrStore: unknown,
|
|
41
43
|
private llm: NodeLLMCore,
|
|
42
44
|
record: ChatRecord,
|
|
43
45
|
options: ChatOptions = {},
|
|
44
46
|
tableNames: TableNames = {}
|
|
45
47
|
) {
|
|
46
48
|
super(record, options);
|
|
47
|
-
this.
|
|
48
|
-
chat: tableNames.chat || "llmChat",
|
|
49
|
-
message: tableNames.message || "llmMessage",
|
|
50
|
-
toolCall: tableNames.toolCall || "llmToolCall",
|
|
51
|
-
request: tableNames.request || "llmRequest"
|
|
52
|
-
};
|
|
49
|
+
this.store = toStore(prismaOrStore, tableNames);
|
|
53
50
|
|
|
54
51
|
this.persistenceConfig = {
|
|
55
52
|
toolCalls: options.persistence?.toolCalls ?? true,
|
|
@@ -91,16 +88,13 @@ export class Chat extends BaseChat {
|
|
|
91
88
|
coreChat.onToolCallStart(async (call: any) => {
|
|
92
89
|
// Only persist if toolCalls persistence is enabled
|
|
93
90
|
if (this.persistenceConfig.toolCalls) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
thought: (call as any).thought || null,
|
|
102
|
-
thoughtSignature: (call as any).thought_signature || null
|
|
103
|
-
}
|
|
91
|
+
await this.store.createToolCall({
|
|
92
|
+
messageId: assistantMessageId,
|
|
93
|
+
toolCallId: call.id,
|
|
94
|
+
name: call.function?.name || "unknown",
|
|
95
|
+
arguments: JSON.stringify(call.function?.arguments || {}),
|
|
96
|
+
thought: (call as any).thought || null,
|
|
97
|
+
thoughtSignature: (call as any).thought_signature || null
|
|
104
98
|
});
|
|
105
99
|
}
|
|
106
100
|
|
|
@@ -111,15 +105,11 @@ export class Chat extends BaseChat {
|
|
|
111
105
|
coreChat.onToolCallEnd(async (call: any, result: any) => {
|
|
112
106
|
// Only persist if toolCalls persistence is enabled
|
|
113
107
|
if (this.persistenceConfig.toolCalls) {
|
|
114
|
-
const toolCallModel = this.tables.toolCall;
|
|
115
108
|
const resString = typeof result === "string" ? result : JSON.stringify(result);
|
|
116
109
|
|
|
117
|
-
await
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
result: resString,
|
|
121
|
-
thought: (call as any).thought || null
|
|
122
|
-
}
|
|
110
|
+
await this.store.finishToolCall(assistantMessageId, call.id, {
|
|
111
|
+
result: resString,
|
|
112
|
+
thought: (call as any).thought || null
|
|
123
113
|
});
|
|
124
114
|
}
|
|
125
115
|
|
|
@@ -140,19 +130,17 @@ export class Chat extends BaseChat {
|
|
|
140
130
|
|
|
141
131
|
// Only persist if requests persistence is enabled
|
|
142
132
|
if (this.persistenceConfig.requests) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
cost: finalResp.usage?.cost || 0
|
|
155
|
-
}
|
|
133
|
+
await this.store.createRequest({
|
|
134
|
+
chatId: this.id,
|
|
135
|
+
messageId: assistantMessageId,
|
|
136
|
+
provider: finalResp.provider || provider || "unknown",
|
|
137
|
+
model: finalResp.model || model || "unknown",
|
|
138
|
+
statusCode: 200,
|
|
139
|
+
duration: finalResp.latency || 0,
|
|
140
|
+
inputTokens: finalResp.usage?.input_tokens || 0,
|
|
141
|
+
outputTokens: finalResp.usage?.output_tokens || 0,
|
|
142
|
+
cachedTokens: finalResp.usage?.cached_tokens,
|
|
143
|
+
cost: finalResp.usage?.cost || 0
|
|
156
144
|
});
|
|
157
145
|
}
|
|
158
146
|
|
|
@@ -216,19 +204,21 @@ export class Chat extends BaseChat {
|
|
|
216
204
|
* Send a message and persist the conversation.
|
|
217
205
|
*/
|
|
218
206
|
async ask(message: string, options: AskOptions = {}): Promise<MessageRecord> {
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
207
|
+
const userMessage = await this.store.createMessage({
|
|
208
|
+
chatId: this.id,
|
|
209
|
+
role: "user",
|
|
210
|
+
content: message
|
|
222
211
|
});
|
|
223
212
|
|
|
224
|
-
const assistantMessage = await
|
|
225
|
-
|
|
213
|
+
const assistantMessage = await this.store.createMessage({
|
|
214
|
+
chatId: this.id,
|
|
215
|
+
role: "assistant",
|
|
216
|
+
content: null
|
|
226
217
|
});
|
|
227
218
|
|
|
228
219
|
try {
|
|
229
|
-
const historyRecords = await (this.
|
|
230
|
-
|
|
231
|
-
orderBy: { createdAt: "asc" }
|
|
220
|
+
const historyRecords = await this.store.listMessages(this.id, {
|
|
221
|
+
excludeIds: [userMessage.id, assistantMessage.id]
|
|
232
222
|
});
|
|
233
223
|
|
|
234
224
|
const history = historyRecords.map((m: any) => ({
|
|
@@ -239,24 +229,21 @@ export class Chat extends BaseChat {
|
|
|
239
229
|
const coreChat = await this.prepareCoreChat(history, assistantMessage!.id);
|
|
240
230
|
const response = await coreChat.ask(message, options);
|
|
241
231
|
|
|
242
|
-
return await
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
provider: response.provider || null
|
|
254
|
-
}
|
|
255
|
-
});
|
|
232
|
+
return (await this.store.updateMessage(assistantMessage.id, {
|
|
233
|
+
content: response.content,
|
|
234
|
+
contentRaw: JSON.stringify(response.meta),
|
|
235
|
+
inputTokens: response.usage?.input_tokens || 0,
|
|
236
|
+
outputTokens: response.usage?.output_tokens || 0,
|
|
237
|
+
thinkingText: response.thinking?.text || null,
|
|
238
|
+
thinkingSignature: response.thinking?.signature || null,
|
|
239
|
+
thinkingTokens: response.thinking?.tokens || null,
|
|
240
|
+
modelId: response.model || null,
|
|
241
|
+
provider: response.provider || null
|
|
242
|
+
})) as MessageRecord;
|
|
256
243
|
} catch (error) {
|
|
257
244
|
// Delete the empty assistant message, but keep the user message
|
|
258
245
|
// so there's a record of what was asked (useful for debugging)
|
|
259
|
-
await
|
|
246
|
+
await this.store.deleteMessage(assistantMessage.id);
|
|
260
247
|
throw error;
|
|
261
248
|
}
|
|
262
249
|
}
|
|
@@ -269,19 +256,21 @@ export class Chat extends BaseChat {
|
|
|
269
256
|
message: string,
|
|
270
257
|
options: AskOptions = {}
|
|
271
258
|
): AsyncGenerator<ChatChunk, MessageRecord, undefined> {
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
|
|
259
|
+
const userMessage = await this.store.createMessage({
|
|
260
|
+
chatId: this.id,
|
|
261
|
+
role: "user",
|
|
262
|
+
content: message
|
|
275
263
|
});
|
|
276
264
|
|
|
277
|
-
const assistantMessage = await
|
|
278
|
-
|
|
265
|
+
const assistantMessage = await this.store.createMessage({
|
|
266
|
+
chatId: this.id,
|
|
267
|
+
role: "assistant",
|
|
268
|
+
content: null
|
|
279
269
|
});
|
|
280
270
|
|
|
281
271
|
try {
|
|
282
|
-
const historyRecords = await (this.
|
|
283
|
-
|
|
284
|
-
orderBy: { createdAt: "asc" }
|
|
272
|
+
const historyRecords = await this.store.listMessages(this.id, {
|
|
273
|
+
excludeIds: [userMessage.id, assistantMessage.id]
|
|
285
274
|
});
|
|
286
275
|
|
|
287
276
|
const history = historyRecords.map((m: any) => ({
|
|
@@ -313,24 +302,21 @@ export class Chat extends BaseChat {
|
|
|
313
302
|
}
|
|
314
303
|
}
|
|
315
304
|
|
|
316
|
-
return await
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
provider: this.localOptions.provider || this.record.provider || null
|
|
328
|
-
}
|
|
329
|
-
});
|
|
305
|
+
return (await this.store.updateMessage(assistantMessage.id, {
|
|
306
|
+
content: fullContent,
|
|
307
|
+
contentRaw: JSON.stringify(metadata),
|
|
308
|
+
inputTokens: metadata.input_tokens || 0,
|
|
309
|
+
outputTokens: metadata.output_tokens || 0,
|
|
310
|
+
thinkingText: metadata.thinking?.text || null,
|
|
311
|
+
thinkingSignature: metadata.thinking?.signature || null,
|
|
312
|
+
thinkingTokens: metadata.thinking?.tokens || null,
|
|
313
|
+
modelId: this.localOptions.model || this.record.model || null,
|
|
314
|
+
provider: this.localOptions.provider || this.record.provider || null
|
|
315
|
+
})) as MessageRecord;
|
|
330
316
|
} catch (error) {
|
|
331
317
|
// Delete the empty assistant message, but keep the user message
|
|
332
318
|
// so there's a record of what was asked (useful for debugging)
|
|
333
|
-
await
|
|
319
|
+
await this.store.deleteMessage(assistantMessage.id);
|
|
334
320
|
throw error;
|
|
335
321
|
}
|
|
336
322
|
}
|
|
@@ -339,69 +325,32 @@ export class Chat extends BaseChat {
|
|
|
339
325
|
* Get all messages for this chat.
|
|
340
326
|
*/
|
|
341
327
|
async messages(): Promise<MessageRecord[]> {
|
|
342
|
-
|
|
343
|
-
return await (this.prisma as any)[messageModel].findMany({
|
|
344
|
-
where: { chatId: this.id },
|
|
345
|
-
orderBy: { createdAt: "asc" }
|
|
346
|
-
});
|
|
328
|
+
return (await this.store.listMessages(this.id)) as MessageRecord[];
|
|
347
329
|
}
|
|
348
330
|
|
|
349
331
|
/**
|
|
350
332
|
* Returns a usage summary for this chat session.
|
|
351
333
|
*/
|
|
352
334
|
async stats(): Promise<Usage> {
|
|
353
|
-
const
|
|
354
|
-
const aggregate = await (this.prisma as any)[requestModel].aggregate({
|
|
355
|
-
where: { chatId: this.id },
|
|
356
|
-
_sum: {
|
|
357
|
-
inputTokens: true,
|
|
358
|
-
outputTokens: true,
|
|
359
|
-
cost: true
|
|
360
|
-
}
|
|
361
|
-
});
|
|
335
|
+
const totals = await this.store.requestTotals(this.id);
|
|
362
336
|
|
|
363
337
|
return {
|
|
364
|
-
input_tokens:
|
|
365
|
-
output_tokens:
|
|
366
|
-
total_tokens:
|
|
367
|
-
cost:
|
|
338
|
+
input_tokens: totals.inputTokens,
|
|
339
|
+
output_tokens: totals.outputTokens,
|
|
340
|
+
total_tokens: totals.inputTokens + totals.outputTokens,
|
|
341
|
+
cost: totals.cost
|
|
368
342
|
};
|
|
369
343
|
}
|
|
370
344
|
}
|
|
371
345
|
|
|
372
346
|
/**
|
|
373
|
-
*
|
|
374
|
-
* Prisma usually camelCases model names (e.g., AssistantChat -> assistantChat),
|
|
375
|
-
* but mapping can vary based on configuration.
|
|
376
|
-
*/
|
|
377
|
-
function getTable(prisma: any, tableName: string): any {
|
|
378
|
-
if (prisma[tableName]) return prisma[tableName];
|
|
379
|
-
|
|
380
|
-
// Try case-insensitive match if not found directly
|
|
381
|
-
const keys = Object.keys(prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
|
|
382
|
-
const match = keys.find((k) => k.toLowerCase() === tableName.toLowerCase());
|
|
383
|
-
|
|
384
|
-
if (match) return prisma[match];
|
|
385
|
-
|
|
386
|
-
// If still not found, search for the model name if it's different from the property name
|
|
387
|
-
// but for now, we'll just throw a clear error
|
|
388
|
-
throw new Error(
|
|
389
|
-
`[@node-llm/orm] Prisma table "${tableName}" not found. Available tables: ${keys.join(", ")}`
|
|
390
|
-
);
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* Convenience method to create a new chat session.
|
|
347
|
+
* Creates a new chat session, recorded through a Prisma client or any ChatStore.
|
|
395
348
|
*/
|
|
396
349
|
export async function createChat<T = Record<string, any>>(
|
|
397
|
-
|
|
350
|
+
prismaOrStore: unknown,
|
|
398
351
|
llm: NodeLLMCore,
|
|
399
352
|
options: ChatOptions & { tableNames?: TableNames } & T = {} as any
|
|
400
353
|
): Promise<Chat> {
|
|
401
|
-
const chatTable = options.tableNames?.chat || "llmChat";
|
|
402
|
-
|
|
403
|
-
// Extract known options so we don't double-pass them or pass them incorrectly
|
|
404
|
-
// runtime options should NOT be persisted to DB
|
|
405
354
|
const {
|
|
406
355
|
model,
|
|
407
356
|
provider,
|
|
@@ -421,50 +370,34 @@ export async function createChat<T = Record<string, any>>(
|
|
|
421
370
|
...extras
|
|
422
371
|
} = options;
|
|
423
372
|
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
console.log(
|
|
427
|
-
`[@node-llm/orm] createChat: table=${chatTable}, availableTables=${keys.join(", ")}`
|
|
428
|
-
);
|
|
429
|
-
}
|
|
373
|
+
const store = toStore(prismaOrStore, options.tableNames);
|
|
374
|
+
if (options.debug) console.log(`[@node-llm/orm] createChat: store=${store.constructor.name}`);
|
|
430
375
|
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
metadata: metadata ?? null,
|
|
438
|
-
...(extras as any)
|
|
439
|
-
}
|
|
376
|
+
const record = await store.createChat({
|
|
377
|
+
model,
|
|
378
|
+
provider,
|
|
379
|
+
instructions,
|
|
380
|
+
metadata: metadata ?? null,
|
|
381
|
+
...(extras as any)
|
|
440
382
|
});
|
|
441
383
|
|
|
442
|
-
return new Chat(
|
|
384
|
+
return new Chat(store, llm, record, options, options.tableNames);
|
|
443
385
|
}
|
|
444
386
|
|
|
445
387
|
/**
|
|
446
|
-
*
|
|
388
|
+
* Loads an existing chat session from a Prisma client or any ChatStore.
|
|
447
389
|
*/
|
|
448
390
|
export async function loadChat(
|
|
449
|
-
|
|
391
|
+
prismaOrStore: unknown,
|
|
450
392
|
llm: NodeLLMCore,
|
|
451
393
|
chatId: string,
|
|
452
394
|
options: ChatOptions & { tableNames?: TableNames; debug?: boolean } = {}
|
|
453
395
|
): Promise<Chat | null> {
|
|
454
|
-
const
|
|
455
|
-
|
|
456
|
-
if (options.debug) {
|
|
457
|
-
const keys = Object.keys(prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
|
|
458
|
-
console.log(`[@node-llm/orm] loadChat: table=${chatTable}, availableTables=${keys.join(", ")}`);
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
const table = getTable(prisma, chatTable);
|
|
462
|
-
const record = await table.findUnique({
|
|
463
|
-
where: { id: chatId }
|
|
464
|
-
});
|
|
396
|
+
const store = toStore(prismaOrStore, options.tableNames);
|
|
397
|
+
if (options.debug) console.log(`[@node-llm/orm] loadChat: store=${store.constructor.name}`);
|
|
465
398
|
|
|
399
|
+
const record = await store.findChat(chatId);
|
|
466
400
|
if (!record) return null;
|
|
467
401
|
|
|
468
|
-
|
|
469
|
-
return new Chat(prisma, llm, record, options, options.tableNames);
|
|
402
|
+
return new Chat(store, llm, record, options, options.tableNames);
|
|
470
403
|
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import type {
|
|
3
|
+
ChatStore,
|
|
4
|
+
MessageUpdate,
|
|
5
|
+
NewChat,
|
|
6
|
+
NewRequest,
|
|
7
|
+
NewToolCall,
|
|
8
|
+
RequestTotals,
|
|
9
|
+
StoredAgentSession,
|
|
10
|
+
StoredChat,
|
|
11
|
+
StoredMessage
|
|
12
|
+
} from "../../Store.js";
|
|
13
|
+
import { isChatStore } from "../../Store.js";
|
|
14
|
+
|
|
15
|
+
export interface TableNames {
|
|
16
|
+
agentSession?: string;
|
|
17
|
+
chat?: string;
|
|
18
|
+
message?: string;
|
|
19
|
+
toolCall?: string;
|
|
20
|
+
request?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Keeps chats in a database through a Prisma client, in the tables of the reference schema or
|
|
25
|
+
* the ones named in tableNames.
|
|
26
|
+
*/
|
|
27
|
+
export class PrismaStore implements ChatStore {
|
|
28
|
+
private tables: Required<TableNames>;
|
|
29
|
+
|
|
30
|
+
constructor(
|
|
31
|
+
private prisma: any,
|
|
32
|
+
tableNames: TableNames = {}
|
|
33
|
+
) {
|
|
34
|
+
this.tables = {
|
|
35
|
+
agentSession: tableNames.agentSession || "llmAgentSession",
|
|
36
|
+
chat: tableNames.chat || "llmChat",
|
|
37
|
+
message: tableNames.message || "llmMessage",
|
|
38
|
+
toolCall: tableNames.toolCall || "llmToolCall",
|
|
39
|
+
request: tableNames.request || "llmRequest"
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async createChat(chat: NewChat): Promise<StoredChat> {
|
|
44
|
+
return await this.table("chat").create({ data: chat });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async findChat(id: string): Promise<StoredChat | null> {
|
|
48
|
+
return await this.table("chat").findUnique({ where: { id } });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async deleteChat(id: string): Promise<void> {
|
|
52
|
+
await this.table("chat").delete({ where: { id } });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async createMessage(message: {
|
|
56
|
+
chatId: string;
|
|
57
|
+
role: string;
|
|
58
|
+
content: string | null;
|
|
59
|
+
}): Promise<StoredMessage> {
|
|
60
|
+
return await this.table("message").create({ data: message });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async updateMessage(id: string, changes: MessageUpdate): Promise<StoredMessage> {
|
|
64
|
+
return await this.table("message").update({ where: { id }, data: changes });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async deleteMessage(id: string): Promise<void> {
|
|
68
|
+
await this.table("message").delete({ where: { id } });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async listMessages(
|
|
72
|
+
chatId: string,
|
|
73
|
+
options: { excludeIds?: string[] } = {}
|
|
74
|
+
): Promise<StoredMessage[]> {
|
|
75
|
+
const where = options.excludeIds?.length
|
|
76
|
+
? { chatId, id: { notIn: options.excludeIds } }
|
|
77
|
+
: { chatId };
|
|
78
|
+
return await this.table("message").findMany({ where, orderBy: { createdAt: "asc" } });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async createToolCall(toolCall: NewToolCall): Promise<void> {
|
|
82
|
+
await this.table("toolCall").create({ data: toolCall });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async finishToolCall(
|
|
86
|
+
messageId: string,
|
|
87
|
+
toolCallId: string,
|
|
88
|
+
finished: { result: string; thought?: string | null }
|
|
89
|
+
): Promise<void> {
|
|
90
|
+
await this.table("toolCall").update({
|
|
91
|
+
where: { messageId_toolCallId: { messageId, toolCallId } },
|
|
92
|
+
data: finished
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async createRequest(request: NewRequest): Promise<void> {
|
|
97
|
+
const table = this.table("request");
|
|
98
|
+
// A database set up before v0.10.0 has no cachedTokens column; its requests save as before.
|
|
99
|
+
const { cachedTokens, ...rest } = request;
|
|
100
|
+
const data = cachedTokens !== undefined && table.fields?.cachedTokens ? request : rest;
|
|
101
|
+
await table.create({ data });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async requestTotals(chatId: string): Promise<RequestTotals> {
|
|
105
|
+
const aggregate = await this.table("request").aggregate({
|
|
106
|
+
where: { chatId },
|
|
107
|
+
_sum: { inputTokens: true, outputTokens: true, cost: true }
|
|
108
|
+
});
|
|
109
|
+
return {
|
|
110
|
+
inputTokens: Number(aggregate._sum.inputTokens || 0),
|
|
111
|
+
outputTokens: Number(aggregate._sum.outputTokens || 0),
|
|
112
|
+
cost: Number(aggregate._sum.cost || 0)
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async createAgentSession(session: {
|
|
117
|
+
agentClass: string;
|
|
118
|
+
chatId: string;
|
|
119
|
+
metadata: Record<string, unknown> | null;
|
|
120
|
+
}): Promise<StoredAgentSession> {
|
|
121
|
+
return await this.table("agentSession").create({ data: session });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async findAgentSession(id: string): Promise<StoredAgentSession | null> {
|
|
125
|
+
return await this.table("agentSession").findUnique({ where: { id } });
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async updateAgentSession(
|
|
129
|
+
id: string,
|
|
130
|
+
changes: { metadata: Record<string, unknown> }
|
|
131
|
+
): Promise<void> {
|
|
132
|
+
await this.table("agentSession").update({ where: { id }, data: changes });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The Prisma model for a table. Prisma usually camelCases model names (AssistantChat becomes
|
|
137
|
+
* assistantChat), but mapping can vary, so a case-insensitive match is accepted too.
|
|
138
|
+
*/
|
|
139
|
+
private table(kind: keyof TableNames): any {
|
|
140
|
+
const name = this.tables[kind];
|
|
141
|
+
if (this.prisma[name]) return this.prisma[name];
|
|
142
|
+
|
|
143
|
+
const keys = Object.keys(this.prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
|
|
144
|
+
const match = keys.find((k) => k.toLowerCase() === name.toLowerCase());
|
|
145
|
+
if (match) return this.prisma[match];
|
|
146
|
+
|
|
147
|
+
throw new Error(
|
|
148
|
+
`[@node-llm/orm] Prisma table "${name}" not found. Available tables: ${keys.join(", ")}`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** A ChatStore as given, or a Prisma client wrapped in a PrismaStore. */
|
|
154
|
+
export function toStore(prismaOrStore: unknown, tableNames?: TableNames): ChatStore {
|
|
155
|
+
return isChatStore(prismaOrStore) ? prismaOrStore : new PrismaStore(prismaOrStore, tableNames);
|
|
156
|
+
}
|
|
@@ -48,6 +48,9 @@
|
|
|
48
48
|
export { Chat, createChat, loadChat } from "./Chat.js";
|
|
49
49
|
export type { ChatRecord, MessageRecord, ChatOptions } from "./Chat.js";
|
|
50
50
|
|
|
51
|
+
// Stores: where chats are recorded
|
|
52
|
+
export { PrismaStore, toStore } from "./PrismaStore.js";
|
|
53
|
+
|
|
51
54
|
// AgentSession API
|
|
52
55
|
export { AgentSession, createAgentSession, loadAgentSession } from "./AgentSession.js";
|
|
53
56
|
export type {
|
package/src/index.ts
CHANGED
|
@@ -58,3 +58,17 @@
|
|
|
58
58
|
|
|
59
59
|
// Re-export Prisma adapter as default
|
|
60
60
|
export * from "./adapters/prisma/index.js";
|
|
61
|
+
|
|
62
|
+
// Custom stores
|
|
63
|
+
export { isChatStore } from "./Store.js";
|
|
64
|
+
export type {
|
|
65
|
+
ChatStore,
|
|
66
|
+
StoredChat,
|
|
67
|
+
StoredMessage,
|
|
68
|
+
StoredAgentSession,
|
|
69
|
+
NewChat,
|
|
70
|
+
NewToolCall,
|
|
71
|
+
NewRequest,
|
|
72
|
+
MessageUpdate,
|
|
73
|
+
RequestTotals
|
|
74
|
+
} from "./Store.js";
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { describe, it, expect, vi } from "vitest";
|
|
3
|
+
import { PrismaStore } from "../src/adapters/prisma/PrismaStore.js";
|
|
4
|
+
|
|
5
|
+
const request = {
|
|
6
|
+
chatId: "chat-1",
|
|
7
|
+
messageId: "message-1",
|
|
8
|
+
provider: "azure",
|
|
9
|
+
model: "gpt-6-luna",
|
|
10
|
+
statusCode: 200,
|
|
11
|
+
duration: 900,
|
|
12
|
+
inputTokens: 12000,
|
|
13
|
+
outputTokens: 80,
|
|
14
|
+
cachedTokens: 9000,
|
|
15
|
+
cost: 0.001
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const prismaWith = (fields: Record<string, unknown> | undefined) => {
|
|
19
|
+
const create = vi.fn();
|
|
20
|
+
return { client: { llmRequest: { fields, create } } as any, create };
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe("cached tokens on requests", () => {
|
|
24
|
+
it("saves the tokens a provider served from its cache when the database has the column", async () => {
|
|
25
|
+
const { client, create } = prismaWith({ inputTokens: {}, cachedTokens: {} });
|
|
26
|
+
|
|
27
|
+
await new PrismaStore(client).createRequest(request);
|
|
28
|
+
|
|
29
|
+
expect(create).toHaveBeenCalledWith({ data: request });
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("saves requests as before on a database set up before the column existed", async () => {
|
|
33
|
+
const { client, create } = prismaWith({ inputTokens: {} });
|
|
34
|
+
|
|
35
|
+
await new PrismaStore(client).createRequest(request);
|
|
36
|
+
|
|
37
|
+
const { cachedTokens: _cached, ...before } = request;
|
|
38
|
+
expect(create).toHaveBeenCalledWith({ data: before });
|
|
39
|
+
});
|
|
40
|
+
});
|