Files
worklane/.opencode/tools/worklane.ts
T
Ansho ReiandClaude Opus 5 df97b06275 (3e8b6fa) Relay worklane's own error text from the opencode tools
None of the five tools could report why a command failed. mv and comment
spawned without stderr set, and Bun defaults it to inherit, so the text
went to the plugin host and never to the model. show, ls and init used
Bun.$, whose ShellError message is only "Failed with exit code 1", and
each of them interpolated just that message. The CLI explained itself
every time and nothing carried the explanation.

Replace all four spawn and Bun.$ blocks with one run() helper that pipes
both streams and throws with stderr, and strip anyhow's "Error: " prefix
so a relayed message does not read "Error adding comment: Error: ...".

Verified against the real binary with a Bun.spawn shim over node's
child_process, since bun is not installed here: an ambiguous show, mv and
comment now each return the candidate list to the caller. The file is
twenty lines shorter than the duplication it replaces.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-20 14:10:11 +02:00

227 lines
7.3 KiB
TypeScript

import { tool, ToolContext } from "@opencode-ai/plugin";
import path from "path";
function resolveWorklaneDir(context: ToolContext): string {
return path.join(context.worktree || context.directory || ".");
}
/**
* Run worklane and return its stdout, throwing with its stderr on failure.
*
* Both streams have to be piped. Bun defaults stderr to inherit, so it went to
* this process and never reached the model, and Bun.$ throws a ShellError whose
* message is only "Failed with exit code 1". Either way the CLI explained itself
* and nothing relayed the explanation.
*
* stdout is the fallback, not the source: worklane writes errors to stderr.
*/
async function run(cmd: string[], context: ToolContext): Promise<string> {
const proc = Bun.spawn(["worklane", ...cmd], {
cwd: resolveWorklaneDir(context),
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
proc.exited,
]);
if (exitCode !== 0) {
// anyhow prefixes its own "Error: ", and every caller here adds a prefix of
// its own, so drop it rather than relaying "Error adding comment: Error: ...".
const reason = stderr.trim().replace(/^Error:\s*/, "");
throw new Error(reason || stdout.trim() || `worklane exited with code ${exitCode}`);
}
return stdout.trim();
}
export const init = tool({
description:
"Initialize worklane in the current project. Creates the .worklane/ directory with backlog, committed, reviewing, and done subdirectories.",
args: {},
async execute(_args: Record<string, any>, context: ToolContext) {
try {
return await run(["init"], context);
} catch (err: any) {
return `Error initializing worklane: ${err.message}`;
}
},
});
export const add = tool({
description:
"Add a new ticket to the worklane backlog. Requires a title. Description, acceptance criteria, and plan each support multiple lines (one item per line). Tags are comma-separated.",
args: {
title: tool.schema
.string()
.describe("Ticket title (required)"),
description: tool.schema
.string()
.describe("Description text. Use newlines between paragraphs."),
acceptance: tool.schema
.string()
.describe("Acceptance criteria, one per line. Put each criterium on its own line."),
plan: tool.schema
.string()
.describe("Implementation plan steps, one per line. Put each step on its own line."),
tags: tool.schema
.string()
.describe("Comma-separated tags, e.g. 'bug,frontend,urgent'"),
due: tool.schema
.string()
.describe("Due date in YYYY-MM-DD format"),
owner: tool.schema
.string()
.describe("Ticket owner name. If omitted, defaults to the git user."),
},
async execute(args: Record<string, any>, context: ToolContext) {
if (!args.title) {
return "Error adding ticket: title is required";
}
const cmd: string[] = ["add", "--title", args.title];
if (args.description) {
args.description
.split("\n")
.map((l: string) => l.trim())
.filter(Boolean)
.forEach((line: string) => cmd.push("--description", line));
}
if (args.acceptance) {
args.acceptance
.split("\n")
.map((l: string) => l.trim())
.filter(Boolean)
.forEach((line: string) => cmd.push("--acceptance", line));
}
if (args.plan) {
args.plan
.split("\n")
.map((l: string) => l.trim())
.filter(Boolean)
.forEach((line: string) => cmd.push("--plan", line));
}
if (args.tags) cmd.push("--tags", args.tags);
if (args.due) cmd.push("--due", args.due);
if (args.owner) cmd.push("--owner", args.owner);
try {
return await run(cmd, context);
} catch (err: any) {
return `Error adding ticket: ${err.message}`;
}
},
});
export const ls = tool({
description:
"List all worklane tickets. Optionally filter by status (backlog, committed, reviewing, done).",
args: {
status: tool.schema
.enum(["backlog", "committed", "reviewing", "done"])
.describe("Filter by status")
.optional(),
},
async execute(args: Record<string, any>, context: ToolContext) {
const cmd = args.status ? ["ls", args.status] : ["ls"];
try {
return await run(cmd, context);
} catch (err: any) {
// The directory is worth naming here: the usual failure is that there is
// no .worklane above it.
return `Error listing tickets: ${err.message}, Dir: ${resolveWorklaneDir(context)}`;
}
},
});
export const show = tool({
description:
"Show a complete ticket by UUID or title. Displays title, description, acceptance criteria, implementation plan, metadata, and comments.",
args: {
id: tool.schema
.string()
.describe("Ticket UUID (8+ chars) or title substring (required)"),
},
async execute(args: Record<string, any>, context: ToolContext) {
if (!args.id) {
return "Error showing ticket: id is required";
}
try {
return await run(["show", args.id], context);
} catch (err: any) {
return `Error showing ticket: ${err.message}`;
}
},
});
export const mv = tool({
description:
"Move a ticket to a different status in the workflow (backlog -> committed -> reviewing -> done).",
args: {
ticket: tool.schema
.string()
.describe("Ticket UUID (8+ chars) or title substring (required)"),
status: tool.schema
.enum(["backlog", "committed", "reviewing", "done"])
.describe("Target status (required)"),
force: tool.schema
.boolean()
.describe("Force move (allows non-sequential moves like done -> backlog). Use with caution.")
.optional(),
},
async execute(args: Record<string, any>, context: ToolContext) {
if (!args.ticket) {
return "Error moving ticket: ticket is required";
}
if (!args.status) {
return "Error moving ticket: status is required. Must be one of: backlog, committed, reviewing, done";
}
const cmd: string[] = ["mv", args.ticket, "--status", args.status];
if (args.force) cmd.push("--force");
try {
return await run(cmd, context);
} catch (err: any) {
return `Error moving ticket: ${err.message}`;
}
},
});
export const comment = tool({
description: "Add a comment to an existing ticket. The comment body supports markdown and newlines. Use it to add review feedback, notes, or status updates.",
args: {
ticket: tool.schema
.string()
.describe("Ticket UUID (8+ chars) or title substring (required)"),
body: tool.schema
.string()
.describe("Comment body text (required). Supports markdown formatting and multiple paragraphs separated by newlines."),
owner: tool.schema
.string()
.describe("Comment author. If omitted, defaults to the git user.")
.optional(),
},
async execute(args: Record<string, any>, context: ToolContext) {
if (!args.ticket) {
return "Error adding comment: ticket is required";
}
if (!args.body) {
return "Error adding comment: body is required";
}
const cmd: string[] = ["comment", args.ticket, args.body];
if (args.owner) cmd.push("--owner", args.owner);
try {
return await run(cmd, context);
} catch (err: any) {
return `Error adding comment: ${err.message}`;
}
},
});