fix(agents): guide Transcript Agent setup

This commit is contained in:
yiling
2026-12-04 19:15:58 +08:00
parent 553269012c
commit 01c471f292
15 changed files with 238 additions and 53 deletions
@@ -0,0 +1,88 @@
// @vitest-environment happy-dom
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { AgentsSettings } from './AgentsSettings'
const getAgents = vi.fn()
vi.mock('@/ui/api/client', () => ({
client: {
api: {
teams: { ':teamId': { agents: { $get: () => getAgents() } } },
},
},
}))
vi.mock('@/ui/hooks/use-permissions', () => ({
usePermissions: () => ({ canAdmin: true }),
}))
vi.mock('./AgentFormDialog', () => ({
AgentFormDialog: ({ title }: { title: string }) => <div role="dialog">{title}</div>,
}))
function renderSettings(initiallyConfigured: boolean) {
const queryClient = new QueryClient()
const onOpenIntegrations = vi.fn()
const view = (configured: boolean) => (
<QueryClientProvider client={queryClient}>
<AgentsSettings
teamId="team"
localEmbeddingProviderConfigured={configured}
onOpenIntegrations={onOpenIntegrations}
/>
</QueryClientProvider>
)
const { rerender } = render(view(initiallyConfigured))
return {
onOpenIntegrations,
updateConfigured: (configured: boolean) => rerender(view(configured)),
}
}
describe('Transcript Agent setup', () => {
afterEach(() => {
cleanup()
vi.resetAllMocks()
})
it('blocks creation and offers to open Integrations when setup is missing', async () => {
getAgents.mockResolvedValue(Response.json([]))
const { onOpenIntegrations, updateConfigured } = renderSettings(false)
await screen.findByText('Set up a local embedding server to use the Transcript Agent.')
const create = screen.getByRole<HTMLButtonElement>('button', {
name: 'Create Transcript Agent',
})
expect(create.disabled).toBe(true)
fireEvent.click(screen.getByRole('button', { name: 'Open Integrations' }))
expect(onOpenIntegrations).toHaveBeenCalledOnce()
updateConfigured(true)
expect(create.disabled).toBe(false)
fireEvent.click(create)
expect(screen.getByRole('dialog')).toBeTruthy()
})
it('keeps an existing Transcript Agent visible with its controls disabled', async () => {
getAgents.mockResolvedValue(
Response.json([
{ id: 'transcriber', name: 'Video Transcriber', type: 'transcription', enabled: true },
]),
)
const { updateConfigured } = renderSettings(false)
const transcriber = await screen.findByText('Video Transcriber')
const enabled = screen.getByRole<HTMLButtonElement>('switch')
expect(enabled.disabled).toBe(true)
fireEvent.click(transcriber)
expect(screen.queryByRole('dialog')).toBeNull()
updateConfigured(true)
expect(enabled.disabled).toBe(false)
fireEvent.click(transcriber)
expect(screen.getByRole('dialog')).toBeTruthy()
})
})
@@ -50,7 +50,9 @@ import { AgentFormDialog } from './AgentFormDialog'
interface AgentsSettingsProps {
teamId: string
localEmbeddingProviderConfigured: boolean
transcriptionAgentManagedByEnv?: boolean
onOpenIntegrations: () => void
}
const AGENT_TYPES: {
@@ -64,7 +66,12 @@ const AGENT_TYPES: {
{ type: 'transcription', title: m.transcript_agent, icon: Captions },
]
export function AgentsSettings({ teamId, transcriptionAgentManagedByEnv }: AgentsSettingsProps) {
export function AgentsSettings({
teamId,
localEmbeddingProviderConfigured,
transcriptionAgentManagedByEnv,
onOpenIntegrations,
}: AgentsSettingsProps) {
const queryClient = useQueryClient()
const { canAdmin } = usePermissions()
const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false)
@@ -265,8 +272,11 @@ export function AgentsSettings({ teamId, transcriptionAgentManagedByEnv }: Agent
const typeAgents = agents.filter((a) => a.type === section.type)
const Icon = section.icon
const isSingleAgentType = section.type !== 'chat'
const isManagedTranscriptAgent =
section.type === 'transcription' && transcriptionAgentManagedByEnv
const isTranscriptSection = section.type === 'transcription'
const requiresEmbeddingServer = isTranscriptSection && !localEmbeddingProviderConfigured
const isManagedTranscriptAgent = isTranscriptSection && transcriptionAgentManagedByEnv
const canCreate =
canAdmin && !isManagedTranscriptAgent && !(isSingleAgentType && typeAgents.length > 0)
return (
<div
@@ -289,23 +299,22 @@ export function AgentsSettings({ teamId, transcriptionAgentManagedByEnv }: Agent
)}
</div>
<div className="flex items-center gap-2">
{canAdmin &&
!isManagedTranscriptAgent &&
!(isSingleAgentType && typeAgents.length > 0) && (
<Button
variant="ghost"
size="icon"
aria-label={m.create_type_agent({ type: getSectionLabel(section.type) })}
className="h-8 w-8 rounded-full bg-primary/10 text-primary hover:bg-primary/20"
onClick={(e) => {
e.stopPropagation()
setCreateType(section.type)
setIsCreateDialogOpen(true)
}}
>
<Plus className="w-4 h-4" />
</Button>
)}
{canCreate && (
<Button
variant="ghost"
size="icon"
aria-label={m.create_type_agent({ type: getSectionLabel(section.type) })}
disabled={requiresEmbeddingServer}
className="h-8 w-8 rounded-full bg-primary/10 text-primary hover:bg-primary/20"
onClick={(e) => {
e.stopPropagation()
setCreateType(section.type)
setIsCreateDialogOpen(true)
}}
>
<Plus className="w-4 h-4" />
</Button>
)}
{!isSingleAgentType && (
<ChevronDown
className={cn(
@@ -319,19 +328,42 @@ export function AgentsSettings({ teamId, transcriptionAgentManagedByEnv }: Agent
{(isSingleAgentType || expandedSections[section.type]) && (
<div className="px-6 pb-6 pt-2 space-y-3">
{section.type === 'transcription' && (
<p className="text-sm text-muted-foreground">
{isManagedTranscriptAgent && typeAgents.length === 0
? m.agent_managed_by_system_hint()
: m.transcript_agent_description()}
</p>
{requiresEmbeddingServer ? (
<div className="flex flex-wrap items-center justify-between gap-2 rounded-lg border border-border bg-muted/40 px-4 py-3">
<p className="text-sm text-muted-foreground">
{m.transcript_agent_setup_required()}
</p>
<Button
type="button"
variant="outline"
size="sm"
onClick={onOpenIntegrations}
>
{m.transcript_agent_open_integrations()}
</Button>
</div>
) : (
isTranscriptSection && (
<p className="text-sm text-muted-foreground">
{isManagedTranscriptAgent && typeAgents.length === 0
? m.agent_managed_by_system_hint()
: m.transcript_agent_description()}
</p>
)
)}
<div className="grid grid-cols-1 gap-4">
{typeAgents.map((agent) => (
<Card
key={agent.id}
className="group relative overflow-hidden border-border hover:border-primary/50 transition-all duration-200 cursor-pointer pb-1 pt-1"
onClick={() => setAgentToEdit(agent)}
className={cn(
'group relative overflow-hidden border-border pb-1 pt-1',
requiresEmbeddingServer
? 'opacity-60'
: 'cursor-pointer transition-all duration-200 hover:border-primary/50',
)}
onClick={
requiresEmbeddingServer ? undefined : () => setAgentToEdit(agent)
}
>
<CardContent className="p-4 flex flex-col gap-3">
<div className="flex items-center justify-between">
@@ -372,6 +404,7 @@ export function AgentsSettings({ teamId, transcriptionAgentManagedByEnv }: Agent
<Button
variant="ghost"
size="icon"
disabled={requiresEmbeddingServer}
className="h-8 w-8 text-muted-foreground hover:text-foreground"
>
<MoreVertical className="w-4 h-4" />
@@ -436,7 +469,7 @@ export function AgentsSettings({ teamId, transcriptionAgentManagedByEnv }: Agent
<div onClick={(e) => e.stopPropagation()}>
<Switch
checked={agent.enabled}
disabled={agent.readOnly}
disabled={agent.readOnly || requiresEmbeddingServer}
onCheckedChange={(enabled) => {
updateAgentMutation.mutate({
teamId,
@@ -461,7 +494,7 @@ export function AgentsSettings({ teamId, transcriptionAgentManagedByEnv }: Agent
</CardContent>
</Card>
))}
{typeAgents.length === 0 && (
{typeAgents.length === 0 && !requiresEmbeddingServer && (
<div className="col-span-full py-8 flex flex-col items-center justify-center border-2 border-dashed border-border rounded-xl bg-muted/30">
<div className="w-10 h-10 rounded-full bg-card flex items-center justify-center shadow-sm mb-3">
<Icon className="w-5 h-5 text-muted-foreground" />
@@ -469,17 +502,19 @@ export function AgentsSettings({ teamId, transcriptionAgentManagedByEnv }: Agent
<p className="text-sm text-muted-foreground">
{m.no_agents_found({ type: getSectionLabel(section.type) })}
</p>
<Button
variant="link"
size="sm"
className="text-primary font-medium h-auto p-0 mt-1"
onClick={() => {
setCreateType(section.type)
setIsCreateDialogOpen(true)
}}
>
{m.create_one_now()}
</Button>
{canCreate && (
<Button
variant="link"
size="sm"
className="text-primary font-medium h-auto p-0 mt-1"
onClick={() => {
setCreateType(section.type)
setIsCreateDialogOpen(true)
}}
>
{m.create_one_now()}
</Button>
)}
</div>
)}
</div>