Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AddProviderInstanceDialog.logic.ts

132 lines4.0 KB

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;
10​
11export interface ProviderIdentityDraft {
12 readonly label: string;
13 readonly accentColor: string;
14 readonly instanceIdOverride: string | null;
16​
17const EMPTY_PROVIDER_IDENTITY_DRAFT: ProviderIdentityDraft = {
18 label: "",
19 accentColor: "",
20 instanceIdOverride: null,
21};
22​
23export function getProviderIdentityDraft(
24 drafts: Readonly<Record<string, ProviderIdentityDraft>>,
25 driver: string,
26): ProviderIdentityDraft {
27 return drafts[driver] ?? EMPTY_PROVIDER_IDENTITY_DRAFT;
29​
30export function updateProviderIdentityDraft(
31 drafts: Readonly<Record<string, ProviderIdentityDraft>>,
32 driver: string,
33 update: Partial<ProviderIdentityDraft>,
34): Record<string, ProviderIdentityDraft> {
35 return {
36 ...drafts,
37 [driver]: { ...getProviderIdentityDraft(drafts, driver), ...update },
38 };
40​
41export function deriveAvailableInstanceId(
42 derive: (label: string) => string,
43 label: string,
44 existing: ReadonlySet<string>,
45): string {
46 const base = derive(label);
47 if (!base || !existing.has(base)) return base;
48​
49 for (let suffix = 2; ; suffix += 1) {
50 const suffixText = `_${suffix}`;
51 const candidate = `${base.slice(0, 64 - suffixText.length)}${suffixText}`;
52 if (!existing.has(candidate)) return candidate;
53 }
55​
56export function isConfiguredAcpRegistryAgent(
57 instances: Readonly<Record<string, { readonly driver: string; readonly config?: unknown }>>,
58 agentId: string,
59): boolean {
60 return Object.values(instances).some((instance) => {
61 if (
62 instance.driver !== "acpRegistry" ||
63 !instance.config ||
64 typeof instance.config !== "object"
65 ) {
66 return false;
67 }
68 return (instance.config as Record<string, unknown>).agentId === agentId;
69 });
71​
72/**
73 * Resolve navigation within the add-provider wizard.
74 *
75 * Moving forward past Identity requires a valid instance id, whether the user
76 * advances one step at a time or skips directly to Config from a step header.
77 * A blocked skip lands on Identity so its existing inline validation is
78 * visible. Backward navigation is always preserved.
79 */
80export function resolveWizardNavigation(
81 currentStep: number,
82 requestedStep: number,
83 stepCount: number,
84 validation: {
85 readonly instanceIdError: string | null;
86 readonly identityStep?: number;
87 readonly prerequisite?: {
88 readonly step: number;
89 readonly error: string | null;
90 };
91 },
92): WizardNavigation {
93 const lastStep = Math.max(0, stepCount - 1);
94 const targetStep = Math.max(0, Math.min(lastStep, requestedStep));
95 const identityStep = validation.identityStep ?? IDENTITY_STEP;
96 const prerequisite = validation.prerequisite;
97​
98 if (prerequisite?.error && currentStep <= prerequisite.step && targetStep > prerequisite.step) {
99 return {
100 kind: "blocked",
101 step: Math.min(prerequisite.step, lastStep),
102 error: prerequisite.error,
103 };
104 }
105​
106 const movesForwardPastIdentity = currentStep <= identityStep && targetStep > identityStep;
107​
108 if (movesForwardPastIdentity && validation.instanceIdError !== null) {
109 return {
110 kind: "blocked",
111 step: Math.min(identityStep, lastStep),
112 error: validation.instanceIdError,
113 };
114 }
115​
116 return { kind: "navigate", step: targetStep };
118​
119export function resolveAcpRegistryWizardNavigation(
120 currentStep: number,
121 requestedStep: number,
122 validation: {
123 readonly instanceIdError: string | null;
124 readonly selectionError: string | null;
125 },
126): WizardNavigation {
127 return resolveWizardNavigation(currentStep, requestedStep, ACP_REGISTRY_WIZARD_STEPS.length, {
128 instanceIdError: validation.instanceIdError,
129 identityStep: ACP_REGISTRY_IDENTITY_STEP,
130 prerequisite: { step: 0, error: validation.selectionError },
131 });