@moda-ai/cli 1.22.1 → 1.23.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.
Files changed (118) hide show
  1. package/dist/cli-c4se7a5d.js +762 -0
  2. package/dist/cli-mnqa4rp6.js +1563 -0
  3. package/dist/cli-stmjdgvy.js +2842 -0
  4. package/dist/cli-y55bgg14.js +6980 -0
  5. package/dist/cli-ysegdrm5.js +286 -0
  6. package/dist/cli.js +4779 -1159
  7. package/dist/harness-github-actions-7t3cncnt.js +95 -0
  8. package/dist/index-4ser01zr.js +2769 -0
  9. package/dist/provision-hm1ppwj6.js +74 -0
  10. package/package.json +3 -2
  11. package/skills/integration/index.json +2 -2
  12. package/dist/agent-schemas.d.ts +0 -3
  13. package/dist/agent-schemas.js +0 -358
  14. package/dist/agent.d.ts +0 -5
  15. package/dist/agent.js +0 -477
  16. package/dist/analyst-tui.d.ts +0 -81
  17. package/dist/analyst-tui.js +0 -314
  18. package/dist/api-client.d.ts +0 -46
  19. package/dist/api-client.js +0 -120
  20. package/dist/ask-stream.d.ts +0 -146
  21. package/dist/ask-stream.js +0 -473
  22. package/dist/auth-storage.d.ts +0 -35
  23. package/dist/auth-storage.js +0 -159
  24. package/dist/auth.d.ts +0 -50
  25. package/dist/auth.js +0 -694
  26. package/dist/cli-session.d.ts +0 -26
  27. package/dist/cli-session.js +0 -169
  28. package/dist/cli.d.ts +0 -44
  29. package/dist/command-context.d.ts +0 -62
  30. package/dist/command-context.js +0 -57
  31. package/dist/command-registry.d.ts +0 -22
  32. package/dist/command-registry.js +0 -15
  33. package/dist/config.d.ts +0 -60
  34. package/dist/config.js +0 -110
  35. package/dist/doctor.d.ts +0 -79
  36. package/dist/doctor.js +0 -1182
  37. package/dist/errors.d.ts +0 -50
  38. package/dist/errors.js +0 -64
  39. package/dist/exit-funnel.d.ts +0 -14
  40. package/dist/exit-funnel.js +0 -53
  41. package/dist/harness-artifact-bodies.d.ts +0 -28
  42. package/dist/harness-artifact-bodies.js +0 -276
  43. package/dist/harness-candidates.d.ts +0 -77
  44. package/dist/harness-candidates.js +0 -482
  45. package/dist/harness-remote.d.ts +0 -135
  46. package/dist/harness-remote.js +0 -540
  47. package/dist/harness-report.d.ts +0 -145
  48. package/dist/harness-report.js +0 -1579
  49. package/dist/harness-sync-limits.d.ts +0 -8
  50. package/dist/harness-sync-limits.js +0 -21
  51. package/dist/harness.d.ts +0 -463
  52. package/dist/harness.js +0 -4009
  53. package/dist/init/index.d.ts +0 -78
  54. package/dist/init/index.js +0 -1442
  55. package/dist/init/mcp.d.ts +0 -2
  56. package/dist/init/mcp.js +0 -27
  57. package/dist/init/progress-board.d.ts +0 -80
  58. package/dist/init/progress-board.js +0 -225
  59. package/dist/init/rules.d.ts +0 -6
  60. package/dist/init/rules.js +0 -171
  61. package/dist/init/sdk-catalog.d.ts +0 -10
  62. package/dist/init/sdk-catalog.js +0 -80
  63. package/dist/init/sdk-guide.d.ts +0 -19
  64. package/dist/init/sdk-guide.js +0 -185
  65. package/dist/init/sdk-integration.d.ts +0 -53
  66. package/dist/init/sdk-integration.js +0 -835
  67. package/dist/init/sdk-protocol.d.ts +0 -135
  68. package/dist/init/sdk-protocol.js +0 -152
  69. package/dist/init/sdk-verification.d.ts +0 -27
  70. package/dist/init/sdk-verification.js +0 -123
  71. package/dist/init/tenant.d.ts +0 -67
  72. package/dist/init/tenant.js +0 -421
  73. package/dist/intelligence-schema.d.ts +0 -349
  74. package/dist/intelligence-schema.js +0 -174
  75. package/dist/keys.d.ts +0 -47
  76. package/dist/keys.js +0 -136
  77. package/dist/manifest.d.ts +0 -5
  78. package/dist/manifest.js +0 -114
  79. package/dist/markdown-terminal.d.ts +0 -49
  80. package/dist/markdown-terminal.js +0 -212
  81. package/dist/output.d.ts +0 -191
  82. package/dist/output.js +0 -624
  83. package/dist/production-intelligence.d.ts +0 -175
  84. package/dist/production-intelligence.js +0 -1430
  85. package/dist/profile-commands.d.ts +0 -10
  86. package/dist/profile-commands.js +0 -342
  87. package/dist/profiles.d.ts +0 -112
  88. package/dist/profiles.js +0 -573
  89. package/dist/prompts-ab.d.ts +0 -71
  90. package/dist/prompts-ab.js +0 -313
  91. package/dist/prompts-propose.d.ts +0 -15
  92. package/dist/prompts-propose.js +0 -212
  93. package/dist/prompts.d.ts +0 -58
  94. package/dist/prompts.js +0 -418
  95. package/dist/provision.d.ts +0 -28
  96. package/dist/provision.js +0 -104
  97. package/dist/redact.d.ts +0 -2
  98. package/dist/redact.js +0 -90
  99. package/dist/schemas.d.ts +0 -225
  100. package/dist/schemas.js +0 -126
  101. package/dist/skills-bundled.d.ts +0 -35
  102. package/dist/skills-bundled.js +0 -157
  103. package/dist/skills-manifest.d.ts +0 -33
  104. package/dist/skills-manifest.js +0 -244
  105. package/dist/skills.d.ts +0 -57
  106. package/dist/skills.js +0 -783
  107. package/dist/sync.d.ts +0 -5
  108. package/dist/sync.js +0 -36
  109. package/dist/telemetry.d.ts +0 -56
  110. package/dist/telemetry.js +0 -167
  111. package/dist/transforms.d.ts +0 -25
  112. package/dist/transforms.js +0 -102
  113. package/dist/tui.d.ts +0 -48
  114. package/dist/tui.js +0 -393
  115. package/dist/update-check.d.ts +0 -31
  116. package/dist/update-check.js +0 -141
  117. package/dist/workspace.d.ts +0 -12
  118. package/dist/workspace.js +0 -1294
