chainlesschain 0.37.10 → 0.37.11

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 (39) hide show
  1. package/README.md +10 -10
  2. package/package.json +1 -1
  3. package/src/commands/a2a.js +374 -0
  4. package/src/commands/bi.js +240 -0
  5. package/src/commands/cowork.js +317 -0
  6. package/src/commands/economy.js +375 -0
  7. package/src/commands/evolution.js +398 -0
  8. package/src/commands/hmemory.js +273 -0
  9. package/src/commands/hook.js +260 -0
  10. package/src/commands/init.js +184 -0
  11. package/src/commands/lowcode.js +320 -0
  12. package/src/commands/plugin.js +55 -2
  13. package/src/commands/sandbox.js +366 -0
  14. package/src/commands/skill.js +254 -201
  15. package/src/commands/workflow.js +359 -0
  16. package/src/commands/zkp.js +277 -0
  17. package/src/index.js +44 -0
  18. package/src/lib/a2a-protocol.js +371 -0
  19. package/src/lib/agent-coordinator.js +273 -0
  20. package/src/lib/agent-economy.js +369 -0
  21. package/src/lib/app-builder.js +377 -0
  22. package/src/lib/bi-engine.js +299 -0
  23. package/src/lib/cowork/ab-comparator-cli.js +180 -0
  24. package/src/lib/cowork/code-knowledge-graph-cli.js +232 -0
  25. package/src/lib/cowork/debate-review-cli.js +144 -0
  26. package/src/lib/cowork/decision-kb-cli.js +153 -0
  27. package/src/lib/cowork/project-style-analyzer-cli.js +168 -0
  28. package/src/lib/cowork-adapter.js +106 -0
  29. package/src/lib/evolution-system.js +508 -0
  30. package/src/lib/hierarchical-memory.js +471 -0
  31. package/src/lib/hook-manager.js +387 -0
  32. package/src/lib/plugin-manager.js +118 -0
  33. package/src/lib/project-detector.js +53 -0
  34. package/src/lib/sandbox-v2.js +503 -0
  35. package/src/lib/service-container.js +183 -0
  36. package/src/lib/skill-loader.js +274 -0
  37. package/src/lib/workflow-engine.js +503 -0
  38. package/src/lib/zkp-engine.js +241 -0
  39. package/src/repl/agent-repl.js +117 -112
