test-workflow-run-sync.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env node
  2. import assert from 'assert';
  3. import fs from 'fs';
  4. import os from 'os';
  5. import path from 'path';
  6. import { ToolRegistry } from './src/core/tool-registry.js';
  7. import { WorkflowExecutor } from './src/vl/workflow-executor.js';
  8. import { createReadFileTool } from './src/tools/read-file.js';
  9. import { createWorkflowRunTool } from './src/tools/workflow-run.js';
  10. function createTempProject() {
  11. const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'vlcode-workflow-run-sync-'));
  12. fs.mkdirSync(path.join(dir, '.vl-code'), { recursive: true });
  13. fs.writeFileSync(path.join(dir, '.vl-code', 'workspace.json'), '{}');
  14. return dir;
  15. }
  16. async function main() {
  17. const workDir = createTempProject();
  18. const outputDirRel = 'Artifacts';
  19. const outputDir = path.join(workDir, 'Artifacts');
  20. fs.mkdirSync(outputDir, { recursive: true });
  21. const workerSpecPath = path.join(workDir, 'worker-spec.json');
  22. const workerReviewPath = path.join(workDir, 'worker-review.json');
  23. const supervisorPath = path.join(workDir, 'vlclaw-supervisor.json');
  24. fs.writeFileSync(workerSpecPath, JSON.stringify({
  25. version: '3.16',
  26. name: 'SpecWorker',
  27. steps: [
  28. { id: 'Set_010_Content', target: '$content', value: '="SPEC:" + goal', next: 'Write_020_File' },
  29. { id: 'Write_020_File', target: '=artifactPath', value: '=$content', mode: 'overwrite', next: 'Stop_End' },
  30. { id: 'Stop_End' },
  31. ],
  32. }, null, 2), 'utf8');
  33. fs.writeFileSync(workerReviewPath, JSON.stringify({
  34. version: '3.16',
  35. name: 'ReviewWorker',
  36. steps: [
  37. { id: 'Set_010_Content', target: '$content', value: '="REVIEW:" + goal', next: 'Write_020_File' },
  38. { id: 'Write_020_File', target: '=artifactPath', value: '=$content', mode: 'overwrite', next: 'Stop_End' },
  39. { id: 'Stop_End' },
  40. ],
  41. }, null, 2), 'utf8');
  42. fs.writeFileSync(supervisorPath, JSON.stringify({
  43. version: '3.16',
  44. name: 'VLClawSupervisorPrototype',
  45. steps: [
  46. { id: 'Set_010_SpecPath', target: '$specPath', value: '=outputDir + "/spec.txt"', next: 'Set_020_ReviewPath' },
  47. { id: 'Set_020_ReviewPath', target: '$reviewPath', value: '=outputDir + "/review.txt"', next: 'Fork_030_Dispatch' },
  48. { id: 'Fork_030_Dispatch', children: ['Tool_031_RunSpecWorker', 'Tool_032_RunReviewWorker'], next: 'Tool_040_ReadSpec' },
  49. {
  50. id: 'Tool_031_RunSpecWorker',
  51. tool: 'WorkflowRun',
  52. input: {
  53. mode: 'sync',
  54. workflow_path: '="worker-spec.json"',
  55. params: { goal: '=goal', artifactPath: '=$specPath' },
  56. },
  57. out: { '$specRun': '=_result' },
  58. next: 'RETURN',
  59. },
  60. {
  61. id: 'Tool_032_RunReviewWorker',
  62. tool: 'WorkflowRun',
  63. input: {
  64. mode: 'sync',
  65. workflow_path: '="worker-review.json"',
  66. params: { goal: '=goal', artifactPath: '=$reviewPath' },
  67. },
  68. out: { '$reviewRun': '=_result' },
  69. next: 'RETURN',
  70. },
  71. {
  72. id: 'Tool_040_ReadSpec',
  73. tool: 'ReadFile',
  74. input: { file_path: '=$specPath' },
  75. out: { '$specText': '=_result' },
  76. next: 'Tool_050_ReadReview',
  77. },
  78. {
  79. id: 'Tool_050_ReadReview',
  80. tool: 'ReadFile',
  81. input: { file_path: '=$reviewPath' },
  82. out: { '$reviewText': '=_result' },
  83. next: 'Set_060_Summary',
  84. },
  85. {
  86. id: 'Set_060_Summary',
  87. target: '$summary',
  88. value: '="goal=" + goal + "; spec=" + $specRun.variables["$content"] + "; review=" + $reviewRun.variables["$content"]',
  89. next: 'Write_070_Report',
  90. },
  91. {
  92. id: 'Write_070_Report',
  93. target: '=outputDir + "/supervisor.txt"',
  94. value: '=$summary',
  95. mode: 'overwrite',
  96. next: 'Stop_End',
  97. },
  98. { id: 'Stop_End' },
  99. ],
  100. }, null, 2), 'utf8');
  101. const toolRegistry = new ToolRegistry();
  102. const config = {
  103. workDir,
  104. model: 'claude-opus-4-6',
  105. llmProvider: 'cli',
  106. toolRegistry,
  107. };
  108. toolRegistry.register('ReadFile', createReadFileTool(config));
  109. toolRegistry.register('WorkflowRun', createWorkflowRunTool(config));
  110. const executor = new WorkflowExecutor({
  111. workDir,
  112. model: 'claude-opus-4-6',
  113. llmProvider: 'cli',
  114. toolRegistry,
  115. });
  116. const toolMessages = [];
  117. await executor.execute(JSON.parse(fs.readFileSync(supervisorPath, 'utf8')), {
  118. goal: 'Land the VLClaw manager workflow',
  119. outputDir: outputDirRel,
  120. }, {
  121. onToolMessage: (info) => toolMessages.push(info),
  122. });
  123. const spec = fs.readFileSync(path.join(outputDir, 'spec.txt'), 'utf8');
  124. const review = fs.readFileSync(path.join(outputDir, 'review.txt'), 'utf8');
  125. const supervisor = fs.readFileSync(path.join(outputDir, 'supervisor.txt'), 'utf8');
  126. assert.equal(spec, 'SPEC:Land the VLClaw manager workflow');
  127. assert.equal(review, 'REVIEW:Land the VLClaw manager workflow');
  128. assert.equal(
  129. supervisor,
  130. 'goal=Land the VLClaw manager workflow; spec=SPEC:Land the VLClaw manager workflow; review=REVIEW:Land the VLClaw manager workflow'
  131. );
  132. assert(toolMessages.some((info) =>
  133. info.name === 'WorkflowRun'
  134. && info.data?.workflowName === 'SpecWorker'
  135. && info.data?.event === 'node_start'
  136. ), 'missing nested SpecWorker node_start message');
  137. assert(toolMessages.some((info) =>
  138. info.name === 'WorkflowRun'
  139. && info.data?.workflowName === 'ReviewWorker'
  140. && info.data?.event === 'done'
  141. ), 'missing nested ReviewWorker done message');
  142. fs.rmSync(workDir, { recursive: true, force: true });
  143. console.log('\n── WorkflowRun Sync Orchestration ──');
  144. console.log('PASS test-workflow-run-sync.js');
  145. process.exit(0);
  146. }
  147. main().catch((err) => {
  148. console.error(err);
  149. process.exit(1);
  150. });