Open History to see the last commit
1import {
2 defaultLaunchForFeaturedAgent,
3 featuredAgentById,
4 formatAcpLaunchArgs,
5 ProviderDriverKind,
6} from "@t3tools/contracts";
7
8export type WizardNavigation =
9 | { readonly kind: "navigate"; readonly step: number }
10 | { readonly kind: "blocked"; readonly step: number; readonly error: string };
11
12const IDENTITY_STEP = 1;
13export const ACP_REGISTRY_DRIVER = ProviderDriverKind.make("acpRegistry");
14export const ACP_PICKER_PREFIX = "acp:";
15
16export const ADD_PROVIDER_WIZARD_STEPS = ["Driver", "Identity", "Config"] as const;
17
18export function acpPickerValue(catalogId: string): string {
19 return `${ACP_PICKER_PREFIX}${catalogId}`;
20}
21
22export function parseAddProviderPickerValue(value: string): {
23 readonly driver: ProviderDriverKind;
24 readonly catalogId: string | undefined;
25} {
26 if (value.startsWith(ACP_PICKER_PREFIX)) {
27 return {
28 driver: ACP_REGISTRY_DRIVER,
29 catalogId: value.slice(ACP_PICKER_PREFIX.length),
30 };
31 }
32 return { driver: ProviderDriverKind.make(value), catalogId: undefined };
33}
34
35export function configDraftForFeaturedAgent(catalogId: string): Record<string, unknown> {
36 const agent = featuredAgentById(catalogId);
37 if (!agent || agent.id === "custom") {
38 return {};
39 }
40 const launch = defaultLaunchForFeaturedAgent(agent);
41 return {
42 catalogId: agent.id,
43 ...(launch
44 ? {
45 command: launch.command,
46 launchArgs: formatAcpLaunchArgs(launch.args),
47 }
48 : {}),
49 };
50}
51
52/**
53 * Resolve navigation within the add-provider wizard.
54 *
55 * Moving forward past Identity requires a valid instance id, whether the user
56 * advances one step at a time or skips directly to Config from a step header.
57 * A blocked skip lands on Identity so its existing inline validation is
58 * visible. Backward navigation is always preserved.
59 */
60export function resolveWizardNavigation(
61 currentStep: number,
62 requestedStep: number,
63 stepCount: number,
64 validation: { readonly instanceIdError: string | null },
65): WizardNavigation {
66 const lastStep = Math.max(0, stepCount - 1);
67 const targetStep = Math.max(0, Math.min(lastStep, requestedStep));
68 const movesForwardPastIdentity = currentStep <= IDENTITY_STEP && targetStep > IDENTITY_STEP;
69
70 if (movesForwardPastIdentity && validation.instanceIdError !== null) {
71 return {
72 kind: "blocked",
73 step: Math.min(IDENTITY_STEP, lastStep),
74 error: validation.instanceIdError,
75 };
76 }
77
78 return { kind: "navigate", step: targetStep };
79}