feat(agent): use deterministic storage keys for preset avatars (#404)

This commit is contained in:
Yiling-J
2026-09-13 21:40:34 +08:00
committed by GitHub
parent 7d56b5bf90
commit ddbf897b27
6 changed files with 58 additions and 12 deletions
+31 -4
View File
@@ -335,10 +335,24 @@ describe('AgentService', () => {
providerId: provider.id,
modelId: model.id,
})
expect(agent2!.user.image).toMatch(/^files\/[A-Z0-9]{26}\.webp$/)
expect(agent2!.user.image).toBe('files/avatars/avatar-2.webp')
expect((agent2!.config as unknown as PrismaJson.AgentConfig).avatarPreset).toBe('avatar-2')
const initialKey = agent2!.user.image
// 1b. Create another agent with the SAME preset avatar (should share identical storage key)
const agent2Duplicate = await svc.createAgent({
teamId: team.id,
name: 'Agent Preset 2 Duplicate',
type: 'chat',
enabled: true,
thinkingLevel: 'off',
avatar: 'avatar-2',
providerId: provider.id,
modelId: model.id,
})
expect(agent2Duplicate!.user.image).toBe('files/avatars/avatar-2.webp')
expect(agent2Duplicate!.user.image).toBe(agent2!.user.image)
// 2. Create without avatar (should default to avatar-1)
const agentDefault = await svc.createAgent({
teamId: team.id,
@@ -349,7 +363,7 @@ describe('AgentService', () => {
providerId: provider.id,
modelId: model.id,
})
expect(agentDefault!.user.image).toMatch(/^files\/[A-Z0-9]{26}\.webp$/)
expect(agentDefault!.user.image).toBe('files/avatars/avatar-1.webp')
expect((agentDefault!.config as unknown as PrismaJson.AgentConfig).avatarPreset).toBe(
'avatar-1',
)
@@ -368,7 +382,7 @@ describe('AgentService', () => {
providerId: provider.id,
modelId: model.id,
})
expect(updatedPreset.user.image).toMatch(/^files\/[A-Z0-9]{26}\.webp$/)
expect(updatedPreset.user.image).toBe('files/avatars/avatar-3.webp')
expect(updatedPreset.user.image).not.toBe(initialKey)
expect((updatedPreset.config as unknown as PrismaJson.AgentConfig).avatarPreset).toBe(
'avatar-3',
@@ -407,6 +421,19 @@ describe('AgentService', () => {
(updatedPresigned.config as unknown as PrismaJson.AgentConfig).avatarPreset,
).toBeUndefined()
// 4b. Update with presigned URL pointing to a preset avatar path
const updatedPresignedPreset = await svc.updateAgent({
agentId: agent2!.id,
name: 'Agent Presigned Preset',
type: 'chat',
enabled: true,
thinkingLevel: 'off',
avatar: 'https://shumai.s3.amazonaws.com/files/avatars/avatar-2.webp?foo=bar',
providerId: provider.id,
modelId: model.id,
})
expect(updatedPresignedPreset.user.image).toBe('files/avatars/avatar-2.webp')
// 5. Update without avatar specified (avatar undefined) - preserves image
const updatedWithoutAvatar = await svc.updateAgent({
agentId: agent2!.id,
@@ -417,7 +444,7 @@ describe('AgentService', () => {
providerId: provider.id,
modelId: model.id,
})
expect(updatedWithoutAvatar.user.image).toBe('files/01JK23456789ABCDEF01234567.webp')
expect(updatedWithoutAvatar.user.image).toBe('files/avatars/avatar-2.webp')
})
test('Delete Agent', async () => {
+6 -5
View File
@@ -19,8 +19,7 @@ import { s3Service } from '@shumai/core/src/s3/s3'
import { getAvatarUrl } from '@shumai/core/src/user/avatar'
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
import { getAllowedAgentRoles } from './permissions'
import { ulid } from 'ulid'
import { isPresetAvatarId, getPresetAvatarBuffer } from './presets'
import { isPresetAvatarId, getPresetAvatarBuffer, getPresetAvatarStorageKey } from './presets'
import AdmZip from 'adm-zip'
import * as fs from 'fs'
import * as path from 'path'
@@ -50,7 +49,9 @@ interface AgentInfoSource {
function extractS3Key(urlOrKey?: string | null): string | null | undefined {
if (!urlOrKey) return urlOrKey
const match = urlOrKey.match(/files\/[A-Z0-9]{26}(\.[a-zA-Z0-9]+)?/)
const match = urlOrKey.match(
/(files\/avatars\/[a-zA-Z0-9_-]+\.webp|files\/[A-Z0-9]{26}(\.[a-zA-Z0-9]+)?)/,
)
return match ? match[0] : urlOrKey
}
@@ -190,7 +191,7 @@ export class AgentService {
const buffer = getPresetAvatarBuffer(targetAvatar)
if (buffer) {
const bucket = process.env.S3_BUCKET || 'shumai'
const key = `files/${ulid()}.webp`
const key = getPresetAvatarStorageKey(targetAvatar)
await s3Service.putObject(bucket, key, buffer, buffer.length, 'image/webp')
imageKey = key
}
@@ -335,7 +336,7 @@ export class AgentService {
const buffer = getPresetAvatarBuffer(avatar)
if (buffer) {
const bucket = process.env.S3_BUCKET || 'shumai'
const key = `files/${ulid()}.webp`
const key = getPresetAvatarStorageKey(avatar)
await s3Service.putObject(bucket, key, buffer, buffer.length, 'image/webp')
imageKey = key
newAvatarPreset = avatar
+1 -1
View File
@@ -88,7 +88,7 @@ describe('migrateLegacyAgentAvatars', () => {
const updatedAgent = await prisma.user.findUnique({
where: { id: agentUser.id },
})
expect(updatedAgent?.image).toMatch(/^files\/[A-Z0-9]{26}\.webp$/)
expect(updatedAgent?.image).toBe('files/avatars/avatar-1.webp')
const updatedAgentRecord = await prisma.agent.findUnique({
where: { id: agentUser.id },
+9 -2
View File
@@ -2,7 +2,12 @@ import { prisma } from '@shumai/db'
import { s3Service } from '@shumai/core/src/s3/s3'
import { logger } from '@shumai/core/src/logger'
import { ulid } from 'ulid'
import { PRESET_AVATAR_IDS, getPresetAvatarBuffer } from './presets'
import {
PRESET_AVATAR_IDS,
getPresetAvatarBuffer,
getPresetAvatarStorageKey,
type PresetAvatarId,
} from './presets'
export async function migrateLegacyAgentAvatars(
client: typeof prisma = prisma,
@@ -58,7 +63,9 @@ export async function migrateLegacyAgentAvatars(
}
}
const key = `files/${ulid()}.${ext}`
const key = matchedPresetId
? getPresetAvatarStorageKey(matchedPresetId as PresetAvatarId)
: `files/${ulid()}.${ext}`
await s3Service.putObject(bucket, key, buffer, buffer.length, contentType)
await client.user.update({
+7
View File
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest'
import {
getPresetAvatarBuffer,
getPresetAvatarStorageKey,
isPresetAvatarId,
PRESET_AVATAR_IDS,
type PresetAvatarId,
@@ -27,6 +28,12 @@ describe('agent presets', () => {
expect(getPresetAvatarBuffer('unknown' as PresetAvatarId)).toBeNull()
})
it('returns deterministic storage key for preset avatars', () => {
for (const id of PRESET_AVATAR_IDS) {
expect(getPresetAvatarStorageKey(id)).toBe(`files/avatars/${id}.webp`)
}
})
it('provides AVAILABLE_AVATARS with 8 previews via dtos', async () => {
const { AVAILABLE_AVATARS } = await import('@shumai/dtos')
expect(AVAILABLE_AVATARS).toHaveLength(8)
+4
View File
@@ -7,6 +7,10 @@ import {
export { PRESET_AVATAR_IDS, type PresetAvatarId, isPresetAvatarId }
export function getPresetAvatarStorageKey(id: PresetAvatarId): string {
return `files/avatars/${id}.webp`
}
const bufferCache = new Map<PresetAvatarId, Buffer>()
export function getPresetAvatarBuffer(id: PresetAvatarId): Buffer | null {