@moda-ai/cli 1.9.6 → 1.10.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,349 @@
1
+ /**
2
+ * moda.intelligence.v1 — the unified Moda intelligence wire format (MODA-388).
3
+ *
4
+ * ONE versioned payload that every surface projects from:
5
+ * - CLI `moda ask --json` / `--agent` (the agent-facing contract),
6
+ * - the backend `POST /api/cli/ask` response,
7
+ * - the (future) MCP server's `structuredContent`,
8
+ * - the dashboard UI.
9
+ *
10
+ * Design rules:
11
+ * - snake_case everywhere (no mixed casing — the prior split between the
12
+ * snake_case envelope and the camelCase `production_ask.v0.1` data payload
13
+ * was the main source of agent-integration pain).
14
+ * - It is a SUPERSET. Differentiation between a human reader and an agent is a
15
+ * RENDERING concern at the edge, not a forked schema:
16
+ * · human projection → narrative `answer_markdown`, `summary.confidence`
17
+ * as a word, raw ids/anchors hidden.
18
+ * · agent projection → full `evidence_refs` (with `anchor` + `grounding`),
19
+ * `answer_claims` (claim → evidence), structured
20
+ * `next_commands`.
21
+ * - Additive evolution only within v1; breaking changes bump the version.
22
+ *
23
+ * This module is the SINGLE SOURCE OF TRUTH for the shape. The backend mirrors
24
+ * these field names on the wire and the contract tests validate real CLI output
25
+ * against {@link MODA_INTELLIGENCE_V1_JSON_SCHEMA}.
26
+ */
27
+ export declare const INTELLIGENCE_SCHEMA_VERSION: "moda.intelligence.v1";
28
+ export type IntelligenceConfidence = 'high' | 'medium' | 'low' | 'unknown';
29
+ /** Where the answer came from. `local_fallback` ⇒ degraded (lower trust). */
30
+ export type IntelligenceSource = 'cloud' | 'local_fallback';
31
+ /** How a signal was derived — a measured fact vs a model/heuristic estimate. */
32
+ export type Provenance = 'measured' | 'inferred';
33
+ /** Whether a suggested command reads, writes, or syncs state. */
34
+ export type CommandMutability = 'read' | 'write' | 'sync' | 'unknown';
35
+ /** Classification of a single quantitative/factual claim in the answer. */
36
+ export type ClaimKind = 'percentage' | 'count' | 'qualitative';
37
+ /**
38
+ * Verdict for one claim made in the answer, checked against the structured tool
39
+ * data the agent actually saw (MODA-377/378). `unverifiable` means the claim is
40
+ * not a checkable figure (or there was no tool data to check it against).
41
+ */
42
+ export type ClaimStatus = 'grounded' | 'ungrounded' | 'unverifiable';
43
+ export interface IntelligenceSummary {
44
+ text: string;
45
+ confidence: IntelligenceConfidence;
46
+ }
47
+ /**
48
+ * A structured locator so a surface can deep-link a piece of evidence. All
49
+ * fields optional — different evidence kinds populate different anchors.
50
+ */
51
+ export interface EvidenceAnchor {
52
+ conversation_id?: string;
53
+ message_index?: number;
54
+ run_id?: string;
55
+ tool_name?: string;
56
+ }
57
+ export interface IntelligenceEvidenceRef {
58
+ /** Stable id, referenced by `findings[].evidence_ref_ids` and `answer_claims[].evidence_ref_ids`. */
59
+ id: string;
60
+ /** 'overview' | 'conversation' | 'message' | 'tool_failure' | 'run' | 'frustration' | 'cluster' | ... */
61
+ kind: string;
62
+ label: string;
63
+ anchor?: EvidenceAnchor;
64
+ /** Short human-readable excerpt of the underlying value. */
65
+ snippet?: string;
66
+ /** measured (exact) vs inferred (model-derived estimate) — MODA-379. */
67
+ grounding: Provenance;
68
+ }
69
+ /**
70
+ * A claim extracted from the answer with its grounding verdict. `value` is set
71
+ * for numeric claims; `evidence_ref_ids` links to the evidence that supports it
72
+ * (empty when ungrounded/unverifiable).
73
+ */
74
+ export interface IntelligenceClaim {
75
+ text: string;
76
+ value?: number;
77
+ kind: ClaimKind;
78
+ status: ClaimStatus;
79
+ evidence_ref_ids: string[];
80
+ /** Path into the structured tool data where the value matched, when grounded. */
81
+ matched_path?: string;
82
+ }
83
+ export interface IntelligenceRecommendedAction {
84
+ id: string;
85
+ title: string;
86
+ rationale?: string;
87
+ command?: string;
88
+ mutability: CommandMutability;
89
+ requires_approval: boolean;
90
+ }
91
+ export interface IntelligenceFinding {
92
+ id: string;
93
+ title: string;
94
+ severity?: 'critical' | 'high' | 'medium' | 'low' | 'info';
95
+ description?: string;
96
+ evidence_ref_ids: string[];
97
+ recommended_actions: IntelligenceRecommendedAction[];
98
+ }
99
+ export interface IntelligenceNextCommand {
100
+ command: string;
101
+ purpose?: string;
102
+ mutability: CommandMutability;
103
+ requires_approval: boolean;
104
+ }
105
+ /**
106
+ * The canonical `moda.intelligence.v1` payload. Emitted by the backend ask
107
+ * endpoint and surfaced as the CLI agent envelope's `data` (and by `--json`).
108
+ */
109
+ export interface ModaIntelligenceV1 {
110
+ schema_version: typeof INTELLIGENCE_SCHEMA_VERSION;
111
+ generated_at: string;
112
+ /** The originating command, e.g. "ask". */
113
+ command: string;
114
+ source: IntelligenceSource;
115
+ /** True when the answer was NOT produced by Moda Cloud intelligence. */
116
+ degraded: boolean;
117
+ question: string;
118
+ summary: IntelligenceSummary;
119
+ /** Full markdown answer (the human-facing narrative). */
120
+ answer_markdown: string;
121
+ /** Per-claim grounding verdicts (agent projection). */
122
+ answer_claims: IntelligenceClaim[];
123
+ /** Ranked findings; may be empty for a plain `ask`. */
124
+ findings: IntelligenceFinding[];
125
+ evidence_refs: IntelligenceEvidenceRef[];
126
+ next_commands: IntelligenceNextCommand[];
127
+ warnings: string[];
128
+ meta?: Record<string, unknown>;
129
+ }
130
+ /**
131
+ * Published draft-07 JSON Schema for {@link ModaIntelligenceV1}. This is what
132
+ * `moda --json-schemas` emits and what the contract tests validate CLI/backend
133
+ * output against. Keep in lockstep with the interfaces above.
134
+ */
135
+ export declare const MODA_INTELLIGENCE_V1_JSON_SCHEMA: {
136
+ readonly $schema: "http://json-schema.org/draft-07/schema#";
137
+ readonly $id: "moda.intelligence.v1";
138
+ readonly title: "Moda Intelligence v1";
139
+ readonly type: "object";
140
+ readonly additionalProperties: false;
141
+ readonly required: readonly ["schema_version", "generated_at", "command", "source", "degraded", "question", "summary", "answer_markdown", "answer_claims", "findings", "evidence_refs", "next_commands", "warnings"];
142
+ readonly properties: {
143
+ readonly schema_version: {
144
+ readonly const: "moda.intelligence.v1";
145
+ };
146
+ readonly generated_at: {
147
+ readonly type: "string";
148
+ readonly format: "date-time";
149
+ };
150
+ readonly command: {
151
+ readonly type: "string";
152
+ };
153
+ readonly source: {
154
+ readonly type: "string";
155
+ readonly enum: readonly ["cloud", "local_fallback"];
156
+ };
157
+ readonly degraded: {
158
+ readonly type: "boolean";
159
+ };
160
+ readonly question: {
161
+ readonly type: "string";
162
+ };
163
+ readonly summary: {
164
+ readonly type: "object";
165
+ readonly additionalProperties: false;
166
+ readonly required: readonly ["text", "confidence"];
167
+ readonly properties: {
168
+ readonly text: {
169
+ readonly type: "string";
170
+ };
171
+ readonly confidence: {
172
+ readonly type: "string";
173
+ readonly enum: readonly ["high", "medium", "low", "unknown"];
174
+ };
175
+ };
176
+ };
177
+ readonly answer_markdown: {
178
+ readonly type: "string";
179
+ };
180
+ readonly answer_claims: {
181
+ readonly type: "array";
182
+ readonly items: {
183
+ readonly type: "object";
184
+ readonly additionalProperties: false;
185
+ readonly required: readonly ["text", "kind", "status", "evidence_ref_ids"];
186
+ readonly properties: {
187
+ readonly text: {
188
+ readonly type: "string";
189
+ };
190
+ readonly value: {
191
+ readonly type: "number";
192
+ };
193
+ readonly kind: {
194
+ readonly type: "string";
195
+ readonly enum: readonly ["percentage", "count", "qualitative"];
196
+ };
197
+ readonly status: {
198
+ readonly type: "string";
199
+ readonly enum: readonly ["grounded", "ungrounded", "unverifiable"];
200
+ };
201
+ readonly evidence_ref_ids: {
202
+ readonly type: "array";
203
+ readonly items: {
204
+ readonly type: "string";
205
+ };
206
+ };
207
+ readonly matched_path: {
208
+ readonly type: "string";
209
+ };
210
+ };
211
+ };
212
+ };
213
+ readonly findings: {
214
+ readonly type: "array";
215
+ readonly items: {
216
+ readonly type: "object";
217
+ readonly additionalProperties: false;
218
+ readonly required: readonly ["id", "title", "evidence_ref_ids", "recommended_actions"];
219
+ readonly properties: {
220
+ readonly id: {
221
+ readonly type: "string";
222
+ };
223
+ readonly title: {
224
+ readonly type: "string";
225
+ };
226
+ readonly severity: {
227
+ readonly type: "string";
228
+ readonly enum: readonly ["critical", "high", "medium", "low", "info"];
229
+ };
230
+ readonly description: {
231
+ readonly type: "string";
232
+ };
233
+ readonly evidence_ref_ids: {
234
+ readonly type: "array";
235
+ readonly items: {
236
+ readonly type: "string";
237
+ };
238
+ };
239
+ readonly recommended_actions: {
240
+ readonly type: "array";
241
+ readonly items: {
242
+ readonly type: "object";
243
+ readonly additionalProperties: false;
244
+ readonly required: readonly ["id", "title", "mutability", "requires_approval"];
245
+ readonly properties: {
246
+ readonly id: {
247
+ readonly type: "string";
248
+ };
249
+ readonly title: {
250
+ readonly type: "string";
251
+ };
252
+ readonly rationale: {
253
+ readonly type: "string";
254
+ };
255
+ readonly command: {
256
+ readonly type: "string";
257
+ };
258
+ readonly mutability: {
259
+ readonly type: "string";
260
+ readonly enum: readonly ["read", "write", "sync", "unknown"];
261
+ };
262
+ readonly requires_approval: {
263
+ readonly type: "boolean";
264
+ };
265
+ };
266
+ };
267
+ };
268
+ };
269
+ };
270
+ };
271
+ readonly evidence_refs: {
272
+ readonly type: "array";
273
+ readonly items: {
274
+ readonly type: "object";
275
+ readonly additionalProperties: false;
276
+ readonly required: readonly ["id", "kind", "label", "grounding"];
277
+ readonly properties: {
278
+ readonly id: {
279
+ readonly type: "string";
280
+ };
281
+ readonly kind: {
282
+ readonly type: "string";
283
+ };
284
+ readonly label: {
285
+ readonly type: "string";
286
+ };
287
+ readonly anchor: {
288
+ readonly type: "object";
289
+ readonly additionalProperties: false;
290
+ readonly properties: {
291
+ readonly conversation_id: {
292
+ readonly type: "string";
293
+ };
294
+ readonly message_index: {
295
+ readonly type: "number";
296
+ };
297
+ readonly run_id: {
298
+ readonly type: "string";
299
+ };
300
+ readonly tool_name: {
301
+ readonly type: "string";
302
+ };
303
+ };
304
+ };
305
+ readonly snippet: {
306
+ readonly type: "string";
307
+ };
308
+ readonly grounding: {
309
+ readonly type: "string";
310
+ readonly enum: readonly ["measured", "inferred"];
311
+ };
312
+ };
313
+ };
314
+ };
315
+ readonly next_commands: {
316
+ readonly type: "array";
317
+ readonly items: {
318
+ readonly type: "object";
319
+ readonly additionalProperties: false;
320
+ readonly required: readonly ["command", "mutability", "requires_approval"];
321
+ readonly properties: {
322
+ readonly command: {
323
+ readonly type: "string";
324
+ };
325
+ readonly purpose: {
326
+ readonly type: "string";
327
+ };
328
+ readonly mutability: {
329
+ readonly type: "string";
330
+ readonly enum: readonly ["read", "write", "sync", "unknown"];
331
+ };
332
+ readonly requires_approval: {
333
+ readonly type: "boolean";
334
+ };
335
+ };
336
+ };
337
+ };
338
+ readonly warnings: {
339
+ readonly type: "array";
340
+ readonly items: {
341
+ readonly type: "string";
342
+ };
343
+ };
344
+ readonly meta: {
345
+ readonly type: "object";
346
+ };
347
+ };
348
+ };
349
+ //# sourceMappingURL=intelligence-schema.d.ts.map
@@ -0,0 +1,174 @@
1
+ /**
2
+ * moda.intelligence.v1 — the unified Moda intelligence wire format (MODA-388).
3
+ *
4
+ * ONE versioned payload that every surface projects from:
5
+ * - CLI `moda ask --json` / `--agent` (the agent-facing contract),
6
+ * - the backend `POST /api/cli/ask` response,
7
+ * - the (future) MCP server's `structuredContent`,
8
+ * - the dashboard UI.
9
+ *
10
+ * Design rules:
11
+ * - snake_case everywhere (no mixed casing — the prior split between the
12
+ * snake_case envelope and the camelCase `production_ask.v0.1` data payload
13
+ * was the main source of agent-integration pain).
14
+ * - It is a SUPERSET. Differentiation between a human reader and an agent is a
15
+ * RENDERING concern at the edge, not a forked schema:
16
+ * · human projection → narrative `answer_markdown`, `summary.confidence`
17
+ * as a word, raw ids/anchors hidden.
18
+ * · agent projection → full `evidence_refs` (with `anchor` + `grounding`),
19
+ * `answer_claims` (claim → evidence), structured
20
+ * `next_commands`.
21
+ * - Additive evolution only within v1; breaking changes bump the version.
22
+ *
23
+ * This module is the SINGLE SOURCE OF TRUTH for the shape. The backend mirrors
24
+ * these field names on the wire and the contract tests validate real CLI output
25
+ * against {@link MODA_INTELLIGENCE_V1_JSON_SCHEMA}.
26
+ */
27
+ export const INTELLIGENCE_SCHEMA_VERSION = 'moda.intelligence.v1';
28
+ // ───────────────────────────── JSON Schema (draft-07) ─────────────────────────
29
+ const provenanceSchema = { type: 'string', enum: ['measured', 'inferred'] };
30
+ const mutabilitySchema = {
31
+ type: 'string',
32
+ enum: ['read', 'write', 'sync', 'unknown'],
33
+ };
34
+ const confidenceSchema = {
35
+ type: 'string',
36
+ enum: ['high', 'medium', 'low', 'unknown'],
37
+ };
38
+ /**
39
+ * Published draft-07 JSON Schema for {@link ModaIntelligenceV1}. This is what
40
+ * `moda --json-schemas` emits and what the contract tests validate CLI/backend
41
+ * output against. Keep in lockstep with the interfaces above.
42
+ */
43
+ export const MODA_INTELLIGENCE_V1_JSON_SCHEMA = {
44
+ $schema: 'http://json-schema.org/draft-07/schema#',
45
+ $id: 'moda.intelligence.v1',
46
+ title: 'Moda Intelligence v1',
47
+ type: 'object',
48
+ additionalProperties: false,
49
+ required: [
50
+ 'schema_version',
51
+ 'generated_at',
52
+ 'command',
53
+ 'source',
54
+ 'degraded',
55
+ 'question',
56
+ 'summary',
57
+ 'answer_markdown',
58
+ 'answer_claims',
59
+ 'findings',
60
+ 'evidence_refs',
61
+ 'next_commands',
62
+ 'warnings',
63
+ ],
64
+ properties: {
65
+ schema_version: { const: 'moda.intelligence.v1' },
66
+ generated_at: { type: 'string', format: 'date-time' },
67
+ command: { type: 'string' },
68
+ source: { type: 'string', enum: ['cloud', 'local_fallback'] },
69
+ degraded: { type: 'boolean' },
70
+ question: { type: 'string' },
71
+ summary: {
72
+ type: 'object',
73
+ additionalProperties: false,
74
+ required: ['text', 'confidence'],
75
+ properties: { text: { type: 'string' }, confidence: confidenceSchema },
76
+ },
77
+ answer_markdown: { type: 'string' },
78
+ answer_claims: {
79
+ type: 'array',
80
+ items: {
81
+ type: 'object',
82
+ additionalProperties: false,
83
+ required: ['text', 'kind', 'status', 'evidence_ref_ids'],
84
+ properties: {
85
+ text: { type: 'string' },
86
+ value: { type: 'number' },
87
+ kind: { type: 'string', enum: ['percentage', 'count', 'qualitative'] },
88
+ status: {
89
+ type: 'string',
90
+ enum: ['grounded', 'ungrounded', 'unverifiable'],
91
+ },
92
+ evidence_ref_ids: { type: 'array', items: { type: 'string' } },
93
+ matched_path: { type: 'string' },
94
+ },
95
+ },
96
+ },
97
+ findings: {
98
+ type: 'array',
99
+ items: {
100
+ type: 'object',
101
+ additionalProperties: false,
102
+ required: ['id', 'title', 'evidence_ref_ids', 'recommended_actions'],
103
+ properties: {
104
+ id: { type: 'string' },
105
+ title: { type: 'string' },
106
+ severity: {
107
+ type: 'string',
108
+ enum: ['critical', 'high', 'medium', 'low', 'info'],
109
+ },
110
+ description: { type: 'string' },
111
+ evidence_ref_ids: { type: 'array', items: { type: 'string' } },
112
+ recommended_actions: {
113
+ type: 'array',
114
+ items: {
115
+ type: 'object',
116
+ additionalProperties: false,
117
+ required: ['id', 'title', 'mutability', 'requires_approval'],
118
+ properties: {
119
+ id: { type: 'string' },
120
+ title: { type: 'string' },
121
+ rationale: { type: 'string' },
122
+ command: { type: 'string' },
123
+ mutability: mutabilitySchema,
124
+ requires_approval: { type: 'boolean' },
125
+ },
126
+ },
127
+ },
128
+ },
129
+ },
130
+ },
131
+ evidence_refs: {
132
+ type: 'array',
133
+ items: {
134
+ type: 'object',
135
+ additionalProperties: false,
136
+ required: ['id', 'kind', 'label', 'grounding'],
137
+ properties: {
138
+ id: { type: 'string' },
139
+ kind: { type: 'string' },
140
+ label: { type: 'string' },
141
+ anchor: {
142
+ type: 'object',
143
+ additionalProperties: false,
144
+ properties: {
145
+ conversation_id: { type: 'string' },
146
+ message_index: { type: 'number' },
147
+ run_id: { type: 'string' },
148
+ tool_name: { type: 'string' },
149
+ },
150
+ },
151
+ snippet: { type: 'string' },
152
+ grounding: provenanceSchema,
153
+ },
154
+ },
155
+ },
156
+ next_commands: {
157
+ type: 'array',
158
+ items: {
159
+ type: 'object',
160
+ additionalProperties: false,
161
+ required: ['command', 'mutability', 'requires_approval'],
162
+ properties: {
163
+ command: { type: 'string' },
164
+ purpose: { type: 'string' },
165
+ mutability: mutabilitySchema,
166
+ requires_approval: { type: 'boolean' },
167
+ },
168
+ },
169
+ },
170
+ warnings: { type: 'array', items: { type: 'string' } },
171
+ meta: { type: 'object' },
172
+ },
173
+ };
174
+ //# sourceMappingURL=intelligence-schema.js.map
package/dist/output.d.ts CHANGED
@@ -1,7 +1,18 @@
1
1
  export type CliOutputMode = 'human' | 'json' | 'agent' | 'agent-stream';
