diff --git a/packages/agent/src/activities/agent.test.ts b/packages/agent/src/activities/agent.test.ts index 3706837f..d10f4f07 100644 --- a/packages/agent/src/activities/agent.test.ts +++ b/packages/agent/src/activities/agent.test.ts @@ -306,7 +306,7 @@ describe('Agent Database Activities Integration', () => { teamId: team.id, name: 'openai', config: { - api: 'openai', + api: 'openai-completions', apiKey: 'mock-key', }, }, diff --git a/packages/core/src/authz/authz.test.ts b/packages/core/src/authz/authz.test.ts index 7a706f76..7a4ccecd 100644 --- a/packages/core/src/authz/authz.test.ts +++ b/packages/core/src/authz/authz.test.ts @@ -315,7 +315,7 @@ describe('AuthzService', () => { // Provider 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 diff --git a/packages/core/src/provider/provider.test.ts b/packages/core/src/provider/provider.test.ts index 5ae3b053..453fbce1 100644 --- a/packages/core/src/provider/provider.test.ts +++ b/packages/core/src/provider/provider.test.ts @@ -38,7 +38,7 @@ describe('ProviderService', () => { 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() await providerService.initBuiltinProviders(team.id) @@ -46,9 +46,9 @@ describe('ProviderService', () => { const builtin = providers.find((p) => p.isBuiltin) expect(builtin).toBeDefined() - await expect(providerService.delete(team.id, builtin!.id)).rejects.toThrow( - 'Cannot delete builtin provider', - ) + await providerService.delete(team.id, builtin!.id) + const afterDelete = await providerService.listByTeam(team.id) + expect(afterDelete.find((p) => p.id === builtin!.id)).toBeUndefined() }) it('should allow editing builtin providers', async () => { diff --git a/packages/core/src/provider/provider.ts b/packages/core/src/provider/provider.ts index 3c1c255f..f072f567 100644 --- a/packages/core/src/provider/provider.ts +++ b/packages/core/src/provider/provider.ts @@ -143,10 +143,6 @@ export class ProviderService { throw new Error('Provider not found') } - if (provider.isBuiltin) { - throw new Error('Cannot delete builtin provider') - } - return this.prismaClient.provider.delete({ where: { id, teamId }, }) diff --git a/packages/dtos/src/provider.ts b/packages/dtos/src/provider.ts index 38b1119f..5469f3c7 100644 --- a/packages/dtos/src/provider.ts +++ b/packages/dtos/src/provider.ts @@ -1,10 +1,22 @@ import { z } from 'zod' 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 // as the original ProviderConfig interface includes non-serializable fields (functions) export const providerModelConfigSchema = z.object({ - api: z.string().optional(), + api: z.enum(KNOWN_APIS).optional(), reasoning: z.boolean().default(false), input: z.array(z.enum(['text', 'image'])).default(['text']), contextWindow: z.number().int().positive().default(128000), @@ -27,7 +39,7 @@ export const providerModelSchema = z.object({ export const providerConfigSchema = z.object({ baseUrl: z.string().url('Must be a valid URL').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(), authHeader: z.boolean().optional(), }) diff --git a/packages/webui/components/settings/ProvidersSettings.tsx b/packages/webui/components/settings/ProvidersSettings.tsx index 39e28536..2a254d4e 100644 --- a/packages/webui/components/settings/ProvidersSettings.tsx +++ b/packages/webui/components/settings/ProvidersSettings.tsx @@ -38,7 +38,7 @@ import { SelectValue, } from '@/ui/components/ui/select' 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 { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { InferResponseType } from 'hono/client' @@ -60,6 +60,18 @@ import { useEffect, useMemo, useState } from 'react' import { toast } from 'sonner' 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({ name: z.string().min(1, 'Provider name is required'), config: providerConfigSchema, @@ -292,7 +304,6 @@ export function ProvidersSettings({ teamId }: ProvidersSettingsProps) { { e.stopPropagation() setProviderToDelete(provider) @@ -490,8 +501,8 @@ function ProviderFormDialog({ return ( - - + + {title} Configure authentication and model details for your AI provider. @@ -504,373 +515,382 @@ function ProviderFormDialog({ e.stopPropagation() form.handleSubmit() }} - className="flex flex-col flex-1 overflow-hidden" + className="flex-1 flex flex-col min-h-0 overflow-hidden" > - -
- {/* Row 1: Basic Config */} -
- { - const isInvalid = !!field.state.meta.errors.length && field.state.meta.isTouched - return ( - - Provider Name - field.handleChange(e.target.value)} - placeholder="e.g., My OpenAI" - disabled={isBuiltin} - aria-invalid={isInvalid} - /> - {isInvalid && } - - ) - }} - /> - - { - const isInvalid = !!field.state.meta.errors.length && field.state.meta.isTouched - return ( - - Global API Protocol - - {isInvalid && } - - ) - }} - /> - -
+
+ +
+ {/* Row 1: Basic Config */} +
{ const isInvalid = !!field.state.meta.errors.length && field.state.meta.isTouched return ( - Base URL (Optional) + Provider Name field.handleChange(e.target.value)} - placeholder="https://api.openai.com/v1" + placeholder="e.g., My OpenAI" + disabled={isBuiltin} aria-invalid={isInvalid} /> - Override the default endpoint. {isInvalid && } ) }} /> -
-
- {/* Row 2: API Key Card */} - - { const isInvalid = !!field.state.meta.errors.length && field.state.meta.isTouched return ( - - API Key - - field.handleChange(e.target.value)} - placeholder="Enter API Key or Environment Variable" - aria-invalid={isInvalid} - /> - - You can provide a literal value (e.g.{' '} - sk-...) or - an Environment variable name (e.g.{' '} - MY_API_KEY - ). - - {isInvalid && } - - ) - }} - /> - - - - {/* Models Section */} -
- ( -
-
- - -
- -
- {modelsField.state.value.map((_, index) => ( -
Global API Protocol + field.handleChange(e.target.value)} - placeholder="gpt-4o" - aria-invalid={isInvalid} - /> - {isInvalid && ( - - )} - - ) - }} - /> - { - const isInvalid = - !!field.state.meta.errors.length && field.state.meta.isTouched - return ( - + + + + + {KNOWN_APIS.map((api) => ( + + {API_PROTOCOL_LABELS[api]} + + ))} + + + {isInvalid && } + + ) + }} + /> + +
+ { + const isInvalid = + !!field.state.meta.errors.length && field.state.meta.isTouched + return ( + + Base URL (Optional) + field.handleChange(e.target.value)} + placeholder="https://api.openai.com/v1" + aria-invalid={isInvalid} + /> + Override the default endpoint. + {isInvalid && ( + + )} + + ) + }} + /> +
+
+ + {/* Row 2: API Key Card */} + + + { + const isInvalid = + !!field.state.meta.errors.length && field.state.meta.isTouched + return ( + + + API Key + + field.handleChange(e.target.value)} + placeholder="Enter API Key or Environment Variable" + aria-invalid={isInvalid} + /> + + You can provide a literal value (e.g.{' '} + sk-...) or + an Environment variable name (e.g.{' '} + MY_API_KEY + ). + + {isInvalid && ( + + )} + + ) + }} + /> + + + + {/* Models Section */} +
+ ( +
+
+ + +
+ +
+ {modelsField.state.value.map((_, index) => ( +
+ {/* Row 1: ID and Name */} +
+ { + const isInvalid = + !!field.state.meta.errors.length && field.state.meta.isTouched + return ( + + Model ID + field.handleChange(e.target.value)} + placeholder="gpt-4o" + aria-invalid={isInvalid} + /> + {isInvalid && ( + + )} + + ) + }} + /> + { + const isInvalid = + !!field.state.meta.errors.length && field.state.meta.isTouched + return ( + + + Display Name (Optional) + + field.handleChange(e.target.value)} + placeholder="e.g., GPT-4o" + aria-invalid={isInvalid} + disabled={isBuiltin} + /> + {isInvalid && ( + + )} + + ) + }} + /> +
+ + {/* Row 2: Reasoning and Context Window */} +
+ ( + - Display Name (Optional) + Reasoning Support - field.handleChange(e.target.value)} - placeholder="e.g., GPT-4o" - aria-invalid={isInvalid} + checked={field.state.value} + onCheckedChange={field.handleChange} disabled={isBuiltin} /> - {isInvalid && ( - - )} - ) - }} - /> -
+ )} + /> + { + const isInvalid = + !!field.state.meta.errors.length && field.state.meta.isTouched + return ( + + + Context Window (tokens) + + + field.handleChange(parseInt(e.target.value) || 0) + } + aria-invalid={isInvalid} + disabled={isBuiltin} + /> + {isInvalid && ( + + )} + + ) + }} + /> +
- {/* Row 2: Reasoning and Context Window */} -
- ( - - Reasoning Support - - - )} - /> - { - const isInvalid = - !!field.state.meta.errors.length && field.state.meta.isTouched - return ( - - - Context Window (tokens) - - - field.handleChange(parseInt(e.target.value) || 0) - } - aria-invalid={isInvalid} - disabled={isBuiltin} - /> - {isInvalid && ( - - )} - - ) - }} - /> -
+ {/* Row 3: Constraints and Cost */} +
+ { + const isInvalid = + !!field.state.meta.errors.length && field.state.meta.isTouched + return ( + + + Max Output Tokens + + + field.handleChange(parseInt(e.target.value) || 0) + } + aria-invalid={isInvalid} + disabled={isBuiltin} + /> + {isInvalid && ( + + )} + + ) + }} + /> + { + const isInvalid = + !!field.state.meta.errors.length && field.state.meta.isTouched + return ( + + + Input Cost (1M) + + + field.handleChange(parseFloat(e.target.value) || 0) + } + aria-invalid={isInvalid} + disabled={isBuiltin} + /> + {isInvalid && ( + + )} + + ) + }} + /> + { + const isInvalid = + !!field.state.meta.errors.length && field.state.meta.isTouched + return ( + + + Output Cost (1M) + + + field.handleChange(parseFloat(e.target.value) || 0) + } + aria-invalid={isInvalid} + disabled={isBuiltin} + /> + {isInvalid && ( + + )} + + ) + }} + /> +
- {/* Row 3: Constraints and Cost */} -
- { - const isInvalid = - !!field.state.meta.errors.length && field.state.meta.isTouched - return ( - - - Max Output Tokens - - - field.handleChange(parseInt(e.target.value) || 0) - } - aria-invalid={isInvalid} - disabled={isBuiltin} - /> - {isInvalid && ( - - )} - - ) - }} - /> - { - const isInvalid = - !!field.state.meta.errors.length && field.state.meta.isTouched - return ( - - - Input Cost (1M) - - - field.handleChange(parseFloat(e.target.value) || 0) - } - aria-invalid={isInvalid} - disabled={isBuiltin} - /> - {isInvalid && ( - - )} - - ) - }} - /> - { - const isInvalid = - !!field.state.meta.errors.length && field.state.meta.isTouched - return ( - - - Output Cost (1M) - - - field.handleChange(parseFloat(e.target.value) || 0) - } - aria-invalid={isInvalid} - disabled={isBuiltin} - /> - {isInvalid && ( - - )} - - ) - }} - /> -
- - {!isBuiltin && ( - )} -
- ))} +
+ ))} +
-
- )} - /> + )} + /> +
-
- + +
- +