@duct-sdk/sdk 0.1.6
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 +92 -0
- package/dist/adapters/next/index.d.ts +4 -0
- package/dist/adapters/next/index.js +5 -0
- package/dist/adapters/next/receiver.d.ts +44 -0
- package/dist/adapters/next/receiver.js +162 -0
- package/dist/adapters/next/shell.d.ts +88 -0
- package/dist/adapters/next/shell.js +255 -0
- package/dist/adapters/react/index.d.ts +2 -0
- package/dist/adapters/react/index.js +4 -0
- package/dist/adapters/react/shell.d.ts +88 -0
- package/dist/adapters/react/shell.js +255 -0
- package/dist/adapters/remix/index.d.ts +2 -0
- package/dist/adapters/remix/index.js +4 -0
- package/dist/adapters/remix/receiver.d.ts +44 -0
- package/dist/adapters/remix/receiver.js +153 -0
- package/dist/adapters/sveltekit/index.d.ts +2 -0
- package/dist/adapters/sveltekit/index.js +4 -0
- package/dist/adapters/sveltekit/receiver.d.ts +56 -0
- package/dist/adapters/sveltekit/receiver.js +154 -0
- package/dist/adapters/vite/index.d.ts +2 -0
- package/dist/adapters/vite/index.js +4 -0
- package/dist/adapters/vite/receiver.d.ts +82 -0
- package/dist/adapters/vite/receiver.js +164 -0
- package/dist/assets/logo.d.ts +1 -0
- package/dist/assets/logo.js +1 -0
- package/dist/cli/agents/api-agent.d.ts +14 -0
- package/dist/cli/agents/api-agent.js +656 -0
- package/dist/cli/agents/auth-agent.d.ts +6 -0
- package/dist/cli/agents/auth-agent.js +198 -0
- package/dist/cli/agents/deeplink-agent.d.ts +6 -0
- package/dist/cli/agents/deeplink-agent.js +263 -0
- package/dist/cli/agents/llm.d.ts +33 -0
- package/dist/cli/agents/llm.js +276 -0
- package/dist/cli/agents/pipeline.d.ts +51 -0
- package/dist/cli/agents/pipeline.js +236 -0
- package/dist/cli/agents/reviewer-agent.d.ts +11 -0
- package/dist/cli/agents/reviewer-agent.js +442 -0
- package/dist/cli/agents/route-agent.d.ts +13 -0
- package/dist/cli/agents/route-agent.js +346 -0
- package/dist/cli/detectors/auth.d.ts +18 -0
- package/dist/cli/detectors/auth.js +273 -0
- package/dist/cli/detectors/framework.d.ts +22 -0
- package/dist/cli/detectors/framework.js +242 -0
- package/dist/cli/drift.d.ts +39 -0
- package/dist/cli/drift.js +247 -0
- package/dist/cli/fs/sandbox.d.ts +77 -0
- package/dist/cli/fs/sandbox.js +295 -0
- package/dist/cli/generators/config.d.ts +7 -0
- package/dist/cli/generators/config.js +255 -0
- package/dist/cli/generators/receiver.d.ts +12 -0
- package/dist/cli/generators/receiver.js +683 -0
- package/dist/cli/generators/shell.d.ts +18 -0
- package/dist/cli/generators/shell.js +672 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +603 -0
- package/dist/cli/init.d.ts +26 -0
- package/dist/cli/init.js +835 -0
- package/dist/core/action.d.ts +37 -0
- package/dist/core/action.js +140 -0
- package/dist/core/config.d.ts +514 -0
- package/dist/core/config.js +221 -0
- package/dist/core/endpoints.d.ts +17 -0
- package/dist/core/endpoints.js +128 -0
- package/dist/core/token.d.ts +70 -0
- package/dist/core/token.js +174 -0
- package/dist/core/types.d.ts +450 -0
- package/dist/core/types.js +11 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +9 -0
- package/package.json +84 -0
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — SandboxFS
|
|
3
|
+
// Secure filesystem wrapper for CLI agent use
|
|
4
|
+
// ============================================================
|
|
5
|
+
import * as fs from "fs/promises";
|
|
6
|
+
import * as path from "path";
|
|
7
|
+
import { glob } from "glob";
|
|
8
|
+
// ----------------------------------------------------------------
|
|
9
|
+
// Block/Allow lists
|
|
10
|
+
// ----------------------------------------------------------------
|
|
11
|
+
const BLOCKED_PATTERNS = [
|
|
12
|
+
/^\.env$/i,
|
|
13
|
+
/^\.env\./i,
|
|
14
|
+
/\.secret\./i,
|
|
15
|
+
/private/i,
|
|
16
|
+
/node_modules/,
|
|
17
|
+
/^\.git\//,
|
|
18
|
+
/^\.git$/,
|
|
19
|
+
/\.lock$/,
|
|
20
|
+
/^package-lock\.json$/,
|
|
21
|
+
/^yarn\.lock$/,
|
|
22
|
+
/^pnpm-lock\.yaml$/,
|
|
23
|
+
/\.pem$/i,
|
|
24
|
+
/\.key$/i,
|
|
25
|
+
/\.p12$/i,
|
|
26
|
+
/\.pfx$/i,
|
|
27
|
+
/\.crt$/i,
|
|
28
|
+
/\.cert$/i,
|
|
29
|
+
];
|
|
30
|
+
const ALLOWED_EXTENSIONS = new Set([
|
|
31
|
+
".ts",
|
|
32
|
+
".tsx",
|
|
33
|
+
".js",
|
|
34
|
+
".jsx",
|
|
35
|
+
".mjs",
|
|
36
|
+
".cjs",
|
|
37
|
+
".json",
|
|
38
|
+
".yaml",
|
|
39
|
+
".yml",
|
|
40
|
+
".toml",
|
|
41
|
+
".md",
|
|
42
|
+
".mdx",
|
|
43
|
+
// Backend framework support — Python and Ruby are safe to read
|
|
44
|
+
".py",
|
|
45
|
+
".rb",
|
|
46
|
+
]);
|
|
47
|
+
// Maximum file size to read (500KB)
|
|
48
|
+
const MAX_FILE_SIZE = 500 * 1024;
|
|
49
|
+
// ----------------------------------------------------------------
|
|
50
|
+
// SandboxFS class
|
|
51
|
+
// ----------------------------------------------------------------
|
|
52
|
+
export class SandboxFS {
|
|
53
|
+
root;
|
|
54
|
+
constructor(projectRoot) {
|
|
55
|
+
this.root = path.resolve(projectRoot);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Get the project root directory.
|
|
59
|
+
*/
|
|
60
|
+
get projectRoot() {
|
|
61
|
+
return this.root;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Resolve a relative path within the sandbox.
|
|
65
|
+
* Throws if the resolved path escapes the root.
|
|
66
|
+
*/
|
|
67
|
+
resolveSafe(relativePath) {
|
|
68
|
+
// Normalize and resolve
|
|
69
|
+
const normalized = path.normalize(relativePath);
|
|
70
|
+
const absolute = path.resolve(this.root, normalized);
|
|
71
|
+
// Path traversal check
|
|
72
|
+
if (!absolute.startsWith(this.root + path.sep) && absolute !== this.root) {
|
|
73
|
+
throw new Error(`Path traversal detected: "${relativePath}" resolves outside project root`);
|
|
74
|
+
}
|
|
75
|
+
return absolute;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Get the relative path from root, used for block pattern matching.
|
|
79
|
+
*/
|
|
80
|
+
getRelative(absolutePath) {
|
|
81
|
+
return path.relative(this.root, absolutePath);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Check whether a path is blocked by security rules.
|
|
85
|
+
*/
|
|
86
|
+
isBlocked(relativePath) {
|
|
87
|
+
const normalized = relativePath.replace(/\\/g, "/");
|
|
88
|
+
// Check each path segment for blocked patterns
|
|
89
|
+
const segments = normalized.split("/");
|
|
90
|
+
for (const segment of segments) {
|
|
91
|
+
for (const pattern of BLOCKED_PATTERNS) {
|
|
92
|
+
if (pattern.test(segment)) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// Also check the full path
|
|
98
|
+
for (const pattern of BLOCKED_PATTERNS) {
|
|
99
|
+
if (pattern.test(normalized)) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Check whether a file extension is in the allow list.
|
|
107
|
+
*/
|
|
108
|
+
isAllowedExtension(filePath) {
|
|
109
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
110
|
+
// Files with no extension may be config files (e.g., .gitignore-like)
|
|
111
|
+
if (!ext) {
|
|
112
|
+
const basename = path.basename(filePath);
|
|
113
|
+
// Allow well-known config files without extension
|
|
114
|
+
return ["dockerfile", "makefile", "procfile", "gemfile"].includes(basename.toLowerCase());
|
|
115
|
+
}
|
|
116
|
+
return ALLOWED_EXTENSIONS.has(ext);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Read a file within the sandbox.
|
|
120
|
+
* Returns null if the file doesn't exist or is blocked.
|
|
121
|
+
*/
|
|
122
|
+
async readFile(relativePath) {
|
|
123
|
+
try {
|
|
124
|
+
if (this.isBlocked(relativePath)) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
if (!this.isAllowedExtension(relativePath)) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
const absolute = this.resolveSafe(relativePath);
|
|
131
|
+
const stat = await fs.stat(absolute);
|
|
132
|
+
if (!stat.isFile()) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
if (stat.size > MAX_FILE_SIZE) {
|
|
136
|
+
// Return a truncated notice instead of null so agents know the file exists
|
|
137
|
+
return `[File truncated: ${stat.size} bytes exceeds ${MAX_FILE_SIZE} byte limit. First portion shown.]\n`;
|
|
138
|
+
}
|
|
139
|
+
return await fs.readFile(absolute, "utf-8");
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
if (err.code === "ENOENT") {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
throw err;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* List directory contents within the sandbox.
|
|
150
|
+
* Returns entries with type information, filtering blocked items.
|
|
151
|
+
*/
|
|
152
|
+
async listDir(relativePath = ".") {
|
|
153
|
+
if (this.isBlocked(relativePath)) {
|
|
154
|
+
return [];
|
|
155
|
+
}
|
|
156
|
+
const absolute = this.resolveSafe(relativePath);
|
|
157
|
+
try {
|
|
158
|
+
const entries = await fs.readdir(absolute, { withFileTypes: true });
|
|
159
|
+
const results = [];
|
|
160
|
+
for (const entry of entries) {
|
|
161
|
+
const entryRelative = path.join(relativePath, entry.name).replace(/\\/g, "/");
|
|
162
|
+
if (this.isBlocked(entryRelative)) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (entry.isFile() && !this.isAllowedExtension(entry.name)) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
results.push({
|
|
169
|
+
name: entry.name,
|
|
170
|
+
type: entry.isDirectory() ? "dir" : "file",
|
|
171
|
+
path: entryRelative,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
return results;
|
|
175
|
+
}
|
|
176
|
+
catch (err) {
|
|
177
|
+
if (err.code === "ENOENT") {
|
|
178
|
+
return [];
|
|
179
|
+
}
|
|
180
|
+
throw err;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Check if a path exists within the sandbox.
|
|
185
|
+
*/
|
|
186
|
+
async exists(relativePath) {
|
|
187
|
+
if (this.isBlocked(relativePath)) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
try {
|
|
191
|
+
const absolute = this.resolveSafe(relativePath);
|
|
192
|
+
await fs.access(absolute);
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Check if a path is a directory.
|
|
201
|
+
*/
|
|
202
|
+
async isDirectory(relativePath) {
|
|
203
|
+
if (this.isBlocked(relativePath)) {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
const absolute = this.resolveSafe(relativePath);
|
|
208
|
+
const stat = await fs.stat(absolute);
|
|
209
|
+
return stat.isDirectory();
|
|
210
|
+
}
|
|
211
|
+
catch {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Find files matching a glob pattern within the sandbox.
|
|
217
|
+
* Blocked files and disallowed extensions are filtered out.
|
|
218
|
+
*/
|
|
219
|
+
async findFiles(pattern, options = {}) {
|
|
220
|
+
const { maxFiles = 200 } = options;
|
|
221
|
+
try {
|
|
222
|
+
const matches = await glob(pattern, {
|
|
223
|
+
cwd: this.root,
|
|
224
|
+
absolute: false,
|
|
225
|
+
ignore: [
|
|
226
|
+
"node_modules/**",
|
|
227
|
+
".git/**",
|
|
228
|
+
"**/.env",
|
|
229
|
+
"**/.env.*",
|
|
230
|
+
"**/*.secret.*",
|
|
231
|
+
"**/*private*",
|
|
232
|
+
"**/*.lock",
|
|
233
|
+
"**/package-lock.json",
|
|
234
|
+
"**/yarn.lock",
|
|
235
|
+
"**/pnpm-lock.yaml",
|
|
236
|
+
],
|
|
237
|
+
nodir: true,
|
|
238
|
+
});
|
|
239
|
+
return matches
|
|
240
|
+
.filter((f) => !this.isBlocked(f) && this.isAllowedExtension(f))
|
|
241
|
+
.slice(0, maxFiles)
|
|
242
|
+
.map((f) => f.replace(/\\/g, "/"));
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
return [];
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Read multiple files, returning a map of relative path → content.
|
|
250
|
+
* Silently skips blocked or non-existent files.
|
|
251
|
+
*/
|
|
252
|
+
async readFiles(relativePaths) {
|
|
253
|
+
const results = {};
|
|
254
|
+
await Promise.all(relativePaths.map(async (p) => {
|
|
255
|
+
const content = await this.readFile(p);
|
|
256
|
+
if (content !== null) {
|
|
257
|
+
results[p] = content;
|
|
258
|
+
}
|
|
259
|
+
}));
|
|
260
|
+
return results;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Read a JSON file and parse it.
|
|
264
|
+
* Returns null if the file doesn't exist, is blocked, or fails to parse.
|
|
265
|
+
*/
|
|
266
|
+
async readJSON(relativePath) {
|
|
267
|
+
const content = await this.readFile(relativePath);
|
|
268
|
+
if (content === null)
|
|
269
|
+
return null;
|
|
270
|
+
try {
|
|
271
|
+
return JSON.parse(content);
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Get the relative path of a file, useful for display.
|
|
279
|
+
*/
|
|
280
|
+
relativize(absolutePath) {
|
|
281
|
+
return this.getRelative(absolutePath).replace(/\\/g, "/");
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Return a new SandboxFS rooted at a subdirectory of this instance.
|
|
285
|
+
* All paths passed to the returned instance are resolved relative to
|
|
286
|
+
* `path.join(this.root, subdir)` — path-traversal protections still apply.
|
|
287
|
+
*
|
|
288
|
+
* This is the idiomatic way to scope a scanner to a monorepo subdirectory
|
|
289
|
+
* (e.g., `sandboxFs.scoped("frontend")`).
|
|
290
|
+
*/
|
|
291
|
+
scoped(subdir) {
|
|
292
|
+
const newRoot = path.join(this.root, subdir);
|
|
293
|
+
return new SandboxFS(newRoot);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { DuctConfig } from "../../core/types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Generate the contents of a `duct.config.ts` file from a DuctConfig object.
|
|
4
|
+
*
|
|
5
|
+
* @returns A formatted TypeScript source string
|
|
6
|
+
*/
|
|
7
|
+
export declare function generateConfigFile(config: DuctConfig): string;
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — Config File Generator
|
|
3
|
+
// Generates duct.config.ts from a DuctConfig object
|
|
4
|
+
// ============================================================
|
|
5
|
+
// ----------------------------------------------------------------
|
|
6
|
+
// Helpers
|
|
7
|
+
// ----------------------------------------------------------------
|
|
8
|
+
function indent(str, spaces) {
|
|
9
|
+
const pad = " ".repeat(spaces);
|
|
10
|
+
return str
|
|
11
|
+
.split("\n")
|
|
12
|
+
.map((line) => (line.trim() === "" ? "" : pad + line))
|
|
13
|
+
.join("\n");
|
|
14
|
+
}
|
|
15
|
+
function jsonValue(value, indentLevel = 0) {
|
|
16
|
+
if (value === null)
|
|
17
|
+
return "null";
|
|
18
|
+
if (typeof value === "boolean")
|
|
19
|
+
return String(value);
|
|
20
|
+
if (typeof value === "number")
|
|
21
|
+
return String(value);
|
|
22
|
+
if (typeof value === "string")
|
|
23
|
+
return JSON.stringify(value);
|
|
24
|
+
if (Array.isArray(value)) {
|
|
25
|
+
if (value.length === 0)
|
|
26
|
+
return "[]";
|
|
27
|
+
const items = value
|
|
28
|
+
.map((v) => indent(jsonValue(v, indentLevel + 2), indentLevel + 2))
|
|
29
|
+
.join(",\n");
|
|
30
|
+
return `[\n${items},\n${" ".repeat(indentLevel)}]`;
|
|
31
|
+
}
|
|
32
|
+
if (typeof value === "object" && value !== null) {
|
|
33
|
+
const entries = Object.entries(value).filter(([, v]) => v !== undefined);
|
|
34
|
+
if (entries.length === 0)
|
|
35
|
+
return "{}";
|
|
36
|
+
const lines = entries
|
|
37
|
+
.map(([k, v]) => `${" ".repeat(indentLevel + 2)}${k}: ${jsonValue(v, indentLevel + 2)}`)
|
|
38
|
+
.join(",\n");
|
|
39
|
+
return `{\n${lines},\n${" ".repeat(indentLevel)}}`;
|
|
40
|
+
}
|
|
41
|
+
return JSON.stringify(value);
|
|
42
|
+
}
|
|
43
|
+
function formatStringArray(arr, indentSpaces) {
|
|
44
|
+
if (arr.length === 0)
|
|
45
|
+
return "[]";
|
|
46
|
+
const pad = " ".repeat(indentSpaces);
|
|
47
|
+
const inner = arr.map((s) => `${pad} ${JSON.stringify(s)}`).join(",\n");
|
|
48
|
+
return `[\n${inner},\n${pad}]`;
|
|
49
|
+
}
|
|
50
|
+
// ----------------------------------------------------------------
|
|
51
|
+
// Section generators
|
|
52
|
+
// ----------------------------------------------------------------
|
|
53
|
+
function generateRoutes(routes, indentSpaces) {
|
|
54
|
+
if (routes.length === 0) {
|
|
55
|
+
return "routes: [],";
|
|
56
|
+
}
|
|
57
|
+
const pad = " ".repeat(indentSpaces);
|
|
58
|
+
const routeBlocks = routes.map((route) => {
|
|
59
|
+
const params = route.params.length > 0
|
|
60
|
+
? `\n${pad} params: [\n${route.params
|
|
61
|
+
.map((p) => `${pad} { name: ${JSON.stringify(p.name)}, type: ${JSON.stringify(p.type)}, required: ${p.required}${p.description ? `, description: ${JSON.stringify(p.description)}` : ""} }`)
|
|
62
|
+
.join(",\n")}\n${pad} ],`
|
|
63
|
+
: `\n${pad} params: [],`;
|
|
64
|
+
const queryParams = route.queryParams.length > 0
|
|
65
|
+
? `\n${pad} queryParams: [\n${route.queryParams
|
|
66
|
+
.map((p) => `${pad} { name: ${JSON.stringify(p.name)}, type: ${JSON.stringify(p.type)}, required: ${p.required}${p.description ? `, description: ${JSON.stringify(p.description)}` : ""}${p.defaultValue !== undefined ? `, defaultValue: ${JSON.stringify(p.defaultValue)}` : ""} }`)
|
|
67
|
+
.join(",\n")}\n${pad} ],`
|
|
68
|
+
: `\n${pad} queryParams: [],`;
|
|
69
|
+
return `${pad} {
|
|
70
|
+
${pad} path: ${JSON.stringify(route.path)},
|
|
71
|
+
${pad} name: ${JSON.stringify(route.name)},
|
|
72
|
+
${pad} description: ${JSON.stringify(route.description)},${params}${queryParams}
|
|
73
|
+
${pad} uiStateHint: ${JSON.stringify(route.uiStateHint)},
|
|
74
|
+
${pad} requiresAuth: ${route.requiresAuth},
|
|
75
|
+
${pad} deeplinkable: ${route.deeplinkable},
|
|
76
|
+
${pad} }`;
|
|
77
|
+
});
|
|
78
|
+
return `routes: [\n${routeBlocks.join(",\n")},\n${pad}],`;
|
|
79
|
+
}
|
|
80
|
+
function generateActions(actions, indentSpaces) {
|
|
81
|
+
if (actions.length === 0) {
|
|
82
|
+
return "actions: [],";
|
|
83
|
+
}
|
|
84
|
+
const pad = " ".repeat(indentSpaces);
|
|
85
|
+
const actionBlocks = actions.map((action) => {
|
|
86
|
+
const params = action.params.length > 0
|
|
87
|
+
? `\n${pad} params: [\n${action.params
|
|
88
|
+
.map((p) => `${pad} { name: ${JSON.stringify(p.name)}, type: ${JSON.stringify(p.type)}, required: ${p.required}${p.description ? `, description: ${JSON.stringify(p.description)}` : ""} }`)
|
|
89
|
+
.join(",\n")}\n${pad} ],`
|
|
90
|
+
: `\n${pad} params: [],`;
|
|
91
|
+
return `${pad} {
|
|
92
|
+
${pad} id: ${JSON.stringify(action.id)},
|
|
93
|
+
${pad} name: ${JSON.stringify(action.name)},
|
|
94
|
+
${pad} description: ${JSON.stringify(action.description)},
|
|
95
|
+
${pad} method: ${JSON.stringify(action.method)},
|
|
96
|
+
${pad} path: ${JSON.stringify(action.path)},
|
|
97
|
+
${pad} // Set agentAccessible: true to expose this action to AI agents via the Duct Agent API.
|
|
98
|
+
${pad} // Actions with sideEffects: true require a user consent token regardless.
|
|
99
|
+
${pad} // Default: false for all write operations (safe default).
|
|
100
|
+
${pad} agentAccessible: ${action.agentAccessible},
|
|
101
|
+
${pad} sideEffects: ${action.sideEffects},
|
|
102
|
+
${pad} // sensitivityLevel controls the minimum auth required for agent access.
|
|
103
|
+
${pad} // "low" — any authenticated shell
|
|
104
|
+
${pad} // "medium" — requires user consent token
|
|
105
|
+
${pad} // "high" — requires user consent token + explicit allowedCallers entry
|
|
106
|
+
${pad} sensitivityLevel: ${JSON.stringify(action.sensitivityLevel)},${params}
|
|
107
|
+
${pad} requiresAuth: ${action.requiresAuth},${action.rateLimit !== undefined ? `\n${pad} rateLimit: ${action.rateLimit},` : ""}
|
|
108
|
+
${pad} }`;
|
|
109
|
+
});
|
|
110
|
+
return `actions: [\n${actionBlocks.join(",\n")},\n${pad}],`;
|
|
111
|
+
}
|
|
112
|
+
function generateAuth(auth, indentSpaces) {
|
|
113
|
+
const pad = " ".repeat(indentSpaces);
|
|
114
|
+
const lines = [
|
|
115
|
+
`${pad} strategy: ${JSON.stringify(auth.strategy)},`,
|
|
116
|
+
`${pad} sessionFields: ${formatStringArray(auth.sessionFields, indentSpaces + 2)},`,
|
|
117
|
+
`${pad} guestAllowed: ${auth.guestAllowed},`,
|
|
118
|
+
`${pad} tokenExpirySeconds: ${auth.tokenExpirySeconds},`,
|
|
119
|
+
];
|
|
120
|
+
if (auth.tokenEndpoint) {
|
|
121
|
+
lines.push(`${pad} tokenEndpoint: ${JSON.stringify(auth.tokenEndpoint)},`);
|
|
122
|
+
}
|
|
123
|
+
if (auth.customClaims && Object.keys(auth.customClaims).length > 0) {
|
|
124
|
+
lines.push(`${pad} customClaims: ${jsonValue(auth.customClaims, indentSpaces + 2)},`);
|
|
125
|
+
}
|
|
126
|
+
return `auth: {\n${lines.join("\n")}\n${pad}},`;
|
|
127
|
+
}
|
|
128
|
+
function generateDeeplinks(deeplinks, indentSpaces) {
|
|
129
|
+
if (deeplinks.length === 0) {
|
|
130
|
+
return "deeplinks: [],";
|
|
131
|
+
}
|
|
132
|
+
const pad = " ".repeat(indentSpaces);
|
|
133
|
+
const deeplinkBlocks = deeplinks.map((dl) => {
|
|
134
|
+
const stateFields = dl.stateFields.length > 0
|
|
135
|
+
? `\n${pad} stateFields: [\n${dl.stateFields
|
|
136
|
+
.map((f) => `${pad} {\n${pad} key: ${JSON.stringify(f.key)},\n${pad} type: ${JSON.stringify(f.type)},\n${pad} required: ${f.required},${f.description ? `\n${pad} description: ${JSON.stringify(f.description)},` : ""}${f.sessionSource ? `\n${pad} sessionSource: ${JSON.stringify(f.sessionSource)},` : ""}\n${pad} }`)
|
|
137
|
+
.join(",\n")}\n${pad} ],`
|
|
138
|
+
: `\n${pad} stateFields: [],`;
|
|
139
|
+
return `${pad} {
|
|
140
|
+
${pad} id: ${JSON.stringify(dl.id)},
|
|
141
|
+
${pad} name: ${JSON.stringify(dl.name)},
|
|
142
|
+
${pad} description: ${JSON.stringify(dl.description)},
|
|
143
|
+
${pad} targetRoute: ${JSON.stringify(dl.targetRoute)},${stateFields}
|
|
144
|
+
${pad} expirySeconds: ${dl.expirySeconds},
|
|
145
|
+
${pad} requiresAuth: ${dl.requiresAuth},${dl.intent ? `\n${pad} intent: ${JSON.stringify(dl.intent)},` : ""}
|
|
146
|
+
${pad} }`;
|
|
147
|
+
});
|
|
148
|
+
return `deeplinks: [\n${deeplinkBlocks.join(",\n")},\n${pad}],`;
|
|
149
|
+
}
|
|
150
|
+
function generateAgentPermissions(perms, indentSpaces) {
|
|
151
|
+
const pad = " ".repeat(indentSpaces);
|
|
152
|
+
const lines = [
|
|
153
|
+
`${pad} defaultLevel: ${JSON.stringify(perms.defaultLevel)},`,
|
|
154
|
+
`${pad} requireUserConsent: ${perms.requireUserConsent},`,
|
|
155
|
+
];
|
|
156
|
+
if (perms.allowedActions && perms.allowedActions.length > 0) {
|
|
157
|
+
lines.push(`${pad} allowedActions: ${formatStringArray(perms.allowedActions, indentSpaces + 2)},`);
|
|
158
|
+
}
|
|
159
|
+
if (perms.allowedAgents && perms.allowedAgents.length > 0) {
|
|
160
|
+
lines.push(`${pad} allowedAgents: ${formatStringArray(perms.allowedAgents, indentSpaces + 2)},`);
|
|
161
|
+
}
|
|
162
|
+
if (perms.actionOverrides && Object.keys(perms.actionOverrides).length > 0) {
|
|
163
|
+
lines.push(`${pad} actionOverrides: ${jsonValue(perms.actionOverrides, indentSpaces + 2)},`);
|
|
164
|
+
}
|
|
165
|
+
return `agentPermissions: {\n${lines.join("\n")}\n${pad}},`;
|
|
166
|
+
}
|
|
167
|
+
function generateWidget(widget, indentSpaces) {
|
|
168
|
+
const pad = " ".repeat(indentSpaces);
|
|
169
|
+
const lines = [];
|
|
170
|
+
if (widget.primaryColor)
|
|
171
|
+
lines.push(`${pad} primaryColor: ${JSON.stringify(widget.primaryColor)},`);
|
|
172
|
+
if (widget.position)
|
|
173
|
+
lines.push(`${pad} position: ${JSON.stringify(widget.position)},`);
|
|
174
|
+
if (widget.defaultOpen !== undefined)
|
|
175
|
+
lines.push(`${pad} defaultOpen: ${widget.defaultOpen},`);
|
|
176
|
+
if (widget.launcherText)
|
|
177
|
+
lines.push(`${pad} launcherText: ${JSON.stringify(widget.launcherText)},`);
|
|
178
|
+
if (widget.welcomeMessage)
|
|
179
|
+
lines.push(`${pad} welcomeMessage: ${JSON.stringify(widget.welcomeMessage)},`);
|
|
180
|
+
if (widget.hideLauncher !== undefined)
|
|
181
|
+
lines.push(`${pad} hideLauncher: ${widget.hideLauncher},`);
|
|
182
|
+
return `widget: {\n${lines.join("\n")}\n${pad}},`;
|
|
183
|
+
}
|
|
184
|
+
// ----------------------------------------------------------------
|
|
185
|
+
// generateConfigFile
|
|
186
|
+
// ----------------------------------------------------------------
|
|
187
|
+
/**
|
|
188
|
+
* Generate the contents of a `duct.config.ts` file from a DuctConfig object.
|
|
189
|
+
*
|
|
190
|
+
* @returns A formatted TypeScript source string
|
|
191
|
+
*/
|
|
192
|
+
export function generateConfigFile(config) {
|
|
193
|
+
const i = " "; // 2-space indent
|
|
194
|
+
const sections = [
|
|
195
|
+
`${i}// Your Duct shell ID (assigned by Duct cloud)`,
|
|
196
|
+
`${i}shellId: ${JSON.stringify(config.shellId)},`,
|
|
197
|
+
"",
|
|
198
|
+
`${i}// Product metadata — helps Duct understand your app`,
|
|
199
|
+
`${i}product: {`,
|
|
200
|
+
`${i} name: ${JSON.stringify(config.product.name)},`,
|
|
201
|
+
`${i} description: ${JSON.stringify(config.product.description)},`,
|
|
202
|
+
`${i} baseUrl: ${JSON.stringify(config.product.baseUrl)},`,
|
|
203
|
+
...(config.product.locale
|
|
204
|
+
? [`${i} locale: ${JSON.stringify(config.product.locale)},`]
|
|
205
|
+
: []),
|
|
206
|
+
...(config.product.industry
|
|
207
|
+
? [`${i} industry: ${JSON.stringify(config.product.industry)},`]
|
|
208
|
+
: []),
|
|
209
|
+
`${i}},`,
|
|
210
|
+
"",
|
|
211
|
+
`${i}// Authentication configuration`,
|
|
212
|
+
`${i}// Duct uses this to request tokens and understand your session shape`,
|
|
213
|
+
`${i}${generateAuth(config.auth, 2)}`,
|
|
214
|
+
"",
|
|
215
|
+
`${i}// All navigable routes in your application`,
|
|
216
|
+
`${i}// Duct uses these to understand where to send users via deeplinks`,
|
|
217
|
+
`${i}${generateRoutes(config.routes, 2)}`,
|
|
218
|
+
"",
|
|
219
|
+
`${i}// API actions Duct can optionally expose to AI agents`,
|
|
220
|
+
`${i}// Set agentAccessible: true for actions you want agents to invoke`,
|
|
221
|
+
`${i}${generateActions(config.actions, 2)}`,
|
|
222
|
+
"",
|
|
223
|
+
`${i}// Deeplinks are signed URLs Duct issues to navigate your frontend to a specific state.`,
|
|
224
|
+
`${i}// The shell generates the URL. Your receiver (duct/receiver.ts) validates and applies the state.`,
|
|
225
|
+
`${i}// Expiry is enforced server-side. Replay attacks are blocked by token ID.`,
|
|
226
|
+
`${i}${generateDeeplinks(config.deeplinks, 2)}`,
|
|
227
|
+
"",
|
|
228
|
+
`${i}// Agent permission configuration`,
|
|
229
|
+
`${i}// Controls what AI agents/shells can do in your shell`,
|
|
230
|
+
`${i}${generateAgentPermissions(config.agentPermissions, 2)}`,
|
|
231
|
+
];
|
|
232
|
+
if (config.widget) {
|
|
233
|
+
sections.push("", `${i}// Shell widget display configuration`, `${i}${generateWidget(config.widget, 2)}`);
|
|
234
|
+
}
|
|
235
|
+
const docsBase = config.product?.baseUrl
|
|
236
|
+
? `https://docs.${new URL(config.product.baseUrl).hostname.split('.').slice(-2).join('.')}`
|
|
237
|
+
: 'https://docs.duct.sh';
|
|
238
|
+
return `/**
|
|
239
|
+
* Duct Shell Configuration — ${config.shellId}
|
|
240
|
+
*
|
|
241
|
+
* This file is the manifest Duct uses to understand your product.
|
|
242
|
+
* It defines what routes exist, what actions agents can invoke,
|
|
243
|
+
* and what deeplinks the shell can hand off to.
|
|
244
|
+
*
|
|
245
|
+
* Edit freely. Run \`npx duct init --update\` to regenerate from source.
|
|
246
|
+
* Docs: ${docsBase}/config
|
|
247
|
+
*/
|
|
248
|
+
|
|
249
|
+
import { defineDuctConfig } from "@shoryasethia/duct-sdk";
|
|
250
|
+
|
|
251
|
+
export default defineDuctConfig({
|
|
252
|
+
${sections.join("\n")}
|
|
253
|
+
});
|
|
254
|
+
`;
|
|
255
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DuctConfig } from "../../core/types.js";
|
|
2
|
+
import type { FrameworkInfo } from "../detectors/framework.js";
|
|
3
|
+
/**
|
|
4
|
+
* Generate a framework-specific receiver file.
|
|
5
|
+
*
|
|
6
|
+
* @returns Object with file paths and their contents
|
|
7
|
+
*/
|
|
8
|
+
export declare function generateReceiver(config: DuctConfig, framework: FrameworkInfo): string;
|
|
9
|
+
/**
|
|
10
|
+
* Generate the token API route file (separate from the receiver).
|
|
11
|
+
*/
|
|
12
|
+
export declare function generateTokenRoute(config: DuctConfig, framework: FrameworkInfo): string;
|