@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
package/CHANGELOG.md CHANGED
@@ -2,6 +2,145 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## [1.23.0] - 2026-11-22 (@node-llm/core)
6
+
7
+ ### Features
8
+
9
+ - **Lead and Workers (`runAgents`, `agentsTool`)**: a lead agent can send several agents at once, each on its own task in its own conversation, and merge what they found. `runAgents(jobs, { concurrency, maxCost, failFast, signal, llm })` returns one result per job, in order: `status` (`done`, `failed`, `cancelled`), `content`, `data` parsed by the worker's schema, and its `usage` and `cost`. `agentsTool({ agents, concurrency, maxTasks })` lets the model choose the tasks. Started from a tool, workers count toward the lead's `usage` and `maxCost` and stop once it is reached; a job's `maxCost` caps one worker. A failed worker doesn't stop the others unless `failFast` is set.
10
+ - **Subagents Are Cancelled With Their Parent**: a subagent's ask (`toTool()` or a worker) now uses the `signal` of the ask it was started from, and is refused before its first request once an ask above it has reached its `maxCost`.
11
+ - **Subagent Hooks**: `beforeSubagentRequest(handler)` and `afterSubagentResponse(handler)` on chats and agents run for every request of any subagent below them, with its `usage`, `model`, `provider`, `latency` and `depth`. A `beforeSubagentRequest` handler that throws stops that subagent.
12
+ - **Model per Step (`routeModel`)**: a function on a chat or agent (`routeModel`, `static routeModel`) chooses the model for each request of an ask from `{ turn, toolCalls, messages, model }`, such as an economical model after routine tool calls and the main model for decisions. Works for `ask()` and `stream()`; prompt caching stays on for every model. Each request's response, `afterResponse` hooks and @node-llm/orm request rows carry the model it used, and what `ask()` returns has `usageByModel`.
13
+
14
+ ## [0.12.0] - 2026-11-22 (@node-llm/orm)
15
+
16
+ ### Features
17
+
18
+ - **Subagent Requests Are Recorded and Stopped by the Store**: sessions and chats record every request of a subagent (`toTool()`, `runAgents()`) as a request row under the answer being written, so totals include what workers spend, and a store's `createRequest` returning `{ stop }` stops the workers too.
19
+
20
+ ## [1.22.1] - 2026-11-22 (@node-llm/core)
21
+
22
+ ### Security
23
+
24
+ - **Custom Base URLs Can't Be Steered Inside**: requests to a base URL set in config (any `*ApiBase`) no longer follow a redirect to another host, so a gateway can't answer `302` to `169.254.169.254` or an internal service and have the reply read as the model's answer; they throw `ApiBaseRefusedError` (code `API_BASE_REFUSED`), which is not retried. Cloud metadata addresses are refused, checked as each connection is made. Redirects within the same host, private gateways and local models work as before; a provider's default URL is not touched.
25
+ - **`apiBaseSecurity.blockPrivateNetwork`**: for apps where users or admins set a base URL, refuses private, loopback, link-local and shared addresses on every connection, so a name that was public when saved can't later point inside (DNS rebinding). New type `ApiBaseSecurity`.
26
+
27
+ ## [1.22.0] - 2026-11-22 (@node-llm/core)
28
+
29
+ ### Security
30
+
31
+ - **Secret Files and Cloud Metadata Are Refused**: `images`, `files`, Gemini's and OpenAI's image inputs and transcription files no longer load files that hold secrets (`.env` in any form, `.npmrc`, `.netrc`, SSH keys, cloud credential files, `secrets.yml`, `database.yml`, `terraform.tfstate`, anything in `.ssh`, `.aws`, `.kube` or `.git`, `.pem` and `.key` files, `/proc`, `/sys` and `/dev`, and links to them), devices, pipes, or files over 100 MB, and no longer fetch cloud metadata addresses (`169.254.x.x`, `192.0.0.192`, `fd00:ec2::254`, `metadata.google.internal`, and IPv6 forms that carry them). The address is checked when each connection is made, redirects included, so DNS rebinding can't reach them. They throw `FileAccessError`. Ordinary files, files with no extension and other private hosts load as before; `fileSecurity.allowSensitiveFiles` loads secret files again.
32
+ - **Remote Files Time Out and Stop at 50 MB While Reading**: a remote file now times out after 60 seconds (`fileSecurity.fetchTimeout`). One without a `content-length` was read in full before its size was checked; it is now stopped once it passes 50 MB, in every loader.
33
+ - **More File Rules (`fileSecurity`)**: for apps where a path or URL comes from a user, `root` keeps local files inside a folder (after following links), `blockPrivateNetwork` refuses every private address, `allowedHosts` allows only listed hosts, and `throwOnFetchError` throws instead of passing a failed URL on to the provider. Set on `config`, `createLLM()` or a chat; a chat's rules add to the others instead of replacing them.
34
+ - **Gemini Keys Are Sent in a Header**: the key goes in `x-goog-api-key` instead of `?key=` in the URL, so proxies, tracing and error reports that record URLs don't see it.
35
+ - **Debug Logs Mask Keys and Files**: with `debug: true`, printed request and response bodies mask keys and tokens (OpenAI, Anthropic, OpenRouter, Google, xAI, Stripe, Slack, AWS `AKIA`/`ASIA` and named secret keys, GitHub `ghp_`/`github_pat_`, JWTs, `Bearer` and `Basic` credentials, PEM private keys), passwords in URLs and fields named like secrets (`apiKey`, `openaiApiKey`, `secretAccessKey`, `token`, `private_key`, ...), and replace base64 files in any field with their size. Prompts and answers are still printed. `logger.error`, which is always on, masks the errors it prints. The patterns are bounded, so long hostile input can't slow them. `redactForLog()` is exported.
36
+ - **Provider Errors Mask Keys**: `APIError`'s `message` and `body` mask key-like values (such as an OpenRouter key named in a credit error). Everything else in them is unchanged.
37
+ - **Tool Arguments Are Checked Against JSON Schemas**: a tool defined with a JSON Schema (plain definitions, `Tool` classes with one, MCP tools) no longer runs with arguments its schema rules out: missing required fields, wrong types, values outside an `enum`, or extra fields where `additionalProperties` is `false`. The model is told what to fix. Zod tools are checked by Zod as before.
38
+ - **Tool Errors Sent to the Model Are Masked and Cut Short**: key-like values and passwords in URLs are masked, and the message is cut to 500 characters.
39
+ - **Unknown-Tool Replies Don't List Deferred Tools**: when the model calls a tool that doesn't exist, the reply lists only the tools it can see.
40
+ - **Subagent Spending and Nesting Are Limited**: a subagent's requests now count toward its parent ask's `usage` and `maxCost`, and it stops once an ask above it has reached its limit. Subagents nest at most 8 deep; deeper, `AgentDepthError` is thrown, so an agent handing a task to itself can't loop forever.
41
+ - **PII Masking Covers Tool Results, Errors and Answers**: `PIIMaskMiddleware` now masks every tool result before the model sees it, a tool's error included (before, only messages present when an ask started were masked), masking JSON results in their string values only so they stay valid. `redactOutput: true`, which was accepted but did nothing, now masks the answer `ask()` returns and the one the history keeps. It also masks international phone numbers and IBANs, and its email pattern is bounded so long input can't slow it.
42
+ - **Middlewares Can Change Tool Results**: a new `onToolResult(ctx, toolCall, content, { isError })` hook may return `{ content }` to change any tool result the model is sent; `onToolCallEnd` may too, for successful calls. A `REPLACE` from `onResponse` now also replaces the answer in the history, and another middleware's `"CONTINUE"` no longer hides it. New type `ToolResultChange`.
43
+
44
+ ### Features
45
+
46
+ - **Subagents**: an agent class goes wherever a tool goes. The model hands it a `task`, a fresh instance works on it in its own conversation, and only the content of its last answer comes back, so its searches and reading never fill the calling conversation. `Agent.toTool(inputs, { name, description, llm })` fixes some of the agent's inputs and lets the model fill the rest. Agents gain `description` and `agentName` (`name` and `description` in `defineAgent()`). A subagent inside an agent runs on that agent's `llm` instance.
47
+ - **Cap on Concurrent Tool Calls**: `toolConcurrency` (and `withToolConcurrency`) also takes a number, the most tool calls to run at once. Every call still runs, that many at a time, and results stay in the order the model asked for them. Useful for tools that reach a rate-limited API. Works for chats, streams, agents, `config` and `@node-llm/orm` chats.
48
+ - **Context Budgets**: `config.contextBudget = { perResult, perRequest }` (or `chat.withContextBudget()`, or `static contextBudget` on an agent) keeps tool results within shares of the model's context window. A result longer than `perResult` is cut when it is added, with a note telling the model to ask for less. A request larger than `perRequest` leaves out its oldest tool results, in steps of about 15% of the budget so the prompt cache holds; the latest round goes out whenever it fits. `window` sets the context window for a model the registry doesn't know, and `null` turns a budget off.
49
+ - **Ending With a Tool**: `endWith: "finish"` on a chat, an agent or one ask names a tool the model must call before its turn ends. When the model ends its turn without calling it, it is reminded once; if it still doesn't, the ask throws `EndToolNotCalledError` with the tool and the ask's usage. `{ tool, reminder }` sets the reminder's wording, a tool that halts the loop counts as called, and `endWith: null` turns it off for one ask. Works for `ask()` and `stream()`.
50
+ - **Stall Guard**: `stallGuard: { repeats, noProgress, progress }` on a chat or an agent stops a tool loop that is going nowhere, before its next request, with `StalledError`. `repeats` counts the same call with the same arguments getting the same result; `noProgress` counts calls in a row that failed or got a result the tool already gave in the ask, or that an app's own `progress(call, result, failed)` says made no progress. The error has the `reason`, the `calls`, their `count` and the ask's `usage`, and every call that ran keeps its result in the history. Works for `ask()` and `stream()`; off unless set.
51
+ - **Anthropic Structured Outputs**: on Claude models that support it (Fable and Mythos 5, Opus 5 and 4.8, Sonnet 5, Haiku 5 and 4.5, and Opus 4.5 and 4.1), a schema is now sent as `output_config.format`, so the answer is held to it instead of only being asked for in the prompt. Constraints Anthropic doesn't take, such as minimums and lengths, are left out of what is sent, and every object gets `additionalProperties: false`; a Zod schema still checks the full schema on `response.data`. Older models, and schemas with records or self-references, keep the prompt instruction. For `ask()` and `stream()`.
52
+ - **Counting Calls a Tool Makes**: `chat.countUsage(usage)` and `agent.countUsage(usage)` count a call made outside the chat's own requests, such as a tool asking a vision model about a screenshot. It is added to the ask running now, so its `usage` and `maxCost` include it, and to `totalUsage`. In `@node-llm/orm`, `session.countCall(answer)` also saves it as a request row under the answer being written, so `stats()` includes it. Works for `ask()` and `stream()`.
53
+
54
+ ### Bug Fixes
55
+
56
+ - **Readable Schema Errors**: when a structured response doesn't match its Zod schema, `response.data` still throws Zod's `ZodError`, now with a one-line message naming each field, the values the schema allows and the value received, such as `status: Invalid option: expected one of "passed"|"failed" (got "blocked")`. Before, a Zod 4 error's message was its issues as indented JSON, so logs that keep an error's first line showed only `[`. The parts are on `error.problems`. `SchemaSelfCorrection` re-asks with the new message. The docs show how to accept unknown enum values with Zod's `.catch()`.
57
+ - **Self-Correction Re-Asks With the Feedback in `ask()`**: a middleware `RETRY`, such as `SchemaSelfCorrection`'s, sent the first request again, without the rejected answer or the feedback, so the model was not told what to fix. It now sends both, as `stream()` already did.
58
+ - **Network Errors Are Retried**: a request that can't reach the provider, such as `fetch failed` with `ECONNRESET`, now throws `NetworkError`, with the `host` and the original error as `cause`, and is retried like a server error. Before, fetch's `TypeError` escaped on the first attempt whatever `retry` was set to. It still moves on to a fallback model. The error-handling docs now also say that `InsufficientQuotaError` isn't retried, as the code already did.
59
+
60
+ ## [0.11.0] - 2026-11-22 (@node-llm/orm)
61
+
62
+ ### Security
63
+
64
+ - **Secrets Aren't Saved**: API keys, tokens, private keys and passwords in URLs are masked in everything a chat or agent session saves (message text, reasoning, thinking, tool arguments and results, a chat's instructions and metadata, and a session's inputs), through the Prisma store or a custom one. Tool arguments stay valid JSON. A chat or session that uses `PIIMaskMiddleware` also saves masked text. `redact` on `createChat`, `loadChat`, `createAgentSession` and `loadAgentSession` masks more. `maskSecrets` and `redactingStore` are exported.
65
+
66
+ ### Features
67
+
68
+ - **Loaded Sessions Keep Their Tool Calls**: `loadAgentSession()` gives the agent the tool calls of earlier turns with their results, before the answer they led to. Before, it gave only each message's text, so a resumed agent didn't know what it had already looked up. A run that stopped part way, such as when its service restarted, can be loaded and asked to continue: the calls that finished come back, and a call that never finished is left out. Custom stores add an optional `listToolCalls(messageIds)`; without it, sessions load as before.
69
+ - **Agent Session Answers Have Their Data**: for an agent with a `schema`, the record `session.ask()` returns has the parsed answer as `data`, typed from the schema, found inside a code fence or surrounding text and checked against a Zod schema, as `response.data` is in core. Before, apps parsed `content` themselves. `data` isn't a column: it isn't saved and is left out when the record is serialized. New types `AgentAnswer` and `AgentData`.
70
+ - **Agent Settings per Session**: `createAgentSession()` and `loadAgentSession()` take any agent setting except `schema` for that session, in place of the class's: `maxCost`, `maxTokens`, `temperature`, `thinking`, `keepToolResults`, `promptCache`, `middlewares` and the rest. Before, only `model`, `provider`, `instructions`, `maxToolCalls` and `tools` were passed on, and `loadAgentSession()` took none, so an app had to subclass the agent for each budget. Settings left `undefined` keep the class's. New type `AgentSessionOverrides`.
71
+ - **A Store Can Stop a Run**: a `ChatStore` can return `{ stop: reason }` from `createRequest`, such as when a spending limit kept outside the app is reached. The chat or agent session then throws `StoppedByStoreError` with that `reason` before its next request, and on later asks. The tool calls that already ran keep their results in the history. Stores that return nothing work as before.
72
+ - **Counting Calls Outside the Agent**: `session.countCall(answer)` saves a call a tool makes, such as asking a vision model about a screenshot, as a request row under the answer being written, and counts it in the agent's usage and `maxCost` (see core's `countUsage`).
73
+
74
+ ### Bug Fixes
75
+
76
+ - **No Empty Assistant Messages on Load**: an assistant message with no text, left by a run that stopped before it finished, is no longer sent in a loaded session's history. Providers such as Anthropic reject an empty message.
77
+
78
+ ## [0.4.0] - 2026-11-22 (@node-llm/mcp)
79
+
80
+ ### Security
81
+
82
+ - **Hidden Characters Are Removed From Server Tools**: zero-width, bidirectional-control and Unicode tag characters are removed from the names, descriptions and schemas of a server's tools and prompts, where a server could hide instructions for the model that a person can't see. Visible text is unchanged.
83
+ - **Keys in Server Errors Are Masked**: key-like values in an MCP server's error are masked before the model sees it.
84
+ - **Sampling Guards**: `sampling: { llm, model, approve, maxTokens, maxCost }` (and `createLLMSamplingHandler(llm, model, guards)`) can refuse a request, cap an answer's length, and cap what a server's sampling spends in total. A refused request throws `SamplingRefusedError`. Without guards, sampling works as before.
85
+
86
+ ## [0.7.0] - 2026-11-22 (@node-llm/testing)
87
+
88
+ ### Security
89
+
90
+ - **More Key Formats Are Scrubbed From Cassettes**: keys with hyphens (`sk-ant-...`, `sk-proj-...`, `sk-or-v1-...`, which the old pattern missed), xAI keys, AWS access key IDs, GitHub tokens and Bearer tokens, plus the `x-goog-api-key`, `api-key` and `proxy-authorization` headers. An `sk-` inside a word, as in `ask-user`, is no longer scrubbed.
91
+
92
+ ## [1.21.0] - 2026-11-21 (@node-llm/core)
93
+
94
+ ### Features
95
+
96
+ - **`onFallback` Middleware Hook**: middlewares can now see model fallbacks. `onFallback(context, event)` runs, with the request's own context, before a chat request is sent to a fallback model, for `ask()` and `stream()`. The OpenTelemetry middleware uses it to add a `node_llm.fallback` span event and to name the provider that answered.
97
+ - **Native Deferred Tools on Anthropic**: on Claude 4.5 and later, a chat with deferred tools sends every tool on every request, deferred ones with `defer_loading: true`, and sends `tool_search` results as `tool_reference` blocks. The tool list no longer changes when the model loads a tool, so the prompt cache holds. Other providers and older Claude models keep the portable behavior.
98
+
99
+ ### Bug Fixes
100
+
101
+ - **Finish Reason on Every Provider**: `response.finishReason` is now set by OpenAI (and OpenAI-based providers: Azure, OpenRouter, Ollama), Anthropic, Gemini, DeepSeek and xAI, for both `ask()` and `stream()`. Before, only Bedrock and Mistral set it, and streamed responses never had it. The value is the provider's own (`stop`, `length`, `end_turn`, `max_tokens`, `MAX_TOKENS`, ...). The OpenTelemetry middleware now fills `gen_ai.response.finish_reasons` from it.
102
+ - **`keepToolResults` Keeps `tool_search` Results**: windowing no longer replaces `tool_search` results with a placeholder. They are small, and they record which deferred tools a conversation has loaded.
103
+ - **Fallbacks Leave Out Settings the Fallback Model Doesn't Take**: the request sent to a fallback model was the one built for the chat's own model. A temperature, thinking setting, forced tool choice or prompt-cache flag that the fallback model rejects made the fallback fail with a bad request, which is not retried. These settings are now left out for a model that doesn't support them, for `ask()` and `stream()`; a predicted output is now also dropped for streams.
104
+
105
+ ## [1.20.1] - 2026-11-12 (@node-llm/core)
106
+
107
+ ### Bug Fixes
108
+
109
+ - **Fatal Tool Errors Stop the Loop**: a `ToolError` with `fatal: true`, or a 401/403, thrown from a tool built with the `Tool` class or `defineTool` now stops the agent loop and is thrown, as documented. Before, `Tool.handler` turned it into an ordinary error message and the model carried on. Non-fatal errors still reach the model as text.
110
+ - **Anthropic Instructions Stay Cached**: with `promptCache` on, the system prompt is sent with its own `cache_control` breakpoint. An app that rewrites earlier messages, such as leaving out old tool results, no longer misses the cache and pays for its instructions and tools in full on every call.
111
+
112
+ ## [1.20.0] - 2026-11-11 (@node-llm/core)
113
+
114
+ ### Features
115
+
116
+ - **Model Fallbacks**: `chat.withFallbacks(...targets)` tries other models, on the same or another provider, when a request still fails after retries with a rate limit, server or network error. Each request starts on the chat's own model; streams fall back only before their first chunk. `onFallback` reports each attempt, and responses report the model and provider that answered.
117
+ - **Deferred Tools**: a tool marked `defer: true` is left out of requests until the model looks for it with a built-in `tool_search` tool; tools a search loads are sent with every later request. What is loaded is read back from the conversation, so it survives serialization. Works on every provider.
118
+ - **`rescueFrom` on Tools**: a tool can turn a specific error class thrown by `execute()` into the result the model sees (a string, an object as JSON, or a `ToolHalt`). Handlers match subclasses; the one registered last wins. `defineTool` takes the same handlers as `[ErrorClass, handler]` pairs.
119
+ - **OpenTelemetry Tracing Middleware**: `OpenTelemetry(otel)` emits a span per chat, embedding, image, transcription, moderation and judgment call, and a child span per tool call, using the GenAI semantic conventions. Spans never include prompts, responses or tool arguments, and core gains no dependency: the app passes its own `@opentelemetry/api`.
120
+ - **Base URL Checks**: every provider checks its `*ApiBase` setting when created. A base URL without `http(s)://`, or one ending in a path NodeLLM adds itself (such as `/chat/completions` or `/messages`), throws a `ConfigurationError` naming the setting and the expected form. `anthropicApiBase` and `geminiApiBase` now reach their providers; before, they were read from config but never passed on.
121
+
122
+ ### Bug Fixes
123
+
124
+ - **`requestTimeout` Covers the Response Body**: the timeout no longer stops when the headers arrive. Some providers, OpenRouter among them, send headers at once and then hold the body open while the model works. A call to a stalled model then waited forever. Non-streaming calls now fail with `Request timeout after …ms` when the body takes too long. Streams keep the old behavior: the timeout covers the wait for the first response only.
125
+
126
+ ## [1.19.3] - 2026-11-06 (@node-llm/core)
127
+
128
+ ### Features & Bug Fixes
129
+
130
+ - **Rate Limit Exponential Backoff**: Added automated exponential backoff retry handling for HTTP 429 rate limit errors with `retry-after` header parsing and jittered backoff.
131
+ - **GPT-5 Completion Token Compatibility**: Normalized token limit parameters across OpenAI models, ensuring `max_tokens` automatically maps to `max_completion_tokens` for GPT-5, o1/o3/o4 series models and compatible endpoints.
132
+ - **Azure Provider Hardening**: Normalized error status translation and capabilities resolution to unify custom endpoint support without branching.
133
+
134
+ ## [1.19.2] - 2026-11-03 (@node-llm/core)
135
+
136
+ ### Bug Fixes
137
+
138
+ - **Prompt Caching on OpenRouter**: `withPromptCache()` (or `static promptCache = true` on an Agent) now asks OpenRouter to cache prompts for Claude models (`anthropic/...`). Before, the option never reached OpenRouter, so every request was billed at the full input price.
139
+ - **Prompt Caching on Bedrock**: the option now adds a cache point after the system prompt and after the latest message, for Claude 3.5 Haiku, 3.7 Sonnet and later, and Nova. Older Claude models, which refuse cache points, are left alone. Blocks marked with `cache_control` keep working as before.
140
+ - **Cached Tokens From Every Provider**: `usage.cached_tokens` is now filled in for OpenAI streaming, xAI, DeepSeek (`prompt_cache_hit_tokens`) and Mistral. On DeepSeek and xAI the reported cost is lower when the cache is hit, because cached input is now priced at its own rate; `input_tokens` is unchanged.
141
+
142
+ Requests are unchanged when prompt caching is off.
143
+
5
144
  ## [1.19.1] - 2026-11-03 (@node-llm/core)
