feat(agent): add agent avatar selection and S3 uploading

This commit is contained in:
yiling
2026-06-03 23:24:21 +08:00
parent 499458e69b
commit cf2e6907ba
7 changed files with 127 additions and 5 deletions
+39
View File
@@ -190,6 +190,45 @@ describe('AgentService', () => {
expect(reUpdated.skills?.length).toBe(1)
})
test('Create Agent with presigned URL extracts S3 key', async () => {
const db = prisma
const svc = new AgentService()
const { team, provider, model } = await setupTestData(db)
const avatarPresigned = 'https://shumai.s3.amazonaws.com/files/01JK23456789ABCDEF01234567?AWSAccessKeyId=foo'
const agent = await svc.createAgent({
teamId: team.id,
name: 'Agent with URL',
type: 'chat',
enabled: true,
avatar: avatarPresigned,
providerId: provider.id,
modelId: model.id,
})
expect(agent?.id).toBeDefined()
expect(agent?.user.image).toBe('files/01JK23456789ABCDEF01234567')
})
test('Update Agent with presigned URL extracts S3 key', async () => {
const db = prisma
const svc = new AgentService()
const { agent } = await setupTestData(db)
const avatarPresigned = 'https://shumai.s3.amazonaws.com/files/01JK23456789ABCDEF01234567?AWSAccessKeyId=foo'
const updated = await svc.updateAgent({
agentId: agent.id,
name: 'Updated with URL',
type: 'chat',
enabled: true,
avatar: avatarPresigned,
providerId: agent.providerId!,
modelId: agent.modelId!,
})
expect(updated.user.image).toBe('files/01JK23456789ABCDEF01234567')
})
test('Delete Agent', async () => {
const db = prisma
const svc = new AgentService()
+8 -2
View File
@@ -11,6 +11,12 @@ import AdmZip from 'adm-zip'
import * as fs from 'fs'
import * as path from 'path'
function extractS3Key(urlOrKey?: string | null): string | null | undefined {
if (!urlOrKey) return urlOrKey
const match = urlOrKey.match(/files\/[A-Z0-9]{26}/)
return match ? match[0] : urlOrKey
}
export class AgentService {
constructor(private readonly prismaClient: typeof prisma = prisma) {}
@@ -153,7 +159,7 @@ export class AgentService {
name,
email: `agent-${Date.now()}-${Math.random().toString(36).substring(7)}@shumai.ai`,
type: 'agent',
image: avatar,
image: extractS3Key(avatar),
},
})
@@ -252,7 +258,7 @@ export class AgentService {
where: { id: agentId },
data: {
name,
image: avatar,
image: extractS3Key(avatar),
},
})
@@ -28,6 +28,14 @@ import { AgentInfo, AgentType } from '@shumai/dtos'
import { Textarea } from '@/ui/components/ui/textarea'
import { ScrollArea } from '@/ui/components/ui/scroll-area'
import { z } from 'zod'
import { cn } from '@/ui/lib/utils'
const AVAILABLE_AVATARS = [
'/avatars/gpt_top_left.webp',
'/avatars/gpt_top_right.webp',
'/avatars/gpt_bottom_left.webp',
'/avatars/gpt_bottom_right.webp',
]
interface AgentFormDialogProps {
isOpen: boolean
@@ -115,7 +123,7 @@ export function AgentFormDialog({
defaultValues: {
name: initialValues?.name || '',
type: type || initialValues?.type || 'chat',
avatar: initialValues?.avatar || '',
avatar: initialValues?.avatar || '/avatars/gpt_top_left.webp',
providerId: initialValues?.providerId || '',
modelId: initialValues?.modelId || '',
soul: initialValues?.soul || '',
@@ -128,10 +136,34 @@ export function AgentFormDialog({
},
onSubmit: async ({ value }) => {
try {
let avatarKey = value.avatar
if (avatarKey && avatarKey.startsWith('/avatars/')) {
try {
const response = await fetch(avatarKey)
const blob = await response.blob()
const fileToUpload = new File([blob], 'avatar.webp', { type: 'image/webp' })
const uploadRes = await client.api.teams[':teamId'].files.$post({
param: { teamId },
form: { file: fileToUpload },
})
if (!uploadRes.ok) throw new Error('Failed to upload avatar')
const uploadData = await uploadRes.json()
avatarKey = (uploadData as { key: string }).key
} catch (err) {
toast.error('Failed to upload agent avatar')
throw err
}
}
const payload = {
...value,
avatar: avatarKey || undefined,
}
if (initialValues?.id) {
await updateMutation.mutateAsync({ id: initialValues.id, values: value })
await updateMutation.mutateAsync({ id: initialValues.id, values: payload })
} else {
await createMutation.mutateAsync(value)
await createMutation.mutateAsync(payload)
}
} catch {
// Error handled by mutation
@@ -273,6 +305,51 @@ export function AgentFormDialog({
</Field>
)}
/>
<form.Field
name="avatar"
children={(field) => (
<Field className="md:col-span-2">
<FieldLabel>Agent Avatar</FieldLabel>
<div className="flex gap-4 mt-2">
{AVAILABLE_AVATARS.map((avatarPath) => {
const isSelected = field.state.value === avatarPath
return (
<button
key={avatarPath}
type="button"
onClick={() => field.handleChange(avatarPath)}
className={cn(
'relative w-14 h-14 rounded-full overflow-hidden border-2 transition-all hover:scale-105',
isSelected
? 'border-primary ring-2 ring-primary ring-offset-2'
: 'border-border hover:border-muted-foreground',
)}
>
<img
src={avatarPath}
className="w-full h-full object-cover"
alt="Preset Avatar Option"
/>
</button>
)
})}
{field.state.value && !AVAILABLE_AVATARS.includes(field.state.value) && (
<button
type="button"
className="relative w-14 h-14 rounded-full overflow-hidden border-2 border-primary ring-2 ring-primary ring-offset-2 scale-105"
>
<img
src={field.state.value}
className="w-full h-full object-cover"
alt="Current Custom Avatar"
/>
</button>
)}
</div>
</Field>
)}
/>
</div>
{isAiType && (
Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB