@librechat/agents 4.2.11 → 4.2.13

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 (348) hide show
  1. package/dist/cjs/graphs/Graph.cjs +15 -3
  2. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  3. package/dist/cjs/langfuse.cjs +63 -35
  4. package/dist/cjs/langfuse.cjs.map +1 -1
  5. package/dist/cjs/langfuseConfig.cjs +6 -0
  6. package/dist/cjs/langfuseConfig.cjs.map +1 -1
  7. package/dist/cjs/langfuseToolOutputTracing.cjs +1 -1
  8. package/dist/cjs/langfuseToolOutputTracing.cjs.map +1 -1
  9. package/dist/cjs/langfuseTraceShaping.cjs +18 -8
  10. package/dist/cjs/langfuseTraceShaping.cjs.map +1 -1
  11. package/dist/cjs/llm/anthropic/index.cjs +4 -3
  12. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  13. package/dist/cjs/llm/anthropic/utils/container.cjs +37 -0
  14. package/dist/cjs/llm/anthropic/utils/container.cjs.map +1 -0
  15. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +25 -23
  16. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
  17. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +15 -21
  18. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
  19. package/dist/cjs/llm/anthropic/utils/stream_events.cjs +16 -14
  20. package/dist/cjs/llm/anthropic/utils/stream_events.cjs.map +1 -1
  21. package/dist/cjs/llm/bedrock/index.cjs +1 -1
  22. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  23. package/dist/cjs/llm/bedrock/toolCache.cjs +1 -1
  24. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +19 -13
  25. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -1
  26. package/dist/cjs/llm/google/utils/common.cjs +59 -13
  27. package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
  28. package/dist/cjs/llm/init.cjs +12 -2
  29. package/dist/cjs/llm/init.cjs.map +1 -1
  30. package/dist/cjs/llm/openai/index.cjs +15 -2
  31. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  32. package/dist/cjs/llm/openai/responseToolArguments.cjs +62 -0
  33. package/dist/cjs/llm/openai/responseToolArguments.cjs.map +1 -0
  34. package/dist/cjs/llm/openrouter/index.cjs +11 -2
  35. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  36. package/dist/cjs/llm/openrouter/toolCache.cjs +1 -1
  37. package/dist/cjs/llm/prepareProviderRequest.cjs +35 -7
  38. package/dist/cjs/llm/prepareProviderRequest.cjs.map +1 -1
  39. package/dist/cjs/main.cjs +9 -3
  40. package/dist/cjs/messages/alternation.cjs +1 -1
  41. package/dist/cjs/messages/anthropicServerTools.cjs +20 -0
  42. package/dist/cjs/messages/anthropicServerTools.cjs.map +1 -0
  43. package/dist/cjs/messages/budget.cjs +1 -1
  44. package/dist/cjs/messages/cache.cjs +14 -1
  45. package/dist/cjs/messages/cache.cjs.map +1 -1
  46. package/dist/cjs/messages/core.cjs +4 -3
  47. package/dist/cjs/messages/core.cjs.map +1 -1
  48. package/dist/cjs/messages/format.cjs +3 -3
  49. package/dist/cjs/messages/index.cjs +1 -1
  50. package/dist/cjs/messages/prune.cjs +1 -1
  51. package/dist/cjs/messages/recency.cjs +1 -1
  52. package/dist/cjs/messages/toolResultTypes.cjs +21 -45
  53. package/dist/cjs/messages/toolResultTypes.cjs.map +1 -1
  54. package/dist/cjs/protection/providerText.cjs +4 -0
  55. package/dist/cjs/protection/providerText.cjs.map +1 -1
  56. package/dist/cjs/protection/toolResult.cjs +119 -0
  57. package/dist/cjs/protection/toolResult.cjs.map +1 -0
  58. package/dist/cjs/run.cjs +31 -13
  59. package/dist/cjs/run.cjs.map +1 -1
  60. package/dist/cjs/runtime/admission.cjs +1 -1
  61. package/dist/cjs/runtime/client.cjs +3 -0
  62. package/dist/cjs/runtime/client.cjs.map +1 -1
  63. package/dist/cjs/stream.cjs +2 -1
  64. package/dist/cjs/stream.cjs.map +1 -1
  65. package/dist/cjs/summarization/node.cjs +9 -4
  66. package/dist/cjs/summarization/node.cjs.map +1 -1
  67. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +1 -1
  68. package/dist/cjs/tools/ToolNode.cjs +47 -14
  69. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  70. package/dist/cjs/tools/local/CompileCheckTool.cjs +1 -1
  71. package/dist/cjs/tools/local/LocalCodingTools.cjs +1 -1
  72. package/dist/cjs/tools/search/content.cjs +163 -42
  73. package/dist/cjs/tools/search/content.cjs.map +1 -1
  74. package/dist/cjs/tools/subagent/SubagentExecutor.cjs +8 -0
  75. package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
  76. package/dist/cjs/utils/events.cjs +2 -2
  77. package/dist/cjs/utils/events.cjs.map +1 -1
  78. package/dist/cjs/utils/tokens.cjs +7 -2
  79. package/dist/cjs/utils/tokens.cjs.map +1 -1
  80. package/dist/esm/graphs/Graph.mjs +15 -3
  81. package/dist/esm/graphs/Graph.mjs.map +1 -1
  82. package/dist/esm/langfuse.mjs +63 -35
  83. package/dist/esm/langfuse.mjs.map +1 -1
  84. package/dist/esm/langfuseConfig.mjs +6 -1
  85. package/dist/esm/langfuseConfig.mjs.map +1 -1
  86. package/dist/esm/langfuseToolOutputTracing.mjs +2 -2
  87. package/dist/esm/langfuseToolOutputTracing.mjs.map +1 -1
  88. package/dist/esm/langfuseTraceShaping.mjs +18 -9
  89. package/dist/esm/langfuseTraceShaping.mjs.map +1 -1
  90. package/dist/esm/llm/anthropic/index.mjs +4 -3
  91. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  92. package/dist/esm/llm/anthropic/utils/container.mjs +37 -0
  93. package/dist/esm/llm/anthropic/utils/container.mjs.map +1 -0
  94. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +25 -23
  95. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
  96. package/dist/esm/llm/anthropic/utils/message_outputs.mjs +15 -21
  97. package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
  98. package/dist/esm/llm/anthropic/utils/stream_events.mjs +16 -14
  99. package/dist/esm/llm/anthropic/utils/stream_events.mjs.map +1 -1
  100. package/dist/esm/llm/bedrock/index.mjs +2 -2
  101. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  102. package/dist/esm/llm/bedrock/toolCache.mjs +1 -1
  103. package/dist/esm/llm/bedrock/utils/message_inputs.mjs +19 -14
  104. package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -1
  105. package/dist/esm/llm/google/utils/common.mjs +59 -13
  106. package/dist/esm/llm/google/utils/common.mjs.map +1 -1
  107. package/dist/esm/llm/init.mjs +12 -2
  108. package/dist/esm/llm/init.mjs.map +1 -1
  109. package/dist/esm/llm/openai/index.mjs +16 -3
  110. package/dist/esm/llm/openai/index.mjs.map +1 -1
  111. package/dist/esm/llm/openai/responseToolArguments.mjs +62 -0
  112. package/dist/esm/llm/openai/responseToolArguments.mjs.map +1 -0
  113. package/dist/esm/llm/openrouter/index.mjs +11 -2
  114. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  115. package/dist/esm/llm/openrouter/toolCache.mjs +1 -1
  116. package/dist/esm/llm/prepareProviderRequest.mjs +35 -7
  117. package/dist/esm/llm/prepareProviderRequest.mjs.map +1 -1
  118. package/dist/esm/main.mjs +5 -4
  119. package/dist/esm/messages/alternation.mjs +1 -1
  120. package/dist/esm/messages/anthropicServerTools.mjs +18 -0
  121. package/dist/esm/messages/anthropicServerTools.mjs.map +1 -0
  122. package/dist/esm/messages/budget.mjs +1 -1
  123. package/dist/esm/messages/cache.mjs +12 -2
  124. package/dist/esm/messages/cache.mjs.map +1 -1
  125. package/dist/esm/messages/core.mjs +4 -3
  126. package/dist/esm/messages/core.mjs.map +1 -1
  127. package/dist/esm/messages/format.mjs +3 -3
  128. package/dist/esm/messages/index.mjs +1 -1
  129. package/dist/esm/messages/prune.mjs +1 -1
  130. package/dist/esm/messages/recency.mjs +1 -1
  131. package/dist/esm/messages/toolResultTypes.mjs +21 -45
  132. package/dist/esm/messages/toolResultTypes.mjs.map +1 -1
  133. package/dist/esm/protection/providerText.mjs +4 -1
  134. package/dist/esm/protection/providerText.mjs.map +1 -1
  135. package/dist/esm/protection/toolResult.mjs +114 -0
  136. package/dist/esm/protection/toolResult.mjs.map +1 -0
  137. package/dist/esm/run.mjs +32 -14
  138. package/dist/esm/run.mjs.map +1 -1
  139. package/dist/esm/runtime/admission.mjs +1 -1
  140. package/dist/esm/runtime/client.mjs +3 -0
  141. package/dist/esm/runtime/client.mjs.map +1 -1
  142. package/dist/esm/stream.mjs +2 -1
  143. package/dist/esm/stream.mjs.map +1 -1
  144. package/dist/esm/summarization/node.mjs +9 -4
  145. package/dist/esm/summarization/node.mjs.map +1 -1
  146. package/dist/esm/tools/ProgrammaticToolCalling.mjs +1 -1
  147. package/dist/esm/tools/ToolNode.mjs +47 -14
  148. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  149. package/dist/esm/tools/local/CompileCheckTool.mjs +1 -1
  150. package/dist/esm/tools/local/LocalCodingTools.mjs +1 -1
  151. package/dist/esm/tools/search/content.mjs +163 -42
  152. package/dist/esm/tools/search/content.mjs.map +1 -1
  153. package/dist/esm/tools/subagent/SubagentExecutor.mjs +8 -0
  154. package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
  155. package/dist/esm/utils/events.mjs +2 -2
  156. package/dist/esm/utils/events.mjs.map +1 -1
  157. package/dist/esm/utils/tokens.mjs +7 -2
  158. package/dist/esm/utils/tokens.mjs.map +1 -1
  159. package/dist/types/common/index.d.ts +2 -2
  160. package/dist/types/decisions/breaker.d.ts +2 -2
  161. package/dist/types/decisions/core.d.ts +13 -13
  162. package/dist/types/decisions/dialect.d.ts +1 -1
  163. package/dist/types/decisions/http.d.ts +2 -2
  164. package/dist/types/decisions/index.d.ts +5 -5
  165. package/dist/types/decisions/node.d.ts +2 -2
  166. package/dist/types/decisions/presets.d.ts +2 -2
  167. package/dist/types/decisions/probe.d.ts +1 -1
  168. package/dist/types/decisions/questions.d.ts +1 -1
  169. package/dist/types/decisions/structuredChat.d.ts +2 -2
  170. package/dist/types/decisions/transport.d.ts +1 -1
  171. package/dist/types/eventActor/EventActorExecutor.d.ts +1 -1
  172. package/dist/types/eventActor/index.d.ts +2 -2
  173. package/dist/types/graphs/Graph.d.ts +6 -2
  174. package/dist/types/graphs/MultiAgentGraph.d.ts +1 -1
  175. package/dist/types/graphs/applyGraphRuntimeConfig.d.ts +1 -1
  176. package/dist/types/graphs/graphFactory.d.ts +1 -1
  177. package/dist/types/graphs/index.d.ts +5 -5
  178. package/dist/types/harness/fileBindings.d.ts +2 -2
  179. package/dist/types/harness/fileService.d.ts +2 -2
  180. package/dist/types/harness/fileStream.d.ts +1 -1
  181. package/dist/types/harness/index.d.ts +15 -15
  182. package/dist/types/harness/model.d.ts +1 -1
  183. package/dist/types/harness/modelService.d.ts +2 -2
  184. package/dist/types/harness/modelStreamCallbacks.d.ts +1 -1
  185. package/dist/types/harness/providerText.d.ts +1 -1
  186. package/dist/types/hitl/index.d.ts +3 -3
  187. package/dist/types/hooks/HookRegistry.d.ts +1 -1
  188. package/dist/types/hooks/createToolPolicyHook.d.ts +1 -1
  189. package/dist/types/hooks/createWorkspacePolicyHook.d.ts +1 -1
  190. package/dist/types/hooks/executeHooks.d.ts +2 -2
  191. package/dist/types/hooks/index.d.ts +11 -11
  192. package/dist/types/index.d.ts +62 -60
  193. package/dist/types/langchain/index.d.ts +8 -8
  194. package/dist/types/langfuse.d.ts +7 -4
  195. package/dist/types/langfuseConfig.d.ts +5 -0
  196. package/dist/types/langfuseTraceShaping.d.ts +5 -0
  197. package/dist/types/llm/anthropic/utils/container.d.ts +4 -0
  198. package/dist/types/llm/anthropic/utils/message_inputs.d.ts +1 -1
  199. package/dist/types/llm/anthropic/utils/message_outputs.d.ts +1 -1
  200. package/dist/types/llm/anthropic/utils/stream_events.d.ts +1 -1
  201. package/dist/types/llm/bedrock/index.d.ts +2 -3
  202. package/dist/types/llm/bedrock/utils/harmony.d.ts +1 -1
  203. package/dist/types/llm/bedrock/utils/index.d.ts +2 -2
  204. package/dist/types/llm/bedrock/utils/message_inputs.d.ts +3 -12
  205. package/dist/types/llm/bedrock/utils/message_outputs.d.ts +1 -1
  206. package/dist/types/llm/google/index.d.ts +2 -2
  207. package/dist/types/llm/google/utils/common.d.ts +1 -1
  208. package/dist/types/llm/google/utils/tools.d.ts +1 -1
  209. package/dist/types/llm/openai/index.d.ts +2 -1
  210. package/dist/types/llm/openai/responseToolArguments.d.ts +7 -0
  211. package/dist/types/llm/openrouter/index.d.ts +1 -0
  212. package/dist/types/llm/prepareProviderRequest.d.ts +4 -1
  213. package/dist/types/llm/providerRegistry.d.ts +1 -1
  214. package/dist/types/llm/providers.d.ts +1 -1
  215. package/dist/types/messages/anthropicServerTools.d.ts +13 -0
  216. package/dist/types/messages/cache.d.ts +13 -0
  217. package/dist/types/messages/contextPruning.d.ts +1 -1
  218. package/dist/types/messages/core.d.ts +4 -4
  219. package/dist/types/messages/format.d.ts +1 -1
  220. package/dist/types/messages/index.d.ts +21 -21
  221. package/dist/types/messages/prune.d.ts +1 -1
  222. package/dist/types/openai/index.d.ts +2 -2
  223. package/dist/types/openai/toolProjection.d.ts +1 -1
  224. package/dist/types/prompts/index.d.ts +2 -2
  225. package/dist/types/protection/providerText.d.ts +3 -1
  226. package/dist/types/protection/toolResult.d.ts +69 -0
  227. package/dist/types/provider-registration.d.ts +1 -1
  228. package/dist/types/rerank/bm25.d.ts +1 -1
  229. package/dist/types/rerank/core.d.ts +17 -17
  230. package/dist/types/rerank/crossEncoder.d.ts +3 -3
  231. package/dist/types/rerank/fallback.d.ts +1 -1
  232. package/dist/types/rerank/index.d.ts +2 -2
  233. package/dist/types/rerank/outcome.d.ts +2 -2
  234. package/dist/types/rerank/pin.d.ts +2 -2
  235. package/dist/types/rerank/search.d.ts +1 -1
  236. package/dist/types/rerank/systemone.d.ts +4 -4
  237. package/dist/types/rerank/types.d.ts +1 -1
  238. package/dist/types/run.d.ts +2 -0
  239. package/dist/types/runtime/admission.d.ts +1 -1
  240. package/dist/types/runtime/checkpoint.d.ts +1 -1
  241. package/dist/types/runtime/client.d.ts +2 -2
  242. package/dist/types/runtime/files.d.ts +4 -4
  243. package/dist/types/runtime/hostTools.d.ts +2 -2
  244. package/dist/types/runtime/index.d.ts +12 -12
  245. package/dist/types/runtime/model.d.ts +5 -5
  246. package/dist/types/runtime/policy.d.ts +3 -3
  247. package/dist/types/runtime/recovery/client.d.ts +3 -3
  248. package/dist/types/runtime/recovery/log.d.ts +2 -2
  249. package/dist/types/runtime/recovery/redis.d.ts +1 -1
  250. package/dist/types/runtime/recovery/server.d.ts +3 -3
  251. package/dist/types/runtime/resume.d.ts +1 -1
  252. package/dist/types/runtime/server.d.ts +3 -3
  253. package/dist/types/runtime/snapshot.d.ts +1 -1
  254. package/dist/types/runtime/types.d.ts +2 -2
  255. package/dist/types/session/AgentSession.d.ts +2 -2
  256. package/dist/types/session/JsonlSessionStore.d.ts +1 -1
  257. package/dist/types/session/deriveMessages.d.ts +1 -1
  258. package/dist/types/session/handlers.d.ts +1 -1
  259. package/dist/types/session/index.d.ts +7 -7
  260. package/dist/types/session/messageSerialization.d.ts +1 -1
  261. package/dist/types/session/sessionProjection.d.ts +3 -3
  262. package/dist/types/summarization/index.d.ts +2 -2
  263. package/dist/types/summarization/node.d.ts +2 -0
  264. package/dist/types/tools/BashProgrammaticToolCalling.d.ts +1 -1
  265. package/dist/types/tools/GitHubCompare.d.ts +6 -6
  266. package/dist/types/tools/ProgrammaticToolCalling.d.ts +1 -1
  267. package/dist/types/tools/ReadFile.d.ts +6 -6
  268. package/dist/types/tools/SkillTool.d.ts +6 -6
  269. package/dist/types/tools/ToolNode.d.ts +5 -1
  270. package/dist/types/tools/cloudflare/index.d.ts +4 -4
  271. package/dist/types/tools/local/FileCheckpointer.d.ts +1 -1
  272. package/dist/types/tools/local/LocalExecutionEngine.d.ts +1 -1
  273. package/dist/types/tools/local/attachments.d.ts +1 -1
  274. package/dist/types/tools/local/index.d.ts +12 -12
  275. package/dist/types/tools/search/anthropic.d.ts +1 -1
  276. package/dist/types/tools/search/content.d.ts +38 -1
  277. package/dist/types/tools/search/crw-scraper.d.ts +1 -1
  278. package/dist/types/tools/search/crw-search.d.ts +1 -1
  279. package/dist/types/tools/search/firecrawl.d.ts +1 -1
  280. package/dist/types/tools/search/format.d.ts +1 -1
  281. package/dist/types/tools/search/highlights.d.ts +1 -1
  282. package/dist/types/tools/search/index.d.ts +3 -3
  283. package/dist/types/tools/search/keenable-scraper.d.ts +1 -1
  284. package/dist/types/tools/search/keenable-search.d.ts +1 -1
  285. package/dist/types/tools/search/metrics.d.ts +1 -1
  286. package/dist/types/tools/search/rerankers.d.ts +1 -1
  287. package/dist/types/tools/search/schema.d.ts +6 -6
  288. package/dist/types/tools/search/search.d.ts +1 -1
  289. package/dist/types/tools/search/serper-scraper.d.ts +1 -1
  290. package/dist/types/tools/search/tavily-scraper.d.ts +1 -1
  291. package/dist/types/tools/search/tavily-search.d.ts +1 -1
  292. package/dist/types/tools/search/tool.d.ts +3 -3
  293. package/dist/types/tools/search/types.d.ts +2 -2
  294. package/dist/types/tools/search/utils.d.ts +3 -3
  295. package/dist/types/tools/subagent/SubagentExecutionRegistry.d.ts +1 -1
  296. package/dist/types/tools/subagent/SubagentExecutor.d.ts +9 -3
  297. package/dist/types/tools/subagent/diagnostics.d.ts +1 -1
  298. package/dist/types/tools/subagent/index.d.ts +7 -7
  299. package/dist/types/types/attachments.d.ts +25 -0
  300. package/dist/types/types/graph.d.ts +15 -0
  301. package/dist/types/types/index.d.ts +14 -13
  302. package/dist/types/types/llm.d.ts +2 -2
  303. package/dist/types/types/run.d.ts +7 -2
  304. package/dist/types/types/subagentTasks.d.ts +2 -2
  305. package/dist/types/types/tools.d.ts +6 -3
  306. package/dist/types/utils/events.d.ts +2 -1
  307. package/dist/types/utils/index.d.ts +10 -10
  308. package/dist/types/utils/tokens.d.ts +1 -0
  309. package/package.json +14 -12
  310. package/src/graphs/Graph.ts +32 -1
  311. package/src/index.ts +8 -0
  312. package/src/langfuse.ts +129 -41
  313. package/src/langfuseConfig.ts +14 -0
  314. package/src/langfuseToolOutputTracing.ts +5 -4
  315. package/src/langfuseTraceShaping.ts +49 -12
  316. package/src/llm/anthropic/index.ts +11 -3
  317. package/src/llm/anthropic/utils/container.ts +63 -0
  318. package/src/llm/anthropic/utils/message_inputs.ts +57 -51
  319. package/src/llm/anthropic/utils/message_outputs.ts +12 -39
  320. package/src/llm/anthropic/utils/stream_events.ts +17 -12
  321. package/src/llm/bedrock/index.ts +7 -5
  322. package/src/llm/bedrock/utils/message_inputs.ts +28 -35
  323. package/src/llm/google/utils/common.ts +88 -36
  324. package/src/llm/init.ts +35 -3
  325. package/src/llm/openai/index.ts +17 -2
  326. package/src/llm/openai/responseToolArguments.ts +104 -0
  327. package/src/llm/openrouter/index.ts +13 -0
  328. package/src/llm/prepareProviderRequest.ts +75 -13
  329. package/src/messages/anthropicServerTools.ts +24 -0
  330. package/src/messages/cache.ts +25 -5
  331. package/src/messages/core.ts +12 -11
  332. package/src/messages/toolResultTypes.ts +80 -88
  333. package/src/protection/providerText.ts +127 -40
  334. package/src/protection/toolResult.ts +272 -0
  335. package/src/run.ts +112 -47
  336. package/src/runtime/client.ts +7 -0
  337. package/src/stream.ts +7 -1
  338. package/src/summarization/node.ts +17 -4
  339. package/src/tools/ToolNode.ts +114 -18
  340. package/src/tools/search/content.ts +282 -62
  341. package/src/tools/subagent/SubagentExecutor.ts +12 -0
  342. package/src/types/attachments.ts +31 -0
  343. package/src/types/graph.ts +12 -0
  344. package/src/types/index.ts +1 -0
  345. package/src/types/run.ts +8 -1
  346. package/src/types/tools.ts +3 -0
  347. package/src/utils/events.ts +9 -2
  348. package/src/utils/tokens.ts +28 -2
