@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
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { Agent, NodeLLM } from "@node-llm/core";
|
|
3
|
+
import { createChat, loadChat } from "../src/adapters/prisma/Chat.js";
|
|
4
|
+
import { createAgentSession, loadAgentSession } from "../src/adapters/prisma/AgentSession.js";
|
|
5
|
+
import { isChatStore, type ChatStore, type StoredMessage } from "../src/Store.js";
|
|
6
|
+
|
|
7
|
+
// Keeps everything in memory, as an app's own store might keep it behind an HTTP API.
|
|
8
|
+
class MemoryStore implements ChatStore {
|
|
9
|
+
chats = new Map<string, any>();
|
|
10
|
+
messages: StoredMessage[] = [];
|
|
11
|
+
toolCalls: any[] = [];
|
|
12
|
+
requests: any[] = [];
|
|
13
|
+
sessions = new Map<string, any>();
|
|
14
|
+
private next = 0;
|
|
15
|
+
|
|
16
|
+
private id(prefix: string) {
|
|
17
|
+
this.next += 1;
|
|
18
|
+
return `${prefix}-${this.next}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async createChat(chat: any) {
|
|
22
|
+
const record = { ...chat, id: this.id("chat"), createdAt: new Date(), updatedAt: new Date() };
|
|
23
|
+
this.chats.set(record.id, record);
|
|
24
|
+
return record;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async findChat(id: string) {
|
|
28
|
+
return this.chats.get(id) ?? null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async deleteChat(id: string) {
|
|
32
|
+
this.chats.delete(id);
|
|
33
|
+
this.messages = this.messages.filter((message) => message.chatId !== id);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async createMessage(message: { chatId: string; role: string; content: string | null }) {
|
|
37
|
+
const record = { ...message, id: this.id("message"), createdAt: new Date() };
|
|
38
|
+
this.messages.push(record);
|
|
39
|
+
return record;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async updateMessage(id: string, changes: any) {
|
|
43
|
+
const record = this.messages.find((message) => message.id === id)!;
|
|
44
|
+
Object.assign(record, changes);
|
|
45
|
+
return record;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async deleteMessage(id: string) {
|
|
49
|
+
this.messages = this.messages.filter((message) => message.id !== id);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async listMessages(chatId: string, options: { excludeIds?: string[] } = {}) {
|
|
53
|
+
return this.messages.filter(
|
|
54
|
+
(message) => message.chatId === chatId && !options.excludeIds?.includes(message.id)
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async createToolCall(toolCall: any) {
|
|
59
|
+
this.toolCalls.push({ ...toolCall });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async finishToolCall(messageId: string, toolCallId: string, finished: any) {
|
|
63
|
+
const record = this.toolCalls.find(
|
|
64
|
+
(call) => call.messageId === messageId && call.toolCallId === toolCallId
|
|
65
|
+
);
|
|
66
|
+
Object.assign(record, finished);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async createRequest(request: any) {
|
|
70
|
+
this.requests.push(request);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async requestTotals(chatId: string) {
|
|
74
|
+
const requests = this.requests.filter((request) => request.chatId === chatId);
|
|
75
|
+
return {
|
|
76
|
+
inputTokens: requests.reduce((sum, request) => sum + request.inputTokens, 0),
|
|
77
|
+
outputTokens: requests.reduce((sum, request) => sum + request.outputTokens, 0),
|
|
78
|
+
cost: requests.reduce((sum, request) => sum + request.cost, 0)
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async createAgentSession(session: any) {
|
|
83
|
+
const record = {
|
|
84
|
+
...session,
|
|
85
|
+
id: this.id("session"),
|
|
86
|
+
createdAt: new Date(),
|
|
87
|
+
updatedAt: new Date()
|
|
88
|
+
};
|
|
89
|
+
this.sessions.set(record.id, record);
|
|
90
|
+
return record;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async findAgentSession(id: string) {
|
|
94
|
+
return this.sessions.get(id) ?? null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async updateAgentSession(id: string, changes: { metadata: Record<string, unknown> }) {
|
|
98
|
+
Object.assign(this.sessions.get(id), changes);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// A model that calls one tool, then answers: it fires the hooks a real chat fires, in order.
|
|
103
|
+
const fakeLlm = () => {
|
|
104
|
+
const chat: any = {};
|
|
105
|
+
const hooks: Record<string, (...args: any[]) => any> = {};
|
|
106
|
+
for (const hook of [
|
|
107
|
+
"onToolCallStart",
|
|
108
|
+
"onToolCallEnd",
|
|
109
|
+
"afterResponse",
|
|
110
|
+
"onToolCallError",
|
|
111
|
+
"onEndMessage",
|
|
112
|
+
"beforeRequest",
|
|
113
|
+
"onNewMessage",
|
|
114
|
+
"onConfirmToolCall"
|
|
115
|
+
]) {
|
|
116
|
+
chat[hook] = (callback: (...args: any[]) => any) => {
|
|
117
|
+
hooks[hook] = callback;
|
|
118
|
+
return chat;
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
Object.assign(chat, {
|
|
122
|
+
withTools: () => chat,
|
|
123
|
+
withSchema: () => chat,
|
|
124
|
+
withInstructions: () => chat,
|
|
125
|
+
history: [],
|
|
126
|
+
totalUsage: { input_tokens: 0, output_tokens: 0 },
|
|
127
|
+
modelId: "fake-model",
|
|
128
|
+
ask: async () => {
|
|
129
|
+
const call = { id: "call-1", function: { name: "lookup", arguments: '{"sku":"PEN"}' } };
|
|
130
|
+
await hooks.onToolCallStart?.(call);
|
|
131
|
+
await hooks.onToolCallEnd?.(call, "4 in stock");
|
|
132
|
+
const response = {
|
|
133
|
+
content: "There are 4 pens.",
|
|
134
|
+
meta: {},
|
|
135
|
+
model: "fake-model",
|
|
136
|
+
provider: "fake",
|
|
137
|
+
latency: 120,
|
|
138
|
+
usage: { input_tokens: 30, output_tokens: 8, cost: 0.002 }
|
|
139
|
+
};
|
|
140
|
+
await hooks.afterResponse?.(response);
|
|
141
|
+
return response;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
return {
|
|
145
|
+
chat: () => chat,
|
|
146
|
+
withProvider: () => ({ chat: () => chat })
|
|
147
|
+
} as unknown as typeof NodeLLM;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
class StockAgent extends Agent {
|
|
151
|
+
static model = "fake-model";
|
|
152
|
+
static instructions = "Answer stock questions.";
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
describe("A custom ChatStore", () => {
|
|
156
|
+
it("records a chat's messages, tool calls and requests in the store", async () => {
|
|
157
|
+
const store = new MemoryStore();
|
|
158
|
+
|
|
159
|
+
const chat = await createChat(store, fakeLlm(), { model: "fake-model", userId: "user-1" });
|
|
160
|
+
const answer = await chat.ask("How many pens?");
|
|
161
|
+
|
|
162
|
+
expect(store.chats.get(chat.id)).toMatchObject({ model: "fake-model", userId: "user-1" });
|
|
163
|
+
expect(answer.content).toBe("There are 4 pens.");
|
|
164
|
+
expect(store.messages.map((message) => [message.role, message.content])).toEqual([
|
|
165
|
+
["user", "How many pens?"],
|
|
166
|
+
["assistant", "There are 4 pens."]
|
|
167
|
+
]);
|
|
168
|
+
expect(store.toolCalls).toEqual([
|
|
169
|
+
expect.objectContaining({ toolCallId: "call-1", name: "lookup", result: "4 in stock" })
|
|
170
|
+
]);
|
|
171
|
+
expect(store.requests).toEqual([
|
|
172
|
+
expect.objectContaining({ provider: "fake", duration: 120, inputTokens: 30, outputTokens: 8 })
|
|
173
|
+
]);
|
|
174
|
+
expect(await chat.stats()).toEqual({
|
|
175
|
+
input_tokens: 30,
|
|
176
|
+
output_tokens: 8,
|
|
177
|
+
total_tokens: 38,
|
|
178
|
+
cost: 0.002
|
|
179
|
+
});
|
|
180
|
+
expect(await loadChat(store, fakeLlm(), chat.id)).not.toBeNull();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("records an agent session, with tools given for the session and real request durations", async () => {
|
|
184
|
+
const store = new MemoryStore();
|
|
185
|
+
const lookup = { name: "lookup" };
|
|
186
|
+
let given: any;
|
|
187
|
+
class WatchedAgent extends StockAgent {
|
|
188
|
+
constructor(overrides: any) {
|
|
189
|
+
super(overrides);
|
|
190
|
+
given = overrides;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const session = await createAgentSession(store, fakeLlm(), WatchedAgent, {
|
|
195
|
+
metadata: { team: "stock" },
|
|
196
|
+
tools: [lookup as any]
|
|
197
|
+
});
|
|
198
|
+
await session.ask("How many pens?");
|
|
199
|
+
|
|
200
|
+
expect(given.tools).toEqual([lookup]);
|
|
201
|
+
expect(store.sessions.get(session.id)).toMatchObject({ agentClass: "WatchedAgent" });
|
|
202
|
+
expect(store.requests).toEqual([
|
|
203
|
+
expect.objectContaining({ chatId: session.chatId, duration: 120 })
|
|
204
|
+
]);
|
|
205
|
+
|
|
206
|
+
const loaded = await loadAgentSession(store, fakeLlm(), WatchedAgent, session.id);
|
|
207
|
+
expect(loaded?.chatId).toBe(session.chatId);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("tells a store from a Prisma client", () => {
|
|
211
|
+
expect(isChatStore(new MemoryStore())).toBe(true);
|
|
212
|
+
expect(isChatStore({ llmChat: { create: () => {} } })).toBe(false);
|
|
213
|
+
});
|
|
214
|
+
});
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
2
|
+
import { createChat } from "../src/adapters/prisma/index.js";
|
|
3
|
+
|
|
4
|
+
// Mock Prisma Client
|
|
5
|
+
const createMockPrisma = () => {
|
|
6
|
+
const messages: any[] = [];
|
|
7
|
+
const chats: any[] = [];
|
|
8
|
+
|
|
9
|
+
const tables: any = {
|
|
10
|
+
chat: {
|
|
11
|
+
create: vi.fn(async ({ data }) => {
|
|
12
|
+
const chat = { id: "chat-123", ...data, createdAt: new Date(), updatedAt: new Date() };
|
|
13
|
+
chats.push(chat);
|
|
14
|
+
return chat;
|
|
15
|
+
})
|
|
16
|
+
},
|
|
17
|
+
message: {
|
|
18
|
+
create: vi.fn(async ({ data }) => {
|
|
19
|
+
const message = { id: `msg-${messages.length}`, ...data, createdAt: new Date() };
|
|
20
|
+
messages.push(message);
|
|
21
|
+
return message;
|
|
22
|
+
}),
|
|
23
|
+
findMany: vi.fn(async ({ where }) => {
|
|
24
|
+
let filtered = messages.filter((m) => m.chatId === where.chatId);
|
|
25
|
+
if (where.id?.notIn) {
|
|
26
|
+
filtered = filtered.filter((m) => !where.id.notIn.includes(m.id));
|
|
27
|
+
}
|
|
28
|
+
return filtered;
|
|
29
|
+
}),
|
|
30
|
+
update: vi.fn(async ({ where, data }) => {
|
|
31
|
+
const message = messages.find((m) => m.id === where.id);
|
|
32
|
+
if (message) {
|
|
33
|
+
Object.assign(message, data);
|
|
34
|
+
}
|
|
35
|
+
return message;
|
|
36
|
+
}),
|
|
37
|
+
delete: vi.fn()
|
|
38
|
+
},
|
|
39
|
+
toolCall: {
|
|
40
|
+
create: vi.fn(),
|
|
41
|
+
update: vi.fn()
|
|
42
|
+
},
|
|
43
|
+
request: {
|
|
44
|
+
create: vi.fn()
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return new Proxy(tables, {
|
|
49
|
+
get(target, prop) {
|
|
50
|
+
if (prop in target) return target[prop];
|
|
51
|
+
if (typeof prop === "string") {
|
|
52
|
+
if (prop.toLowerCase().includes("chat")) return target.chat;
|
|
53
|
+
if (prop.toLowerCase().includes("message")) return target.message;
|
|
54
|
+
if (prop.toLowerCase().includes("toolcall")) return target.toolCall;
|
|
55
|
+
if (prop.toLowerCase().includes("request")) return target.request;
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// Mock NodeLLM capturing everything passed/registered on the core chat
|
|
63
|
+
const createMockLLM = () => {
|
|
64
|
+
let capturedOptions: any;
|
|
65
|
+
let capturedSchema: any;
|
|
66
|
+
|
|
67
|
+
const mockChat: any = {
|
|
68
|
+
withTools: vi.fn(() => mockChat),
|
|
69
|
+
withSchema: vi.fn((schema) => {
|
|
70
|
+
capturedSchema = schema;
|
|
71
|
+
return mockChat;
|
|
72
|
+
}),
|
|
73
|
+
onToolCallStart: vi.fn(() => mockChat),
|
|
74
|
+
onToolCallEnd: vi.fn(() => mockChat),
|
|
75
|
+
onToolCallError: vi.fn(() => mockChat),
|
|
76
|
+
onConfirmToolCall: vi.fn(() => mockChat),
|
|
77
|
+
afterResponse: vi.fn(() => mockChat),
|
|
78
|
+
onNewMessage: vi.fn(() => mockChat),
|
|
79
|
+
onEndMessage: vi.fn(() => mockChat),
|
|
80
|
+
beforeRequest: vi.fn(() => mockChat),
|
|
81
|
+
ask: vi.fn(async () => ({
|
|
82
|
+
content: "Test response",
|
|
83
|
+
meta: {},
|
|
84
|
+
usage: { input_tokens: 10, output_tokens: 5, total_tokens: 15 },
|
|
85
|
+
model: "gpt-4o-mini",
|
|
86
|
+
provider: "openai"
|
|
87
|
+
}))
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const llm: any = {
|
|
91
|
+
chat: vi.fn((_model, options) => {
|
|
92
|
+
capturedOptions = options;
|
|
93
|
+
return mockChat;
|
|
94
|
+
}),
|
|
95
|
+
withProvider: vi.fn(() => llm),
|
|
96
|
+
getCapturedOptions: () => capturedOptions,
|
|
97
|
+
getCapturedSchema: () => capturedSchema
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
return llm;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
describe("ORM toolChoice/toolCalls/schema options", () => {
|
|
104
|
+
let mockPrisma: any;
|
|
105
|
+
let mockLLM: any;
|
|
106
|
+
|
|
107
|
+
beforeEach(() => {
|
|
108
|
+
mockPrisma = createMockPrisma();
|
|
109
|
+
mockLLM = createMockLLM();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("forwards toolChoice to the underlying core chat", async () => {
|
|
113
|
+
const chat = await createChat(mockPrisma, mockLLM, {
|
|
114
|
+
model: "gpt-4o-mini",
|
|
115
|
+
toolChoice: { type: "function", function: { name: "get_weather" } }
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
await chat.ask("Hello");
|
|
119
|
+
|
|
120
|
+
expect(mockLLM.getCapturedOptions()).toEqual(
|
|
121
|
+
expect.objectContaining({
|
|
122
|
+
toolChoice: { type: "function", function: { name: "get_weather" } }
|
|
123
|
+
})
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("forwards toolCalls to the underlying core chat", async () => {
|
|
128
|
+
const chat = await createChat(mockPrisma, mockLLM, {
|
|
129
|
+
model: "gpt-4o-mini",
|
|
130
|
+
toolCalls: "one"
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
await chat.ask("Hello");
|
|
134
|
+
|
|
135
|
+
expect(mockLLM.getCapturedOptions()).toEqual(expect.objectContaining({ toolCalls: "one" }));
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("supports withToolChoice/withToolCalls fluent methods", async () => {
|
|
139
|
+
const chat = await createChat(mockPrisma, mockLLM, { model: "gpt-4o-mini" });
|
|
140
|
+
chat.withToolChoice("required").withToolCalls(1);
|
|
141
|
+
|
|
142
|
+
await chat.ask("Hello");
|
|
143
|
+
|
|
144
|
+
expect(mockLLM.getCapturedOptions()).toEqual(
|
|
145
|
+
expect.objectContaining({ toolChoice: "required", toolCalls: 1 })
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("applies a schema passed at createChat() time via withSchema(), not the raw options spread", async () => {
|
|
150
|
+
const schema = { name: "output", shape: {} };
|
|
151
|
+
const chat = await createChat(mockPrisma, mockLLM, {
|
|
152
|
+
model: "gpt-4o-mini",
|
|
153
|
+
schema
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
await chat.ask("Hello");
|
|
157
|
+
|
|
158
|
+
// schema must NOT be forwarded raw (core doesn't normalize it outside withSchema())
|
|
159
|
+
expect(mockLLM.getCapturedOptions().schema).toBeUndefined();
|
|
160
|
+
expect(mockLLM.getCapturedSchema()).toBe(schema);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("applies a schema set via the fluent withSchema() method", async () => {
|
|
164
|
+
const schema = { name: "output", shape: {} };
|
|
165
|
+
const chat = await createChat(mockPrisma, mockLLM, { model: "gpt-4o-mini" });
|
|
166
|
+
chat.withSchema(schema);
|
|
167
|
+
|
|
168
|
+
await chat.ask("Hello");
|
|
169
|
+
|
|
170
|
+
expect(mockLLM.getCapturedSchema()).toBe(schema);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("does not call withSchema when no schema is configured", async () => {
|
|
174
|
+
const chat = await createChat(mockPrisma, mockLLM, { model: "gpt-4o-mini" });
|
|
175
|
+
await chat.ask("Hello");
|
|
176
|
+
|
|
177
|
+
const mockChat = mockLLM.chat.mock.results[0].value;
|
|
178
|
+
expect(mockChat.withSchema).not.toHaveBeenCalled();
|
|
179
|
+
});
|
|
180
|
+
});
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
2
|
+
import { createChat } from "../src/adapters/prisma/index.js";
|
|
3
|
+
import { ToolExecutionMode } from "@node-llm/core";
|
|
4
|
+
|
|
5
|
+
// Mock Prisma Client
|
|
6
|
+
const createMockPrisma = () => {
|
|
7
|
+
const messages: any[] = [];
|
|
8
|
+
const chats: any[] = [];
|
|
9
|
+
|
|
10
|
+
const tables: any = {
|
|
11
|
+
chat: {
|
|
12
|
+
create: vi.fn(async ({ data }) => {
|
|
13
|
+
const chat = { id: "chat-123", ...data, createdAt: new Date(), updatedAt: new Date() };
|
|
14
|
+
chats.push(chat);
|
|
15
|
+
return chat;
|
|
16
|
+
})
|
|
17
|
+
},
|
|
18
|
+
message: {
|
|
19
|
+
create: vi.fn(async ({ data }) => {
|
|
20
|
+
const message = { id: `msg-${messages.length}`, ...data, createdAt: new Date() };
|
|
21
|
+
messages.push(message);
|
|
22
|
+
return message;
|
|
23
|
+
}),
|
|
24
|
+
findMany: vi.fn(async ({ where }) => {
|
|
25
|
+
let filtered = messages.filter((m) => m.chatId === where.chatId);
|
|
26
|
+
if (where.id?.notIn) {
|
|
27
|
+
filtered = filtered.filter((m) => !where.id.notIn.includes(m.id));
|
|
28
|
+
}
|
|
29
|
+
return filtered;
|
|
30
|
+
}),
|
|
31
|
+
update: vi.fn(async ({ where, data }) => {
|
|
32
|
+
const message = messages.find((m) => m.id === where.id);
|
|
33
|
+
if (message) {
|
|
34
|
+
Object.assign(message, data);
|
|
35
|
+
}
|
|
36
|
+
return message;
|
|
37
|
+
}),
|
|
38
|
+
delete: vi.fn()
|
|
39
|
+
},
|
|
40
|
+
toolCall: {
|
|
41
|
+
create: vi.fn(),
|
|
42
|
+
update: vi.fn()
|
|
43
|
+
},
|
|
44
|
+
request: {
|
|
45
|
+
create: vi.fn()
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return new Proxy(tables, {
|
|
50
|
+
get(target, prop) {
|
|
51
|
+
if (prop in target) return target[prop];
|
|
52
|
+
if (typeof prop === "string") {
|
|
53
|
+
if (prop.toLowerCase().includes("chat")) return target.chat;
|
|
54
|
+
if (prop.toLowerCase().includes("message")) return target.message;
|
|
55
|
+
if (prop.toLowerCase().includes("toolcall")) return target.toolCall;
|
|
56
|
+
if (prop.toLowerCase().includes("request")) return target.request;
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// Mock NodeLLM capturing everything passed/registered on the core chat
|
|
64
|
+
const createMockLLM = () => {
|
|
65
|
+
let capturedOptions: any;
|
|
66
|
+
let capturedErrorHandler: ((call: any, error: Error) => any) | undefined;
|
|
67
|
+
let capturedConfirmHandler: ((call: any) => any) | undefined;
|
|
68
|
+
|
|
69
|
+
const mockChat: any = {
|
|
70
|
+
withTools: vi.fn(() => mockChat),
|
|
71
|
+
onToolCallStart: vi.fn(() => mockChat),
|
|
72
|
+
onToolCallEnd: vi.fn(() => mockChat),
|
|
73
|
+
onToolCallError: vi.fn((handler) => {
|
|
74
|
+
capturedErrorHandler = handler;
|
|
75
|
+
return mockChat;
|
|
76
|
+
}),
|
|
77
|
+
onConfirmToolCall: vi.fn((handler) => {
|
|
78
|
+
capturedConfirmHandler = handler;
|
|
79
|
+
return mockChat;
|
|
80
|
+
}),
|
|
81
|
+
afterResponse: vi.fn(() => mockChat),
|
|
82
|
+
onNewMessage: vi.fn(() => mockChat),
|
|
83
|
+
onEndMessage: vi.fn(() => mockChat),
|
|
84
|
+
beforeRequest: vi.fn(() => mockChat),
|
|
85
|
+
ask: vi.fn(async () => ({
|
|
86
|
+
content: "Test response",
|
|
87
|
+
meta: {},
|
|
88
|
+
usage: { input_tokens: 10, output_tokens: 5, total_tokens: 15 },
|
|
89
|
+
model: "gpt-4o-mini",
|
|
90
|
+
provider: "openai"
|
|
91
|
+
}))
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const llm: any = {
|
|
95
|
+
chat: vi.fn((_model, options) => {
|
|
96
|
+
capturedOptions = options;
|
|
97
|
+
return mockChat;
|
|
98
|
+
}),
|
|
99
|
+
withProvider: vi.fn(() => llm),
|
|
100
|
+
getCapturedOptions: () => capturedOptions,
|
|
101
|
+
getErrorHandler: () => capturedErrorHandler,
|
|
102
|
+
getConfirmHandler: () => capturedConfirmHandler
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
return llm;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
describe("ORM tool execution options", () => {
|
|
109
|
+
let mockPrisma: any;
|
|
110
|
+
let mockLLM: any;
|
|
111
|
+
|
|
112
|
+
beforeEach(() => {
|
|
113
|
+
mockPrisma = createMockPrisma();
|
|
114
|
+
mockLLM = createMockLLM();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("forwards toolConcurrency to the underlying core chat", async () => {
|
|
118
|
+
const chat = await createChat(mockPrisma, mockLLM, {
|
|
119
|
+
model: "gpt-4o-mini",
|
|
120
|
+
toolConcurrency: true
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
await chat.ask("Hello");
|
|
124
|
+
|
|
125
|
+
expect(mockLLM.getCapturedOptions()).toEqual(
|
|
126
|
+
expect.objectContaining({ toolConcurrency: true })
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("forwards toolExecution mode to the underlying core chat", async () => {
|
|
131
|
+
const chat = await createChat(mockPrisma, mockLLM, {
|
|
132
|
+
model: "gpt-4o-mini",
|
|
133
|
+
toolExecution: ToolExecutionMode.CONFIRM
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
await chat.ask("Hello");
|
|
137
|
+
|
|
138
|
+
expect(mockLLM.getCapturedOptions()).toEqual(
|
|
139
|
+
expect.objectContaining({ toolExecution: ToolExecutionMode.CONFIRM })
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("supports withToolConcurrency/withToolExecution fluent methods", async () => {
|
|
144
|
+
const chat = await createChat(mockPrisma, mockLLM, { model: "gpt-4o-mini" });
|
|
145
|
+
chat.withToolConcurrency(true).withToolExecution(ToolExecutionMode.DRY_RUN);
|
|
146
|
+
|
|
147
|
+
await chat.ask("Hello");
|
|
148
|
+
|
|
149
|
+
expect(mockLLM.getCapturedOptions()).toEqual(
|
|
150
|
+
expect.objectContaining({
|
|
151
|
+
toolConcurrency: true,
|
|
152
|
+
toolExecution: ToolExecutionMode.DRY_RUN
|
|
153
|
+
})
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("does not register onToolCallError/onConfirmToolCall on the core chat when no hooks are set", async () => {
|
|
158
|
+
const chat = await createChat(mockPrisma, mockLLM, { model: "gpt-4o-mini" });
|
|
159
|
+
await chat.ask("Hello");
|
|
160
|
+
|
|
161
|
+
const mockChat = mockLLM.chat.mock.results[0].value;
|
|
162
|
+
expect(mockChat.onToolCallError).not.toHaveBeenCalled();
|
|
163
|
+
expect(mockChat.onConfirmToolCall).not.toHaveBeenCalled();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("wires onToolCallError so the first directive returned wins across all handlers", async () => {
|
|
167
|
+
const chat = await createChat(mockPrisma, mockLLM, { model: "gpt-4o-mini" });
|
|
168
|
+
const calls: string[] = [];
|
|
169
|
+
|
|
170
|
+
chat.onToolCallError(() => {
|
|
171
|
+
calls.push("first");
|
|
172
|
+
return "RETRY";
|
|
173
|
+
});
|
|
174
|
+
chat.onToolCallError(() => {
|
|
175
|
+
calls.push("second");
|
|
176
|
+
return "STOP";
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
await chat.ask("Hello");
|
|
180
|
+
|
|
181
|
+
const directive = await mockLLM.getErrorHandler()({ id: "call_1" }, new Error("boom"));
|
|
182
|
+
expect(calls).toEqual(["first", "second"]);
|
|
183
|
+
expect(directive).toBe("RETRY");
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("wires onConfirmToolCall so every handler must approve", async () => {
|
|
187
|
+
const chat = await createChat(mockPrisma, mockLLM, { model: "gpt-4o-mini" });
|
|
188
|
+
const calls: string[] = [];
|
|
189
|
+
|
|
190
|
+
chat.onConfirmToolCall(() => {
|
|
191
|
+
calls.push("approver");
|
|
192
|
+
return true;
|
|
193
|
+
});
|
|
194
|
+
chat.onConfirmToolCall(() => {
|
|
195
|
+
calls.push("vetoer");
|
|
196
|
+
return false;
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
await chat.ask("Hello");
|
|
200
|
+
|
|
201
|
+
const approved = await mockLLM.getConfirmHandler()({ id: "call_1" });
|
|
202
|
+
expect(calls).toEqual(["approver", "vetoer"]);
|
|
203
|
+
expect(approved).toBe(false);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
@@ -24,6 +24,14 @@ describe("readme-exports", () => {
|
|
|
24
24
|
});
|
|
25
25
|
});
|
|
26
26
|
|
|
27
|
+
describe("Store Exports", () => {
|
|
28
|
+
it("exports PrismaStore and isChatStore", async () => {
|
|
29
|
+
const { PrismaStore, isChatStore } = await import("../../src/index.js");
|
|
30
|
+
expect(typeof PrismaStore).toBe("function");
|
|
31
|
+
expect(typeof isChatStore).toBe("function");
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
27
35
|
describe("Type Exports", () => {
|
|
28
36
|
it("exports ChatRecord type", async () => {
|
|
29
37
|
// Type verification - just confirm the module exports it
|