@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 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/harness/provider.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Ported from caretaker server's src/runner/openai_style.ts.
|
|
2
|
+
// Only the OpenAI-compatible streaming primitives — no DB, no MCP, no plugins.
|
|
3
|
+
import { ThinkTagSplitter } from './think_tag_splitter.js';
|
|
4
|
+
export function buildRequestBody(params) {
|
|
5
|
+
const body = {
|
|
6
|
+
model: params.model,
|
|
7
|
+
messages: params.messages,
|
|
8
|
+
stream: true,
|
|
9
|
+
stream_options: { include_usage: true },
|
|
10
|
+
};
|
|
11
|
+
if (params.tools.length > 0)
|
|
12
|
+
body.tools = params.tools;
|
|
13
|
+
return body;
|
|
14
|
+
}
|
|
15
|
+
/** Parse a single SSE line from an OpenAI-compatible chat completions stream. */
|
|
16
|
+
export function parseSseChunk(rawLine) {
|
|
17
|
+
const line = rawLine.trimEnd();
|
|
18
|
+
if (!line)
|
|
19
|
+
return [];
|
|
20
|
+
if (!line.startsWith('data:'))
|
|
21
|
+
return [];
|
|
22
|
+
const payload = line.slice(5).trim();
|
|
23
|
+
if (payload === '[DONE]')
|
|
24
|
+
return [{ kind: 'done' }];
|
|
25
|
+
if (!payload)
|
|
26
|
+
return [];
|
|
27
|
+
let obj;
|
|
28
|
+
try {
|
|
29
|
+
obj = JSON.parse(payload);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
const events = [];
|
|
35
|
+
if (obj && typeof obj === 'object' && obj.usage && typeof obj.usage === 'object') {
|
|
36
|
+
const u = obj.usage;
|
|
37
|
+
const cacheRead = typeof u.cache_read_input_tokens === 'number'
|
|
38
|
+
? u.cache_read_input_tokens
|
|
39
|
+
: typeof u.prompt_tokens_details?.cached_tokens === 'number'
|
|
40
|
+
? u.prompt_tokens_details.cached_tokens
|
|
41
|
+
: undefined;
|
|
42
|
+
const cacheWrite = typeof u.cache_creation_input_tokens === 'number' ? u.cache_creation_input_tokens : undefined;
|
|
43
|
+
const reasoning = typeof u.completion_tokens_details?.reasoning_tokens === 'number'
|
|
44
|
+
? u.completion_tokens_details.reasoning_tokens
|
|
45
|
+
: undefined;
|
|
46
|
+
const promptTokens = typeof u.prompt_tokens === 'number' ? u.prompt_tokens : 0;
|
|
47
|
+
const completion = typeof u.completion_tokens === 'number' ? u.completion_tokens : 0;
|
|
48
|
+
const input = Math.max(0, promptTokens - (cacheRead ?? 0) - (cacheWrite ?? 0));
|
|
49
|
+
const usage = { input, output: completion };
|
|
50
|
+
if (cacheRead !== undefined)
|
|
51
|
+
usage.cacheRead = cacheRead;
|
|
52
|
+
if (cacheWrite !== undefined)
|
|
53
|
+
usage.cacheWrite = cacheWrite;
|
|
54
|
+
if (reasoning !== undefined)
|
|
55
|
+
usage.reasoning = reasoning;
|
|
56
|
+
events.push({ kind: 'usage', usage });
|
|
57
|
+
}
|
|
58
|
+
const choice = obj?.choices?.[0];
|
|
59
|
+
if (!choice)
|
|
60
|
+
return events;
|
|
61
|
+
const delta = choice.delta ?? {};
|
|
62
|
+
if (typeof delta.content === 'string' && delta.content.length > 0) {
|
|
63
|
+
events.push({ kind: 'content', text: delta.content });
|
|
64
|
+
}
|
|
65
|
+
if (typeof delta.reasoning_content === 'string' && delta.reasoning_content.length > 0) {
|
|
66
|
+
events.push({ kind: 'thinking', text: delta.reasoning_content });
|
|
67
|
+
}
|
|
68
|
+
else if (typeof delta.reasoning === 'string' && delta.reasoning.length > 0) {
|
|
69
|
+
events.push({ kind: 'thinking', text: delta.reasoning });
|
|
70
|
+
}
|
|
71
|
+
if (Array.isArray(delta.tool_calls)) {
|
|
72
|
+
for (const tc of delta.tool_calls) {
|
|
73
|
+
events.push({
|
|
74
|
+
kind: 'tool_call_delta',
|
|
75
|
+
index: typeof tc.index === 'number' ? tc.index : 0,
|
|
76
|
+
id: typeof tc.id === 'string' ? tc.id : undefined,
|
|
77
|
+
name: typeof tc?.function?.name === 'string' ? tc.function.name : undefined,
|
|
78
|
+
argumentsDelta: typeof tc?.function?.arguments === 'string' ? tc.function.arguments : undefined,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (typeof choice.finish_reason === 'string') {
|
|
83
|
+
events.push({ kind: 'finish', reason: choice.finish_reason });
|
|
84
|
+
}
|
|
85
|
+
return events;
|
|
86
|
+
}
|
|
87
|
+
export async function readStream(response, onEvent) {
|
|
88
|
+
if (!response.body)
|
|
89
|
+
return;
|
|
90
|
+
const reader = response.body.getReader();
|
|
91
|
+
const dec = new TextDecoder();
|
|
92
|
+
let buf = '';
|
|
93
|
+
// Intercept `content` events to split inline `<think>...</think>` tags into thinking events.
|
|
94
|
+
const splitter = new ThinkTagSplitter();
|
|
95
|
+
const dispatch = (evt) => {
|
|
96
|
+
if (evt.kind === 'content') {
|
|
97
|
+
for (const s of splitter.push(evt.text))
|
|
98
|
+
onEvent(s);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
onEvent(evt);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
let streamErrored = false;
|
|
105
|
+
try {
|
|
106
|
+
while (true) {
|
|
107
|
+
const { value, done } = await reader.read();
|
|
108
|
+
if (done)
|
|
109
|
+
break;
|
|
110
|
+
buf += dec.decode(value, { stream: true });
|
|
111
|
+
let nl;
|
|
112
|
+
while ((nl = buf.indexOf('\n')) >= 0) {
|
|
113
|
+
const line = buf.slice(0, nl);
|
|
114
|
+
buf = buf.slice(nl + 1);
|
|
115
|
+
for (const evt of parseSseChunk(line))
|
|
116
|
+
dispatch(evt);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (buf.length > 0) {
|
|
120
|
+
for (const evt of parseSseChunk(buf))
|
|
121
|
+
dispatch(evt);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
streamErrored = true;
|
|
126
|
+
throw err;
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
if (!streamErrored) {
|
|
130
|
+
for (const s of splitter.flush())
|
|
131
|
+
onEvent(s);
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
reader.releaseLock();
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
/* already released */
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime_info.d.ts","sourceRoot":"","sources":["../../src/harness/runtime_info.ts"],"names":[],"mappings":"AAOA,wBAAgB,sBAAsB,CAAC,KAAK,EAAE;IAC5C,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;CACpB,GAAG,MAAM,CAQT"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Per-run static identity block appended to the system prompt. Same idea
|
|
2
|
+
// as the sister repo's <runtime-info> tag: tells the model who it is,
|
|
3
|
+
// which model + provider it runs against, and where its working directory
|
|
4
|
+
// is. Live values (token usage, context window) live in the
|
|
5
|
+
// `get_agent_context` builtin so the prompt stays cache-friendly across
|
|
6
|
+
// turns.
|
|
7
|
+
export function formatRuntimeInfoBlock(input) {
|
|
8
|
+
const lines = [
|
|
9
|
+
`agent_name: ${input.agentName || '(unnamed)'}`,
|
|
10
|
+
`model: ${input.model || '(unset)'}`,
|
|
11
|
+
`provider: ${input.provider || '(unset)'}`,
|
|
12
|
+
`working_dir: ${input.workingDir}`,
|
|
13
|
+
];
|
|
14
|
+
return `<runtime-info>\n${lines.join('\n')}\n</runtime-info>`;
|
|
15
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stateful streaming splitter for `<think>...</think>` tags inside content.
|
|
3
|
+
*
|
|
4
|
+
* Some models (GLM, Qwen3 thinking, QwQ, open-weight reasoning tunes) emit
|
|
5
|
+
* their reasoning as inline tags in `delta.content`. This util takes raw
|
|
6
|
+
* chunks (in stream order) and yields normalized events:
|
|
7
|
+
* - `{ kind: "thinking", text }` for content inside `<think>...</think>`
|
|
8
|
+
* - `{ kind: "content", text }` for content outside the tag
|
|
9
|
+
*
|
|
10
|
+
* Tags can span chunk boundaries; the splitter buffers partial tag matches
|
|
11
|
+
* until enough characters arrive to disambiguate.
|
|
12
|
+
*
|
|
13
|
+
* Tag matching is lenient: case-insensitive on the tag name, allows whitespace
|
|
14
|
+
* inside the brackets (e.g. `<think >`, `</ think >`), no attributes.
|
|
15
|
+
*/
|
|
16
|
+
export type ThinkSplitEvent = {
|
|
17
|
+
kind: 'content';
|
|
18
|
+
text: string;
|
|
19
|
+
} | {
|
|
20
|
+
kind: 'thinking';
|
|
21
|
+
text: string;
|
|
22
|
+
};
|
|
23
|
+
export declare class ThinkTagSplitter {
|
|
24
|
+
private inThink;
|
|
25
|
+
private buffer;
|
|
26
|
+
/** Push a raw content chunk and pull all events that can be emitted now. */
|
|
27
|
+
push(chunk: string): ThinkSplitEvent[];
|
|
28
|
+
/** Flush remaining buffered text. Call when the stream ends. */
|
|
29
|
+
flush(): ThinkSplitEvent[];
|
|
30
|
+
/** True when the splitter is currently inside an open <think> tag. */
|
|
31
|
+
get isInsideThink(): boolean;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=think_tag_splitter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"think_tag_splitter.d.ts","sourceRoot":"","sources":["../../src/harness/think_tag_splitter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAUH,MAAM,MAAM,eAAe,GACvB;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACjC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvC,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,MAAM,CAAM;IAEpB,4EAA4E;IAC5E,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,EAAE;IA6BtC,gEAAgE;IAChE,KAAK,IAAI,eAAe,EAAE;IAS1B,sEAAsE;IACtE,IAAI,aAAa,IAAI,OAAO,CAE3B;CACF"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stateful streaming splitter for `<think>...</think>` tags inside content.
|
|
3
|
+
*
|
|
4
|
+
* Some models (GLM, Qwen3 thinking, QwQ, open-weight reasoning tunes) emit
|
|
5
|
+
* their reasoning as inline tags in `delta.content`. This util takes raw
|
|
6
|
+
* chunks (in stream order) and yields normalized events:
|
|
7
|
+
* - `{ kind: "thinking", text }` for content inside `<think>...</think>`
|
|
8
|
+
* - `{ kind: "content", text }` for content outside the tag
|
|
9
|
+
*
|
|
10
|
+
* Tags can span chunk boundaries; the splitter buffers partial tag matches
|
|
11
|
+
* until enough characters arrive to disambiguate.
|
|
12
|
+
*
|
|
13
|
+
* Tag matching is lenient: case-insensitive on the tag name, allows whitespace
|
|
14
|
+
* inside the brackets (e.g. `<think >`, `</ think >`), no attributes.
|
|
15
|
+
*/
|
|
16
|
+
const OPEN_RE = /<\s*think\s*>/i;
|
|
17
|
+
const CLOSE_RE = /<\s*\/\s*think\s*>/i;
|
|
18
|
+
// Longest possible "ambiguous prefix" we may have to retain at the end of a
|
|
19
|
+
// chunk while waiting for more bytes to confirm or reject a tag match.
|
|
20
|
+
// "</ think >" → 10 chars, plus a small margin.
|
|
21
|
+
const MAX_PARTIAL = 16;
|
|
22
|
+
export class ThinkTagSplitter {
|
|
23
|
+
inThink = false;
|
|
24
|
+
buffer = '';
|
|
25
|
+
/** Push a raw content chunk and pull all events that can be emitted now. */
|
|
26
|
+
push(chunk) {
|
|
27
|
+
this.buffer += chunk;
|
|
28
|
+
const out = [];
|
|
29
|
+
// Loop: find the next tag transition; emit text up to it; switch state; repeat.
|
|
30
|
+
// Stop when no more transitions are available in the buffered text.
|
|
31
|
+
// Whatever is left in `this.buffer` is held until either:
|
|
32
|
+
// - more data arrives (next push), OR
|
|
33
|
+
// - flush() is called.
|
|
34
|
+
while (true) {
|
|
35
|
+
const re = this.inThink ? CLOSE_RE : OPEN_RE;
|
|
36
|
+
const match = re.exec(this.buffer);
|
|
37
|
+
if (!match) {
|
|
38
|
+
// No tag found. Emit all but the last MAX_PARTIAL chars (which might
|
|
39
|
+
// be the start of an upcoming tag we can't yet verify).
|
|
40
|
+
if (this.buffer.length > MAX_PARTIAL) {
|
|
41
|
+
const head = this.buffer.slice(0, this.buffer.length - MAX_PARTIAL);
|
|
42
|
+
this.buffer = this.buffer.slice(this.buffer.length - MAX_PARTIAL);
|
|
43
|
+
if (head)
|
|
44
|
+
out.push({ kind: this.inThink ? 'thinking' : 'content', text: head });
|
|
45
|
+
}
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
const before = this.buffer.slice(0, match.index);
|
|
49
|
+
if (before)
|
|
50
|
+
out.push({ kind: this.inThink ? 'thinking' : 'content', text: before });
|
|
51
|
+
this.buffer = this.buffer.slice(match.index + match[0].length);
|
|
52
|
+
this.inThink = !this.inThink;
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
/** Flush remaining buffered text. Call when the stream ends. */
|
|
57
|
+
flush() {
|
|
58
|
+
if (!this.buffer)
|
|
59
|
+
return [];
|
|
60
|
+
const out = [
|
|
61
|
+
{ kind: this.inThink ? 'thinking' : 'content', text: this.buffer },
|
|
62
|
+
];
|
|
63
|
+
this.buffer = '';
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
/** True when the splitter is currently inside an open <think> tag. */
|
|
67
|
+
get isInsideThink() {
|
|
68
|
+
return this.inThink;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AgentConfig, ProviderConfig } from '../types.js';
|
|
2
|
+
export interface GenerateTitleInput {
|
|
3
|
+
agent: AgentConfig;
|
|
4
|
+
provider: ProviderConfig;
|
|
5
|
+
firstUserPrompt: string;
|
|
6
|
+
firstAssistantText: string;
|
|
7
|
+
/** Caller-provided abort. Independent of our 15s internal timeout. */
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
}
|
|
10
|
+
/** Returns the cleaned title, or null on any failure (network, non-OK,
|
|
11
|
+
* empty response, parse error). The session keeps its fallback title in
|
|
12
|
+
* that case — title generation is best-effort. */
|
|
13
|
+
export declare function generateTitle(input: GenerateTitleInput): Promise<string | null>;
|
|
14
|
+
/** Strip surrounding quotes/asterisks, trailing period, collapse whitespace,
|
|
15
|
+
* cap length. Exported for unit testing. */
|
|
16
|
+
export declare function cleanTitle(s: string): string;
|
|
17
|
+
//# sourceMappingURL=title.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"title.d.ts","sourceRoot":"","sources":["../../src/harness/title.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAM/D,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,WAAW,CAAC;IACnB,QAAQ,EAAE,cAAc,CAAC;IACzB,eAAe,EAAE,MAAM,CAAC;IACxB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,sEAAsE;IACtE,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED;;mDAEmD;AACnD,wBAAsB,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CA6CrF;AAED;6CAC6C;AAC7C,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAW5C"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// One-shot title generator: a hidden non-streaming chat completion request
|
|
2
|
+
// that asks the agent's own model to summarize the first turn into a 5–7
|
|
3
|
+
// word chat title. Used as fire-and-forget after the first successful turn
|
|
4
|
+
// of a freshly created session — failure leaves the fallback title in place
|
|
5
|
+
// (no UI surfacing, no retry).
|
|
6
|
+
const TITLE_INSTRUCTION = 'Summarize this conversation in 5–7 words for a chat title. ' +
|
|
7
|
+
'Reply with only the title — no quotes, no leading/trailing punctuation, no prefix.';
|
|
8
|
+
/** Returns the cleaned title, or null on any failure (network, non-OK,
|
|
9
|
+
* empty response, parse error). The session keeps its fallback title in
|
|
10
|
+
* that case — title generation is best-effort. */
|
|
11
|
+
export async function generateTitle(input) {
|
|
12
|
+
const { agent, provider, firstUserPrompt, firstAssistantText, signal } = input;
|
|
13
|
+
if (input.provider.type === 'claude-code')
|
|
14
|
+
return null; // no HTTP endpoint; keep fallback title
|
|
15
|
+
if (!firstUserPrompt.trim() && !firstAssistantText.trim())
|
|
16
|
+
return null;
|
|
17
|
+
const baseUrl = provider.endpoint.replace(/\/+$/, '');
|
|
18
|
+
const endpoint = `${baseUrl}/v1/chat/completions`;
|
|
19
|
+
const headers = { 'Content-Type': 'application/json' };
|
|
20
|
+
if (provider.apiKey)
|
|
21
|
+
headers.Authorization = `Bearer ${provider.apiKey}`;
|
|
22
|
+
const body = {
|
|
23
|
+
model: agent.model,
|
|
24
|
+
stream: false,
|
|
25
|
+
messages: [
|
|
26
|
+
{ role: 'user', content: firstUserPrompt },
|
|
27
|
+
{ role: 'assistant', content: firstAssistantText },
|
|
28
|
+
{ role: 'user', content: TITLE_INSTRUCTION },
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
const ac = new AbortController();
|
|
32
|
+
const timer = setTimeout(() => ac.abort(), 15_000);
|
|
33
|
+
const onParentAbort = () => ac.abort();
|
|
34
|
+
if (signal)
|
|
35
|
+
signal.addEventListener('abort', onParentAbort, { once: true });
|
|
36
|
+
try {
|
|
37
|
+
const res = await fetch(endpoint, {
|
|
38
|
+
method: 'POST',
|
|
39
|
+
headers,
|
|
40
|
+
body: JSON.stringify(body),
|
|
41
|
+
signal: ac.signal,
|
|
42
|
+
});
|
|
43
|
+
if (!res.ok)
|
|
44
|
+
return null;
|
|
45
|
+
const json = (await res.json().catch(() => null));
|
|
46
|
+
const raw = json?.choices?.[0]?.message?.content?.trim();
|
|
47
|
+
if (!raw)
|
|
48
|
+
return null;
|
|
49
|
+
return cleanTitle(raw);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
clearTimeout(timer);
|
|
56
|
+
if (signal)
|
|
57
|
+
signal.removeEventListener('abort', onParentAbort);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** Strip surrounding quotes/asterisks, trailing period, collapse whitespace,
|
|
61
|
+
* cap length. Exported for unit testing. */
|
|
62
|
+
export function cleanTitle(s) {
|
|
63
|
+
let out = s.trim();
|
|
64
|
+
// Drop surrounding decorations: quotes, backticks, markdown emphasis.
|
|
65
|
+
out = out.replace(/^["'`*_]+/, '').replace(/["'`*_]+$/, '');
|
|
66
|
+
// Drop a single trailing period — but not "..." which models sometimes use.
|
|
67
|
+
if (out.endsWith('.') && !out.endsWith('..'))
|
|
68
|
+
out = out.slice(0, -1);
|
|
69
|
+
// Collapse internal whitespace.
|
|
70
|
+
out = out.replace(/\s+/g, ' ').trim();
|
|
71
|
+
// Cap length so the list view stays tidy.
|
|
72
|
+
if (out.length > 80)
|
|
73
|
+
out = out.slice(0, 79) + '…';
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bash.d.ts","sourceRoot":"","sources":["../../../../src/harness/tools/builtin/bash.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAqCxC,eAAO,MAAM,QAAQ,EAAE,IA4GtB,CAAC"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { StringDecoder } from 'node:string_decoder';
|
|
3
|
+
import { mergeShellEnv } from './shell-env.js';
|
|
4
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
5
|
+
/** Cap on stdout+stderr we echo back to the model. The harness applies a
|
|
6
|
+
* separate global cap on tool results; this keeps memory bounded inside
|
|
7
|
+
* the bash tool itself for very chatty commands. */
|
|
8
|
+
const MAX_OUTPUT_BYTES = 50_000;
|
|
9
|
+
// Env var patterns scrubbed from the spawned child env. Mirrors caretaker
|
|
10
|
+
// server's policy in src/mcp/shell.ts: keep tokens/keys/secrets out of
|
|
11
|
+
// command-line tools the agent runs.
|
|
12
|
+
const SECRET_ENV_PATTERNS = [/^OPENCODE_/, /^CLAUDE_/, /_TOKEN$/, /_KEY$/, /_SECRET$/];
|
|
13
|
+
function scrubbedEnv() {
|
|
14
|
+
const out = {};
|
|
15
|
+
for (const [k, v] of Object.entries(process.env)) {
|
|
16
|
+
if (SECRET_ENV_PATTERNS.some((re) => re.test(k)))
|
|
17
|
+
continue;
|
|
18
|
+
out[k] = v;
|
|
19
|
+
}
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Build the environment for bash subprocesses.
|
|
24
|
+
* On Linux, merges the probed interactive shell environment to capture
|
|
25
|
+
* PATH and version manager variables (NVM, volta, fnm, etc.) that
|
|
26
|
+
* .bashrc sets but which are missing in non-interactive shells.
|
|
27
|
+
*/
|
|
28
|
+
function bashEnv() {
|
|
29
|
+
const base = scrubbedEnv();
|
|
30
|
+
if (process.platform === 'linux') {
|
|
31
|
+
return mergeShellEnv(base);
|
|
32
|
+
}
|
|
33
|
+
return base;
|
|
34
|
+
}
|
|
35
|
+
export const bashTool = {
|
|
36
|
+
name: 'bash',
|
|
37
|
+
description: 'Execute a shell command in the working directory. Returns the exit code, ' +
|
|
38
|
+
'stdout and stderr concatenated. Output is capped at ~50 KB; the command ' +
|
|
39
|
+
'is killed after the timeout.',
|
|
40
|
+
parameters: {
|
|
41
|
+
type: 'object',
|
|
42
|
+
properties: {
|
|
43
|
+
command: { type: 'string', description: 'Shell command to execute.' },
|
|
44
|
+
timeoutMs: {
|
|
45
|
+
type: 'number',
|
|
46
|
+
description: `Timeout in milliseconds. Default ${DEFAULT_TIMEOUT_MS}.`,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
required: ['command'],
|
|
50
|
+
additionalProperties: false,
|
|
51
|
+
},
|
|
52
|
+
dangerous: true,
|
|
53
|
+
async execute(args, ctx) {
|
|
54
|
+
const a = args;
|
|
55
|
+
if (typeof a.command !== 'string' || !a.command.trim()) {
|
|
56
|
+
return { content: 'Error: command must be a non-empty string' };
|
|
57
|
+
}
|
|
58
|
+
const timeout = typeof a.timeoutMs === 'number' && Number.isFinite(a.timeoutMs) && a.timeoutMs > 0
|
|
59
|
+
? a.timeoutMs
|
|
60
|
+
: DEFAULT_TIMEOUT_MS;
|
|
61
|
+
return await new Promise((resolve) => {
|
|
62
|
+
// On Linux, use bash -c with the probed interactive shell environment
|
|
63
|
+
// to capture PATH and version manager variables (NVM, volta, fnm, etc.).
|
|
64
|
+
// On Windows, fall back to cmd.exe via shell:true. On macOS, login
|
|
65
|
+
// shells source profiles correctly so we use the default env.
|
|
66
|
+
const isWindows = process.platform === 'win32';
|
|
67
|
+
const child = isWindows
|
|
68
|
+
? spawn(a.command, [], {
|
|
69
|
+
cwd: ctx.workingDir,
|
|
70
|
+
shell: true,
|
|
71
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
72
|
+
env: bashEnv(),
|
|
73
|
+
})
|
|
74
|
+
: spawn('bash', ['-c', a.command], {
|
|
75
|
+
cwd: ctx.workingDir,
|
|
76
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
77
|
+
env: bashEnv(),
|
|
78
|
+
});
|
|
79
|
+
const decoder = new StringDecoder('utf8');
|
|
80
|
+
let out = '';
|
|
81
|
+
let truncated = false;
|
|
82
|
+
const append = (chunk) => {
|
|
83
|
+
if (truncated)
|
|
84
|
+
return;
|
|
85
|
+
const room = MAX_OUTPUT_BYTES - out.length;
|
|
86
|
+
if (room <= 0) {
|
|
87
|
+
truncated = true;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const s = decoder.write(chunk);
|
|
91
|
+
if (s.length > room) {
|
|
92
|
+
out += s.slice(0, room);
|
|
93
|
+
truncated = true;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
out += s;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
child.stdout?.on('data', append);
|
|
100
|
+
child.stderr?.on('data', append);
|
|
101
|
+
let killReason = null;
|
|
102
|
+
const timer = setTimeout(() => {
|
|
103
|
+
killReason = 'timeout';
|
|
104
|
+
child.kill('SIGTERM');
|
|
105
|
+
}, timeout);
|
|
106
|
+
const onAbort = () => {
|
|
107
|
+
killReason = 'abort';
|
|
108
|
+
child.kill('SIGTERM');
|
|
109
|
+
};
|
|
110
|
+
ctx.signal.addEventListener('abort', onAbort, { once: true });
|
|
111
|
+
child.on('error', (err) => {
|
|
112
|
+
clearTimeout(timer);
|
|
113
|
+
ctx.signal.removeEventListener('abort', onAbort);
|
|
114
|
+
resolve({ content: `Error: failed to spawn shell: ${err.message}` });
|
|
115
|
+
});
|
|
116
|
+
child.on('close', (code, signal) => {
|
|
117
|
+
clearTimeout(timer);
|
|
118
|
+
ctx.signal.removeEventListener('abort', onAbort);
|
|
119
|
+
const flushed = decoder.end();
|
|
120
|
+
if (flushed && !truncated) {
|
|
121
|
+
const room = MAX_OUTPUT_BYTES - out.length;
|
|
122
|
+
if (room > 0)
|
|
123
|
+
out += flushed.slice(0, room);
|
|
124
|
+
}
|
|
125
|
+
const status = killReason === 'timeout'
|
|
126
|
+
? `[killed after ${timeout}ms timeout]`
|
|
127
|
+
: killReason === 'abort'
|
|
128
|
+
? `[aborted]`
|
|
129
|
+
: signal
|
|
130
|
+
? `[exit signal: ${signal}]`
|
|
131
|
+
: `[exit ${code}]`;
|
|
132
|
+
const tail = truncated ? `\n[...output truncated at ${MAX_OUTPUT_BYTES} bytes]` : '';
|
|
133
|
+
resolve({ content: `${status}\n${out}${tail}` });
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
},
|
|
137
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Tool } from '../types.js';
|
|
2
|
+
/** Pure: apply one replacement and return the new content. Throws on
|
|
3
|
+
* missing/non-unique. Exported for testing and reuse by multiedit. */
|
|
4
|
+
export declare function applyEdit(content: string, oldString: string, newString: string, replaceAll: boolean): string;
|
|
5
|
+
export declare const editTool: Tool;
|
|
6
|
+
//# sourceMappingURL=edit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edit.d.ts","sourceRoot":"","sources":["../../../../src/harness/tools/builtin/edit.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAGxC;uEACuE;AACvE,wBAAgB,SAAS,CACvB,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,OAAO,GAClB,MAAM,CAkBR;AAED,eAAO,MAAM,QAAQ,EAAE,IA0DtB,CAAC"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Ported from caretaker server's src/mcp/fs.ts (edit + applyEdit).
|
|
2
|
+
// Single-occurrence string replace with optional replaceAll. Errors out if
|
|
3
|
+
// the substring is missing, or non-unique without replaceAll.
|
|
4
|
+
// Note: edit 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
|
+
/** Pure: apply one replacement and return the new content. Throws on
|
|
12
|
+
* missing/non-unique. Exported for testing and reuse by multiedit. */
|
|
13
|
+
export function applyEdit(content, oldString, newString, replaceAll) {
|
|
14
|
+
if (oldString === newString)
|
|
15
|
+
return content;
|
|
16
|
+
if (replaceAll) {
|
|
17
|
+
if (!content.includes(oldString)) {
|
|
18
|
+
throw new Error(`EOLDSTRING_NOT_FOUND: oldString not found: ${JSON.stringify(oldString)}`);
|
|
19
|
+
}
|
|
20
|
+
return content.split(oldString).join(newString);
|
|
21
|
+
}
|
|
22
|
+
const first = content.indexOf(oldString);
|
|
23
|
+
if (first < 0) {
|
|
24
|
+
throw new Error(`EOLDSTRING_NOT_FOUND: oldString not found: ${JSON.stringify(oldString)}`);
|
|
25
|
+
}
|
|
26
|
+
if (content.indexOf(oldString, first + oldString.length) >= 0) {
|
|
27
|
+
throw new Error(`EOLDSTRING_NOT_UNIQUE: ${JSON.stringify(oldString)}; pass replaceAll=true to replace every occurrence`);
|
|
28
|
+
}
|
|
29
|
+
return content.slice(0, first) + newString + content.slice(first + oldString.length);
|
|
30
|
+
}
|
|
31
|
+
export const editTool = {
|
|
32
|
+
name: 'edit',
|
|
33
|
+
description: 'Replace exactly one occurrence of oldString with newString in a file. ' +
|
|
34
|
+
'Set replaceAll=true to replace every occurrence.',
|
|
35
|
+
parameters: {
|
|
36
|
+
type: 'object',
|
|
37
|
+
properties: {
|
|
38
|
+
path: { type: 'string', description: 'Path within the working directory.' },
|
|
39
|
+
oldString: { type: 'string' },
|
|
40
|
+
newString: { type: 'string' },
|
|
41
|
+
replaceAll: { type: 'boolean', description: 'Replace every occurrence (default false).' },
|
|
42
|
+
},
|
|
43
|
+
required: ['path', 'oldString', 'newString'],
|
|
44
|
+
additionalProperties: false,
|
|
45
|
+
},
|
|
46
|
+
dangerous: true,
|
|
47
|
+
async execute(args, ctx) {
|
|
48
|
+
const a = args;
|
|
49
|
+
if (typeof a.path !== 'string' || !a.path.trim()) {
|
|
50
|
+
return { content: 'Error: path must be a non-empty string' };
|
|
51
|
+
}
|
|
52
|
+
if (typeof a.oldString !== 'string' || typeof a.newString !== 'string') {
|
|
53
|
+
return { content: 'Error: oldString and newString must be strings' };
|
|
54
|
+
}
|
|
55
|
+
let abs;
|
|
56
|
+
try {
|
|
57
|
+
abs = assertWithinRoot(ctx.workingDir, a.path);
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
if (err instanceof OutsideRootError)
|
|
61
|
+
return { content: `Error: ${err.message}` };
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
let content;
|
|
65
|
+
try {
|
|
66
|
+
content = await readFile(abs, 'utf-8');
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
const code = err.code;
|
|
70
|
+
if (code === 'ENOENT')
|
|
71
|
+
return { content: `Error: file not found: ${a.path}` };
|
|
72
|
+
return { content: `Error: ${err instanceof Error ? err.message : String(err)}` };
|
|
73
|
+
}
|
|
74
|
+
let next;
|
|
75
|
+
try {
|
|
76
|
+
next = applyEdit(content, a.oldString, a.newString, a.replaceAll === true);
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
return { content: `Error: ${err instanceof Error ? err.message : String(err)}` };
|
|
80
|
+
}
|
|
81
|
+
await writeFile(abs, next, 'utf-8');
|
|
82
|
+
ctx.readPaths.add(abs);
|
|
83
|
+
return { content: `Applied edit to ${a.path}` };
|
|
84
|
+
},
|
|
85
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../../../src/harness/tools/builtin/fetch.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAKxC,eAAO,MAAM,SAAS,EAAE,IAkEvB,CAAC"}
|