@juanbenjumea/pi-agy 0.7.6 → 0.7.8

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.
@@ -1,62 +1,28 @@
1
1
  import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
2
2
 
3
3
  import {
4
- checkAgyUsage,
5
- formatAgyUsage,
6
- inspectAgyAgents,
4
+ inspectAgyModels,
7
5
  isAgyModel,
8
6
  normalizeAgyAgentName,
9
7
  resolveAgyModelId,
10
- type AgyModel,
11
8
  } from "./lib/cli.js";
12
- import { buildAgyContextFromEntries, type AgyContextMode } from "./lib/context.js";
13
- import { runAgyDoctor } from "./lib/doctor.js";
14
- import { executeAgyTask } from "./lib/execute.js";
15
- import { describeWhen, truncate } from "./lib/output.js";
16
- import { describePreRunDirt } from "./lib/postflight.js";
17
- import {
18
- AGY_RUN_ENTRY_TYPE,
19
- renderAgyRunReceipt,
20
- type AgyRunReceipt,
21
- } from "./lib/render.js";
22
- import { getHistory, getSession } from "./lib/sessions.js";
9
+ import { truncate } from "./lib/output.js";
10
+ import { AGY_RUN_ENTRY_TYPE, renderAgyRunReceipt } from "./lib/render.js";
11
+ import { getSession } from "./lib/sessions.js";
23
12
 
24
- import { DEFAULT_TIMEOUT_MS, MODEL_ALIASES, MODEL_KEYS, MODE_KEYS, parseAgyCommandArgs } from "./lib/command-args.js";
13
+ import { MODEL_ALIASES, MODEL_KEYS, MODE_KEYS, parseAgyCommandArgs } from "./lib/command-args.js";
14
+ import { executeConfirmedRun, MODE_OPTIONS, optionKey, runSessionsPicker, type InteractiveRun } from "./command-run.js";
15
+ import { showAgents, showBridge, showDoctor, showUsage } from "./command-info.js";
25
16
  import { buildAcceptEditsConfirm } from "./lib/confirm.js";
26
- import { loadAgyConfig, saveAgyConfig } from "./lib/config.js";
27
- import { describeProviderBridges, resetProviderBridges } from "./lib/provider.js";
28
-
29
- const MODEL_OPTIONS = [
30
- "flash — fast, cheap (Gemini Flash medium)",
31
- "flash-low — trivial / high-volume",
32
- "flash-high — harder agentic work",
33
- "pro — deep reasoning (Gemini Pro high)",
34
- "pro-low — pro, lighter reasoning",
35
- "pro-high — max Gemini reasoning",
36
- "sonnet — Claude default (review/code)",
37
- "opus — Claude hard problems",
38
- "gpt-oss — open-model alternative",
39
- ];
40
17
 
41
- const MODE_OPTIONS = [
42
- "accept-edits — writes files (default)",
43
- "plan — exploration, no edits",
44
- "sandbox — isolated preview",
45
- ];
18
+ function modelOptions(): string[] {
19
+ return Object.keys(MODEL_ALIASES).map((alias) =>
20
+ `${alias} — ${resolveAgyModelId(MODEL_ALIASES[alias])}`,
21
+ );
22
+ }
46
23
 
47
24
  export { parseAgyCommandArgs, type AgyCommandArgs } from "./lib/command-args.js";
48
25
 
