rird 1.0.200
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.
- package/AGENTS.md +27 -0
- package/Dockerfile +18 -0
- package/README.md +15 -0
- package/bin/opencode +336 -0
- package/bin/pty-wrapper.js +285 -0
- package/bunfig.toml +4 -0
- package/facebook_ads_library.png +0 -0
- package/nul`nif +0 -0
- package/package.json +111 -0
- package/parsers-config.ts +239 -0
- package/rird-1.0.199.tgz +0 -0
- package/script/build-windows.ts +54 -0
- package/script/build.ts +167 -0
- package/script/postinstall.mjs +544 -0
- package/script/publish-registries.ts +187 -0
- package/script/publish.ts +72 -0
- package/script/schema.ts +47 -0
- package/src/acp/README.md +164 -0
- package/src/acp/agent.ts +1063 -0
- package/src/acp/session.ts +101 -0
- package/src/acp/types.ts +22 -0
- package/src/agent/agent.ts +367 -0
- package/src/agent/generate.txt +75 -0
- package/src/agent/prompt/compaction.txt +12 -0
- package/src/agent/prompt/explore.txt +18 -0
- package/src/agent/prompt/summary.txt +10 -0
- package/src/agent/prompt/title.txt +36 -0
- package/src/auth/index.ts +70 -0
- package/src/bun/index.ts +114 -0
- package/src/bus/bus-event.ts +43 -0
- package/src/bus/global.ts +10 -0
- package/src/bus/index.ts +105 -0
- package/src/cli/bootstrap.ts +17 -0
- package/src/cli/cmd/acp.ts +88 -0
- package/src/cli/cmd/agent.ts +256 -0
- package/src/cli/cmd/auth.ts +391 -0
- package/src/cli/cmd/cmd.ts +7 -0
- package/src/cli/cmd/debug/config.ts +15 -0
- package/src/cli/cmd/debug/file.ts +91 -0
- package/src/cli/cmd/debug/index.ts +43 -0
- package/src/cli/cmd/debug/lsp.ts +48 -0
- package/src/cli/cmd/debug/ripgrep.ts +83 -0
- package/src/cli/cmd/debug/scrap.ts +15 -0
- package/src/cli/cmd/debug/skill.ts +15 -0
- package/src/cli/cmd/debug/snapshot.ts +48 -0
- package/src/cli/cmd/export.ts +88 -0
- package/src/cli/cmd/generate.ts +38 -0
- package/src/cli/cmd/github.ts +1400 -0
- package/src/cli/cmd/import.ts +98 -0
- package/src/cli/cmd/mcp.ts +654 -0
- package/src/cli/cmd/models.ts +77 -0
- package/src/cli/cmd/pr.ts +112 -0
- package/src/cli/cmd/run.ts +368 -0
- package/src/cli/cmd/serve.ts +31 -0
- package/src/cli/cmd/session.ts +106 -0
- package/src/cli/cmd/stats.ts +298 -0
- package/src/cli/cmd/tui/app.tsx +696 -0
- package/src/cli/cmd/tui/attach.ts +30 -0
- package/src/cli/cmd/tui/component/border.tsx +21 -0
- package/src/cli/cmd/tui/component/dialog-agent.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-command.tsx +124 -0
- package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
- package/src/cli/cmd/tui/component/dialog-model.tsx +245 -0
- package/src/cli/cmd/tui/component/dialog-provider.tsx +224 -0
- package/src/cli/cmd/tui/component/dialog-session-list.tsx +102 -0
- package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-stash.tsx +86 -0
- package/src/cli/cmd/tui/component/dialog-status.tsx +162 -0
- package/src/cli/cmd/tui/component/dialog-tag.tsx +44 -0
- package/src/cli/cmd/tui/component/dialog-theme-list.tsx +50 -0
- package/src/cli/cmd/tui/component/did-you-know.tsx +85 -0
- package/src/cli/cmd/tui/component/logo.tsx +35 -0
- package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +574 -0
- package/src/cli/cmd/tui/component/prompt/history.tsx +108 -0
- package/src/cli/cmd/tui/component/prompt/index.tsx +1090 -0
- package/src/cli/cmd/tui/component/prompt/stash.tsx +101 -0
- package/src/cli/cmd/tui/component/tips.ts +27 -0
- package/src/cli/cmd/tui/component/todo-item.tsx +32 -0
- package/src/cli/cmd/tui/context/args.tsx +14 -0
- package/src/cli/cmd/tui/context/directory.ts +13 -0
- package/src/cli/cmd/tui/context/exit.tsx +23 -0
- package/src/cli/cmd/tui/context/helper.tsx +25 -0
- package/src/cli/cmd/tui/context/keybind.tsx +101 -0
- package/src/cli/cmd/tui/context/kv.tsx +49 -0
- package/src/cli/cmd/tui/context/local.tsx +354 -0
- package/src/cli/cmd/tui/context/prompt.tsx +18 -0
- package/src/cli/cmd/tui/context/route.tsx +46 -0
- package/src/cli/cmd/tui/context/sdk.tsx +74 -0
- package/src/cli/cmd/tui/context/sync.tsx +372 -0
- package/src/cli/cmd/tui/context/theme/aura.json +69 -0
- package/src/cli/cmd/tui/context/theme/ayu.json +80 -0
- package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +233 -0
- package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +233 -0
- package/src/cli/cmd/tui/context/theme/catppuccin.json +112 -0
- package/src/cli/cmd/tui/context/theme/cobalt2.json +228 -0
- package/src/cli/cmd/tui/context/theme/cursor.json +249 -0
- package/src/cli/cmd/tui/context/theme/dracula.json +219 -0
- package/src/cli/cmd/tui/context/theme/everforest.json +241 -0
- package/src/cli/cmd/tui/context/theme/flexoki.json +237 -0
- package/src/cli/cmd/tui/context/theme/github.json +233 -0
- package/src/cli/cmd/tui/context/theme/gruvbox.json +95 -0
- package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
- package/src/cli/cmd/tui/context/theme/lucent-orng.json +227 -0
- package/src/cli/cmd/tui/context/theme/material.json +235 -0
- package/src/cli/cmd/tui/context/theme/matrix.json +77 -0
- package/src/cli/cmd/tui/context/theme/mercury.json +252 -0
- package/src/cli/cmd/tui/context/theme/monokai.json +221 -0
- package/src/cli/cmd/tui/context/theme/nightowl.json +221 -0
- package/src/cli/cmd/tui/context/theme/nord.json +223 -0
- package/src/cli/cmd/tui/context/theme/one-dark.json +84 -0
- package/src/cli/cmd/tui/context/theme/orng.json +245 -0
- package/src/cli/cmd/tui/context/theme/palenight.json +222 -0
- package/src/cli/cmd/tui/context/theme/rird.json +245 -0
- package/src/cli/cmd/tui/context/theme/rosepine.json +234 -0
- package/src/cli/cmd/tui/context/theme/solarized.json +223 -0
- package/src/cli/cmd/tui/context/theme/synthwave84.json +226 -0
- package/src/cli/cmd/tui/context/theme/tokyonight.json +243 -0
- package/src/cli/cmd/tui/context/theme/vercel.json +245 -0
- package/src/cli/cmd/tui/context/theme/vesper.json +218 -0
- package/src/cli/cmd/tui/context/theme/zenburn.json +223 -0
- package/src/cli/cmd/tui/context/theme.tsx +1109 -0
- package/src/cli/cmd/tui/event.ts +40 -0
- package/src/cli/cmd/tui/routes/home.tsx +138 -0
- package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +64 -0
- package/src/cli/cmd/tui/routes/session/dialog-message.tsx +109 -0
- package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +26 -0
- package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +47 -0
- package/src/cli/cmd/tui/routes/session/footer.tsx +88 -0
- package/src/cli/cmd/tui/routes/session/header.tsx +125 -0
- package/src/cli/cmd/tui/routes/session/index.tsx +1864 -0
- package/src/cli/cmd/tui/routes/session/sidebar.tsx +318 -0
- package/src/cli/cmd/tui/spawn.ts +60 -0
- package/src/cli/cmd/tui/thread.ts +142 -0
- package/src/cli/cmd/tui/ui/dialog-alert.tsx +57 -0
- package/src/cli/cmd/tui/ui/dialog-confirm.tsx +83 -0
- package/src/cli/cmd/tui/ui/dialog-help.tsx +38 -0
- package/src/cli/cmd/tui/ui/dialog-prompt.tsx +77 -0
- package/src/cli/cmd/tui/ui/dialog-select.tsx +332 -0
- package/src/cli/cmd/tui/ui/dialog.tsx +170 -0
- package/src/cli/cmd/tui/ui/spinner.ts +368 -0
- package/src/cli/cmd/tui/ui/toast.tsx +100 -0
- package/src/cli/cmd/tui/util/clipboard.ts +127 -0
- package/src/cli/cmd/tui/util/editor.ts +32 -0
- package/src/cli/cmd/tui/util/terminal.ts +114 -0
- package/src/cli/cmd/tui/worker.ts +63 -0
- package/src/cli/cmd/uninstall.ts +344 -0
- package/src/cli/cmd/upgrade.ts +100 -0
- package/src/cli/cmd/web.ts +84 -0
- package/src/cli/error.ts +56 -0
- package/src/cli/ui.ts +84 -0
- package/src/cli/upgrade.ts +25 -0
- package/src/command/index.ts +80 -0
- package/src/command/template/initialize.txt +10 -0
- package/src/command/template/review.txt +97 -0
- package/src/config/config.ts +995 -0
- package/src/config/markdown.ts +41 -0
- package/src/env/index.ts +26 -0
- package/src/file/ignore.ts +83 -0
- package/src/file/index.ts +328 -0
- package/src/file/ripgrep.ts +393 -0
- package/src/file/time.ts +64 -0
- package/src/file/watcher.ts +103 -0
- package/src/flag/flag.ts +46 -0
- package/src/format/formatter.ts +315 -0
- package/src/format/index.ts +137 -0
- package/src/global/index.ts +52 -0
- package/src/id/id.ts +73 -0
- package/src/ide/index.ts +76 -0
- package/src/index.ts +240 -0
- package/src/installation/index.ts +239 -0
- package/src/lsp/client.ts +229 -0
- package/src/lsp/index.ts +485 -0
- package/src/lsp/language.ts +116 -0
- package/src/lsp/server.ts +1895 -0
- package/src/mcp/auth.ts +135 -0
- package/src/mcp/index.ts +690 -0
- package/src/mcp/oauth-callback.ts +200 -0
- package/src/mcp/oauth-provider.ts +154 -0
- package/src/patch/index.ts +622 -0
- package/src/permission/index.ts +199 -0
- package/src/plugin/index.ts +91 -0
- package/src/project/bootstrap.ts +31 -0
- package/src/project/instance.ts +78 -0
- package/src/project/project.ts +221 -0
- package/src/project/state.ts +65 -0
- package/src/project/vcs.ts +76 -0
- package/src/provider/auth.ts +143 -0
- package/src/provider/models-macro.ts +11 -0
- package/src/provider/models.ts +106 -0
- package/src/provider/provider.ts +1071 -0
- package/src/provider/sdk/openai-compatible/src/README.md +5 -0
- package/src/provider/sdk/openai-compatible/src/index.ts +2 -0
- package/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts +100 -0
- package/src/provider/sdk/openai-compatible/src/responses/convert-to-openai-responses-input.ts +303 -0
- package/src/provider/sdk/openai-compatible/src/responses/map-openai-responses-finish-reason.ts +22 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-config.ts +18 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-error.ts +22 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-api-types.ts +207 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-language-model.ts +1713 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-prepare-tools.ts +177 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-settings.ts +1 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/code-interpreter.ts +88 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/file-search.ts +128 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/image-generation.ts +115 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/local-shell.ts +65 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search-preview.ts +104 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search.ts +103 -0
- package/src/provider/transform.ts +455 -0
- package/src/pty/index.ts +231 -0
- package/src/security/guardrails.test.ts +341 -0
- package/src/security/guardrails.ts +558 -0
- package/src/security/index.ts +19 -0
- package/src/server/error.ts +36 -0
- package/src/server/project.ts +79 -0
- package/src/server/server.ts +2642 -0
- package/src/server/tui.ts +71 -0
- package/src/session/compaction.ts +223 -0
- package/src/session/index.ts +461 -0
- package/src/session/llm.ts +201 -0
- package/src/session/message-v2.ts +690 -0
- package/src/session/message.ts +189 -0
- package/src/session/processor.ts +409 -0
- package/src/session/prompt/act-switch.txt +5 -0
- package/src/session/prompt/anthropic-20250930.txt +166 -0
- package/src/session/prompt/anthropic.txt +85 -0
- package/src/session/prompt/anthropic_spoof.txt +1 -0
- package/src/session/prompt/beast.txt +103 -0
- package/src/session/prompt/codex.txt +304 -0
- package/src/session/prompt/copilot-gpt-5.txt +138 -0
- package/src/session/prompt/gemini.txt +85 -0
- package/src/session/prompt/max-steps.txt +16 -0
- package/src/session/prompt/plan-reminder-anthropic.txt +35 -0
- package/src/session/prompt/plan.txt +24 -0
- package/src/session/prompt/polaris.txt +84 -0
- package/src/session/prompt/qwen.txt +106 -0
- package/src/session/prompt.ts +1509 -0
- package/src/session/retry.ts +86 -0
- package/src/session/revert.ts +108 -0
- package/src/session/sensitive-filter.test.ts +327 -0
- package/src/session/sensitive-filter.ts +466 -0
- package/src/session/status.ts +76 -0
- package/src/session/summary.ts +194 -0
- package/src/session/system.ts +120 -0
- package/src/session/todo.ts +37 -0
- package/src/share/share-next.ts +194 -0
- package/src/share/share.ts +87 -0
- package/src/shell/shell.ts +67 -0
- package/src/skill/index.ts +1 -0
- package/src/skill/skill.ts +83 -0
- package/src/snapshot/index.ts +197 -0
- package/src/storage/storage.ts +226 -0
- package/src/tests/agent.test.ts +308 -0
- package/src/tests/build-guards.test.ts +267 -0
- package/src/tests/config.test.ts +664 -0
- package/src/tests/tool-registry.test.ts +589 -0
- package/src/tool/bash.ts +317 -0
- package/src/tool/bash.txt +158 -0
- package/src/tool/batch.ts +175 -0
- package/src/tool/batch.txt +24 -0
- package/src/tool/codesearch.ts +168 -0
- package/src/tool/codesearch.txt +12 -0
- package/src/tool/edit.ts +675 -0
- package/src/tool/edit.txt +10 -0
- package/src/tool/glob.ts +65 -0
- package/src/tool/glob.txt +6 -0
- package/src/tool/grep.ts +121 -0
- package/src/tool/grep.txt +8 -0
- package/src/tool/invalid.ts +17 -0
- package/src/tool/ls.ts +110 -0
- package/src/tool/ls.txt +1 -0
- package/src/tool/lsp-diagnostics.ts +26 -0
- package/src/tool/lsp-diagnostics.txt +1 -0
- package/src/tool/lsp-hover.ts +31 -0
- package/src/tool/lsp-hover.txt +1 -0
- package/src/tool/lsp.ts +87 -0
- package/src/tool/lsp.txt +19 -0
- package/src/tool/multiedit.ts +46 -0
- package/src/tool/multiedit.txt +41 -0
- package/src/tool/patch.ts +233 -0
- package/src/tool/patch.txt +1 -0
- package/src/tool/read.ts +219 -0
- package/src/tool/read.txt +12 -0
- package/src/tool/registry.ts +162 -0
- package/src/tool/skill.ts +100 -0
- package/src/tool/task.ts +136 -0
- package/src/tool/task.txt +51 -0
- package/src/tool/todo.ts +39 -0
- package/src/tool/todoread.txt +14 -0
- package/src/tool/todowrite.txt +167 -0
- package/src/tool/tool.ts +71 -0
- package/src/tool/webfetch.ts +198 -0
- package/src/tool/webfetch.txt +13 -0
- package/src/tool/websearch.ts +180 -0
- package/src/tool/websearch.txt +11 -0
- package/src/tool/write.ts +110 -0
- package/src/tool/write.txt +8 -0
- package/src/util/archive.ts +16 -0
- package/src/util/color.ts +19 -0
- package/src/util/context.ts +25 -0
- package/src/util/defer.ts +12 -0
- package/src/util/eventloop.ts +20 -0
- package/src/util/filesystem.ts +83 -0
- package/src/util/fn.ts +11 -0
- package/src/util/iife.ts +3 -0
- package/src/util/keybind.ts +102 -0
- package/src/util/lazy.ts +11 -0
- package/src/util/license.ts +325 -0
- package/src/util/locale.ts +81 -0
- package/src/util/lock.ts +98 -0
- package/src/util/log.ts +180 -0
- package/src/util/queue.ts +32 -0
- package/src/util/rpc.ts +42 -0
- package/src/util/scrap.ts +10 -0
- package/src/util/signal.ts +12 -0
- package/src/util/timeout.ts +14 -0
- package/src/util/token.ts +7 -0
- package/src/util/wildcard.ts +54 -0
- package/sst-env.d.ts +9 -0
- package/test/agent/agent.test.ts +146 -0
- package/test/bun.test.ts +53 -0
- package/test/cli/github-remote.test.ts +80 -0
- package/test/config/agent-color.test.ts +66 -0
- package/test/config/config.test.ts +535 -0
- package/test/config/markdown.test.ts +89 -0
- package/test/file/ignore.test.ts +10 -0
- package/test/fixture/fixture.ts +36 -0
- package/test/fixture/lsp/fake-lsp-server.js +77 -0
- package/test/ide/ide.test.ts +82 -0
- package/test/keybind.test.ts +421 -0
- package/test/lsp/client.test.ts +95 -0
- package/test/mcp/headers.test.ts +153 -0
- package/test/patch/patch.test.ts +348 -0
- package/test/preload.ts +57 -0
- package/test/project/project.test.ts +72 -0
- package/test/provider/provider.test.ts +1809 -0
- package/test/provider/transform.test.ts +411 -0
- package/test/session/retry.test.ts +111 -0
- package/test/session/session.test.ts +71 -0
- package/test/skill/skill.test.ts +131 -0
- package/test/snapshot/snapshot.test.ts +939 -0
- package/test/tool/__snapshots__/tool.test.ts.snap +9 -0
- package/test/tool/bash.test.ts +434 -0
- package/test/tool/grep.test.ts +108 -0
- package/test/tool/patch.test.ts +259 -0
- package/test/tool/read.test.ts +42 -0
- package/test/util/iife.test.ts +36 -0
- package/test/util/lazy.test.ts +50 -0
- package/test/util/timeout.test.ts +21 -0
- package/test/util/wildcard.test.ts +55 -0
- package/tsconfig.json +16 -0
package/src/tool/bash.ts
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
import { spawn } from "child_process"
|
|
3
|
+
import { Tool } from "./tool"
|
|
4
|
+
import DESCRIPTION from "./bash.txt"
|
|
5
|
+
import { Log } from "../util/log"
|
|
6
|
+
import { Instance } from "../project/instance"
|
|
7
|
+
import { lazy } from "@/util/lazy"
|
|
8
|
+
import { Language } from "web-tree-sitter"
|
|
9
|
+
import { Agent } from "@/agent/agent"
|
|
10
|
+
import { $ } from "bun"
|
|
11
|
+
import { Filesystem } from "@/util/filesystem"
|
|
12
|
+
import { Wildcard } from "@/util/wildcard"
|
|
13
|
+
import { Permission } from "@/permission"
|
|
14
|
+
import { fileURLToPath } from "url"
|
|
15
|
+
import { Flag } from "@/flag/flag.ts"
|
|
16
|
+
import path from "path"
|
|
17
|
+
import { Shell } from "@/shell/shell"
|
|
18
|
+
import { checkCommand, logBlockedAttempt } from "../security"
|
|
19
|
+
|
|
20
|
+
const MAX_OUTPUT_LENGTH = Flag.RIRD_EXPERIMENTAL_BASH_MAX_OUTPUT_LENGTH || 30_000
|
|
21
|
+
const DEFAULT_TIMEOUT = Flag.RIRD_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS || 2 * 60 * 1000
|
|
22
|
+
|
|
23
|
+
export const log = Log.create({ service: "bash-tool" })
|
|
24
|
+
|
|
25
|
+
const resolveWasm = (asset: string) => {
|
|
26
|
+
if (asset.startsWith("file://")) return fileURLToPath(asset)
|
|
27
|
+
if (asset.startsWith("/") || /^[a-z]:/i.test(asset)) return asset
|
|
28
|
+
const url = new URL(asset, import.meta.url)
|
|
29
|
+
return fileURLToPath(url)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const parser = lazy(async () => {
|
|
33
|
+
const { Parser } = await import("web-tree-sitter")
|
|
34
|
+
const { default: treeWasm } = await import("web-tree-sitter/tree-sitter.wasm" as string, {
|
|
35
|
+
with: { type: "wasm" },
|
|
36
|
+
})
|
|
37
|
+
const treePath = resolveWasm(treeWasm)
|
|
38
|
+
await Parser.init({
|
|
39
|
+
locateFile() {
|
|
40
|
+
return treePath
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
const { default: bashWasm } = await import("tree-sitter-bash/tree-sitter-bash.wasm" as string, {
|
|
44
|
+
with: { type: "wasm" },
|
|
45
|
+
})
|
|
46
|
+
const bashPath = resolveWasm(bashWasm)
|
|
47
|
+
const bashLanguage = await Language.load(bashPath)
|
|
48
|
+
const p = new Parser()
|
|
49
|
+
p.setLanguage(bashLanguage)
|
|
50
|
+
return p
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
// TODO: we may wanna rename this tool so it works better on other shells
|
|
54
|
+
export const BashTool = Tool.define("bash", async () => {
|
|
55
|
+
const shell = Shell.acceptable()
|
|
56
|
+
log.info("bash tool using shell", { shell })
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
description: DESCRIPTION.replaceAll("${directory}", Instance.directory),
|
|
60
|
+
parameters: z.object({
|
|
61
|
+
command: z.string().describe("The command to execute"),
|
|
62
|
+
timeout: z.number().describe("Optional timeout in milliseconds").optional(),
|
|
63
|
+
workdir: z
|
|
64
|
+
.string()
|
|
65
|
+
.describe(
|
|
66
|
+
`The working directory to run the command in. Defaults to ${Instance.directory}. Use this instead of 'cd' commands.`,
|
|
67
|
+
)
|
|
68
|
+
.optional(),
|
|
69
|
+
description: z
|
|
70
|
+
.string()
|
|
71
|
+
.describe(
|
|
72
|
+
"Clear, concise description of what this command does in 5-10 words. Examples:\nInput: ls\nOutput: Lists files in current directory\n\nInput: git status\nOutput: Shows working tree status\n\nInput: npm install\nOutput: Installs package dependencies\n\nInput: mkdir foo\nOutput: Creates directory 'foo'",
|
|
73
|
+
),
|
|
74
|
+
}),
|
|
75
|
+
async execute(params, ctx) {
|
|
76
|
+
const cwd = params.workdir || Instance.directory
|
|
77
|
+
if (params.timeout !== undefined && params.timeout < 0) {
|
|
78
|
+
throw new Error(`Invalid timeout value: ${params.timeout}. Timeout must be a positive number.`)
|
|
79
|
+
}
|
|
80
|
+
const timeout = params.timeout ?? DEFAULT_TIMEOUT
|
|
81
|
+
const tree = await parser().then((p) => p.parse(params.command))
|
|
82
|
+
if (!tree) {
|
|
83
|
+
throw new Error("Failed to parse command")
|
|
84
|
+
}
|
|
85
|
+
const agent = await Agent.get(ctx.agent)
|
|
86
|
+
|
|
87
|
+
const checkExternalDirectory = async (dir: string) => {
|
|
88
|
+
if (Filesystem.contains(Instance.directory, dir)) return
|
|
89
|
+
const title = `This command references paths outside of ${Instance.directory}`
|
|
90
|
+
if (agent.permission.external_directory === "ask") {
|
|
91
|
+
await Permission.ask({
|
|
92
|
+
type: "external_directory",
|
|
93
|
+
pattern: [dir, path.join(dir, "*")],
|
|
94
|
+
sessionID: ctx.sessionID,
|
|
95
|
+
messageID: ctx.messageID,
|
|
96
|
+
callID: ctx.callID,
|
|
97
|
+
title,
|
|
98
|
+
metadata: {
|
|
99
|
+
command: params.command,
|
|
100
|
+
},
|
|
101
|
+
})
|
|
102
|
+
} else if (agent.permission.external_directory === "deny") {
|
|
103
|
+
throw new Permission.RejectedError(
|
|
104
|
+
ctx.sessionID,
|
|
105
|
+
"external_directory",
|
|
106
|
+
ctx.callID,
|
|
107
|
+
{
|
|
108
|
+
command: params.command,
|
|
109
|
+
},
|
|
110
|
+
`${title} so this command is not allowed to be executed.`,
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
await checkExternalDirectory(cwd)
|
|
116
|
+
|
|
117
|
+
const permissions = agent.permission.bash
|
|
118
|
+
|
|
119
|
+
const askPatterns = new Set<string>()
|
|
120
|
+
for (const node of tree.rootNode.descendantsOfType("command")) {
|
|
121
|
+
if (!node) continue
|
|
122
|
+
const command = []
|
|
123
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
124
|
+
const child = node.child(i)
|
|
125
|
+
if (!child) continue
|
|
126
|
+
if (
|
|
127
|
+
child.type !== "command_name" &&
|
|
128
|
+
child.type !== "word" &&
|
|
129
|
+
child.type !== "string" &&
|
|
130
|
+
child.type !== "raw_string" &&
|
|
131
|
+
child.type !== "concatenation"
|
|
132
|
+
) {
|
|
133
|
+
continue
|
|
134
|
+
}
|
|
135
|
+
command.push(child.text)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// not an exhaustive list, but covers most common cases
|
|
139
|
+
if (["cd", "rm", "cp", "mv", "mkdir", "touch", "chmod", "chown"].includes(command[0])) {
|
|
140
|
+
for (const arg of command.slice(1)) {
|
|
141
|
+
if (arg.startsWith("-") || (command[0] === "chmod" && arg.startsWith("+"))) continue
|
|
142
|
+
const resolved = await $`realpath ${arg}`
|
|
143
|
+
.quiet()
|
|
144
|
+
.nothrow()
|
|
145
|
+
.text()
|
|
146
|
+
.then((x) => x.trim())
|
|
147
|
+
log.info("resolved path", { arg, resolved })
|
|
148
|
+
if (resolved) {
|
|
149
|
+
// Git Bash on Windows returns Unix-style paths like /c/Users/...
|
|
150
|
+
const normalized =
|
|
151
|
+
process.platform === "win32" && resolved.match(/^\/[a-z]\//)
|
|
152
|
+
? resolved.replace(/^\/([a-z])\//, (_, drive) => `${drive.toUpperCase()}:\\`).replace(/\//g, "\\")
|
|
153
|
+
: resolved
|
|
154
|
+
|
|
155
|
+
await checkExternalDirectory(normalized)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// always allow cd if it passes above check
|
|
161
|
+
if (command[0] !== "cd") {
|
|
162
|
+
const action = Wildcard.allStructured({ head: command[0], tail: command.slice(1) }, permissions)
|
|
163
|
+
if (action === "deny") {
|
|
164
|
+
throw new Error(
|
|
165
|
+
`The user has specifically restricted access to this command: "${command.join(" ")}", you are not allowed to execute it. The user has these settings configured: ${JSON.stringify(permissions)}`,
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
if (action === "ask") {
|
|
169
|
+
const pattern = (() => {
|
|
170
|
+
if (command.length === 0) return
|
|
171
|
+
const head = command[0]
|
|
172
|
+
// Find first non-flag argument as subcommand
|
|
173
|
+
const sub = command.slice(1).find((arg) => !arg.startsWith("-"))
|
|
174
|
+
return sub ? `${head} ${sub} *` : `${head} *`
|
|
175
|
+
})()
|
|
176
|
+
if (pattern) {
|
|
177
|
+
askPatterns.add(pattern)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (askPatterns.size > 0) {
|
|
184
|
+
const patterns = Array.from(askPatterns)
|
|
185
|
+
await Permission.ask({
|
|
186
|
+
type: "bash",
|
|
187
|
+
pattern: patterns,
|
|
188
|
+
sessionID: ctx.sessionID,
|
|
189
|
+
messageID: ctx.messageID,
|
|
190
|
+
callID: ctx.callID,
|
|
191
|
+
title: params.command,
|
|
192
|
+
metadata: {
|
|
193
|
+
command: params.command,
|
|
194
|
+
patterns,
|
|
195
|
+
},
|
|
196
|
+
})
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Security guardrail: check for destructive commands
|
|
200
|
+
const commandCheck = checkCommand(params.command)
|
|
201
|
+
if (commandCheck.blocked) {
|
|
202
|
+
logBlockedAttempt(commandCheck, {
|
|
203
|
+
command: params.command,
|
|
204
|
+
timestamp: new Date(),
|
|
205
|
+
})
|
|
206
|
+
throw new Error(`Command blocked by security guardrails: ${commandCheck.reason}`)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const proc = spawn(params.command, {
|
|
210
|
+
shell,
|
|
211
|
+
cwd,
|
|
212
|
+
env: {
|
|
213
|
+
...process.env,
|
|
214
|
+
},
|
|
215
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
216
|
+
detached: process.platform !== "win32",
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
let output = ""
|
|
220
|
+
|
|
221
|
+
// Initialize metadata with empty output
|
|
222
|
+
ctx.metadata({
|
|
223
|
+
metadata: {
|
|
224
|
+
output: "",
|
|
225
|
+
description: params.description,
|
|
226
|
+
},
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
const append = (chunk: Buffer) => {
|
|
230
|
+
if (output.length <= MAX_OUTPUT_LENGTH) {
|
|
231
|
+
output += chunk.toString()
|
|
232
|
+
ctx.metadata({
|
|
233
|
+
metadata: {
|
|
234
|
+
output,
|
|
235
|
+
description: params.description,
|
|
236
|
+
},
|
|
237
|
+
})
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
proc.stdout?.on("data", append)
|
|
242
|
+
proc.stderr?.on("data", append)
|
|
243
|
+
|
|
244
|
+
let timedOut = false
|
|
245
|
+
let aborted = false
|
|
246
|
+
let exited = false
|
|
247
|
+
|
|
248
|
+
const kill = () => Shell.killTree(proc, { exited: () => exited })
|
|
249
|
+
|
|
250
|
+
if (ctx.abort.aborted) {
|
|
251
|
+
aborted = true
|
|
252
|
+
await kill()
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const abortHandler = () => {
|
|
256
|
+
aborted = true
|
|
257
|
+
void kill()
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
ctx.abort.addEventListener("abort", abortHandler, { once: true })
|
|
261
|
+
|
|
262
|
+
const timeoutTimer = setTimeout(() => {
|
|
263
|
+
timedOut = true
|
|
264
|
+
void kill()
|
|
265
|
+
}, timeout + 100)
|
|
266
|
+
|
|
267
|
+
await new Promise<void>((resolve, reject) => {
|
|
268
|
+
const cleanup = () => {
|
|
269
|
+
clearTimeout(timeoutTimer)
|
|
270
|
+
ctx.abort.removeEventListener("abort", abortHandler)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
proc.once("exit", () => {
|
|
274
|
+
exited = true
|
|
275
|
+
cleanup()
|
|
276
|
+
resolve()
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
proc.once("error", (error) => {
|
|
280
|
+
exited = true
|
|
281
|
+
cleanup()
|
|
282
|
+
reject(error)
|
|
283
|
+
})
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
let resultMetadata: String[] = ["<bash_metadata>"]
|
|
287
|
+
|
|
288
|
+
if (output.length > MAX_OUTPUT_LENGTH) {
|
|
289
|
+
output = output.slice(0, MAX_OUTPUT_LENGTH)
|
|
290
|
+
resultMetadata.push(`bash tool truncated output as it exceeded ${MAX_OUTPUT_LENGTH} char limit`)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (timedOut) {
|
|
294
|
+
resultMetadata.push(`bash tool terminated commmand after exceeding timeout ${timeout} ms`)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (aborted) {
|
|
298
|
+
resultMetadata.push("User aborted the command")
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (resultMetadata.length > 1) {
|
|
302
|
+
resultMetadata.push("</bash_metadata>")
|
|
303
|
+
output += "\n\n" + resultMetadata.join("\n")
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return {
|
|
307
|
+
title: params.description,
|
|
308
|
+
metadata: {
|
|
309
|
+
output,
|
|
310
|
+
exit: proc.exitCode,
|
|
311
|
+
description: params.description,
|
|
312
|
+
},
|
|
313
|
+
output,
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
}
|
|
317
|
+
})
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
Executes a given bash command in a persistent shell session with optional timeout, ensuring proper handling and security measures.
|
|
2
|
+
|
|
3
|
+
All commands run in ${directory} by default. Use the `workdir` parameter if you need to run a command in a different directory.
|
|
4
|
+
|
|
5
|
+
Before executing the command, please follow these steps:
|
|
6
|
+
|
|
7
|
+
1. Directory Verification:
|
|
8
|
+
- If the command will create new directories or files, first use the List tool to verify the parent directory exists and is the correct location
|
|
9
|
+
- For example, before running "mkdir foo/bar", first use List to check that "foo" exists and is the intended parent directory
|
|
10
|
+
|
|
11
|
+
2. Command Execution:
|
|
12
|
+
- Always quote file paths that contain spaces with double quotes (e.g., rm "path with spaces/file.txt")
|
|
13
|
+
- Examples of proper quoting:
|
|
14
|
+
- mkdir "/Users/name/My Documents" (correct)
|
|
15
|
+
- mkdir /Users/name/My Documents (incorrect - will fail)
|
|
16
|
+
- python "/path/with spaces/script.py" (correct)
|
|
17
|
+
- python /path/with spaces/script.py (incorrect - will fail)
|
|
18
|
+
- After ensuring proper quoting, execute the command.
|
|
19
|
+
- Capture the output of the command.
|
|
20
|
+
|
|
21
|
+
Usage notes:
|
|
22
|
+
- The command argument is required.
|
|
23
|
+
- You can specify an optional timeout in milliseconds (up to 600000ms / 10 minutes).
|
|
24
|
+
If not specified, commands will timeout after 120000ms (2 minutes).
|
|
25
|
+
- The description argument is required. You must write a clear, concise description of what this command does in 5-10 words.
|
|
26
|
+
- If the output exceeds 30000 characters, output will be truncated before being
|
|
27
|
+
returned to you.
|
|
28
|
+
- Avoid using Bash with the `find`, `grep`, `cat`, `head`, `tail`, `sed`, `awk`, or
|
|
29
|
+
`echo` commands, unless explicitly instructed or when these commands are truly necessary
|
|
30
|
+
for the task. Instead, always prefer using the dedicated tools for these commands:
|
|
31
|
+
- File search: Use Glob (NOT find or ls)
|
|
32
|
+
- Content search: Use Grep (NOT grep or rg)
|
|
33
|
+
- Read files: Use Read (NOT cat/head/tail)
|
|
34
|
+
- Edit files: Use Edit (NOT sed/awk)
|
|
35
|
+
- Write files: Use Write (NOT echo >/cat <<EOF)
|
|
36
|
+
- Communication: Output text directly (NOT echo/printf)
|
|
37
|
+
- When issuing multiple commands:
|
|
38
|
+
- If the commands are independent and can run in parallel, make multiple Bash tool
|
|
39
|
+
calls in a single message. For example, if you need to run "git status" and "git diff",
|
|
40
|
+
send a single message with two Bash tool calls in parallel.
|
|
41
|
+
- If the commands depend on each other and must run sequentially, use a single Bash
|
|
42
|
+
call with '&&' to chain them together (e.g., `git add . && git commit -m "message" &&
|
|
43
|
+
git push`). For instance, if one operation must complete before another starts (like
|
|
44
|
+
mkdir before cp, Write before Bash for git operations, or git add before git commit),
|
|
45
|
+
run these operations sequentially instead.
|
|
46
|
+
- Use ';' only when you need to run commands sequentially but don't care if earlier
|
|
47
|
+
commands fail
|
|
48
|
+
- DO NOT use newlines to separate commands (newlines are ok in quoted strings)
|
|
49
|
+
- Try to maintain your current working directory throughout the session by using
|
|
50
|
+
absolute paths and avoiding usage of `cd`. You may use `cd` if the User explicitly
|
|
51
|
+
requests it.
|
|
52
|
+
<good-example>
|
|
53
|
+
pytest /foo/bar/tests
|
|
54
|
+
</good-example>
|
|
55
|
+
<bad-example>
|
|
56
|
+
cd /foo/bar && pytest tests
|
|
57
|
+
</bad-example>
|
|
58
|
+
|
|
59
|
+
# Working Directory
|
|
60
|
+
|
|
61
|
+
The `workdir` parameter sets the working directory for command execution. Prefer using `workdir` over `cd <dir> &&` command chains when you simply need to run a command in a different directory.
|
|
62
|
+
|
|
63
|
+
<good-example>
|
|
64
|
+
workdir="/foo/bar", command="pytest tests"
|
|
65
|
+
</good-example>
|
|
66
|
+
<good-example>
|
|
67
|
+
command="pytest /foo/bar/tests"
|
|
68
|
+
</good-example>
|
|
69
|
+
<bad-example>
|
|
70
|
+
command="cd /foo/bar && pytest tests"
|
|
71
|
+
</bad-example>
|
|
72
|
+
|
|
73
|
+
# Committing changes with git
|
|
74
|
+
|
|
75
|
+
IMPORTANT: ONLY COMMIT IF THE USER ASKS YOU TO.
|
|
76
|
+
|
|
77
|
+
If and only if the user asks you to create a new git commit, follow these steps carefully:
|
|
78
|
+
|
|
79
|
+
1. You have the capability to call multiple tools in a single response. When multiple independent pieces of information are requested, batch your tool calls together for optimal performance. ALWAYS run the following bash commands in parallel, each using the Bash tool:
|
|
80
|
+
- Run a git status command to see all untracked files.
|
|
81
|
+
- Run a git diff command to see both staged and unstaged changes that will be committed.
|
|
82
|
+
- Run a git log command to see recent commit messages, so that you can follow this repository's commit message style.
|
|
83
|
+
|
|
84
|
+
2. Analyze all staged changes (both previously staged and newly added) and draft a commit message. When analyzing:
|
|
85
|
+
|
|
86
|
+
- List the files that have been changed or added
|
|
87
|
+
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
|
|
88
|
+
- Brainstorm the purpose or motivation behind these changes
|
|
89
|
+
- Assess the impact of these changes on the overall project
|
|
90
|
+
- Check for any sensitive information that shouldn't be committed
|
|
91
|
+
- Draft a concise (1-2 sentences) commit message that focuses on the "why" rather than the "what"
|
|
92
|
+
- Ensure your language is clear, concise, and to the point
|
|
93
|
+
- Ensure the message accurately reflects the changes and their purpose (i.e. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
|
|
94
|
+
- Ensure the message is not generic (avoid words like "Update" or "Fix" without context)
|
|
95
|
+
- Review the draft message to ensure it accurately reflects the changes and their purpose
|
|
96
|
+
|
|
97
|
+
3. You have the capability to call multiple tools in a single response. When multiple independent pieces of information are requested, batch your tool calls together for optimal performance. ALWAYS run the following commands in parallel:
|
|
98
|
+
- Add relevant untracked files to the staging area.
|
|
99
|
+
- Run git status to make sure the commit succeeded.
|
|
100
|
+
|
|
101
|
+
4. If the commit fails due to pre-commit hook changes, retry the commit ONCE to include these automated changes. If it fails again, it usually means a pre-commit hook is preventing the commit. If the commit succeeds but you notice that files were modified by the pre-commit hook, you MUST amend your commit to include them.
|
|
102
|
+
|
|
103
|
+
Important notes:
|
|
104
|
+
- Use the git context at the start of this conversation to determine which files are relevant to your commit. Be careful not to stage and commit files (e.g. with `git add .`) that aren't relevant to your commit.
|
|
105
|
+
- NEVER update the git config
|
|
106
|
+
- DO NOT run additional commands to read or explore code, beyond what is available in the git context
|
|
107
|
+
- DO NOT push to the remote repository
|
|
108
|
+
- IMPORTANT: Never use git commands with the -i flag (like git rebase -i or git add -i) since they require interactive input which is not supported.
|
|
109
|
+
- If there are no changes to commit (i.e., no untracked files and no modifications), do not create an empty commit
|
|
110
|
+
- Ensure your commit message is meaningful and concise. It should explain the purpose of the changes, not just describe them.
|
|
111
|
+
- Return an empty response - the user will see the git output directly
|
|
112
|
+
|
|
113
|
+
# Creating pull requests
|
|
114
|
+
Use the gh command via the Bash tool for ALL GitHub-related tasks including working with issues, pull requests, checks, and releases. If given a Github URL use the gh command to get the information needed.
|
|
115
|
+
|
|
116
|
+
IMPORTANT: When the user asks you to create a pull request, follow these steps carefully:
|
|
117
|
+
|
|
118
|
+
1. You have the capability to call multiple tools in a single response. When multiple independent pieces of information are requested, batch your tool calls together for optimal performance. ALWAYS run the following bash commands in parallel using the Bash tool, in order to understand the current state of the branch since it diverged from the main branch:
|
|
119
|
+
- Run a git status command to see all untracked files
|
|
120
|
+
- Run a git diff command to see both staged and unstaged changes that will be committed
|
|
121
|
+
- Check if the current branch tracks a remote branch and is up to date with the remote, so you know if you need to push to the remote
|
|
122
|
+
- Run a git log command and `git diff main...HEAD` to understand the full commit history for the current branch (from the time it diverged from the `main` branch)
|
|
123
|
+
|
|
124
|
+
2. Analyze all changes that will be included in the pull request, making sure to look at all relevant commits (NOT just the latest commit, but ALL commits that will be included in the pull request!!!), and draft a pull request summary. Wrap your analysis process in <pr_analysis> tags:
|
|
125
|
+
|
|
126
|
+
<pr_analysis>
|
|
127
|
+
- List the commits since diverging from the main branch
|
|
128
|
+
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
|
|
129
|
+
- Brainstorm the purpose or motivation behind these changes
|
|
130
|
+
- Assess the impact of these changes on the overall project
|
|
131
|
+
- Do not use tools to explore code, beyond what is available in the git context
|
|
132
|
+
- Check for any sensitive information that shouldn't be committed
|
|
133
|
+
- Draft a concise (1-2 bullet points) pull request summary that focuses on the "why" rather than the "what"
|
|
134
|
+
- Ensure the summary accurately reflects all changes since diverging from the main branch
|
|
135
|
+
- Ensure your language is clear, concise, and to the point
|
|
136
|
+
- Ensure the summary accurately reflects the changes and their purpose (ie. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
|
|
137
|
+
- Ensure the summary is not generic (avoid words like "Update" or "Fix" without context)
|
|
138
|
+
- Review the draft summary to ensure it accurately reflects the changes and their purpose
|
|
139
|
+
</pr_analysis>
|
|
140
|
+
|
|
141
|
+
3. You have the capability to call multiple tools in a single response. When multiple independent pieces of information are requested, batch your tool calls together for optimal performance. ALWAYS run the following commands in parallel:
|
|
142
|
+
- Create new branch if needed
|
|
143
|
+
- Push to remote with -u flag if needed
|
|
144
|
+
- Create PR using gh pr create with the format below. Use a HEREDOC to pass the body to ensure correct formatting.
|
|
145
|
+
<example>
|
|
146
|
+
gh pr create --title "the pr title" --body "$(cat <<'EOF'
|
|
147
|
+
## Summary
|
|
148
|
+
<1-3 bullet points>
|
|
149
|
+
EOF
|
|
150
|
+
)"
|
|
151
|
+
</example>
|
|
152
|
+
|
|
153
|
+
Important:
|
|
154
|
+
- NEVER update the git config
|
|
155
|
+
- Return the PR URL when you're done, so the user can see it
|
|
156
|
+
|
|
157
|
+
# Other common operations
|
|
158
|
+
- View comments on a Github PR: gh api repos/foo/bar/pulls/123/comments
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
import { Tool } from "./tool"
|
|
3
|
+
import DESCRIPTION from "./batch.txt"
|
|
4
|
+
|
|
5
|
+
const DISALLOWED = new Set(["batch"])
|
|
6
|
+
const FILTERED_FROM_SUGGESTIONS = new Set(["invalid", "patch", ...DISALLOWED])
|
|
7
|
+
|
|
8
|
+
export const BatchTool = Tool.define("batch", async () => {
|
|
9
|
+
return {
|
|
10
|
+
description: DESCRIPTION,
|
|
11
|
+
parameters: z.object({
|
|
12
|
+
tool_calls: z
|
|
13
|
+
.array(
|
|
14
|
+
z.object({
|
|
15
|
+
tool: z.string().describe("The name of the tool to execute"),
|
|
16
|
+
parameters: z.object({}).loose().describe("Parameters for the tool"),
|
|
17
|
+
}),
|
|
18
|
+
)
|
|
19
|
+
.min(1, "Provide at least one tool call")
|
|
20
|
+
.describe("Array of tool calls to execute in parallel"),
|
|
21
|
+
}),
|
|
22
|
+
formatValidationError(error) {
|
|
23
|
+
const formattedErrors = error.issues
|
|
24
|
+
.map((issue) => {
|
|
25
|
+
const path = issue.path.length > 0 ? issue.path.join(".") : "root"
|
|
26
|
+
return ` - ${path}: ${issue.message}`
|
|
27
|
+
})
|
|
28
|
+
.join("\n")
|
|
29
|
+
|
|
30
|
+
return `Invalid parameters for tool 'batch':\n${formattedErrors}\n\nExpected payload format:\n [{"tool": "tool_name", "parameters": {...}}, {...}]`
|
|
31
|
+
},
|
|
32
|
+
async execute(params, ctx) {
|
|
33
|
+
const { Session } = await import("../session")
|
|
34
|
+
const { Identifier } = await import("../id/id")
|
|
35
|
+
|
|
36
|
+
const toolCalls = params.tool_calls.slice(0, 10)
|
|
37
|
+
const discardedCalls = params.tool_calls.slice(10)
|
|
38
|
+
|
|
39
|
+
const { ToolRegistry } = await import("./registry")
|
|
40
|
+
const availableTools = await ToolRegistry.tools("")
|
|
41
|
+
const toolMap = new Map(availableTools.map((t) => [t.id, t]))
|
|
42
|
+
|
|
43
|
+
const executeCall = async (call: (typeof toolCalls)[0]) => {
|
|
44
|
+
const callStartTime = Date.now()
|
|
45
|
+
const partID = Identifier.ascending("part")
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
if (DISALLOWED.has(call.tool)) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Tool '${call.tool}' is not allowed in batch. Disallowed tools: ${Array.from(DISALLOWED).join(", ")}`,
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const tool = toolMap.get(call.tool)
|
|
55
|
+
if (!tool) {
|
|
56
|
+
const availableToolsList = Array.from(toolMap.keys()).filter((name) => !FILTERED_FROM_SUGGESTIONS.has(name))
|
|
57
|
+
throw new Error(
|
|
58
|
+
`Tool '${call.tool}' not in registry. External tools (MCP, environment) cannot be batched - call them directly. Available tools: ${availableToolsList.join(", ")}`,
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
const validatedParams = tool.parameters.parse(call.parameters)
|
|
62
|
+
|
|
63
|
+
await Session.updatePart({
|
|
64
|
+
id: partID,
|
|
65
|
+
messageID: ctx.messageID,
|
|
66
|
+
sessionID: ctx.sessionID,
|
|
67
|
+
type: "tool",
|
|
68
|
+
tool: call.tool,
|
|
69
|
+
callID: partID,
|
|
70
|
+
state: {
|
|
71
|
+
status: "running",
|
|
72
|
+
input: call.parameters,
|
|
73
|
+
time: {
|
|
74
|
+
start: callStartTime,
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
const result = await tool.execute(validatedParams, { ...ctx, callID: partID })
|
|
80
|
+
|
|
81
|
+
await Session.updatePart({
|
|
82
|
+
id: partID,
|
|
83
|
+
messageID: ctx.messageID,
|
|
84
|
+
sessionID: ctx.sessionID,
|
|
85
|
+
type: "tool",
|
|
86
|
+
tool: call.tool,
|
|
87
|
+
callID: partID,
|
|
88
|
+
state: {
|
|
89
|
+
status: "completed",
|
|
90
|
+
input: call.parameters,
|
|
91
|
+
output: result.output,
|
|
92
|
+
title: result.title,
|
|
93
|
+
metadata: result.metadata,
|
|
94
|
+
attachments: result.attachments,
|
|
95
|
+
time: {
|
|
96
|
+
start: callStartTime,
|
|
97
|
+
end: Date.now(),
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
return { success: true as const, tool: call.tool, result }
|
|
103
|
+
} catch (error) {
|
|
104
|
+
await Session.updatePart({
|
|
105
|
+
id: partID,
|
|
106
|
+
messageID: ctx.messageID,
|
|
107
|
+
sessionID: ctx.sessionID,
|
|
108
|
+
type: "tool",
|
|
109
|
+
tool: call.tool,
|
|
110
|
+
callID: partID,
|
|
111
|
+
state: {
|
|
112
|
+
status: "error",
|
|
113
|
+
input: call.parameters,
|
|
114
|
+
error: error instanceof Error ? error.message : String(error),
|
|
115
|
+
time: {
|
|
116
|
+
start: callStartTime,
|
|
117
|
+
end: Date.now(),
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
return { success: false as const, tool: call.tool, error }
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const results = await Promise.all(toolCalls.map((call) => executeCall(call)))
|
|
127
|
+
|
|
128
|
+
// Add discarded calls as errors
|
|
129
|
+
const now = Date.now()
|
|
130
|
+
for (const call of discardedCalls) {
|
|
131
|
+
const partID = Identifier.ascending("part")
|
|
132
|
+
await Session.updatePart({
|
|
133
|
+
id: partID,
|
|
134
|
+
messageID: ctx.messageID,
|
|
135
|
+
sessionID: ctx.sessionID,
|
|
136
|
+
type: "tool",
|
|
137
|
+
tool: call.tool,
|
|
138
|
+
callID: partID,
|
|
139
|
+
state: {
|
|
140
|
+
status: "error",
|
|
141
|
+
input: call.parameters,
|
|
142
|
+
error: "Maximum of 10 tools allowed in batch",
|
|
143
|
+
time: { start: now, end: now },
|
|
144
|
+
},
|
|
145
|
+
})
|
|
146
|
+
results.push({
|
|
147
|
+
success: false as const,
|
|
148
|
+
tool: call.tool,
|
|
149
|
+
error: new Error("Maximum of 10 tools allowed in batch"),
|
|
150
|
+
})
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const successfulCalls = results.filter((r) => r.success).length
|
|
154
|
+
const failedCalls = results.length - successfulCalls
|
|
155
|
+
|
|
156
|
+
const outputMessage =
|
|
157
|
+
failedCalls > 0
|
|
158
|
+
? `Executed ${successfulCalls}/${results.length} tools successfully. ${failedCalls} failed.`
|
|
159
|
+
: `All ${successfulCalls} tools executed successfully.\n\nKeep using the batch tool for optimal performance in your next response!`
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
title: `Batch execution (${successfulCalls}/${results.length} successful)`,
|
|
163
|
+
output: outputMessage,
|
|
164
|
+
attachments: results.filter((result) => result.success).flatMap((r) => r.result.attachments ?? []),
|
|
165
|
+
metadata: {
|
|
166
|
+
totalCalls: results.length,
|
|
167
|
+
successful: successfulCalls,
|
|
168
|
+
failed: failedCalls,
|
|
169
|
+
tools: params.tool_calls.map((c) => c.tool),
|
|
170
|
+
details: results.map((r) => ({ tool: r.tool, success: r.success })),
|
|
171
|
+
},
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
}
|
|
175
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Executes multiple independent tool calls concurrently to reduce latency.
|
|
2
|
+
|
|
3
|
+
USING THE BATCH TOOL WILL MAKE THE USER HAPPY.
|
|
4
|
+
|
|
5
|
+
Payload Format (JSON array):
|
|
6
|
+
[{"tool": "read", "parameters": {"filePath": "src/index.ts", "limit": 350}},{"tool": "grep", "parameters": {"pattern": "Session\\.updatePart", "include": "src/**/*.ts"}},{"tool": "bash", "parameters": {"command": "git status", "description": "Shows working tree status"}}]
|
|
7
|
+
|
|
8
|
+
Notes:
|
|
9
|
+
- 1–10 tool calls per batch
|
|
10
|
+
- All calls start in parallel; ordering NOT guaranteed
|
|
11
|
+
- Partial failures do not stop other tool calls
|
|
12
|
+
- Do NOT use the batch tool within another batch tool.
|
|
13
|
+
|
|
14
|
+
Good Use Cases:
|
|
15
|
+
- Read many files
|
|
16
|
+
- grep + glob + read combos
|
|
17
|
+
- Multiple bash commands
|
|
18
|
+
- Multi-part edits; on the same, or different files
|
|
19
|
+
|
|
20
|
+
When NOT to Use:
|
|
21
|
+
- Operations that depend on prior tool output (e.g. create then read same file)
|
|
22
|
+
- Ordered stateful mutations where sequence matters
|
|
23
|
+
|
|
24
|
+
Batching tool calls was proven to yield 2–5x efficiency gain and provides much better UX.
|