mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 05:54:44 +00:00
Merge pull request #121 from shumaiOne/feat/api-protocol-enum
feat(api): use enum for API protocol in provider config
This commit is contained in:
@@ -306,7 +306,7 @@ describe('Agent Database Activities Integration', () => {
|
|||||||
teamId: team.id,
|
teamId: team.id,
|
||||||
name: 'openai',
|
name: 'openai',
|
||||||
config: {
|
config: {
|
||||||
api: 'openai',
|
api: 'openai-completions',
|
||||||
apiKey: 'mock-key',
|
apiKey: 'mock-key',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -315,7 +315,7 @@ describe('AuthzService', () => {
|
|||||||
|
|
||||||
// Provider
|
// Provider
|
||||||
const provider = await prisma.provider.create({
|
const provider = await prisma.provider.create({
|
||||||
data: { name: 'Prov 1', teamId: team.id, config: { api: 'openai' } },
|
data: { name: 'Prov 1', teamId: team.id, config: { api: 'openai-completions' } },
|
||||||
})
|
})
|
||||||
|
|
||||||
// Invite
|
// Invite
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ describe('ProviderService', () => {
|
|||||||
expect(providers[0].name).toBe(firstInGenerated)
|
expect(providers[0].name).toBe(firstInGenerated)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should prevent deletion of builtin providers', async () => {
|
it('should allow deletion of builtin providers', async () => {
|
||||||
const team = await teamService.ensureDefaultTeam()
|
const team = await teamService.ensureDefaultTeam()
|
||||||
await providerService.initBuiltinProviders(team.id)
|
await providerService.initBuiltinProviders(team.id)
|
||||||
|
|
||||||
@@ -46,9 +46,9 @@ describe('ProviderService', () => {
|
|||||||
const builtin = providers.find((p) => p.isBuiltin)
|
const builtin = providers.find((p) => p.isBuiltin)
|
||||||
expect(builtin).toBeDefined()
|
expect(builtin).toBeDefined()
|
||||||
|
|
||||||
await expect(providerService.delete(team.id, builtin!.id)).rejects.toThrow(
|
await providerService.delete(team.id, builtin!.id)
|
||||||
'Cannot delete builtin provider',
|
const afterDelete = await providerService.listByTeam(team.id)
|
||||||
)
|
expect(afterDelete.find((p) => p.id === builtin!.id)).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should allow editing builtin providers', async () => {
|
it('should allow editing builtin providers', async () => {
|
||||||
|
|||||||
@@ -143,10 +143,6 @@ export class ProviderService {
|
|||||||
throw new Error('Provider not found')
|
throw new Error('Provider not found')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (provider.isBuiltin) {
|
|
||||||
throw new Error('Cannot delete builtin provider')
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.prismaClient.provider.delete({
|
return this.prismaClient.provider.delete({
|
||||||
where: { id, teamId },
|
where: { id, teamId },
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,10 +1,22 @@
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
export { z }
|
export { z }
|
||||||
|
|
||||||
|
export const KNOWN_APIS = [
|
||||||
|
'openai-completions',
|
||||||
|
'mistral-conversations',
|
||||||
|
'openai-responses',
|
||||||
|
'azure-openai-responses',
|
||||||
|
'openai-codex-responses',
|
||||||
|
'anthropic-messages',
|
||||||
|
'bedrock-converse-stream',
|
||||||
|
'google-generative-ai',
|
||||||
|
'google-vertex',
|
||||||
|
] as const
|
||||||
|
|
||||||
// We define a serializable version of ProviderConfig for our API and DB
|
// We define a serializable version of ProviderConfig for our API and DB
|
||||||
// as the original ProviderConfig interface includes non-serializable fields (functions)
|
// as the original ProviderConfig interface includes non-serializable fields (functions)
|
||||||
export const providerModelConfigSchema = z.object({
|
export const providerModelConfigSchema = z.object({
|
||||||
api: z.string().optional(),
|
api: z.enum(KNOWN_APIS).optional(),
|
||||||
reasoning: z.boolean().default(false),
|
reasoning: z.boolean().default(false),
|
||||||
input: z.array(z.enum(['text', 'image'])).default(['text']),
|
input: z.array(z.enum(['text', 'image'])).default(['text']),
|
||||||
contextWindow: z.number().int().positive().default(128000),
|
contextWindow: z.number().int().positive().default(128000),
|
||||||
@@ -27,7 +39,7 @@ export const providerModelSchema = z.object({
|
|||||||
export const providerConfigSchema = z.object({
|
export const providerConfigSchema = z.object({
|
||||||
baseUrl: z.string().url('Must be a valid URL').optional().or(z.literal('')),
|
baseUrl: z.string().url('Must be a valid URL').optional().or(z.literal('')),
|
||||||
apiKey: z.string().optional().or(z.literal('')),
|
apiKey: z.string().optional().or(z.literal('')),
|
||||||
api: z.string().min(1, 'API Protocol is required'),
|
api: z.enum(KNOWN_APIS),
|
||||||
headers: z.record(z.string(), z.string()).optional(),
|
headers: z.record(z.string(), z.string()).optional(),
|
||||||
authHeader: z.boolean().optional(),
|
authHeader: z.boolean().optional(),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ import {
|
|||||||
SelectValue,
|
SelectValue,
|
||||||
} from '@/ui/components/ui/select'
|
} from '@/ui/components/ui/select'
|
||||||
import { Switch } from '@/ui/components/ui/switch'
|
import { Switch } from '@/ui/components/ui/switch'
|
||||||
import { providerConfigSchema, providerModelSchema } from '@shumai/dtos'
|
import { providerConfigSchema, providerModelSchema, KNOWN_APIS } from '@shumai/dtos'
|
||||||
import { useForm } from '@tanstack/react-form'
|
import { useForm } from '@tanstack/react-form'
|
||||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||||
import { InferResponseType } from 'hono/client'
|
import { InferResponseType } from 'hono/client'
|
||||||
@@ -60,6 +60,18 @@ import { useEffect, useMemo, useState } from 'react'
|
|||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
const API_PROTOCOL_LABELS: Record<(typeof KNOWN_APIS)[number], string> = {
|
||||||
|
'openai-completions': 'OpenAI Completions',
|
||||||
|
'mistral-conversations': 'Mistral Conversations',
|
||||||
|
'openai-responses': 'OpenAI Responses',
|
||||||
|
'azure-openai-responses': 'Azure OpenAI Responses',
|
||||||
|
'openai-codex-responses': 'OpenAI Codex Responses',
|
||||||
|
'anthropic-messages': 'Anthropic Messages',
|
||||||
|
'bedrock-converse-stream': 'AWS Bedrock',
|
||||||
|
'google-generative-ai': 'Google Generative AI',
|
||||||
|
'google-vertex': 'Google Vertex',
|
||||||
|
}
|
||||||
|
|
||||||
const providerFormSchemaBase = z.object({
|
const providerFormSchemaBase = z.object({
|
||||||
name: z.string().min(1, 'Provider name is required'),
|
name: z.string().min(1, 'Provider name is required'),
|
||||||
config: providerConfigSchema,
|
config: providerConfigSchema,
|
||||||
@@ -292,7 +304,6 @@ export function ProvidersSettings({ teamId }: ProvidersSettingsProps) {
|
|||||||
</DropdownMenuTrigger>
|
</DropdownMenuTrigger>
|
||||||
<DropdownMenuContent align="end" className="w-32">
|
<DropdownMenuContent align="end" className="w-32">
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
disabled={provider.isBuiltin}
|
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
setProviderToDelete(provider)
|
setProviderToDelete(provider)
|
||||||
@@ -490,8 +501,8 @@ function ProviderFormDialog({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={isOpen} onOpenChange={onClose}>
|
<Dialog open={isOpen} onOpenChange={onClose}>
|
||||||
<DialogContent className="sm:max-w-4xl max-h-[90vh] flex flex-col p-0 overflow-hidden">
|
<DialogContent className="sm:max-w-4xl h-[90vh] flex flex-col p-0 overflow-hidden">
|
||||||
<DialogHeader className="p-6 pb-2">
|
<DialogHeader className="p-6 pb-2 shrink-0">
|
||||||
<DialogTitle className="text-xl">{title}</DialogTitle>
|
<DialogTitle className="text-xl">{title}</DialogTitle>
|
||||||
<DialogDescription>
|
<DialogDescription>
|
||||||
Configure authentication and model details for your AI provider.
|
Configure authentication and model details for your AI provider.
|
||||||
@@ -504,373 +515,382 @@ function ProviderFormDialog({
|
|||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
form.handleSubmit()
|
form.handleSubmit()
|
||||||
}}
|
}}
|
||||||
className="flex flex-col flex-1 overflow-hidden"
|
className="flex-1 flex flex-col min-h-0 overflow-hidden"
|
||||||
>
|
>
|
||||||
<ScrollArea className="flex-1 min-h-0">
|
<div className="flex-1 min-h-0">
|
||||||
<div className="p-6 pt-2 space-y-8 pr-4">
|
<ScrollArea className="h-full">
|
||||||
{/* Row 1: Basic Config */}
|
<div className="p-6 pt-2 space-y-8 pr-6">
|
||||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 p-6 rounded-2xl border">
|
{/* Row 1: Basic Config */}
|
||||||
<form.Field
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 p-6 rounded-2xl border">
|
||||||
name="name"
|
|
||||||
children={(field) => {
|
|
||||||
const isInvalid = !!field.state.meta.errors.length && field.state.meta.isTouched
|
|
||||||
return (
|
|
||||||
<Field data-invalid={isInvalid}>
|
|
||||||
<FieldLabel htmlFor={field.name}>Provider Name</FieldLabel>
|
|
||||||
<Input
|
|
||||||
id={field.name}
|
|
||||||
name={field.name}
|
|
||||||
value={field.state.value}
|
|
||||||
onBlur={field.handleBlur}
|
|
||||||
onChange={(e) => field.handleChange(e.target.value)}
|
|
||||||
placeholder="e.g., My OpenAI"
|
|
||||||
disabled={isBuiltin}
|
|
||||||
aria-invalid={isInvalid}
|
|
||||||
/>
|
|
||||||
{isInvalid && <FieldError errors={mapErrors(field.state.meta.errors)} />}
|
|
||||||
</Field>
|
|
||||||
)
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<form.Field
|
|
||||||
name="config.api"
|
|
||||||
children={(field) => {
|
|
||||||
const isInvalid = !!field.state.meta.errors.length && field.state.meta.isTouched
|
|
||||||
return (
|
|
||||||
<Field data-invalid={isInvalid}>
|
|
||||||
<FieldLabel htmlFor={field.name}>Global API Protocol</FieldLabel>
|
|
||||||
<Select
|
|
||||||
onValueChange={field.handleChange}
|
|
||||||
value={field.state.value}
|
|
||||||
disabled={isBuiltin}
|
|
||||||
>
|
|
||||||
<SelectTrigger id={field.name} aria-invalid={isInvalid}>
|
|
||||||
<SelectValue placeholder="Select API Protocol" />
|
|
||||||
</SelectTrigger>
|
|
||||||
<SelectContent>
|
|
||||||
<SelectItem value="openai-completions">OpenAI Completions</SelectItem>
|
|
||||||
<SelectItem value="anthropic-messages">Anthropic Messages</SelectItem>
|
|
||||||
<SelectItem value="google-genai">Google GenAI</SelectItem>
|
|
||||||
<SelectItem value="bedrock-converse-stream">AWS Bedrock</SelectItem>
|
|
||||||
</SelectContent>
|
|
||||||
</Select>
|
|
||||||
{isInvalid && <FieldError errors={mapErrors(field.state.meta.errors)} />}
|
|
||||||
</Field>
|
|
||||||
)
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<div className="md:col-span-2">
|
|
||||||
<form.Field
|
<form.Field
|
||||||
name="config.baseUrl"
|
name="name"
|
||||||
children={(field) => {
|
children={(field) => {
|
||||||
const isInvalid =
|
const isInvalid =
|
||||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||||
return (
|
return (
|
||||||
<Field data-invalid={isInvalid}>
|
<Field data-invalid={isInvalid}>
|
||||||
<FieldLabel htmlFor={field.name}>Base URL (Optional)</FieldLabel>
|
<FieldLabel htmlFor={field.name}>Provider Name</FieldLabel>
|
||||||
<Input
|
<Input
|
||||||
id={field.name}
|
id={field.name}
|
||||||
name={field.name}
|
name={field.name}
|
||||||
value={field.state.value || ''}
|
value={field.state.value}
|
||||||
onBlur={field.handleBlur}
|
onBlur={field.handleBlur}
|
||||||
onChange={(e) => field.handleChange(e.target.value)}
|
onChange={(e) => field.handleChange(e.target.value)}
|
||||||
placeholder="https://api.openai.com/v1"
|
placeholder="e.g., My OpenAI"
|
||||||
|
disabled={isBuiltin}
|
||||||
aria-invalid={isInvalid}
|
aria-invalid={isInvalid}
|
||||||
/>
|
/>
|
||||||
<FieldDescription>Override the default endpoint.</FieldDescription>
|
|
||||||
{isInvalid && <FieldError errors={mapErrors(field.state.meta.errors)} />}
|
{isInvalid && <FieldError errors={mapErrors(field.state.meta.errors)} />}
|
||||||
</Field>
|
</Field>
|
||||||
)
|
)
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Row 2: API Key Card */}
|
|
||||||
<Card className="rounded-2xl border bg-transparent">
|
|
||||||
<CardContent className="p-6 pt-2">
|
|
||||||
<form.Field
|
<form.Field
|
||||||
name="config.apiKey"
|
name="config.api"
|
||||||
children={(field) => {
|
children={(field) => {
|
||||||
const isInvalid =
|
const isInvalid =
|
||||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||||
return (
|
return (
|
||||||
<Field data-invalid={isInvalid}>
|
<Field data-invalid={isInvalid}>
|
||||||
<FieldLabel htmlFor={field.name} className="text-base font-semibold">
|
<FieldLabel htmlFor={field.name}>Global API Protocol</FieldLabel>
|
||||||
API Key
|
<Select
|
||||||
</FieldLabel>
|
onValueChange={field.handleChange as (value: string) => void}
|
||||||
<Input
|
value={field.state.value}
|
||||||
id={field.name}
|
disabled={isBuiltin}
|
||||||
name={field.name}
|
|
||||||
type="text"
|
|
||||||
value={field.state.value || ''}
|
|
||||||
onBlur={field.handleBlur}
|
|
||||||
onChange={(e) => field.handleChange(e.target.value)}
|
|
||||||
placeholder="Enter API Key or Environment Variable"
|
|
||||||
aria-invalid={isInvalid}
|
|
||||||
/>
|
|
||||||
<FieldDescription className="text-xs mt-2">
|
|
||||||
You can provide a literal value (e.g.{' '}
|
|
||||||
<code className="bg-muted px-1 rounded text-primary">sk-...</code>) or
|
|
||||||
an Environment variable name (e.g.{' '}
|
|
||||||
<code className="bg-muted px-1 rounded text-primary">MY_API_KEY</code>
|
|
||||||
).
|
|
||||||
</FieldDescription>
|
|
||||||
{isInvalid && <FieldError errors={mapErrors(field.state.meta.errors)} />}
|
|
||||||
</Field>
|
|
||||||
)
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
{/* Models Section */}
|
|
||||||
<div className="space-y-4">
|
|
||||||
<form.Field
|
|
||||||
name="models"
|
|
||||||
mode="array"
|
|
||||||
children={(modelsField) => (
|
|
||||||
<div className="space-y-4">
|
|
||||||
<div className="flex items-center justify-between">
|
|
||||||
<Label className="text-lg font-bold">Models Configuration</Label>
|
|
||||||
<Button
|
|
||||||
type="button"
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
onClick={() =>
|
|
||||||
modelsField.pushValue({
|
|
||||||
modelId: '',
|
|
||||||
name: '',
|
|
||||||
config: {
|
|
||||||
api: form.getFieldValue('config.api'),
|
|
||||||
reasoning: false,
|
|
||||||
input: ['text'],
|
|
||||||
contextWindow: 128000,
|
|
||||||
maxTokens: 4096,
|
|
||||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
className="gap-2"
|
|
||||||
>
|
|
||||||
<Plus className="w-4 h-4" />
|
|
||||||
Add Model
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="space-y-6">
|
|
||||||
{modelsField.state.value.map((_, index) => (
|
|
||||||
<div
|
|
||||||
key={index}
|
|
||||||
className="group relative p-6 bg-transparent rounded-2xl border shadow-sm animate-in fade-in zoom-in-95 duration-300"
|
|
||||||
>
|
>
|
||||||
{/* Row 1: ID and Name */}
|
<SelectTrigger id={field.name} aria-invalid={isInvalid}>
|
||||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
<SelectValue placeholder="Select API Protocol" />
|
||||||
<form.Field
|
</SelectTrigger>
|
||||||
name={`models[${index}].modelId`}
|
<SelectContent>
|
||||||
children={(field) => {
|
{KNOWN_APIS.map((api) => (
|
||||||
const isInvalid =
|
<SelectItem key={api} value={api}>
|
||||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
{API_PROTOCOL_LABELS[api]}
|
||||||
return (
|
</SelectItem>
|
||||||
<Field data-invalid={isInvalid}>
|
))}
|
||||||
<FieldLabel htmlFor={field.name}>Model ID</FieldLabel>
|
</SelectContent>
|
||||||
<Input
|
</Select>
|
||||||
id={field.name}
|
{isInvalid && <FieldError errors={mapErrors(field.state.meta.errors)} />}
|
||||||
name={field.name}
|
</Field>
|
||||||
value={field.state.value}
|
)
|
||||||
onBlur={field.handleBlur}
|
}}
|
||||||
onChange={(e) => field.handleChange(e.target.value)}
|
/>
|
||||||
placeholder="gpt-4o"
|
|
||||||
aria-invalid={isInvalid}
|
<div className="md:col-span-2">
|
||||||
/>
|
<form.Field
|
||||||
{isInvalid && (
|
name="config.baseUrl"
|
||||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
children={(field) => {
|
||||||
)}
|
const isInvalid =
|
||||||
</Field>
|
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||||
)
|
return (
|
||||||
}}
|
<Field data-invalid={isInvalid}>
|
||||||
/>
|
<FieldLabel htmlFor={field.name}>Base URL (Optional)</FieldLabel>
|
||||||
<form.Field
|
<Input
|
||||||
name={`models[${index}].name`}
|
id={field.name}
|
||||||
children={(field) => {
|
name={field.name}
|
||||||
const isInvalid =
|
value={field.state.value || ''}
|
||||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
onBlur={field.handleBlur}
|
||||||
return (
|
onChange={(e) => field.handleChange(e.target.value)}
|
||||||
<Field data-invalid={isInvalid}>
|
placeholder="https://api.openai.com/v1"
|
||||||
|
aria-invalid={isInvalid}
|
||||||
|
/>
|
||||||
|
<FieldDescription>Override the default endpoint.</FieldDescription>
|
||||||
|
{isInvalid && (
|
||||||
|
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||||
|
)}
|
||||||
|
</Field>
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Row 2: API Key Card */}
|
||||||
|
<Card className="rounded-2xl border bg-transparent">
|
||||||
|
<CardContent className="p-6 pt-2">
|
||||||
|
<form.Field
|
||||||
|
name="config.apiKey"
|
||||||
|
children={(field) => {
|
||||||
|
const isInvalid =
|
||||||
|
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||||
|
return (
|
||||||
|
<Field data-invalid={isInvalid}>
|
||||||
|
<FieldLabel htmlFor={field.name} className="text-base font-semibold">
|
||||||
|
API Key
|
||||||
|
</FieldLabel>
|
||||||
|
<Input
|
||||||
|
id={field.name}
|
||||||
|
name={field.name}
|
||||||
|
type="text"
|
||||||
|
value={field.state.value || ''}
|
||||||
|
onBlur={field.handleBlur}
|
||||||
|
onChange={(e) => field.handleChange(e.target.value)}
|
||||||
|
placeholder="Enter API Key or Environment Variable"
|
||||||
|
aria-invalid={isInvalid}
|
||||||
|
/>
|
||||||
|
<FieldDescription className="text-xs mt-2">
|
||||||
|
You can provide a literal value (e.g.{' '}
|
||||||
|
<code className="bg-muted px-1 rounded text-primary">sk-...</code>) or
|
||||||
|
an Environment variable name (e.g.{' '}
|
||||||
|
<code className="bg-muted px-1 rounded text-primary">MY_API_KEY</code>
|
||||||
|
).
|
||||||
|
</FieldDescription>
|
||||||
|
{isInvalid && (
|
||||||
|
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||||
|
)}
|
||||||
|
</Field>
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
{/* Models Section */}
|
||||||
|
<div className="space-y-4">
|
||||||
|
<form.Field
|
||||||
|
name="models"
|
||||||
|
mode="array"
|
||||||
|
children={(modelsField) => (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<Label className="text-lg font-bold">Models Configuration</Label>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={() =>
|
||||||
|
modelsField.pushValue({
|
||||||
|
modelId: '',
|
||||||
|
name: '',
|
||||||
|
config: {
|
||||||
|
api: form.getFieldValue('config.api'),
|
||||||
|
reasoning: false,
|
||||||
|
input: ['text'],
|
||||||
|
contextWindow: 128000,
|
||||||
|
maxTokens: 4096,
|
||||||
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
className="gap-2"
|
||||||
|
>
|
||||||
|
<Plus className="w-4 h-4" />
|
||||||
|
Add Model
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-6">
|
||||||
|
{modelsField.state.value.map((_, index) => (
|
||||||
|
<div
|
||||||
|
key={index}
|
||||||
|
className="group relative p-6 bg-transparent rounded-2xl border shadow-sm animate-in fade-in zoom-in-95 duration-300"
|
||||||
|
>
|
||||||
|
{/* Row 1: ID and Name */}
|
||||||
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||||
|
<form.Field
|
||||||
|
name={`models[${index}].modelId`}
|
||||||
|
children={(field) => {
|
||||||
|
const isInvalid =
|
||||||
|
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||||
|
return (
|
||||||
|
<Field data-invalid={isInvalid}>
|
||||||
|
<FieldLabel htmlFor={field.name}>Model ID</FieldLabel>
|
||||||
|
<Input
|
||||||
|
id={field.name}
|
||||||
|
name={field.name}
|
||||||
|
value={field.state.value}
|
||||||
|
onBlur={field.handleBlur}
|
||||||
|
onChange={(e) => field.handleChange(e.target.value)}
|
||||||
|
placeholder="gpt-4o"
|
||||||
|
aria-invalid={isInvalid}
|
||||||
|
/>
|
||||||
|
{isInvalid && (
|
||||||
|
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||||
|
)}
|
||||||
|
</Field>
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<form.Field
|
||||||
|
name={`models[${index}].name`}
|
||||||
|
children={(field) => {
|
||||||
|
const isInvalid =
|
||||||
|
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||||
|
return (
|
||||||
|
<Field data-invalid={isInvalid}>
|
||||||
|
<FieldLabel htmlFor={field.name}>
|
||||||
|
Display Name (Optional)
|
||||||
|
</FieldLabel>
|
||||||
|
<Input
|
||||||
|
id={field.name}
|
||||||
|
name={field.name}
|
||||||
|
value={field.state.value}
|
||||||
|
onBlur={field.handleBlur}
|
||||||
|
onChange={(e) => field.handleChange(e.target.value)}
|
||||||
|
placeholder="e.g., GPT-4o"
|
||||||
|
aria-invalid={isInvalid}
|
||||||
|
disabled={isBuiltin}
|
||||||
|
/>
|
||||||
|
{isInvalid && (
|
||||||
|
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||||
|
)}
|
||||||
|
</Field>
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Row 2: Reasoning and Context Window */}
|
||||||
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
|
||||||
|
<form.Field
|
||||||
|
name={`models[${index}].config.reasoning`}
|
||||||
|
children={(field) => (
|
||||||
|
<Field
|
||||||
|
orientation="horizontal"
|
||||||
|
className="rounded-lg border border-border p-3 shadow-sm bg-muted/50 justify-between"
|
||||||
|
>
|
||||||
<FieldLabel htmlFor={field.name}>
|
<FieldLabel htmlFor={field.name}>
|
||||||
Display Name (Optional)
|
Reasoning Support
|
||||||
</FieldLabel>
|
</FieldLabel>
|
||||||
<Input
|
<Switch
|
||||||
id={field.name}
|
id={field.name}
|
||||||
name={field.name}
|
checked={field.state.value}
|
||||||
value={field.state.value}
|
onCheckedChange={field.handleChange}
|
||||||
onBlur={field.handleBlur}
|
|
||||||
onChange={(e) => field.handleChange(e.target.value)}
|
|
||||||
placeholder="e.g., GPT-4o"
|
|
||||||
aria-invalid={isInvalid}
|
|
||||||
disabled={isBuiltin}
|
disabled={isBuiltin}
|
||||||
/>
|
/>
|
||||||
{isInvalid && (
|
|
||||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
|
||||||
)}
|
|
||||||
</Field>
|
</Field>
|
||||||
)
|
)}
|
||||||
}}
|
/>
|
||||||
/>
|
<form.Field
|
||||||
</div>
|
name={`models[${index}].config.contextWindow`}
|
||||||
|
children={(field) => {
|
||||||
|
const isInvalid =
|
||||||
|
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||||
|
return (
|
||||||
|
<Field data-invalid={isInvalid}>
|
||||||
|
<FieldLabel htmlFor={field.name} className="text-xs">
|
||||||
|
Context Window (tokens)
|
||||||
|
</FieldLabel>
|
||||||
|
<Input
|
||||||
|
id={field.name}
|
||||||
|
name={field.name}
|
||||||
|
type="number"
|
||||||
|
value={field.state.value}
|
||||||
|
onBlur={field.handleBlur}
|
||||||
|
onChange={(e) =>
|
||||||
|
field.handleChange(parseInt(e.target.value) || 0)
|
||||||
|
}
|
||||||
|
aria-invalid={isInvalid}
|
||||||
|
disabled={isBuiltin}
|
||||||
|
/>
|
||||||
|
{isInvalid && (
|
||||||
|
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||||
|
)}
|
||||||
|
</Field>
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
{/* Row 2: Reasoning and Context Window */}
|
{/* Row 3: Constraints and Cost */}
|
||||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
|
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 mt-6">
|
||||||
<form.Field
|
<form.Field
|
||||||
name={`models[${index}].config.reasoning`}
|
name={`models[${index}].config.maxTokens`}
|
||||||
children={(field) => (
|
children={(field) => {
|
||||||
<Field
|
const isInvalid =
|
||||||
orientation="horizontal"
|
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||||
className="rounded-lg border border-border p-3 shadow-sm bg-muted/50 justify-between"
|
return (
|
||||||
>
|
<Field data-invalid={isInvalid}>
|
||||||
<FieldLabel htmlFor={field.name}>Reasoning Support</FieldLabel>
|
<FieldLabel htmlFor={field.name} className="text-xs">
|
||||||
<Switch
|
Max Output Tokens
|
||||||
id={field.name}
|
</FieldLabel>
|
||||||
checked={field.state.value}
|
<Input
|
||||||
onCheckedChange={field.handleChange}
|
id={field.name}
|
||||||
disabled={isBuiltin}
|
name={field.name}
|
||||||
/>
|
type="number"
|
||||||
</Field>
|
value={field.state.value}
|
||||||
)}
|
onBlur={field.handleBlur}
|
||||||
/>
|
onChange={(e) =>
|
||||||
<form.Field
|
field.handleChange(parseInt(e.target.value) || 0)
|
||||||
name={`models[${index}].config.contextWindow`}
|
}
|
||||||
children={(field) => {
|
aria-invalid={isInvalid}
|
||||||
const isInvalid =
|
disabled={isBuiltin}
|
||||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
/>
|
||||||
return (
|
{isInvalid && (
|
||||||
<Field data-invalid={isInvalid}>
|
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||||
<FieldLabel htmlFor={field.name} className="text-xs">
|
)}
|
||||||
Context Window (tokens)
|
</Field>
|
||||||
</FieldLabel>
|
)
|
||||||
<Input
|
}}
|
||||||
id={field.name}
|
/>
|
||||||
name={field.name}
|
<form.Field
|
||||||
type="number"
|
name={`models[${index}].config.cost.input`}
|
||||||
value={field.state.value}
|
children={(field) => {
|
||||||
onBlur={field.handleBlur}
|
const isInvalid =
|
||||||
onChange={(e) =>
|
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||||
field.handleChange(parseInt(e.target.value) || 0)
|
return (
|
||||||
}
|
<Field data-invalid={isInvalid}>
|
||||||
aria-invalid={isInvalid}
|
<FieldLabel
|
||||||
disabled={isBuiltin}
|
htmlFor={field.name}
|
||||||
/>
|
className="text-xs flex items-center gap-1"
|
||||||
{isInvalid && (
|
>
|
||||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
<DollarSign className="w-3 h-3" /> Input Cost (1M)
|
||||||
)}
|
</FieldLabel>
|
||||||
</Field>
|
<Input
|
||||||
)
|
id={field.name}
|
||||||
}}
|
name={field.name}
|
||||||
/>
|
type="number"
|
||||||
</div>
|
step="0.01"
|
||||||
|
value={field.state.value}
|
||||||
|
onBlur={field.handleBlur}
|
||||||
|
onChange={(e) =>
|
||||||
|
field.handleChange(parseFloat(e.target.value) || 0)
|
||||||
|
}
|
||||||
|
aria-invalid={isInvalid}
|
||||||
|
disabled={isBuiltin}
|
||||||
|
/>
|
||||||
|
{isInvalid && (
|
||||||
|
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||||
|
)}
|
||||||
|
</Field>
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<form.Field
|
||||||
|
name={`models[${index}].config.cost.output`}
|
||||||
|
children={(field) => {
|
||||||
|
const isInvalid =
|
||||||
|
!!field.state.meta.errors.length && field.state.meta.isTouched
|
||||||
|
return (
|
||||||
|
<Field data-invalid={isInvalid}>
|
||||||
|
<FieldLabel
|
||||||
|
htmlFor={field.name}
|
||||||
|
className="text-xs flex items-center gap-1"
|
||||||
|
>
|
||||||
|
<DollarSign className="w-3 h-3" /> Output Cost (1M)
|
||||||
|
</FieldLabel>
|
||||||
|
<Input
|
||||||
|
id={field.name}
|
||||||
|
name={field.name}
|
||||||
|
type="number"
|
||||||
|
step="0.01"
|
||||||
|
value={field.state.value}
|
||||||
|
onBlur={field.handleBlur}
|
||||||
|
onChange={(e) =>
|
||||||
|
field.handleChange(parseFloat(e.target.value) || 0)
|
||||||
|
}
|
||||||
|
aria-invalid={isInvalid}
|
||||||
|
disabled={isBuiltin}
|
||||||
|
/>
|
||||||
|
{isInvalid && (
|
||||||
|
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
||||||
|
)}
|
||||||
|
</Field>
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
{/* Row 3: Constraints and Cost */}
|
|
||||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 mt-6">
|
|
||||||
<form.Field
|
|
||||||
name={`models[${index}].config.maxTokens`}
|
|
||||||
children={(field) => {
|
|
||||||
const isInvalid =
|
|
||||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
|
||||||
return (
|
|
||||||
<Field data-invalid={isInvalid}>
|
|
||||||
<FieldLabel htmlFor={field.name} className="text-xs">
|
|
||||||
Max Output Tokens
|
|
||||||
</FieldLabel>
|
|
||||||
<Input
|
|
||||||
id={field.name}
|
|
||||||
name={field.name}
|
|
||||||
type="number"
|
|
||||||
value={field.state.value}
|
|
||||||
onBlur={field.handleBlur}
|
|
||||||
onChange={(e) =>
|
|
||||||
field.handleChange(parseInt(e.target.value) || 0)
|
|
||||||
}
|
|
||||||
aria-invalid={isInvalid}
|
|
||||||
disabled={isBuiltin}
|
|
||||||
/>
|
|
||||||
{isInvalid && (
|
|
||||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
|
||||||
)}
|
|
||||||
</Field>
|
|
||||||
)
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<form.Field
|
|
||||||
name={`models[${index}].config.cost.input`}
|
|
||||||
children={(field) => {
|
|
||||||
const isInvalid =
|
|
||||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
|
||||||
return (
|
|
||||||
<Field data-invalid={isInvalid}>
|
|
||||||
<FieldLabel
|
|
||||||
htmlFor={field.name}
|
|
||||||
className="text-xs flex items-center gap-1"
|
|
||||||
>
|
|
||||||
<DollarSign className="w-3 h-3" /> Input Cost (1M)
|
|
||||||
</FieldLabel>
|
|
||||||
<Input
|
|
||||||
id={field.name}
|
|
||||||
name={field.name}
|
|
||||||
type="number"
|
|
||||||
step="0.01"
|
|
||||||
value={field.state.value}
|
|
||||||
onBlur={field.handleBlur}
|
|
||||||
onChange={(e) =>
|
|
||||||
field.handleChange(parseFloat(e.target.value) || 0)
|
|
||||||
}
|
|
||||||
aria-invalid={isInvalid}
|
|
||||||
disabled={isBuiltin}
|
|
||||||
/>
|
|
||||||
{isInvalid && (
|
|
||||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
|
||||||
)}
|
|
||||||
</Field>
|
|
||||||
)
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<form.Field
|
|
||||||
name={`models[${index}].config.cost.output`}
|
|
||||||
children={(field) => {
|
|
||||||
const isInvalid =
|
|
||||||
!!field.state.meta.errors.length && field.state.meta.isTouched
|
|
||||||
return (
|
|
||||||
<Field data-invalid={isInvalid}>
|
|
||||||
<FieldLabel
|
|
||||||
htmlFor={field.name}
|
|
||||||
className="text-xs flex items-center gap-1"
|
|
||||||
>
|
|
||||||
<DollarSign className="w-3 h-3" /> Output Cost (1M)
|
|
||||||
</FieldLabel>
|
|
||||||
<Input
|
|
||||||
id={field.name}
|
|
||||||
name={field.name}
|
|
||||||
type="number"
|
|
||||||
step="0.01"
|
|
||||||
value={field.state.value}
|
|
||||||
onBlur={field.handleBlur}
|
|
||||||
onChange={(e) =>
|
|
||||||
field.handleChange(parseFloat(e.target.value) || 0)
|
|
||||||
}
|
|
||||||
aria-invalid={isInvalid}
|
|
||||||
disabled={isBuiltin}
|
|
||||||
/>
|
|
||||||
{isInvalid && (
|
|
||||||
<FieldError errors={mapErrors(field.state.meta.errors)} />
|
|
||||||
)}
|
|
||||||
</Field>
|
|
||||||
)
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{!isBuiltin && (
|
|
||||||
<Button
|
<Button
|
||||||
type="button"
|
type="button"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
@@ -880,18 +900,18 @@ function ProviderFormDialog({
|
|||||||
>
|
>
|
||||||
<Trash2 className="h-4 w-4" />
|
<Trash2 className="h-4 w-4" />
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
</div>
|
||||||
</div>
|
))}
|
||||||
))}
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
)}
|
||||||
)}
|
/>
|
||||||
/>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</ScrollArea>
|
||||||
</ScrollArea>
|
</div>
|
||||||
|
|
||||||
<DialogFooter className="p-6 pt-4 border-t border-border gap-2">
|
<DialogFooter className="p-6 pt-4 border-t border-border gap-2 shrink-0">
|
||||||
<Button type="button" variant="outline" onClick={onClose} disabled={isLoading}>
|
<Button type="button" variant="outline" onClick={onClose} disabled={isLoading}>
|
||||||
Cancel
|
Cancel
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Reference in New Issue
Block a user