Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/fixtures/failure-report-shapes.json
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ The published package:
- installs the firstdraft executable;
- runs reviewed JavaScript source directly;
- has no runtime dependencies or install scripts;
- performs no telemetry, update check, or network request unless the caller invokes an API command;
- sends nothing in the background or beyond the command the caller runs. [What the CLI sends](docs/commands.md#what-the-cli-sends)
lists each command's requests and their `FirstDraft-Client` header: CLI and plugin versions, agent, and operating
system. `report` prints its exact report and `conversation send` the file's path, size, and digest before sending;
- makes no update check of its own: its update line comes from First Draft's answer to the command;
- reads Bearer credentials from the environment or its private `firstdraft login` credentials file; and
- carries npm provenance linking registry bytes to its GitHub workflow and commit.

Expand Down
234 changes: 194 additions & 40 deletions docs/commands.md

Large diffs are not rendered by default.

169 changes: 120 additions & 49 deletions docs/errors.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion release/compatibility.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"component": "cli",
"version": "0.9.0",
"requires": {
"api_contract": [">= 0.8.0", "< 0.9.0"],
"api_contract": [">= 0.9.0", "< 0.10.0"],
"foundation_plan_formats": ["firstdraft.foundation-plan.sketch/0.23"]
}
}
4 changes: 4 additions & 0 deletions scripts/check-pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,25 @@ if (result.status !== 0) {
"src/api-response.js",
"src/application-identity.js",
"src/cli.js",
"src/commands/account.js",
"src/commands/compilation.js",
"src/commands/conversation.js",
"src/commands/login.js",
"src/commands/logout.js",
"src/commands/plan-compile.js",
"src/commands/plan-init.js",
"src/commands/plan-publish.js",
"src/commands/plan-push.js",
"src/commands/plan-status.js",
"src/commands/report.js",
"src/compilation-artifact.js",
"src/credentials.js",
"src/file-system.js",
"src/oauth.js",
"src/plan-compile-progress.js",
"src/plan-state.js",
"src/root-output.js",
"src/service-client.js",
"src/uuid-v7.js",
"src/version.js",
]);
Expand Down
39 changes: 39 additions & 0 deletions scripts/smoke-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,45 @@ try {
"Invalid arguments. Run 'firstdraft compilation cancel --help' for usage.",
});

const invalidAccount = spawnPackedCli(
["account", "--canary-secret-option"],
installationDirectory,
);
assertHandledFailure(invalidAccount, 2, {
error: "invalid_arguments",
detail: "Invalid arguments. Run 'firstdraft account --help' for usage.",
});

const invalidReport = spawnPackedCli(
["report", "progress", "not-a-uuid", "--step", "setup"],
installationDirectory,
);
assertHandledFailure(invalidReport, 2, {
error: "invalid_arguments",
detail:
"Invalid arguments. Run 'firstdraft report progress --help' for usage.",
});

const invalidFailureReport = spawnPackedCli(
["report", "failure", "not-a-uuid", "--kind", "lint"],
installationDirectory,
);
assertHandledFailure(invalidFailureReport, 2, {
error: "invalid_arguments",
detail:
"Invalid arguments. Run 'firstdraft report failure --help' for usage.",
});

const conversationWithoutConsent = spawnPackedCli(
["conversation", "send", "canary-secret.jsonl"],
installationDirectory,
);
assertHandledFailure(conversationWithoutConsent, 2, {
error: "consent_required",
detail:
"Nothing was sent. Ask the person whether to send this conversation, show them the file, and pass --person-agreed only after they say yes this time.",
});

const invalidLogin = spawnPackedCli(
["login", "--canary-secret-option"],
installationDirectory,
Expand Down
15 changes: 11 additions & 4 deletions src/api-authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export function authenticateApiCommand({

let selectedOrigin = staging ? STAGING_API_URL : undefined;
const request = fetchFunction ?? globalThis.fetch;
/** @param {string} origin */
const tokenFor = (origin) =>
environmentTokenFor(origin, { apiToken, stagingApiToken }) ??
storedToken(readStoredTokens(), origin);
/** @type {typeof globalThis.fetch} */
const authorizedFetch = (input, init) => {
const endpoint = new URL(input instanceof Request ? input.url : input);
Expand All @@ -74,9 +78,7 @@ export function authenticateApiCommand({
"The requested API environment does not match the Project origin. No request was made.",
);
}
const token =
environmentTokenFor(endpoint.origin, { apiToken, stagingApiToken }) ??
storedToken(readStoredTokens(), endpoint.origin);
const token = tokenFor(endpoint.origin);
if (token === undefined) throw new ApiAuthenticationRequiredError();
selectedOrigin = endpoint.origin;
return request(input, {
Expand All @@ -87,7 +89,12 @@ export function authenticateApiCommand({
},
});
};
return { apiUrl: configured, fetchFunction: authorizedFetch };
return {
apiUrl: configured,
fetchFunction: authorizedFetch,
/** Whether a request to `origin` would carry a token. @param {string} origin */
hasTokenFor: (origin) => tokenFor(origin) !== undefined,
};
}

/** @param {Readonly<Record<string, string>>} tokens @param {string} origin */
Expand Down
33 changes: 33 additions & 0 deletions src/api-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ export const SERVICE_ROUTES = {
method: "GET",
path: "/v1/projects/{project_id}/github-publication",
},
readAccount: {
method: "GET",
path: "/v1/account",
},
sendProgressReport: {
method: "POST",
path: "/v1/projects/{project_id}/compilations/{compilation_id}/progress-reports",
},
sendFailureReport: {
method: "POST",
path: "/v1/projects/{project_id}/compilations/{compilation_id}/failure-reports",
},
sendConversation: {
method: "POST",
path: "/v1/projects/{project_id}/conversations",
},
requestOAuthToken: {
method: "POST",
path: "/oauth/token",
Expand Down Expand Up @@ -164,6 +180,23 @@ export function isProblemBody(response, body) {
);
}

/**
* The fields of a problem that isProblemBody accepted, and nothing else.
*
* @param {unknown} body
*/
export function problemProjection(body) {
const problem = /** @type {Record<string, unknown>} */ (body);

return {
...(problem.type === "about:blank" ? { type: problem.type } : {}),
title: problem.title,
status: problem.status,
code: problem.code,
detail: problem.detail,
};
}

/**
* @param {unknown} value
* @returns {value is Record<string, unknown> & {severity: "error" | "warning"}}
Expand Down
Loading
Loading