@gaberrb/polypus 0.2.1 โ†’ 0.2.2

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/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![npm](https://img.shields.io/npm/v/@gaberrb/polypus?color=9266F5&logo=npm)](https://www.npmjs.com/package/@gaberrb/polypus)
5
5
  [![license](https://img.shields.io/badge/license-MIT-AB8EFA)](LICENSE)
6
6
 
7
- **๐Ÿ“– Site:** https://gaberrb.github.io/polypus/ ยท **๐Ÿ“ฆ npm:** [`@gaberrb/polypus`](https://www.npmjs.com/package/@gaberrb/polypus)
7
+ **๐Ÿ“– Site:** https://gaberrb.github.io/polypus/ ยท **๐Ÿ“ฆ npm:** [`@gaberrb/polypus`](https://www.npmjs.com/package/@gaberrb/polypus) ยท **๐Ÿ“ Changelog:** [CHANGELOG.md](https://github.com/GaberRB/polypus/blob/main/CHANGELOG.md)
8
8
 
9
9
  An agentic coding harness that makes **any** AI API generate and apply code โ€”
10
10
  even models **without** native tool-calling. Bring your own keys: OpenRouter,
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  // src/cli/index.ts
4
4
  import { Command } from "commander";
5
5
  import { createRequire } from "module";
6
- import pc10 from "picocolors";
6
+ import pc12 from "picocolors";
7
7
 
8
8
  // src/cli/commands/add-agent.ts
9
9
  import pc from "picocolors";
@@ -80,6 +80,16 @@ var en = {
80
80
  "cli.cmd.run": "Run a coding task with an agent",
81
81
  "cli.cmd.swarm": "Split a task across multiple agents working in parallel git worktrees",
82
82
  "cli.cmd.models": "Browse OpenRouter models (price, context, tool support)",
83
+ "cli.cmd.prd": "Generate a PRD from a GitHub issue (uses a free OpenRouter model)",
84
+ "cli.arg.prdIssue": "issue number to turn into a PRD",
85
+ "cli.cmd.review": "Review a pull request diff (uses a free OpenRouter model)",
86
+ "cli.arg.reviewPr": "pull request number to review",
87
+ "cli.opt.out": "write output to this file instead of stdout",
88
+ "cli.opt.input": 'read input from a file (or "-" for stdin) instead of calling gh',
89
+ "prd.wrote": "\u2713 PRD written to {path}",
90
+ "review.wrote": "\u2713 Review written to {path}",
91
+ "cli.invalidRef": "Invalid number '{ref}': expected a numeric issue/PR number.",
92
+ "cli.stdinTty": "--input - expects piped stdin, but none was provided.",
83
93
  "cli.opt.search": "filter by id/name substring",
84
94
  "cli.opt.toolsOnly": "only models that support tool-calling",
85
95
  "cli.opt.free": "only free models",
@@ -270,6 +280,16 @@ var ptBR = {
270
280
  "cli.cmd.run": "Executa uma tarefa de c\xF3digo com um agente",
271
281
  "cli.cmd.swarm": "Divide uma tarefa entre v\xE1rios agentes trabalhando em paralelo em git worktrees",
272
282
  "cli.cmd.models": "Explora os modelos do OpenRouter (pre\xE7o, contexto, suporte a tools)",
283
+ "cli.cmd.prd": "Gera um PRD a partir de uma issue do GitHub (usa um modelo gratuito do OpenRouter)",
284
+ "cli.arg.prdIssue": "n\xFAmero da issue para transformar em PRD",
285
+ "cli.cmd.review": "Revisa o diff de um pull request (usa um modelo gratuito do OpenRouter)",
286
+ "cli.arg.reviewPr": "n\xFAmero do pull request a revisar",
287
+ "cli.opt.out": "grava a sa\xEDda neste arquivo em vez do stdout",
288
+ "cli.opt.input": 'l\xEA a entrada de um arquivo (ou "-" para stdin) em vez de chamar o gh',
289
+ "prd.wrote": "\u2713 PRD gravado em {path}",
290
+ "review.wrote": "\u2713 Review gravado em {path}",
291
+ "cli.invalidRef": "N\xFAmero inv\xE1lido '{ref}': esperado um n\xFAmero de issue/PR.",
292
+ "cli.stdinTty": "--input - espera entrada via pipe (stdin), mas nenhuma foi fornecida.",
273
293
  "cli.opt.search": "filtra por trecho do id/nome",
274
294
  "cli.opt.toolsOnly": "apenas modelos com suporte a tool-calling",
275
295
  "cli.opt.free": "apenas modelos gratuitos",
@@ -1448,6 +1468,14 @@ function getTool(name) {
1448
1468
  // src/core/agent/correction.ts
1449
1469
  import { readFile as readFile4, readdir as readdir2 } from "fs/promises";
1450
1470
  import { dirname as dirname2, resolve as resolve6 } from "path";
1471
+ function truncationGuidance(toolName) {
1472
+ const fileHint = toolName === "write_file" || toolName === "edit_file" ? " Write large files in parts: create the file with the first chunk via write_file, then append the rest with edit_file in the next steps." : "";
1473
+ return [
1474
+ "AUTO-CORRECTION \u2014 your previous response was cut off at the output token limit,",
1475
+ "so the tool call was incomplete (e.g. 'content' missing or partial).",
1476
+ "Do NOT resend the same large output \u2014 produce a smaller one this time." + fileHint
1477
+ ].join("\n");
1478
+ }
1451
1479
  async function buildCorrection(call, output, deps) {
1452
1480
  const deterministic = await deterministicCorrection(call, output, deps);
1453
1481
  if (deterministic) return deterministic;
@@ -1455,6 +1483,7 @@ async function buildCorrection(call, output, deps) {
1455
1483
  return null;
1456
1484
  }
1457
1485
  async function deterministicCorrection(call, output, deps) {
1486
+ if (deps.truncated) return truncationGuidance(call.name);
1458
1487
  const path = typeof call.arguments.path === "string" ? call.arguments.path : void 0;
1459
1488
  if (call.name === "edit_file" && /was not found/i.test(output) && path) {
1460
1489
  const search = typeof call.arguments.search === "string" ? call.arguments.search : "";
@@ -1694,7 +1723,18 @@ async function runAgent(opts) {
1694
1723
  const { toolCalls, text: text2 } = driver.parse(response);
1695
1724
  messages.push(driver.assistantMessage(response, toolCalls));
1696
1725
  if (text2) events?.onAssistantText?.(text2);
1726
+ const truncated = response.finishReason === "length" || response.finishReason === "max_tokens";
1697
1727
  if (toolCalls.length === 0) {
1728
+ if (truncated && autoCorrect) {
1729
+ if (consecutiveNoTool < maxReprompts) {
1730
+ consecutiveNoTool++;
1731
+ const guidance = truncationGuidance();
1732
+ events?.onCorrection?.({ id: "trunc", name: "", arguments: {} }, guidance);
1733
+ messages.push({ role: "user", content: guidance });
1734
+ continue;
1735
+ }
1736
+ return { finished: false, reason: "stalled", steps: step, messages, usage };
1737
+ }
1698
1738
  const stalled = text2.trim().length === 0 || looksLikeStall(text2);
1699
1739
  if (stalled) {
1700
1740
  if (consecutiveNoTool < maxReprompts) {
@@ -1725,6 +1765,7 @@ async function runAgent(opts) {
1725
1765
  workspace: opts.workspace,
1726
1766
  allow: opts.promptContext.allow,
1727
1767
  toolSpec: tool?.spec,
1768
+ truncated,
1728
1769
  // Only spend a fixer-LLM call once the model has already repeated a
1729
1770
  // failing call โ€” the first failure gets deterministic help for free.
1730
1771
  escalate: sig === lastFailSig ? makeLLMEscalator(agent.provider) : void 0
@@ -1735,6 +1776,12 @@ async function runAgent(opts) {
1735
1776
  ${guidance}`;
1736
1777
  events?.onCorrection?.(call, guidance);
1737
1778
  }
1779
+ } else if (result.ok && truncated && autoCorrect) {
1780
+ const guidance = truncationGuidance(call.name);
1781
+ resultText = `${result.output}
1782
+
1783
+ ${guidance}`;
1784
+ events?.onCorrection?.(call, guidance);
1738
1785
  }
1739
1786
  messages.push(driver.toolResultMessage(call, resultText));
1740
1787
  if (result.ok) {
@@ -2908,6 +2955,221 @@ async function resolveOpenRouterKey() {
2908
2955
  }
2909
2956
  }
2910
2957
 
2958
+ // src/cli/commands/prd.ts
2959
+ import { writeFile as writeFile4, readFile as readFile5 } from "fs/promises";
2960
+ import { execFile } from "child_process";
2961
+ import { promisify as promisify2 } from "util";
2962
+ import pc10 from "picocolors";
2963
+
2964
+ // src/core/agent/prd.ts
2965
+ var SYSTEM = [
2966
+ "You are a product analyst. You turn a GitHub issue into a concise, structured PRD",
2967
+ "(Product Requirements Document) in Markdown.",
2968
+ "Rules:",
2969
+ "- Use ONLY information present in the issue and its comments. Do NOT invent scope,",
2970
+ " numbers, deadlines, or stakeholders. If something is unknown, write 'A definir'.",
2971
+ "- Be objective and short; prefer bullet points over prose.",
2972
+ "- Write in the same language as the issue (Portuguese if the issue is in Portuguese)."
2973
+ ].join("\n");
2974
+ function buildPrdPrompt(issue) {
2975
+ const comments = (issue.comments ?? []).map((c, i) => `Coment\xE1rio ${i + 1}${c.author ? ` (@${c.author})` : ""}:
2976
+ ${c.body}`).join("\n\n");
2977
+ return [
2978
+ `Issue${issue.number ? ` #${issue.number}` : ""}: ${issue.title}`,
2979
+ "",
2980
+ "Corpo:",
2981
+ issue.body || "(vazio)",
2982
+ comments ? `
2983
+ ${comments}` : "",
2984
+ "",
2985
+ "Gere um PRD com EXATAMENTE estas se\xE7\xF5es (H2 com ##):",
2986
+ "## Contexto / Problema",
2987
+ "## Objetivo",
2988
+ "## Escopo (in / out)",
2989
+ "## Requisitos funcionais",
2990
+ "## Crit\xE9rios de aceite",
2991
+ "## Riscos e alternativas"
2992
+ ].join("\n");
2993
+ }
2994
+ async function generatePrd(issue, provider) {
2995
+ const res = await provider.chat({
2996
+ messages: [
2997
+ { role: "system", content: SYSTEM },
2998
+ { role: "user", content: buildPrdPrompt(issue) }
2999
+ ],
3000
+ params: { maxTokens: 2e3, temperature: 0.2 }
3001
+ });
3002
+ const text2 = res.content.trim();
3003
+ if (!text2) throw new Error("The model returned an empty PRD.");
3004
+ return text2;
3005
+ }
3006
+
3007
+ // src/core/agent/free-provider.ts
3008
+ function resolveFreeProvider(model) {
3009
+ if (!process.env.OPENROUTER_API_KEY) {
3010
+ throw new Error(
3011
+ "OPENROUTER_API_KEY is not set. Export it (or add it as a repo secret in CI) before running this command."
3012
+ );
3013
+ }
3014
+ const config = {
3015
+ name: "openrouter-free",
3016
+ provider: "openrouter",
3017
+ model,
3018
+ apiKey: "${OPENROUTER_API_KEY}",
3019
+ toolMode: "native"
3020
+ };
3021
+ return createProvider(config);
3022
+ }
3023
+ var DEFAULT_PRD_MODEL = process.env.POLYPUS_PRD_MODEL ?? "openai/gpt-oss-120b:free";
3024
+ var DEFAULT_REVIEW_MODEL = process.env.POLYPUS_REVIEW_MODEL ?? "openai/gpt-oss-120b:free";
3025
+ async function withRetry(fn, opts = {}) {
3026
+ const attempts = opts.attempts ?? 4;
3027
+ const baseMs = opts.baseMs ?? 2e3;
3028
+ let lastErr;
3029
+ for (let i = 0; i < attempts; i++) {
3030
+ try {
3031
+ return await fn();
3032
+ } catch (err) {
3033
+ lastErr = err;
3034
+ const msg = err?.message ?? "";
3035
+ const transient = /\b429\b|\b5\d\d\b|rate|timeout|ETIMEDOUT|ECONNRESET|EAI_AGAIN/i.test(msg);
3036
+ if (!transient || i === attempts - 1) throw err;
3037
+ await new Promise((r) => setTimeout(r, baseMs * 2 ** i));
3038
+ }
3039
+ }
3040
+ throw lastErr;
3041
+ }
3042
+
3043
+ // src/cli/commands/cli-io.ts
3044
+ function numericRef(ref) {
3045
+ const num = ref.replace(/^#/, "");
3046
+ if (!/^\d+$/.test(num)) throw new Error(t("cli.invalidRef", { ref }));
3047
+ return num;
3048
+ }
3049
+ async function readStdin() {
3050
+ if (process.stdin.isTTY) throw new Error(t("cli.stdinTty"));
3051
+ const chunks = [];
3052
+ for await (const chunk of process.stdin) chunks.push(chunk);
3053
+ return Buffer.concat(chunks).toString("utf8");
3054
+ }
3055
+ function stripBom(s) {
3056
+ return s.charCodeAt(0) === 65279 ? s.slice(1) : s;
3057
+ }
3058
+
3059
+ // src/cli/commands/prd.ts
3060
+ var exec2 = promisify2(execFile);
3061
+ async function prd(issueRef, opts) {
3062
+ const issue = await loadIssue(issueRef, opts.input);
3063
+ const { provider } = resolveFreeProvider(opts.model ?? DEFAULT_PRD_MODEL);
3064
+ const markdown = await withRetry(() => generatePrd(issue, provider));
3065
+ if (opts.out) {
3066
+ await writeFile4(opts.out, markdown + "\n", "utf8");
3067
+ console.error(pc10.green(t("prd.wrote", { path: opts.out })));
3068
+ } else {
3069
+ process.stdout.write(markdown + "\n");
3070
+ }
3071
+ }
3072
+ async function loadIssue(issueRef, input) {
3073
+ if (input) {
3074
+ const raw = input === "-" ? await readStdin() : await readFile5(input, "utf8");
3075
+ return normalize2(JSON.parse(stripBom(raw)));
3076
+ }
3077
+ const num = numericRef(issueRef);
3078
+ const { stdout: stdout2 } = await exec2("gh", ["issue", "view", num, "--json", "number,title,body,comments"]);
3079
+ const data = normalize2(JSON.parse(stdout2));
3080
+ data.number ??= Number(num);
3081
+ return data;
3082
+ }
3083
+ function normalize2(raw) {
3084
+ return {
3085
+ number: raw.number,
3086
+ title: raw.title ?? "",
3087
+ body: raw.body ?? "",
3088
+ comments: (raw.comments ?? []).map((c) => ({ author: c.author?.login, body: c.body ?? "" }))
3089
+ };
3090
+ }
3091
+
3092
+ // src/cli/commands/review.ts
3093
+ import { writeFile as writeFile5, readFile as readFile6 } from "fs/promises";
3094
+ import { execFile as execFile2 } from "child_process";
3095
+ import { promisify as promisify3 } from "util";
3096
+ import pc11 from "picocolors";
3097
+
3098
+ // src/core/agent/review.ts
3099
+ var MAX_DIFF_CHARS = Number(process.env.POLYPUS_MAX_DIFF_CHARS) || 6e4;
3100
+ var SYSTEM2 = [
3101
+ "You are a senior code reviewer. Review the pull request diff below and report",
3102
+ "concrete findings in Markdown.",
3103
+ "Rules:",
3104
+ "- Focus on correctness bugs, security risks, and clear, actionable improvements.",
3105
+ "- Reference file paths (and line hints when visible) so findings are easy to locate.",
3106
+ "- Be concise. Skip style nitpicks unless they hide a real problem.",
3107
+ "- If there are no relevant problems, say so briefly instead of inventing issues.",
3108
+ "- Write in the same language as the PR description (Portuguese if it is in Portuguese)."
3109
+ ].join("\n");
3110
+ function clampDiff(diff, max = MAX_DIFF_CHARS) {
3111
+ if (diff.length <= max) return diff;
3112
+ const dropped = diff.length - max;
3113
+ return `${diff.slice(0, max)}
3114
+
3115
+ \u2026 [diff truncado: ${dropped} caracteres omitidos para caber no contexto do modelo]`;
3116
+ }
3117
+ function buildReviewPrompt(diff, meta) {
3118
+ return [
3119
+ `PR${meta.number ? ` #${meta.number}` : ""}: ${meta.title}`,
3120
+ "",
3121
+ "Descri\xE7\xE3o:",
3122
+ meta.body || "(vazia)",
3123
+ "",
3124
+ "Diff:",
3125
+ "```diff",
3126
+ clampDiff(diff),
3127
+ "```",
3128
+ "",
3129
+ "Liste os achados agrupados por severidade (\u{1F534} bug, \u{1F7E1} aten\xE7\xE3o, \u{1F7E2} sugest\xE3o)."
3130
+ ].join("\n");
3131
+ }
3132
+ async function reviewDiff(diff, meta, provider) {
3133
+ if (!diff.trim()) return "_Sem altera\xE7\xF5es no diff para revisar._";
3134
+ const res = await provider.chat({
3135
+ messages: [
3136
+ { role: "system", content: SYSTEM2 },
3137
+ { role: "user", content: buildReviewPrompt(diff, meta) }
3138
+ ],
3139
+ params: { maxTokens: 1500, temperature: 0.2 }
3140
+ });
3141
+ const text2 = res.content.trim();
3142
+ if (!text2) throw new Error("The model returned an empty review.");
3143
+ return text2;
3144
+ }
3145
+
3146
+ // src/cli/commands/review.ts
3147
+ var exec3 = promisify3(execFile2);
3148
+ async function review(prRef, opts) {
3149
+ const num = opts.input ? prRef.replace(/^#/, "") : numericRef(prRef);
3150
+ const diff = await loadDiff(num, opts.input);
3151
+ const meta = await loadMeta(num, opts.input);
3152
+ const { provider } = resolveFreeProvider(opts.model ?? DEFAULT_REVIEW_MODEL);
3153
+ const markdown = await withRetry(() => reviewDiff(diff, meta, provider));
3154
+ if (opts.out) {
3155
+ await writeFile5(opts.out, markdown + "\n", "utf8");
3156
+ console.error(pc11.green(t("review.wrote", { path: opts.out })));
3157
+ } else {
3158
+ process.stdout.write(markdown + "\n");
3159
+ }
3160
+ }
3161
+ async function loadDiff(num, input) {
3162
+ if (input) return input === "-" ? readStdin() : readFile6(input, "utf8");
3163
+ const { stdout: stdout2 } = await exec3("gh", ["pr", "diff", num]);
3164
+ return stdout2;
3165
+ }
3166
+ async function loadMeta(num, input) {
3167
+ if (input) return { number: Number(num) || void 0, title: `PR ${num}`, body: "" };
3168
+ const { stdout: stdout2 } = await exec3("gh", ["pr", "view", num, "--json", "number,title,body"]);
3169
+ const raw = JSON.parse(stdout2);
3170
+ return { number: raw.number, title: raw.title ?? "", body: raw.body ?? "" };
3171
+ }
3172
+
2911
3173
  // src/cli/index.ts
2912
3174
  import { join as join3 } from "path";
2913
3175
 
@@ -2943,7 +3205,7 @@ async function launchInteractive() {
2943
3205
  const config = await loadConfig();
2944
3206
  if (config.agents.length === 0) {
2945
3207
  console.log(banner());
2946
- console.log(" " + pc10.yellow(t("welcome.firstRun")) + "\n");
3208
+ console.log(" " + pc12.yellow(t("welcome.firstRun")) + "\n");
2947
3209
  await setup();
2948
3210
  }
2949
3211
  await run(void 0, {});
@@ -2972,6 +3234,8 @@ function buildProgram() {
2972
3234
  program.command("run").argument("[task]", t("cli.arg.runTask")).option("--agent <name>", t("cli.opt.agent")).option("--mode <mode>", t("cli.opt.mode")).option("--max-steps <n>", t("cli.opt.maxSteps")).description(t("cli.cmd.run")).action((task, opts) => run(task, opts));
2973
3235
  program.command("swarm").argument("<task>", t("cli.arg.swarmTask")).option("--agents <names>", t("cli.opt.agents")).option("--max-subtasks <n>", t("cli.opt.maxSubtasks")).description(t("cli.cmd.swarm")).action((task, opts) => swarm(task, opts));
2974
3236
  program.command("models").option("--search <text>", t("cli.opt.search")).option("--tools", t("cli.opt.toolsOnly")).option("--free", t("cli.opt.free")).option("--max-price <usd>", t("cli.opt.maxPrice")).option("--sort <order>", t("cli.opt.sort")).option("--limit <n>", t("cli.opt.limit")).description(t("cli.cmd.models")).action((opts) => models(opts));
3237
+ program.command("prd").argument("<issue>", t("cli.arg.prdIssue")).option("--out <file>", t("cli.opt.out")).option("--model <model>", t("cli.opt.model")).option("--input <file>", t("cli.opt.input")).description(t("cli.cmd.prd")).action((issue, opts) => prd(issue, opts));
3238
+ program.command("review").argument("<pr>", t("cli.arg.reviewPr")).option("--out <file>", t("cli.opt.out")).option("--model <model>", t("cli.opt.model")).option("--input <file>", t("cli.opt.input")).description(t("cli.cmd.review")).action((pr, opts) => review(pr, opts));
2975
3239
  return program;
2976
3240
  }
2977
3241
  async function main() {
@@ -2980,7 +3244,7 @@ async function main() {
2980
3244
  await resolveLocale();
2981
3245
  await buildProgram().parseAsync(process.argv);
2982
3246
  } catch (err) {
2983
- console.error(pc10.red(`\u2717 ${err.message}`));
3247
+ console.error(pc12.red(`\u2717 ${err.message}`));
2984
3248
  process.exitCode = 1;
2985
3249
  }
2986
3250
  }