49
- interface InteractiveRun {
50
- mode: "plan" | "accept-edits" | "sandbox";
51
- model: AgyModel;
52
- agent?: string;
53
- prompt: string;
54
- conversation_id?: string;
55
- continue?: boolean;
56
- timeout_ms?: number;
57
- context?: AgyContextMode;
58
- }
59
-
60
26
  /**
61
27
  * Register the human-callable `/agy` command. Missing parts are filled with
62
28
  * dialogs, write-capable runs are confirmed, and agy executes directly — the
@@ -200,7 +166,14 @@ export function registerAgyCommand(pi: ExtensionAPI): void {
200
166
  }
201
167
  }
202
168
  if (!model) {
203
- const pick = await ctx.ui.select("agy model", MODEL_OPTIONS);
169
+ // Refresh once when opening the picker so labels show the concrete IDs
170
+ // exposed by this account, while retaining the stable alias interface.
171
+ try {
172
+ await inspectAgyModels(ctx.cwd, ctx.signal);
173
+ } catch {
174
+ // Keep the picker usable with the built-in fallback aliases.
175
+ }
176
+ const pick = await ctx.ui.select("agy model", modelOptions());
204
177
  if (!pick) {
205
178
  ctx.ui.notify("agy: cancelled", "info");
206
179
  return;
@@ -230,273 +203,3 @@ export function registerAgyCommand(pi: ExtensionAPI): void {
230
203
  },
231
204
  });
232
205
  }
233
-
234
- /** `/agy doctor` — diagnose the CLI and local integration without inference. */
235
- async function showDoctor(ctx: ExtensionCommandContext): Promise<void> {
236
- try {
237
- await ctx.waitForIdle();
238
- ctx.ui.setStatus("agy", "agy: running diagnostics…");
239
- const report = await runAgyDoctor(ctx.cwd, ctx.signal);
240
- const type = report.status === "error" ? "error" : report.status === "warn" ? "warning" : "info";
241
- ctx.ui.notify(report.text, type);
242
- } catch (error) {
243
- if (ctx.signal?.aborted) {
244
- ctx.ui.notify("agy: doctor cancelled", "info");
245
- } else {
246
- const message = error instanceof Error ? error.message : String(error);
247
- ctx.ui.notify(`agy doctor failed: ${message}`, "error");
248
- }
249
- } finally {
250
- ctx.ui.setStatus("agy", undefined);
251
- }
252
- }
253
-
254
- /** `/agy agents` — list configured custom agents without inference. */
255
- async function showAgents(ctx: ExtensionCommandContext): Promise<void> {
256
- try {
257
- await ctx.waitForIdle();
258
- ctx.ui.setStatus("agy", "agy: discovering custom agents…");
259
- const agents = await inspectAgyAgents(ctx.cwd, ctx.signal);
260
- ctx.ui.notify(
261
- agents.length ? `configured agy agents:\n${agents.map((agent) => `- ${agent}`).join("\n")}` : "No custom agy agents are configured.",
262
- "info",
263
- );
264
- } catch (error) {
265
- if (ctx.signal?.aborted) ctx.ui.notify("agy: agent discovery cancelled", "info");
266
- else {
267
- const message = error instanceof Error ? error.message : String(error);
268
- ctx.ui.notify(`agy agent discovery failed: ${message}`, "error");
269
- }
270
- } finally {
271
- ctx.ui.setStatus("agy", undefined);
272
- }
273
- }
274
-
275
- /** `/agy usage` — show model quotas without starting an agent turn. */
276
- async function showUsage(
277
- ctx: ExtensionCommandContext,
278
- model?: AgyModel,
279
- ): Promise<void> {
280
- try {
281
- await ctx.waitForIdle();
282
- const snapshot = await checkAgyUsage(ctx.cwd, ctx.signal);
283
- const report = formatAgyUsage(snapshot, model ? resolveAgyModelId(model) : undefined);
284
- ctx.ui.notify(report ?? "agy quota information is unavailable in this CLI version", report ? "info" : "warning");
285
- } catch (error) {
286
- const message = error instanceof Error ? error.message : String(error);
287
- ctx.ui.notify(`agy usage check failed: ${message}`, "error");
288
- }
289
- }
290
-
291
- /** `/agy bridge [on|off|status]` — inspect or toggle the provider MCP bridge. */
292
- async function showBridge(ctx: ExtensionCommandContext, arg?: string): Promise<void> {
293
- const sub = (arg ?? "status").toLowerCase();
294
- if (sub !== "on" && sub !== "off" && sub !== "status") {
295
- ctx.ui.notify("agy: bridge takes on, off, or no argument (status)", "error");
296
- return;
297
- }
298
- try {
299
- await ctx.waitForIdle();
300
- if (sub === "on" || sub === "off") {
301
- ctx.ui.setStatus("agy", `agy: turning the provider bridge ${sub}…`);
302
- await saveAgyConfig({ providerBridge: sub === "on" });
303
- // Drop memoized servers so the next provider turn re-resolves;
304
- // drivers recycle on the bridge-dir change by themselves.
305
- await resetProviderBridges();
306
- ctx.ui.notify(
307
- sub === "on"
308
- ? "Provider bridge on: agy turns get Pi tools via localhost MCP (takes effect on the next turn)."
309
- : "Provider bridge off: agy turns run plain (takes effect on the next turn).",
310
- "info",
311
- );
312
- return;
313
- }
314
- const config = await loadAgyConfig().catch(() => ({ providerBridge: true as const }));
315
- const enabled = config.providerBridge !== false;
316
- const bridges = describeProviderBridges();
317
- const running = bridges.filter((bridge) => bridge.running);
318
- const lines = [
319
- `Provider bridge: ${enabled ? "on" : "off"} (agy-config.json${enabled ? "" : " — set providerBridge or run /agy bridge on"})`,
320
- running.length > 0
321
- ? `running servers (${running.length}):\n${running.map((bridge) => `- ${bridge.dir} · ${bridge.toolCount ?? 0} tools`).join("\n")}`
322
- : "no bridge servers running (one starts on the next provider turn)",
323
- ];
324
- ctx.ui.notify(lines.join("\n"), "info");
325
- } catch (error) {
326
- if (ctx.signal?.aborted) ctx.ui.notify("agy: bridge check cancelled", "info");
327
- else {
328
- const message = error instanceof Error ? error.message : String(error);
329
- ctx.ui.notify(`agy bridge failed: ${message}`, "error");
330
- }
331
- } finally {
332
- ctx.ui.setStatus("agy", undefined);
333
- }
334
- }
335
-
336
- /** `/agy sessions` — pick a recorded conversation and resume it with a follow-up. */
337
- async function runSessionsPicker(
338
- pi: ExtensionAPI,
339
- ctx: ExtensionCommandContext,
340
- ): Promise<void> {
341
- const history = await getHistory(ctx.cwd);
342
- if (history.length === 0) {
343
- ctx.ui.notify("agy: no recorded conversations for this directory", "info");
344
- return;
345
- }
346
-
347
- const options = history.map((entry, index) => {
348
- const id = entry.conversation_id.slice(0, 8);
349
- const model = entry.model ?? "unknown model";
350
- const agent = entry.agent ? ` · agent ${entry.agent}` : "";
351
- const summary = entry.summary ? `${entry.summary.slice(0, 50)} · ` : "";
352
- return `${index + 1}. ${summary}${model}${agent} · ${describeWhen(entry.updated_at)} · ${id}…`;
353
- });
354
- const pick = await ctx.ui.select("resume agy conversation", options);
355
- if (!pick) {
356
- ctx.ui.notify("agy: cancelled", "info");
357
- return;
358
- }
359
- const entry = history[options.indexOf(pick)];
360
- if (!entry) {
361
- ctx.ui.notify("agy: cancelled", "info");
362
- return;
363
- }
364
-
365
- const modePick = await ctx.ui.select("agy mode", MODE_OPTIONS);
366
- if (!modePick) {
367
- ctx.ui.notify("agy: cancelled", "info");
368
- return;
369
- }
370
- const mode = optionKey(modePick) as InteractiveRun["mode"];
371
-
372
- const text = await ctx.ui.editor("agy follow-up task", "");
373
- if (!text?.trim()) {
374
- ctx.ui.notify("agy: cancelled (empty task)", "info");
375
- return;
376
- }
377
-
378
- await executeConfirmedRun(pi, ctx, {
379
- mode,
380
- model: isAgyModel(entry.model) ? entry.model : "flash-medium",
381
- agent: entry.agent,
382
- prompt: text.trim(),
383
- conversation_id: entry.conversation_id,
384
- });
385
- }
386
-
387
- async function executeConfirmedRun(
388
- pi: ExtensionAPI,
389
- ctx: ExtensionCommandContext,
390
- run: InteractiveRun,
391
- ): Promise<void> {
392
- const cwd = ctx.cwd;
393
- await ctx.waitForIdle();
394
- const contextMode = run.context ?? "none";
395
- const contextText =
396
- contextMode === "none"
397
- ? undefined
398
- : buildAgyContextFromEntries(ctx.sessionManager.buildContextEntries(), contextMode);
399
-
400
- if (run.mode === "accept-edits") {
401
- const dirt = await describePreRunDirt(cwd, ctx.signal).catch(() => null);
402
- const { title, body } = buildAcceptEditsConfirm({
403
- modelLabel: `${run.model} (${resolveAgyModelId(run.model)})`,
404
- agent: run.agent,
405
- cwd,
406
- prompt: run.prompt,
407
- contextMode,
408
- contextText,
409
- dirt,
410
- });
411
- const ok = await ctx.ui.confirm(title, body);
412
- if (!ok) {
413
- ctx.ui.notify("agy: cancelled", "info");
414
- return;
415
- }
416
- }
417
-
418
- // Execute directly so the confirmed parameters cannot be changed by
419
- // a second LLM turn and the command works even when tools are limited.
420
- const status = createStatusThrottler(ctx);
421
- try {
422
- status(`starting (${run.model} · ${run.mode}${run.agent ? ` · agent ${run.agent}` : ""})`);
423
- const result = await executeAgyTask(
424
- {
425
- prompt: run.prompt,
426
- model: run.model,
427
- agent: run.agent,
428
- mode: run.mode,
429
- dir: cwd,
430
- timeout_ms: run.timeout_ms ?? DEFAULT_TIMEOUT_MS,
431
- conversation_id: run.conversation_id,
432
- continue: run.continue,
433
- new_session: run.conversation_id || run.continue ? false : true,
434
- stream: true,
435
- context: contextMode,
436
- context_text: contextText,
437
- },
438
- ctx.signal,
439
- (progress) => status(progress),
440
- );
441
- status(undefined);
442
- const text = truncate(result.text || "(empty response)");
443
- if (typeof pi.appendEntry === "function") {
444
- pi.appendEntry(AGY_RUN_ENTRY_TYPE, {
445
- task: truncate(run.prompt, 500),
446
- text,
447
- details: result.details,
448
- completed_at: new Date().toISOString(),
449
- } satisfies AgyRunReceipt);
450
- } else {
451
- // Compatibility fallback for stripped-down hosts and test harnesses.
452
- ctx.ui.notify(truncate(text, 4000), "info");
453
- }
454
- } catch (error) {
455
- status(undefined);
456
- if (ctx.signal?.aborted) {
457
- ctx.ui.notify("agy: cancelled", "info");
458
- } else {
459
- const message = error instanceof Error ? error.message : String(error);
460
- ctx.ui.notify(`agy failed: ${message}`, "error");
461
- }
462
- }
463
- }
464
-
465
- /** Coalesce status-bar updates so per-token deltas do not churn the TUI. */
466
- function createStatusThrottler(ctx: ExtensionCommandContext, intervalMs = 300) {
467
- let lastEmit = 0;
468
- let timer: ReturnType<typeof setTimeout> | undefined;
469
- let pending: string | undefined;
470
-
471
- const emit = (text: string) => {
472
- lastEmit = Date.now();
473
- ctx.ui.setStatus("agy", text.slice(0, 200));
474
- };
475
-
476
- return (text: string | undefined): void => {
477
- if (text === undefined) {
478
- if (timer !== undefined) clearTimeout(timer);
479
- timer = undefined;
480
- pending = undefined;
481
- ctx.ui.setStatus("agy", undefined);
482
- return;
483
- }
484
- const now = Date.now();
485
- if (now - lastEmit >= intervalMs) {
486
- emit(text);
487
- return;
488
- }
489
- pending = text;
490
- if (timer === undefined) {
491
- timer = setTimeout(() => {
492
- timer = undefined;
493
- if (pending !== undefined) emit(pending);
494
- pending = undefined;
495
- }, intervalMs - (now - lastEmit));
496
- }
497
- };
498
- }
499
-
500
- function optionKey(option: string): string {
501
- return option.split(" — ")[0];
502
- }