agent-state-machine 2.2.0 → 2.2.2

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 (36) hide show
  1. package/bin/cli.js +78 -2
  2. package/lib/remote/client.js +37 -8
  3. package/lib/runtime/agent.js +6 -2
  4. package/lib/runtime/interaction.js +2 -1
  5. package/lib/runtime/prompt.js +37 -1
  6. package/lib/runtime/runtime.js +67 -5
  7. package/package.json +1 -1
  8. package/templates/project-builder/README.md +304 -56
  9. package/templates/project-builder/agents/code-fixer.md +50 -0
  10. package/templates/project-builder/agents/code-writer.md +3 -0
  11. package/templates/project-builder/agents/sanity-checker.md +6 -0
  12. package/templates/project-builder/agents/sanity-runner.js +3 -1
  13. package/templates/project-builder/agents/test-planner.md +3 -1
  14. package/templates/project-builder/config.js +4 -4
  15. package/templates/project-builder/scripts/workflow-helpers.js +104 -2
  16. package/templates/project-builder/workflow.js +151 -14
  17. package/templates/starter/README.md +291 -42
  18. package/templates/starter/config.js +1 -1
  19. package/vercel-server/api/submit/[token].js +2 -13
  20. package/vercel-server/api/ws/cli.js +40 -2
  21. package/vercel-server/local-server.js +32 -22
  22. package/vercel-server/public/remote/assets/index-BsJsLDKc.css +1 -0
  23. package/vercel-server/public/remote/assets/index-CmtT6ADh.js +168 -0
  24. package/vercel-server/public/remote/index.html +2 -2
  25. package/vercel-server/ui/src/App.jsx +69 -62
  26. package/vercel-server/ui/src/components/ChoiceInteraction.jsx +69 -18
  27. package/vercel-server/ui/src/components/ConfirmInteraction.jsx +7 -7
  28. package/vercel-server/ui/src/components/ContentCard.jsx +600 -104
  29. package/vercel-server/ui/src/components/EventsLog.jsx +20 -13
  30. package/vercel-server/ui/src/components/Footer.jsx +9 -4
  31. package/vercel-server/ui/src/components/Header.jsx +12 -3
  32. package/vercel-server/ui/src/components/SendingCard.jsx +33 -0
  33. package/vercel-server/ui/src/components/TextInteraction.jsx +8 -8
  34. package/vercel-server/ui/src/index.css +82 -10
  35. package/vercel-server/public/remote/assets/index-BOKpYANC.js +0 -148
  36. package/vercel-server/public/remote/assets/index-DHL_iHQW.css +0 -1
@@ -8,11 +8,12 @@
8
8
  * 4. Task lifecycle with optimal agent sequencing
9
9
  */
10
10
 
11
- import { agent, memory, askHuman } from 'agent-state-machine';
11
+ import { agent, memory, askHuman, getCurrentRuntime } from 'agent-state-machine';
12
12
  import path from 'path';
13
13
  import { fileURLToPath } from 'url';