2
2
  export declare const AGENT_SCHEMA_VERSION = "moda.agent.v1";
3
3
  export declare const AGENT_EVENT_SCHEMA_VERSION = "moda.agent_event.v1";
4
- export type AgentStatus = 'ok' | 'error';
4
+ /**
5
+ * Stable process exit codes that an external agent can branch on:
6
+ * 0 ok — grounded answer from Moda Cloud intelligence.
7
+ * 3 degraded — answer produced from a local/degraded fallback (lower trust).
8
+ * 1 error — command/input/unexpected failure (see envelope `errors[]`).
9
+ * `EXIT_DEGRADED` is intentionally distinct so a caller can tell a degraded
10
+ * answer apart from both success and hard failure without parsing the payload.
11
+ */
12
+ export declare const EXIT_OK = 0;
13
+ export declare const EXIT_ERROR = 1;
14
+ export declare const EXIT_DEGRADED = 3;
15
+ export type AgentStatus = 'ok' | 'degraded' | 'error';
5
16
  export type AgentConfidence = 'high' | 'medium' | 'low' | 'unknown';
6
17
  export interface AgentSummary {
7
18
  text: string;
@@ -94,12 +105,24 @@ export interface CliOutput {
94
105
  mode: CliOutputMode;
95
106
  quiet: boolean;
96
107
  writeData(data: unknown, options?: {
97
- status?: 'success' | 'error';
108
+ status?: 'success' | 'degraded' | 'error';
98
109
  meta?: Record<string, unknown>;
99
110
  } & AgentWriteOptions): void;
100
111
  writeEvent(event: Omit<AgentEvent, 'schema_version' | 'command' | 'generated_at'>): void;
101
112
  }
113
+ /** Flag-only resolution (default human). Kept for callers that only have flags. */
102
114
  export declare function outputModeFromFlags(flags: Record<string, string>): CliOutputMode;
115
+ /**
116
+ * Resolve the effective output mode for a real CLI invocation, in priority
117
+ * order: explicit flags -> `MODA_FORMAT` -> agent-environment detection ->
118
+ * TTY detection (piped/redirected => agent JSON) -> human prose. This is what
119
+ * makes the CLI agent-safe: a piped/automated caller gets structured output
120
+ * with zero flags, while a human at a terminal still gets prose.
121
+ */
122
+ export declare function resolveOutputMode(flags: Record<string, string>, opts?: {
123
+ isTTY?: boolean;
124
+ env?: NodeJS.ProcessEnv;
125
+ }): CliOutputMode;
103
126
  export declare function createCliOutput(options: {
104
127
  command: string;
105
128
  mode: CliOutputMode;
package/dist/output.js CHANGED
@@ -1,12 +1,77 @@
1
1
  export const AGENT_SCHEMA_VERSION = 'moda.agent.v1';
2
2
  export const AGENT_EVENT_SCHEMA_VERSION = 'moda.agent_event.v1';
3
- export function outputModeFromFlags(flags) {
3
+ /**
4
+ * Stable process exit codes that an external agent can branch on:
5
+ * 0 ok — grounded answer from Moda Cloud intelligence.
6
+ * 3 degraded — answer produced from a local/degraded fallback (lower trust).
7
+ * 1 error — command/input/unexpected failure (see envelope `errors[]`).
8
+ * `EXIT_DEGRADED` is intentionally distinct so a caller can tell a degraded
9
+ * answer apart from both success and hard failure without parsing the payload.
10
+ */
11
+ export const EXIT_OK = 0;
12
+ export const EXIT_ERROR = 1;
13
+ export const EXIT_DEGRADED = 3;
14
+ /**
15
+ * Output mode requested by explicit flags only, or `undefined` when the caller
16
+ * did not pick one. Explicit flags always win over auto-detection.
17
+ */
18
+ function explicitModeFromFlags(flags) {
4
19
  if (flags.agent === 'true' && (flags.stream === 'true' || flags.format === 'ndjson'))
5
20
  return 'agent-stream';
6
21
  if (flags.agent === 'true')
7
22
  return 'agent';
8
23
  if (flags.json === 'true')
9
24
  return 'json';
25
+ if (flags.human === 'true' || flags.prose === 'true')
26
+ return 'human';
27
+ return undefined;
28
+ }
29
+ /** Flag-only resolution (default human). Kept for callers that only have flags. */
30
+ export function outputModeFromFlags(flags) {
31
+ return explicitModeFromFlags(flags) ?? 'human';
32
+ }
33
+ function outputModeFromEnv(env) {
34
+ const fmt = (env.MODA_FORMAT ?? '').trim().toLowerCase();
35
+ if (fmt === 'human' || fmt === 'json' || fmt === 'agent')
36
+ return fmt;
37
+ if (fmt === 'agent-stream' || fmt === 'ndjson')
38
+ return 'agent-stream';
39
+ return undefined;
40
+ }
41
+ function truthyEnv(value) {
42
+ return value != null && value !== '' && value !== '0' && value.toLowerCase() !== 'false';
43
+ }
44
+ /**
45
+ * True when the process appears to be driven by an agent / automation rather
46
+ * than an interactive human (Claude Code, Cursor, generic CI, or an explicit
47
+ * `MODA_AGENT` opt-in). Such callers get machine-parseable output by default.
48
+ */
49
+ function isAgentEnvironment(env) {
50
+ return (truthyEnv(env.CLAUDECODE) ||
51
+ truthyEnv(env.CURSOR) ||
52
+ truthyEnv(env.CI) ||
53
+ truthyEnv(env.MODA_AGENT));
54
+ }
55
+ /**
56
+ * Resolve the effective output mode for a real CLI invocation, in priority
57
+ * order: explicit flags -> `MODA_FORMAT` -> agent-environment detection ->
58
+ * TTY detection (piped/redirected => agent JSON) -> human prose. This is what
59
+ * makes the CLI agent-safe: a piped/automated caller gets structured output
60
+ * with zero flags, while a human at a terminal still gets prose.
61
+ */
62
+ export function resolveOutputMode(flags, opts = {}) {
63
+ const explicit = explicitModeFromFlags(flags);
64
+ if (explicit)
65
+ return explicit;
66
+ const env = opts.env ?? process.env;
67
+ const fromEnv = outputModeFromEnv(env);
68
+ if (fromEnv)
69
+ return fromEnv;
70
+ if (isAgentEnvironment(env))
71
+ return 'agent';
72
+ const isTTY = opts.isTTY ?? Boolean(process.stdout.isTTY);
73
+ if (!isTTY)
74
+ return 'agent';
10
75
  return 'human';
11
76
  }
12
77
  export function createCliOutput(options) {
@@ -19,7 +84,11 @@ export function createCliOutput(options) {
19
84
  if (options.mode === 'agent' || options.mode === 'agent-stream') {
20
85
  const envelope = createAgentEnvelope({
21
86
  command: options.command,
22
- status: writeOptions.status === 'error' ? 'error' : 'ok',
87
+ status: writeOptions.status === 'error'
88
+ ? 'error'
89
+ : writeOptions.status === 'degraded'
90
+ ? 'degraded'
91
+ : 'ok',
23
92
  data,
24
93
  meta: writeOptions.meta,
25
94
  summary: writeOptions.summary,