@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.
@@ -15,7 +15,8 @@ import {
15
15
  * We use 'any' here because PrismaClient has no index signature by default,
16
16
  * making it hard to access models dynamically by string name.
17
17
  */
18
- type GenericPrismaClient = any;
18
+ import type { ChatStore } from "../../Store.js";
19
+ import { toStore } from "./PrismaStore.js";
19
20
 
20
21
  /**
21
22
  * Record structure for the LLM Agent Session table.
@@ -62,17 +63,6 @@ export interface TableNames {
62
63
  /**
63
64
  * Internal interface for dynamic Prisma model access
64
65
  */
65
- interface PrismaModel<T = Record<string, unknown>> {
66
- create(args: { data: Record<string, unknown> }): Promise<T>;
67
- update(args: { where: { id: string }; data: Record<string, unknown> }): Promise<T>;
68
- delete(args: { where: { id: string } }): Promise<void>;
69
- findMany(args: {
70
- where: Record<string, unknown>;
71
- orderBy?: Record<string, string>;
72
- }): Promise<T[]>;
73
- findUnique(args: { where: { id: string } }): Promise<T | null>;
74
- }
75
-
76
66
  type AgentClass<T extends Agent<any, any> = Agent<any, any>> = (new (
77
67
  overrides?: Partial<AgentConfig<any> & ChatOptions>
78
68
  ) => T) & {
@@ -109,11 +99,11 @@ export class AgentSession<
109
99
  T extends Agent<I, any> = Agent<I, any>
110
100
  > {
111
101
  private currentMessageId: string | null = null;
112
- private tableNames: Required<TableNames>;
102
+ private store: ChatStore;
113
103
  private debug: boolean;
114
104
 
115
105
  constructor(
116
- private prisma: any,
106
+ prismaOrStore: unknown,
117
107
  private llm: NodeLLMCore,
118
108
  private AgentClass: AgentClass<T>,
119
109
  private record: AgentSessionRecord,
@@ -125,13 +115,7 @@ export class AgentSession<
125
115
  debug: boolean = false
126
116
  ) {
127
117
  this.debug = debug;
128
- this.tableNames = {
129
- agentSession: tableNames?.agentSession || "llmAgentSession",
130
- chat: tableNames?.chat || "llmChat",
131
- message: tableNames?.message || "llmMessage",
132
- toolCall: tableNames?.toolCall || "llmToolCall",
133
- request: tableNames?.request || "llmRequest"
134
- };
118
+ this.store = toStore(prismaOrStore, tableNames);
135
119
 
136
120
  this.registerHooks();
137
121
  }
@@ -185,10 +169,6 @@ export class AgentSession<
185
169
  /**
186
170
  * Helper to get a typed Prisma model by its dynamic name.
187
171
  */
188
- private getModel<R = Record<string, unknown>>(name: string): PrismaModel<R> {
189
- return getTable(this.prisma, name) as unknown as PrismaModel<R>;
190
- }
191
-
192
172
  /**
193
173
  * Register persistence hooks on the agent.
194
174
  */
@@ -196,32 +176,20 @@ export class AgentSession<
196
176
  this.agent.onToolCallStart(async (tc) => {
197
177
  const toolCall = tc as ToolCall;
198
178
  if (!this.currentMessageId) return;
199
- const model = this.getModel(this.tableNames.toolCall);
200
- await model.create({
201
- data: {
202
- messageId: this.currentMessageId,
203
- toolCallId: toolCall.id,
204
- name: toolCall.function.name,
205
- arguments: toolCall.function.arguments
206
- }
179
+ await this.store.createToolCall({
180
+ messageId: this.currentMessageId,
181
+ toolCallId: toolCall.id,
182
+ name: toolCall.function.name,
183
+ arguments: toolCall.function.arguments
207
184
  });
208
185
  });
209
186
 
210
187
  this.agent.onToolCallEnd(async (tc, result) => {
211
188
  const toolCall = tc as ToolCall;
212
189
  if (!this.currentMessageId) return;
213
- const model = this.getModel(this.tableNames.toolCall);
214
190
  try {
215
- await model.update({
216
- where: {
217
- messageId_toolCallId: {
218
- messageId: this.currentMessageId,
219
- toolCallId: toolCall.id
220
- }
221
- } as any,
222
- data: {
223
- result: typeof result === "string" ? result : JSON.stringify(result)
224
- }
191
+ await this.store.finishToolCall(this.currentMessageId, toolCall.id, {
192
+ result: typeof result === "string" ? result : JSON.stringify(result)
225
193
  });
226
194
  } catch (e) {
227
195
  this.log(`Failed to update tool call result: ${e}`);
@@ -229,19 +197,16 @@ export class AgentSession<
229
197
  });
230
198
 
231
199
  this.agent.afterResponse(async (response) => {
232
- const model = this.getModel(this.tableNames.request);
233
- await model.create({
234
- data: {
235
- chatId: this.chatId,
236
- messageId: this.currentMessageId,
237
- provider: response.provider || "unknown",
238
- model: response.model || "unknown",
239
- statusCode: 200,
240
- duration: 0,
241
- inputTokens: response.usage?.input_tokens || 0,
242
- outputTokens: response.usage?.output_tokens || 0,
243
- cost: response.usage?.cost || 0
244
- }
200
+ await this.store.createRequest({
201
+ chatId: this.chatId,
202
+ messageId: this.currentMessageId,
203
+ provider: response.provider || "unknown",
204
+ model: response.model || "unknown",
205
+ statusCode: 200,
206
+ duration: (response as { latency?: number }).latency || 0,
207
+ inputTokens: response.usage?.input_tokens || 0,
208
+ outputTokens: response.usage?.output_tokens || 0,
209
+ cost: response.usage?.cost || 0
245
210
  });
246
211
  });
247
212
  }
@@ -250,16 +215,14 @@ export class AgentSession<
250
215
  * Send a message and persist the conversation.
251
216
  */
252
217
  async ask(message: string, options: AskOptions & { inputs?: I } = {}): Promise<MessageRecord> {
253
- const model = this.getModel<MessageRecord>(this.tableNames.message);
254
-
255
218
  // Persist user message
256
- await model.create({
257
- data: { chatId: this.chatId, role: "user", content: message }
258
- });
219
+ await this.store.createMessage({ chatId: this.chatId, role: "user", content: message });
259
220
 
260
221
  // Create placeholder for assistant message
261
- const assistantMessage = await model.create({
262
- data: { chatId: this.chatId, role: "assistant", content: null }
222
+ const assistantMessage = await this.store.createMessage({
223
+ chatId: this.chatId,
224
+ role: "assistant",
225
+ content: null
263
226
  });
264
227
 
265
228
  this.currentMessageId = assistantMessage.id;
@@ -272,23 +235,20 @@ export class AgentSession<
272
235
  const response = await this.agent.ask(message, { ...options, inputs });
273
236
 
274
237
  // Update assistant message with response
275
- return await model.update({
276
- where: { id: assistantMessage.id },
277
- data: {
278
- content: response.content,
279
- contentRaw: JSON.stringify(response.meta),
280
- inputTokens: response.usage?.input_tokens || 0,
281
- outputTokens: response.usage?.output_tokens || 0,
282
- thinkingText: response.thinking?.text || null,
283
- thinkingSignature: response.thinking?.signature || null,
284
- thinkingTokens: response.thinking?.tokens || null,
285
- modelId: response.model || null,
286
- provider: response.provider || null
287
- }
288
- });
238
+ return (await this.store.updateMessage(assistantMessage.id, {
239
+ content: response.content,
240
+ contentRaw: JSON.stringify(response.meta),
241
+ inputTokens: response.usage?.input_tokens || 0,
242
+ outputTokens: response.usage?.output_tokens || 0,
243
+ thinkingText: response.thinking?.text || null,
244
+ thinkingSignature: response.thinking?.signature || null,
245
+ thinkingTokens: response.thinking?.tokens || null,
246
+ modelId: response.model || null,
247
+ provider: response.provider || null
248
+ })) as MessageRecord;
289
249
  } catch (error) {
290
250
  // Clean up placeholder on error
291
- await model.delete({ where: { id: assistantMessage.id } });
251
+ await this.store.deleteMessage(assistantMessage.id);
292
252
  throw error;
293
253
  }
294
254
  }
@@ -300,16 +260,14 @@ export class AgentSession<
300
260
  message: string,
301
261
  options: AskOptions & { inputs?: I } = {}
302
262
  ): AsyncGenerator<ChatChunk, MessageRecord, undefined> {
303
- const model = this.getModel<MessageRecord>(this.tableNames.message);
304
-
305
263
  // Persist user message
306
- await model.create({
307
- data: { chatId: this.chatId, role: "user", content: message }
308
- });
264
+ await this.store.createMessage({ chatId: this.chatId, role: "user", content: message });
309
265
 
310
266
  // Create placeholder for assistant message
311
- const assistantMessage = await model.create({
312
- data: { chatId: this.chatId, role: "assistant", content: null }
267
+ const assistantMessage = await this.store.createMessage({
268
+ chatId: this.chatId,
269
+ role: "assistant",
270
+ content: null
313
271
  });
314
272
 
315
273
  this.currentMessageId = assistantMessage.id;
@@ -329,21 +287,18 @@ export class AgentSession<
329
287
  }
330
288
 
331
289
  // Final update with accumulated result
332
- return await model.update({
333
- where: { id: assistantMessage.id },
334
- data: {
335
- content: fullContent,
336
- inputTokens: lastChunk?.usage?.input_tokens || 0,
337
- outputTokens: lastChunk?.usage?.output_tokens || 0,
338
- thinkingText: lastChunk?.thinking?.text || null,
339
- thinkingSignature: lastChunk?.thinking?.signature || null,
340
- thinkingTokens: lastChunk?.thinking?.tokens || null,
341
- modelId: (lastChunk?.metadata?.model as string) || null,
342
- provider: (lastChunk?.metadata?.provider as string) || null
343
- }
344
- });
290
+ return (await this.store.updateMessage(assistantMessage.id, {
291
+ content: fullContent,
292
+ inputTokens: lastChunk?.usage?.input_tokens || 0,
293
+ outputTokens: lastChunk?.usage?.output_tokens || 0,
294
+ thinkingText: lastChunk?.thinking?.text || null,
295
+ thinkingSignature: lastChunk?.thinking?.signature || null,
296
+ thinkingTokens: lastChunk?.thinking?.tokens || null,
297
+ modelId: (lastChunk?.metadata?.model as string) || null,
298
+ provider: (lastChunk?.metadata?.provider as string) || null
299
+ })) as MessageRecord;
345
300
  } catch (error) {
346
- await model.delete({ where: { id: assistantMessage.id } });
301
+ await this.store.deleteMessage(assistantMessage.id);
347
302
  throw error;
348
303
  }
349
304
  }
@@ -352,21 +307,13 @@ export class AgentSession<
352
307
  * Returns a usage summary for this chat session.
353
308
  */
354
309
  async stats(): Promise<Usage> {
355
- const requestModel = getTable(this.prisma, this.tableNames.request);
356
- const aggregate = await (requestModel as any).aggregate({
357
- where: { chatId: this.chatId },
358
- _sum: {
359
- inputTokens: true,
360
- outputTokens: true,
361
- cost: true
362
- }
363
- });
310
+ const totals = await this.store.requestTotals(this.chatId);
364
311
 
365
312
  return {
366
- input_tokens: Number(aggregate._sum.inputTokens || 0),
367
- output_tokens: Number(aggregate._sum.outputTokens || 0),
368
- total_tokens: Number((aggregate._sum.inputTokens || 0) + (aggregate._sum.outputTokens || 0)),
369
- cost: Number(aggregate._sum.cost || 0)
313
+ input_tokens: totals.inputTokens,
314
+ output_tokens: totals.outputTokens,
315
+ total_tokens: totals.inputTokens + totals.outputTokens,
316
+ cost: totals.cost
370
317
  };
371
318
  }
372
319
 
@@ -390,19 +337,14 @@ export class AgentSession<
390
337
  * Returns the current full message history for this session.
391
338
  */
392
339
  async messages(): Promise<MessageRecord[]> {
393
- const model = this.getModel<MessageRecord>(this.tableNames.message);
394
- return await model.findMany({
395
- where: { chatId: this.chatId },
396
- orderBy: { createdAt: "asc" }
397
- });
340
+ return (await this.store.listMessages(this.chatId)) as MessageRecord[];
398
341
  }
399
342
 
400
343
  /**
401
344
  * Delete the entire session and its history.
402
345
  */
403
346
  async delete(): Promise<void> {
404
- const chatTable = this.getModel(this.tableNames.chat);
405
- await chatTable.delete({ where: { id: this.chatId } });
347
+ await this.store.deleteChat(this.chatId);
406
348
  // AgentSession record is deleted via Cascade from LlmChat
407
349
  }
408
350
 
@@ -410,13 +352,9 @@ export class AgentSession<
410
352
  * Update session metadata and re-resolve agent configuration.
411
353
  */
412
354
  async updateMetadata(metadata: Partial<I>): Promise<void> {
413
- const sessionTable = this.getModel<AgentSessionRecord>(this.tableNames.agentSession);
414
355
  const newMetadata = { ...(this.record.metadata as I), ...metadata };
415
356
 
416
- await sessionTable.update({
417
- where: { id: this.id },
418
- data: { metadata: newMetadata as any }
419
- });
357
+ await this.store.updateAgentSession(this.id, { metadata: newMetadata as any });
420
358
 
421
359
  this.record.metadata = newMetadata as any;
422
360
 
@@ -438,49 +376,41 @@ export interface CreateAgentSessionOptions<I = any> {
438
376
  provider?: string;
439
377
  instructions?: string;
440
378
  maxToolCalls?: number;
379
+ /** Tools for this session, in place of the ones the agent class defines */
380
+ tools?: ChatOptions["tools"];
441
381
  }
442
382
 
443
383
  /**
444
384
  * Creates a new agent session and its persistent chat record.
445
385
  */
446
386
  export async function createAgentSession<I extends Record<string, any>, T extends Agent<I, any>>(
447
- prisma: any,
387
+ prismaOrStore: unknown,
448
388
  llm: NodeLLMCore,
449
389
  AgentClass: AgentClass<T>,
450
390
  options: CreateAgentSessionOptions<I> = {}
451
391
  ): Promise<AgentSession<I, T>> {
452
- const tableNames = {
453
- agentSession: options.tableNames?.agentSession || "llmAgentSession",
454
- chat: options.tableNames?.chat || "llmChat",
455
- message: options.tableNames?.message || "llmMessage"
456
- };
392
+ const store = toStore(prismaOrStore, options.tableNames);
457
393
 
458
394
  if (options.debug) {
459
395
  console.log(`[@node-llm/orm] createAgentSession: agentClass=${AgentClass.name}`);
460
396
  }
461
397
 
462
398
  // 1. Create underlying LlmChat record
463
- const chatTable = getTable(prisma, tableNames.chat);
464
- const chatRecord = (await chatTable.create({
465
- data: {
466
- model: options.model || AgentClass.model || null,
467
- provider: options.provider || null,
468
- instructions:
469
- options.instructions ||
470
- (typeof AgentClass.instructions === "string" ? AgentClass.instructions : null),
471
- metadata: null // Runtime metadata goes in Chat, session context in AgentSession
472
- }
473
- })) as unknown as { id: string };
399
+ const chatRecord = await store.createChat({
400
+ model: options.model || AgentClass.model || null,
401
+ provider: options.provider || null,
402
+ instructions:
403
+ options.instructions ||
404
+ (typeof AgentClass.instructions === "string" ? AgentClass.instructions : null),
405
+ metadata: null // Runtime metadata goes in Chat, session context in AgentSession
406
+ });
474
407
 
475
408
  // 2. Create AgentSession record
476
- const sessionTable = getTable(prisma, tableNames.agentSession);
477
- const sessionRecord = (await sessionTable.create({
478
- data: {
479
- agentClass: AgentClass.name,
480
- chatId: chatRecord.id,
481
- metadata: (options.metadata as any) || null
482
- }
483
- })) as unknown as AgentSessionRecord;
409
+ const sessionRecord = (await store.createAgentSession({
410
+ agentClass: AgentClass.name,
411
+ chatId: chatRecord.id,
412
+ metadata: (options.metadata as any) || null
413
+ })) as AgentSessionRecord;
484
414
 
485
415
  // 3. Instantiate Agent with overrides
486
416
  const agent = new AgentClass({
@@ -489,11 +419,12 @@ export async function createAgentSession<I extends Record<string, any>, T extend
489
419
  model: options.model,
490
420
  provider: options.provider,
491
421
  instructions: options.instructions,
492
- maxToolCalls: options.maxToolCalls
422
+ maxToolCalls: options.maxToolCalls,
423
+ ...(options.tools ? { tools: options.tools } : {})
493
424
  });
494
425
 
495
426
  return new AgentSession<I, T>(
496
- prisma,
427
+ store,
497
428
  llm,
498
429
  AgentClass,
499
430
  sessionRecord,
@@ -515,27 +446,20 @@ export interface LoadAgentSessionOptions {
515
446
  * Loads an existing agent session and re-instantiates the agent with history.
516
447
  */
517
448
  export async function loadAgentSession<I extends Record<string, any>, T extends Agent<I, any>>(
518
- prisma: any,
449
+ prismaOrStore: unknown,
519
450
  llm: NodeLLMCore,
520
451
  AgentClass: AgentClass<T>,
521
452
  sessionId: string,
522
453
  options: LoadAgentSessionOptions = {}
523
454
  ): Promise<AgentSession<I, T> | null> {
524
- const tableNames = {
525
- agentSession: options.tableNames?.agentSession || "llmAgentSession",
526
- chat: options.tableNames?.chat || "llmChat",
527
- message: options.tableNames?.message || "llmMessage"
528
- };
455
+ const store = toStore(prismaOrStore, options.tableNames);
529
456
 
530
457
  if (options.debug) {
531
458
  console.log(`[@node-llm/orm] loadAgentSession: id=${sessionId}`);
532
459
  }
533
460
 
534
461
  // 1. Find session record
535
- const sessionTable = getTable(prisma, tableNames.agentSession);
536
- const sessionRecord = (await sessionTable.findUnique({
537
- where: { id: sessionId }
538
- })) as unknown as AgentSessionRecord | null;
462
+ const sessionRecord = (await store.findAgentSession(sessionId)) as AgentSessionRecord | null;
539
463
 
540
464
  if (!sessionRecord) {
541
465
  return null;
@@ -549,11 +473,7 @@ export async function loadAgentSession<I extends Record<string, any>, T extends
549
473
  }
550
474
 
551
475
  // 2. Load message history
552
- const messageTable = getTable(prisma, tableNames.message);
553
- const messages = (await messageTable.findMany({
554
- where: { chatId: sessionRecord.chatId },
555
- orderBy: { createdAt: "asc" }
556
- })) as unknown as MessageRecord[];
476
+ const messages = (await store.listMessages(sessionRecord.chatId)) as MessageRecord[];
557
477
 
558
478
  // 3. Convert DB messages to NodeLLM Message format
559
479
  const history: Message[] = messages.map((m) => ({
@@ -571,7 +491,7 @@ export async function loadAgentSession<I extends Record<string, any>, T extends
571
491
  }) as T;
572
492
 
573
493
  return new AgentSession<I, T>(
574
- prisma,
494
+ store,
575
495
  llm,
576
496
  AgentClass,
577
497
  sessionRecord,
@@ -585,20 +505,3 @@ export async function loadAgentSession<I extends Record<string, any>, T extends
585
505
  * Dynamic helper to access Prisma models by name.
586
506
  * Handles both case-sensitive and case-insensitive lookups for flexibility.
587
507
  */
588
- function getTable(prisma: GenericPrismaClient, tableName: string): PrismaModel {
589
- const p = prisma as unknown as Record<string, PrismaModel>;
590
-
591
- // 1. Direct match
592
- const table = p[tableName];
593
- if (table) return table;
594
-
595
- // 2. Case-insensitive match
596
- const keys = Object.keys(prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
597
- const match = keys.find((k) => k.toLowerCase() === tableName.toLowerCase());
598
-
599
- if (match && p[match]) return p[match];
600
-
601
- throw new Error(
602
- `[@node-llm/orm] Prisma table "${tableName}" not found. Available tables: ${keys.join(", ")}`
603
- );
604
- }