Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.
Rust ai-agentsai-assistantai-botautomationbotbots-teamchatbotclaude-duetgrokgrok-botgrok-bot-alternativeharnessllmmulti-agentopenai-dotsopenbotpersonal-agentrustself-hostedselfhosted-ai
0.8.1 F7: one avatar per bot in every place (#492)
Oleksandr Ivanchenkoe17cbb4d87c3
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsxindex 095f36e2..a09179a3 100644--- a/frontend/src/App.tsx+++ b/frontend/src/App.tsx@@ -71,6 +71,7 @@ import { playSfx } from './audio/chiptune';import { haptic, messageHaptic } from './haptics';import { Toasts, Toast } from './components/Toasts';import { clearAllTurns, resetTurns, turnEnded, turnStarted } from './kwantActivity';+import { setKnownBots } from './botLooks';import { clearMoods, moodOutput, moodTurnEnded, moodTurnStarted, restingUntil, setMoodNeedsYou, setMoodResting } from './kwantMood';import { browserTabDoc, createTabSignal, needsYouCount } from './tabSignal';import { recentWork, type ChatHome } from './botHome';@@ -3449,6 +3450,8 @@ export const SpaceApp: React.FC<SpaceAppProps> = ({openNotified({ kind: 'human_step', kwant_id: n.kwantId, channel_id: n.channelId ?? null });};const spriteOf = useCallback((id: string) => kwants.find((k) => k.id === id)?.avatar_sprite, [kwants]);+ // Avatars that only know a bot's title or name look its saved style up here.+ useEffect(() => setKnownBots(kwants), [kwants]);// What the empty chat shows: a bot's home, #general's, or nothing special for a team or other channel.const chatHome: ChatHome = currentKwantdiff --git a/frontend/src/botLooks.ts b/frontend/src/botLooks.tsnew file mode 100644index 00000000..aec5ac33--- /dev/null+++ b/frontend/src/botLooks.ts@@ -0,0 +1,69 @@+// One look per bot, everywhere: the avatar style the bot has saved (`avatar_sprite`).+// Some avatars only know a bot's title or name (chat replies, the working line, cards);+// they look the bot up here so they draw the same creature as the sidebar and the Ask chips.++export interface BotLook {+ id: string;+ name: string;+ avatar_sprite?: string | null;+}++let byId = new Map<string, string>();+// A name two bots share maps to null: guessing would give one of them the other's look.+let byName = new Map<string, string | null>();+let rev = 0;+const listeners = new Set<() => void>();++/** Replaces the known bots (the app calls it whenever its bot list changes). */+export function setKnownBots(bots: readonly BotLook[]): void {+ const ids = new Map<string, string>();+ const names = new Map<string, string | null>();+ for (const b of bots) {+ const style = b.avatar_sprite?.trim();+ if (!style) continue;+ ids.set(b.id, style);+ const key = b.name.trim().toLowerCase();+ if (!key) continue;+ names.set(key, names.has(key) && names.get(key) !== style ? null : style);+ }+ byId = ids;+ byName = names;+ rev += 1;+ for (const fn of listeners) fn();+}++export function subscribeBotLooks(fn: () => void): () => void {+ listeners.add(fn);+ return () => {+ listeners.delete(fn);+ };+}++/** Changes whenever the known bots change; the snapshot for useSyncExternalStore. */+export const botLooksRev = (): number => rev;++/** The bot's saved style, by id first, then by a name only one bot has. */+export function savedStyleOf(kwantId?: string | null, name?: string | null): string | undefined {+ if (kwantId) {+ const s = byId.get(kwantId);+ if (s) return s;+ }+ const key = name?.trim().toLowerCase();+ if (!key) return undefined;+ return byName.get(key) ?? undefined;+}++/**+ * The style an avatar draws. A style id the caller passed (`isStyle`) wins: it is already a saved+ * or just-picked style. A title or name hint gives way to the bot's saved style when the bot is+ * known; otherwise the hint stays, as before.+ */+export function avatarStyleFor(+ role: string | undefined,+ kwantId: string | undefined,+ name: string | undefined,+ isStyle: (role: string) => boolean,+): string | undefined {+ if (role && isStyle(role)) return role;+ return savedStyleOf(kwantId, name) ?? role;+}diff --git a/frontend/src/components/ModernAvatar.tsx b/frontend/src/components/ModernAvatar.tsxindex 3cb653fd..cae25c6d 100644--- a/frontend/src/components/ModernAvatar.tsx+++ b/frontend/src/components/ModernAvatar.tsx@@ -4,8 +4,10 @@ import { isKwantWorking, subscribeKwant } from '../kwantActivity';import { AvatarMood, useKwantMood, useKwantNotices } from './AvatarMood';import { moodLabel, restingUntilOf } from '../kwantMood';import { STATUS_DOT, STATUS_PRIORITY, statusText, type BotStatus } from '../botStatus';+import { avatarStyleFor, botLooksRev, subscribeBotLooks } from '../botLooks';interface ModernAvatarProps {+ /** The bot's avatar style, or a title or name hint; a known bot's saved style wins over a hint. */role?: string;name?: string;size?: number; // pixel diameter@@ -126,6 +128,9 @@ const ROLE_CREATURES: Record<string, [keyof typeof PALETTES, Accessory]> = {zen: ['blossom', 'flower'],};+/** Style ids (picked or saved), as opposed to a title or name passed as a hint. */+const isStyleId = (role: string) => role === 'default' || Object.prototype.hasOwnProperty.call(ROLE_CREATURES, role);+const hash = (s: string) => {let h = 0;for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) | 0;@@ -329,7 +334,7 @@ function renderCreature(c: Creature) {}export const ModernAvatar: React.FC<ModernAvatarProps> = ({- role = 'default',+ role,name = '',size = 36,status,@@ -344,7 +349,9 @@ export const ModernAvatar: React.FC<ModernAvatarProps> = ({const notices = useKwantNotices(kwantId);const moodText = moodLabel(mood, name, restingUntilOf(kwantId));const label = moodText ?? (shown ? statusText(name, shown) : name ? (working ? `${name} is working…` : name) : undefined);- const creature = pickCreature(role, name);+ // Same bot, same look in every place: re-reads when the app's bot list changes.+ useSyncExternalStore(subscribeBotLooks, botLooksRev, botLooksRev);+ const creature = pickCreature(avatarStyleFor(role, kwantId, name, isStyleId) ?? 'default', name);// Dev mode: every bot avatar of 28 px and up wears the hard hat (top-left, clear of the status dot).const devOn = useDevModeOn();const hat = devOn && !!kwantId && size >= 28;diff --git a/frontend/tests/botLooks.test.mjs b/frontend/tests/botLooks.test.mjsnew file mode 100644index 00000000..3978ab05--- /dev/null+++ b/frontend/tests/botLooks.test.mjs@@ -0,0 +1,83 @@+// One look per bot: an avatar that only knows a bot's title or name draws the bot's saved style,+// so the sidebar, the Ask chips, chat replies and the working line all show the same creature.+// (Oleks, Nov 10: TechLead from the coding team was violet in the Ask chips, cyan in its replies.)+import assert from 'node:assert/strict';+import test from 'node:test';+import { loadBundled } from './bundle.mjs';++const s = await loadBundled(`+ import React from 'react';+ import { renderToStaticMarkup } from 'react-dom/server.browser';+ import { ModernAvatar } from './src/components/ModernAvatar';+ export * from './src/botLooks';+ // Only the drawing: the wrapper's title and data attributes don't matter here.+ export const look = (p) => {+ const html = renderToStaticMarkup(React.createElement(ModernAvatar, { size: 36, ...p }));+ return html.slice(html.indexOf('<svg'), html.indexOf('</svg>'));+ };+`);++// The coding team's lead: saved as Architect, while its name hints at the Tech Lead look.+const team = [+ { id: 'k-lead', name: 'TechLead', avatar_sprite: 'architect' },+ { id: 'k-fe', name: 'Frontend', avatar_sprite: 'tech_lead' },+ { id: 'k-be', name: 'Backend', avatar_sprite: 'devops' },+];++test('before the bot list loads, a title or name hint draws as it always did', () => {+ s.setKnownBots([]);+ assert.notEqual(s.look({ role: 'Tech Lead', name: 'TechLead' }), s.look({ role: 'architect', name: 'TechLead' }));+ assert.equal(s.look({ role: 'Tech Lead', name: 'TechLead' }), s.look({ role: 'tech_lead', name: 'TechLead' }));+});++test('every place draws the saved style: by style, by title with an id, by name alone', () => {+ s.setKnownBots(team);+ for (const b of team) {+ const saved = s.look({ role: b.avatar_sprite, name: b.name, kwantId: b.id }); // sidebar, Ask chips+ assert.equal(s.look({ role: 'Tech Lead', name: b.name, kwantId: b.id }), saved, `${b.name}: reply with a title`);+ assert.equal(s.look({ role: b.name, name: b.name }), saved, `${b.name}: working line, name only`);+ assert.equal(s.look({ name: b.name }), saved, `${b.name}: no role at all`);+ assert.equal(s.look({ role: b.name.toLowerCase(), name: b.name.toUpperCase() }), saved, `${b.name}: any case`);+ }+});++test('a picked style still wins, so previews show the choice and not the saved look', () => {+ s.setKnownBots(team);+ assert.equal(s.avatarStyleFor('qa', 'k-lead', 'TechLead', (r) => r === 'qa'), 'qa');+ assert.notEqual(s.look({ role: 'qa', name: 'TechLead', kwantId: 'k-lead' }), s.look({ role: 'architect', name: 'TechLead' }));+ // The person is never drawn as a bot that shares their name.+ assert.equal(s.look({ role: 'user', name: 'TechLead' }), s.look({ role: 'user', name: 'Someone' }));+});++test('a name two bots share is not guessed; the id still finds each one', () => {+ s.setKnownBots([+ { id: 'a', name: 'Helper', avatar_sprite: 'chef' },+ { id: 'b', name: 'helper', avatar_sprite: 'qa' },+ { id: 'c', name: 'Twin', avatar_sprite: 'qa' },+ { id: 'd', name: 'Twin', avatar_sprite: 'qa' },+ ]);+ assert.equal(s.savedStyleOf(undefined, 'Helper'), undefined);+ assert.equal(s.savedStyleOf('a', 'Helper'), 'chef');+ assert.equal(s.savedStyleOf('b', 'Helper'), 'qa');+ // Same name, same style: no conflict to avoid.+ assert.equal(s.savedStyleOf(undefined, 'Twin'), 'qa');+ assert.equal(s.avatarStyleFor('Helper Bot', undefined, 'Helper', () => false), 'Helper Bot');+});++test('a bot with no saved style keeps its hint, and an unknown id falls back to the name', () => {+ s.setKnownBots([{ id: 'x', name: 'Plain', avatar_sprite: '' }, { id: 'y', name: 'Named', avatar_sprite: 'writer' }]);+ assert.equal(s.avatarStyleFor('Plain', 'x', 'Plain', () => false), 'Plain');+ assert.equal(s.avatarStyleFor('Named', 'gone', 'Named', () => false), 'writer');+});++test('changes reach subscribers', () => {+ let n = 0;+ const off = s.subscribeBotLooks(() => { n += 1; });+ const before = s.botLooksRev();+ s.setKnownBots(team);+ assert.equal(n, 1);+ assert.ok(s.botLooksRev() > before);+ off();+ s.setKnownBots([]);+ assert.equal(n, 1);+});