@duct-sdk/sdk 0.1.13 → 0.2.1
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/CHANGELOG.md +30 -0
- package/README.md +33 -22
- package/dist/adapters/next/shell.d.ts +1 -1
- package/dist/adapters/react/shell.d.ts +2 -2
- package/dist/adapters/react/shell.js +1 -1
- package/dist/adapters/react/use-page-context.d.ts +1 -1
- package/dist/cli/detectors/topology.d.ts +20 -0
- package/dist/cli/detectors/topology.js +107 -0
- package/dist/cli/generators/config.js +4 -4
- package/dist/cli/generators/shell.js +6 -6
- package/dist/cli/index.js +141 -3
- package/dist/cli/init.js +2 -3
- package/dist/cli/prompts/review.d.ts +25 -0
- package/dist/cli/prompts/review.js +108 -2
- package/dist/cli/selectors/express.d.ts +12 -0
- package/dist/cli/selectors/express.js +117 -0
- package/dist/cli/selectors/fastapi.d.ts +12 -0
- package/dist/cli/selectors/fastapi.js +122 -0
- package/dist/cli/selectors/generic.d.ts +10 -0
- package/dist/cli/selectors/generic.js +74 -0
- package/dist/cli/selectors/index.d.ts +15 -0
- package/dist/cli/selectors/index.js +41 -0
- package/dist/cli/selectors/next.d.ts +12 -0
- package/dist/cli/selectors/next.js +130 -0
- package/dist/core/action.js +0 -2
- package/dist/core/config.d.ts +5 -5
- package/dist/core/config.js +2 -2
- package/dist/core/endpoints.js +2 -2
- package/dist/core/status.d.ts +2 -2
- package/dist/core/status.js +4 -2
- package/dist/core/types.d.ts +10 -6
- package/package.json +6 -3
- package/dist/assets/logo.d.ts +0 -1
- package/dist/assets/logo.js +0 -1
|
@@ -30,3 +30,28 @@ export interface ReviewedPipelineResult {
|
|
|
30
30
|
* - `--no-interactive`: write unresolved decisions to duct-review.json
|
|
31
31
|
*/
|
|
32
32
|
export declare function runInteractiveReview(pipelineResult: PipelineResult, opts?: ReviewOptions): Promise<ReviewedPipelineResult>;
|
|
33
|
+
export interface InteractiveReviewOptions {
|
|
34
|
+
/** Auto-approve everything (--yes / CI mode) */
|
|
35
|
+
yes: boolean;
|
|
36
|
+
/** Write manifest to duct-review.json instead of prompting (--no-interactive) */
|
|
37
|
+
noInteractive: boolean;
|
|
38
|
+
/** Working directory for writing duct-review.json (defaults to cwd) */
|
|
39
|
+
cwd?: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Interactive review for a generated manifest.
|
|
43
|
+
*
|
|
44
|
+
* Decision rules:
|
|
45
|
+
* - `options.yes` → return manifest unchanged, all actions approved
|
|
46
|
+
* - `options.noInteractive` → write to duct-review.json, return manifest unchanged
|
|
47
|
+
* - Otherwise:
|
|
48
|
+
* - GET + confidence ≥ 0.85 → auto-approve (prints "✓ Auto-approved: [id]")
|
|
49
|
+
* - POST / DELETE / PATCH / PUT → always prompt
|
|
50
|
+
* - confidence < 0.85 → prompt regardless of method
|
|
51
|
+
* - Shows evidence: "Detected from: [files]"
|
|
52
|
+
*
|
|
53
|
+
* @param manifest The generated DuctConfig-like manifest (any shape, operates on .actions[])
|
|
54
|
+
* @param options Review mode flags
|
|
55
|
+
* @returns The manifest with only approved actions
|
|
56
|
+
*/
|
|
57
|
+
export declare function interactiveReview(manifest: any, options: InteractiveReviewOptions): Promise<any>;
|
|
@@ -45,7 +45,9 @@ function requiresPrompt(action) {
|
|
|
45
45
|
// ----------------------------------------------------------------
|
|
46
46
|
async function promptActionDecision(action, rl) {
|
|
47
47
|
const confidence = action.confidence ?? 0;
|
|
48
|
-
const evidence = action.evidence
|
|
48
|
+
const evidence = Array.isArray(action.evidence)
|
|
49
|
+
? action.evidence.join(", ")
|
|
50
|
+
: (action.evidence ?? "unknown source");
|
|
49
51
|
const method = methodColor(action.method);
|
|
50
52
|
const conf = confidenceColor(confidence);
|
|
51
53
|
const isSideEffect = action.sideEffects ||
|
|
@@ -136,7 +138,7 @@ export async function runInteractiveReview(pipelineResult, opts = {}) {
|
|
|
136
138
|
method: action.method,
|
|
137
139
|
path: action.path,
|
|
138
140
|
confidence: action.confidence ?? 0,
|
|
139
|
-
evidence: action.evidence,
|
|
141
|
+
evidence: Array.isArray(action.evidence) ? action.evidence.join(", ") : action.evidence,
|
|
140
142
|
reason: requiresPrompt(action)
|
|
141
143
|
? "Requires human review: side-effect or low confidence"
|
|
142
144
|
: "Pending",
|
|
@@ -238,3 +240,107 @@ export async function runInteractiveReview(pipelineResult, opts = {}) {
|
|
|
238
240
|
rejectedActions,
|
|
239
241
|
};
|
|
240
242
|
}
|
|
243
|
+
/**
|
|
244
|
+
* Interactive review for a generated manifest.
|
|
245
|
+
*
|
|
246
|
+
* Decision rules:
|
|
247
|
+
* - `options.yes` → return manifest unchanged, all actions approved
|
|
248
|
+
* - `options.noInteractive` → write to duct-review.json, return manifest unchanged
|
|
249
|
+
* - Otherwise:
|
|
250
|
+
* - GET + confidence ≥ 0.85 → auto-approve (prints "✓ Auto-approved: [id]")
|
|
251
|
+
* - POST / DELETE / PATCH / PUT → always prompt
|
|
252
|
+
* - confidence < 0.85 → prompt regardless of method
|
|
253
|
+
* - Shows evidence: "Detected from: [files]"
|
|
254
|
+
*
|
|
255
|
+
* @param manifest The generated DuctConfig-like manifest (any shape, operates on .actions[])
|
|
256
|
+
* @param options Review mode flags
|
|
257
|
+
* @returns The manifest with only approved actions
|
|
258
|
+
*/
|
|
259
|
+
export async function interactiveReview(
|
|
260
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
261
|
+
manifest, options
|
|
262
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
263
|
+
) {
|
|
264
|
+
const { yes, noInteractive, cwd = process.cwd() } = options;
|
|
265
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
266
|
+
const actions = Array.isArray(manifest?.actions) ? manifest.actions : [];
|
|
267
|
+
// ── --yes mode: return unchanged ─────────────────────────────
|
|
268
|
+
if (yes) {
|
|
269
|
+
return manifest;
|
|
270
|
+
}
|
|
271
|
+
// ── --no-interactive: write to duct-review.json ──────────────
|
|
272
|
+
if (noInteractive) {
|
|
273
|
+
const reviewContent = {
|
|
274
|
+
generatedAt: new Date().toISOString(),
|
|
275
|
+
pendingDecisions: actions
|
|
276
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
277
|
+
.filter((a) => {
|
|
278
|
+
const isSideEffect = ["POST", "PUT", "PATCH", "DELETE"].includes(a.method ?? "");
|
|
279
|
+
const lowConfidence = (a.confidence ?? 0) < 0.85;
|
|
280
|
+
return isSideEffect || lowConfidence;
|
|
281
|
+
})
|
|
282
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
283
|
+
.map((a) => ({
|
|
284
|
+
id: a.id,
|
|
285
|
+
method: a.method,
|
|
286
|
+
path: a.path,
|
|
287
|
+
confidence: a.confidence ?? 0,
|
|
288
|
+
evidence: Array.isArray(a.evidence) ? a.evidence.join(", ") : (a.evidence ?? ""),
|
|
289
|
+
reason: ["POST", "PUT", "PATCH", "DELETE"].includes(a.method ?? "")
|
|
290
|
+
? "Side-effect action requires review"
|
|
291
|
+
: "Low confidence detection",
|
|
292
|
+
})),
|
|
293
|
+
instructions: "Review each entry. Set `approved: true` for actions to include. Re-run `npx duct init` to apply.",
|
|
294
|
+
};
|
|
295
|
+
const reviewFilePath = path.join(cwd, "duct-review.json");
|
|
296
|
+
await fs.writeFile(reviewFilePath, JSON.stringify(reviewContent, null, 2) + "\n", "utf-8");
|
|
297
|
+
console.log(chalk.yellow(`\n [DUCT] --no-interactive: wrote ${reviewContent.pendingDecisions.length} pending decisions to duct-review.json\n`));
|
|
298
|
+
return manifest;
|
|
299
|
+
}
|
|
300
|
+
// ── Interactive mode ─────────────────────────────────────────
|
|
301
|
+
const approved = [];
|
|
302
|
+
const rl = readline.createInterface({
|
|
303
|
+
input: process.stdin,
|
|
304
|
+
output: process.stdout,
|
|
305
|
+
terminal: false,
|
|
306
|
+
});
|
|
307
|
+
for (const action of actions) {
|
|
308
|
+
const method = action.method ?? "GET";
|
|
309
|
+
const confidence = action.confidence ?? 0;
|
|
310
|
+
const isSafeGet = method === "GET" && confidence >= 0.85;
|
|
311
|
+
const isSideEffect = ["POST", "PUT", "PATCH", "DELETE"].includes(method);
|
|
312
|
+
if (isSafeGet) {
|
|
313
|
+
approved.push(action);
|
|
314
|
+
process.stdout.write(chalk.green(` ✓ Auto-approved: `) +
|
|
315
|
+
chalk.cyan(`[${action.id ?? action.path}]`) +
|
|
316
|
+
chalk.gray(` ${method} ${action.path ?? ""} ${Math.round(confidence * 100)}%\n`));
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
// Must prompt
|
|
320
|
+
const evidenceStr = Array.isArray(action.evidence)
|
|
321
|
+
? action.evidence.join(", ")
|
|
322
|
+
: (action.evidence ?? "unknown source");
|
|
323
|
+
const reason = isSideEffect
|
|
324
|
+
? "side-effect action"
|
|
325
|
+
: `confidence ${Math.round(confidence * 100)}% < 85%`;
|
|
326
|
+
process.stdout.write(`\n ${chalk.bold("[DUCT]")} ${methodColor(method)} ${chalk.cyan(action.path ?? "")} — ${reason}\n` +
|
|
327
|
+
` ${chalk.gray(`Confidence: ${confidenceColor(confidence)}`)}\n` +
|
|
328
|
+
` ${chalk.gray(`Detected from: ${chalk.dim(evidenceStr)}`)}\n` +
|
|
329
|
+
` ${chalk.gray(`Include action '${action.id ?? action.path}'` +
|
|
330
|
+
(action.description ? ` (${String(action.description).slice(0, 60)})` : "") +
|
|
331
|
+
`? [y/N]: `)}`);
|
|
332
|
+
const answer = await new Promise((resolve) => {
|
|
333
|
+
rl.once("line", (line) => resolve(line.trim().toLowerCase()));
|
|
334
|
+
});
|
|
335
|
+
if (answer === "y" || answer === "yes") {
|
|
336
|
+
approved.push(action);
|
|
337
|
+
console.log(chalk.green(` ✓ Included: ${action.id ?? action.path}`));
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
console.log(chalk.gray(` — Excluded: ${action.id ?? action.path}`));
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
rl.close();
|
|
344
|
+
console.log(chalk.bold(`\n Review complete: ${approved.length}/${actions.length} actions included\n`));
|
|
345
|
+
return { ...manifest, actions: approved };
|
|
346
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Select files for Express / Fastify / Koa projects.
|
|
3
|
+
*
|
|
4
|
+
* Priority:
|
|
5
|
+
* 1. Route files: routes/...ts, src/routes/...ts
|
|
6
|
+
* 2. Middleware: middleware/...ts, src/middleware/...ts
|
|
7
|
+
* 3. Entry points: src/index.ts, index.ts, app.ts, server.ts
|
|
8
|
+
*
|
|
9
|
+
* @param projectRoot Absolute project root path
|
|
10
|
+
* @param maxFiles Maximum total files to return (default 30)
|
|
11
|
+
*/
|
|
12
|
+
export declare function selectExpressFiles(projectRoot: string, maxFiles?: number): Promise<string[]>;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — Express / Fastify File Selector
|
|
3
|
+
// Selects route files, middleware, and entry points for Express-like projects.
|
|
4
|
+
// ============================================================
|
|
5
|
+
import * as fs from "fs/promises";
|
|
6
|
+
import * as path from "path";
|
|
7
|
+
import { glob } from "glob";
|
|
8
|
+
async function fileExists(filePath) {
|
|
9
|
+
try {
|
|
10
|
+
await fs.access(filePath);
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async function globFiles(root, pattern) {
|
|
18
|
+
try {
|
|
19
|
+
const matches = await glob(pattern, {
|
|
20
|
+
cwd: root,
|
|
21
|
+
ignore: ["**/node_modules/**", "**/dist/**", "**/build/**"],
|
|
22
|
+
nodir: true,
|
|
23
|
+
absolute: false,
|
|
24
|
+
});
|
|
25
|
+
return matches.sort();
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function getFileSize(filePath) {
|
|
32
|
+
try {
|
|
33
|
+
const s = await fs.stat(filePath);
|
|
34
|
+
return s.size;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async function limitFiles(root, files, maxFiles) {
|
|
41
|
+
if (files.length <= maxFiles)
|
|
42
|
+
return files;
|
|
43
|
+
const sized = await Promise.all(files.map(async (f) => ({ f, size: await getFileSize(path.join(root, f)) })));
|
|
44
|
+
sized.sort((a, b) => a.size - b.size);
|
|
45
|
+
return sized.slice(0, maxFiles).map((s) => s.f);
|
|
46
|
+
}
|
|
47
|
+
// ----------------------------------------------------------------
|
|
48
|
+
// Express selector — main export
|
|
49
|
+
// ----------------------------------------------------------------
|
|
50
|
+
/**
|
|
51
|
+
* Select files for Express / Fastify / Koa projects.
|
|
52
|
+
*
|
|
53
|
+
* Priority:
|
|
54
|
+
* 1. Route files: routes/...ts, src/routes/...ts
|
|
55
|
+
* 2. Middleware: middleware/...ts, src/middleware/...ts
|
|
56
|
+
* 3. Entry points: src/index.ts, index.ts, app.ts, server.ts
|
|
57
|
+
*
|
|
58
|
+
* @param projectRoot Absolute project root path
|
|
59
|
+
* @param maxFiles Maximum total files to return (default 30)
|
|
60
|
+
*/
|
|
61
|
+
export async function selectExpressFiles(projectRoot, maxFiles = 30) {
|
|
62
|
+
const priority1 = [];
|
|
63
|
+
const priority2 = [];
|
|
64
|
+
const priority3 = [];
|
|
65
|
+
// ── Priority 1: Route files ──────────────────────────────────
|
|
66
|
+
const routePatterns = [
|
|
67
|
+
"routes/**/*.{ts,js}",
|
|
68
|
+
"src/routes/**/*.{ts,js}",
|
|
69
|
+
"api/**/*.{ts,js}",
|
|
70
|
+
"src/api/**/*.{ts,js}",
|
|
71
|
+
];
|
|
72
|
+
for (const pat of routePatterns) {
|
|
73
|
+
const files = await globFiles(projectRoot, pat);
|
|
74
|
+
priority1.push(...files);
|
|
75
|
+
}
|
|
76
|
+
// ── Priority 2: Middleware ───────────────────────────────────
|
|
77
|
+
const middlewarePatterns = [
|
|
78
|
+
"middleware/**/*.{ts,js}",
|
|
79
|
+
"src/middleware/**/*.{ts,js}",
|
|
80
|
+
"middlewares/**/*.{ts,js}",
|
|
81
|
+
"src/middlewares/**/*.{ts,js}",
|
|
82
|
+
];
|
|
83
|
+
for (const pat of middlewarePatterns) {
|
|
84
|
+
const files = await globFiles(projectRoot, pat);
|
|
85
|
+
priority2.push(...files);
|
|
86
|
+
}
|
|
87
|
+
// ── Priority 3: Entry points ─────────────────────────────────
|
|
88
|
+
const entryCandidates = [
|
|
89
|
+
"src/index.ts",
|
|
90
|
+
"src/index.js",
|
|
91
|
+
"index.ts",
|
|
92
|
+
"index.js",
|
|
93
|
+
"app.ts",
|
|
94
|
+
"app.js",
|
|
95
|
+
"server.ts",
|
|
96
|
+
"server.js",
|
|
97
|
+
"src/app.ts",
|
|
98
|
+
"src/app.js",
|
|
99
|
+
"src/server.ts",
|
|
100
|
+
"src/server.js",
|
|
101
|
+
];
|
|
102
|
+
for (const candidate of entryCandidates) {
|
|
103
|
+
if (await fileExists(path.join(projectRoot, candidate))) {
|
|
104
|
+
priority3.push(candidate);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// ── Combine + deduplicate + limit ───────────────────────────
|
|
108
|
+
const seen = new Set();
|
|
109
|
+
const combined = [];
|
|
110
|
+
for (const f of [...priority1, ...priority2, ...priority3]) {
|
|
111
|
+
if (!seen.has(f)) {
|
|
112
|
+
seen.add(f);
|
|
113
|
+
combined.push(f);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return limitFiles(projectRoot, combined, maxFiles);
|
|
117
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Select files for FastAPI / Django projects.
|
|
3
|
+
*
|
|
4
|
+
* Priority:
|
|
5
|
+
* 1. Router files: routers/...py, api/...py
|
|
6
|
+
* 2. Model/schema files: models.py, schemas.py, models/...py
|
|
7
|
+
* 3. Entry points: main.py, app.py, urls.py (Django)
|
|
8
|
+
*
|
|
9
|
+
* @param projectRoot Absolute project root path
|
|
10
|
+
* @param maxFiles Maximum total files to return (default 30)
|
|
11
|
+
*/
|
|
12
|
+
export declare function selectFastApiFiles(projectRoot: string, maxFiles?: number): Promise<string[]>;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — FastAPI / Django File Selector
|
|
3
|
+
// Selects router files, models, and entry points for Python API projects.
|
|
4
|
+
// ============================================================
|
|
5
|
+
import * as fs from "fs/promises";
|
|
6
|
+
import * as path from "path";
|
|
7
|
+
import { glob } from "glob";
|
|
8
|
+
async function fileExists(filePath) {
|
|
9
|
+
try {
|
|
10
|
+
await fs.access(filePath);
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async function globFiles(root, pattern) {
|
|
18
|
+
try {
|
|
19
|
+
const matches = await glob(pattern, {
|
|
20
|
+
cwd: root,
|
|
21
|
+
ignore: ["**/__pycache__/**", "**/.venv/**", "**/venv/**", "**/dist/**"],
|
|
22
|
+
nodir: true,
|
|
23
|
+
absolute: false,
|
|
24
|
+
});
|
|
25
|
+
return matches.sort();
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function getFileSize(filePath) {
|
|
32
|
+
try {
|
|
33
|
+
const s = await fs.stat(filePath);
|
|
34
|
+
return s.size;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async function limitFiles(root, files, maxFiles) {
|
|
41
|
+
if (files.length <= maxFiles)
|
|
42
|
+
return files;
|
|
43
|
+
const sized = await Promise.all(files.map(async (f) => ({ f, size: await getFileSize(path.join(root, f)) })));
|
|
44
|
+
sized.sort((a, b) => a.size - b.size);
|
|
45
|
+
return sized.slice(0, maxFiles).map((s) => s.f);
|
|
46
|
+
}
|
|
47
|
+
// ----------------------------------------------------------------
|
|
48
|
+
// FastAPI selector — main export
|
|
49
|
+
// ----------------------------------------------------------------
|
|
50
|
+
/**
|
|
51
|
+
* Select files for FastAPI / Django projects.
|
|
52
|
+
*
|
|
53
|
+
* Priority:
|
|
54
|
+
* 1. Router files: routers/...py, api/...py
|
|
55
|
+
* 2. Model/schema files: models.py, schemas.py, models/...py
|
|
56
|
+
* 3. Entry points: main.py, app.py, urls.py (Django)
|
|
57
|
+
*
|
|
58
|
+
* @param projectRoot Absolute project root path
|
|
59
|
+
* @param maxFiles Maximum total files to return (default 30)
|
|
60
|
+
*/
|
|
61
|
+
export async function selectFastApiFiles(projectRoot, maxFiles = 30) {
|
|
62
|
+
const priority1 = [];
|
|
63
|
+
const priority2 = [];
|
|
64
|
+
const priority3 = [];
|
|
65
|
+
// ── Priority 1: Router / API files ──────────────────────────
|
|
66
|
+
const routerPatterns = [
|
|
67
|
+
"routers/**/*.py",
|
|
68
|
+
"router/**/*.py",
|
|
69
|
+
"api/**/*.py",
|
|
70
|
+
"src/api/**/*.py",
|
|
71
|
+
"app/api/**/*.py",
|
|
72
|
+
"endpoints/**/*.py",
|
|
73
|
+
"views/**/*.py", // Django
|
|
74
|
+
"urls.py", // Django root urls
|
|
75
|
+
"**/urls.py", // Django app-level urls
|
|
76
|
+
];
|
|
77
|
+
for (const pat of routerPatterns) {
|
|
78
|
+
const files = await globFiles(projectRoot, pat);
|
|
79
|
+
priority1.push(...files);
|
|
80
|
+
}
|
|
81
|
+
// ── Priority 2: Model / Schema files ────────────────────────
|
|
82
|
+
const modelCandidates = [
|
|
83
|
+
"models.py",
|
|
84
|
+
"schemas.py",
|
|
85
|
+
"serializers.py", // Django REST
|
|
86
|
+
"src/models.py",
|
|
87
|
+
"src/schemas.py",
|
|
88
|
+
"app/models.py",
|
|
89
|
+
];
|
|
90
|
+
for (const candidate of modelCandidates) {
|
|
91
|
+
if (await fileExists(path.join(projectRoot, candidate))) {
|
|
92
|
+
priority2.push(candidate);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const modelDirFiles = await globFiles(projectRoot, "models/**/*.py");
|
|
96
|
+
const schemasDirFiles = await globFiles(projectRoot, "schemas/**/*.py");
|
|
97
|
+
priority2.push(...modelDirFiles, ...schemasDirFiles);
|
|
98
|
+
// ── Priority 3: Entry points ─────────────────────────────────
|
|
99
|
+
const entryCandidates = [
|
|
100
|
+
"main.py",
|
|
101
|
+
"app.py",
|
|
102
|
+
"application.py",
|
|
103
|
+
"wsgi.py",
|
|
104
|
+
"asgi.py",
|
|
105
|
+
"manage.py", // Django
|
|
106
|
+
];
|
|
107
|
+
for (const candidate of entryCandidates) {
|
|
108
|
+
if (await fileExists(path.join(projectRoot, candidate))) {
|
|
109
|
+
priority3.push(candidate);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// ── Combine + deduplicate + limit ───────────────────────────
|
|
113
|
+
const seen = new Set();
|
|
114
|
+
const combined = [];
|
|
115
|
+
for (const f of [...priority1, ...priority2, ...priority3]) {
|
|
116
|
+
if (!seen.has(f)) {
|
|
117
|
+
seen.add(f);
|
|
118
|
+
combined.push(f);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return limitFiles(projectRoot, combined, maxFiles);
|
|
122
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic fallback file selector.
|
|
3
|
+
*
|
|
4
|
+
* Returns the top `maxFiles` .ts/.js/.py files sorted by file size descending
|
|
5
|
+
* (larger files are more likely to contain route/handler definitions).
|
|
6
|
+
*
|
|
7
|
+
* @param projectRoot Absolute project root path
|
|
8
|
+
* @param maxFiles Maximum total files to return (default 30)
|
|
9
|
+
*/
|
|
10
|
+
export declare function selectGenericFiles(projectRoot: string, maxFiles?: number): Promise<string[]>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — Generic File Selector (fallback)
|
|
3
|
+
// Returns the top maxFiles .ts/.js/.py files sorted by file size
|
|
4
|
+
// (larger files are more likely to contain route definitions).
|
|
5
|
+
// ============================================================
|
|
6
|
+
import * as fs from "fs/promises";
|
|
7
|
+
import * as path from "path";
|
|
8
|
+
import { glob } from "glob";
|
|
9
|
+
async function globFiles(root, pattern) {
|
|
10
|
+
try {
|
|
11
|
+
const matches = await glob(pattern, {
|
|
12
|
+
cwd: root,
|
|
13
|
+
ignore: [
|
|
14
|
+
"**/node_modules/**",
|
|
15
|
+
"**/.git/**",
|
|
16
|
+
"**/dist/**",
|
|
17
|
+
"**/build/**",
|
|
18
|
+
"**/out/**",
|
|
19
|
+
"**/.next/**",
|
|
20
|
+
"**/__pycache__/**",
|
|
21
|
+
"**/.venv/**",
|
|
22
|
+
"**/venv/**",
|
|
23
|
+
"**/*.test.{ts,js}",
|
|
24
|
+
"**/*.spec.{ts,js}",
|
|
25
|
+
"**/*.d.ts",
|
|
26
|
+
],
|
|
27
|
+
nodir: true,
|
|
28
|
+
absolute: false,
|
|
29
|
+
});
|
|
30
|
+
return matches;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function getFileSize(filePath) {
|
|
37
|
+
try {
|
|
38
|
+
const s = await fs.stat(filePath);
|
|
39
|
+
return s.size;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// ----------------------------------------------------------------
|
|
46
|
+
// Generic selector — main export
|
|
47
|
+
// ----------------------------------------------------------------
|
|
48
|
+
/**
|
|
49
|
+
* Generic fallback file selector.
|
|
50
|
+
*
|
|
51
|
+
* Returns the top `maxFiles` .ts/.js/.py files sorted by file size descending
|
|
52
|
+
* (larger files are more likely to contain route/handler definitions).
|
|
53
|
+
*
|
|
54
|
+
* @param projectRoot Absolute project root path
|
|
55
|
+
* @param maxFiles Maximum total files to return (default 30)
|
|
56
|
+
*/
|
|
57
|
+
export async function selectGenericFiles(projectRoot, maxFiles = 30) {
|
|
58
|
+
const allFiles = [];
|
|
59
|
+
const tsFiles = await globFiles(projectRoot, "**/*.{ts,tsx}");
|
|
60
|
+
const jsFiles = await globFiles(projectRoot, "**/*.{js,jsx,mjs}");
|
|
61
|
+
const pyFiles = await globFiles(projectRoot, "**/*.py");
|
|
62
|
+
allFiles.push(...tsFiles, ...jsFiles, ...pyFiles);
|
|
63
|
+
if (allFiles.length === 0)
|
|
64
|
+
return [];
|
|
65
|
+
// Deduplicate
|
|
66
|
+
const unique = [...new Set(allFiles)];
|
|
67
|
+
// Sort by file size descending — larger = more likely to contain route definitions
|
|
68
|
+
const sized = await Promise.all(unique.map(async (f) => ({
|
|
69
|
+
f,
|
|
70
|
+
size: await getFileSize(path.join(projectRoot, f)),
|
|
71
|
+
})));
|
|
72
|
+
sized.sort((a, b) => b.size - a.size);
|
|
73
|
+
return sized.slice(0, maxFiles).map((s) => s.f);
|
|
74
|
+
}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import type { SandboxFS } from "../fs/sandbox.js";
|
|
2
2
|
import type { TopoResult } from "../detectors/topology.js";
|
|
3
|
+
export { selectNextFiles } from "./next.js";
|
|
4
|
+
export { selectExpressFiles } from "./express.js";
|
|
5
|
+
export { selectFastApiFiles } from "./fastapi.js";
|
|
6
|
+
export { selectGenericFiles } from "./generic.js";
|
|
3
7
|
/** Per-agent bounded file list, max MAX_FILES_PER_LIST entries each. */
|
|
4
8
|
export interface ReadList {
|
|
5
9
|
/** Files for the Route Agent — page files, layouts */
|
|
@@ -9,6 +13,17 @@ export interface ReadList {
|
|
|
9
13
|
/** Files for the Auth Agent — middleware, auth config, env examples */
|
|
10
14
|
authFiles: string[];
|
|
11
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Select the most relevant source files for a given project topology.
|
|
18
|
+
*
|
|
19
|
+
* Returns a flat, deduplicated list of relative file paths capped at
|
|
20
|
+
* `maxFiles`. Files are ordered by relevance: API routes first, then
|
|
21
|
+
* type definitions, then entry points / middleware.
|
|
22
|
+
*
|
|
23
|
+
* Delegates to framework-specific selectors (next, express, fastapi) or
|
|
24
|
+
* falls back to the generic size-ranked selector for unknown stacks.
|
|
25
|
+
*/
|
|
26
|
+
export declare function selectFilesForFramework(topo: TopoResult, maxFiles?: number): Promise<string[]>;
|
|
12
27
|
/**
|
|
13
28
|
* Build a bounded ReadList per agent from the project's topology.
|
|
14
29
|
*
|
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
// Builds a bounded ReadList per agent from a TopoResult.
|
|
4
4
|
// No LLM. Max MAX_FILES_PER_LIST files per agent list.
|
|
5
5
|
// ============================================================
|
|
6
|
+
import { selectNextFiles } from "./next.js";
|
|
7
|
+
import { selectExpressFiles } from "./express.js";
|
|
8
|
+
import { selectFastApiFiles } from "./fastapi.js";
|
|
9
|
+
import { selectGenericFiles } from "./generic.js";
|
|
10
|
+
export { selectNextFiles } from "./next.js";
|
|
11
|
+
export { selectExpressFiles } from "./express.js";
|
|
12
|
+
export { selectFastApiFiles } from "./fastapi.js";
|
|
13
|
+
export { selectGenericFiles } from "./generic.js";
|
|
6
14
|
const MAX_FILES_PER_LIST = 30;
|
|
7
15
|
// ----------------------------------------------------------------
|
|
8
16
|
// Ranking helpers
|
|
@@ -236,6 +244,39 @@ async function selectGenericFallback(sandboxFs, apiRoot) {
|
|
|
236
244
|
};
|
|
237
245
|
}
|
|
238
246
|
// ----------------------------------------------------------------
|
|
247
|
+
// selectFilesForFramework — flat file list for downstream consumers
|
|
248
|
+
// ----------------------------------------------------------------
|
|
249
|
+
/**
|
|
250
|
+
* Select the most relevant source files for a given project topology.
|
|
251
|
+
*
|
|
252
|
+
* Returns a flat, deduplicated list of relative file paths capped at
|
|
253
|
+
* `maxFiles`. Files are ordered by relevance: API routes first, then
|
|
254
|
+
* type definitions, then entry points / middleware.
|
|
255
|
+
*
|
|
256
|
+
* Delegates to framework-specific selectors (next, express, fastapi) or
|
|
257
|
+
* falls back to the generic size-ranked selector for unknown stacks.
|
|
258
|
+
*/
|
|
259
|
+
export async function selectFilesForFramework(topo, maxFiles = 30) {
|
|
260
|
+
const root = topo.projectRoot ?? process.cwd();
|
|
261
|
+
switch (topo.framework) {
|
|
262
|
+
case "next-app":
|
|
263
|
+
case "next-pages":
|
|
264
|
+
return selectNextFiles(root, maxFiles);
|
|
265
|
+
case "remix":
|
|
266
|
+
case "sveltekit":
|
|
267
|
+
case "nuxt":
|
|
268
|
+
case "vue3":
|
|
269
|
+
case "vite-react":
|
|
270
|
+
// These frameworks share the Express-like pattern for backend selection
|
|
271
|
+
return selectExpressFiles(root, maxFiles);
|
|
272
|
+
default:
|
|
273
|
+
if (topo.language === "python") {
|
|
274
|
+
return selectFastApiFiles(root, maxFiles);
|
|
275
|
+
}
|
|
276
|
+
return selectGenericFiles(root, maxFiles);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
// ----------------------------------------------------------------
|
|
239
280
|
// buildReadList — main export
|
|
240
281
|
// ----------------------------------------------------------------
|
|
241
282
|
/**
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Select files for Next.js projects (App Router + Pages Router).
|
|
3
|
+
*
|
|
4
|
+
* Priority:
|
|
5
|
+
* 1. API route files: app/api/.../route.ts, pages/api/...ts
|
|
6
|
+
* 2. Type files: types/...ts, lib/types.ts, src/types.ts
|
|
7
|
+
* 3. Auth middleware: middleware.ts, app/middleware.ts
|
|
8
|
+
*
|
|
9
|
+
* @param projectRoot Absolute project root path
|
|
10
|
+
* @param maxFiles Maximum total files to return (default 30)
|
|
11
|
+
*/
|
|
12
|
+
export declare function selectNextFiles(projectRoot: string, maxFiles?: number): Promise<string[]>;
|