@node-llm/orm 0.7.1 → 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 +186 -0
- package/README.md +38 -14
- package/dist/BaseChat.d.ts +39 -1
- package/dist/BaseChat.d.ts.map +1 -1
- package/dist/BaseChat.js +56 -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 +13 -6
- package/dist/adapters/prisma/AgentSession.d.ts.map +1 -1
- package/dist/adapters/prisma/AgentSession.js +83 -166
- package/dist/adapters/prisma/Chat.d.ts +8 -9
- package/dist/adapters/prisma/Chat.d.ts.map +1 -1
- package/dist/adapters/prisma/Chat.js +128 -155
- 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 +1 -0
- package/dist/adapters/prisma/index.d.ts.map +1 -1
- package/dist/adapters/prisma/index.js +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/package.json +8 -3
- package/schema.prisma +8 -7
- package/src/BaseChat.ts +70 -1
- package/src/Store.ts +133 -0
- package/src/adapters/prisma/AgentSession.ts +88 -185
- package/src/adapters/prisma/Chat.ts +130 -163
- package/src/adapters/prisma/PrismaStore.ts +152 -0
- package/src/adapters/prisma/index.ts +3 -0
- package/src/index.ts +14 -0
- package/test/AgentSession.test.ts +12 -4
- package/test/Store.test.ts +214 -0
- package/test/ToolChoiceAndSchema.test.ts +180 -0
- package/test/ToolExecutionOptions.test.ts +205 -0
- package/test/docs/readme-exports.test.ts +8 -0
- package/tsconfig.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -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,
|
|
@@ -66,9 +63,14 @@ export class Chat extends BaseChat {
|
|
|
66
63
|
|
|
67
64
|
const llmInstance = provider ? this.llm.withProvider(provider as string) : this.llm;
|
|
68
65
|
|
|
66
|
+
// `schema` needs core's own normalization (raw Zod schema -> Schema
|
|
67
|
+
// instance), so it's applied via withSchema() below rather than spread
|
|
68
|
+
// as a raw ChatOptions field.
|
|
69
|
+
const { schema, ...chatOptions } = this.localOptions;
|
|
70
|
+
|
|
69
71
|
const coreChat = llmInstance.chat(model || undefined, {
|
|
70
72
|
messages: history,
|
|
71
|
-
...
|
|
73
|
+
...chatOptions,
|
|
72
74
|
middlewares: this.customMiddlewares
|
|
73
75
|
}) as any;
|
|
74
76
|
|
|
@@ -77,21 +79,22 @@ export class Chat extends BaseChat {
|
|
|
77
79
|
coreChat.withTools(this.customTools);
|
|
78
80
|
}
|
|
79
81
|
|
|
82
|
+
if (schema) {
|
|
83
|
+
coreChat.withSchema(schema);
|
|
84
|
+
}
|
|
85
|
+
|
|
80
86
|
// --- Persistence Hooks ---
|
|
81
87
|
|
|
82
88
|
coreChat.onToolCallStart(async (call: any) => {
|
|
83
89
|
// Only persist if toolCalls persistence is enabled
|
|
84
90
|
if (this.persistenceConfig.toolCalls) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
thought: (call as any).thought || null,
|
|
93
|
-
thoughtSignature: (call as any).thought_signature || null
|
|
94
|
-
}
|
|
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
|
|
95
98
|
});
|
|
96
99
|
}
|
|
97
100
|
|
|
@@ -102,15 +105,11 @@ export class Chat extends BaseChat {
|
|
|
102
105
|
coreChat.onToolCallEnd(async (call: any, result: any) => {
|
|
103
106
|
// Only persist if toolCalls persistence is enabled
|
|
104
107
|
if (this.persistenceConfig.toolCalls) {
|
|
105
|
-
const toolCallModel = this.tables.toolCall;
|
|
106
108
|
const resString = typeof result === "string" ? result : JSON.stringify(result);
|
|
107
109
|
|
|
108
|
-
await
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
result: resString,
|
|
112
|
-
thought: (call as any).thought || null
|
|
113
|
-
}
|
|
110
|
+
await this.store.finishToolCall(assistantMessageId, call.id, {
|
|
111
|
+
result: resString,
|
|
112
|
+
thought: (call as any).thought || null
|
|
114
113
|
});
|
|
115
114
|
}
|
|
116
115
|
|
|
@@ -131,19 +130,16 @@ export class Chat extends BaseChat {
|
|
|
131
130
|
|
|
132
131
|
// Only persist if requests persistence is enabled
|
|
133
132
|
if (this.persistenceConfig.requests) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
outputTokens: finalResp.usage?.output_tokens || 0,
|
|
145
|
-
cost: finalResp.usage?.cost || 0
|
|
146
|
-
}
|
|
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
|
|
147
143
|
});
|
|
148
144
|
}
|
|
149
145
|
|
|
@@ -174,6 +170,32 @@ export class Chat extends BaseChat {
|
|
|
174
170
|
});
|
|
175
171
|
}
|
|
176
172
|
|
|
173
|
+
if (this.userHooks.onToolCallError.length > 0) {
|
|
174
|
+
coreChat.onToolCallError(async (call: any, error: Error) => {
|
|
175
|
+
// First handler to return a directive (STOP/CONTINUE/RETRY) wins,
|
|
176
|
+
// but every handler still runs so all of them get to observe it.
|
|
177
|
+
let directive: any;
|
|
178
|
+
for (const h of this.userHooks.onToolCallError) {
|
|
179
|
+
const value = await h(call, error);
|
|
180
|
+
if (value !== undefined && directive === undefined) {
|
|
181
|
+
directive = value;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return directive;
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (this.userHooks.onConfirmToolCall.length > 0) {
|
|
189
|
+
coreChat.onConfirmToolCall(async (call: any) => {
|
|
190
|
+
// Every registered handler must approve for the call to proceed.
|
|
191
|
+
for (const h of this.userHooks.onConfirmToolCall) {
|
|
192
|
+
const approved = await h(call);
|
|
193
|
+
if (approved === false) return false;
|
|
194
|
+
}
|
|
195
|
+
return true;
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
177
199
|
return coreChat;
|
|
178
200
|
}
|
|
179
201
|
|
|
@@ -181,19 +203,21 @@ export class Chat extends BaseChat {
|
|
|
181
203
|
* Send a message and persist the conversation.
|
|
182
204
|
*/
|
|
183
205
|
async ask(message: string, options: AskOptions = {}): Promise<MessageRecord> {
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
206
|
+
const userMessage = await this.store.createMessage({
|
|
207
|
+
chatId: this.id,
|
|
208
|
+
role: "user",
|
|
209
|
+
content: message
|
|
187
210
|
});
|
|
188
211
|
|
|
189
|
-
const assistantMessage = await
|
|
190
|
-
|
|
212
|
+
const assistantMessage = await this.store.createMessage({
|
|
213
|
+
chatId: this.id,
|
|
214
|
+
role: "assistant",
|
|
215
|
+
content: null
|
|
191
216
|
});
|
|
192
217
|
|
|
193
218
|
try {
|
|
194
|
-
const historyRecords = await (this.
|
|
195
|
-
|
|
196
|
-
orderBy: { createdAt: "asc" }
|
|
219
|
+
const historyRecords = await this.store.listMessages(this.id, {
|
|
220
|
+
excludeIds: [userMessage.id, assistantMessage.id]
|
|
197
221
|
});
|
|
198
222
|
|
|
199
223
|
const history = historyRecords.map((m: any) => ({
|
|
@@ -204,24 +228,21 @@ export class Chat extends BaseChat {
|
|
|
204
228
|
const coreChat = await this.prepareCoreChat(history, assistantMessage!.id);
|
|
205
229
|
const response = await coreChat.ask(message, options);
|
|
206
230
|
|
|
207
|
-
return await
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
provider: response.provider || null
|
|
219
|
-
}
|
|
220
|
-
});
|
|
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;
|
|
221
242
|
} catch (error) {
|
|
222
243
|
// Delete the empty assistant message, but keep the user message
|
|
223
244
|
// so there's a record of what was asked (useful for debugging)
|
|
224
|
-
await
|
|
245
|
+
await this.store.deleteMessage(assistantMessage.id);
|
|
225
246
|
throw error;
|
|
226
247
|
}
|
|
227
248
|
}
|
|
@@ -234,19 +255,21 @@ export class Chat extends BaseChat {
|
|
|
234
255
|
message: string,
|
|
235
256
|
options: AskOptions = {}
|
|
236
257
|
): AsyncGenerator<ChatChunk, MessageRecord, undefined> {
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
258
|
+
const userMessage = await this.store.createMessage({
|
|
259
|
+
chatId: this.id,
|
|
260
|
+
role: "user",
|
|
261
|
+
content: message
|
|
240
262
|
});
|
|
241
263
|
|
|
242
|
-
const assistantMessage = await
|
|
243
|
-
|
|
264
|
+
const assistantMessage = await this.store.createMessage({
|
|
265
|
+
chatId: this.id,
|
|
266
|
+
role: "assistant",
|
|
267
|
+
content: null
|
|
244
268
|
});
|
|
245
269
|
|
|
246
270
|
try {
|
|
247
|
-
const historyRecords = await (this.
|
|
248
|
-
|
|
249
|
-
orderBy: { createdAt: "asc" }
|
|
271
|
+
const historyRecords = await this.store.listMessages(this.id, {
|
|
272
|
+
excludeIds: [userMessage.id, assistantMessage.id]
|
|
250
273
|
});
|
|
251
274
|
|
|
252
275
|
const history = historyRecords.map((m: any) => ({
|
|
@@ -278,24 +301,21 @@ export class Chat extends BaseChat {
|
|
|
278
301
|
}
|
|
279
302
|
}
|
|
280
303
|
|
|
281
|
-
return await
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
provider: this.localOptions.provider || this.record.provider || null
|
|
293
|
-
}
|
|
294
|
-
});
|
|
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;
|
|
295
315
|
} catch (error) {
|
|
296
316
|
// Delete the empty assistant message, but keep the user message
|
|
297
317
|
// so there's a record of what was asked (useful for debugging)
|
|
298
|
-
await
|
|
318
|
+
await this.store.deleteMessage(assistantMessage.id);
|
|
299
319
|
throw error;
|
|
300
320
|
}
|
|
301
321
|
}
|
|
@@ -304,69 +324,32 @@ export class Chat extends BaseChat {
|
|
|
304
324
|
* Get all messages for this chat.
|
|
305
325
|
*/
|
|
306
326
|
async messages(): Promise<MessageRecord[]> {
|
|
307
|
-
|
|
308
|
-
return await (this.prisma as any)[messageModel].findMany({
|
|
309
|
-
where: { chatId: this.id },
|
|
310
|
-
orderBy: { createdAt: "asc" }
|
|
311
|
-
});
|
|
327
|
+
return (await this.store.listMessages(this.id)) as MessageRecord[];
|
|
312
328
|
}
|
|
313
329
|
|
|
314
330
|
/**
|
|
315
331
|
* Returns a usage summary for this chat session.
|
|
316
332
|
*/
|
|
317
333
|
async stats(): Promise<Usage> {
|
|
318
|
-
const
|
|
319
|
-
const aggregate = await (this.prisma as any)[requestModel].aggregate({
|
|
320
|
-
where: { chatId: this.id },
|
|
321
|
-
_sum: {
|
|
322
|
-
inputTokens: true,
|
|
323
|
-
outputTokens: true,
|
|
324
|
-
cost: true
|
|
325
|
-
}
|
|
326
|
-
});
|
|
334
|
+
const totals = await this.store.requestTotals(this.id);
|
|
327
335
|
|
|
328
336
|
return {
|
|
329
|
-
input_tokens:
|
|
330
|
-
output_tokens:
|
|
331
|
-
total_tokens:
|
|
332
|
-
cost:
|
|
337
|
+
input_tokens: totals.inputTokens,
|
|
338
|
+
output_tokens: totals.outputTokens,
|
|
339
|
+
total_tokens: totals.inputTokens + totals.outputTokens,
|
|
340
|
+
cost: totals.cost
|
|
333
341
|
};
|
|
334
342
|
}
|
|
335
343
|
}
|
|
336
344
|
|
|
337
345
|
/**
|
|
338
|
-
*
|
|
339
|
-
* Prisma usually camelCases model names (e.g., AssistantChat -> assistantChat),
|
|
340
|
-
* but mapping can vary based on configuration.
|
|
341
|
-
*/
|
|
342
|
-
function getTable(prisma: any, tableName: string): any {
|
|
343
|
-
if (prisma[tableName]) return prisma[tableName];
|
|
344
|
-
|
|
345
|
-
// Try case-insensitive match if not found directly
|
|
346
|
-
const keys = Object.keys(prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
|
|
347
|
-
const match = keys.find((k) => k.toLowerCase() === tableName.toLowerCase());
|
|
348
|
-
|
|
349
|
-
if (match) return prisma[match];
|
|
350
|
-
|
|
351
|
-
// If still not found, search for the model name if it's different from the property name
|
|
352
|
-
// but for now, we'll just throw a clear error
|
|
353
|
-
throw new Error(
|
|
354
|
-
`[@node-llm/orm] Prisma table "${tableName}" not found. Available tables: ${keys.join(", ")}`
|
|
355
|
-
);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
/**
|
|
359
|
-
* Convenience method to create a new chat session.
|
|
346
|
+
* Creates a new chat session, recorded through a Prisma client or any ChatStore.
|
|
360
347
|
*/
|
|
361
348
|
export async function createChat<T = Record<string, any>>(
|
|
362
|
-
|
|
349
|
+
prismaOrStore: unknown,
|
|
363
350
|
llm: NodeLLMCore,
|
|
364
351
|
options: ChatOptions & { tableNames?: TableNames } & T = {} as any
|
|
365
352
|
): Promise<Chat> {
|
|
366
|
-
const chatTable = options.tableNames?.chat || "llmChat";
|
|
367
|
-
|
|
368
|
-
// Extract known options so we don't double-pass them or pass them incorrectly
|
|
369
|
-
// runtime options should NOT be persisted to DB
|
|
370
353
|
const {
|
|
371
354
|
model,
|
|
372
355
|
provider,
|
|
@@ -386,50 +369,34 @@ export async function createChat<T = Record<string, any>>(
|
|
|
386
369
|
...extras
|
|
387
370
|
} = options;
|
|
388
371
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
console.log(
|
|
392
|
-
`[@node-llm/orm] createChat: table=${chatTable}, availableTables=${keys.join(", ")}`
|
|
393
|
-
);
|
|
394
|
-
}
|
|
372
|
+
const store = toStore(prismaOrStore, options.tableNames);
|
|
373
|
+
if (options.debug) console.log(`[@node-llm/orm] createChat: store=${store.constructor.name}`);
|
|
395
374
|
|
|
396
|
-
const
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
metadata: metadata ?? null,
|
|
403
|
-
...(extras as any)
|
|
404
|
-
}
|
|
375
|
+
const record = await store.createChat({
|
|
376
|
+
model,
|
|
377
|
+
provider,
|
|
378
|
+
instructions,
|
|
379
|
+
metadata: metadata ?? null,
|
|
380
|
+
...(extras as any)
|
|
405
381
|
});
|
|
406
382
|
|
|
407
|
-
return new Chat(
|
|
383
|
+
return new Chat(store, llm, record, options, options.tableNames);
|
|
408
384
|
}
|
|
409
385
|
|
|
410
386
|
/**
|
|
411
|
-
*
|
|
387
|
+
* Loads an existing chat session from a Prisma client or any ChatStore.
|
|
412
388
|
*/
|
|
413
389
|
export async function loadChat(
|
|
414
|
-
|
|
390
|
+
prismaOrStore: unknown,
|
|
415
391
|
llm: NodeLLMCore,
|
|
416
392
|
chatId: string,
|
|
417
393
|
options: ChatOptions & { tableNames?: TableNames; debug?: boolean } = {}
|
|
418
394
|
): Promise<Chat | null> {
|
|
419
|
-
const
|
|
420
|
-
|
|
421
|
-
if (options.debug) {
|
|
422
|
-
const keys = Object.keys(prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
|
|
423
|
-
console.log(`[@node-llm/orm] loadChat: table=${chatTable}, availableTables=${keys.join(", ")}`);
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
const table = getTable(prisma, chatTable);
|
|
427
|
-
const record = await table.findUnique({
|
|
428
|
-
where: { id: chatId }
|
|
429
|
-
});
|
|
395
|
+
const store = toStore(prismaOrStore, options.tableNames);
|
|
396
|
+
if (options.debug) console.log(`[@node-llm/orm] loadChat: store=${store.constructor.name}`);
|
|
430
397
|
|
|
398
|
+
const record = await store.findChat(chatId);
|
|
431
399
|
if (!record) return null;
|
|
432
400
|
|
|
433
|
-
|
|
434
|
-
return new Chat(prisma, llm, record, options, options.tableNames);
|
|
401
|
+
return new Chat(store, llm, record, options, options.tableNames);
|
|
435
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";
|
|
@@ -76,9 +76,14 @@ describe("AgentSession", () => {
|
|
|
76
76
|
metadata: { userId: "user-1" }
|
|
77
77
|
});
|
|
78
78
|
|
|
79
|
-
const session = await createAgentSession
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
const session = await createAgentSession<Record<string, any>, TestAgent>(
|
|
80
|
+
mockPrisma as any,
|
|
81
|
+
mockLlm,
|
|
82
|
+
TestAgent,
|
|
83
|
+
{
|
|
84
|
+
metadata: { userId: "user-1" }
|
|
85
|
+
}
|
|
86
|
+
);
|
|
82
87
|
|
|
83
88
|
// Verify DB calls
|
|
84
89
|
expect(mockPrisma.llmChat.create).toHaveBeenCalledWith(
|
|
@@ -211,7 +216,10 @@ describe("AgentSession", () => {
|
|
|
211
216
|
|
|
212
217
|
class LazyTestAgent extends Agent<TestInputs> {
|
|
213
218
|
static model = "gpt-4-lazy";
|
|
214
|
-
|
|
219
|
+
// Static members can't reference the class's own generic type parameter
|
|
220
|
+
// in TypeScript, so this matches the base class's static signature
|
|
221
|
+
// (Record<string, any>) and narrows internally.
|
|
222
|
+
static instructions = (i: Record<string, any>) => `Hello ${(i as TestInputs).userName}`;
|
|
215
223
|
}
|
|
216
224
|
|
|
217
225
|
it("injects metadata as inputs for lazy resolution during load", async () => {
|