cli4ai 1.0.4 → 1.1.1

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.
@@ -4,7 +4,10 @@
4
4
 
5
5
  import { readFileSync } from 'fs';
6
6
  import { spawn } from 'child_process';
7
+ import { parse as parseYaml } from 'yaml';
7
8
  import { executeTool, ExecuteToolError } from './execute.js';
9
+ import { remoteRunTool, RemoteConnectionError, RemoteApiError } from './remote-client.js';
10
+ import { getRemote } from './remotes.js';
8
11
 
9
12
  export class RoutineParseError extends Error {
10
13
  constructor(
@@ -78,6 +81,8 @@ export interface RoutineC4aiStep extends RoutineBaseStep {
78
81
  env?: Record<string, string>;
79
82
  stdin?: string;
80
83
  capture?: StepCapture;
84
+ /** Name of a configured remote to execute on (optional) */
85
+ remote?: string;
81
86
  }
82
87
 
83
88
  export interface RoutineSetStep extends RoutineBaseStep {
@@ -153,11 +158,13 @@ export function loadRoutineDefinition(path: string): RoutineDefinition {
153
158
  throw new RoutineParseError(path, `Failed to read: ${err instanceof Error ? err.message : String(err)}`);
154
159
  }
155
160
 
161
+ const isYaml = path.endsWith('.yaml') || path.endsWith('.yml');
156
162
  let data: unknown;
157
163
  try {
158
- data = JSON.parse(content);
159
- } catch {
160
- throw new RoutineParseError(path, 'Invalid JSON');
164
+ data = isYaml ? parseYaml(content) : JSON.parse(content);
165
+ } catch (err) {
166
+ const format = isYaml ? 'YAML' : 'JSON';
167
+ throw new RoutineParseError(path, `Invalid ${format}: ${err instanceof Error ? err.message : String(err)}`);
161
168
  }
162
169
 
163
170
  return validateRoutineDefinition(data, path);
@@ -318,6 +325,9 @@ function validateRoutineDefinition(value: unknown, source?: string): RoutineDefi
318
325
  if (s.capture !== undefined && !['inherit', 'text', 'json'].includes(String(s.capture))) {
319
326
  throw new RoutineValidationError(`Step "${s.id}" has invalid "capture"`, { source, id: s.id, got: s.capture });
320
327
  }
328
+ if (s.remote !== undefined && typeof s.remote !== 'string') {
329
+ throw new RoutineValidationError(`Step "${s.id}" has invalid "remote" (must be string)`, { source, id: s.id, got: s.remote });
330
+ }
321
331
  }
322
332
 
323
333
  if (s.type === 'set') {
@@ -718,39 +728,91 @@ export async function runRoutine(def: RoutineDefinition, vars: Record<string, st
718
728
  const args = renderStringArray(step.args, ctx, { stepId: step.id, field: 'args' });
719
729
  const env = renderEnv(step.env, ctx, { stepId: step.id, field: 'env' });
720
730
  const stdin = step.stdin !== undefined ? renderString(step.stdin, ctx, { stepId: step.id, field: 'stdin' }) : undefined;
731
+ const remoteName = step.remote !== undefined ? renderScalarString(step.remote, ctx, { stepId: step.id, field: 'remote' }) : undefined;
721
732
 
722
733
  const capture = step.capture ?? 'text';
723
734
 
724
735
  try {
725
- const execRes = await executeTool({
726
- packageName: pkg,
727
- command: cmd,
728
- args,
729
- cwd: invocationDir,
730
- env,
731
- stdin,
732
- capture: 'pipe',
733
- timeoutMs: step.timeout,
734
- teeStderr: true
735
- });
736
+ let execResult: { exitCode: number; durationMs: number; stdout?: string; stderr?: string };
737
+
738
+ if (remoteName) {
739
+ // Remote execution
740
+ const remote = getRemote(remoteName);
741
+ if (!remote) {
742
+ throw new ExecuteToolError('NOT_FOUND', `Remote "${remoteName}" not found`, {
743
+ step: step.id,
744
+ hint: 'Use "cli4ai remotes add <name> <url>" to configure a remote'
745
+ });
746
+ }
747
+
748
+ const remoteRes = await remoteRunTool(remoteName, {
749
+ package: pkg,
750
+ command: cmd,
751
+ args,
752
+ env: Object.keys(env).length > 0 ? env : undefined,
753
+ stdin,
754
+ timeout: step.timeout
755
+ });
756
+
757
+ execResult = {
758
+ exitCode: remoteRes.exitCode,
759
+ durationMs: remoteRes.durationMs,
760
+ stdout: remoteRes.stdout,
761
+ stderr: remoteRes.stderr
762
+ };
763
+
764
+ // Log stderr from remote
765
+ if (remoteRes.stderr) {
766
+ process.stderr.write(remoteRes.stderr);
767
+ }
768
+
769
+ // Handle remote-level errors
770
+ if (remoteRes.error && !remoteRes.success) {
771
+ throw new ExecuteToolError(
772
+ remoteRes.error.code,
773
+ remoteRes.error.message,
774
+ remoteRes.error.details
775
+ );
776
+ }
777
+ } else {
778
+ // Local execution
779
+ const localRes = await executeTool({
780
+ packageName: pkg,
781
+ command: cmd,
782
+ args,
783
+ cwd: invocationDir,
784
+ env,
785
+ stdin,
786
+ capture: 'pipe',
787
+ timeoutMs: step.timeout,
788
+ teeStderr: true
789
+ });
790
+
791
+ execResult = {
792
+ exitCode: localRes.exitCode,
793
+ durationMs: localRes.durationMs,
794
+ stdout: localRes.stdout,
795
+ stderr: localRes.stderr
796
+ };
797
+ }
736
798
 
737
799
  const res: StepRunResult = {
738
800
  id: step.id,
739
801
  type: step.type,
740
- status: execRes.exitCode === 0 ? 'success' : 'failed',
741
- exitCode: execRes.exitCode,
742
- durationMs: execRes.durationMs,
743
- stdout: execRes.stdout,
744
- stderr: execRes.stderr
802
+ status: execResult.exitCode === 0 ? 'success' : 'failed',
803
+ exitCode: execResult.exitCode,
804
+ durationMs: execResult.durationMs,
805
+ stdout: execResult.stdout,
806
+ stderr: execResult.stderr
745
807
  };
746
808
 
747
809
  if (capture === 'json') {
748
810
  try {
749
- res.json = execRes.stdout ? JSON.parse(execRes.stdout) : null;
811
+ res.json = execResult.stdout ? JSON.parse(execResult.stdout) : null;
750
812
  } catch {
751
813
  throw new RoutineTemplateError(`JSON parse error in step "${step.id}": stdout is not valid JSON`, {
752
814
  step: step.id,
753
- stdout: (execRes.stdout ?? '').slice(0, 200)
815
+ stdout: (execResult.stdout ?? '').slice(0, 200)
754
816
  });
755
817
  }
756
818
  }
@@ -758,8 +820,8 @@ export async function runRoutine(def: RoutineDefinition, vars: Record<string, st
758
820
  stepsById[step.id] = res;
759
821
  steps.push(res);
760
822
 
761
- if (execRes.exitCode !== 0 && !step.continueOnError) {
762
- return finalizeFailure(def, ctxVars, steps, Date.now() - startTime, execRes.exitCode);
823
+ if (execResult.exitCode !== 0 && !step.continueOnError) {
824
+ return finalizeFailure(def, ctxVars, steps, Date.now() - startTime, execResult.exitCode);
763
825
  }
764
826
  continue;
765
827
  } catch (err) {
@@ -799,6 +861,12 @@ function normalizeError(err: unknown): { code: string; message: string; details?
799
861
  if (err instanceof ExecuteToolError) {
800
862
  return { code: err.code, message: err.message, details: err.details };
801
863
  }
864
+ if (err instanceof RemoteConnectionError) {
865
+ return { code: 'NETWORK_ERROR', message: err.message, details: { remote: err.remoteName, url: err.url } };
866
+ }
867
+ if (err instanceof RemoteApiError) {
868
+ return { code: err.code, message: err.message, details: err.details };
869
+ }
802
870
  if (err instanceof RoutineTemplateError) {
803
871
  return { code: 'INVALID_INPUT', message: err.message, details: err.details };
804
872
  }
@@ -7,15 +7,16 @@
7
7
  *
8
8
  * Resolution order:
9
9
  * - local before global (unless globalOnly)
10
- * - within a scope: .routine.json before .routine.sh
10
+ * - within a scope: .routine.yaml > .routine.yml > .routine.json > .routine.sh
11
11
  */
12
12
 
13
13
  import { existsSync, readdirSync, statSync, readFileSync } from 'fs';
14
14
  import { resolve } from 'path';
15
+ import { parse as parseYaml } from 'yaml';
15
16
  import { ensureCli4aiHome, ensureLocalDir, ROUTINES_DIR, LOCAL_ROUTINES_DIR } from './config.js';
16
17
  import { validateScheduleConfig, type RoutineSchedule } from './routine-engine.js';
17
18
 
18
- export type RoutineKind = 'json' | 'bash';
19
+ export type RoutineKind = 'yaml' | 'json' | 'bash';
19
20
  export type RoutineScope = 'local' | 'global';
20
21
 
21
22
  export interface RoutineInfo {
@@ -30,6 +31,8 @@ export interface ResolveRoutineOptions {
30
31
  }
31
32
 
32
33
  const ROUTINE_FILES = [
34
+ { kind: 'yaml' as const, suffix: '.routine.yaml' },
35
+ { kind: 'yaml' as const, suffix: '.routine.yml' },
33
36
  { kind: 'json' as const, suffix: '.routine.json' },
34
37
  { kind: 'bash' as const, suffix: '.routine.sh' }
35
38
  ] as const;
@@ -127,13 +130,13 @@ export function getScheduledRoutines(projectDir?: string): ScheduledRoutineInfo[
127
130
  const results: ScheduledRoutineInfo[] = [];
128
131
  const seen = new Set<string>();
129
132
 
130
- // Collect all JSON routines
133
+ // Collect all structured routines (YAML and JSON - bash scripts cannot have schedules)
131
134
  const allRoutines: RoutineInfo[] = [];
132
135
 
133
136
  if (projectDir) {
134
- allRoutines.push(...getLocalRoutines(projectDir).filter(r => r.kind === 'json'));
137
+ allRoutines.push(...getLocalRoutines(projectDir).filter(r => r.kind === 'yaml' || r.kind === 'json'));
135
138
  }
136
- allRoutines.push(...getGlobalRoutines().filter(r => r.kind === 'json'));
139
+ allRoutines.push(...getGlobalRoutines().filter(r => r.kind === 'yaml' || r.kind === 'json'));
137
140
 
138
141
  for (const routine of allRoutines) {
139
142
  // Skip if we've already processed a routine with this name (local takes precedence)
@@ -142,7 +145,7 @@ export function getScheduledRoutines(projectDir?: string): ScheduledRoutineInfo[
142
145
 
143
146
  try {
144
147
  const content = readFileSync(routine.path, 'utf-8');
145
- const data = JSON.parse(content);
148
+ const data = routine.kind === 'yaml' ? parseYaml(content) : JSON.parse(content);
146
149
 
147
150
  if (data.schedule && typeof data.schedule === 'object') {
148
151
  // Check if schedule is enabled (defaults to true)
@@ -11,7 +11,7 @@
11
11
  * └── scheduler.log # Daemon logs
12
12
  */
13
13
 
14
- import { readFileSync, writeFileSync, existsSync, mkdirSync, unlinkSync, readdirSync } from 'fs';
14
+ import { readFileSync, writeFileSync, existsSync, mkdirSync, unlinkSync, readdirSync, appendFileSync } from 'fs';
15
15
  import { resolve, join } from 'path';
16
16
  import parser from 'cron-parser';
17
17
  import { SCHEDULER_DIR, ensureCli4aiHome } from './config.js';
@@ -282,7 +282,7 @@ export function appendSchedulerLog(level: LogLevel, message: string): void {
282
282
  ensureSchedulerDirs();
283
283
  const timestamp = new Date().toISOString();
284
284
  const line = `[${timestamp}] [${level.toUpperCase()}] ${message}\n`;
285
- const fd = require('fs').appendFileSync(SCHEDULER_LOG_FILE, line);
285
+ appendFileSync(SCHEDULER_LOG_FILE, line);
286
286
  }
287
287
 
288
288
  // ═══════════════════════════════════════════════════════════════════════════
package/src/lib/cli.ts CHANGED
@@ -4,9 +4,16 @@
4
4
  */
5
5
 
6
6
  import { Command } from 'commander';
7
+ import { readFileSync } from 'fs';
8
+ import { dirname, join } from 'path';
9
+ import { fileURLToPath } from 'url';
10
+
11
+ const __dirname = dirname(fileURLToPath(import.meta.url));
12
+ const pkgPath = join(__dirname, '..', '..', 'package.json');
13
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
7
14
 
8
15
  export const BRAND = 'cli4ai - cli4ai.com';
9
- export const VERSION = '0.9.2';
16
+ export const VERSION = pkg.version;
10
17
 
11
18
  // ═══════════════════════════════════════════════════════════════════════════
12
19
  // TYPES