@@ -0,0 +1,24 @@
1
+ export const ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE = {
2
+ advisor_tool_result: ['advisor'],
3
+ bash_code_execution_tool_result: ['bash_code_execution'],
4
+ code_execution_tool_result: ['code_execution'],
5
+ text_editor_code_execution_tool_result: ['text_editor_code_execution'],
6
+ tool_search_tool_result: ['tool_search_tool_regex', 'tool_search_tool_bm25'],
7
+ web_fetch_tool_result: ['web_fetch'],
8
+ web_search_tool_result: ['web_search'],
9
+ } as const;
10
+
11
+ type AnthropicServerToolResultType =
12
+ keyof typeof ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE;
13
+
14
+ export const ANTHROPIC_SERVER_TOOL_RESULT_TYPES = Object.keys(
15
+ ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE
16
+ ) as AnthropicServerToolResultType[];
17
+
18
+ export function isAnthropicServerToolResultType(
19
+ type: string | undefined
20
+ ): type is AnthropicServerToolResultType {
21
+ return (
22
+ type != null && Object.hasOwn(ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE, type)
23
+ );
24
+ }
@@ -3,8 +3,8 @@ import { BaseMessage, MessageContentComplex } from '@langchain/core/messages';
3
3
  import type Anthropic from '@anthropic-ai/sdk';
