|
| 1 | +import { |
| 2 | + Alert, |
| 3 | + Card, |
| 4 | + CardBody, |
| 5 | + CardFooter, |
| 6 | + FormSubmitButton, |
| 7 | + FormV2, |
| 8 | + Link, |
| 9 | + LinkButton, |
| 10 | + Text, |
| 11 | +} from '@stacklok/ui-kit' |
| 12 | +import { twMerge } from 'tailwind-merge' |
| 13 | +import { useMutationPreferredModelWorkspace } from '../hooks/use-mutation-preferred-model-workspace' |
| 14 | +import { MuxMatcherType } from '@/api/generated' |
| 15 | +import { LinkExternal01 } from '@untitled-ui/icons-react' |
| 16 | +import { useQueryListAllModelsForAllProviders } from '@/hooks/use-query-list-all-models-for-all-providers' |
| 17 | +import { useQueryMuxingRulesWorkspace } from '../hooks/use-query-muxing-rules-workspace' |
| 18 | +import { FormMuxFieldsArray } from './form-mux-fields-array' |
| 19 | +import { |
| 20 | + schemaWorkspaceConfig, |
| 21 | + WorkspaceConfigFieldValues, |
| 22 | +} from '../lib/schema-mux' |
| 23 | +import { zodResolver } from '@hookform/resolvers/zod' |
| 24 | + |
| 25 | +const DEFAULT_VALUES: WorkspaceConfigFieldValues = { |
| 26 | + muxing_rules: [ |
| 27 | + { |
| 28 | + id: MuxMatcherType.CATCH_ALL, |
| 29 | + model: '', |
| 30 | + matcher: '', |
| 31 | + matcher_type: MuxMatcherType.CATCH_ALL, |
| 32 | + }, |
| 33 | + ], |
| 34 | +} |
| 35 | + |
| 36 | +function MissingProviderBanner() { |
| 37 | + return ( |
| 38 | + // TODO needs to update the related ui-kit component that diverges from the design |
| 39 | + <Alert |
| 40 | + variant="warning" |
| 41 | + className="bg-brand-200 font-normal text-primary dark:bg-[#272472]" |
| 42 | + title="You must configure at least one provider before selecting your desired model." |
| 43 | + > |
| 44 | + <LinkButton |
| 45 | + className="mt-4 text-white dark:bg-[#7D7DED]" |
| 46 | + href="/providers" |
| 47 | + > |
| 48 | + Configure a provider |
| 49 | + </LinkButton> |
| 50 | + </Alert> |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +export function WorkspaceMuxingModel({ |
| 55 | + className, |
| 56 | + workspaceName, |
| 57 | + isArchived, |
| 58 | +}: { |
| 59 | + className?: string |
| 60 | + workspaceName: string |
| 61 | + isArchived: boolean | undefined |
| 62 | +}) { |
| 63 | + const { data: muxingRules, isPending } = |
| 64 | + useQueryMuxingRulesWorkspace(workspaceName) |
| 65 | + |
| 66 | + const { mutateAsync } = useMutationPreferredModelWorkspace() |
| 67 | + const { data: providerModels = [] } = useQueryListAllModelsForAllProviders() |
| 68 | + const isModelsEmpty = !isPending && providerModels.length === 0 |
| 69 | + |
| 70 | + const handleSubmit = (data: WorkspaceConfigFieldValues) => { |
| 71 | + mutateAsync({ |
| 72 | + path: { workspace_name: workspaceName }, |
| 73 | + body: data.muxing_rules.map((rule) => { |
| 74 | + return rule.matcher |
| 75 | + ? { ...rule, matcher_type: MuxMatcherType.FILENAME_MATCH } |
| 76 | + : { ...rule } |
| 77 | + }), |
| 78 | + }) |
| 79 | + } |
| 80 | + |
| 81 | + if (isModelsEmpty) { |
| 82 | + return ( |
| 83 | + <Card className={twMerge(className, 'shrink-0')}> |
| 84 | + <CardBody className="flex flex-col gap-2"> |
| 85 | + <Text className="text-primary">Model Muxing</Text> |
| 86 | + <MissingProviderBanner /> |
| 87 | + </CardBody> |
| 88 | + </Card> |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + return ( |
| 93 | + <FormV2<WorkspaceConfigFieldValues> |
| 94 | + onSubmit={handleSubmit} |
| 95 | + data-testid="preferred-model" |
| 96 | + options={{ |
| 97 | + defaultValues: DEFAULT_VALUES, |
| 98 | + resolver: zodResolver(schemaWorkspaceConfig), |
| 99 | + }} |
| 100 | + > |
| 101 | + <Card className={twMerge(className, 'shrink-0')}> |
| 102 | + <CardBody className="flex flex-col gap-6"> |
| 103 | + <div className="flex flex-col justify-start"> |
| 104 | + <Text className="text-primary">Model Muxing</Text> |
| 105 | + <Text className="mb-0 flex items-center gap-1 text-balance text-secondary"> |
| 106 | + Select the model you would like to use in this workspace. This |
| 107 | + section applies only if you are using the MUX endpoint. |
| 108 | + <Link |
| 109 | + variant="primary" |
| 110 | + className="flex items-center gap-1 no-underline" |
| 111 | + href="https://docs.codegate.ai/features/muxing" |
| 112 | + target="_blank" |
| 113 | + > |
| 114 | + Learn more <LinkExternal01 className="size-4" /> |
| 115 | + </Link> |
| 116 | + </Text> |
| 117 | + </div> |
| 118 | + |
| 119 | + <div className="flex w-full flex-col gap-2"> |
| 120 | + <FormMuxFieldsArray /> |
| 121 | + </div> |
| 122 | + </CardBody> |
| 123 | + <div></div> |
| 124 | + |
| 125 | + <CardFooter className="justify-end"> |
| 126 | + <FormSubmitButton /> |
| 127 | + </CardFooter> |
| 128 | + </Card> |
| 129 | + </FormV2> |
| 130 | + ) |
| 131 | +} |
0 commit comments