6
145
 
7
146
  ### Bug Fixes
@@ -26,7 +26,7 @@ export interface ChatOptions {
26
26
  maxTokens?: number;
27
27
  headers?: Record<string, string>;
28
28
  maxToolCalls?: number;
29
- toolConcurrency?: boolean;
29
+ toolConcurrency?: boolean | number;
30
30
  toolExecution?: ToolExecutionMode;
31
31
  toolChoice?: ToolChoice;
32
32
  toolCalls?: "one" | "many" | number;
@@ -81,7 +81,7 @@ export declare abstract class BaseChat<R extends ChatRecord = ChatRecord, O exte
81
81
  * Runs independent tool calls within the same turn concurrently instead
82
82
  * of sequentially. See core's Tools guide for details.
83
83
  */
84
- withToolConcurrency(enabled: boolean): this;
84
+ withToolConcurrency(enabled: boolean | number): this;
85
85
  /**
86
86
  * Controls how tool calls are executed: "auto" (default), "confirm"
87
87
  * (calls onConfirmToolCall before each execution), or "dry-run".
@@ -1 +1 @@
1
- {"version":3,"file":"BaseChat.d.ts","sourceRoot":"","sources":["../src/BaseChat.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEvF,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,QAAQ,CAAC,EAAE,MA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AC,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI;IAK5E,aAAa,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI;IAKhE,eAAe,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,KAAK,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI;IAKxE,YAAY,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;IAKxD,YAAY,CAAC,QAAQ,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;IAKpE;;OAEG;IACH,QAAQ,CAAC,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC;CACjC"}
package/dist/Store.d.ts CHANGED
@@ -27,6 +27,17 @@ export interface StoredMessage {
27
27
  provider?: string | null;
28
28
  createdAt: Date;
29
29
  }
30
+ export interface StoredToolCall {
31
+ messageId: string;
32
+ toolCallId: string;
33
+ name: string;
34
+ arguments: string;
35
+ thought?: string | null;
36
+ thoughtSignature?: string | null;
37
+ /** Null while the call is still running, or when it never finished */
38
+ result?: string | null;
39
+ createdAt?: Date;
40
+ }
30
41
  export interface StoredAgentSession {
31
42
  id: string;
32
43
  agentClass: string;
@@ -66,6 +77,11 @@ export interface NewRequest {
66
77
  cachedTokens?: number;
67
78
  cost: number;
68
79
  }
80
+ /** What a store may answer when it records a request. */
81
+ export interface RequestRecorded {
82
+ /** Stop before the next request, giving this as the reason */
83
+ stop?: string;
84
+ }
69
85
  export interface RequestTotals {
70
86
  inputTokens: number;
71
87
  outputTokens: number;
@@ -88,11 +104,22 @@ export interface ChatStore {
88
104
  excludeIds?: string[];
89
105
  }): Promise<StoredMessage[]>;
90
106
  createToolCall(toolCall: NewToolCall): Promise<void>;
107
+ /**
108
+ * The tool calls recorded for these messages, oldest first. Optional: with it, a loaded agent
109
+ * session gets back the tool calls and results of earlier turns, including one whose run
110
+ * stopped part way; without it, only the messages' text (v0.11.0+).
111
+ */
112
+ listToolCalls?(messageIds: string[]): Promise<StoredToolCall[]>;
91
113
  finishToolCall(messageId: string, toolCallId: string, finished: {
92
114
  result: string;
93
115
  thought?: string | null;
94
116
  }): Promise<void>;
95
- createRequest(request: NewRequest): Promise<void>;
117
+ /**
118
+ * Records one API call. A store can stop the chat or session before its next request by
119
+ * returning `{ stop: reason }`, such as when a spending limit kept outside the app is reached;
120
+ * it then throws StoppedByStoreError (v0.11.0+).
121
+ */
122
+ createRequest(request: NewRequest): Promise<void | RequestRecorded>;
96
123
  requestTotals(chatId: string): Promise<RequestTotals>;
97
124
  createAgentSession(session: {
98
125
  agentClass: string;
@@ -1 +1 @@
1
- {"version":3,"file":"Store.d.ts","sourceRoot":"","sources":["../src/Store.ts"],"names":[],"mappings":"AAEA;;;GAGG;AAEH,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,GAAG,IAA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AClB,QAAQ,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,GACpD,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjB;;;;OAIG;IACH,aAAa,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,GAAG,eAAe,CAAC,CAAC;IACpE,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IAEtD,kBAAkB,CAAC,OAAO,EAAE;QAC1B,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,CAAC;IAChC,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,GAAG,IAAI,CAAC,CAAC;IACjE,kBAAkB,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/F;AAUD,kEAAkE;AAClE,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAM9D"}
@@ -1,4 +1,16 @@
1
1
  import { ChatOptions, AskOptions, NodeLLMCore, Agent, AgentConfig, Message, ChatChunk, Usage } from "@node-llm/core";
2
+ import { type StoreRedactor } from "../../redact.js";
3
+ /** What an agent's answers parse to: its schema's type, or unknown without one. */
4
+ export type AgentData<T> = T extends Agent<any, infer S> ? S : unknown;
5
+ /**
6
+ * The saved assistant message, with the answer's parsed `data`: checked against the agent's schema
7
+ * when it has a Zod one, and found inside a code fence or surrounding text. Reading `data` throws
8
+ * the schema's error when the answer doesn't match. `data` isn't a column, so it isn't saved or
9
+ * sent when the record is serialized.
10
+ */
11
+ export type AgentAnswer<T> = MessageRecord & {
12
+ readonly data: AgentData<T>;
13
+ };
2
14
  /**
3
15
  * Record structure for the LLM Agent Session table.
4
16
  */
@@ -75,6 +87,8 @@ export declare class AgentSession<I extends Record<string, any> = Record<string,
75
87
  private record;
76
88
  private agent;
77
89
  private currentMessageId;
90
+ /** Set when the store asks to stop; the next request throws it instead */
91
+ private stopReason?;
78
92
  private store;
79
93
  private debug;
80
94
  constructor(prismaOrStore: unknown, llm: NodeLLMCore, AgentClass: AgentClass<T>, record: AgentSessionRecord, tableNames?: TableNames, agent?: T, debug?: boolean);
@@ -102,12 +116,23 @@ export declare class AgentSession<I extends Record<string, any> = Record<string,
102
116
  * Register persistence hooks on the agent.
103
117
  */
104
118
  private registerHooks;
119
+ /**
120
+ * Records a call made outside the agent's own requests, such as a tool asking another model
121
+ * about an image: as a request row of this chat, under the answer being written now, and in the
122
+ * agent's usage, so `stats()`, `totalUsage` and the ask's `maxCost` all include it.
123
+ */
124
+ countCall(response: {
125
+ usage?: Usage;
126
+ model?: string;
127
+ provider?: string;
128
+ latency?: number;
129
+ }): Promise<void>;
105
130
  /**
106
131
  * Send a message and persist the conversation.
107
132
  */
108
133
  ask(message: string, options?: AskOptions & {
109
134
  inputs?: I;
110
- }): Promise<MessageRecord>;
135
+ }): Promise<AgentAnswer<T>>;
111
136
  /**
112
137
  * Stream a response and persist the conversation.
113
138
  */
@@ -141,19 +166,25 @@ export declare class AgentSession<I extends Record<string, any> = Record<string,
141
166
  */
142
167
  updateMetadata(metadata: Partial<I>): Promise<void>;
143
168
  }
