@librechat/agents 3.1.77-dev.1 → 3.1.78-dev.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 (188) hide show
  1. package/dist/cjs/common/enum.cjs +54 -0
  2. package/dist/cjs/common/enum.cjs.map +1 -1
  3. package/dist/cjs/graphs/Graph.cjs +148 -4
  4. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  5. package/dist/cjs/hooks/createWorkspacePolicyHook.cjs +291 -0
  6. package/dist/cjs/hooks/createWorkspacePolicyHook.cjs.map +1 -0
  7. package/dist/cjs/llm/openai/index.cjs +317 -1
  8. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  9. package/dist/cjs/main.cjs +90 -0
  10. package/dist/cjs/main.cjs.map +1 -1
  11. package/dist/cjs/messages/anthropicToolCache.cjs +102 -0
  12. package/dist/cjs/messages/anthropicToolCache.cjs.map +1 -0
  13. package/dist/cjs/messages/prune.cjs +27 -0
  14. package/dist/cjs/messages/prune.cjs.map +1 -1
  15. package/dist/cjs/messages/recency.cjs +99 -0
  16. package/dist/cjs/messages/recency.cjs.map +1 -0
  17. package/dist/cjs/run.cjs +30 -0
  18. package/dist/cjs/run.cjs.map +1 -1
  19. package/dist/cjs/summarization/node.cjs +100 -6
  20. package/dist/cjs/summarization/node.cjs.map +1 -1
  21. package/dist/cjs/tools/ToolNode.cjs +635 -23
  22. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  23. package/dist/cjs/tools/local/CompileCheckTool.cjs +227 -0
  24. package/dist/cjs/tools/local/CompileCheckTool.cjs.map +1 -0
  25. package/dist/cjs/tools/local/FileCheckpointer.cjs +90 -0
  26. package/dist/cjs/tools/local/FileCheckpointer.cjs.map +1 -0
  27. package/dist/cjs/tools/local/LocalCodingTools.cjs +1098 -0
  28. package/dist/cjs/tools/local/LocalCodingTools.cjs.map +1 -0
  29. package/dist/cjs/tools/local/LocalExecutionEngine.cjs +1042 -0
  30. package/dist/cjs/tools/local/LocalExecutionEngine.cjs.map +1 -0
  31. package/dist/cjs/tools/local/LocalExecutionTools.cjs +122 -0
  32. package/dist/cjs/tools/local/LocalExecutionTools.cjs.map +1 -0
  33. package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs +453 -0
  34. package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs.map +1 -0
  35. package/dist/cjs/tools/local/attachments.cjs +183 -0
  36. package/dist/cjs/tools/local/attachments.cjs.map +1 -0
  37. package/dist/cjs/tools/local/bashAst.cjs +129 -0
  38. package/dist/cjs/tools/local/bashAst.cjs.map +1 -0
  39. package/dist/cjs/tools/local/editStrategies.cjs +188 -0
  40. package/dist/cjs/tools/local/editStrategies.cjs.map +1 -0
  41. package/dist/cjs/tools/local/resolveLocalExecutionTools.cjs +141 -0
  42. package/dist/cjs/tools/local/resolveLocalExecutionTools.cjs.map +1 -0
  43. package/dist/cjs/tools/local/syntaxCheck.cjs +182 -0
  44. package/dist/cjs/tools/local/syntaxCheck.cjs.map +1 -0
  45. package/dist/cjs/tools/local/textEncoding.cjs +30 -0
  46. package/dist/cjs/tools/local/textEncoding.cjs.map +1 -0
  47. package/dist/cjs/tools/local/workspaceFS.cjs +51 -0
  48. package/dist/cjs/tools/local/workspaceFS.cjs.map +1 -0
  49. package/dist/cjs/tools/subagent/SubagentExecutor.cjs +1 -0
  50. package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
  51. package/dist/esm/common/enum.mjs +53 -1
  52. package/dist/esm/common/enum.mjs.map +1 -1
  53. package/dist/esm/graphs/Graph.mjs +149 -5
  54. package/dist/esm/graphs/Graph.mjs.map +1 -1
  55. package/dist/esm/hooks/createWorkspacePolicyHook.mjs +289 -0
  56. package/dist/esm/hooks/createWorkspacePolicyHook.mjs.map +1 -0
  57. package/dist/esm/llm/openai/index.mjs +318 -2
  58. package/dist/esm/llm/openai/index.mjs.map +1 -1
  59. package/dist/esm/main.mjs +17 -2
  60. package/dist/esm/main.mjs.map +1 -1
  61. package/dist/esm/messages/anthropicToolCache.mjs +99 -0
  62. package/dist/esm/messages/anthropicToolCache.mjs.map +1 -0
  63. package/dist/esm/messages/prune.mjs +26 -1
  64. package/dist/esm/messages/prune.mjs.map +1 -1
  65. package/dist/esm/messages/recency.mjs +97 -0
  66. package/dist/esm/messages/recency.mjs.map +1 -0
  67. package/dist/esm/run.mjs +30 -0
  68. package/dist/esm/run.mjs.map +1 -1
  69. package/dist/esm/summarization/node.mjs +100 -6
  70. package/dist/esm/summarization/node.mjs.map +1 -1
  71. package/dist/esm/tools/ToolNode.mjs +635 -23
  72. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  73. package/dist/esm/tools/local/CompileCheckTool.mjs +223 -0
  74. package/dist/esm/tools/local/CompileCheckTool.mjs.map +1 -0
  75. package/dist/esm/tools/local/FileCheckpointer.mjs +87 -0
  76. package/dist/esm/tools/local/FileCheckpointer.mjs.map +1 -0
  77. package/dist/esm/tools/local/LocalCodingTools.mjs +1075 -0
  78. package/dist/esm/tools/local/LocalCodingTools.mjs.map +1 -0
  79. package/dist/esm/tools/local/LocalExecutionEngine.mjs +1022 -0
  80. package/dist/esm/tools/local/LocalExecutionEngine.mjs.map +1 -0
  81. package/dist/esm/tools/local/LocalExecutionTools.mjs +117 -0
  82. package/dist/esm/tools/local/LocalExecutionTools.mjs.map +1 -0
  83. package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs +448 -0
  84. package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs.map +1 -0
  85. package/dist/esm/tools/local/attachments.mjs +180 -0
  86. package/dist/esm/tools/local/attachments.mjs.map +1 -0
  87. package/dist/esm/tools/local/bashAst.mjs +126 -0
  88. package/dist/esm/tools/local/bashAst.mjs.map +1 -0
  89. package/dist/esm/tools/local/editStrategies.mjs +185 -0
  90. package/dist/esm/tools/local/editStrategies.mjs.map +1 -0
  91. package/dist/esm/tools/local/resolveLocalExecutionTools.mjs +137 -0
  92. package/dist/esm/tools/local/resolveLocalExecutionTools.mjs.map +1 -0
  93. package/dist/esm/tools/local/syntaxCheck.mjs +179 -0
  94. package/dist/esm/tools/local/syntaxCheck.mjs.map +1 -0
  95. package/dist/esm/tools/local/textEncoding.mjs +27 -0
  96. package/dist/esm/tools/local/textEncoding.mjs.map +1 -0
  97. package/dist/esm/tools/local/workspaceFS.mjs +49 -0
  98. package/dist/esm/tools/local/workspaceFS.mjs.map +1 -0
  99. package/dist/esm/tools/subagent/SubagentExecutor.mjs +1 -0
  100. package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
  101. package/dist/types/common/enum.d.ts +39 -1
  102. package/dist/types/graphs/Graph.d.ts +34 -0
  103. package/dist/types/hooks/createWorkspacePolicyHook.d.ts +95 -0
  104. package/dist/types/hooks/index.d.ts +2 -0
  105. package/dist/types/index.d.ts +1 -0
  106. package/dist/types/llm/openai/index.d.ts +17 -0
  107. package/dist/types/messages/anthropicToolCache.d.ts +51 -0
  108. package/dist/types/messages/index.d.ts +2 -0
  109. package/dist/types/messages/prune.d.ts +11 -0
  110. package/dist/types/messages/recency.d.ts +64 -0
  111. package/dist/types/run.d.ts +21 -0
  112. package/dist/types/tools/ToolNode.d.ts +145 -2
  113. package/dist/types/tools/local/CompileCheckTool.d.ts +31 -0
  114. package/dist/types/tools/local/FileCheckpointer.d.ts +39 -0
  115. package/dist/types/tools/local/LocalCodingTools.d.ts +57 -0
  116. package/dist/types/tools/local/LocalExecutionEngine.d.ts +149 -0
  117. package/dist/types/tools/local/LocalExecutionTools.d.ts +9 -0
  118. package/dist/types/tools/local/LocalProgrammaticToolCalling.d.ts +21 -0
  119. package/dist/types/tools/local/attachments.d.ts +84 -0
  120. package/dist/types/tools/local/bashAst.d.ts +11 -0
  121. package/dist/types/tools/local/editStrategies.d.ts +28 -0
  122. package/dist/types/tools/local/index.d.ts +12 -0
  123. package/dist/types/tools/local/resolveLocalExecutionTools.d.ts +38 -0
  124. package/dist/types/tools/local/syntaxCheck.d.ts +42 -0
  125. package/dist/types/tools/local/textEncoding.d.ts +21 -0
  126. package/dist/types/tools/local/workspaceFS.d.ts +49 -0
  127. package/dist/types/types/hitl.d.ts +56 -27
  128. package/dist/types/types/run.d.ts +8 -1
  129. package/dist/types/types/summarize.d.ts +30 -0
  130. package/dist/types/types/tools.d.ts +341 -6
  131. package/package.json +21 -2
  132. package/src/common/enum.ts +54 -0
  133. package/src/graphs/Graph.ts +164 -6
  134. package/src/hooks/__tests__/compactHooks.test.ts +38 -2
  135. package/src/hooks/__tests__/createWorkspacePolicyHook.test.ts +393 -0
  136. package/src/hooks/createWorkspacePolicyHook.ts +355 -0
  137. package/src/hooks/index.ts +6 -0
  138. package/src/index.ts +1 -0
  139. package/src/llm/openai/deepseek.test.ts +479 -0
  140. package/src/llm/openai/index.ts +484 -1
  141. package/src/messages/__tests__/anthropicToolCache.test.ts +125 -0
  142. package/src/messages/__tests__/recency.test.ts +267 -0
  143. package/src/messages/anthropicToolCache.ts +116 -0
  144. package/src/messages/index.ts +2 -0
  145. package/src/messages/prune.ts +27 -1
  146. package/src/messages/recency.ts +155 -0
  147. package/src/run.ts +31 -0
  148. package/src/scripts/compare_pi_vs_ours.ts +840 -0
  149. package/src/scripts/local_engine.ts +166 -0
  150. package/src/scripts/local_engine_checkpointer.ts +205 -0
  151. package/src/scripts/local_engine_compile.ts +263 -0
  152. package/src/scripts/local_engine_hooks.ts +226 -0
  153. package/src/scripts/local_engine_image.ts +201 -0
  154. package/src/scripts/local_engine_ptc.ts +151 -0
  155. package/src/scripts/local_engine_workspace.ts +258 -0
  156. package/src/scripts/summarization-recency.ts +462 -0
  157. package/src/specs/prune.test.ts +39 -0
  158. package/src/summarization/__tests__/node.test.ts +499 -3
  159. package/src/summarization/node.ts +124 -7
  160. package/src/tools/ToolNode.ts +769 -20
  161. package/src/tools/__tests__/LocalExecutionTools.test.ts +2647 -0
  162. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +175 -0
  163. package/src/tools/__tests__/ToolNode.outputReferences.test.ts +114 -0
  164. package/src/tools/__tests__/ToolNode.session.test.ts +84 -0
  165. package/src/tools/__tests__/directToolHITLResumeScope.test.ts +467 -0
  166. package/src/tools/__tests__/directToolHooks.test.ts +411 -0
  167. package/src/tools/__tests__/localToolNames.test.ts +73 -0
  168. package/src/tools/__tests__/workspaceSeam.test.ts +134 -0
  169. package/src/tools/local/CompileCheckTool.ts +278 -0
  170. package/src/tools/local/FileCheckpointer.ts +93 -0
  171. package/src/tools/local/LocalCodingTools.ts +1342 -0
  172. package/src/tools/local/LocalExecutionEngine.ts +1329 -0
  173. package/src/tools/local/LocalExecutionTools.ts +167 -0
  174. package/src/tools/local/LocalProgrammaticToolCalling.ts +594 -0
  175. package/src/tools/local/__tests__/FileCheckpointer.test.ts +120 -0
  176. package/src/tools/local/__tests__/editStrategies.test.ts +134 -0
  177. package/src/tools/local/attachments.ts +251 -0
  178. package/src/tools/local/bashAst.ts +151 -0
  179. package/src/tools/local/editStrategies.ts +188 -0
  180. package/src/tools/local/index.ts +12 -0
  181. package/src/tools/local/resolveLocalExecutionTools.ts +208 -0
  182. package/src/tools/local/syntaxCheck.ts +243 -0
  183. package/src/tools/local/textEncoding.ts +37 -0
  184. package/src/tools/local/workspaceFS.ts +89 -0
  185. package/src/types/hitl.ts +56 -27
  186. package/src/types/run.ts +12 -1
  187. package/src/types/summarize.ts +31 -0
  188. package/src/types/tools.ts +359 -7
