@node-llm/orm 0.10.1 → 0.12.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.
Files changed (47) hide show
  1. package/CHANGELOG.md +139 -0
  2. package/dist/BaseChat.d.ts +2 -2
  3. package/dist/BaseChat.d.ts.map +1 -1
  4. package/dist/Store.d.ts +28 -1
  5. package/dist/Store.d.ts.map +1 -1
  6. package/dist/adapters/prisma/AgentSession.d.ts +42 -9
  7. package/dist/adapters/prisma/AgentSession.d.ts.map +1 -1
  8. package/dist/adapters/prisma/AgentSession.js +89 -16
  9. package/dist/adapters/prisma/Chat.d.ts +5 -0
  10. package/dist/adapters/prisma/Chat.d.ts.map +1 -1
  11. package/dist/adapters/prisma/Chat.js +38 -4
  12. package/dist/adapters/prisma/PrismaStore.d.ts +4 -2
  13. package/dist/adapters/prisma/PrismaStore.d.ts.map +1 -1
  14. package/dist/adapters/prisma/PrismaStore.js +15 -2
  15. package/dist/adapters/prisma/index.d.ts +1 -1
  16. package/dist/adapters/prisma/index.d.ts.map +1 -1
  17. package/dist/errors.d.ts +14 -0
  18. package/dist/errors.d.ts.map +1 -0
  19. package/dist/errors.js +15 -0
  20. package/dist/history.d.ts +10 -0
  21. package/dist/history.d.ts.map +1 -0
  22. package/dist/history.js +42 -0
  23. package/dist/index.d.ts +3 -1
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +2 -0
  26. package/dist/redact.d.ts +20 -0
  27. package/dist/redact.d.ts.map +1 -0
  28. package/dist/redact.js +118 -0
  29. package/package.json +2 -2
  30. package/src/BaseChat.ts +2 -2
  31. package/src/Store.ts +30 -1
  32. package/src/adapters/prisma/AgentSession.ts +149 -25
  33. package/src/adapters/prisma/Chat.ts +45 -5
  34. package/src/adapters/prisma/PrismaStore.ts +21 -3
  35. package/src/adapters/prisma/index.ts +3 -0
  36. package/src/errors.ts +15 -0
  37. package/src/history.ts +48 -0
  38. package/src/index.ts +4 -0
  39. package/src/redact.ts +157 -0
  40. package/test/AgentSession.test.ts +12 -2
  41. package/test/CountCall.test.ts +147 -0
  42. package/test/Redact.test.ts +228 -0
  43. package/test/Resume.test.ts +232 -0
  44. package/test/SessionData.test.ts +109 -0
  45. package/test/SessionOverrides.test.ts +130 -0
  46. package/test/StoreStops.test.ts +262 -0
  47. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,109 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { Agent, createLLM, z } from "@node-llm/core";
