@node-llm/orm 0.7.0 → 0.8.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.
@@ -1,410 +0,0 @@
1
- /**
2
- * AgentSession - Wraps an Agent instance with persistence capabilities.
3
- *
4
- * Follows "Code Wins" sovereignty:
5
- * - Model, Tools, Instructions come from the Agent class (code)
6
- * - Message history comes from the database
7
- * - Metadata from DB is injected as 'inputs' for dynamic resolution
8
- *
9
- * @example
10
- * ```typescript
11
- * // Create a new session
12
- * const session = await createAgentSession(prisma, llm, SupportAgent, {
13
- * metadata: { userId: "123" }
14
- * });
15
- *
16
- * // Resume a session
17
- * const session = await loadAgentSession(prisma, llm, SupportAgent, "sess_abc");
18
- *
19
- * // Agent behavior is always defined in code
20
- * const result = await session.ask("Hello");
21
- * ```
22
- */
23
- export class AgentSession {
24
- prisma;
25
- llm;
26
- AgentClass;
27
- record;
28
- agent;
29
- currentMessageId = null;
30
- tableNames;
31
- debug;
32
- constructor(prisma, llm, AgentClass, record, tableNames, agent = new AgentClass({
33
- llm,
34
- inputs: record.metadata
35
- }), debug = false) {
36
- this.prisma = prisma;
37
- this.llm = llm;
38
- this.AgentClass = AgentClass;
39
- this.record = record;
40
- this.agent = agent;
41
- this.debug = debug;
42
- this.tableNames = {
43
- agentSession: tableNames?.agentSession || "llmAgentSession",
44
- chat: tableNames?.chat || "llmChat",
45
- message: tableNames?.message || "llmMessage",
46
- toolCall: tableNames?.toolCall || "llmToolCall",
47
- request: tableNames?.request || "llmRequest"
48
- };
49
- this.registerHooks();
50
- }
51
- log(...args) {
52
- if (this.debug) {
53
- console.log(`[@node-llm/orm]`, ...args);
54
- }
55
- }
56
- /** Agent instance (for direct access if needed) */
57
- get instance() {
58
- return this.agent;
59
- }
60
- /** Session ID for persistence */
61
- get id() {
62
- return this.record.id;
63
- }
64
- /** Underlying chat ID */
65
- get chatId() {
66
- return this.record.chatId;
67
- }
68
- /** Session metadata */
69
- get metadata() {
70
- return this.record.metadata;
71
- }
72
- /** Agent class name */
73
- get agentClass() {
74
- return this.record.agentClass;
75
- }
76
- /** Model ID used by the agent */
77
- get modelId() {
78
- return this.agent.modelId;
79
- }
80
- /** Cumulative usage for this session (from agent memory) */
81
- get totalUsage() {
82
- return this.agent.totalUsage;
83
- }
84
- /** Current in-memory message history */
85
- get history() {
86
- return this.agent.history;
87
- }
88
- /**
89
- * Helper to get a typed Prisma model by its dynamic name.
90
- */
91
- getModel(name) {
92
- return getTable(this.prisma, name);
93
- }
94
- /**
95
- * Register persistence hooks on the agent.
96
- */
97
- registerHooks() {
98
- this.agent.onToolCallStart(async (tc) => {
99
- const toolCall = tc;
100
- if (!this.currentMessageId)
101
- return;
102
- const model = this.getModel(this.tableNames.toolCall);
103
- await model.create({
104
- data: {
105
- messageId: this.currentMessageId,
106
- toolCallId: toolCall.id,
107
- name: toolCall.function.name,
108
- arguments: toolCall.function.arguments
109
- }
110
- });
111
- });
112
- this.agent.onToolCallEnd(async (tc, result) => {
113
- const toolCall = tc;
114
- if (!this.currentMessageId)
115
- return;
116
- const model = this.getModel(this.tableNames.toolCall);
117
- try {
118
- await model.update({
119
- where: {
120
- messageId_toolCallId: {
121
- messageId: this.currentMessageId,
122
- toolCallId: toolCall.id
123
- }
124
- },
125
- data: {
126
- result: typeof result === "string" ? result : JSON.stringify(result)
127
- }
128
- });
129
- }
130
- catch (e) {
131
- this.log(`Failed to update tool call result: ${e}`);
132
- }
133
- });
134
- this.agent.afterResponse(async (response) => {
135
- const model = this.getModel(this.tableNames.request);
136
- await model.create({
137
- data: {
138
- chatId: this.chatId,
139
- messageId: this.currentMessageId,
140
- provider: response.provider || "unknown",
141
- model: response.model || "unknown",
142
- statusCode: 200,
143
- duration: 0,
144
- inputTokens: response.usage?.input_tokens || 0,
145
- outputTokens: response.usage?.output_tokens || 0,
146
- cost: response.usage?.cost || 0
147
- }
148
- });
149
- });
150
- }
151
- /**
152
- * Send a message and persist the conversation.
153
- */
154
- async ask(message, options = {}) {
155
- const model = this.getModel(this.tableNames.message);
156
- // Persist user message
157
- await model.create({
158
- data: { chatId: this.chatId, role: "user", content: message }
159
- });
160
- // Create placeholder for assistant message
161
- const assistantMessage = await model.create({
162
- data: { chatId: this.chatId, role: "assistant", content: null }
163
- });
164
- this.currentMessageId = assistantMessage.id;
165
- try {
166
- // Merge turn-level inputs with session metadata
167
- const inputs = { ...this.record.metadata, ...options.inputs };
168
- // Get response from agent (uses code-defined config + injected history)
169
- const response = await this.agent.ask(message, { ...options, inputs });
170
- // Update assistant message with response
171
- return await model.update({
172
- where: { id: assistantMessage.id },
173
- data: {
174
- content: response.content,
175
- contentRaw: JSON.stringify(response.meta),
176
- inputTokens: response.usage?.input_tokens || 0,
177
- outputTokens: response.usage?.output_tokens || 0,
178
- thinkingText: response.thinking?.text || null,
179
- thinkingSignature: response.thinking?.signature || null,
180
- thinkingTokens: response.thinking?.tokens || null,
181
- modelId: response.model || null,
182
- provider: response.provider || null
183
- }
184
- });
185
- }
186
- catch (error) {
187
- // Clean up placeholder on error
188
- await model.delete({ where: { id: assistantMessage.id } });
189
- throw error;
190
- }
191
- }
192
- /**
193
- * Stream a response and persist the conversation.
194
- */
195
- async *askStream(message, options = {}) {
196
- const model = this.getModel(this.tableNames.message);
197
- // Persist user message
198
- await model.create({
199
- data: { chatId: this.chatId, role: "user", content: message }
200
- });
201
- // Create placeholder for assistant message
202
- const assistantMessage = await model.create({
203
- data: { chatId: this.chatId, role: "assistant", content: null }
204
- });
205
- this.currentMessageId = assistantMessage.id;
206
- try {
207
- // Merge turn-level inputs with session metadata
208
- const inputs = { ...this.record.metadata, ...options.inputs };
209
- const stream = this.agent.stream(message, { ...options, inputs });
210
- let fullContent = "";
211
- let lastChunk = null;
212
- for await (const chunk of stream) {
213
- fullContent += chunk.content;
214
- lastChunk = chunk;
215
- yield chunk;
216
- }
217
- // Final update with accumulated result
218
- return await model.update({
219
- where: { id: assistantMessage.id },
220
- data: {
221
- content: fullContent,
222
- inputTokens: lastChunk?.usage?.input_tokens || 0,
223
- outputTokens: lastChunk?.usage?.output_tokens || 0,
224
- thinkingText: lastChunk?.thinking?.text || null,
225
- thinkingSignature: lastChunk?.thinking?.signature || null,
226
- thinkingTokens: lastChunk?.thinking?.tokens || null,
227
- modelId: lastChunk?.metadata?.model || null,
228
- provider: lastChunk?.metadata?.provider || null
229
- }
230
- });
231
- }
232
- catch (error) {
233
- await model.delete({ where: { id: assistantMessage.id } });
234
- throw error;
235
- }
236
- }
237
- /**
238
- * Returns a usage summary for this chat session.
239
- */
240
- async stats() {
241
- const requestModel = getTable(this.prisma, this.tableNames.request);
242
- const aggregate = await requestModel.aggregate({
243
- where: { chatId: this.chatId },
244
- _sum: {
245
- inputTokens: true,
246
- outputTokens: true,
247
- cost: true
248
- }
249
- });
250
- return {
251
- input_tokens: Number(aggregate._sum.inputTokens || 0),
252
- output_tokens: Number(aggregate._sum.outputTokens || 0),
253
- total_tokens: Number((aggregate._sum.inputTokens || 0) + (aggregate._sum.outputTokens || 0)),
254
- cost: Number(aggregate._sum.cost || 0)
255
- };
256
- }
257
- /**
258
- * Add a tool to the session (turn-level).
259
- */
260
- withTool(tool) {
261
- this.agent.use(tool);
262
- return this;
263
- }
264
- /**
265
- * Add instructions to the session (turn-level).
266
- */
267
- withInstructions(instructions, options) {
268
- this.agent.withInstructions(instructions, options);
269
- return this;
270
- }
271
- /**
272
- * Returns the current full message history for this session.
273
- */
274
- async messages() {
275
- const model = this.getModel(this.tableNames.message);
276
- return await model.findMany({
277
- where: { chatId: this.chatId },
278
- orderBy: { createdAt: "asc" }
279
- });
280
- }
281
- /**
282
- * Delete the entire session and its history.
283
- */
284
- async delete() {
285
- const chatTable = this.getModel(this.tableNames.chat);
286
- await chatTable.delete({ where: { id: this.chatId } });
287
- // AgentSession record is deleted via Cascade from LlmChat
288
- }
289
- /**
290
- * Update session metadata and re-resolve agent configuration.
291
- */
292
- async updateMetadata(metadata) {
293
- const sessionTable = this.getModel(this.tableNames.agentSession);
294
- const newMetadata = { ...this.record.metadata, ...metadata };
295
- await sessionTable.update({
296
- where: { id: this.id },
297
- data: { metadata: newMetadata }
298
- });
299
- this.record.metadata = newMetadata;
300
- // Apply changes to the underlying agent immediately
301
- // resolveLazyConfig is private, so we need a cast or make it protected.
302
- // Given we are in the same package, we can cast.
303
- this.agent.resolveLazyConfig(newMetadata);
304
- }
305
- }
306
- /**
307
- * Creates a new agent session and its persistent chat record.
308
- */
309
- export async function createAgentSession(prisma, llm, AgentClass, options = {}) {
310
- const tableNames = {
311
- agentSession: options.tableNames?.agentSession || "llmAgentSession",
312
- chat: options.tableNames?.chat || "llmChat",
313
- message: options.tableNames?.message || "llmMessage"
314
- };
315
- if (options.debug) {
316
- console.log(`[@node-llm/orm] createAgentSession: agentClass=${AgentClass.name}`);
317
- }
318
- // 1. Create underlying LlmChat record
319
- const chatTable = getTable(prisma, tableNames.chat);
320
- const chatRecord = (await chatTable.create({
321
- data: {
322
- model: options.model || AgentClass.model || null,
323
- provider: options.provider || null,
324
- instructions: options.instructions ||
325
- (typeof AgentClass.instructions === "string" ? AgentClass.instructions : null),
326
- metadata: null // Runtime metadata goes in Chat, session context in AgentSession
327
- }
328
- }));
329
- // 2. Create AgentSession record
330
- const sessionTable = getTable(prisma, tableNames.agentSession);
331
- const sessionRecord = (await sessionTable.create({
332
- data: {
333
- agentClass: AgentClass.name,
334
- chatId: chatRecord.id,
335
- metadata: options.metadata || null
336
- }
337
- }));
338
- // 3. Instantiate Agent with overrides
339
- const agent = new AgentClass({
340
- llm,
341
- inputs: sessionRecord.metadata,
342
- model: options.model,
343
- provider: options.provider,
344
- instructions: options.instructions,
345
- maxToolCalls: options.maxToolCalls
346
- });
347
- return new AgentSession(prisma, llm, AgentClass, sessionRecord, options.tableNames, agent, options.debug);
348
- }
349
- /**
350
- * Loads an existing agent session and re-instantiates the agent with history.
351
- */
352
- export async function loadAgentSession(prisma, llm, AgentClass, sessionId, options = {}) {
353
- const tableNames = {
354
- agentSession: options.tableNames?.agentSession || "llmAgentSession",
355
- chat: options.tableNames?.chat || "llmChat",
356
- message: options.tableNames?.message || "llmMessage"
357
- };
358
- if (options.debug) {
359
- console.log(`[@node-llm/orm] loadAgentSession: id=${sessionId}`);
360
- }
361
- // 1. Find session record
362
- const sessionTable = getTable(prisma, tableNames.agentSession);
363
- const sessionRecord = (await sessionTable.findUnique({
364
- where: { id: sessionId }
365
- }));
366
- if (!sessionRecord) {
367
- return null;
368
- }
369
- // 1.5. Validate Agent Class (Code Wins Sovereignty)
370
- if (sessionRecord.agentClass !== AgentClass.name) {
371
- throw new Error(`Agent class mismatch: Session "${sessionId}" was created for "${sessionRecord.agentClass}", but is being loaded with "${AgentClass.name}".`);
372
- }
373
- // 2. Load message history
374
- const messageTable = getTable(prisma, tableNames.message);
375
- const messages = (await messageTable.findMany({
376
- where: { chatId: sessionRecord.chatId },
377
- orderBy: { createdAt: "asc" }
378
- }));
379
- // 3. Convert DB messages to NodeLLM Message format
380
- const history = messages.map((m) => ({
381
- role: m.role,
382
- content: m.content || ""
383
- }));
384
- // 4. Instantiate agent with injected history, LLM, AND metadata (as inputs)
385
- // "Code Wins" - model, tools, instructions come from AgentClass
386
- // Metadata from DB handles the lazy resolution of behavior
387
- const agent = new AgentClass({
388
- llm,
389
- messages: history,
390
- inputs: sessionRecord.metadata
391
- });
392
- return new AgentSession(prisma, llm, AgentClass, sessionRecord, options.tableNames, agent, options.debug);
393
- }
394
- /**
395
- * Dynamic helper to access Prisma models by name.
396
- * Handles both case-sensitive and case-insensitive lookups for flexibility.
397
- */
398
- function getTable(prisma, tableName) {
399
- const p = prisma;
400
- // 1. Direct match
401
- const table = p[tableName];
402
- if (table)
403
- return table;
404
- // 2. Case-insensitive match
405
- const keys = Object.keys(prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
406
- const match = keys.find((k) => k.toLowerCase() === tableName.toLowerCase());
407
- if (match && p[match])
408
- return p[match];
409
- throw new Error(`[@node-llm/orm] Prisma table "${tableName}" not found. Available tables: ${keys.join(", ")}`);
410
- }
@@ -1,71 +0,0 @@
1
- import type { PrismaClient } from "@prisma/client";
2
- import type { NodeLLMCore, ChatChunk, AskOptions, Usage } from "@node-llm/core";
3
- import { BaseChat, type ChatRecord, type ChatOptions } from "../../BaseChat.js";
4
- export { type ChatRecord, type ChatOptions };
5
- export interface MessageRecord {
6
- id: string;
7
- chatId: string;
8
- role: string;
9
- content: string | null;
10
- contentRaw: string | null;
11
- reasoning: string | null;
12
- inputTokens: number | null;
13
- outputTokens: number | null;
14
- thinkingText: string | null;
15
- thinkingSignature: string | null;
16
- thinkingTokens: number | null;
17
- modelId: string | null;
18
- provider: string | null;
19
- createdAt: Date;
20
- }
21
- export interface TableNames {
22
- chat?: string;
23
- message?: string;
24
- toolCall?: string;
25
- request?: string;
26
- }
27
- /**
28
- * Prisma-based Chat Implementation.
29
- */
30
- export declare class Chat extends BaseChat {
31
- private prisma;
32
- private llm;
33
- private tables;
34
- private persistenceConfig;
35
- constructor(prisma: PrismaClient, llm: NodeLLMCore, record: ChatRecord, options?: ChatOptions, tableNames?: TableNames);
36
- /**
37
- * Internal prep for core Chat instance with persistence hooks.
38
- */
39
- private prepareCoreChat;
40
- /**
41
- * Send a message and persist the conversation.
42
- */
43
- ask(message: string, options?: AskOptions): Promise<MessageRecord>;
44
- /**
45
- * Stream a response and persist the conversation.
46
- * Yields ChatChunk objects for full visibility of thinking, content, and tools.
47
- */
48
- askStream(message: string, options?: AskOptions): AsyncGenerator<ChatChunk, MessageRecord, undefined>;
49
- /**
50
- * Get all messages for this chat.
51
- */
52
- messages(): Promise<MessageRecord[]>;
53
- /**
54
- * Returns a usage summary for this chat session.
55
- */
56
- stats(): Promise<Usage>;
57
- }
58
- /**
59
- * Convenience method to create a new chat session.
60
- */
61
- export declare function createChat<T = Record<string, any>>(prisma: any, llm: NodeLLMCore, options?: ChatOptions & {
62
- tableNames?: TableNames;
63
- } & T): Promise<Chat>;
64
- /**
65
- * Convenience method to load an existing chat session.
66
- */
67
- export declare function loadChat(prisma: any, llm: NodeLLMCore, chatId: string, options?: ChatOptions & {
68
- tableNames?: TableNames;
69
- debug?: boolean;
70
- }): Promise<Chat | null>;
71
- //# sourceMappingURL=Chat.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Chat.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/Chat.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAChF,OAAO,EAAE,QAAQ,EAAE,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhF,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,CAAC;AAE7C,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CA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