@node-llm/orm 0.3.0 → 0.5.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 +71 -2
- package/README.md +94 -8
- package/bin/cli.js +40 -6
- package/dist/BaseChat.d.ts +3 -1
- package/dist/BaseChat.d.ts.map +1 -1
- package/dist/BaseChat.js +5 -0
- package/dist/adapters/prisma/AgentSession.d.ts +140 -0
- package/dist/adapters/prisma/AgentSession.d.ts.map +1 -0
- package/dist/adapters/prisma/AgentSession.js +284 -0
- package/dist/adapters/prisma/Chat.d.ts +3 -2
- package/dist/adapters/prisma/Chat.d.ts.map +1 -1
- package/dist/adapters/prisma/Chat.js +34 -4
- package/dist/adapters/prisma/index.d.ts +25 -2
- package/dist/adapters/prisma/index.d.ts.map +1 -1
- package/dist/adapters/prisma/index.js +25 -1
- package/dist/index.d.ts +21 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -1
- package/migrations/README.md +53 -0
- package/migrations/add_agent_session.sql +44 -0
- package/migrations/add_thinking_support.sql +34 -0
- package/package.json +6 -2
- package/schema.prisma +50 -33
- package/src/BaseChat.ts +8 -1
- package/src/adapters/prisma/AgentSession.ts +458 -0
- package/src/adapters/prisma/Chat.ts +55 -7
- package/src/adapters/prisma/index.ts +33 -2
- package/src/index.ts +21 -1
- package/test/AgentSession.test.ts +204 -0
- package/test/CodeWins.test.ts +116 -0
- package/test/Middleware.test.ts +137 -0
- package/test/Strictness.test.ts +117 -0
- package/test/docs/prisma-docs.test.ts +221 -0
- package/test/docs/readme-exports.test.ts +62 -0
- package/tsconfig.tsbuildinfo +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,75 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [1.11.0] - 2026-02-07 (@node-llm/core)
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
|
|
9
|
+
- **Agent Class**: Declarative DSL for defining agents with static properties (model, instructions, tools, temperature, thinking, schema).
|
|
10
|
+
- **ToolHalt Mechanism**: Early termination of agentic loops via `this.halt(result)` in Tool.execute().
|
|
11
|
+
- **defineAgent()**: Inline agent definition without creating a class.
|
|
12
|
+
- **Agent Inheritance**: Child agents inherit parent static properties.
|
|
13
|
+
- **Instance Overrides**: Override static properties at instantiation (temperature, maxTokens, etc.).
|
|
14
|
+
|
|
15
|
+
### Documentation
|
|
16
|
+
|
|
17
|
+
- Comprehensive Agent documentation with examples for RAG, model routing, and multi-agent patterns.
|
|
18
|
+
- Added doc tests for Agent, Tool, and ToolHalt patterns.
|
|
19
|
+
|
|
20
|
+
## [0.5.0] - 2026-02-07 (@node-llm/orm)
|
|
21
|
+
|
|
22
|
+
### Features
|
|
23
|
+
|
|
24
|
+
- **AgentSession**: Persistent agent conversations following "Code Wins" principle.
|
|
25
|
+
- `createAgentSession()`: Create new session with metadata.
|
|
26
|
+
- `loadAgentSession()`: Resume session by ID with class validation.
|
|
27
|
+
- Agent class defines behavior (model, tools, instructions), database provides history.
|
|
28
|
+
- **Idempotent Migrations**: Safe migration files for LlmAgentSession table.
|
|
29
|
+
- **Custom Table Names**: Support for custom table name mapping.
|
|
30
|
+
|
|
31
|
+
## [0.5.0] - 2026-02-07 (@node-llm/testing)
|
|
32
|
+
|
|
33
|
+
### Features
|
|
34
|
+
|
|
35
|
+
- **Agent Testing Support**:
|
|
36
|
+
- `mocker.callsTools()`: Mock responses that trigger multiple tool calls.
|
|
37
|
+
- `mocker.sequence()`: Return different responses for multi-turn agent conversations.
|
|
38
|
+
- `mocker.times(n)`: Limit mock matches with fallthrough behavior.
|
|
39
|
+
- **Improved Matching**: First-match semantics enable `times()` fallthrough patterns.
|
|
40
|
+
|
|
41
|
+
## [1.10.0] - 2026-02-01 (@node-llm/core)
|
|
42
|
+
|
|
43
|
+
### Features
|
|
44
|
+
|
|
45
|
+
- **Middleware Architecture**: Standardized interception layer for LLM requests and responses.
|
|
46
|
+
- **Global & Local Interceptors**: Support for global middlewares via `createLLM` and local overrides in `chat()`.
|
|
47
|
+
- **Standard Middleware Library**:
|
|
48
|
+
- `PIIMaskMiddleware`: Automated sensitive data masking.
|
|
49
|
+
- `CostGuardMiddleware`: Budget enforcement and cost monitoring.
|
|
50
|
+
- `UsageLoggerMiddleware`: Unified telemetry for token usage.
|
|
51
|
+
- **Architectural Refinement**: Enhanced the `NodeLLM` instance with lazy-initialization and internal immutability for better configuration safety in concurrent environments.
|
|
52
|
+
- **Enhanced Hooks**: New `onError` hook for robust error orchestration across the pipeline.
|
|
53
|
+
|
|
54
|
+
### Improvements
|
|
55
|
+
|
|
56
|
+
- **Streaming Stability**: Fixed critical state-sharing bug in multi-turn tool loops during streaming.
|
|
57
|
+
- **Lazy Initialization**: Better support for `dotenv` and other environment loaders in ESM.
|
|
58
|
+
|
|
59
|
+
## [0.4.0] - 2026-02-01 (@node-llm/orm)
|
|
60
|
+
|
|
61
|
+
### Features
|
|
62
|
+
|
|
63
|
+
- **Middleware Support**: Native support for LLM middlewares in `BaseChat` and `Chat` adapters.
|
|
64
|
+
- **Persistence Strictness**: Automatically strips NodeLLM runtime options (middlewares, temperature, thinking, etc.) before persisting chat records to the database.
|
|
65
|
+
- **Improved Table Lookup**: Robust Prisma table detection with case-insensitive matching and improved error messaging.
|
|
66
|
+
|
|
67
|
+
## [0.4.0] - 2026-02-01 (@node-llm/testing)
|
|
68
|
+
|
|
69
|
+
### Features
|
|
70
|
+
|
|
71
|
+
- **Middleware Support**: Added comprehensive middleware orchestration in `Mocker`, allowing testing of middleware logic with mocked LLM calls.
|
|
72
|
+
- **History Tracking**: Refined internal call history tracking with improved metadata capture.
|
|
73
|
+
|
|
5
74
|
## [0.3.0] - 2026-01-27 (@node-llm/orm)
|
|
6
75
|
|
|
7
76
|
### Features
|
|
@@ -76,7 +145,7 @@ All notable changes to this project will be documented in this file.
|
|
|
76
145
|
- Shifted from `db push` to professional **Prisma Migrate** workflow for safe, versioned schema updates.
|
|
77
146
|
- Added repository-wide `prisma/migrations` folder for reproducible deployments.
|
|
78
147
|
- New built-in scripts: `npm run db:migrate`, `npm run db:deploy`, and `npm run db:status`.
|
|
79
|
-
- Comprehensive [Database Migration Guide](https://
|
|
148
|
+
- Comprehensive [Database Migration Guide](https://nodellm.dev/orm/migrations) for application scaling.
|
|
80
149
|
|
|
81
150
|
## [1.6.2] - 2026-01-21 (@node-llm/core)
|
|
82
151
|
|
|
@@ -178,7 +247,7 @@ All notable changes to this project will be documented in this file.
|
|
|
178
247
|
- `onToolCallStart`: Triggered when a tool call begins
|
|
179
248
|
- `onToolCallEnd`: Triggered when a tool call completes successfully
|
|
180
249
|
- `onToolCallError`: Triggered when a tool call fails
|
|
181
|
-
- **Security Documentation**: New comprehensive [Security & Compliance](https://
|
|
250
|
+
- **Security Documentation**: New comprehensive [Security & Compliance](https://nodellm.dev/advanced/security.html) guide covering:
|
|
182
251
|
- Smart Context Isolation
|
|
183
252
|
- Content Policy Hooks
|
|
184
253
|
- Tool Execution Policies
|
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
> Database persistence layer for NodeLLM. Automatically tracks chats, messages, tool calls, and API requests.
|
|
9
9
|
|
|
10
|
-
**[Read the Full Documentation](https://
|
|
10
|
+
**[Read the Full Documentation](https://nodellm.dev/orm/prisma)** | **[View Example App](https://github.com/node-llm/node-llm/tree/main/examples/applications/hr-chatbot-rag)**
|
|
11
11
|
|
|
12
12
|
## Features
|
|
13
13
|
|
|
@@ -123,14 +123,15 @@ console.log(messages); // [{ role: 'user', content: '...' }, { role: 'assistant'
|
|
|
123
123
|
|
|
124
124
|
## Architecture
|
|
125
125
|
|
|
126
|
-
The ORM tracks
|
|
126
|
+
The ORM tracks five core entities:
|
|
127
127
|
|
|
128
|
-
| Model
|
|
129
|
-
|
|
|
130
|
-
| **
|
|
131
|
-
| **
|
|
132
|
-
| **
|
|
133
|
-
| **
|
|
128
|
+
| Model | Purpose | Example |
|
|
129
|
+
| ------------------- | -------------------- | ------------------------------------------ |
|
|
130
|
+
| **LlmAgentSession** | Agent persistence | Links Agent class to Chat (v0.5.0+) |
|
|
131
|
+
| **LlmChat** | Session container | Holds model, provider, system instructions |
|
|
132
|
+
| **LlmMessage** | Conversation history | User queries and assistant responses |
|
|
133
|
+
| **LlmToolCall** | Tool executions | Function calls made by the assistant |
|
|
134
|
+
| **LlmRequest** | API metrics | Token usage, latency, cost per API call |
|
|
134
135
|
|
|
135
136
|
### Data Flow
|
|
136
137
|
|
|
@@ -152,6 +153,91 @@ Chat.ask()
|
|
|
152
153
|
Return Response
|
|
153
154
|
```
|
|
154
155
|
|
|
156
|
+
## Agent Sessions (v0.5.0+)
|
|
157
|
+
|
|
158
|
+
For **stateful agents** with persistence, use `AgentSession`. This follows the "Code Wins" principle:
|
|
159
|
+
|
|
160
|
+
- **Model, Tools, Instructions** → from Agent class (code)
|
|
161
|
+
- **Message History** → from database
|
|
162
|
+
|
|
163
|
+
### Define an Agent (in @node-llm/core)
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
import { Agent, Tool, z } from "@node-llm/core";
|
|
167
|
+
|
|
168
|
+
class LookupOrderTool extends Tool {
|
|
169
|
+
static definition = {
|
|
170
|
+
name: "lookup_order",
|
|
171
|
+
description: "Look up order status",
|
|
172
|
+
parameters: z.object({ orderId: z.string() })
|
|
173
|
+
};
|
|
174
|
+
async execute({ orderId }) {
|
|
175
|
+
return { status: "shipped", eta: "Tomorrow" };
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
class SupportAgent extends Agent {
|
|
180
|
+
static model = "gpt-4.1";
|
|
181
|
+
static instructions = "You are a helpful support agent.";
|
|
182
|
+
static tools = [LookupOrderTool];
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Create & Resume Sessions
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { createAgentSession, loadAgentSession } from "@node-llm/orm/prisma";
|
|
190
|
+
|
|
191
|
+
// Create a new persistent session
|
|
192
|
+
const session = await createAgentSession(prisma, llm, SupportAgent, {
|
|
193
|
+
metadata: { userId: "user_123", ticketId: "TKT-456" }
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
await session.ask("Where is my order #789?");
|
|
197
|
+
console.log(session.id); // "sess_abc123" - save this!
|
|
198
|
+
|
|
199
|
+
// Resume later (even after code upgrades)
|
|
200
|
+
const session = await loadAgentSession(prisma, llm, SupportAgent, "sess_abc123");
|
|
201
|
+
await session.ask("Can you cancel it?");
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Code Wins Principle
|
|
205
|
+
|
|
206
|
+
When you deploy a code change (new model, updated tools), resumed sessions use the **new configuration**:
|
|
207
|
+
|
|
208
|
+
| Aspect | Source | Why |
|
|
209
|
+
| ------------ | ----------- | ------------------------------- |
|
|
210
|
+
| Model | Agent class | Immediate upgrades |
|
|
211
|
+
| Tools | Agent class | Only code can execute functions |
|
|
212
|
+
| Instructions | Agent class | Deploy prompt fixes immediately |
|
|
213
|
+
| History | Database | Sacred, never modified |
|
|
214
|
+
|
|
215
|
+
### Schema Addition
|
|
216
|
+
|
|
217
|
+
Add `LlmAgentSession` to your Prisma schema:
|
|
218
|
+
|
|
219
|
+
```prisma
|
|
220
|
+
model LlmAgentSession {
|
|
221
|
+
id String @id @default(uuid())
|
|
222
|
+
agentClass String // For validation (e.g., 'SupportAgent')
|
|
223
|
+
chatId String @unique
|
|
224
|
+
metadata Json? // Session context (userId, ticketId)
|
|
225
|
+
createdAt DateTime @default(now())
|
|
226
|
+
updatedAt DateTime @updatedAt
|
|
227
|
+
|
|
228
|
+
chat LlmChat @relation(fields: [chatId], references: [id], onDelete: Cascade)
|
|
229
|
+
|
|
230
|
+
@@index([agentClass])
|
|
231
|
+
@@index([createdAt])
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Add relation to LlmChat
|
|
235
|
+
model LlmChat {
|
|
236
|
+
// ... existing fields
|
|
237
|
+
agentSession LlmAgentSession?
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
155
241
|
## Advanced Usage
|
|
156
242
|
|
|
157
243
|
### Streaming Responses
|
package/bin/cli.js
CHANGED
|
@@ -63,21 +63,55 @@ function sync() {
|
|
|
63
63
|
"thoughtSignature"
|
|
64
64
|
];
|
|
65
65
|
|
|
66
|
+
// Check for AgentSession model (v0.5.0+)
|
|
67
|
+
const hasAgentSession =
|
|
68
|
+
userSchema.includes("LlmAgentSession") || userSchema.includes("AgentSession");
|
|
69
|
+
|
|
66
70
|
const missingFields = requiredFields.filter((field) => !userSchema.includes(field));
|
|
67
71
|
|
|
68
|
-
if (missingFields.length === 0) {
|
|
69
|
-
console.log("✓ Schema is already up to date with @node-llm/orm v0.
|
|
72
|
+
if (missingFields.length === 0 && hasAgentSession) {
|
|
73
|
+
console.log("✓ Schema is already up to date with @node-llm/orm v0.5.0 features.");
|
|
70
74
|
return;
|
|
71
75
|
}
|
|
72
76
|
|
|
73
|
-
|
|
74
|
-
|
|
77
|
+
if (missingFields.length > 0) {
|
|
78
|
+
console.log("🛠 Syncing missing fields for Extended Thinking support...");
|
|
79
|
+
console.log(`\nDetected missing fields: ${missingFields.join(", ")}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!hasAgentSession) {
|
|
83
|
+
console.log("\n🤖 Missing AgentSession support (v0.5.0+)");
|
|
84
|
+
console.log(
|
|
85
|
+
" AgentSession enables persistent agent conversations with the 'Code Wins' pattern."
|
|
86
|
+
);
|
|
87
|
+
console.log("\n To add AgentSession, copy the migration from:");
|
|
88
|
+
console.log(" node_modules/@node-llm/orm/migrations/add_agent_session.sql");
|
|
89
|
+
console.log("\n Or add this to your schema.prisma:");
|
|
90
|
+
console.log(`
|
|
91
|
+
model LlmAgentSession {
|
|
92
|
+
id String @id @default(uuid())
|
|
93
|
+
agentClass String
|
|
94
|
+
chatId String @unique
|
|
95
|
+
metadata Json?
|
|
96
|
+
createdAt DateTime @default(now())
|
|
97
|
+
updatedAt DateTime @updatedAt
|
|
98
|
+
|
|
99
|
+
chat LlmChat @relation(fields: [chatId], references: [id], onDelete: Cascade)
|
|
100
|
+
|
|
101
|
+
@@index([agentClass])
|
|
102
|
+
@@index([createdAt])
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Also add to LlmChat:
|
|
106
|
+
// agentSession LlmAgentSession?
|
|
107
|
+
`);
|
|
108
|
+
}
|
|
75
109
|
|
|
76
110
|
console.log("\nTo update your schema safely, follow the Reference Schema at:");
|
|
77
111
|
console.log("https://github.com/node-llm/node-llm/blob/main/packages/orm/schema.prisma");
|
|
78
112
|
|
|
79
113
|
console.log("\nOr run this to generate a versioned migration:");
|
|
80
|
-
console.log(" npx prisma migrate dev --name
|
|
114
|
+
console.log(" npx prisma migrate dev --name add_agent_session");
|
|
81
115
|
}
|
|
82
116
|
|
|
83
117
|
function main() {
|
|
@@ -103,7 +137,7 @@ function main() {
|
|
|
103
137
|
console.log(" npx @node-llm/orm sync # Check for missing ORM fields");
|
|
104
138
|
console.log(" npx @node-llm/orm migrate # Rails-style migration helper");
|
|
105
139
|
console.log("\nFor enterprise patterns, see:");
|
|
106
|
-
console.log(" https://
|
|
140
|
+
console.log(" https://nodellm.dev/orm/migrations");
|
|
107
141
|
}
|
|
108
142
|
}
|
|
109
143
|
|
package/dist/BaseChat.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Usage } from "@node-llm/core";
|
|
1
|
+
import type { Usage, Middleware } from "@node-llm/core";
|
|
2
2
|
export interface ChatRecord {
|
|
3
3
|
id: string;
|
|
4
4
|
model?: string | null;
|
|
@@ -28,6 +28,7 @@ export interface ChatOptions {
|
|
|
28
28
|
maxToolCalls?: number;
|
|
29
29
|
requestTimeout?: number;
|
|
30
30
|
params?: Record<string, any>;
|
|
31
|
+
middlewares?: Middleware[];
|
|
31
32
|
}
|
|
32
33
|
export interface UserHooks {
|
|
33
34
|
onToolCallStart: ((call: any) => void | Promise<void>)[];
|
|
@@ -46,6 +47,7 @@ export declare abstract class BaseChat<R extends ChatRecord = ChatRecord, O exte
|
|
|
46
47
|
id: string;
|
|
47
48
|
protected localOptions: any;
|
|
48
49
|
protected customTools: any[];
|
|
50
|
+
protected customMiddlewares: Middleware[];
|
|
49
51
|
protected userHooks: UserHooks;
|
|
50
52
|
constructor(record: R, options?: O);
|
|
51
53
|
protected log(...args: any[]): void;
|
package/dist/BaseChat.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseChat.d.ts","sourceRoot":"","sources":["../src/BaseChat.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"BaseChat.d.ts","sourceRoot":"","sources":["../src/BaseChat.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAExD,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED,MAAM,WAAW,WAAW;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC/B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,WAAW,CAAC,EAAE;QACZ,SAAS,CA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|
9
9
|
customTools = [];
|
|
10
|
+
customMiddlewares = [];
|
|
10
11
|
userHooks = {
|
|
11
12
|
onToolCallStart: [],
|
|
12
13
|
onToolCallEnd: [],
|
|
@@ -30,6 +31,10 @@ export class BaseChat {
|
|
|
30
31
|
this.localOptions.maxToolCalls = options.maxToolCalls;
|
|
31
32
|
this.localOptions.requestTimeout = options.requestTimeout;
|
|
32
33
|
this.localOptions.params = options.params;
|
|
34
|
+
// Initialize middlewares
|
|
35
|
+
if (options.middlewares) {
|
|
36
|
+
this.customMiddlewares = options.middlewares;
|
|
37
|
+
}
|
|
33
38
|
}
|
|
34
39
|
log(...args) {
|
|
35
40
|
if (this.options?.debug) {
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { ChatOptions, AskOptions, NodeLLMCore, Agent, AgentConfig, Message, ChatChunk, Usage } from "@node-llm/core";
|
|
2
|
+
/**
|
|
3
|
+
* Record structure for the LLM Agent Session table.
|
|
4
|
+
*/
|
|
5
|
+
export interface AgentSessionRecord {
|
|
6
|
+
id: string;
|
|
7
|
+
agentClass: string;
|
|
8
|
+
chatId: string;
|
|
9
|
+
metadata?: Record<string, unknown> | null;
|
|
10
|
+
createdAt: Date;
|
|
11
|
+
updatedAt: Date;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Record structure for the LLM Message table.
|
|
15
|
+
*/
|
|
16
|
+
export interface MessageRecord {
|
|
17
|
+
id: string;
|
|
18
|
+
chatId: string;
|
|
19
|
+
role: string;
|
|
20
|
+
content: string | null;
|
|
21
|
+
contentRaw?: string | null;
|
|
22
|
+
thinkingText?: string | null;
|
|
23
|
+
thinkingSignature?: string | null;
|
|
24
|
+
thinkingTokens?: number | null;
|
|
25
|
+
inputTokens?: number | null;
|
|
26
|
+
outputTokens?: number | null;
|
|
27
|
+
modelId?: string | null;
|
|
28
|
+
provider?: string | null;
|
|
29
|
+
createdAt: Date;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Table name customization.
|
|
33
|
+
*/
|
|
34
|
+
export interface TableNames {
|
|
35
|
+
agentSession?: string;
|
|
36
|
+
chat?: string;
|
|
37
|
+
message?: string;
|
|
38
|
+
toolCall?: string;
|
|
39
|
+
request?: string;
|
|
40
|
+
}
|
|
41
|
+
type AgentClass<T extends Agent = Agent> = (new (overrides?: Partial<AgentConfig & ChatOptions>) => T) & {
|
|
42
|
+
name: string;
|
|
43
|
+
model?: string;
|
|
44
|
+
instructions?: string;
|
|
45
|
+
tools?: unknown[];
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* AgentSession - Wraps an Agent instance with persistence capabilities.
|
|
49
|
+
*
|
|
50
|
+
* Follows "Code Wins" sovereignty:
|
|
51
|
+
* - Model, Tools, Instructions come from the Agent class (code)
|
|
52
|
+
* - Message history comes from the database
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // Create a new session
|
|
57
|
+
* const session = await createAgentSession(prisma, llm, SupportAgent, {
|
|
58
|
+
* metadata: { userId: "123" }
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* // Resume a session
|
|
62
|
+
* const session = await loadAgentSession(prisma, llm, SupportAgent, "sess_abc");
|
|
63
|
+
*
|
|
64
|
+
* // Agent behavior is always defined in code
|
|
65
|
+
* const result = await session.ask("Hello");
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare class AgentSession<T extends Agent = Agent> {
|
|
69
|
+
private prisma;
|
|
70
|
+
private llm;
|
|
71
|
+
private AgentClass;
|
|
72
|
+
private record;
|
|
73
|
+
private agent;
|
|
74
|
+
private currentMessageId;
|
|
75
|
+
private tableNames;
|
|
76
|
+
private debug;
|
|
77
|
+
constructor(prisma: any, llm: NodeLLMCore, AgentClass: AgentClass<T>, record: AgentSessionRecord, tableNames?: TableNames, agent?: T, debug?: boolean);
|
|
78
|
+
private log;
|
|
79
|
+
/** Agent instance (for direct access if needed) */
|
|
80
|
+
get instance(): T;
|
|
81
|
+
/** Session ID for persistence */
|
|
82
|
+
get id(): string;
|
|
83
|
+
/** Underlying chat ID */
|
|
84
|
+
get chatId(): string;
|
|
85
|
+
/** Session metadata */
|
|
86
|
+
get metadata(): Record<string, unknown> | null | undefined;
|
|
87
|
+
/** Agent class name */
|
|
88
|
+
get agentClass(): string;
|
|
89
|
+
/** Model ID used by the agent */
|
|
90
|
+
get modelId(): string;
|
|
91
|
+
/** Cumulative usage for this session (from agent memory) */
|
|
92
|
+
get totalUsage(): Usage;
|
|
93
|
+
/** Current in-memory message history */
|
|
94
|
+
get history(): readonly Message[];
|
|
95
|
+
/**
|
|
96
|
+
* Helper to get a typed Prisma model by its dynamic name.
|
|
97
|
+
*/
|
|
98
|
+
private getModel;
|
|
99
|
+
/**
|
|
100
|
+
* Send a message and persist the conversation.
|
|
101
|
+
*/
|
|
102
|
+
ask(input: string, options?: AskOptions): Promise<MessageRecord>;
|
|
103
|
+
/**
|
|
104
|
+
* Stream a response and persist the conversation.
|
|
105
|
+
*/
|
|
106
|
+
askStream(input: string, options?: AskOptions): AsyncGenerator<ChatChunk, MessageRecord, undefined>;
|
|
107
|
+
/**
|
|
108
|
+
* Returns the current full message history for this session.
|
|
109
|
+
*/
|
|
110
|
+
messages(): Promise<MessageRecord[]>;
|
|
111
|
+
/**
|
|
112
|
+
* Delete the entire session and its history.
|
|
113
|
+
*/
|
|
114
|
+
delete(): Promise<void>;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Options for creating a new agent session.
|
|
118
|
+
*/
|
|
119
|
+
export interface CreateAgentSessionOptions {
|
|
120
|
+
metadata?: Record<string, unknown>;
|
|
121
|
+
tableNames?: TableNames;
|
|
122
|
+
debug?: boolean;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Creates a new agent session and its persistent chat record.
|
|
126
|
+
*/
|
|
127
|
+
export declare function createAgentSession<T extends Agent>(prisma: any, llm: NodeLLMCore, AgentClass: AgentClass<T>, options?: CreateAgentSessionOptions): Promise<AgentSession<T>>;
|
|
128
|
+
/**
|
|
129
|
+
* Options for loading an existing agent session.
|
|
130
|
+
*/
|
|
131
|
+
export interface LoadAgentSessionOptions {
|
|
132
|
+
tableNames?: TableNames;
|
|
133
|
+
debug?: boolean;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Loads an existing agent session and re-instantiates the agent with history.
|
|
137
|
+
*/
|
|
138
|
+
export declare function loadAgentSession<T extends Agent>(prisma: any, llm: NodeLLMCore, AgentClass: AgentClass<T>, sessionId: string, options?: LoadAgentSessionOptions): Promise<AgentSession<T> | null>;
|
|
139
|
+
export {};
|
|
140
|
+
//# sourceMappingURL=AgentSession.d.ts.map
|
|
@@ -0,0 +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,EACN,MAAM,gBAAgB,CAAC;AASxB;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAC1C,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED;;GAEG;AACH,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,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,iBAAi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