@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,22 +1,17 @@
|
|
|
1
1
|
import { BaseChat } from "../../BaseChat.js";
|
|
2
|
+
import { toStore } from "./PrismaStore.js";
|
|
2
3
|
/**
|
|
3
|
-
* Prisma
|
|
4
|
+
* A chat whose messages, tool calls and requests are recorded in a store: a Prisma client's
|
|
5
|
+
* database by default, or any ChatStore.
|
|
4
6
|
*/
|
|
5
7
|
export class Chat extends BaseChat {
|
|
6
|
-
prisma;
|
|
7
8
|
llm;
|
|
8
|
-
|
|
9
|
+
store;
|
|
9
10
|
persistenceConfig;
|
|
10
|
-
constructor(
|
|
11
|
+
constructor(prismaOrStore, llm, record, options = {}, tableNames = {}) {
|
|
11
12
|
super(record, options);
|
|
12
|
-
this.prisma = prisma;
|
|
13
13
|
this.llm = llm;
|
|
14
|
-
this.
|
|
15
|
-
chat: tableNames.chat || "llmChat",
|
|
16
|
-
message: tableNames.message || "llmMessage",
|
|
17
|
-
toolCall: tableNames.toolCall || "llmToolCall",
|
|
18
|
-
request: tableNames.request || "llmRequest"
|
|
19
|
-
};
|
|
14
|
+
this.store = toStore(prismaOrStore, tableNames);
|
|
20
15
|
this.persistenceConfig = {
|
|
21
16
|
toolCalls: options.persistence?.toolCalls ?? true,
|
|
22
17
|
requests: options.persistence?.requests ?? true
|
|
@@ -29,29 +24,33 @@ export class Chat extends BaseChat {
|
|
|
29
24
|
const provider = this.localOptions.provider || this.record.provider;
|
|
30
25
|
const model = this.localOptions.model || this.record.model;
|
|
31
26
|
const llmInstance = provider ? this.llm.withProvider(provider) : this.llm;
|
|
27
|
+
// `schema` needs core's own normalization (raw Zod schema -> Schema
|
|
28
|
+
// instance), so it's applied via withSchema() below rather than spread
|
|
29
|
+
// as a raw ChatOptions field.
|
|
30
|
+
const { schema, ...chatOptions } = this.localOptions;
|
|
32
31
|
const coreChat = llmInstance.chat(model || undefined, {
|
|
33
32
|
messages: history,
|
|
34
|
-
...
|
|
33
|
+
...chatOptions,
|
|
35
34
|
middlewares: this.customMiddlewares
|
|
36
35
|
});
|
|
37
36
|
// Register tools
|
|
38
37
|
if (this.customTools.length > 0) {
|
|
39
38
|
coreChat.withTools(this.customTools);
|
|
40
39
|
}
|
|
40
|
+
if (schema) {
|
|
41
|
+
coreChat.withSchema(schema);
|
|
42
|
+
}
|
|
41
43
|
// --- Persistence Hooks ---
|
|
42
44
|
coreChat.onToolCallStart(async (call) => {
|
|
43
45
|
// Only persist if toolCalls persistence is enabled
|
|
44
46
|
if (this.persistenceConfig.toolCalls) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
thought: call.thought || null,
|
|
53
|
-
thoughtSignature: call.thought_signature || null
|
|
54
|
-
}
|
|
47
|
+
await this.store.createToolCall({
|
|
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
|
|
55
54
|
});
|
|
56
55
|
}
|
|
57
56
|
// User hooks
|
|
@@ -61,14 +60,10 @@ export class Chat extends BaseChat {
|
|
|
61
60
|
coreChat.onToolCallEnd(async (call, result) => {
|
|
62
61
|
// Only persist if toolCalls persistence is enabled
|
|
63
62
|
if (this.persistenceConfig.toolCalls) {
|
|
64
|
-
const toolCallModel = this.tables.toolCall;
|
|
65
63
|
const resString = typeof result === "string" ? result : JSON.stringify(result);
|
|
66
|
-
await this.
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
result: resString,
|
|
70
|
-
thought: call.thought || null
|
|
71
|
-
}
|
|
64
|
+
await this.store.finishToolCall(assistantMessageId, call.id, {
|
|
65
|
+
result: resString,
|
|
66
|
+
thought: call.thought || null
|
|
72
67
|
});
|
|
73
68
|
}
|
|
74
69
|
// User hooks
|
|
@@ -85,19 +80,16 @@ export class Chat extends BaseChat {
|
|
|
85
80
|
}
|
|
86
81
|
// Only persist if requests persistence is enabled
|
|
87
82
|
if (this.persistenceConfig.requests) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
outputTokens: finalResp.usage?.output_tokens || 0,
|
|
99
|
-
cost: finalResp.usage?.cost || 0
|
|
100
|
-
}
|
|
83
|
+
await this.store.createRequest({
|
|
84
|
+
chatId: this.id,
|
|
85
|
+
messageId: assistantMessageId,
|
|
86
|
+
provider: finalResp.provider || provider || "unknown",
|
|
87
|
+
model: finalResp.model || model || "unknown",
|
|
88
|
+
statusCode: 200,
|
|
89
|
+
duration: finalResp.latency || 0,
|
|
90
|
+
inputTokens: finalResp.usage?.input_tokens || 0,
|
|
91
|
+
outputTokens: finalResp.usage?.output_tokens || 0,
|
|
92
|
+
cost: finalResp.usage?.cost || 0
|
|
101
93
|
});
|
|
102
94
|
}
|
|
103
95
|
return finalResp;
|
|
@@ -126,23 +118,50 @@ export class Chat extends BaseChat {
|
|
|
126
118
|
return current;
|
|
127
119
|
});
|
|
128
120
|
}
|
|
121
|
+
if (this.userHooks.onToolCallError.length > 0) {
|
|
122
|
+
coreChat.onToolCallError(async (call, error) => {
|
|
123
|
+
// First handler to return a directive (STOP/CONTINUE/RETRY) wins,
|
|
124
|
+
// but every handler still runs so all of them get to observe it.
|
|
125
|
+
let directive;
|
|
126
|
+
for (const h of this.userHooks.onToolCallError) {
|
|
127
|
+
const value = await h(call, error);
|
|
128
|
+
if (value !== undefined && directive === undefined) {
|
|
129
|
+
directive = value;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return directive;
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
if (this.userHooks.onConfirmToolCall.length > 0) {
|
|
136
|
+
coreChat.onConfirmToolCall(async (call) => {
|
|
137
|
+
// Every registered handler must approve for the call to proceed.
|
|
138
|
+
for (const h of this.userHooks.onConfirmToolCall) {
|
|
139
|
+
const approved = await h(call);
|
|
140
|
+
if (approved === false)
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
129
146
|
return coreChat;
|
|
130
147
|
}
|
|
131
148
|
/**
|
|
132
149
|
* Send a message and persist the conversation.
|
|
133
150
|
*/
|
|
134
151
|
async ask(message, options = {}) {
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
152
|
+
const userMessage = await this.store.createMessage({
|
|
153
|
+
chatId: this.id,
|
|
154
|
+
role: "user",
|
|
155
|
+
content: message
|
|
138
156
|
});
|
|
139
|
-
const assistantMessage = await this.
|
|
140
|
-
|
|
157
|
+
const assistantMessage = await this.store.createMessage({
|
|
158
|
+
chatId: this.id,
|
|
159
|
+
role: "assistant",
|
|
160
|
+
content: null
|
|
141
161
|
});
|
|
142
162
|
try {
|
|
143
|
-
const historyRecords = await this.
|
|
144
|
-
|
|
145
|
-
orderBy: { createdAt: "asc" }
|
|
163
|
+
const historyRecords = await this.store.listMessages(this.id, {
|
|
164
|
+
excludeIds: [userMessage.id, assistantMessage.id]
|
|
146
165
|
});
|
|
147
166
|
const history = historyRecords.map((m) => ({
|
|
148
167
|
role: m.role,
|
|
@@ -150,25 +169,22 @@ export class Chat extends BaseChat {
|
|
|
150
169
|
}));
|
|
151
170
|
const coreChat = await this.prepareCoreChat(history, assistantMessage.id);
|
|
152
171
|
const response = await coreChat.ask(message, options);
|
|
153
|
-
return await this.
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
provider: response.provider || null
|
|
165
|
-
}
|
|
166
|
-
});
|
|
172
|
+
return (await this.store.updateMessage(assistantMessage.id, {
|
|
173
|
+
content: response.content,
|
|
174
|
+
contentRaw: JSON.stringify(response.meta),
|
|
175
|
+
inputTokens: response.usage?.input_tokens || 0,
|
|
176
|
+
outputTokens: response.usage?.output_tokens || 0,
|
|
177
|
+
thinkingText: response.thinking?.text || null,
|
|
178
|
+
thinkingSignature: response.thinking?.signature || null,
|
|
179
|
+
thinkingTokens: response.thinking?.tokens || null,
|
|
180
|
+
modelId: response.model || null,
|
|
181
|
+
provider: response.provider || null
|
|
182
|
+
}));
|
|
167
183
|
}
|
|
168
184
|
catch (error) {
|
|
169
185
|
// Delete the empty assistant message, but keep the user message
|
|
170
186
|
// so there's a record of what was asked (useful for debugging)
|
|
171
|
-
await this.
|
|
187
|
+
await this.store.deleteMessage(assistantMessage.id);
|
|
172
188
|
throw error;
|
|
173
189
|
}
|
|
174
190
|
}
|
|
@@ -177,17 +193,19 @@ export class Chat extends BaseChat {
|
|
|
177
193
|
* Yields ChatChunk objects for full visibility of thinking, content, and tools.
|
|
178
194
|
*/
|
|
179
195
|
async *askStream(message, options = {}) {
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
196
|
+
const userMessage = await this.store.createMessage({
|
|
197
|
+
chatId: this.id,
|
|
198
|
+
role: "user",
|
|
199
|
+
content: message
|
|
183
200
|
});
|
|
184
|
-
const assistantMessage = await this.
|
|
185
|
-
|
|
201
|
+
const assistantMessage = await this.store.createMessage({
|
|
202
|
+
chatId: this.id,
|
|
203
|
+
role: "assistant",
|
|
204
|
+
content: null
|
|
186
205
|
});
|
|
187
206
|
try {
|
|
188
|
-
const historyRecords = await this.
|
|
189
|
-
|
|
190
|
-
orderBy: { createdAt: "asc" }
|
|
207
|
+
const historyRecords = await this.store.listMessages(this.id, {
|
|
208
|
+
excludeIds: [userMessage.id, assistantMessage.id]
|
|
191
209
|
});
|
|
192
210
|
const history = historyRecords.map((m) => ({
|
|
193
211
|
role: m.role,
|
|
@@ -213,25 +231,22 @@ export class Chat extends BaseChat {
|
|
|
213
231
|
}
|
|
214
232
|
}
|
|
215
233
|
}
|
|
216
|
-
return await this.
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
provider: this.localOptions.provider || this.record.provider || null
|
|
228
|
-
}
|
|
229
|
-
});
|
|
234
|
+
return (await this.store.updateMessage(assistantMessage.id, {
|
|
235
|
+
content: fullContent,
|
|
236
|
+
contentRaw: JSON.stringify(metadata),
|
|
237
|
+
inputTokens: metadata.input_tokens || 0,
|
|
238
|
+
outputTokens: metadata.output_tokens || 0,
|
|
239
|
+
thinkingText: metadata.thinking?.text || null,
|
|
240
|
+
thinkingSignature: metadata.thinking?.signature || null,
|
|
241
|
+
thinkingTokens: metadata.thinking?.tokens || null,
|
|
242
|
+
modelId: this.localOptions.model || this.record.model || null,
|
|
243
|
+
provider: this.localOptions.provider || this.record.provider || null
|
|
244
|
+
}));
|
|
230
245
|
}
|
|
231
246
|
catch (error) {
|
|
232
247
|
// Delete the empty assistant message, but keep the user message
|
|
233
248
|
// so there's a record of what was asked (useful for debugging)
|
|
234
|
-
await this.
|
|
249
|
+
await this.store.deleteMessage(assistantMessage.id);
|
|
235
250
|
throw error;
|
|
236
251
|
}
|
|
237
252
|
}
|
|
@@ -239,89 +254,47 @@ export class Chat extends BaseChat {
|
|
|
239
254
|
* Get all messages for this chat.
|
|
240
255
|
*/
|
|
241
256
|
async messages() {
|
|
242
|
-
|
|
243
|
-
return await this.prisma[messageModel].findMany({
|
|
244
|
-
where: { chatId: this.id },
|
|
245
|
-
orderBy: { createdAt: "asc" }
|
|
246
|
-
});
|
|
257
|
+
return (await this.store.listMessages(this.id));
|
|
247
258
|
}
|
|
248
259
|
/**
|
|
249
260
|
* Returns a usage summary for this chat session.
|
|
250
261
|
*/
|
|
251
262
|
async stats() {
|
|
252
|
-
const
|
|
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
|
-
});
|
|
263
|
+
const totals = await this.store.requestTotals(this.id);
|
|
261
264
|
return {
|
|
262
|
-
input_tokens:
|
|
263
|
-
output_tokens:
|
|
264
|
-
total_tokens:
|
|
265
|
-
cost:
|
|
265
|
+
input_tokens: totals.inputTokens,
|
|
266
|
+
output_tokens: totals.outputTokens,
|
|
267
|
+
total_tokens: totals.inputTokens + totals.outputTokens,
|
|
268
|
+
cost: totals.cost
|
|
266
269
|
};
|
|
267
270
|
}
|
|
268
271
|
}
|
|
269
272
|
/**
|
|
270
|
-
*
|
|
271
|
-
* Prisma usually camelCases model names (e.g., AssistantChat -> assistantChat),
|
|
272
|
-
* but mapping can vary based on configuration.
|
|
273
|
+
* Creates a new chat session, recorded through a Prisma client or any ChatStore.
|
|
273
274
|
*/
|
|
274
|
-
function
|
|
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
|
|
275
|
+
export async function createChat(prismaOrStore, llm, options = {}) {
|
|
293
276
|
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
|
-
|
|
295
|
-
|
|
296
|
-
console.log(`[@node-llm/orm] createChat:
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
instructions,
|
|
304
|
-
metadata: metadata ?? null,
|
|
305
|
-
...extras
|
|
306
|
-
}
|
|
277
|
+
const store = toStore(prismaOrStore, options.tableNames);
|
|
278
|
+
if (options.debug)
|
|
279
|
+
console.log(`[@node-llm/orm] createChat: store=${store.constructor.name}`);
|
|
280
|
+
const record = await store.createChat({
|
|
281
|
+
model,
|
|
282
|
+
provider,
|
|
283
|
+
instructions,
|
|
284
|
+
metadata: metadata ?? null,
|
|
285
|
+
...extras
|
|
307
286
|
});
|
|
308
|
-
return new Chat(
|
|
287
|
+
return new Chat(store, llm, record, options, options.tableNames);
|
|
309
288
|
}
|
|
310
289
|
/**
|
|
311
|
-
*
|
|
290
|
+
* Loads an existing chat session from a Prisma client or any ChatStore.
|
|
312
291
|
*/
|
|
313
|
-
export async function loadChat(
|
|
314
|
-
const
|
|
315
|
-
if (options.debug)
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
const table = getTable(prisma, chatTable);
|
|
320
|
-
const record = await table.findUnique({
|
|
321
|
-
where: { id: chatId }
|
|
322
|
-
});
|
|
292
|
+
export async function loadChat(prismaOrStore, llm, chatId, options = {}) {
|
|
293
|
+
const store = toStore(prismaOrStore, options.tableNames);
|
|
294
|
+
if (options.debug)
|
|
295
|
+
console.log(`[@node-llm/orm] loadChat: store=${store.constructor.name}`);
|
|
296
|
+
const record = await store.findChat(chatId);
|
|
323
297
|
if (!record)
|
|
324
298
|
return null;
|
|
325
|
-
|
|
326
|
-
return new Chat(prisma, llm, record, options, options.tableNames);
|
|
299
|
+
return new Chat(store, llm, record, options, options.tableNames);
|
|
327
300
|
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { ChatStore, MessageUpdate, NewChat, NewRequest, NewToolCall, RequestTotals, StoredAgentSession, StoredChat, StoredMessage } from "../../Store.js";
|
|
2
|
+
export interface TableNames {
|
|
3
|
+
agentSession?: string;
|
|
4
|
+
chat?: string;
|
|
5
|
+
message?: string;
|
|
6
|
+
toolCall?: string;
|
|
7
|
+
request?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Keeps chats in a database through a Prisma client, in the tables of the reference schema or
|
|
11
|
+
* the ones named in tableNames.
|
|
12
|
+
*/
|
|
13
|
+
export declare class PrismaStore implements ChatStore {
|
|
14
|
+
private prisma;
|
|
15
|
+
private tables;
|
|
16
|
+
constructor(prisma: any, tableNames?: TableNames);
|
|
17
|
+
createChat(chat: NewChat): Promise<StoredChat>;
|
|
18
|
+
findChat(id: string): Promise<StoredChat | null>;
|
|
19
|
+
deleteChat(id: string): Promise<void>;
|
|
20
|
+
createMessage(message: {
|
|
21
|
+
chatId: string;
|
|
22
|
+
role: string;
|
|
23
|
+
content: string | null;
|
|
24
|
+
}): Promise<StoredMessage>;
|
|
25
|
+
updateMessage(id: string, changes: MessageUpdate): Promise<StoredMessage>;
|
|
26
|
+
deleteMessage(id: string): Promise<void>;
|
|
27
|
+
listMessages(chatId: string, options?: {
|
|
28
|
+
excludeIds?: string[];
|
|
29
|
+
}): Promise<StoredMessage[]>;
|
|
30
|
+
createToolCall(toolCall: NewToolCall): Promise<void>;
|
|
31
|
+
finishToolCall(messageId: string, toolCallId: string, finished: {
|
|
32
|
+
result: string;
|
|
33
|
+
thought?: string | null;
|
|
34
|
+
}): Promise<void>;
|
|
35
|
+
createRequest(request: NewRequest): Promise<void>;
|
|
36
|
+
requestTotals(chatId: string): Promise<RequestTotals>;
|
|
37
|
+
createAgentSession(session: {
|
|
38
|
+
agentClass: string;
|
|
39
|
+
chatId: string;
|
|
40
|
+
metadata: Record<string, unknown> | null;
|
|
41
|
+
}): Promise<StoredAgentSession>;
|
|
42
|
+
findAgentSession(id: string): Promise<StoredAgentSession | null>;
|
|
43
|
+
updateAgentSession(id: string, changes: {
|
|
44
|
+
metadata: Record<string, unknown>;
|
|
45
|
+
}): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* The Prisma model for a table. Prisma usually camelCases model names (AssistantChat becomes
|
|
48
|
+
* assistantChat), but mapping can vary, so a case-insensitive match is accepted too.
|
|
49
|
+
*/
|
|
50
|
+
private table;
|
|
51
|
+
}
|
|
52
|
+
/** A ChatStore as given, or a Prisma client wrapped in a PrismaStore. */
|
|
53
|
+
export declare function toStore(prismaOrStore: unknown, tableNames?: TableNames): ChatStore;
|
|
54
|
+
//# sourceMappingURL=PrismaStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PrismaStore.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/PrismaStore.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,SAAS,EACT,aAAa,EACb,OAAO,EACP,UAAU,EACV,WAAW,EACX,aAAa,EACb,kBAAkB,EAClB,UAAU,EACV,aAAa,EACd,MAAM,gBAAgB,CAAC;AAGxB,MAAM,WAAW,UAAU;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,qBAAa,WAAY,YAAW,SAAS;IAIzC,OAAO,CAAC,MAAM;IAHhB,OAAO,CAAC,MAAM,CAAuB;gBAG3B,MAAM,EAAE,GAAG,EACnB,UAAU,GAAE,UAAe;IAWvB,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC;IAI9C,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;IAIhD,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIrC,aAAa,CAAC,OAAO,EAAE;QAC3B,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;KACxB,GAAG,OAAO,CAAC,aAAa,CAAC;IAIpB,aAAa,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;IAIzE,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIxC,YAAY,CAChB,MAAM,EAAE,MAAM,EACd,OAAO,GAAE;QAAE,UAAU,CAAC,EAAE,MAAM,EAAE,CAAA;KAAO,GACtC,OAAO,CAAC,aAAa,EAAE,CAAC;IAOrB,cAAc,CAAC,QAAQ,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAIpD,cAAc,CAClB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,GACpD,OAAO,CAAC,IAAI,CAAC;IAOV,aAAa,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAIjD,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IAYrD,kBAAkB,CAAC,OAAO,EAAE;QAChC,UAAU,EAAE,MAAM,CAAC;QACnB,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;KAC1C,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAIzB,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC;IAIhE,kBAAkB,CACtB,EAAE,EAAE,MAAM,EACV,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,GAC7C,OAAO,CAAC,IAAI,CAAC;IAIhB;;;OAGG;IACH,OAAO,CAAC,KAAK;CAYd;AAED,yEAAyE;AACzE,wBAAgB,OAAO,CAAC,aAAa,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,UAAU,GAAG,SAAS,CAElF"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { isChatStore } from "../../Store.js";
|
|
2
|
+
/**
|
|
3
|
+
* Keeps chats in a database through a Prisma client, in the tables of the reference schema or
|
|
4
|
+
* the ones named in tableNames.
|
|
5
|
+
*/
|
|
6
|
+
export class PrismaStore {
|
|
7
|
+
prisma;
|
|
8
|
+
tables;
|
|
9
|
+
constructor(prisma, tableNames = {}) {
|
|
10
|
+
this.prisma = prisma;
|
|
11
|
+
this.tables = {
|
|
12
|
+
agentSession: tableNames.agentSession || "llmAgentSession",
|
|
13
|
+
chat: tableNames.chat || "llmChat",
|
|
14
|
+
message: tableNames.message || "llmMessage",
|
|
15
|
+
toolCall: tableNames.toolCall || "llmToolCall",
|
|
16
|
+
request: tableNames.request || "llmRequest"
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
async createChat(chat) {
|
|
20
|
+
return await this.table("chat").create({ data: chat });
|
|
21
|
+
}
|
|
22
|
+
async findChat(id) {
|
|
23
|
+
return await this.table("chat").findUnique({ where: { id } });
|
|
24
|
+
}
|
|
25
|
+
async deleteChat(id) {
|
|
26
|
+
await this.table("chat").delete({ where: { id } });
|
|
27
|
+
}
|
|
28
|
+
async createMessage(message) {
|
|
29
|
+
return await this.table("message").create({ data: message });
|
|
30
|
+
}
|
|
31
|
+
async updateMessage(id, changes) {
|
|
32
|
+
return await this.table("message").update({ where: { id }, data: changes });
|
|
33
|
+
}
|
|
34
|
+
async deleteMessage(id) {
|
|
35
|
+
await this.table("message").delete({ where: { id } });
|
|
36
|
+
}
|
|
37
|
+
async listMessages(chatId, options = {}) {
|
|
38
|
+
const where = options.excludeIds?.length
|
|
39
|
+
? { chatId, id: { notIn: options.excludeIds } }
|
|
40
|
+
: { chatId };
|
|
41
|
+
return await this.table("message").findMany({ where, orderBy: { createdAt: "asc" } });
|
|
42
|
+
}
|
|
43
|
+
async createToolCall(toolCall) {
|
|
44
|
+
await this.table("toolCall").create({ data: toolCall });
|
|
45
|
+
}
|
|
46
|
+
async finishToolCall(messageId, toolCallId, finished) {
|
|
47
|
+
await this.table("toolCall").update({
|
|
48
|
+
where: { messageId_toolCallId: { messageId, toolCallId } },
|
|
49
|
+
data: finished
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
async createRequest(request) {
|
|
53
|
+
await this.table("request").create({ data: request });
|
|
54
|
+
}
|
|
55
|
+
async requestTotals(chatId) {
|
|
56
|
+
const aggregate = await this.table("request").aggregate({
|
|
57
|
+
where: { chatId },
|
|
58
|
+
_sum: { inputTokens: true, outputTokens: true, cost: true }
|
|
59
|
+
});
|
|
60
|
+
return {
|
|
61
|
+
inputTokens: Number(aggregate._sum.inputTokens || 0),
|
|
62
|
+
outputTokens: Number(aggregate._sum.outputTokens || 0),
|
|
63
|
+
cost: Number(aggregate._sum.cost || 0)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
async createAgentSession(session) {
|
|
67
|
+
return await this.table("agentSession").create({ data: session });
|
|
68
|
+
}
|
|
69
|
+
async findAgentSession(id) {
|
|
70
|
+
return await this.table("agentSession").findUnique({ where: { id } });
|
|
71
|
+
}
|
|
72
|
+
async updateAgentSession(id, changes) {
|
|
73
|
+
await this.table("agentSession").update({ where: { id }, data: changes });
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The Prisma model for a table. Prisma usually camelCases model names (AssistantChat becomes
|
|
77
|
+
* assistantChat), but mapping can vary, so a case-insensitive match is accepted too.
|
|
78
|
+
*/
|
|
79
|
+
table(kind) {
|
|
80
|
+
const name = this.tables[kind];
|
|
81
|
+
if (this.prisma[name])
|
|
82
|
+
return this.prisma[name];
|
|
83
|
+
const keys = Object.keys(this.prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
|
|
84
|
+
const match = keys.find((k) => k.toLowerCase() === name.toLowerCase());
|
|
85
|
+
if (match)
|
|
86
|
+
return this.prisma[match];
|
|
87
|
+
throw new Error(`[@node-llm/orm] Prisma table "${name}" not found. Available tables: ${keys.join(", ")}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** A ChatStore as given, or a Prisma client wrapped in a PrismaStore. */
|
|
91
|
+
export function toStore(prismaOrStore, tableNames) {
|
|
92
|
+
return isChatStore(prismaOrStore) ? prismaOrStore : new PrismaStore(prismaOrStore, tableNames);
|
|
93
|
+
}
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
*/
|
|
46
46
|
export { Chat, createChat, loadChat } from "./Chat.js";
|
|
47
47
|
export type { ChatRecord, MessageRecord, ChatOptions } from "./Chat.js";
|
|
48
|
+
export { PrismaStore, toStore } from "./PrismaStore.js";
|
|
48
49
|
export { AgentSession, createAgentSession, loadAgentSession } from "./AgentSession.js";
|
|
49
50
|
export type { AgentSessionRecord, CreateAgentSessionOptions, LoadAgentSessionOptions, TableNames } from "./AgentSession.js";
|
|
50
51
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
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
|
+
{"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,WAAW,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAGxD,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"}
|
|
@@ -45,5 +45,7 @@
|
|
|
45
45
|
*/
|
|
46
46
|
// Chat API
|
|
47
47
|
export { Chat, createChat, loadChat } from "./Chat.js";
|
|
48
|
+
// Stores: where chats are recorded
|
|
49
|
+
export { PrismaStore, toStore } from "./PrismaStore.js";
|
|
48
50
|
// AgentSession API
|
|
49
51
|
export { AgentSession, createAgentSession, loadAgentSession } from "./AgentSession.js";
|
package/dist/index.d.ts
CHANGED
|
@@ -56,4 +56,6 @@
|
|
|
56
56
|
* - **Request** - API call metrics (tokens, latency, cost)
|
|
57
57
|
*/
|
|
58
58
|
export * from "./adapters/prisma/index.js";
|
|
59
|
+
export { isChatStore } from "./Store.js";
|
|
60
|
+
export type { ChatStore, StoredChat, StoredMessage, StoredAgentSession, NewChat, NewToolCall, NewRequest, MessageUpdate, RequestTotals } from "./Store.js";
|
|
59
61
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAGH,cAAc,4BAA4B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAGH,cAAc,4BAA4B,CAAC;AAG3C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,YAAY,EACV,SAAS,EACT,UAAU,EACV,aAAa,EACb,kBAAkB,EAClB,OAAO,EACP,WAAW,EACX,UAAU,EACV,aAAa,EACb,aAAa,EACd,MAAM,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-llm/orm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Database persistence layer for NodeLLM - Chat, Message, and ToolCall tracking with streaming support",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -59,17 +59,22 @@
|
|
|
59
59
|
"@node-llm/core": "^1.15.0",
|
|
60
60
|
"@prisma/client": "^5.0.0"
|
|
61
61
|
},
|
|
62
|
+
"peerDependenciesMeta": {
|
|
63
|
+
"@prisma/client": {
|
|
64
|
+
"optional": true
|
|
65
|
+
}
|
|
66
|
+
},
|
|
62
67
|
"devDependencies": {
|
|
63
68
|
"@node-llm/core": "file:../core",
|
|
64
69
|
"@prisma/client": "^5.0.0",
|
|
65
70
|
"@types/node": "^20.0.0",
|
|
66
71
|
"prisma": "^5.0.0",
|
|
67
72
|
"typescript": "^5.0.0",
|
|
68
|
-
"vitest": "^
|
|
73
|
+
"vitest": "^4.0.0"
|
|
69
74
|
},
|
|
70
75
|
"dependencies": {},
|
|
71
76
|
"scripts": {
|
|
72
|
-
"build": "
|
|
77
|
+
"build": "tsc",
|
|
73
78
|
"test": "vitest run",
|
|
74
79
|
"test:docs": "vitest run test/docs",
|
|
75
80
|
"test:watch": "vitest"
|
package/schema.prisma
CHANGED
|
@@ -87,13 +87,14 @@ model LlmRequest {
|
|
|
87
87
|
chatId String
|
|
88
88
|
messageId String? // Optional because requests might fail before message creation
|
|
89
89
|
|
|
90
|
-
provider
|
|
91
|
-
model
|
|
92
|
-
statusCode
|
|
93
|
-
duration
|
|
94
|
-
inputTokens
|
|
95
|
-
outputTokens
|
|
96
|
-
cost
|
|
90
|
+
provider String
|
|
91
|
+
model String
|
|
92
|
+
statusCode Int
|
|
93
|
+
duration Int // milliseconds
|
|
94
|
+
inputTokens Int
|
|
95
|
+
outputTokens Int
|
|
96
|
+
cost Float?
|
|
97
|
+
correctionRounds Int? // Number of self-correction retries (v0.7.0+)
|
|
97
98
|
|
|
98
99
|
createdAt DateTime @default(now())
|
|
99
100
|
|