@hyperwindmill/caretaker-cli 0.11.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.
- package/LICENSE +110 -0
- package/README.md +25 -0
- package/dist/agents/dispatch.d.ts +35 -0
- package/dist/agents/dispatch.d.ts.map +1 -0
- package/dist/agents/dispatch.js +108 -0
- package/dist/agents/sync.d.ts +10 -0
- package/dist/agents/sync.d.ts.map +1 -0
- package/dist/agents/sync.js +95 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +50 -0
- package/dist/cli/run.d.ts +11 -0
- package/dist/cli/run.d.ts.map +1 -0
- package/dist/cli/run.js +89 -0
- package/dist/cli/web/fs.d.ts +3 -0
- package/dist/cli/web/fs.d.ts.map +1 -0
- package/dist/cli/web/fs.js +35 -0
- package/dist/cli/web/mcp_bridge.d.ts +7 -0
- package/dist/cli/web/mcp_bridge.d.ts.map +1 -0
- package/dist/cli/web/mcp_bridge.js +86 -0
- package/dist/cli/web/scheduler/heartbeat.d.ts +16 -0
- package/dist/cli/web/scheduler/heartbeat.d.ts.map +1 -0
- package/dist/cli/web/scheduler/heartbeat.js +181 -0
- package/dist/cli/web/scheduler/locks.d.ts +8 -0
- package/dist/cli/web/scheduler/locks.d.ts.map +1 -0
- package/dist/cli/web/scheduler/locks.js +7 -0
- package/dist/cli/web/scheduler/logs.d.ts +21 -0
- package/dist/cli/web/scheduler/logs.d.ts.map +1 -0
- package/dist/cli/web/scheduler/logs.js +62 -0
- package/dist/cli/web/scheduler/strategy.d.ts +6 -0
- package/dist/cli/web/scheduler/strategy.d.ts.map +1 -0
- package/dist/cli/web/scheduler/strategy.js +1 -0
- package/dist/cli/web/scheduler/task_review.d.ts +18 -0
- package/dist/cli/web/scheduler/task_review.d.ts.map +1 -0
- package/dist/cli/web/scheduler/task_review.js +77 -0
- package/dist/cli/web/scheduler/task_roles.d.ts +22 -0
- package/dist/cli/web/scheduler/task_roles.d.ts.map +1 -0
- package/dist/cli/web/scheduler/task_roles.js +70 -0
- package/dist/cli/web/scheduler/task_strategy.d.ts +2 -0
- package/dist/cli/web/scheduler/task_strategy.d.ts.map +1 -0
- package/dist/cli/web/scheduler/task_strategy.js +437 -0
- package/dist/cli/web/scheduler/telegram.d.ts +7 -0
- package/dist/cli/web/scheduler/telegram.d.ts.map +1 -0
- package/dist/cli/web/scheduler/telegram.js +393 -0
- package/dist/cli/web/scheduler.d.ts +15 -0
- package/dist/cli/web/scheduler.d.ts.map +1 -0
- package/dist/cli/web/scheduler.js +60 -0
- package/dist/cli/web/server.d.ts +38 -0
- package/dist/cli/web/server.d.ts.map +1 -0
- package/dist/cli/web/server.js +970 -0
- package/dist/commands/loader.d.ts +37 -0
- package/dist/commands/loader.d.ts.map +1 -0
- package/dist/commands/loader.js +110 -0
- package/dist/harness/claude_code_runner.d.ts +47 -0
- package/dist/harness/claude_code_runner.d.ts.map +1 -0
- package/dist/harness/claude_code_runner.js +325 -0
- package/dist/harness/claude_code_stream.d.ts +32 -0
- package/dist/harness/claude_code_stream.d.ts.map +1 -0
- package/dist/harness/claude_code_stream.js +121 -0
- package/dist/harness/context_files.d.ts +23 -0
- package/dist/harness/context_files.d.ts.map +1 -0
- package/dist/harness/context_files.js +126 -0
- package/dist/harness/history.d.ts +4 -0
- package/dist/harness/history.d.ts.map +1 -0
- package/dist/harness/history.js +120 -0
- package/dist/harness/index.d.ts +8 -0
- package/dist/harness/index.d.ts.map +1 -0
- package/dist/harness/index.js +8 -0
- package/dist/harness/loop.d.ts +73 -0
- package/dist/harness/loop.d.ts.map +1 -0
- package/dist/harness/loop.js +373 -0
- package/dist/harness/model_limits.d.ts +25 -0
- package/dist/harness/model_limits.d.ts.map +1 -0
- package/dist/harness/model_limits.js +67 -0
- package/dist/harness/models.d.ts +9 -0
- package/dist/harness/models.d.ts.map +1 -0
- package/dist/harness/models.js +38 -0
- package/dist/harness/prelude.d.ts +21 -0
- package/dist/harness/prelude.d.ts.map +1 -0
- package/dist/harness/prelude.js +41 -0
- package/dist/harness/provider.d.ts +87 -0
- package/dist/harness/provider.d.ts.map +1 -0
- package/dist/harness/provider.js +140 -0
- package/dist/harness/runtime_info.d.ts +7 -0
- package/dist/harness/runtime_info.d.ts.map +1 -0
- package/dist/harness/runtime_info.js +15 -0
- package/dist/harness/think_tag_splitter.d.ts +33 -0
- package/dist/harness/think_tag_splitter.d.ts.map +1 -0
- package/dist/harness/think_tag_splitter.js +70 -0
- package/dist/harness/title.d.ts +17 -0
- package/dist/harness/title.d.ts.map +1 -0
- package/dist/harness/title.js +75 -0
- package/dist/harness/tools/builtin/bash.d.ts +3 -0
- package/dist/harness/tools/builtin/bash.d.ts.map +1 -0
- package/dist/harness/tools/builtin/bash.js +137 -0
- package/dist/harness/tools/builtin/edit.d.ts +6 -0
- package/dist/harness/tools/builtin/edit.d.ts.map +1 -0
- package/dist/harness/tools/builtin/edit.js +85 -0
- package/dist/harness/tools/builtin/fetch.d.ts +3 -0
- package/dist/harness/tools/builtin/fetch.d.ts.map +1 -0
- package/dist/harness/tools/builtin/fetch.js +75 -0
- package/dist/harness/tools/builtin/get_agent_context.d.ts +3 -0
- package/dist/harness/tools/builtin/get_agent_context.d.ts.map +1 -0
- package/dist/harness/tools/builtin/get_agent_context.js +76 -0
- package/dist/harness/tools/builtin/glob.d.ts +3 -0
- package/dist/harness/tools/builtin/glob.d.ts.map +1 -0
- package/dist/harness/tools/builtin/glob.js +52 -0
- package/dist/harness/tools/builtin/grep.d.ts +3 -0
- package/dist/harness/tools/builtin/grep.d.ts.map +1 -0
- package/dist/harness/tools/builtin/grep.js +126 -0
- package/dist/harness/tools/builtin/index.d.ts +23 -0
- package/dist/harness/tools/builtin/index.d.ts.map +1 -0
- package/dist/harness/tools/builtin/index.js +74 -0
- package/dist/harness/tools/builtin/invoke_agent.d.ts +3 -0
- package/dist/harness/tools/builtin/invoke_agent.d.ts.map +1 -0
- package/dist/harness/tools/builtin/invoke_agent.js +106 -0
- package/dist/harness/tools/builtin/invoke_command.d.ts +3 -0
- package/dist/harness/tools/builtin/invoke_command.d.ts.map +1 -0
- package/dist/harness/tools/builtin/invoke_command.js +42 -0
- package/dist/harness/tools/builtin/list_agents.d.ts +3 -0
- package/dist/harness/tools/builtin/list_agents.d.ts.map +1 -0
- package/dist/harness/tools/builtin/list_agents.js +32 -0
- package/dist/harness/tools/builtin/list_commands.d.ts +3 -0
- package/dist/harness/tools/builtin/list_commands.d.ts.map +1 -0
- package/dist/harness/tools/builtin/list_commands.js +27 -0
- package/dist/harness/tools/builtin/list_skills.d.ts +3 -0
- package/dist/harness/tools/builtin/list_skills.d.ts.map +1 -0
- package/dist/harness/tools/builtin/list_skills.js +24 -0
- package/dist/harness/tools/builtin/multiedit.d.ts +3 -0
- package/dist/harness/tools/builtin/multiedit.d.ts.map +1 -0
- package/dist/harness/tools/builtin/multiedit.js +88 -0
- package/dist/harness/tools/builtin/read_attachment.d.ts +3 -0
- package/dist/harness/tools/builtin/read_attachment.d.ts.map +1 -0
- package/dist/harness/tools/builtin/read_attachment.js +128 -0
- package/dist/harness/tools/builtin/read_document.d.ts +37 -0
- package/dist/harness/tools/builtin/read_document.d.ts.map +1 -0
- package/dist/harness/tools/builtin/read_document.js +238 -0
- package/dist/harness/tools/builtin/read_file.d.ts +3 -0
- package/dist/harness/tools/builtin/read_file.d.ts.map +1 -0
- package/dist/harness/tools/builtin/read_file.js +141 -0
- package/dist/harness/tools/builtin/read_image.d.ts +3 -0
- package/dist/harness/tools/builtin/read_image.d.ts.map +1 -0
- package/dist/harness/tools/builtin/read_image.js +80 -0
- package/dist/harness/tools/builtin/read_skill.d.ts +3 -0
- package/dist/harness/tools/builtin/read_skill.d.ts.map +1 -0
- package/dist/harness/tools/builtin/read_skill.js +30 -0
- package/dist/harness/tools/builtin/shell-env.d.ts +55 -0
- package/dist/harness/tools/builtin/shell-env.d.ts.map +1 -0
- package/dist/harness/tools/builtin/shell-env.js +181 -0
- package/dist/harness/tools/builtin/task_tools.d.ts +21 -0
- package/dist/harness/tools/builtin/task_tools.d.ts.map +1 -0
- package/dist/harness/tools/builtin/task_tools.js +708 -0
- package/dist/harness/tools/builtin/write.d.ts +3 -0
- package/dist/harness/tools/builtin/write.d.ts.map +1 -0
- package/dist/harness/tools/builtin/write.js +56 -0
- package/dist/harness/tools/index.d.ts +6 -0
- package/dist/harness/tools/index.d.ts.map +1 -0
- package/dist/harness/tools/index.js +7 -0
- package/dist/harness/tools/instance.d.ts +3 -0
- package/dist/harness/tools/instance.d.ts.map +1 -0
- package/dist/harness/tools/instance.js +19 -0
- package/dist/harness/tools/registry.d.ts +21 -0
- package/dist/harness/tools/registry.d.ts.map +1 -0
- package/dist/harness/tools/registry.js +33 -0
- package/dist/harness/tools/resolve.d.ts +5 -0
- package/dist/harness/tools/resolve.d.ts.map +1 -0
- package/dist/harness/tools/resolve.js +60 -0
- package/dist/harness/tools/sandbox.d.ts +6 -0
- package/dist/harness/tools/sandbox.d.ts.map +1 -0
- package/dist/harness/tools/sandbox.js +23 -0
- package/dist/harness/tools/types.d.ts +80 -0
- package/dist/harness/tools/types.d.ts.map +1 -0
- package/dist/harness/tools/types.js +16 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +46 -0
- package/dist/lib/encryption.d.ts +24 -0
- package/dist/lib/encryption.d.ts.map +1 -0
- package/dist/lib/encryption.js +163 -0
- package/dist/lib/open_url.d.ts +9 -0
- package/dist/lib/open_url.d.ts.map +1 -0
- package/dist/lib/open_url.js +23 -0
- package/dist/lib/task_git.d.ts +11 -0
- package/dist/lib/task_git.d.ts.map +1 -0
- package/dist/lib/task_git.js +100 -0
- package/dist/mcp/adapter.d.ts +9 -0
- package/dist/mcp/adapter.d.ts.map +1 -0
- package/dist/mcp/adapter.js +74 -0
- package/dist/mcp/client.d.ts +50 -0
- package/dist/mcp/client.d.ts.map +1 -0
- package/dist/mcp/client.js +243 -0
- package/dist/mcp/oauth.d.ts +30 -0
- package/dist/mcp/oauth.d.ts.map +1 -0
- package/dist/mcp/oauth.js +171 -0
- package/dist/mcp/oauth_callback.d.ts +9 -0
- package/dist/mcp/oauth_callback.d.ts.map +1 -0
- package/dist/mcp/oauth_callback.js +51 -0
- package/dist/mcp/oauth_store.d.ts +22 -0
- package/dist/mcp/oauth_store.d.ts.map +1 -0
- package/dist/mcp/oauth_store.js +40 -0
- package/dist/mcp/server_manager.d.ts +49 -0
- package/dist/mcp/server_manager.d.ts.map +1 -0
- package/dist/mcp/server_manager.js +271 -0
- package/dist/plugins/fetchers/git.d.ts +67 -0
- package/dist/plugins/fetchers/git.d.ts.map +1 -0
- package/dist/plugins/fetchers/git.js +130 -0
- package/dist/plugins/fetchers/path.d.ts +4 -0
- package/dist/plugins/fetchers/path.d.ts.map +1 -0
- package/dist/plugins/fetchers/path.js +22 -0
- package/dist/plugins/loader.d.ts +31 -0
- package/dist/plugins/loader.d.ts.map +1 -0
- package/dist/plugins/loader.js +146 -0
- package/dist/plugins/manifest.d.ts +56 -0
- package/dist/plugins/manifest.d.ts.map +1 -0
- package/dist/plugins/manifest.js +389 -0
- package/dist/plugins/refresh_on_start.d.ts +7 -0
- package/dist/plugins/refresh_on_start.d.ts.map +1 -0
- package/dist/plugins/refresh_on_start.js +35 -0
- package/dist/plugins/source_manager.d.ts +27 -0
- package/dist/plugins/source_manager.d.ts.map +1 -0
- package/dist/plugins/source_manager.js +211 -0
- package/dist/plugins/types.d.ts +41 -0
- package/dist/plugins/types.d.ts.map +1 -0
- package/dist/plugins/types.js +11 -0
- package/dist/session/context_usage.d.ts +13 -0
- package/dist/session/context_usage.d.ts.map +1 -0
- package/dist/session/context_usage.js +25 -0
- package/dist/session/index.d.ts +7 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +5 -0
- package/dist/session/store.d.ts +68 -0
- package/dist/session/store.d.ts.map +1 -0
- package/dist/session/store.js +268 -0
- package/dist/session/types.d.ts +60 -0
- package/dist/session/types.d.ts.map +1 -0
- package/dist/session/types.js +11 -0
- package/dist/store/db.d.ts +64 -0
- package/dist/store/db.d.ts.map +1 -0
- package/dist/store/db.js +91 -0
- package/dist/store/json.d.ts +21 -0
- package/dist/store/json.d.ts.map +1 -0
- package/dist/store/json.js +135 -0
- package/dist/tui/agents.d.ts +4 -0
- package/dist/tui/agents.d.ts.map +1 -0
- package/dist/tui/agents.js +616 -0
- package/dist/tui/app.d.ts +2 -0
- package/dist/tui/app.d.ts.map +1 -0
- package/dist/tui/app.js +32 -0
- package/dist/tui/chat.d.ts +10 -0
- package/dist/tui/chat.d.ts.map +1 -0
- package/dist/tui/chat.js +258 -0
- package/dist/tui/logo.d.ts +2 -0
- package/dist/tui/logo.d.ts.map +1 -0
- package/dist/tui/logo.js +10 -0
- package/dist/tui/mcp_servers.d.ts +4 -0
- package/dist/tui/mcp_servers.d.ts.map +1 -0
- package/dist/tui/mcp_servers.js +287 -0
- package/dist/tui/plugins.d.ts +4 -0
- package/dist/tui/plugins.d.ts.map +1 -0
- package/dist/tui/plugins.js +234 -0
- package/dist/tui/providers.d.ts +4 -0
- package/dist/tui/providers.d.ts.map +1 -0
- package/dist/tui/providers.js +178 -0
- package/dist/tui/tool_picker_state.d.ts +9 -0
- package/dist/tui/tool_picker_state.d.ts.map +1 -0
- package/dist/tui/tool_picker_state.js +37 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/webview/index.html +22 -0
- package/dist/webview/standalone.css +2732 -0
- package/dist/webview/standalone.css.map +7 -0
- package/dist/webview/standalone.js +20641 -0
- package/dist/webview/standalone.js.map +7 -0
- package/package.json +120 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Enumerate other agents the caller can invoke. Returns one entry per
|
|
2
|
+
// configured AgentConfig except the caller itself (no self-recursion at
|
|
3
|
+
// the surface). Managed-by-plugin rows are flagged so the model can
|
|
4
|
+
// decide whether to dispatch a vendor-supplied specialist.
|
|
5
|
+
import { loadAgents } from '../../../store/json.js';
|
|
6
|
+
export const listAgentsTool = {
|
|
7
|
+
name: 'list_agents',
|
|
8
|
+
description: 'List the other agents available for sub-agent dispatch. Returns one ' +
|
|
9
|
+
'entry per agent (excluding yourself) with `name`, `model`, `provider`, ' +
|
|
10
|
+
'and `managed` (true when the agent comes from a plugin). Use the ' +
|
|
11
|
+
'`name` field with `invoke_agent` to delegate a one-shot task.',
|
|
12
|
+
parameters: {
|
|
13
|
+
type: 'object',
|
|
14
|
+
properties: {},
|
|
15
|
+
additionalProperties: false,
|
|
16
|
+
},
|
|
17
|
+
async execute(_args, ctx) {
|
|
18
|
+
const all = await loadAgents();
|
|
19
|
+
const callerId = ctx.callerAgent?.id;
|
|
20
|
+
const summaries = all
|
|
21
|
+
.filter((a) => a.id !== callerId)
|
|
22
|
+
.map((a) => ({
|
|
23
|
+
name: a.name,
|
|
24
|
+
model: a.model,
|
|
25
|
+
provider: a.provider,
|
|
26
|
+
managed: !!a.pluginId,
|
|
27
|
+
}));
|
|
28
|
+
if (summaries.length === 0)
|
|
29
|
+
return { content: 'No other agents available.' };
|
|
30
|
+
return { content: JSON.stringify(summaries, null, 2) };
|
|
31
|
+
},
|
|
32
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"list_commands.d.ts","sourceRoot":"","sources":["../../../../src/harness/tools/builtin/list_commands.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AASxC,eAAO,MAAM,gBAAgB,EAAE,IAqB9B,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Enumerate slash commands available to the current agent. Same plugin
|
|
2
|
+
// gating as skills: a command is visible iff its owning plugin is in the
|
|
3
|
+
// agent's `plugins` whitelist. The user can also invoke commands by typing
|
|
4
|
+
// `/foo args` in the chat — the model gets the same surface programmatically.
|
|
5
|
+
import { listActiveCommands } from '../../../commands/loader.js';
|
|
6
|
+
export const listCommandsTool = {
|
|
7
|
+
name: 'list_commands',
|
|
8
|
+
description: 'List the slash commands available to this agent (from active plugins). ' +
|
|
9
|
+
'Returns one entry per command with `name`, `description?`, and ' +
|
|
10
|
+
'`argumentHint?`. Use `invoke_command` to expand and execute one.',
|
|
11
|
+
parameters: {
|
|
12
|
+
type: 'object',
|
|
13
|
+
properties: {},
|
|
14
|
+
additionalProperties: false,
|
|
15
|
+
},
|
|
16
|
+
async execute(_args, ctx) {
|
|
17
|
+
const summaries = await listActiveCommands(ctx.activePlugins ?? []);
|
|
18
|
+
if (summaries.length === 0)
|
|
19
|
+
return { content: 'No commands available.' };
|
|
20
|
+
const out = summaries.map((s) => ({
|
|
21
|
+
name: s.name,
|
|
22
|
+
description: s.description,
|
|
23
|
+
argumentHint: s.argumentHint,
|
|
24
|
+
}));
|
|
25
|
+
return { content: JSON.stringify(out, null, 2) };
|
|
26
|
+
},
|
|
27
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"list_skills.d.ts","sourceRoot":"","sources":["../../../../src/harness/tools/builtin/list_skills.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC,eAAO,MAAM,cAAc,EAAE,IAoB5B,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Enumerate the plugin skills active for the current agent. The model uses
|
|
2
|
+
// this catalog to decide which skill (if any) is worth pulling into context
|
|
3
|
+
// via `read_skill`. When no plugins are active, returns an empty list.
|
|
4
|
+
import { listActiveSkills } from '../../../plugins/loader.js';
|
|
5
|
+
export const listSkillsTool = {
|
|
6
|
+
name: 'list_skills',
|
|
7
|
+
description: 'List the skills available to this agent. Returns one entry per skill ' +
|
|
8
|
+
'across all active plugins (a cc-plugin pack contributes multiple ' +
|
|
9
|
+
'entries, one per `skills/<name>/SKILL.md`). Each entry has `name`, ' +
|
|
10
|
+
'`description`, and `plugin` (the owning plugin). Call `read_skill` ' +
|
|
11
|
+
"with a `name` from this list to load that skill's full instructions.",
|
|
12
|
+
parameters: {
|
|
13
|
+
type: 'object',
|
|
14
|
+
properties: {},
|
|
15
|
+
additionalProperties: false,
|
|
16
|
+
},
|
|
17
|
+
async execute(_args, ctx) {
|
|
18
|
+
const skills = await listActiveSkills(ctx.activePlugins ?? []);
|
|
19
|
+
if (skills.length === 0) {
|
|
20
|
+
return { content: 'No skills available.' };
|
|
21
|
+
}
|
|
22
|
+
return { content: JSON.stringify(skills, null, 2) };
|
|
23
|
+
},
|
|
24
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multiedit.d.ts","sourceRoot":"","sources":["../../../../src/harness/tools/builtin/multiedit.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAoBxC,eAAO,MAAM,aAAa,EAAE,IAoE3B,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Ported from caretaker server's src/mcp/fs.ts (multiedit).
|
|
2
|
+
// Apply a sequence of edits atomically — all-or-nothing. Each edit reuses
|
|
3
|
+
// the same applyEdit helper as the single-edit tool.
|
|
4
|
+
// Note: multiedit does NOT consult ctx.readPaths. The oldString match is an
|
|
5
|
+
// implicit "you've seen this content" check — if the model invents
|
|
6
|
+
// content, the EOLDSTRING_NOT_FOUND error surfaces immediately. This
|
|
7
|
+
// matches caretaker server's behavior; only `write` enforces the explicit
|
|
8
|
+
// read-before-write guard.
|
|
9
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
10
|
+
import { assertWithinRoot, OutsideRootError } from '../sandbox.js';
|
|
11
|
+
import { applyEdit } from './edit.js';
|
|
12
|
+
function isEditSpec(x) {
|
|
13
|
+
if (!x || typeof x !== 'object')
|
|
14
|
+
return false;
|
|
15
|
+
const o = x;
|
|
16
|
+
return (typeof o.oldString === 'string' &&
|
|
17
|
+
typeof o.newString === 'string' &&
|
|
18
|
+
(o.replaceAll === undefined || typeof o.replaceAll === 'boolean'));
|
|
19
|
+
}
|
|
20
|
+
export const multieditTool = {
|
|
21
|
+
name: 'multiedit',
|
|
22
|
+
description: 'Apply a sequence of edits to a file atomically (all or nothing). ' +
|
|
23
|
+
'Each edit follows the same rules as the edit tool — first occurrence by default, ' +
|
|
24
|
+
'or all occurrences with replaceAll=true.',
|
|
25
|
+
parameters: {
|
|
26
|
+
type: 'object',
|
|
27
|
+
properties: {
|
|
28
|
+
path: { type: 'string', description: 'Path within the working directory.' },
|
|
29
|
+
edits: {
|
|
30
|
+
type: 'array',
|
|
31
|
+
items: {
|
|
32
|
+
type: 'object',
|
|
33
|
+
properties: {
|
|
34
|
+
oldString: { type: 'string' },
|
|
35
|
+
newString: { type: 'string' },
|
|
36
|
+
replaceAll: { type: 'boolean' },
|
|
37
|
+
},
|
|
38
|
+
required: ['oldString', 'newString'],
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
required: ['path', 'edits'],
|
|
43
|
+
additionalProperties: false,
|
|
44
|
+
},
|
|
45
|
+
dangerous: true,
|
|
46
|
+
async execute(args, ctx) {
|
|
47
|
+
const a = args;
|
|
48
|
+
if (typeof a.path !== 'string' || !a.path.trim()) {
|
|
49
|
+
return { content: 'Error: path must be a non-empty string' };
|
|
50
|
+
}
|
|
51
|
+
if (!Array.isArray(a.edits) || a.edits.length === 0 || !a.edits.every(isEditSpec)) {
|
|
52
|
+
return {
|
|
53
|
+
content: 'Error: edits must be a non-empty array of {oldString,newString,replaceAll?}',
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
let abs;
|
|
57
|
+
try {
|
|
58
|
+
abs = assertWithinRoot(ctx.workingDir, a.path);
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
if (err instanceof OutsideRootError)
|
|
62
|
+
return { content: `Error: ${err.message}` };
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
let content;
|
|
66
|
+
try {
|
|
67
|
+
content = await readFile(abs, 'utf-8');
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
const code = err.code;
|
|
71
|
+
if (code === 'ENOENT')
|
|
72
|
+
return { content: `Error: file not found: ${a.path}` };
|
|
73
|
+
return { content: `Error: ${err instanceof Error ? err.message : String(err)}` };
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
for (const e of a.edits) {
|
|
77
|
+
content = applyEdit(content, e.oldString, e.newString, e.replaceAll === true);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
// Atomic: any failure aborts the whole operation, no partial write.
|
|
82
|
+
return { content: `Error: ${err instanceof Error ? err.message : String(err)}` };
|
|
83
|
+
}
|
|
84
|
+
await writeFile(abs, content, 'utf-8');
|
|
85
|
+
ctx.readPaths.add(abs);
|
|
86
|
+
return { content: `Applied ${a.edits.length} edits to ${a.path}` };
|
|
87
|
+
},
|
|
88
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read_attachment.d.ts","sourceRoot":"","sources":["../../../../src/harness/tools/builtin/read_attachment.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAIxC,eAAO,MAAM,kBAAkB,EAAE,IA2HhC,CAAC"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { readFile, stat } from 'node:fs/promises';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { attachmentsDir } from '../../../session/store.js';
|
|
4
|
+
import { activeParsers, MAX_OUTPUT_CHARS, extractPdfWithFallback } from './read_document.js';
|
|
5
|
+
export const readAttachmentTool = {
|
|
6
|
+
name: 'read_attachment',
|
|
7
|
+
description: 'Read the content of a file attached to the current conversation (such as a PDF, Word document, Excel spreadsheet, image, or text file) by specifying its filename ID (e.g. uuid.pdf, uuid.png).',
|
|
8
|
+
parameters: {
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: {
|
|
11
|
+
id: {
|
|
12
|
+
type: 'string',
|
|
13
|
+
description: 'The filename ID of the attachment (e.g. "uuid.pdf", "uuid.png") as shown in the conversation logs.',
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
required: ['id'],
|
|
17
|
+
additionalProperties: false,
|
|
18
|
+
},
|
|
19
|
+
async execute(args, ctx) {
|
|
20
|
+
const a = args;
|
|
21
|
+
if (typeof a.id !== 'string' || !a.id.trim()) {
|
|
22
|
+
return { content: 'Error: id must be a non-empty string' };
|
|
23
|
+
}
|
|
24
|
+
if (!ctx.sessionId) {
|
|
25
|
+
return { content: 'Error: no active session context' };
|
|
26
|
+
}
|
|
27
|
+
const id = path.basename(a.id); // Prevent path traversal
|
|
28
|
+
const dir = attachmentsDir(ctx.sessionId);
|
|
29
|
+
const filePath = path.join(dir, id);
|
|
30
|
+
let st;
|
|
31
|
+
try {
|
|
32
|
+
st = await stat(filePath);
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
const code = err.code;
|
|
36
|
+
if (code === 'ENOENT')
|
|
37
|
+
return { content: `Error: attachment not found: ${id}` };
|
|
38
|
+
if (code === 'EACCES')
|
|
39
|
+
return { content: `Error: permission denied reading attachment: ${id}` };
|
|
40
|
+
return { content: `Error: ${err instanceof Error ? err.message : String(err)}` };
|
|
41
|
+
}
|
|
42
|
+
if (st.isDirectory()) {
|
|
43
|
+
return { content: `Error: attachment ID resolved to a directory: ${id}` };
|
|
44
|
+
}
|
|
45
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
46
|
+
// Route image formats
|
|
47
|
+
if (['.png', '.jpg', '.jpeg', '.webp', '.gif'].includes(ext)) {
|
|
48
|
+
let mime = 'image/png';
|
|
49
|
+
if (ext === '.jpg' || ext === '.jpeg')
|
|
50
|
+
mime = 'image/jpeg';
|
|
51
|
+
else if (ext === '.webp')
|
|
52
|
+
mime = 'image/webp';
|
|
53
|
+
else if (ext === '.gif')
|
|
54
|
+
mime = 'image/gif';
|
|
55
|
+
try {
|
|
56
|
+
const buffer = await readFile(filePath);
|
|
57
|
+
ctx.readPaths.add(filePath);
|
|
58
|
+
return {
|
|
59
|
+
content: `Loaded image attachment ${id} (${buffer.length} bytes)`,
|
|
60
|
+
attachments: [
|
|
61
|
+
{
|
|
62
|
+
mime,
|
|
63
|
+
data: buffer,
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
return { content: `Error reading image attachment: ${err instanceof Error ? err.message : String(err)}` };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const buffer = await readFile(filePath);
|
|
74
|
+
ctx.readPaths.add(filePath);
|
|
75
|
+
let textResult = '';
|
|
76
|
+
const textExtensions = new Set([
|
|
77
|
+
'.txt', '.md', '.markdown', '.csv', '.json', '.js', '.ts', '.tsx', '.jsx',
|
|
78
|
+
'.html', '.css', '.py', '.sh', '.yaml', '.yml', '.xml', '.ini', '.cfg', '.conf'
|
|
79
|
+
]);
|
|
80
|
+
if (ext === '.pdf') {
|
|
81
|
+
textResult = await extractPdfWithFallback(buffer, filePath, ctx.signal);
|
|
82
|
+
}
|
|
83
|
+
else if (ext === '.docx') {
|
|
84
|
+
textResult = await activeParsers.extractDocx(buffer);
|
|
85
|
+
}
|
|
86
|
+
else if (ext === '.xlsx') {
|
|
87
|
+
textResult = await activeParsers.extractXlsx(buffer);
|
|
88
|
+
}
|
|
89
|
+
else if (textExtensions.has(ext)) {
|
|
90
|
+
textResult = buffer.toString('utf8');
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
// Fallback to Pandoc if installed
|
|
94
|
+
const isPandoc = await activeParsers.checkPandoc();
|
|
95
|
+
if (isPandoc) {
|
|
96
|
+
const pandocRes = await activeParsers.runPandoc(filePath, ctx.signal);
|
|
97
|
+
if (pandocRes.error) {
|
|
98
|
+
return { content: `Error: pandoc conversion failed: ${pandocRes.error}` };
|
|
99
|
+
}
|
|
100
|
+
textResult = pandocRes.content;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
// If no Pandoc, check if it's UTF-8 readable text
|
|
104
|
+
const text = buffer.toString('utf8');
|
|
105
|
+
const isBinary = /[\x00-\x08\x0E-\x1F]/.test(text.slice(0, 1024));
|
|
106
|
+
if (isBinary) {
|
|
107
|
+
return {
|
|
108
|
+
content: `Error: unsupported attachment format '${ext}' and pandoc is not installed on this system.\n` +
|
|
109
|
+
`Supported formats: .pdf, .docx, .xlsx, plain text, and common code extensions.\n` +
|
|
110
|
+
`Install pandoc (e.g. 'apt install pandoc') to read other formats.`,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
textResult = text;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
let formatted = textResult;
|
|
117
|
+
if (formatted.length > MAX_OUTPUT_CHARS) {
|
|
118
|
+
formatted =
|
|
119
|
+
formatted.slice(0, MAX_OUTPUT_CHARS) +
|
|
120
|
+
`\n\n[...Output truncated to first ${MAX_OUTPUT_CHARS} characters to fit context limit...]`;
|
|
121
|
+
}
|
|
122
|
+
return { content: formatted };
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
return { content: `Error parsing attachment: ${err instanceof Error ? err.message : String(err)}` };
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Tool } from '../types.js';
|
|
2
|
+
export declare const MAX_OUTPUT_CHARS = 150000;
|
|
3
|
+
export interface DocumentParsers {
|
|
4
|
+
extractPdf(buffer: Buffer): Promise<string>;
|
|
5
|
+
extractDocx(buffer: Buffer): Promise<string>;
|
|
6
|
+
extractXlsx(buffer: Buffer): Promise<string>;
|
|
7
|
+
checkPandoc(): Promise<boolean>;
|
|
8
|
+
runPandoc(filePath: string, signal?: AbortSignal): Promise<{
|
|
9
|
+
content: string;
|
|
10
|
+
error?: string;
|
|
11
|
+
}>;
|
|
12
|
+
runPdftotext(filePath: string, signal?: AbortSignal): Promise<{
|
|
13
|
+
content: string;
|
|
14
|
+
error?: string;
|
|
15
|
+
notInstalled?: boolean;
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* unpdf's bundled pdfjs runs `globalThis.navigator ??= {}` at import time.
|
|
20
|
+
* In plain Node the built-in navigator getter returns a real object so the
|
|
21
|
+
* assignment is skipped, but some hosts (VSCode extension host) expose
|
|
22
|
+
* `navigator` as a getter-only property that yields undefined — the strict-mode
|
|
23
|
+
* assignment then throws "Cannot set property navigator ... only a getter".
|
|
24
|
+
* Redefine it as a writable data property before unpdf loads.
|
|
25
|
+
*/
|
|
26
|
+
export declare function ensureWritableNavigator(): void;
|
|
27
|
+
/**
|
|
28
|
+
* Try unpdf first; if it throws on a PDF the bundled pdfjs cannot handle,
|
|
29
|
+
* fall back to pdftotext (poppler) if installed. Pandoc is not an option
|
|
30
|
+
* here: it can only write PDFs, not read them.
|
|
31
|
+
*/
|
|
32
|
+
export declare function extractPdfWithFallback(buffer: Buffer, filePath: string, signal?: AbortSignal): Promise<string>;
|
|
33
|
+
export declare let activeParsers: DocumentParsers;
|
|
34
|
+
/** Testing seam: inject mock document parsers. Pass `null` to restore real implementations. */
|
|
35
|
+
export declare function __setDocumentParsers(impl: DocumentParsers | null): void;
|
|
36
|
+
export declare const readDocumentTool: Tool;
|
|
37
|
+
//# sourceMappingURL=read_document.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read_document.d.ts","sourceRoot":"","sources":["../../../../src/harness/tools/builtin/read_document.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAMxC,eAAO,MAAM,gBAAgB,SAAU,CAAC;AAExC,MAAM,WAAW,eAAe;IAC9B,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC5C,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC7C,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC7C,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAChC,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAChG,YAAY,CACV,QAAQ,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CACzE;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,IAAI,IAAI,CAa9C;AA4GD;;;;GAIG;AACH,wBAAsB,sBAAsB,CAC1C,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,MAAM,CAAC,CAYjB;AAED,eAAO,IAAI,aAAa,EAAE,eAA6B,CAAC;AAExD,+FAA+F;AAC/F,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,eAAe,GAAG,IAAI,GAAG,IAAI,CAEvE;AAED,eAAO,MAAM,gBAAgB,EAAE,IAwF9B,CAAC"}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { readFile, stat } from 'node:fs/promises';
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import { assertWithinRoot, OutsideRootError } from '../sandbox.js';
|
|
5
|
+
import { extractText, getDocumentProxy } from 'unpdf';
|
|
6
|
+
import mammoth from 'mammoth';
|
|
7
|
+
import ExcelJS from 'exceljs';
|
|
8
|
+
export const MAX_OUTPUT_CHARS = 150_000; // soft limit (~150 KB of text)
|
|
9
|
+
/**
|
|
10
|
+
* unpdf's bundled pdfjs runs `globalThis.navigator ??= {}` at import time.
|
|
11
|
+
* In plain Node the built-in navigator getter returns a real object so the
|
|
12
|
+
* assignment is skipped, but some hosts (VSCode extension host) expose
|
|
13
|
+
* `navigator` as a getter-only property that yields undefined — the strict-mode
|
|
14
|
+
* assignment then throws "Cannot set property navigator ... only a getter".
|
|
15
|
+
* Redefine it as a writable data property before unpdf loads.
|
|
16
|
+
*/
|
|
17
|
+
export function ensureWritableNavigator() {
|
|
18
|
+
if (globalThis.navigator != null)
|
|
19
|
+
return;
|
|
20
|
+
const desc = Object.getOwnPropertyDescriptor(globalThis, 'navigator');
|
|
21
|
+
if (!desc || desc.set || desc.writable)
|
|
22
|
+
return; // absent or assignable — unpdf's shim will work
|
|
23
|
+
try {
|
|
24
|
+
Object.defineProperty(globalThis, 'navigator', {
|
|
25
|
+
value: {},
|
|
26
|
+
writable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
// Not configurable: nothing we can do; unpdf will surface its own error.
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const realParsers = {
|
|
35
|
+
async extractPdf(buffer) {
|
|
36
|
+
ensureWritableNavigator();
|
|
37
|
+
const doc = await getDocumentProxy(new Uint8Array(buffer));
|
|
38
|
+
const { text } = await extractText(doc, { mergePages: true });
|
|
39
|
+
return text;
|
|
40
|
+
},
|
|
41
|
+
async extractDocx(buffer) {
|
|
42
|
+
const { value } = await mammoth.extractRawText({ buffer });
|
|
43
|
+
return value;
|
|
44
|
+
},
|
|
45
|
+
async extractXlsx(buffer) {
|
|
46
|
+
const workbook = new ExcelJS.Workbook();
|
|
47
|
+
// ponytail: cast bridges @types/node's Buffer<ArrayBufferLike> vs exceljs's plain Buffer typedef
|
|
48
|
+
await workbook.xlsx.load(buffer);
|
|
49
|
+
let output = '';
|
|
50
|
+
for (const worksheet of workbook.worksheets) {
|
|
51
|
+
const maxCols = worksheet.columnCount;
|
|
52
|
+
const rowCount = worksheet.rowCount;
|
|
53
|
+
if (maxCols === 0 || rowCount === 0)
|
|
54
|
+
continue;
|
|
55
|
+
output += `## Sheet: ${worksheet.name}\n\n`;
|
|
56
|
+
// Header row (first row; `cell.text` flattens formulas/dates/rich text to display text)
|
|
57
|
+
const headerRow = worksheet.getRow(1);
|
|
58
|
+
const headers = Array.from({ length: maxCols }, (_, i) => {
|
|
59
|
+
const val = headerRow.getCell(i + 1).text.trim();
|
|
60
|
+
return val !== '' ? val : `Column ${i + 1}`;
|
|
61
|
+
});
|
|
62
|
+
output += `| ${headers.join(' | ')} |\n`;
|
|
63
|
+
output += `| ${headers.map(() => '---').join(' | ')} |\n`;
|
|
64
|
+
// Body rows
|
|
65
|
+
for (let r = 2; r <= rowCount; r++) {
|
|
66
|
+
const row = worksheet.getRow(r);
|
|
67
|
+
const cells = Array.from({ length: maxCols }, (_, i) => row.getCell(i + 1).text.replace(/\r?\n/g, ' ').trim());
|
|
68
|
+
output += `| ${cells.join(' | ')} |\n`;
|
|
69
|
+
}
|
|
70
|
+
output += '\n';
|
|
71
|
+
}
|
|
72
|
+
return output;
|
|
73
|
+
},
|
|
74
|
+
checkPandoc() {
|
|
75
|
+
return new Promise((resolve) => {
|
|
76
|
+
const p = spawn('pandoc', ['--version']);
|
|
77
|
+
p.on('error', () => resolve(false));
|
|
78
|
+
p.on('close', (code) => resolve(code === 0));
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
runPandoc(filePath, signal) {
|
|
82
|
+
return runCommand('pandoc', ['-t', 'markdown', filePath], signal);
|
|
83
|
+
},
|
|
84
|
+
runPdftotext(filePath, signal) {
|
|
85
|
+
return runCommand('pdftotext', [filePath, '-'], signal);
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
function runCommand(cmd, args, signal) {
|
|
89
|
+
return new Promise((resolve) => {
|
|
90
|
+
const child = spawn(cmd, args, {
|
|
91
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
92
|
+
});
|
|
93
|
+
let stdout = '';
|
|
94
|
+
let stderr = '';
|
|
95
|
+
child.stdout.on('data', (chunk) => {
|
|
96
|
+
stdout += chunk.toString();
|
|
97
|
+
});
|
|
98
|
+
child.stderr.on('data', (chunk) => {
|
|
99
|
+
stderr += chunk.toString();
|
|
100
|
+
});
|
|
101
|
+
const onAbort = () => {
|
|
102
|
+
child.kill('SIGTERM');
|
|
103
|
+
};
|
|
104
|
+
if (signal) {
|
|
105
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
106
|
+
}
|
|
107
|
+
child.on('error', (err) => {
|
|
108
|
+
if (signal)
|
|
109
|
+
signal.removeEventListener('abort', onAbort);
|
|
110
|
+
resolve({
|
|
111
|
+
content: '',
|
|
112
|
+
error: err.message,
|
|
113
|
+
notInstalled: err.code === 'ENOENT',
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
child.on('close', (code) => {
|
|
117
|
+
if (signal)
|
|
118
|
+
signal.removeEventListener('abort', onAbort);
|
|
119
|
+
if (code === 0) {
|
|
120
|
+
resolve({ content: stdout });
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
resolve({ content: '', error: stderr.trim() || `Exit code ${code}` });
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Try unpdf first; if it throws on a PDF the bundled pdfjs cannot handle,
|
|
130
|
+
* fall back to pdftotext (poppler) if installed. Pandoc is not an option
|
|
131
|
+
* here: it can only write PDFs, not read them.
|
|
132
|
+
*/
|
|
133
|
+
export async function extractPdfWithFallback(buffer, filePath, signal) {
|
|
134
|
+
try {
|
|
135
|
+
return await activeParsers.extractPdf(buffer);
|
|
136
|
+
}
|
|
137
|
+
catch (unpdfErr) {
|
|
138
|
+
const res = await activeParsers.runPdftotext(filePath, signal);
|
|
139
|
+
if (!res.error)
|
|
140
|
+
return res.content;
|
|
141
|
+
if (res.notInstalled)
|
|
142
|
+
throw unpdfErr;
|
|
143
|
+
throw new Error(`unpdf failed (${unpdfErr instanceof Error ? unpdfErr.message : String(unpdfErr)})` +
|
|
144
|
+
` and pdftotext fallback also failed: ${res.error}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export let activeParsers = realParsers;
|
|
148
|
+
/** Testing seam: inject mock document parsers. Pass `null` to restore real implementations. */
|
|
149
|
+
export function __setDocumentParsers(impl) {
|
|
150
|
+
activeParsers = impl ?? realParsers;
|
|
151
|
+
}
|
|
152
|
+
export const readDocumentTool = {
|
|
153
|
+
name: 'read_document',
|
|
154
|
+
description: 'Read a PDF, DOCX (Word), or XLSX/XLS (Excel) file natively, extracting its text content. ' +
|
|
155
|
+
'For PDFs, falls back to the system pdftotext (poppler) if the native parser (unpdf/pdfjs) fails. ' +
|
|
156
|
+
'For other document formats (e.g. EPUB, ODT, RTF), it will try to use the system pandoc command if installed.',
|
|
157
|
+
parameters: {
|
|
158
|
+
type: 'object',
|
|
159
|
+
properties: {
|
|
160
|
+
path: { type: 'string', description: 'Path within the working directory.' },
|
|
161
|
+
},
|
|
162
|
+
required: ['path'],
|
|
163
|
+
additionalProperties: false,
|
|
164
|
+
},
|
|
165
|
+
async execute(args, ctx) {
|
|
166
|
+
const a = args;
|
|
167
|
+
if (typeof a.path !== 'string' || !a.path.trim()) {
|
|
168
|
+
return { content: 'Error: path must be a non-empty string' };
|
|
169
|
+
}
|
|
170
|
+
let abs;
|
|
171
|
+
try {
|
|
172
|
+
abs = assertWithinRoot(ctx.workingDir, a.path);
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
if (err instanceof OutsideRootError)
|
|
176
|
+
return { content: `Error: ${err.message}` };
|
|
177
|
+
throw err;
|
|
178
|
+
}
|
|
179
|
+
let st;
|
|
180
|
+
try {
|
|
181
|
+
st = await stat(abs);
|
|
182
|
+
}
|
|
183
|
+
catch (err) {
|
|
184
|
+
const code = err.code;
|
|
185
|
+
if (code === 'ENOENT')
|
|
186
|
+
return { content: `Error: file not found: ${a.path}` };
|
|
187
|
+
if (code === 'EACCES')
|
|
188
|
+
return { content: `Error: permission denied: ${a.path}` };
|
|
189
|
+
return { content: `Error: ${err instanceof Error ? err.message : String(err)}` };
|
|
190
|
+
}
|
|
191
|
+
if (st.isDirectory()) {
|
|
192
|
+
return { content: `Error: path is a directory: ${a.path}` };
|
|
193
|
+
}
|
|
194
|
+
const ext = path.extname(abs).toLowerCase();
|
|
195
|
+
let textResult = '';
|
|
196
|
+
try {
|
|
197
|
+
if (ext === '.pdf') {
|
|
198
|
+
const buffer = await readFile(abs);
|
|
199
|
+
textResult = await extractPdfWithFallback(buffer, abs, ctx.signal);
|
|
200
|
+
}
|
|
201
|
+
else if (ext === '.docx') {
|
|
202
|
+
const buffer = await readFile(abs);
|
|
203
|
+
textResult = await activeParsers.extractDocx(buffer);
|
|
204
|
+
}
|
|
205
|
+
else if (ext === '.xlsx') {
|
|
206
|
+
const buffer = await readFile(abs);
|
|
207
|
+
textResult = await activeParsers.extractXlsx(buffer);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
// Fallback to Pandoc
|
|
211
|
+
const isPandoc = await activeParsers.checkPandoc();
|
|
212
|
+
if (!isPandoc) {
|
|
213
|
+
return {
|
|
214
|
+
content: `Error: unsupported format '${ext}' and pandoc is not installed on this system.\n` +
|
|
215
|
+
`Supported formats out-of-the-box: .pdf, .docx, .xlsx.\n` +
|
|
216
|
+
`Please install pandoc (e.g. 'sudo apt install pandoc' or 'brew install pandoc') to read '${ext}' files.`,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
const pandocRes = await activeParsers.runPandoc(abs, ctx.signal);
|
|
220
|
+
if (pandocRes.error) {
|
|
221
|
+
return { content: `Error: pandoc conversion failed: ${pandocRes.error}` };
|
|
222
|
+
}
|
|
223
|
+
textResult = pandocRes.content;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
catch (err) {
|
|
227
|
+
return { content: `Error parsing document: ${err instanceof Error ? err.message : String(err)}` };
|
|
228
|
+
}
|
|
229
|
+
ctx.readPaths.add(abs);
|
|
230
|
+
let formatted = textResult;
|
|
231
|
+
if (formatted.length > MAX_OUTPUT_CHARS) {
|
|
232
|
+
formatted =
|
|
233
|
+
formatted.slice(0, MAX_OUTPUT_CHARS) +
|
|
234
|
+
`\n\n[...Output truncated to first ${MAX_OUTPUT_CHARS} characters to fit context limit...]`;
|
|
235
|
+
}
|
|
236
|
+
return { content: formatted };
|
|
237
|
+
},
|
|
238
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read_file.d.ts","sourceRoot":"","sources":["../../../../src/harness/tools/builtin/read_file.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAyExC,eAAO,MAAM,YAAY,EAAE,IA+D1B,CAAC"}
|