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