@@ -0,0 +1,291 @@
1
+ 'use strict';
2
+
3
+ var os = require('os');
4
+ var path = require('path');
5
+ var promises = require('fs/promises');
6
+ var _enum = require('../common/enum.cjs');
7
+
8
+ /**
9
+ * Workspace boundary policy as a `PreToolUse` hook.
10
+ *
11
+ * Local-engine file tools enforce a hard workspace boundary at the
12
+ * tool implementation layer (`resolveWorkspacePathSafe`). This hook
13
+ * adds a complementary, host-controlled layer on top that uses the
14
+ * standard PreToolUse / HITL machinery to *negotiate* access to
15
+ * paths outside the workspace — instead of just throwing.
16
+ *
17
+ * The host opts in by registering this hook on a `HookRegistry`; the
18
+ * hook inspects each tool call's input, extracts the file paths it
19
+ * mentions via per-tool extractors, and returns:
20
+ *
21
+ * - `allow` — every path is inside `workspace.root`
22
+ * (or `additionalRoots`)
23
+ * - `deny` — at least one path is outside, and the
24
+ * configured outside-policy is `'deny'`
25
+ * - `ask` — at least one path is outside, and the
26
+ * outside-policy is `'ask'` (default).
27
+ * When `humanInTheLoop.enabled` is true,
28
+ * the existing PreToolUse `'ask'` flow
29
+ * raises a tool_approval interrupt the
30
+ * host UI can render. When HITL is off,
31
+ * `'ask'` collapses to `deny` (matches
32
+ * the rest of the SDK's default).
33
+ *
34
+ * Default per-tool path extractors cover the local-engine coding
35
+ * suite (`read_file`, `write_file`, `edit_file`, `grep_search`,
36
+ * `glob_search`, `list_directory`, `compile_check`). The host can
37
+ * override or extend via `pathExtractors`. Bash/code paths are not
38
+ * extracted by default — bash command parsing is its own concern, and
39
+ * the existing `bashAst` validator + sandbox-runtime fs allowlist are
40
+ * the right gates for those.
41
+ *
42
+ * Important: this hook does NOT replace `resolveWorkspacePathSafe`.
43
+ * Even if the hook returns `allow`, the file tool still enforces its
44
+ * own clamp unless `workspace.allowReadOutside` /
45
+ * `workspace.allowWriteOutside` (or the legacy
46
+ * `allowOutsideWorkspace`) is set. The recommended composition for
47
+ * "ask the user" semantics is:
48
+ *
49
+ * workspace: {
50
+ * root,
51
+ * allowReadOutside: true,
52
+ * allowWriteOutside: true,
53
+ * },
54
+ * // …with the hook installed and humanInTheLoop.enabled = true.
55
+ */
56
+ const READ_TOOLS = new Set([
57
+ _enum.Constants.READ_FILE,
58
+ _enum.Constants.GREP_SEARCH,
59
+ _enum.Constants.GLOB_SEARCH,
60
+ _enum.Constants.LIST_DIRECTORY,
61
+ _enum.Constants.COMPILE_CHECK,
62
+ ]);
63
+ const WRITE_TOOLS = new Set([
64
+ _enum.Constants.WRITE_FILE,
65
+ _enum.Constants.EDIT_FILE,
66
+ ]);
67
+ /**
68
+ * Best-effort extractor for `compile_check` — pulls absolute and `~/`
69
+ * path tokens out of the `command` string so the workspace boundary
70
+ * sees them. Without this, a model could ship `command: 'cat
71
+ * /etc/passwd'` and the policy hook would short-circuit to `allow`
72
+ * (Codex P1 #26 — the prior `() => []` made the hook a no-op for
73
+ * compile_check). Conservative by design:
74
+ *
75
+ * - Matches `/foo`, `~/foo`, `$HOME/foo`, `${HOME}/foo` followed by
76
+ * non-shell-special chars. Stops at whitespace, quotes, redirect
77
+ * operators, pipes, semicolons.
78
+ * - Strips a leading `--flag=` so `--out=/etc/foo` extracts as
79
+ * `/etc/foo` (the path the agent's actually trying to write).
80
+ * - Misses relative paths (intended — those resolve under cwd
81
+ * anyway), and shell-substituted paths whose final form isn't
82
+ * visible at extract time. Hosts that need bulletproof gating
83
+ * should pair this with a `bash_tool`-level policy.
84
+ */
85
+ // `["']?` slots before AND after the captured path cover quoted
86
+ // forms like `cat "/etc/passwd"` and `--out='/tmp/x'`. Codex P1 #31
87
+ // — the previous regex only matched unquoted tokens, so a model
88
+ // could trivially bypass the workspace policy by quoting any
89
+ // destination path. The path content character class still excludes
90
+ // quotes/whitespace/shell-specials so we don't over-extract; that's
91
+ // the defensive trade we want for fallback-grep style matching.
92
+ //
93
+ // The `\.\.(?:\/[^…]*)?` alternation covers parent-traversal forms
94
+ // (`..`, `../secrets.txt`, `../foo/bar`). Without it, a model could
95
+ // exfiltrate parent-directory files via `cat ../secrets` and the
96
+ // hook would short-circuit to `allow` because the extractor saw no
97
+ // "absolute" token. The boundary check at the call site resolves
98
+ // non-absolute extracted tokens against `root`, so `../secrets`
99
+ // becomes `<parent-of-workspace>/secrets` which the boundary then
100
+ // correctly flags as outside. Codex P2 #35.
101
+ const PATH_TOKEN = /(?:^|[\s=])(?:--[^\s=]+=)?["']?(\/[^\s'"|;&<>()`]+|~\/[^\s'"|;&<>()`]+|\$\{?HOME\}?\/[^\s'"|;&<>()`]+|\.\.(?:\/[^\s'"|;&<>()`]*)?)["']?/g;
102
+ // Back-compat alias kept for any downstream import.
103
+ const ABSOLUTE_PATH_TOKEN = PATH_TOKEN;
104
+ function expandHomeRelative(token) {
105
+ // Expand ~/foo and $HOME/foo and ${HOME}/foo to absolute. The
106
+ // workspace boundary check resolves non-absolute paths against the
107
+ // workspace root, which would silently treat `~/secret` as
108
+ // `<workspace>/~/secret` — exactly the bypass the codex flagged.
109
+ const home = os.homedir();
110
+ if (token.startsWith('~/'))
111
+ return `${home}/${token.slice(2)}`;
112
+ if (token.startsWith('${HOME}/'))
113
+ return `${home}/${token.slice(8)}`;
114
+ if (token.startsWith('$HOME/'))
115
+ return `${home}/${token.slice(6)}`;
116
+ return token;
117
+ }
118
+ function extractCompileCheckPaths(input) {
119
+ const command = typeof input.command === 'string' ? input.command : '';
120
+ if (command === '')
121
+ return [];
122
+ const out = [];
123
+ for (const match of command.matchAll(ABSOLUTE_PATH_TOKEN)) {
124
+ out.push(expandHomeRelative(match[1]));
125
+ }
126
+ return out;
127
+ }
128
+ const DEFAULT_EXTRACTORS = {
129
+ [_enum.Constants.READ_FILE]: (i) => typeof i.file_path === 'string' ? [i.file_path] : [],
130
+ [_enum.Constants.WRITE_FILE]: (i) => typeof i.file_path === 'string' ? [i.file_path] : [],
131
+ [_enum.Constants.EDIT_FILE]: (i) => typeof i.file_path === 'string' ? [i.file_path] : [],
132
+ [_enum.Constants.GREP_SEARCH]: (i) => typeof i.path === 'string' && i.path !== '' ? [i.path] : [],
133
+ [_enum.Constants.GLOB_SEARCH]: (i) => typeof i.path === 'string' && i.path !== '' ? [i.path] : [],
134
+ [_enum.Constants.LIST_DIRECTORY]: (i) => typeof i.path === 'string' && i.path !== '' ? [i.path] : [],
135
+ [_enum.Constants.COMPILE_CHECK]: extractCompileCheckPaths,
136
+ };
137
+ function isInsideAnyRoot(absolutePath, roots) {
138
+ for (const root of roots) {
139
+ if (absolutePath === root)
140
+ return true;
141
+ const rel = path.relative(root, absolutePath);
142
+ if (!rel.startsWith('..') && !path.isAbsolute(rel))
143
+ return true;
144
+ }
145
+ return false;
146
+ }
147
+ /**
148
+ * Symlink-aware variant: realpaths the candidate AND the roots before
149
+ * comparing. Without this, a symlink inside the workspace pointing
150
+ * outside (e.g. `workspace/link → /etc/passwd`) compares as
151
+ * "in-workspace" lexically, but actually grants the agent reach
152
+ * outside the boundary. Critical when this hook is the primary gate
153
+ * (i.e. the host opted into `workspace.allowReadOutside: true` /
154
+ * `allowWriteOutside: true` so the file tools' own clamp is off).
155
+ *
156
+ * Handles paths that don't yet exist (e.g. `write_file` to a brand
157
+ * new path) by walking up to the nearest existing ancestor and
158
+ * realpathing that, then re-attaching the unresolved suffix. Mirrors
159
+ * `resolveWorkspacePathSafe`'s approach in LocalExecutionEngine.
160
+ */
161
+ async function realpathOrSelf(absolutePath) {
162
+ try {
163
+ return await promises.realpath(absolutePath);
164
+ }
165
+ catch {
166
+ return absolutePath;
167
+ }
168
+ }
169
+ async function realpathOfPathOrAncestor(absolutePath) {
170
+ let current = absolutePath;
171
+ let suffix = '';
172
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
173
+ while (true) {
174
+ try {
175
+ const real = await promises.realpath(current);
176
+ return suffix === '' ? real : path.resolve(real, suffix);
177
+ }
178
+ catch {
179
+ const parent = path.resolve(current, '..');
180
+ if (parent === current) {
181
+ return absolutePath;
182
+ }
183
+ const base = current.slice(parent.length + 1);
184
+ suffix = suffix === '' ? base : `${base}/${suffix}`;
185
+ current = parent;
186
+ }
187
+ }
188
+ }
189
+ async function isInsideAnyRootRealpath(absolutePath, realRoots) {
190
+ const real = await realpathOfPathOrAncestor(absolutePath);
191
+ return isInsideAnyRoot(real, [...realRoots]);
192
+ }
193
+ function formatReason(template, toolName, outsidePaths) {
194
+ const fallback = `Tool "${toolName}" wants to touch ${outsidePaths.length} path(s) outside the workspace: ${outsidePaths.join(', ')}`;
195
+ if (template == null)
196
+ return fallback;
197
+ return template
198
+ .replace(/\{tool\}/g, toolName)
199
+ .replace(/\{paths\}/g, outsidePaths.join(', '));
200
+ }
201
+ /**
202
+ * Build a `PreToolUse` callback that enforces the workspace policy.
203
+ * Register it on a `HookRegistry`:
204
+ *
205
+ * ```ts
206
+ * registry.register('PreToolUse', {
207
+ * hooks: [createWorkspacePolicyHook({ root, outsideWrite: 'ask' })],
208
+ * });
209
+ * ```
210
+ *
211
+ * The hook is composable with `createToolPolicyHook` — register both;
212
+ * `executeHooks` precedence (`deny > ask > allow`) sorts out which
213
+ * decision wins per call.
214
+ */
215
+ function createWorkspacePolicyHook(config) {
216
+ const root = path.resolve(config.root);
217
+ // Relative `additionalRoots` entries are anchored to `root` so a
218
+ // monorepo config like `additionalRoots: ['../shared']` resolves
219
+ // to a sibling of `root`, not of process.cwd. Matches
220
+ // `getWorkspaceRoots` in LocalExecutionEngine.
221
+ const additionalRoots = (config.additionalRoots ?? []).map((p) => path.isAbsolute(p) ? path.resolve(p) : path.resolve(root, p));
222
+ const allRoots = [root, ...additionalRoots];
223
+ // Pre-realpath the roots once at construction — these are stable
224
+ // per Run. The candidate paths get realpath'd lazily inside the
225
+ // hook callback. Cached so the per-call cost is just one realpath.
226
+ let realRootsPromise;
227
+ const getRealRoots = () => {
228
+ if (realRootsPromise == null) {
229
+ realRootsPromise = Promise.all(allRoots.map(realpathOrSelf));
230
+ }
231
+ return realRootsPromise;
232
+ };
233
+ const readPolicy = config.outsideRead ?? 'ask';
234
+ const writePolicy = config.outsideWrite ?? 'ask';
235
+ const extractors = {
236
+ ...DEFAULT_EXTRACTORS,
237
+ ...(config.pathExtractors ?? {}),
238
+ };
239
+ return async (input) => {
240
+ const extractor = extractors[input.toolName];
241
+ if (extractor == null)
242
+ return { decision: 'allow' };
243
+ const paths = extractor((input.toolInput ?? {}));
244
+ if (paths.length === 0)
245
+ return { decision: 'allow' };
246
+ // Two-stage check:
247
+ // 1. Lexical fast path — anything that's lexically inside the
248
+ // workspace AND doesn't get redirected by realpath stays
249
+ // allow-able without paying the realpath cost on every call.
250
+ // 2. For paths that look outside lexically OR look inside but
251
+ // may have been routed through a symlink, realpath both the
252
+ // candidate and the roots and compare. This catches the
253
+ // `workspace/link → /etc/passwd` escape that lexical-only
254
+ // checks miss.
255
+ const outside = [];
256
+ const realRoots = await getRealRoots();
257
+ for (const p of paths) {
258
+ const abs = path.isAbsolute(p) ? path.resolve(p) : path.resolve(root, p);
259
+ // Realpath is the source of truth — it catches both the
260
+ // symlink-escape case (lexically-inside path that resolves
261
+ // outside) and the alternate-mount case (lexically-outside
262
+ // path that resolves back inside the workspace). The lexical
263
+ // check alone gives the wrong answer for either, so we don't
264
+ // bother computing it.
265
+ const realInside = await isInsideAnyRootRealpath(abs, realRoots);
266
+ if (!realInside) {
267
+ outside.push(p);
268
+ }
269
+ }
270
+ if (outside.length === 0)
271
+ return { decision: 'allow' };
272
+ const policy = WRITE_TOOLS.has(input.toolName)
273
+ ? writePolicy
274
+ : READ_TOOLS.has(input.toolName)
275
+ ? readPolicy
276
+ : writePolicy; // unknown tools — treat as write (stricter)
277
+ if (policy === 'allow')
278
+ return { decision: 'allow' };
279
+ const decision = policy === 'deny' ? 'deny' : 'ask';
280
+ return {
281
+ decision,
282
+ reason: formatReason(config.reason, input.toolName, outside),
283
+ ...(decision === 'ask'
284
+ ? { allowedDecisions: ['approve', 'reject'] }
285
+ : {}),
286
+ };
287
+ };
288
+ }
289
+
290
+ exports.createWorkspacePolicyHook = createWorkspacePolicyHook;
291
+ //# sourceMappingURL=createWorkspacePolicyHook.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createWorkspacePolicyHook.cjs","sources":["../../../src/hooks/createWorkspacePolicyHook.ts"],"sourcesContent":["/**\n * Workspace boundary policy as a `PreToolUse` hook.\n *\n * Local-engine file tools enforce a hard workspace boundary at the\n * tool implementation layer (`resolveWorkspacePathSafe`). This hook\n * adds a complementary, host-controlled layer on top that uses the\n * standard PreToolUse / HITL machinery to *negotiate* access to\n * paths outside the workspace — instead of just throwing.\n *\n * The host opts in by registering this hook on a `HookRegistry`; the\n * hook inspects each tool call's input, extracts the file paths it\n * mentions via per-tool extractors, and returns:\n *\n * - `allow` — every path is inside `workspace.root`\n * (or `additionalRoots`)\n * - `deny` — at least one path is outside, and the\n * configured outside-policy is `'deny'`\n * - `ask` — at least one path is outside, and the\n * outside-policy is `'ask'` (default).\n * When `humanInTheLoop.enabled` is true,\n * the existing PreToolUse `'ask'` flow\n * raises a tool_approval interrupt the\n * host UI can render. When HITL is off,\n * `'ask'` collapses to `deny` (matches\n * the rest of the SDK's default).\n *\n * Default per-tool path extractors cover the local-engine coding\n * suite (`read_file`, `write_file`, `edit_file`, `grep_search`,\n * `glob_search`, `list_directory`, `compile_check`). The host can\n * override or extend via `pathExtractors`. Bash/code paths are not\n * extracted by default — bash command parsing is its own concern, and\n * the existing `bashAst` validator + sandbox-runtime fs allowlist are\n * the right gates for those.\n *\n * Important: this hook does NOT replace `resolveWorkspacePathSafe`.\n * Even if the hook returns `allow`, the file tool still enforces its\n * own clamp unless `workspace.allowReadOutside` /\n * `workspace.allowWriteOutside` (or the legacy\n * `allowOutsideWorkspace`) is set. The recommended composition for\n * \"ask the user\" semantics is:\n *\n * workspace: {\n * root,\n * allowReadOutside: true,\n * allowWriteOutside: true,\n * },\n * // …with the hook installed and humanInTheLoop.enabled = true.\n */\n\nimport { homedir } from 'os';\nimport { isAbsolute, relative, resolve } from 'path';\nimport { realpath } from 'fs/promises';\nimport { Constants } from '@/common';\nimport type {\n HookCallback,\n PreToolUseHookInput,\n PreToolUseHookOutput,\n ToolDecision,\n} from './types';\n\n/**\n * What to do when a tool call references a path outside the workspace.\n *\n * - `'ask'` : default. Raise a PreToolUse `ask` (host UI prompts\n * via the HITL interrupt path).\n * - `'allow'` : let the call through (use the existing tool clamp\n * to actually enforce — the hook is purely advisory).\n * - `'deny'` : block the call with an error ToolMessage.\n */\nexport type OutsideAccessPolicy = 'ask' | 'allow' | 'deny';\n\nexport interface WorkspacePolicyConfig {\n /** Canonical workspace root. Required. */\n root: string;\n /** Sibling roots that count as inside-workspace. */\n additionalRoots?: readonly string[];\n /** Policy applied to read-only file tools. Defaults to `'ask'`. */\n outsideRead?: OutsideAccessPolicy;\n /** Policy applied to write-shaped file tools. Defaults to `'ask'`. */\n outsideWrite?: OutsideAccessPolicy;\n /**\n * Optional reason template surfaced in the `ask`/`deny` decision.\n * Supports `{tool}` and `{paths}` substitution.\n */\n reason?: string;\n /**\n * Per-tool path extractors. Defaults cover the local-engine coding\n * suite. Returning an empty array opts that tool out of policy.\n */\n pathExtractors?: Record<string, PathExtractor>;\n}\n\nexport type PathExtractor = (\n toolInput: Record<string, unknown>\n) => readonly string[];\n\nconst READ_TOOLS = new Set<string>([\n Constants.READ_FILE,\n Constants.GREP_SEARCH,\n Constants.GLOB_SEARCH,\n Constants.LIST_DIRECTORY,\n Constants.COMPILE_CHECK,\n]);\n\nconst WRITE_TOOLS = new Set<string>([\n Constants.WRITE_FILE,\n Constants.EDIT_FILE,\n]);\n\n/**\n * Best-effort extractor for `compile_check` — pulls absolute and `~/`\n * path tokens out of the `command` string so the workspace boundary\n * sees them. Without this, a model could ship `command: 'cat\n * /etc/passwd'` and the policy hook would short-circuit to `allow`\n * (Codex P1 #26 — the prior `() => []` made the hook a no-op for\n * compile_check). Conservative by design:\n *\n * - Matches `/foo`, `~/foo`, `$HOME/foo`, `${HOME}/foo` followed by\n * non-shell-special chars. Stops at whitespace, quotes, redirect\n * operators, pipes, semicolons.\n * - Strips a leading `--flag=` so `--out=/etc/foo` extracts as\n * `/etc/foo` (the path the agent's actually trying to write).\n * - Misses relative paths (intended — those resolve under cwd\n * anyway), and shell-substituted paths whose final form isn't\n * visible at extract time. Hosts that need bulletproof gating\n * should pair this with a `bash_tool`-level policy.\n */\n// `[\"']?` slots before AND after the captured path cover quoted\n// forms like `cat \"/etc/passwd\"` and `--out='/tmp/x'`. Codex P1 #31\n// — the previous regex only matched unquoted tokens, so a model\n// could trivially bypass the workspace policy by quoting any\n// destination path. The path content character class still excludes\n// quotes/whitespace/shell-specials so we don't over-extract; that's\n// the defensive trade we want for fallback-grep style matching.\n//\n// The `\\.\\.(?:\\/[^…]*)?` alternation covers parent-traversal forms\n// (`..`, `../secrets.txt`, `../foo/bar`). Without it, a model could\n// exfiltrate parent-directory files via `cat ../secrets` and the\n// hook would short-circuit to `allow` because the extractor saw no\n// \"absolute\" token. The boundary check at the call site resolves\n// non-absolute extracted tokens against `root`, so `../secrets`\n// becomes `<parent-of-workspace>/secrets` which the boundary then\n// correctly flags as outside. Codex P2 #35.\nconst PATH_TOKEN =\n /(?:^|[\\s=])(?:--[^\\s=]+=)?[\"']?(\\/[^\\s'\"|;&<>()`]+|~\\/[^\\s'\"|;&<>()`]+|\\$\\{?HOME\\}?\\/[^\\s'\"|;&<>()`]+|\\.\\.(?:\\/[^\\s'\"|;&<>()`]*)?)[\"']?/g;\n// Back-compat alias kept for any downstream import.\nconst ABSOLUTE_PATH_TOKEN = PATH_TOKEN;\nfunction expandHomeRelative(token: string): string {\n // Expand ~/foo and $HOME/foo and ${HOME}/foo to absolute. The\n // workspace boundary check resolves non-absolute paths against the\n // workspace root, which would silently treat `~/secret` as\n // `<workspace>/~/secret` — exactly the bypass the codex flagged.\n const home = homedir();\n if (token.startsWith('~/')) return `${home}/${token.slice(2)}`;\n if (token.startsWith('${HOME}/')) return `${home}/${token.slice(8)}`;\n if (token.startsWith('$HOME/')) return `${home}/${token.slice(6)}`;\n return token;\n}\nfunction extractCompileCheckPaths(input: Record<string, unknown>): string[] {\n const command = typeof input.command === 'string' ? input.command : '';\n if (command === '') return [];\n const out: string[] = [];\n for (const match of command.matchAll(ABSOLUTE_PATH_TOKEN)) {\n out.push(expandHomeRelative(match[1]));\n }\n return out;\n}\n\nconst DEFAULT_EXTRACTORS: Record<string, PathExtractor> = {\n [Constants.READ_FILE]: (i) =>\n typeof i.file_path === 'string' ? [i.file_path] : [],\n [Constants.WRITE_FILE]: (i) =>\n typeof i.file_path === 'string' ? [i.file_path] : [],\n [Constants.EDIT_FILE]: (i) =>\n typeof i.file_path === 'string' ? [i.file_path] : [],\n [Constants.GREP_SEARCH]: (i) =>\n typeof i.path === 'string' && i.path !== '' ? [i.path] : [],\n [Constants.GLOB_SEARCH]: (i) =>\n typeof i.path === 'string' && i.path !== '' ? [i.path] : [],\n [Constants.LIST_DIRECTORY]: (i) =>\n typeof i.path === 'string' && i.path !== '' ? [i.path] : [],\n [Constants.COMPILE_CHECK]: extractCompileCheckPaths,\n};\n\nfunction isInsideAnyRoot(absolutePath: string, roots: string[]): boolean {\n for (const root of roots) {\n if (absolutePath === root) return true;\n const rel = relative(root, absolutePath);\n if (!rel.startsWith('..') && !isAbsolute(rel)) return true;\n }\n return false;\n}\n\n/**\n * Symlink-aware variant: realpaths the candidate AND the roots before\n * comparing. Without this, a symlink inside the workspace pointing\n * outside (e.g. `workspace/link → /etc/passwd`) compares as\n * \"in-workspace\" lexically, but actually grants the agent reach\n * outside the boundary. Critical when this hook is the primary gate\n * (i.e. the host opted into `workspace.allowReadOutside: true` /\n * `allowWriteOutside: true` so the file tools' own clamp is off).\n *\n * Handles paths that don't yet exist (e.g. `write_file` to a brand\n * new path) by walking up to the nearest existing ancestor and\n * realpathing that, then re-attaching the unresolved suffix. Mirrors\n * `resolveWorkspacePathSafe`'s approach in LocalExecutionEngine.\n */\nasync function realpathOrSelf(absolutePath: string): Promise<string> {\n try {\n return await realpath(absolutePath);\n } catch {\n return absolutePath;\n }\n}\n\nasync function realpathOfPathOrAncestor(\n absolutePath: string\n): Promise<string> {\n let current = absolutePath;\n let suffix = '';\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n while (true) {\n try {\n const real = await realpath(current);\n return suffix === '' ? real : resolve(real, suffix);\n } catch {\n const parent = resolve(current, '..');\n if (parent === current) {\n return absolutePath;\n }\n const base = current.slice(parent.length + 1);\n suffix = suffix === '' ? base : `${base}/${suffix}`;\n current = parent;\n }\n }\n}\n\nasync function isInsideAnyRootRealpath(\n absolutePath: string,\n realRoots: readonly string[]\n): Promise<boolean> {\n const real = await realpathOfPathOrAncestor(absolutePath);\n return isInsideAnyRoot(real, [...realRoots]);\n}\n\nfunction formatReason(\n template: string | undefined,\n toolName: string,\n outsidePaths: readonly string[]\n): string {\n const fallback = `Tool \"${toolName}\" wants to touch ${outsidePaths.length} path(s) outside the workspace: ${outsidePaths.join(', ')}`;\n if (template == null) return fallback;\n return template\n .replace(/\\{tool\\}/g, toolName)\n .replace(/\\{paths\\}/g, outsidePaths.join(', '));\n}\n\n/**\n * Build a `PreToolUse` callback that enforces the workspace policy.\n * Register it on a `HookRegistry`:\n *\n * ```ts\n * registry.register('PreToolUse', {\n * hooks: [createWorkspacePolicyHook({ root, outsideWrite: 'ask' })],\n * });\n * ```\n *\n * The hook is composable with `createToolPolicyHook` — register both;\n * `executeHooks` precedence (`deny > ask > allow`) sorts out which\n * decision wins per call.\n */\nexport function createWorkspacePolicyHook(\n config: WorkspacePolicyConfig\n): HookCallback<'PreToolUse'> {\n const root = resolve(config.root);\n // Relative `additionalRoots` entries are anchored to `root` so a\n // monorepo config like `additionalRoots: ['../shared']` resolves\n // to a sibling of `root`, not of process.cwd. Matches\n // `getWorkspaceRoots` in LocalExecutionEngine.\n const additionalRoots = (config.additionalRoots ?? []).map((p) =>\n isAbsolute(p) ? resolve(p) : resolve(root, p)\n );\n const allRoots = [root, ...additionalRoots];\n\n // Pre-realpath the roots once at construction — these are stable\n // per Run. The candidate paths get realpath'd lazily inside the\n // hook callback. Cached so the per-call cost is just one realpath.\n let realRootsPromise: Promise<string[]> | undefined;\n const getRealRoots = (): Promise<string[]> => {\n if (realRootsPromise == null) {\n realRootsPromise = Promise.all(allRoots.map(realpathOrSelf));\n }\n return realRootsPromise;\n };\n\n const readPolicy: OutsideAccessPolicy = config.outsideRead ?? 'ask';\n const writePolicy: OutsideAccessPolicy = config.outsideWrite ?? 'ask';\n\n const extractors: Record<string, PathExtractor> = {\n ...DEFAULT_EXTRACTORS,\n ...(config.pathExtractors ?? {}),\n };\n\n return async (input: PreToolUseHookInput): Promise<PreToolUseHookOutput> => {\n const extractor = extractors[input.toolName];\n if (extractor == null) return { decision: 'allow' };\n\n const paths = extractor(\n (input.toolInput ?? {}) as Record<string, unknown>\n );\n if (paths.length === 0) return { decision: 'allow' };\n\n // Two-stage check:\n // 1. Lexical fast path — anything that's lexically inside the\n // workspace AND doesn't get redirected by realpath stays\n // allow-able without paying the realpath cost on every call.\n // 2. For paths that look outside lexically OR look inside but\n // may have been routed through a symlink, realpath both the\n // candidate and the roots and compare. This catches the\n // `workspace/link → /etc/passwd` escape that lexical-only\n // checks miss.\n const outside: string[] = [];\n const realRoots = await getRealRoots();\n for (const p of paths) {\n const abs = isAbsolute(p) ? resolve(p) : resolve(root, p);\n // Realpath is the source of truth — it catches both the\n // symlink-escape case (lexically-inside path that resolves\n // outside) and the alternate-mount case (lexically-outside\n // path that resolves back inside the workspace). The lexical\n // check alone gives the wrong answer for either, so we don't\n // bother computing it.\n const realInside = await isInsideAnyRootRealpath(abs, realRoots);\n if (!realInside) {\n outside.push(p);\n }\n }\n if (outside.length === 0) return { decision: 'allow' };\n\n const policy = WRITE_TOOLS.has(input.toolName)\n ? writePolicy\n : READ_TOOLS.has(input.toolName)\n ? readPolicy\n : writePolicy; // unknown tools — treat as write (stricter)\n if (policy === 'allow') return { decision: 'allow' };\n\n const decision: ToolDecision = policy === 'deny' ? 'deny' : 'ask';\n return {\n decision,\n reason: formatReason(config.reason, input.toolName, outside),\n ...(decision === 'ask'\n ? { allowedDecisions: ['approve', 'reject'] as const }\n : {}),\n };\n };\n}\n"],"names":["Constants","homedir","relative","isAbsolute","realpath","resolve"],"mappings":";;;;;;;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CG;AAiDH,MAAM,UAAU,GAAG,IAAI,GAAG,CAAS;AACjC,IAAAA,eAAS,CAAC,SAAS;AACnB,IAAAA,eAAS,CAAC,WAAW;AACrB,IAAAA,eAAS,CAAC,WAAW;AACrB,IAAAA,eAAS,CAAC,cAAc;AACxB,IA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7B,kBAAE;AACF,kBAAE,WAAW,CAAC;QAClB,IAAI,MAAM,KAAK,OAAO;AAAE,YAAA,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE;AAEpD,QAAA,MAAM,QAAQ,GAAiB,MAAM,KAAK,MAAM,GAAG,MAAM,GAAG,KAAK;QACjE,OAAO;YACL,QAAQ;AACR,YAAA,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC;YAC5D,IAAI,QAAQ,KAAK;kBACb,EAAE,gBAAgB,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAU;kBAClD,EAAE,CAAC;SACR;AACH,IAAA,CAAC;AACH;;;;"}
@@ -53,6 +53,47 @@ function normalizeHeaders(headers) {
53
53
  });
