135 lines
4.1 KiB
TypeScript
135 lines
4.1 KiB
TypeScript
export type WizardNavigation =
|
|
| { readonly kind: "navigate"; readonly step: number }
|
|
| { readonly kind: "blocked"; readonly step: number; readonly error: string };
|
|
|
|
const IDENTITY_STEP = 1;
|
|
const ACP_REGISTRY_IDENTITY_STEP = 1;
|
|
|
|
export const ADD_PROVIDER_WIZARD_STEPS = ["Provider", "Identity", "Config"] as const;
|
|
export const ACP_REGISTRY_WIZARD_STEPS = ["Provider", "Identity", "Sign in"] as const;
|
|
export const LOCAL_ACP_WIZARD_STEPS = ["Provider", "Identity"] as const;
|
|
|
|
export interface ProviderIdentityDraft {
|
|
readonly label: string;
|
|
readonly accentColor: string;
|
|
readonly instanceIdOverride: string | null;
|
|
}
|
|
|
|
const EMPTY_PROVIDER_IDENTITY_DRAFT: ProviderIdentityDraft = {
|
|
label: "",
|
|
accentColor: "",
|
|
instanceIdOverride: null,
|
|
};
|
|
|
|
export function getProviderIdentityDraft(
|
|
drafts: Readonly<Record<string, ProviderIdentityDraft>>,
|
|
driver: string,
|
|
): ProviderIdentityDraft {
|
|
return drafts[driver] ?? EMPTY_PROVIDER_IDENTITY_DRAFT;
|
|
}
|
|
|
|
export function updateProviderIdentityDraft(
|
|
drafts: Readonly<Record<string, ProviderIdentityDraft>>,
|
|
driver: string,
|
|
update: Partial<ProviderIdentityDraft>,
|
|
): Record<string, ProviderIdentityDraft> {
|
|
return {
|
|
...drafts,
|
|
[driver]: { ...getProviderIdentityDraft(drafts, driver), ...update },
|
|
};
|
|
}
|
|
|
|
export function deriveAvailableInstanceId(
|
|
derive: (label: string) => string,
|
|
label: string,
|
|
existing: ReadonlySet<string>,
|
|
): string {
|
|
const base = derive(label);
|
|
if (!base || !existing.has(base)) return base;
|
|
|
|
for (let suffix = 2; ; suffix += 1) {
|
|
const suffixText = `_${suffix}`;
|
|
const candidate = `${base.slice(0, 64 - suffixText.length)}${suffixText}`;
|
|
if (!existing.has(candidate)) return candidate;
|
|
}
|
|
}
|
|
|
|
export function isConfiguredAcpRegistryAgent(
|
|
instances: Readonly<Record<string, { readonly driver: string; readonly config?: unknown }>>,
|
|
agentId: string,
|
|
): boolean {
|
|
return Object.values(instances).some((instance) => {
|
|
if (
|
|
instance.driver !== "acpRegistry" ||
|
|
!instance.config ||
|
|
typeof instance.config !== "object"
|
|
) {
|
|
return false;
|
|
}
|
|
const config = instance.config as Record<string, unknown>;
|
|
return config.source !== "local" && config.agentId === agentId;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Resolve navigation within the add-provider wizard.
|
|
*
|
|
* Moving forward past Identity requires a valid instance id, whether the user
|
|
* advances one step at a time or skips directly to Config from a step header.
|
|
* A blocked skip lands on Identity so its existing inline validation is
|
|
* visible. Backward navigation is always preserved.
|
|
*/
|
|
export function resolveWizardNavigation(
|
|
currentStep: number,
|
|
requestedStep: number,
|
|
stepCount: number,
|
|
validation: {
|
|
readonly instanceIdError: string | null;
|
|
readonly identityStep?: number;
|
|
readonly prerequisite?: {
|
|
readonly step: number;
|
|
readonly error: string | null;
|
|
};
|
|
},
|
|
): WizardNavigation {
|
|
const lastStep = Math.max(0, stepCount - 1);
|
|
const targetStep = Math.max(0, Math.min(lastStep, requestedStep));
|
|
const identityStep = validation.identityStep ?? IDENTITY_STEP;
|
|
const prerequisite = validation.prerequisite;
|
|
|
|
if (prerequisite?.error && currentStep <= prerequisite.step && targetStep > prerequisite.step) {
|
|
return {
|
|
kind: "blocked",
|
|
step: Math.min(prerequisite.step, lastStep),
|
|
error: prerequisite.error,
|
|
};
|
|
}
|
|
|
|
const movesForwardPastIdentity = currentStep <= identityStep && targetStep > identityStep;
|
|
|
|
if (movesForwardPastIdentity && validation.instanceIdError !== null) {
|
|
return {
|
|
kind: "blocked",
|
|
step: Math.min(identityStep, lastStep),
|
|
error: validation.instanceIdError,
|
|
};
|
|
}
|
|
|
|
return { kind: "navigate", step: targetStep };
|
|
}
|
|
|
|
export function resolveAcpRegistryWizardNavigation(
|
|
currentStep: number,
|
|
requestedStep: number,
|
|
validation: {
|
|
readonly instanceIdError: string | null;
|
|
readonly selectionError: string | null;
|
|
},
|
|
): WizardNavigation {
|
|
return resolveWizardNavigation(currentStep, requestedStep, ACP_REGISTRY_WIZARD_STEPS.length, {
|
|
instanceIdError: validation.instanceIdError,
|
|
identityStep: ACP_REGISTRY_IDENTITY_STEP,
|
|
prerequisite: { step: 0, error: validation.selectionError },
|
|
});
|
|
}
|