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
74 changes: 56 additions & 18 deletions packages/spark/src/clack-wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ type InstrumentationModeChoice =

type OwnAgentPromptDelivery = "clipboard" | "terminal";

type BraintrustCliBackgroundTask = {
readonly wait: (spinner: WizardStepSpinner) => Promise<void>;
};

function createBraintrustCliBackgroundTask(
task: Promise<unknown>,
): BraintrustCliBackgroundTask {
const result = task.then(
() => undefined,
(error: unknown) => error,
);

return {
wait: async (waitSpinner) => {
const error = await result;
if (error !== undefined) {
waitSpinner.clear();
clack.log.warn(
COPY.braintrustCli.configureFailed(
summarizeBraintrustCliError(error),
),
);
}
},
};
}

const BUILT_IN_INSTRUMENTATION_CHOICE: BuiltInInstrumentationChoice = {
id: "built-in",
label: COPY.instrumentation.modes.builtIn.label,
Expand Down Expand Up @@ -225,8 +252,13 @@ export async function runClackWizard(deps: WizardDeps): Promise<WizardResult> {

const setupSpinner = new WizardStepSpinner();
let codingToolStatuses: readonly CodingToolStatus[];
let braintrustCliBackgroundTask: BraintrustCliBackgroundTask | undefined;
try {
await handleBraintrustCliSetup(deps, session, setupSpinner);
braintrustCliBackgroundTask = await handleBraintrustCliSetup(
deps,
session,
setupSpinner,
);
codingToolStatuses = await preflightCodingTools(deps, setupSpinner);
} finally {
setupSpinner.clear();
Expand All @@ -242,6 +274,8 @@ export async function runClackWizard(deps: WizardDeps): Promise<WizardResult> {
await selectInstrumentationMode({
includeBuiltIn: hasUsableCodingTool,
});
await braintrustCliBackgroundTask?.wait(setupSpinner);
setupSpinner.clear();

if (instrumentationMode.id === "built-in") {
const instrumentation = await selectBuiltInCodingTool(codingToolStatuses);
Expand Down Expand Up @@ -391,7 +425,7 @@ async function handleBraintrustCliSetup(
deps: WizardDeps,
session: WizardSessionCompleteResult,
spinner: WizardStepSpinner,
): Promise<void> {
): Promise<BraintrustCliBackgroundTask | undefined> {
let discovery = await deps.braintrustCli.discover();
let commandPath = discovery.commandPath;

Expand Down Expand Up @@ -434,7 +468,7 @@ async function handleBraintrustCliSetup(
choices: COPY.braintrustCli.installChoices,
yesFirst: true,
});
if (!shouldInstall) return;
if (!shouldInstall) return undefined;

spinner.update(COPY.braintrustCli.installing);
try {
Expand All @@ -444,27 +478,27 @@ async function handleBraintrustCliSetup(
clack.log.warn(
COPY.braintrustCli.installFailed(summarizeBraintrustCliError(error)),
);
return;
return undefined;
}

discovery = await deps.braintrustCli.discover();
commandPath = discovery.commandPath;
if (!discovery.installed || !commandPath) {
spinner.clear();
clack.log.warn(COPY.braintrustCli.installedButNotFound);
return;
return undefined;
}
}

if (!commandPath) return;
if (!commandPath) return undefined;

let currentContext: BraintrustCliContext;
spinner.update(COPY.braintrustCli.checkingContext);
try {
currentContext = await deps.braintrustCli.status(commandPath);
} catch {
spinner.clear();
return;
return undefined;
}

const targetContext = {
Expand All @@ -483,25 +517,29 @@ async function handleBraintrustCliSetup(
yesFirst: true,
});
if (!shouldSwitch) {
return;
return undefined;
}

return createBraintrustCliBackgroundTask(
deps.braintrustCli.loginAndSwitch(commandPath, {
apiKey: session.apiKey,
apiUrl: deps.options.apiUrl,
appUrl: deps.options.appUrl,
orgName: session.orgName,
projectName: session.projectName,
}),
);
}

spinner.update(COPY.braintrustCli.configuringContext);
try {
await deps.braintrustCli.loginAndSwitch(commandPath, {
return createBraintrustCliBackgroundTask(
deps.braintrustCli.loginAndSwitch(commandPath, {
apiKey: session.apiKey,
apiUrl: deps.options.apiUrl,
appUrl: deps.options.appUrl,
orgName: session.orgName,
projectName: session.projectName,
});
} catch (error) {
spinner.clear();
clack.log.warn(
COPY.braintrustCli.configureFailed(summarizeBraintrustCliError(error)),
);
}
}),
);
}

function braintrustCliContextConflicts(
Expand Down
1 change: 0 additions & 1 deletion packages/spark/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function buildParser() {
return yargs([])
.scriptName("braintrust-setup")
.usage("$0 [options]")
.env("BRAINTRUST")
.option("api-url", {
type: "string",
description: "Override API URL",
Expand Down
88 changes: 56 additions & 32 deletions packages/spark/test/clack-wizard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -841,32 +841,17 @@ describe("runClackWizard", () => {
expect(events).toContain(
"spinner.message:Checking Braintrust CLI login state...",
);
expect(events).toContain(
"spinner.message:Configuring Braintrust CLI login state...",
);
expect(events).toContain(`spinner.message:${CODING_AGENT_SCAN_MESSAGE}`);
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(
expect(events).not.toContain(
"spinner.message:Configuring Braintrust CLI login state...",
);
const codingAgentSpinnerIndex = events.indexOf(
`spinner.message:${CODING_AGENT_SCAN_MESSAGE}`,
);
expect(codingAgentSpinnerIndex).toBeGreaterThan(configureSpinnerIndex);
expect(
events.slice(configureSpinnerIndex, codingAgentSpinnerIndex),
).not.toContain("spinner.clear");
expect(events).not.toContain(`spinner.start:${CODING_AGENT_SCAN_MESSAGE}`);
expect(
events.indexOf("spinner.message:Checking Braintrust CLI login state..."),
).toBeLessThan(events.indexOf(`select:${INSTRUMENTATION_MODE_MESSAGE}`));
expect(
events.indexOf(
"spinner.message:Configuring Braintrust CLI login state...",
),
).toBeLessThan(events.indexOf(`select:${INSTRUMENTATION_MODE_MESSAGE}`));
expect(calls).toEqual([
"install",
"status:/usr/local/bin/bt",
Expand Down Expand Up @@ -1045,23 +1030,13 @@ describe("runClackWizard", () => {
expect(events).toContain(
"spinner.message:Checking Braintrust CLI login state...",
);
expect(events).toContain(
"spinner.message:Configuring Braintrust CLI login state...",
);
expect(events).toContain(`spinner.message:${CODING_AGENT_SCAN_MESSAGE}`);
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(
expect(events).not.toContain(
"spinner.message:Configuring Braintrust CLI login state...",
);
const codingAgentSpinnerIndex = events.indexOf(
`spinner.message:${CODING_AGENT_SCAN_MESSAGE}`,
);
expect(codingAgentSpinnerIndex).toBeGreaterThan(configureSpinnerIndex);
expect(
events.slice(configureSpinnerIndex, codingAgentSpinnerIndex),
).not.toContain("spinner.clear");
expect(events).not.toContain(`spinner.start:${CODING_AGENT_SCAN_MESSAGE}`);
expect(calls).toEqual(["update:/bin/bt", "status:/bin/bt", "login"]);
});
Expand Down Expand Up @@ -1131,7 +1106,7 @@ describe("runClackWizard", () => {
expect(events).toContain(
"spinner.start:Checking Braintrust CLI login state...",
);
expect(events).toContain(
expect(events).not.toContain(
"spinner.message:Configuring Braintrust CLI login state...",
);
expect(calls).toEqual(["login"]);
Expand Down Expand Up @@ -1229,7 +1204,11 @@ describe("runClackWizard", () => {

it("switches a different Braintrust CLI context when accepted", async () => {
const calls: string[] = [];
createPrompts({
let finishSwitch!: () => void;
const switchDone = new Promise<void>((resolve) => {
finishSwitch = resolve;
});
const { events } = createPrompts({
selects: [
"yes",
"no",
Expand All @@ -1249,14 +1228,59 @@ describe("runClackWizard", () => {
Promise.resolve({ profile: "work", org: "other", project: "old" }),
loginAndSwitch: () => {
calls.push("login");
return Promise.resolve();
return switchDone;
},
}),
codingTools: {
discover: () => {
calls.push("discover-coding-tools");
return Promise.resolve([
{
id: "claude",
label: "Claude Code",
command: "claude",
installed: true,
usable: true,
authMode: "pro",
},
]);
},
smokeTest: () =>
Promise.resolve({
exitCode: 0,
signal: null,
finalText: "BRAINTRUST_SETUP_TOOL_OK",
}),
run: () =>
Promise.resolve({
exitCode: 0,
signal: null,
finalText: "BRAINTRUST_SETUP_TOOL_OK",
}),
},
});

await runClackWizard(deps);
let runComplete = false;
const runPromise = runClackWizard(deps).then(() => {
runComplete = true;
});
await vi.waitFor(() => {
expect(calls).toEqual(["login", "discover-coding-tools"]);
expect(events).toContain(
"select:How do you want to add Braintrust to your application?",
);
});
expect(runComplete).toBe(false);

expect(calls).toEqual(["login"]);
finishSwitch();
await runPromise;

expect(runComplete).toBe(true);
expect(calls).toEqual(["login", "discover-coding-tools"]);
expect(events).not.toContain(
"spinner.start:Configuring Braintrust CLI login state...",
);
expect(events).toContain(`spinner.start:${CODING_AGENT_SCAN_MESSAGE}`);
});

it("does not configure the Braintrust CLI when status inspection fails", async () => {
Expand Down
Loading