@node-llm/orm 0.8.0 → 0.10.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.
Files changed (38) hide show
  1. package/CHANGELOG.md +155 -0
  2. package/LICENSE +21 -0
  3. package/README.md +38 -14
  4. package/dist/BaseChat.d.ts +124 -0
  5. package/dist/BaseChat.d.ts.map +1 -0
  6. package/dist/BaseChat.js +182 -0
  7. package/dist/Store.d.ts +109 -0
  8. package/dist/Store.d.ts.map +1 -0
  9. package/dist/Store.js +14 -0
  10. package/dist/adapters/prisma/AgentSession.d.ts +178 -0
  11. package/dist/adapters/prisma/AgentSession.d.ts.map +1 -0
  12. package/dist/adapters/prisma/AgentSession.js +328 -0
  13. package/dist/adapters/prisma/Chat.d.ts +70 -0
  14. package/dist/adapters/prisma/Chat.d.ts.map +1 -0
  15. package/dist/adapters/prisma/Chat.js +301 -0
  16. package/dist/adapters/prisma/PrismaStore.d.ts +54 -0
  17. package/dist/adapters/prisma/PrismaStore.d.ts.map +1 -0
  18. package/dist/adapters/prisma/PrismaStore.js +97 -0
  19. package/dist/adapters/prisma/index.d.ts +51 -0
  20. package/dist/adapters/prisma/index.d.ts.map +1 -0
  21. package/dist/adapters/prisma/index.js +51 -0
  22. package/dist/index.d.ts +61 -0
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +61 -0
  25. package/migrations/README.md +5 -4
  26. package/migrations/add_cached_tokens.sql +5 -0
  27. package/package.json +14 -12
  28. package/schema.prisma +1 -0
  29. package/src/Store.ts +135 -0
  30. package/src/adapters/prisma/AgentSession.ts +89 -185
  31. package/src/adapters/prisma/Chat.ts +95 -162
  32. package/src/adapters/prisma/PrismaStore.ts +156 -0
  33. package/src/adapters/prisma/index.ts +3 -0
  34. package/src/index.ts +14 -0
  35. package/test/CachedTokens.test.ts +40 -0
  36. package/test/Store.test.ts +214 -0
  37. package/test/docs/readme-exports.test.ts +8 -0
  38. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,70 @@