54
54
  return Object.fromEntries(output.entries());
55
55
  }
56
+ function createUsageMetadata(usage) {
57
+ const usageMetadata = {
58
+ input_tokens: usage?.prompt_tokens ?? 0,
59
+ output_tokens: usage?.completion_tokens ?? 0,
60
+ total_tokens: usage?.total_tokens ?? 0,
61
+ };
62
+ if (usage == null) {
63
+ return usageMetadata;
64
+ }
65
+ const inputTokenDetails = {};
66
+ const outputTokenDetails = {};
67
+ let hasInputTokenDetails = false;
68
+ let hasOutputTokenDetails = false;
69
+ const audioInputTokens = usage.prompt_tokens_details?.audio_tokens;
70
+ const cachedInputTokens = usage.prompt_tokens_details?.cached_tokens;
71
+ const audioOutputTokens = usage.completion_tokens_details?.audio_tokens;
72
+ const reasoningOutputTokens = usage.completion_tokens_details?.reasoning_tokens;
73
+ if (audioInputTokens != null) {
74
+ inputTokenDetails.audio = audioInputTokens;
75
+ hasInputTokenDetails = true;
76
+ }
77
+ if (cachedInputTokens != null) {
78
+ inputTokenDetails.cache_read = cachedInputTokens;
79
+ hasInputTokenDetails = true;
80
+ }
81
+ if (audioOutputTokens != null) {
82
+ outputTokenDetails.audio = audioOutputTokens;
83
+ hasOutputTokenDetails = true;
84
+ }
85
+ if (reasoningOutputTokens != null) {
86
+ outputTokenDetails.reasoning = reasoningOutputTokens;
87
+ hasOutputTokenDetails = true;
88
+ }
89
+ if (hasInputTokenDetails) {
90
+ usageMetadata.input_token_details = inputTokenDetails;
91
+ }
92
+ if (hasOutputTokenDetails) {
93
+ usageMetadata.output_token_details = outputTokenDetails;
94
+ }
95
+ return usageMetadata;
96
+ }
56
97
  function getExposedOpenAIClient(completions, responses, preferResponses) {
57
98
  const responsesClient = responses.client;
58
99
  if (responsesClient?.abortHandler != null) {
@@ -793,6 +834,54 @@ class ChatDeepSeek extends deepseek.ChatDeepSeek {
793
834
  static lc_name() {
794
835
  return 'LibreChatDeepSeek';
795
836
  }
837
+ _convertDeepSeekMessages(messages) {
838
+ return index._convertMessagesToOpenAIParams(messages, this.model, {
839
+ includeReasoningContent: true,
840
+ });
841
+ }
842
+ async _generate(messages$1, options, runManager) {
843
+ options.signal?.throwIfAborted();
844
+ const params = this.invocationParams(options);
845
+ if (params.stream === true) {
846
+ return super._generate(messages$1, options, runManager);
847
+ }
848
+ const messagesMapped = this._convertDeepSeekMessages(messages$1);
849
+ const response = await this.completionWithRetry({
850
+ ...params,
851
+ stream: false,
852
+ messages: messagesMapped,
853
+ }, {
854
+ signal: options.signal,
855
+ ...options.options,
856
+ });
857
+ const usageMetadata = createUsageMetadata(response.usage);
858
+ const generations = response.choices.map((part) => {
859
+ const text = part.message.content ?? '';
860
+ const generation = {
861
+ text,
862
+ message: this._convertCompletionsMessageToBaseMessage(part.message, response),
863
+ };
864
+ generation.generationInfo = {
865
+ finish_reason: part.finish_reason,
866
+ ...(part.logprobs != null ? { logprobs: part.logprobs } : {}),
867
+ };
868
+ if (messages.isAIMessage(generation.message)) {
869
+ generation.message.usage_metadata = usageMetadata;
870
+ }
871
+ generation.message = new messages.AIMessage(Object.fromEntries(Object.entries(generation.message).filter(([key]) => !key.startsWith('lc_'))));
872
+ return generation;
873
+ });
874
+ return {
875
+ generations,
876
+ llmOutput: {
877
+ tokenUsage: {
878
+ promptTokens: usageMetadata.input_tokens,
879
+ completionTokens: usageMetadata.output_tokens,
880
+ totalTokens: usageMetadata.total_tokens,
881
+ },
882
+ },
883
+ };
884
+ }
796
885
  _getClientOptions(options) {
797
886
  if (!this.client) {
798
887
  const openAIEndpointConfig = {
@@ -817,7 +906,234 @@ class ChatDeepSeek extends deepseek.ChatDeepSeek {
817
906
  return requestOptions;
818
907
  }
819
908
  async *_streamResponseChunks(messages, options, runManager) {
820
- yield* delayStreamChunks(super._streamResponseChunks(messages, options, runManager), this._lc_stream_delay);
909
+ yield* delayStreamChunks(this._streamResponseChunksWithReasoning(messages, options, runManager), this._lc_stream_delay);
910
+ }
911
+ /** Parses raw `<think>` fallback tags across chunks and emits sanitized DeepSeek stream chunks. */
912
+ async *_streamResponseChunksWithReasoning(messages$1, options, runManager) {
913
+ const stream = this._streamResponseChunksFromReasoningMessages(messages$1, options);
914
+ const thinkStartTag = '<think>';
915
+ const thinkEndTag = '</think>';
916
+ let tokensBuffer = '';
917
+ let isThinking = false;
918
+ for await (const chunk of stream) {
919
+ if (options.signal?.aborted === true) {
920
+ throw new Error('AbortError');
921
+ }
922
+ const reasoningContent = chunk.message.additional_kwargs.reasoning_content;
923
+ if (reasoningContent != null && reasoningContent !== '') {
924
+ yield* this._yieldDeepSeekStreamChunk(chunk, runManager);
925
+ continue;
926
+ }
927
+ const text = chunk.text;
928
+ if (text === '') {
929
+ yield* this._yieldDeepSeekStreamChunk(chunk, runManager);
930
+ continue;
931
+ }
932
+ tokensBuffer += text;
933
+ while (tokensBuffer !== '') {
934
+ if (isThinking) {
935
+ const thinkEndIndex = tokensBuffer.indexOf(thinkEndTag);
936
+ if (thinkEndIndex !== -1) {
937
+ const thoughtContent = tokensBuffer.substring(0, thinkEndIndex);
938
+ if (thoughtContent !== '') {
939
+ yield* this._yieldDeepSeekReasoningText(chunk, thoughtContent, runManager);
940
+ }
941
+ tokensBuffer = tokensBuffer.substring(thinkEndIndex + thinkEndTag.length);
942
+ isThinking = false;
943
+ continue;
944
+ }
945
+ const splitIndex = this._getDeepSeekPartialTagSplitIndex(tokensBuffer, thinkEndTag);
946
+ if (splitIndex !== -1) {
947
+ const safeToYield = tokensBuffer.substring(0, splitIndex);
948
+ if (safeToYield !== '') {
949
+ yield* this._yieldDeepSeekReasoningText(chunk, safeToYield, runManager);
950
+ }
951
+ tokensBuffer = tokensBuffer.substring(splitIndex);
952
+ break;
953
+ }
954
+ yield* this._yieldDeepSeekReasoningText(chunk, tokensBuffer, runManager);
955
+ tokensBuffer = '';
956
+ break;
957
+ }
958
+ const thinkStartIndex = tokensBuffer.indexOf(thinkStartTag);
959
+ if (thinkStartIndex !== -1) {
960
+ const beforeThink = tokensBuffer.substring(0, thinkStartIndex);
961
+ if (beforeThink !== '') {
962
+ yield* this._yieldDeepSeekStreamChunk(this._createDeepSeekStreamChunk(chunk, beforeThink), runManager);
963
+ }
964
+ tokensBuffer = tokensBuffer.substring(thinkStartIndex + thinkStartTag.length);
965
+ isThinking = true;
966
+ continue;
967
+ }
968
+ const splitIndex = this._getDeepSeekPartialTagSplitIndex(tokensBuffer, thinkStartTag);
969
+ if (splitIndex !== -1) {
970
+ const safeToYield = tokensBuffer.substring(0, splitIndex);
971
+ if (safeToYield !== '') {
972
+ yield* this._yieldDeepSeekStreamChunk(this._createDeepSeekStreamChunk(chunk, safeToYield), runManager);
973
+ }
974
+ tokensBuffer = tokensBuffer.substring(splitIndex);
975
+ break;
976
+ }
977
+ yield* this._yieldDeepSeekStreamChunk(this._createDeepSeekStreamChunk(chunk, tokensBuffer), runManager);
978
+ tokensBuffer = '';
979
+ break;
980
+ }
981
+ }
982
+ if (tokensBuffer === '') {
983
+ return;
984
+ }
985
+ if (isThinking) {
986
+ yield* this._yieldDeepSeekStreamChunk(new outputs.ChatGenerationChunk({
987
+ message: new messages.AIMessageChunk({
988
+ content: '',
989
+ additional_kwargs: {
990
+ reasoning_content: tokensBuffer,
991
+ },
992
+ }),
993
+ text: '',
994
+ }), runManager);
995
+ return;
996
+ }
997
+ yield* this._yieldDeepSeekStreamChunk(new outputs.ChatGenerationChunk({
998
+ message: new messages.AIMessageChunk({
999
+ content: tokensBuffer,
1000
+ }),
1001
+ text: tokensBuffer,
1002
+ }), runManager);
1003
+ }
1004
+ async *_streamResponseChunksFromReasoningMessages(messages$1, options) {
1005
+ const params = {
1006
+ ...this.invocationParams(options, { streaming: true }),
1007
+ stream: true,
1008
+ };
1009
+ const messagesMapped = this._convertDeepSeekMessages(messages$1);
1010
+ const streamIterable = await this.completionWithRetry({
1011
+ ...params,
1012
+ messages: messagesMapped,
1013
+ }, {
1014
+ signal: options.signal,
1015
+ ...options.options,
1016
+ });
1017
+ let defaultRole;
1018
+ let usage;
1019
+ for await (const data of streamIterable) {
1020
+ if (options.signal?.aborted === true) {
1021
+ throw new Error('AbortError');
1022
+ }
1023
+ if (data.usage != null) {
1024
+ usage = data.usage;
1025
+ }
1026
+ if (data.choices.length === 0) {
1027
+ continue;
1028
+ }
1029
+ const choice = data.choices[0];
1030
+ const { delta } = choice;
1031
+ const messageChunk = this._convertCompletionsDeltaToBaseMessageChunk(delta, data, defaultRole);
1032
+ defaultRole = delta.role ?? defaultRole;
1033
+ if (typeof messageChunk.content !== 'string') {
1034
+ continue;
1035
+ }
1036
+ const messageText = messageChunk.content;
1037
+ const newTokenIndices = {
1038
+ prompt: options.promptIndex ?? 0,
1039
+ completion: choice.index,
1040
+ };
1041
+ const generationInfo = { ...newTokenIndices };
1042
+ if (choice.finish_reason != null) {
1043
+ Object.assign(generationInfo, {
1044
+ finish_reason: choice.finish_reason,
1045
+ system_fingerprint: data.system_fingerprint,
1046
+ model_name: data.model,
1047
+ service_tier: data.service_tier,
1048
+ });
1049
+ }
1050
+ if (this.logprobs === true) {
1051
+ Object.assign(generationInfo, { logprobs: choice.logprobs });
1052
+ }
1053
+ const generationChunk = new outputs.ChatGenerationChunk({
1054
+ message: messageChunk,
1055
+ text: messageText,
1056
+ generationInfo,
1057
+ });
1058
+ yield generationChunk;
1059
+ }
1060
+ if (usage != null) {
1061
+ const usageMetadata = createUsageMetadata(usage);
1062
+ const generationChunk = new outputs.ChatGenerationChunk({
1063
+ message: new messages.AIMessageChunk({
1064
+ content: '',
1065
+ response_metadata: {
1066
+ usage: { ...usage },
1067
+ },
1068
+ usage_metadata: usageMetadata,
1069
+ }),
1070
+ text: '',
1071
+ generationInfo: {
1072
+ prompt: 0,
1073
+ completion: 0,
1074
+ },
1075
+ });
1076
+ yield generationChunk;
1077
+ }
1078
+ if (options.signal?.aborted === true) {
1079
+ throw new Error('AbortError');
1080
+ }
1081
+ }
1082
+ _createDeepSeekStreamChunk(chunk, content, additionalKwargs, text = content) {
1083
+ if (!(chunk.message instanceof messages.AIMessageChunk)) {
1084
+ return new outputs.ChatGenerationChunk({
1085
+ message: new messages.AIMessageChunk({
1086
+ content,
1087
+ additional_kwargs: additionalKwargs ?? chunk.message.additional_kwargs,
1088
+ response_metadata: chunk.message.response_metadata,
1089
+ id: chunk.message.id,
1090
+ }),
1091
+ text,
1092
+ generationInfo: chunk.generationInfo,
1093
+ });
1094
+ }
1095
+ const message = chunk.message;
1096
+ return new outputs.ChatGenerationChunk({
1097
+ message: new messages.AIMessageChunk({
1098
+ content,
1099
+ additional_kwargs: additionalKwargs ?? message.additional_kwargs,
1100
+ response_metadata: message.response_metadata,
1101
+ tool_calls: message.tool_calls,
1102
+ tool_call_chunks: message.tool_call_chunks,
1103
+ id: message.id,
1104
+ }),
1105
+ text,
1106
+ generationInfo: chunk.generationInfo,
1107
+ });
1108
+ }
1109
+ _createDeepSeekReasoningStreamChunk(chunk, reasoningContent) {
1110
+ return this._createDeepSeekStreamChunk(chunk, '', {
1111
+ ...chunk.message.additional_kwargs,
1112
+ reasoning_content: reasoningContent,
1113
+ }, '');
1114
+ }
1115
+ async *_yieldDeepSeekReasoningText(chunk, reasoningContent, runManager) {
1116
+ yield* this._yieldDeepSeekStreamChunk(this._createDeepSeekReasoningStreamChunk(chunk, reasoningContent), runManager);
1117
+ }
1118
+ async *_yieldDeepSeekStreamChunk(chunk, runManager) {
1119
+ yield chunk;
1120
+ await runManager?.handleLLMNewToken(chunk.text, this._getDeepSeekTokenIndices(chunk), undefined, undefined, undefined, { chunk });
1121
+ }
1122
+ _getDeepSeekTokenIndices(chunk) {
1123
+ const prompt = chunk.generationInfo?.prompt;
1124
+ const completion = chunk.generationInfo?.completion;
1125
+ if (typeof prompt === 'number' && typeof completion === 'number') {
1126
+ return { prompt, completion };
1127
+ }
1128
+ return undefined;
1129
+ }
1130
+ _getDeepSeekPartialTagSplitIndex(text, tag) {
1131
+ for (let i = tag.length - 1; i >= 1; i--) {
1132
+ if (text.endsWith(tag.substring(0, i))) {
1133
+ return text.length - i;
1134
+ }
1135
+ }
1136
+ return -1;
821
1137
  }
822
1138
  }
823
1139
  class ChatMoonshot extends ChatOpenAI {