Merge pull request #61 from shumaiOne/feat/agent-avatars

feat(agent): add agent avatar selection and S3 uploading
This commit is contained in:
Yiling-J
2026-06-04 10:21:18 +08:00
committed by GitHub
5 changed files with 114 additions and 5 deletions
+41
View File
@@ -190,6 +190,47 @@ 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,
thinkingLevel: 'medium',
})
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!,
thinkingLevel: 'medium',
})
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),
},
})
+5 -1
View File
@@ -8,7 +8,11 @@ export async function getAvatarUrl(
imageKeyOrUrl: string | null | undefined,
): Promise<string | undefined> {
if (!imageKeyOrUrl) return undefined
if (imageKeyOrUrl.startsWith('http://') || imageKeyOrUrl.startsWith('https://')) {
if (
imageKeyOrUrl.startsWith('data:') ||
imageKeyOrUrl.startsWith('http://') ||
imageKeyOrUrl.startsWith('https://')
) {
return imageKeyOrUrl
}
try {
@@ -28,6 +28,8 @@ 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'
import { AVAILABLE_AVATARS } from './avatars'
interface AgentFormDialogProps {
isOpen: boolean
@@ -115,7 +117,7 @@ export function AgentFormDialog({
defaultValues: {
name: initialValues?.name || '',
type: type || initialValues?.type || 'chat',
avatar: initialValues?.avatar || '',
avatar: initialValues?.avatar || AVAILABLE_AVATARS[0],
providerId: initialValues?.providerId || '',
modelId: initialValues?.modelId || '',
soul: initialValues?.soul || '',
@@ -228,7 +230,7 @@ export function AgentFormDialog({
}}
className="flex-1 flex flex-col min-h-0 overflow-hidden"
>
<ScrollArea className="flex-1 h-full">
<ScrollArea className="flex-1 min-h-0">
<div className="p-6 pt-2 space-y-6">
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
<form.Field
@@ -273,6 +275,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 && (
File diff suppressed because one or more lines are too long