14
14
  import {
15
15
  writeMarkdownFile,
16
+ writeImplementationFiles,
16
17
  isApproval,
17
18
  renderRoadmapMarkdown,
18
19
  renderTasksMarkdown,
@@ -20,7 +21,12 @@ import {
20
21
  getTaskStage,
21
22
  setTaskStage,
22
23
  getTaskData,
23
- setTaskData
24
+ setTaskData,
25
+ clearPartialTaskData,
26
+ getQuickFixAttempts,
27
+ incrementQuickFixAttempts,
28
+ resetQuickFixAttempts,
29
+ detectTestFramework
24
30
  } from './scripts/workflow-helpers.js';
25
31
  import {
26
32
  createInteraction,
@@ -34,6 +40,57 @@ const __dirname = path.dirname(__filename);
34
40
  const WORKFLOW_DIR = __dirname;
35
41
  const STATE_DIR = path.join(WORKFLOW_DIR, 'state');
36
42
 
43
+ // ANSI Colors for console output
44
+ const C = {
45
+ reset: '\x1b[0m',
46
+ bold: '\x1b[1m',
47
+ cyan: '\x1b[36m',
48
+ green: '\x1b[32m',
49
+ yellow: '\x1b[33m'
50
+ };
51
+
52
+ function applyFixesToImplementation(originalImplementation, fixes) {
53
+ if (!originalImplementation || !Array.isArray(fixes) || fixes.length === 0) {
54
+ return originalImplementation;
55
+ }
56
+
57
+ const updated = { ...originalImplementation };
58
+ const container = updated.implementation ? { ...updated.implementation } : updated;
59
+ const files = Array.isArray(container.files) ? [...container.files] : [];
60
+
61
+ for (const fix of fixes) {
62
+ if (!fix?.path || !fix?.code) {
63
+ console.warn(` [Fix] Skipping invalid fix entry: ${JSON.stringify(fix)}`);
64
+ continue;
65
+ }
66
+ if (fix.operation && fix.operation !== 'replace') {
67
+ console.warn(` [Fix] Unsupported operation "${fix.operation}" for ${fix.path}`);
68
+ continue;
69
+ }
70
+
71
+ const existingIndex = files.findIndex((file) => file.path === fix.path);
72
+ const nextFile = {
73
+ ...(existingIndex >= 0 ? files[existingIndex] : {}),
74
+ path: fix.path,
75
+ code: fix.code,
76
+ purpose: fix.purpose || (existingIndex >= 0 ? files[existingIndex].purpose : 'Updated by code-fixer')
77
+ };
78
+
79
+ if (existingIndex >= 0) {
80
+ files[existingIndex] = nextFile;
81
+ } else {
82
+ files.push(nextFile);
83
+ }
84
+ }
85
+
86
+ if (updated.implementation) {
87
+ updated.implementation = { ...container, files };
88
+ return updated;
89
+ }
90
+
91
+ return { ...updated, files };
92
+ }
93
+
37
94
  // ============================================
38
95
  // MAIN WORKFLOW
39
96
  // ============================================
@@ -334,6 +391,14 @@ export default async function () {
334
391
  });
335
392
  setTaskData(i, taskId, 'code', implementation);
336
393
  }
394
+
395
+ // Write implementation files to disk
396
+ const implementation = getTaskData(i, taskId, 'code');
397
+ if (implementation) {
398
+ console.log(' > Writing files to disk...');
399
+ writeImplementationFiles(implementation);
400
+ }
401
+
337
402
  setTaskStage(i, taskId, TASK_STAGES.CODE_REVIEW);
338
403
  stage = TASK_STAGES.CODE_REVIEW;
339
404
  }