169
+ /**
170
+ * Agent settings for one session, in place of the agent class's own, such as `maxCost` or
171
+ * `maxToolCalls` for a run with its own budget. Not kept in the store: pass them again to
172
+ * loadAgentSession().
173
+ */
174
+ export type AgentSessionOverrides<I = any> = Omit<Partial<AgentConfig<I>>, "llm" | "inputs" | "tools" | "schema"> & {
175
+ /** Tools for this session, in place of the ones the agent class defines */
176
+ tools?: ChatOptions["tools"];
177
+ };
144
178
  /**
145
179
  * Options for creating a new agent session.
146
180
  */
147
- export interface CreateAgentSessionOptions<I = any> {
181
+ export interface CreateAgentSessionOptions<I = any> extends AgentSessionOverrides<I> {
148
182
  metadata?: I;
149
183
  tableNames?: TableNames;
184
+ /** Changes text before it is saved, after keys and tokens are masked; see StoreRedactor. */
185
+ redact?: StoreRedactor;
150
186
  debug?: boolean;
151
- model?: string;
152
- provider?: string;
153
187
  instructions?: string;
154
- maxToolCalls?: number;
155
- /** Tools for this session, in place of the ones the agent class defines */
156
- tools?: ChatOptions["tools"];
157
188
  }
158
189
  /**
159
190
  * Creates a new agent session and its persistent chat record.
@@ -162,14 +193,16 @@ export declare function createAgentSession<I extends Record<string, any>, T exte
162
193
  /**
163
194
  * Options for loading an existing agent session.
164
195
  */
165
- export interface LoadAgentSessionOptions {
196
+ export interface LoadAgentSessionOptions<I = any> extends AgentSessionOverrides<I> {
166
197
  tableNames?: TableNames;
198
+ /** Changes text before it is saved, after keys and tokens are masked; see StoreRedactor. */
199
+ redact?: StoreRedactor;
167
200
  debug?: boolean;
168
201
  }
169
202
  /**
170
203
  * Loads an existing agent session and re-instantiates the agent with history.
171
204
  */
172
- export declare function loadAgentSession<I extends Record<string, any>, T extends Agent<I, any>>(prismaOrStore: unknown, llm: NodeLLMCore, AgentClass: AgentClass<T>, sessionId: string, options?: LoadAgentSessionOptions): Promise<AgentSession<I, T> | null>;
205
+ export declare function loadAgentSession<I extends Record<string, any>, T extends Agent<I, any>>(prismaOrStore: unknown, llm: NodeLLMCore, AgentClass: AgentClass<T>, sessionId: string, options?: LoadAgentSessionOptions<I>): Promise<AgentSession<I, T> | null>;
173
206
  export {};
174
207
  /**
175
208
  * Dynamic helper to access Prisma models by name.
@@ -1 +1 @@
1
- {"version":3,"file":"AgentSession.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/AgentSession.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,UAAU,EACV,WAAW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yB,CAAC,CAAC,CAAM,GACzC,OAAO,CAAC,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CA+C7B;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB,CAAC,CAAC,GAAG,GAAG,CAAE,SAAQ,qBAAqB,CAAC,CAAC,CAAC;IAChF,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,4FAA4F;IAC5F,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC,SAAS,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,EAC3F,aAAa,EAAE,OAAO,EACtB,GAAG,EAAE,WAAW,EAChB,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,EACzB,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,uBAAuB,CAAC,CAAC,CAAM,GACvC,OAAO,CAAC,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,CAwDpC;;AAED;;;GAGG"}
@@ -1,4 +1,15 @@
1
1
  import { toStore } from "./PrismaStore.js";
2
+ import { storedHistory } from "../../history.js";
3
+ import { StoppedByStoreError } from "../../errors.js";
4
+ import { redactorFor } from "../../redact.js";
5
+ function withData(record, response) {
6
+ Object.defineProperty(record, "data", {
7
+ get: () => response.data,
8
+ enumerable: false,
9
+ configurable: true
10
+ });
11
+ return record;
12
+ }
2
13
  /**
3
14
  * AgentSession - Wraps an Agent instance with persistence capabilities.
4
15
  *
@@ -27,6 +38,8 @@ export class AgentSession {
27
38
  record;
28
39
  agent;
29
40
  currentMessageId = null;
41
+ /** Set when the store asks to stop; the next request throws it instead */
42
+ stopReason;
30
43
  store;
31
44
  debug;
32
45
  constructor(prismaOrStore, llm, AgentClass, record, tableNames, agent = new AgentClass({
@@ -110,8 +123,34 @@ export class AgentSession {
110
123
  this.log(`Failed to update tool call result: ${e}`);
111
124
  }
112
125
  });
126
+ this.agent.beforeRequest(async () => {
127
+ if (this.stopReason !== undefined)
128
+ throw new StoppedByStoreError(this.stopReason);
129
+ });
130
+ // Subagents started from this agent's tools (Agent.toTool, runAgents): their requests are
131
+ // recorded under the answer being written, and the store's `{ stop }` stops them too.
132
+ this.agent.beforeSubagentRequest?.(async () => {
133
+ if (this.stopReason !== undefined)
134
+ throw new StoppedByStoreError(this.stopReason);
135
+ });
136
+ this.agent.afterSubagentResponse?.(async (sub) => {
137
+ const recorded = await this.store.createRequest({
138
+ chatId: this.chatId,
139
+ messageId: this.currentMessageId,
140
+ provider: sub.provider || "unknown",
141
+ model: sub.model || "unknown",
142
+ statusCode: 200,
143
+ duration: sub.latency || 0,
144
+ inputTokens: sub.usage?.input_tokens || 0,
145
+ outputTokens: sub.usage?.output_tokens || 0,
146
+ cachedTokens: sub.usage?.cached_tokens,
147
+ cost: sub.usage?.cost || 0
148
+ });
149
+ if (recorded && recorded.stop !== undefined)
150
+ this.stopReason = recorded.stop;
151
+ });
113
152
  this.agent.afterResponse(async (response) => {
114
- await this.store.createRequest({
153
+ const recorded = await this.store.createRequest({
115
154
  chatId: this.chatId,
116
155
  messageId: this.currentMessageId,
117
156
  provider: response.provider || "unknown",
@@ -123,6 +162,31 @@ export class AgentSession {
123
162
  cachedTokens: response.usage?.cached_tokens,
124
163
  cost: response.usage?.cost || 0
125
164
  });
165
+ if (recorded && recorded.stop !== undefined)
166
+ this.stopReason = recorded.stop;
167
+ });
168
+ }
169
+ /**
170
+ * Records a call made outside the agent's own requests, such as a tool asking another model
171
+ * about an image: as a request row of this chat, under the answer being written now, and in the
172
+ * agent's usage, so `stats()`, `totalUsage` and the ask's `maxCost` all include it.
173
+ */
174
+ async countCall(response) {
175
+ const usage = response.usage;
176
+ if (!usage)
177
+ return;
178
+ this.agent.countUsage(usage);
179
+ await this.store.createRequest({
180
+ chatId: this.chatId,
181
+ messageId: this.currentMessageId,
182
+ provider: response.provider || "unknown",
183
+ model: response.model || "unknown",
184
+ statusCode: 200,
185
+ duration: response.latency || 0,
186
+ inputTokens: usage.input_tokens || 0,
187
+ outputTokens: usage.output_tokens || 0,
188
+ cachedTokens: usage.cached_tokens,
189
+ cost: usage.cost || 0
126
190
  });
127
191
  }
128
192
  /**
@@ -144,7 +208,7 @@ export class AgentSession {
144
208
  // Get response from agent (uses code-defined config + injected history)
145
209
  const response = await this.agent.ask(message, { ...options, inputs });
146
210
  // Update assistant message with response
147
- return (await this.store.updateMessage(assistantMessage.id, {
211
+ const saved = (await this.store.updateMessage(assistantMessage.id, {
148
212
  content: response.content,
149
213
  contentRaw: JSON.stringify(response.meta),
150
214
  inputTokens: response.usage?.input_tokens || 0,
@@ -155,6 +219,7 @@ export class AgentSession {
155
219
  modelId: response.model || null,
156
220
  provider: response.provider || null
157
221
  }));
222
+ return withData(saved, response);
158
223
  }
159
224
  catch (error) {
160
225
  // Clean up placeholder on error
@@ -255,11 +320,19 @@ export class AgentSession {
255
320
  this.agent.resolveLazyConfig(newMetadata);
256
321
  }
257
322
  }
323
+ /** The agent settings in session options, leaving out the session's own options. */
324
+ function agentOverrides(options) {
325
+ const { metadata: _metadata, tableNames: _tableNames, debug: _debug, redact: _redact, ...overrides } = options;
326
+ return Object.fromEntries(Object.entries(overrides).filter(([, value]) => value !== undefined));
327
+ }
258
328
  /**
259
329
  * Creates a new agent session and its persistent chat record.
260
330
  */
261
331
  export async function createAgentSession(prismaOrStore, llm, AgentClass, options = {}) {
262
- const store = toStore(prismaOrStore, options.tableNames);
332
+ const store = toStore(prismaOrStore, options.tableNames, redactorFor(options.redact, [
333
+ ...(AgentClass.middlewares ?? []),
334
+ ...(options.middlewares ?? [])
335
+ ]));
263
336
  if (options.debug) {
264
337
  console.log(`[@node-llm/orm] createAgentSession: agentClass=${AgentClass.name}`);
265
338
  }
@@ -279,13 +352,9 @@ export async function createAgentSession(prismaOrStore, llm, AgentClass, options
279
352
  }));
280
353
  // 3. Instantiate Agent with overrides
281
354
  const agent = new AgentClass({
355
+ ...agentOverrides(options),
282
356
  llm,
283
- inputs: sessionRecord.metadata,
284
- model: options.model,
285
- provider: options.provider,
286
- instructions: options.instructions,
287
- maxToolCalls: options.maxToolCalls,
288
- ...(options.tools ? { tools: options.tools } : {})
357
+ inputs: sessionRecord.metadata
289
358
  });
290
359
  return new AgentSession(store, llm, AgentClass, sessionRecord, options.tableNames, agent, options.debug);
291
360
  }
@@ -293,7 +362,10 @@ export async function createAgentSession(prismaOrStore, llm, AgentClass, options
293
362
  * Loads an existing agent session and re-instantiates the agent with history.
294
363
  */
295
364
  export async function loadAgentSession(prismaOrStore, llm, AgentClass, sessionId, options = {}) {
296
- const store = toStore(prismaOrStore, options.tableNames);
365
+ const store = toStore(prismaOrStore, options.tableNames, redactorFor(options.redact, [
366
+ ...(AgentClass.middlewares ?? []),
367
+ ...(options.middlewares ?? [])
368
+ ]));
297
369
  if (options.debug) {
298
370
  console.log(`[@node-llm/orm] loadAgentSession: id=${sessionId}`);
299
371
  }
@@ -306,17 +378,18 @@ export async function loadAgentSession(prismaOrStore, llm, AgentClass, sessionId
306
378
  if (sessionRecord.agentClass !== AgentClass.name) {
307
379
  throw new Error(`Agent class mismatch: Session "${sessionId}" was created for "${sessionRecord.agentClass}", but is being loaded with "${AgentClass.name}".`);
308
380
  }
309
- // 2. Load message history
381
+ // 2. Load message history, with the tool calls of earlier turns when the store keeps them
310
382
  const messages = (await store.listMessages(sessionRecord.chatId));
311
- // 3. Convert DB messages to NodeLLM Message format
312
- const history = messages.map((m) => ({
313
- role: m.role,
314
- content: m.content || ""
315
- }));
383
+ const toolCalls = store.listToolCalls
384
+ ? await store.listToolCalls(messages.map((message) => message.id))
385
+ : [];
386
+ // 3. Convert DB records to NodeLLM messages
387
+ const history = storedHistory(messages, toolCalls);
316
388
  // 4. Instantiate agent with injected history, LLM, AND metadata (as inputs)
317
389
  // "Code Wins" - model, tools, instructions come from AgentClass
318
390
  // Metadata from DB handles the lazy resolution of behavior
319
391
  const agent = new AgentClass({
392
+ ...agentOverrides(options),
320
393
  llm,
321
394
  messages: history,
322
395
  inputs: sessionRecord.metadata
@@ -1,5 +1,6 @@
1
1
  import type { NodeLLMCore, ChatChunk, AskOptions, Usage } from "@node-llm/core";
2
2
  import { BaseChat, type ChatRecord, type ChatOptions } from "../../BaseChat.js";
3
+ import { type StoreRedactor } from "../../redact.js";
3
4
  export { type ChatRecord, type ChatOptions };
4
5
  export interface MessageRecord {
5
6
  id: string;
@@ -31,6 +32,8 @@ export declare class Chat extends BaseChat {
31
32
  private llm;
32
33
  private store;
33
34
  private persistenceConfig;
35
+ /** Set when the store asks to stop; the next request throws it instead */
36
+ private stopReason?;
34
37
  constructor(prismaOrStore: unknown, llm: NodeLLMCore, record: ChatRecord, options?: ChatOptions, tableNames?: TableNames);
35
38
  /**
36
39
  * Internal prep for core Chat instance with persistence hooks.
@@ -59,6 +62,7 @@ export declare class Chat extends BaseChat {
59
62
  */
60
63
  export declare function createChat<T = Record<string, any>>(prismaOrStore: unknown, llm: NodeLLMCore, options?: ChatOptions & {
61
64
  tableNames?: TableNames;
65
+ redact?: StoreRedactor;
62
66
  } & T): Promise<Chat>;
63
67
  /**
64
68
  * Loads an existing chat session from a Prisma client or any ChatStore.
@@ -66,5 +70,6 @@ export declare function createChat<T = Record<string, any>>(prismaOrStore: unkno
66
70
  export declare function loadChat(prismaOrStore: unknown, llm: NodeLLMCore, chatId: string, options?: ChatOptions & {
67
71
  tableNames?: TableNames;
68
72
  debug?: boolean;
73
+ redact?: StoreRedactor;
69
74
  }): Promise<Chat | null>;
70
75
  //# sourceMappingURL=Chat.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Chat.d.ts","sourceRoot":"","sources":["../../../src/adapters/prisma/Chat.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAChF,OAA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