@mcuste/pi-herdr-worktree 0.1.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.
@@ -0,0 +1,91 @@
1
+ import { execFile } from "node:child_process";
2
+ const DEFAULT_MAX_OUTPUT_BYTES = 2 * 1024 * 1024;
3
+ class CommandExecutionError extends Error {
4
+ result;
5
+ constructor(message, result) {
6
+ super(message);
7
+ this.name = "CommandExecutionError";
8
+ this.result = result;
9
+ }
10
+ }
11
+ export class CommandInvocationError extends Error {
12
+ command;
13
+ code;
14
+ constructor(command, message, code, options) {
15
+ super(message, options);
16
+ this.name = "CommandInvocationError";
17
+ this.command = command;
18
+ this.code = code;
19
+ }
20
+ }
21
+ export class CommandCancelledError extends Error {
22
+ constructor(command) {
23
+ super(`${command} was cancelled.`);
24
+ this.name = "CommandCancelledError";
25
+ }
26
+ }
27
+ /**
28
+ * Node kills the child once its output passes `maxBuffer`. That is a size limit, not a
29
+ * broken executable, so it gets its own type instead of looking like a failed spawn.
30
+ */
31
+ export class CommandOutputLimitError extends Error {
32
+ command;
33
+ maxOutputBytes;
34
+ constructor(command, maxOutputBytes, options) {
35
+ super(`${command} produced more than ${maxOutputBytes} bytes of output and was stopped.`, {
36
+ ...options,
37
+ });
38
+ this.name = "CommandOutputLimitError";
39
+ this.command = command;
40
+ this.maxOutputBytes = maxOutputBytes;
41
+ }
42
+ }
43
+ export const runCommand = (command, args, options) => {
44
+ if (options.signal?.aborted) {
45
+ return Promise.reject(new CommandCancelledError(command));
46
+ }
47
+ const maxOutputBytes = options.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES;
48
+ const { promise, resolve, reject } = Promise.withResolvers();
49
+ execFile(command, [...args], {
50
+ cwd: options.cwd,
51
+ encoding: "utf8",
52
+ maxBuffer: maxOutputBytes,
53
+ signal: options.signal,
54
+ windowsHide: true,
55
+ }, (error, stdout, stderr) => {
56
+ if (options.signal?.aborted || error?.name === "AbortError") {
57
+ reject(new CommandCancelledError(command));
58
+ return;
59
+ }
60
+ if (error?.code === "ERR_CHILD_PROCESS_STDIO_MAXBUFFER") {
61
+ reject(new CommandOutputLimitError(command, maxOutputBytes, { cause: error }));
62
+ return;
63
+ }
64
+ if (error && typeof error.code !== "number") {
65
+ const detail = error.message.trim();
66
+ reject(new CommandInvocationError(command, `Unable to execute ${command}: ${detail}`, typeof error.code === "string" ? error.code : undefined, { cause: error }));
67
+ return;
68
+ }
69
+ resolve({
70
+ command,
71
+ args: [...args],
72
+ exitCode: typeof error?.code === "number" ? error.code : 0,
73
+ stdout,
74
+ stderr,
75
+ });
76
+ });
77
+ return promise;
78
+ };
79
+ export async function runChecked(runner, command, args, options, failureContext) {
80
+ const result = await runner(command, args, options);
81
+ if (result.exitCode === 0) {
82
+ return result;
83
+ }
84
+ throw new CommandExecutionError(formatFailure(failureContext, result), result);
85
+ }
86
+ function formatFailure(context, result) {
87
+ const output = [result.stderr.trim(), result.stdout.trim()].filter(Boolean).join("\n");
88
+ const suffix = output ? `\n${output}` : "";
89
+ return `${context} (exit ${result.exitCode}).${suffix}`;
90
+ }
91
+ //# sourceMappingURL=process.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process.js","sourceRoot":"","sources":["../src/process.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAE9C,MAAM,wBAAwB,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AAsBjD,MAAM,qBAAsB,SAAQ,KAAK;IAC9B,MAAM,CAAgB;IAE/B,YAAY,OAAe,EAAE,MAAqB;QAChD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;QACpC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AAED,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IACtC,OAAO,CAAS;IAChB,IAAI,CAAqB;IAElC,YAAY,OAAe,EAA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CAAC,CAAC;AAEF,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,MAAqB,EACrB,OAAe,EACf,IAAuB,EACvB,OAA0B,EAC1B,cAAsB;IAEtB,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IACpD,IAAI,MAAM,CAAC,QAAQ,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,IAAI,qBAAqB,CAAC,aAAa,CAAC,cAAc,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;AACjF,CAAC;AAED,SAAS,aAAa,CAAC,OAAe,EAAE,MAAqB;IAC3D,MAAM,MAAM,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACvF,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IAC3C,OAAO,GAAG,OAAO,UAAU,MAAM,CAAC,QAAQ,KAAK,MAAM,EAAE,CAAC;AAC1D,CAAC"}
@@ -0,0 +1,52 @@
1
+ import type { CommandResult } from "./process.js";
2
+ export interface WorktreeInfo {
3
+ readonly path: string;
4
+ readonly label: string;
5
+ readonly branch: string | null;
6
+ readonly openWorkspaceId: string | null;
7
+ readonly isBare: boolean;
8
+ readonly isDetached: boolean;
9
+ readonly isPrunable: boolean;
10
+ readonly isLinkedWorktree: boolean;
11
+ }
12
+ interface WorktreeSource {
13
+ readonly repoName: string;
14
+ readonly repoRoot: string;
15
+ readonly sourceCheckoutPath: string;
16
+ readonly sourceWorkspaceId: string | null;
17
+ }
18
+ export interface WorktreeListResult {
19
+ readonly type: "worktree_list";
20
+ readonly source: WorktreeSource;
21
+ readonly worktrees: readonly WorktreeInfo[];
22
+ }
23
+ export interface WorktreeOpenedResult {
24
+ readonly type: "worktree_created" | "worktree_opened";
25
+ readonly workspaceId: string;
26
+ readonly tabId: string;
27
+ readonly rootPaneId: string;
28
+ readonly worktree: WorktreeInfo;
29
+ readonly alreadyOpen: boolean;
30
+ }
31
+ export interface WorktreeRemovedResult {
32
+ readonly type: "worktree_removed";
33
+ readonly workspaceId: string;
34
+ readonly path: string;
35
+ readonly forced: boolean;
36
+ }
37
+ /** A Herdr socket API error, reported as JSON on stderr with a stable machine-readable code. */
38
+ export declare class HerdrApiError extends Error {
39
+ readonly code: string;
40
+ constructor(code: string, message: string);
41
+ }
42
+ /**
43
+ * Herdr prints one JSON document per command: a success envelope carrying `result`, or an
44
+ * error envelope carrying `error`. A non-zero exit with an error envelope is a rejected
45
+ * request, not a broken CLI, so it keeps its own error type.
46
+ */
47
+ export declare function parseEnvelope(result: CommandResult): Record<string, unknown>;
48
+ export declare function parseWorktreeList(payload: Record<string, unknown>): WorktreeListResult;
49
+ export declare function parseWorktreeOpened(payload: Record<string, unknown>): WorktreeOpenedResult;
50
+ export declare function parseWorktreeRemoved(payload: Record<string, unknown>): WorktreeRemovedResult;
51
+ export {};
52
+ //# sourceMappingURL=response.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,QAAQ,CAAC,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IACxC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B,Q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M,CAAC,MAAM,EAAE,OAAO,CAAC,CA0B5E;AAwBD,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,kBAAkB,CAsBtF;AAED,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,oBAAoB,CAc1F;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,qBAAqB,CAQ5F"}
@@ -0,0 +1,129 @@
1
+ /** A Herdr socket API error, reported as JSON on stderr with a stable machine-readable code. */
2
+ export class HerdrApiError extends Error {
3
+ code;
4
+ constructor(code, message) {
5
+ super(`Herdr rejected the request (${code}): ${message}`);
6
+ this.name = "HerdrApiError";
7
+ this.code = code;
8
+ }
9
+ }
10
+ function fail(detail) {
11
+ throw new Error(`Herdr returned an unreadable response: ${detail}.`);
12
+ }
13
+ function record(value, field) {
14
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
15
+ fail(`${field} is not an object`);
16
+ }
17
+ return value;
18
+ }
19
+ function text(value, field) {
20
+ if (typeof value !== "string" || !value) {
21
+ fail(`${field} is not a non-empty string`);
22
+ }
23
+ return value;
24
+ }
25
+ function optionalText(value, field) {
26
+ if (value === undefined || value === null) {
27
+ return null;
28
+ }
29
+ return text(value, field);
30
+ }
31
+ function flag(value, field) {
32
+ if (typeof value !== "boolean") {
33
+ fail(`${field} is not a boolean`);
34
+ }
35
+ return value;
36
+ }
37
+ /**
38
+ * Herdr prints one JSON document per command: a success envelope carrying `result`, or an
39
+ * error envelope carrying `error`. A non-zero exit with an error envelope is a rejected
40
+ * request, not a broken CLI, so it keeps its own error type.
41
+ */
42
+ export function parseEnvelope(result) {
43
+ const raw = (result.stdout.trim() || result.stderr.trim()).trim();
44
+ if (!raw) {
45
+ fail(`empty output (exit ${result.exitCode})`);
46
+ }
47
+ let document;
48
+ try {
49
+ document = JSON.parse(raw);
50
+ }
51
+ catch (error) {
52
+ throw new Error(`Herdr returned output that is not JSON: ${raw.slice(0, 500)}`, {
53
+ cause: error,
54
+ });
55
+ }
56
+ const envelope = record(document, "the response");
57
+ const failure = envelope.error;
58
+ if (failure !== undefined) {
59
+ const detail = record(failure, "error");
60
+ throw new HerdrApiError(text(detail.code, "error.code"), text(detail.message, "error.message"));
61
+ }
62
+ // A failed command must say why, so a result with no error field is not read as a success.
63
+ if (result.exitCode !== 0) {
64
+ fail(`a result with no error field and exit ${result.exitCode}`);
65
+ }
66
+ return record(envelope.result, "result");
67
+ }
68
+ function parseWorktreeInfo(value, field) {
69
+ const info = record(value, field);
70
+ return {
71
+ path: text(info.path, `${field}.path`),
72
+ label: text(info.label, `${field}.label`),
73
+ branch: optionalText(info.branch, `${field}.branch`),
74
+ openWorkspaceId: optionalText(info.open_workspace_id, `${field}.open_workspace_id`),
75
+ isBare: flag(info.is_bare, `${field}.is_bare`),
76
+ isDetached: flag(info.is_detached, `${field}.is_detached`),
77
+ isPrunable: flag(info.is_prunable, `${field}.is_prunable`),
78
+ isLinkedWorktree: flag(info.is_linked_worktree, `${field}.is_linked_worktree`),
79
+ };
80
+ }
81
+ function assertType(payload, expected) {
82
+ const type = text(payload.type, "result.type");
83
+ if (!expected.includes(type)) {
84
+ fail(`result.type is ${JSON.stringify(type)} instead of ${expected.join(" or ")}`);
85
+ }
86
+ return type;
87
+ }
88
+ export function parseWorktreeList(payload) {
89
+ assertType(payload, ["worktree_list"]);
90
+ const source = record(payload.source, "result.source");
91
+ const worktrees = payload.worktrees;
92
+ if (!Array.isArray(worktrees)) {
93
+ fail("result.worktrees is not an array");
94
+ }
95
+ return {
96
+ type: "worktree_list",
97
+ source: {
98
+ repoName: text(source.repo_name, "result.source.repo_name"),
99
+ repoRoot: text(source.repo_root, "result.source.repo_root"),
100
+ sourceCheckoutPath: text(source.source_checkout_path, "result.source.source_checkout_path"),
101
+ sourceWorkspaceId: optionalText(source.source_workspace_id, "result.source.source_workspace_id"),
102
+ },
103
+ worktrees: worktrees.map((entry, index) => parseWorktreeInfo(entry, `result.worktrees[${index}]`)),
104
+ };
105
+ }
106
+ export function parseWorktreeOpened(payload) {
107
+ const type = assertType(payload, ["worktree_created", "worktree_opened"]);
108
+ const workspace = record(payload.workspace, "result.workspace");
109
+ const tab = record(payload.tab, "result.tab");
110
+ const rootPane = record(payload.root_pane, "result.root_pane");
111
+ return {
112
+ type: type,
113
+ workspaceId: text(workspace.workspace_id, "result.workspace.workspace_id"),
114
+ tabId: text(tab.tab_id, "result.tab.tab_id"),
115
+ rootPaneId: text(rootPane.pane_id, "result.root_pane.pane_id"),
116
+ worktree: parseWorktreeInfo(payload.worktree, "result.worktree"),
117
+ alreadyOpen: type === "worktree_opened" ? flag(payload.already_open, "result.already_open") : false,
118
+ };
119
+ }
120
+ export function parseWorktreeRemoved(payload) {
121
+ assertType(payload, ["worktree_removed"]);
122
+ return {
123
+ type: "worktree_removed",
124
+ workspaceId: text(payload.workspace_id, "result.workspace_id"),
125
+ path: text(payload.path, "result.path"),
126
+ forced: flag(payload.forced, "result.forced"),
127
+ };
128
+ }
129
+ //# sourceMappingURL=response.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"response.js","sourceRoot":"","sources":["../src/response.ts"],"names":[],"mappings":"AA0CA,gGAAgG;AAChG,MAAM,OAAO,aAAc,SAAQ,KAAK;IAC7B,IAAI,CAAS;IAEtB,YAAY,IAAY,EAAE,OAAe;QACvC,KAAK,CAAC,+BAA+B,IAAI,MAAM,OAAO,EAAE,CAAC,CAAC;QAC1D,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAED,SAAS,IAAI,CAAC,MAAc;IAC1B,MAAM,IAAI,KAAK,CAAC,0CAA0C,MAAM,GAAG,CAAC,CAAC;AACvE,CAAC;AAED,SAAS,MAAM,CAAC,KAAc,EAAE,KAAa;IAC3C,IA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AAO,EAAE,CAAC,kBAAkB,CAAC,CAAC,CAAC;IAC1C,OAAO;QACL,IAAI,EAAE,kBAAkB;QACxB,WAAW,EAAE,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,qBAAqB,CAAC;QAC9D,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,aAAa,CAAC;QACvC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,eAAe,CAAC;KAC9C,CAAC;AACJ,CAAC"}
@@ -0,0 +1,79 @@
1
+ import { type Static, type TSchema, Type } from "typebox";
2
+ import { HerdrCapabilityResolver } from "./capability.js";
3
+ import { type HerdrPromptApi } from "./guidance.js";
4
+ import { type CommandRunner } from "./process.js";
5
+ declare const HerdrWorktreeParameters: Type.TUnion<[Type.TObject<{
6
+ operation: Type.TLiteral<"list">;
7
+ }>, Type.TObject<{
8
+ operation: Type.TLiteral<"create">;
9
+ branch: Type.TString;
10
+ base: Type.TOptional<Type.TString>;
11
+ path: Type.TOptional<Type.TString>;
12
+ label: Type.TOptional<Type.TString>;
13
+ focus: Type.TOptional<Type.TBoolean>;
14
+ }>, Type.TObject<{
15
+ operation: Type.TLiteral<"open">;
16
+ path: Type.TOptional<Type.TString>;
17
+ branch: Type.TOptional<Type.TString>;
18
+ label: Type.TOptional<Type.TString>;
19
+ focus: Type.TOptional<Type.TBoolean>;
20
+ }>, Type.TObject<{
21
+ operation: Type.TLiteral<"remove">;
22
+ workspace_id: Type.TString;
23
+ force: Type.TOptional<Type.TBoolean>;
24
+ }>]>;
25
+ type HerdrWorktreeParameters = Static<typeof HerdrWorktreeParameters>;
26
+ type HerdrWorktreeOperation = HerdrWorktreeParameters["operation"];
27
+ /** The fields one operation accepts, typed against the schema so the two cannot drift. */
28
+ type FieldsOf<TOperation extends HerdrWorktreeOperation> = keyof Extract<HerdrWorktreeParameters, {
29
+ operation: TOperation;
30
+ }> & string;
31
+ interface RequestSchema<TOperation extends HerdrWorktreeOperation> {
32
+ readonly required: readonly FieldsOf<TOperation>[];
33
+ readonly optional: readonly FieldsOf<TOperation>[];
34
+ }
35
+ export declare const OPERATION_FIELDS: {
36
+ readonly [K in HerdrWorktreeOperation]: RequestSchema<K>;
37
+ };
38
+ interface TextContent {
39
+ readonly type: "text";
40
+ readonly text: string;
41
+ }
42
+ interface ToolResult<TDetails> {
43
+ readonly content: readonly TextContent[];
44
+ readonly details: TDetails;
45
+ }
46
+ interface ToolContext {
47
+ readonly cwd: string;
48
+ readonly ui?: {
49
+ confirm(title: string, message: string): Promise<boolean>;
50
+ };
51
+ }
52
+ type ToolTier = "read" | "write" | "exec";
53
+ type ToolApprovalDecision = ToolTier | {
54
+ readonly tier: ToolTier;
55
+ readonly reason?: string;
56
+ readonly policy?: "allow" | "deny" | "prompt";
57
+ };
58
+ interface ToolDefinition<TParameters extends TSchema, TDetails> {
59
+ readonly name: string;
60
+ readonly label: string;
61
+ readonly description: string;
62
+ readonly parameters: TParameters;
63
+ readonly approval: ToolApprovalDecision | ((args: unknown) => ToolApprovalDecision);
64
+ readonly loadMode: "essential" | "discoverable";
65
+ readonly concurrency?: "shared" | "exclusive" | ((args: Partial<Static<TParameters>>) => "shared" | "exclusive");
66
+ readonly executionMode?: "sequential" | "parallel";
67
+ readonly formatApprovalDetails?: (args: unknown) => string | readonly string[] | undefined;
68
+ execute(toolCallId: string, parameters: Static<TParameters>, signal: AbortSignal | undefined, onUpdate: unknown, context: ToolContext): Promise<ToolResult<TDetails>>;
69
+ }
70
+ export interface HerdrExtensionApi extends HerdrPromptApi {
71
+ registerTool<TParameters extends TSchema, TDetails>(definition: ToolDefinition<TParameters, TDetails>): void;
72
+ }
73
+ export interface HerdrExtensionDependencies {
74
+ readonly runner?: CommandRunner;
75
+ readonly capabilities?: HerdrCapabilityResolver;
76
+ }
77
+ export declare function registerHerdrWorktreeTools(pi: HerdrExtensionApi, dependencies?: HerdrExtensionDependencies): void;
78
+ export {};
79
+ //# sourceMappingURL=tools.d.ts.map
@@ -0,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