@sylphx/flow 2.1.2 → 2.1.4

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 (70) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +44 -0
  3. package/package.json +79 -73
  4. package/src/commands/flow/execute-v2.ts +39 -30
  5. package/src/commands/flow/index.ts +2 -4
  6. package/src/commands/flow/prompt.ts +5 -3
  7. package/src/commands/flow/types.ts +0 -9
  8. package/src/commands/flow-command.ts +20 -13
  9. package/src/commands/hook-command.ts +1 -3
  10. package/src/commands/settings-command.ts +36 -33
  11. package/src/config/ai-config.ts +60 -41
  12. package/src/core/agent-loader.ts +11 -6
  13. package/src/core/attach-manager.ts +92 -84
  14. package/src/core/backup-manager.ts +35 -29
  15. package/src/core/cleanup-handler.ts +11 -8
  16. package/src/core/error-handling.ts +23 -30
  17. package/src/core/flow-executor.ts +58 -76
  18. package/src/core/formatting/bytes.ts +2 -4
  19. package/src/core/functional/async.ts +5 -4
  20. package/src/core/functional/error-handler.ts +2 -2
  21. package/src/core/git-stash-manager.ts +21 -10
  22. package/src/core/installers/file-installer.ts +0 -1
  23. package/src/core/installers/mcp-installer.ts +0 -1
  24. package/src/core/project-manager.ts +24 -18
  25. package/src/core/secrets-manager.ts +54 -73
  26. package/src/core/session-manager.ts +20 -22
  27. package/src/core/state-detector.ts +139 -80
  28. package/src/core/template-loader.ts +13 -31
  29. package/src/core/upgrade-manager.ts +122 -69
  30. package/src/index.ts +8 -5
  31. package/src/services/auto-upgrade.ts +1 -1
  32. package/src/services/config-service.ts +41 -29
  33. package/src/services/global-config.ts +2 -2
  34. package/src/services/target-installer.ts +9 -7
  35. package/src/targets/claude-code.ts +28 -15
  36. package/src/targets/opencode.ts +17 -6
  37. package/src/types/cli.types.ts +2 -2
  38. package/src/types/provider.types.ts +1 -7
  39. package/src/types/session.types.ts +11 -11
  40. package/src/types/target.types.ts +3 -1
  41. package/src/types/todo.types.ts +1 -1
  42. package/src/types.ts +1 -1
  43. package/src/utils/__tests__/package-manager-detector.test.ts +6 -6
  44. package/src/utils/agent-enhancer.ts +111 -3
  45. package/src/utils/config/paths.ts +3 -1
  46. package/src/utils/config/target-utils.ts +2 -2
  47. package/src/utils/display/banner.ts +2 -2
  48. package/src/utils/display/notifications.ts +58 -45
  49. package/src/utils/display/status.ts +29 -12
  50. package/src/utils/files/file-operations.ts +1 -1
  51. package/src/utils/files/sync-utils.ts +38 -41
  52. package/src/utils/index.ts +19 -27
  53. package/src/utils/package-manager-detector.ts +15 -5
  54. package/src/utils/security/security.ts +8 -4
  55. package/src/utils/target-selection.ts +5 -2
  56. package/src/utils/version.ts +4 -2
  57. package/src/commands/flow/execute.ts +0 -453
  58. package/src/commands/flow/setup.ts +0 -312
  59. package/src/commands/flow-orchestrator.ts +0 -328
  60. package/src/commands/init-command.ts +0 -92
  61. package/src/commands/init-core.ts +0 -331
  62. package/src/commands/run-command.ts +0 -126
  63. package/src/core/agent-manager.ts +0 -174
  64. package/src/core/loop-controller.ts +0 -200
  65. package/src/core/rule-loader.ts +0 -147
  66. package/src/core/rule-manager.ts +0 -240
  67. package/src/services/claude-config-service.ts +0 -252
  68. package/src/services/first-run-setup.ts +0 -220
  69. package/src/services/smart-config-service.ts +0 -269
  70. package/src/types/api.types.ts +0 -9
