@duct-sdk/sdk 0.5.12 → 0.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/CHANGELOG.md +385 -336
- package/README.md +418 -264
- package/TOKEN_SPEC.md +243 -237
- package/dist/cli/add.d.ts +7 -0
- package/dist/cli/add.js +152 -0
- package/dist/cli/agent-prompt.d.ts +3 -0
- package/dist/cli/agent-prompt.js +39 -0
- package/dist/cli/api-sources.d.ts +26 -0
- package/dist/cli/api-sources.js +222 -0
- package/dist/cli/cloud.d.ts +133 -0
- package/dist/cli/cloud.js +298 -0
- package/dist/cli/config-loader.d.ts +14 -0
- package/dist/cli/config-loader.js +56 -7
- package/dist/cli/device-login.d.ts +17 -0
- package/dist/cli/device-login.js +75 -0
- package/dist/cli/drift.js +6 -6
- package/dist/cli/generators/any-stack.d.ts +3 -0
- package/dist/cli/generators/any-stack.js +97 -0
- package/dist/cli/generators/config.js +3 -2
- package/dist/cli/generators/receiver.js +7 -7
- package/dist/cli/generators/shell.js +12 -12
- package/dist/cli/generators/token-route.js +245 -245
- package/dist/cli/index.js +402 -611
- package/dist/cli/mcp-core.d.ts +42 -0
- package/dist/cli/mcp-core.js +161 -0
- package/dist/cli/mcp-setup.d.ts +4 -0
- package/dist/cli/mcp-setup.js +360 -0
- package/dist/cli/mcp.d.ts +2 -0
- package/dist/cli/mcp.js +457 -0
- package/dist/cli/migrate-intershell.js +5 -5
- package/dist/cli/policy-defaults.d.ts +37 -0
- package/dist/cli/policy-defaults.js +127 -0
- package/dist/cli/processors/integration-audit.js +2 -2
- package/dist/cli/processors/openapi-parser.d.ts +5 -0
- package/dist/cli/processors/openapi-parser.js +8 -3
- package/dist/cli/project-loader.d.ts +58 -0
- package/dist/cli/project-loader.js +198 -0
- package/dist/cli/project-writer.d.ts +23 -0
- package/dist/cli/project-writer.js +171 -0
- package/dist/cli/push.d.ts +43 -0
- package/dist/cli/push.js +276 -0
- package/dist/cli/safety-checks.d.ts +9 -0
- package/dist/cli/safety-checks.js +29 -0
- package/dist/cli/setup.d.ts +20 -0
- package/dist/cli/setup.js +349 -0
- package/dist/cli/shell-name.d.ts +8 -0
- package/dist/cli/shell-name.js +29 -0
- package/dist/cli/shells.d.ts +22 -0
- package/dist/cli/shells.js +122 -0
- package/dist/cli/split.d.ts +7 -0
- package/dist/cli/split.js +71 -0
- package/dist/cli/sync.d.ts +25 -0
- package/dist/cli/sync.js +120 -0
- package/dist/cli/test-runner.d.ts +21 -0
- package/dist/cli/test-runner.js +281 -0
- package/dist/core/compile.d.ts +43 -0
- package/dist/core/compile.js +510 -0
- package/dist/core/config.d.ts +533 -1
- package/dist/core/config.js +32 -12
- package/dist/core/hosted-upstream-check.d.ts +3 -0
- package/dist/core/hosted-upstream-check.js +35 -0
- package/dist/core/login-handoff.d.ts +1 -1
- package/dist/core/login-handoff.js +1 -1
- package/dist/core/project.d.ts +1798 -0
- package/dist/core/project.js +278 -0
- package/dist/core/readiness-checks.js +2 -2
- package/dist/core/types.d.ts +26 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.js +16 -0
- package/dist/project.d.ts +4 -0
- package/dist/project.js +8 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.js +1 -0
- package/fixtures/token-conformance-vectors.json +26 -0
- package/package.json +94 -94
- package/dist/adapters/react/page-context-guard.test.d.ts +0 -1
- package/dist/adapters/react/page-context-guard.test.js +0 -15
- package/dist/cli/agents/api-agent.d.ts +0 -22
- package/dist/cli/agents/api-agent.js +0 -761
- package/dist/cli/agents/auth-agent.d.ts +0 -7
- package/dist/cli/agents/auth-agent.js +0 -212
- package/dist/cli/agents/deeplink-agent.d.ts +0 -6
- package/dist/cli/agents/deeplink-agent.js +0 -285
- package/dist/cli/agents/llm.d.ts +0 -33
- package/dist/cli/agents/llm.js +0 -309
- package/dist/cli/agents/pipeline.d.ts +0 -72
- package/dist/cli/agents/pipeline.js +0 -421
- package/dist/cli/agents/reviewer-agent.d.ts +0 -13
- package/dist/cli/agents/reviewer-agent.js +0 -450
- package/dist/cli/agents/route-agent.d.ts +0 -17
- package/dist/cli/agents/route-agent.js +0 -383
- package/dist/cli/config-loader.test.d.ts +0 -1
- package/dist/cli/config-loader.test.js +0 -63
- package/dist/cli/detectors/topology.d.ts +0 -46
- package/dist/cli/detectors/topology.js +0 -417
- package/dist/cli/generators/config.test.d.ts +0 -1
- package/dist/cli/generators/config.test.js +0 -222
- package/dist/cli/generators/shell.test.d.ts +0 -1
- package/dist/cli/generators/shell.test.js +0 -73
- package/dist/cli/generators/token-route.test.d.ts +0 -1
- package/dist/cli/generators/token-route.test.js +0 -53
- package/dist/cli/generators/wiring-snippets.test.d.ts +0 -1
- package/dist/cli/generators/wiring-snippets.test.js +0 -80
- package/dist/cli/init.d.ts +0 -32
- package/dist/cli/init.js +0 -1336
- package/dist/cli/manifest-diff.test.d.ts +0 -1
- package/dist/cli/manifest-diff.test.js +0 -144
- package/dist/cli/processors/curl-parser.test.d.ts +0 -1
- package/dist/cli/processors/curl-parser.test.js +0 -24
- package/dist/cli/processors/integration-audit.test.d.ts +0 -1
- package/dist/cli/processors/integration-audit.test.js +0 -154
- package/dist/cli/processors/postprocess.d.ts +0 -22
- package/dist/cli/processors/postprocess.js +0 -107
- package/dist/cli/processors/prisma-parser.d.ts +0 -35
- package/dist/cli/processors/prisma-parser.js +0 -117
- package/dist/cli/processors/verify-discovery.d.ts +0 -33
- package/dist/cli/processors/verify-discovery.js +0 -193
- package/dist/cli/processors/verify-discovery.test.d.ts +0 -1
- package/dist/cli/processors/verify-discovery.test.js +0 -153
- package/dist/cli/processors/zod-extractor.d.ts +0 -27
- package/dist/cli/processors/zod-extractor.js +0 -153
- package/dist/cli/prompts/review.d.ts +0 -61
- package/dist/cli/prompts/review.js +0 -410
- package/dist/cli/selectors/express.d.ts +0 -12
- package/dist/cli/selectors/express.js +0 -117
- package/dist/cli/selectors/fastapi.d.ts +0 -12
- package/dist/cli/selectors/fastapi.js +0 -122
- package/dist/cli/selectors/generic.d.ts +0 -10
- package/dist/cli/selectors/generic.js +0 -74
- package/dist/cli/selectors/index.d.ts +0 -37
- package/dist/cli/selectors/index.js +0 -315
- package/dist/cli/selectors/next.d.ts +0 -12
- package/dist/cli/selectors/next.js +0 -130
- package/dist/core/config-policy.test.d.ts +0 -1
- package/dist/core/config-policy.test.js +0 -101
- package/dist/core/config-roundtrip.test.d.ts +0 -1
- package/dist/core/config-roundtrip.test.js +0 -180
- package/dist/core/endpoint-guard.test.d.ts +0 -1
- package/dist/core/endpoint-guard.test.js +0 -57
- package/dist/core/env-diagnostics.test.d.ts +0 -1
- package/dist/core/env-diagnostics.test.js +0 -31
- package/dist/core/evidence-bundle.conformance.test.d.ts +0 -1
- package/dist/core/evidence-bundle.conformance.test.js +0 -110
- package/dist/core/evidence-bundle.test.d.ts +0 -1
- package/dist/core/evidence-bundle.test.js +0 -58
- package/dist/core/invariants.test.d.ts +0 -1
- package/dist/core/invariants.test.js +0 -217
- package/dist/core/login-handoff.test.d.ts +0 -1
- package/dist/core/login-handoff.test.js +0 -31
- package/dist/core/profile.test.d.ts +0 -1
- package/dist/core/profile.test.js +0 -95
- package/dist/core/readiness-checks.test.d.ts +0 -1
- package/dist/core/readiness-checks.test.js +0 -106
- package/dist/core/receipt.test.d.ts +0 -1
- package/dist/core/receipt.test.js +0 -75
- package/dist/core/side-effects-review.test.d.ts +0 -1
- package/dist/core/side-effects-review.test.js +0 -28
- package/dist/core/token.test.d.ts +0 -1
- package/dist/core/token.test.js +0 -66
|
@@ -21,7 +21,7 @@ export async function runIntegrationAudit(sandboxFs, ctx) {
|
|
|
21
21
|
severity: "error",
|
|
22
22
|
area: "manifest",
|
|
23
23
|
issue: "duct.config.ts is missing",
|
|
24
|
-
fix: "Run `npx duct
|
|
24
|
+
fix: "Run `npx @duct-sdk/sdk` (setup) or restore duct/",
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
27
|
const tokenExists = await sandboxFs.exists(rel(ctx.outputPaths.tokenRoutePath));
|
|
@@ -173,7 +173,7 @@ export async function runIntegrationAudit(sandboxFs, ctx) {
|
|
|
173
173
|
severity: "info",
|
|
174
174
|
area: "agents",
|
|
175
175
|
issue: "Human shell works with duct_delegated only; partner agents need uct_ or OAuth",
|
|
176
|
-
fix: "Re-run init and choose third-party agent support, or
|
|
176
|
+
fix: "Re-run init and choose third-party agent support, or run `npx @duct-sdk/sdk add identity` (docs: /docs/agents#third-party-user-identity)",
|
|
177
177
|
});
|
|
178
178
|
}
|
|
179
179
|
return issues;
|
|
@@ -9,6 +9,11 @@ export interface OpenApiParseResult {
|
|
|
9
9
|
/** Title from spec info block */
|
|
10
10
|
title?: string;
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Leading slash + OpenAPI `{param}` → `:param`. The Duct gateway interpolates
|
|
14
|
+
* `:param` and `[param]` placeholders only, so `{param}` would never be filled.
|
|
15
|
+
*/
|
|
16
|
+
export declare function normalizePath(rawPath: string): string;
|
|
12
17
|
/**
|
|
13
18
|
* Parse an OpenAPI 3.x or Swagger 2.x spec file deterministically.
|
|
14
19
|
* No LLM is required — all results come directly from the spec structure.
|
|
@@ -106,8 +106,13 @@ function mapJsonType(type) {
|
|
|
106
106
|
return "string";
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
|
-
|
|
110
|
-
|
|
109
|
+
/**
|
|
110
|
+
* Leading slash + OpenAPI `{param}` → `:param`. The Duct gateway interpolates
|
|
111
|
+
* `:param` and `[param]` placeholders only, so `{param}` would never be filled.
|
|
112
|
+
*/
|
|
113
|
+
export function normalizePath(rawPath) {
|
|
114
|
+
const withSlash = rawPath.startsWith("/") ? rawPath : `/${rawPath}`;
|
|
115
|
+
return withSlash.replace(/\{([A-Za-z0-9_]+)\}/g, ":$1");
|
|
111
116
|
}
|
|
112
117
|
function isInternalRoute(path) {
|
|
113
118
|
const p = path.toLowerCase();
|
|
@@ -123,7 +128,7 @@ function isValidSpecPath(path) {
|
|
|
123
128
|
if (/\s/.test(path))
|
|
124
129
|
return false;
|
|
125
130
|
// Allow alphanumeric, hyphens, dots, slashes, colons, brackets (OpenAPI params)
|
|
126
|
-
return /^[\w\-./:{[\]()]+$/.test(path);
|
|
131
|
+
return /^[\w\-./:{}[\]()]+$/.test(path);
|
|
127
132
|
}
|
|
128
133
|
function operationHasSecurityRequirement(operation, spec) {
|
|
129
134
|
// Explicit empty array on operation = "no auth required" override
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type ParsedCapabilitiesFile, type ParsedPolicyFile, type ParsedProjectFile, type ParsedShellFile, type ParsedTestsFile } from "../core/project.js";
|
|
2
|
+
export type ProjectLayout = "split" | "legacy";
|
|
3
|
+
export interface ProjectFiles {
|
|
4
|
+
project: ParsedProjectFile;
|
|
5
|
+
capabilities: ParsedCapabilitiesFile;
|
|
6
|
+
policy: ParsedPolicyFile;
|
|
7
|
+
shell: ParsedShellFile | null;
|
|
8
|
+
tests: ParsedTestsFile | null;
|
|
9
|
+
}
|
|
10
|
+
export interface ProjectPaths {
|
|
11
|
+
root: string;
|
|
12
|
+
dir: string;
|
|
13
|
+
project: string;
|
|
14
|
+
capabilities: string;
|
|
15
|
+
policy: string;
|
|
16
|
+
shell: string;
|
|
17
|
+
tests: string;
|
|
18
|
+
}
|
|
19
|
+
export interface ResolvedProject {
|
|
20
|
+
layout: ProjectLayout;
|
|
21
|
+
/** Directory that holds duct/ or duct.config.ts. */
|
|
22
|
+
root: string;
|
|
23
|
+
/** Compiled manifest (same shape as a legacy duct.config.ts), not yet schema-validated. */
|
|
24
|
+
config: Record<string, unknown>;
|
|
25
|
+
warnings: string[];
|
|
26
|
+
/** Present for the split layout. */
|
|
27
|
+
files?: ProjectFiles;
|
|
28
|
+
paths?: ProjectPaths;
|
|
29
|
+
/** Present for the legacy layout. */
|
|
30
|
+
configPath?: string;
|
|
31
|
+
}
|
|
32
|
+
export declare class ProjectNotFoundError extends Error {
|
|
33
|
+
readonly searchedFrom: string;
|
|
34
|
+
constructor(searchedFrom: string);
|
|
35
|
+
}
|
|
36
|
+
export declare class ProjectLoadError extends Error {
|
|
37
|
+
readonly issues: string[];
|
|
38
|
+
constructor(issues: string[]);
|
|
39
|
+
}
|
|
40
|
+
export declare function projectPaths(root: string): ProjectPaths;
|
|
41
|
+
export declare function hasSplitProject(root: string): boolean;
|
|
42
|
+
export declare function hasLegacyConfig(root: string): boolean;
|
|
43
|
+
/** Walk up from `startDir` to the nearest folder with duct/project.ts or duct.config.ts. */
|
|
44
|
+
export declare function findProjectRoot(startDir: string): {
|
|
45
|
+
root: string;
|
|
46
|
+
layout: ProjectLayout;
|
|
47
|
+
} | null;
|
|
48
|
+
/** Load and validate every file in duct/. Throws ProjectLoadError listing all problems at once. */
|
|
49
|
+
export declare function loadProjectFiles(root: string): Promise<{
|
|
50
|
+
files: ProjectFiles;
|
|
51
|
+
paths: ProjectPaths;
|
|
52
|
+
}>;
|
|
53
|
+
/**
|
|
54
|
+
* Resolve the manifest a command should use.
|
|
55
|
+
* - `--config <file>` forces the legacy single-file layout (or a duct/ folder path).
|
|
56
|
+
* - Otherwise the nearest duct/ folder wins, then a legacy duct.config.ts.
|
|
57
|
+
*/
|
|
58
|
+
export declare function resolveProjectConfig(cwd: string, explicitConfig?: string): Promise<ResolvedProject>;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct-sdk/sdk — Find and load a Duct project (duct/ folder or legacy duct.config.ts)
|
|
3
|
+
// ============================================================
|
|
4
|
+
import * as fs from "fs/promises";
|
|
5
|
+
import * as path from "path";
|
|
6
|
+
import { existsSync } from "fs";
|
|
7
|
+
import { CapabilitiesFileSchema, PROJECT_DIR, PROJECT_FILES, PROJECT_MARKERS, PolicyFileSchema, ProjectFileSchema, ShellFileSchema, TestsFileSchema, } from "../core/project.js";
|
|
8
|
+
import { compileProject } from "../core/compile.js";
|
|
9
|
+
import { loadDuctConfig, loadStaticDefinition, StaticDefinitionError } from "./config-loader.js";
|
|
10
|
+
export class ProjectNotFoundError extends Error {
|
|
11
|
+
searchedFrom;
|
|
12
|
+
constructor(searchedFrom) {
|
|
13
|
+
super(`No Duct project found from ${searchedFrom}. Run \`npx @duct-sdk/sdk setup\` to create duct/, ` +
|
|
14
|
+
"or pass --config <path>.");
|
|
15
|
+
this.searchedFrom = searchedFrom;
|
|
16
|
+
this.name = "ProjectNotFoundError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class ProjectLoadError extends Error {
|
|
20
|
+
issues;
|
|
21
|
+
constructor(issues) {
|
|
22
|
+
super(`Your Duct project has ${issues.length} problem${issues.length === 1 ? "" : "s"}:\n` +
|
|
23
|
+
issues.map((issue) => ` • ${issue}`).join("\n"));
|
|
24
|
+
this.issues = issues;
|
|
25
|
+
this.name = "ProjectLoadError";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function projectPaths(root) {
|
|
29
|
+
const dir = path.join(root, PROJECT_DIR);
|
|
30
|
+
return {
|
|
31
|
+
root,
|
|
32
|
+
dir,
|
|
33
|
+
project: path.join(dir, PROJECT_FILES.project),
|
|
34
|
+
capabilities: path.join(dir, PROJECT_FILES.capabilities),
|
|
35
|
+
policy: path.join(dir, PROJECT_FILES.policy),
|
|
36
|
+
shell: path.join(dir, PROJECT_FILES.shell),
|
|
37
|
+
tests: path.join(dir, PROJECT_FILES.tests),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export function hasSplitProject(root) {
|
|
41
|
+
return existsSync(projectPaths(root).project);
|
|
42
|
+
}
|
|
43
|
+
export function hasLegacyConfig(root) {
|
|
44
|
+
return existsSync(path.join(root, "duct.config.ts")) || existsSync(path.join(root, "duct.config.js"));
|
|
45
|
+
}
|
|
46
|
+
/** Walk up from `startDir` to the nearest folder with duct/project.ts or duct.config.ts. */
|
|
47
|
+
export function findProjectRoot(startDir) {
|
|
48
|
+
let dir = path.resolve(startDir);
|
|
49
|
+
for (let depth = 0; depth < 8; depth++) {
|
|
50
|
+
if (hasSplitProject(dir))
|
|
51
|
+
return { root: dir, layout: "split" };
|
|
52
|
+
if (hasLegacyConfig(dir))
|
|
53
|
+
return { root: dir, layout: "legacy" };
|
|
54
|
+
if (existsSync(path.join(dir, ".git")))
|
|
55
|
+
return null;
|
|
56
|
+
const parent = path.dirname(dir);
|
|
57
|
+
if (parent === dir)
|
|
58
|
+
return null;
|
|
59
|
+
dir = parent;
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
function formatZodIssues(label, error) {
|
|
64
|
+
return error.issues.map((issue) => {
|
|
65
|
+
const where = issue.path.length > 0 ? ` › ${issue.path.join(".")}` : "";
|
|
66
|
+
return `${label}${where}: ${issue.message}`;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function relativeLabel(root, filePath) {
|
|
70
|
+
return path.relative(root, filePath).split(path.sep).join("/");
|
|
71
|
+
}
|
|
72
|
+
async function readJsonFile(filePath, label, issues) {
|
|
73
|
+
try {
|
|
74
|
+
return JSON.parse(await fs.readFile(filePath, "utf-8"));
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
issues.push(`${label}: ${err instanceof SyntaxError ? `invalid JSON — ${err.message}` : "could not be read"}`);
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async function readDefinition(filePath, marker, label, issues) {
|
|
82
|
+
try {
|
|
83
|
+
return await loadStaticDefinition(filePath, marker);
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
issues.push(err instanceof StaticDefinitionError ? `${label}: ${err.message}` : `${label}: could not be read`);
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** Load and validate every file in duct/. Throws ProjectLoadError listing all problems at once. */
|
|
91
|
+
export async function loadProjectFiles(root) {
|
|
92
|
+
const paths = projectPaths(root);
|
|
93
|
+
const issues = [];
|
|
94
|
+
const label = (p) => relativeLabel(root, p);
|
|
95
|
+
if (!existsSync(paths.project))
|
|
96
|
+
issues.push(`${label(paths.project)}: missing (run \`duct setup\`)`);
|
|
97
|
+
if (!existsSync(paths.capabilities)) {
|
|
98
|
+
issues.push(`${label(paths.capabilities)}: missing (run \`duct sync\` or \`duct setup\`)`);
|
|
99
|
+
}
|
|
100
|
+
if (issues.length > 0)
|
|
101
|
+
throw new ProjectLoadError(issues);
|
|
102
|
+
const rawProject = await readDefinition(paths.project, PROJECT_MARKERS.project, label(paths.project), issues);
|
|
103
|
+
const rawCapabilities = await readJsonFile(paths.capabilities, label(paths.capabilities), issues);
|
|
104
|
+
const rawPolicy = existsSync(paths.policy)
|
|
105
|
+
? await readDefinition(paths.policy, PROJECT_MARKERS.policy, label(paths.policy), issues)
|
|
106
|
+
: {};
|
|
107
|
+
const rawShell = existsSync(paths.shell)
|
|
108
|
+
? await readDefinition(paths.shell, PROJECT_MARKERS.shell, label(paths.shell), issues)
|
|
109
|
+
: null;
|
|
110
|
+
const rawTests = existsSync(paths.tests)
|
|
111
|
+
? await readDefinition(paths.tests, PROJECT_MARKERS.tests, label(paths.tests), issues)
|
|
112
|
+
: null;
|
|
113
|
+
const project = rawProject ? ProjectFileSchema.safeParse(rawProject) : null;
|
|
114
|
+
if (project && !project.success)
|
|
115
|
+
issues.push(...formatZodIssues(label(paths.project), project.error));
|
|
116
|
+
const capabilities = rawCapabilities !== undefined ? CapabilitiesFileSchema.safeParse(rawCapabilities) : null;
|
|
117
|
+
if (capabilities && !capabilities.success) {
|
|
118
|
+
issues.push(...formatZodIssues(label(paths.capabilities), capabilities.error));
|
|
119
|
+
}
|
|
120
|
+
const policy = rawPolicy ? PolicyFileSchema.safeParse(rawPolicy) : null;
|
|
121
|
+
if (policy && !policy.success)
|
|
122
|
+
issues.push(...formatZodIssues(label(paths.policy), policy.error));
|
|
123
|
+
const shell = rawShell ? ShellFileSchema.safeParse(rawShell) : null;
|
|
124
|
+
if (shell && !shell.success)
|
|
125
|
+
issues.push(...formatZodIssues(label(paths.shell), shell.error));
|
|
126
|
+
const tests = rawTests ? TestsFileSchema.safeParse(rawTests) : null;
|
|
127
|
+
if (tests && !tests.success)
|
|
128
|
+
issues.push(...formatZodIssues(label(paths.tests), tests.error));
|
|
129
|
+
if (issues.length > 0 ||
|
|
130
|
+
!project?.success ||
|
|
131
|
+
!capabilities?.success ||
|
|
132
|
+
!policy?.success ||
|
|
133
|
+
(shell && !shell.success) ||
|
|
134
|
+
(tests && !tests.success)) {
|
|
135
|
+
throw new ProjectLoadError(issues.length > 0 ? issues : ["Project files could not be loaded"]);
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
files: {
|
|
139
|
+
project: project.data,
|
|
140
|
+
capabilities: capabilities.data,
|
|
141
|
+
policy: policy.data,
|
|
142
|
+
shell: shell?.success ? shell.data : null,
|
|
143
|
+
tests: tests?.success ? tests.data : null,
|
|
144
|
+
},
|
|
145
|
+
paths,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Resolve the manifest a command should use.
|
|
150
|
+
* - `--config <file>` forces the legacy single-file layout (or a duct/ folder path).
|
|
151
|
+
* - Otherwise the nearest duct/ folder wins, then a legacy duct.config.ts.
|
|
152
|
+
*/
|
|
153
|
+
export async function resolveProjectConfig(cwd, explicitConfig) {
|
|
154
|
+
if (explicitConfig) {
|
|
155
|
+
const target = path.resolve(cwd, explicitConfig);
|
|
156
|
+
const isProjectDir = path.basename(target) === PROJECT_DIR && existsSync(path.join(target, PROJECT_FILES.project));
|
|
157
|
+
if (isProjectDir)
|
|
158
|
+
return resolveSplit(path.dirname(target));
|
|
159
|
+
if (!existsSync(target))
|
|
160
|
+
throw new ProjectNotFoundError(target);
|
|
161
|
+
return {
|
|
162
|
+
layout: "legacy",
|
|
163
|
+
root: path.dirname(target),
|
|
164
|
+
configPath: target,
|
|
165
|
+
config: await loadDuctConfig(target),
|
|
166
|
+
warnings: [],
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const found = findProjectRoot(cwd);
|
|
170
|
+
if (!found)
|
|
171
|
+
throw new ProjectNotFoundError(cwd);
|
|
172
|
+
if (found.layout === "split")
|
|
173
|
+
return resolveSplit(found.root);
|
|
174
|
+
const configPath = existsSync(path.join(found.root, "duct.config.ts"))
|
|
175
|
+
? path.join(found.root, "duct.config.ts")
|
|
176
|
+
: path.join(found.root, "duct.config.js");
|
|
177
|
+
return {
|
|
178
|
+
layout: "legacy",
|
|
179
|
+
root: found.root,
|
|
180
|
+
configPath,
|
|
181
|
+
config: await loadDuctConfig(configPath),
|
|
182
|
+
warnings: [
|
|
183
|
+
"Using legacy duct.config.ts — run `npx @duct-sdk/sdk split` to move to the duct/ folder layout.",
|
|
184
|
+
],
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
async function resolveSplit(root) {
|
|
188
|
+
const { files, paths } = await loadProjectFiles(root);
|
|
189
|
+
const { config, warnings } = compileProject(files);
|
|
190
|
+
return {
|
|
191
|
+
layout: "split",
|
|
192
|
+
root,
|
|
193
|
+
config: config,
|
|
194
|
+
warnings,
|
|
195
|
+
files,
|
|
196
|
+
paths,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { CapabilitiesFile, PolicyFile, ProjectFile, ShellFile, TestsFile } from "../core/project.js";
|
|
2
|
+
export declare const PROJECT_IMPORT = "@duct-sdk/sdk/project";
|
|
3
|
+
/** Serialize a JSON-safe value as a readable TypeScript object literal. */
|
|
4
|
+
export declare function toTsLiteral(value: unknown, indent?: number): string;
|
|
5
|
+
export declare function renderProjectFile(project: ProjectFile): string;
|
|
6
|
+
export declare function renderPolicyFile(policy: PolicyFile, opts?: {
|
|
7
|
+
annotate?: boolean;
|
|
8
|
+
}): string;
|
|
9
|
+
export declare function renderShellFile(shell: ShellFile): string;
|
|
10
|
+
export declare function renderTestsFile(tests: TestsFile): string;
|
|
11
|
+
export declare function renderCapabilitiesFile(capabilities: CapabilitiesFile): string;
|
|
12
|
+
export interface ProjectFileSet {
|
|
13
|
+
project: ProjectFile;
|
|
14
|
+
capabilities: CapabilitiesFile;
|
|
15
|
+
policy: PolicyFile;
|
|
16
|
+
shell?: ShellFile | null;
|
|
17
|
+
tests?: TestsFile | null;
|
|
18
|
+
/** Render policy.ts with explanatory comments (new projects). */
|
|
19
|
+
annotatePolicy?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** Render every file in the set, keyed by path relative to the project root. */
|
|
22
|
+
export declare function renderProjectFiles(files: ProjectFileSet): Record<string, string>;
|
|
23
|
+
export declare function writeRenderedFiles(root: string, rendered: Record<string, string>): Promise<string[]>;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct-sdk/sdk — Render duct/ project files
|
|
3
|
+
//
|
|
4
|
+
// Output is plain object literals (read statically by the CLI) with
|
|
5
|
+
// identifier keys unquoted so the files look hand-written.
|
|
6
|
+
// ============================================================
|
|
7
|
+
import * as fs from "fs/promises";
|
|
8
|
+
import * as path from "path";
|
|
9
|
+
import { PROJECT_DIR, PROJECT_FILES } from "../core/project.js";
|
|
10
|
+
export const PROJECT_IMPORT = "@duct-sdk/sdk/project";
|
|
11
|
+
const IDENTIFIER_RE = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
12
|
+
function isPlainObject(value) {
|
|
13
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
function isShortPrimitiveArray(value) {
|
|
16
|
+
return (value.length <= 6 &&
|
|
17
|
+
value.every((item) => typeof item !== "object" || item === null) &&
|
|
18
|
+
JSON.stringify(value).length <= 60);
|
|
19
|
+
}
|
|
20
|
+
/** Serialize a JSON-safe value as a readable TypeScript object literal. */
|
|
21
|
+
export function toTsLiteral(value, indent = 0) {
|
|
22
|
+
const pad = " ".repeat(indent);
|
|
23
|
+
const inner = " ".repeat(indent + 1);
|
|
24
|
+
if (value === null)
|
|
25
|
+
return "null";
|
|
26
|
+
if (typeof value === "string")
|
|
27
|
+
return JSON.stringify(value);
|
|
28
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
29
|
+
return String(value);
|
|
30
|
+
if (value === undefined)
|
|
31
|
+
return "undefined";
|
|
32
|
+
if (Array.isArray(value)) {
|
|
33
|
+
if (value.length === 0)
|
|
34
|
+
return "[]";
|
|
35
|
+
if (isShortPrimitiveArray(value))
|
|
36
|
+
return `[${value.map((item) => toTsLiteral(item)).join(", ")}]`;
|
|
37
|
+
return `[\n${value.map((item) => `${inner}${toTsLiteral(item, indent + 1)},`).join("\n")}\n${pad}]`;
|
|
38
|
+
}
|
|
39
|
+
if (isPlainObject(value)) {
|
|
40
|
+
const entries = Object.entries(value).filter(([, v]) => v !== undefined);
|
|
41
|
+
if (entries.length === 0)
|
|
42
|
+
return "{}";
|
|
43
|
+
const lines = entries.map(([key, v]) => {
|
|
44
|
+
const renderedKey = IDENTIFIER_RE.test(key) ? key : JSON.stringify(key);
|
|
45
|
+
return `${inner}${renderedKey}: ${toTsLiteral(v, indent + 1)},`;
|
|
46
|
+
});
|
|
47
|
+
return `{\n${lines.join("\n")}\n${pad}}`;
|
|
48
|
+
}
|
|
49
|
+
return JSON.stringify(value);
|
|
50
|
+
}
|
|
51
|
+
function header(lines) {
|
|
52
|
+
return lines.map((line) => (line ? `// ${line}` : "//")).join("\n");
|
|
53
|
+
}
|
|
54
|
+
export function renderProjectFile(project) {
|
|
55
|
+
return `${header([
|
|
56
|
+
"Who you are: shell id, product, and where your API lives.",
|
|
57
|
+
"Read statically by the Duct CLI (never executed) — keep values literal.",
|
|
58
|
+
])}
|
|
59
|
+
import { defineProject } from "${PROJECT_IMPORT}";
|
|
60
|
+
|
|
61
|
+
export default defineProject(${toTsLiteral(project)});
|
|
62
|
+
`;
|
|
63
|
+
}
|
|
64
|
+
export function renderPolicyFile(policy, opts = {}) {
|
|
65
|
+
if (!opts.annotate) {
|
|
66
|
+
return `${header([
|
|
67
|
+
"What agents may do. Duct enforces this on every call — agents cannot bypass it.",
|
|
68
|
+
"Docs: /docs/configuration#policy",
|
|
69
|
+
])}
|
|
70
|
+
import { definePolicy } from "${PROJECT_IMPORT}";
|
|
71
|
+
|
|
72
|
+
export default definePolicy(${toTsLiteral(policy)});
|
|
73
|
+
`;
|
|
74
|
+
}
|
|
75
|
+
const agents = policy.agents ?? {};
|
|
76
|
+
const body = [];
|
|
77
|
+
body.push(" agents: {");
|
|
78
|
+
body.push(" // none = no agent access · read = read-only actions · write = reads and writes");
|
|
79
|
+
body.push(` access: ${toTsLiteral(agents.access ?? "read")},`);
|
|
80
|
+
if (agents.allow) {
|
|
81
|
+
body.push(" // Actions agents may call (snake_case ids, * wildcards). Omit to allow everything `access` permits.");
|
|
82
|
+
body.push(` allow: ${toTsLiteral(agents.allow, 2)},`);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
body.push(" // allow: [\"get_*\"], // optional allowlist — omit to allow everything `access` permits");
|
|
86
|
+
}
|
|
87
|
+
body.push(" // Actions agents may never call. Deny always wins.");
|
|
88
|
+
body.push(` deny: ${toTsLiteral(agents.deny ?? [], 2)},`);
|
|
89
|
+
body.push(" // Agents must present the user's consent before acting for that user.");
|
|
90
|
+
body.push(` userConsent: ${toTsLiteral(agents.userConsent ?? true)},`);
|
|
91
|
+
body.push(" },");
|
|
92
|
+
body.push("");
|
|
93
|
+
body.push(" // Ask a human before running a call that matches. Example:");
|
|
94
|
+
body.push(" // create_refund: { amount: { gt: 500 } },");
|
|
95
|
+
body.push(` approveWhen: ${toTsLiteral(policy.approveWhen ?? {}, 1)},`);
|
|
96
|
+
body.push("");
|
|
97
|
+
body.push(" // Rate limits. Applied top to bottom; later entries override earlier ones.");
|
|
98
|
+
body.push(` limits: ${toTsLiteral(policy.limits ?? {}, 1)},`);
|
|
99
|
+
if (policy.sensitivity && Object.keys(policy.sensitivity).length > 0) {
|
|
100
|
+
body.push("");
|
|
101
|
+
body.push(" // Raise sensitivity for matching actions (high/critical are never exposed to agents by default).");
|
|
102
|
+
body.push(` sensitivity: ${toTsLiteral(policy.sensitivity, 1)},`);
|
|
103
|
+
}
|
|
104
|
+
if (policy.approvers) {
|
|
105
|
+
body.push("");
|
|
106
|
+
body.push(" // Who is notified when an agent request is waiting for approval.");
|
|
107
|
+
body.push(` approvers: ${toTsLiteral(policy.approvers, 1)},`);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
body.push("");
|
|
111
|
+
body.push(" // approvers: { emails: [\"ops@example.com\"] },");
|
|
112
|
+
}
|
|
113
|
+
return `${header([
|
|
114
|
+
"What agents may do. Duct enforces this on every call — agents cannot bypass it.",
|
|
115
|
+
"Writes never run without a human: agents get a 403 or an approval request, never a silent success.",
|
|
116
|
+
"Check it with `npx @duct-sdk/sdk test`. Docs: /docs/configuration#policy",
|
|
117
|
+
])}
|
|
118
|
+
import { definePolicy } from "${PROJECT_IMPORT}";
|
|
119
|
+
|
|
120
|
+
export default definePolicy({
|
|
121
|
+
${body.join("\n")}
|
|
122
|
+
});
|
|
123
|
+
`;
|
|
124
|
+
}
|
|
125
|
+
export function renderShellFile(shell) {
|
|
126
|
+
return `${header([
|
|
127
|
+
"Optional: the embeddable chat shell — widget, product routes, deeplinks, and chat hints.",
|
|
128
|
+
"Delete this file if you only expose your product to agents over the API.",
|
|
129
|
+
])}
|
|
130
|
+
import { defineShell } from "${PROJECT_IMPORT}";
|
|
131
|
+
|
|
132
|
+
export default defineShell(${toTsLiteral(shell)});
|
|
133
|
+
`;
|
|
134
|
+
}
|
|
135
|
+
export function renderTestsFile(tests) {
|
|
136
|
+
return `${header([
|
|
137
|
+
"Policy expectations. \`npx @duct-sdk/sdk test\` asks Duct cloud to evaluate each case",
|
|
138
|
+
"against the exact policy you are about to push. expect: allow | deny | approval | blocked",
|
|
139
|
+
"(blocked = must not run without a human).",
|
|
140
|
+
])}
|
|
141
|
+
import { defineTests } from "${PROJECT_IMPORT}";
|
|
142
|
+
|
|
143
|
+
export default defineTests(${toTsLiteral(tests)});
|
|
144
|
+
`;
|
|
145
|
+
}
|
|
146
|
+
export function renderCapabilitiesFile(capabilities) {
|
|
147
|
+
return `${JSON.stringify(capabilities, null, 2)}\n`;
|
|
148
|
+
}
|
|
149
|
+
/** Render every file in the set, keyed by path relative to the project root. */
|
|
150
|
+
export function renderProjectFiles(files) {
|
|
151
|
+
const out = {
|
|
152
|
+
[`${PROJECT_DIR}/${PROJECT_FILES.project}`]: renderProjectFile(files.project),
|
|
153
|
+
[`${PROJECT_DIR}/${PROJECT_FILES.capabilities}`]: renderCapabilitiesFile(files.capabilities),
|
|
154
|
+
[`${PROJECT_DIR}/${PROJECT_FILES.policy}`]: renderPolicyFile(files.policy, { annotate: files.annotatePolicy }),
|
|
155
|
+
};
|
|
156
|
+
if (files.shell)
|
|
157
|
+
out[`${PROJECT_DIR}/${PROJECT_FILES.shell}`] = renderShellFile(files.shell);
|
|
158
|
+
if (files.tests)
|
|
159
|
+
out[`${PROJECT_DIR}/${PROJECT_FILES.tests}`] = renderTestsFile(files.tests);
|
|
160
|
+
return out;
|
|
161
|
+
}
|
|
162
|
+
export async function writeRenderedFiles(root, rendered) {
|
|
163
|
+
const written = [];
|
|
164
|
+
for (const [relative, content] of Object.entries(rendered)) {
|
|
165
|
+
const target = path.join(root, relative);
|
|
166
|
+
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
167
|
+
await fs.writeFile(target, content, "utf-8");
|
|
168
|
+
written.push(relative);
|
|
169
|
+
}
|
|
170
|
+
return written;
|
|
171
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ValidatedDuctConfig } from "../core/config.js";
|
|
2
|
+
import { type ResolvedProject } from "./project-loader.js";
|
|
3
|
+
import { type PushResult } from "./cloud.js";
|
|
4
|
+
export declare class PushAbortedError extends Error {
|
|
5
|
+
readonly details: string[];
|
|
6
|
+
constructor(message: string, details?: string[]);
|
|
7
|
+
}
|
|
8
|
+
export interface PushOptions {
|
|
9
|
+
cwd: string;
|
|
10
|
+
/** --config path (legacy file or duct/ folder). Omit to auto-detect. */
|
|
11
|
+
config?: string;
|
|
12
|
+
dryRun?: boolean;
|
|
13
|
+
/** Acknowledge warnings and privilege increases (CI override). */
|
|
14
|
+
yes?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface PushOutcome {
|
|
17
|
+
pushed: boolean;
|
|
18
|
+
shellId: string;
|
|
19
|
+
layout: ResolvedProject["layout"];
|
|
20
|
+
result?: PushResult;
|
|
21
|
+
actionCount: number;
|
|
22
|
+
}
|
|
23
|
+
export interface ValidatedProject {
|
|
24
|
+
resolved: ResolvedProject;
|
|
25
|
+
validated: ValidatedDuctConfig;
|
|
26
|
+
config: Record<string, unknown>;
|
|
27
|
+
/** Agent-safety warnings that require --yes before push. */
|
|
28
|
+
safetyWarnings: string[];
|
|
29
|
+
}
|
|
30
|
+
/** Load + validate without network calls. Throws PushAbortedError with every problem found. */
|
|
31
|
+
export declare function loadAndValidateProject(cwd: string, explicitConfig?: string): Promise<ValidatedProject>;
|
|
32
|
+
interface PrecomputedWorkflow {
|
|
33
|
+
id: string;
|
|
34
|
+
name: string;
|
|
35
|
+
steps: Array<{
|
|
36
|
+
action_id: string;
|
|
37
|
+
}>;
|
|
38
|
+
intent_patterns: string[];
|
|
39
|
+
}
|
|
40
|
+
export declare function computePrecomputedWorkflows(config: Record<string, unknown>): PrecomputedWorkflow[];
|
|
41
|
+
export declare function runPush(opts: PushOptions): Promise<PushOutcome>;
|
|
42
|
+
export declare function printPushAbort(err: unknown): void;
|
|
43
|
+
export {};
|