@stan-chen/simple-cli 0.2.6 → 0.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.
@@ -0,0 +1,110 @@
1
+ import { spawn, exec } from 'child_process';
2
+ import { mkdir, writeFile, readFile, readdir } from 'fs/promises';
3
+ import { existsSync } from 'fs';
4
+ import { join } from 'path';
5
+ import { promisify } from 'util';
6
+ const execAsync = promisify(exec);
7
+ export class AsyncTaskManager {
8
+ static instance;
9
+ tasksDir;
10
+ constructor(cwd) {
11
+ this.tasksDir = join(cwd, '.agent', 'tasks');
12
+ }
13
+ static getInstance(cwd = process.cwd()) {
14
+ if (!AsyncTaskManager.instance) {
15
+ AsyncTaskManager.instance = new AsyncTaskManager(cwd);
16
+ }
17
+ return AsyncTaskManager.instance;
18
+ }
19
+ async init() {
20
+ if (!existsSync(this.tasksDir)) {
21
+ await mkdir(this.tasksDir, { recursive: true });
22
+ }
23
+ }
24
+ async startTask(command, args, env = {}) {
25
+ await this.init();
26
+ const id = `task-${Date.now()}-${Math.floor(Math.random() * 1000)}`;
27
+ const logFile = join(this.tasksDir, `${id}.log`);
28
+ const metaFile = join(this.tasksDir, `${id}.json`);
29
+ // Prepare Task Metadata
30
+ const taskCmd = `${command} ${args.join(' ')}`;
31
+ // Spawn detached process
32
+ const out = await import('fs').then(fs => fs.openSync(logFile, 'a'));
33
+ const err = await import('fs').then(fs => fs.openSync(logFile, 'a'));
34
+ const child = spawn(command, args, {
35
+ detached: true,
36
+ stdio: ['ignore', out, err],
37
+ env: { ...process.env, ...env }
38
+ });
39
+ child.unref();
40
+ const task = {
41
+ id,
42
+ command: taskCmd,
43
+ startTime: Date.now(),
44
+ pid: child.pid,
45
+ logFile,
46
+ status: 'running'
47
+ };
48
+ await writeFile(metaFile, JSON.stringify(task, null, 2));
49
+ return id;
50
+ }
51
+ async getTaskStatus(id) {
52
+ await this.init();
53
+ const metaFile = join(this.tasksDir, `${id}.json`);
54
+ if (!existsSync(metaFile)) {
55
+ throw new Error(`Task ${id} not found`);
56
+ }
57
+ const task = JSON.parse(await readFile(metaFile, 'utf-8'));
58
+ // Check if process is still running
59
+ if (task.status === 'running') {
60
+ try {
61
+ // process.kill(pid, 0) checks if process exists
62
+ process.kill(task.pid, 0);
63
+ }
64
+ catch (e) {
65
+ // Process not found, it must have finished
66
+ task.status = 'completed'; // We act optimistic, need to check exit code?
67
+ // Actually, we can't easily check exit code of detached process without waiting for it
68
+ // Ideally, we'd check the log file for success indicators or have the process write its exit code.
69
+ // For now, let's check log file for "SUCCESS" or "FAILURE" if possible,
70
+ // or just mark as 'completed' (meaning finished execution).
71
+ const logs = await this.getTaskLogs(id, 1000); // Check last lines
72
+ if (logs.includes('FAILURE') || logs.includes('Error:')) {
73
+ task.status = 'failed';
74
+ }
75
+ else if (logs.includes('SUCCESS') || logs.includes('PR Created:')) {
76
+ task.status = 'completed';
77
+ }
78
+ else {
79
+ task.status = 'completed'; // Assumed success
80
+ }
81
+ await writeFile(metaFile, JSON.stringify(task, null, 2));
82
+ }
83
+ }
84
+ return task;
85
+ }
86
+ async getTaskLogs(id, lines = 20) {
87
+ await this.init();
88
+ const logFile = join(this.tasksDir, `${id}.log`);
89
+ if (!existsSync(logFile))
90
+ return '';
91
+ const content = await readFile(logFile, 'utf-8');
92
+ const allLines = content.split('\n');
93
+ return allLines.slice(-lines).join('\n');
94
+ }
95
+ async listTasks() {
96
+ await this.init();
97
+ const files = await readdir(this.tasksDir);
98
+ const tasks = [];
99
+ for (const file of files) {
100
+ if (file.endsWith('.json')) {
101
+ try {
102
+ const content = await readFile(join(this.tasksDir, file), 'utf-8');
103
+ tasks.push(JSON.parse(content));
104
+ }
105
+ catch { }
106
+ }
107
+ }
108
+ return tasks.sort((a, b) => b.startTime - a.startTime);
109
+ }
110
+ }
@@ -33,25 +33,25 @@ export declare const writeFiles: {
33
33
  search: z.ZodString;
34
34
  replace: z.ZodString;
35
35
  }, "strip", z.ZodTypeAny, {
36
- search: string;
37
36
  replace: string;
38
- }, {
39
37
  search: string;
38
+ }, {
40
39
  replace: string;
40
+ search: string;
41
41
  }>, "many">>;
