callx402 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +227 -2
- package/SPEC.md +264 -0
- package/bin/callx402.js +257 -0
- package/core/config.js +104 -0
- package/core/envelope.js +80 -0
- package/core/index.js +364 -0
- package/core/pipeline.js +371 -0
- package/core/remote.js +242 -0
- package/core/subsystems.js +136 -0
- package/dist/callx402-1.0.0.tgz +0 -0
- package/docs/ACTION_LANGUAGE.md +23 -0
- package/docs/MCP_IDENTITY.md +27 -0
- package/docs/README.md +315 -0
- package/docs/action-reference.md +247 -0
- package/docs/cli-help-verified-2026-10-06.txt +39 -0
- package/docs/logo.png +0 -0
- package/docs/problem-map.md +65 -0
- package/docs/troubleshooting/cx402-402afterfail-001-402-after-failed-settle.md +143 -0
- package/docs/troubleshooting/cx402-agentcap-001-agent-spending.md +114 -0
- package/docs/troubleshooting/cx402-eip3009-001-authorization-checklist.md +124 -0
- package/docs/troubleshooting/cx402-paidnores-001-paid-no-result.md +97 -0
- package/docs/troubleshooting/cx402-safretry-001-retry-after-payment.md +106 -0
- package/docs/troubleshooting/cx402-settlepend-001-settlement-pending.md +92 -0
- package/docs/troubleshooting/cx402-solanafail-001-solana-settlement.md +119 -0
- package/docs/troubleshooting/cx402-verset-001-verify-ok-settle-fails.md +101 -0
- package/docs/visuals/01-action-map.png +0 -0
- package/docs/visuals/02-diagnose.png +0 -0
- package/docs/visuals/03-resolve.png +0 -0
- package/docs/visuals/04-route.png +0 -0
- package/docs/visuals/05-preflight.png +0 -0
- package/docs/visuals/06-inspect.png +0 -0
- package/docs/visuals/07-status.png +0 -0
- package/docs/visuals/08-architecture.svg +49 -0
- package/docs/visuals/MANIFEST.md +40 -0
- package/docs/visuals/raw/01-help.out +39 -0
- package/docs/visuals/raw/02-diagnose.out +2 -0
- package/docs/visuals/raw/03-resolve.out +4 -0
- package/docs/visuals/raw/04-route.out +2 -0
- package/docs/visuals/raw/05-preflight.out +2 -0
- package/docs/visuals/raw/06-inspect.out +23 -0
- package/docs/visuals/raw/07-status.out +16 -0
- package/docs/visuals/render_terminal.py +149 -0
- package/docs/what-is-callx402.md +251 -0
- package/integrations/README.md +62 -0
- package/integrations/core.py +329 -0
- package/integrations/crewai/callx402_crewai.py +109 -0
- package/integrations/facilitator/settle-guard.js +99 -0
- package/integrations/langchain/callx402_tools.py +180 -0
- package/integrations/mcp-clients/README.md +121 -0
- package/integrations/n8n/callx402-incident-guard.json +1 -0
- package/mcp/README.md +63 -0
- package/mcp/index.js +239 -0
- package/package.json +39 -4
- package/scripts/drift-check.js +202 -0
- package/sdk/js/index.js +207 -0
- package/sdk/python/README.md +67 -0
- package/sdk/python/callx402/__init__.py +317 -0
- package/sdk/python/callx402/cli.py +193 -0
- package/sdk/python/pyproject.toml +30 -0
- package/sdk/python/setup.py +56 -0
- package/server/index.js +520 -0
- package/server/openapi.yaml +537 -0
- package/server.json +1 -0
- package/test/cli-dispatch.test.js +248 -0
- package/test/helpers.js +103 -0
- package/test/integrity.test.js +43 -0
- package/test/intent-pipeline.test.js +205 -0
- package/test/remote.test.js +166 -0
- package/test/safety.test.js +234 -0
- package/test/sdk.test.js +198 -0
- package/test/server.test.js +190 -0
- package/test/subsystems.test.js +118 -0
package/core/index.js
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* core/index.js — action dispatcher.
|
|
5
|
+
*
|
|
6
|
+
* runAction(name, args, { deps, config, timeoutMs }) -> Promise<{ result, exitCode }>
|
|
7
|
+
* `deps` is an internal/test seam: deps[<subsystem>] replaces the real module.
|
|
8
|
+
* getStatus({ deps, config }) -> per-subsystem reachability report.
|
|
9
|
+
* runIntent re-exported from pipeline.js.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const { EXIT, ok, fail, exitCodeFor } = require('./envelope');
|
|
13
|
+
const { loadConfig } = require('./config');
|
|
14
|
+
const { getSubsystem, getStatus: statusOf, SUBSYSTEMS } = require('./subsystems');
|
|
15
|
+
const { runIntent } = require('./pipeline');
|
|
16
|
+
const { dispatchRemote } = require('./remote');
|
|
17
|
+
|
|
18
|
+
const ACTIONS = ['diagnose', 'rescue', 'route', 'resolve', 'doctor', 'execute', 'monitor', 'status', 'preflight', 'inspect'];
|
|
19
|
+
|
|
20
|
+
/** Parse --evidence style input: JSON string or @file. */
|
|
21
|
+
function readEvidenceInput(input, fs, path) {
|
|
22
|
+
if (input === undefined || input === null || input === '') return {};
|
|
23
|
+
if (typeof input !== 'string') return input;
|
|
24
|
+
const s = input.trim();
|
|
25
|
+
if (s.startsWith('@')) {
|
|
26
|
+
const p = s.slice(1);
|
|
27
|
+
const raw = fs.readFileSync(path.resolve(p), 'utf8');
|
|
28
|
+
return JSON.parse(raw);
|
|
29
|
+
}
|
|
30
|
+
return JSON.parse(s);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function disabledResult(action, subsystem, handle) {
|
|
34
|
+
return {
|
|
35
|
+
result: fail(action, {
|
|
36
|
+
subsystem, subsystemStatus: 'disabled',
|
|
37
|
+
code: 'subsystem_disabled',
|
|
38
|
+
message: `${subsystem} subsystem is disabled. Set ${handle.flag}=1 to enable it. Nothing was executed.`,
|
|
39
|
+
disposition: `${subsystem} disabled: nothing executed.`,
|
|
40
|
+
data: { flag: handle.flag, note: handle.note },
|
|
41
|
+
}),
|
|
42
|
+
exitCode: EXIT.UNAVAILABLE,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function unreachableResult(action, subsystem, handle) {
|
|
47
|
+
return {
|
|
48
|
+
result: fail(action, {
|
|
49
|
+
subsystem, subsystemStatus: 'unreachable',
|
|
50
|
+
code: 'subsystem_unreachable',
|
|
51
|
+
message: `${subsystem} subsystem could not be loaded (${handle.error || 'unknown error'}). Nothing was executed.`,
|
|
52
|
+
disposition: `${subsystem} unreachable: nothing executed.`,
|
|
53
|
+
data: { error: handle.error },
|
|
54
|
+
}),
|
|
55
|
+
exitCode: EXIT.UNAVAILABLE,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function requireSubsystem(action, subsystem, { config, deps }) {
|
|
60
|
+
const h = getSubsystem(subsystem, { config, deps });
|
|
61
|
+
if (!h.reachable) return { ...unreachableResult(action, subsystem, h), handle: null };
|
|
62
|
+
if (!h.enabled) return { ...disabledResult(action, subsystem, h), handle: null };
|
|
63
|
+
return { result: null, exitCode: null, handle: h };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function actDiagnose(args, ctx) {
|
|
67
|
+
const g = requireSubsystem('diagnose', 'doctor', ctx);
|
|
68
|
+
if (!g.handle) return g;
|
|
69
|
+
try {
|
|
70
|
+
const evidence = readEvidenceInput(args.evidence ?? args.target, require('fs'), require('path'));
|
|
71
|
+
const report = g.handle.module.runMcpDoctor(evidence);
|
|
72
|
+
const whatFailed = report.whatFailed || 'unknown';
|
|
73
|
+
return {
|
|
74
|
+
result: ok('diagnose', { subsystem: 'doctor', disposition: `Diagnosis complete: ${whatFailed}`, data: { report } }),
|
|
75
|
+
exitCode: EXIT.OK,
|
|
76
|
+
};
|
|
77
|
+
} catch (err) {
|
|
78
|
+
const isMalformed = err instanceof SyntaxError;
|
|
79
|
+
return { result: fail('diagnose', { subsystem: 'doctor', code: isMalformed ? 'usage' : 'failed', message: isMalformed ? `malformed evidence JSON: ${err.message}` : err.message }), exitCode: isMalformed ? EXIT.USAGE : EXIT.FAIL };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function actRescue(args, ctx) {
|
|
84
|
+
const g = requireSubsystem('rescue', 'rescue', ctx);
|
|
85
|
+
if (!g.handle) return g;
|
|
86
|
+
const config = ctx.config;
|
|
87
|
+
const authed = args.auth === true || args.auth === '1' || !!config.authToken || !!process.env.CALLX402_AUTH_TOKEN || config.rescueAuth === true;
|
|
88
|
+
if (!authed) {
|
|
89
|
+
return {
|
|
90
|
+
result: fail('rescue', {
|
|
91
|
+
subsystem: 'rescue', subsystemStatus: g.handle.subsystemStatus,
|
|
92
|
+
code: 'auth_required',
|
|
93
|
+
message: 'Rescue requires explicit authorization: set CALLX402_AUTH_TOKEN, config authToken, or pass --auth. Detection/quote tiers are free and read-only; execution never happens without auth.',
|
|
94
|
+
disposition: 'Rescue refused: no authorization provided.',
|
|
95
|
+
data: { incident: args.incident || null, flag: g.handle.flag },
|
|
96
|
+
}),
|
|
97
|
+
exitCode: EXIT.UNAVAILABLE,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
const rescue = g.handle.module.createRescue({});
|
|
102
|
+
const incidentId = args.incident || null;
|
|
103
|
+
const context = { incidentId, ...(args.context || {}) };
|
|
104
|
+
const incidents = rescue.detect(context);
|
|
105
|
+
const incident = Array.isArray(incidents) && incidents.length > 0
|
|
106
|
+
? incidents.find((i) => !incidentId || i.incidentId === incidentId || i.type === incidentId) || incidents[0]
|
|
107
|
+
: null;
|
|
108
|
+
let quote = null;
|
|
109
|
+
let offer = null;
|
|
110
|
+
if (incident) {
|
|
111
|
+
quote = await rescue.quoteRescue(incident, { network: args.network || config.defaultNetwork });
|
|
112
|
+
offer = rescue.freeVsPaid(incident, quote);
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
result: ok('rescue', {
|
|
116
|
+
subsystem: 'rescue',
|
|
117
|
+
disposition: incident
|
|
118
|
+
? `Rescue triage (free, read-only): ${incident.type}. Paid execution was NOT performed; quote requires explicit paid engagement.`
|
|
119
|
+
: 'Rescue detection ran (free, read-only): no incident matched the supplied context.',
|
|
120
|
+
data: { incident, quote, offer, version: rescue.version },
|
|
121
|
+
}),
|
|
122
|
+
exitCode: EXIT.OK,
|
|
123
|
+
};
|
|
124
|
+
} catch (err) {
|
|
125
|
+
return { result: fail('rescue', { subsystem: 'rescue', code: 'failed', message: err.message }), exitCode: EXIT.FAIL };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function actRoute(args, ctx) {
|
|
130
|
+
const g = requireSubsystem('route', 'router', ctx);
|
|
131
|
+
if (!g.handle) return g;
|
|
132
|
+
try {
|
|
133
|
+
const goal = args.goal || args.intent || args.text || '';
|
|
134
|
+
if (!goal.trim()) {
|
|
135
|
+
return { result: fail('route', { subsystem: 'router', code: 'usage', message: '--goal <text> is required' }), exitCode: EXIT.USAGE };
|
|
136
|
+
}
|
|
137
|
+
const speedPolicy = ({ fast: 'FASTEST_VERIFIED', cheap: 'LOWEST_TOTAL_COST', balanced: 'BEST_VALUE' })[String(args.speed || 'balanced').toLowerCase()] || 'BEST_VALUE';
|
|
138
|
+
const m = g.handle.module;
|
|
139
|
+
// Candidates: use capability graph when it is enabled; otherwise an
|
|
140
|
+
// explicit single candidate set supplied via args.candidates.
|
|
141
|
+
const cap = getSubsystem('capabilities', ctx);
|
|
142
|
+
let candidates = [];
|
|
143
|
+
if (cap.enabled && cap.reachable) {
|
|
144
|
+
try {
|
|
145
|
+
const graph = cap.module.createGraph();
|
|
146
|
+
const found = graph.findTools(goal);
|
|
147
|
+
candidates = Array.isArray(found.candidates) ? found.candidates : [];
|
|
148
|
+
} catch (_) { /* fall through to args candidates */ }
|
|
149
|
+
}
|
|
150
|
+
if (candidates.length === 0 && Array.isArray(args.candidates) && args.candidates.length > 0) {
|
|
151
|
+
candidates = args.candidates;
|
|
152
|
+
}
|
|
153
|
+
if (candidates.length === 0) {
|
|
154
|
+
const capNote = cap.reachable && cap.enabled
|
|
155
|
+
? 'capability graph is enabled but holds no registered tools'
|
|
156
|
+
: 'capability graph is disabled';
|
|
157
|
+
return {
|
|
158
|
+
result: ok('route', {
|
|
159
|
+
subsystem: 'router',
|
|
160
|
+
disposition: `No candidate tools available: ${capNote} and no --candidates were supplied. No route selected; nothing executed.`,
|
|
161
|
+
data: { goal, candidates: 0, flag: m.FLAG },
|
|
162
|
+
}),
|
|
163
|
+
exitCode: EXIT.OK,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
// normalizeCandidate(c, opts): opts is required by the real smart-router
|
|
167
|
+
// (reads opts.latency_value_usd_per_sec); pass routing constraints too.
|
|
168
|
+
// Awaited so a slow router still respects timeoutMs (no-op for sync).
|
|
169
|
+
const normalizeOpts = { networks: args.networks || null, assets: args.assets || null };
|
|
170
|
+
const normalized = [];
|
|
171
|
+
for (const c of candidates) {
|
|
172
|
+
try { normalized.push(await m.normalizeCandidate(c, normalizeOpts)); } catch (_) { /* skip */ }
|
|
173
|
+
}
|
|
174
|
+
if (normalized.length === 0) {
|
|
175
|
+
return { result: fail('route', { subsystem: 'router', code: 'failed', message: 'candidates could not be normalized by the router' }), exitCode: EXIT.FAIL };
|
|
176
|
+
}
|
|
177
|
+
const ranked = await m.rankTools(normalized, { policy: speedPolicy, networks: args.networks || null, assets: args.assets || null });
|
|
178
|
+
const selected = await m.selectTool(ranked);
|
|
179
|
+
let pathChoice = null;
|
|
180
|
+
if (Array.isArray(args.paths) && args.paths.length > 0 && typeof m.choosePath === 'function') {
|
|
181
|
+
pathChoice = m.choosePath({ paths: args.paths, policy: speedPolicy });
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
result: ok('route', {
|
|
185
|
+
subsystem: 'router',
|
|
186
|
+
disposition: `Route selected under ${speedPolicy} policy. Selection only: nothing executed, no money moved.`,
|
|
187
|
+
data: { goal, policy: speedPolicy, ranked, selected, pathChoice },
|
|
188
|
+
}),
|
|
189
|
+
exitCode: EXIT.OK,
|
|
190
|
+
};
|
|
191
|
+
} catch (err) {
|
|
192
|
+
return { result: fail('route', { subsystem: 'router', code: 'failed', message: err.message }), exitCode: EXIT.FAIL };
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async function actResolve(args, ctx) {
|
|
197
|
+
const g = requireSubsystem('resolve', 'settlement', ctx);
|
|
198
|
+
if (!g.handle) return g;
|
|
199
|
+
try {
|
|
200
|
+
const evidence = readEvidenceInput(args.evidence, require('fs'), require('path'));
|
|
201
|
+
const r = g.handle.module.resolve(evidence);
|
|
202
|
+
const state = r.state;
|
|
203
|
+
const isUnknown = state === 'UNKNOWN';
|
|
204
|
+
const disposition = isUnknown
|
|
205
|
+
? 'Settlement UNKNOWN: no auto-retry, no repay. Resolve manually with fresh evidence; an operator override requires callx402-level explicit action outside this dispatch.'
|
|
206
|
+
: `Settlement resolved: ${state} (${r.confidence || 'n/a'} confidence). ${r.policy ? `Policy: ${r.policy.retry}.` : ''}`;
|
|
207
|
+
const result = isUnknown
|
|
208
|
+
? fail('resolve', { subsystem: 'settlement', code: 'settlement_unknown', message: 'Settlement state is UNKNOWN. Per money-safety rules this must never auto-retry or repay.', disposition, settlement: state, data: { resolution: r } })
|
|
209
|
+
: ok('resolve', { subsystem: 'settlement', disposition, settlement: state, txHash: evidence.txHash || null, data: { resolution: r } });
|
|
210
|
+
return { result, exitCode: isUnknown ? EXIT.SETTLEMENT_UNKNOWN : EXIT.OK };
|
|
211
|
+
} catch (err) {
|
|
212
|
+
const isMalformed = err instanceof SyntaxError;
|
|
213
|
+
return { result: fail('resolve', { subsystem: 'settlement', code: isMalformed ? 'usage' : 'failed', message: isMalformed ? `malformed evidence JSON: ${err.message}` : err.message }), exitCode: isMalformed ? EXIT.USAGE : EXIT.FAIL };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function actExecute(args, ctx) {
|
|
218
|
+
const intent = args.intent || args.text || '';
|
|
219
|
+
if (!intent.trim()) {
|
|
220
|
+
return { result: fail('execute', { code: 'usage', message: '--intent <text> is required' }), exitCode: EXIT.USAGE };
|
|
221
|
+
}
|
|
222
|
+
return runIntent(intent, {
|
|
223
|
+
maxBudget: args.maxBudget, deadline: args.deadline, speed: args.speed, risk: args.risk,
|
|
224
|
+
networks: args.networks, assets: args.assets, providers: args.providers,
|
|
225
|
+
approvalThreshold: args.approvalThreshold, idempotencyKey: args.idempotencyKey,
|
|
226
|
+
timeoutMs: args.timeoutMs, dryRun: args.dryRun, approve: args.approve,
|
|
227
|
+
forceRetry: args.forceRetry, retry: args.retry,
|
|
228
|
+
}, ctx);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async function actMonitor(args, ctx) {
|
|
232
|
+
const g = requireSubsystem('monitor', 'sentinel', ctx);
|
|
233
|
+
if (!g.handle) return g;
|
|
234
|
+
try {
|
|
235
|
+
const sentinel = g.handle.module.createSentinel({ ...(args.sentinelOpts || {}) });
|
|
236
|
+
const snapshot = {
|
|
237
|
+
inert: !!sentinel.inert,
|
|
238
|
+
enabled: g.handle.enabled,
|
|
239
|
+
incidentTypes: Object.keys(g.handle.module.INCIDENT_TYPES || {}),
|
|
240
|
+
note: sentinel.inert ? 'Inert sentinel: set PAYLOAD_SENTINEL=1 for a live tracker.' : 'Live sentinel tracker.',
|
|
241
|
+
};
|
|
242
|
+
if (args.watch) {
|
|
243
|
+
// Stream snapshots until interrupted. JSON lines when --json.
|
|
244
|
+
const json = !!args.json;
|
|
245
|
+
const intervalMs = Number(args.intervalMs || args.interval) > 0 ? Number(args.intervalMs || args.interval) : 5000;
|
|
246
|
+
if (!json) console.log('# monitoring sentinel snapshots every ' + intervalMs + 'ms (Ctrl+C to stop)');
|
|
247
|
+
// eslint-disable-next-line no-constant-condition
|
|
248
|
+
while (true) {
|
|
249
|
+
const line = { ...snapshot, at: new Date().toISOString() };
|
|
250
|
+
if (json) console.log(JSON.stringify(line)); else console.log(JSON.stringify(line));
|
|
251
|
+
await new Promise((r) => setTimeout(r, intervalMs));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
result: ok('monitor', { subsystem: 'sentinel', disposition: sentinel.inert ? 'Sentinel snapshot (inert): no live tracking.' : 'Sentinel snapshot.', data: { snapshot } }),
|
|
256
|
+
exitCode: EXIT.OK,
|
|
257
|
+
};
|
|
258
|
+
} catch (err) {
|
|
259
|
+
return { result: fail('monitor', { subsystem: 'sentinel', code: 'failed', message: err.message }), exitCode: EXIT.FAIL };
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async function actStatus(args, ctx) {
|
|
264
|
+
const subsystems = statusOf({ config: ctx.config, deps: ctx.deps });
|
|
265
|
+
const all = Object.values(subsystems);
|
|
266
|
+
const reachable = all.filter((s) => s.reachable).length;
|
|
267
|
+
const enabledCount = all.filter((s) => s.enabled).length;
|
|
268
|
+
return {
|
|
269
|
+
result: ok('status', {
|
|
270
|
+
subsystem: null,
|
|
271
|
+
disposition: `callx402 status: ${reachable}/${all.length} subsystems reachable, ${enabledCount}/${all.length} enabled.`,
|
|
272
|
+
data: { version: ctx.version || '0.1.0', v2Root: require('./subsystems').getV2Root(ctx.config || {}), subsystems },
|
|
273
|
+
}),
|
|
274
|
+
exitCode: EXIT.OK,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async function actPreflight(args, ctx) {
|
|
279
|
+
const g = requireSubsystem('preflight', 'preflight', ctx);
|
|
280
|
+
if (!g.handle) return g;
|
|
281
|
+
try {
|
|
282
|
+
const report = await g.handle.module.runMcpPreflight(args.context || args.ctx || {});
|
|
283
|
+
return {
|
|
284
|
+
result: ok('preflight', { subsystem: 'preflight', disposition: 'Preflight checks complete.', data: { report } }),
|
|
285
|
+
exitCode: EXIT.OK,
|
|
286
|
+
};
|
|
287
|
+
} catch (err) {
|
|
288
|
+
return { result: fail('preflight', { subsystem: 'preflight', code: 'failed', message: err.message }), exitCode: EXIT.FAIL };
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async function actInspect(args, ctx) {
|
|
293
|
+
const g = requireSubsystem('inspect', 'capabilities', ctx);
|
|
294
|
+
if (!g.handle) return g;
|
|
295
|
+
try {
|
|
296
|
+
const graph = g.handle.module.createGraph();
|
|
297
|
+
const query = args.query || args.text || '';
|
|
298
|
+
const found = query ? graph.findTools(query) : { intent: query, candidates: [], took_ms: 0 };
|
|
299
|
+
return {
|
|
300
|
+
result: ok('inspect', {
|
|
301
|
+
subsystem: 'capabilities',
|
|
302
|
+
disposition: query ? `Capability lookup for "${query}": ${(found.candidates || []).length} candidate(s).` : 'Capability graph stats.',
|
|
303
|
+
data: { query, stats: graph.stats(), candidates: found.candidates || [] },
|
|
304
|
+
}),
|
|
305
|
+
exitCode: EXIT.OK,
|
|
306
|
+
};
|
|
307
|
+
} catch (err) {
|
|
308
|
+
return { result: fail('inspect', { subsystem: 'capabilities', code: 'failed', message: err.message }), exitCode: EXIT.FAIL };
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* runAction(name, args, { deps, config, timeoutMs, version }) -> Promise<{ result, exitCode }>
|
|
314
|
+
*/
|
|
315
|
+
async function runAction(name, args = {}, opts = {}) {
|
|
316
|
+
const config = opts.config || loadConfig();
|
|
317
|
+
const ctx = { deps: opts.deps || {}, config, timeoutMs: opts.timeoutMs, version: opts.version || '0.1.0' };
|
|
318
|
+
|
|
319
|
+
// SPEC §5: remote mode dispatches over HTTP to a callx402 server. The deps
|
|
320
|
+
// seam is local-only: an explicit subsystem override means local dispatch.
|
|
321
|
+
if (config.mode === 'remote' && Object.keys(ctx.deps).length === 0) {
|
|
322
|
+
return dispatchRemote(config, name, args, { timeoutMs: opts.timeoutMs || args.timeoutMs });
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const work = (async () => {
|
|
326
|
+
switch (name) {
|
|
327
|
+
case 'diagnose': return actDiagnose(args, ctx);
|
|
328
|
+
case 'doctor': return actDiagnose(args, ctx);
|
|
329
|
+
case 'rescue': return actRescue(args, ctx);
|
|
330
|
+
case 'route': return actRoute(args, ctx);
|
|
331
|
+
case 'resolve': return actResolve(args, ctx);
|
|
332
|
+
case 'execute': return actExecute(args, ctx);
|
|
333
|
+
case 'monitor': return actMonitor(args, ctx);
|
|
334
|
+
case 'status': return actStatus(args, ctx);
|
|
335
|
+
case 'preflight': return actPreflight(args, ctx);
|
|
336
|
+
case 'inspect': return actInspect(args, ctx);
|
|
337
|
+
default:
|
|
338
|
+
return {
|
|
339
|
+
result: fail(name, {
|
|
340
|
+
code: 'unknown_action',
|
|
341
|
+
message: `unknown command '${name}'. Valid commands: ${ACTIONS.join(', ')}.`,
|
|
342
|
+
disposition: `Unknown command '${name}'.`,
|
|
343
|
+
data: { commands: ACTIONS },
|
|
344
|
+
}),
|
|
345
|
+
exitCode: EXIT.USAGE,
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
})();
|
|
349
|
+
|
|
350
|
+
const timeoutMs = Number(opts.timeoutMs || args.timeoutMs);
|
|
351
|
+
if (Number.isFinite(timeoutMs) && timeoutMs > 0) {
|
|
352
|
+
const raced = await Promise.race([
|
|
353
|
+
work,
|
|
354
|
+
new Promise((resolve) => setTimeout(() => resolve({
|
|
355
|
+
result: fail(name, { code: 'timeout', message: `action '${name}' timed out after ${timeoutMs}ms` }),
|
|
356
|
+
exitCode: EXIT.FAIL,
|
|
357
|
+
}), timeoutMs)),
|
|
358
|
+
]);
|
|
359
|
+
return raced;
|
|
360
|
+
}
|
|
361
|
+
return work;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
module.exports = { ACTIONS, runAction, runIntent, getStatus: statusOf, SUBSYSTEMS };
|