@wrongstack/tools 0.1.10 → 0.2.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.
@@ -4,7 +4,7 @@ var batchToolUseTool = {
4
4
  description: "Execute multiple tool calls in sequence or parallel. Returns all results.",
5
5
  usageHint: "Set `calls` array with tool names and inputs. `stop_on_error` halts on first failure. `parallel` runs concurrently (default: true).",
6
6
  permission: "confirm",
7
- mutating: false,
7
+ mutating: true,
8
8
  timeoutMs: 12e4,
9
9
  inputSchema: {
10
10
  type: "object",
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/batch-tool-use.ts"],"names":[],"mappings":";AAyBO,IAAM,gBAAA,GAAgE;AAAA,EAC3E,IAAA,EAAM,gBAAA;AAAA,EACN,WAAA,EAAa,2EAAA;AAAA,EACb,SAAA,EACE,qIAAA;AAAA,EACF,UAAA,EAAY,SAAA;AAAA,EACZ,QAAA,EAAU,KAAA;AAAA,EACV,SAAA,EAAW,IAAA;AAAA,EACX,WAAA,EAAa;AAAA,IACX,IAAA,EAAM,QAAA;AAAA,IACN,UAAA,EAAY;AAAA,MACV,KAAA,EAAO;AAAA,QACL,IAAA,EAAM,OAAA;AAAA,QACN,KAAA,EAAO;AAAA,UACL,IAAA,EAAM,QAAA;AAAA,UACN,UAAA,EAAY;AAAA,YACV,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAS;AAAA,YACvB,KAAA,EAAO,EAAE,IAAA,EAAM,QAAA;AAAS,WAC1B;AAAA,UACA,QAAA,EAAU,CAAC,MAAM;AAAA,SACnB;AAAA,QACA,WAAA,EAAa;AAAA,OACf;AAAA,MACA,aAAA,EAAe;AAAA,QACb,IAAA,EAAM,SAAA;AAAA,QACN,WAAA,EAAa;AAAA,OACf;AAAA,MACA,QAAA,EAAU;AAAA,QACR,IAAA,EAAM,SAAA;AAAA,QACN,WAAA,EAAa;AAAA;AACf,KACF;AAAA,IACA,QAAA,EAAU,CAAC,OAAO;AAA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A,GAAU,OAAO,CAAC,CAAA;AAAA,MAChD,WAAA,EAAa,IAAA,CAAK,GAAA,EAAI,GAAI;AAAA,KAC5B;AAAA,EACF;AACF","file":"batch-tool-use.js","sourcesContent":["import type { Tool } from '@wrongstack/core';\n\ninterface BatchToolUseInput {\n calls: {\n tool: string;\n input: Record<string, unknown>;\n }[];\n stop_on_error?: boolean;\n parallel?: boolean;\n}\n\ninterface BatchToolUseOutput {\n results: {\n tool: string;\n success: boolean;\n result?: unknown;\n error?: string;\n executionMs: number;\n }[];\n total: number;\n succeeded: number;\n failed: number;\n stop_on_error: boolean;\n}\n\nexport const batchToolUseTool: Tool<BatchToolUseInput, BatchToolUseOutput> = {\n name: 'batch_tool_use',\n description: 'Execute multiple tool calls in sequence or parallel. Returns all results.',\n usageHint:\n 'Set `calls` array with tool names and inputs. `stop_on_error` halts on first failure. `parallel` runs concurrently (default: true).',\n permission: 'confirm',\n mutating: false,\n timeoutMs: 120_000,\n inputSchema: {\n type: 'object',\n properties: {\n calls: {\n type: 'array',\n items: {\n type: 'object',\n properties: {\n tool: { type: 'string' },\n input: { type: 'object' },\n },\n required: ['tool'],\n },\n description: 'Array of tool calls to execute',\n },\n stop_on_error: {\n type: 'boolean',\n description: 'Stop execution on first error (default: false)',\n },\n parallel: {\n type: 'boolean',\n description: 'Execute calls in parallel (default: true)',\n },\n },\n required: ['calls'],\n },\n async execute(input, ctx, opts) {\n if (!input?.calls || input.calls.length === 0) {\n return {\n results: [],\n total: 0,\n succeeded: 0,\n failed: 0,\n stop_on_error: false,\n };\n }\n\n const results: BatchToolUseOutput['results'] = [];\n let succeeded = 0;\n let failed = 0;\n\n if (input.parallel !== false) {\n const promises = input.calls.map(async (call) => executeSingle(call, ctx, opts));\n const allResults = await Promise.all(promises);\n results.push(...allResults);\n succeeded = allResults.filter((r) => r.success).length;\n failed = allResults.filter((r) => !r.success).length;\n } else {\n for (const call of input.calls) {\n const result = await executeSingle(call, ctx, opts);\n results.push(result);\n if (result.success) {\n succeeded++;\n } else {\n failed++;\n if (input.stop_on_error) break;\n }\n }\n }\n\n return {\n results,\n total: input.calls.length,\n succeeded,\n failed,\n stop_on_error: input.stop_on_error ?? false,\n };\n },\n};\n\nasync function executeSingle(\n call: { tool: string; input: Record<string, unknown> },\n ctx: import('@wrongstack/core').Context,\n opts: { signal: AbortSignal },\n): Promise<BatchToolUseOutput['results'][0]> {\n const start = Date.now();\n const tool = ctx.tools.find((t: Tool) => t.name === call.tool);\n\n if (!tool) {\n return {\n tool: call.tool,\n success: false,\n error: `tool \"${call.tool}\" not found`,\n executionMs: Date.now() - start,\n };\n }\n\n try {\n const result = await tool.execute(call.input, ctx, opts);\n return {\n tool: call.tool,\n success: true,\n result,\n executionMs: Date.now() - start,\n };\n } catch (e) {\n return {\n tool: call.tool,\n success: false,\n error: e instanceof Error ? e.message : String(e),\n executionMs: Date.now() - start,\n };\n }\n}\n"]}
1
+ {"version":3,"sources":["../src/batch-tool-use.ts"],"names":[],"mappings":";AAyBO,IAAM,gBAAA,GAAgE;AAAA,EAC3E,IAAA,EAAM,gBAAA;AAAA,EACN,WAAA,EAAa,2EAAA;AAAA,EACb,SAAA,EACE,qIAAA;AAAA,EACF,UAAA,EAAY,SAAA;AAAA,EACZ,QAAA,EAAU,IAAA;AAAA,EACV,SAAA,EAAW,IAAA;AAAA,EACX,WAAA,EAAa;AAAA,IACX,IAAA,EAAM,QAAA;AAAA,IACN,UAAA,EAAY;AAAA,MACV,KAAA,EAAO;AAAA,QACL,IAAA,EAAM,OAAA;AAAA,QACN,KAAA,EAAO;AAAA,UACL,IAAA,EAAM,QAAA;AAAA,UACN,UAAA,EAAY;AAAA,YACV,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAS;AAAA,YACvB,KAAA,EAAO,EAAE,IAAA,EAAM,QAAA;AAAS,WAC1B;AAAA,UACA,QAAA,EAAU,CAAC,MAAM;AAAA,SACnB;AAAA,QACA,WAAA,EAAa;AAAA,OACf;AAAA,MACA,aAAA,E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AA,EAAI,GAAI;AAAA,KAC5B;AAAA,EACF,SAAS,CAAA,EAAG;AACV,IAAA,OAAO;AAAA,MACL,MAAM,IAAA,CAAK,IAAA;AAAA,MACX,OAAA,EAAS,KAAA;AAAA,MACT,OAAO,CAAA,YAAa,KAAA,GAAQ,CAAA,CAAE,OAAA,GAAU,OAAO,CAAC,CAAA;AAAA,MAChD,WAAA,EAAa,IAAA,CAAK,GAAA,EAAI,GAAI;AAAA,KAC5B;AAAA,EACF;AACF","file":"batch-tool-use.js","sourcesContent":["import type { Tool } from '@wrongstack/core';\n\ninterface BatchToolUseInput {\n calls: {\n tool: string;\n input: Record<string, unknown>;\n }[];\n stop_on_error?: boolean;\n parallel?: boolean;\n}\n\ninterface BatchToolUseOutput {\n results: {\n tool: string;\n success: boolean;\n result?: unknown;\n error?: string;\n executionMs: number;\n }[];\n total: number;\n succeeded: number;\n failed: number;\n stop_on_error: boolean;\n}\n\nexport const batchToolUseTool: Tool<BatchToolUseInput, BatchToolUseOutput> = {\n name: 'batch_tool_use',\n description: 'Execute multiple tool calls in sequence or parallel. Returns all results.',\n usageHint:\n 'Set `calls` array with tool names and inputs. `stop_on_error` halts on first failure. `parallel` runs concurrently (default: true).',\n permission: 'confirm',\n mutating: true,\n timeoutMs: 120_000,\n inputSchema: {\n type: 'object',\n properties: {\n calls: {\n type: 'array',\n items: {\n type: 'object',\n properties: {\n tool: { type: 'string' },\n input: { type: 'object' },\n },\n required: ['tool'],\n },\n description: 'Array of tool calls to execute',\n },\n stop_on_error: {\n type: 'boolean',\n description: 'Stop execution on first error (default: false)',\n },\n parallel: {\n type: 'boolean',\n description: 'Execute calls in parallel (default: true)',\n },\n },\n required: ['calls'],\n },\n async execute(input, ctx, opts) {\n if (!input?.calls || input.calls.length === 0) {\n return {\n results: [],\n total: 0,\n succeeded: 0,\n failed: 0,\n stop_on_error: false,\n };\n }\n\n const results: BatchToolUseOutput['results'] = [];\n let succeeded = 0;\n let failed = 0;\n\n if (input.parallel !== false) {\n const promises = input.calls.map(async (call) => executeSingle(call, ctx, opts));\n const allResults = await Promise.all(promises);\n results.push(...allResults);\n succeeded = allResults.filter((r) => r.success).length;\n failed = allResults.filter((r) => !r.success).length;\n } else {\n for (const call of input.calls) {\n const result = await executeSingle(call, ctx, opts);\n results.push(result);\n if (result.success) {\n succeeded++;\n } else {\n failed++;\n if (input.stop_on_error) break;\n }\n }\n }\n\n return {\n results,\n total: input.calls.length,\n succeeded,\n failed,\n stop_on_error: input.stop_on_error ?? false,\n };\n },\n};\n\nasync function executeSingle(\n call: { tool: string; input: Record<string, unknown> },\n ctx: import('@wrongstack/core').Context,\n opts: { signal: AbortSignal },\n): Promise<BatchToolUseOutput['results'][0]> {\n const start = Date.now();\n const tool = ctx.tools.find((t: Tool) => t.name === call.tool);\n\n if (!tool) {\n return {\n tool: call.tool,\n success: false,\n error: `tool \"${call.tool}\" not found`,\n executionMs: Date.now() - start,\n };\n }\n\n try {\n const result = await tool.execute(call.input, ctx, opts);\n return {\n tool: call.tool,\n success: true,\n result,\n executionMs: Date.now() - start,\n };\n } catch (e) {\n return {\n tool: call.tool,\n success: false,\n error: e instanceof Error ? e.message : String(e),\n executionMs: Date.now() - start,\n };\n }\n}\n"]}
package/dist/builtin.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { spawn } from 'child_process';
2
- import { buildChildEnv, stripAnsi, detectNewlineStyle, normalizeToLf, toStyle, atomicWrite, unifiedDiff, compileGlob } from '@wrongstack/core';
2
+ import { buildChildEnv, stripAnsi, detectNewlineStyle, normalizeToLf, toStyle, atomicWrite, unifiedDiff, compileGlob, loadPlan, emptyPlan, clearPlan, savePlan, removePlanItem, setPlanItemStatus, addPlanItem, formatPlan } from '@wrongstack/core';
3
3
  import * as path from 'path';
4
4
  import { dirname } from 'path';
5
5
  import * as os from 'os';
@@ -420,7 +420,7 @@ var batchToolUseTool = {
420
420
  description: "Execute multiple tool calls in sequence or parallel. Returns all results.",
421
421
  usageHint: "Set `calls` array with tool names and inputs. `stop_on_error` halts on first failure. `parallel` runs concurrently (default: true).",
422
422
  permission: "confirm",
423
- mutating: false,
423
+ mutating: true,
424
424
  timeoutMs: 12e4,
425
425
  inputSchema: {
426
426
  type: "object",
@@ -941,7 +941,7 @@ var execTool = {
941
941
  description: "Restricted shell that only runs pre-approved commands with constrained arguments. Safer alternative to `bash`.",
942
942
  usageHint: "Set `command` (must be in allowlist). `args` passed through. For arbitrary shell access use the `bash` tool instead.",
943
943
  permission: "confirm",
944
- mutating: false,
944
+ mutating: true,
945
945
  timeoutMs: TIMEOUT_MS,
946
946
  inputSchema: {
947
947
  type: "object",
@@ -2653,6 +2653,103 @@ function extractPatchedFiles(output) {
2653
2653
  }
2654
2654
  return files;
2655
2655
  }
2656
+ var planTool = {
2657
+ name: "plan",
2658
+ description: "Inspect or edit the strategic plan board for this session. Plans persist across resume (unlike todos). Use this to lay out the multi-step approach before diving in, then mark steps in_progress/done as the work proceeds.",
2659
+ usageHint: "Set action to one of: show | add | start | done | remove | clear. Pass `title` for add. Pass `target` (item id, 1-based index, or title substring) for start/done/remove. Always returns the formatted plan plus open/total counts.",
2660
+ permission: "auto",
2661
+ mutating: false,
2662
+ timeoutMs: 2e3,
2663
+ inputSchema: {
2664
+ type: "object",
2665
+ properties: {
2666
+ action: {
2667
+ type: "string",
2668
+ enum: ["show", "add", "start", "done", "remove", "clear"]
2669
+ },
2670
+ title: { type: "string", description: "Required when action = add." },
2671
+ details: { type: "string", description: "Optional extra context for add." },
2672
+ target: {
2673
+ type: "string",
2674
+ description: "Plan item id, 1-based index, or title substring. Required for start/done/remove."
2675
+ }
2676
+ },
2677
+ required: ["action"]
2678
+ },
2679
+ async execute(input, ctx) {
2680
+ const planPath = ctx.meta["plan.path"];
2681
+ if (typeof planPath !== "string" || !planPath) {
2682
+ return {
2683
+ ok: false,
2684
+ message: "Plan storage path is not configured for this session.",
2685
+ plan: "",
2686
+ count: 0,
2687
+ open: 0
2688
+ };
2689
+ }
2690
+ const sessionId = ctx.session?.id ?? "unknown";
2691
+ let plan = await loadPlan(planPath) ?? emptyPlan(sessionId);
2692
+ switch (input.action) {
2693
+ case "show":
2694
+ break;
2695
+ case "add": {
2696
+ const title = input.title?.trim();
2697
+ if (!title) {
2698
+ return mkResult(plan, false, "add requires `title`.");
2699
+ }
2700
+ ({ plan } = addPlanItem(plan, title, input.details?.trim() || void 0));
2701
+ await savePlan(planPath, plan);
2702
+ break;
2703
+ }
2704
+ case "start":
2705
+ case "done": {
2706
+ if (!input.target) {
2707
+ return mkResult(plan, false, `${input.action} requires \`target\` (id|index|substring).`);
2708
+ }
2709
+ const next = setPlanItemStatus(
2710
+ plan,
2711
+ input.target,
2712
+ input.action === "start" ? "in_progress" : "done"
2713
+ );
2714
+ if (next === plan) {
2715
+ return mkResult(plan, false, `No plan item matched "${input.target}".`);
2716
+ }
2717
+ plan = next;
2718
+ await savePlan(planPath, plan);
2719
+ break;
2720
+ }
2721
+ case "remove": {
2722
+ if (!input.target) {
2723
+ return mkResult(plan, false, "remove requires `target` (id|index|substring).");
2724
+ }
2725
+ const next = removePlanItem(plan, input.target);
2726
+ if (next === plan) {
2727
+ return mkResult(plan, false, `No plan item matched "${input.target}".`);
2728
+ }
2729
+ plan = next;
2730
+ await savePlan(planPath, plan);
2731
+ break;
2732
+ }
2733
+ case "clear":
2734
+ plan = clearPlan(plan);
2735
+ await savePlan(planPath, plan);
2736
+ break;
2737
+ default:
2738
+ return mkResult(plan, false, `Unknown action "${input.action}".`);
2739
+ }
2740
+ return mkResult(plan, true, `Plan ${input.action} ok.`);
2741
+ }
2742
+ };
2743
+ function mkResult(plan, ok, message) {
2744
+ const open = plan.items.filter((i) => i.status !== "done").length;
2745
+ return {
2746
+ ok,
2747
+ message,
2748
+ plan: formatPlan(plan),
2749
+ count: plan.items.length,
2750
+ open
2751
+ };
2752
+ }
2656
2753
  var MAX_BYTES2 = 5 * 1024 * 1024;
2657
2754
  var readTool = {
2658
2755
  name: "read",
@@ -2736,7 +2833,7 @@ var replaceTool = {
2736
2833
  if (input.replacement === void 0) throw new Error("replace: replacement is required");
2737
2834
  if (!input?.files) throw new Error("replace: files is required");
2738
2835
  const replaceAll = input.replace_all ?? true;
2739
- const compiled = compileUserRegex(input.pattern, replaceAll ? "g" : "");
2836
+ const compiled = compileUserRegex(input.pattern, "g");
2740
2837
  if (!compiled.ok) {
2741
2838
  throw new Error(`replace: ${compiled.reason}`);
2742
2839
  }
@@ -2775,11 +2872,17 @@ var replaceTool = {
2775
2872
  const style = detectNewlineStyle(content);
2776
2873
  const contentLf = normalizeToLf(content);
2777
2874
  re.lastIndex = 0;
2778
- const matches = [...contentLf.matchAll(re)];
2779
- if (matches.length === 0) continue;
2780
- const newContentLf = contentLf.replace(re, input.replacement);
2875
+ const allMatches = [...contentLf.matchAll(re)];
2876
+ if (allMatches.length === 0) continue;
2877
+ const matches = replaceAll ? allMatches : allMatches.slice(0, 1);
2878
+ const count = matches.length;
2879
+ let newContentLf = contentLf;
2880
+ for (let i = matches.length - 1; i >= 0; i--) {
2881
+ const m = matches[i];
2882
+ newContentLf = newContentLf.slice(0, m.index) + input.replacement + newContentLf.slice(m.index + m[0].length);
2883
+ }
2781
2884
  re.lastIndex = 0;
2782
- totalReplacements += matches.length;
2885
+ totalReplacements += count;
2783
2886
  if (!dryRun) {
2784
2887
  const newContent = toStyle(newContentLf, style);
2785
2888
  await atomicWrite(realPath, newContent, { mode: stat9.mode & 511 });
@@ -3665,7 +3768,7 @@ var toolUseTool = {
3665
3768
  description: "Execute a specific tool by name with given input. Useful when the agent knows exactly which tool to call.",
3666
3769
  usageHint: "Set `tool` with exact tool name and `input` with the tool parameters. Returns result or error.",
3667
3770
  permission: "confirm",
3668
- mutating: false,
3771
+ mutating: true,
3669
3772
  timeoutMs: 6e4,
3670
3773
  inputSchema: {
3671
3774
  type: "object",
@@ -4032,6 +4135,7 @@ var builtinTools = [
4032
4135
  fetchTool,
4033
4136
  searchTool,
4034
4137
  todoTool,
4138
+ planTool,
4035
4139
  gitTool,
4036
4140
  patchTool,
4037
4141
  jsonTool,