@@ -0,0 +1,286 @@
1
+ import {
2
+ authFetch,
3
+ getCachedLoginContext
4
+ } from "./cli-c4se7a5d.js";
5
+ import {
6
+ CliAccessError,
7
+ CliInputError,
8
+ CliInputRequiredError,
9
+ embeddedTenantIdFromApiKey,
10
+ getModaDir,
11
+ loadCliConfig,
12
+ resolveApiKeyFromActiveProfile,
13
+ resolveIngestUrl,
14
+ resolveModaBaseUrl,
15
+ resolveTenantIdFromActiveProfile,
16
+ saveCliConfig,
17
+ writeSecretJsonFile
18
+ } from "./cli-mnqa4rp6.js";
19
+
20
+ // src/init/tenant.ts
21
+ import { randomUUID } from "node:crypto";
22
+ import { existsSync, readFileSync, rmSync } from "node:fs";
23
+ import { hostname } from "node:os";
24
+ import { join } from "node:path";
25
+ import { select } from "@inquirer/prompts";
26
+ function formatControlPlaneFetchError(action, status, body) {
27
+ let parsed;
28
+ try {
29
+ parsed = JSON.parse(body);
30
+ } catch {}
31
+ const isDbUnavailable = status === 503 && (parsed?.error === "database_unavailable" || parsed?.retryable === true || /database_unavailable|too many database connections|rds_reserved|connection pool/i.test(body));
32
+ if (isDbUnavailable) {
33
+ return `Failed to ${action} (${status}): Moda control-plane database is temporarily unavailable ` + `(connection pool exhausted). This is not an authentication failure — your session token ` + `is fine. Retry in a few seconds.` + (parsed?.message ? ` (${parsed.message})` : "");
34
+ }
35
+ if (status >= 500 && /too many database connections|rds_reserved|remaining connection slots/i.test(body)) {
36
+ return `Failed to ${action} (${status}): Moda control-plane database ran out of connections. ` + `This is a retryable infra outage, not an auth failure. Retry in a few seconds. Body: ${body}`;
37
+ }
38
+ return `Failed to ${action} (${status}): ${body}`;
39
+ }
40
+ var ADMIN_ACCESS_MESSAGE = "You need admin or owner access to create API keys.";
41
+ var ADMIN_ACCESS_GUIDANCE = "Ask your organization owner to upgrade your role.";
42
+ var VALIDATE_TIMEOUT_MS = 3000;
43
+ var POST_BOOTSTRAP_VALIDATE_DELAYS_MS = [
44
+ 500,
45
+ 1000,
46
+ 2000
47
+ ];
48
+ var defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
49
+ var MAX_LABEL_LENGTH = 60;
50
+ var IDEMPOTENCY_KEY_PATTERN = /^[A-Za-z0-9_-]{16,128}$/;
51
+ function pendingBootstrapPath(options = {}) {
52
+ return join(getModaDir(options), "pending-bootstrap.json");
53
+ }
54
+ function loadPendingBootstrapState(options = {}) {
55
+ try {
56
+ const path = pendingBootstrapPath(options);
57
+ if (!existsSync(path))
58
+ return {};
59
+ const parsed = JSON.parse(readFileSync(path, "utf8"));
60
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
61
+ return {};
62
+ }
63
+ const state = {};
64
+ for (const [tenantId, entry] of Object.entries(parsed)) {
65
+ if (entry && typeof entry === "object" && typeof entry.idempotency_key === "string") {
66
+ state[tenantId] = entry;
67
+ }
68
+ }
69
+ return state;
70
+ } catch {
71
+ return {};
72
+ }
73
+ }
74
+ function reserveBootstrapIdempotencyKey(tenantId, options = {}) {
75
+ const state = loadPendingBootstrapState(options);
76
+ const existing = state[tenantId]?.idempotency_key;
77
+ if (existing && IDEMPOTENCY_KEY_PATTERN.test(existing)) {
78
+ return existing;
79
+ }
80
+ const key = randomUUID();
81
+ state[tenantId] = {
82
+ idempotency_key: key,
83
+ created_at: new Date().toISOString()
84
+ };
85
+ try {
86
+ writeSecretJsonFile(pendingBootstrapPath(options), state);
87
+ } catch {}
88
+ return key;
89
+ }
90
+ function clearBootstrapIdempotencyKey(tenantId, options = {}) {
91
+ try {
92
+ const state = loadPendingBootstrapState(options);
93
+ if (!(tenantId in state))
94
+ return;
95
+ delete state[tenantId];
96
+ const path = pendingBootstrapPath(options);
97
+ if (Object.keys(state).length === 0) {
98
+ rmSync(path, { force: true });
99
+ } else {
100
+ writeSecretJsonFile(path, state);
101
+ }
102
+ } catch {}
103
+ }
104
+ function bootstrapLabel(custom) {
105
+ const clipped = custom?.trim().slice(0, MAX_LABEL_LENGTH);
106
+ if (clipped)
107
+ return clipped;
108
+ try {
109
+ return hostname().slice(0, MAX_LABEL_LENGTH) || "moda init";
110
+ } catch {
111
+ return "moda init";
112
+ }
113
+ }
114
+ var embeddedTenantId = embeddedTenantIdFromApiKey;
115
+ async function isStoredKeyLive(key, options = {}) {
116
+ try {
117
+ const response = await fetch(`${resolveModaBaseUrl(options)}/api/v1/data/overview`, {
118
+ method: "GET",
119
+ headers: { "x-api-key": key },
120
+ signal: AbortSignal.timeout(VALIDATE_TIMEOUT_MS)
121
+ });
122
+ if (response.status !== 200)
123
+ return false;
124
+ await response.json();
125
+ return true;
126
+ } catch {
127
+ return false;
128
+ }
129
+ }
130
+ function findStoredKeyForTenant(tenantId, options = {}) {
131
+ const legacy = loadCliConfig(options);
132
+ if (legacy.api_key && legacy.tenant_id === tenantId) {
133
+ return legacy.api_key;
134
+ }
135
+ if (resolveTenantIdFromActiveProfile(options) === tenantId) {
136
+ const profileKey = resolveApiKeyFromActiveProfile(options);
137
+ if (profileKey)
138
+ return profileKey;
139
+ }
140
+ return;
141
+ }
142
+ async function verifyFreshKeyLive(key, probeOptions, overrides) {
143
+ const delays = overrides?.delaysMs ?? POST_BOOTSTRAP_VALIDATE_DELAYS_MS;
144
+ const sleep = overrides?.sleep ?? defaultSleep;
145
+ if (await isStoredKeyLive(key, probeOptions))
146
+ return;
147
+ for (const delayMs of delays) {
148
+ await sleep(delayMs);
149
+ if (await isStoredKeyLive(key, probeOptions))
150
+ return;
151
+ }
152
+ console.error(" Warning: API key created but not yet active; propagation can take a few seconds. moda doctor --online will confirm.");
153
+ }
154
+ async function selectTenantAndCreateKey(options = {}) {
155
+ const log = (message) => {
156
+ if (!options.silent)
157
+ console.log(message);
158
+ };
159
+ const authOptions = {
160
+ profile: options.profile,
161
+ env: options.env
162
+ };
163
+ let ctx;
164
+ const cachedContext = getCachedLoginContext(authOptions);
165
+ if (cachedContext) {
166
+ ctx = cachedContext;
167
+ } else {
168
+ const contextRes = await authFetch("/api/cli/context", {}, authOptions);
169
+ if (!contextRes.ok) {
170
+ const body = await contextRes.text();
171
+ throw new Error(formatControlPlaneFetchError("fetch context", contextRes.status, body));
172
+ }
173
+ ctx = await contextRes.json();
174
+ }
175
+ log(` Authenticated as ${ctx.user.email}`);
176
+ if (ctx.tenants.length === 0) {
177
+ throw new CliAccessError("No organizations found. Create one at https://moda.dev");
178
+ }
179
+ let selected;
180
+ if (options.tenantId !== undefined) {
181
+ const match = ctx.tenants.find((tenant) => tenant.id === options.tenantId);
182
+ if (!match) {
183
+ const valid = ctx.tenants.map((tenant) => ` ${tenant.id} ${tenant.name} (${tenant.role})`).join(`
184
+ `);
185
+ throw new CliInputError(`No organization with id '${options.tenantId}'. Valid organizations:
186
+ ${valid}`, "Re-run with --tenant-id=<id> using one of the ids listed above.");
187
+ }
188
+ selected = match;
189
+ log(` Organization: ${selected.name}`);
190
+ } else if (ctx.tenants.length === 1) {
191
+ selected = ctx.tenants[0];
192
+ log(` Organization: ${selected.name}`);
193
+ } else if (options.nonInteractive) {
194
+ throw new CliInputRequiredError("Multiple Moda organizations are available; choose one and re-run with --tenant-id.", {
195
+ id: "init_tenant_selection",
196
+ question: "Select organization",
197
+ kind: "select",
198
+ choices: ctx.tenants.map((tenant) => ({
199
+ value: tenant.id,
200
+ label: `${tenant.name} (${tenant.role})`,
201
+ resume_flags: ["--tenant-id", tenant.id]
202
+ })),
203
+ resume_command: options.resumeCommand
204
+ }, "Re-run `moda init --tenant-id <tenant_id>`.");
205
+ } else {
206
+ selected = await select({
207
+ message: "Select organization",
208
+ choices: ctx.tenants.map((tenant) => ({
209
+ name: `${tenant.name} (${tenant.role})`,
210
+ value: tenant
211
+ }))
212
+ });
213
+ }
214
+ if (selected.role.toLowerCase() === "viewer") {
215
+ throw new CliAccessError(ADMIN_ACCESS_MESSAGE, { guidance: ADMIN_ACCESS_GUIDANCE });
216
+ }
217
+ let result;
218
+ let reused = false;
219
+ const storedKey = findStoredKeyForTenant(selected.id, authOptions);
220
+ if (storedKey && embeddedTenantId(storedKey) === selected.id && await isStoredKeyLive(storedKey, authOptions)) {
221
+ log(` Using existing CLI key for ${selected.name} (created earlier)`);
222
+ clearBootstrapIdempotencyKey(selected.id, authOptions);
223
+ reused = true;
224
+ result = {
225
+ apiKey: storedKey,
226
+ tenantId: selected.id,
227
+ tenantName: selected.name,
228
+ tenantSlug: selected.slug
229
+ };
230
+ }
231
+ if (!result) {
232
+ const idempotencyKey = reserveBootstrapIdempotencyKey(selected.id, authOptions);
233
+ const bootstrapRes = await authFetch("/api/cli/bootstrap", {
234
+ method: "POST",
235
+ headers: {
236
+ "Content-Type": "application/json",
237
+ "Idempotency-Key": idempotencyKey
238
+ },
239
+ body: JSON.stringify({
240
+ tenantId: selected.id,
241
+ label: bootstrapLabel(options.label)
242
+ })
243
+ }, authOptions);
244
+ if (bootstrapRes.status === 403) {
245
+ throw new CliAccessError(ADMIN_ACCESS_MESSAGE, { guidance: ADMIN_ACCESS_GUIDANCE });
246
+ }
247
+ if (bootstrapRes.status === 409) {
248
+ const body = await bootstrapRes.text();
249
+ let parsed;
250
+ try {
251
+ parsed = JSON.parse(body);
252
+ } catch {}
253
+ if (parsed?.error === "cli_key_cap_reached") {
254
+ throw new CliAccessError(parsed.message || "This organization has reached its CLI API key limit.", {
255
+ guidance: "Ask your organization admin to raise the CLI key limit in the dashboard " + '(Settings → API Keys → CLI key limit), or revoke unused "CLI - ..." keys, ' + "or reuse the existing key already saved in ~/.moda/config.json from a previous " + "moda init / moda provision --save, then retry."
256
+ });
257
+ }
258
+ throw new Error(`Failed to create API key (409): ${body}`);
259
+ }
260
+ if (!bootstrapRes.ok) {
261
+ const body = await bootstrapRes.text();
262
+ throw new Error(formatControlPlaneFetchError("create API key", bootstrapRes.status, body));
263
+ }
264
+ result = await bootstrapRes.json();
265
+ clearBootstrapIdempotencyKey(selected.id, authOptions);
266
+ await verifyFreshKeyLive(result.apiKey, authOptions, options.validateRetry);
267
+ }
268
+ if (options.persistConfig !== false) {
269
+ saveCliConfig({
270
+ api_key: result.apiKey,
271
+ tenant_id: result.tenantId,
272
+ ingest_url: resolveIngestUrl(authOptions)
273
+ }, authOptions);
274
+ const envTenantId = (options.env ?? process.env).MODA_TENANT_ID;
275
+ if (envTenantId && options.tenantIdSource !== "env" && envTenantId !== result.tenantId) {
276
+ console.error(` Warning: MODA_TENANT_ID is set to a different organization id and will shadow the saved selection (${result.tenantId}) for later commands. Unset it or set it to the selected organization id.`);
277
+ }
278
+ log(` API key + tenant saved to ~/.moda/config.json
279
+ `);
280
+ log(` (Set MODA_API_KEY / MODA_TENANT_ID to override for scripts/CI.)
281
+ `);
282
+ }
283
+ return { ...result, reused };
284
+ }
285
+
286
+ export { selectTenantAndCreateKey };