@node-llm/orm 0.8.0 → 0.9.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 +149 -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 +107 -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 +327 -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 +300 -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 +93 -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/package.json +14 -12
- package/src/Store.ts +133 -0
- package/src/adapters/prisma/AgentSession.ts +88 -185
- package/src/adapters/prisma/Chat.ts +94 -162
- package/src/adapters/prisma/PrismaStore.ts +152 -0
- package/src/adapters/prisma/index.ts +3 -0
- package/src/index.ts +14 -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,16 @@ 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
|
-
outputTokens: finalResp.usage?.output_tokens || 0,
|
|
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
|
+
cost: finalResp.usage?.cost || 0
|
|
156
143
|
});
|
|
157
144
|
}
|
|
158
145
|
|
|
@@ -216,19 +203,21 @@ export class Chat extends BaseChat {
|
|
|
216
203
|
* Send a message and persist the conversation.
|
|
217
204
|
*/
|
|
218
205
|
async ask(message: string, options: AskOptions = {}): Promise<MessageRecord> {
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
206
|
+
const userMessage = await this.store.createMessage({
|
|
207
|
+
chatId: this.id,
|
|
208
|
+
role: "user",
|
|
209
|
+
content: message
|
|
222
210
|
});
|
|
223
211
|
|
|
224
|
-
const assistantMessage = await
|
|
225
|
-
|
|
212
|
+
const assistantMessage = await this.store.createMessage({
|
|
213
|
+
chatId: this.id,
|
|
214
|
+
role: "assistant",
|
|
215
|
+
content: null
|
|
226
216
|
});
|
|
227
217
|
|
|
228
218
|
try {
|
|
229
|
-
const historyRecords = await (this.
|
|
230
|
-
|
|
231
|
-
orderBy: { createdAt: "asc" }
|
|
219
|
+
const historyRecords = await this.store.listMessages(this.id, {
|
|
220
|
+
excludeIds: [userMessage.id, assistantMessage.id]
|
|
232
221
|
});
|
|
233
222
|
|
|
234
223
|
const history = historyRecords.map((m: any) => ({
|
|
@@ -239,24 +228,21 @@ export class Chat extends BaseChat {
|
|
|
239
228
|
const coreChat = await this.prepareCoreChat(history, assistantMessage!.id);
|
|
240
229
|
const response = await coreChat.ask(message, options);
|
|
241
230
|
|
|
242
|
-
return await
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
provider: response.provider || null
|
|
254
|
-
}
|
|
255
|
-
});
|
|
231
|
+
return (await this.store.updateMessage(assistantMessage.id, {
|
|
232
|
+
content: response.content,
|
|
233
|
+
contentRaw: JSON.stringify(response.meta),
|
|
234
|
+
inputTokens: response.usage?.input_tokens || 0,
|
|
235
|
+
outputTokens: response.usage?.output_tokens || 0,
|
|
236
|
+
thinkingText: response.thinking?.text || null,
|
|
237
|
+
thinkingSignature: response.thinking?.signature || null,
|
|
238
|
+
thinkingTokens: response.thinking?.tokens || null,
|
|
239
|
+
modelId: response.model || null,
|
|
240
|
+
provider: response.provider || null
|
|
241
|
+
})) as MessageRecord;
|
|
256
242
|
} catch (error) {
|
|
257
243
|
// Delete the empty assistant message, but keep the user message
|
|
258
244
|
// so there's a record of what was asked (useful for debugging)
|
|
259
|
-
await
|
|
245
|
+
await this.store.deleteMessage(assistantMessage.id);
|
|
260
246
|
throw error;
|
|
261
247
|
}
|
|
262
248
|
}
|
|
@@ -269,19 +255,21 @@ export class Chat extends BaseChat {
|
|
|
269
255
|
message: string,
|
|
270
256
|
options: AskOptions = {}
|
|
271
257
|
): AsyncGenerator<ChatChunk, MessageRecord, undefined> {
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
|
|
258
|
+
const userMessage = await this.store.createMessage({
|
|
259
|
+
chatId: this.id,
|
|
260
|
+
role: "user",
|
|
261
|
+
content: message
|
|
275
262
|
});
|
|
276
263
|
|
|
277
|
-
const assistantMessage = await
|
|
278
|
-
|
|
264
|
+
const assistantMessage = await this.store.createMessage({
|
|
265
|
+
chatId: this.id,
|
|
266
|
+
role: "assistant",
|
|
267
|
+
content: null
|
|
279
268
|
});
|
|
280
269
|
|
|
281
270
|
try {
|
|
282
|
-
const historyRecords = await (this.
|
|
283
|
-
|
|
284
|
-
orderBy: { createdAt: "asc" }
|
|
271
|
+
const historyRecords = await this.store.listMessages(this.id, {
|
|
272
|
+
excludeIds: [userMessage.id, assistantMessage.id]
|
|
285
273
|
});
|
|
286
274
|
|
|
287
275
|
const history = historyRecords.map((m: any) => ({
|
|
@@ -313,24 +301,21 @@ export class Chat extends BaseChat {
|
|
|
313
301
|
}
|
|
314
302
|
}
|
|
315
303
|
|
|
316
|
-
return await
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
provider: this.localOptions.provider || this.record.provider || null
|
|
328
|
-
}
|
|
329
|
-
});
|
|
304
|
+
return (await this.store.updateMessage(assistantMessage.id, {
|
|
305
|
+
content: fullContent,
|
|
306
|
+
contentRaw: JSON.stringify(metadata),
|
|
307
|
+
inputTokens: metadata.input_tokens || 0,
|
|
308
|
+
outputTokens: metadata.output_tokens || 0,
|
|
309
|
+
thinkingText: metadata.thinking?.text || null,
|
|
310
|
+
thinkingSignature: metadata.thinking?.signature || null,
|
|
311
|
+
thinkingTokens: metadata.thinking?.tokens || null,
|
|
312
|
+
modelId: this.localOptions.model || this.record.model || null,
|
|
313
|
+
provider: this.localOptions.provider || this.record.provider || null
|
|
314
|
+
})) as MessageRecord;
|
|
330
315
|
} catch (error) {
|
|
331
316
|
// Delete the empty assistant message, but keep the user message
|
|
332
317
|
// so there's a record of what was asked (useful for debugging)
|
|
333
|
-
await
|
|
318
|
+
await this.store.deleteMessage(assistantMessage.id);
|
|
334
319
|
throw error;
|
|
335
320
|
}
|
|
336
321
|
}
|
|
@@ -339,69 +324,32 @@ export class Chat extends BaseChat {
|
|
|
339
324
|
* Get all messages for this chat.
|
|
340
325
|
*/
|
|
341
326
|
async messages(): Promise<MessageRecord[]> {
|
|
342
|
-
|
|
343
|
-
return await (this.prisma as any)[messageModel].findMany({
|
|
344
|
-
where: { chatId: this.id },
|
|
345
|
-
orderBy: { createdAt: "asc" }
|
|
346
|
-
});
|
|
327
|
+
return (await this.store.listMessages(this.id)) as MessageRecord[];
|
|
347
328
|
}
|
|
348
329
|
|
|
349
330
|
/**
|
|
350
331
|
* Returns a usage summary for this chat session.
|
|
351
332
|
*/
|
|
352
333
|
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
|
-
});
|
|
334
|
+
const totals = await this.store.requestTotals(this.id);
|
|
362
335
|
|
|
363
336
|
return {
|
|
364
|
-
input_tokens:
|
|
365
|
-
output_tokens:
|
|
366
|
-
total_tokens:
|
|
367
|
-
cost:
|
|
337
|
+
input_tokens: totals.inputTokens,
|
|
338
|
+
output_tokens: totals.outputTokens,
|
|
339
|
+
total_tokens: totals.inputTokens + totals.outputTokens,
|
|
340
|
+
cost: totals.cost
|
|
368
341
|
};
|
|
369
342
|
}
|
|
370
343
|
}
|
|
371
344
|
|
|
372
345
|
/**
|
|
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.
|
|
346
|
+
* Creates a new chat session, recorded through a Prisma client or any ChatStore.
|
|
395
347
|
*/
|
|
396
348
|
export async function createChat<T = Record<string, any>>(
|
|
397
|
-
|
|
349
|
+
prismaOrStore: unknown,
|
|
398
350
|
llm: NodeLLMCore,
|
|
399
351
|
options: ChatOptions & { tableNames?: TableNames } & T = {} as any
|
|
400
352
|
): 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
353
|
const {
|
|
406
354
|
model,
|
|
407
355
|
provider,
|
|
@@ -421,50 +369,34 @@ export async function createChat<T = Record<string, any>>(
|
|
|
421
369
|
...extras
|
|
422
370
|
} = options;
|
|
423
371
|
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
console.log(
|
|
427
|
-
`[@node-llm/orm] createChat: table=${chatTable}, availableTables=${keys.join(", ")}`
|
|
428
|
-
);
|
|
429
|
-
}
|
|
372
|
+
const store = toStore(prismaOrStore, options.tableNames);
|
|
373
|
+
if (options.debug) console.log(`[@node-llm/orm] createChat: store=${store.constructor.name}`);
|
|
430
374
|
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
metadata: metadata ?? null,
|
|
438
|
-
...(extras as any)
|
|
439
|
-
}
|
|
375
|
+
const record = await store.createChat({
|
|
376
|
+
model,
|
|
377
|
+
provider,
|
|
378
|
+
instructions,
|
|
379
|
+
metadata: metadata ?? null,
|
|
380
|
+
...(extras as any)
|
|
440
381
|
});
|
|
441
382
|
|
|
442
|
-
return new Chat(
|
|
383
|
+
return new Chat(store, llm, record, options, options.tableNames);
|
|
443
384
|
}
|
|
444
385
|
|
|
445
386
|
/**
|
|
446
|
-
*
|
|
387
|
+
* Loads an existing chat session from a Prisma client or any ChatStore.
|
|
447
388
|
*/
|
|
448
389
|
export async function loadChat(
|
|
449
|
-
|
|
390
|
+
prismaOrStore: unknown,
|
|
450
391
|
llm: NodeLLMCore,
|
|
451
392
|
chatId: string,
|
|
452
393
|
options: ChatOptions & { tableNames?: TableNames; debug?: boolean } = {}
|
|
453
394
|
): 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
|
-
});
|
|
395
|
+
const store = toStore(prismaOrStore, options.tableNames);
|
|
396
|
+
if (options.debug) console.log(`[@node-llm/orm] loadChat: store=${store.constructor.name}`);
|
|
465
397
|
|
|
398
|
+
const record = await store.findChat(chatId);
|
|
466
399
|
if (!record) return null;
|
|
467
400
|
|
|
468
|
-
|
|
469
|
-
return new Chat(prisma, llm, record, options, options.tableNames);
|
|
401
|
+
return new Chat(store, llm, record, options, options.tableNames);
|
|
470
402
|
}
|
|
@@ -0,0 +1,152 @@
|
|
|
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
|
+
await this.table("request").create({ data: request });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async requestTotals(chatId: string): Promise<RequestTotals> {
|
|
101
|
+
const aggregate = await this.table("request").aggregate({
|
|
102
|
+
where: { chatId },
|
|
103
|
+
_sum: { inputTokens: true, outputTokens: true, cost: true }
|
|
104
|
+
});
|
|
105
|
+
return {
|
|
106
|
+
inputTokens: Number(aggregate._sum.inputTokens || 0),
|
|
107
|
+
outputTokens: Number(aggregate._sum.outputTokens || 0),
|
|
108
|
+
cost: Number(aggregate._sum.cost || 0)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async createAgentSession(session: {
|
|
113
|
+
agentClass: string;
|
|
114
|
+
chatId: string;
|
|
115
|
+
metadata: Record<string, unknown> | null;
|
|
116
|
+
}): Promise<StoredAgentSession> {
|
|
117
|
+
return await this.table("agentSession").create({ data: session });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async findAgentSession(id: string): Promise<StoredAgentSession | null> {
|
|
121
|
+
return await this.table("agentSession").findUnique({ where: { id } });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async updateAgentSession(
|
|
125
|
+
id: string,
|
|
126
|
+
changes: { metadata: Record<string, unknown> }
|
|
127
|
+
): Promise<void> {
|
|
128
|
+
await this.table("agentSession").update({ where: { id }, data: changes });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The Prisma model for a table. Prisma usually camelCases model names (AssistantChat becomes
|
|
133
|
+
* assistantChat), but mapping can vary, so a case-insensitive match is accepted too.
|
|
134
|
+
*/
|
|
135
|
+
private table(kind: keyof TableNames): any {
|
|
136
|
+
const name = this.tables[kind];
|
|
137
|
+
if (this.prisma[name]) return this.prisma[name];
|
|
138
|
+
|
|
139
|
+
const keys = Object.keys(this.prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
|
|
140
|
+
const match = keys.find((k) => k.toLowerCase() === name.toLowerCase());
|
|
141
|
+
if (match) return this.prisma[match];
|
|
142
|
+
|
|
143
|
+
throw new Error(
|
|
144
|
+
`[@node-llm/orm] Prisma table "${name}" not found. Available tables: ${keys.join(", ")}`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** A ChatStore as given, or a Prisma client wrapped in a PrismaStore. */
|
|
150
|
+
export function toStore(prismaOrStore: unknown, tableNames?: TableNames): ChatStore {
|
|
151
|
+
return isChatStore(prismaOrStore) ? prismaOrStore : new PrismaStore(prismaOrStore, tableNames);
|
|
152
|
+
}
|
|
@@ -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";
|