cognitive-modules-cli 2.2.1 → 2.2.7
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.
- package/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/README.md +35 -29
- package/dist/cli.js +519 -23
- package/dist/commands/add.d.ts +33 -14
- package/dist/commands/add.js +383 -16
- package/dist/commands/compose.js +60 -23
- package/dist/commands/index.d.ts +4 -0
- package/dist/commands/index.js +4 -0
- package/dist/commands/init.js +23 -1
- package/dist/commands/migrate.d.ts +30 -0
- package/dist/commands/migrate.js +650 -0
- package/dist/commands/pipe.d.ts +1 -0
- package/dist/commands/pipe.js +31 -11
- package/dist/commands/remove.js +33 -2
- package/dist/commands/run.d.ts +2 -0
- package/dist/commands/run.js +61 -28
- package/dist/commands/search.d.ts +28 -0
- package/dist/commands/search.js +143 -0
- package/dist/commands/test.d.ts +65 -0
- package/dist/commands/test.js +454 -0
- package/dist/commands/update.d.ts +1 -0
- package/dist/commands/update.js +106 -14
- package/dist/commands/validate.d.ts +36 -0
- package/dist/commands/validate.js +97 -0
- package/dist/errors/index.d.ts +225 -0
- package/dist/errors/index.js +420 -0
- package/dist/mcp/server.js +84 -79
- package/dist/modules/composition.js +97 -32
- package/dist/modules/loader.js +4 -2
- package/dist/modules/runner.d.ts +72 -5
- package/dist/modules/runner.js +306 -59
- package/dist/modules/subagent.d.ts +6 -1
- package/dist/modules/subagent.js +18 -13
- package/dist/modules/validator.js +14 -6
- package/dist/providers/anthropic.d.ts +15 -0
- package/dist/providers/anthropic.js +147 -5
- package/dist/providers/base.d.ts +11 -0
- package/dist/providers/base.js +18 -0
- package/dist/providers/gemini.d.ts +15 -0
- package/dist/providers/gemini.js +122 -5
- package/dist/providers/ollama.d.ts +15 -0
- package/dist/providers/ollama.js +111 -3
- package/dist/providers/openai.d.ts +11 -0
- package/dist/providers/openai.js +133 -0
- package/dist/registry/client.d.ts +212 -0
- package/dist/registry/client.js +359 -0
- package/dist/registry/index.d.ts +4 -0
- package/dist/registry/index.js +4 -0
- package/dist/registry/tar.d.ts +8 -0
- package/dist/registry/tar.js +353 -0
- package/dist/server/http.js +301 -45
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +1 -0
- package/dist/server/sse.d.ts +13 -0
- package/dist/server/sse.js +22 -0
- package/dist/types.d.ts +32 -1
- package/dist/types.js +4 -1
- package/dist/version.d.ts +1 -0
- package/dist/version.js +4 -0
- package/package.json +31 -7
- package/dist/modules/composition.test.d.ts +0 -11
- package/dist/modules/composition.test.js +0 -450
- package/dist/modules/policy.test.d.ts +0 -10
- package/dist/modules/policy.test.js +0 -369
- package/src/cli.ts +0 -471
- package/src/commands/add.ts +0 -315
- package/src/commands/compose.ts +0 -185
- package/src/commands/index.ts +0 -13
- package/src/commands/init.ts +0 -94
- package/src/commands/list.ts +0 -33
- package/src/commands/pipe.ts +0 -76
- package/src/commands/remove.ts +0 -57
- package/src/commands/run.ts +0 -80
- package/src/commands/update.ts +0 -130
- package/src/commands/versions.ts +0 -79
- package/src/index.ts +0 -90
- package/src/mcp/index.ts +0 -5
- package/src/mcp/server.ts +0 -403
- package/src/modules/composition.test.ts +0 -558
- package/src/modules/composition.ts +0 -1674
- package/src/modules/index.ts +0 -9
- package/src/modules/loader.ts +0 -508
- package/src/modules/policy.test.ts +0 -455
- package/src/modules/runner.ts +0 -1983
- package/src/modules/subagent.ts +0 -277
- package/src/modules/validator.ts +0 -700
- package/src/providers/anthropic.ts +0 -89
- package/src/providers/base.ts +0 -29
- package/src/providers/deepseek.ts +0 -83
- package/src/providers/gemini.ts +0 -117
- package/src/providers/index.ts +0 -78
- package/src/providers/minimax.ts +0 -81
- package/src/providers/moonshot.ts +0 -82
- package/src/providers/ollama.ts +0 -83
- package/src/providers/openai.ts +0 -84
- package/src/providers/qwen.ts +0 -82
- package/src/server/http.ts +0 -316
- package/src/server/index.ts +0 -6
- package/src/types.ts +0 -599
- package/tsconfig.json +0 -17
package/src/types.ts
DELETED
|
@@ -1,599 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cognitive Runtime - Core Types
|
|
3
|
-
* Version 2.2 - With Control/Data plane separation, tier, overflow, extensible enums
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
// =============================================================================
|
|
7
|
-
// Provider Interface
|
|
8
|
-
// =============================================================================
|
|
9
|
-
|
|
10
|
-
export interface Provider {
|
|
11
|
-
name: string;
|
|
12
|
-
invoke(params: InvokeParams): Promise<InvokeResult>;
|
|
13
|
-
isConfigured(): boolean;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface InvokeParams {
|
|
17
|
-
messages: Message[];
|
|
18
|
-
jsonSchema?: object;
|
|
19
|
-
temperature?: number;
|
|
20
|
-
maxTokens?: number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface Message {
|
|
24
|
-
role: 'system' | 'user' | 'assistant';
|
|
25
|
-
content: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface InvokeResult {
|
|
29
|
-
content: string;
|
|
30
|
-
usage?: {
|
|
31
|
-
promptTokens: number;
|
|
32
|
-
completionTokens: number;
|
|
33
|
-
totalTokens: number;
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// =============================================================================
|
|
38
|
-
// v2.2 Core Types
|
|
39
|
-
// =============================================================================
|
|
40
|
-
|
|
41
|
-
/** Module classification for schema strictness */
|
|
42
|
-
export type ModuleTier = 'exec' | 'decision' | 'exploration';
|
|
43
|
-
|
|
44
|
-
/** Schema validation strictness level */
|
|
45
|
-
export type SchemaStrictness = 'high' | 'medium' | 'low';
|
|
46
|
-
|
|
47
|
-
/** Risk level (used in both meta and changes) */
|
|
48
|
-
export type RiskLevel = 'none' | 'low' | 'medium' | 'high';
|
|
49
|
-
|
|
50
|
-
/** Enum extension strategy */
|
|
51
|
-
export type EnumStrategy = 'strict' | 'extensible';
|
|
52
|
-
|
|
53
|
-
/** Risk aggregation rule */
|
|
54
|
-
export type RiskRule = 'max_changes_risk' | 'max_issues_risk' | 'explicit';
|
|
55
|
-
|
|
56
|
-
// =============================================================================
|
|
57
|
-
// Composition Types (v2.2)
|
|
58
|
-
// =============================================================================
|
|
59
|
-
|
|
60
|
-
/** Composition pattern types */
|
|
61
|
-
export type CompositionPattern = 'sequential' | 'parallel' | 'conditional' | 'iterative';
|
|
62
|
-
|
|
63
|
-
/** Aggregation strategy for combining multiple outputs */
|
|
64
|
-
export type AggregationStrategy = 'merge' | 'array' | 'first' | 'custom';
|
|
65
|
-
|
|
66
|
-
/** Semver-like version matching pattern */
|
|
67
|
-
export type VersionPattern = string; // e.g., ">=1.0.0", "^1.0.0", "~1.0.0", "*"
|
|
68
|
-
|
|
69
|
-
/** Dependency declaration for composition.requires */
|
|
70
|
-
export interface DependencyDeclaration {
|
|
71
|
-
/** Module name */
|
|
72
|
-
name: string;
|
|
73
|
-
/** Semver version pattern */
|
|
74
|
-
version?: VersionPattern;
|
|
75
|
-
/** Whether dependency is optional */
|
|
76
|
-
optional?: boolean;
|
|
77
|
-
/** Fallback module if unavailable */
|
|
78
|
-
fallback?: string | null;
|
|
79
|
-
/** Per-module timeout (ms) */
|
|
80
|
-
timeout_ms?: number;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Dataflow mapping expression */
|
|
84
|
-
export interface DataflowMapping {
|
|
85
|
-
[key: string]: string; // target_field: "$.source.path"
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** Dataflow step configuration */
|
|
89
|
-
export interface DataflowStep {
|
|
90
|
-
/** Source of data: 'input' or 'module-name.output' */
|
|
91
|
-
from: string | string[];
|
|
92
|
-
/** Destination: module name or 'output' */
|
|
93
|
-
to: string | string[];
|
|
94
|
-
/** Field mapping expressions */
|
|
95
|
-
mapping?: DataflowMapping;
|
|
96
|
-
/** Condition for execution */
|
|
97
|
-
condition?: string;
|
|
98
|
-
/** Aggregation strategy when from is an array */
|
|
99
|
-
aggregate?: AggregationStrategy;
|
|
100
|
-
/** Custom aggregation function name */
|
|
101
|
-
aggregator?: string;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** Conditional routing rule */
|
|
105
|
-
export interface RoutingRule {
|
|
106
|
-
/** Condition expression */
|
|
107
|
-
condition: string;
|
|
108
|
-
/** Next module to execute (null means use current result) */
|
|
109
|
-
next: string | null;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/** Iteration configuration */
|
|
113
|
-
export interface IterationConfig {
|
|
114
|
-
/** Maximum iterations */
|
|
115
|
-
max_iterations?: number;
|
|
116
|
-
/** Condition to continue iterating */
|
|
117
|
-
continue_condition?: string;
|
|
118
|
-
/** Condition to stop iterating */
|
|
119
|
-
stop_condition?: string;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/** Full composition configuration (from module.yaml) */
|
|
123
|
-
export interface CompositionConfig {
|
|
124
|
-
/** Composition pattern */
|
|
125
|
-
pattern: CompositionPattern;
|
|
126
|
-
/** Required dependencies */
|
|
127
|
-
requires?: DependencyDeclaration[];
|
|
128
|
-
/** Dataflow configuration */
|
|
129
|
-
dataflow?: DataflowStep[];
|
|
130
|
-
/** Conditional routing rules */
|
|
131
|
-
routing?: RoutingRule[];
|
|
132
|
-
/** Maximum composition depth */
|
|
133
|
-
max_depth?: number;
|
|
134
|
-
/** Total timeout for composition (ms) */
|
|
135
|
-
timeout_ms?: number;
|
|
136
|
-
/** Iteration configuration */
|
|
137
|
-
iteration?: IterationConfig;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// =============================================================================
|
|
141
|
-
// Module Configuration (v2.2)
|
|
142
|
-
// =============================================================================
|
|
143
|
-
|
|
144
|
-
export interface CognitiveModule {
|
|
145
|
-
// Core identity
|
|
146
|
-
name: string;
|
|
147
|
-
version: string;
|
|
148
|
-
responsibility: string;
|
|
149
|
-
|
|
150
|
-
// Constraints
|
|
151
|
-
excludes: string[];
|
|
152
|
-
constraints?: ModuleConstraints;
|
|
153
|
-
|
|
154
|
-
// Unified policies (v2.1+)
|
|
155
|
-
policies?: ModulePolicies;
|
|
156
|
-
|
|
157
|
-
// Tools policy
|
|
158
|
-
tools?: ToolsPolicy;
|
|
159
|
-
|
|
160
|
-
// Output contract
|
|
161
|
-
output?: OutputContract;
|
|
162
|
-
|
|
163
|
-
// Failure contract
|
|
164
|
-
failure?: FailureContract;
|
|
165
|
-
|
|
166
|
-
// Runtime requirements
|
|
167
|
-
runtimeRequirements?: RuntimeRequirements;
|
|
168
|
-
|
|
169
|
-
// v2.2: Module tier
|
|
170
|
-
tier?: ModuleTier;
|
|
171
|
-
|
|
172
|
-
// v2.2: Schema strictness
|
|
173
|
-
schemaStrictness?: SchemaStrictness;
|
|
174
|
-
|
|
175
|
-
// v2.2: Overflow configuration
|
|
176
|
-
overflow?: OverflowConfig;
|
|
177
|
-
|
|
178
|
-
// v2.2: Enum configuration
|
|
179
|
-
enums?: EnumConfig;
|
|
180
|
-
|
|
181
|
-
// v2.2: Compatibility configuration
|
|
182
|
-
compat?: CompatConfig;
|
|
183
|
-
|
|
184
|
-
// v2.2: Meta configuration (including risk_rule)
|
|
185
|
-
metaConfig?: MetaConfig;
|
|
186
|
-
|
|
187
|
-
// v2.2: Composition configuration
|
|
188
|
-
composition?: CompositionConfig;
|
|
189
|
-
|
|
190
|
-
// Execution context
|
|
191
|
-
context?: 'fork' | 'main';
|
|
192
|
-
|
|
193
|
-
// Prompt (from prompt.md or MODULE.md body)
|
|
194
|
-
prompt: string;
|
|
195
|
-
|
|
196
|
-
// Schemas
|
|
197
|
-
inputSchema?: object;
|
|
198
|
-
outputSchema?: object; // v2.1 compat
|
|
199
|
-
dataSchema?: object; // v2.2: same as outputSchema
|
|
200
|
-
metaSchema?: object; // v2.2: control plane schema
|
|
201
|
-
errorSchema?: object;
|
|
202
|
-
|
|
203
|
-
// Metadata
|
|
204
|
-
location: string;
|
|
205
|
-
format: 'v0' | 'v1' | 'v2';
|
|
206
|
-
formatVersion?: string; // v2.0, v2.1, v2.2
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export interface ModuleConstraints {
|
|
210
|
-
no_network?: boolean;
|
|
211
|
-
no_side_effects?: boolean;
|
|
212
|
-
no_file_write?: boolean;
|
|
213
|
-
no_inventing_data?: boolean;
|
|
214
|
-
behavior_equivalence_false_max_confidence?: number;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
export interface ModulePolicies {
|
|
218
|
-
network?: 'allow' | 'deny';
|
|
219
|
-
filesystem_write?: 'allow' | 'deny';
|
|
220
|
-
side_effects?: 'allow' | 'deny';
|
|
221
|
-
code_execution?: 'allow' | 'deny';
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
export interface ToolsPolicy {
|
|
225
|
-
policy?: 'allow_by_default' | 'deny_by_default';
|
|
226
|
-
allowed: string[];
|
|
227
|
-
denied?: string[];
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
export interface OutputContract {
|
|
231
|
-
format?: 'json_strict' | 'json_lenient' | 'text';
|
|
232
|
-
envelope?: boolean;
|
|
233
|
-
require?: string[];
|
|
234
|
-
require_confidence?: boolean;
|
|
235
|
-
require_rationale?: boolean;
|
|
236
|
-
require_behavior_equivalence?: boolean;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
export interface FailureContract {
|
|
240
|
-
contract?: 'error_union' | 'throw';
|
|
241
|
-
partial_allowed?: boolean;
|
|
242
|
-
must_return_error_schema?: boolean;
|
|
243
|
-
schema?: object;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
export interface RuntimeRequirements {
|
|
247
|
-
structured_output?: boolean;
|
|
248
|
-
max_input_tokens?: number;
|
|
249
|
-
preferred_capabilities?: string[];
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// =============================================================================
|
|
253
|
-
// v2.2 New Configuration Types
|
|
254
|
-
// =============================================================================
|
|
255
|
-
|
|
256
|
-
/** Overflow configuration for extensions.insights */
|
|
257
|
-
export interface OverflowConfig {
|
|
258
|
-
enabled: boolean;
|
|
259
|
-
recoverable?: boolean;
|
|
260
|
-
max_items?: number;
|
|
261
|
-
require_suggested_mapping?: boolean;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/** Enum extension configuration */
|
|
265
|
-
export interface EnumConfig {
|
|
266
|
-
strategy: EnumStrategy;
|
|
267
|
-
unknown_tag?: string; // How to represent unknown enums (default: "custom")
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/** Compatibility configuration for migration */
|
|
271
|
-
export interface CompatConfig {
|
|
272
|
-
accepts_v21_payload?: boolean;
|
|
273
|
-
runtime_auto_wrap?: boolean;
|
|
274
|
-
schema_output_alias?: 'data' | 'output';
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/** Meta field configuration (v2.2) */
|
|
278
|
-
export interface MetaConfig {
|
|
279
|
-
required?: string[];
|
|
280
|
-
risk_rule?: RiskRule;
|
|
281
|
-
confidence?: { min?: number; max?: number };
|
|
282
|
-
explain?: { max_chars?: number };
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// =============================================================================
|
|
286
|
-
// Envelope Types (v2.2)
|
|
287
|
-
// =============================================================================
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Control plane metadata - unified across all modules.
|
|
291
|
-
* Used for routing, logging, UI cards, and middleware decisions.
|
|
292
|
-
*/
|
|
293
|
-
export interface EnvelopeMeta {
|
|
294
|
-
/** Confidence score [0, 1] - unified across all modules */
|
|
295
|
-
confidence: number;
|
|
296
|
-
|
|
297
|
-
/** Aggregated risk level: max(changes[*].risk) */
|
|
298
|
-
risk: RiskLevel;
|
|
299
|
-
|
|
300
|
-
/** Short explanation for middleware/UI (max 280 chars) */
|
|
301
|
-
explain: string;
|
|
302
|
-
|
|
303
|
-
/** Distributed tracing ID */
|
|
304
|
-
trace_id?: string;
|
|
305
|
-
|
|
306
|
-
/** Provider and model identifier */
|
|
307
|
-
model?: string;
|
|
308
|
-
|
|
309
|
-
/** Execution latency in milliseconds */
|
|
310
|
-
latency_ms?: number;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* Enhanced error structure with retry and recovery info (v2.2.1).
|
|
315
|
-
*/
|
|
316
|
-
export interface EnvelopeError {
|
|
317
|
-
/** Error code (e.g., "INVALID_INPUT", "PARSE_ERROR") */
|
|
318
|
-
code: string;
|
|
319
|
-
|
|
320
|
-
/** Human-readable error message */
|
|
321
|
-
message: string;
|
|
322
|
-
|
|
323
|
-
/** Whether the error can be retried */
|
|
324
|
-
recoverable?: boolean;
|
|
325
|
-
|
|
326
|
-
/** Suggested wait time before retry (in milliseconds) */
|
|
327
|
-
retry_after_ms?: number;
|
|
328
|
-
|
|
329
|
-
/** Additional error context */
|
|
330
|
-
details?: Record<string, unknown>;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/** Success response in v2.2 envelope format */
|
|
334
|
-
export interface EnvelopeSuccessV22<T = unknown> {
|
|
335
|
-
ok: true;
|
|
336
|
-
version?: string; // Envelope version (e.g., "2.2")
|
|
337
|
-
meta: EnvelopeMeta;
|
|
338
|
-
data: T;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/** Error response in v2.2 envelope format */
|
|
342
|
-
export interface EnvelopeErrorV22 {
|
|
343
|
-
ok: false;
|
|
344
|
-
version?: string; // Envelope version (e.g., "2.2")
|
|
345
|
-
meta: EnvelopeMeta;
|
|
346
|
-
error: EnvelopeError;
|
|
347
|
-
partial_data?: unknown;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
/** v2.2 envelope response (union type) */
|
|
351
|
-
export type EnvelopeResponseV22<T = unknown> = EnvelopeSuccessV22<T> | EnvelopeErrorV22;
|
|
352
|
-
|
|
353
|
-
// =============================================================================
|
|
354
|
-
// Legacy Envelope Types (v2.1 - for backward compatibility)
|
|
355
|
-
// =============================================================================
|
|
356
|
-
|
|
357
|
-
export interface EnvelopeSuccessV21<T = unknown> {
|
|
358
|
-
ok: true;
|
|
359
|
-
data: T;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
export interface EnvelopeErrorV21 {
|
|
363
|
-
ok: false;
|
|
364
|
-
error: {
|
|
365
|
-
code: string;
|
|
366
|
-
message: string;
|
|
367
|
-
};
|
|
368
|
-
partial_data?: unknown;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
export type EnvelopeResponseV21<T = unknown> = EnvelopeSuccessV21<T> | EnvelopeErrorV21;
|
|
372
|
-
|
|
373
|
-
/** Generic envelope response (supports both v2.1 and v2.2) */
|
|
374
|
-
export type EnvelopeResponse<T = unknown> = EnvelopeResponseV22<T> | EnvelopeResponseV21<T>;
|
|
375
|
-
|
|
376
|
-
// =============================================================================
|
|
377
|
-
// Overflow Types (v2.2)
|
|
378
|
-
// =============================================================================
|
|
379
|
-
|
|
380
|
-
/** An insight that doesn't fit the schema but is valuable */
|
|
381
|
-
export interface Insight {
|
|
382
|
-
/** The observation or insight */
|
|
383
|
-
text: string;
|
|
384
|
-
|
|
385
|
-
/** Suggested field/enum to add to schema for future versions */
|
|
386
|
-
suggested_mapping: string;
|
|
387
|
-
|
|
388
|
-
/** Supporting evidence for this insight */
|
|
389
|
-
evidence?: string;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/** Extensions container for overflow data */
|
|
393
|
-
export interface Extensions {
|
|
394
|
-
insights?: Insight[];
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
// =============================================================================
|
|
398
|
-
// Extensible Enum Pattern (v2.2)
|
|
399
|
-
// =============================================================================
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* Extensible enum type - allows both predefined values and custom extensions.
|
|
403
|
-
*
|
|
404
|
-
* Usage:
|
|
405
|
-
* type ChangeType = ExtensibleEnum<'remove_redundancy' | 'simplify_logic' | 'other'>;
|
|
406
|
-
*
|
|
407
|
-
* Valid values:
|
|
408
|
-
* - "remove_redundancy" (predefined)
|
|
409
|
-
* - { custom: "inline_callback", reason: "Converted callback to arrow function" }
|
|
410
|
-
*/
|
|
411
|
-
export type ExtensibleEnum<T extends string> = T | { custom: string; reason: string };
|
|
412
|
-
|
|
413
|
-
// =============================================================================
|
|
414
|
-
// Module Result Types
|
|
415
|
-
// =============================================================================
|
|
416
|
-
|
|
417
|
-
/** Base interface for module result data */
|
|
418
|
-
export interface ModuleResultData {
|
|
419
|
-
[key: string]: unknown;
|
|
420
|
-
rationale: string;
|
|
421
|
-
extensions?: Extensions;
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
/** v2.2 module result with meta and data separation */
|
|
425
|
-
export interface ModuleResultV22 {
|
|
426
|
-
ok: boolean;
|
|
427
|
-
version?: string; // Envelope version (e.g., "2.2")
|
|
428
|
-
meta: EnvelopeMeta;
|
|
429
|
-
data?: ModuleResultData;
|
|
430
|
-
error?: EnvelopeError;
|
|
431
|
-
partial_data?: unknown;
|
|
432
|
-
raw?: string;
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
/** Legacy module result (v2.1) */
|
|
436
|
-
export interface ModuleResultV21 {
|
|
437
|
-
ok: boolean;
|
|
438
|
-
data?: ModuleResultData & { confidence: number };
|
|
439
|
-
error?: {
|
|
440
|
-
code: string;
|
|
441
|
-
message: string;
|
|
442
|
-
};
|
|
443
|
-
partial_data?: unknown;
|
|
444
|
-
raw?: string;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
/** Generic module result */
|
|
448
|
-
export type ModuleResult = ModuleResultV22 | ModuleResultV21;
|
|
449
|
-
|
|
450
|
-
// =============================================================================
|
|
451
|
-
// Legacy Types (for backward compatibility)
|
|
452
|
-
// =============================================================================
|
|
453
|
-
|
|
454
|
-
export interface LegacyModuleResult {
|
|
455
|
-
output: unknown;
|
|
456
|
-
confidence: number;
|
|
457
|
-
rationale: string;
|
|
458
|
-
behaviorEquivalence?: boolean;
|
|
459
|
-
raw?: string;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
// =============================================================================
|
|
463
|
-
// Command Types
|
|
464
|
-
// =============================================================================
|
|
465
|
-
|
|
466
|
-
export interface CommandContext {
|
|
467
|
-
cwd: string;
|
|
468
|
-
provider: Provider;
|
|
469
|
-
verbose?: boolean;
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
export interface CommandResult {
|
|
473
|
-
success: boolean;
|
|
474
|
-
data?: unknown;
|
|
475
|
-
error?: string;
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
// =============================================================================
|
|
479
|
-
// Module Input
|
|
480
|
-
// =============================================================================
|
|
481
|
-
|
|
482
|
-
export interface ModuleInput {
|
|
483
|
-
code?: string;
|
|
484
|
-
query?: string;
|
|
485
|
-
language?: string;
|
|
486
|
-
options?: Record<string, unknown>;
|
|
487
|
-
[key: string]: unknown;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
// =============================================================================
|
|
491
|
-
// Utility Types
|
|
492
|
-
// =============================================================================
|
|
493
|
-
|
|
494
|
-
/** Check if response is v2.2 format */
|
|
495
|
-
export function isV22Envelope<T>(response: EnvelopeResponse<T>): response is EnvelopeResponseV22<T> {
|
|
496
|
-
return 'meta' in response;
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
/** Check if response is successful */
|
|
500
|
-
export function isEnvelopeSuccess<T>(
|
|
501
|
-
response: EnvelopeResponse<T>
|
|
502
|
-
): response is EnvelopeSuccessV22<T> | EnvelopeSuccessV21<T> {
|
|
503
|
-
return response.ok === true;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
/** Extract meta from any envelope response */
|
|
507
|
-
export function extractMeta<T>(
|
|
508
|
-
response: EnvelopeResponse<T>,
|
|
509
|
-
riskRule: RiskRule = 'max_changes_risk'
|
|
510
|
-
): EnvelopeMeta {
|
|
511
|
-
if (isV22Envelope(response)) {
|
|
512
|
-
return response.meta;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
// Synthesize meta from v2.1 response
|
|
516
|
-
if (response.ok) {
|
|
517
|
-
const data = (response.data ?? {}) as Record<string, unknown>;
|
|
518
|
-
return {
|
|
519
|
-
confidence: (data.confidence as number) ?? 0.5,
|
|
520
|
-
risk: aggregateRisk(data, riskRule),
|
|
521
|
-
explain: ((data.rationale as string) ?? '').slice(0, 280) || 'No explanation',
|
|
522
|
-
};
|
|
523
|
-
} else {
|
|
524
|
-
return {
|
|
525
|
-
confidence: 0,
|
|
526
|
-
risk: 'high',
|
|
527
|
-
explain: response.error?.message?.slice(0, 280) ?? 'Error occurred',
|
|
528
|
-
};
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
/** Aggregate risk from list of items */
|
|
533
|
-
function aggregateRiskFromList(items: Array<{ risk?: RiskLevel }>): RiskLevel {
|
|
534
|
-
const riskLevels: Record<RiskLevel, number> = { none: 0, low: 1, medium: 2, high: 3 };
|
|
535
|
-
const riskNames: RiskLevel[] = ['none', 'low', 'medium', 'high'];
|
|
536
|
-
|
|
537
|
-
if (!items || items.length === 0) {
|
|
538
|
-
return 'medium';
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
let maxLevel = 0;
|
|
542
|
-
for (const item of items) {
|
|
543
|
-
const level = riskLevels[item.risk ?? 'medium'];
|
|
544
|
-
maxLevel = Math.max(maxLevel, level);
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
return riskNames[maxLevel];
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
/**
|
|
551
|
-
* Aggregate risk based on configured rule.
|
|
552
|
-
*
|
|
553
|
-
* Rules:
|
|
554
|
-
* - max_changes_risk: max(data.changes[*].risk) - default
|
|
555
|
-
* - max_issues_risk: max(data.issues[*].risk) - for review modules
|
|
556
|
-
* - explicit: return "medium", module should set risk explicitly
|
|
557
|
-
*/
|
|
558
|
-
export function aggregateRisk(
|
|
559
|
-
data: Record<string, unknown>,
|
|
560
|
-
riskRule: RiskRule = 'max_changes_risk'
|
|
561
|
-
): RiskLevel {
|
|
562
|
-
if (riskRule === 'max_changes_risk') {
|
|
563
|
-
const changes = (data.changes as Array<{ risk?: RiskLevel }>) ?? [];
|
|
564
|
-
return aggregateRiskFromList(changes);
|
|
565
|
-
} else if (riskRule === 'max_issues_risk') {
|
|
566
|
-
const issues = (data.issues as Array<{ risk?: RiskLevel }>) ?? [];
|
|
567
|
-
return aggregateRiskFromList(issues);
|
|
568
|
-
} else if (riskRule === 'explicit') {
|
|
569
|
-
return 'medium'; // Module should override
|
|
570
|
-
}
|
|
571
|
-
// Fallback to changes
|
|
572
|
-
const changes = (data.changes as Array<{ risk?: RiskLevel }>) ?? [];
|
|
573
|
-
return aggregateRiskFromList(changes);
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
/** Check if result should be escalated to human review */
|
|
577
|
-
export function shouldEscalate<T>(
|
|
578
|
-
response: EnvelopeResponse<T>,
|
|
579
|
-
confidenceThreshold: number = 0.7
|
|
580
|
-
): boolean {
|
|
581
|
-
const meta = extractMeta(response);
|
|
582
|
-
|
|
583
|
-
// Escalate if low confidence
|
|
584
|
-
if (meta.confidence < confidenceThreshold) {
|
|
585
|
-
return true;
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
// Escalate if high risk
|
|
589
|
-
if (meta.risk === 'high') {
|
|
590
|
-
return true;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
// Escalate if error
|
|
594
|
-
if (!response.ok) {
|
|
595
|
-
return true;
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
return false;
|
|
599
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2023",
|
|
4
|
-
"lib": ["ES2023"],
|
|
5
|
-
"module": "NodeNext",
|
|
6
|
-
"moduleResolution": "NodeNext",
|
|
7
|
-
"outDir": "./dist",
|
|
8
|
-
"rootDir": "./src",
|
|
9
|
-
"strict": true,
|
|
10
|
-
"esModuleInterop": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"declaration": true,
|
|
13
|
-
"resolveJsonModule": true
|
|
14
|
-
},
|
|
15
|
-
"include": ["src/**/*"],
|
|
16
|
-
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
|
17
|
-
}
|