@node-llm/orm 0.10.0 → 0.11.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 +130 -0
- package/dist/BaseChat.d.ts +2 -2
- package/dist/BaseChat.d.ts.map +1 -1
- package/dist/Store.d.ts +28 -1
- package/dist/Store.d.ts.map +1 -1
- package/dist/adapters/prisma/AgentSession.d.ts +42 -9
- package/dist/adapters/prisma/AgentSession.d.ts.map +1 -1
- package/dist/adapters/prisma/AgentSession.js +69 -17
- package/dist/adapters/prisma/Chat.d.ts +5 -0
- package/dist/adapters/prisma/Chat.d.ts.map +1 -1
- package/dist/adapters/prisma/Chat.js +14 -4
- package/dist/adapters/prisma/PrismaStore.d.ts +4 -2
- package/dist/adapters/prisma/PrismaStore.d.ts.map +1 -1
- package/dist/adapters/prisma/PrismaStore.js +15 -2
- package/dist/adapters/prisma/index.d.ts +1 -1
- package/dist/adapters/prisma/index.d.ts.map +1 -1
- package/dist/errors.d.ts +14 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +15 -0
- package/dist/history.d.ts +10 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +42 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/redact.d.ts +20 -0
- package/dist/redact.d.ts.map +1 -0
- package/dist/redact.js +118 -0
- package/package.json +2 -2
- package/src/BaseChat.ts +2 -2
- package/src/Store.ts +30 -1
- package/src/adapters/prisma/AgentSession.ts +130 -26
- package/src/adapters/prisma/Chat.ts +23 -5
- package/src/adapters/prisma/PrismaStore.ts +21 -3
- package/src/adapters/prisma/index.ts +3 -0
- package/src/errors.ts +15 -0
- package/src/history.ts +48 -0
- package/src/index.ts +4 -0
- package/src/redact.ts +157 -0
- package/test/AgentSession.test.ts +12 -2
- package/test/CountCall.test.ts +147 -0
- package/test/Redact.test.ts +228 -0
- package/test/Resume.test.ts +232 -0
- package/test/SessionData.test.ts +109 -0
- package/test/SessionOverrides.test.ts +130 -0
- package/test/StoreStops.test.ts +163 -0
- 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,163 @@
|
|
|
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
|
+
});
|