ai-engineering-loop 1.7.0 → 1.9.0

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.
@@ -6,15 +6,40 @@ const http = require('http');
6
6
  const path = require('path');
7
7
  const { URL } = require('url');
8
8
  const { listRecipes, loadRecipe, validateRecipe, compileRecipe } = require('./recipe.js');
9
- const { inspectRecipe, installRecipe, recipeCatalog } = require('./recipe-builder.js');
10
- const { getCurrentRun } = require('./run-state.js');
9
+ const {
10
+ applyRecipeImport,
11
+ inspectRecipe,
12
+ installRecipe,
13
+ planRecipeImport,
14
+ recipeCatalog
15
+ } = require('./recipe-builder.js');
16
+ const { getCurrentRun, updateRunDisplayName } = require('./run-state.js');
11
17
  const { budgetStatus } = require('./budget.js');
12
18
  const {
13
19
  workflowStatus,
14
20
  approveWorkflowNode,
21
+ completeWorkflowNode,
22
+ failWorkflowNode,
15
23
  retryWorkflowNode,
16
- recordWorkflowActivity
24
+ startWorkflowNode,
25
+ recordWorkflowActivity,
26
+ resolveLoopLimit,
27
+ resolveWorkflowDecision
17
28
  } = require('./workflow-runtime.js');
29
+ const { listRunHistory, runHistoryDetail, inspectRunArtifact } = require('./run-history.js');
30
+ const {
31
+ duplicateRunAsRecipe,
32
+ runCheckout,
33
+ runNodeIo,
34
+ studioAgentHandoff
35
+ } = require('./run-checkout.js');
36
+ const {
37
+ appendRunAnswer,
38
+ appendRunQuestion,
39
+ listRunInteractions
40
+ } = require('./run-interactions.js');
41
+ const { saveGoalDraft, freezeGoal, unfreezeGoal } = require('./goal-runtime.js');
42
+ const { createStudioRun, executeStudioRun } = require('./studio-runtime.js');
18
43
  const { createHandoff, handoffBrief, recordDecision } = require('./handoff.js');
19
44
  const { loadStudioLayout, saveStudioLayout } = require('./studio-layout.js');