@@ -1,174 +0,0 @@
1
- /**
2
- * Agent Manager
3
- * Manages agent state and operations
4
- */
5
-
6
- import type { Agent } from '../types/agent.types.js';
7
- import { loadAllAgents } from './agent-loader.js';
8
- import { DEFAULT_AGENT_ID } from './builtin-agents.js';
9
-
10
- /**
11
- * Agent manager state
12
- */
13
- interface AgentManagerState {
14
- agents: Map<string, Agent>;
15
- cwd: string;
16
- }
17
-
18
- let state: AgentManagerState | null = null;
19
-
20
- /**
21
- * Get the app store (lazy import to avoid circular dependencies)
22
- */
23
- let getAppStore: (() => any) | null = null;
24
-
25
- /**
26
- * Fallback agent when state is not initialized
27
- */
28
- const FALLBACK_AGENT: Agent = {
29
- id: DEFAULT_AGENT_ID,
30
- metadata: {
31
- name: 'Coder',
32
- description: 'Fallback agent (agent manager not initialized)',
33
- },
34
- systemPrompt: 'You are a helpful coding assistant.',
35
- isBuiltin: true,
36
- };
37
-
38
- /**
39
- * Set the app store getter (called during initialization)
40
- */
41
- export function setAppStoreGetter(getter: () => any): void {
42
- getAppStore = getter;
43
- }
44
-
45
- /**
46
- * Initialize agent manager
47
- */
48
- export async function initializeAgentManager(cwd: string): Promise<void> {
49
- const allAgents = await loadAllAgents(cwd);
50
-
51
- const agentMap = new Map<string, Agent>();
52
- for (const agent of allAgents) {
53
- agentMap.set(agent.id, agent);
54
- }
55
-
56
- state = {
57
- agents: agentMap,
58
- cwd,
59
- };
60
-
61
- // Initialize store with default agent if store is available
62
- if (getAppStore) {
63
- const store = getAppStore();
64
- if (store.getState) {
65
- const currentAgentId = store.getState().currentAgentId || DEFAULT_AGENT_ID;
66
- // Ensure the current agent exists, fallback to default if not
67
- if (!agentMap.has(currentAgentId)) {
68
- store.getState().setCurrentAgentId(DEFAULT_AGENT_ID);
69
- }
70
- }
71
- }
72
- }
73
-
74
- /**
75
- * Get all available agents
76
- */
77
- export function getAllAgents(): Agent[] {
78
- if (!state) {
79
- return [FALLBACK_AGENT];
80
- }
81
- return Array.from(state.agents.values());
82
- }
83
-
84
- /**
85
- * Get agent by ID
86
- */
87
- export function getAgentById(id: string): Agent | null {
88
- if (!state) {
89
- return id === DEFAULT_AGENT_ID ? FALLBACK_AGENT : null;
90
- }
91
- return state.agents.get(id) || null;
92
- }
93
-
94
- /**
95
- * Get current agent
96
- */
97
- export function getCurrentAgent(): Agent {
98
- const currentAgentId = getCurrentAgentId();
99
-
100
- if (!state) {
101
- return FALLBACK_AGENT;
102
- }
103
- return state.agents.get(currentAgentId) || FALLBACK_AGENT;
104
- }
105
-
106
- /**
107
- * Get current agent ID
108
- */
109
- export function getCurrentAgentId(): string {
110
- // Try to get from store first
111
- if (getAppStore) {
112
- const store = getAppStore();
113
- if (store.getState) {
114
- return store.getState().currentAgentId || DEFAULT_AGENT_ID;
115
- }
116
- }
117
- // Fallback to default
118
- return DEFAULT_AGENT_ID;
119
- }
120
-
121
- /**
122
- * Switch to a different agent
123
- */
124
- export function switchAgent(agentId: string): boolean {
125
- if (!state) {
126
- return false;
127
- }
128
-
129
- const agent = state.agents.get(agentId);
130
- if (!agent) {
131
- return false;
132
- }
133
-
134
- // Update store if available (this triggers reactive updates)
135
- if (getAppStore) {
136
- const store = getAppStore();
137
- if (store.getState) {
138
- store.getState().setCurrentAgentId(agentId);
139
- }
140
- }
141
-
142
- return true;
143
- }
144
-
145
- /**
146
- * Reload agents from disk
147
- */
148
- export async function reloadAgents(): Promise<void> {
149
- if (!state) {
150
- return;
151
- }
152
-
153
- const cwd = state.cwd;
154
- const currentAgentId = getCurrentAgentId();
155
-
156
- await initializeAgentManager(cwd);
157
-
158
- // Restore current agent if it still exists, otherwise reset to default
159
- if (state && !state.agents.has(currentAgentId)) {
160
- if (getAppStore) {
161
- const store = getAppStore();
162
- if (store.getState) {
163
- store.getState().setCurrentAgentId(DEFAULT_AGENT_ID);
164
- }
165
- }
166
- }
167
- }
168
-
169
- /**
170
- * Get system prompt for current agent
171
- */
172
- export function getCurrentSystemPrompt(): string {
173
- return getCurrentAgent().systemPrompt;
174
- }
@@ -1,200 +0,0 @@
1
- /**
2
- * Loop Controller - Simple continuous execution
3
- *
4
- * Core concept: Keep executing the same task with context persistence
5
- * - First run: Fresh start
6
- * - Subsequent runs: Auto-continue (builds on previous work)
7
- * - Stop: Manual (Ctrl+C) or max-runs limit
8
- *
9
- * Use case: "Keep working on X until I stop you"
10
- */
11
-
12
- import chalk from 'chalk';
13
- import type { FlowOptions } from '../commands/flow/types.js';
14
-
15
- export interface LoopOptions {
16
- enabled: boolean;
17
- interval: number; // Wait time in seconds between runs (0 = no wait)
18
- maxRuns?: number; // Optional max iterations (default: infinite)
19
- }
20
-
21
- export interface LoopResult {
22
- exitCode: number;
23
- error?: Error;
24
- }
25
-
26
- export interface LoopState {
27
- iteration: number;
28
- startTime: Date;
29
- successCount: number;
30
- errorCount: number;
31
- }
32
-
33
- /**
34
- * Controller for loop execution mode
35
- */
36
- export class LoopController {
37
- private state: LoopState;
38
- private shouldStop: boolean = false;
39
-
40
- constructor() {
41
- this.state = {
42
- iteration: 0,
43
- startTime: new Date(),
44
- successCount: 0,
45
- errorCount: 0,
46
- };
47
-
48
- // Handle graceful shutdown
49
- this.setupSignalHandlers();
50
- }
51
-
52
- /**
53
- * Execute task in loop mode
54
- * Simple: Keep running same task until manual stop or max-runs
55
- */
56
- async run(
57
- executor: () => Promise<LoopResult>,
58
- options: LoopOptions
59
- ): Promise<void> {
60
- console.log(chalk.cyan.bold('\n━━━ 🔄 Loop Mode Activated\n'));
61
- console.log(chalk.dim(` Wait time: ${options.interval}s`));
62
- console.log(chalk.dim(` Max runs: ${options.maxRuns || '∞'}`));
63
- console.log(chalk.dim(` Stop: Ctrl+C or max-runs limit\n`));
64
-
65
- while (this.shouldContinue(options)) {
66
- this.state.iteration++;
67
-
68
- try {
69
- await this.executeIteration(executor, options);
70
- } catch (error) {
71
- this.handleError(error as Error);
72
- }
73
-
74
- // Wait for next iteration
75
- if (this.shouldContinue(options)) {
76
- await this.waitForNextRun(options);
77
- }
78
- }
79
-
80
- this.printSummary();
81
- }
82
-
83
- /**
84
- * Execute single iteration
85
- * Simple: Run task, track success/error, continue
86
- */
87
- private async executeIteration(
88
- executor: () => Promise<LoopResult>,
89
- options: LoopOptions
90
- ): Promise<void> {
91
- const maxDisplay = options.maxRuns || '∞';
92
- console.log(
93
- chalk.cyan(
94
- `\n🔄 Loop iteration ${this.state.iteration}/${maxDisplay}`
95
- )
96
- );
97
- console.log(chalk.dim(`Started: ${new Date().toLocaleTimeString()}\n`));
98
-
99
- const result = await executor();
100
-
101
- // Update state (just count success/error)
102
- if (result.error || result.exitCode !== 0) {
103
- this.state.errorCount++;
104
- console.log(chalk.yellow(`\n⚠️ Task encountered error (continuing...)`));
105
- } else {
106
- this.state.successCount++;
107
- }
108
- }
109
-
110
- /**
111
- * Handle execution error
112
- * Simple: Log and continue (resilient)
113
- */
114
- private handleError(error: Error): void {
115
- this.state.errorCount++;
116
- console.error(chalk.yellow('\n⚠️ Error occurred - continuing to next iteration'));
117
- console.error(chalk.dim(`Error: ${error.message}\n`));
118
- }
119
-
120
- /**
121
- * Wait for next iteration
122
- */
123
- private async waitForNextRun(options: LoopOptions): Promise<void> {
124
- const maxDisplay = options.maxRuns || '∞';
125
- const progress = `${this.state.iteration}/${maxDisplay}`;
126
-
127
- console.log(
128
- chalk.dim(
129
- `\n⏳ Waiting ${options.interval}s until next run... (completed: ${progress})`
130
- )
131
- );
132
-
133
- // Countdown display (optional, can be removed if too verbose)
134
- const startTime = Date.now();
135
- const endTime = startTime + options.interval * 1000;
136
-
137
- while (Date.now() < endTime && !this.shouldStop) {
138
- await this.sleep(1000);
139
-
140
- const remaining = Math.ceil((endTime - Date.now()) / 1000);
141
- if (remaining > 0 && remaining % 10 === 0) {
142
- process.stdout.write(chalk.dim(`\r⏳ ${remaining}s remaining...`));
143
- }
144
- }
145
-
146
- if (!this.shouldStop) {
147
- process.stdout.write('\r' + ' '.repeat(50) + '\r'); // Clear line
148
- }
149
- }
150
-
151
- /**
152
- * Check if should continue looping
153
- * Simple: Stop only on manual interrupt or max-runs
154
- */
155
- private shouldContinue(options: LoopOptions): boolean {
156
- if (this.shouldStop) return false;
157
- if (options.maxRuns && this.state.iteration >= options.maxRuns) return false;
158
- return true;
159
- }
160
-
161
- /**
162
- * Print execution summary
163
- */
164
- private printSummary(): void {
165
- const duration = Date.now() - this.state.startTime.getTime();
166
- const minutes = Math.floor(duration / 60000);
167
- const seconds = Math.floor((duration % 60000) / 1000);
168
-
169
- console.log(chalk.cyan.bold('\n━━━ 🏁 Loop Summary\n'));
170
- console.log(` Total iterations: ${this.state.iteration}`);
171
- console.log(
172
- ` Successful: ${chalk.green(this.state.successCount.toString())}`
173
- );
174
- console.log(` Errors: ${chalk.red(this.state.errorCount.toString())}`);
175
- console.log(` Duration: ${minutes}m ${seconds}s`);
176
- console.log();
177
- }
178
-
179
- /**
180
- * Setup signal handlers for graceful shutdown
181
- */
182
- private setupSignalHandlers(): void {
183
- const handler = () => {
184
- console.log(
185
- chalk.yellow('\n\n⚠️ Interrupt received - finishing current iteration...')
186
- );
187
- this.shouldStop = true;
188
- };
189
-
190
- process.on('SIGINT', handler);
191
- process.on('SIGTERM', handler);
192
- }
193
-
194
- /**
195
- * Sleep helper
196
- */
197
- private sleep(ms: number): Promise<void> {
198
- return new Promise((resolve) => setTimeout(resolve, ms));
199
- }
200
- }
@@ -1,147 +0,0 @@
1
- /**
2
- * Rule Loader
3
- * Loads rule definitions from markdown files with front matter
4
- */
5
-
6
- import { readFile, readdir, access } from 'node:fs/promises';
7
- import { join, parse, relative, dirname } from 'node:path';
8
- import { homedir } from 'node:os';
9
- import { fileURLToPath } from 'node:url';
10
- import matter from 'gray-matter';
11
- import type { Rule, RuleMetadata } from '../types/rule.types.js';
12
-
13
- /**
14
- * Load a single rule from a markdown file
15
- */
16
- export async function loadRuleFromFile(
17
- filePath: string,
18
- isBuiltin: boolean = false,
19
- ruleId?: string
20
- ): Promise<Rule | null> {
21
- try {
22
- const fileContent = await readFile(filePath, 'utf-8');
23
- const { data, content } = matter(fileContent);
24
-
25
- // Validate front matter
26
- if (!data.name || typeof data.name !== 'string') {
27
- console.error(`Rule file ${filePath} missing required 'name' field`);
28
- return null;
29
- }
30
-
31
- const metadata: RuleMetadata = {
32
- name: data.name,
33
- description: data.description || '',
34
- enabled: data.enabled !== undefined ? Boolean(data.enabled) : true,
35
- };
36
-
37
- // Get rule ID from parameter or filename
38
- const id = ruleId || parse(filePath).name;
39
-
40
- return {
41
- id,
42
- metadata,
43
- content: content.trim(),
44
- isBuiltin,
45
- filePath,
46
- };
47
- } catch (error) {
48
- console.error(`Failed to load rule from ${filePath}:`, error);
49
- return null;
50
- }
51
- }
52
-
53
- /**
54
- * Load all rules from a directory (recursively)
55
- */
56
- export async function loadRulesFromDirectory(dirPath: string, isBuiltin: boolean = false): Promise<Rule[]> {
57
- try {
58
- // Read directory recursively to support subdirectories
59
- const files = await readdir(dirPath, { recursive: true, withFileTypes: true });
60
-
61
- // Filter for .md files and calculate rule IDs from relative paths
62
- const ruleFiles = files
63
- .filter((f) => f.isFile() && f.name.endsWith('.md'))
64
- .map((f) => {
65
- const fullPath = join(f.parentPath || f.path, f.name);
66
- // Calculate relative path from dirPath and remove .md extension
67
- const relativePath = relative(dirPath, fullPath).replace(/\.md$/, '');
68
- return { fullPath, ruleId: relativePath };
69
- });
70
-
71
- const rules = await Promise.all(
72
- ruleFiles.map(({ fullPath, ruleId }) => loadRuleFromFile(fullPath, isBuiltin, ruleId))
73
- );
74
-
75
- return rules.filter((rule): rule is Rule => rule !== null);
76
- } catch (error) {
77
- // Directory doesn't exist or can't be read
78
- return [];
79
- }
80
- }
81
-
82
- /**
83
- * Get system rules path (bundled with the app)
84
- */
85
- export async function getSystemRulesPath(): Promise<string> {
86
- // Get the directory of the current module (cross-platform)
87
- const currentFile = fileURLToPath(import.meta.url);
88
- const currentDir = dirname(currentFile);
89
-
90
- // In production (dist), assets are at dist/assets/rules
91
- // In development (src), go up to project root: src/core -> project root
92
- const distPath = join(currentDir, '..', 'assets', 'rules');
93
- const devPath = join(currentDir, '..', '..', 'assets', 'rules');
94
-
95
- // Check which one exists (try dist first, then dev)
96
- try {
97
- await access(distPath);
98
- return distPath;
99
- } catch {
100
- return devPath;
101
- }
102
- }
103
-
104
- /**
105
- * Get all rule search paths
106
- */
107
- export function getRuleSearchPaths(cwd: string): string[] {
108
- const globalPath = join(homedir(), '.sylphx-flow', 'rules');
109
- const projectPath = join(cwd, '.sylphx-flow', 'rules');
110
-
111
- return [globalPath, projectPath];
112
- }
113
-
114
- /**
115
- * Load all available rules from all sources
116
- */
117
- export async function loadAllRules(cwd: string): Promise<Rule[]> {
118
- const systemPath = await getSystemRulesPath();
119
- const [globalPath, projectPath] = getRuleSearchPaths(cwd);
120
-
121
- const [systemRules, globalRules, projectRules] = await Promise.all([
122
- loadRulesFromDirectory(systemPath, true), // System rules are marked as builtin
123
- loadRulesFromDirectory(globalPath, false),
124
- loadRulesFromDirectory(projectPath, false),
125
- ]);
126
-
127
- // Priority: system < global < project
128
- // Use Map to deduplicate by ID (later entries override earlier ones)
129
- const ruleMap = new Map<string, Rule>();
130
-
131
- // Add system rules first (lowest priority)
132
- for (const rule of systemRules) {
133
- ruleMap.set(rule.id, rule);
134
- }
135
-
136
- // Add global rules (override system)
137
- for (const rule of globalRules) {
138
- ruleMap.set(rule.id, rule);
139
- }
140
-
141
- // Add project rules (override globals and system)
142
- for (const rule of projectRules) {
143
- ruleMap.set(rule.id, rule);
144
- }
145
-
146
- return Array.from(ruleMap.values());
147
- }