@relipa/ai-flow-kit 0.0.9-beta.1 → 0.1.0-beta.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/bin/aiflow.js CHANGED
@@ -1,475 +1,525 @@
1
- #!/usr/bin/env node
2
-
3
- const { program } = require('commander');
4
- const chalk = require('chalk');
5
- const packageJson = require('../package.json');
6
- const initCommand = require('../scripts/init');
7
- const updateCommand = require('../scripts/update');
8
- const doctorCommand = require('../scripts/doctor');
9
- const useCommand = require('../scripts/use');
10
- const TaskDetector = require('../scripts/detect');
11
- const validateCommand = require('../scripts/validate');
12
- const memoryCommand = require('../scripts/memory');
13
- const contextCommand = require('../scripts/context');
14
- const promptCommand = require('../scripts/prompt');
15
- const removeCommand = require('../scripts/remove');
16
- const guideCommand = require('../scripts/guide');
17
- const telemetryCommand= require('../scripts/telemetry/cli');
18
- const taskCommand = require('../scripts/task');
19
- const checkpointCommand = require('../scripts/checkpoint');
20
- const { record } = require('../scripts/telemetry/record');
21
- const { updateTaskGateState } = require('../scripts/task');
22
-
23
- // ── DEPRECATION WARNING ───────────────────────────────────────
24
- if (!process.env.AIFLOW_IS_AK) {
25
- console.warn(chalk.yellow('\n⚠ Warning: "aiflow" command is deprecated and will be removed in future versions.'));
26
- console.warn(chalk.yellow('Please use the shorter "ak" command instead.\n'));
27
- }
28
-
29
- program
30
- .version(packageJson.version)
31
- .description('AI Flow Kit CLI - AI-powered development workflows for teams');
32
-
33
- // ── GLOBAL TELEMETRY HOOK ────────────────────────────────────
34
- // We intercept raw argv here so we can catch --version, --help,
35
- // and invalid commands *before* Commander exits the process.
36
- // Also alias `-v` to commander's `-V` at the root only — subcommands still
37
- // keep `-v` as `--verbose`. And alias `--fw` to `--framework`.
38
- (() => {
39
- if (process.argv[2] === '-v') process.argv[2] = '-V';
40
- for (let i = 3; i < process.argv.length; i++) {
41
- if (process.argv[i] === '--fw') process.argv[i] = '--framework';
42
- }
43
- const args = process.argv.slice(2);
44
- let cmd = 'help';
45
- if (args.length > 0) {
46
- let first = args[0];
47
-
48
- // 1. Phân giải alias cho root commands
49
- const aliases = {
50
- 'i': 'init',
51
- 'u': 'use',
52
- 'p': 'prompt',
53
- 'd': 'detect',
54
- 't': 'task',
55
- 'ctx': 'context',
56
- 'cp': 'checkpoint',
57
- 'vl': 'validate',
58
- 'mem': 'memory',
59
- 'g': 'guide',
60
- 'rm': 'remove',
61
- 'up': 'update',
62
- 'sync': 'sync-skills',
63
- 'dr': 'doctor',
64
- 'tel': 'telemetry'
65
- };
66
- if (aliases[first]) {
67
- first = aliases[first];
68
- }
69
-
70
- if (['-V', '-v', '--version'].includes(first)) {
71
- cmd = 'version';
72
- } else if (['-h', '--help'].includes(first)) {
73
- cmd = 'help';
74
- } else if (first.startsWith('-')) {
75
- cmd = 'unknown_flag';
76
- if (args.includes('--help') || args.includes('-h')) cmd = 'help';
77
- } else if (first === 'memory') {
78
- const sec = args[1];
79
- // Phân giải alias cho memory subcommands
80
- let sub = sec;
81
- if (sec && !sec.startsWith('-')) {
82
- const memAliases = {
83
- 's': 'save',
84
- 'g': 'get',
85
- 'ls': 'list',
86
- 'sr': 'search',
87
- 'd': 'delete',
88
- 'cl': 'clear'
89
- };
90
- if (memAliases[sec]) sub = memAliases[sec];
91
- cmd = `memory.${sub}`;
92
- } else {
93
- cmd = 'memory';
94
- }
95
- } else if (first === 'task') {
96
- const sec = args[1];
97
- // Phân giải alias subcommand của task
98
- let sub = sec;
99
- if (sec && !sec.startsWith('-')) {
100
- const subAliases = {
101
- 'st': 'status',
102
- 'ls': 'list',
103
- 'p': 'pause',
104
- 'sw': 'switch',
105
- 'r': 'resume',
106
- 'rst': 'reset',
107
- 'rm': 'remove',
108
- 'n': 'next'
109
- };
110
- if (subAliases[sec]) sub = subAliases[sec];
111
- cmd = `task.${sub}`;
112
- } else {
113
- cmd = 'task';
114
- }
115
- } else {
116
- cmd = first;
117
- if (args.includes('--help') || args.includes('-h')) {
118
- cmd = `${first}.help`;
119
- }
120
- }
121
- }
122
- // Record everything except telemetry and gate actions (gate records its own rich event)
123
- if (cmd !== 'telemetry' && !cmd.startsWith('telemetry.') && cmd !== 'gate') {
124
- record('command.invoked', { command: cmd });
125
- }
126
- })();
127
-
128
-
129
- // ── init ──────────────────────────────────────────────────────
130
- program
131
- .command('init')
132
- .alias('i')
133
- .description('Initialize AI Flow Kit in your project')
134
- .option('-f, --framework <types>', 'framework(s), comma-separated (e.g. spring-boot,reactjs)')
135
- .option('-a, --adapter <types>', 'adapter(s), comma-separated (e.g. backlog,jira)')
136
- .option('-e, --env <types>', 'AI environment(s)/tool(s), comma-separated (e.g. cursor,gemini,copilot)')
137
- .option('--with-rtk', 'force enable RTK token compression hook')
138
- .option('--no-rtk', 'skip RTK setup even if RTK is detected')
139
- .action((options) => {
140
- options.frameworks = options.framework
141
- ? options.framework.split(',').map(s => s.trim()).filter(Boolean)
142
- : [];
143
- options.adapters = options.adapter
144
- ? options.adapter.split(',').map(s => s.trim()).filter(Boolean)
145
- : [];
146
- options.aiTools = options.env
147
- ? options.env.split(',').map(s => s.trim()).filter(Boolean)
148
- : Object.keys(require('../scripts/init').AI_TOOL_FILES || {}).filter(t => t !== 'generic');
149
-
150
- initCommand(options);
151
- });
152
-
153
- // ── use ───────────────────────────────────────────────────────
154
- program
155
- .command('use [target]')
156
- .alias('u')
157
- .description('Load context from ticket, version, or file')
158
- .option('-m, --manual', 'manual context entry')
159
- .option('-f, --file <path>', 'load from file')
160
- .option('-s, --save <name>', 'save as named context')
161
- .option('-c, --coms', 'load all comments (alias for --with-comments)')
162
- .option('--with-comments', 'load all comments')
163
- .option('--cid <id>', 'load a specific comment by ID')
164
- .option('--clast <n>', 'load the last N comments', parseInt)
165
- .option('--cfrom <id>', 'load comments from ID N onward', parseInt)
166
- .option('--cto <id>', 'load comments up to ID N', parseInt)
167
- .option('-F, --fast', 'fast mode: minimal Q&A, concise requirement doc (Default)')
168
- .option('-U, --full', 'full mode: force complete analysis with Q&A (default)')
169
- .action((target, options) => {
170
- useCommand(target, options);
171
- });
172
-
173
- // ── prompt ────────────────────────────────────────────────────
174
- program
175
- .command('prompt [type]')
176
- .alias('p')
177
- .description('Generate AI prompt with context and rules')
178
- .option('-l, --list', 'list available prompt types')
179
- .option('-o, --output <file>', 'save to file')
180
- .option('-L, --lang <lang>', 'language: english (default) or vietnamese')
181
- .option('-d, --detail <level>', 'detail level: minimal | standard (default) | comprehensive')
182
- .action((type, options) => {
183
- promptCommand(type, options);
184
- });
185
-
186
- // ── detect ────────────────────────────────────────────────────
187
- program
188
- .command('detect [description]')
189
- .alias('d')
190
- .description('Auto-detect task type from description')
191
- .option('-v, --verbose', 'show detection details')
192
- .option('-t, --threshold <number>', 'confidence threshold (0-100)')
193
- .action((description, options) => {
194
- const detector = new TaskDetector();
195
- const threshold = options.threshold ? parseInt(options.threshold) / 100 : 0.8;
196
- const result = detector.detect(description, threshold);
197
- console.log(chalk.cyan('\nTask Detection Results:\n'));
198
- console.log(detector.formatOutput(result, options.verbose));
199
- });
200
-
201
- // ── task ─────────────────────────────────────────────────────
202
- const taskCmd = program.command('task').alias('t').description('Manage multiple tasks — pause, switch, resume');
203
-
204
- taskCmd
205
- .command('status')
206
- .alias('st')
207
- .description('Show active task and pending tasks')
208
- .action(() => { taskCommand('status'); });
209
-
210
- taskCmd
211
- .command('list')
212
- .alias('ls')
213
- .description('List all saved tasks')
214
- .action(() => { taskCommand('list'); });
215
-
216
- taskCmd
217
- .command('pause')
218
- .alias('p')
219
- .description('Pause current task and save progress')
220
- .option('-n, --note <text>', 'note about why you are pausing')
221
- .action((opts) => { taskCommand('pause', { note: opts.note }); });
222
-
223
- taskCmd
224
- .command('switch <ticket-id>')
225
- .alias('sw')
226
- .description('Pause current task and switch to another')
227
- .action((ticketId) => { taskCommand('switch', { taskId: ticketId }); });
228
-
229
- taskCmd
230
- .command('resume <ticket-id>')
231
- .alias('r')
232
- .description('Resume a paused task')
233
- .action((ticketId) => { taskCommand('resume', { taskId: ticketId }); });
234
-
235
- taskCmd
236
- .command('reset <ticket-id>')
237
- .alias('rst')
238
- .description('Reset task to Gate 1 (keeps ticket context, clears gate progress)')
239
- .action((ticketId) => { taskCommand('reset', { taskId: ticketId }); });
240
-
241
- taskCmd
242
- .command('remove <ticket-id>')
243
- .alias('rm')
244
- .description('Permanently delete all saved data for a task')
245
- .action((ticketId) => { taskCommand('remove', { taskId: ticketId }); });
246
-
247
- taskCmd
248
- .command('next')
249
- .alias('n')
250
- .description('Approve current gate and prepare next session (saves state for resume)')
251
- .option('-t, --ticket <id>', 'ticket ID (defaults to active task)')
252
- .action((opts) => {
253
- taskCommand('next', { taskId: opts.ticket });
254
- });
255
-
256
- // ── context ───────────────────────────────────────────────────
257
- program
258
- .command('context [action]')
259
- .alias('ctx')
260
- .description('Manage task contexts (list|show|save <name>|clear)')
261
- .option('-s, --save <name>', 'save current context with name')
262
- .option('-l, --load <name>', 'load saved context by name')
263
- .option('-d, --delete <name>', 'delete saved context')
264
- .option('--clear', 'clear all saved contexts')
265
- .option('--show', 'show current context')
266
- .action((action, options) => {
267
- contextCommand(action, options);
268
- });
269
-
270
- // ── checkpoint ────────────────────────────────────────────────
271
- program
272
- .command('checkpoint')
273
- .alias('cp')
274
- .description('Record token usage checkpoint (called by AI during gate work)')
275
- .requiredOption('-g, --gate <n>', 'gate number (1–5)', parseInt)
276
- .requiredOption('-s, --step <name>', 'step name (e.g. "tests-written")')
277
- .requiredOption('-n, --tokens <n>', 'estimated token count', parseInt)
278
- .option('-t, --ticket <id>', 'ticket ID (defaults to active task)')
279
- .action((opts) => {
280
- checkpointCommand({ gate: opts.gate, step: opts.step, tokens: opts.tokens, ticket: opts.ticket });
281
- });
282
-
283
- // ── validate ──────────────────────────────────────────────────
284
- program
285
- .command('validate <file>')
286
- .alias('vl')
287
- .description('Validate output against team rules')
288
- .option('-r, --ruleset <set>', 'rule set: default | strict | lenient', 'default')
289
- .option('-v, --verbose', 'detailed report')
290
- .option('-x, --fix', 'auto-fix trailing whitespace')
291
- .action((file, options) => {
292
- validateCommand(file, {
293
- ruleset: options.ruleset,
294
- verbose: options.verbose,
295
- fix: options.fix
296
- });
297
- });
298
-
299
- // ── memory ────────────────────────────────────────────────────
300
- // Sub-commands to avoid the ambiguous "memory <action> --save key" pattern
301
- const memCmd = program.command('memory').alias('mem').description('Manage team knowledge and memory');
302
-
303
- memCmd
304
- .command('save <key> <value>')
305
- .alias('s')
306
- .description('Save a memory entry')
307
- .action((key, value) => { memoryCommand('save', { key, value }); });
308
-
309
- memCmd
310
- .command('get <key>')
311
- .alias('g')
312
- .description('Retrieve a memory entry')
313
- .action((key) => { memoryCommand('get', { key }); });
314
-
315
- memCmd
316
- .command('list')
317
- .alias('ls')
318
- .description('List all memories')
319
- .action(() => { memoryCommand('list'); });
320
-
321
- memCmd
322
- .command('search <query>')
323
- .alias('sr')
324
- .description('Search memories by keyword')
325
- .action((query) => { memoryCommand('search', { query }); });
326
-
327
- memCmd
328
- .command('delete <key>')
329
- .alias('d')
330
- .description('Delete a memory entry')
331
- .action((key) => { memoryCommand('delete', { key }); });
332
-
333
- memCmd
334
- .command('clear')
335
- .alias('cl')
336
- .description('Clear all memories')
337
- .action(() => { memoryCommand('clear'); });
338
-
339
- // ── guide ─────────────────────────────────────────────────────
340
- program
341
- .command('guide')
342
- .alias('g')
343
- .description('Show quickstart guide, architecture, and command reference')
344
- .option('-f, --flow', 'show architecture & flow diagram only')
345
- .option('-c, --commands', 'show command reference only')
346
- .action((options) => {
347
- guideCommand(options);
348
- });
349
-
350
- // ── telemetry feedback helper ─────────────────────────────────
351
- function showTelResult(result, label) {
352
- try {
353
- if (!result) return;
354
- if (result.ok) {
355
- console.log(chalk.green(` [tel] ✓ ${label} logged`));
356
- } else if (result.reason === 'disabled') {
357
- console.log(chalk.yellow(` [tel] skipped telemetry is disabled (run 'aiflow telemetry enable')`));
358
- } else if (result.reason === 'opted-out') {
359
- console.log(chalk.yellow(` [tel] ⚠ skipped — repo has .aiflow/no-telemetry opt-out`));
360
- } else if (result.reason === 'no-url') {
361
- console.log(chalk.yellow(` [tel] ⚠ skipped — no server URL configured (run 'aiflow telemetry enable')`));
362
- } else if (result.error) {
363
- console.log(chalk.yellow(` [tel] error logging '${label}': ${result.error}`));
364
- }
365
- } catch (e) { /* never block main flow */ }
366
- }
367
-
368
- // ── gate (telemetry for gate workflow events) ─────────────────
369
- program
370
- .command('gate <number> <action>')
371
- .description('Log a gate workflow event (start|approved) — called by AI during gate workflow')
372
- .option('--ticket <id>', 'ticket ID')
373
- .option('--ai-tool <tool>', 'AI tool name')
374
- .action((number, action, options) => {
375
- const gateNum = parseInt(number);
376
- if (isNaN(gateNum) || gateNum < 1 || gateNum > 5) return;
377
- if (!['start', 'approved'].includes(action)) return;
378
-
379
- const ticketId = options.ticket || '';
380
-
381
- const telResult = record(`gate.${action}`, {
382
- gate: gateNum,
383
- ticket_id: ticketId,
384
- command: `gate${gateNum}.${action}`,
385
- ai_tool: options.aiTool || ''
386
- });
387
- showTelResult(telResult, `gate${gateNum}.${action}`);
388
-
389
- // Keep task-state.json currentGate in sync (silent — never blocks the AI)
390
- if (ticketId) {
391
- updateTaskGateState(ticketId, gateNum, action).catch(() => {});
392
- }
393
-
394
- if (action === 'approved') {
395
- console.log(chalk.cyan(`\n Gate ${gateNum} approved!`));
396
- console.log(chalk.yellow(` Pro-Tip: To avoid context pollution, it's recommended to start a fresh chat session.`));
397
- console.log(chalk.gray(` Run: aiflow task next${ticketId ? ` --ticket ${ticketId}` : ''} to save progress,`));
398
- console.log(chalk.gray(` then open a NEW chatbox and type "continue".\n`));
399
- }
400
- });
401
-
402
- // ── remove ────────────────────────────────────────────────────
403
- program
404
- .command('remove')
405
- .alias('rm')
406
- .description('Remove ai-flow-kit from project, a cached version, or uninstall globally')
407
- .option('--version <ver>', 'remove a specific cached version from .aiflow/versions/')
408
- .option('-g, --global', 'uninstall the global npm package (removes `aiflow` command)')
409
- .action((options) => {
410
- removeCommand(options);
411
- });
412
-
413
- // ── update ────────────────────────────────────────────────────
414
- program
415
- .command('update')
416
- .alias('up')
417
- .description('Update to latest version')
418
- .option('-f, --force', 'force update even if already on latest')
419
- .action((options) => {
420
- updateCommand(options);
421
- });
422
-
423
- // ── sync-skills ───────────────────────────────────────────────
424
- program
425
- .command('sync-skills')
426
- .alias('sync')
427
- .description('Manually synchronize AI Instruction files with local custom skills')
428
- .action(async () => {
429
- const projectDir = process.cwd();
430
- const { setupFramework, AI_TOOL_FILES, ensureAiflowGitignored } = require('../scripts/init');
431
- const fs = require('fs-extra');
432
- const path = require('path');
433
- const chalk = require('chalk');
434
-
435
- const stateFile = path.join(projectDir, '.aiflow', 'state.json');
436
- if (!(await fs.pathExists(stateFile))) {
437
- console.log(chalk.red('Project is not initialized. Please run `aiflow init` first.'));
438
- return;
439
- }
440
-
441
- const state = await fs.readJson(stateFile);
442
- const frameworks = state.frameworks || [];
443
- const selectedTools = state.aiTools || Object.keys(AI_TOOL_FILES);
444
-
445
- console.log(chalk.cyan('⟳ Syncing AI Instruction files...'));
446
- for (const fw of frameworks) {
447
- await setupFramework(projectDir, fw, frameworks.length > 1, selectedTools);
448
- }
449
- await ensureAiflowGitignored(projectDir);
450
- console.log(chalk.green('✨ Sync completed!'));
451
- });
452
-
453
- // ── doctor ────────────────────────────────────────────────────
454
- program
455
- .command('doctor')
456
- .alias('dr')
457
- .description('Health check of AI Flow Kit setup')
458
- .option('-t, --ticket <id>', 'show token breakdown for specific ticket')
459
- .option('-v, --verbose', 'detailed output')
460
- .action((opts) => {
461
- doctorCommand({ ticket: opts.ticket, verbose: opts.verbose });
462
- });
463
-
464
- // ── telemetry ──────────────────────────────────────────────────
465
- program
466
- .command('telemetry [action]')
467
- .alias('tel')
468
- .description('Manage telemetry logging (enable|disable|status|log)')
469
- .option('--event <event>', 'event name (for log action)')
470
- .option('--detail <detail>', 'detail string (for log action)')
471
- .action((action, options) => {
472
- telemetryCommand(action || 'status', options);
473
- });
474
-
475
- program.parse(process.argv);
1
+ #!/usr/bin/env node
2
+
3
+ const { program } = require('commander');
4
+ const chalk = require('chalk');
5
+ const packageJson = require('../package.json');
6
+ const initCommand = require('../scripts/init');
7
+ const updateCommand = require('../scripts/update');
8
+ const doctorCommand = require('../scripts/doctor');
9
+ const useCommand = require('../scripts/use');
10
+ const TaskDetector = require('../scripts/detect');
11
+ const validateCommand = require('../scripts/validate');
12
+ const memoryCommand = require('../scripts/memory');
13
+ const contextCommand = require('../scripts/context');
14
+ const promptCommand = require('../scripts/prompt');
15
+ const removeCommand = require('../scripts/remove');
16
+ const guideCommand = require('../scripts/guide');
17
+ const telemetryCommand= require('../scripts/telemetry/cli');
18
+ const taskCommand = require('../scripts/task');
19
+ const checkpointCommand = require('../scripts/checkpoint');
20
+ const { record } = require('../scripts/telemetry/record');
21
+ const { updateTaskGateState } = require('../scripts/task');
22
+
23
+ // ── DEPRECATION WARNING ───────────────────────────────────────
24
+ if (!process.env.AIFLOW_IS_AK) {
25
+ console.warn(chalk.yellow('\n⚠ Warning: "aiflow" command is deprecated and will be removed in future versions.'));
26
+ console.warn(chalk.yellow('Please use the shorter "ak" command instead.\n'));
27
+ }
28
+
29
+ program
30
+ .version(packageJson.version)
31
+ .description('AI Flow Kit CLI - AI-powered development workflows for teams');
32
+
33
+ // ── GLOBAL TELEMETRY HOOK ────────────────────────────────────
34
+ // We intercept raw argv here so we can catch --version, --help,
35
+ // and invalid commands *before* Commander exits the process.
36
+ // Also alias `-v` to commander's `-V` at the root only — subcommands still
37
+ // keep `-v` as `--verbose`. And alias `--fw` to `--framework`.
38
+ (() => {
39
+ if (process.argv[2] === '-v') process.argv[2] = '-V';
40
+ for (let i = 3; i < process.argv.length; i++) {
41
+ if (process.argv[i] === '--fw') process.argv[i] = '--framework';
42
+ }
43
+ const args = process.argv.slice(2);
44
+ let cmd = 'help';
45
+ if (args.length > 0) {
46
+ let first = args[0];
47
+
48
+ // 1. Phân giải alias cho root commands
49
+ const aliases = {
50
+ 'i': 'init',
51
+ 'u': 'use',
52
+ 'p': 'prompt',
53
+ 'd': 'detect',
54
+ 't': 'task',
55
+ 'ctx': 'context',
56
+ 'cp': 'checkpoint',
57
+ 'vl': 'validate',
58
+ 'mem': 'memory',
59
+ 'g': 'guide',
60
+ 'rm': 'remove',
61
+ 'up': 'update',
62
+ 'sync': 'sync-skills',
63
+ 'dr': 'doctor',
64
+ 'tel': 'telemetry'
65
+ };
66
+ if (aliases[first]) {
67
+ first = aliases[first];
68
+ }
69
+
70
+ if (['-V', '-v', '--version'].includes(first)) {
71
+ cmd = 'version';
72
+ } else if (['-h', '--help'].includes(first)) {
73
+ cmd = 'help';
74
+ } else if (first.startsWith('-')) {
75
+ cmd = 'unknown_flag';
76
+ if (args.includes('--help') || args.includes('-h')) cmd = 'help';
77
+ } else if (first === 'memory') {
78
+ const sec = args[1];
79
+ // Phân giải alias cho memory subcommands
80
+ let sub = sec;
81
+ if (sec && !sec.startsWith('-')) {
82
+ const memAliases = {
83
+ 's': 'save',
84
+ 'g': 'get',
85
+ 'ls': 'list',
86
+ 'sr': 'search',
87
+ 'd': 'delete',
88
+ 'cl': 'clear'
89
+ };
90
+ if (memAliases[sec]) sub = memAliases[sec];
91
+ cmd = `memory.${sub}`;
92
+ } else {
93
+ cmd = 'memory';
94
+ }
95
+ } else if (first === 'task') {
96
+ const sec = args[1];
97
+ // Phân giải alias subcommand của task
98
+ let sub = sec;
99
+ if (sec && !sec.startsWith('-')) {
100
+ const subAliases = {
101
+ 'st': 'status',
102
+ 'ls': 'list',
103
+ 'p': 'pause',
104
+ 'sw': 'switch',
105
+ 'r': 'resume',
106
+ 'rst': 'reset',
107
+ 'rm': 'remove',
108
+ 'n': 'next'
109
+ };
110
+ if (subAliases[sec]) sub = subAliases[sec];
111
+ cmd = `task.${sub}`;
112
+ } else {
113
+ cmd = 'task';
114
+ }
115
+ } else {
116
+ cmd = first;
117
+ if (args.includes('--help') || args.includes('-h')) {
118
+ cmd = `${first}.help`;
119
+ }
120
+ }
121
+ }
122
+ // Record everything except telemetry and gate actions (gate records its own rich event)
123
+ if (cmd !== 'telemetry' && !cmd.startsWith('telemetry.') && cmd !== 'gate') {
124
+ record('command.invoked', { command: cmd });
125
+ }
126
+ })();
127
+
128
+
129
+ // ── init ──────────────────────────────────────────────────────
130
+ program
131
+ .command('init')
132
+ .alias('i')
133
+ .description('Initialize AI Flow Kit in your project')
134
+ .option('-f, --framework <types>', 'framework(s), comma-separated (e.g. spring-boot,reactjs)')
135
+ .option('-a, --adapter <types>', 'adapter(s), comma-separated (e.g. backlog,jira)')
136
+ .option('-e, --env <types>', 'AI environment(s)/tool(s), comma-separated (e.g. cursor,gemini,copilot)')
137
+ .option('--with-rtk', 'force enable RTK token compression hook')
138
+ .option('--no-rtk', 'skip RTK setup even if RTK is detected')
139
+ .action((options) => {
140
+ options.frameworks = options.framework
141
+ ? options.framework.split(',').map(s => s.trim()).filter(Boolean)
142
+ : [];
143
+ options.adapters = options.adapter
144
+ ? options.adapter.split(',').map(s => s.trim()).filter(Boolean)
145
+ : [];
146
+ options.aiTools = options.env
147
+ ? options.env.split(',').map(s => s.trim()).filter(Boolean)
148
+ : Object.keys(require('../scripts/init').AI_TOOL_FILES || {}).filter(t => t !== 'generic');
149
+
150
+ initCommand(options);
151
+ });
152
+
153
+ // ── use ───────────────────────────────────────────────────────
154
+ program
155
+ .command('use [targets...]')
156
+ .alias('u')
157
+ .description('Load context from ticket(s), version, or file(s). First target is primary; rest become supplementary context.')
158
+ .option('-m, --manual', 'manual context entry')
159
+ .option('-f, --file <path>', 'load from file')
160
+ .option('-s, --save <name>', 'save as named context')
161
+ .option('-c, --coms', 'load all comments (alias for --with-comments)')
162
+ .option('--with-comments', 'load all comments')
163
+ .option('--cid <id>', 'load a specific comment by ID')
164
+ .option('--clast <n>', 'load the last N comments', parseInt)
165
+ .option('--cfrom <id>', 'load comments from ID N onward', parseInt)
166
+ .option('--cto <id>', 'load comments up to ID N', parseInt)
167
+ .option('-F, --fast', 'fast mode: minimal Q&A, concise requirement doc (Default)')
168
+ .option('-U, --full', 'full mode: force complete analysis with Q&A (default)')
169
+ .addHelpText('after', `
170
+ Examples:
171
+ $ ak use PROJ-33 Single ticket (primary only)
172
+ $ ak use PROJ-33 PROJ-10 docs/arch.md Multi-target: PROJ-33 primary, rest supplementary (v0.1.0+)
173
+ $ ak use https://company.backlog.com/view/PROJ-33 Load by Backlog URL
174
+ $ ak use PROJ-33 -c Include all comments
175
+ $ ak use PROJ-33 --clast 5 Newest 5 comments only
176
+ $ ak use --file task.md Load from local file
177
+ $ ak use --manual Manual entry
178
+
179
+ Auto link resolution (v0.1.0+):
180
+ Backlog/Jira URLs in the primary ticket description are auto-fetched and added
181
+ to supplementaryContext[]. Comment links (#comment-456 or ?focusedCommentId=456)
182
+ fetch only that comment. Capped at 5 auto-resolved links per call.
183
+
184
+ Related:
185
+ ak fetch-links <url> Fetch a single Backlog/Jira link → JSON (AI runtime use)
186
+ `)
187
+ .action((targets, options) => {
188
+ useCommand(targets, options);
189
+ });
190
+
191
+ // ── fetch-links ───────────────────────────────────────────────
192
+ program
193
+ .command('fetch-links <url>')
194
+ .description('Fetch a backlog/jira link and output SupplementaryContext JSON to stdout. Used by AI at runtime.')
195
+ .addHelpText('after', `
196
+ Examples:
197
+ $ ak fetch-links "https://company.backlog.com/view/PROJ-10"
198
+ $ ak fetch-links "https://company.backlog.com/view/PROJ-10#comment-456"
199
+ $ ak fetch-links "https://company.atlassian.net/browse/PROJ-10"
200
+ $ ak fetch-links "https://company.atlassian.net/browse/PROJ-10?focusedCommentId=789"
201
+
202
+ Exit codes:
203
+ 0 Success — JSON written to stdout
204
+ 1 URL not recognised, or fetch failed (error on stderr)
205
+ `)
206
+ .action(async (url) => {
207
+ const { fetchLink } = require('../scripts/link-resolver');
208
+ const { loadCredentials } = require('../scripts/use');
209
+ const credentials = await loadCredentials();
210
+ try {
211
+ const result = await fetchLink(url, credentials);
212
+ if (!result) {
213
+ process.stderr.write(`Not a recognized backlog/jira URL: ${url}\n`);
214
+ process.exit(1);
215
+ }
216
+ process.stdout.write(JSON.stringify(result, null, 2) + '\n');
217
+ } catch (err) {
218
+ process.stderr.write(`Error fetching link: ${err.message}\n`);
219
+ process.exit(1);
220
+ }
221
+ });
222
+
223
+ // ── prompt ────────────────────────────────────────────────────
224
+ program
225
+ .command('prompt [type]')
226
+ .alias('p')
227
+ .description('Generate AI prompt with context and rules')
228
+ .option('-l, --list', 'list available prompt types')
229
+ .option('-o, --output <file>', 'save to file')
230
+ .option('-L, --lang <lang>', 'language: english (default) or vietnamese')
231
+ .option('-d, --detail <level>', 'detail level: minimal | standard (default) | comprehensive')
232
+ .action((type, options) => {
233
+ promptCommand(type, options);
234
+ });
235
+
236
+ // ── detect ────────────────────────────────────────────────────
237
+ program
238
+ .command('detect [description]')
239
+ .alias('d')
240
+ .description('Auto-detect task type from description')
241
+ .option('-v, --verbose', 'show detection details')
242
+ .option('-t, --threshold <number>', 'confidence threshold (0-100)')
243
+ .action((description, options) => {
244
+ const detector = new TaskDetector();
245
+ const threshold = options.threshold ? parseInt(options.threshold) / 100 : 0.8;
246
+ const result = detector.detect(description, threshold);
247
+ console.log(chalk.cyan('\nTask Detection Results:\n'));
248
+ console.log(detector.formatOutput(result, options.verbose));
249
+ });
250
+
251
+ // ── task ─────────────────────────────────────────────────────
252
+ const taskCmd = program.command('task').alias('t').description('Manage multiple tasks — pause, switch, resume');
253
+
254
+ taskCmd
255
+ .command('status')
256
+ .alias('st')
257
+ .description('Show active task and pending tasks')
258
+ .action(() => { taskCommand('status'); });
259
+
260
+ taskCmd
261
+ .command('list')
262
+ .alias('ls')
263
+ .description('List all saved tasks')
264
+ .action(() => { taskCommand('list'); });
265
+
266
+ taskCmd
267
+ .command('pause')
268
+ .alias('p')
269
+ .description('Pause current task and save progress')
270
+ .option('-n, --note <text>', 'note about why you are pausing')
271
+ .action((opts) => { taskCommand('pause', { note: opts.note }); });
272
+
273
+ taskCmd
274
+ .command('switch <ticket-id>')
275
+ .alias('sw')
276
+ .description('Pause current task and switch to another')
277
+ .action((ticketId) => { taskCommand('switch', { taskId: ticketId }); });
278
+
279
+ taskCmd
280
+ .command('resume <ticket-id>')
281
+ .alias('r')
282
+ .description('Resume a paused task')
283
+ .action((ticketId) => { taskCommand('resume', { taskId: ticketId }); });
284
+
285
+ taskCmd
286
+ .command('reset <ticket-id>')
287
+ .alias('rst')
288
+ .description('Reset task to Gate 1 (keeps ticket context, clears gate progress)')
289
+ .action((ticketId) => { taskCommand('reset', { taskId: ticketId }); });
290
+
291
+ taskCmd
292
+ .command('remove <ticket-id>')
293
+ .alias('rm')
294
+ .description('Permanently delete all saved data for a task')
295
+ .action((ticketId) => { taskCommand('remove', { taskId: ticketId }); });
296
+
297
+ taskCmd
298
+ .command('next')
299
+ .alias('n')
300
+ .description('Approve current gate and prepare next session (saves state for resume)')
301
+ .option('-t, --ticket <id>', 'ticket ID (defaults to active task)')
302
+ .action((opts) => {
303
+ taskCommand('next', { taskId: opts.ticket });
304
+ });
305
+
306
+ // ── context ───────────────────────────────────────────────────
307
+ program
308
+ .command('context [action]')
309
+ .alias('ctx')
310
+ .description('Manage task contexts (list|show|save <name>|clear)')
311
+ .option('-s, --save <name>', 'save current context with name')
312
+ .option('-l, --load <name>', 'load saved context by name')
313
+ .option('-d, --delete <name>', 'delete saved context')
314
+ .option('--clear', 'clear all saved contexts')
315
+ .option('--show', 'show current context')
316
+ .action((action, options) => {
317
+ contextCommand(action, options);
318
+ });
319
+
320
+ // ── checkpoint ────────────────────────────────────────────────
321
+ program
322
+ .command('checkpoint')
323
+ .alias('cp')
324
+ .description('Record token usage checkpoint (called by AI during gate work)')
325
+ .requiredOption('-g, --gate <n>', 'gate number (1–5)', parseInt)
326
+ .requiredOption('-s, --step <name>', 'step name (e.g. "tests-written")')
327
+ .requiredOption('-n, --tokens <n>', 'estimated token count', parseInt)
328
+ .option('-t, --ticket <id>', 'ticket ID (defaults to active task)')
329
+ .action((opts) => {
330
+ checkpointCommand({ gate: opts.gate, step: opts.step, tokens: opts.tokens, ticket: opts.ticket });
331
+ });
332
+
333
+ // ── validate ──────────────────────────────────────────────────
334
+ program
335
+ .command('validate <file>')
336
+ .alias('vl')
337
+ .description('Validate output against team rules')
338
+ .option('-r, --ruleset <set>', 'rule set: default | strict | lenient', 'default')
339
+ .option('-v, --verbose', 'detailed report')
340
+ .option('-x, --fix', 'auto-fix trailing whitespace')
341
+ .action((file, options) => {
342
+ validateCommand(file, {
343
+ ruleset: options.ruleset,
344
+ verbose: options.verbose,
345
+ fix: options.fix
346
+ });
347
+ });
348
+
349
+ // ── memory ────────────────────────────────────────────────────
350
+ // Sub-commands to avoid the ambiguous "memory <action> --save key" pattern
351
+ const memCmd = program.command('memory').alias('mem').description('Manage team knowledge and memory');
352
+
353
+ memCmd
354
+ .command('save <key> <value>')
355
+ .alias('s')
356
+ .description('Save a memory entry')
357
+ .action((key, value) => { memoryCommand('save', { key, value }); });
358
+
359
+ memCmd
360
+ .command('get <key>')
361
+ .alias('g')
362
+ .description('Retrieve a memory entry')
363
+ .action((key) => { memoryCommand('get', { key }); });
364
+
365
+ memCmd
366
+ .command('list')
367
+ .alias('ls')
368
+ .description('List all memories')
369
+ .action(() => { memoryCommand('list'); });
370
+
371
+ memCmd
372
+ .command('search <query>')
373
+ .alias('sr')
374
+ .description('Search memories by keyword')
375
+ .action((query) => { memoryCommand('search', { query }); });
376
+
377
+ memCmd
378
+ .command('delete <key>')
379
+ .alias('d')
380
+ .description('Delete a memory entry')
381
+ .action((key) => { memoryCommand('delete', { key }); });
382
+
383
+ memCmd
384
+ .command('clear')
385
+ .alias('cl')
386
+ .description('Clear all memories')
387
+ .action(() => { memoryCommand('clear'); });
388
+
389
+ // ── guide ─────────────────────────────────────────────────────
390
+ program
391
+ .command('guide')
392
+ .alias('g')
393
+ .description('Show quickstart guide, architecture, and command reference')
394
+ .option('-f, --flow', 'show architecture & flow diagram only')
395
+ .option('-c, --commands', 'show command reference only')
396
+ .action((options) => {
397
+ guideCommand(options);
398
+ });
399
+
400
+ // ── telemetry feedback helper ─────────────────────────────────
401
+ function showTelResult(result, label) {
402
+ try {
403
+ if (!result) return;
404
+ if (result.ok) {
405
+ console.log(chalk.green(` [tel] ✓ ${label} logged`));
406
+ } else if (result.reason === 'disabled') {
407
+ console.log(chalk.yellow(` [tel] skipped telemetry is disabled (run 'aiflow telemetry enable')`));
408
+ } else if (result.reason === 'opted-out') {
409
+ console.log(chalk.yellow(` [tel] skipped — repo has .aiflow/no-telemetry opt-out`));
410
+ } else if (result.reason === 'no-url') {
411
+ console.log(chalk.yellow(` [tel] ⚠ skipped — no server URL configured (run 'aiflow telemetry enable')`));
412
+ } else if (result.error) {
413
+ console.log(chalk.yellow(` [tel] error logging '${label}': ${result.error}`));
414
+ }
415
+ } catch (e) { /* never block main flow */ }
416
+ }
417
+
418
+ // ── gate (telemetry for gate workflow events) ─────────────────
419
+ program
420
+ .command('gate <number> <action>')
421
+ .description('Log a gate workflow event (start|approved) — called by AI during gate workflow')
422
+ .option('--ticket <id>', 'ticket ID')
423
+ .option('--ai-tool <tool>', 'AI tool name')
424
+ .action((number, action, options) => {
425
+ const gateNum = parseInt(number);
426
+ if (isNaN(gateNum) || gateNum < 1 || gateNum > 5) return;
427
+ if (!['start', 'approved'].includes(action)) return;
428
+
429
+ const ticketId = options.ticket || '';
430
+
431
+ const telResult = record(`gate.${action}`, {
432
+ gate: gateNum,
433
+ ticket_id: ticketId,
434
+ command: `gate${gateNum}.${action}`,
435
+ ai_tool: options.aiTool || ''
436
+ });
437
+ showTelResult(telResult, `gate${gateNum}.${action}`);
438
+
439
+ // Keep task-state.json currentGate in sync (silent — never blocks the AI)
440
+ if (ticketId) {
441
+ updateTaskGateState(ticketId, gateNum, action).catch(() => {});
442
+ }
443
+
444
+ if (action === 'approved') {
445
+ console.log(chalk.cyan(`\n Gate ${gateNum} approved!`));
446
+ console.log(chalk.yellow(` Pro-Tip: To avoid context pollution, it's recommended to start a fresh chat session.`));
447
+ console.log(chalk.gray(` Run: aiflow task next${ticketId ? ` --ticket ${ticketId}` : ''} to save progress,`));
448
+ console.log(chalk.gray(` then open a NEW chatbox and type "continue".\n`));
449
+ }
450
+ });
451
+
452
+ // ── remove ────────────────────────────────────────────────────
453
+ program
454
+ .command('remove')
455
+ .alias('rm')
456
+ .description('Remove ai-flow-kit from project, a cached version, or uninstall globally')
457
+ .option('--version <ver>', 'remove a specific cached version from .aiflow/versions/')
458
+ .option('-g, --global', 'uninstall the global npm package (removes `aiflow` command)')
459
+ .action((options) => {
460
+ removeCommand(options);
461
+ });
462
+
463
+ // ── update ────────────────────────────────────────────────────
464
+ program
465
+ .command('update')
466
+ .alias('up')
467
+ .description('Update to latest version')
468
+ .option('-f, --force', 'force update even if already on latest')
469
+ .action((options) => {
470
+ updateCommand(options);
471
+ });
472
+
473
+ // ── sync-skills ───────────────────────────────────────────────
474
+ program
475
+ .command('sync-skills')
476
+ .alias('sync')
477
+ .description('Manually synchronize AI Instruction files with local custom skills')
478
+ .action(async () => {
479
+ const projectDir = process.cwd();
480
+ const { setupFramework, AI_TOOL_FILES, ensureAiflowGitignored } = require('../scripts/init');
481
+ const fs = require('fs-extra');
482
+ const path = require('path');
483
+ const chalk = require('chalk');
484
+
485
+ const stateFile = path.join(projectDir, '.aiflow', 'state.json');
486
+ if (!(await fs.pathExists(stateFile))) {
487
+ console.log(chalk.red('Project is not initialized. Please run `aiflow init` first.'));
488
+ return;
489
+ }
490
+
491
+ const state = await fs.readJson(stateFile);
492
+ const frameworks = state.frameworks || [];
493
+ const selectedTools = state.aiTools || Object.keys(AI_TOOL_FILES);
494
+
495
+ console.log(chalk.cyan('⟳ Syncing AI Instruction files...'));
496
+ for (const fw of frameworks) {
497
+ await setupFramework(projectDir, fw, frameworks.length > 1, selectedTools);
498
+ }
499
+ await ensureAiflowGitignored(projectDir);
500
+ console.log(chalk.green('✨ Sync completed!'));
501
+ });
502
+
503
+ // ── doctor ────────────────────────────────────────────────────
504
+ program
505
+ .command('doctor')
506
+ .alias('dr')
507
+ .description('Health check of AI Flow Kit setup')
508
+ .option('-t, --ticket <id>', 'show token breakdown for specific ticket')
509
+ .option('-v, --verbose', 'detailed output')
510
+ .action((opts) => {
511
+ doctorCommand({ ticket: opts.ticket, verbose: opts.verbose });
512
+ });
513
+
514
+ // ── telemetry ──────────────────────────────────────────────────
515
+ program
516
+ .command('telemetry [action]')
517
+ .alias('tel')
518
+ .description('Manage telemetry logging (enable|disable|status|log)')
519
+ .option('--event <event>', 'event name (for log action)')
520
+ .option('--detail <detail>', 'detail string (for log action)')
521
+ .action((action, options) => {
522
+ telemetryCommand(action || 'status', options);
523
+ });
524
+
525
+ program.parse(process.argv);