ai-engineering-loop 1.2.1 → 1.6.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.
- package/README.md +47 -4
- package/README.npm.md +7 -0
- package/SECURITY.md +3 -1
- package/agents/recipe-builder.md +28 -0
- package/bin/ai-engineering-loop.js +432 -11
- package/core/controlled-workflow-runtime.md +77 -0
- package/core/local-workflow-studio.md +77 -0
- package/core/workflow-recipes.md +140 -0
- package/lib/budget.js +42 -4
- package/lib/doctor.js +26 -2
- package/lib/handoff.js +209 -0
- package/lib/node-registry.js +155 -0
- package/lib/recipe-builder.js +200 -0
- package/lib/recipe.js +495 -0
- package/lib/run-state.js +50 -3
- package/lib/runtime-files.js +2 -0
- package/lib/studio-server.js +183 -0
- package/lib/workflow-runtime.js +718 -0
- package/package.json +3 -1
- package/recipes/audit.json +32 -0
- package/recipes/bugfix.json +63 -0
- package/recipes/default.json +49 -0
- package/recipes/docs-light.json +63 -0
- package/recipes/high-risk.json +69 -0
- package/recipes/refactor.json +63 -0
- package/schemas/compiled-plan.schema.json +19 -0
- package/schemas/handoff.schema.json +36 -0
- package/schemas/node-definition.schema.json +33 -0
- package/schemas/recipe.schema.json +26 -0
- package/schemas/workflow-event.schema.json +29 -0
- package/schemas/workflow-state.schema.json +29 -0
- package/studio/app.js +461 -0
- package/studio/index.html +31 -0
- package/studio/styles.css +75 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const http = require('http');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { URL } = require('url');
|
|
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');
|
|
11
|
+
const { budgetStatus } = require('./budget.js');
|
|
12
|
+
const {
|
|
13
|
+
workflowStatus,
|
|
14
|
+
approveWorkflowNode,
|
|
15
|
+
retryWorkflowNode,
|
|
16
|
+
recordWorkflowActivity
|
|
17
|
+
} = require('./workflow-runtime.js');
|
|
18
|
+
const { createHandoff, handoffBrief, recordDecision } = require('./handoff.js');
|
|
19
|
+
|
|
20
|
+
const MAX_BODY = 256 * 1024;
|
|
21
|
+
const ASSET_DIR = path.join(__dirname, '..', 'studio');
|
|
22
|
+
|
|
23
|
+
function json(response, status, value) {
|
|
24
|
+
response.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
25
|
+
response.end(JSON.stringify(value));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function readBody(request) {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
let text = '';
|
|
31
|
+
request.setEncoding('utf8');
|
|
32
|
+
request.on('data', (chunk) => {
|
|
33
|
+
text += chunk;
|
|
34
|
+
if (Buffer.byteLength(text) > MAX_BODY) request.destroy(new Error('Request body exceeds 256 KiB'));
|
|
35
|
+
});
|
|
36
|
+
request.on('end', () => {
|
|
37
|
+
try { resolve(text ? JSON.parse(text) : {}); } catch (error) { reject(error); }
|
|
38
|
+
});
|
|
39
|
+
request.on('error', reject);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function liveSnapshot(rootDir) {
|
|
44
|
+
const run = getCurrentRun(rootDir);
|
|
45
|
+
if (!run?.workflow) return null;
|
|
46
|
+
const workflow = workflowStatus(rootDir, { runId: run.runId });
|
|
47
|
+
return {
|
|
48
|
+
run,
|
|
49
|
+
plan: workflow.plan,
|
|
50
|
+
nodes: workflow.state.nodes,
|
|
51
|
+
budget: budgetStatus(rootDir, { runId: run.runId }),
|
|
52
|
+
events: workflow.events.slice(-50).map(({ initialNodes, changes, ...event }) => ({
|
|
53
|
+
...event,
|
|
54
|
+
changedNodes: (changes || []).map((change) => change.nodeId)
|
|
55
|
+
}))
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function bootstrap(rootDir) {
|
|
60
|
+
const recipes = listRecipes(rootDir).map(({ id, source }) => ({ ...inspectRecipe(rootDir, id), source }));
|
|
61
|
+
return { recipes, nodeTypes: recipeCatalog(), live: liveSnapshot(rootDir) };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function constantTimeEqual(left, right) {
|
|
65
|
+
const a = Buffer.from(String(left || ''));
|
|
66
|
+
const b = Buffer.from(String(right || ''));
|
|
67
|
+
return a.length === b.length && crypto.timingSafeEqual(a, b);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function localHostname(hostHeader) {
|
|
71
|
+
try {
|
|
72
|
+
return new URL(`http://${hostHeader}`).hostname.replace(/^\[|\]$/g, '');
|
|
73
|
+
} catch {
|
|
74
|
+
return '';
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function createStudioServer(rootDir, { token = crypto.randomBytes(24).toString('base64url') } = {}) {
|
|
79
|
+
const server = http.createServer(async (request, response) => {
|
|
80
|
+
const hostHeader = String(request.headers.host || '');
|
|
81
|
+
const host = localHostname(hostHeader);
|
|
82
|
+
if (!['127.0.0.1', 'localhost', '::1'].includes(host)) {
|
|
83
|
+
response.writeHead(403).end('Forbidden host');
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const url = new URL(request.url, `http://${hostHeader}`);
|
|
87
|
+
const cookieToken = String(request.headers.cookie || '').split(';').map((item) => item.trim())
|
|
88
|
+
.find((item) => item.startsWith('ael_studio='))?.slice('ael_studio='.length);
|
|
89
|
+
const supplied = request.headers['x-ael-studio-token'] || cookieToken;
|
|
90
|
+
if (url.pathname === '/' && constantTimeEqual(url.searchParams.get('token'), token)) {
|
|
91
|
+
response.writeHead(302, {
|
|
92
|
+
location: '/',
|
|
93
|
+
'set-cookie': `ael_studio=${token}; HttpOnly; SameSite=Strict; Path=/`,
|
|
94
|
+
'cache-control': 'no-store',
|
|
95
|
+
'referrer-policy': 'no-referrer'
|
|
96
|
+
}).end();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (!constantTimeEqual(supplied, token)) {
|
|
100
|
+
response.writeHead(401).end('Studio session required');
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
response.setHeader('content-security-policy', "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'");
|
|
104
|
+
response.setHeader('x-content-type-options', 'nosniff');
|
|
105
|
+
response.setHeader('referrer-policy', 'no-referrer');
|
|
106
|
+
try {
|
|
107
|
+
if (request.method === 'GET' && url.pathname === '/api/bootstrap') {
|
|
108
|
+
json(response, 200, { ok: true, ...bootstrap(rootDir) });
|
|
109
|
+
} else if (request.method === 'GET' && url.pathname === '/api/live') {
|
|
110
|
+
json(response, 200, { ok: true, live: liveSnapshot(rootDir) });
|
|
111
|
+
} else if (request.method === 'GET' && url.pathname === '/api/recipe') {
|
|
112
|
+
const loaded = loadRecipe(rootDir, url.searchParams.get('id'));
|
|
113
|
+
json(response, 200, { ok: true, source: loaded.source, recipe: loaded.recipe });
|
|
114
|
+
} else if (request.method === 'POST' && url.pathname === '/api/validate') {
|
|
115
|
+
const body = await readBody(request);
|
|
116
|
+
const validation = validateRecipe(body.recipe, body.mode ? { mode: body.mode } : {});
|
|
117
|
+
json(response, validation.valid ? 200 : 422, {
|
|
118
|
+
ok: validation.valid,
|
|
119
|
+
validation,
|
|
120
|
+
plans: validation.valid
|
|
121
|
+
? (body.mode ? [body.mode] : body.recipe.compatibleModes).map((mode) => compileRecipe(body.recipe, { mode }))
|
|
122
|
+
: []
|
|
123
|
+
});
|
|
124
|
+
} else if (request.method === 'POST' && url.pathname === '/api/install') {
|
|
125
|
+
const body = await readBody(request);
|
|
126
|
+
const drafts = path.join(rootDir, '.ai-engineering-loop', 'drafts');
|
|
127
|
+
fs.mkdirSync(drafts, { recursive: true, mode: 0o700 });
|
|
128
|
+
const candidate = path.join(drafts, `${crypto.randomUUID()}.json`);
|
|
129
|
+
fs.writeFileSync(candidate, JSON.stringify(body.recipe), { mode: 0o600 });
|
|
130
|
+
try {
|
|
131
|
+
const result = installRecipe(rootDir, path.relative(rootDir, candidate), { replace: body.replace === true });
|
|
132
|
+
json(response, 200, { ok: true, id: result.recipe.id, version: result.recipe.version, sourceHash: result.sourceHash });
|
|
133
|
+
} finally {
|
|
134
|
+
try { fs.unlinkSync(candidate); } catch {}
|
|
135
|
+
}
|
|
136
|
+
} else if (request.method === 'GET' && url.pathname === '/api/handoff') {
|
|
137
|
+
const bundle = createHandoff(rootDir, { audience: url.searchParams.get('audience') || 'developer' });
|
|
138
|
+
json(response, 200, { ok: true, bundle, brief: handoffBrief(bundle) });
|
|
139
|
+
} else if (request.method === 'POST' && url.pathname === '/api/node/approve') {
|
|
140
|
+
const body = await readBody(request);
|
|
141
|
+
approveWorkflowNode(rootDir, body.nodeId, { runId: body.runId, approvedBy: body.approvedBy || 'human' });
|
|
142
|
+
json(response, 200, { ok: true });
|
|
143
|
+
} else if (request.method === 'POST' && url.pathname === '/api/node/retry') {
|
|
144
|
+
const body = await readBody(request);
|
|
145
|
+
retryWorkflowNode(rootDir, body.nodeId, { runId: body.runId });
|
|
146
|
+
json(response, 200, { ok: true });
|
|
147
|
+
} else if (request.method === 'POST' && url.pathname === '/api/node/activity') {
|
|
148
|
+
const body = await readBody(request);
|
|
149
|
+
recordWorkflowActivity(rootDir, body.nodeId, {
|
|
150
|
+
runId: body.runId,
|
|
151
|
+
message: body.message
|
|
152
|
+
});
|
|
153
|
+
json(response, 200, { ok: true });
|
|
154
|
+
} else if (request.method === 'POST' && url.pathname === '/api/decision') {
|
|
155
|
+
const body = await readBody(request);
|
|
156
|
+
const result = recordDecision(rootDir, {
|
|
157
|
+
runId: body.runId,
|
|
158
|
+
summary: body.summary,
|
|
159
|
+
rationale: body.rationale,
|
|
160
|
+
consequences: body.consequences
|
|
161
|
+
});
|
|
162
|
+
json(response, 200, { ok: true, ...result });
|
|
163
|
+
} else {
|
|
164
|
+
const asset = url.pathname === '/' ? 'index.html' : url.pathname.slice(1);
|
|
165
|
+
if (!['index.html', 'app.js', 'styles.css'].includes(asset)) {
|
|
166
|
+
response.writeHead(404).end('Not found');
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const types = { html: 'text/html', js: 'text/javascript', css: 'text/css' };
|
|
170
|
+
response.writeHead(200, {
|
|
171
|
+
'content-type': `${types[asset.split('.').at(-1)]}; charset=utf-8`,
|
|
172
|
+
'cache-control': 'no-store'
|
|
173
|
+
});
|
|
174
|
+
response.end(fs.readFileSync(path.join(ASSET_DIR, asset)));
|
|
175
|
+
}
|
|
176
|
+
} catch (error) {
|
|
177
|
+
json(response, 400, { ok: false, code: error.code || 'STUDIO_ERROR', error: error.message, details: error.details || [] });
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
return { server, token };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
module.exports = { createStudioServer, bootstrap, liveSnapshot };
|