@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
@@ -1,5 +1,7 @@
1
1
  import { BaseChat } from "../../BaseChat.js";
2
2
  import { toStore } from "./PrismaStore.js";
3
+ import { StoppedByStoreError } from "../../errors.js";
4
+ import { redactorFor } from "../../redact.js";
3
5
  /**
4
6
  * A chat whose messages, tool calls and requests are recorded in a store: a Prisma client's
5
7
  * database by default, or any ChatStore.
@@ -8,6 +10,8 @@ export class Chat extends BaseChat {
8
10
  llm;
9
11
  store;
10
12
  persistenceConfig;
13
+ /** Set when the store asks to stop; the next request throws it instead */
14
+ stopReason;
11
15
  constructor(prismaOrStore, llm, record, options = {}, tableNames = {}) {
12
16
  super(record, options);
13
17
  this.llm = llm;
@@ -70,6 +74,34 @@ export class Chat extends BaseChat {
70
74
  for (const h of this.userHooks.onToolCallEnd)
71
75
  await h(call, result);
72
76
  });
77
+ coreChat.beforeRequest(async () => {
78
+ if (this.stopReason !== undefined)
79
+ throw new StoppedByStoreError(this.stopReason);
80
+ });
81
+ // Subagents started from this chat's tools (Agent.toTool, runAgents): their requests are
82
+ // recorded under this chat's answer, and the store's `{ stop }` stops them too.
83
+ coreChat.beforeSubagentRequest?.(async () => {
84
+ if (this.stopReason !== undefined)
85
+ throw new StoppedByStoreError(this.stopReason);
86
+ });
87
+ coreChat.afterSubagentResponse?.(async (sub) => {
88
+ if (!this.persistenceConfig.requests)
89
+ return;
90
+ const recorded = await this.store.createRequest({
91
+ chatId: this.id,
92
+ messageId: assistantMessageId,
93
+ provider: sub.provider || "unknown",
94
+ model: sub.model || "unknown",
95
+ statusCode: 200,
96
+ duration: sub.latency || 0,
97
+ inputTokens: sub.usage?.input_tokens || 0,
98
+ outputTokens: sub.usage?.output_tokens || 0,
99
+ cachedTokens: sub.usage?.cached_tokens,
100
+ cost: sub.usage?.cost || 0
101
+ });
102
+ if (recorded && recorded.stop !== undefined)
103
+ this.stopReason = recorded.stop;
104
+ });
73
105
  coreChat.afterResponse(async (finalResp) => {
74
106
  this.log(`Internal afterResponse triggered. Calling ${this.userHooks.afterResponse.length} user hooks.`);
75
107
  // User hooks
@@ -80,7 +112,7 @@ export class Chat extends BaseChat {
80
112
  }
81
113
  // Only persist if requests persistence is enabled
82
114
  if (this.persistenceConfig.requests) {
83
- await this.store.createRequest({
115
+ const recorded = await this.store.createRequest({
84
116
  chatId: this.id,
85
117
  messageId: assistantMessageId,
86
118
  provider: finalResp.provider || provider || "unknown",
@@ -92,6 +124,8 @@ export class Chat extends BaseChat {
92
124
  cachedTokens: finalResp.usage?.cached_tokens,
93
125
  cost: finalResp.usage?.cost || 0
94
126
  });
127
+ if (recorded && recorded.stop !== undefined)
128
+ this.stopReason = recorded.stop;
95
129
  }
96
130
  return finalResp;
97
131
  });
@@ -274,8 +308,8 @@ export class Chat extends BaseChat {
274
308
  * Creates a new chat session, recorded through a Prisma client or any ChatStore.
275
309
  */
276
310
  export async function createChat(prismaOrStore, llm, options = {}) {
277
- const { model, provider, instructions, metadata, tableNames: _tableNames, debug: _debug, persistence: _persistence, middlewares: _middlewares, maxToolCalls: _maxToolCalls, thinking: _thinking, temperature: _temperature, maxTokens: _maxTokens, headers: _headers, requestTimeout: _requestTimeout, params: _params, ...extras } = options;
278
- const store = toStore(prismaOrStore, options.tableNames);
311
+ const { model, provider, instructions, metadata, tableNames: _tableNames, debug: _debug, persistence: _persistence, middlewares: _middlewares, maxToolCalls: _maxToolCalls, thinking: _thinking, temperature: _temperature, maxTokens: _maxTokens, headers: _headers, requestTimeout: _requestTimeout, params: _params, redact: _redact, ...extras } = options;
312
+ const store = toStore(prismaOrStore, options.tableNames, redactorFor(options.redact, options.middlewares));
279
313
  if (options.debug)
280
314
  console.log(`[@node-llm/orm] createChat: store=${store.constructor.name}`);
281
315
  const record = await store.createChat({
@@ -291,7 +325,7 @@ export async function createChat(prismaOrStore, llm, options = {}) {
291
325
  * Loads an existing chat session from a Prisma client or any ChatStore.
292
326
  */
293
327
  export async function loadChat(prismaOrStore, llm, chatId, options = {}) {
294
- const store = toStore(prismaOrStore, options.tableNames);
328
+ const store = toStore(prismaOrStore, options.tableNames, redactorFor(options.redact, options.middlewares));
295
329
  if (options.debug)
296
330
  console.log(`[@node-llm/orm] loadChat: store=${store.constructor.name}`);
297
331
  const record = await store.findChat(chatId);
@@ -1,4 +1,5 @@
1
- import type { ChatStore, MessageUpdate, NewChat, NewRequest, NewToolCall, RequestTotals, StoredAgentSession, StoredChat, StoredMessage } from "../../Store.js";
1
+ import type { ChatStore, MessageUpdate, NewChat, NewRequest, NewToolCall, RequestTotals, StoredAgentSession, StoredChat, StoredMessage, StoredToolCall } from "../../Store.js";
2
+ import { type StoreRedactor } from "../../redact.js";
2
3
  export interface TableNames {
3
4
  agentSession?: string;
4
5
  chat?: string;
@@ -28,6 +29,7 @@ export declare class PrismaStore implements ChatStore {
28
29
  excludeIds?: string[];
29
30
  }): Promise<StoredMessage[]>;
30
31
  createToolCall(toolCall: NewToolCall): Promise<void>;
32
+ listToolCalls(messageIds: string[]): Promise<StoredToolCall[]>;
31
33
  finishToolCall(messageId: string, toolCallId: string, finished: {
32
34
  result: string;
33
35
  thought?: string | null;
@@ -50,5 +52,5 @@ export declare class PrismaStore implements ChatStore {
50
52
  private table;
51
53
  }
52
54
  /** A ChatStore as given, or a Prisma client wrapped in a PrismaStore. */
53
- export declare function toStore(prismaOrStore: unknown, tableNames?: TableNames): ChatStore;
55
+ export declare function toStore(prismaOrStore: unknown, tableNames?: TableNames, redact?: StoreRedactor): ChatStore;
54
56
  //# sourceMappingURL=PrismaStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"PrismaStore.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/PrismaStore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Q9D,cAAc,CAClB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,GACpD,OAAO,CAAC,IAAI,CAAC;IAOV,aAAa,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAQjD,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IAYrD,kBAAkB,CAAC,OAAO,EAAE;QAChC,UAAU,EAAE,MAAM,CAAC;QACnB,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;KAC1C,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAIzB,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC;IAIhE,kBAAkB,CACtB,EAAE,EAAE,MAAM,EACV,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,GAC7C,OAAO,CAAC,IAAI,CAAC;IAIhB;;;OAGG;IACH,OAAO,CAAC,KAAK;CAYd;AAED,yEAAyE;AACzE,wBAAgB,OAAO,CACrB,aAAa,EAAE,OAAO,EACtB,UAAU,CAAC,EAAE,UAAU,EACvB,MAAM,CAAC,EAAE,aAAa,GACrB,SAAS,CAMX"}
@@ -1,4 +1,5 @@
1
1
  import { isChatStore } from "../../Store.js";
2
+ import { redactingStore } from "../../redact.js";
2
3
  /**
3
4
  * Keeps chats in a database through a Prisma client, in the tables of the reference schema or
4
5
  * the ones named in tableNames.
@@ -43,6 +44,14 @@ export class PrismaStore {
43
44
  async createToolCall(toolCall) {
44
45
  await this.table("toolCall").create({ data: toolCall });
45
46
  }
47
+ async listToolCalls(messageIds) {
48
+ if (messageIds.length === 0)
49
+ return [];
50
+ return await this.table("toolCall").findMany({
51
+ where: { messageId: { in: messageIds } },
52
+ orderBy: { createdAt: "asc" }
53
+ });
54
+ }
46
55
  async finishToolCall(messageId, toolCallId, finished) {
47
56
  await this.table("toolCall").update({
48
57
  where: { messageId_toolCallId: { messageId, toolCallId } },
@@ -92,6 +101,10 @@ export class PrismaStore {
92
101
  }
93
102
  }
94
103
  /** A ChatStore as given, or a Prisma client wrapped in a PrismaStore. */
95
- export function toStore(prismaOrStore, tableNames) {
96
- return isChatStore(prismaOrStore) ? prismaOrStore : new PrismaStore(prismaOrStore, tableNames);
104
+ export function toStore(prismaOrStore, tableNames, redact) {
105
+ const store = isChatStore(prismaOrStore)
106
+ ? prismaOrStore
107
+ : new PrismaStore(prismaOrStore, tableNames);
108
+ // Keys and tokens are masked in everything saved, whatever the store.
109
+ return redactingStore(store, redact);
97
110
  }
@@ -47,5 +47,5 @@ export { Chat, createChat, loadChat } from "./Chat.js";
47
47
  export type { ChatRecord, MessageRecord, ChatOptions } from "./Chat.js";
48
48
  export { PrismaStore, toStore } from "./PrismaStore.js";
49
49
  export { AgentSession, createAgentSession, loadAgentSession } from "./AgentSession.js";
50
- export type { AgentSessionRecord, CreateAgentSessionOptions, LoadAgentSessionOptions, TableNames } from "./AgentSession.js";
50
+ export type { AgentAnswer, AgentData, AgentSessionOverrides, AgentSessionRecord, CreateAgentSessionOptions, LoadAgentSessionOptions, TableNames } from "./AgentSession.js";
51
51
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAGH,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACvD,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAGxE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAGxD,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACvF,YAAY,EACV,kBAAkB,EAClB,yBAAyB,EACzB,uBAAuB,EACvB,UAAU,EACX,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAGH,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACvD,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAGxE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAGxD,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACvF,YAAY,EACV,WAAW,EACX,SAAS,EACT,qBAAqB,EACrB,kBAAkB,EAClB,yBAAyB,EACzB,uBAAuB,EACvB,UAAU,EACX,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { LLMError } from "@node-llm/core";
2
+ /**
3
+ * The store stopped a chat or agent session, before its next request, by answering
4
+ * `createRequest` with `{ stop }`: for example, when the account has reached a spending limit
5
+ * kept outside the app. Every tool call that ran has its result in the history.
6
+ */
7
+ export declare class StoppedByStoreError extends LLMError {
8
+ /** What the store gave as the reason */
9
+ readonly reason: string;
10
+ constructor(
11
+ /** What the store gave as the reason */
12
+ reason: string);
13
+ }
14
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C;;;;GAIG;AACH,qBAAa,mBAAoB,SAAQ,QAAQ;IAE7C,wCAAwC;aACxB,MAAM,EAAE,MAAM;;IAD9B,wCAAwC;IACxB,MAAM,EAAE,MAAM;CAIjC"}
package/dist/errors.js ADDED
@@ -0,0 +1,15 @@
1
+ import { LLMError } from "@node-llm/core";
2
+ /**
3
+ * The store stopped a chat or agent session, before its next request, by answering
4
+ * `createRequest` with `{ stop }`: for example, when the account has reached a spending limit
5
+ * kept outside the app. Every tool call that ran has its result in the history.
6
+ */
7
+ export class StoppedByStoreError extends LLMError {
8
+ reason;
9
+ constructor(
10
+ /** What the store gave as the reason */
11
+ reason) {
12
+ super(reason, "STOPPED_BY_STORE");
13
+ this.reason = reason;
14
+ }
15
+ }
@@ -0,0 +1,10 @@
1
+ import type { Message } from "@node-llm/core";
2
+ import type { StoredMessage, StoredToolCall } from "./Store.js";
3
+ /**
4
+ * The chat history to give an agent from what the store recorded. Each assistant message is
5
+ * preceded by the tool calls it made, with their results, so the model sees what it did. A call
6
+ * with no result, whose run stopped before it finished, is left out with its message's empty
7
+ * text, so the history stays valid and the run can be asked to continue.
8
+ */
9
+ export declare function storedHistory(messages: StoredMessage[], toolCalls?: StoredToolCall[]): Message[];
10
+ //# sourceMappingURL=history.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"history.d.ts","sourceRoot":"","sources":["../src/history.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEhE;;;;;GAKG;AACH,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,aAAa,EAAE,EACzB,SAAS,GAAE,cAAc,EAAO,GAC/B,OAAO,EAAE,CAmCX"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The chat history to give an agent from what the store recorded. Each assistant message is
3
+ * preceded by the tool calls it made, with their results, so the model sees what it did. A call
4
+ * with no result, whose run stopped before it finished, is left out with its message's empty
5
+ * text, so the history stays valid and the run can be asked to continue.
6
+ */
7
+ export function storedHistory(messages, toolCalls = []) {
8
+ const callsByMessage = new Map();
9
+ for (const call of toolCalls) {
10
+ if (call.result === null || call.result === undefined)
11
+ continue;
12
+ callsByMessage.set(call.messageId, [...(callsByMessage.get(call.messageId) ?? []), call]);
13
+ }
14
+ const history = [];
15
+ for (const message of messages) {
16
+ const role = message.role;
17
+ if (role !== "assistant") {
18
+ history.push({ role, content: message.content || "" });
19
+ continue;
20
+ }
21
+ const calls = callsByMessage.get(message.id) ?? [];
22
+ if (calls.length > 0) {
23
+ history.push({
24
+ role: "assistant",
25
+ content: null,
26
+ tool_calls: calls.map((call) => ({
27
+ id: call.toolCallId,
28
+ type: "function",
29
+ function: { name: call.name, arguments: call.arguments },
30
+ ...(call.thoughtSignature ? { thought_signature: call.thoughtSignature } : {})
31
+ }))
32
+ });
33
+ for (const call of calls) {
34
+ history.push({ role: "tool", tool_call_id: call.toolCallId, content: call.result });
35
+ }
36
+ }
37
+ // Providers reject an empty assistant message, such as one whose run never finished.
38
+ if (message.content)
39
+ history.push({ role: "assistant", content: message.content });
40
+ }
41
+ return history;
42
+ }
package/dist/index.d.ts CHANGED
@@ -57,5 +57,7 @@
57
57
  */
58
58
  export * from "./adapters/prisma/index.js";
59
59
  export { isChatStore } from "./Store.js";
60
- export type { ChatStore, StoredChat, StoredMessage, StoredAgentSession, NewChat, NewToolCall, NewRequest, MessageUpdate, RequestTotals } from "./Store.js";
60
+ export { StoppedByStoreError } from "./errors.js";
61
+ export { maskSecrets, redactingStore, type StoreRedactor } from "./redact.js";
62
+ export type { ChatStore, StoredChat, StoredMessage, StoredAgentSession, StoredToolCall, NewChat, NewToolCall, RequestRecorded, NewRequest, MessageUpdate, RequestTotals } from "./Store.js";
61
63
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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"}
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,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAC9E,YAAY,EACV,SAAS,EACT,UAAU,EACV,aAAa,EACb,kBAAkB,EAClB,cAAc,EACd,OAAO,EACP,WAAW,EACX,eAAe,EACf,UAAU,EACV,aAAa,EACb,aAAa,EACd,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -59,3 +59,5 @@
59
59
  export * from "./adapters/prisma/index.js";
60
60
  // Custom stores
61
61
  export { isChatStore } from "./Store.js";
62
+ export { StoppedByStoreError } from "./errors.js";
63
+ export { maskSecrets, redactingStore } from "./redact.js";
@@ -0,0 +1,20 @@
1
+ import type { ChatStore } from "./Store.js";
2
+ /**
3
+ * Changes text before it is saved, such as to mask personal data. Runs after NodeLLM's own
4
+ * masking of keys and tokens.
5
+ */
6
+ export type StoreRedactor = (text: string) => string;
7
+ /** Masks API keys, tokens, private keys and passwords in URLs, which a conversation should never keep. */
8
+ export declare function maskSecrets(text: string): string;
9
+ /**
10
+ * The redactor for a chat or session: `redact`, then the masking of any PIIMaskMiddleware among
11
+ * its middlewares, so what is saved is masked as what the model is sent.
12
+ */
13
+ export declare function redactorFor(redact: StoreRedactor | undefined, middlewares: unknown[] | undefined): StoreRedactor | undefined;
14
+ /**
15
+ * A store that masks keys and tokens, then applies `redact`, in everything it saves: message
16
+ * text, reasoning and thinking, tool arguments and results, a chat's instructions and metadata,
17
+ * and a session's inputs. Reads are passed through as they are.
18
+ */
19
+ export declare function redactingStore(store: ChatStore, redact?: StoreRedactor): ChatStore;
20
+ //# sourceMappingURL=redact.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redact.d.ts","sourceRoot":"","sources":["../src/redact.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAA8B,MAAM,YAAY,CAAC;AAExE;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;AAyCrD,0GAA0G;AAC1G,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAOhD;AAID;;;GAGG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,aAAa,GAAG,SAAS,EACjC,WAAW,EAAE,OAAO,EAAE,GAAG,SAAS,GACjC,aAAa,GAAG,SAAS,CAa3B;AAgBD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,aAAa,GAAG,SAAS,CAwDlF"}
package/dist/redact.js ADDED
@@ -0,0 +1,118 @@
1
+ // Kept the same as @node-llm/core's utils/redact.ts, which this package can't import while
2
+ // it supports older core versions.
3
+ /** Key and token formats, masked wherever they appear in text. */
4
+ const SECRET_VALUE = new RegExp("(?<![A-Za-z0-9])(?:" +
5
+ [
6
+ "sk-(?:ant-|proj-|or-v1-|svcacct-|admin-)?[A-Za-z0-9_-]{16,256}", // OpenAI, Anthropic, OpenRouter
7
+ "[sr]k_(?:live|test)_[A-Za-z0-9]{16,256}", // Stripe
8
+ "AIza[0-9A-Za-z_-]{30,64}", // Google
9
+ "xai-[A-Za-z0-9]{20,256}", // xAI
10
+ "(?:AKIA|ASIA)[0-9A-Z]{16}", // AWS access key IDs, long-lived and temporary
11
+ "gh[pousr]_[A-Za-z0-9]{30,255}", // GitHub tokens
12
+ "github_pat_[A-Za-z0-9_]{20,255}", // GitHub fine-grained tokens
13
+ "xox[abprs]-[A-Za-z0-9-]{10,255}", // Slack tokens
14
+ "eyJ[A-Za-z0-9_-]{8,4096}\\.eyJ[A-Za-z0-9_-]{8,8192}\\.[A-Za-z0-9_-]{8,4096}" // JWTs
15
+ ].join("|") +
16
+ ")", "g");
17
+ /** The credential after `Bearer` or `Basic`, keeping the scheme so the text still reads. */
18
+ const AUTH_SCHEME = /(?<=\b(?:Bearer|Basic)\s{1,4})[A-Za-z0-9._~+/=-]{16,4096}/g;
19
+ /** A PEM private key, from its BEGIN line to its END line. */
20
+ const PRIVATE_KEY = /-----BEGIN [A-Z0-9 ]{0,40}PRIVATE KEY-----[\s\S]{0,16384}?-----END [A-Z0-9 ]{0,40}PRIVATE KEY-----/g;
21
+ /** A secret written as `name = value` or `"name": "value"`, such as an AWS secret access key. */
22
+ const NAMED_SECRET = /((?:aws_secret_access_key|aws_session_token|secret_access_key|secretAccessKey|sessionToken|client_secret|clientSecret|api_key|apiKey|password|passwd)["']?\s{0,4}[:=]\s{0,4}["']?)[^\s"',;&]{8,512}/gi;
23
+ /**
24
+ * The password in a URL such as postgres://user:password@host or redis://:password@host. The
25
+ * scheme must start the word, and no part may hold quotes or spaces, so it never spans JSON
26
+ * fields; the password runs to the last `@` before the host.
27
+ */
28
+ const URL_PASSWORD = /(?<![a-z0-9+.-])([a-z][a-z0-9+.-]{0,30}:\/\/[^\s:/@"'<>\\]{0,256}:)[^\s/"'<>\\]{1,256}@/gi;
29
+ /** Masks API keys, tokens, private keys and passwords in URLs, which a conversation should never keep. */
30
+ export function maskSecrets(text) {
31
+ return text
32
+ .replace(PRIVATE_KEY, "[REDACTED PRIVATE KEY]")
33
+ .replace(SECRET_VALUE, "[REDACTED]")
34
+ .replace(AUTH_SCHEME, "[REDACTED]")
35
+ .replace(NAMED_SECRET, "$1[REDACTED]")
36
+ .replace(URL_PASSWORD, "$1[REDACTED]@");
37
+ }
38
+ const REDACTING = Symbol.for("nodellm.orm.redacting");
39
+ /**
40
+ * The redactor for a chat or session: `redact`, then the masking of any PIIMaskMiddleware among
41
+ * its middlewares, so what is saved is masked as what the model is sent.
42
+ */
43
+ export function redactorFor(redact, middlewares) {
44
+ const masks = (middlewares ?? [])
45
+ .filter((middleware) => typeof middleware === "object" &&
46
+ middleware !== null &&
47
+ middleware.name === "PIIMask" &&
48
+ typeof middleware.mask === "function")
49
+ .map((middleware) => (text) => middleware.mask(text));
50
+ const steps = [...(redact ? [redact] : []), ...masks];
51
+ if (steps.length === 0)
52
+ return undefined;
53
+ return (text) => steps.reduce((current, step) => step(current), text);
54
+ }
55
+ /** A copy of a value with every string in it cleaned. */
56
+ function cleanDeep(value, clean, depth = 0) {
57
+ if (typeof value === "string")
58
+ return clean(value);
59
+ if (depth > 30 || value === null || typeof value !== "object" || value instanceof Date)
60
+ return value;
61
+ if (Array.isArray(value))
62
+ return value.map((item) => cleanDeep(item, clean, depth + 1));
63
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [
64
+ key,
65
+ cleanDeep(item, clean, depth + 1)
66
+ ]));
67
+ }
68
+ /**
69
+ * A store that masks keys and tokens, then applies `redact`, in everything it saves: message
70
+ * text, reasoning and thinking, tool arguments and results, a chat's instructions and metadata,
71
+ * and a session's inputs. Reads are passed through as they are.
72
+ */
73
+ export function redactingStore(store, redact) {
74
+ if (store[REDACTING])
75
+ return store;
76
+ const clean = (text) => typeof text === "string" ? (redact ? redact(maskSecrets(text)) : maskSecrets(text)) : text;
77
+ const cleanText = (text) => (redact ? redact(maskSecrets(text)) : maskSecrets(text));
78
+ const wrapped = Object.create(store);
79
+ wrapped.createChat = (chat) => store.createChat({
80
+ ...chat,
81
+ ...(typeof chat.instructions === "string" ? { instructions: clean(chat.instructions) } : {}),
82
+ ...(chat.metadata !== undefined ? { metadata: cleanDeep(chat.metadata, cleanText) } : {})
83
+ });
84
+ wrapped.createAgentSession = (session) => store.createAgentSession({
85
+ ...session,
86
+ metadata: session.metadata ? cleanDeep(session.metadata, cleanText) : session.metadata
87
+ });
88
+ wrapped.updateAgentSession = (id, changes) => store.updateAgentSession(id, { ...changes, metadata: cleanDeep(changes.metadata, cleanText) });
89
+ wrapped.createMessage = (message) => store.createMessage({ ...message, content: clean(message.content) ?? null });
90
+ wrapped.updateMessage = (id, changes) => store.updateMessage(id, {
91
+ ...changes,
92
+ ...("content" in changes ? { content: clean(changes.content) ?? null } : {}),
93
+ ...("contentRaw" in changes ? { contentRaw: clean(changes.contentRaw) ?? null } : {}),
94
+ ...("thinkingText" in changes ? { thinkingText: clean(changes.thinkingText) ?? null } : {}),
95
+ ...("reasoning" in changes ? { reasoning: clean(changes.reasoning) ?? null } : {})
96
+ });
97
+ wrapped.createToolCall = (toolCall) => store.createToolCall({
98
+ ...toolCall,
99
+ arguments: clean(toolCall.arguments) ?? "",
100
+ ...("thought" in toolCall ? { thought: clean(toolCall.thought) ?? null } : {})
101
+ });
102
+ wrapped.finishToolCall = (messageId, toolCallId, finished) => store.finishToolCall(messageId, toolCallId, {
103
+ ...finished,
104
+ result: clean(finished.result) ?? "",
105
+ ...("thought" in finished ? { thought: clean(finished.thought) ?? null } : {})
106
+ });
107
+ // Methods the app's store has beyond ChatStore keep working through the prototype.
108
+ for (const name of Object.getOwnPropertyNames(Object.getPrototypeOf(store))) {
109
+ const value = store[name];
110
+ if (name !== "constructor" &&
111
+ typeof value === "function" &&
112
+ !(name in wrapped && Object.hasOwn(wrapped, name))) {
113
+ wrapped[name] = value.bind(store);
114
+ }
115
+ }
116
+ Object.defineProperty(wrapped, REDACTING, { value: true });
117
+ return wrapped;
118
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node-llm/orm",
3
- "version": "0.10.1",
3
+ "version": "0.12.0",
4
4
  "description": "Database persistence layer for NodeLLM - Chat, Message, and ToolCall tracking with streaming support",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -56,7 +56,7 @@
56
56
  "author": "NodeLLM Contributors",
57
57
  "license": "MIT",
58
58
  "peerDependencies": {
59
- "@node-llm/core": "^1.15.0",
59
+ "@node-llm/core": "^1.22.0",
60
60
  "@prisma/client": "^5.0.0"
61
61
  },
62
62
  "peerDependenciesMeta": {
package/src/BaseChat.ts CHANGED
@@ -29,7 +29,7 @@ export interface ChatOptions {
29
29
  maxTokens?: number;
30
30
  headers?: Record<string, string>;
31
31
  maxToolCalls?: number;
32
- toolConcurrency?: boolean;
32
+ toolConcurrency?: boolean | number;
33
33
  toolExecution?: ToolExecutionMode;
34
34
  toolChoice?: ToolChoice;
35
35
  toolCalls?: "one" | "many" | number;
@@ -174,7 +174,7 @@ export abstract class BaseChat<
174
174
  * Runs independent tool calls within the same turn concurrently instead
175
175
  * of sequentially. See core's Tools guide for details.
176
176
  */
177
- withToolConcurrency(enabled: boolean): this {
177
+ withToolConcurrency(enabled: boolean | number): this {
178
178
  this.localOptions.toolConcurrency = enabled;
179
179
  return this;
180
180
  }
package/src/Store.ts CHANGED
@@ -32,6 +32,18 @@ export interface StoredMessage {
32
32
  createdAt: Date;
33
33
  }
34
34
 
35
+ export interface StoredToolCall {
36
+ messageId: string;
37
+ toolCallId: string;
38
+ name: string;
39
+ arguments: string;
40
+ thought?: string | null;
41
+ thoughtSignature?: string | null;
42
+ /** Null while the call is still running, or when it never finished */
43
+ result?: string | null;
44
+ createdAt?: Date;
45
+ }
46
+
35
47
  export interface StoredAgentSession {
36
48
  id: string;
37
49
  agentClass: string;
@@ -76,6 +88,12 @@ export interface NewRequest {
76
88
  cost: number;
77
89
  }
78
90
 
91
+ /** What a store may answer when it records a request. */
92
+ export interface RequestRecorded {
93
+ /** Stop before the next request, giving this as the reason */
94
+ stop?: string;
95
+ }
96
+
79
97
  export interface RequestTotals {
80
98
  inputTokens: number;
81
99
  outputTokens: number;
@@ -99,13 +117,24 @@ export interface ChatStore {
99
117
  listMessages(chatId: string, options?: { excludeIds?: string[] }): Promise<StoredMessage[]>;
100
118
 
101
119
  createToolCall(toolCall: NewToolCall): Promise<void>;
120
+ /**
121
+ * The tool calls recorded for these messages, oldest first. Optional: with it, a loaded agent
122
+ * session gets back the tool calls and results of earlier turns, including one whose run
123
+ * stopped part way; without it, only the messages' text (v0.11.0+).
124
+ */
125
+ listToolCalls?(messageIds: string[]): Promise<StoredToolCall[]>;
102
126
  finishToolCall(
103
127
  messageId: string,
104
128
  toolCallId: string,
105
129
  finished: { result: string; thought?: string | null }
106
130
  ): Promise<void>;
107
131
 
108
- createRequest(request: NewRequest): Promise<void>;
132
+ /**
133
+ * Records one API call. A store can stop the chat or session before its next request by
134
+ * returning `{ stop: reason }`, such as when a spending limit kept outside the app is reached;
135
+ * it then throws StoppedByStoreError (v0.11.0+).
136
+ */
137
+ createRequest(request: NewRequest): Promise<void | RequestRecorded>;
109
138
  requestTotals(chatId: string): Promise<RequestTotals>;
110
139
 
111
140
  createAgentSession(session: {