@open-multi-agent/core 1.4.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 (207) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +373 -0
  3. package/dist/agent/agent.d.ts +153 -0
  4. package/dist/agent/agent.d.ts.map +1 -0
  5. package/dist/agent/agent.js +559 -0
  6. package/dist/agent/agent.js.map +1 -0
  7. package/dist/agent/loop-detector.d.ts +39 -0
  8. package/dist/agent/loop-detector.d.ts.map +1 -0
  9. package/dist/agent/loop-detector.js +122 -0
  10. package/dist/agent/loop-detector.js.map +1 -0
  11. package/dist/agent/pool.d.ts +158 -0
  12. package/dist/agent/pool.d.ts.map +1 -0
  13. package/dist/agent/pool.js +320 -0
  14. package/dist/agent/pool.js.map +1 -0
  15. package/dist/agent/runner.d.ts +242 -0
  16. package/dist/agent/runner.d.ts.map +1 -0
  17. package/dist/agent/runner.js +943 -0
  18. package/dist/agent/runner.js.map +1 -0
  19. package/dist/agent/structured-output.d.ts +33 -0
  20. package/dist/agent/structured-output.d.ts.map +1 -0
  21. package/dist/agent/structured-output.js +116 -0
  22. package/dist/agent/structured-output.js.map +1 -0
  23. package/dist/cli/oma.d.ts +30 -0
  24. package/dist/cli/oma.d.ts.map +1 -0
  25. package/dist/cli/oma.js +433 -0
  26. package/dist/cli/oma.js.map +1 -0
  27. package/dist/dashboard/layout-tasks.d.ts +23 -0
  28. package/dist/dashboard/layout-tasks.d.ts.map +1 -0
  29. package/dist/dashboard/layout-tasks.js +79 -0
  30. package/dist/dashboard/layout-tasks.js.map +1 -0
  31. package/dist/dashboard/render-team-run-dashboard.d.ts +11 -0
  32. package/dist/dashboard/render-team-run-dashboard.d.ts.map +1 -0
  33. package/dist/dashboard/render-team-run-dashboard.js +456 -0
  34. package/dist/dashboard/render-team-run-dashboard.js.map +1 -0
  35. package/dist/errors.d.ts +14 -0
  36. package/dist/errors.d.ts.map +1 -0
  37. package/dist/errors.js +20 -0
  38. package/dist/errors.js.map +1 -0
  39. package/dist/index.d.ts +79 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +92 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/llm/adapter.d.ts +54 -0
  44. package/dist/llm/adapter.d.ts.map +1 -0
  45. package/dist/llm/adapter.js +101 -0
  46. package/dist/llm/adapter.js.map +1 -0
  47. package/dist/llm/anthropic.d.ts +57 -0
  48. package/dist/llm/anthropic.d.ts.map +1 -0
  49. package/dist/llm/anthropic.js +432 -0
  50. package/dist/llm/anthropic.js.map +1 -0
  51. package/dist/llm/azure-openai.d.ts +74 -0
  52. package/dist/llm/azure-openai.d.ts.map +1 -0
  53. package/dist/llm/azure-openai.js +267 -0
  54. package/dist/llm/azure-openai.js.map +1 -0
  55. package/dist/llm/bedrock.d.ts +41 -0
  56. package/dist/llm/bedrock.d.ts.map +1 -0
  57. package/dist/llm/bedrock.js +345 -0
  58. package/dist/llm/bedrock.js.map +1 -0
  59. package/dist/llm/copilot.d.ts +92 -0
  60. package/dist/llm/copilot.d.ts.map +1 -0
  61. package/dist/llm/copilot.js +433 -0
  62. package/dist/llm/copilot.js.map +1 -0
  63. package/dist/llm/deepseek.d.ts +21 -0
  64. package/dist/llm/deepseek.d.ts.map +1 -0
  65. package/dist/llm/deepseek.js +24 -0
  66. package/dist/llm/deepseek.js.map +1 -0
  67. package/dist/llm/gemini.d.ts +65 -0
  68. package/dist/llm/gemini.d.ts.map +1 -0
  69. package/dist/llm/gemini.js +427 -0
  70. package/dist/llm/gemini.js.map +1 -0
  71. package/dist/llm/grok.d.ts +21 -0
  72. package/dist/llm/grok.d.ts.map +1 -0
  73. package/dist/llm/grok.js +24 -0
  74. package/dist/llm/grok.js.map +1 -0
  75. package/dist/llm/minimax.d.ts +21 -0
  76. package/dist/llm/minimax.d.ts.map +1 -0
  77. package/dist/llm/minimax.js +24 -0
  78. package/dist/llm/minimax.js.map +1 -0
  79. package/dist/llm/openai-common.d.ts +65 -0
  80. package/dist/llm/openai-common.d.ts.map +1 -0
  81. package/dist/llm/openai-common.js +286 -0
  82. package/dist/llm/openai-common.js.map +1 -0
  83. package/dist/llm/openai.d.ts +63 -0
  84. package/dist/llm/openai.d.ts.map +1 -0
  85. package/dist/llm/openai.js +256 -0
  86. package/dist/llm/openai.js.map +1 -0
  87. package/dist/llm/qiniu.d.ts +21 -0
  88. package/dist/llm/qiniu.d.ts.map +1 -0
  89. package/dist/llm/qiniu.js +24 -0
  90. package/dist/llm/qiniu.js.map +1 -0
  91. package/dist/mcp.d.ts +3 -0
  92. package/dist/mcp.d.ts.map +1 -0
  93. package/dist/mcp.js +2 -0
  94. package/dist/mcp.js.map +1 -0
  95. package/dist/memory/shared.d.ts +162 -0
  96. package/dist/memory/shared.d.ts.map +1 -0
  97. package/dist/memory/shared.js +294 -0
  98. package/dist/memory/shared.js.map +1 -0
  99. package/dist/memory/store.d.ts +72 -0
  100. package/dist/memory/store.d.ts.map +1 -0
  101. package/dist/memory/store.js +121 -0
  102. package/dist/memory/store.js.map +1 -0
  103. package/dist/orchestrator/orchestrator.d.ts +245 -0
  104. package/dist/orchestrator/orchestrator.d.ts.map +1 -0
  105. package/dist/orchestrator/orchestrator.js +1400 -0
  106. package/dist/orchestrator/orchestrator.js.map +1 -0
  107. package/dist/orchestrator/scheduler.d.ts +112 -0
  108. package/dist/orchestrator/scheduler.d.ts.map +1 -0
  109. package/dist/orchestrator/scheduler.js +256 -0
  110. package/dist/orchestrator/scheduler.js.map +1 -0
  111. package/dist/task/queue.d.ts +191 -0
  112. package/dist/task/queue.d.ts.map +1 -0
  113. package/dist/task/queue.js +408 -0
  114. package/dist/task/queue.js.map +1 -0
  115. package/dist/task/task.d.ts +90 -0
  116. package/dist/task/task.d.ts.map +1 -0
  117. package/dist/task/task.js +206 -0
  118. package/dist/task/task.js.map +1 -0
  119. package/dist/team/messaging.d.ts +106 -0
  120. package/dist/team/messaging.d.ts.map +1 -0
  121. package/dist/team/messaging.js +183 -0
  122. package/dist/team/messaging.js.map +1 -0
  123. package/dist/team/team.d.ts +141 -0
  124. package/dist/team/team.d.ts.map +1 -0
  125. package/dist/team/team.js +293 -0
  126. package/dist/team/team.js.map +1 -0
  127. package/dist/tool/built-in/bash.d.ts +12 -0
  128. package/dist/tool/built-in/bash.d.ts.map +1 -0
  129. package/dist/tool/built-in/bash.js +133 -0
  130. package/dist/tool/built-in/bash.js.map +1 -0
  131. package/dist/tool/built-in/delegate.d.ts +29 -0
  132. package/dist/tool/built-in/delegate.d.ts.map +1 -0
  133. package/dist/tool/built-in/delegate.js +92 -0
  134. package/dist/tool/built-in/delegate.js.map +1 -0
  135. package/dist/tool/built-in/file-edit.d.ts +14 -0
  136. package/dist/tool/built-in/file-edit.d.ts.map +1 -0
  137. package/dist/tool/built-in/file-edit.js +130 -0
  138. package/dist/tool/built-in/file-edit.js.map +1 -0
  139. package/dist/tool/built-in/file-read.d.ts +12 -0
  140. package/dist/tool/built-in/file-read.d.ts.map +1 -0
  141. package/dist/tool/built-in/file-read.js +82 -0
  142. package/dist/tool/built-in/file-read.js.map +1 -0
  143. package/dist/tool/built-in/file-write.d.ts +11 -0
  144. package/dist/tool/built-in/file-write.d.ts.map +1 -0
  145. package/dist/tool/built-in/file-write.js +70 -0
  146. package/dist/tool/built-in/file-write.js.map +1 -0
  147. package/dist/tool/built-in/fs-walk.d.ts +23 -0
  148. package/dist/tool/built-in/fs-walk.d.ts.map +1 -0
  149. package/dist/tool/built-in/fs-walk.js +78 -0
  150. package/dist/tool/built-in/fs-walk.js.map +1 -0
  151. package/dist/tool/built-in/glob.d.ts +12 -0
  152. package/dist/tool/built-in/glob.d.ts.map +1 -0
  153. package/dist/tool/built-in/glob.js +82 -0
  154. package/dist/tool/built-in/glob.js.map +1 -0
  155. package/dist/tool/built-in/grep.d.ts +15 -0
  156. package/dist/tool/built-in/grep.d.ts.map +1 -0
  157. package/dist/tool/built-in/grep.js +218 -0
  158. package/dist/tool/built-in/grep.js.map +1 -0
  159. package/dist/tool/built-in/index.d.ts +48 -0
  160. package/dist/tool/built-in/index.d.ts.map +1 -0
  161. package/dist/tool/built-in/index.js +56 -0
  162. package/dist/tool/built-in/index.js.map +1 -0
  163. package/dist/tool/executor.d.ts +100 -0
  164. package/dist/tool/executor.d.ts.map +1 -0
  165. package/dist/tool/executor.js +184 -0
  166. package/dist/tool/executor.js.map +1 -0
  167. package/dist/tool/framework.d.ts +167 -0
  168. package/dist/tool/framework.d.ts.map +1 -0
  169. package/dist/tool/framework.js +402 -0
  170. package/dist/tool/framework.js.map +1 -0
  171. package/dist/tool/mcp.d.ts +31 -0
  172. package/dist/tool/mcp.d.ts.map +1 -0
  173. package/dist/tool/mcp.js +175 -0
  174. package/dist/tool/mcp.js.map +1 -0
  175. package/dist/tool/text-tool-extractor.d.ts +32 -0
  176. package/dist/tool/text-tool-extractor.d.ts.map +1 -0
  177. package/dist/tool/text-tool-extractor.js +195 -0
  178. package/dist/tool/text-tool-extractor.js.map +1 -0
  179. package/dist/types.d.ts +916 -0
  180. package/dist/types.d.ts.map +1 -0
  181. package/dist/types.js +8 -0
  182. package/dist/types.js.map +1 -0
  183. package/dist/utils/keywords.d.ts +18 -0
  184. package/dist/utils/keywords.d.ts.map +1 -0
  185. package/dist/utils/keywords.js +32 -0
  186. package/dist/utils/keywords.js.map +1 -0
  187. package/dist/utils/semaphore.d.ts +49 -0
  188. package/dist/utils/semaphore.d.ts.map +1 -0
  189. package/dist/utils/semaphore.js +89 -0
  190. package/dist/utils/semaphore.js.map +1 -0
  191. package/dist/utils/tokens.d.ts +7 -0
  192. package/dist/utils/tokens.d.ts.map +1 -0
  193. package/dist/utils/tokens.js +30 -0
  194. package/dist/utils/tokens.js.map +1 -0
  195. package/dist/utils/trace.d.ts +12 -0
  196. package/dist/utils/trace.d.ts.map +1 -0
  197. package/dist/utils/trace.js +30 -0
  198. package/dist/utils/trace.js.map +1 -0
  199. package/docs/DECISIONS.md +49 -0
  200. package/docs/cli.md +265 -0
  201. package/docs/context-management.md +24 -0
  202. package/docs/featured-partner.md +28 -0
  203. package/docs/observability.md +56 -0
  204. package/docs/providers.md +78 -0
  205. package/docs/shared-memory.md +27 -0
  206. package/docs/tool-configuration.md +152 -0
  207. package/package.json +96 -0