@@ -373,10 +438,12 @@ export default async function () {
373
438
 
374
439
  // 6. Sanity check generation & execution
375
440
  if (stage === TASK_STAGES.SANITY_CHECK) {
441
+ const testFramework = detectTestFramework();
376
442
  const executableChecks = await agent('sanity-checker', {
377
443
  task: task,
378
444
  implementation: getTaskData(i, taskId, 'code'),
379
- testPlan: getTaskData(i, taskId, 'tests')
445
+ testPlan: getTaskData(i, taskId, 'tests'),
446
+ testFramework
380
447
  });
381
448
  setTaskData(i, taskId, 'sanity_checks', executableChecks);
382
449
 
@@ -387,8 +454,8 @@ export default async function () {
387
454
  const sanityChoice = createInteraction('choice', `phase-${i + 1}-task-${taskId}-sanity-choice`, {
388
455
  prompt: `Sanity checks for "${task.title}":\n\n${checksDisplay}\n\nHow would you like to proceed?`,
389
456
  options: [
390
- { key: 'manual', label: 'Run checks manually', description: 'You run the commands and confirm results' },
391
457
  { key: 'auto', label: 'Run automatically', description: 'Agent executes checks and reports results' },
458
+ { key: 'manual', label: 'Run checks manually', description: 'You run the commands and confirm results' },
392
459
  { key: 'skip', label: 'Skip verification', description: 'Approve without running checks' }
393
460
  ],
394
461
  allowCustom: true
@@ -402,6 +469,7 @@ export default async function () {
402
469
 
403
470
  if (sanityResponse.isCustom) {
404
471
  setTaskData(i, taskId, 'feedback', sanityResponse.customText || sanityResponse.raw || sanityRaw);
472
+ resetQuickFixAttempts(i, taskId);
405
473
  setTaskStage(i, taskId, TASK_STAGES.PENDING);
406
474
  t--;
407
475
  continue;
@@ -423,12 +491,26 @@ export default async function () {
423
491
  .map((r) => ` - Check ${r.id}: ${r.error}`)
424
492
  .join('\n');
425
493
 
494
+ const quickFixAttempts = getQuickFixAttempts(i, taskId);
495
+ const runtime = getCurrentRuntime();
496
+ const maxAttempts = runtime?.workflowConfig?.maxQuickFixAttempts ?? 10;
497
+ const failOptions = [];
498
+ if (quickFixAttempts < maxAttempts) {
499
+ failOptions.push({
500
+ key: 'quickfix',
501
+ label: 'Quick fix',
502
+ description: `Run targeted fixes (attempt ${quickFixAttempts + 1} of ${maxAttempts})`
503
+ });
504
+ }
505
+ failOptions.push(
506
+ { key: 'partial', label: 'Partial reimplement', description: 'Keep security review and test plan, redo implementation' },
507
+ { key: 'reimplement', label: 'Full reimplement', description: 'Restart task from scratch' },
508
+ { key: 'ignore', label: 'Ignore failures and approve anyway' }
509
+ );
510
+
426
511
  const failChoice = createInteraction('choice', `phase-${i + 1}-task-${taskId}-sanity-fail`, {
427
512
  prompt: `${results.summary.failed} sanity check(s) failed:\n\n${failedChecks}\n\nHow would you like to proceed?`,
428
- options: [
429
- { key: 'reimplement', label: 'Re-implement task with this feedback' },
430
- { key: 'ignore', label: 'Ignore failures and approve anyway' }
431
- ],
513
+ options: failOptions,
432
514
  allowCustom: true
433
515
  });
434
516
 
@@ -438,19 +520,71 @@ export default async function () {
438
520
  });
439
521
  const failResponse = await parseResponse(failChoice, failRaw);
440
522
 
441
- if (failResponse.selectedKey === 'reimplement' || failResponse.isCustom) {
523
+ if (failResponse.isCustom) {
524
+ const customFeedback = failResponse.customText || failResponse.text || failResponse.raw || failRaw;
525
+ const combinedFeedback = `${customFeedback}\n\nSanity check failures:\n${failedChecks}`;
526
+ setTaskData(i, taskId, 'feedback', combinedFeedback);
527
+ clearPartialTaskData(i, taskId);
528
+ resetQuickFixAttempts(i, taskId);
529
+ setTaskStage(i, taskId, TASK_STAGES.PENDING);
530
+ t--;
531
+ continue;
532
+ }
533
+
534
+ if (failResponse.selectedKey === 'quickfix') {
535
+ console.log(' > Running quick fix...');
536
+ const fixerResult = await agent('code-fixer', {
537
+ task: task,
538
+ originalImplementation: getTaskData(i, taskId, 'code'),
539
+ sanityCheckResults: {
540
+ summary: results.summary,
541
+ results: results.results,
542
+ checks: executableChecks.checks
543
+ },
544
+ testPlan: getTaskData(i, taskId, 'tests'),
545
+ previousAttempts: quickFixAttempts
546
+ });
547
+
548
+ const fixes = fixerResult?.fixes || [];
549
+ const fixFiles = fixes
550
+ .filter((fix) => fix?.path && fix?.code && (!fix.operation || fix.operation === 'replace'))
551
+ .map((fix) => ({ path: fix.path, code: fix.code }));
552
+
553
+ if (fixFiles.length > 0) {
554
+ console.log(' > Applying fixes to disk...');
555
+ writeImplementationFiles({ files: fixFiles });
556
+ }
557
+
558
+ const updatedImplementation = applyFixesToImplementation(getTaskData(i, taskId, 'code'), fixes);
559
+ setTaskData(i, taskId, 'code', updatedImplementation);
560
+ incrementQuickFixAttempts(i, taskId);
561
+ setTaskData(i, taskId, 'sanity_checks', null);
562
+ setTaskData(i, taskId, 'sanity_results', null);
563
+ setTaskStage(i, taskId, TASK_STAGES.SANITY_CHECK);
564
+ t--;
565
+ continue;
566
+ }
567
+
568
+ if (failResponse.selectedKey === 'partial') {
569
+ setTaskData(i, taskId, 'feedback', `Sanity check failures:\n${failedChecks}`);
570
+ clearPartialTaskData(i, taskId, ['security_pre', 'tests']);
571
+ resetQuickFixAttempts(i, taskId);
572
+ setTaskStage(i, taskId, TASK_STAGES.IMPLEMENTING);
573
+ t--;
574
+ continue;
575
+ }
576
+
577
+ if (failResponse.selectedKey === 'reimplement') {
442
578
  setTaskData(i, taskId, 'feedback', `Sanity check failures:\n${failedChecks}`);
443
- setTaskData(i, taskId, 'security_pre', null);
444
- setTaskData(i, taskId, 'tests', null);
445
- setTaskData(i, taskId, 'code', null);
446
- setTaskData(i, taskId, 'review', null);
447
- setTaskData(i, taskId, 'security_post', null);
579
+ clearPartialTaskData(i, taskId);
580
+ resetQuickFixAttempts(i, taskId);
448
581
  setTaskStage(i, taskId, TASK_STAGES.PENDING);
449
582
  t--;
450
583
  continue;
451
584
  }
452
585
  }
453
586
 
587
+ resetQuickFixAttempts(i, taskId);
454
588
  setTaskStage(i, taskId, TASK_STAGES.COMPLETED);
455
589
  stage = TASK_STAGES.COMPLETED;
456
590
  task.stage = 'completed';
@@ -458,6 +592,7 @@ export default async function () {
458
592
  writeMarkdownFile(STATE_DIR, `phase-${i + 1}-tasks.md`, renderTasksMarkdown(i + 1, phase.title, tasks));
459
593
  console.log(` Task ${t + 1} confirmed complete!\n`);
460
594
  } else if (action === 'skip') {
595
+ resetQuickFixAttempts(i, taskId);
461
596
  setTaskStage(i, taskId, TASK_STAGES.COMPLETED);
462
597
  stage = TASK_STAGES.COMPLETED;
463
598
  task.stage = 'completed';
@@ -489,6 +624,7 @@ export default async function () {
489
624
 
490
625
  if (approvalResponse.selectedKey === 'approve' || isApproval(approvalResponse.raw || approvalRaw)) {
491
626
  setTaskStage(i, taskId, TASK_STAGES.COMPLETED);
627
+ resetQuickFixAttempts(i, taskId);
492
628
  task.stage = 'completed';
493
629
  memory[tasksKey] = tasks;
494
630
  writeMarkdownFile(STATE_DIR, `phase-${i + 1}-tasks.md`, renderTasksMarkdown(i + 1, phase.title, tasks));
@@ -505,6 +641,7 @@ export default async function () {
505
641
  setTaskData(i, taskId, 'security_post', null);
506
642
  setTaskData(i, taskId, 'sanity_checks', null);
507
643
  setTaskData(i, taskId, 'sanity_results', null);
644
+ resetQuickFixAttempts(i, taskId);
508
645
 
509
646
  setTaskStage(i, taskId, TASK_STAGES.PENDING);
510
647
  t--;
@@ -1,62 +1,100 @@
1
- # __WORKFLOW_NAME__
1
+ # agent-state-machine
2
2
 
3
- A workflow created with agent-state-machine (native JS format).
3
+ A workflow runner for building **linear, stateful agent workflows** in plain JavaScript.
4
4
 
5
- ## Structure
5
+ You write normal `async/await` code. The runtime handles:
6
+ - **Auto-persisted** `memory` (saved to disk on mutation)
7
+ - **Auto-tracked** `fileTree` (detects file changes made by agents via Git)
8
+ - **Human-in-the-loop** blocking via `askHuman()` or agent-driven interactions
9
+ - Local **JS agents** + **Markdown agents** (LLM-powered)
10
+ - **Agent retries** with history logging for failures
6
11
 
7
- ```
8
- __WORKFLOW_NAME__/
9
- ├── workflow.js # Native JS workflow (async/await)
10
- ├── config.js # Model/API key configuration
11
- ├── agents/ # Custom agents (.js/.mjs/.cjs or .md)
12
- ├── interactions/ # Human-in-the-loop inputs (created at runtime)
13
- ├── state/ # Runtime state (current.json, history.jsonl)
14
- └── steering/ # Steering configuration
15
- ```
12
+ ---
16
13
 
17
- ## Usage
14
+ ## Install
18
15
 
19
- Edit `config.js` to set models and API keys for this workflow.
16
+ You need to install the package **globally** to get the CLI, and **locally** in your project so your workflow can import the library.
20
17
 
21
- Run the workflow (or resume if interrupted):
22
- ```bash
23
- state-machine run __WORKFLOW_NAME__
24
- ```
18
+ ### Global CLI
19
+ Provides the `state-machine` command.
25
20
 
26
- Check status:
27
21
  ```bash
28
- state-machine status __WORKFLOW_NAME__
29
- ```
22
+ # npm
23
+ npm i -g agent-state-machine
30
24
 
31
- View history:
32
- ```bash
33
- state-machine history __WORKFLOW_NAME__
25
+ # pnpm
26
+ pnpm add -g agent-state-machine
34
27
  ```
35
28
 
36
- View trace logs in browser with live updates:
29
+ ### Local Library
30
+ Required so your `workflow.js` can `import { agent, memory, fileTree } from 'agent-state-machine'`.
31
+
37
32
  ```bash
38
- state-machine follow __WORKFLOW_NAME__
33
+ # npm
34
+ npm i agent-state-machine
35
+
36
+ # pnpm (for monorepos/turbo, install in root)
37
+ pnpm add agent-state-machine -w
39
38
  ```
40
39
 
41
- Reset state (clears memory/state):
40
+ Requirements: Node.js >= 16.
41
+
42
+ ---
43
+
44
+ ## CLI
45
+
42
46
  ```bash
43
- state-machine reset __WORKFLOW_NAME__
47
+ state-machine --setup <workflow-name>
48
+ state-machine --setup <workflow-name> --template <template-name>
49
+ state-machine run <workflow-name>
50
+ state-machine run <workflow-name> -reset
51
+ state-machine run <workflow-name> -reset-hard
52
+
53
+ state-machine -reset <workflow-name>
54
+ state-machine -reset-hard <workflow-name>
55
+
56
+ state-machine history <workflow-name> [limit]
44
57
  ```
45
58
 
46
- Hard reset (clears everything: history/interactions/memory):
47
- ```bash
48
- state-machine reset-hard __WORKFLOW_NAME__
59
+ Templates live in `templates/` and `starter` is used by default.
60
+
61
+ Workflows live in:
62
+
63
+ ```text
64
+ workflows/<name>/
65
+ ├── workflow.js # Native JS workflow (async/await)
66
+ ├── config.js # Model/API key configuration
67
+ ├── package.json # Sets "type": "module" for this workflow folder
68
+ ├── agents/ # Custom agents (.js/.mjs/.cjs or .md)
69
+ ├── interactions/ # Human-in-the-loop files (auto-created)
70
+ ├── state/ # current.json, history.jsonl
71
+ └── steering/ # global.md + config.json
49
72
  ```
50
73
 
51
- ## Writing Workflows
74
+ ---
52
75
 
53
- Edit `workflow.js` - write normal async JavaScript:
76
+ ## Writing workflows (native JS)
77
+
78
+ Edit `config.js` to set models and API keys for the workflow.
54
79
 
55
80
  ```js
81
+ /**
82
+ /**
83
+ * project-builder Workflow
84
+ *
85
+ * Native JavaScript workflow - write normal async/await code!
86
+ *
87
+ * Features:
88
+ * - memory object auto-persists to disk (use memory guards for idempotency)
89
+ * - Use standard JS control flow (if, for, etc.)
90
+ * - Interactive prompts pause and wait for user input
91
+ */
92
+
56
93
  import { agent, memory, askHuman, parallel } from 'agent-state-machine';
94
+ import { notify } from './scripts/mac-notification.js';
57
95
 
58
96
  export default async function() {
59
- console.log('Starting __WORKFLOW_NAME__ workflow...');
97
+ console.log('Starting project-builder workflow...');
60
98
 
61
99
  // Example: Get user input (saved to memory)
62
100
  const userLocation = await askHuman('Where do you live?');
@@ -88,31 +126,242 @@ export default async function() {
88
126
  // console.log('b: ' + JSON.stringify(b))
89
127
  // console.log('c: ' + JSON.stringify(c))
90
128
 
91
- notify(['__WORKFLOW_NAME__', userInfo.name || userInfo + ' has been greeted!']);
129
+ notify(['project-builder', userInfo.name || userInfo + ' has been greeted!']);
92
130
 
93
131
  console.log('Workflow completed!');
94
132
  }
95
133
  ```
96
134
 
97
- ## Creating Agents
135
+ ### Resuming workflows
136
+
137
+ `state-machine run` restarts your workflow from the top, loading the persisted state.
138
+
139
+ If the workflow needs human input, it will **block inline** in the terminal. You can answer in the terminal, edit `interactions/<slug>.md`, or respond in the browser.
140
+
141
+ If the process is interrupted, running `state-machine run <workflow-name>` again will continue execution (assuming your workflow uses `memory` to skip completed steps).
142
+
143
+ ---
144
+
145
+ ## Core API
146
+
147
+ ### `agent(name, params?, options?)`
148
+
149
+ Runs `workflows/<name>/agents/<agent>.(js|mjs|cjs)` or `<agent>.md`.
150
+
151
+ ```js
152
+ const out = await agent('review', { file: 'src/app.js' });
153
+ memory.lastReview = out;
154
+ ```
155
+
156
+ Options:
157
+ - `retry` (number | false): default `2` (3 total attempts). Use `false` to disable retries.
158
+ - `steering` (string | string[]): extra steering files to load from `workflows/<name>/steering/`.
159
+
160
+ Context is explicit: only `params` are provided to agents unless you pass additional data.
161
+
162
+ ### `memory`
163
+
164
+ A persisted object for your workflow.
165
+
166
+ - Mutations auto-save to `workflows/<name>/state/current.json`.
167
+ - Use it as your "long-lived state" between runs.
168
+
169
+ ```js
170
+ memory.count = (memory.count || 0) + 1;
171
+ ```
172
+
173
+ ### `fileTree`
174
+
175
+ Auto-tracked file changes made by agents.
176
+
177
+ - Before each `await agent(...)`, the runtime captures a Git baseline
178
+ - After the agent completes, it detects created/modified/deleted files
179
+ - Changes are stored in `memory.fileTree` and persisted to `current.json`
180
+
181
+ ```js
182
+ // Files are auto-tracked when agents create them
183
+ await agent('code-writer', { task: 'Create auth module' });
184
+
185
+ // Access tracked files
186
+ console.log(memory.fileTree);
187
+ // { "src/auth.js": { status: "created", createdBy: "code-writer", ... } }
188
+
189
+ // Pass file context to other agents
190
+ await agent('code-reviewer', { fileTree: memory.fileTree });
191
+ ```
192
+
193
+ Configuration in `config.js`:
194
+
195
+ ```js
196
+ export const config = {
197
+ // ... models and apiKeys ...
198
+ projectRoot: process.env.PROJECT_ROOT, // defaults to ../.. from workflow
199
+ fileTracking: true, // enable/disable (default: true)
200
+ fileTrackingIgnore: ['node_modules/**', '.git/**', 'dist/**'],
201
+ fileTrackingKeepDeleted: false // keep deleted files in tree
202
+ };
203
+ ```
204
+
205
+ ### `trackFile(path, options?)` / `untrackFile(path)`
206
+
207
+ Manual file tracking utilities:
208
+
209
+ ```js
210
+ import { trackFile, getFileTree, untrackFile } from 'agent-state-machine';
211
+
212
+ trackFile('README.md', { caption: 'Project docs' });
213
+ const tree = getFileTree();
214
+ untrackFile('old-file.js');
215
+ ```
216
+
217
+ ### `askHuman(question, options?)`
218
+
219
+ Gets user input.
220
+
221
+ - In a TTY, it prompts in the terminal (or via the browser when remote follow is enabled).
222
+ - Otherwise it creates `interactions/<slug>.md` and blocks until you confirm in the terminal (or respond in the browser).
223
+
224
+ ```js
225
+ const repo = await askHuman('What repo should I work on?', { slug: 'repo' });
226
+ memory.repo = repo;
227
+ ```
228
+
229
+ ### `parallel([...])` / `parallelLimit([...], limit)`
98
230
 
99
- **JavaScript agent** (`agents/my-agent.js`):
231
+ Run multiple `agent()` calls concurrently:
100
232
 
101
233
  ```js
234
+ import { agent, parallel, parallelLimit } from 'agent-state-machine';
235
+
236
+ const [a, b] = await parallel([
237
+ agent('review', { file: 'src/a.js' }),
238
+ agent('review', { file: 'src/b.js' }),
239
+ ]);
240
+
241
+ const results = await parallelLimit(
242
+ ['a.js', 'b.js', 'c.js'].map(f => agent('review', { file: f })),
243
+ 2
244
+ );
245
+ ```
246
+
247
+ ---
248
+
249
+ ## Agents
250
+
251
+ Agents live in `workflows/<workflow>/agents/`.
252
+
253
+ ### JavaScript agents
254
+
255
+ **ESM (`.js` / `.mjs`)**:
256
+
257
+ ```js
258
+ // workflows/<name>/agents/example.js
102
259
  import { llm } from 'agent-state-machine';
103
260
 
104
261
  export default async function handler(context) {
105
- const response = await llm(context, { model: 'smart', prompt: 'Hello!' });
106
- return { greeting: response.text };
262
+ // context includes:
263
+ // - params passed to agent(name, params)
264
+ // - context._steering (global + optional additional steering content)
265
+ // - context._config (models/apiKeys/workflowDir/projectRoot)
266
+
267
+ // Optionally return _files to annotate tracked files
268
+ return {
269
+ ok: true,
270
+ _files: [{ path: 'src/example.js', caption: 'Example module' }]
271
+ };
272
+ }
273
+ ```
274
+
275
+ **CommonJS (`.cjs`)** (only if you prefer CJS):
276
+
277
+ ```js
278
+ // workflows/<name>/agents/example.cjs
279
+ async function handler(context) {
280
+ return { ok: true };
107
281
  }
282
+
283
+ module.exports = handler;
284
+ module.exports.handler = handler;
108
285
  ```
109
286
 
110
- **Markdown agent** (`agents/greeter.md`):
287
+ If you need to request human input from a JS agent, return an `_interaction` payload:
288
+
289
+ ```js
290
+ return {
291
+ _interaction: {
292
+ slug: 'approval',
293
+ targetKey: 'approval',
294
+ content: 'Please approve this change (yes/no).'
295
+ }
296
+ };
297
+ ```
298
+
299
+ The runtime will block execution and wait for your response in the terminal.
300
+
301
+ ### Markdown agents (`.md`)
302
+
303
+ Markdown agents are LLM-backed prompt templates with optional frontmatter.
304
+ Frontmatter can include `steering` to load additional files from `workflows/<name>/steering/`.
111
305
 
112
306
  ```md
113
307
  ---
114
- model: fast
308
+ model: smart
115
309
  output: greeting
310
+ steering: tone, product
311
+ ---
312
+ Generate a friendly greeting for {{name}}.
313
+ ```
314
+
315
+ Calling it:
316
+
317
+ ```js
318
+ const { greeting } = await agent('greeter', { name: 'Sam' });
319
+ memory.greeting = greeting;
320
+ ```
321
+
116
322
  ---
117
- Generate a greeting for {{name}}.
323
+
324
+ ## Models & LLM execution
325
+
326
+ In your workflow’s `export const config = { models: { ... } }`, each model value can be:
327
+
328
+ ### CLI command
329
+
330
+ ```js
331
+ export const config = {
332
+ models: {
333
+ smart: "claude -m claude-sonnet-4-20250514 -p"
334
+ }
335
+ };
118
336
  ```
337
+
338
+ ### API target
339
+
340
+ Format: `api:<provider>:<model>`
341
+
342
+ ```js
343
+ export const config = {
344
+ models: {
345
+ smart: "api:openai:gpt-4.1-mini"
346
+ },
347
+ apiKeys: {
348
+ openai: process.env.OPENAI_API_KEY
349
+ }
350
+ };
351
+ ```
352
+
353
+ The runtime captures the fully-built prompt in `state/history.jsonl`, viewable in the browser with live updates when running with the `--local` flag or via the remote URL. Remote follow links persist across runs (stored in `config.js`) unless you pass `-n`/`--new` to regenerate.
354
+
355
+ ---
356
+
357
+ ## State & persistence
358
+
359
+ Native JS workflows persist to:
360
+
361
+ - `workflows/<name>/state/current.json` — status, memory (includes fileTree), pending interaction
362
+ - `workflows/<name>/state/history.jsonl` — event log (newest entries first, includes agent retry/failure entries)
363
+ - `workflows/<name>/interactions/*.md` — human input files (when paused)
364
+
365
+ ## License
366
+
367
+ MIT
@@ -1,6 +1,6 @@
1
1
  export const config = {
2
2
  models: {
3
- low: "gemini",
3
+ low: "gemini -m gemini-2.5-flash-lite",
4
4
  med: "codex --model gpt-5.2",
5
5
  high: "claude -m claude-opus-4-20250514 -p",
6
6
  },
@@ -6,7 +6,6 @@
6
6
 
7
7
  import {
8
8
  getSession,
9
- addEvent,
10
9
  redis,
11
10
  KEYS,
12
11
  } from '../../lib/redis.js';
@@ -66,18 +65,8 @@ export default async function handler(req, res) {
66
65
  response,
67
66
  }));
68
67
 
69
- // Set TTL on pending list
70
- await redis.expire(pendingKey, 300); // 5 minutes
71
-
72
- // Log event to events list (single source of truth for UI)
73
- await addEvent(token, {
74
- timestamp: new Date().toISOString(),
75
- event: 'INTERACTION_SUBMITTED',
76
- slug,
77
- targetKey: targetKey || `_interaction_${slug}`,
78
- answer: responseString.substring(0, 200) + (responseString.length > 200 ? '...' : ''),
79
- source: 'remote',
80
- });
68
+ // Set TTL on pending list (24 hours - same as session, allows laptop sleep)
69
+ await redis.expire(pendingKey, 24 * 60 * 60);
81
70
 
82
71
  return res.status(200).json({ success: true });
83
72
  } catch (err) {