42
42
  }, "strip", z.ZodTypeAny, {
43
43
  path: string;
44
44
  content?: string | undefined;
45
45
  searchReplace?: {
46
- search: string;
47
46
  replace: string;
47
+ search: string;
48
48
  }[] | undefined;
49
49
  }, {
50
50
  path: string;
51
51
  content?: string | undefined;
52
52
  searchReplace?: {
53
- search: string;
54
53
  replace: string;
54
+ search: string;
55
55
  }[] | undefined;
56
56
  }>, "many">;
57
57
  }, "strip", z.ZodTypeAny, {
@@ -59,8 +59,8 @@ export declare const writeFiles: {
59
59
  path: string;
60
60
  content?: string | undefined;
61
61
  searchReplace?: {
62
- search: string;
63
62
  replace: string;
63
+ search: string;
64
64
  }[] | undefined;
65
65
  }[];
66
66
  }, {
@@ -68,8 +68,8 @@ export declare const writeFiles: {
68
68
  path: string;
69
69
  content?: string | undefined;
70
70
  searchReplace?: {
71
- search: string;
72
71
  replace: string;
72
+ search: string;
73
73
  }[] | undefined;
74
74
  }[];
75
75
  }>;
@@ -159,15 +159,15 @@ export declare const listFiles: {
159
159
  includeDirectories: z.ZodDefault<z.ZodBoolean>;
160
160
  maxResults: z.ZodOptional<z.ZodNumber>;
161
161
  }, "strip", z.ZodTypeAny, {
162
+ ignore: string[];
162
163
  path: string;
163
164
  pattern: string;
164
- ignore: string[];
165
165
  includeDirectories: boolean;
166
166
  maxResults?: number | undefined;
167
167
  }, {
168
168
  pattern: string;
169
- path?: string | undefined;
170
169
  ignore?: string[] | undefined;
170
+ path?: string | undefined;
171
171
  includeDirectories?: boolean | undefined;
172
172
  maxResults?: number | undefined;
173
173
  }>;
@@ -178,9 +178,15 @@ export declare const listFiles: {
178
178
  includeDirectories: boolean;
179
179
  maxResults?: number;
180
180
  }) => Promise<{
181
+ matches: never[];
182
+ count: number;
183
+ truncated: boolean;
184
+ error: string;
185
+ } | {
181
186
  matches: string[];
182
187
  count: number;
183
188
  truncated: boolean;
189
+ error?: undefined;
184
190
  }>;
185
191
  };
186
192
  export declare const searchFiles: {
@@ -222,9 +228,15 @@ export declare const searchFiles: {
222
228
  }) => Promise<{
223
229
  matches: never[];
224
230
  count: number;
231
+ truncated: boolean;
225
232
  error: string;
226
233
  files?: undefined;
234
+ } | {
235
+ matches: never[];
236
+ count: number;
237
+ error: string;
227
238
  truncated?: undefined;
239
+ files?: undefined;
228
240
  } | {
229
241
  matches: any[];
230
242
  count: number;
@@ -245,7 +257,7 @@ export declare const listDir: {
245
257
  }>;
246
258
  execute: ({ path }: {
247
259
  path: string;
248
- }) => Promise<{
260
+ }) => Promise<"Access denied: Path is outside the allowed workspace." | {
249
261
  name: string;
250
262
  isDir: boolean;
251
263
  }[]>;