@@ -0,0 +1,218 @@
1
+ /**
2
+ * Built-in grep tool.
3
+ *
4
+ * Searches for a regex pattern in files. Prefers the `rg` (ripgrep) binary
5
+ * when available for performance; falls back to a pure Node.js recursive
6
+ * implementation using the standard `fs` module so the tool works in
7
+ * environments without ripgrep installed.
8
+ */
9
+ import { spawn } from 'child_process';
10
+ import { readFile, stat } from 'fs/promises';
11
+ import { relative } from 'path';
12
+ import { z } from 'zod';
13
+ import { defineTool } from '../framework.js';
14
+ import { collectFiles } from './fs-walk.js';
15
+ // ---------------------------------------------------------------------------
16
+ // Constants
17
+ // ---------------------------------------------------------------------------
18
+ const DEFAULT_MAX_RESULTS = 100;
19
+ // ---------------------------------------------------------------------------
20
+ // Tool definition
21
+ // ---------------------------------------------------------------------------
22
+ export const grepTool = defineTool({
23
+ name: 'grep',
24
+ description: 'Search for a regular-expression pattern in one or more files. ' +
25
+ 'Returns matching lines with their file paths and 1-based line numbers. ' +
26
+ 'Use the `glob` parameter to restrict the search to specific file types ' +
27
+ '(e.g. "*.ts"). ' +
28
+ 'To list matching file paths without reading contents, use the `glob` tool. ' +
29
+ 'Results are capped by `maxResults` to keep the response manageable.',
30
+ inputSchema: z.object({
31
+ pattern: z
32
+ .string()
33
+ .describe('Regular expression pattern to search for in file contents.'),
34
+ path: z
35
+ .string()
36
+ .optional()
37
+ .describe('Directory or file path to search in. ' +
38
+ 'Defaults to the current working directory.'),
39
+ glob: z
40
+ .string()
41
+ .optional()
42
+ .describe('Glob pattern to filter which files are searched ' +
43
+ '(e.g. "*.ts", "**/*.json"). ' +
44
+ 'Only used when `path` is a directory.'),
45
+ maxResults: z
46
+ .number()
47
+ .int()
48
+ .positive()
49
+ .optional()
50
+ .describe(`Maximum number of matching lines to return. ` +
51
+ `Defaults to ${DEFAULT_MAX_RESULTS}.`),
52
+ }),
53
+ execute: async (input, context) => {
54
+ const searchPath = input.path ?? process.cwd();
55
+ const maxResults = input.maxResults ?? DEFAULT_MAX_RESULTS;
56
+ // Compile the regex once and surface bad patterns immediately.
57
+ let regex;
58
+ try {
59
+ regex = new RegExp(input.pattern);
60
+ }
61
+ catch {
62
+ return {
63
+ data: `Invalid regular expression: "${input.pattern}"`,
64
+ isError: true,
65
+ };
66
+ }
67
+ // Attempt ripgrep first.
68
+ const rgAvailable = await isRipgrepAvailable();
69
+ if (rgAvailable) {
70
+ return runRipgrep(input.pattern, searchPath, {
71
+ glob: input.glob,
72
+ maxResults,
73
+ signal: context.abortSignal,
74
+ });
75
+ }
76
+ // Fallback: pure Node.js recursive search.
77
+ return runNodeSearch(regex, searchPath, {
78
+ glob: input.glob,
79
+ maxResults,
80
+ signal: context.abortSignal,
81
+ });
82
+ },
83
+ });
84
+ async function runRipgrep(pattern, searchPath, options) {
85
+ const args = [
86
+ '--line-number',
87
+ '--no-heading',
88
+ '--color=never',
89
+ `--max-count=${options.maxResults}`,
90
+ ];
91
+ if (options.glob !== undefined) {
92
+ args.push('--glob', options.glob);
93
+ }
94
+ args.push('--', pattern, searchPath);
95
+ return new Promise((resolve) => {
96
+ const chunks = [];
97
+ const errChunks = [];
98
+ const child = spawn('rg', args, { stdio: ['ignore', 'pipe', 'pipe'] });
99
+ child.stdout.on('data', (d) => chunks.push(d));
100
+ child.stderr.on('data', (d) => errChunks.push(d));
101
+ const onAbort = () => { child.kill('SIGKILL'); };
102
+ if (options.signal !== undefined) {
103
+ options.signal.addEventListener('abort', onAbort, { once: true });
104
+ }
105
+ child.on('close', (code) => {
106
+ if (options.signal !== undefined) {
107
+ options.signal.removeEventListener('abort', onAbort);
108
+ }
109
+ const output = Buffer.concat(chunks).toString('utf8').trimEnd();
110
+ // rg exit code 1 = no matches (not an error)
111
+ if (code !== 0 && code !== 1) {
112
+ const errMsg = Buffer.concat(errChunks).toString('utf8').trim();
113
+ resolve({
114
+ data: `ripgrep failed (exit ${code}): ${errMsg}`,
115
+ isError: true,
116
+ });
117
+ return;
118
+ }
119
+ if (output.length === 0) {
120
+ resolve({ data: 'No matches found.', isError: false });
121
+ return;
122
+ }
123
+ const lines = output.split('\n');
124
+ resolve({
125
+ data: lines.join('\n'),
126
+ isError: false,
127
+ });
128
+ });
129
+ child.on('error', () => {
130
+ if (options.signal !== undefined) {
131
+ options.signal.removeEventListener('abort', onAbort);
132
+ }
133
+ // Caller will see an error result — the tool won't retry with Node search
134
+ // since this branch is only reachable after we confirmed rg is available.
135
+ resolve({
136
+ data: 'ripgrep process error — run may be retried with the Node.js fallback.',
137
+ isError: true,
138
+ });
139
+ });
140
+ });
141
+ }
142
+ async function runNodeSearch(regex, searchPath, options) {
143
+ // Collect files
144
+ let files;
145
+ try {
146
+ const info = await stat(searchPath);
147
+ if (info.isFile()) {
148
+ files = [searchPath];
149
+ }
150
+ else {
151
+ files = await collectFiles(searchPath, options.glob, options.signal);
152
+ }
153
+ }
154
+ catch (err) {
155
+ const message = err instanceof Error ? err.message : 'Unknown error';
156
+ return {
157
+ data: `Cannot access path "${searchPath}": ${message}`,
158
+ isError: true,
159
+ };
160
+ }
161
+ const matches = [];
162
+ for (const file of files) {
163
+ if (options.signal?.aborted === true)
164
+ break;
165
+ if (matches.length >= options.maxResults)
166
+ break;
167
+ let fileContent;
168
+ try {
169
+ fileContent = (await readFile(file)).toString('utf8');
170
+ }
171
+ catch {
172
+ // Skip unreadable files (binary, permission denied, etc.)
173
+ continue;
174
+ }
175
+ const lines = fileContent.split('\n');
176
+ for (let i = 0; i < lines.length; i++) {
177
+ if (matches.length >= options.maxResults)
178
+ break;
179
+ // Reset lastIndex for global regexes
180
+ regex.lastIndex = 0;
181
+ if (regex.test(lines[i])) {
182
+ matches.push({
183
+ file: relative(process.cwd(), file) || file,
184
+ lineNumber: i + 1,
185
+ text: lines[i],
186
+ });
187
+ }
188
+ }
189
+ }
190
+ if (matches.length === 0) {
191
+ return { data: 'No matches found.', isError: false };
192
+ }
193
+ const formatted = matches
194
+ .map((m) => `${m.file}:${m.lineNumber}:${m.text}`)
195
+ .join('\n');
196
+ const truncationNote = matches.length >= options.maxResults
197
+ ? `\n\n(results capped at ${options.maxResults}; use maxResults to raise the limit)`
198
+ : '';
199
+ return {
200
+ data: formatted + truncationNote,
201
+ isError: false,
202
+ };
203
+ }
204
+ // ---------------------------------------------------------------------------
205
+ // ripgrep availability check (cached per process)
206
+ // ---------------------------------------------------------------------------
207
+ let rgAvailableCache;
208
+ async function isRipgrepAvailable() {
209
+ if (rgAvailableCache !== undefined)
210
+ return rgAvailableCache;
211
+ rgAvailableCache = await new Promise((resolve) => {
212
+ const child = spawn('rg', ['--version'], { stdio: 'ignore' });
213
+ child.on('close', (code) => resolve(code === 0));
214
+ child.on('error', () => resolve(false));
215
+ });
216
+ return rgAvailableCache;
217
+ }
218
+ //# sourceMappingURL=grep.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grep.js","sourceRoot":"","sources":["../../../src/tool/built-in/grep.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAA;AACrC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAA;AAC/B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAE3C,8EAA8E;AAC9E,YAAY;AACZ,8EAA8E;AAE9E,MAAM,mBAAmB,GAAG,GAAG,CAAA;AAE/B,8EAA8E;AAC9E,kBAAkB;AAClB,8EAA8E;AAE9E,MAAM,CAAC,MAAM,QAAQ,GAAG,UAAU,CAAC;IACjC,IAAI,EAAE,MAAM;IACZ,WAAW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,CAAC,CAAA;IAEb,MAAM,cAAc,GAClB,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,UAAU;QAClC,CAAC,CAAC,0BAA0B,OAAO,CAAC,UAAU,sCAAsC;QACpF,CAAC,CAAC,EAAE,CAAA;IAER,OAAO;QACL,IAAI,EAAE,SAAS,GAAG,cAAc;QAChC,OAAO,EAAE,KAAK;KACf,CAAA;AACH,CAAC;AAED,8EAA8E;AAC9E,kDAAkD;AAClD,8EAA8E;AAE9E,IAAI,gBAAqC,CAAA;AAEzC,KAAK,UAAU,kBAAkB;IAC/B,IAAI,gBAAgB,KAAK,SAAS;QAAE,OAAO,gBAAgB,CAAA;IAE3D,gBAAgB,GAAG,MAAM,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,EAAE;QACxD,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,WAAW,CAAC,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAA;QAC7D,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,CAAA;QAChD,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAA;IACzC,CAAC,CAAC,CAAA;IAEF,OAAO,gBAAgB,CAAA;AACzB,CAAC"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Built-in tool collection.
3
+ *
4
+ * Re-exports every built-in tool and provides a convenience function to
5
+ * register them all with a {@link ToolRegistry} in one call.
6
+ */
7
+ import type { ToolDefinition } from '../../types.js';
8
+ import { ToolRegistry } from '../framework.js';
9
+ import { bashTool } from './bash.js';
10
+ import { delegateToAgentTool } from './delegate.js';
11
+ import { fileEditTool } from './file-edit.js';
12
+ import { fileReadTool } from './file-read.js';
13
+ import { fileWriteTool } from './file-write.js';
14
+ import { globTool } from './glob.js';
15
+ import { grepTool } from './grep.js';
16
+ export { bashTool, delegateToAgentTool, fileEditTool, fileReadTool, fileWriteTool, globTool, grepTool };
17
+ /** Options for {@link registerBuiltInTools}. */
18
+ export interface RegisterBuiltInToolsOptions {
19
+ /**
20
+ * When true, registers `delegate_to_agent` (team orchestration handoff).
21
+ * Default false so standalone agents and `runAgent` do not expose a tool that always errors.
22
+ */
23
+ readonly includeDelegateTool?: boolean;
24
+ }
25
+ /**
26
+ * The ordered list of all built-in tools. Import this when you need to
27
+ * iterate over them without calling `registerBuiltInTools`.
28
+ *
29
+ * The array is typed as `ToolDefinition<unknown>[]` so it can be passed to
30
+ * APIs that accept any ToolDefinition without requiring a union type.
31
+ */
32
+ export declare const BUILT_IN_TOOLS: ToolDefinition<any>[];
33
+ /** All built-ins including `delegate_to_agent` (for team registry setup). */
34
+ export declare const ALL_BUILT_IN_TOOLS_WITH_DELEGATE: ToolDefinition<any>[];
35
+ /**
36
+ * Register all built-in tools with the given registry.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import { ToolRegistry } from '../framework.js'
41
+ * import { registerBuiltInTools } from './built-in/index.js'
42
+ *
43
+ * const registry = new ToolRegistry()
44
+ * registerBuiltInTools(registry)
45
+ * ```
46
+ */
47
+ export declare function registerBuiltInTools(registry: ToolRegistry, options?: RegisterBuiltInToolsOptions): void;
48
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/tool/built-in/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpC,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAEpC,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAA;AAEvG,gDAAgD;AAChD,MAAM,WAAW,2BAA2B;IAC1C;;;OAGG;IACH,QAAQ,CAAC,mBAAmB,CAAC,EAAE,OAAO,CAAA;CACvC;AAED;;;;;;GAMG;AAEH,eAAO,MAAM,cAAc,EAAE,cAAc,CAAC,GAAG,CAAC,EAO/C,CAAA;AAED,6EAA6E;AAC7E,eAAO,MAAM,gCAAgC,EAAE,cAAc,CAAC,GAAG,CAAC,EAGjE,CAAA;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,YAAY,EACtB,OAAO,CAAC,EAAE,2BAA2B,GACpC,IAAI,CAON"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Built-in tool collection.
3
+ *
4
+ * Re-exports every built-in tool and provides a convenience function to
5
+ * register them all with a {@link ToolRegistry} in one call.
6
+ */
7
+ import { bashTool } from './bash.js';
8
+ import { delegateToAgentTool } from './delegate.js';
9
+ import { fileEditTool } from './file-edit.js';
10
+ import { fileReadTool } from './file-read.js';
11
+ import { fileWriteTool } from './file-write.js';
12
+ import { globTool } from './glob.js';
13
+ import { grepTool } from './grep.js';
14
+ export { bashTool, delegateToAgentTool, fileEditTool, fileReadTool, fileWriteTool, globTool, grepTool };
15
+ /**
16
+ * The ordered list of all built-in tools. Import this when you need to
17
+ * iterate over them without calling `registerBuiltInTools`.
18
+ *
19
+ * The array is typed as `ToolDefinition<unknown>[]` so it can be passed to
20
+ * APIs that accept any ToolDefinition without requiring a union type.
21
+ */
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
+ export const BUILT_IN_TOOLS = [
24
+ bashTool,
25
+ fileReadTool,
26
+ fileWriteTool,
27
+ fileEditTool,
28
+ grepTool,
29
+ globTool,
30
+ ];
31
+ /** All built-ins including `delegate_to_agent` (for team registry setup). */
32
+ export const ALL_BUILT_IN_TOOLS_WITH_DELEGATE = [
33
+ ...BUILT_IN_TOOLS,
34
+ delegateToAgentTool,
35
+ ];
36
+ /**
37
+ * Register all built-in tools with the given registry.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * import { ToolRegistry } from '../framework.js'
42
+ * import { registerBuiltInTools } from './built-in/index.js'
43
+ *
44
+ * const registry = new ToolRegistry()
45
+ * registerBuiltInTools(registry)
46
+ * ```
47
+ */
48
+ export function registerBuiltInTools(registry, options) {
49
+ for (const tool of BUILT_IN_TOOLS) {
50
+ registry.register(tool);
51
+ }
52
+ if (options?.includeDelegateTool) {
53
+ registry.register(delegateToAgentTool);
54
+ }
55
+ }
56
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/tool/built-in/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpC,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAEpC,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAA;AAWvG;;;;;;GAMG;AACH,8DAA8D;AAC9D,MAAM,CAAC,MAAM,cAAc,GAA0B;IACnD,QAAQ;IACR,YAAY;IACZ,aAAa;IACb,YAAY;IACZ,QAAQ;IACR,QAAQ;CACT,CAAA;AAED,6EAA6E;AAC7E,MAAM,CAAC,MAAM,gCAAgC,GAA0B;IACrE,GAAG,cAAc;IACjB,mBAAmB;CACpB,CAAA;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,oBAAoB,CAClC,QAAsB,EACtB,OAAqC;IAErC,KAAK,MAAM,IAAI,IAAI,cAAc,EAAE,CAAC;QAClC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;IACzB,CAAC;IACD,IAAI,OAAO,EAAE,mBAAmB,EAAE,CAAC;QACjC,QAAQ,CAAC,QAAQ,CAAC,mBAAmB,CAAC,CAAA;IACxC,CAAC;AACH,CAAC"}
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Parallel tool executor with concurrency control and error isolation.
3
+ *
4
+ * Validates input via Zod schemas, optionally validates tool output via
5
+ * `tool.outputSchema`, enforces a maximum concurrency limit using a lightweight
6
+ * semaphore, and surfaces any execution errors as ToolResult objects rather
7
+ * than thrown exceptions.
8
+ *
9
+ * Types are imported from `../types` to ensure consistency with the rest of
10
+ * the framework.
11
+ */
12
+ import type { ToolResult, ToolUseContext } from '../types.js';
13
+ import { ToolRegistry } from './framework.js';
14
+ export interface ToolExecutorOptions {
15
+ /**
16
+ * Maximum number of tool calls that may run in parallel.
17
+ * Defaults to 4.
18
+ */
19
+ maxConcurrency?: number;
20
+ /**
21
+ * Agent-level default for maximum tool output length in characters.
22
+ * Per-tool `maxOutputChars` takes priority over this value.
23
+ */
24
+ maxToolOutputChars?: number;
25
+ }
26
+ /** Describes one call in a batch. */
27
+ export interface BatchToolCall {
28
+ /** Caller-assigned ID used as the key in the result map. */
29
+ id: string;
30
+ /** Registered tool name. */
31
+ name: string;
32
+ /** Raw (unparsed) input object from the LLM. */
33
+ input: Record<string, unknown>;
34
+ }
35
+ /**
36
+ * Executes tools from a {@link ToolRegistry}, validating input against each
37
+ * tool's Zod schema and enforcing a concurrency limit for batch execution.
38
+ *
39
+ * All errors — including unknown tool names, Zod validation failures, and
40
+ * execution exceptions — are caught and returned as `ToolResult` objects with
41
+ * `isError: true` so the agent runner can forward them to the LLM.
42
+ */
43
+ export declare class ToolExecutor {
44
+ private readonly registry;
45
+ private readonly semaphore;
46
+ private readonly maxToolOutputChars?;
47
+ constructor(registry: ToolRegistry, options?: ToolExecutorOptions);
48
+ /**
49
+ * Execute a single tool by name.
50
+ *
51
+ * Errors are caught and returned as a {@link ToolResult} with
52
+ * `isError: true` — this method itself never rejects.
53
+ *
54
+ * @param toolName The registered tool name.
55
+ * @param input Raw input object (before Zod validation).
56
+ * @param context Execution context forwarded to the tool.
57
+ */
58
+ execute(toolName: string, input: Record<string, unknown>, context: ToolUseContext): Promise<ToolResult>;
59
+ /**
60
+ * Execute multiple tool calls in parallel, honouring the concurrency limit.
61
+ *
62
+ * Returns a `Map` from call ID to result. Every call in `calls` is
63
+ * guaranteed to produce an entry — errors are captured as results.
64
+ *
65
+ * @param calls Array of tool calls to execute.
66
+ * @param context Shared execution context for all calls in this batch.
67
+ */
68
+ executeBatch(calls: BatchToolCall[], context: ToolUseContext): Promise<Map<string, ToolResult>>;
69
+ /**
70
+ * Validate input with the tool's Zod schema, then call `execute`.
71
+ *
72
+ * When `tool.outputSchema` is configured and the tool returned a
73
+ * **non-error** result, `result.data` is validated against the schema
74
+ * before truncation is applied. Error results are forwarded as-is so the
75
+ * LLM still sees the original failure message.
76
+ *
77
+ * Any synchronous or asynchronous error thrown by the tool is caught and
78
+ * turned into an error {@link ToolResult} instead of propagating.
79
+ */
80
+ private runTool;
81
+ /** Run a Zod schema and return a flattened issue string on failure. */
82
+ private runZodSchema;
83
+ /**
84
+ * Apply truncation to a tool result if a character limit is configured.
85
+ * Priority: per-tool `maxOutputChars` > agent-level `maxToolOutputChars`.
86
+ */
87
+ private maybeTruncate;
88
+ /** Construct an error ToolResult. */
89
+ private errorResult;
90
+ }
91
+ /**
92
+ * Truncate tool output to fit within `maxChars`, preserving the head (~70%)
93
+ * and tail (~30%) with a marker indicating how many characters were removed.
94
+ *
95
+ * The marker itself is counted against the budget so the returned string
96
+ * never exceeds `maxChars`. When `maxChars` is too small to fit any
97
+ * content alongside the marker, a marker-only string is returned.
98
+ */
99
+ export declare function truncateToolOutput(data: string, maxChars: number): string;
100
+ //# sourceMappingURL=executor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../src/tool/executor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAE7D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAQ7C,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B;AAED,qCAAqC;AACrC,MAAM,WAAW,aAAa;IAC5B,4DAA4D;IAC5D,EAAE,EAAE,MAAM,CAAA;IACV,4BAA4B;IAC5B,IAAI,EAAE,MAAM,CAAA;IACZ,gDAAgD;IAChD,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAC/B;AAED;;;;;;;GAOG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAc;IACvC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAW;IACrC,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAQ;gBAEhC,QAAQ,EAAE,YAAY,EAAE,OAAO,GAAE,mBAAwB;IAUrE;;;;;;;;;OASG;IACG,OAAO,CACX,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9B,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,UAAU,CAAC;IAsBtB;;;;;;;;OAQG;IACG,YAAY,CAChB,KAAK,EAAE,aAAa,EAAE,EACtB,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IAmBnC;;;;;;;;;;OAUG;YACW,OAAO;IA4CrB,uEAAuE;IACvE,OAAO,CAAC,YAAY;IAcpB;;;OAGG;IACH,OAAO,CAAC,aAAa;IAYrB,qCAAqC;IACrC,OAAO,CAAC,WAAW;CAMpB;AAMD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAoBzE"}
@@ -0,0 +1,184 @@
1
+ /**
2
+ * Parallel tool executor with concurrency control and error isolation.
3
+ *
4
+ * Validates input via Zod schemas, optionally validates tool output via
5
+ * `tool.outputSchema`, enforces a maximum concurrency limit using a lightweight
6
+ * semaphore, and surfaces any execution errors as ToolResult objects rather
7
+ * than thrown exceptions.
8
+ *
9
+ * Types are imported from `../types` to ensure consistency with the rest of
10
+ * the framework.
11
+ */
12
+ import { Semaphore } from '../utils/semaphore.js';
13
+ /**
14
+ * Executes tools from a {@link ToolRegistry}, validating input against each
15
+ * tool's Zod schema and enforcing a concurrency limit for batch execution.
16
+ *
17
+ * All errors — including unknown tool names, Zod validation failures, and
18
+ * execution exceptions — are caught and returned as `ToolResult` objects with
19
+ * `isError: true` so the agent runner can forward them to the LLM.
20
+ */
21
+ export class ToolExecutor {
22
+ registry;
23
+ semaphore;
24
+ maxToolOutputChars;
25
+ constructor(registry, options = {}) {
26
+ this.registry = registry;
27
+ this.semaphore = new Semaphore(options.maxConcurrency ?? 4);
28
+ this.maxToolOutputChars = options.maxToolOutputChars;
29
+ }
30
+ // -------------------------------------------------------------------------
31
+ // Single execution
32
+ // -------------------------------------------------------------------------
33
+ /**
34
+ * Execute a single tool by name.
35
+ *
36
+ * Errors are caught and returned as a {@link ToolResult} with
37
+ * `isError: true` — this method itself never rejects.
38
+ *
39
+ * @param toolName The registered tool name.
40
+ * @param input Raw input object (before Zod validation).
41
+ * @param context Execution context forwarded to the tool.
42
+ */
43
+ async execute(toolName, input, context) {
44
+ const tool = this.registry.get(toolName);
45
+ if (tool === undefined) {
46
+ return this.errorResult(`Tool "${toolName}" is not registered in the ToolRegistry.`);
47
+ }
48
+ // Check abort before even starting
49
+ if (context.abortSignal?.aborted === true) {
50
+ return this.errorResult(`Tool "${toolName}" was aborted before execution began.`);
51
+ }
52
+ return this.runTool(tool, input, context);
53
+ }
54
+ // -------------------------------------------------------------------------
55
+ // Batch execution
56
+ // -------------------------------------------------------------------------
57
+ /**
58
+ * Execute multiple tool calls in parallel, honouring the concurrency limit.
59
+ *
60
+ * Returns a `Map` from call ID to result. Every call in `calls` is
61
+ * guaranteed to produce an entry — errors are captured as results.
62
+ *
63
+ * @param calls Array of tool calls to execute.
64
+ * @param context Shared execution context for all calls in this batch.
65
+ */
66
+ async executeBatch(calls, context) {
67
+ const results = new Map();
68
+ await Promise.all(calls.map(async (call) => {
69
+ const result = await this.semaphore.run(() => this.execute(call.name, call.input, context));
70
+ results.set(call.id, result);
71
+ }));
72
+ return results;
73
+ }
74
+ // -------------------------------------------------------------------------
75
+ // Private helpers
76
+ // -------------------------------------------------------------------------
77
+ /**
78
+ * Validate input with the tool's Zod schema, then call `execute`.
79
+ *
80
+ * When `tool.outputSchema` is configured and the tool returned a
81
+ * **non-error** result, `result.data` is validated against the schema
82
+ * before truncation is applied. Error results are forwarded as-is so the
83
+ * LLM still sees the original failure message.
84
+ *
85
+ * Any synchronous or asynchronous error thrown by the tool is caught and
86
+ * turned into an error {@link ToolResult} instead of propagating.
87
+ */
88
+ async runTool(
89
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
90
+ tool, rawInput, context) {
91
+ // --- Zod validation ---
92
+ const inputParseResult = this.runZodSchema(tool.inputSchema, rawInput);
93
+ if (!inputParseResult.success) {
94
+ return this.errorResult(`Invalid input for tool "${tool.name}":\n${inputParseResult.issuesMessage}`);
95
+ }
96
+ // --- Abort check after parse (parse can be expensive for large inputs) ---
97
+ if (context.abortSignal?.aborted === true) {
98
+ return this.errorResult(`Tool "${tool.name}" was aborted before execution began.`);
99
+ }
100
+ // --- Execute ---
101
+ try {
102
+ const result = await tool.execute(inputParseResult.data, context);
103
+ if (!result.isError && tool.outputSchema) {
104
+ const outputParseResult = this.runZodSchema(tool.outputSchema, result.data);
105
+ if (!outputParseResult.success) {
106
+ return this.errorResult(`Invalid output for tool "${tool.name}":\n${outputParseResult.issuesMessage}`);
107
+ }
108
+ }
109
+ return this.maybeTruncate(tool, result);
110
+ }
111
+ catch (err) {
112
+ const message = err instanceof Error
113
+ ? err.message
114
+ : typeof err === 'string'
115
+ ? err
116
+ : JSON.stringify(err);
117
+ return this.maybeTruncate(tool, this.errorResult(`Tool "${tool.name}" threw an error: ${message}`));
118
+ }
119
+ }
120
+ /** Run a Zod schema and return a flattened issue string on failure. */
121
+ runZodSchema(schema, rawInput) {
122
+ const parseResult = schema.safeParse(rawInput);
123
+ if (!parseResult.success) {
124
+ const issuesMessage = parseResult.error.issues
125
+ .map((issue) => ` • ${issue.path.join('.')}: ${issue.message}`)
126
+ .join('\n');
127
+ return {
128
+ ...parseResult,
129
+ issuesMessage,
130
+ };
131
+ }
132
+ return parseResult;
133
+ }
134
+ /**
135
+ * Apply truncation to a tool result if a character limit is configured.
136
+ * Priority: per-tool `maxOutputChars` > agent-level `maxToolOutputChars`.
137
+ */
138
+ maybeTruncate(
139
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
140
+ tool, result) {
141
+ const maxChars = tool.maxOutputChars ?? this.maxToolOutputChars;
142
+ if (maxChars === undefined || maxChars <= 0 || result.data.length <= maxChars) {
143
+ return result;
144
+ }
145
+ return { ...result, data: truncateToolOutput(result.data, maxChars) };
146
+ }
147
+ /** Construct an error ToolResult. */
148
+ errorResult(message) {
149
+ return {
150
+ data: message,
151
+ isError: true,
152
+ };
153
+ }
154
+ }
155
+ // ---------------------------------------------------------------------------
156
+ // Truncation helper
157
+ // ---------------------------------------------------------------------------
158
+ /**
159
+ * Truncate tool output to fit within `maxChars`, preserving the head (~70%)
160
+ * and tail (~30%) with a marker indicating how many characters were removed.
161
+ *
162
+ * The marker itself is counted against the budget so the returned string
163
+ * never exceeds `maxChars`. When `maxChars` is too small to fit any
164
+ * content alongside the marker, a marker-only string is returned.
165
+ */
166
+ export function truncateToolOutput(data, maxChars) {
167
+ if (data.length <= maxChars)
168
+ return data;
169
+ // Estimate marker length (digit count may shrink after subtracting content,
170
+ // but using data.length gives a safe upper-bound for the digit count).
171
+ const markerTemplate = '\n\n[...truncated characters...]\n\n';
172
+ const markerOverhead = markerTemplate.length + String(data.length).length;
173
+ // When maxChars is too small to fit any content alongside the marker,
174
+ // fall back to a hard slice so the result never exceeds maxChars.
175
+ if (maxChars <= markerOverhead) {
176
+ return data.slice(0, maxChars);
177
+ }
178
+ const available = maxChars - markerOverhead;
179
+ const headChars = Math.floor(available * 0.7);
180
+ const tailChars = available - headChars;
181
+ const truncatedCount = data.length - headChars - tailChars;
182
+ return `${data.slice(0, headChars)}\n\n[...truncated ${truncatedCount} characters...]\n\n${data.slice(-tailChars)}`;
183
+ }
184
+ //# sourceMappingURL=executor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"executor.js","sourceRoot":"","sources":["../../src/tool/executor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAKH,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AA8BjD;;;;;;;GAOG;AACH,MAAM,OAAO,YAAY;IACN,QAAQ,CAAc;IACtB,SAAS,CAAW;IACpB,kBAAkB,CAAS;IAE5C,YAAY,QAAsB,EAAE,UAA+B,EAAE;QACnE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,SAAS,GAAG,IAA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