Open History to see the last commit
1export type WizardNavigation =
2 | { readonly kind: "navigate"; readonly step: number }
3 | { readonly kind: "blocked"; readonly step: number; readonly error: string };
4
5const IDENTITY_STEP = 1;
6const ACP_REGISTRY_IDENTITY_STEP = 1;
7
8export const ADD_PROVIDER_WIZARD_STEPS = ["Provider", "Identity", "Config"] as const;
9export const ACP_REGISTRY_WIZARD_STEPS = ["Provider", "Identity", "Sign in"] as const;
10export const LOCAL_ACP_WIZARD_STEPS = ["Provider", "Identity"] as const;
11
12export interface ProviderIdentityDraft {
13 readonly label: string;
14 readonly accentColor: string;
15 readonly instanceIdOverride: string | null;
16}
17
18const EMPTY_PROVIDER_IDENTITY_DRAFT: ProviderIdentityDraft = {
19 label: "",
20 accentColor: "",
21 instanceIdOverride: null,
22};
23
24export function getProviderIdentityDraft(
25 drafts: Readonly<Record<string, ProviderIdentityDraft>>,
26 driver: string,
27): ProviderIdentityDraft {
28 return drafts[driver] ?? EMPTY_PROVIDER_IDENTITY_DRAFT;
29}
30
31export function updateProviderIdentityDraft(
32 drafts: Readonly<Record<string, ProviderIdentityDraft>>,
33 driver: string,
34 update: Partial<ProviderIdentityDraft>,
35): Record<string, ProviderIdentityDraft> {
36 return {
37 ...drafts,
38 [driver]: { ...getProviderIdentityDraft(drafts, driver), ...update },
39 };
40}
41
42export function deriveAvailableInstanceId(
43 derive: (label: string) => string,
44 label: string,
45 existing: ReadonlySet<string>,
46): string {
47 const base = derive(label);
48 if (!base || !existing.has(base)) return base;
49
50 for (let suffix = 2; ; suffix += 1) {
51 const suffixText = `_${suffix}`;
52 const candidate = `${base.slice(0, 64 - suffixText.length)}${suffixText}`;
53 if (!existing.has(candidate)) return candidate;
54 }
55}
56
57export function isConfiguredAcpRegistryAgent(
58 instances: Readonly<Record<string, { readonly driver: string; readonly config?: unknown }>>,
59 agentId: string,
60): boolean {
61 return Object.values(instances).some((instance) => {
62 if (
63 instance.driver !== "acpRegistry" ||
64 !instance.config ||
65 typeof instance.config !== "object"
66 ) {
67 return false;
68 }
69 const config = instance.config as Record<string, unknown>;
70 return config.source !== "local" && config.agentId === agentId;
71 });
72}
73
74export function resolveWizardNavigation(
75 currentStep: number,
76 requestedStep: number,
77 stepCount: number,
78 validation: {
79 readonly instanceIdError: string | null;
80 readonly identityStep?: number;
81 readonly prerequisite?: {
82 readonly step: number;
83 readonly error: string | null;
84 };
85 },
86): WizardNavigation {
87 const lastStep = Math.max(0, stepCount - 1);
88 const targetStep = Math.max(0, Math.min(lastStep, requestedStep));
89 const identityStep = validation.identityStep ?? IDENTITY_STEP;
90 const prerequisite = validation.prerequisite;
91
92 if (prerequisite?.error && currentStep <= prerequisite.step && targetStep > prerequisite.step) {
93 return {
94 kind: "blocked",
95 step: Math.min(prerequisite.step, lastStep),
96 error: prerequisite.error,
97 };
98 }
99
100 const movesForwardPastIdentity = currentStep <= identityStep && targetStep > identityStep;
101
102 if (movesForwardPastIdentity && validation.instanceIdError !== null) {
103 return {
104 kind: "blocked",
105 step: Math.min(identityStep, lastStep),
106 error: validation.instanceIdError,
107 };
108 }
109
110 return { kind: "navigate", step: targetStep };
111}
112
113export function resolveAcpRegistryWizardNavigation(
114 currentStep: number,
115 requestedStep: number,
116 validation: {
117 readonly instanceIdError: string | null;
118 readonly selectionError: string | null;
119 },
120): WizardNavigation {
121 return resolveWizardNavigation(currentStep, requestedStep, ACP_REGISTRY_WIZARD_STEPS.length, {
122 instanceIdError: validation.instanceIdError,
123 identityStep: ACP_REGISTRY_IDENTITY_STEP,
124 prerequisite: { step: 0, error: validation.selectionError },
125 });
126}