@juanbenjumea/pi-agy 0.5.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,186 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import { Type } from "@sinclair/typebox";
3
+ import * as path from "node:path";
4
+
5
+ import { resolveAgyModelAlias, resolveAgyModelId, type AgyModel } from "./lib/cli.js";
6
+ import { describePreRunDirt } from "./lib/postflight.js";
7
+ import { registerAgyCommand } from "./commands.js";
8
+ import {
9
+ executeAgyTask,
10
+ validateAgyExecutionOptions,
11
+ type AgyMode,
12
+ } from "./lib/execute.js";
13
+ import { truncate } from "./lib/output.js";
14
+
15
+ const DEFAULT_TIMEOUT_MS = 300_000;
16
+ const MAX_TIMEOUT_MS = 600_000;
17
+
18
+ export { truncate } from "./lib/output.js";
19
+
20
+ export function resolveAgyMode(mode?: AgyMode): AgyMode {
21
+ return mode ?? "accept-edits";
22
+ }
23
+
24
+ export default function piAgyExtension(pi: ExtensionAPI) {
25
+ registerAgyCommand(pi);
26
+
27
+ pi.registerTool({
28
+ name: "agy_execute",
29
+ label: "Antigravity CLI",
30
+ description:
31
+ "Run a task through the Antigravity CLI (agy) for bulk implementation, scaffolding, or test generation.",
32
+ promptSnippet: "Run a task through the Antigravity CLI (agy)",
33
+ promptGuidelines: [
34
+ "Use agy_execute with accept-edits for scoped implementation; use plan for exploration and review.",
35
+ "Plan or research with one family only when needed; implement with flash-medium or sonnet according to which quota group should carry the work.",
36
+ "Use flash-medium by default for bulk coding, exploration, tests, and repetitive work.",
37
+ "Use flash-low for trivial few-step or high-volume work, and flash-high for difficult agentic work.",
38
+ "Use pro-low or pro-high only when advanced reasoning needs escalation within the Gemini quota group.",
39
+ "Use sonnet for normal coding or review in the Claude quota group; reserve opus for the hardest architecture, root-cause, or adversarial review.",
40
+ "Use gpt-oss when an open-model alternative is specifically desired.",
41
+ "For consequential work, use one family to produce and the opposite family to cross-review in mode=plan; do not spend both quota groups on trivial tasks.",
42
+ "Reuse conversation_id or continue=true for multi-step plan→implement→review handoffs.",
43
+ "Batch related work, prefer digest output for non-write calls, and avoid parallel agy_execute calls within one shared-quota group or directory.",
44
+ "Always review the git diff and run just ci (or the project gate) after agy_execute with mode=accept-edits.",
45
+ "Never use agy for irreversible production changes.",
46
+ "Set an appropriate timeout_ms for large tasks (default 5m).",
47
+ ],
48
+ parameters: Type.Object({
49
+ prompt: Type.String({
50
+ description: "The task instruction for agy.",
51
+ minLength: 1,
52
+ }),
53
+ model: Type.Optional(
54
+ Type.Union(
55
+ [
56
+ Type.Literal("flash-low"),
57
+ Type.Literal("flash-medium"),
58
+ Type.Literal("flash-high"),
59
+ Type.Literal("pro-low"),
60
+ Type.Literal("pro-high"),
61
+ Type.Literal("sonnet"),
62
+ Type.Literal("opus"),
63
+ Type.Literal("gpt-oss"),
64
+ ],
65
+ { description: "Model alias. Defaults to 'flash-medium'.", default: "flash-medium" },
66
+ ),
67
+ ),
68
+ effort: Type.Optional(
69
+ Type.Union([Type.Literal("low"), Type.Literal("medium"), Type.Literal("high")], {
70
+ description:
71
+ "Reasoning effort passed to agy (--effort). Optional; mostly useful for sonnet/opus/gpt-oss since Gemini aliases encode effort in the model id.",
72
+ }),
73
+ ),
74
+ tier: Type.Optional(
75
+ Type.Union(
76
+ [Type.Literal("flash"), Type.Literal("flash-lo"), Type.Literal("pro")],
77
+ { description: "Legacy Gemini tier. Ignored when model is set." },
78
+ ),
79
+ ),
80
+ mode: Type.Optional(
81
+ Type.Union(
82
+ [Type.Literal("accept-edits"), Type.Literal("plan"), Type.Literal("sandbox")],
83
+ { description: "'accept-edits' (default), 'plan', or 'sandbox'.", default: "accept-edits" },
84
+ ),
85
+ ),
86
+ dir: Type.Optional(
87
+ Type.String({
88
+ description: "Working directory. Defaults to current project root.",
89
+ }),
90
+ ),
91
+ digest: Type.Optional(
92
+ Type.Boolean({
93
+ description:
94
+ "Request compact digests instead of full output. Defaults on for plan/sandbox and off for accept-edits.",
95
+ }),
96
+ ),
97
+ timeout_ms: Type.Optional(
98
+ Type.Number({
99
+ description: "Timeout in milliseconds (default 300000 = 5m, max 600000).",
100
+ minimum: 1000,
101
+ maximum: MAX_TIMEOUT_MS,
102
+ }),
103
+ ),
104
+ conversation_id: Type.Optional(
105
+ Type.String({
106
+ description: "Resume a previous agy conversation by ID.",
107
+ }),
108
+ ),
109
+ continue: Type.Optional(
110
+ Type.Boolean({
111
+ description: "Continue the most recent agy conversation for this workspace.",
112
+ }),
113
+ ),
114
+ new_session: Type.Optional(
115
+ Type.Boolean({
116
+ description: "Force a fresh agy conversation (default when no conversation_id/continue).",
117
+ }),
118
+ ),
119
+ stream: Type.Optional(
120
+ Type.Boolean({
121
+ description: "Stream agy progress via stream-json (default true).",
122
+ default: true,
123
+ }),
124
+ ),
125
+ }),
126
+
127
+ async execute(_toolCallId, params, signal, onUpdate, ctx) {
128
+ const cwd = params.dir ? path.resolve(ctx.cwd, params.dir) : ctx.cwd;
129
+ const abortSignal = signal ?? new AbortController().signal;
130
+ const timeoutMs = Math.min(params.timeout_ms ?? DEFAULT_TIMEOUT_MS, MAX_TIMEOUT_MS);
131
+ const mode = resolveAgyMode(params.mode);
132
+ const model = params.model as AgyModel | undefined;
133
+ const executionOptions = {
134
+ prompt: params.prompt,
135
+ model,
136
+ tier: params.tier,
137
+ effort: params.effort,
138
+ mode,
139
+ dir: cwd,
140
+ digest: params.digest,
141
+ timeout_ms: timeoutMs,
142
+ conversation_id: params.conversation_id,
143
+ continue: params.continue,
144
+ new_session: params.new_session,
145
+ stream: params.stream ?? true,
146
+ };
147
+ validateAgyExecutionOptions(executionOptions);
148
+
149
+ if (mode === "accept-edits") {
150
+ if (!ctx.hasUI) {
151
+ throw new Error("accept-edits requires interactive confirmation");
152
+ }
153
+ const requestedModel = resolveAgyModelAlias(model, params.tier);
154
+ const modelLabel = requestedModel
155
+ ? `${requestedModel} (${resolveAgyModelId(model, params.tier)})`
156
+ : "configured default (fallback flash-medium)";
157
+ const dirt = await describePreRunDirt(cwd, abortSignal).catch(() => null);
158
+ const excerpt =
159
+ params.prompt.slice(0, 200) + (params.prompt.length > 200 ? "…" : "");
160
+ const dirtLine = dirt ? `\nworkspace: ${dirt}` : "";
161
+ const warning =
162
+ dirt && dirt !== "clean"
163
+ ? `\n\nWarning: uncommitted changes already exist — the result summary only attributes newly-dirty files to agy.`
164
+ : "";
165
+ const approved = await ctx.ui.confirm(
166
+ "Run agy (accept-edits)?",
167
+ `model: ${modelLabel}\ndir: ${cwd}${dirtLine}\n\ntask: ${excerpt}\n\nThis grants agy permission to modify files and run commands.${warning}`,
168
+ );
169
+ if (!approved) throw new Error("agy accept-edits cancelled by user");
170
+ }
171
+
172
+ const result = await executeAgyTask(
173
+ executionOptions,
174
+ abortSignal,
175
+ (progress) => {
176
+ onUpdate?.({ content: [{ type: "text", text: progress }], details: {} });
177
+ },
178
+ );
179
+
180
+ return {
181
+ content: [{ type: "text", text: truncate(result.text || "(empty response)") }],
182
+ details: result.details,
183
+ };
184
+ },
185
+ });
186
+ }