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

AddProviderInstanceDialog.logic.ts

111 lines3.4 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;
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;
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;
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 };
41​
42export function isConfiguredAcpRegistryAgent(
43 instances: Readonly<Record<string, { readonly driver: string; readonly config?: unknown }>>,
44 agentId: string,
45): boolean {
46 return Object.values(instances).some((instance) => {
47 if (
48 instance.driver !== "acpRegistry" ||
49 !instance.config ||
50 typeof instance.config !== "object"
51 ) {
52 return false;
53 }
54 const config = instance.config as Record<string, unknown>;
55 return config.source !== "local" && config.agentId === agentId;
56 });
58​
59export function resolveWizardNavigation(
60 currentStep: number,
61 requestedStep: number,
62 stepCount: number,
63 validation: {
64 readonly instanceIdError: string | null;
65 readonly identityStep?: number;
66 readonly prerequisite?: {
67 readonly step: number;
68 readonly error: string | null;
69 };
70 },
71): WizardNavigation {
72 const lastStep = Math.max(0, stepCount - 1);
73 const targetStep = Math.max(0, Math.min(lastStep, requestedStep));
74 const identityStep = validation.identityStep ?? IDENTITY_STEP;
75 const prerequisite = validation.prerequisite;
76​
77 if (prerequisite?.error && currentStep <= prerequisite.step && targetStep > prerequisite.step) {
78 return {
79 kind: "blocked",
80 step: Math.min(prerequisite.step, lastStep),
81 error: prerequisite.error,
82 };
83 }
84​
85 const movesForwardPastIdentity = currentStep <= identityStep && targetStep > identityStep;
86​
87 if (movesForwardPastIdentity && validation.instanceIdError !== null) {
88 return {
89 kind: "blocked",
90 step: Math.min(identityStep, lastStep),
91 error: validation.instanceIdError,
92 };
93 }
94​
95 return { kind: "navigate", step: targetStep };
97​
98export function resolveAcpRegistryWizardNavigation(
99 currentStep: number,
100 requestedStep: number,
101 validation: {
102 readonly instanceIdError: string | null;
103 readonly selectionError: string | null;
104 },
105): WizardNavigation {
106 return resolveWizardNavigation(currentStep, requestedStep, ACP_REGISTRY_WIZARD_STEPS.length, {
107 instanceIdError: validation.instanceIdError,
108 identityStep: ACP_REGISTRY_IDENTITY_STEP,
109 prerequisite: { step: 0, error: validation.selectionError },
110 });