Skip to content
Merged
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
81 changes: 47 additions & 34 deletions packages/spark/src/clack-wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ const MANUAL_INSTRUMENTATION_CHOICE: ManualInstrumentationChoice = {
label: COPY.instrumentation.modes.manual.label,
};

class WizardStepSpinner {
private spinner: ReturnType<typeof clack.spinner> | undefined;
private active = false;

update(message: string): void {
if (this.active) {
this.spinner!.message(message);
return;
}

this.spinner ??= clack.spinner();
this.spinner.start(message);
this.active = true;
}

clear(): void {
if (!this.active) return;

this.spinner!.clear();
this.active = false;
}
}

type BooleanSelectChoices = {
readonly yes: {
readonly label: string;
Expand Down Expand Up @@ -198,9 +221,14 @@ export async function runClackWizard(deps: WizardDeps): Promise<WizardResult> {

const envFilePath = await writeLocalEnvBraintrust(deps, session.apiKey);

await handleBraintrustCliSetup(deps, session);

const codingToolStatuses = await preflightCodingTools(deps);
const setupSpinner = new WizardStepSpinner();
let codingToolStatuses: readonly CodingToolStatus[];
try {
await handleBraintrustCliSetup(deps, session, setupSpinner);
codingToolStatuses = await preflightCodingTools(deps, setupSpinner);
} finally {
setupSpinner.clear();
}
const hasUsableCodingTool = codingToolStatuses.some(
(status) => status.usable,
);
Expand Down Expand Up @@ -360,25 +388,10 @@ async function selectInstrumentationMode(args: {
async function handleBraintrustCliSetup(
deps: WizardDeps,
session: WizardSessionCompleteResult,
spinner: WizardStepSpinner,
): Promise<void> {
let discovery = await deps.braintrustCli.discover();
let commandPath = discovery.commandPath;
const spinner = clack.spinner();
let spinnerActive = false;
const updateSpinner = (message: string) => {
if (spinnerActive) {
spinner.message(message);
} else {
spinner.start(message);
spinnerActive = true;
}
};
const clearSpinner = () => {
if (spinnerActive) {
spinner.clear();
spinnerActive = false;
}
};

if (discovery.installed) {
const installedLabel =
Expand All @@ -391,13 +404,13 @@ async function handleBraintrustCliSetup(
yesFirst: true,
});
if (shouldUpdate && commandPath) {
updateSpinner(COPY.braintrustCli.updating);
spinner.update(COPY.braintrustCli.updating);
try {
await deps.braintrustCli.update(commandPath);
discovery = await deps.braintrustCli.discover();
commandPath = discovery.commandPath ?? commandPath;
} catch (error) {
clearSpinner();
spinner.clear();
clack.log.warn(
COPY.braintrustCli.updateFailed(summarizeBraintrustCliError(error)),
);
Expand All @@ -411,11 +424,11 @@ async function handleBraintrustCliSetup(
});
if (!shouldInstall) return;

updateSpinner(COPY.braintrustCli.installing);
spinner.update(COPY.braintrustCli.installing);
try {
await deps.braintrustCli.install();
} catch (error) {
clearSpinner();
spinner.clear();
clack.log.warn(
COPY.braintrustCli.installFailed(summarizeBraintrustCliError(error)),
);
Expand All @@ -425,7 +438,7 @@ async function handleBraintrustCliSetup(
discovery = await deps.braintrustCli.discover();
commandPath = discovery.commandPath;
if (!discovery.installed || !commandPath) {
clearSpinner();
spinner.clear();
clack.log.warn(COPY.braintrustCli.installedButNotFound);
return;
}
Expand All @@ -434,11 +447,11 @@ async function handleBraintrustCliSetup(
if (!commandPath) return;

let currentContext: BraintrustCliContext;
updateSpinner(COPY.braintrustCli.checkingContext);
spinner.update(COPY.braintrustCli.checkingContext);
try {
currentContext = await deps.braintrustCli.status(commandPath);
} catch (error) {
clearSpinner();
spinner.clear();
clack.log.warn(
COPY.braintrustCli.statusFailed(summarizeBraintrustCliError(error)),
);
Expand All @@ -451,7 +464,7 @@ async function handleBraintrustCliSetup(
project: session.projectName,
};
if (braintrustCliContextConflicts(currentContext, targetContext)) {
clearSpinner();
spinner.clear();
const shouldSwitch = await selectBoolean({
message: COPY.braintrustCli.switchContextQuestion({
currentContext,
Expand All @@ -465,7 +478,7 @@ async function handleBraintrustCliSetup(
}
}

updateSpinner(COPY.braintrustCli.configuringContext);
spinner.update(COPY.braintrustCli.configuringContext);
try {
await deps.braintrustCli.loginAndSwitch(commandPath, {
apiKey: session.apiKey,
Expand All @@ -474,9 +487,8 @@ async function handleBraintrustCliSetup(
orgName: session.orgName,
projectName: session.projectName,
});
clearSpinner();
} catch (error) {
clearSpinner();
spinner.clear();
clack.log.warn(
COPY.braintrustCli.configureFailed(summarizeBraintrustCliError(error)),
);
Expand Down Expand Up @@ -504,9 +516,9 @@ function braintrustCliContextConflicts(

async function preflightCodingTools(
deps: WizardDeps,
spinner: WizardStepSpinner,
): Promise<readonly CodingToolStatus[]> {
const spinner = clack.spinner();
spinner.start(COPY.instrumentation.builtIn.determiningAvailable);
spinner.update(COPY.instrumentation.builtIn.determiningAvailable);
try {
const statuses = await deps.codingTools.discover();
return await Promise.all(
Expand Down Expand Up @@ -746,9 +758,9 @@ async function runInstrumentation(
renderer.start();
let toolResult: CodingToolRunResult;

setTimeout(() => {
const spinnerDelay = setTimeout(() => {
spinner.start(COPY.instrumentation.builtIn.running(toolLabel));
}, 50);
}, 150);
try {
toolResult = await deps.codingTools.run({
id: args.toolId,
Expand All @@ -765,6 +777,7 @@ async function runInstrumentation(
await renderer.error(COPY.instrumentation.builtIn.codingAgentFailed);
throw error;
} finally {
clearTimeout(spinnerDelay);
spinner.clear();
}

Expand Down
46 changes: 46 additions & 0 deletions packages/spark/test/clack-wizard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,20 @@ describe("runClackWizard", () => {
expect(events).toContain("taskLog.success:Instrumentation complete.");
});

it("does not start the delayed coding agent spinner after a fast run", async () => {
const { events } = createPrompts({
selects: ["yes", "no", "first", "proceed", "understood"],
});
const deps = buildDeps();

await runClackWizard(deps);
await new Promise<void>((resolve) => setTimeout(resolve, 60));

expect(events).not.toContain(
"spinner.start:Checking Claude Code can run...",
);
});

it("passes browser auth mode based on the account answer", async () => {
const cases = [
{ answer: true, expectedAuthMode: "signin" },
Expand Down Expand Up @@ -768,9 +782,25 @@ describe("runClackWizard", () => {
expect(events).toContain(
"spinner.message:Configuring Braintrust CLI context...",
);
expect(events).toContain(
"spinner.message:Determining available coding agents...",
);
expect(events).toContain("spinner.clear");
expect(events).not.toContain("spinner.stop:Installed Braintrust CLI.");
expect(events).not.toContain("success:Configured Braintrust CLI.");
const configureSpinnerIndex = events.indexOf(
"spinner.message:Configuring Braintrust CLI context...",
);
const codingAgentSpinnerIndex = events.indexOf(
"spinner.message:Determining available coding agents...",
);
expect(codingAgentSpinnerIndex).toBeGreaterThan(configureSpinnerIndex);
expect(
events.slice(configureSpinnerIndex, codingAgentSpinnerIndex),
).not.toContain("spinner.clear");
expect(events).not.toContain(
"spinner.start:Determining available coding agents...",
);
expect(
events.indexOf("spinner.message:Checking Braintrust CLI context..."),
).toBeLessThan(events.indexOf(`select:${INSTRUMENTATION_MODE_MESSAGE}`));
Expand Down Expand Up @@ -882,9 +912,25 @@ describe("runClackWizard", () => {
expect(events).toContain(
"spinner.message:Configuring Braintrust CLI context...",
);
expect(events).toContain(
"spinner.message:Determining available coding agents...",
);
expect(events).toContain("spinner.clear");
expect(events).not.toContain("spinner.stop:Updated Braintrust CLI.");
expect(events).not.toContain("success:Configured Braintrust CLI.");
const configureSpinnerIndex = events.indexOf(
"spinner.message:Configuring Braintrust CLI context...",
);
const codingAgentSpinnerIndex = events.indexOf(
"spinner.message:Determining available coding agents...",
);
expect(codingAgentSpinnerIndex).toBeGreaterThan(configureSpinnerIndex);
expect(
events.slice(configureSpinnerIndex, codingAgentSpinnerIndex),
).not.toContain("spinner.clear");
expect(events).not.toContain(
"spinner.start:Determining available coding agents...",
);
expect(calls).toEqual(["update:/bin/bt", "status:/bin/bt", "login"]);
});

Expand Down
Loading