agent-detective 1.0.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,193 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ importPluginModuleFromSpecifier,
4
+ validatePluginConfig,
5
+ validatePluginSchema
6
+ } from "./chunk-OIYJYLCB.js";
7
+ import {
8
+ isAgentInstalled,
9
+ loadConfig,
10
+ normalizeAgent
11
+ } from "./chunk-H2IXGHNA.js";
12
+
13
+ // src/cli/doctor.ts
14
+ import { resolve } from "path";
15
+ import { existsSync } from "fs";
16
+ function resolveConfigDirFromInstallRoot(installRoot) {
17
+ if (!installRoot) return void 0;
18
+ if (installRoot.split(/[\\/]/).pop() === "config") return installRoot;
19
+ return resolve(installRoot, "config");
20
+ }
21
+ function parseFlags(argv) {
22
+ const args = argv.slice(2);
23
+ return {
24
+ json: args.includes("--json"),
25
+ verbose: args.includes("--verbose")
26
+ };
27
+ }
28
+ function hasExplicitConfigRootArg(argv) {
29
+ const args = argv.slice(2);
30
+ return args.some((a) => a === "--config-root" || a.startsWith("--config-root="));
31
+ }
32
+ function hasExplicitConfigRootEnv() {
33
+ return typeof process.env.AGENT_DETECTIVE_CONFIG_ROOT === "string" && process.env.AGENT_DETECTIVE_CONFIG_ROOT.length > 0;
34
+ }
35
+ function coercePlugin(mod) {
36
+ const m = mod;
37
+ return (m && typeof m === "object" && "default" in m ? m.default : m) ?? mod;
38
+ }
39
+ function applyPluginDefaults(plugin, options) {
40
+ const merged = { ...options };
41
+ if (plugin.schema?.properties) {
42
+ for (const [key, prop] of Object.entries(plugin.schema.properties)) {
43
+ if (merged[key] === void 0 && prop.default !== void 0) {
44
+ merged[key] = prop.default;
45
+ }
46
+ }
47
+ }
48
+ return merged;
49
+ }
50
+ async function runDoctor({ installRoot, argv }) {
51
+ const { json, verbose } = parseFlags(argv);
52
+ const checks = [];
53
+ const configRoot = resolveConfigDirFromInstallRoot(installRoot);
54
+ const configRootUsed = configRoot ?? resolve(process.cwd(), "config");
55
+ const resolutionRoot = installRoot ?? process.cwd();
56
+ const explicitConfigRoot = hasExplicitConfigRootArg(argv) || hasExplicitConfigRootEnv();
57
+ const configDirExists = existsSync(configRootUsed);
58
+ const defaultJsonExists = existsSync(resolve(configRootUsed, "default.json"));
59
+ const localJsonExists = existsSync(resolve(configRootUsed, "local.json"));
60
+ if (explicitConfigRoot) {
61
+ const ok2 = configDirExists && (defaultJsonExists || localJsonExists);
62
+ checks.push({
63
+ id: "config.files",
64
+ ok: ok2,
65
+ message: ok2 ? "Config directory and files present" : `Config directory/files missing under ${configRootUsed} (expected default.json and/or local.json)`,
66
+ details: verbose ? {
67
+ configRootUsed,
68
+ configDirExists,
69
+ defaultJsonExists,
70
+ localJsonExists
71
+ } : { configRootUsed }
72
+ });
73
+ }
74
+ let config = null;
75
+ try {
76
+ config = loadConfig({ configRoot });
77
+ checks.push({
78
+ id: "config.load",
79
+ ok: true,
80
+ message: "Config loaded and validated",
81
+ details: { configRootUsed }
82
+ });
83
+ } catch (err) {
84
+ checks.push({
85
+ id: "config.load",
86
+ ok: false,
87
+ message: `Config failed to load/validate: ${err.message}`,
88
+ details: { configRootAttempted: configRoot, configRootUsed }
89
+ });
90
+ }
91
+ if (config) {
92
+ const agentId = normalizeAgent(config.agent);
93
+ const installed = isAgentInstalled(agentId);
94
+ checks.push({
95
+ id: "agent.installed",
96
+ ok: installed,
97
+ message: installed ? `Agent '${agentId}' is installed` : `Agent '${agentId}' is not installed or not on PATH`
98
+ });
99
+ const pluginEntries = config.plugins ?? [];
100
+ for (let idx = 0; idx < pluginEntries.length; idx++) {
101
+ const entry = pluginEntries[idx];
102
+ const spec = typeof entry === "string" ? entry : entry.package;
103
+ if (!spec) continue;
104
+ try {
105
+ const mod = await importPluginModuleFromSpecifier(spec, resolutionRoot);
106
+ const plugin = coercePlugin(mod);
107
+ validatePluginSchema(plugin);
108
+ const options = typeof entry === "object" && entry.options ? entry.options : {};
109
+ const mergedOptions = applyPluginDefaults(plugin, options);
110
+ validatePluginConfig(plugin, mergedOptions);
111
+ checks.push({
112
+ id: `plugin.${idx}`,
113
+ ok: true,
114
+ message: `Plugin OK: ${plugin.name}@${plugin.version}`,
115
+ details: verbose ? {
116
+ spec,
117
+ resolutionRoot
118
+ } : void 0
119
+ });
120
+ } catch (err) {
121
+ checks.push({
122
+ id: `plugin.${idx}`,
123
+ ok: false,
124
+ message: `Plugin failed (${spec}): ${err.message}`,
125
+ details: verbose ? {
126
+ spec,
127
+ resolutionRoot
128
+ } : void 0
129
+ });
130
+ }
131
+ }
132
+ }
133
+ const ok = checks.every((c) => c.ok);
134
+ if (json) {
135
+ console.log(JSON.stringify({ ok, configRootUsed, resolutionRoot, checks }, null, 2));
136
+ } else {
137
+ console.log(`agent-detective doctor: ${ok ? "OK" : "FAILED"}`);
138
+ console.log(`Using configRoot: ${configRootUsed}`);
139
+ for (const c of checks) {
140
+ console.log(`${c.ok ? "PASS" : "FAIL"} ${c.id} - ${c.message}`);
141
+ if (verbose && c.details) {
142
+ console.log(` details: ${JSON.stringify(c.details)}`);
143
+ }
144
+ }
145
+ }
146
+ process.exitCode = ok ? 0 : 1;
147
+ }
148
+ async function runValidateConfig({ installRoot, argv }) {
149
+ const { json, verbose } = parseFlags(argv);
150
+ const configRoot = resolveConfigDirFromInstallRoot(installRoot);
151
+ const configRootUsed = configRoot ?? resolve(process.cwd(), "config");
152
+ const explicitConfigRoot = hasExplicitConfigRootArg(argv) || hasExplicitConfigRootEnv();
153
+ const configDirExists = existsSync(configRootUsed);
154
+ const defaultJsonExists = existsSync(resolve(configRootUsed, "default.json"));
155
+ const localJsonExists = existsSync(resolve(configRootUsed, "local.json"));
156
+ let ok = true;
157
+ let message = "Config loaded and validated";
158
+ let details;
159
+ if (explicitConfigRoot && (!configDirExists || !defaultJsonExists && !localJsonExists)) {
160
+ ok = false;
161
+ message = `Config directory/files missing under ${configRootUsed} (expected default.json and/or local.json)`;
162
+ details = verbose ? {
163
+ configRootUsed,
164
+ configDirExists,
165
+ defaultJsonExists,
166
+ localJsonExists
167
+ } : { configRootUsed };
168
+ }
169
+ try {
170
+ if (ok) {
171
+ loadConfig({ configRoot });
172
+ details = { ...details ?? {}, configRootUsed };
173
+ }
174
+ } catch (err) {
175
+ ok = false;
176
+ message = `Config failed to load/validate: ${err.message}`;
177
+ details = { configRootAttempted: configRoot, configRootUsed };
178
+ }
179
+ if (json) {
180
+ console.log(JSON.stringify({ ok, check: { id: "config.load", ok, message, details } }, null, 2));
181
+ } else {
182
+ console.log(`${ok ? "PASS" : "FAIL"} config.load - ${message}`);
183
+ console.log(`Using configRoot: ${configRootUsed}`);
184
+ if (verbose && details) {
185
+ console.log(` details: ${JSON.stringify(details)}`);
186
+ }
187
+ }
188
+ process.exitCode = ok ? 0 : 1;
189
+ }
190
+ export {
191
+ runDoctor,
192
+ runValidateConfig
193
+ };