20
45
  const {
@@ -54,6 +79,7 @@ function liveSnapshot(rootDir) {
54
79
  run,
55
80
  plan: workflow.plan,
56
81
  nodes: workflow.state.nodes,
82
+ loopGroups: workflow.state.loopGroups,
57
83
  budget: budgetStatus(rootDir, { runId: run.runId }),
58
84
  events: workflow.events.slice(-50).map(({ initialNodes, changes, ...event }) => ({
59
85
  ...event,
@@ -121,10 +147,220 @@ function createStudioServer(rootDir, { token = crypto.randomBytes(24).toString('
121
147
  response.setHeader('x-content-type-options', 'nosniff');
122
148
  response.setHeader('referrer-policy', 'no-referrer');
123
149
  try {
150
+ const scopedRun = url.pathname.match(/^\/api\/runs\/([^/]+)$/);
151
+ const scopedArtifact = url.pathname.match(/^\/api\/runs\/([^/]+)\/artifacts$/);
152
+ const scopedExecute = url.pathname.match(/^\/api\/runs\/([^/]+)\/execute$/);
153
+ const scopedGoal = url.pathname.match(/^\/api\/runs\/([^/]+)\/goal$/);
154
+ const scopedGoalFreeze = url.pathname.match(/^\/api\/runs\/([^/]+)\/goal\/freeze$/);
155
+ const scopedGoalUnfreeze = url.pathname.match(/^\/api\/runs\/([^/]+)\/goal\/unfreeze$/);
156
+ const scopedCheckout = url.pathname.match(/^\/api\/runs\/([^/]+)\/checkout$/);
157
+ const scopedRunNode = url.pathname.match(/^\/api\/runs\/([^/]+)\/nodes\/([^/]+)$/);
158
+ const scopedDecision = url.pathname.match(/^\/api\/runs\/([^/]+)\/nodes\/([^/]+)\/decision$/);
159
+ const scopedLoopLimit = url.pathname.match(/^\/api\/runs\/([^/]+)\/loops\/([^/]+)\/limit$/);
160
+ const scopedDuplicate = url.pathname.match(/^\/api\/runs\/([^/]+)\/duplicate$/);
161
+ const scopedAgentHandoff = url.pathname.match(/^\/api\/runs\/([^/]+)\/agent-handoff$/);
162
+ const scopedInteractions = url.pathname.match(/^\/api\/runs\/([^/]+)\/interactions$/);
163
+ const scopedQuestions = url.pathname.match(/^\/api\/runs\/([^/]+)\/questions$/);
164
+ const scopedAnswer = url.pathname.match(/^\/api\/runs\/([^/]+)\/questions\/([^/]+)\/answer$/);
124
165
  if (request.method === 'GET' && url.pathname === '/api/bootstrap') {
125
166
  json(response, 200, { ok: true, ...bootstrap(rootDir) });
126
167
  } else if (request.method === 'GET' && url.pathname === '/api/live') {
127
168
  json(response, 200, { ok: true, live: liveSnapshot(rootDir) });
169
+ } else if (request.method === 'GET' && url.pathname === '/api/runs') {
170
+ json(response, 200, {
171
+ ok: true,
172
+ ...listRunHistory(rootDir, {
173
+ query: url.searchParams.get('q') || '',
174
+ status: url.searchParams.get('status') || undefined,
175
+ mode: url.searchParams.get('mode') || undefined,
176
+ from: url.searchParams.get('from') || undefined,
177
+ to: url.searchParams.get('to') || undefined
178
+ })
179
+ });
180
+ } else if (request.method === 'GET' && scopedRun) {
181
+ json(response, 200, {
182
+ ok: true,
183
+ run: runHistoryDetail(rootDir, decodeURIComponent(scopedRun[1]))
184
+ });
185
+ } else if (request.method === 'GET' && scopedArtifact) {
186
+ json(response, 200, {
187
+ ok: true,
188
+ artifact: inspectRunArtifact(
189
+ rootDir,
190
+ decodeURIComponent(scopedArtifact[1]),
191
+ url.searchParams.get('path')
192
+ )
193
+ });
194
+ } else if (request.method === 'GET' && scopedCheckout) {
195
+ json(response, 200, {
196
+ ok: true,
197
+ checkout: runCheckout(rootDir, decodeURIComponent(scopedCheckout[1]))
198
+ });
199
+ } else if (request.method === 'POST' && scopedDecision) {
200
+ const body = await readBody(request);
201
+ const workflow = resolveWorkflowDecision(
202
+ rootDir,
203
+ decodeURIComponent(scopedDecision[2]),
204
+ {
205
+ runId: decodeURIComponent(scopedDecision[1]),
206
+ outcome: body.outcome,
207
+ actor: body.actor || 'studio-user'
208
+ }
209
+ );
210
+ json(response, 200, { ok: true, nodes: workflow.state.nodes, loopGroups: workflow.state.loopGroups });
211
+ } else if (request.method === 'POST' && scopedLoopLimit) {
212
+ const body = await readBody(request);
213
+ const workflow = resolveLoopLimit(
214
+ rootDir,
215
+ decodeURIComponent(scopedLoopLimit[2]),
216
+ {
217
+ runId: decodeURIComponent(scopedLoopLimit[1]),
218
+ action: body.action,
219
+ expectedIteration: body.expectedIteration,
220
+ actor: body.actor || 'studio-user',
221
+ reason: body.reason
222
+ }
223
+ );
224
+ json(response, 200, { ok: true, nodes: workflow.state.nodes, loopGroups: workflow.state.loopGroups });
225
+ } else if (request.method === 'GET' && scopedRunNode) {
226
+ json(response, 200, {
227
+ ok: true,
228
+ io: runNodeIo(
229
+ rootDir,
230
+ decodeURIComponent(scopedRunNode[1]),
231
+ decodeURIComponent(scopedRunNode[2])
232
+ )
233
+ });
234
+ } else if (request.method === 'POST' && scopedDuplicate) {
235
+ const body = await readBody(request);
236
+ json(response, 200, {
237
+ ok: true,
238
+ preview: duplicateRunAsRecipe(rootDir, decodeURIComponent(scopedDuplicate[1]), body)
239
+ });
240
+ } else if (request.method === 'GET' && scopedAgentHandoff) {
241
+ json(response, 200, {
242
+ ok: true,
243
+ handoff: studioAgentHandoff(rootDir, decodeURIComponent(scopedAgentHandoff[1]))
244
+ });
245
+ } else if (request.method === 'GET' && scopedInteractions) {
246
+ json(response, 200, {
247
+ ok: true,
248
+ interactions: listRunInteractions(rootDir, decodeURIComponent(scopedInteractions[1]))
249
+ });
250
+ } else if (request.method === 'POST' && scopedQuestions) {
251
+ const body = await readBody(request);
252
+ json(response, 201, {
253
+ ok: true,
254
+ question: appendRunQuestion(rootDir, decodeURIComponent(scopedQuestions[1]), body)
255
+ });
256
+ } else if (request.method === 'POST' && scopedAnswer) {
257
+ const body = await readBody(request);
258
+ json(response, 201, {
259
+ ok: true,
260
+ answer: appendRunAnswer(
261
+ rootDir,
262
+ decodeURIComponent(scopedAnswer[1]),
263
+ decodeURIComponent(scopedAnswer[2]),
264
+ body
265
+ )
266
+ });
267
+ } else if (request.method === 'POST' && scopedExecute) {
268
+ const result = executeStudioRun(rootDir, decodeURIComponent(scopedExecute[1]));
269
+ json(response, 200, {
270
+ ok: true,
271
+ runId: result.workflow.run.runId,
272
+ execution: {
273
+ dispatch: 'LOCAL_RUNTIME_ONLY',
274
+ externalDispatch: false,
275
+ triggerNodeId: result.workflow.events.at(-1).triggerNodeId
276
+ },
277
+ nodes: result.workflow.state.nodes,
278
+ budget: result.budget
279
+ });
280
+ } else if (request.method === 'PUT' && scopedGoal) {
281
+ const body = await readBody(request);
282
+ const goal = saveGoalDraft(rootDir, decodeURIComponent(scopedGoal[1]), body.goal);
283
+ json(response, 200, { ok: true, goal, frozen: false });
284
+ } else if (request.method === 'POST' && scopedGoalFreeze) {
285
+ const body = await readBody(request);
286
+ const run = freezeGoal(rootDir, decodeURIComponent(scopedGoalFreeze[1]), { actor: body.actor });
287
+ json(response, 200, { ok: true, run });
288
+ } else if (request.method === 'POST' && scopedGoalUnfreeze) {
289
+ const body = await readBody(request);
290
+ const run = unfreezeGoal(rootDir, decodeURIComponent(scopedGoalUnfreeze[1]), {
291
+ actor: body.actor,
292
+ reason: body.reason
293
+ });
294
+ json(response, 200, { ok: true, run });
295
+ } else if (request.method === 'GET' && url.pathname === '/api/run') {
296
+ json(response, 200, { ok: true, run: runHistoryDetail(rootDir, url.searchParams.get('runId')) });
297
+ } else if (request.method === 'GET' && url.pathname === '/api/run/artifact') {
298
+ json(response, 200, {
299
+ ok: true,
300
+ artifact: inspectRunArtifact(
301
+ rootDir,
302
+ url.searchParams.get('runId'),
303
+ url.searchParams.get('path')
304
+ )
305
+ });
306
+ } else if (request.method === 'POST' && ['/api/runs', '/api/run/create'].includes(url.pathname)) {
307
+ const body = await readBody(request);
308
+ const run = createStudioRun(rootDir, body);
309
+ json(response, 201, { ok: true, run });
310
+ } else if (request.method === 'PATCH' && url.pathname === '/api/run/name') {
311
+ const body = await readBody(request);
312
+ const run = updateRunDisplayName(rootDir, body.runId, body.displayName, {
313
+ actor: body.actor || 'human'
314
+ });
315
+ json(response, 200, { ok: true, run });
316
+ } else if (request.method === 'PUT' && ['/api/goal', '/api/goal/save'].includes(url.pathname)) {
317
+ const body = await readBody(request);
318
+ const goal = saveGoalDraft(rootDir, body.runId, body.goal);
319
+ json(response, 200, { ok: true, goal, frozen: false });
320
+ } else if (request.method === 'POST' && url.pathname === '/api/goal/freeze') {
321
+ const body = await readBody(request);
322
+ const run = freezeGoal(rootDir, body.runId, { actor: body.actor });
323
+ json(response, 200, { ok: true, run });
324
+ } else if (request.method === 'POST' && url.pathname === '/api/goal/unfreeze') {
325
+ const body = await readBody(request);
326
+ const run = unfreezeGoal(rootDir, body.runId, {
327
+ actor: body.actor,
328
+ reason: body.reason
329
+ });
330
+ json(response, 200, { ok: true, run });
331
+ } else if (request.method === 'POST' && ['/api/run/execute', '/api/execute'].includes(url.pathname)) {
332
+ const body = await readBody(request);
333
+ const result = executeStudioRun(rootDir, body.runId);
334
+ json(response, 200, {
335
+ ok: true,
336
+ runId: result.workflow.run.runId,
337
+ execution: {
338
+ dispatch: 'LOCAL_RUNTIME_ONLY',
339
+ externalDispatch: false,
340
+ triggerNodeId: result.workflow.events.at(-1).triggerNodeId
341
+ },
342
+ nodes: result.workflow.state.nodes,
343
+ budget: result.budget
344
+ });
345
+ } else if (request.method === 'POST' && url.pathname === '/api/import/preview') {
346
+ const body = await readBody(request);
347
+ const plan = planRecipeImport(
348
+ body.existingRecipe,
349
+ body.importedRecipe,
350
+ body.operation || 'merge'
351
+ );
352
+ json(response, 200, { ok: true, plan });
353
+ } else if (request.method === 'POST' && url.pathname === '/api/import/apply') {
354
+ const body = await readBody(request);
355
+ const recipe = applyRecipeImport(body.existingRecipe, body.importedRecipe, body.plan);
356
+ const validation = validateRecipe(recipe);
357
+ if (!validation.valid) {
358
+ const error = new Error('Imported workflow is invalid after applying the preview');
359
+ error.code = 'INVALID_RECIPE';
360
+ error.details = validation.errors;
361
+ throw error;
362
+ }
363
+ json(response, 200, { ok: true, recipe, validation });
128
364
  } else if (request.method === 'GET' && url.pathname === '/api/recipe') {
129
365
  const loaded = loadRecipe(rootDir, url.searchParams.get('id'));
130
366
  json(response, 200, { ok: true, source: loaded.source, recipe: loaded.recipe });
@@ -188,6 +424,18 @@ function createStudioServer(rootDir, { token = crypto.randomBytes(24).toString('
188
424
  const body = await readBody(request);
189
425
  approveWorkflowNode(rootDir, body.nodeId, { runId: body.runId, approvedBy: body.approvedBy || 'human' });
190
426
  json(response, 200, { ok: true });
427
+ } else if (request.method === 'POST' && url.pathname === '/api/node/start') {
428
+ const body = await readBody(request);
429
+ startWorkflowNode(rootDir, body.nodeId, { runId: body.runId });
430
+ json(response, 200, { ok: true });
431
+ } else if (request.method === 'POST' && url.pathname === '/api/node/complete') {
432
+ const body = await readBody(request);
433
+ completeWorkflowNode(rootDir, body.nodeId, { runId: body.runId, artifact: body.artifact });
434
+ json(response, 200, { ok: true });
435
+ } else if (request.method === 'POST' && url.pathname === '/api/node/fail') {
436
+ const body = await readBody(request);
437
+ failWorkflowNode(rootDir, body.nodeId, { runId: body.runId, reason: body.reason });
438
+ json(response, 200, { ok: true });
191
439
  } else if (request.method === 'POST' && url.pathname === '/api/node/retry') {
192
440
  const body = await readBody(request);
193
441
  retryWorkflowNode(rootDir, body.nodeId, { runId: body.runId });