1
+ import type { NodeLLMCore, ChatChunk, AskOptions, Usage } from "@node-llm/core";
2
+ import { BaseChat, type ChatRecord, type ChatOptions } from "../../BaseChat.js";
3
+ export { type ChatRecord, type ChatOptions };
4
+ export interface MessageRecord {
5
+ id: string;
6
+ chatId: string;
7
+ role: string;
8
+ content: string | null;
9
+ contentRaw: string | null;
10
+ reasoning: string | null;
11
+ inputTokens: number | null;
12
+ outputTokens: number | null;
13
+ thinkingText: string | null;
14
+ thinkingSignature: string | null;
15
+ thinkingTokens: number | null;
16
+ modelId: string | null;
17
+ provider: string | null;
18
+ createdAt: Date;
19
+ }
20
+ export interface TableNames {
21
+ chat?: string;
22
+ message?: string;
23
+ toolCall?: string;
24
+ request?: string;
25
+ }
26
+ /**
27
+ * A chat whose messages, tool calls and requests are recorded in a store: a Prisma client's
28
+ * database by default, or any ChatStore.
29
+ */
30
+ export declare class Chat extends BaseChat {
31
+ private llm;
32
+ private store;
33
+ private persistenceConfig;
34
+ constructor(prismaOrStore: unknown, llm: NodeLLMCore, record: ChatRecord, options?: ChatOptions, tableNames?: TableNames);
35
+ /**
36
+ * Internal prep for core Chat instance with persistence hooks.
37
+ */
38
+ private prepareCoreChat;
39
+ /**
40
+ * Send a message and persist the conversation.
41
+ */
42
+ ask(message: string, options?: AskOptions): Promise<MessageRecord>;
43
+ /**
44
+ * Stream a response and persist the conversation.
45
+ * Yields ChatChunk objects for full visibility of thinking, content, and tools.
46
+ */
47
+ askStream(message: string, options?: AskOptions): AsyncGenerator<ChatChunk, MessageRecord, undefined>;
48
+ /**
49
+ * Get all messages for this chat.
50
+ */
51
+ messages(): Promise<MessageRecord[]>;
52
+ /**
53
+ * Returns a usage summary for this chat session.
54
+ */
55
+ stats(): Promise<Usage>;
56
+ }
57
+ /**
58
+ * Creates a new chat session, recorded through a Prisma client or any ChatStore.
59
+ */
60
+ export declare function createChat<T = Record<string, any>>(prismaOrStore: unknown, llm: NodeLLMCore, options?: ChatOptions & {
61
+ tableNames?: TableNames;
62
+ } & T): Promise<Chat>;
63
+ /**
64
+ * Loads an existing chat session from a Prisma client or any ChatStore.
65
+ */
66
+ export declare function loadChat(prismaOrStore: unknown, llm: NodeLLMCore, chatId: string, options?: ChatOptions & {
67
+ tableNames?: TableNames;
68
+ debug?: boolean;
69
+ }): Promise<Chat | null>;
70
+ //# sourceMappingURL=Chat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Chat.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/Chat.ts"],"names":[],"mappings":"AACA,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;AAIhF,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,OA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,IAAI,GAAG,IAAI,CAAC,CAQtB"}
@@ -0,0 +1,301 @@
1
+ import { BaseChat } from "../../BaseChat.js";
2
+ import { toStore } from "./PrismaStore.js";
3
+ /**
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.
6
+ */
7
+ export class Chat extends BaseChat {
8
+ llm;
9
+ store;
10
+ persistenceConfig;
11
+ constructor(prismaOrStore, llm, record, options = {}, tableNames = {}) {
12
+ super(record, options);
13
+ this.llm = llm;
14
+ this.store = toStore(prismaOrStore, tableNames);
15
+ this.persistenceConfig = {
16
+ toolCalls: options.persistence?.toolCalls ?? true,
17
+ requests: options.persistence?.requests ?? true
18
+ };
19
+ }
20
+ /**
21
+ * Internal prep for core Chat instance with persistence hooks.
22
+ */
23
+ async prepareCoreChat(history = [], assistantMessageId) {
24
+ const provider = this.localOptions.provider || this.record.provider;
25
+ const model = this.localOptions.model || this.record.model;
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;
31
+ const coreChat = llmInstance.chat(model || undefined, {
32
+ messages: history,
33
+ ...chatOptions,
34
+ middlewares: this.customMiddlewares
35
+ });
36
+ // Register tools
37
+ if (this.customTools.length > 0) {
38
+ coreChat.withTools(this.customTools);
39
+ }
40
+ if (schema) {
41
+ coreChat.withSchema(schema);
42
+ }
43
+ // --- Persistence Hooks ---
44
+ coreChat.onToolCallStart(async (call) => {
45
+ // Only persist if toolCalls persistence is enabled
46
+ if (this.persistenceConfig.toolCalls) {
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
54
+ });
55
+ }
56
+ // User hooks
57
+ for (const h of this.userHooks.onToolCallStart)
58
+ await h(call);
59
+ });
60
+ coreChat.onToolCallEnd(async (call, result) => {
61
+ // Only persist if toolCalls persistence is enabled
62
+ if (this.persistenceConfig.toolCalls) {
63
+ const resString = typeof result === "string" ? result : JSON.stringify(result);
64
+ await this.store.finishToolCall(assistantMessageId, call.id, {
65
+ result: resString,
66
+ thought: call.thought || null
67
+ });
68
+ }
69
+ // User hooks
70
+ for (const h of this.userHooks.onToolCallEnd)
71
+ await h(call, result);
72
+ });
73
+ coreChat.afterResponse(async (finalResp) => {
74
+ this.log(`Internal afterResponse triggered. Calling ${this.userHooks.afterResponse.length} user hooks.`);
75
+ // User hooks
76
+ for (const h of this.userHooks.afterResponse) {
77
+ const modified = await h(finalResp);
78
+ if (modified)
79
+ finalResp = modified;
80
+ }
81
+ // Only persist if requests persistence is enabled
82
+ if (this.persistenceConfig.requests) {
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
+ cachedTokens: finalResp.usage?.cached_tokens,
93
+ cost: finalResp.usage?.cost || 0
94
+ });
95
+ }
96
+ return finalResp;
97
+ });
98
+ // Other core hooks
99
+ if (this.userHooks.onNewMessage.length > 0) {
100
+ coreChat.onNewMessage(async () => {
101
+ for (const h of this.userHooks.onNewMessage)
102
+ await h();
103
+ });
104
+ }
105
+ if (this.userHooks.onEndMessage.length > 0) {
106
+ coreChat.onEndMessage(async (msg) => {
107
+ for (const h of this.userHooks.onEndMessage)
108
+ await h(msg);
109
+ });
110
+ }
111
+ if (this.userHooks.onBeforeRequest.length > 0) {
112
+ coreChat.beforeRequest(async (msgs) => {
113
+ let current = msgs;
114
+ for (const h of this.userHooks.onBeforeRequest) {
115
+ const mod = await h(current);
116
+ if (mod)
117
+ current = mod;
118
+ }
119
+ return current;
120
+ });
121
+ }
122
+ if (this.userHooks.onToolCallError.length > 0) {
123
+ coreChat.onToolCallError(async (call, error) => {
124
+ // First handler to return a directive (STOP/CONTINUE/RETRY) wins,
125
+ // but every handler still runs so all of them get to observe it.
126
+ let directive;
127
+ for (const h of this.userHooks.onToolCallError) {
128
+ const value = await h(call, error);
129
+ if (value !== undefined && directive === undefined) {
130
+ directive = value;
131
+ }
132
+ }
133
+ return directive;
134
+ });
135
+ }
136
+ if (this.userHooks.onConfirmToolCall.length > 0) {
137
+ coreChat.onConfirmToolCall(async (call) => {
138
+ // Every registered handler must approve for the call to proceed.
139
+ for (const h of this.userHooks.onConfirmToolCall) {
140
+ const approved = await h(call);
141
+ if (approved === false)
142
+ return false;
143
+ }
144
+ return true;
145
+ });
146
+ }
147
+ return coreChat;
148
+ }
149
+ /**
150
+ * Send a message and persist the conversation.
151
+ */
152
+ async ask(message, options = {}) {
153
+ const userMessage = await this.store.createMessage({
154
+ chatId: this.id,
155
+ role: "user",
156
+ content: message
157
+ });
158
+ const assistantMessage = await this.store.createMessage({
159
+ chatId: this.id,
160
+ role: "assistant",
161
+ content: null
162
+ });
163
+ try {
164
+ const historyRecords = await this.store.listMessages(this.id, {
165
+ excludeIds: [userMessage.id, assistantMessage.id]
166
+ });
167
+ const history = historyRecords.map((m) => ({
168
+ role: m.role,
169
+ content: m.content || ""
170
+ }));
171
+ const coreChat = await this.prepareCoreChat(history, assistantMessage.id);
172
+ const response = await coreChat.ask(message, options);
173
+ return (await this.store.updateMessage(assistantMessage.id, {
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
+ catch (error) {
186
+ // Delete the empty assistant message, but keep the user message
187
+ // so there's a record of what was asked (useful for debugging)
188
+ await this.store.deleteMessage(assistantMessage.id);
189
+ throw error;
190
+ }
191
+ }
192
+ /**
193
+ * Stream a response and persist the conversation.
194
+ * Yields ChatChunk objects for full visibility of thinking, content, and tools.
195
+ */
196
+ async *askStream(message, options = {}) {
197
+ const userMessage = await this.store.createMessage({
198
+ chatId: this.id,
199
+ role: "user",
200
+ content: message
201
+ });
202
+ const assistantMessage = await this.store.createMessage({
203
+ chatId: this.id,
204
+ role: "assistant",
205
+ content: null
206
+ });
207
+ try {
208
+ const historyRecords = await this.store.listMessages(this.id, {
209
+ excludeIds: [userMessage.id, assistantMessage.id]
210
+ });
211
+ const history = historyRecords.map((m) => ({
212
+ role: m.role,
213
+ content: m.content || ""
214
+ }));
215
+ const coreChat = await this.prepareCoreChat(history, assistantMessage.id);
216
+ const stream = coreChat.stream(message, options);
217
+ let fullContent = "";
218
+ let metadata = {};
219
+ for await (const chunk of stream) {
220
+ if (chunk.content) {
221
+ fullContent += chunk.content;
222
+ }
223
+ // Yield the full chunk to the caller
224
+ yield chunk;
225
+ if (chunk.usage) {
226
+ metadata = { ...metadata, ...chunk.usage };
227
+ }
228
+ if (chunk.thinking) {
229
+ metadata.thinking = { ...(metadata.thinking || {}), ...chunk.thinking };
230
+ if (chunk.thinking.text) {
231
+ metadata.thinking.text = (metadata.thinking.text || "") + chunk.thinking.text;
232
+ }
233
+ }
234
+ }
235
+ return (await this.store.updateMessage(assistantMessage.id, {
236
+ content: fullContent,
237
+ contentRaw: JSON.stringify(metadata),
238
+ inputTokens: metadata.input_tokens || 0,
239
+ outputTokens: metadata.output_tokens || 0,
240
+ thinkingText: metadata.thinking?.text || null,
241
+ thinkingSignature: metadata.thinking?.signature || null,
242
+ thinkingTokens: metadata.thinking?.tokens || null,
243
+ modelId: this.localOptions.model || this.record.model || null,
244
+ provider: this.localOptions.provider || this.record.provider || null
245
+ }));
246
+ }
247
+ catch (error) {
248
+ // Delete the empty assistant message, but keep the user message
249
+ // so there's a record of what was asked (useful for debugging)
250
+ await this.store.deleteMessage(assistantMessage.id);
251
+ throw error;
252
+ }
253
+ }
254
+ /**
255
+ * Get all messages for this chat.
256
+ */
257
+ async messages() {
258
+ return (await this.store.listMessages(this.id));
259
+ }
260
+ /**
261
+ * Returns a usage summary for this chat session.
262
+ */
263
+ async stats() {
264
+ const totals = await this.store.requestTotals(this.id);
265
+ return {
266
+ input_tokens: totals.inputTokens,
267
+ output_tokens: totals.outputTokens,
268
+ total_tokens: totals.inputTokens + totals.outputTokens,
269
+ cost: totals.cost
270
+ };
271
+ }
272
+ }
273
+ /**
274
+ * Creates a new chat session, recorded through a Prisma client or any ChatStore.
275
+ */
276
+ export async function createChat(prismaOrStore, llm, options = {}) {
277
+ 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;
278
+ const store = toStore(prismaOrStore, options.tableNames);
279
+ if (options.debug)
280
+ console.log(`[@node-llm/orm] createChat: store=${store.constructor.name}`);
281
+ const record = await store.createChat({
282
+ model,
283
+ provider,
284
+ instructions,
285
+ metadata: metadata ?? null,
286
+ ...extras
287
+ });
288
+ return new Chat(store, llm, record, options, options.tableNames);
289
+ }
290
+ /**
291
+ * Loads an existing chat session from a Prisma client or any ChatStore.
292
+ */
293
+ export async function loadChat(prismaOrStore, llm, chatId, options = {}) {
294
+ const store = toStore(prismaOrStore, options.tableNames);
295
+ if (options.debug)
296
+ console.log(`[@node-llm/orm] loadChat: store=${store.constructor.name}`);
297
+ const record = await store.findChat(chatId);
298
+ if (!record)
299
+ return null;
300
+ return new Chat(store, llm, record, options, options.tableNames);
301
+ }
@@ -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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AAC,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;IAQjD,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,97 @@
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
+ const table = this.table("request");
54
+ // A database set up before v0.10.0 has no cachedTokens column; its requests save as before.
55
+ const { cachedTokens, ...rest } = request;
56
+ const data = cachedTokens !== undefined && table.fields?.cachedTokens ? request : rest;
57
+ await table.create({ data });
58
+ }
59
+ async requestTotals(chatId) {
60
+ const aggregate = await this.table("request").aggregate({
61
+ where: { chatId },
62
+ _sum: { inputTokens: true, outputTokens: true, cost: true }
63
+ });
64
+ return {
65
+ inputTokens: Number(aggregate._sum.inputTokens || 0),
66
+ outputTokens: Number(aggregate._sum.outputTokens || 0),
67
+ cost: Number(aggregate._sum.cost || 0)
68
+ };
69
+ }
70
+ async createAgentSession(session) {
71
+ return await this.table("agentSession").create({ data: session });
72
+ }
73
+ async findAgentSession(id) {
74
+ return await this.table("agentSession").findUnique({ where: { id } });
75
+ }
76
+ async updateAgentSession(id, changes) {
77
+ await this.table("agentSession").update({ where: { id }, data: changes });
78
+ }
79
+ /**
80
+ * The Prisma model for a table. Prisma usually camelCases model names (AssistantChat becomes
81
+ * assistantChat), but mapping can vary, so a case-insensitive match is accepted too.
82
+ */
83
+ table(kind) {
84
+ const name = this.tables[kind];
85
+ if (this.prisma[name])
86
+ return this.prisma[name];
87
+ const keys = Object.keys(this.prisma).filter((k) => !k.startsWith("$") && !k.startsWith("_"));
88
+ const match = keys.find((k) => k.toLowerCase() === name.toLowerCase());
89
+ if (match)
90
+ return this.prisma[match];
91
+ throw new Error(`[@node-llm/orm] Prisma table "${name}" not found. Available tables: ${keys.join(", ")}`);
92
+ }
93
+ }
94
+ /** A ChatStore as given, or a Prisma client wrapped in a PrismaStore. */
95
+ export function toStore(prismaOrStore, tableNames) {
96
+ return isChatStore(prismaOrStore) ? prismaOrStore : new PrismaStore(prismaOrStore, tableNames);
97
+ }
@@ -0,0 +1,51 @@
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 { PrismaStore, toStore } from "./PrismaStore.js";
49
+ export { AgentSession, createAgentSession, loadAgentSession } from "./AgentSession.js";
50
+ export type { AgentSessionRecord, CreateAgentSessionOptions, LoadAgentSessionOptions, TableNames } from "./AgentSession.js";
51
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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,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"}
@@ -0,0 +1,51 @@
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
+ // Stores: where chats are recorded
49
+ export { PrismaStore, toStore } from "./PrismaStore.js";
50
+ // AgentSession API
51
+ export { AgentSession, createAgentSession, loadAgentSession } from "./AgentSession.js";