The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { msg } from "gt-react"; |
| 2 | |
| 3 | export type WizardNavigation = |
| 4 | | { readonly kind: "navigate"; readonly step: number } |
| 5 | | { readonly kind: "blocked"; readonly step: number; readonly error: string }; |
| 6 | |
| 7 | const IDENTITY_STEP = 1; |
| 8 | const ACP_REGISTRY_IDENTITY_STEP = 1; |
| 9 | |
| 10 | const PROVIDER_STEP = msg("Provider", { $context: "wizard step" }); |
| 11 | const IDENTITY_STEP_LABEL = msg("Identity", { $context: "wizard step" }); |
| 12 | |
| 13 | export const ADD_PROVIDER_WIZARD_STEPS = [ |
| 14 | PROVIDER_STEP, |
| 15 | IDENTITY_STEP_LABEL, |
| 16 | msg("Config", { $context: "wizard step" }), |
| 17 | ] as const; |
| 18 | export const ACP_REGISTRY_WIZARD_STEPS = [ |
| 19 | PROVIDER_STEP, |
| 20 | IDENTITY_STEP_LABEL, |
| 21 | msg("Sign in", { $context: "wizard step" }), |
| 22 | ] as const; |
| 23 | export const LOCAL_ACP_WIZARD_STEPS = [PROVIDER_STEP, IDENTITY_STEP_LABEL] as const; |
| 24 | |
| 25 | export interface ProviderIdentityDraft { |
| 26 | readonly label: string; |
| 27 | readonly accentColor: string; |
| 28 | readonly instanceIdOverride: string | null; |
| 29 | } |
| 30 | |
| 31 | const EMPTY_PROVIDER_IDENTITY_DRAFT: ProviderIdentityDraft = { |
| 32 | label: "", |
| 33 | accentColor: "", |
| 34 | instanceIdOverride: null, |
| 35 | }; |
| 36 | |
| 37 | export function getProviderIdentityDraft( |
| 38 | drafts: Readonly<Record<string, ProviderIdentityDraft>>, |
| 39 | driver: string, |
| 40 | ): ProviderIdentityDraft { |
| 41 | return drafts[driver] ?? EMPTY_PROVIDER_IDENTITY_DRAFT; |
| 42 | } |
| 43 | |
| 44 | export function updateProviderIdentityDraft( |
| 45 | drafts: Readonly<Record<string, ProviderIdentityDraft>>, |
| 46 | driver: string, |
| 47 | update: Partial<ProviderIdentityDraft>, |
| 48 | ): Record<string, ProviderIdentityDraft> { |
| 49 | return { |
| 50 | ...drafts, |
| 51 | [driver]: { ...getProviderIdentityDraft(drafts, driver), ...update }, |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | export function isConfiguredAcpRegistryAgent( |
| 56 | instances: Readonly<Record<string, { readonly driver: string; readonly config?: unknown }>>, |
| 57 | agentId: string, |
| 58 | ): boolean { |
| 59 | return Object.values(instances).some((instance) => { |
| 60 | if ( |
| 61 | instance.driver !== "acpRegistry" || |
| 62 | !instance.config || |
| 63 | typeof instance.config !== "object" |
| 64 | ) { |
| 65 | return false; |
| 66 | } |
| 67 | const config = instance.config as Record<string, unknown>; |
| 68 | return config.source !== "local" && config.agentId === agentId; |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | export function resolveWizardNavigation( |
| 73 | currentStep: number, |
| 74 | requestedStep: number, |
| 75 | stepCount: number, |
| 76 | validation: { |
| 77 | readonly instanceIdError: string | null; |
| 78 | readonly identityStep?: number; |
| 79 | readonly prerequisite?: { |
| 80 | readonly step: number; |
| 81 | readonly error: string | null; |
| 82 | }; |
| 83 | }, |
| 84 | ): WizardNavigation { |
| 85 | const lastStep = Math.max(0, stepCount - 1); |
| 86 | const targetStep = Math.max(0, Math.min(lastStep, requestedStep)); |
| 87 | const identityStep = validation.identityStep ?? IDENTITY_STEP; |
| 88 | const prerequisite = validation.prerequisite; |
| 89 | |
| 90 | if (prerequisite?.error && currentStep <= prerequisite.step && targetStep > prerequisite.step) { |
| 91 | return { |
| 92 | kind: "blocked", |
| 93 | step: Math.min(prerequisite.step, lastStep), |
| 94 | error: prerequisite.error, |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | const movesForwardPastIdentity = currentStep <= identityStep && targetStep > identityStep; |
| 99 | |
| 100 | if (movesForwardPastIdentity && validation.instanceIdError !== null) { |
| 101 | return { |
| 102 | kind: "blocked", |
| 103 | step: Math.min(identityStep, lastStep), |
| 104 | error: validation.instanceIdError, |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | return { kind: "navigate", step: targetStep }; |
| 109 | } |
| 110 | |
| 111 | export function resolveAcpRegistryWizardNavigation( |
| 112 | currentStep: number, |
| 113 | requestedStep: number, |
| 114 | validation: { |
| 115 | readonly instanceIdError: string | null; |
| 116 | readonly selectionError: string | null; |
| 117 | }, |
| 118 | ): WizardNavigation { |
| 119 | return resolveWizardNavigation(currentStep, requestedStep, ACP_REGISTRY_WIZARD_STEPS.length, { |
| 120 | instanceIdError: validation.instanceIdError, |
| 121 | identityStep: ACP_REGISTRY_IDENTITY_STEP, |
| 122 | prerequisite: { step: 0, error: validation.selectionError }, |
| 123 | }); |
| 124 | } |