@node-llm/orm 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +149 -0
- package/LICENSE +21 -0
- package/README.md +38 -14
- package/dist/BaseChat.d.ts +124 -0
- package/dist/BaseChat.d.ts.map +1 -0
- package/dist/BaseChat.js +182 -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 +178 -0
- package/dist/adapters/prisma/AgentSession.d.ts.map +1 -0
- package/dist/adapters/prisma/AgentSession.js +327 -0
- package/dist/adapters/prisma/Chat.d.ts +70 -0
- package/dist/adapters/prisma/Chat.d.ts.map +1 -0
- package/dist/adapters/prisma/Chat.js +300 -0
- 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 +51 -0
- package/dist/adapters/prisma/index.d.ts.map +1 -0
- package/dist/adapters/prisma/index.js +51 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +61 -0
- package/package.json +14 -12
- package/src/Store.ts +133 -0
- package/src/adapters/prisma/AgentSession.ts +88 -185
- package/src/adapters/prisma/Chat.ts +94 -162
- package/src/adapters/prisma/PrismaStore.ts +152 -0
- package/src/adapters/prisma/index.ts +3 -0
- package/src/index.ts +14 -0
- package/test/Store.test.ts +214 -0
- package/test/docs/readme-exports.test.ts +8 -0
- 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,G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AE,WAAW,GAAG;IAAE,UAAU,CAAC,EAAE,UAAU,CAAA;CAAE,GAAG,CAAa,GACjE,OAAO,CAAC,IAAI,CAAC,CAgCf;AAED;;GAEG;AACH,wBAAsB,QAAQ,CAC5B,aAAa,EAAE,OAAO,EACtB,GAAG,EAAE,WAAW,EAChB,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,WAAW,GAAG;IAAE,UAAU,CAAC,EAAE,UAAU,CAAC;IAAC,KAAK,CAAC,EAAE,OAAO,CAAA;CAAO,GACvE,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAQtB"}
|
|
@@ -0,0 +1,300 @@
|
|
|
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
|
+
cost: finalResp.usage?.cost || 0
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return finalResp;
|
|
96
|
+
});
|
|
97
|
+
// Other core hooks
|
|
98
|
+
if (this.userHooks.onNewMessage.length > 0) {
|
|
99
|
+
coreChat.onNewMessage(async () => {
|
|
100
|
+
for (const h of this.userHooks.onNewMessage)
|
|
101
|
+
await h();
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (this.userHooks.onEndMessage.length > 0) {
|
|
105
|
+
coreChat.onEndMessage(async (msg) => {
|
|
106
|
+
for (const h of this.userHooks.onEndMessage)
|
|
107
|
+
await h(msg);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
if (this.userHooks.onBeforeRequest.length > 0) {
|
|
111
|
+
coreChat.beforeRequest(async (msgs) => {
|
|
112
|
+
let current = msgs;
|
|
113
|
+
for (const h of this.userHooks.onBeforeRequest) {
|
|
114
|
+
const mod = await h(current);
|
|
115
|
+
if (mod)
|
|
116
|
+
current = mod;
|
|
117
|
+
}
|
|
118
|
+
return current;
|
|
119
|
+
});
|
|
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
|
+
}
|
|
146
|
+
return coreChat;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Send a message and persist the conversation.
|
|
150
|
+
*/
|
|
151
|
+
async ask(message, options = {}) {
|
|
152
|
+
const userMessage = await this.store.createMessage({
|
|
153
|
+
chatId: this.id,
|
|
154
|
+
role: "user",
|
|
155
|
+
content: message
|
|
156
|
+
});
|
|
157
|
+
const assistantMessage = await this.store.createMessage({
|
|
158
|
+
chatId: this.id,
|
|
159
|
+
role: "assistant",
|
|
160
|
+
content: null
|
|
161
|
+
});
|
|
162
|
+
try {
|
|
163
|
+
const historyRecords = await this.store.listMessages(this.id, {
|
|
164
|
+
excludeIds: [userMessage.id, assistantMessage.id]
|
|
165
|
+
});
|
|
166
|
+
const history = historyRecords.map((m) => ({
|
|
167
|
+
role: m.role,
|
|
168
|
+
content: m.content || ""
|
|
169
|
+
}));
|
|
170
|
+
const coreChat = await this.prepareCoreChat(history, assistantMessage.id);
|
|
171
|
+
const response = await coreChat.ask(message, options);
|
|
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
|
+
}));
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
// Delete the empty assistant message, but keep the user message
|
|
186
|
+
// so there's a record of what was asked (useful for debugging)
|
|
187
|
+
await this.store.deleteMessage(assistantMessage.id);
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Stream a response and persist the conversation.
|
|
193
|
+
* Yields ChatChunk objects for full visibility of thinking, content, and tools.
|
|
194
|
+
*/
|
|
195
|
+
async *askStream(message, options = {}) {
|
|
196
|
+
const userMessage = await this.store.createMessage({
|
|
197
|
+
chatId: this.id,
|
|
198
|
+
role: "user",
|
|
199
|
+
content: message
|
|
200
|
+
});
|
|
201
|
+
const assistantMessage = await this.store.createMessage({
|
|
202
|
+
chatId: this.id,
|
|
203
|
+
role: "assistant",
|
|
204
|
+
content: null
|
|
205
|
+
});
|
|
206
|
+
try {
|
|
207
|
+
const historyRecords = await this.store.listMessages(this.id, {
|
|
208
|
+
excludeIds: [userMessage.id, assistantMessage.id]
|
|
209
|
+
});
|
|
210
|
+
const history = historyRecords.map((m) => ({
|
|
211
|
+
role: m.role,
|
|
212
|
+
content: m.content || ""
|
|
213
|
+
}));
|
|
214
|
+
const coreChat = await this.prepareCoreChat(history, assistantMessage.id);
|
|
215
|
+
const stream = coreChat.stream(message, options);
|
|
216
|
+
let fullContent = "";
|
|
217
|
+
let metadata = {};
|
|
218
|
+
for await (const chunk of stream) {
|
|
219
|
+
if (chunk.content) {
|
|
220
|
+
fullContent += chunk.content;
|
|
221
|
+
}
|
|
222
|
+
// Yield the full chunk to the caller
|
|
223
|
+
yield chunk;
|
|
224
|
+
if (chunk.usage) {
|
|
225
|
+
metadata = { ...metadata, ...chunk.usage };
|
|
226
|
+
}
|
|
227
|
+
if (chunk.thinking) {
|
|
228
|
+
metadata.thinking = { ...(metadata.thinking || {}), ...chunk.thinking };
|
|
229
|
+
if (chunk.thinking.text) {
|
|
230
|
+
metadata.thinking.text = (metadata.thinking.text || "") + chunk.thinking.text;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
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
|
+
}));
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
// Delete the empty assistant message, but keep the user message
|
|
248
|
+
// so there's a record of what was asked (useful for debugging)
|
|
249
|
+
await this.store.deleteMessage(assistantMessage.id);
|
|
250
|
+
throw error;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Get all messages for this chat.
|
|
255
|
+
*/
|
|
256
|
+
async messages() {
|
|
257
|
+
return (await this.store.listMessages(this.id));
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Returns a usage summary for this chat session.
|
|
261
|
+
*/
|
|
262
|
+
async stats() {
|
|
263
|
+
const totals = await this.store.requestTotals(this.id);
|
|
264
|
+
return {
|
|
265
|
+
input_tokens: totals.inputTokens,
|
|
266
|
+
output_tokens: totals.outputTokens,
|
|
267
|
+
total_tokens: totals.inputTokens + totals.outputTokens,
|
|
268
|
+
cost: totals.cost
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Creates a new chat session, recorded through a Prisma client or any ChatStore.
|
|
274
|
+
*/
|
|
275
|
+
export async function createChat(prismaOrStore, llm, options = {}) {
|
|
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;
|
|
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
|
|
286
|
+
});
|
|
287
|
+
return new Chat(store, llm, record, options, options.tableNames);
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Loads an existing chat session from a Prisma client or any ChatStore.
|
|
291
|
+
*/
|
|
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);
|
|
297
|
+
if (!record)
|
|
298
|
+
return null;
|
|
299
|
+
return new Chat(store, llm, record, options, options.tableNames);
|
|
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,EAA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ACV,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
|
+
}
|
|
@@ -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";
|