@@ -0,0 +1,317 @@
1
+ /**
2
+ * Multi-agent collaboration commands
3
+ * chainlesschain cowork debate|compare|analyze|status
4
+ */
5
+
6
+ import chalk from "chalk";
7
+ import ora from "ora";
8
+ import fs from "fs";
9
+ import path from "path";
10
+ import { logger } from "../lib/logger.js";
11
+
12
+ export function registerCoworkCommand(program) {
13
+ const cowork = program
14
+ .command("cowork")
15
+ .description(
16
+ "Multi-agent collaboration (debate review, A/B comparison, analysis)",
17
+ );
18
+
19
+ // cowork debate — multi-perspective code review
20
+ cowork
21
+ .command("debate")
22
+ .description("Multi-agent debate review of a file or topic")
23
+ .argument("<file-or-topic>", "File path to review, or a topic/question")
24
+ .option(
25
+ "--perspectives <list>",
26
+ "Comma-separated perspectives (performance,security,maintainability,correctness,architecture)",
27
+ "performance,security,maintainability",
28
+ )
29
+ .option("--provider <name>", "LLM provider to use")
30
+ .option("--model <name>", "LLM model to use")
31
+ .option("--json", "Output as JSON")
32
+ .action(async (target, options) => {
33
+ const { startDebate, DEFAULT_PERSPECTIVES } =
34
+ await import("../lib/cowork/debate-review-cli.js");
35
+
36
+ const perspectives = options.perspectives
37
+ .split(",")
38
+ .map((p) => p.trim())
39
+ .filter(Boolean);
40
+
41
+ // Read file content if target is a file path
42
+ let code = target;
43
+ let targetLabel = target;
44
+ const resolved = path.resolve(target);
45
+ if (fs.existsSync(resolved)) {
46
+ try {
47
+ code = fs.readFileSync(resolved, "utf-8");
48
+ targetLabel = resolved;
49
+ if (code.length > 15000) {
50
+ code = code.substring(0, 15000) + "\n... (truncated)";
51
+ }
52
+ } catch (err) {
53
+ logger.error(`Cannot read file: ${err.message}`);
54
+ process.exit(1);
55
+ }
56
+ }
57
+
58
+ const spinner = ora(
59
+ `Running debate review with ${perspectives.length} perspectives...`,
60
+ ).start();
61
+
62
+ try {
63
+ const llmOptions = {};
64
+ if (options.provider) llmOptions.provider = options.provider;
65
+ if (options.model) llmOptions.model = options.model;
66
+
67
+ const result = await startDebate({
68
+ target: targetLabel,
69
+ code,
70
+ perspectives,
71
+ llmOptions,
72
+ });
73
+ spinner.stop();
74
+
75
+ if (options.json) {
76
+ console.log(JSON.stringify(result, null, 2));
77
+ return;
78
+ }
79
+
80
+ // Display results
81
+ logger.log(chalk.bold(`\nDebate Review: ${targetLabel}\n`));
82
+
83
+ for (const review of result.reviews) {
84
+ const verdictColor =
85
+ review.verdict === "APPROVE"
86
+ ? chalk.green
87
+ : review.verdict === "REJECT"
88
+ ? chalk.red
89
+ : chalk.yellow;
90
+ logger.log(
91
+ chalk.bold(` ${review.role}: `) + verdictColor(review.verdict),
92
+ );
93
+ // Show first few lines of the review
94
+ const lines = review.review.split("\n").slice(0, 8);
95
+ for (const line of lines) {
96
+ logger.log(chalk.gray(` ${line}`));
97
+ }
98
+ if (review.review.split("\n").length > 8) {
99
+ logger.log(chalk.gray(" ..."));
100
+ }
101
+ logger.log("");
102
+ }
103
+
104
+ logger.log(chalk.bold("Final Verdict"));
105
+ const verdictColor =
106
+ result.verdict === "APPROVE"
107
+ ? chalk.green
108
+ : result.verdict === "REJECT"
109
+ ? chalk.red
110
+ : chalk.yellow;
111
+ logger.log(` Verdict: ${verdictColor(result.verdict)}`);
112
+ logger.log(` Consensus: ${result.consensusScore}%`);
113
+ logger.log("");
114
+
115
+ // Show moderator summary
116
+ const summaryLines = result.summary.split("\n").slice(0, 15);
117
+ for (const line of summaryLines) {
118
+ logger.log(chalk.gray(` ${line}`));
119
+ }
120
+ logger.log("");
121
+ } catch (err) {
122
+ spinner.fail(`Debate review failed: ${err.message}`);
123
+ if (program.opts().verbose) console.error(err.stack);
124
+ process.exit(1);
125
+ }
126
+ });
127
+
128
+ // cowork compare — A/B solution comparison
129
+ cowork
130
+ .command("compare")
131
+ .description("Generate and compare multiple solution variants")
132
+ .argument("<prompt>", "Task or problem description")
133
+ .option("--variants <n>", "Number of variants to generate (max 4)", "3")
134
+ .option(
135
+ "--criteria <list>",
136
+ "Comma-separated evaluation criteria",
137
+ "quality,performance,readability",
138
+ )
139
+ .option("--provider <name>", "LLM provider to use")
140
+ .option("--model <name>", "LLM model to use")
141
+ .option("--json", "Output as JSON")
142
+ .action(async (prompt, options) => {
143
+ const { compare } = await import("../lib/cowork/ab-comparator-cli.js");
144
+
145
+ const variants = parseInt(options.variants, 10) || 3;
146
+ const criteria = options.criteria
147
+ .split(",")
148
+ .map((c) => c.trim())
149
+ .filter(Boolean);
150
+
151
+ const spinner = ora(
152
+ `Generating ${variants} solution variants...`,
153
+ ).start();
154
+
155
+ try {
156
+ const llmOptions = {};
157
+ if (options.provider) llmOptions.provider = options.provider;
158
+ if (options.model) llmOptions.model = options.model;
159
+
160
+ const result = await compare({
161
+ prompt,
162
+ variants,
163
+ criteria,
164
+ llmOptions,
165
+ });
166
+ spinner.stop();
167
+
168
+ if (options.json) {
169
+ console.log(JSON.stringify(result, null, 2));
170
+ return;
171
+ }
172
+
173
+ logger.log(chalk.bold(`\nA/B Comparison: ${prompt}\n`));
174
+ logger.log(chalk.gray(`Criteria: ${criteria.join(", ")}\n`));
175
+
176
+ for (const v of result.variants) {
177
+ logger.log(
178
+ chalk.bold(` Variant: ${chalk.cyan(v.name)}`) +
179
+ chalk.gray(` (total: ${v.totalScore})`),
180
+ );
181
+
182
+ // Show scores
183
+ if (v.scores && Object.keys(v.scores).length > 0) {
184
+ const scoreStr = Object.entries(v.scores)
185
+ .map(([k, val]) => `${k}=${val}`)
186
+ .join(", ");
187
+ logger.log(chalk.gray(` Scores: ${scoreStr}`));
188
+ }
189
+
190
+ // Show first lines of solution
191
+ const lines = v.solution.split("\n").slice(0, 6);
192
+ for (const line of lines) {
193
+ logger.log(chalk.gray(` ${line}`));
194
+ }
195
+ if (v.solution.split("\n").length > 6) {
196
+ logger.log(chalk.gray(" ..."));
197
+ }
198
+ logger.log("");
199
+ }
200
+
201
+ logger.log(chalk.bold("Result"));
202
+ logger.log(` Winner: ${chalk.green(result.winner)}`);
203
+ logger.log(` Reason: ${chalk.gray(result.reason)}`);
204
+ if (result.ranking.length > 0) {
205
+ logger.log(
206
+ ` Ranking: ${result.ranking.map((r, i) => (i === 0 ? chalk.green(r) : chalk.gray(r))).join(" > ")}`,
207
+ );
208
+ }
209
+ logger.log("");
210
+ } catch (err) {
211
+ spinner.fail(`Comparison failed: ${err.message}`);
212
+ if (program.opts().verbose) console.error(err.stack);
213
+ process.exit(1);
214
+ }
215
+ });
216
+
217
+ // cowork analyze — code analysis (Phase 4B placeholder, delegates to knowledge modules)
218
+ cowork
219
+ .command("analyze")
220
+ .description("Analyze code structure, style, or knowledge graph")
221
+ .argument("<path>", "File or directory to analyze")
222
+ .option(
223
+ "--type <type>",
224
+ "Analysis type (style, knowledge-graph, decisions)",
225
+ "style",
226
+ )
227
+ .option("--provider <name>", "LLM provider to use")
228
+ .option("--model <name>", "LLM model to use")
229
+ .option("--json", "Output as JSON")
230
+ .action(async (targetPath, options) => {
231
+ const resolved = path.resolve(targetPath);
232
+ if (!fs.existsSync(resolved)) {
233
+ logger.error(`Path not found: ${resolved}`);
234
+ process.exit(1);
235
+ }
236
+
237
+ const spinner = ora(`Analyzing (${options.type})...`).start();
238
+
239
+ try {
240
+ let result;
241
+
242
+ if (options.type === "style") {
243
+ const { analyzeProjectStyle } =
244
+ await import("../lib/cowork/project-style-analyzer-cli.js");
245
+ const llmOptions = {};
246
+ if (options.provider) llmOptions.provider = options.provider;
247
+ if (options.model) llmOptions.model = options.model;
248
+ result = await analyzeProjectStyle({
249
+ targetPath: resolved,
250
+ llmOptions,
251
+ });
252
+ } else if (options.type === "knowledge-graph") {
253
+ const { buildKnowledgeGraph } =
254
+ await import("../lib/cowork/code-knowledge-graph-cli.js");
255
+ result = await buildKnowledgeGraph({ targetPath: resolved });
256
+ } else if (options.type === "decisions") {
257
+ const { extractDecisions } =
258
+ await import("../lib/cowork/decision-kb-cli.js");
259
+ const llmOptions = {};
260
+ if (options.provider) llmOptions.provider = options.provider;
261
+ if (options.model) llmOptions.model = options.model;
262
+ result = await extractDecisions({
263
+ targetPath: resolved,
264
+ llmOptions,
265
+ });
266
+ } else {
267
+ spinner.fail(`Unknown analysis type: ${options.type}`);
268
+ process.exit(1);
269
+ }
270
+
271
+ spinner.stop();
272
+
273
+ if (options.json) {
274
+ console.log(JSON.stringify(result, null, 2));
275
+ } else {
276
+ logger.log(
277
+ chalk.bold(`\nAnalysis (${options.type}): ${targetPath}\n`),
278
+ );
279
+ if (result.summary) {
280
+ logger.log(result.summary);
281
+ } else {
282
+ console.log(JSON.stringify(result, null, 2));
283
+ }
284
+ logger.log("");
285
+ }
286
+ } catch (err) {
287
+ spinner.fail(`Analysis failed: ${err.message}`);
288
+ if (program.opts().verbose) console.error(err.stack);
289
+ process.exit(1);
290
+ }
291
+ });
292
+
293
+ // cowork status — show collaboration state
294
+ cowork
295
+ .command("status")
296
+ .description("Show cowork collaboration status")
297
+ .action(async () => {
298
+ logger.log(chalk.bold("\nCowork Status\n"));
299
+ logger.log(` Available commands:`);
300
+ logger.log(
301
+ ` ${chalk.cyan("cowork debate <file>")} Multi-perspective code review`,
302
+ );
303
+ logger.log(
304
+ ` ${chalk.cyan("cowork compare <prompt>")} A/B solution comparison`,
305
+ );
306
+ logger.log(
307
+ ` ${chalk.cyan("cowork analyze <path>")} Code analysis (style/knowledge-graph/decisions)`,
308
+ );
309
+ logger.log("");
310
+ logger.log(
311
+ chalk.gray(
312
+ " All commands use the configured LLM provider. Override with --provider/--model.",
313
+ ),
314
+ );
315
+ logger.log("");
316
+ });
317
+ }
@@ -0,0 +1,375 @@
1
+ /**
2
+ * Economy commands
3
+ * chainlesschain economy price|pay|balance|channel|market|trade|nft|revenue|contribute
4
+ */
5
+
6
+ import chalk from "chalk";
7
+ import { logger } from "../lib/logger.js";
8
+ import { bootstrap, shutdown } from "../runtime/bootstrap.js";
9
+ import {
10
+ ensureEconomyTables,
11
+ priceService,
12
+ getServicePrice,
13
+ pay,
14
+ getBalance,
15
+ openChannel,
16
+ closeChannel,
17
+ listResource,
18
+ getMarketListings,
19
+ tradeResource,
20
+ mintNFT,
21
+ recordContribution,
22
+ getContributions,
23
+ distributeRevenue,
24
+ } from "../lib/agent-economy.js";
25
+
26
+ export function registerEconomyCommand(program) {
27
+ const economy = program
28
+ .command("economy")
29
+ .description("Agent economy — payments, channels, marketplace, NFTs");
30
+
31
+ // economy price
32
+ economy
33
+ .command("price <service-id> <price>")
34
+ .description("Set a service price")
35
+ .action(async (serviceId, price) => {
36
+ try {
37
+ const ctx = await bootstrap({ verbose: program.opts().verbose });
38
+ if (!ctx.db) {
39
+ logger.error("Database not available");
40
+ process.exit(1);
41
+ }
42
+ const db = ctx.db.getDatabase();
43
+ ensureEconomyTables(db);
44
+
45
+ const result = priceService(db, serviceId, parseFloat(price), {});
46
+ logger.success(
47
+ `Price set for ${chalk.cyan(serviceId)}: ${chalk.bold(result.price)}`,
48
+ );
49
+
50
+ await shutdown();
51
+ } catch (err) {
52
+ logger.error(`Failed: ${err.message}`);
53
+ process.exit(1);
54
+ }
55
+ });
56
+
57
+ // economy pay
58
+ economy
59
+ .command("pay <from> <to> <amount>")
60
+ .description("Make a payment between agents")
61
+ .option("--description <text>", "Payment description")
62
+ .action(async (from, to, amount, options) => {
63
+ try {
64
+ const ctx = await bootstrap({ verbose: program.opts().verbose });
65
+ if (!ctx.db) {
66
+ logger.error("Database not available");
67
+ process.exit(1);
68
+ }
69
+ const db = ctx.db.getDatabase();
70
+ ensureEconomyTables(db);
71
+
72
+ const result = pay(
73
+ db,
74
+ from,
75
+ to,
76
+ parseFloat(amount),
77
+ options.description,
78
+ );
79
+ logger.success("Payment complete");
80
+ logger.log(` ${chalk.bold("Tx ID:")} ${chalk.cyan(result.txId)}`);
81
+ logger.log(` ${chalk.bold("From:")} ${result.from}`);
82
+ logger.log(` ${chalk.bold("To:")} ${result.to}`);
83
+ logger.log(` ${chalk.bold("Amount:")} ${result.amount}`);
84
+ logger.log(` ${chalk.bold("Balance:")} ${result.balance}`);
85
+
86
+ await shutdown();
87
+ } catch (err) {
88
+ logger.error(`Failed: ${err.message}`);
89
+ process.exit(1);
90
+ }
91
+ });
92
+
93
+ // economy balance
94
+ economy
95
+ .command("balance <agent-id>")
96
+ .description("Show agent balance")
97
+ .option("--json", "Output as JSON")
98
+ .action(async (agentId, options) => {
99
+ try {
100
+ const ctx = await bootstrap({ verbose: program.opts().verbose });
101
+ if (!ctx.db) {
102
+ logger.error("Database not available");
103
+ process.exit(1);
104
+ }
105
+ const db = ctx.db.getDatabase();
106
+ ensureEconomyTables(db);
107
+
108
+ const bal = getBalance(agentId);
109
+ if (options.json) {
110
+ console.log(JSON.stringify({ agentId, ...bal }, null, 2));
111
+ } else {
112
+ logger.log(` ${chalk.bold("Agent:")} ${chalk.cyan(agentId)}`);
113
+ logger.log(` ${chalk.bold("Balance:")} ${bal.balance}`);
114
+ logger.log(` ${chalk.bold("Locked:")} ${bal.locked}`);
115
+ }
116
+
117
+ await shutdown();
118
+ } catch (err) {
119
+ logger.error(`Failed: ${err.message}`);
120
+ process.exit(1);
121
+ }
122
+ });
123
+
124
+ // economy channel open / close
125
+ const channel = economy
126
+ .command("channel")
127
+ .description("State channel management");
128
+
129
+ channel
130
+ .command("open <party-a> <party-b>")
131
+ .description("Open a state channel")
132
+ .option("--deposit <amount>", "Initial deposit from party A", "0")
133
+ .action(async (partyA, partyB, options) => {
134
+ try {
135
+ const ctx = await bootstrap({ verbose: program.opts().verbose });
136
+ if (!ctx.db) {
137
+ logger.error("Database not available");
138
+ process.exit(1);
139
+ }
140
+ const db = ctx.db.getDatabase();
141
+ ensureEconomyTables(db);
142
+
143
+ const ch = openChannel(db, partyA, partyB, parseFloat(options.deposit));
144
+ logger.success("Channel opened");
145
+ logger.log(` ${chalk.bold("ID:")} ${chalk.cyan(ch.id)}`);
146
+ logger.log(` ${chalk.bold("Party A:")} ${ch.partyA} (${ch.balanceA})`);
147
+ logger.log(` ${chalk.bold("Party B:")} ${ch.partyB} (${ch.balanceB})`);
148
+
149
+ await shutdown();
150
+ } catch (err) {
151
+ logger.error(`Failed: ${err.message}`);
152
+ process.exit(1);
153
+ }
154
+ });
155
+
156
+ channel
157
+ .command("close <channel-id>")
158
+ .description("Close and settle a state channel")
159
+ .action(async (channelId) => {
160
+ try {
161
+ const ctx = await bootstrap({ verbose: program.opts().verbose });
162
+ if (!ctx.db) {
163
+ logger.error("Database not available");
164
+ process.exit(1);
165
+ }
166
+ const db = ctx.db.getDatabase();
167
+ ensureEconomyTables(db);
168
+
169
+ const ch = closeChannel(db, channelId);
170
+ logger.success("Channel closed and settled");
171
+ logger.log(` ${chalk.bold("ID:")} ${chalk.cyan(ch.id)}`);
172
+ logger.log(` ${chalk.bold("Status:")} ${ch.status}`);
173
+
174
+ await shutdown();
175
+ } catch (err) {
176
+ logger.error(`Failed: ${err.message}`);
177
+ process.exit(1);
178
+ }
179
+ });
180
+
181
+ // economy market list / browse
182
+ const market = economy.command("market").description("Resource marketplace");
183
+
184
+ market
185
+ .command("list <type> <provider> <price> <amount>")
186
+ .description("List a resource on the marketplace")
187
+ .action(async (type, provider, price, amount) => {
188
+ try {
189
+ const ctx = await bootstrap({ verbose: program.opts().verbose });
190
+ if (!ctx.db) {
191
+ logger.error("Database not available");
192
+ process.exit(1);
193
+ }
194
+ const db = ctx.db.getDatabase();
195
+ ensureEconomyTables(db);
196
+
197
+ const listing = listResource(
198
+ db,
199
+ type,
200
+ provider,
201
+ parseFloat(price),
202
+ parseFloat(amount),
203
+ "unit",
204
+ );
205
+ logger.success("Resource listed");
206
+ logger.log(` ${chalk.bold("ID:")} ${chalk.cyan(listing.id)}`);
207
+ logger.log(` ${chalk.bold("Type:")} ${listing.resourceType}`);
208
+ logger.log(` ${chalk.bold("Provider:")} ${listing.provider}`);
209
+ logger.log(` ${chalk.bold("Price:")} ${listing.price}`);
210
+ logger.log(` ${chalk.bold("Available:")} ${listing.available}`);
211
+
212
+ await shutdown();
213
+ } catch (err) {
214
+ logger.error(`Failed: ${err.message}`);
215
+ process.exit(1);
216
+ }
217
+ });
218
+
219
+ market
220
+ .command("browse")
221
+ .description("Browse marketplace listings")
222
+ .option("--type <filter>", "Filter by resource type")
223
+ .option("--json", "Output as JSON")
224
+ .action(async (options) => {
225
+ try {
226
+ const ctx = await bootstrap({ verbose: program.opts().verbose });
227
+ if (!ctx.db) {
228
+ logger.error("Database not available");
229
+ process.exit(1);
230
+ }
231
+ const db = ctx.db.getDatabase();
232
+ ensureEconomyTables(db);
233
+
234
+ const filter = options.type ? { type: options.type } : undefined;
235
+ const listings = getMarketListings(filter);
236
+
237
+ if (options.json) {
238
+ console.log(JSON.stringify(listings, null, 2));
239
+ } else {
240
+ if (listings.length === 0) {
241
+ logger.log("No active listings");
242
+ } else {
243
+ for (const l of listings) {
244
+ logger.log(
245
+ ` ${chalk.cyan(l.id.slice(0, 8))} ${l.resourceType} by ${l.provider} — ${l.price}/${l.unit} (${l.available} avail)`,
246
+ );
247
+ }
248
+ }
249
+ }
250
+
251
+ await shutdown();
252
+ } catch (err) {
253
+ logger.error(`Failed: ${err.message}`);
254
+ process.exit(1);
255
+ }
256
+ });
257
+
258
+ // economy trade
259
+ economy
260
+ .command("trade <listing-id> <buyer> <quantity>")
261
+ .description("Buy a resource from the marketplace")
262
+ .action(async (listingId, buyer, quantity) => {
263
+ try {
264
+ const ctx = await bootstrap({ verbose: program.opts().verbose });
265
+ if (!ctx.db) {
266
+ logger.error("Database not available");
267
+ process.exit(1);
268
+ }
269
+ const db = ctx.db.getDatabase();
270
+ ensureEconomyTables(db);
271
+
272
+ const result = tradeResource(listingId, buyer, parseFloat(quantity));
273
+ logger.success("Trade complete");
274
+ logger.log(` ${chalk.bold("Cost:")} ${result.cost}`);
275
+ logger.log(` ${chalk.bold("Quantity:")} ${result.quantity}`);
276
+ logger.log(` ${chalk.bold("Remaining:")} ${result.remaining}`);
277
+
278
+ await shutdown();
279
+ } catch (err) {
280
+ logger.error(`Failed: ${err.message}`);
281
+ process.exit(1);
282
+ }
283
+ });
284
+
285
+ // economy nft mint
286
+ const nft = economy.command("nft").description("NFT management");
287
+
288
+ nft
289
+ .command("mint <owner> <type>")
290
+ .description("Mint a new NFT")
291
+ .option("--metadata <json>", "NFT metadata as JSON")
292
+ .action(async (owner, type, options) => {
293
+ try {
294
+ const ctx = await bootstrap({ verbose: program.opts().verbose });
295
+ if (!ctx.db) {
296
+ logger.error("Database not available");
297
+ process.exit(1);
298
+ }
299
+ const db = ctx.db.getDatabase();
300
+ ensureEconomyTables(db);
301
+
302
+ const metadata = options.metadata ? JSON.parse(options.metadata) : {};
303
+ const nftObj = mintNFT(db, owner, type, metadata);
304
+ logger.success("NFT minted");
305
+ logger.log(` ${chalk.bold("ID:")} ${chalk.cyan(nftObj.id)}`);
306
+ logger.log(` ${chalk.bold("Owner:")} ${nftObj.owner}`);
307
+ logger.log(` ${chalk.bold("Type:")} ${nftObj.type}`);
308
+
309
+ await shutdown();
310
+ } catch (err) {
311
+ logger.error(`Failed: ${err.message}`);
312
+ process.exit(1);
313
+ }
314
+ });
315
+
316
+ // economy revenue
317
+ economy
318
+ .command("revenue <pool> <agent-ids>")
319
+ .description("Distribute revenue pool (comma-separated agent IDs)")
320
+ .action(async (pool, agentIds) => {
321
+ try {
322
+ const ctx = await bootstrap({ verbose: program.opts().verbose });
323
+ if (!ctx.db) {
324
+ logger.error("Database not available");
325
+ process.exit(1);
326
+ }
327
+ const db = ctx.db.getDatabase();
328
+ ensureEconomyTables(db);
329
+
330
+ const ids = agentIds.split(",").map((s) => s.trim());
331
+ const results = distributeRevenue(db, parseFloat(pool), ids);
332
+ logger.success(
333
+ `Revenue distributed: ${pool} among ${ids.length} agents`,
334
+ );
335
+ for (const r of results) {
336
+ logger.log(
337
+ ` ${chalk.cyan(r.agentId)}: +${r.share} → ${r.newBalance}`,
338
+ );
339
+ }
340
+
341
+ await shutdown();
342
+ } catch (err) {
343
+ logger.error(`Failed: ${err.message}`);
344
+ process.exit(1);
345
+ }
346
+ });
347
+
348
+ // economy contribute
349
+ economy
350
+ .command("contribute <agent-id> <type> <value>")
351
+ .description("Record agent contribution")
352
+ .action(async (agentId, type, value) => {
353
+ try {
354
+ const ctx = await bootstrap({ verbose: program.opts().verbose });
355
+ if (!ctx.db) {
356
+ logger.error("Database not available");
357
+ process.exit(1);
358
+ }
359
+ const db = ctx.db.getDatabase();
360
+ ensureEconomyTables(db);
361
+
362
+ const c = recordContribution(db, agentId, type, parseFloat(value), "");
363
+ logger.success("Contribution recorded");
364
+ logger.log(` ${chalk.bold("ID:")} ${chalk.cyan(c.id)}`);
365
+ logger.log(` ${chalk.bold("Agent:")} ${c.agentId}`);
366
+ logger.log(` ${chalk.bold("Type:")} ${c.type}`);
367
+ logger.log(` ${chalk.bold("Value:")} ${c.value}`);
368
+
369
+ await shutdown();
370
+ } catch (err) {
371
+ logger.error(`Failed: ${err.message}`);
372
+ process.exit(1);
373
+ }
374
+ });
375
+ }