From ebce47485e9c92006127458efcb1f48b58f9adbf Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:12:33 +0800 Subject: [PATCH] Import apps/android/src/screens/bot/avatar.tsx --- apps/android/src/screens/bot/avatar.tsx | 174 ++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 apps/android/src/screens/bot/avatar.tsx diff --git a/apps/android/src/screens/bot/avatar.tsx b/apps/android/src/screens/bot/avatar.tsx new file mode 100644 index 0000000..329bfa3 --- /dev/null +++ b/apps/android/src/screens/bot/avatar.tsx @@ -0,0 +1,174 @@ +import { useId, useState } from 'react' +import { Image, StyleSheet, View } from 'react-native' +import { Star } from 'lucide-react-native' +import Svg, { Defs, G, LinearGradient, Mask, Path, Rect, Stop } from 'react-native-svg' +import type { BotEntry } from '../../services/bot-catalog' +import { useTheme } from '../../ui/theme-context' +import { badgeOf, BotAgentAvatar, engineOfModel } from './agent-badge' +import assets from './assets/characters.json' + +// Static native rendering of DeepSeek-Bot v0.2.0+ characters and v0.3.0 local-agent marks (Apache-2.0). +// Attribution and upstream license are retained in assets/. +const shapes = ['star', 'moon', 'gumdrop', 'peanut', 'bunny', 'kitty', 'acorn'] +const inks = ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'violet', 'magenta'] +function hash(id: string) { + let value = 2166136261 + for (let i = 0; i < id.length; i++) value = Math.imul(value ^ id.charCodeAt(i), 16777619) + return value >>> 0 +} +interface Character { body: string; shade: string; light: string; paints?: (string | number)[][]; eyes: [number, number, number, number, number][] } +export function BotAvatar({ bot, size = 40 }: { + bot: Pick & Partial> + size?: number +}) { + const { scheme } = useTheme() + const uid = useId().replace(/:/g, '') + const [brokenImage, setBrokenImage] = useState('') + const rawImage = bot.avatar?.image && bot.avatar.image !== brokenImage ? bot.avatar.image : undefined + const engine = bot.engine ?? engineOfModel(bot.model || bot.appliedModel) + if (rawImage?.startsWith('data:image/svg+xml') && engine !== undefined && badgeOf(engine) !== undefined) { + return + } + if (rawImage !== undefined && /^data:image\/(png|jpe?g|webp|gif);base64,/i.test(rawImage)) { + return ( + setBrokenImage(rawImage)} + /> + ) + } + const seed = hash(bot.id) + let shapeHash = Math.imul(seed ^ (seed >>> 16), 73244475) + shapeHash = Math.imul(shapeHash ^ (shapeHash >>> 13), 3266489909) + const shape = bot.avatar?.shape || shapes[((shapeHash ^ (shapeHash >>> 16)) >>> 0) % shapes.length] || 'gumdrop' + const character = ((assets.characters as unknown as Record)[shape] ?? assets.characters.gumdrop) as Character + const t = (seed + 1831565813) | 0 + let n = Math.imul(t ^ (t >>> 15), 1 | t) + n = (n + Math.imul(n ^ (n >>> 7), 61 | n)) ^ n + const color = bot.color || inks[Math.floor((((n ^ (n >>> 14)) >>> 0) / 4294967296) * inks.length)] || 'deepseek' + const fill = /^#[0-9a-f]{6}$/i.test(color) ? color : (assets.inks as Record)[color]?.[scheme === 'dark' ? 1 : 0] ?? assets.inks.deepseek[0] + return + + + + + {character.eyes.map(([x, y, w, h, tilt], i) => )} + + + + + {character.paints?.map(([d, paint, opacity], i) => )} + + + +} + +export function BotAvatarWithBadge({ + bot, + size = 48, + showMainStar = false, + statusColor, + badgeBorderColor, +}: { + bot: Pick + size?: number + showMainStar?: boolean + statusColor?: string + badgeBorderColor?: string +}) { + const { colors } = useTheme() + const borderCol = badgeBorderColor ?? colors.surface + const isMain = showMainStar && Boolean(bot.main) + const badgeSize = Math.max(16, Math.round(size * 0.31)) + const starSize = Math.max(9, Math.round(badgeSize * 0.56)) + const dotSize = Math.max(11, Math.round(size * 0.24)) + + return ( + + + {isMain ? ( + = 72 ? 2.5 : 1.75, + }, + ]} + > + + + ) : statusColor !== undefined ? ( + + ) : null} + + ) +} + +const badgeStyles = StyleSheet.create({ + starBadge: { + position: 'absolute', + right: -1, + bottom: -1, + backgroundColor: '#F5A623', + alignItems: 'center', + justifyContent: 'center', + }, + statusDot: { + position: 'absolute', + right: 0, + bottom: 0, + borderWidth: 2, + }, +}) + +/** A group chat's mark: its first members' characters, overlapping like the roster row. */ +export function RoomAvatar({ + members, + bots, + size = 26, +}: { + members: string[] + bots: Pick[] + size?: number +}) { + const shown = members.slice(0, 3) + if (shown.length === 0) return null + const inner = Math.max(16, Math.round(size * 0.72)) + const step = Math.max(4, Math.round(size * 0.28)) + return ( + + {shown.map((id, index) => { + const member = bots.find(entry => entry.id === id) + if (member === undefined) return null + return ( + + + + ) + })} + + ) +}