diff --git a/CHANGELOG.md b/CHANGELOG.md index 19a4f200..ded79881 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Bot avatars show what their bot is doing, using `open-bot-avatar` 0.3.0: three hopping dots while it thinks, bent over the work while it writes, searches, remembers, or uses tools, an exclamation mark while it retries, a short celebration when a reply finishes, and waiting in a group chat while it answers next. Live avatars ease from one state to the next instead of restarting. The `/avatars` page previews every state. Paused bots and disabled automations no longer show a dimmed, still avatar. - A bot's details in the web chat show **Usage**: tokens and cost for each model and tool its bot chat has used, with a total. Job panes now include tool costs, such as intent checks, in their cost. - When a bot's model can't read images and no vision model is set, the web chat warns as soon as an image is attached and links to the bot's profile and the vision model setting. The server refuses such a message before recording a turn, so the error shows once, names the bot, and the message goes back to the composer. `GET /v1/models` returns `images` for each model. - Pinned conversations are saved on the account, so every app shows the same pins in the same order. The session returns them as `pinnedConversations`, `update-user` saves the whole list (up to nine `agent:` or `group:` keys), and `/v1/events` sends a `pins` event to the account's open apps when they change. On the web, pin or unpin a bot chat or group chat from its **More** menu in the sidebar, and drag pins in the **Pinned** section to reorder them. diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index a7e7313c..00da6394 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -2,7 +2,7 @@ ## Open Bot Avatar -Web avatars use `open-bot-avatar` 0.2.0, by Dominik Scholz, under the MIT License. The generation prompt in `apps/web/features/avatars/generate-avatar-prompt.txt` is copied from `DominikScholz/open-bot-avatar`, commit `386cd20258ae48f2e75345ce3c05e046e04d6ad7`, `skills/generate-avatar/references/system-prompt.md`. +Web avatars use `open-bot-avatar` 0.3.0, by Dominik Scholz, under the MIT License. The generation prompt in `apps/web/features/avatars/generate-avatar-prompt.txt` is copied from `DominikScholz/open-bot-avatar`, commit `386cd20258ae48f2e75345ce3c05e046e04d6ad7`, `skills/generate-avatar/references/system-prompt.md`. ```text MIT License diff --git a/apps/docs/content/getting-started/first-bot.md b/apps/docs/content/getting-started/first-bot.md index 7e6ffc41..9e72536d 100644 --- a/apps/docs/content/getting-started/first-bot.md +++ b/apps/docs/content/getting-started/first-bot.md @@ -31,9 +31,15 @@ JSON response into **Import**. The copied prompt includes your description. Crumble doesn't call a model from the avatar editor. A bot can also suggest its own definition in chat for you to import. -The `/avatars` page previews idle, working, and sleeping states. Select a bot -and click **Save avatar to bot** to apply your choice. Sleeping avatars use a -dimmed still image. Live avatars respect your system's reduced-motion setting. +The `/avatars` page previews the idle, thinking, working, waiting, blocked, +and done states. Select a bot and click **Save avatar to bot** to apply your +choice. Live avatars respect your system's reduced-motion setting. + +An avatar shows what its bot is doing. While the bot thinks, its body turns +into three hopping dots. While it writes, searches, remembers, or uses tools, it +bends over the work. When it retries, it shows an exclamation mark. When a reply +finishes, it celebrates for a few seconds. In a group chat, a bot that answers +next waits its turn. Bots without a saved definition get a fresh gallery avatar with default colors. The bot ID selects the shape; previous avatar shapes and colors don't affect it. diff --git a/apps/server/package.json b/apps/server/package.json index 7136b54d..37843cec 100644 --- a/apps/server/package.json +++ b/apps/server/package.json @@ -38,7 +38,7 @@ "effect": "4.0.6", "hono": "4.13.13", "microsandbox": "0.7.8", - "open-bot-avatar": "0.2.0", + "open-bot-avatar": "0.3.0", "pele": "1.9.0", "pkijs": "4.0.0", "railway": "3.16.0", diff --git a/apps/web/components/bot-avatar.test.tsx b/apps/web/components/bot-avatar.test.tsx index 6bbd73cf..afc47363 100644 --- a/apps/web/components/bot-avatar.test.tsx +++ b/apps/web/components/bot-avatar.test.tsx @@ -3,7 +3,7 @@ import { Window } from "happy-dom"; import { act } from "react"; import { defaultAvatarDefinition } from "@crumble/server/contract/avatar"; -test("static and live avatars update geometry, moods and colors, stop sleeping, and honor reduced motion", async () => { +test("static and live avatars update geometry, moods and colors, and honor reduced motion", async () => { const window = new Window(); const globals = Object.getOwnPropertyDescriptors(globalThis); const frames = new Map(); @@ -82,17 +82,6 @@ test("static and live avatars update geometry, moods and colors, stop sleeping, for (const fn of changes) fn(); await tick(); expect(frames.size).toBe(1); - await act(async () => - root.render(), - ); - expect(container.querySelector("[data-avatar-scene]")).toBeNull(); - expect(container.querySelector("img")!.src).toBe(idle); - expect( - Number( - container.querySelector("[data-state]")!.style.opacity, - ), - ).toBeLessThan(1); - expect(frames.size).toBe(0); const next = { ...bot, diff --git a/apps/web/components/bot-avatar.tsx b/apps/web/components/bot-avatar.tsx index 248eef05..c227c91e 100644 --- a/apps/web/components/bot-avatar.tsx +++ b/apps/web/components/bot-avatar.tsx @@ -1,10 +1,10 @@ import { type CSSProperties, useLayoutEffect, useMemo, useRef } from "react"; -import { renderAvatarSvg } from "open-bot-avatar"; -import { mountAvatar } from "open-bot-avatar/dom"; +import { type AvatarState, renderAvatarSvg } from "open-bot-avatar"; +import { type MountedAvatar, mountAvatar } from "open-bot-avatar/dom"; import type { BotAvatarDefinition } from "@crumble/server/contract/avatar"; import { cn } from "@/lib/utils"; -export type BotState = "idle" | "working" | "sleeping"; +export type BotState = AvatarState; export type AvatarBot = { id: string; avatar: BotAvatarDefinition }; @@ -20,23 +20,35 @@ export function botStyle(color: string) { function useLiveAvatar( still: boolean, look: BotAvatarDefinition, - mood: "thinking" | "idle", + state: AvatarState, ) { const host = useRef(null); + const avatar = useRef(null); + const latest = useRef(state); useLayoutEffect(() => { if (still || !host.current) return; - const avatar = mountAvatar(host.current, { + const mounted = mountAvatar(host.current, { ...look, - mood, + state: latest.current, reducedMotion: "system", followPointer: true, label: "", }); - return () => avatar.destroy(); - }, [still, look, mood]); + avatar.current = mounted; + + return () => { + mounted.destroy(); + avatar.current = null; + }; + }, [still, look]); + + useLayoutEffect(() => { + latest.current = state; + avatar.current?.update({ state }); + }, [state]); return host; } @@ -57,9 +69,8 @@ export function BotAvatar({ }) { const look = bot.avatar; - const still = !live || state === "sleeping"; - const mood = state === "working" ? "thinking" : "idle"; - const host = useLiveAvatar(still, look, mood); + const still = !live; + const host = useLiveAvatar(still, look, state); const svg = useMemo( () => @@ -67,11 +78,11 @@ export function BotAvatar({ ? renderAvatarSvg(look.definition, { color: look.color, eyeColor: look.eyeColor, - motion: mood, + state, reducedMotion: true, }) : "", - [still, look, mood], + [still, look, state], ); return ( @@ -79,11 +90,7 @@ export function BotAvatar({ aria-hidden="true" data-state={state} className={cn("inline-block shrink-0", className)} - style={{ - width: size, - height: size, - opacity: state === "sleeping" ? 0.45 : 1, - }} + style={{ width: size, height: size }} > {still ? ( - +
{schedule.name} diff --git a/apps/web/features/avatars/avatars.tsx b/apps/web/features/avatars/avatars.tsx index ada94b80..dff54c7d 100644 --- a/apps/web/features/avatars/avatars.tsx +++ b/apps/web/features/avatars/avatars.tsx @@ -14,7 +14,8 @@ import { invalidateBots, } from "@/features/bots/api"; import { send } from "@/lib/api"; -import { BotAvatar, type BotState } from "@/components/bot-avatar"; +import { STATES } from "open-bot-avatar"; +import { BotAvatar } from "@/components/bot-avatar"; import { AvatarEditor } from "./avatar-editor"; function useAvatarDraft() { @@ -50,15 +51,15 @@ function useAvatarDraft() { function AvatarStatePreviews({ value }: { value: BotAvatarDefinition }) { return ( -
- {(["idle", "working", "sleeping"] satisfies BotState[]).map((state) => ( +
+ {STATES.map((state) => (
diff --git a/apps/web/features/bot-chat/avatar.tsx b/apps/web/features/bot-chat/avatar.tsx index f683a7e8..fdf06081 100644 --- a/apps/web/features/bot-chat/avatar.tsx +++ b/apps/web/features/bot-chat/avatar.tsx @@ -1,32 +1,54 @@ +import { useEffect, useRef, useState } from "react"; import { type AvatarBot, type BotState, BotAvatar, } from "@/components/bot-avatar"; -import type { Bot } from "@/features/bots/api"; -import { isBusy, useWorking } from "./working"; +import { type Working, isBusy, useWorking, workingAvatar } from "./working"; -const avatarState = ( - enabled: boolean, - working: ReturnType, -): BotState => { - if (!enabled) return "sleeping"; +const DONE_MS = 4000; - return isBusy(working) ? "working" : "idle"; -}; +function useJustFinished(working: Working) { + const [finished, setFinished] = useState(false); + const previous = useRef(working); + + useEffect(() => { + const was = previous.current; + + previous.current = working; + + if (!isBusy(was) || working !== "idle") { + setFinished(false); + + return; + } + + setFinished(true); + + const timer = setTimeout(() => setFinished(false), DONE_MS); + + return () => clearTimeout(timer); + }, [working]); + + return finished; +} + +const avatarState = (working: Working, finished: boolean): BotState => + finished ? "done" : workingAvatar(working); export function LiveBotAvatar({ bot, size, className, }: { - bot: AvatarBot & Pick; + bot: AvatarBot; size: number; className?: string; }) { const working = useWorking(bot.id); + const finished = useJustFinished(working); - const state = avatarState(bot.enabled, working); + const state = avatarState(working, finished); return ( - + {workingLabel[activity]}… diff --git a/apps/web/features/bot-chat/working.ts b/apps/web/features/bot-chat/working.ts index c6af91b4..dcc2fb7c 100644 --- a/apps/web/features/bot-chat/working.ts +++ b/apps/web/features/bot-chat/working.ts @@ -2,6 +2,7 @@ import { useSyncExternalStore } from "react"; import { type ChatSnapshot, chatQuery } from "@/features/chat/api"; import { queryClient } from "@/lib/api"; import { type Bot } from "@/features/bots/api"; +import type { BotState } from "@/components/bot-avatar"; export type Working = ChatSnapshot["working"]; @@ -19,6 +20,14 @@ export function useWorking(id: string) { export const isBusy = (working: Working) => working !== "idle" && working !== "failed"; +export const workingAvatar = (working: Working): BotState => { + if (working === "thinking") return "thinking"; + + if (working === "struggling") return "blocked"; + + return isBusy(working) ? "working" : "idle"; +}; + export const workingLabel: Record = { idle: "Working", thinking: "Thinking", diff --git a/apps/web/features/group-chats/groups.tsx b/apps/web/features/group-chats/groups.tsx index e356ed19..3cd3d733 100644 --- a/apps/web/features/group-chats/groups.tsx +++ b/apps/web/features/group-chats/groups.tsx @@ -35,7 +35,7 @@ import { } from "./api"; import type { Message } from "@/features/chat/api"; import { call, queryClient, send } from "@/lib/api"; -import { BotAvatar, botColor } from "@/components/bot-avatar"; +import { BotAvatar, type BotState, botColor } from "@/components/bot-avatar"; import { ConfirmDeleteContent, MobileSidebarTrigger } from "@/components/page"; import { ConversationEmpty, @@ -78,6 +78,12 @@ const MEMBER_STATUS = { idle: "is here", }; +const memberAvatar = (status: keyof typeof MEMBER_STATUS): BotState => { + if (status === "running") return "working"; + + return status === "queued" ? "waiting" : "idle"; +}; + const groupConversation = ( id: string, ): ConversationTarget => { @@ -124,7 +130,7 @@ function GroupHeader({ diff --git a/apps/web/package.json b/apps/web/package.json index 5aa9c452..52d26fa6 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -25,7 +25,7 @@ "cmdk": "1.1.1", "cn": "0.5.0", "cuelume": "0.2.8", - "open-bot-avatar": "0.2.0", + "open-bot-avatar": "0.3.0", "pele": "1.9.0", "react": "19.3.1", "react-dom": "19.3.1", diff --git a/bun.lock b/bun.lock index 3af34973..495ded49 100644 --- a/bun.lock +++ b/bun.lock @@ -45,7 +45,7 @@ "effect": "4.0.6", "hono": "4.13.13", "microsandbox": "0.7.8", - "open-bot-avatar": "0.2.0", + "open-bot-avatar": "0.3.0", "pele": "1.9.0", "pkijs": "4.0.0", "railway": "3.16.0", @@ -81,7 +81,7 @@ "cmdk": "1.1.1", "cn": "0.5.0", "cuelume": "0.2.8", - "open-bot-avatar": "0.2.0", + "open-bot-avatar": "0.3.0", "pele": "1.9.0", "react": "19.3.1", "react-dom": "19.3.1", @@ -883,7 +883,7 @@ "once": ["once@1.4.0", "", { "dependencies": { "wrappy": "1" } }, "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w=="], - "open-bot-avatar": ["open-bot-avatar@0.2.0", "", {}, "sha512-NHiTNWkZTCt1BA+x3l6OTqN7qAAaRfk4z6/kwpiIi9/4qHyzFb0syxJEw5PBbk6MbPhBHEltCD0Q/QuY4nxucg=="], + "open-bot-avatar": ["open-bot-avatar@0.3.0", "", {}, "sha512-3P0o8eiClHHiLr7lIFZE/WxUtqHLjVb0xQ0PqYzA48XG/oQlWyeIdcY0egK6H8AgFuoY1aKVhc8QKSaj4z5ZOw=="], "openai": ["openai@7.19.0", "", { "peerDependencies": { "@aws-sdk/credential-provider-node": ">=3.972.0 <4", "@smithy/hash-node": ">=4.3.0 <5", "@smithy/signature-v4": ">=5.4.0 <6", "undici": ">=5 <9", "ws": "^8.21.0", "zod": "^3.25 || ^4.0" }, "optionalPeers": ["@aws-sdk/credential-provider-node", "@smithy/hash-node", "@smithy/signature-v4", "undici", "ws", "zod"] }, "sha512-MX2s3u2L5racTO0CC/SWpCOasJQBCJrqLKXK+l82cAhdeF8mPMBEe/gxMm0ZFa2xpKpOFLRjxv5afYEZbBXmbQ=="],