mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-28 20:04:26 +00:00
feat(agent): add built-in image and video generation tools and settings (#382)
* feat(agent): add built-in image and video generation tools and settings * fix(settings): refine media generation layout and expose media tools in agent and quota forms * feat(media-generation): add alibaba, bytedance, minimax, prodia, and azure providers * feat(agent): support s3 key references for media generation and s3KeyOnly in read_asset
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, render, screen } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { AgentFormDialog } from './AgentFormDialog'
|
||||
|
||||
vi.mock('@/ui/api/client', () => ({
|
||||
client: {
|
||||
api: {
|
||||
teams: {
|
||||
':teamId': {
|
||||
providers: {
|
||||
$get: vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => [],
|
||||
}),
|
||||
},
|
||||
skills: {
|
||||
$get: vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ skills: [] }),
|
||||
}),
|
||||
},
|
||||
mcp: {
|
||||
servers: {
|
||||
$get: vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => [],
|
||||
}),
|
||||
},
|
||||
},
|
||||
agents: {
|
||||
$post: vi.fn(),
|
||||
':agentId': {
|
||||
$put: vi.fn(),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/ui/hooks/use-permissions', () => ({
|
||||
usePermissions: () => ({
|
||||
canAdmin: true,
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('sonner', () => ({
|
||||
toast: {
|
||||
success: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
describe('AgentFormDialog', () => {
|
||||
let queryClient: QueryClient
|
||||
|
||||
beforeEach(() => {
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
mutations: { retry: false },
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
it('renders all builtin tools including generate_image and generate_video', () => {
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<AgentFormDialog
|
||||
isOpen={true}
|
||||
onClose={vi.fn()}
|
||||
teamId="team-123"
|
||||
type="chat"
|
||||
title="Create Agent"
|
||||
/>
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
expect(screen.getByText('Generate Image')).toBeDefined()
|
||||
expect(screen.getByText('Generate Video')).toBeDefined()
|
||||
expect(screen.getByText('Sandboxed Terminal')).toBeDefined()
|
||||
expect(screen.getByText('Read Asset')).toBeDefined()
|
||||
})
|
||||
})
|
||||
@@ -95,6 +95,16 @@ const BUILTIN_TOOLS = [
|
||||
name: () => m.agent_tool_create_version_name(),
|
||||
description: () => m.agent_tool_create_version_desc(),
|
||||
},
|
||||
{
|
||||
id: 'generate_image',
|
||||
name: () => m.agent_tool_generate_image_name(),
|
||||
description: () => m.agent_tool_generate_image_desc(),
|
||||
},
|
||||
{
|
||||
id: 'generate_video',
|
||||
name: () => m.agent_tool_generate_video_name(),
|
||||
description: () => m.agent_tool_generate_video_desc(),
|
||||
},
|
||||
]
|
||||
|
||||
type AgentFormValues = z.infer<typeof agentFormSchema>
|
||||
|
||||
@@ -0,0 +1,240 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, render, screen, fireEvent, waitFor } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { ImageVideoGenerationSettings } from './ImageVideoGenerationSettings'
|
||||
import type { MediaGenerationSettingsResponse } from '@shumai/dtos'
|
||||
|
||||
const mockGetSettings = vi.fn()
|
||||
const mockPutProvider = vi.fn()
|
||||
const mockPostModel = vi.fn()
|
||||
const mockDeleteModel = vi.fn()
|
||||
const mockGetCurated = vi.fn()
|
||||
|
||||
vi.mock('@/ui/api/client', () => ({
|
||||
client: {
|
||||
api: {
|
||||
teams: {
|
||||
':teamId': {
|
||||
'media-generation': {
|
||||
$get: () => mockGetSettings(),
|
||||
providers: {
|
||||
':provider': {
|
||||
$put: (args: unknown) => mockPutProvider(args),
|
||||
},
|
||||
},
|
||||
models: {
|
||||
$post: (args: unknown) => mockPostModel(args),
|
||||
':modelId': {
|
||||
$delete: (args: unknown) => mockDeleteModel(args),
|
||||
},
|
||||
curated: {
|
||||
$get: (args: unknown) => mockGetCurated(args),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('sonner', () => ({
|
||||
toast: {
|
||||
success: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
describe('ImageVideoGenerationSettings', () => {
|
||||
const initialSettings: MediaGenerationSettingsResponse = {
|
||||
providers: [
|
||||
{
|
||||
provider: 'openai',
|
||||
defaultEnvKey: 'OPENAI_API_KEY',
|
||||
apiKeyConfigured: true,
|
||||
status: 'configured_env',
|
||||
supportedTypes: ['image'],
|
||||
},
|
||||
{
|
||||
provider: 'klingai',
|
||||
defaultEnvKey: 'KLINGAI_API_KEY',
|
||||
apiKeyConfigured: true,
|
||||
status: 'configured_custom',
|
||||
customApiKeyOrEnv: 'my-custom-key',
|
||||
supportedTypes: ['video'],
|
||||
},
|
||||
{
|
||||
provider: 'fal',
|
||||
defaultEnvKey: 'FAL_KEY',
|
||||
apiKeyConfigured: false,
|
||||
status: 'not_configured',
|
||||
supportedTypes: ['image', 'video'],
|
||||
},
|
||||
],
|
||||
enabledModels: [
|
||||
{
|
||||
id: 'model-1',
|
||||
type: 'image',
|
||||
provider: 'openai',
|
||||
modelId: 'dall-e-3',
|
||||
name: 'DALL-E 3',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
{
|
||||
id: 'model-2',
|
||||
type: 'video',
|
||||
provider: 'klingai',
|
||||
modelId: 'kling-v1-standard',
|
||||
name: 'Kling v1',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
let queryClient: QueryClient
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
mutations: { retry: false },
|
||||
},
|
||||
})
|
||||
mockGetSettings.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => initialSettings,
|
||||
})
|
||||
mockPutProvider.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ success: true }),
|
||||
})
|
||||
mockPostModel.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
id: 'new-model-id',
|
||||
type: 'image',
|
||||
provider: 'openai',
|
||||
modelId: 'dall-e-2',
|
||||
name: 'DALL-E 2',
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
}),
|
||||
})
|
||||
mockDeleteModel.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ success: true }),
|
||||
})
|
||||
mockGetCurated.mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => [
|
||||
{ modelId: 'dall-e-3', name: 'DALL-E 3', type: 'image' },
|
||||
{ modelId: 'dall-e-2', name: 'DALL-E 2', type: 'image' },
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
const renderComponent = () =>
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ImageVideoGenerationSettings teamId="team-123" />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
it('renders provider list and enabled models', async () => {
|
||||
renderComponent()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getAllByText('OpenAI').length).toBeGreaterThan(0)
|
||||
expect(screen.getAllByText('Kling AI').length).toBeGreaterThan(0)
|
||||
expect(screen.getAllByText('Fal').length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
// Verify enabled models table
|
||||
expect(screen.getByText('dall-e-3')).toBeDefined()
|
||||
expect(screen.getByText('kling-v1-standard')).toBeDefined()
|
||||
})
|
||||
|
||||
it('opens configure API key dialog and saves new key', async () => {
|
||||
renderComponent()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getAllByText('OpenAI').length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
const editButtons = screen.getAllByRole('button', { name: /Edit API Key/i })
|
||||
fireEvent.click(editButtons[0])
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByRole('dialog')).toBeDefined()
|
||||
})
|
||||
|
||||
const input = document.querySelector('input[type="password"]') as HTMLInputElement
|
||||
expect(input).toBeDefined()
|
||||
fireEvent.change(input, { target: { value: 'sk-new-openai-test-key' } })
|
||||
|
||||
const saveButton = screen.getByRole('button', { name: 'Save' })
|
||||
fireEvent.click(saveButton)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockPutProvider).toHaveBeenCalledWith({
|
||||
param: { teamId: 'team-123', provider: 'openai' },
|
||||
json: { apiKey: 'sk-new-openai-test-key' },
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('opens add model dialog and enables adding model', async () => {
|
||||
renderComponent()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getAllByText('OpenAI').length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
const addModelButtons = screen.getAllByRole('button', { name: /Add Model/i })
|
||||
fireEvent.click(addModelButtons[0])
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByRole('dialog')).toBeDefined()
|
||||
})
|
||||
expect(
|
||||
screen.getByText('Select the media generation type, provider, and model to enable.'),
|
||||
).toBeDefined()
|
||||
})
|
||||
|
||||
it('masks existing custom key and allows removing custom key', async () => {
|
||||
renderComponent()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getAllByText('Kling AI').length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
const editButtons = screen.getAllByRole('button', { name: /Edit API Key/i })
|
||||
// Kling AI is index 1
|
||||
fireEvent.click(editButtons[1])
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByRole('dialog')).toBeDefined()
|
||||
})
|
||||
|
||||
const input = document.querySelector('input[type="password"]') as HTMLInputElement
|
||||
expect(input).toBeDefined()
|
||||
expect(input.value).toBe('') // Secret is not prefilled in the input
|
||||
expect(input.placeholder).toBe('••••••••') // Masked indicator
|
||||
|
||||
const removeButton = screen.getByRole('button', { name: 'Remove Key' })
|
||||
fireEvent.click(removeButton)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockPutProvider).toHaveBeenCalledWith({
|
||||
param: { teamId: 'team-123', provider: 'klingai' },
|
||||
json: { apiKey: '' },
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,741 @@
|
||||
import { useState } from 'react'
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/ui/components/ui/card'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import { Input } from '@/ui/components/ui/input'
|
||||
import { Badge } from '@/ui/components/ui/badge'
|
||||
import { ScrollArea } from '@/ui/components/ui/scroll-area'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/ui/components/ui/dialog'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@/ui/components/ui/alert-dialog'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/ui/components/ui/select'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@/ui/components/ui/table'
|
||||
import {
|
||||
Key,
|
||||
Plus,
|
||||
Trash2,
|
||||
AlertCircle,
|
||||
ImageIcon,
|
||||
VideoIcon,
|
||||
CheckCircle2,
|
||||
Loader2,
|
||||
} from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import type {
|
||||
MediaGenerationSettingsResponse,
|
||||
MediaProviderStatus,
|
||||
EnabledMediaModel,
|
||||
CuratedMediaModel,
|
||||
} from '@shumai/dtos'
|
||||
|
||||
const PROVIDER_NAMES: Record<string, string> = {
|
||||
openai: 'OpenAI',
|
||||
google: 'Google',
|
||||
'google-vertex': 'Google Vertex AI',
|
||||
xai: 'xAI',
|
||||
fal: 'Fal',
|
||||
replicate: 'Replicate',
|
||||
'black-forest-labs': 'Black Forest Labs',
|
||||
klingai: 'Kling AI',
|
||||
togetherai: 'Together AI',
|
||||
fireworks: 'Fireworks AI',
|
||||
deepinfra: 'DeepInfra',
|
||||
luma: 'Luma',
|
||||
'amazon-bedrock': 'Amazon Bedrock',
|
||||
alibaba: 'Alibaba',
|
||||
bytedance: 'ByteDance',
|
||||
minimax: 'MiniMax',
|
||||
prodia: 'Prodia',
|
||||
azure: 'Azure OpenAI',
|
||||
}
|
||||
|
||||
export function ImageVideoGenerationSettings({ teamId }: { teamId: string }) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
// API Key Dialog State
|
||||
const [keyDialogOpen, setKeyDialogOpen] = useState(false)
|
||||
const [editingProvider, setEditingProvider] = useState<MediaProviderStatus | null>(null)
|
||||
const [apiKeyInput, setApiKeyInput] = useState('')
|
||||
|
||||
// Add Model Dialog State
|
||||
const [addDialogOpen, setAddDialogOpen] = useState(false)
|
||||
const [addType, setAddType] = useState<'image' | 'video'>('image')
|
||||
const [addProvider, setAddProvider] = useState<string>('')
|
||||
const [selectedModelId, setSelectedModelId] = useState<string>('')
|
||||
const [customModelId, setCustomModelId] = useState<string>('')
|
||||
const [modelDisplayName, setModelDisplayName] = useState<string>('')
|
||||
|
||||
// Delete Model Confirmation State
|
||||
const [modelToDelete, setModelToDelete] = useState<EnabledMediaModel | null>(null)
|
||||
|
||||
// Fetch Media Generation Settings
|
||||
const { data: settings, isLoading } = useQuery<MediaGenerationSettingsResponse>({
|
||||
queryKey: ['teams', teamId, 'media-generation'],
|
||||
queryFn: async () => {
|
||||
const res = await client.api.teams[':teamId']['media-generation'].$get({
|
||||
param: { teamId },
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to fetch media generation settings')
|
||||
return (await res.json()) as MediaGenerationSettingsResponse
|
||||
},
|
||||
})
|
||||
|
||||
// Fetch Curated Models for Add Dialog
|
||||
const { data: curatedModels, isLoading: isLoadingCurated } = useQuery<CuratedMediaModel[]>({
|
||||
queryKey: ['teams', teamId, 'media-generation', 'curated', addType, addProvider],
|
||||
queryFn: async () => {
|
||||
if (!addProvider) return []
|
||||
const res = await client.api.teams[':teamId']['media-generation']['models']['curated'].$get({
|
||||
param: { teamId },
|
||||
query: { provider: addProvider, type: addType },
|
||||
})
|
||||
if (!res.ok) return []
|
||||
return (await res.json()) as CuratedMediaModel[]
|
||||
},
|
||||
enabled: addDialogOpen && !!addProvider,
|
||||
})
|
||||
|
||||
// Mutation: Update Provider API Key
|
||||
const { mutate: updateApiKey, isPending: isSavingKey } = useMutation({
|
||||
mutationFn: async ({ provider, apiKey }: { provider: string; apiKey?: string }) => {
|
||||
const res = await client.api.teams[':teamId']['media-generation']['providers'][
|
||||
':provider'
|
||||
].$put({
|
||||
param: { teamId, provider },
|
||||
json: { apiKey },
|
||||
})
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}))
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
throw new Error((err as any)?.message || 'Failed to update API key')
|
||||
}
|
||||
return await res.json()
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'media-generation'] })
|
||||
toast.success(m.api_key_saved_successfully())
|
||||
setKeyDialogOpen(false)
|
||||
setEditingProvider(null)
|
||||
setApiKeyInput('')
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err instanceof Error ? err.message : m.failed_to_save_api_key())
|
||||
},
|
||||
})
|
||||
|
||||
// Mutation: Add Enabled Model
|
||||
const { mutate: addModel, isPending: isAddingModel } = useMutation({
|
||||
mutationFn: async (payload: {
|
||||
type: 'image' | 'video'
|
||||
provider: string
|
||||
modelId: string
|
||||
name?: string
|
||||
}) => {
|
||||
const res = await client.api.teams[':teamId']['media-generation']['models'].$post({
|
||||
param: { teamId },
|
||||
json: payload,
|
||||
})
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}))
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
throw new Error((err as any)?.message || 'Failed to add model')
|
||||
}
|
||||
return await res.json()
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'media-generation'] })
|
||||
toast.success(m.model_added_successfully())
|
||||
setAddDialogOpen(false)
|
||||
resetAddForm()
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err instanceof Error ? err.message : m.failed_to_add_model())
|
||||
},
|
||||
})
|
||||
|
||||
// Mutation: Remove Enabled Model
|
||||
const { mutate: removeModel, isPending: isRemovingModel } = useMutation({
|
||||
mutationFn: async (modelId: string) => {
|
||||
const res = await client.api.teams[':teamId']['media-generation']['models'][
|
||||
':modelId'
|
||||
].$delete({
|
||||
param: { teamId, modelId },
|
||||
})
|
||||
if (!res.ok) throw new Error('Failed to remove model')
|
||||
return await res.json()
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'media-generation'] })
|
||||
toast.success(m.model_removed_successfully())
|
||||
setModelToDelete(null)
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err instanceof Error ? err.message : m.failed_to_remove_model())
|
||||
},
|
||||
})
|
||||
|
||||
const resetAddForm = () => {
|
||||
setAddType('image')
|
||||
setAddProvider('')
|
||||
setSelectedModelId('')
|
||||
setCustomModelId('')
|
||||
setModelDisplayName('')
|
||||
}
|
||||
|
||||
const handleOpenKeyDialog = (provider: MediaProviderStatus) => {
|
||||
setEditingProvider(provider)
|
||||
setApiKeyInput('')
|
||||
setKeyDialogOpen(true)
|
||||
}
|
||||
|
||||
const handleSaveKey = () => {
|
||||
if (!editingProvider) return
|
||||
const trimmed = apiKeyInput.trim()
|
||||
if (!trimmed) {
|
||||
setKeyDialogOpen(false)
|
||||
return
|
||||
}
|
||||
updateApiKey({
|
||||
provider: editingProvider.provider,
|
||||
apiKey: trimmed,
|
||||
})
|
||||
}
|
||||
|
||||
const handleRemoveKey = () => {
|
||||
if (!editingProvider) return
|
||||
updateApiKey({
|
||||
provider: editingProvider.provider,
|
||||
apiKey: '',
|
||||
})
|
||||
}
|
||||
|
||||
const handleAddModelSubmit = () => {
|
||||
const finalModelId =
|
||||
selectedModelId === '__custom__' ? customModelId.trim() : selectedModelId.trim()
|
||||
if (!addProvider || !finalModelId) return
|
||||
|
||||
addModel({
|
||||
type: addType,
|
||||
provider: addProvider,
|
||||
modelId: finalModelId,
|
||||
name: modelDisplayName.trim() || undefined,
|
||||
})
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex h-64 items-center justify-center">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const providers = settings?.providers || []
|
||||
const enabledModels = settings?.enabledModels || []
|
||||
const activeProvidersMap = new Map(providers.map((p) => [p.provider, p.apiKeyConfigured]))
|
||||
|
||||
// Filter providers that support the currently selected addType
|
||||
const compatibleProviders = providers.filter((p) => p.supportedTypes.includes(addType))
|
||||
|
||||
return (
|
||||
<ScrollArea className="h-full pr-1">
|
||||
<div className="space-y-8 pb-12">
|
||||
{/* Section 1: Media Providers */}
|
||||
<Card className="border-border">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base font-semibold">{m.media_providers()}</CardTitle>
|
||||
<CardDescription>{m.media_providers_description()}</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-2.5">
|
||||
{providers.map((provider) => {
|
||||
const displayName = PROVIDER_NAMES[provider.provider] || provider.provider
|
||||
const isCustom = provider.status === 'configured_custom'
|
||||
const isEnv = provider.status === 'configured_env'
|
||||
|
||||
return (
|
||||
<div
|
||||
key={provider.provider}
|
||||
className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 p-3.5 rounded-lg border border-border bg-card/50 hover:bg-card/80 transition-colors"
|
||||
>
|
||||
<div className="flex flex-col sm:flex-row sm:items-center gap-3 sm:gap-4 min-w-0 flex-1">
|
||||
<div className="min-w-[160px] flex items-center gap-2">
|
||||
<span className="font-medium text-sm text-foreground truncate">
|
||||
{displayName}
|
||||
</span>
|
||||
{isCustom && (
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="bg-emerald-500/10 text-emerald-600 dark:text-emerald-400 border-emerald-500/20 text-[11px] shrink-0"
|
||||
>
|
||||
<CheckCircle2 className="w-3 h-3 mr-1" />
|
||||
{m.provider_status_configured_custom()}
|
||||
</Badge>
|
||||
)}
|
||||
{isEnv && (
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="bg-sky-500/10 text-sky-600 dark:text-sky-400 border-sky-500/20 text-[11px] shrink-0"
|
||||
>
|
||||
<CheckCircle2 className="w-3 h-3 mr-1" />
|
||||
{m.provider_status_configured_env()}
|
||||
</Badge>
|
||||
)}
|
||||
{!isCustom && !isEnv && (
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="bg-muted text-muted-foreground border-border text-[11px] shrink-0"
|
||||
>
|
||||
{m.provider_status_not_configured()}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<div className="flex items-center gap-1.5">
|
||||
{provider.supportedTypes.includes('image') && (
|
||||
<span className="inline-flex items-center gap-1 text-[11px] text-muted-foreground bg-accent/60 px-1.5 py-0.5 rounded">
|
||||
<ImageIcon className="w-3 h-3" />
|
||||
{m.media_type_image()}
|
||||
</span>
|
||||
)}
|
||||
{provider.supportedTypes.includes('video') && (
|
||||
<span className="inline-flex items-center gap-1 text-[11px] text-muted-foreground bg-accent/60 px-1.5 py-0.5 rounded">
|
||||
<VideoIcon className="w-3 h-3" />
|
||||
{m.media_type_video()}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<span className="text-xs text-muted-foreground font-mono opacity-70">
|
||||
{provider.defaultEnvKey}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-end shrink-0">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="text-xs h-8 whitespace-nowrap"
|
||||
onClick={() => handleOpenKeyDialog(provider)}
|
||||
>
|
||||
<Key className="w-3.5 h-3.5 mr-1.5" />
|
||||
{provider.apiKeyConfigured ? m.edit_api_key() : m.configure_api_key()}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Section 2: Enabled Models */}
|
||||
<Card className="border-border">
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0">
|
||||
<div>
|
||||
<CardTitle className="text-base font-semibold">{m.enabled_media_models()}</CardTitle>
|
||||
<CardDescription className="mt-1">
|
||||
{m.enabled_media_models_description()}
|
||||
</CardDescription>
|
||||
</div>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
resetAddForm()
|
||||
setAddDialogOpen(true)
|
||||
}}
|
||||
>
|
||||
<Plus className="w-4 h-4 mr-1.5" />
|
||||
{m.add_media_model()}
|
||||
</Button>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{enabledModels.length === 0 ? (
|
||||
<div className="text-center py-8 text-sm text-muted-foreground">
|
||||
<p>{m.no_enabled_models()}</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="rounded-md border border-border">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead className="w-[100px]">{m.model_type()}</TableHead>
|
||||
<TableHead>{m.providers()}</TableHead>
|
||||
<TableHead>{m.model()}</TableHead>
|
||||
<TableHead className="w-[80px] text-right"></TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{enabledModels.map((model) => {
|
||||
const isConfigured = activeProvidersMap.get(model.provider) ?? false
|
||||
const providerDisplayName = PROVIDER_NAMES[model.provider] || model.provider
|
||||
|
||||
return (
|
||||
<TableRow key={model.id}>
|
||||
<TableCell>
|
||||
{model.type === 'image' ? (
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="bg-primary/10 text-primary border-primary/20 text-xs gap-1"
|
||||
>
|
||||
<ImageIcon className="w-3 h-3" />
|
||||
{m.media_type_image()}
|
||||
</Badge>
|
||||
) : (
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="bg-purple-500/10 text-purple-600 dark:text-purple-400 border-purple-500/20 text-xs gap-1"
|
||||
>
|
||||
<VideoIcon className="w-3 h-3" />
|
||||
{m.media_type_video()}
|
||||
</Badge>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="font-medium text-sm">
|
||||
{providerDisplayName}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="font-mono text-xs">{model.modelId}</span>
|
||||
{model.name && (
|
||||
<span className="text-xs text-muted-foreground">
|
||||
({model.name})
|
||||
</span>
|
||||
)}
|
||||
{!isConfigured && (
|
||||
<div
|
||||
className="flex items-center gap-1 text-xs text-amber-600 dark:text-amber-400 bg-amber-500/10 px-2 py-0.5 rounded"
|
||||
title={m.model_requires_api_key_warning()}
|
||||
>
|
||||
<AlertCircle className="w-3 h-3" />
|
||||
<span>{m.provider_status_not_configured()}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-8 w-8 text-muted-foreground hover:text-destructive"
|
||||
onClick={() => setModelToDelete(model)}
|
||||
disabled={isRemovingModel}
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Dialog: Configure Provider API Key */}
|
||||
<Dialog open={keyDialogOpen} onOpenChange={setKeyDialogOpen}>
|
||||
<DialogContent className="sm:max-w-[480px]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>
|
||||
{editingProvider
|
||||
? `${PROVIDER_NAMES[editingProvider.provider] || editingProvider.provider} API Key`
|
||||
: m.configure_api_key()}
|
||||
</DialogTitle>
|
||||
<DialogDescription>
|
||||
{editingProvider &&
|
||||
m.api_key_or_env_var_hint({ envKey: editingProvider.defaultEnvKey })}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="space-y-4 py-3">
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs font-medium text-foreground">
|
||||
{m.api_key_or_env_var()}
|
||||
</label>
|
||||
<Input
|
||||
type="password"
|
||||
value={apiKeyInput}
|
||||
onChange={(e) => setApiKeyInput(e.target.value)}
|
||||
placeholder={
|
||||
editingProvider?.status === 'configured_custom'
|
||||
? '••••••••'
|
||||
: editingProvider
|
||||
? m.api_key_or_env_var_placeholder({
|
||||
envKey: editingProvider.defaultEnvKey,
|
||||
})
|
||||
: ''
|
||||
}
|
||||
className="font-mono text-sm"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<DialogFooter className="flex items-center justify-between w-full">
|
||||
{editingProvider?.status === 'configured_custom' ? (
|
||||
<Button
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
onClick={handleRemoveKey}
|
||||
disabled={isSavingKey}
|
||||
>
|
||||
{m.remove_key()}
|
||||
</Button>
|
||||
) : (
|
||||
<div />
|
||||
)}
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => setKeyDialogOpen(false)}
|
||||
disabled={isSavingKey}
|
||||
>
|
||||
{m.cancel()}
|
||||
</Button>
|
||||
<Button onClick={handleSaveKey} disabled={isSavingKey}>
|
||||
{isSavingKey && <Loader2 className="w-4 h-4 mr-1.5 animate-spin" />}
|
||||
{m.save()}
|
||||
</Button>
|
||||
</div>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
{/* Dialog: Add Model */}
|
||||
<Dialog open={addDialogOpen} onOpenChange={setAddDialogOpen}>
|
||||
<DialogContent className="sm:max-w-[500px]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{m.add_media_model()}</DialogTitle>
|
||||
<DialogDescription>{m.add_media_model_desc()}</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="space-y-4 py-3">
|
||||
{/* Type Select */}
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-foreground">{m.model_type()}</label>
|
||||
<Select
|
||||
value={addType}
|
||||
onValueChange={(val: 'image' | 'video') => {
|
||||
setAddType(val)
|
||||
setAddProvider('')
|
||||
setSelectedModelId('')
|
||||
setCustomModelId('')
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="image">
|
||||
<div className="flex items-center gap-2">
|
||||
<ImageIcon className="w-4 h-4" />
|
||||
<span>{m.media_type_image()}</span>
|
||||
</div>
|
||||
</SelectItem>
|
||||
<SelectItem value="video">
|
||||
<div className="flex items-center gap-2">
|
||||
<VideoIcon className="w-4 h-4" />
|
||||
<span>{m.media_type_video()}</span>
|
||||
</div>
|
||||
</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{/* Provider Select */}
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-foreground">{m.select_provider()}</label>
|
||||
<Select
|
||||
value={addProvider}
|
||||
onValueChange={(val) => {
|
||||
setAddProvider(val)
|
||||
setSelectedModelId('')
|
||||
setCustomModelId('')
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder={m.select_provider()} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{compatibleProviders.map((p) => (
|
||||
<SelectItem key={p.provider} value={p.provider}>
|
||||
<div className="flex items-center justify-between w-full gap-4">
|
||||
<span>{PROVIDER_NAMES[p.provider] || p.provider}</span>
|
||||
{!p.apiKeyConfigured && (
|
||||
<span className="text-[11px] text-muted-foreground">
|
||||
({m.provider_status_not_configured()})
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{/* Model Select */}
|
||||
{addProvider && (
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-foreground">{m.select_model()}</label>
|
||||
{isLoadingCurated ? (
|
||||
<div className="flex items-center gap-2 text-xs text-muted-foreground py-2">
|
||||
<Loader2 className="w-3.5 h-3.5 animate-spin" />
|
||||
<span>{m.loading_models()}</span>
|
||||
</div>
|
||||
) : (
|
||||
<Select
|
||||
value={selectedModelId}
|
||||
onValueChange={(val) => {
|
||||
setSelectedModelId(val)
|
||||
if (val !== '__custom__') {
|
||||
const found = curatedModels?.find((m) => m.modelId === val)
|
||||
if (found) setModelDisplayName(found.name)
|
||||
} else {
|
||||
setModelDisplayName('')
|
||||
}
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder={m.select_model()} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{curatedModels?.map((curated) => (
|
||||
<SelectItem key={curated.modelId} value={curated.modelId}>
|
||||
<div className="flex items-center justify-between w-full gap-2">
|
||||
<span>{curated.name}</span>
|
||||
<span className="font-mono text-[11px] text-muted-foreground">
|
||||
{curated.modelId}
|
||||
</span>
|
||||
</div>
|
||||
</SelectItem>
|
||||
))}
|
||||
<SelectItem value="__custom__">
|
||||
<span className="italic text-muted-foreground">
|
||||
{m.custom_model_option()}
|
||||
</span>
|
||||
</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Custom Model ID Input */}
|
||||
{selectedModelId === '__custom__' && (
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-foreground">
|
||||
{m.custom_model_id()}
|
||||
</label>
|
||||
<Input
|
||||
value={customModelId}
|
||||
onChange={(e) => setCustomModelId(e.target.value)}
|
||||
placeholder={m.enter_custom_model_id()}
|
||||
className="font-mono text-sm"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Display Name Input */}
|
||||
{addProvider && selectedModelId && (
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-foreground">
|
||||
{m.model_display_name()}
|
||||
</label>
|
||||
<Input
|
||||
value={modelDisplayName}
|
||||
onChange={(e) => setModelDisplayName(e.target.value)}
|
||||
placeholder={m.model_display_name_placeholder()}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => setAddDialogOpen(false)}
|
||||
disabled={isAddingModel}
|
||||
>
|
||||
{m.cancel()}
|
||||
</Button>
|
||||
<Button
|
||||
onClick={handleAddModelSubmit}
|
||||
disabled={
|
||||
isAddingModel ||
|
||||
!addProvider ||
|
||||
!selectedModelId ||
|
||||
(selectedModelId === '__custom__' && !customModelId.trim())
|
||||
}
|
||||
>
|
||||
{isAddingModel && <Loader2 className="w-4 h-4 mr-1.5 animate-spin" />}
|
||||
{m.add()}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
{/* Alert Dialog: Confirm Model Deletion */}
|
||||
<AlertDialog
|
||||
open={!!modelToDelete}
|
||||
onOpenChange={(open) => {
|
||||
if (!open) setModelToDelete(null)
|
||||
}}
|
||||
>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>{m.remove_model_confirm()}</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
{modelToDelete &&
|
||||
m.remove_model_prompt({
|
||||
modelId: modelToDelete.modelId,
|
||||
provider: PROVIDER_NAMES[modelToDelete.provider] || modelToDelete.provider,
|
||||
})}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel disabled={isRemovingModel}>{m.cancel()}</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={() => {
|
||||
if (modelToDelete) removeModel(modelToDelete.id)
|
||||
}}
|
||||
disabled={isRemovingModel}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
{isRemovingModel && <Loader2 className="w-4 h-4 mr-1.5 animate-spin" />}
|
||||
{m.delete()}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
</ScrollArea>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, render, screen } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { QuotaRuleDialog } from './QuotaRuleDialog'
|
||||
import type { QuotaRuleResponse } from '@shumai/dtos'
|
||||
|
||||
vi.mock('@/ui/api/client', () => ({
|
||||
client: {
|
||||
api: {
|
||||
teams: {
|
||||
':teamId': {
|
||||
members: {
|
||||
$get: vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => [],
|
||||
}),
|
||||
},
|
||||
mcp: {
|
||||
servers: {
|
||||
$get: vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ servers: [] }),
|
||||
}),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('sonner', () => ({
|
||||
toast: {
|
||||
success: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
describe('QuotaRuleDialog', () => {
|
||||
let queryClient: QueryClient
|
||||
|
||||
beforeEach(() => {
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: { retry: false },
|
||||
mutations: { retry: false },
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
it('displays generate_image and generate_video in tool selector for agent_tool_call_count', () => {
|
||||
const existingRule: QuotaRuleResponse = {
|
||||
id: 'rule-1',
|
||||
teamId: 'team-123',
|
||||
scopeMode: 'each_member',
|
||||
role: null,
|
||||
resource: 'agent_tool_call_count',
|
||||
resourceData: { name: 'generate_image' },
|
||||
limit: 50,
|
||||
period: '1day',
|
||||
enabled: true,
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-01T00:00:00.000Z',
|
||||
}
|
||||
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<QuotaRuleDialog open={true} onOpenChange={vi.fn()} teamId="team-123" rule={existingRule} />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
|
||||
// The select trigger and option both display the selected tool name
|
||||
expect(screen.getAllByText('Generate Image').length).toBeGreaterThan(0)
|
||||
})
|
||||
})
|
||||
@@ -74,6 +74,8 @@ const STANDARD_TOOLS = [
|
||||
{ value: 'create_folder', label: () => m.agent_tool_create_folder_name() },
|
||||
{ value: 'create_file', label: () => m.agent_tool_create_file_name() },
|
||||
{ value: 'create_version', label: () => m.agent_tool_create_version_name() },
|
||||
{ value: 'generate_image', label: () => m.agent_tool_generate_image_name() },
|
||||
{ value: 'generate_video', label: () => m.agent_tool_generate_video_name() },
|
||||
]
|
||||
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
|
||||
Reference in New Issue
Block a user