santree 0.1.2 → 0.1.3
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/dist/commands/helpers/template.d.ts +2 -0
- package/dist/commands/helpers/template.js +48 -11
- package/dist/commands/pr/create.js +5 -7
- package/dist/commands/pr/fix.js +19 -12
- package/dist/commands/pr/review.js +18 -11
- package/dist/commands/worktree/work.js +16 -10
- package/dist/lib/ai.d.ts +20 -7
- package/dist/lib/ai.js +89 -23
- package/dist/lib/exec.d.ts +7 -0
- package/dist/lib/exec.js +15 -1
- package/dist/lib/github.d.ts +20 -0
- package/dist/lib/github.js +137 -1
- package/package.json +1 -1
|
@@ -7,12 +7,13 @@ import { z } from "zod/v4";
|
|
|
7
7
|
import { findRepoRoot, findMainRepoRoot, getCurrentBranch, extractTicketId, } from "../../lib/git.js";
|
|
8
8
|
import { renderTicket } from "../../lib/prompts.js";
|
|
9
9
|
import { getTicketContent } from "../../lib/linear.js";
|
|
10
|
-
import { fetchAndRenderPR, fetchAndRenderDiff } from "../../lib/ai.js";
|
|
10
|
+
import { resolveAIContext, renderAIPrompt, fetchAndRenderPR, fetchAndRenderDiff, } from "../../lib/ai.js";
|
|
11
11
|
export const description = "Render a template to stdout";
|
|
12
12
|
export const args = z.tuple([
|
|
13
|
-
z
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
z.enum(["linear", "git-changes", "pr", "fix-pr", "review"]).describe(argument({
|
|
14
|
+
name: "type",
|
|
15
|
+
description: "Template type (linear, git-changes, pr, fix-pr, or review)",
|
|
16
|
+
})),
|
|
16
17
|
]);
|
|
17
18
|
export default function Template({ args }) {
|
|
18
19
|
const [type] = args;
|
|
@@ -41,13 +42,13 @@ export default function Template({ args }) {
|
|
|
41
42
|
return;
|
|
42
43
|
}
|
|
43
44
|
if (type === "git-changes") {
|
|
44
|
-
const output = fetchAndRenderDiff(branch);
|
|
45
|
+
const output = await fetchAndRenderDiff(branch);
|
|
45
46
|
process.stdout.write(output);
|
|
46
47
|
setStatus("done");
|
|
47
48
|
setTimeout(() => exit(), 100);
|
|
48
49
|
}
|
|
49
50
|
else if (type === "pr") {
|
|
50
|
-
const output = fetchAndRenderPR(branch);
|
|
51
|
+
const output = await fetchAndRenderPR(branch);
|
|
51
52
|
if (!output) {
|
|
52
53
|
setStatus("error");
|
|
53
54
|
setMessage(`No pull request found for branch '${branch}'`);
|
|
@@ -58,6 +59,39 @@ export default function Template({ args }) {
|
|
|
58
59
|
setStatus("done");
|
|
59
60
|
setTimeout(() => exit(), 100);
|
|
60
61
|
}
|
|
62
|
+
else if (type === "fix-pr" || type === "review") {
|
|
63
|
+
const result = await resolveAIContext();
|
|
64
|
+
if (!result.ok) {
|
|
65
|
+
setStatus("error");
|
|
66
|
+
setMessage(result.error);
|
|
67
|
+
setTimeout(() => exit(), 100);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const ctx = result.context;
|
|
71
|
+
const diffContent = await fetchAndRenderDiff(branch);
|
|
72
|
+
if (type === "fix-pr") {
|
|
73
|
+
const prFeedback = await fetchAndRenderPR(branch);
|
|
74
|
+
if (!prFeedback) {
|
|
75
|
+
setStatus("error");
|
|
76
|
+
setMessage(`No pull request found for branch '${branch}'`);
|
|
77
|
+
setTimeout(() => exit(), 100);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const output = renderAIPrompt("fix-pr", ctx, {
|
|
81
|
+
pr_feedback: prFeedback,
|
|
82
|
+
diff_content: diffContent,
|
|
83
|
+
});
|
|
84
|
+
process.stdout.write(output);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const output = renderAIPrompt("review", ctx, {
|
|
88
|
+
diff_content: diffContent,
|
|
89
|
+
});
|
|
90
|
+
process.stdout.write(output);
|
|
91
|
+
}
|
|
92
|
+
setStatus("done");
|
|
93
|
+
setTimeout(() => exit(), 100);
|
|
94
|
+
}
|
|
61
95
|
else {
|
|
62
96
|
const ticketId = extractTicketId(branch);
|
|
63
97
|
if (!ticketId) {
|
|
@@ -83,10 +117,13 @@ export default function Template({ args }) {
|
|
|
83
117
|
}, [type]);
|
|
84
118
|
if (status === "done")
|
|
85
119
|
return null;
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
:
|
|
89
|
-
|
|
90
|
-
|
|
120
|
+
const spinnerTexts = {
|
|
121
|
+
linear: "Fetching Linear ticket...",
|
|
122
|
+
"git-changes": "Gathering changes...",
|
|
123
|
+
pr: "Fetching PR feedback...",
|
|
124
|
+
"fix-pr": "Building fix-pr prompt...",
|
|
125
|
+
review: "Building review prompt...",
|
|
126
|
+
};
|
|
127
|
+
const spinnerText = spinnerTexts[type] ?? "Loading...";
|
|
91
128
|
return (_jsxs(Box, { children: [status === "loading" && (_jsxs(_Fragment, { children: [_jsx(Text, { color: "cyan", children: _jsx(Spinner, { type: "dots" }) }), _jsxs(Text, { children: [" ", spinnerText] })] })), status === "error" && (_jsx(Text, { color: "red", bold: true, children: message }))] }));
|
|
92
129
|
}
|
|
@@ -3,7 +3,7 @@ import { useEffect, useState } from "react";
|
|
|
3
3
|
import { Text, Box, useInput, useApp } from "ink";
|
|
4
4
|
import Spinner from "ink-spinner";
|
|
5
5
|
import { z } from "zod";
|
|
6
|
-
import { exec
|
|
6
|
+
import { exec } from "child_process";
|
|
7
7
|
import { promisify } from "util";
|
|
8
8
|
import { join } from "path";
|
|
9
9
|
import { writeFileSync } from "fs";
|
|
@@ -11,6 +11,7 @@ import { tmpdir } from "os";
|
|
|
11
11
|
import { findMainRepoRoot, findRepoRoot, getCurrentBranch, getBaseBranch, hasUncommittedChanges, getCommitsAhead, remoteBranchExists, getUnpushedCommits, extractTicketId, isInWorktree, getFirstCommitMessage, getCommitLog, getDiffStat, getDiffContent, } from "../../lib/git.js";
|
|
12
12
|
import { ghCliAvailable, getPRInfoAsync, pushBranch, createPR, getPRTemplate, } from "../../lib/github.js";
|
|
13
13
|
import { renderPrompt, renderDiff } from "../../lib/prompts.js";
|
|
14
|
+
import { runAgent } from "../../lib/ai.js";
|
|
14
15
|
const execAsync = promisify(exec);
|
|
15
16
|
export const description = "Create a GitHub pull request";
|
|
16
17
|
export const options = z.object({
|
|
@@ -72,17 +73,14 @@ export default function PR({ options }) {
|
|
|
72
73
|
ticket_id: ticketId ?? "",
|
|
73
74
|
branch_name: branch,
|
|
74
75
|
});
|
|
75
|
-
const result =
|
|
76
|
-
|
|
77
|
-
maxBuffer: 10 * 1024 * 1024,
|
|
78
|
-
});
|
|
79
|
-
if (result.status !== 0) {
|
|
76
|
+
const result = runAgent(prompt);
|
|
77
|
+
if (!result.success) {
|
|
80
78
|
setStatus("error");
|
|
81
79
|
setMessage("Failed to generate PR body with Claude");
|
|
82
80
|
setTimeout(() => exit(), 100);
|
|
83
81
|
return;
|
|
84
82
|
}
|
|
85
|
-
const body = result.
|
|
83
|
+
const body = result.output;
|
|
86
84
|
bodyFile = join(tmpdir(), `santree-pr-${Date.now()}.md`);
|
|
87
85
|
writeFileSync(bodyFile, body);
|
|
88
86
|
}
|
package/dist/commands/pr/fix.js
CHANGED
|
@@ -2,7 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
2
2
|
import { useEffect, useState } from "react";
|
|
3
3
|
import { Text, Box } from "ink";
|
|
4
4
|
import Spinner from "ink-spinner";
|
|
5
|
-
import { resolveAIContext, renderAIPrompt,
|
|
5
|
+
import { resolveAIContext, renderAIPrompt, launchAgent, cleanupImages, fetchAndRenderPR, fetchAndRenderDiff, } from "../../lib/ai.js";
|
|
6
6
|
export const description = "Fix PR review comments";
|
|
7
7
|
export default function Fix() {
|
|
8
8
|
const [status, setStatus] = useState("loading");
|
|
@@ -22,28 +22,35 @@ export default function Fix() {
|
|
|
22
22
|
const ctx = result.context;
|
|
23
23
|
setBranch(ctx.branch);
|
|
24
24
|
setTicketId(ctx.ticketId);
|
|
25
|
-
const prFeedback = fetchAndRenderPR(ctx.branch);
|
|
25
|
+
const prFeedback = await fetchAndRenderPR(ctx.branch);
|
|
26
26
|
if (!prFeedback) {
|
|
27
27
|
setStatus("error");
|
|
28
28
|
setError(`No pull request found for branch '${ctx.branch}'`);
|
|
29
29
|
return;
|
|
30
30
|
}
|
|
31
|
-
const diffContent = fetchAndRenderDiff(ctx.branch);
|
|
31
|
+
const diffContent = await fetchAndRenderDiff(ctx.branch);
|
|
32
32
|
setStatus("launching");
|
|
33
33
|
const prompt = renderAIPrompt("fix-pr", ctx, {
|
|
34
34
|
pr_feedback: prFeedback,
|
|
35
35
|
diff_content: diffContent,
|
|
36
36
|
});
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
try {
|
|
38
|
+
const child = launchAgent(prompt);
|
|
39
|
+
child.on("error", (err) => {
|
|
40
|
+
setStatus("error");
|
|
41
|
+
setError(`Failed to launch agent: ${err.message}`);
|
|
42
|
+
});
|
|
43
|
+
child.on("close", () => {
|
|
44
|
+
if (ctx.ticketId)
|
|
45
|
+
cleanupImages(ctx.ticketId);
|
|
46
|
+
process.exit(0);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
39
50
|
setStatus("error");
|
|
40
|
-
setError(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (ctx.ticketId)
|
|
44
|
-
cleanupImages(ctx.ticketId);
|
|
45
|
-
process.exit(0);
|
|
46
|
-
});
|
|
51
|
+
setError(err instanceof Error ? err.message : "Failed to launch agent");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
47
54
|
}
|
|
48
55
|
init();
|
|
49
56
|
}, []);
|
|
@@ -2,7 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
2
2
|
import { useEffect, useState } from "react";
|
|
3
3
|
import { Text, Box } from "ink";
|
|
4
4
|
import Spinner from "ink-spinner";
|
|
5
|
-
import { resolveAIContext, renderAIPrompt,
|
|
5
|
+
import { resolveAIContext, renderAIPrompt, launchAgent, cleanupImages, fetchAndRenderDiff, } from "../../lib/ai.js";
|
|
6
6
|
export const description = "Review changes against ticket requirements";
|
|
7
7
|
export default function Review() {
|
|
8
8
|
const [status, setStatus] = useState("loading");
|
|
@@ -22,21 +22,28 @@ export default function Review() {
|
|
|
22
22
|
const ctx = result.context;
|
|
23
23
|
setBranch(ctx.branch);
|
|
24
24
|
setTicketId(ctx.ticketId);
|
|
25
|
-
const diffContent = fetchAndRenderDiff(ctx.branch);
|
|
25
|
+
const diffContent = await fetchAndRenderDiff(ctx.branch);
|
|
26
26
|
setStatus("launching");
|
|
27
27
|
const prompt = renderAIPrompt("review", ctx, {
|
|
28
28
|
diff_content: diffContent,
|
|
29
29
|
});
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
try {
|
|
31
|
+
const child = launchAgent(prompt);
|
|
32
|
+
child.on("error", (err) => {
|
|
33
|
+
setStatus("error");
|
|
34
|
+
setError(`Failed to launch agent: ${err.message}`);
|
|
35
|
+
});
|
|
36
|
+
child.on("close", () => {
|
|
37
|
+
if (ctx.ticketId)
|
|
38
|
+
cleanupImages(ctx.ticketId);
|
|
39
|
+
process.exit(0);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
32
43
|
setStatus("error");
|
|
33
|
-
setError(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (ctx.ticketId)
|
|
37
|
-
cleanupImages(ctx.ticketId);
|
|
38
|
-
process.exit(0);
|
|
39
|
-
});
|
|
44
|
+
setError(err instanceof Error ? err.message : "Failed to launch agent");
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
40
47
|
}
|
|
41
48
|
init();
|
|
42
49
|
}, []);
|
|
@@ -3,7 +3,7 @@ import { useEffect, useState } from "react";
|
|
|
3
3
|
import { Text, Box } from "ink";
|
|
4
4
|
import Spinner from "ink-spinner";
|
|
5
5
|
import { z } from "zod";
|
|
6
|
-
import { resolveAIContext, renderAIPrompt,
|
|
6
|
+
import { resolveAIContext, renderAIPrompt, launchAgent, cleanupImages, } from "../../lib/ai.js";
|
|
7
7
|
export const description = "Launch Claude to work on current ticket";
|
|
8
8
|
export const options = z.object({
|
|
9
9
|
plan: z.boolean().optional().describe("Only create implementation plan"),
|
|
@@ -45,16 +45,22 @@ export default function Work({ options }) {
|
|
|
45
45
|
return;
|
|
46
46
|
setStatus("launching");
|
|
47
47
|
const prompt = renderAIPrompt("work", aiContext, { mode });
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
try {
|
|
49
|
+
const child = launchAgent(prompt, { planMode: mode === "plan" });
|
|
50
|
+
child.on("error", (err) => {
|
|
51
|
+
setStatus("error");
|
|
52
|
+
setError(`Failed to launch agent: ${err.message}`);
|
|
53
|
+
});
|
|
54
|
+
child.on("close", () => {
|
|
55
|
+
if (aiContext.ticketId)
|
|
56
|
+
cleanupImages(aiContext.ticketId);
|
|
57
|
+
process.exit(0);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
50
61
|
setStatus("error");
|
|
51
|
-
setError(
|
|
52
|
-
}
|
|
53
|
-
child.on("close", () => {
|
|
54
|
-
if (aiContext.ticketId)
|
|
55
|
-
cleanupImages(aiContext.ticketId);
|
|
56
|
-
process.exit(0);
|
|
57
|
-
});
|
|
62
|
+
setError(err instanceof Error ? err.message : "Failed to launch agent");
|
|
63
|
+
}
|
|
58
64
|
}, [status, aiContext, mode]);
|
|
59
65
|
return (_jsxs(Box, { flexDirection: "column", padding: 1, width: "100%", children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { bold: true, color: "cyan", children: "Work" }) }), _jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: status === "error" ? "red" : getModeColor(mode), paddingX: 1, width: "100%", children: [branch && (_jsxs(Box, { gap: 1, children: [_jsx(Text, { dimColor: true, children: "branch:" }), _jsx(Text, { color: "cyan", bold: true, children: branch })] })), ticketId && (_jsxs(Box, { gap: 1, children: [_jsx(Text, { dimColor: true, children: "ticket:" }), _jsx(Text, { color: "blue", bold: true, children: ticketId })] })), _jsxs(Box, { gap: 1, children: [_jsx(Text, { dimColor: true, children: "mode:" }), _jsx(Text, { backgroundColor: getModeColor(mode), color: "white", bold: true, children: ` ${getModeLabel(mode)} ` })] })] }), _jsxs(Box, { marginTop: 1, children: [status === "loading" && (_jsxs(Box, { children: [_jsx(Text, { color: "cyan", children: _jsx(Spinner, { type: "dots" }) }), _jsx(Text, { children: " Loading..." })] })), status === "fetching" && (_jsxs(Box, { children: [_jsx(Text, { color: "cyan", children: _jsx(Spinner, { type: "dots" }) }), _jsx(Text, { children: " Fetching ticket from Linear..." })] })), status === "launching" && (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: "green", bold: true, children: "\u2713 Launching Claude (through Happy)..." }), _jsxs(Text, { dimColor: true, children: [" ", "happy", mode === "plan" ? " --permission-mode plan" : "", " ", `"<${getModeLabel(mode)} prompt for ${ticketId}>"`] })] })), status === "error" && (_jsxs(Text, { color: "red", bold: true, children: ["\u2717 ", error] }))] })] }));
|
|
60
66
|
}
|
package/dist/lib/ai.d.ts
CHANGED
|
@@ -27,22 +27,35 @@ export declare function buildPromptContext(ctx: AIContext, extra?: Record<string
|
|
|
27
27
|
*/
|
|
28
28
|
export declare function renderAIPrompt(template: string, ctx: AIContext, extra?: Record<string, string | undefined>): string;
|
|
29
29
|
/**
|
|
30
|
-
* Fetch and render PR feedback for a branch.
|
|
30
|
+
* Fetch and render PR feedback for a branch (async, non-blocking).
|
|
31
31
|
* Returns rendered markdown or null if no PR exists.
|
|
32
32
|
*/
|
|
33
|
-
export declare function fetchAndRenderPR(branch: string): string | null
|
|
33
|
+
export declare function fetchAndRenderPR(branch: string): Promise<string | null>;
|
|
34
34
|
/**
|
|
35
|
-
* Fetch and render diff for a branch against its base branch.
|
|
35
|
+
* Fetch and render diff for a branch against its base branch (async, non-blocking).
|
|
36
36
|
* Returns rendered markdown.
|
|
37
37
|
*/
|
|
38
|
-
export declare function fetchAndRenderDiff(branch: string): string
|
|
38
|
+
export declare function fetchAndRenderDiff(branch: string): Promise<string>;
|
|
39
39
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
40
|
+
* Launch an interactive agent session with a prompt.
|
|
41
|
+
* Resolves the agent binary (happy > claude), passes prompt directly
|
|
42
|
+
* or via temp file if too large for OS arg limit.
|
|
43
|
+
* Throws if no agent binary is found.
|
|
42
44
|
*/
|
|
43
|
-
export declare function
|
|
45
|
+
export declare function launchAgent(prompt: string, opts?: {
|
|
44
46
|
planMode?: boolean;
|
|
45
47
|
}): ChildProcess;
|
|
48
|
+
export interface RunAgentResult {
|
|
49
|
+
success: boolean;
|
|
50
|
+
output: string;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Run an agent in non-interactive print mode and capture output.
|
|
54
|
+
* Resolves the agent binary (happy > claude), passes prompt directly
|
|
55
|
+
* or via temp file if too large for OS arg limit.
|
|
56
|
+
* Throws if no agent binary is found.
|
|
57
|
+
*/
|
|
58
|
+
export declare function runAgent(prompt: string): RunAgentResult;
|
|
46
59
|
/**
|
|
47
60
|
* Cleanup images downloaded for a ticket.
|
|
48
61
|
*/
|
package/dist/lib/ai.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import { spawn } from "child_process";
|
|
2
|
-
import {
|
|
1
|
+
import { execSync, spawn, spawnSync } from "child_process";
|
|
2
|
+
import { writeFileSync } from "fs";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
import { tmpdir } from "os";
|
|
5
|
+
import { getCurrentBranch, extractTicketId, findRepoRoot, findMainRepoRoot, getBaseBranch, } from "./git.js";
|
|
3
6
|
import { renderPrompt, renderTicket, renderDiff, renderPR } from "./prompts.js";
|
|
4
7
|
import { getTicketContent, cleanupImages } from "./linear.js";
|
|
5
|
-
import {
|
|
8
|
+
import { getPRInfoAsync, getPRChecksAsync, getPRReviewsAsync, getPRReviewCommentsAsync, getPRConversationCommentsAsync, getFailedCheckDetailsAsync, } from "./github.js";
|
|
9
|
+
import { runAsync } from "./exec.js";
|
|
6
10
|
/**
|
|
7
11
|
* Resolves repo, branch, ticket ID, and fetches the Linear ticket.
|
|
8
12
|
* Returns an error string if any required context is missing.
|
|
@@ -56,20 +60,20 @@ const BOT_AUTHORS = new Set([
|
|
|
56
60
|
"vercel",
|
|
57
61
|
]);
|
|
58
62
|
/**
|
|
59
|
-
* Fetch and render PR feedback for a branch.
|
|
63
|
+
* Fetch and render PR feedback for a branch (async, non-blocking).
|
|
60
64
|
* Returns rendered markdown or null if no PR exists.
|
|
61
65
|
*/
|
|
62
|
-
export function fetchAndRenderPR(branch) {
|
|
63
|
-
const prInfo =
|
|
66
|
+
export async function fetchAndRenderPR(branch) {
|
|
67
|
+
const prInfo = await getPRInfoAsync(branch);
|
|
64
68
|
if (!prInfo)
|
|
65
69
|
return null;
|
|
66
|
-
const checks =
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const
|
|
70
|
+
const [checks, reviews, reviewComments, allComments] = await Promise.all([
|
|
71
|
+
getPRChecksAsync(prInfo.number),
|
|
72
|
+
getPRReviewsAsync(prInfo.number),
|
|
73
|
+
getPRReviewCommentsAsync(prInfo.number),
|
|
74
|
+
getPRConversationCommentsAsync(prInfo.number),
|
|
75
|
+
]);
|
|
76
|
+
const failedChecks = await Promise.all((checks ?? []).filter((c) => c.bucket === "fail").map((c) => getFailedCheckDetailsAsync(c)));
|
|
73
77
|
const conversationComments = (allComments ?? []).filter((c) => !BOT_AUTHORS.has(c.author) && !c.author.endsWith("[bot]"));
|
|
74
78
|
return renderPR({
|
|
75
79
|
pr_number: prInfo.number,
|
|
@@ -83,29 +87,91 @@ export function fetchAndRenderPR(branch) {
|
|
|
83
87
|
});
|
|
84
88
|
}
|
|
85
89
|
/**
|
|
86
|
-
* Fetch and render diff for a branch against its base branch.
|
|
90
|
+
* Fetch and render diff for a branch against its base branch (async, non-blocking).
|
|
87
91
|
* Returns rendered markdown.
|
|
88
92
|
*/
|
|
89
|
-
export function fetchAndRenderDiff(branch) {
|
|
93
|
+
export async function fetchAndRenderDiff(branch) {
|
|
90
94
|
const baseBranch = getBaseBranch(branch);
|
|
95
|
+
const [commitLog, diffStat, diff] = await Promise.all([
|
|
96
|
+
runAsync(`git log ${baseBranch}..HEAD --format="- %s"`).then((v) => v || null),
|
|
97
|
+
runAsync(`git diff ${baseBranch}..HEAD --stat`).then((v) => v || null),
|
|
98
|
+
runAsync(`git diff ${baseBranch}..HEAD`, { maxBuffer: 10 * 1024 * 1024 }).then((v) => v || null),
|
|
99
|
+
]);
|
|
91
100
|
return renderDiff({
|
|
92
101
|
base_branch: baseBranch,
|
|
93
|
-
commit_log:
|
|
94
|
-
diff_stat:
|
|
95
|
-
diff
|
|
102
|
+
commit_log: commitLog,
|
|
103
|
+
diff_stat: diffStat,
|
|
104
|
+
diff,
|
|
96
105
|
});
|
|
97
106
|
}
|
|
98
107
|
/**
|
|
99
|
-
*
|
|
100
|
-
* Returns the
|
|
108
|
+
* Resolve which agent binary to use (happy or claude).
|
|
109
|
+
* Returns the binary name, or null if neither is installed.
|
|
101
110
|
*/
|
|
102
|
-
|
|
111
|
+
function resolveAgentBinary() {
|
|
112
|
+
for (const bin of ["happy", "claude"]) {
|
|
113
|
+
try {
|
|
114
|
+
execSync(`which ${bin}`, { stdio: "ignore" });
|
|
115
|
+
return bin;
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
// Conservative limit: 200KB leaves room for env vars within macOS 256KB ARG_MAX
|
|
124
|
+
const ARG_MAX_SAFE = 200 * 1024;
|
|
125
|
+
/**
|
|
126
|
+
* Build the prompt argument for the agent.
|
|
127
|
+
* If the prompt fits in ARG_MAX, returns it directly.
|
|
128
|
+
* Otherwise, writes to a temp file and returns a short instruction to read it.
|
|
129
|
+
*/
|
|
130
|
+
function promptArg(prompt) {
|
|
131
|
+
if (Buffer.byteLength(prompt) <= ARG_MAX_SAFE) {
|
|
132
|
+
return prompt;
|
|
133
|
+
}
|
|
134
|
+
const filePath = join(tmpdir(), `santree-prompt-${Date.now()}.md`);
|
|
135
|
+
writeFileSync(filePath, prompt);
|
|
136
|
+
return `Read ${filePath} and follow the instructions inside.`;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Launch an interactive agent session with a prompt.
|
|
140
|
+
* Resolves the agent binary (happy > claude), passes prompt directly
|
|
141
|
+
* or via temp file if too large for OS arg limit.
|
|
142
|
+
* Throws if no agent binary is found.
|
|
143
|
+
*/
|
|
144
|
+
export function launchAgent(prompt, opts) {
|
|
145
|
+
const bin = resolveAgentBinary();
|
|
146
|
+
if (!bin) {
|
|
147
|
+
throw new Error("No agent found. Install happy (npm i -g happy-coder) or claude (npm i -g @anthropic-ai/claude-code).");
|
|
148
|
+
}
|
|
103
149
|
const args = [];
|
|
104
150
|
if (opts?.planMode) {
|
|
105
151
|
args.push("--permission-mode", "plan");
|
|
106
152
|
}
|
|
107
|
-
args.push(prompt);
|
|
108
|
-
return spawn(
|
|
153
|
+
args.push("--", promptArg(prompt));
|
|
154
|
+
return spawn(bin, args, { stdio: "inherit" });
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Run an agent in non-interactive print mode and capture output.
|
|
158
|
+
* Resolves the agent binary (happy > claude), passes prompt directly
|
|
159
|
+
* or via temp file if too large for OS arg limit.
|
|
160
|
+
* Throws if no agent binary is found.
|
|
161
|
+
*/
|
|
162
|
+
export function runAgent(prompt) {
|
|
163
|
+
const bin = resolveAgentBinary();
|
|
164
|
+
if (!bin) {
|
|
165
|
+
throw new Error("No agent found. Install happy (npm i -g happy-coder) or claude (npm i -g @anthropic-ai/claude-code).");
|
|
166
|
+
}
|
|
167
|
+
const result = spawnSync(bin, ["-p", "--output-format", "text", "--", promptArg(prompt)], {
|
|
168
|
+
encoding: "utf-8",
|
|
169
|
+
maxBuffer: 10 * 1024 * 1024,
|
|
170
|
+
});
|
|
171
|
+
return {
|
|
172
|
+
success: result.status === 0,
|
|
173
|
+
output: result.stdout?.trim() ?? "",
|
|
174
|
+
};
|
|
109
175
|
}
|
|
110
176
|
/**
|
|
111
177
|
* Cleanup images downloaded for a ticket.
|
package/dist/lib/exec.d.ts
CHANGED
|
@@ -5,6 +5,13 @@ export declare function run(command: string, options?: {
|
|
|
5
5
|
cwd?: string;
|
|
6
6
|
maxBuffer?: number;
|
|
7
7
|
}): string | null;
|
|
8
|
+
/**
|
|
9
|
+
* Run a shell command asynchronously and return trimmed stdout, or null on failure.
|
|
10
|
+
*/
|
|
11
|
+
export declare function runAsync(command: string, options?: {
|
|
12
|
+
cwd?: string;
|
|
13
|
+
maxBuffer?: number;
|
|
14
|
+
}): Promise<string | null>;
|
|
8
15
|
/**
|
|
9
16
|
* Spawn a command asynchronously and capture its output.
|
|
10
17
|
* Returns the exit code and combined stdout/stderr.
|
package/dist/lib/exec.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { execSync, spawn } from "child_process";
|
|
1
|
+
import { execSync, exec, spawn } from "child_process";
|
|
2
|
+
import { promisify } from "util";
|
|
3
|
+
const execPromise = promisify(exec);
|
|
2
4
|
/**
|
|
3
5
|
* Run a shell command and return trimmed stdout, or null on failure.
|
|
4
6
|
*/
|
|
@@ -10,6 +12,18 @@ export function run(command, options) {
|
|
|
10
12
|
return null;
|
|
11
13
|
}
|
|
12
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Run a shell command asynchronously and return trimmed stdout, or null on failure.
|
|
17
|
+
*/
|
|
18
|
+
export async function runAsync(command, options) {
|
|
19
|
+
try {
|
|
20
|
+
const { stdout } = await execPromise(command, { encoding: "utf-8", ...options });
|
|
21
|
+
return stdout.trim();
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
13
27
|
/**
|
|
14
28
|
* Spawn a command asynchronously and capture its output.
|
|
15
29
|
* Returns the exit code and combined stdout/stderr.
|
package/dist/lib/github.d.ts
CHANGED
|
@@ -47,6 +47,26 @@ export declare function getPRTemplate(): string | null;
|
|
|
47
47
|
* Returns empty string if the PR has no comments or on failure.
|
|
48
48
|
*/
|
|
49
49
|
export declare function getPRComments(prNumber: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* Async version of getPRChecks.
|
|
52
|
+
*/
|
|
53
|
+
export declare function getPRChecksAsync(prNumber: string): Promise<PRCheck[] | null>;
|
|
54
|
+
/**
|
|
55
|
+
* Async version of getPRReviews.
|
|
56
|
+
*/
|
|
57
|
+
export declare function getPRReviewsAsync(prNumber: string): Promise<PRReview[] | null>;
|
|
58
|
+
/**
|
|
59
|
+
* Async version of getPRReviewComments.
|
|
60
|
+
*/
|
|
61
|
+
export declare function getPRReviewCommentsAsync(prNumber: string): Promise<PRReviewComment[] | null>;
|
|
62
|
+
/**
|
|
63
|
+
* Async version of getPRConversationComments.
|
|
64
|
+
*/
|
|
65
|
+
export declare function getPRConversationCommentsAsync(prNumber: string): Promise<PRConversationComment[] | null>;
|
|
66
|
+
/**
|
|
67
|
+
* Async version of getFailedCheckDetails.
|
|
68
|
+
*/
|
|
69
|
+
export declare function getFailedCheckDetailsAsync(check: PRCheck): Promise<FailedCheckDetail>;
|
|
50
70
|
export interface PRConversationComment {
|
|
51
71
|
author: string;
|
|
52
72
|
body: string;
|
package/dist/lib/github.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { execSync, exec } from "child_process";
|
|
2
2
|
import { promisify } from "util";
|
|
3
|
-
import { run } from "./exec.js";
|
|
3
|
+
import { run, runAsync } from "./exec.js";
|
|
4
4
|
const execAsync = promisify(exec);
|
|
5
5
|
/**
|
|
6
6
|
* Get PR info (number, state, url) for a branch using the GitHub CLI.
|
|
@@ -109,6 +109,142 @@ export function getPRTemplate() {
|
|
|
109
109
|
export function getPRComments(prNumber) {
|
|
110
110
|
return (run(`gh pr view ${prNumber} --json comments --jq '.comments[] | "- \\(.author.login): \\(.body)"'`) ?? "");
|
|
111
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Async version of getPRChecks.
|
|
114
|
+
*/
|
|
115
|
+
export async function getPRChecksAsync(prNumber) {
|
|
116
|
+
const output = await runAsync(`gh pr checks ${prNumber} --json name,state,bucket,link,description,workflow`);
|
|
117
|
+
if (!output)
|
|
118
|
+
return null;
|
|
119
|
+
try {
|
|
120
|
+
return JSON.parse(output);
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Async version of getPRReviews.
|
|
128
|
+
*/
|
|
129
|
+
export async function getPRReviewsAsync(prNumber) {
|
|
130
|
+
const output = await runAsync(`gh pr view ${prNumber} --json reviews`);
|
|
131
|
+
if (!output)
|
|
132
|
+
return null;
|
|
133
|
+
try {
|
|
134
|
+
const data = JSON.parse(output);
|
|
135
|
+
return data.reviews ?? null;
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Async version of getPRReviewComments.
|
|
143
|
+
*/
|
|
144
|
+
export async function getPRReviewCommentsAsync(prNumber) {
|
|
145
|
+
const output = await runAsync(`gh api repos/{owner}/{repo}/pulls/${prNumber}/comments --paginate`);
|
|
146
|
+
if (!output)
|
|
147
|
+
return null;
|
|
148
|
+
try {
|
|
149
|
+
return JSON.parse(output);
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Async version of getPRConversationComments.
|
|
157
|
+
*/
|
|
158
|
+
export async function getPRConversationCommentsAsync(prNumber) {
|
|
159
|
+
const output = await runAsync(`gh pr view ${prNumber} --json comments`);
|
|
160
|
+
if (!output)
|
|
161
|
+
return null;
|
|
162
|
+
try {
|
|
163
|
+
const data = JSON.parse(output);
|
|
164
|
+
return (data.comments ?? []).map((c) => ({
|
|
165
|
+
author: c.author?.login ?? "unknown",
|
|
166
|
+
body: c.body ?? "",
|
|
167
|
+
createdAt: c.createdAt ?? "",
|
|
168
|
+
}));
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Async version of getFailedCheckDetails.
|
|
176
|
+
*/
|
|
177
|
+
export async function getFailedCheckDetailsAsync(check) {
|
|
178
|
+
const detail = {
|
|
179
|
+
name: check.name,
|
|
180
|
+
workflow: check.workflow,
|
|
181
|
+
description: check.description,
|
|
182
|
+
link: check.link,
|
|
183
|
+
failed_step: null,
|
|
184
|
+
log: null,
|
|
185
|
+
};
|
|
186
|
+
const urlMatch = check.link?.match(/job\/(\d+)/);
|
|
187
|
+
if (!urlMatch)
|
|
188
|
+
return detail;
|
|
189
|
+
const jobId = urlMatch[1];
|
|
190
|
+
let stepStartMs = 0;
|
|
191
|
+
let stepEndMs = 0;
|
|
192
|
+
const jobOutput = await runAsync(`gh api repos/{owner}/{repo}/actions/jobs/${jobId}`);
|
|
193
|
+
if (jobOutput) {
|
|
194
|
+
try {
|
|
195
|
+
const job = JSON.parse(jobOutput);
|
|
196
|
+
const failedStep = job.steps?.find((s) => s.conclusion === "failure");
|
|
197
|
+
if (failedStep) {
|
|
198
|
+
detail.failed_step = failedStep.name;
|
|
199
|
+
stepStartMs = new Date(failedStep.started_at).getTime();
|
|
200
|
+
stepEndMs = new Date(failedStep.completed_at).getTime() + 999;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
catch { }
|
|
204
|
+
}
|
|
205
|
+
if (!stepStartMs)
|
|
206
|
+
return detail;
|
|
207
|
+
const logOutput = await runAsync(`gh api repos/{owner}/{repo}/actions/jobs/${jobId}/logs 2>/dev/null`);
|
|
208
|
+
if (logOutput) {
|
|
209
|
+
const lines = logOutput.split("\n");
|
|
210
|
+
const stepLines = lines.filter((line) => {
|
|
211
|
+
const m = line.match(/^(\d{4}-\d{2}-\d{2}T[\d:.]+Z)/);
|
|
212
|
+
if (!m)
|
|
213
|
+
return false;
|
|
214
|
+
const ms = new Date(m[1]).getTime();
|
|
215
|
+
return ms >= stepStartMs && ms <= stepEndMs;
|
|
216
|
+
});
|
|
217
|
+
const errorIdx = stepLines.findIndex((l) => l.includes("##[error]"));
|
|
218
|
+
const bounded = errorIdx !== -1 ? stepLines.slice(0, errorIdx) : stepLines;
|
|
219
|
+
const segments = [];
|
|
220
|
+
let current = [];
|
|
221
|
+
let inGroup = false;
|
|
222
|
+
for (const raw of bounded) {
|
|
223
|
+
const line = raw.replace(/^\d{4}-\d{2}-\d{2}T[\d:.]+Z\s*/, "");
|
|
224
|
+
if (line.startsWith("##[group]")) {
|
|
225
|
+
if (current.length) {
|
|
226
|
+
segments.push(current);
|
|
227
|
+
current = [];
|
|
228
|
+
}
|
|
229
|
+
inGroup = true;
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (line.startsWith("##[endgroup]")) {
|
|
233
|
+
inGroup = false;
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
if (line.startsWith("##["))
|
|
237
|
+
continue;
|
|
238
|
+
if (!inGroup)
|
|
239
|
+
current.push(line);
|
|
240
|
+
}
|
|
241
|
+
if (current.length)
|
|
242
|
+
segments.push(current);
|
|
243
|
+
if (segments.length)
|
|
244
|
+
detail.log = segments[segments.length - 1].join("\n");
|
|
245
|
+
}
|
|
246
|
+
return detail;
|
|
247
|
+
}
|
|
112
248
|
/**
|
|
113
249
|
* Fetch structured conversation comments on a pull request.
|
|
114
250
|
* Runs: `gh pr view <prNumber> --json comments`
|