@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
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @node-llm/orm
|
|
3
|
+
*
|
|
4
|
+
* Database persistence layer for NodeLLM.
|
|
5
|
+
* Automatically tracks chats, messages, tool calls, and API requests.
|
|
6
|
+
*
|
|
7
|
+
* ## Quick Start
|
|
8
|
+
*
|
|
9
|
+
* 1. Copy `schema.prisma` from this package into your project
|
|
10
|
+
* 2. Run `npx prisma migrate dev`
|
|
11
|
+
* 3. Use the ORM:
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { createChat } from '@node-llm/orm/prisma';
|
|
15
|
+
* import { prisma } from './db.js';
|
|
16
|
+
* import { llm } from './llm.js';
|
|
17
|
+
*
|
|
18
|
+
* const chat = await createChat(prisma, llm, {
|
|
19
|
+
* model: 'gpt-4',
|
|
20
|
+
* instructions: 'You are a helpful assistant.'
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* await chat.ask('Hello!');
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* ## Agent Sessions (Recommended for Agents)
|
|
27
|
+
*
|
|
28
|
+
* ```typescript
|
|
29
|
+
* import { Agent } from '@node-llm/core';
|
|
30
|
+
* import { createAgentSession, loadAgentSession } from '@node-llm/orm/prisma';
|
|
31
|
+
*
|
|
32
|
+
* class SupportAgent extends Agent {
|
|
33
|
+
* static model = 'gpt-4.1';
|
|
34
|
+
* static instructions = 'You are a helpful support agent.';
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* // Create and persist
|
|
38
|
+
* const session = await createAgentSession(prisma, llm, SupportAgent);
|
|
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
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* ## Adapters
|
|
46
|
+
*
|
|
47
|
+
* - `@node-llm/orm/prisma` - Prisma adapter (recommended)
|
|
48
|
+
*
|
|
49
|
+
* ## Schema
|
|
50
|
+
*
|
|
51
|
+
* The ORM tracks five core entities:
|
|
52
|
+
* - **AgentSession** - Links Agent class to persistent Chat (v0.5.0+)
|
|
53
|
+
* - **Chat** - Session container (model, provider, instructions)
|
|
54
|
+
* - **Message** - User/Assistant conversation history
|
|
55
|
+
* - **ToolCall** - Tool executions (name, arguments, results)
|
|
56
|
+
* - **Request** - API call metrics (tokens, latency, cost)
|
|
57
|
+
*/
|
|
58
|
+
export * from "./adapters/prisma/index.js";
|
|
59
|
+
export { isChatStore } from "./Store.js";
|
|
60
|
+
export type { ChatStore, StoredChat, StoredMessage, StoredAgentSession, NewChat, NewToolCall, NewRequest, MessageUpdate, RequestTotals } from "./Store.js";
|
|
61
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AAGH,cAAc,4BAA4B,CAAC;AAG3C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,YAAY,EACV,SAAS,EACT,UAAU,EACV,aAAa,EACb,kBAAkB,EAClB,OAAO,EACP,WAAW,EACX,UAAU,EACV,aAAa,EACb,aAAa,EACd,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @node-llm/orm
|
|
3
|
+
*
|
|
4
|
+
* Database persistence layer for NodeLLM.
|
|
5
|
+
* Automatically tracks chats, messages, tool calls, and API requests.
|
|
6
|
+
*
|
|
7
|
+
* ## Quick Start
|
|
8
|
+
*
|
|
9
|
+
* 1. Copy `schema.prisma` from this package into your project
|
|
10
|
+
* 2. Run `npx prisma migrate dev`
|
|
11
|
+
* 3. Use the ORM:
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { createChat } from '@node-llm/orm/prisma';
|
|
15
|
+
* import { prisma } from './db.js';
|
|
16
|
+
* import { llm } from './llm.js';
|
|
17
|
+
*
|
|
18
|
+
* const chat = await createChat(prisma, llm, {
|
|
19
|
+
* model: 'gpt-4',
|
|
20
|
+
* instructions: 'You are a helpful assistant.'
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* await chat.ask('Hello!');
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* ## Agent Sessions (Recommended for Agents)
|
|
27
|
+
*
|
|
28
|
+
* ```typescript
|
|
29
|
+
* import { Agent } from '@node-llm/core';
|
|
30
|
+
* import { createAgentSession, loadAgentSession } from '@node-llm/orm/prisma';
|
|
31
|
+
*
|
|
32
|
+
* class SupportAgent extends Agent {
|
|
33
|
+
* static model = 'gpt-4.1';
|
|
34
|
+
* static instructions = 'You are a helpful support agent.';
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* // Create and persist
|
|
38
|
+
* const session = await createAgentSession(prisma, llm, SupportAgent);
|
|
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
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* ## Adapters
|
|
46
|
+
*
|
|
47
|
+
* - `@node-llm/orm/prisma` - Prisma adapter (recommended)
|
|
48
|
+
*
|
|
49
|
+
* ## Schema
|
|
50
|
+
*
|
|
51
|
+
* The ORM tracks five core entities:
|
|
52
|
+
* - **AgentSession** - Links Agent class to persistent Chat (v0.5.0+)
|
|
53
|
+
* - **Chat** - Session container (model, provider, instructions)
|
|
54
|
+
* - **Message** - User/Assistant conversation history
|
|
55
|
+
* - **ToolCall** - Tool executions (name, arguments, results)
|
|
56
|
+
* - **Request** - API call metrics (tokens, latency, cost)
|
|
57
|
+
*/
|
|
58
|
+
// Re-export Prisma adapter as default
|
|
59
|
+
export * from "./adapters/prisma/index.js";
|
|
60
|
+
// Custom stores
|
|
61
|
+
export { isChatStore } from "./Store.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-llm/orm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Database persistence layer for NodeLLM - Chat, Message, and ToolCall tracking with streaming support",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -21,15 +21,6 @@
|
|
|
21
21
|
"types": "./dist/adapters/prisma/index.d.ts"
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
|
-
"scripts": {
|
|
25
|
-
"build": "prisma generate --schema=schema.prisma && tsc",
|
|
26
|
-
"test": "vitest run",
|
|
27
|
-
"test:docs": "vitest run test/docs",
|
|
28
|
-
"test:watch": "vitest",
|
|
29
|
-
"prepack": "npm run build && cp ../../CHANGELOG.md ./CHANGELOG.md",
|
|
30
|
-
"postpack": "rm ./CHANGELOG.md",
|
|
31
|
-
"prepublishOnly": "npm run build && npm run test:docs"
|
|
32
|
-
},
|
|
33
24
|
"keywords": [
|
|
34
25
|
"llm",
|
|
35
26
|
"orm",
|
|
@@ -68,6 +59,11 @@
|
|
|
68
59
|
"@node-llm/core": "^1.15.0",
|
|
69
60
|
"@prisma/client": "^5.0.0"
|
|
70
61
|
},
|
|
62
|
+
"peerDependenciesMeta": {
|
|
63
|
+
"@prisma/client": {
|
|
64
|
+
"optional": true
|
|
65
|
+
}
|
|
66
|
+
},
|
|
71
67
|
"devDependencies": {
|
|
72
68
|
"@node-llm/core": "file:../core",
|
|
73
69
|
"@prisma/client": "^5.0.0",
|
|
@@ -76,5 +72,11 @@
|
|
|
76
72
|
"typescript": "^5.0.0",
|
|
77
73
|
"vitest": "^4.0.0"
|
|
78
74
|
},
|
|
79
|
-
"dependencies": {}
|
|
80
|
-
|
|
75
|
+
"dependencies": {},
|
|
76
|
+
"scripts": {
|
|
77
|
+
"build": "tsc",
|
|
78
|
+
"test": "vitest run",
|
|
79
|
+
"test:docs": "vitest run test/docs",
|
|
80
|
+
"test:watch": "vitest"
|
|
81
|
+
}
|
|
82
|
+
}
|
package/src/Store.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Where an ORM chat or agent session keeps its records. The Prisma store is the default; an app
|
|
5
|
+
* can pass its own to keep the same records anywhere else, such as behind its own HTTP API.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface StoredChat {
|
|
9
|
+
id: string;
|
|
10
|
+
model?: string | null;
|
|
11
|
+
provider?: string | null;
|
|
12
|
+
instructions?: string | null;
|
|
13
|
+
metadata?: any;
|
|
14
|
+
createdAt: Date;
|
|
15
|
+
updatedAt: Date;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface StoredMessage {
|
|
19
|
+
id: string;
|
|
20
|
+
chatId: string;
|
|
21
|
+
role: string;
|
|
22
|
+
content: string | null;
|
|
23
|
+
contentRaw?: string | null;
|
|
24
|
+
reasoning?: string | null;
|
|
25
|
+
inputTokens?: number | null;
|
|
26
|
+
outputTokens?: number | null;
|
|
27
|
+
thinkingText?: string | null;
|
|
28
|
+
thinkingSignature?: string | null;
|
|
29
|
+
thinkingTokens?: number | null;
|
|
30
|
+
modelId?: string | null;
|
|
31
|
+
provider?: string | null;
|
|
32
|
+
createdAt: Date;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface StoredAgentSession {
|
|
36
|
+
id: string;
|
|
37
|
+
agentClass: string;
|
|
38
|
+
chatId: string;
|
|
39
|
+
metadata?: Record<string, unknown> | null;
|
|
40
|
+
createdAt: Date;
|
|
41
|
+
updatedAt: Date;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface NewChat {
|
|
45
|
+
model?: string | null;
|
|
46
|
+
provider?: string | null;
|
|
47
|
+
instructions?: string | null;
|
|
48
|
+
metadata?: any;
|
|
49
|
+
/** Extra columns an app adds to its chat table, such as a user id */
|
|
50
|
+
[column: string]: unknown;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type MessageUpdate = Partial<Omit<StoredMessage, "id" | "chatId" | "role" | "createdAt">>;
|
|
54
|
+
|
|
55
|
+
export interface NewToolCall {
|
|
56
|
+
messageId: string;
|
|
57
|
+
toolCallId: string;
|
|
58
|
+
name: string;
|
|
59
|
+
arguments: string;
|
|
60
|
+
thought?: string | null;
|
|
61
|
+
thoughtSignature?: string | null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface NewRequest {
|
|
65
|
+
chatId: string;
|
|
66
|
+
messageId: string | null;
|
|
67
|
+
provider: string;
|
|
68
|
+
model: string;
|
|
69
|
+
statusCode: number;
|
|
70
|
+
/** Milliseconds */
|
|
71
|
+
duration: number;
|
|
72
|
+
inputTokens: number;
|
|
73
|
+
outputTokens: number;
|
|
74
|
+
cost: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface RequestTotals {
|
|
78
|
+
inputTokens: number;
|
|
79
|
+
outputTokens: number;
|
|
80
|
+
cost: number;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface ChatStore {
|
|
84
|
+
createChat(chat: NewChat): Promise<StoredChat>;
|
|
85
|
+
findChat(id: string): Promise<StoredChat | null>;
|
|
86
|
+
/** Removes the chat with everything recorded under it */
|
|
87
|
+
deleteChat(id: string): Promise<void>;
|
|
88
|
+
|
|
89
|
+
createMessage(message: {
|
|
90
|
+
chatId: string;
|
|
91
|
+
role: string;
|
|
92
|
+
content: string | null;
|
|
93
|
+
}): Promise<StoredMessage>;
|
|
94
|
+
updateMessage(id: string, changes: MessageUpdate): Promise<StoredMessage>;
|
|
95
|
+
deleteMessage(id: string): Promise<void>;
|
|
96
|
+
/** A chat's messages, oldest first, leaving out any listed in excludeIds */
|
|
97
|
+
listMessages(chatId: string, options?: { excludeIds?: string[] }): Promise<StoredMessage[]>;
|
|
98
|
+
|
|
99
|
+
createToolCall(toolCall: NewToolCall): Promise<void>;
|
|
100
|
+
finishToolCall(
|
|
101
|
+
messageId: string,
|
|
102
|
+
toolCallId: string,
|
|
103
|
+
finished: { result: string; thought?: string | null }
|
|
104
|
+
): Promise<void>;
|
|
105
|
+
|
|
106
|
+
createRequest(request: NewRequest): Promise<void>;
|
|
107
|
+
requestTotals(chatId: string): Promise<RequestTotals>;
|
|
108
|
+
|
|
109
|
+
createAgentSession(session: {
|
|
110
|
+
agentClass: string;
|
|
111
|
+
chatId: string;
|
|
112
|
+
metadata: Record<string, unknown> | null;
|
|
113
|
+
}): Promise<StoredAgentSession>;
|
|
114
|
+
findAgentSession(id: string): Promise<StoredAgentSession | null>;
|
|
115
|
+
updateAgentSession(id: string, changes: { metadata: Record<string, unknown> }): Promise<void>;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const storeMethods: (keyof ChatStore)[] = [
|
|
119
|
+
"createChat",
|
|
120
|
+
"createMessage",
|
|
121
|
+
"updateMessage",
|
|
122
|
+
"listMessages",
|
|
123
|
+
"createRequest"
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
/** Whether a value is a ChatStore rather than a Prisma client. */
|
|
127
|
+
export function isChatStore(value: unknown): value is ChatStore {
|
|
128
|
+
return (
|
|
129
|
+
typeof value === "object" &&
|
|
130
|
+
value !== null &&
|
|
131
|
+
storeMethods.every((method) => typeof (value as any)[method] === "function")
|
|
132
|
+
);
|
|
133
|
+
}
|