4
4
  import type { AnthropicMessage } from '@/types/messages';
5
5
  import { hasComputerCallOutputMarker } from '@/utils/toolContent';
6
- import { toLangChainContent } from './langchain';
7
6
  import { isReasoningContentBlock } from './reasoningTypes';
7
+ import { toLangChainContent } from './langchain';
8
8
  import { ContentTypes } from '@/common/enum';
9
9
 
10
10
  type MessageWithContent = {
@@ -412,9 +412,7 @@ function isCachePoint(block: unknown): boolean {
412
412
  * Anchoring the only breakpoint on a block that is about to disappear silently
413
413
  * loses tail caching, so all of these are excluded.
414
414
  */
415
- const NON_ANCHORABLE_BLOCK_TYPES = new Set([
416
- 'input_json_delta',
417
- ]);
415
+ const NON_ANCHORABLE_BLOCK_TYPES = new Set(['input_json_delta']);
418
416
 
419
417
  /**
420
418
  * A block can anchor the tail cache breakpoint when it is a real content block
@@ -422,7 +420,7 @@ const NON_ANCHORABLE_BLOCK_TYPES = new Set([
422
420
  * conversion. Reasoning and dropped-delta blocks are excluded, and empty text
423
421
  * blocks are not cacheable, so all are skipped.
424
422
  */
425
- function isTailCacheableBlock(block: MessageContentComplex): boolean {
423
+ export function isTailCacheableBlock(block: MessageContentComplex): boolean {
426
424
  if (isCachePoint(block)) {
427
425
  return false;
428
426
  }
@@ -441,6 +439,28 @@ function isTailCacheableBlock(block: MessageContentComplex): boolean {
441
439
  return true;
442
440
  }
443
441
 
442
+ type CacheControl = NonNullable<Anthropic.TextBlockParam['cache_control']>;
443
+
444
+ /** The Anthropic cache breakpoint a block carries, if any. */
445
+ export function getCacheControl(
446
+ block: MessageContentComplex
447
+ ): CacheControl | undefined {
448
+ if (!('cache_control' in block) || block.cache_control == null) {
449
+ return undefined;
450
+ }
451
+ return block.cache_control as CacheControl;
452
+ }
453
+
454
+ /** `block` carrying `marker`, unless it already has a breakpoint of its own. */
455
+ export function withCacheControl<T extends MessageContentComplex>(
456
+ block: T,
457
+ marker: CacheControl
458
+ ): T {
459
+ return getCacheControl(block) == null
460
+ ? { ...block, cache_control: marker }
461
+ : block;
462
+ }
463
+
444
464
  /**
445
465
  * Anthropic API: single tail cache breakpoint (default strategy).
446
466
  *
@@ -9,13 +9,13 @@ import {
9
9
  } from '@langchain/core/messages';
10
10
  import type { ContentBlock as LangChainContentBlock } from '@langchain/core/messages';
11
11
  import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
12
- import type { ProviderMessageProvenancePart } from './provenance';
13
- import type * as t from '@/types';
14
12
  import type {
15
13
  ResponsesReplayPosition,
16
14
  ToolHistoryPreparation,
17
15
  OrderedToolHistoryProjection,
18
16
  } from './toolHistoryProjection';
17
+ import type { ProviderMessageProvenancePart } from './provenance';
18
+ import type * as t from '@/types';
19
19
  import {
20
20
  cloneToolMessageWithContent,
21
21
  compactToolContent,
@@ -27,6 +27,13 @@ import {
27
27
  isComputerCallOutputMessage,
28
28
  serializeToolContentBounded,
29
29
  } from '@/utils/toolContent';
30
+ import {
31
+ OPENAI_RESPONSES_REPLAY_POSITIONS_KEY,
32
+ createToolHistoryPreparation,
33
+ getGeneratedImageMimeType,
34
+ getResponsesHistorySource,
35
+ isResponsesReplayPosition,
36
+ } from './toolHistoryProjection';
30
37
  import {
31
38
  getProviderMessageProvenance,
32
39
  getProviderSourceMessageIds,
@@ -34,18 +41,12 @@ import {
34
41
  setProviderMessageProvenance,
35
42
  stampSyntheticProviderMessage,
36
43
  } from './provenance';
44
+ import { ANTHROPIC_SERVER_TOOL_RESULT_TYPES } from './anthropicServerTools';
37
45
  import { HARD_MAX_TOOL_RESULT_CHARS } from '@/utils/truncation';
38
- import { stripAnthropicCacheControl } from './cache';
39
46
  import { isReasoningContentBlock } from './reasoningTypes';
47
+ import { stripAnthropicCacheControl } from './cache';
40
48
  import { ContentTypes, Providers } from '@/common';
41
49
  import { toLangChainContent } from './langchain';
42
- import {
43
- OPENAI_RESPONSES_REPLAY_POSITIONS_KEY,
44
- createToolHistoryPreparation,
45
- getGeneratedImageMimeType,
46
- getResponsesHistorySource,
47
- isResponsesReplayPosition,
48
- } from './toolHistoryProjection';
49
50
 
50
51
  type ReasoningSummary = { summary?: Array<{ text?: string }> };
51
52
  type ReasoningDetail = { type?: string; text?: string };
@@ -91,7 +92,7 @@ const allowedTypesByProvider: Record<string, string[]> = {
91
92
  'thinking',
92
93
  'redacted_thinking',
93
94
  'server_tool_use',
94
- 'web_search_tool_result',
95
+ ...ANTHROPIC_SERVER_TOOL_RESULT_TYPES,
95
96
  'web_search_result',
96
97
  ],
97
98
  [Providers.BEDROCK]: [..._allowedTypes, 'reasoning_content'],
@@ -5,6 +5,10 @@ import type {
5
5
  ToolMessage,
6
6
  } from '@langchain/core/messages';
7
7
  import type { ProviderName } from '@/types/llm';
8
+ import {
9
+ ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE,
10
+ isAnthropicServerToolResultType,
11
+ } from './anthropicServerTools';
8
12
  import { getProviderFamily } from '@/llm/providers';
9
13
 
10
14
  export type ProviderToolCallKind =
@@ -61,16 +65,6 @@ const MCP_CALL_KINDS = ['mcp'] as const;
61
65
  const GOOGLE_CALL_KINDS = ['google'] as const;
62
66
  const BEDROCK_CALL_KINDS = ['bedrock', 'tool'] as const;
63
67
 
64
- const ADVISOR_TOOL_NAMES = ['advisor'] as const;
65
- const BASH_TOOL_NAMES = ['bash_code_execution'] as const;
66
- const CODE_EXECUTION_TOOL_NAMES = ['code_execution'] as const;
67
- const TEXT_EDITOR_TOOL_NAMES = ['text_editor_code_execution'] as const;
68
- const TOOL_SEARCH_TOOL_NAMES = [
69
- 'tool_search_tool_regex',
70
- 'tool_search_tool_bm25',
71
- ] as const;
72
- const WEB_FETCH_TOOL_NAMES = ['web_fetch'] as const;
73
- const WEB_SEARCH_TOOL_NAMES = ['web_search'] as const;
74
68
  const ANTHROPIC_SERVER_TOOL_ID_PREFIX = 'srvtoolu_';
75
69
 
76
70
  function stringSet(values: readonly string[]): ReadonlySet<string> {
@@ -95,15 +89,9 @@ const PROVIDER_TOOL_CALL_KINDS = stringSet([
95
89
  'server',
96
90
  'tool',
97
91
  ]);
98
- const ANTHROPIC_SERVER_TOOL_NAMES = stringSet([
99
- ...ADVISOR_TOOL_NAMES,
100
- ...BASH_TOOL_NAMES,
101
- ...CODE_EXECUTION_TOOL_NAMES,
102
- ...TEXT_EDITOR_TOOL_NAMES,
103
- ...TOOL_SEARCH_TOOL_NAMES,
104
- ...WEB_FETCH_TOOL_NAMES,
105
- ...WEB_SEARCH_TOOL_NAMES,
106
- ]);
92
+ const ANTHROPIC_SERVER_TOOL_NAMES = stringSet(
93
+ Object.values(ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE).flat()
94
+ );
107
95
 
108
96
  function getRecord(value: unknown): Record<string, unknown> | undefined {
109
97
  if (value == null || typeof value !== 'object') {
@@ -399,17 +387,6 @@ function hasValidProviderMetadata(record: Record<string, unknown>): boolean {
399
387
  );
400
388
  }
401
389
 
402
- const ANTHROPIC_STRUCTURED_RESULT_TOOL_NAMES: Readonly<
403
- Partial<Record<string, readonly string[]>>
404
- > = {
405
- advisor_tool_result: ADVISOR_TOOL_NAMES,
406
- bash_code_execution_tool_result: BASH_TOOL_NAMES,
407
- code_execution_tool_result: CODE_EXECUTION_TOOL_NAMES,
408
- text_editor_code_execution_tool_result: TEXT_EDITOR_TOOL_NAMES,
409
- tool_search_tool_result: TOOL_SEARCH_TOOL_NAMES,
410
- web_fetch_tool_result: WEB_FETCH_TOOL_NAMES,
411
- };
412
-
413
390
  /** Designated provider output is opaque once its exact envelope and matching
414
391
  * call establish authorship. Only its container size/proxy identity is read;
415
392
  * nested values are deliberately not walked on the formatting hot path. */
@@ -601,42 +578,36 @@ const LOCAL_RESULT_METADATA_FIELDS = [
601
578
  'agentId',
602
579
  'groupId',
603
580
  ] as const;
604
- const TOOL_RESULT_ENVELOPE_FIELDS: Readonly<Record<string, readonly string[]>> =
605
- {
606
- advisor_tool_result: ANTHROPIC_RESULT_FIELDS,
607
- bash_code_execution_tool_result: ANTHROPIC_RESULT_FIELDS,
608
- code_execution_tool_result: ANTHROPIC_RESULT_FIELDS,
609
- mcp_tool_result: ANTHROPIC_GENERIC_RESULT_FIELDS,
610
- text_editor_code_execution_tool_result: ANTHROPIC_RESULT_FIELDS,
611
- tool_search_tool_result: ANTHROPIC_RESULT_FIELDS,
612
- tool_result: ANTHROPIC_GENERIC_RESULT_FIELDS,
613
- web_fetch_tool_result: ANTHROPIC_SERVER_RESULT_FIELDS,
614
- web_search_tool_result: ANTHROPIC_SERVER_RESULT_FIELDS,
615
- server_tool_call_result: [
616
- 'type',
617
- 'toolCallId',
618
- 'status',
619
- 'output',
620
- ...LOCAL_RESULT_METADATA_FIELDS,
621
- ],
622
- server_tool_result: [
623
- 'type',
624
- 'tool_call_id',
625
- 'status',
626
- 'output',
627
- ...LOCAL_RESULT_METADATA_FIELDS,
628
- ],
629
- codeExecutionResult: ['type', 'codeExecutionResult', 'agentId', 'groupId'],
630
- toolResponse: [
631
- 'type',
632
- 'toolResponse',
633
- 'thought',
634
- 'thoughtSignature',
635
- 'agentId',
636
- 'groupId',
637
- ],
638
- toolResult: ['type', 'toolResult', 'agentId', 'groupId'],
639
- };
581
+ const TOOL_RESULT_ENVELOPE_FIELDS: Readonly<
582
+ Partial<Record<string, readonly string[]>>
583
+ > = {
584
+ mcp_tool_result: ANTHROPIC_GENERIC_RESULT_FIELDS,
585
+ tool_result: ANTHROPIC_GENERIC_RESULT_FIELDS,
586
+ server_tool_call_result: [
587
+ 'type',
588
+ 'toolCallId',
589
+ 'status',
590
+ 'output',
591
+ ...LOCAL_RESULT_METADATA_FIELDS,
592
+ ],
593
+ server_tool_result: [
594
+ 'type',
595
+ 'tool_call_id',
596
+ 'status',
597
+ 'output',
598
+ ...LOCAL_RESULT_METADATA_FIELDS,
599
+ ],
600
+ codeExecutionResult: ['type', 'codeExecutionResult', 'agentId', 'groupId'],
601
+ toolResponse: [
602
+ 'type',
603
+ 'toolResponse',
604
+ 'thought',
605
+ 'thoughtSignature',
606
+ 'agentId',
607
+ 'groupId',
608
+ ],
609
+ toolResult: ['type', 'toolResult', 'agentId', 'groupId'],
610
+ };
640
611
 
641
612
  function anthropicDescriptor(
642
613
  record: Record<string, unknown>,
@@ -663,10 +634,18 @@ export function getProviderToolResultPartDescriptor(
663
634
  return undefined;
664
635
  }
665
636
  const type = readString(record, 'type');
666
- const allowedFields =
667
- type == null ? undefined : TOOL_RESULT_ENVELOPE_FIELDS[type];
637
+ if (type == null) {
638
+ return undefined;
639
+ }
640
+ const isAnthropicResult = isAnthropicServerToolResultType(type);
641
+ let allowedFields = TOOL_RESULT_ENVELOPE_FIELDS[type];
642
+ if (isAnthropicResult) {
643
+ allowedFields =
644
+ type === 'web_search_tool_result' || type === 'web_fetch_tool_result'
645
+ ? ANTHROPIC_SERVER_RESULT_FIELDS
646
+ : ANTHROPIC_RESULT_FIELDS;
647
+ }
668
648
  if (
669
- type == null ||
670
649
  allowedFields == null ||
671
650
  !hasOnlyOwnDataProperties(record, allowedFields) ||
672
651
  !hasValidProviderMetadata(record) ||
@@ -675,9 +654,12 @@ export function getProviderToolResultPartDescriptor(
675
654
  ) {
676
655
  return undefined;
677
656
  }
678
- const structuredToolNames = ANTHROPIC_STRUCTURED_RESULT_TOOL_NAMES[type];
679
- if (structuredToolNames != null) {
680
- return anthropicDescriptor(record, type, structuredToolNames);
657
+ if (isAnthropicResult && type !== 'web_search_tool_result') {
658
+ return anthropicDescriptor(
659
+ record,
660
+ type,
661
+ ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE[type]
662
+ );
681
663
  }
682
664
  if (type === 'web_search_tool_result') {
683
665
  const toolCallId = readPairingString(record, 'tool_use_id');
@@ -689,7 +671,7 @@ export function getProviderToolResultPartDescriptor(
689
671
  type,
690
672
  toolCallId,
691
673
  compatibleCallKinds: ANTHROPIC_SERVER_CALL_KINDS,
692
- expectedToolNames: WEB_SEARCH_TOOL_NAMES,
674
+ expectedToolNames: ANTHROPIC_SERVER_TOOL_NAMES_BY_RESULT_TYPE[type],
693
675
  }
694
676
  : undefined;
695
677
  }
@@ -776,7 +758,8 @@ export function isProviderToolContentPart(part: unknown): boolean {
776
758
  return (
777
759
  type != null &&
778
760
  (PROVIDER_TOOL_CALL_CONTENT_TYPES.has(type) ||
779
- Object.prototype.hasOwnProperty.call(TOOL_RESULT_ENVELOPE_FIELDS, type))
761
+ isAnthropicServerToolResultType(type) ||
762
+ Object.hasOwn(TOOL_RESULT_ENVELOPE_FIELDS, type))
780
763
  );
781
764
  }
782
765
 
@@ -847,6 +830,10 @@ const STANDARD_CALL_FIELDS = [
847
830
  'index',
848
831
  ...LOCAL_CALL_METADATA_FIELDS,
849
832
  ] as const;
833
+ const STANDARD_CALL_WITH_CALLER_FIELDS = [
834
+ ...STANDARD_CALL_FIELDS,
835
+ 'caller',
836
+ ] as const;
850
837
  const LIBRECHAT_CALL_FIELDS = [
851
838
  'type',
852
839
  'id',
@@ -927,7 +914,8 @@ export function getProviderToolCallPartDescriptor(
927
914
  ['type', 'tool_call', 'agentId', 'groupId'],
928
915
  ['tool_call']
929
916
  )
930
- : hasExactCallShape(record, STANDARD_CALL_FIELDS, ['id', 'name', 'args']);
917
+ : hasExactCallShape(record, STANDARD_CALL_WITH_CALLER_FIELDS, ['id', 'name', 'args']) &&
918
+ hasValidCallerProperty(record);
931
919
  const callIsValid = nested.found
932
920
  ? call != null &&
933
921
  hasExactCallShape(call, LIBRECHAT_CALL_FIELDS, [
@@ -993,23 +981,27 @@ export function getProviderToolCallPartDescriptor(
993
981
  };
994
982
  }
995
983
  if (type === 'server_tool_call') {
984
+ const callId = readPairingString(record, 'id');
985
+ const name = optionalName(record);
986
+ if (callId == null || name == null) {
987
+ return undefined;
988
+ }
989
+ const isAnthropic = isAnthropicServerToolCall(callId, name.name);
990
+ const fields = isAnthropic
991
+ ? STANDARD_CALL_WITH_CALLER_FIELDS
992
+ : STANDARD_CALL_FIELDS;
996
993
  if (
997
- !hasExactCallShape(record, STANDARD_CALL_FIELDS, ['id', 'name', 'args'])
994
+ !hasExactCallShape(record, fields, ['id', 'name', 'args']) ||
995
+ !hasValidCallerProperty(record)
998
996
  ) {
999
997
  return undefined;
1000
998
  }
1001
- const callId = readPairingString(record, 'id');
1002
- const name = optionalName(record);
1003
- return callId == null || name == null
1004
- ? undefined
1005
- : {
1006
- callId,
1007
- kind: isAnthropicServerToolCall(callId, name.name)
1008
- ? 'anthropic-server'
1009
- : 'server',
1010
- sourceType: type,
1011
- ...name,
1012
- };
999
+ return {
1000
+ callId,
1001
+ kind: isAnthropic ? 'anthropic-server' : 'server',
1002
+ sourceType: type,
1003
+ ...name,
1004
+ };
1013
1005
  }
1014
1006
  if (type === 'toolCall' || type === 'toolUse') {
1015
1007
  const outerFields =
@@ -1,5 +1,8 @@
1
1
  import { performance } from 'node:perf_hooks';
2
- import type { ProviderTextAdmission, ProviderTextAdmissionLease } from './providerTextAdmission';
2
+ import type {
3
+ ProviderTextAdmission,
4
+ ProviderTextAdmissionLease,
5
+ } from './providerTextAdmission';
3
6
  import { PreparedSubagentError } from '@/tools/preparedSubagents';
4
7
  import { StreamLimitExceededError } from '@/llm/streamLimits';
5
8
 
@@ -72,14 +75,28 @@ export function createProviderTextRestartCancellation(): ProviderTextProtectionE
72
75
  }
73
76
 
74
77
  export function isProviderTextRestartCancellation(error: unknown): boolean {
75
- return error instanceof ProviderTextProtectionError && restartCancellations.has(error);
78
+ return (
79
+ error instanceof ProviderTextProtectionError &&
80
+ restartCancellations.has(error)
81
+ );
76
82
  }
77
83
 
78
- function protectionAbortError(parent?: AbortSignal): ProviderTextProtectionError | StreamLimitExceededError | PreparedSubagentError {
84
+ function protectionAbortError(
85
+ parent?: AbortSignal
86
+ ):
87
+ | ProviderTextProtectionError
88
+ | StreamLimitExceededError
89
+ | PreparedSubagentError {
79
90
  const reason: unknown = parent?.reason;
80
- if (reason instanceof StreamLimitExceededError || reason instanceof PreparedSubagentError) return reason;
91
+ if (
92
+ reason instanceof StreamLimitExceededError ||
93
+ reason instanceof PreparedSubagentError
94
+ )
95
+ return reason;
81
96
  if (reason instanceof ProviderTextProtectionError) {
82
- return isProviderTextRestartCancellation(reason) ? reason : new ProviderTextProtectionError(reason.code);
97
+ return isProviderTextRestartCancellation(reason)
98
+ ? reason
99
+ : new ProviderTextProtectionError(reason.code);
83
100
  }
84
101
  return new ProviderTextProtectionError('cancelled');
85
102
  }
@@ -102,19 +119,34 @@ const errorCodes = new Set<ProviderTextProtectionErrorCode>([
102
119
  'incompatible',
103
120
  ]);
104
121
 
105
- export type ProviderTextProtectionFailure = Extract<ProviderTextProtectionResult, { ok: false }>;
106
-
107
- function isProtectionErrorCode(code: unknown): code is ProviderTextProtectionErrorCode {
108
- return typeof code === 'string' && (errorCodes as ReadonlySet<string>).has(code);
122
+ export type ProviderTextProtectionFailure = Extract<
123
+ ProviderTextProtectionResult,
124
+ { ok: false }
125
+ >;
126
+
127
+ function isProtectionErrorCode(
128
+ code: unknown
129
+ ): code is ProviderTextProtectionErrorCode {
130
+ return (
131
+ typeof code === 'string' && (errorCodes as ReadonlySet<string>).has(code)
132
+ );
109
133
  }
110
134
 
111
135
  /** Safe wire failure, including errors raised by a separate CJS/ESM copy of the SDK. */
112
- export function serializeProviderTextProtectionError(error: unknown): ProviderTextProtectionFailure {
136
+ export function serializeProviderTextProtectionError(
137
+ error: unknown
138
+ ): ProviderTextProtectionFailure {
113
139
  let code: ProviderTextProtectionErrorCode = 'unavailable';
114
140
  try {
115
- if (error != null && typeof error === 'object' &&
116
- 'name' in error && error.name === 'ProviderTextProtectionError' &&
117
- 'code' in error && isProtectionErrorCode(error.code)) code = error.code;
141
+ if (
142
+ error != null &&
143
+ typeof error === 'object' &&
144
+ 'name' in error &&
145
+ error.name === 'ProviderTextProtectionError' &&
146
+ 'code' in error &&
147
+ isProtectionErrorCode(error.code)
148
+ )
149
+ code = error.code;
118
150
  } catch {
119
151
  // Never serialize arbitrary exception messages, stacks, causes, or getters.
120
152
  }
@@ -122,13 +154,23 @@ export function serializeProviderTextProtectionError(error: unknown): ProviderTe
122
154
  }
123
155
 
124
156
  /** Decode only an authenticated host's failure envelope; unknown versions/codes fail closed. */
125
- export function deserializeProviderTextProtectionError(failure: unknown): ProviderTextProtectionError {
157
+ export function deserializeProviderTextProtectionError(
158
+ failure: unknown
159
+ ): ProviderTextProtectionError {
126
160
  try {
127
- if (failure != null && typeof failure === 'object' &&
128
- 'version' in failure && failure.version === 1 &&
129
- 'ok' in failure && failure.ok === false &&
130
- 'error' in failure && failure.error != null && typeof failure.error === 'object' &&
131
- 'code' in failure.error && isProtectionErrorCode(failure.error.code)) {
161
+ if (
162
+ failure != null &&
163
+ typeof failure === 'object' &&
164
+ 'version' in failure &&
165
+ failure.version === 1 &&
166
+ 'ok' in failure &&
167
+ failure.ok === false &&
168
+ 'error' in failure &&
169
+ failure.error != null &&
170
+ typeof failure.error === 'object' &&
171
+ 'code' in failure.error &&
172
+ isProtectionErrorCode(failure.error.code)
173
+ ) {
132
174
  return new ProviderTextProtectionError(failure.error.code);
133
175
  }
134
176
  } catch {
@@ -138,7 +180,11 @@ export function deserializeProviderTextProtectionError(failure: unknown): Provid
138
180
  }
139
181
 
140
182
  export function validateProviderTextTimeout(timeoutMs: number): void {
141
- if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2_147_483_647)
183
+ if (
184
+ !Number.isSafeInteger(timeoutMs) ||
185
+ timeoutMs <= 0 ||
186
+ timeoutMs > 2_147_483_647
187
+ )
142
188
  throw new ProviderTextProtectionError('incompatible');
143
189
  }
144
190
 
@@ -146,8 +192,7 @@ export function validateProviderTextProtection(
146
192
  policy: ProviderTextProtection
147
193
  ): void {
148
194
  const version: number = policy.version;
149
- if (version !== 1)
150
- throw new ProviderTextProtectionError('incompatible');
195
+ if (version !== 1) throw new ProviderTextProtectionError('incompatible');
151
196
  if (
152
197
  typeof policy.inspect !== 'function' ||
153
198
  typeof policy.classify !== 'function'
@@ -209,9 +254,12 @@ export class ProviderTextAttempt {
209
254
  this.charge(512);
210
255
  parent?.addEventListener('abort', this.abort, { once: true });
211
256
  this.check();
212
- this.timer = setTimeout(() => {
213
- this.controller.abort(new ProviderTextProtectionError('timeout'));
214
- }, Math.min(timeoutMs, policy.timeoutMs));
257
+ this.timer = setTimeout(
258
+ () => {
259
+ this.controller.abort(new ProviderTextProtectionError('timeout'));
260
+ },
261
+ Math.min(timeoutMs, policy.timeoutMs)
262
+ );
215
263
  this.timer.unref();
216
264
  protectedSignals.add(this.signal);
217
265
  } catch (error) {
@@ -223,7 +271,12 @@ export class ProviderTextAttempt {
223
271
  } catch {
224
272
  // Malformed transport signals must not prevent admission rollback.
225
273
  }
226
- if (error instanceof ProviderTextProtectionError || error instanceof StreamLimitExceededError || error instanceof PreparedSubagentError) throw error;
274
+ if (
275
+ error instanceof ProviderTextProtectionError ||
276
+ error instanceof StreamLimitExceededError ||
277
+ error instanceof PreparedSubagentError
278
+ )
279
+ throw error;
227
280
  throw new ProviderTextProtectionError('incompatible');
228
281
  }
229
282
  }
@@ -235,8 +288,7 @@ export class ProviderTextAttempt {
235
288
  check(): void {
236
289
  if (this.finished) throw new ProviderTextProtectionError('incompatible');
237
290
  if (this.signal.aborted) throw this.signal.reason;
238
- if (this.parent?.aborted === true)
239
- throw protectionAbortError(this.parent);
291
+ if (this.parent?.aborted === true) throw protectionAbortError(this.parent);
240
292
  if (performance.now() >= this.deadline) {
241
293
  this.controller.abort(new ProviderTextProtectionError('timeout'));
242
294
  throw this.signal.reason;
@@ -263,7 +315,8 @@ export class ProviderTextAttempt {
263
315
 
264
316
  append(text: string, cumulativeReplay = false): void {
265
317
  this.check();
266
- if (cumulativeReplay && this.hasCandidatePrefix(text)) text = text.slice(this.textLength);
318
+ if (cumulativeReplay && this.hasCandidatePrefix(text))
319
+ text = text.slice(this.textLength);
267
320
  if (!text) return;
268
321
  const bytes = text.length * 2;
269
322
  this.charge(bytes * 2 + 64);
@@ -283,7 +336,10 @@ export class ProviderTextAttempt {
283
336
  }
284
337
 
285
338
  matchesText(text: string): boolean {
286
- return text.length === this.textLength && (text.length === 0 || this.hasCandidatePrefix(text));
339
+ return (
340
+ text.length === this.textLength &&
341
+ (text.length === 0 || this.hasCandidatePrefix(text))
342
+ );
287
343
  }
288
344
 
289
345
  retain(bytes: number): void {
@@ -310,8 +366,14 @@ export class ProviderTextAttempt {
310
366
  this.pending.delete(work);
311
367
  };
312
368
  void work.then(
313
- (value) => { settle(); resolve(value); },
314
- (error: Error) => { settle(); reject(error); }
369
+ (value) => {
370
+ settle();
371
+ resolve(value);
372
+ },
373
+ (error: Error) => {
374
+ settle();
375
+ reject(error);
376
+ }
315
377
  );
316
378
  });
317
379
  }
@@ -340,7 +402,9 @@ export class ProviderTextAttempt {
340
402
  } catch (error) {
341
403
  this.check();
342
404
  if (isProviderTextRestartCancellation(error)) throw error;
343
- throw deserializeProviderTextProtectionError(serializeProviderTextProtectionError(error));
405
+ throw deserializeProviderTextProtectionError(
406
+ serializeProviderTextProtectionError(error)
407
+ );
344
408
  }
345
409
  this.check();
346
410
  const canonical = validateResult(result);
@@ -375,10 +439,21 @@ export class ProviderTextAttempt {
375
439
  interface UncheckedResult {
376
440
  version?: number;
377
441
  ok?: boolean;
378
- value?: { content?: string; replacements?: number; categories?: readonly { category?: string; count?: number }[] };
442
+ value?: {
443
+ content?: string;
444
+ replacements?: number;
445
+ categories?: readonly { category?: string; count?: number }[];
446
+ };
379
447
  error?: { code?: ProviderTextProtectionErrorCode };
380
448
  }
381
449
 
450
+ /** Returns the canonical text of a host inspection result, or throws its failure code. */
451
+ export function readProviderTextResult(
452
+ result: ProviderTextProtectionResult
453
+ ): string {
454
+ return validateResult(result);
455
+ }
456
+
382
457
  function validateResult(result: UncheckedResult | undefined): string {
383
458
  try {
384
459
  if (result?.version !== 1 || typeof result.ok !== 'boolean') {
@@ -386,14 +461,26 @@ function validateResult(result: UncheckedResult | undefined): string {
386
461
  }
387
462
  if (!result.ok) {
388
463
  const code = result.error?.code;
389
- throw new ProviderTextProtectionError(code != null && errorCodes.has(code) ? code : 'incompatible');
464
+ throw new ProviderTextProtectionError(
465
+ code != null && errorCodes.has(code) ? code : 'incompatible'
466
+ );
390
467
  }
391
468
  const value = result.value;
392
- if (typeof value?.content !== 'string' || typeof value.replacements !== 'number' ||
393
- !Number.isSafeInteger(value.replacements) || value.replacements < 0 ||
394
- !Array.isArray(value.categories) || value.categories.length > 32 ||
395
- value.categories.some((entry) => typeof entry.category !== 'string' ||
396
- typeof entry.count !== 'number' || !Number.isSafeInteger(entry.count) || entry.count <= 0)) {
469
+ if (
470
+ typeof value?.content !== 'string' ||
471
+ typeof value.replacements !== 'number' ||
472
+ !Number.isSafeInteger(value.replacements) ||
473
+ value.replacements < 0 ||
474
+ !Array.isArray(value.categories) ||
475
+ value.categories.length > 32 ||
476
+ value.categories.some(
477
+ (entry) =>
478
+ typeof entry.category !== 'string' ||
479
+ typeof entry.count !== 'number' ||
480
+ !Number.isSafeInteger(entry.count) ||
481
+ entry.count <= 0
482
+ )
483
+ ) {
397
484
  throw new ProviderTextProtectionError('incompatible');
398
485
  }
399
486
  return value.content;