@node-llm/orm 0.7.1 → 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,327 +0,0 @@
1
- import { BaseChat } from "../../BaseChat.js";
2
- /**
3
- * Prisma-based Chat Implementation.
4
- */
5
- export class Chat extends BaseChat {
6
- prisma;
7
- llm;
8
- tables;
9
- persistenceConfig;
10
- constructor(prisma, llm, record, options = {}, tableNames = {}) {
11
- super(record, options);
12
- this.prisma = prisma;
13
- this.llm = llm;
14
- this.tables = {
15
- chat: tableNames.chat || "llmChat",
16
- message: tableNames.message || "llmMessage",
17
- toolCall: tableNames.toolCall || "llmToolCall",
18
- request: tableNames.request || "llmRequest"
19
- };
20
- this.persistenceConfig = {
21
- toolCalls: options.persistence?.toolCalls ?? true,
22
- requests: options.persistence?.requests ?? true
23
- };
24
- }
25
- /**
26
- * Internal prep for core Chat instance with persistence hooks.
27
- */
28
- async prepareCoreChat(history = [], assistantMessageId) {
29
- const provider = this.localOptions.provider || this.record.provider;
30
- const model = this.localOptions.model || this.record.model;
31
- const llmInstance = provider ? this.llm.withProvider(provider) : this.llm;
32
- const coreChat = llmInstance.chat(model || undefined, {
33
- messages: history,
34
- ...this.localOptions,
35
- middlewares: this.customMiddlewares
36
- });
37
- // Register tools
38
- if (this.customTools.length > 0) {
39
- coreChat.withTools(this.customTools);
40
- }
41
- // --- Persistence Hooks ---
42
- coreChat.onToolCallStart(async (call) => {
43
- // Only persist if toolCalls persistence is enabled
44
- if (this.persistenceConfig.toolCalls) {
45
- const toolCallModel = this.tables.toolCall;
46
- await this.prisma[toolCallModel].create({
47
- data: {
48
- messageId: assistantMessageId,
49
- toolCallId: call.id,
50
- name: call.function?.name || "unknown",
51
- arguments: JSON.stringify(call.function?.arguments || {}),
52
- thought: call.thought || null,
53
- thoughtSignature: call.thought_signature || null
54
- }
55
- });
56
- }
57
- // User hooks
58
- for (const h of this.userHooks.onToolCallStart)
59
- await h(call);
60
- });
61
- coreChat.onToolCallEnd(async (call, result) => {
62
- // Only persist if toolCalls persistence is enabled
63
- if (this.persistenceConfig.toolCalls) {
64
- const toolCallModel = this.tables.toolCall;
65
- const resString = typeof result === "string" ? result : JSON.stringify(result);
66
- await this.prisma[toolCallModel].update({
67
- where: { messageId_toolCallId: { messageId: assistantMessageId, toolCallId: call.id } },
68
- data: {
69
- result: resString,
70
- thought: call.thought || null
71
- }
72
- });
73
- }
74
- // User hooks
75
- for (const h of this.userHooks.onToolCallEnd)
76
- await h(call, result);
77
- });
78
- coreChat.afterResponse(async (finalResp) => {
79
- this.log(`Internal afterResponse triggered. Calling ${this.userHooks.afterResponse.length} user hooks.`);
80
- // User hooks
81
- for (const h of this.userHooks.afterResponse) {
82
- const modified = await h(finalResp);
83
- if (modified)
84
- finalResp = modified;
85
- }
86
- // Only persist if requests persistence is enabled
87
- if (this.persistenceConfig.requests) {
88
- const modelName = this.tables.request;
89
- await this.prisma[modelName].create({
90
- data: {
91
- chatId: this.id,
92
- messageId: assistantMessageId,
93
- provider: finalResp.provider || provider || "unknown",
94
- model: finalResp.model || model || "unknown",
95
- statusCode: 200,
96
- duration: finalResp.latency || 0,
97
- inputTokens: finalResp.usage?.input_tokens || 0,
98
- outputTokens: finalResp.usage?.output_tokens || 0,
99
- cost: finalResp.usage?.cost || 0
100
- }
101
- });
102
- }
103
- return finalResp;
104
- });
105
- // Other core hooks
106
- if (this.userHooks.onNewMessage.length > 0) {
107
- coreChat.onNewMessage(async () => {
108
- for (const h of this.userHooks.onNewMessage)
109
- await h();
110
- });
111
- }
112
- if (this.userHooks.onEndMessage.length > 0) {
113
- coreChat.onEndMessage(async (msg) => {
114
- for (const h of this.userHooks.onEndMessage)
115
- await h(msg);
116
- });
117
- }
118
- if (this.userHooks.onBeforeRequest.length > 0) {
119
- coreChat.beforeRequest(async (msgs) => {
120
- let current = msgs;
121
- for (const h of this.userHooks.onBeforeRequest) {
122
- const mod = await h(current);
123
- if (mod)
124
- current = mod;
125
- }
126
- return current;
127
- });
128
- }
129
- return coreChat;
130
- }
131
- /**
132
- * Send a message and persist the conversation.
133
- */
134
- async ask(message, options = {}) {
135
- const messageModel = this.tables.message;
136
- const userMessage = await this.prisma[messageModel].create({
137
- data: { chatId: this.id, role: "user", content: message }
138
- });
139
- const assistantMessage = await this.prisma[messageModel].create({
140
- data: { chatId: this.id, role: "assistant", content: null }
141
- });
142
- try {
143
- const historyRecords = await this.prisma[messageModel].findMany({
144
- where: { chatId: this.id, id: { notIn: [userMessage.id, assistantMessage.id] } },
145
- orderBy: { createdAt: "asc" }
146
- });
147
- const history = historyRecords.map((m) => ({
148
- role: m.role,
149
- content: m.content || ""
150
- }));
151
- const coreChat = await this.prepareCoreChat(history, assistantMessage.id);
152
- const response = await coreChat.ask(message, options);
153
- return await this.prisma[messageModel].update({
154
- where: { id: assistantMessage.id },
155
- data: {
156
- content: response.content,
157
- contentRaw: JSON.stringify(response.meta),
158
- inputTokens: response.usage?.input_tokens || 0,
159
- outputTokens: response.usage?.output_tokens || 0,
160
- thinkingText: response.thinking?.text || null,
161
- thinkingSignature: response.thinking?.signature || null,
162
- thinkingTokens: response.thinking?.tokens || null,
163
- modelId: response.model || null,
164
- provider: response.provider || null
165
- }
166
- });
167
- }
168
- catch (error) {
169
- // Delete the empty assistant message, but keep the user message
170
- // so there's a record of what was asked (useful for debugging)
171
- await this.prisma[messageModel].delete({ where: { id: assistantMessage.id } });
172
- throw error;
173
- }
174
- }
175
- /**
176
- * Stream a response and persist the conversation.
177
- * Yields ChatChunk objects for full visibility of thinking, content, and tools.
178
- */
179
- async *askStream(message, options = {}) {
180
- const messageModel = this.tables.message;
181
- const userMessage = await this.prisma[messageModel].create({
182
- data: { chatId: this.id, role: "user", content: message }
183
- });
184
- const assistantMessage = await this.prisma[messageModel].create({
185
- data: { chatId: this.id, role: "assistant", content: null }
186
- });
187
- try {
188
- const historyRecords = await this.prisma[messageModel].findMany({
189
- where: { chatId: this.id, id: { notIn: [userMessage.id, assistantMessage.id] } },
190
- orderBy: { createdAt: "asc" }
191
- });
192
- const history = historyRecords.map((m) => ({
193
- role: m.role,
194
- content: m.content || ""
195
- }));
196
- const coreChat = await this.prepareCoreChat(history, assistantMessage.id);
197
- const stream = coreChat.stream(message, options);
198
- let fullContent = "";
199
- let metadata = {};
200
- for await (const chunk of stream) {
201
- if (chunk.content) {
202
- fullContent += chunk.content;
203
- }
204
- // Yield the full chunk to the caller
205
- yield chunk;
206
- if (chunk.usage) {
207
- metadata = { ...metadata, ...chunk.usage };
208
- }
209
- if (chunk.thinking) {
210
- metadata.thinking = { ...(metadata.thinking || {}), ...chunk.thinking };
211
- if (chunk.thinking.text) {
212
- metadata.thinking.text = (metadata.thinking.text || "") + chunk.thinking.text;
213
- }
214
- }
215
- }
216
- return await this.prisma[messageModel].update({
217
- where: { id: assistantMessage.id },
218
- data: {
219
- content: fullContent,
220
- contentRaw: JSON.stringify(metadata),
221
- inputTokens: metadata.input_tokens || 0,
222
- outputTokens: metadata.output_tokens || 0,
223
- thinkingText: metadata.thinking?.text || null,
224
- thinkingSignature: metadata.thinking?.signature || null,
225
- thinkingTokens: metadata.thinking?.tokens || null,
226
- modelId: this.localOptions.model || this.record.model || null,
227
- provider: this.localOptions.provider || this.record.provider || null
228
- }
229
- });
230
- }
231
- catch (error) {
232
- // Delete the empty assistant message, but keep the user message
233
- // so there's a record of what was asked (useful for debugging)
234
- await this.prisma[messageModel].delete({ where: { id: assistantMessage.id } });
235
- throw error;
236
- }
237
- }
238
- /**
239
- * Get all messages for this chat.
240
- */
241
- async messages() {
242
- const messageModel = this.tables.message;
243
- return await this.prisma[messageModel].findMany({
244
- where: { chatId: this.id },
245
- orderBy: { createdAt: "asc" }
246
- });
247
- }
248
- /**
249
- * Returns a usage summary for this chat session.
250
- */
251
- async stats() {
252
- const requestModel = this.tables.request;
253
- const aggregate = await this.prisma[requestModel].aggregate({
254
- where: { chatId: this.id },
255
- _sum: {
256
- inputTokens: true,
257
- outputTokens: true,
258
- cost: true
259
- }
260
- });
261
- return {
262
- input_tokens: Number(aggregate._sum.inputTokens || 0),
263
- output_tokens: Number(aggregate._sum.outputTokens || 0),
264
- total_tokens: Number((aggregate._sum.inputTokens || 0) + (aggregate._sum.outputTokens || 0)),
265
- cost: Number(aggregate._sum.cost || 0)
266
- };
267
- }
268
- }
269
- /**
270
- * Helper to find the correct table property in the prisma client.
271
- * Prisma usually camelCases model names (e.g., AssistantChat -> assistantChat),
272
- * but mapping can vary based on configuration.
273
- */
274
- function getTable(prisma, tableName) {
275
- if (prisma[tableName])
276
- return prisma[tableName];
277
- // Try case-insensitive match if not found directly
278
- const keys = Object.keys(prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
279
- const match = keys.find((k) => k.toLowerCase() === tableName.toLowerCase());
280
- if (match)
281
- return prisma[match];
282
- // If still not found, search for the model name if it's different from the property name
283
- // but for now, we'll just throw a clear error
284
- throw new Error(`[@node-llm/orm] Prisma table "${tableName}" not found. Available tables: ${keys.join(", ")}`);
285
- }
286
- /**
287
- * Convenience method to create a new chat session.
288
- */
289
- export async function createChat(prisma, llm, options = {}) {
290
- const chatTable = options.tableNames?.chat || "llmChat";
291
- // Extract known options so we don't double-pass them or pass them incorrectly
292
- // runtime options should NOT be persisted to DB
293
- const { model, provider, instructions, metadata, tableNames: _tableNames, debug: _debug, persistence: _persistence, middlewares: _middlewares, maxToolCalls: _maxToolCalls, thinking: _thinking, temperature: _temperature, maxTokens: _maxTokens, headers: _headers, requestTimeout: _requestTimeout, params: _params, ...extras } = options;
294
- if (options.debug) {
295
- const keys = Object.keys(prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
296
- console.log(`[@node-llm/orm] createChat: table=${chatTable}, availableTables=${keys.join(", ")}`);
297
- }
298
- const table = getTable(prisma, chatTable);
299
- const record = await table.create({
300
- data: {
301
- model,
302
- provider,
303
- instructions,
304
- metadata: metadata ?? null,
305
- ...extras
306
- }
307
- });
308
- return new Chat(prisma, llm, record, options, options.tableNames);
309
- }
310
- /**
311
- * Convenience method to load an existing chat session.
312
- */
313
- export async function loadChat(prisma, llm, chatId, options = {}) {
314
- const chatTable = options.tableNames?.chat || "llmChat";
315
- if (options.debug) {
316
- const keys = Object.keys(prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
317
- console.log(`[@node-llm/orm] loadChat: table=${chatTable}, availableTables=${keys.join(", ")}`);
318
- }
319
- const table = getTable(prisma, chatTable);
320
- const record = await table.findUnique({
321
- where: { id: chatId }
322
- });
323
- if (!record)
324
- return null;
325
- // Reconstruct chat with options from DB or manual overrides if needed
326
- return new Chat(prisma, llm, record, options, options.tableNames);
327
- }
@@ -1,50 +0,0 @@
1
- /**
2
- * @node-llm/orm/prisma
3
- *
4
- * Prisma adapter for NodeLLM ORM.
5
- * Provides automatic persistence of chats, messages, tool calls, and API requests.
6
- *
7
- * @example Chat API (low-level)
8
- * ```typescript
9
- * import { PrismaClient } from '@prisma/client';
10
- * import { createLLM } from '@node-llm/core';
11
- * import { createChat } from '@node-llm/orm/prisma';
12
- *
13
- * const prisma = new PrismaClient();
14
- * const llm = createLLM({ provider: 'openai' });
15
- *
16
- * const chat = await createChat(prisma, llm, {
17
- * model: 'gpt-4',
18
- * instructions: 'You are a helpful assistant.'
19
- * });
20
- *
21
- * const response = await chat.ask('Hello!');
22
- * console.log(response.content);
23
- * ```
24
- *
25
- * @example AgentSession API (recommended for agents)
26
- * ```typescript
27
- * import { Agent } from '@node-llm/core';
28
- * import { createAgentSession, loadAgentSession } from '@node-llm/orm/prisma';
29
- *
30
- * class SupportAgent extends Agent {
31
- * static model = 'gpt-4.1';
32
- * static instructions = 'You are a helpful support agent.';
33
- * }
34
- *
35
- * // Create new session
36
- * const session = await createAgentSession(prisma, llm, SupportAgent, {
37
- * metadata: { userId: 'user_123' }
38
- * });
39
- * await session.ask('Hello!');
40
- *
41
- * // Resume later (Code Wins - model/tools from class, history from DB)
42
- * const session = await loadAgentSession(prisma, llm, SupportAgent, sessionId);
43
- * await session.ask('Continue our conversation');
44
- * ```
45
- */
46
- export { Chat, createChat, loadChat } from "./Chat.js";
47
- export type { ChatRecord, MessageRecord, ChatOptions } from "./Chat.js";
48
- export { AgentSession, createAgentSession, loadAgentSession } from "./AgentSession.js";
49
- export type { AgentSessionRecord, CreateAgentSessionOptions, LoadAgentSessionOptions, TableNames } from "./AgentSession.js";
50
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAGH,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACvD,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAGxE,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACvF,YAAY,EACV,kBAAkB,EAClB,yBAAyB,EACzB,uBAAuB,EACvB,UAAU,EACX,MAAM,mBAAmB,CAAC"}
@@ -1,49 +0,0 @@
1
- /**
2
- * @node-llm/orm/prisma
3
- *
4
- * Prisma adapter for NodeLLM ORM.
5
- * Provides automatic persistence of chats, messages, tool calls, and API requests.
6
- *
7
- * @example Chat API (low-level)
8
- * ```typescript
9
- * import { PrismaClient } from '@prisma/client';
10
- * import { createLLM } from '@node-llm/core';
11
- * import { createChat } from '@node-llm/orm/prisma';
12
- *
13
- * const prisma = new PrismaClient();
14
- * const llm = createLLM({ provider: 'openai' });
15
- *
16
- * const chat = await createChat(prisma, llm, {
17
- * model: 'gpt-4',
18
- * instructions: 'You are a helpful assistant.'
19
- * });
20
- *
21
- * const response = await chat.ask('Hello!');
22
- * console.log(response.content);
23
- * ```
24
- *
25
- * @example AgentSession API (recommended for agents)
26
- * ```typescript
27
- * import { Agent } from '@node-llm/core';
28
- * import { createAgentSession, loadAgentSession } from '@node-llm/orm/prisma';
29
- *
30
- * class SupportAgent extends Agent {
31
- * static model = 'gpt-4.1';
32
- * static instructions = 'You are a helpful support agent.';
33
- * }
34
- *
35
- * // Create new session
36
- * const session = await createAgentSession(prisma, llm, SupportAgent, {
37
- * metadata: { userId: 'user_123' }
38
- * });
39
- * await session.ask('Hello!');
40
- *
41
- * // Resume later (Code Wins - model/tools from class, history from DB)
42
- * const session = await loadAgentSession(prisma, llm, SupportAgent, sessionId);
43
- * await session.ask('Continue our conversation');
44
- * ```
45
- */
46
- // Chat API
47
- export { Chat, createChat, loadChat } from "./Chat.js";
48
- // AgentSession API
49
- export { AgentSession, createAgentSession, loadAgentSession } from "./AgentSession.js";
package/dist/index.d.ts DELETED
@@ -1,59 +0,0 @@
1
- /**
2
- * @node-llm/orm
3
- *
4
- * Database persistence layer for NodeLLM.
5
- * Automatically tracks chats, messages, tool calls, and API requests.
6
- *
7
- * ## Quick Start
8
- *
9
- * 1. Copy `schema.prisma` from this package into your project
10
- * 2. Run `npx prisma migrate dev`
11
- * 3. Use the ORM:
12
- *
13
- * ```typescript
14
- * import { createChat } from '@node-llm/orm/prisma';
15
- * import { prisma } from './db.js';
16
- * import { llm } from './llm.js';
17
- *
18
- * const chat = await createChat(prisma, llm, {
19
- * model: 'gpt-4',
20
- * instructions: 'You are a helpful assistant.'
21
- * });
22
- *
23
- * await chat.ask('Hello!');
24
- * ```
25
- *
26
- * ## Agent Sessions (Recommended for Agents)
27
- *
28
- * ```typescript
29
- * import { Agent } from '@node-llm/core';
30
- * import { createAgentSession, loadAgentSession } from '@node-llm/orm/prisma';
31
- *
32
- * class SupportAgent extends Agent {
33
- * static model = 'gpt-4.1';
34
- * static instructions = 'You are a helpful support agent.';
35
- * }
36
- *
37
- * // Create and persist
38
- * const session = await createAgentSession(prisma, llm, SupportAgent);
39
- * await session.ask('Hello!');
40
- *
41
- * // Resume later (Code Wins - model/tools from class, history from DB)
42
- * const session = await loadAgentSession(prisma, llm, SupportAgent, sessionId);
43
- * ```
44
- *
45
- * ## Adapters
46
- *
47
- * - `@node-llm/orm/prisma` - Prisma adapter (recommended)
48
- *
49
- * ## Schema
50
- *
51
- * The ORM tracks five core entities:
52
- * - **AgentSession** - Links Agent class to persistent Chat (v0.5.0+)
53
- * - **Chat** - Session container (model, provider, instructions)
54
- * - **Message** - User/Assistant conversation history
55
- * - **ToolCall** - Tool executions (name, arguments, results)
56
- * - **Request** - API call metrics (tokens, latency, cost)
57
- */
58
- export * from "./adapters/prisma/index.js";
59
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAGH,cAAc,4BAA4B,CAAC"}
package/dist/index.js DELETED
@@ -1,59 +0,0 @@
1
- /**
2
- * @node-llm/orm
3
- *
4
- * Database persistence layer for NodeLLM.
5
- * Automatically tracks chats, messages, tool calls, and API requests.
6
- *
7
- * ## Quick Start
8
- *
9
- * 1. Copy `schema.prisma` from this package into your project
10
- * 2. Run `npx prisma migrate dev`
11
- * 3. Use the ORM:
12
- *
13
- * ```typescript
14
- * import { createChat } from '@node-llm/orm/prisma';
15
- * import { prisma } from './db.js';
16
- * import { llm } from './llm.js';
17
- *
18
- * const chat = await createChat(prisma, llm, {
19
- * model: 'gpt-4',
20
- * instructions: 'You are a helpful assistant.'
21
- * });
22
- *
23
- * await chat.ask('Hello!');
24
- * ```
25
- *
26
- * ## Agent Sessions (Recommended for Agents)
27
- *
28
- * ```typescript
29
- * import { Agent } from '@node-llm/core';
30
- * import { createAgentSession, loadAgentSession } from '@node-llm/orm/prisma';
31
- *
32
- * class SupportAgent extends Agent {
33
- * static model = 'gpt-4.1';
34
- * static instructions = 'You are a helpful support agent.';
35
- * }
36
- *
37
- * // Create and persist
38
- * const session = await createAgentSession(prisma, llm, SupportAgent);
39
- * await session.ask('Hello!');
40
- *
41
- * // Resume later (Code Wins - model/tools from class, history from DB)
42
- * const session = await loadAgentSession(prisma, llm, SupportAgent, sessionId);
43
- * ```
44
- *
45
- * ## Adapters
46
- *
47
- * - `@node-llm/orm/prisma` - Prisma adapter (recommended)
48
- *
49
- * ## Schema
50
- *
51
- * The ORM tracks five core entities:
52
- * - **AgentSession** - Links Agent class to persistent Chat (v0.5.0+)
53
- * - **Chat** - Session container (model, provider, instructions)
54
- * - **Message** - User/Assistant conversation history
55
- * - **ToolCall** - Tool executions (name, arguments, results)
56
- * - **Request** - API call metrics (tokens, latency, cost)
57
- */
58
- // Re-export Prisma adapter as default
59
- export * from "./adapters/prisma/index.js";