@gtrabanco/pi-nan-provider 1.5.1 → 1.5.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.
@@ -0,0 +1,124 @@
1
+ /**
2
+ * `/nan-debug` command (issue #35) — deterministic, redacted evidence
3
+ * collection following the `/nan-usage` precedent.
4
+ *
5
+ * Flow: parse → collect → write the redacted report locally → notify a preview.
6
+ * The shareable zip is written only with `--emit` (the explicit confirmation
7
+ * the issue asks for). `/v1/usage` is consulted only with `--window`, and it
8
+ * stays permission-gated: the user invoked the command with that flag.
9
+ */
10
+
11
+ import { join } from "node:path";
12
+ import { homedir as osHomedir } from "node:os";
13
+ import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
14
+ import { getAgentDir } from "@earendil-works/pi-coding-agent";
15
+ import { NAN_API_KEY_ENV, resolveStoredNanApiKey, tryResolveNanApiKeyViaRegistry } from "./mcp/api-key.ts";
16
+ import {
17
+ DEBUG_USAGE_LINE,
18
+ NAN_DEBUG_REPORT_DIR,
19
+ NAN_DEBUG_ZIP,
20
+ buildDebugReport,
21
+ fetchDebugUsageWindow,
22
+ parseDebugArgs,
23
+ renderDebugReport,
24
+ writeDebugReport,
25
+ writeDebugZip,
26
+ type FetchUsageWindow,
27
+ } from "./debug-report.ts";
28
+
29
+ export interface NanDebugCommandDeps {
30
+ /** Injectable for tests; defaults to pi's agent dir. */
31
+ agentDir?: string;
32
+ /** Injectable clock for deterministic tests. */
33
+ now?: () => Date;
34
+ /** Injectable `GET /v1/usage` client for tests; defaults to the live call. */
35
+ fetchUsage?: FetchUsageWindow;
36
+ /** Injectable key resolution for tests. */
37
+ resolveApiKey?: (ctx: ExtensionCommandContext) => Promise<string | undefined>;
38
+ /** Injectable home directory for tests; defaults to the OS home. */
39
+ homedir?: string;
40
+ }
41
+
42
+ async function defaultResolveApiKey(ctx: ExtensionCommandContext): Promise<string | undefined> {
43
+ const registryKey = await tryResolveNanApiKeyViaRegistry(ctx.modelRegistry);
44
+ return registryKey ?? resolveStoredNanApiKey() ?? (process.env[NAN_API_KEY_ENV] || undefined);
45
+ }
46
+
47
+ /**
48
+ * Expand a leading `~` (or `~/`) against the home directory so the README
49
+ * example `--out ~/evidence` writes where it says (EV-2026-11-11 A2).
50
+ * Every other path passes through untouched.
51
+ */
52
+ function expandHomePath(value: string, home: string): string {
53
+ if (value === "~") return home;
54
+ if (value.startsWith("~/")) return join(home, value.slice(2));
55
+ return value;
56
+ }
57
+
58
+ export function registerNanDebugCommand(pi: ExtensionAPI, deps: NanDebugCommandDeps = {}): void {
59
+ pi.registerCommand("nan-debug", {
60
+ description:
61
+ "Collect a redacted NaN evidence report locally; --emit writes the shareable zip (/nan-debug help)",
62
+ handler: async (args: string, ctx: ExtensionCommandContext) => {
63
+ if (args.trim().toLowerCase() === "help") {
64
+ ctx.ui.notify(DEBUG_USAGE_LINE, "info");
65
+ return;
66
+ }
67
+
68
+ const parsed = parseDebugArgs(args);
69
+ if (!parsed.ok) {
70
+ ctx.ui.notify(parsed.error, "warning");
71
+ return;
72
+ }
73
+
74
+ const now = deps.now?.() ?? new Date();
75
+ const agentDir = deps.agentDir ?? getAgentDir();
76
+ const apiKey = parsed.args.windowProvided
77
+ ? await (deps.resolveApiKey ?? defaultResolveApiKey)(ctx)
78
+ : undefined;
79
+
80
+ let rendered;
81
+ try {
82
+ const report = await buildDebugReport({
83
+ agentDir,
84
+ now,
85
+ windowDays: parsed.args.windowDays,
86
+ topic: parsed.args.topic,
87
+ apiKey,
88
+ includeUsage: parsed.args.windowProvided,
89
+ fetchUsage: parsed.args.windowProvided ? (deps.fetchUsage ?? fetchDebugUsageWindow) : undefined,
90
+ });
91
+ rendered = renderDebugReport(report);
92
+ } catch (error) {
93
+ ctx.ui.notify(
94
+ `nan-debug failed to collect the report: ${error instanceof Error ? error.message : String(error)}`,
95
+ "warning",
96
+ );
97
+ return;
98
+ }
99
+
100
+ const explicitZipTarget = parsed.args.out ? expandHomePath(parsed.args.out, deps.homedir ?? osHomedir()) : undefined;
101
+ const explicitZip = explicitZipTarget?.endsWith(".zip") ? explicitZipTarget : undefined;
102
+ const outDir = explicitZip
103
+ ? explicitZip.replace(/\.zip$/, "")
104
+ : (explicitZipTarget ?? join(agentDir, NAN_DEBUG_REPORT_DIR));
105
+ const zipPath = explicitZip ?? (explicitZipTarget ? `${explicitZipTarget}.zip` : join(agentDir, NAN_DEBUG_ZIP));
106
+
107
+ try {
108
+ writeDebugReport(rendered.files, outDir);
109
+ if (parsed.args.emit) writeDebugZip(rendered.files, zipPath);
110
+ } catch (error) {
111
+ ctx.ui.notify(
112
+ `nan-debug wrote nothing: ${error instanceof Error ? error.message : String(error)}`,
113
+ "warning",
114
+ );
115
+ return;
116
+ }
117
+
118
+ const message = parsed.args.emit
119
+ ? `${rendered.preview}\n→ zip written to ${zipPath}`
120
+ : `${rendered.preview}\n→ local report: ${outDir}`;
121
+ ctx.ui.notify(message, "info");
122
+ },
123
+ });
124
+ }