@kody-ade/engine 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.
- package/LICENSE +21 -0
- package/README.md +322 -0
- package/dist/agent-runner.d.ts +4 -0
- package/dist/agent-runner.js +122 -0
- package/dist/bin/cli.js +11276 -0
- package/dist/ci/parse-inputs.d.ts +6 -0
- package/dist/ci/parse-inputs.js +76 -0
- package/dist/ci/parse-safety.d.ts +6 -0
- package/dist/ci/parse-safety.js +22 -0
- package/dist/cli/args.d.ts +13 -0
- package/dist/cli/args.js +42 -0
- package/dist/cli/litellm.d.ts +2 -0
- package/dist/cli/litellm.js +85 -0
- package/dist/cli/task-resolution.d.ts +2 -0
- package/dist/cli/task-resolution.js +41 -0
- package/dist/config.d.ts +49 -0
- package/dist/config.js +72 -0
- package/dist/context.d.ts +4 -0
- package/dist/context.js +83 -0
- package/dist/definitions.d.ts +3 -0
- package/dist/definitions.js +59 -0
- package/dist/entry.d.ts +1 -0
- package/dist/entry.js +236 -0
- package/dist/git-utils.d.ts +13 -0
- package/dist/git-utils.js +174 -0
- package/dist/github-api.d.ts +14 -0
- package/dist/github-api.js +114 -0
- package/dist/kody-utils.d.ts +1 -0
- package/dist/kody-utils.js +9 -0
- package/dist/learning/auto-learn.d.ts +2 -0
- package/dist/learning/auto-learn.js +169 -0
- package/dist/logger.d.ts +14 -0
- package/dist/logger.js +51 -0
- package/dist/memory.d.ts +1 -0
- package/dist/memory.js +20 -0
- package/dist/observer.d.ts +9 -0
- package/dist/observer.js +80 -0
- package/dist/pipeline/complexity.d.ts +3 -0
- package/dist/pipeline/complexity.js +12 -0
- package/dist/pipeline/executor-registry.d.ts +3 -0
- package/dist/pipeline/executor-registry.js +20 -0
- package/dist/pipeline/hooks.d.ts +17 -0
- package/dist/pipeline/hooks.js +110 -0
- package/dist/pipeline/questions.d.ts +2 -0
- package/dist/pipeline/questions.js +44 -0
- package/dist/pipeline/runner-selection.d.ts +2 -0
- package/dist/pipeline/runner-selection.js +13 -0
- package/dist/pipeline/state.d.ts +4 -0
- package/dist/pipeline/state.js +37 -0
- package/dist/pipeline.d.ts +3 -0
- package/dist/pipeline.js +213 -0
- package/dist/preflight.d.ts +1 -0
- package/dist/preflight.js +69 -0
- package/dist/retrospective.d.ts +26 -0
- package/dist/retrospective.js +211 -0
- package/dist/stages/agent.d.ts +2 -0
- package/dist/stages/agent.js +94 -0
- package/dist/stages/gate.d.ts +2 -0
- package/dist/stages/gate.js +32 -0
- package/dist/stages/review.d.ts +2 -0
- package/dist/stages/review.js +32 -0
- package/dist/stages/ship.d.ts +3 -0
- package/dist/stages/ship.js +154 -0
- package/dist/stages/verify.d.ts +2 -0
- package/dist/stages/verify.js +94 -0
- package/dist/types.d.ts +61 -0
- package/dist/types.js +1 -0
- package/dist/validators.d.ts +8 -0
- package/dist/validators.js +42 -0
- package/dist/verify-runner.d.ts +11 -0
- package/dist/verify-runner.js +110 -0
- package/kody.config.schema.json +299 -0
- package/package.json +39 -0
- package/prompts/autofix.md +52 -0
- package/prompts/build.md +26 -0
- package/prompts/decompose.md +77 -0
- package/prompts/plan.md +65 -0
- package/prompts/review-fix.md +27 -0
- package/prompts/review.md +115 -0
- package/prompts/taskify-ticket.md +122 -0
- package/prompts/taskify.md +70 -0
- package/templates/kody-watch.yml +57 -0
- package/templates/kody.yml +450 -0
- package/templates/watch-agents/branch-cleanup/agent.json +7 -0
- package/templates/watch-agents/branch-cleanup/agent.md +13 -0
- package/templates/watch-agents/dependency-checker/agent.json +7 -0
- package/templates/watch-agents/dependency-checker/agent.md +14 -0
- package/templates/watch-agents/readme-health/agent.json +7 -0
- package/templates/watch-agents/readme-health/agent.md +17 -0
- package/templates/watch-agents/stale-pr-reviewer/agent.json +7 -0
- package/templates/watch-agents/stale-pr-reviewer/agent.md +13 -0
- package/templates/watch-agents/todo-scanner/agent.json +7 -0
- package/templates/watch-agents/todo-scanner/agent.md +10 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses @kody / /kody comment body into structured inputs.
|
|
3
|
+
* Run by the parse job in GitHub Actions.
|
|
4
|
+
* Reads from env, writes to $GITHUB_OUTPUT.
|
|
5
|
+
*/
|
|
6
|
+
import * as fs from "fs";
|
|
7
|
+
const outputFile = process.env.GITHUB_OUTPUT;
|
|
8
|
+
const triggerType = process.env.TRIGGER_TYPE ?? "dispatch";
|
|
9
|
+
function output(key, value) {
|
|
10
|
+
if (outputFile) {
|
|
11
|
+
fs.appendFileSync(outputFile, `${key}=${value}\n`);
|
|
12
|
+
}
|
|
13
|
+
console.log(`${key}=${value}`);
|
|
14
|
+
}
|
|
15
|
+
// For workflow_dispatch, pass through inputs
|
|
16
|
+
if (triggerType === "dispatch") {
|
|
17
|
+
output("task_id", process.env.INPUT_TASK_ID ?? "");
|
|
18
|
+
output("mode", process.env.INPUT_MODE ?? "full");
|
|
19
|
+
output("from_stage", process.env.INPUT_FROM_STAGE ?? "");
|
|
20
|
+
output("issue_number", process.env.INPUT_ISSUE_NUMBER ?? "");
|
|
21
|
+
output("feedback", process.env.INPUT_FEEDBACK ?? "");
|
|
22
|
+
output("valid", process.env.INPUT_TASK_ID ? "true" : "false");
|
|
23
|
+
output("trigger_type", "dispatch");
|
|
24
|
+
process.exit(0);
|
|
25
|
+
}
|
|
26
|
+
// For issue_comment, parse the comment body
|
|
27
|
+
const commentBody = process.env.COMMENT_BODY ?? "";
|
|
28
|
+
const issueNumber = process.env.ISSUE_NUMBER ?? "";
|
|
29
|
+
// Match: @kody [mode] [task-id] [--from stage] [--feedback "text"]
|
|
30
|
+
const kodyMatch = commentBody.match(/(?:@kody|\/kody)\s*(.*)/i);
|
|
31
|
+
if (!kodyMatch) {
|
|
32
|
+
output("valid", "false");
|
|
33
|
+
output("trigger_type", "comment");
|
|
34
|
+
process.exit(0);
|
|
35
|
+
}
|
|
36
|
+
const parts = kodyMatch[1].trim().split(/\s+/);
|
|
37
|
+
const validModes = ["full", "rerun", "status"];
|
|
38
|
+
let mode = "full";
|
|
39
|
+
let taskId = "";
|
|
40
|
+
let fromStage = "";
|
|
41
|
+
let feedback = "";
|
|
42
|
+
let i = 0;
|
|
43
|
+
// First arg: mode or task-id
|
|
44
|
+
if (parts[i] && validModes.includes(parts[i])) {
|
|
45
|
+
mode = parts[i];
|
|
46
|
+
i++;
|
|
47
|
+
}
|
|
48
|
+
// Second arg: task-id
|
|
49
|
+
if (parts[i] && !parts[i].startsWith("--")) {
|
|
50
|
+
taskId = parts[i];
|
|
51
|
+
i++;
|
|
52
|
+
}
|
|
53
|
+
// Named args
|
|
54
|
+
while (i < parts.length) {
|
|
55
|
+
if (parts[i] === "--from" && parts[i + 1]) {
|
|
56
|
+
fromStage = parts[i + 1];
|
|
57
|
+
i += 2;
|
|
58
|
+
}
|
|
59
|
+
else if (parts[i] === "--feedback" && parts[i + 1]) {
|
|
60
|
+
// Collect quoted feedback
|
|
61
|
+
const rest = parts.slice(i + 1).join(" ");
|
|
62
|
+
const quoted = rest.match(/^"([^"]*)"/);
|
|
63
|
+
feedback = quoted ? quoted[1] : parts[i + 1];
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
i++;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
output("task_id", taskId);
|
|
71
|
+
output("mode", mode);
|
|
72
|
+
output("from_stage", fromStage);
|
|
73
|
+
output("issue_number", issueNumber);
|
|
74
|
+
output("feedback", feedback);
|
|
75
|
+
output("valid", taskId ? "true" : "false");
|
|
76
|
+
output("trigger_type", "comment");
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates that a comment trigger is safe to execute.
|
|
3
|
+
* Run by the parse job in GitHub Actions.
|
|
4
|
+
* Reads from env, writes to $GITHUB_OUTPUT.
|
|
5
|
+
*/
|
|
6
|
+
import * as fs from "fs";
|
|
7
|
+
const ALLOWED_ASSOCIATIONS = ["COLLABORATOR", "MEMBER", "OWNER"];
|
|
8
|
+
const association = process.env.COMMENT_AUTHOR_ASSOCIATION ?? "";
|
|
9
|
+
const outputFile = process.env.GITHUB_OUTPUT;
|
|
10
|
+
function output(key, value) {
|
|
11
|
+
if (outputFile) {
|
|
12
|
+
fs.appendFileSync(outputFile, `${key}=${value}\n`);
|
|
13
|
+
}
|
|
14
|
+
console.log(`${key}=${value}`);
|
|
15
|
+
}
|
|
16
|
+
if (!ALLOWED_ASSOCIATIONS.includes(association)) {
|
|
17
|
+
output("valid", "false");
|
|
18
|
+
output("reason", `Author association '${association}' not in allowlist: ${ALLOWED_ASSOCIATIONS.join(", ")}`);
|
|
19
|
+
process.exit(0);
|
|
20
|
+
}
|
|
21
|
+
output("valid", "true");
|
|
22
|
+
output("reason", "");
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface CliInput {
|
|
2
|
+
command: "run" | "rerun" | "fix" | "status";
|
|
3
|
+
taskId?: string;
|
|
4
|
+
task?: string;
|
|
5
|
+
fromStage?: string;
|
|
6
|
+
dryRun?: boolean;
|
|
7
|
+
cwd?: string;
|
|
8
|
+
issueNumber?: number;
|
|
9
|
+
feedback?: string;
|
|
10
|
+
local?: boolean;
|
|
11
|
+
complexity?: "low" | "medium" | "high";
|
|
12
|
+
}
|
|
13
|
+
export declare function parseArgs(): CliInput;
|
package/dist/cli/args.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const isCI = !!process.env.GITHUB_ACTIONS;
|
|
2
|
+
function getArg(args, flag) {
|
|
3
|
+
const idx = args.indexOf(flag);
|
|
4
|
+
if (idx !== -1 && args[idx + 1] && !args[idx + 1].startsWith("--")) {
|
|
5
|
+
return args[idx + 1];
|
|
6
|
+
}
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
function hasFlag(args, flag) {
|
|
10
|
+
return args.includes(flag);
|
|
11
|
+
}
|
|
12
|
+
export function parseArgs() {
|
|
13
|
+
const args = process.argv.slice(2);
|
|
14
|
+
if (hasFlag(args, "--help") || hasFlag(args, "-h") || args.length === 0) {
|
|
15
|
+
console.log(`Usage:
|
|
16
|
+
kody run --task-id <id> [--task "<desc>"] [--cwd <path>] [--issue-number <n>] [--complexity low|medium|high] [--feedback "<text>"] [--local] [--dry-run]
|
|
17
|
+
kody rerun --task-id <id> --from <stage> [--cwd <path>] [--issue-number <n>]
|
|
18
|
+
kody fix --task-id <id> [--cwd <path>] [--issue-number <n>] [--feedback "<text>"]
|
|
19
|
+
kody status --task-id <id> [--cwd <path>]
|
|
20
|
+
kody --help`);
|
|
21
|
+
process.exit(0);
|
|
22
|
+
}
|
|
23
|
+
const command = args[0];
|
|
24
|
+
if (!["run", "rerun", "fix", "status"].includes(command)) {
|
|
25
|
+
console.error(`Unknown command: ${command}`);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
const issueStr = getArg(args, "--issue-number") ?? process.env.ISSUE_NUMBER;
|
|
29
|
+
const localFlag = hasFlag(args, "--local");
|
|
30
|
+
return {
|
|
31
|
+
command,
|
|
32
|
+
taskId: getArg(args, "--task-id") ?? process.env.TASK_ID,
|
|
33
|
+
task: getArg(args, "--task"),
|
|
34
|
+
fromStage: getArg(args, "--from") ?? process.env.FROM_STAGE,
|
|
35
|
+
dryRun: hasFlag(args, "--dry-run") || process.env.DRY_RUN === "true",
|
|
36
|
+
cwd: getArg(args, "--cwd"),
|
|
37
|
+
issueNumber: issueStr ? parseInt(issueStr, 10) : undefined,
|
|
38
|
+
feedback: getArg(args, "--feedback") ?? process.env.FEEDBACK,
|
|
39
|
+
local: localFlag || (!isCI && !hasFlag(args, "--no-local")),
|
|
40
|
+
complexity: (getArg(args, "--complexity") ?? process.env.COMPLEXITY),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { execFileSync } from "child_process";
|
|
4
|
+
import { logger } from "../logger.js";
|
|
5
|
+
export async function checkLitellmHealth(url) {
|
|
6
|
+
try {
|
|
7
|
+
const response = await fetch(`${url}/health`, { signal: AbortSignal.timeout(3000) });
|
|
8
|
+
return response.ok;
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export async function tryStartLitellm(url, projectDir) {
|
|
15
|
+
const configPath = path.join(projectDir, "litellm-config.yaml");
|
|
16
|
+
if (!fs.existsSync(configPath)) {
|
|
17
|
+
logger.warn("litellm-config.yaml not found — cannot start proxy");
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
// Extract port from URL
|
|
21
|
+
const portMatch = url.match(/:(\d+)/);
|
|
22
|
+
const port = portMatch ? portMatch[1] : "4000";
|
|
23
|
+
// Check if litellm is installed
|
|
24
|
+
try {
|
|
25
|
+
execFileSync("litellm", ["--version"], { timeout: 5000, stdio: "pipe" });
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
try {
|
|
29
|
+
execFileSync("python3", ["-m", "litellm", "--version"], { timeout: 5000, stdio: "pipe" });
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
logger.warn("litellm not installed (pip install 'litellm[proxy]')");
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
logger.info(`Starting LiteLLM proxy on port ${port}...`);
|
|
37
|
+
// Determine command
|
|
38
|
+
let cmd;
|
|
39
|
+
let args;
|
|
40
|
+
try {
|
|
41
|
+
execFileSync("litellm", ["--version"], { timeout: 5000, stdio: "pipe" });
|
|
42
|
+
cmd = "litellm";
|
|
43
|
+
args = ["--config", configPath, "--port", port];
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
cmd = "python3";
|
|
47
|
+
args = ["-m", "litellm", "--config", configPath, "--port", port];
|
|
48
|
+
}
|
|
49
|
+
// Load API key env vars from project .env (only *_API_KEY patterns)
|
|
50
|
+
const dotenvPath = path.join(projectDir, ".env");
|
|
51
|
+
const dotenvVars = {};
|
|
52
|
+
if (fs.existsSync(dotenvPath)) {
|
|
53
|
+
for (const line of fs.readFileSync(dotenvPath, "utf-8").split("\n")) {
|
|
54
|
+
const match = line.match(/^([A-Z_][A-Z0-9_]*_API_KEY)=(.*)$/);
|
|
55
|
+
if (match)
|
|
56
|
+
dotenvVars[match[1]] = match[2];
|
|
57
|
+
}
|
|
58
|
+
if (Object.keys(dotenvVars).length > 0) {
|
|
59
|
+
logger.info(` Loaded API keys: ${Object.keys(dotenvVars).join(", ")}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const { spawn } = await import("child_process");
|
|
63
|
+
const child = spawn(cmd, args, {
|
|
64
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
65
|
+
detached: true,
|
|
66
|
+
env: { ...process.env, ...dotenvVars },
|
|
67
|
+
});
|
|
68
|
+
// Capture stderr for debugging
|
|
69
|
+
let proxyStderr = "";
|
|
70
|
+
child.stderr?.on("data", (chunk) => { proxyStderr += chunk.toString(); });
|
|
71
|
+
// Wait for health
|
|
72
|
+
for (let i = 0; i < 30; i++) {
|
|
73
|
+
await new Promise((r) => setTimeout(r, 2000));
|
|
74
|
+
if (await checkLitellmHealth(url)) {
|
|
75
|
+
logger.info(`LiteLLM proxy ready at ${url}`);
|
|
76
|
+
return child;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (proxyStderr) {
|
|
80
|
+
logger.warn(`LiteLLM stderr: ${proxyStderr.slice(-1000)}`);
|
|
81
|
+
}
|
|
82
|
+
logger.warn("LiteLLM proxy failed to start within 60s");
|
|
83
|
+
child.kill();
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { execFileSync } from "child_process";
|
|
4
|
+
export function findLatestTaskForIssue(issueNumber, projectDir) {
|
|
5
|
+
const tasksDir = path.join(projectDir, ".tasks");
|
|
6
|
+
if (!fs.existsSync(tasksDir))
|
|
7
|
+
return null;
|
|
8
|
+
// Only consider directories (not files)
|
|
9
|
+
const allDirs = fs.readdirSync(tasksDir, { withFileTypes: true })
|
|
10
|
+
.filter((d) => d.isDirectory())
|
|
11
|
+
.map((d) => d.name)
|
|
12
|
+
.sort()
|
|
13
|
+
.reverse();
|
|
14
|
+
// Direct match: tasks starting with issue number
|
|
15
|
+
const prefix = `${issueNumber}-`;
|
|
16
|
+
const direct = allDirs.find((d) => d.startsWith(prefix));
|
|
17
|
+
if (direct)
|
|
18
|
+
return direct;
|
|
19
|
+
// Fallback for PR comments: extract issue number from current git branch
|
|
20
|
+
// Branch format: <issueNum>--<slug> (e.g., 1031--security-8x-route)
|
|
21
|
+
try {
|
|
22
|
+
const branch = execFileSync("git", ["branch", "--show-current"], {
|
|
23
|
+
encoding: "utf-8", cwd: projectDir, timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
|
|
24
|
+
}).trim();
|
|
25
|
+
const branchIssueMatch = branch.match(/^(\d+)-/);
|
|
26
|
+
if (branchIssueMatch) {
|
|
27
|
+
const branchIssueNum = branchIssueMatch[1];
|
|
28
|
+
const branchPrefix = `${branchIssueNum}-`;
|
|
29
|
+
const fromBranch = allDirs.find((d) => d.startsWith(branchPrefix));
|
|
30
|
+
if (fromBranch)
|
|
31
|
+
return fromBranch;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch { /* ignore */ }
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
export function generateTaskId() {
|
|
38
|
+
const now = new Date();
|
|
39
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
40
|
+
return `${String(now.getFullYear()).slice(2)}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
|
|
41
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export interface RunnerConfig {
|
|
2
|
+
type: "claude-code";
|
|
3
|
+
}
|
|
4
|
+
export interface KodyConfig {
|
|
5
|
+
quality: {
|
|
6
|
+
typecheck: string;
|
|
7
|
+
lint: string;
|
|
8
|
+
lintFix: string;
|
|
9
|
+
format: string;
|
|
10
|
+
formatFix: string;
|
|
11
|
+
testUnit: string;
|
|
12
|
+
};
|
|
13
|
+
git: {
|
|
14
|
+
defaultBranch: string;
|
|
15
|
+
userEmail?: string;
|
|
16
|
+
userName?: string;
|
|
17
|
+
};
|
|
18
|
+
github: {
|
|
19
|
+
owner: string;
|
|
20
|
+
repo: string;
|
|
21
|
+
};
|
|
22
|
+
paths: {
|
|
23
|
+
taskDir: string;
|
|
24
|
+
};
|
|
25
|
+
agent: {
|
|
26
|
+
runner?: string;
|
|
27
|
+
modelMap: {
|
|
28
|
+
cheap: string;
|
|
29
|
+
mid: string;
|
|
30
|
+
strong: string;
|
|
31
|
+
};
|
|
32
|
+
litellmUrl?: string;
|
|
33
|
+
usePerStageRouting?: boolean;
|
|
34
|
+
defaultRunner?: string;
|
|
35
|
+
runners?: Record<string, RunnerConfig>;
|
|
36
|
+
stageRunners?: Record<string, string>;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export declare const SIGKILL_GRACE_MS = 5000;
|
|
40
|
+
export declare const MAX_PR_TITLE_LENGTH = 72;
|
|
41
|
+
export declare const STDERR_TAIL_CHARS = 500;
|
|
42
|
+
export declare const API_TIMEOUT_MS = 30000;
|
|
43
|
+
export declare const DEFAULT_MAX_FIX_ATTEMPTS = 2;
|
|
44
|
+
export declare const AGENT_RETRY_DELAY_MS = 2000;
|
|
45
|
+
export declare const VERIFY_COMMAND_TIMEOUT_MS: number;
|
|
46
|
+
export declare const FIX_COMMAND_TIMEOUT_MS: number;
|
|
47
|
+
export declare function setConfigDir(dir: string): void;
|
|
48
|
+
export declare function getProjectConfig(): KodyConfig;
|
|
49
|
+
export declare function resetProjectConfig(): void;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { logger } from "./logger.js";
|
|
4
|
+
const DEFAULT_CONFIG = {
|
|
5
|
+
quality: {
|
|
6
|
+
typecheck: "pnpm -s tsc --noEmit",
|
|
7
|
+
lint: "pnpm -s lint",
|
|
8
|
+
lintFix: "pnpm lint:fix",
|
|
9
|
+
format: "pnpm -s format:check",
|
|
10
|
+
formatFix: "pnpm format:fix",
|
|
11
|
+
testUnit: "pnpm -s test",
|
|
12
|
+
},
|
|
13
|
+
git: {
|
|
14
|
+
defaultBranch: "dev",
|
|
15
|
+
},
|
|
16
|
+
github: {
|
|
17
|
+
owner: "",
|
|
18
|
+
repo: "",
|
|
19
|
+
},
|
|
20
|
+
paths: {
|
|
21
|
+
taskDir: ".tasks",
|
|
22
|
+
},
|
|
23
|
+
agent: {
|
|
24
|
+
runner: "claude-code",
|
|
25
|
+
defaultRunner: "claude",
|
|
26
|
+
modelMap: { cheap: "haiku", mid: "sonnet", strong: "opus" },
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
// Pipeline constants
|
|
30
|
+
export const SIGKILL_GRACE_MS = 5000;
|
|
31
|
+
export const MAX_PR_TITLE_LENGTH = 72;
|
|
32
|
+
export const STDERR_TAIL_CHARS = 500;
|
|
33
|
+
export const API_TIMEOUT_MS = 30_000;
|
|
34
|
+
export const DEFAULT_MAX_FIX_ATTEMPTS = 2;
|
|
35
|
+
export const AGENT_RETRY_DELAY_MS = 2000;
|
|
36
|
+
export const VERIFY_COMMAND_TIMEOUT_MS = 5 * 60 * 1000;
|
|
37
|
+
export const FIX_COMMAND_TIMEOUT_MS = 2 * 60 * 1000;
|
|
38
|
+
let _config = null;
|
|
39
|
+
let _configDir = null;
|
|
40
|
+
export function setConfigDir(dir) {
|
|
41
|
+
_configDir = dir;
|
|
42
|
+
_config = null;
|
|
43
|
+
}
|
|
44
|
+
export function getProjectConfig() {
|
|
45
|
+
if (_config)
|
|
46
|
+
return _config;
|
|
47
|
+
const configPath = path.join(_configDir ?? process.cwd(), "kody.config.json");
|
|
48
|
+
if (fs.existsSync(configPath)) {
|
|
49
|
+
try {
|
|
50
|
+
const raw = JSON.parse(fs.readFileSync(configPath, "utf-8"));
|
|
51
|
+
_config = {
|
|
52
|
+
quality: { ...DEFAULT_CONFIG.quality, ...raw.quality },
|
|
53
|
+
git: { ...DEFAULT_CONFIG.git, ...raw.git },
|
|
54
|
+
github: { ...DEFAULT_CONFIG.github, ...raw.github },
|
|
55
|
+
paths: { ...DEFAULT_CONFIG.paths, ...raw.paths },
|
|
56
|
+
agent: { ...DEFAULT_CONFIG.agent, ...raw.agent },
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
logger.warn("kody.config.json is invalid JSON — using defaults");
|
|
61
|
+
_config = { ...DEFAULT_CONFIG };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
_config = { ...DEFAULT_CONFIG };
|
|
66
|
+
}
|
|
67
|
+
return _config;
|
|
68
|
+
}
|
|
69
|
+
export function resetProjectConfig() {
|
|
70
|
+
_config = null;
|
|
71
|
+
_configDir = null;
|
|
72
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function readPromptFile(stageName: string): string;
|
|
2
|
+
export declare function injectTaskContext(prompt: string, taskId: string, taskDir: string, feedback?: string): string;
|
|
3
|
+
export declare function buildFullPrompt(stageName: string, taskId: string, taskDir: string, projectDir: string, feedback?: string): string;
|
|
4
|
+
export declare function resolveModel(modelTier: string, stageName?: string): string;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { readProjectMemory } from "./memory.js";
|
|
4
|
+
import { getProjectConfig } from "./config.js";
|
|
5
|
+
const DEFAULT_MODEL_MAP = {
|
|
6
|
+
cheap: "haiku",
|
|
7
|
+
mid: "sonnet",
|
|
8
|
+
strong: "opus",
|
|
9
|
+
};
|
|
10
|
+
const MAX_TASK_CONTEXT_PLAN = 1500;
|
|
11
|
+
const MAX_TASK_CONTEXT_SPEC = 2000;
|
|
12
|
+
export function readPromptFile(stageName) {
|
|
13
|
+
const scriptDir = new URL(".", import.meta.url).pathname;
|
|
14
|
+
// Try multiple resolution paths (dev: src/../prompts, prod: dist/bin/../../prompts)
|
|
15
|
+
const candidates = [
|
|
16
|
+
path.resolve(scriptDir, "..", "prompts", `${stageName}.md`),
|
|
17
|
+
path.resolve(scriptDir, "..", "..", "prompts", `${stageName}.md`),
|
|
18
|
+
];
|
|
19
|
+
for (const candidate of candidates) {
|
|
20
|
+
if (fs.existsSync(candidate)) {
|
|
21
|
+
return fs.readFileSync(candidate, "utf-8");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
throw new Error(`Prompt file not found: tried ${candidates.join(", ")}`);
|
|
25
|
+
}
|
|
26
|
+
export function injectTaskContext(prompt, taskId, taskDir, feedback) {
|
|
27
|
+
let context = `## Task Context\n`;
|
|
28
|
+
context += `Task ID: ${taskId}\n`;
|
|
29
|
+
context += `Task Directory: ${taskDir}\n`;
|
|
30
|
+
const taskMdPath = path.join(taskDir, "task.md");
|
|
31
|
+
if (fs.existsSync(taskMdPath)) {
|
|
32
|
+
const taskMd = fs.readFileSync(taskMdPath, "utf-8");
|
|
33
|
+
context += `\n## Task Description\n${taskMd}\n`;
|
|
34
|
+
}
|
|
35
|
+
const taskJsonPath = path.join(taskDir, "task.json");
|
|
36
|
+
if (fs.existsSync(taskJsonPath)) {
|
|
37
|
+
try {
|
|
38
|
+
const taskDef = JSON.parse(fs.readFileSync(taskJsonPath, "utf-8"));
|
|
39
|
+
context += `\n## Task Classification\n`;
|
|
40
|
+
context += `Type: ${taskDef.task_type ?? "unknown"}\n`;
|
|
41
|
+
context += `Title: ${taskDef.title ?? "unknown"}\n`;
|
|
42
|
+
context += `Risk: ${taskDef.risk_level ?? "unknown"}\n`;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// Ignore parse errors
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const specPath = path.join(taskDir, "spec.md");
|
|
49
|
+
if (fs.existsSync(specPath)) {
|
|
50
|
+
const spec = fs.readFileSync(specPath, "utf-8");
|
|
51
|
+
const truncated = spec.slice(0, MAX_TASK_CONTEXT_SPEC);
|
|
52
|
+
context += `\n## Spec Summary\n${truncated}${spec.length > MAX_TASK_CONTEXT_SPEC ? "\n..." : ""}\n`;
|
|
53
|
+
}
|
|
54
|
+
const planPath = path.join(taskDir, "plan.md");
|
|
55
|
+
if (fs.existsSync(planPath)) {
|
|
56
|
+
const plan = fs.readFileSync(planPath, "utf-8");
|
|
57
|
+
const truncated = plan.slice(0, MAX_TASK_CONTEXT_PLAN);
|
|
58
|
+
context += `\n## Plan Summary\n${truncated}${plan.length > MAX_TASK_CONTEXT_PLAN ? "\n..." : ""}\n`;
|
|
59
|
+
}
|
|
60
|
+
if (feedback) {
|
|
61
|
+
context += `\n## Human Feedback\n${feedback}\n`;
|
|
62
|
+
}
|
|
63
|
+
return prompt.replace("{{TASK_CONTEXT}}", context);
|
|
64
|
+
}
|
|
65
|
+
export function buildFullPrompt(stageName, taskId, taskDir, projectDir, feedback) {
|
|
66
|
+
const memory = readProjectMemory(projectDir);
|
|
67
|
+
const promptTemplate = readPromptFile(stageName);
|
|
68
|
+
const prompt = injectTaskContext(promptTemplate, taskId, taskDir, feedback);
|
|
69
|
+
return memory ? `${memory}\n---\n\n${prompt}` : prompt;
|
|
70
|
+
}
|
|
71
|
+
export function resolveModel(modelTier, stageName) {
|
|
72
|
+
const config = getProjectConfig();
|
|
73
|
+
// Per-stage routing: use stage name as LiteLLM alias
|
|
74
|
+
if (config.agent.usePerStageRouting && stageName) {
|
|
75
|
+
return stageName;
|
|
76
|
+
}
|
|
77
|
+
// Config model map (may be LiteLLM aliases or direct model names)
|
|
78
|
+
const mapped = config.agent.modelMap[modelTier];
|
|
79
|
+
if (mapped)
|
|
80
|
+
return mapped;
|
|
81
|
+
// Fallback to defaults
|
|
82
|
+
return DEFAULT_MODEL_MAP[modelTier] ?? "sonnet";
|
|
83
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export const STAGES = [
|
|
2
|
+
{
|
|
3
|
+
name: "taskify",
|
|
4
|
+
type: "agent",
|
|
5
|
+
modelTier: "cheap",
|
|
6
|
+
timeout: 300_000,
|
|
7
|
+
maxRetries: 1,
|
|
8
|
+
outputFile: "task.json",
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
name: "plan",
|
|
12
|
+
type: "agent",
|
|
13
|
+
modelTier: "strong",
|
|
14
|
+
timeout: 600_000,
|
|
15
|
+
maxRetries: 1,
|
|
16
|
+
outputFile: "plan.md",
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
name: "build",
|
|
20
|
+
type: "agent",
|
|
21
|
+
modelTier: "mid",
|
|
22
|
+
timeout: 1_200_000,
|
|
23
|
+
maxRetries: 1,
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: "verify",
|
|
27
|
+
type: "gate",
|
|
28
|
+
modelTier: "cheap",
|
|
29
|
+
timeout: 300_000,
|
|
30
|
+
maxRetries: 2,
|
|
31
|
+
retryWithAgent: "autofix",
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: "review",
|
|
35
|
+
type: "agent",
|
|
36
|
+
modelTier: "strong",
|
|
37
|
+
timeout: 600_000,
|
|
38
|
+
maxRetries: 1,
|
|
39
|
+
outputFile: "review.md",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: "review-fix",
|
|
43
|
+
type: "agent",
|
|
44
|
+
modelTier: "mid",
|
|
45
|
+
timeout: 600_000,
|
|
46
|
+
maxRetries: 1,
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: "ship",
|
|
50
|
+
type: "deterministic",
|
|
51
|
+
modelTier: "cheap",
|
|
52
|
+
timeout: 120_000,
|
|
53
|
+
maxRetries: 1,
|
|
54
|
+
outputFile: "ship.md",
|
|
55
|
+
},
|
|
56
|
+
];
|
|
57
|
+
export function getStage(name) {
|
|
58
|
+
return STAGES.find((s) => s.name === name);
|
|
59
|
+
}
|
package/dist/entry.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|