3
+ import type { ChatRequest, ChatResponse, Message, Provider } from "@node-llm/core";
4
+ import { createAgentSession } from "../src/adapters/prisma/AgentSession.js";
5
+ import type { ChatStore, StoredMessage } from "../src/Store.js";
6
+
7
+ class MemoryStore implements ChatStore {
8
+ messages: StoredMessage[] = [];
9
+ sessions = new Map<string, any>();
10
+ private next = 0;
11
+ private id(prefix: string) {
12
+ this.next += 1;
13
+ return `${prefix}-${this.next}`;
14
+ }
15
+ async createChat(chat: any) {
16
+ return { ...chat, id: this.id("chat"), createdAt: new Date(), updatedAt: new Date() };
17
+ }
18
+ async findChat() {
19
+ return null;
20
+ }
21
+ async deleteChat() {}
22
+ async createMessage(message: { chatId: string; role: string; content: string | null }) {
23
+ const record = { ...message, id: this.id("message"), createdAt: new Date() };
24
+ this.messages.push(record);
25
+ return record;
26
+ }
27
+ async updateMessage(id: string, changes: any) {
28
+ return Object.assign(this.messages.find((m) => m.id === id)!, changes);
29
+ }
30
+ async deleteMessage(id: string) {
31
+ this.messages = this.messages.filter((m) => m.id !== id);
32
+ }
33
+ async listMessages(chatId: string) {
34
+ return this.messages.filter((m) => m.chatId === chatId);
35
+ }
36
+ async createToolCall() {}
37
+ async finishToolCall() {}
38
+ async createRequest() {}
39
+ async requestTotals() {
40
+ return { inputTokens: 0, outputTokens: 0, cost: 0 };
41
+ }
42
+ async createAgentSession(session: any) {
43
+ const record = {
44
+ ...session,
45
+ id: this.id("session"),
46
+ createdAt: new Date(),
47
+ updatedAt: new Date()
48
+ };
49
+ this.sessions.set(record.id, record);
50
+ return record;
51
+ }
52
+ async findAgentSession(id: string) {
53
+ return this.sessions.get(id) ?? null;
54
+ }
55
+ async updateAgentSession() {}
56
+ }
57
+
58
+ const usage = { input_tokens: 10, output_tokens: 5, total_tokens: 15 };
59
+ class OneAnswer implements Provider {
60
+ id = "scripted";
61
+ constructor(private content: string) {}
62
+ defaultModel() {
63
+ return "test-model";
64
+ }
65
+ async chat(_request: ChatRequest): Promise<ChatResponse> {
66
+ return { content: this.content, usage };
67
+ }
68
+ formatToolResultMessage(toolCallId: string, content: string): Message {
69
+ return { role: "tool", tool_call_id: toolCallId, content };
70
+ }
71
+ }
72
+
73
+ const proposal = z.object({ action: z.enum(["click", "fill"]), target: z.string() });
74
+
75
+ class Repairer extends Agent<Record<string, never>, z.infer<typeof proposal>> {
76
+ static model = "test-model";
77
+ static instructions = "Propose a repair.";
78
+ static schema = proposal;
79
+ static assumeModelExists = true;
80
+ }
81
+
82
+ describe("an agent session's answer", () => {
83
+ it("has the parsed data, found inside a code fence", async () => {
84
+ const llm = createLLM({
85
+ provider: new OneAnswer('Here it is:\n```json\n{"action":"click","target":"Save"}\n```')
86
+ });
87
+ const session = await createAgentSession(new MemoryStore(), llm, Repairer);
88
+
89
+ const answer = await session.ask("The Save button moved.");
90
+
91
+ expect(answer.data).toEqual({ action: "click", target: "Save" });
92
+ expect(answer.data.action).toBe("click");
93
+ // @ts-expect-error data is typed from the schema
94
+ void answer.data.missing;
95
+ // The saved record is unchanged: data is not a column.
96
+ expect(Object.keys(answer)).not.toContain("data");
97
+ expect(JSON.parse(JSON.stringify(answer))).not.toHaveProperty("data");
98
+ });
99
+
100
+ it("throws the schema's readable error when the answer doesn't match", async () => {
101
+ const llm = createLLM({ provider: new OneAnswer('{"action":"hover","target":"Save"}') });
102
+ const session = await createAgentSession(new MemoryStore(), llm, Repairer);
103
+
104
+ const answer = await session.ask("The Save button moved.");
105
+
106
+ expect(() => answer.data).toThrow(/action/);
107
+ expect(answer.content).toBe('{"action":"hover","target":"Save"}');
108
+ });
109
+ });
@@ -0,0 +1,130 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { Agent, CostLimitError, createLLM } from "@node-llm/core";
3
+ import type { ChatRequest, ChatResponse, Message, Provider } from "@node-llm/core";
4
+ import { createAgentSession, loadAgentSession } from "../src/adapters/prisma/AgentSession.js";
5
+ import type { ChatStore, StoredMessage } from "../src/Store.js";
6
+
7
+ class MemoryStore implements ChatStore {
8
+ messages: StoredMessage[] = [];
9
+ sessions = new Map<string, any>();
10
+ private next = 0;
11
+ private id(prefix: string) {
12
+ this.next += 1;
13
+ return `${prefix}-${this.next}`;
14
+ }
15
+ async createChat(chat: any) {
16
+ return { ...chat, id: this.id("chat"), createdAt: new Date(), updatedAt: new Date() };
17
+ }
18
+ async findChat() {
19
+ return null;
20
+ }
21
+ async deleteChat() {}
22
+ async createMessage(message: { chatId: string; role: string; content: string | null }) {
23
+ const record = { ...message, id: this.id("message"), createdAt: new Date() };
24
+ this.messages.push(record);
25
+ return record;
26
+ }
27
+ async updateMessage(id: string, changes: any) {
28
+ return Object.assign(this.messages.find((m) => m.id === id)!, changes);
29
+ }
30
+ async deleteMessage(id: string) {
31
+ this.messages = this.messages.filter((m) => m.id !== id);
32
+ }
33
+ async listMessages(chatId: string) {
34
+ return this.messages.filter((m) => m.chatId === chatId);
35
+ }
36
+ async createToolCall() {}
37
+ async finishToolCall() {}
38
+ async createRequest() {}
39
+ async requestTotals() {
40
+ return { inputTokens: 0, outputTokens: 0, cost: 0 };
41
+ }
42
+ async createAgentSession(session: any) {
43
+ const record = {
44
+ ...session,
45
+ id: this.id("session"),
46
+ createdAt: new Date(),
47
+ updatedAt: new Date()
48
+ };
49
+ this.sessions.set(record.id, record);
50
+ return record;
51
+ }
52
+ async findAgentSession(id: string) {
53
+ return this.sessions.get(id) ?? null;
54
+ }
55
+ async updateAgentSession() {}
56
+ }
57
+
58
+ // Calls a tool forever, each request costing $0.01.
59
+ class Looping implements Provider {
60
+ id = "scripted";
61
+ calls = 0;
62
+ defaultModel() {
63
+ return "test-model";
64
+ }
65
+ async chat(_request: ChatRequest): Promise<ChatResponse> {
66
+ this.calls++;
67
+ return {
68
+ content: null,
69
+ usage: { input_tokens: 10, output_tokens: 5, total_tokens: 15, cost: 0.01 },
70
+ tool_calls: [
71
+ { id: `c${this.calls}`, type: "function", function: { name: "look", arguments: "{}" } }
72
+ ]
73
+ };
74
+ }
75
+ formatToolResultMessage(toolCallId: string, content: string): Message {
76
+ return { role: "tool", tool_call_id: toolCallId, content };
77
+ }
78
+ }
79
+
80
+ const look = {
81
+ type: "function" as const,
82
+ function: { name: "look", description: "Look", parameters: { type: "object", properties: {} } },
83
+ handler: async () => "a page"
84
+ };
85
+
86
+ class Explorer extends Agent {
87
+ static model = "test-model";
88
+ static tools = [look];
89
+ static maxToolCalls = 100;
90
+ static assumeModelExists = true;
91
+ }
92
+
93
+ describe("agent settings for one session", () => {
94
+ it("applies the session's maxCost in place of the class's", async () => {
95
+ const provider = new Looping();
96
+ const session = await createAgentSession(new MemoryStore(), createLLM({ provider }), Explorer, {
97
+ maxCost: 0.03
98
+ });
99
+
100
+ await expect(session.ask("Explore")).rejects.toBeInstanceOf(CostLimitError);
101
+ expect(provider.calls).toBe(3);
102
+ });
103
+
104
+ it("applies them again when the session is loaded", async () => {
105
+ const store = new MemoryStore();
106
+ const created = await createAgentSession(
107
+ store,
108
+ createLLM({ provider: new Looping() }),
109
+ Explorer
110
+ );
111
+ const provider = new Looping();
112
+
113
+ const loaded = await loadAgentSession(store, createLLM({ provider }), Explorer, created.id, {
114
+ maxToolCalls: 2
115
+ });
116
+
117
+ await expect(loaded!.ask("Explore")).rejects.toMatchObject({ limit: 2 });
118
+ });
119
+
120
+ it("keeps the class's settings when none are given", async () => {
121
+ const provider = new Looping();
122
+ const session = await createAgentSession(new MemoryStore(), createLLM({ provider }), Explorer, {
123
+ maxToolCalls: undefined,
124
+ maxCost: 0.02
125
+ });
126
+
127
+ await expect(session.ask("Explore")).rejects.toBeInstanceOf(CostLimitError);
128
+ expect(provider.calls).toBe(2);
129
+ });
130
+ });
@@ -0,0 +1,262 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { Agent, createLLM } from "@node-llm/core";
3
+ import type { ChatRequest, ChatResponse, Message, Provider } from "@node-llm/core";
4
+ import { createAgentSession } from "../src/adapters/prisma/AgentSession.js";
5
+ import { createChat } from "../src/adapters/prisma/Chat.js";
6
+ import { StoppedByStoreError } from "../src/index.js";
7
+ import type { ChatStore, NewRequest, RequestRecorded, StoredMessage } from "../src/Store.js";
8
+
9
+ // Says to stop once it has recorded `limit` requests, as a store that keeps a monthly budget might.
10
+ class BudgetStore implements ChatStore {
11
+ messages: StoredMessage[] = [];
12
+ requests: NewRequest[] = [];
13
+ toolCalls: any[] = [];
14
+ sessions = new Map<string, any>();
15
+ private next = 0;
16
+ constructor(private limit: number) {}
17
+ private id(prefix: string) {
18
+ this.next += 1;
19
+ return `${prefix}-${this.next}`;
20
+ }
21
+ async createChat(chat: any) {
22
+ return { ...chat, id: this.id("chat"), createdAt: new Date(), updatedAt: new Date() };
23
+ }
24
+ async findChat() {
25
+ return null;
26
+ }
27
+ async deleteChat() {}
28
+ async createMessage(message: { chatId: string; role: string; content: string | null }) {
29
+ const record = { ...message, id: this.id("message"), createdAt: new Date() };
30
+ this.messages.push(record);
31
+ return record;
32
+ }
33
+ async updateMessage(id: string, changes: any) {
34
+ return Object.assign(this.messages.find((m) => m.id === id)!, changes);
35
+ }
36
+ async deleteMessage(id: string) {
37
+ this.messages = this.messages.filter((m) => m.id !== id);
38
+ }
39
+ async listMessages(chatId: string, options: { excludeIds?: string[] } = {}) {
40
+ return this.messages.filter((m) => m.chatId === chatId && !options.excludeIds?.includes(m.id));
41
+ }
42
+ async createToolCall(call: any) {
43
+ this.toolCalls.push(call);
44
+ }
45
+ async finishToolCall() {}
46
+ async createRequest(request: NewRequest): Promise<RequestRecorded | void> {
47
+ this.requests.push(request);
48
+ if (this.requests.length >= this.limit) return { stop: "The monthly AI limit is reached." };
49
+ }
50
+ async requestTotals() {
51
+ return { inputTokens: 0, outputTokens: 0, cost: 0 };
52
+ }
53
+ async createAgentSession(session: any) {
54
+ const record = {
55
+ ...session,
56
+ id: this.id("session"),
57
+ createdAt: new Date(),
58
+ updatedAt: new Date()
59
+ };
60
+ this.sessions.set(record.id, record);
61
+ return record;
62
+ }
63
+ async findAgentSession(id: string) {
64
+ return this.sessions.get(id) ?? null;
65
+ }
66
+ async updateAgentSession() {}
67
+ }
68
+
69
+ // Calls `look` a few times, then answers.
70
+ class Looking implements Provider {
71
+ id = "scripted";
72
+ requests: ChatRequest[] = [];
73
+ constructor(private looks = 5) {}
74
+ defaultModel() {
75
+ return "test-model";
76
+ }
77
+ async chat(request: ChatRequest): Promise<ChatResponse> {
78
+ this.requests.push(request);
79
+ const usage = { input_tokens: 10, output_tokens: 5, total_tokens: 15 };
80
+ if (this.requests.length > this.looks) return { content: "Done.", usage };
81
+ return {
82
+ content: null,
83
+ usage,
84
+ tool_calls: [
85
+ {
86
+ id: `c${this.requests.length}`,
87
+ type: "function",
88
+ function: { name: "look", arguments: "{}" }
89
+ }
90
+ ]
91
+ };
92
+ }
93
+ formatToolResultMessage(toolCallId: string, content: string): Message {
94
+ return { role: "tool", tool_call_id: toolCallId, content };
95
+ }
96
+ }
97
+
98
+ const look = {
99
+ type: "function" as const,
100
+ function: { name: "look", description: "Look", parameters: { type: "object", properties: {} } },
101
+ handler: async () => "a page"
102
+ };
103
+
104
+ class Explorer extends Agent {
105
+ static model = "test-model";
106
+ static tools = [look];
107
+ static maxToolCalls = 20;
108
+ static assumeModelExists = true;
109
+ }
110
+
111
+ describe("a store that stops a run", () => {
112
+ it("stops an agent session before its next request, with the store's reason", async () => {
113
+ const store = new BudgetStore(2);
114
+ const provider = new Looking();
115
+ const session = await createAgentSession(store, createLLM({ provider }), Explorer);
116
+
117
+ const error = await session.ask("Explore").catch((caught) => caught);
118
+
119
+ expect(error).toBeInstanceOf(StoppedByStoreError);
120
+ expect(error).toMatchObject({
121
+ reason: "The monthly AI limit is reached.",
122
+ code: "STOPPED_BY_STORE",
123
+ message: "The monthly AI limit is reached."
124
+ });
125
+ expect(provider.requests).toHaveLength(2);
126
+ // The tool call the second answer asked for ran, so the history stays valid.
127
+ expect(session.history.filter((m) => m.role === "tool")).toHaveLength(2);
128
+ });
129
+
130
+ it("stops the session's next ask too", async () => {
131
+ const provider = new Looking(0);
132
+ const session = await createAgentSession(new BudgetStore(1), createLLM({ provider }), Explorer);
133
+ await session.ask("Hello");
134
+
135
+ await expect(session.ask("Again")).rejects.toBeInstanceOf(StoppedByStoreError);
136
+ expect(provider.requests).toHaveLength(1);
137
+ });
138
+
139
+ it("stops a chat too", async () => {
140
+ const provider = new Looking();
141
+ const chat = await createChat(new BudgetStore(3), createLLM({ provider }), {
142
+ model: "test-model",
143
+ assumeModelExists: true,
144
+ maxToolCalls: 20
145
+ } as any);
146
+
147
+ await expect(chat.withTool(look as any).ask("Explore")).rejects.toBeInstanceOf(
148
+ StoppedByStoreError
149
+ );
150
+ expect(provider.requests).toHaveLength(3);
151
+ });
152
+
153
+ it("carries on when the store answers nothing", async () => {
154
+ const provider = new Looking(1);
155
+ const session = await createAgentSession(
156
+ new BudgetStore(100),
157
+ createLLM({ provider }),
158
+ Explorer
159
+ );
160
+
161
+ expect((await session.ask("Explore")).content).toBe("Done.");
162
+ });
163
+ });
164
+
165
+ describe("a store and subagents", () => {
166
+ // The lead sends three workers through run_agents, one at a time; workers answer at once.
167
+ class Fanning implements Provider {
168
+ id = "scripted";
169
+ requests: ChatRequest[] = [];
170
+ defaultModel() {
171
+ return "test-model";
172
+ }
173
+ async chat(request: ChatRequest): Promise<ChatResponse> {
174
+ this.requests.push(request);
175
+ const usage = { input_tokens: 10, output_tokens: 5, total_tokens: 15, cost: 0.01 };
176
+ const system = String(request.messages.find((m) => m.role === "system")?.content ?? "");
177
+ const last = request.messages[request.messages.length - 1];
178
+ if (system === "Lead" && last?.role !== "tool") {
179
+ const tasks = ["a", "b", "c"].map((task) => ({ agent: "look", task }));
180
+ return {
181
+ content: null,
182
+ usage,
183
+ tool_calls: [
184
+ {
185
+ id: "fan",
186
+ type: "function",
187
+ function: { name: "run_agents", arguments: JSON.stringify({ tasks }) }
188
+ }
189
+ ]
190
+ };
191
+ }
192
+ return { content: last?.role === "tool" ? String(last.content) : "looked", usage };
193
+ }
194
+ formatToolResultMessage(toolCallId: string, content: string): Message {
195
+ return { role: "tool", tool_call_id: toolCallId, content };
196
+ }
197
+ }
198
+
199
+ async function leadSession(limit: number) {
200
+ const { agentsTool } = await import("@node-llm/core");
201
+ const provider = new Fanning();
202
+ const llm = createLLM({ provider });
203
+ class Look extends Agent {
204
+ static model = "test-model";
205
+ static instructions = "Look";
206
+ static assumeModelExists = true;
207
+ }
208
+ class Lead extends Agent {
209
+ static model = "test-model";
210
+ static instructions = "Lead";
211
+ static assumeModelExists = true;
212
+ static tools = [agentsTool({ agents: { look: Look }, llm, concurrency: 1 })];
213
+ }
214
+ const store = new BudgetStore(limit);
215
+ const session = await createAgentSession(store, llm, Lead);
216
+ return { store, session, provider };
217
+ }
218
+
219
+ it("records each worker's request under the lead's answer", async () => {
220
+ const { store, session } = await leadSession(100);
221
+ await session.ask("Look at the app");
222
+ // The lead's two requests and the three workers'.
223
+ expect(store.requests).toHaveLength(5);
224
+ expect(store.requests.every((r) => r.cost === 0.01)).toBe(true);
225
+ });
226
+
227
+ it("stops the workers when the store says stop", async () => {
228
+ const { store, session, provider } = await leadSession(2);
229
+ const error = await session.ask("Look at the app").catch((caught) => caught);
230
+ // Lead request 1, worker a's request 2 says stop: b and c are stopped, then the lead.
231
+ expect(error).toBeInstanceOf(StoppedByStoreError);
232
+ expect(provider.requests).toHaveLength(2);
233
+ const result = JSON.parse(
234
+ String(session.history.find((m) => m.role === "tool")?.content ?? "[]")
235
+ );
236
+ expect(result.map((r: { status: string }) => r.status)).toEqual(["done", "failed", "failed"]);
237
+ expect(store.requests).toHaveLength(2);
238
+ });
239
+ });
240
+
241
+ describe("a store and model routing", () => {
242
+ it("records each request with the model its turn used", async () => {
243
+ const store = new BudgetStore(100);
244
+ const provider = new Looking(2);
245
+ class Routed extends Explorer {
246
+ static routeModel = (step: { toolCalls: unknown[] }) =>
247
+ step.toolCalls.length > 0 ? "cheap-model" : undefined;
248
+ }
249
+ const session = await createAgentSession(store, createLLM({ provider }), Routed);
250
+ await session.ask("Explore");
251
+ expect(provider.requests.map((r) => r.model)).toEqual([
252
+ "test-model",
253
+ "cheap-model",
254
+ "cheap-model"
255
+ ]);
256
+ expect(store.requests.map((r) => r.model)).toEqual([
257
+ "test-model",
258
+ "cheap-model",
259
+ "cheap-model"
260
+ ]);
261
+ });
262
+ });