@@ -402,6 +414,152 @@ export declare const linter: {
402
414
  export declare const getCurrentBranch: (cwd: string) => Promise<string | null>;
403
415
  export declare const getChangedFiles: (cwd: string) => Promise<string[]>;
404
416
  export declare const getTrackedFiles: (cwd: string) => Promise<string[]>;
417
+ export declare const delegate_cli: {
418
+ name: string;
419
+ description: string;
420
+ inputSchema: z.ZodObject<{
421
+ cli: z.ZodString;
422
+ task: z.ZodString;
423
+ context_files: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
424
+ async: z.ZodDefault<z.ZodBoolean>;
425
+ }, "strip", z.ZodTypeAny, {
426
+ cli: string;
427
+ task: string;
428
+ async: boolean;
429
+ context_files?: string[] | undefined;
430
+ }, {
431
+ cli: string;
432
+ task: string;
433
+ context_files?: string[] | undefined;
434
+ async?: boolean | undefined;
435
+ }>;
436
+ execute: ({ cli, task, context_files, async }: {
437
+ cli: string;
438
+ task: string;
439
+ context_files?: string[];
440
+ async: boolean;
441
+ }) => Promise<unknown>;
442
+ };
443
+ export declare const schedule_task: {
444
+ name: string;
445
+ description: string;
446
+ inputSchema: z.ZodObject<{
447
+ cron: z.ZodString;
448
+ prompt: z.ZodString;
449
+ description: z.ZodString;
450
+ }, "strip", z.ZodTypeAny, {
451
+ description: string;
452
+ cron: string;
453
+ prompt: string;
454
+ }, {
455
+ description: string;
456
+ cron: string;
457
+ prompt: string;
458
+ }>;
459
+ execute: ({ cron, prompt, description }: {
460
+ cron: string;
461
+ prompt: string;
462
+ description: string;
463
+ }) => Promise<string>;
464
+ };
465
+ export declare const pr_list: {
466
+ name: string;
467
+ description: string;
468
+ inputSchema: z.ZodObject<{
469
+ limit: z.ZodDefault<z.ZodNumber>;
470
+ }, "strip", z.ZodTypeAny, {
471
+ limit: number;
472
+ }, {
473
+ limit?: number | undefined;
474
+ }>;
475
+ execute: ({ limit }: {
476
+ limit: number;
477
+ }) => Promise<string>;
478
+ };
479
+ export declare const pr_review: {
480
+ name: string;
481
+ description: string;
482
+ inputSchema: z.ZodObject<{
483
+ pr_number: z.ZodNumber;
484
+ }, "strip", z.ZodTypeAny, {
485
+ pr_number: number;
486
+ }, {
487
+ pr_number: number;
488
+ }>;
489
+ execute: ({ pr_number }: {
490
+ pr_number: number;
491
+ }) => Promise<string>;
492
+ };
493
+ export declare const pr_comment: {
494
+ name: string;
495
+ description: string;
496
+ inputSchema: z.ZodObject<{
497
+ pr_number: z.ZodNumber;
498
+ body: z.ZodString;
499
+ }, "strip", z.ZodTypeAny, {
500
+ pr_number: number;
501
+ body: string;
502
+ }, {
503
+ pr_number: number;
504
+ body: string;
505
+ }>;
506
+ execute: ({ pr_number, body }: {
507
+ pr_number: number;
508
+ body: string;
509
+ }) => Promise<string>;
510
+ };
511
+ export declare const pr_ready: {
512
+ name: string;
513
+ description: string;
514
+ inputSchema: z.ZodObject<{
515
+ pr_number: z.ZodNumber;
516
+ }, "strip", z.ZodTypeAny, {
517
+ pr_number: number;
518
+ }, {
519
+ pr_number: number;
520
+ }>;
521
+ execute: ({ pr_number }: {
522
+ pr_number: number;
523
+ }) => Promise<string>;
524
+ };
525
+ export declare const pr_merge: {
526
+ name: string;
527
+ description: string;
528
+ inputSchema: z.ZodObject<{
529
+ pr_number: z.ZodNumber;
530
+ method: z.ZodDefault<z.ZodEnum<["merge", "squash", "rebase"]>>;
531
+ }, "strip", z.ZodTypeAny, {
532
+ pr_number: number;
533
+ method: "merge" | "squash" | "rebase";
534
+ }, {
535
+ pr_number: number;
536
+ method?: "merge" | "squash" | "rebase" | undefined;
537
+ }>;
538
+ execute: ({ pr_number, method }: {
539
+ pr_number: number;
540
+ method: string;
541
+ }) => Promise<string>;
542
+ };
543
+ export declare const check_task_status: {
544
+ name: string;
545
+ description: string;
546
+ inputSchema: z.ZodObject<{
547
+ id: z.ZodString;
548
+ }, "strip", z.ZodTypeAny, {
549
+ id: string;
550
+ }, {
551
+ id: string;
552
+ }>;
553
+ execute: ({ id }: {
554
+ id: string;
555
+ }) => Promise<string>;
556
+ };
557
+ export declare const list_bg_tasks: {
558
+ name: string;
559
+ description: string;
560
+ inputSchema: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
561
+ execute: () => Promise<string>;
562
+ };
405
563
  export declare const allBuiltins: ({
406
564
  name: string;
407
565
  description: string;
@@ -434,25 +592,25 @@ export declare const allBuiltins: ({
434
592
  search: z.ZodString;
435
593
  replace: z.ZodString;
436
594
  }, "strip", z.ZodTypeAny, {
437
- search: string;
438
595
  replace: string;
439
- }, {
440
596
  search: string;
597
+ }, {
441
598
  replace: string;
599
+ search: string;
442
600
  }>, "many">>;
443
601
  }, "strip", z.ZodTypeAny, {
444
602
  path: string;
445
603
  content?: string | undefined;
446
604
  searchReplace?: {
447
- search: string;
448
605
  replace: string;
606
+ search: string;
449
607
  }[] | undefined;
450
608
  }, {
451
609
  path: string;
452
610
  content?: string | undefined;
453
611
  searchReplace?: {
454
- search: string;
455
612
  replace: string;
613
+ search: string;
456
614
  }[] | undefined;
457
615
  }>, "many">;
458
616
  }, "strip", z.ZodTypeAny, {
@@ -460,8 +618,8 @@ export declare const allBuiltins: ({
460
618
  path: string;
461
619
  content?: string | undefined;
462
620
  searchReplace?: {
463
- search: string;
464
621
  replace: string;
622
+ search: string;
465
623
  }[] | undefined;
466
624
  }[];
467
625
  }, {
@@ -469,8 +627,8 @@ export declare const allBuiltins: ({
469
627
  path: string;
470
628
  content?: string | undefined;
471
629
  searchReplace?: {
472
- search: string;
473
630
  replace: string;
631
+ search: string;
474
632
  }[] | undefined;
475
633
  }[];
476
634
  }>;
@@ -557,15 +715,15 @@ export declare const allBuiltins: ({
557
715
  includeDirectories: z.ZodDefault<z.ZodBoolean>;
558
716
  maxResults: z.ZodOptional<z.ZodNumber>;
559
717
  }, "strip", z.ZodTypeAny, {
718
+ ignore: string[];
560
719
  path: string;
561
720
  pattern: string;
562
- ignore: string[];
563
721
  includeDirectories: boolean;
564
722
  maxResults?: number | undefined;
565
723
  }, {
566
724
  pattern: string;
567
- path?: string | undefined;
568
725
  ignore?: string[] | undefined;
726
+ path?: string | undefined;
569
727
  includeDirectories?: boolean | undefined;
570
728
  maxResults?: number | undefined;
571
729
  }>;
@@ -576,9 +734,15 @@ export declare const allBuiltins: ({
576
734
  includeDirectories: boolean;
577
735
  maxResults?: number;
578
736
  }) => Promise<{
737
+ matches: never[];
738
+ count: number;
739
+ truncated: boolean;
740
+ error: string;
741
+ } | {
579
742
  matches: string[];
580
743
  count: number;
581
744
  truncated: boolean;
745
+ error?: undefined;
582
746
  }>;
583
747
  } | {
584
748
  name: string;
@@ -619,9 +783,15 @@ export declare const allBuiltins: ({
619
783
  }) => Promise<{
620
784
  matches: never[];
621
785
  count: number;
786
+ truncated: boolean;
622
787
  error: string;
623
788
  files?: undefined;
789
+ } | {
790
+ matches: never[];
791
+ count: number;
792
+ error: string;
624
793
  truncated?: undefined;
794
+ files?: undefined;
625
795
  } | {
626
796
  matches: any[];
627
797
  count: number;
@@ -641,7 +811,7 @@ export declare const allBuiltins: ({
641
811
  }>;
642
812
  execute: ({ path }: {
643
813
  path: string;
644
- }) => Promise<{
814
+ }) => Promise<"Access denied: Path is outside the allowed workspace." | {
645
815
  name: string;
646
816
  isDir: boolean;
647
817
  }[]>;
@@ -789,4 +959,128 @@ export declare const allBuiltins: ({
789
959
  output: any;
790
960
  file: string;
791
961
  }>;
962
+ } | {
963
+ name: string;
964
+ description: string;
965
+ inputSchema: z.ZodObject<{
966
+ cli: z.ZodString;
967
+ task: z.ZodString;
968
+ context_files: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
969
+ async: z.ZodDefault<z.ZodBoolean>;
970
+ }, "strip", z.ZodTypeAny, {
971
+ cli: string;
972
+ task: string;
973
+ async: boolean;
974
+ context_files?: string[] | undefined;
975
+ }, {
976
+ cli: string;
977
+ task: string;
978
+ context_files?: string[] | undefined;
979
+ async?: boolean | undefined;
980
+ }>;
981
+ execute: ({ cli, task, context_files, async }: {
982
+ cli: string;
983
+ task: string;
984
+ context_files?: string[];
985
+ async: boolean;
986
+ }) => Promise<unknown>;
987
+ } | {
988
+ name: string;
989
+ description: string;
990
+ inputSchema: z.ZodObject<{
991
+ cron: z.ZodString;
992
+ prompt: z.ZodString;
993
+ description: z.ZodString;
994
+ }, "strip", z.ZodTypeAny, {
995
+ description: string;
996
+ cron: string;
997
+ prompt: string;
998
+ }, {
999
+ description: string;
1000
+ cron: string;
1001
+ prompt: string;
1002
+ }>;
1003
+ execute: ({ cron, prompt, description }: {
1004
+ cron: string;
1005
+ prompt: string;
1006
+ description: string;
1007
+ }) => Promise<string>;
1008
+ } | {
1009
+ name: string;
1010
+ description: string;
1011
+ inputSchema: z.ZodObject<{
1012
+ limit: z.ZodDefault<z.ZodNumber>;
1013
+ }, "strip", z.ZodTypeAny, {
1014
+ limit: number;
1015
+ }, {
1016
+ limit?: number | undefined;
1017
+ }>;
1018
+ execute: ({ limit }: {
1019
+ limit: number;
1020
+ }) => Promise<string>;
1021
+ } | {
1022
+ name: string;
1023
+ description: string;
1024
+ inputSchema: z.ZodObject<{
1025
+ pr_number: z.ZodNumber;
1026
+ }, "strip", z.ZodTypeAny, {
1027
+ pr_number: number;
1028
+ }, {
1029
+ pr_number: number;
1030
+ }>;
1031
+ execute: ({ pr_number }: {
1032
+ pr_number: number;
1033
+ }) => Promise<string>;
1034
+ } | {
1035
+ name: string;
1036
+ description: string;
1037
+ inputSchema: z.ZodObject<{
1038
+ pr_number: z.ZodNumber;
1039
+ body: z.ZodString;
1040
+ }, "strip", z.ZodTypeAny, {
1041
+ pr_number: number;
1042
+ body: string;
1043
+ }, {
1044
+ pr_number: number;
1045
+ body: string;
1046
+ }>;
1047
+ execute: ({ pr_number, body }: {
1048
+ pr_number: number;
1049
+ body: string;
1050
+ }) => Promise<string>;
1051
+ } | {
1052
+ name: string;
1053
+ description: string;
1054
+ inputSchema: z.ZodObject<{
1055
+ pr_number: z.ZodNumber;
1056
+ method: z.ZodDefault<z.ZodEnum<["merge", "squash", "rebase"]>>;
1057
+ }, "strip", z.ZodTypeAny, {
1058
+ pr_number: number;
1059
+ method: "merge" | "squash" | "rebase";
1060
+ }, {
1061
+ pr_number: number;
1062
+ method?: "merge" | "squash" | "rebase" | undefined;
1063
+ }>;
1064
+ execute: ({ pr_number, method }: {
1065
+ pr_number: number;
1066
+ method: string;
1067
+ }) => Promise<string>;
1068
+ } | {
1069
+ name: string;
1070
+ description: string;
1071
+ inputSchema: z.ZodObject<{
1072
+ id: z.ZodString;
1073
+ }, "strip", z.ZodTypeAny, {
1074
+ id: string;
1075
+ }, {
1076
+ id: string;
1077
+ }>;
1078
+ execute: ({ id }: {
1079
+ id: string;
1080
+ }) => Promise<string>;
1081
+ } | {
1082
+ name: string;
1083
+ description: string;
1084
+ inputSchema: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
1085
+ execute: () => Promise<string>;
792
1086
  })[];