@kodax-ai/botui 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +140 -113
  2. package/core/identity.js +46 -0
  3. package/{themes/solids → core}/motion.js +114 -114
  4. package/core/registry.js +26 -18
  5. package/docs/usage.md +202 -0
  6. package/element.js +5 -0
  7. package/index.d.ts +2 -0
  8. package/index.js +11 -5
  9. package/package.json +38 -2
  10. package/themes/caret/README.md +43 -0
  11. package/themes/caret/avatar.js +10 -0
  12. package/themes/caret/engine.js +143 -0
  13. package/themes/caret/index.d.ts +24 -0
  14. package/themes/caret/index.js +35 -0
  15. package/themes/critter/README.md +43 -0
  16. package/themes/critter/avatar.js +12 -0
  17. package/themes/critter/engine.js +113 -0
  18. package/themes/critter/index.d.ts +28 -0
  19. package/themes/critter/index.js +35 -0
  20. package/themes/doodle/README.md +43 -0
  21. package/themes/doodle/avatar.js +10 -0
  22. package/themes/doodle/engine.js +149 -0
  23. package/themes/doodle/index.d.ts +24 -0
  24. package/themes/doodle/index.js +36 -0
  25. package/themes/dots/README.md +42 -0
  26. package/themes/dots/avatar.js +15 -0
  27. package/themes/dots/engine.js +179 -0
  28. package/themes/dots/index.d.ts +24 -0
  29. package/themes/dots/index.js +35 -0
  30. package/themes/kaomoji/README.md +42 -0
  31. package/themes/kaomoji/avatar.js +10 -0
  32. package/themes/kaomoji/engine.js +110 -0
  33. package/themes/kaomoji/index.d.ts +24 -0
  34. package/themes/kaomoji/index.js +35 -0
  35. package/themes/liquid/README.md +47 -0
  36. package/themes/liquid/avatar.js +10 -0
  37. package/themes/liquid/engine.js +161 -0
  38. package/themes/liquid/index.d.ts +24 -0
  39. package/themes/liquid/index.js +35 -0
  40. package/themes/matrix/README.md +48 -0
  41. package/themes/matrix/avatar.js +227 -0
  42. package/themes/matrix/engine.js +479 -0
  43. package/themes/matrix/index.d.ts +47 -0
  44. package/themes/matrix/index.js +34 -0
  45. package/themes/orb/README.md +44 -0
  46. package/themes/orb/avatar.js +10 -0
  47. package/themes/orb/engine.js +130 -0
  48. package/themes/orb/index.d.ts +24 -0
  49. package/themes/orb/index.js +36 -0
  50. package/themes/solids/README.md +2 -2
  51. package/themes/solids/avatar.js +561 -579
  52. package/themes/solids/face.js +52 -56
  53. package/themes/solids/index.js +34 -28
package/core/registry.js CHANGED
@@ -1,18 +1,26 @@
1
- // Themes register here so a product can pick one by name.
2
- //
3
- // A theme is {name, zh, create(options), catalog()}. `create` returns an avatar
4
- // with at least: el, update, setState, play, poke, express, look, gaze, blink,
5
- // step, restart, suspend, resume, toSVG, dispose.
6
-
7
- const themes = new Map();
8
-
9
- export function registerTheme(theme) { themes.set(theme.name, theme); return theme; }
10
- export const getTheme = name => themes.get(name) || null;
11
- export const themeNames = () => [...themes.keys()];
12
-
13
- /** Create an avatar. `theme` picks the theme by name; without it the first registered theme is used. */
14
- export function createAvatar({theme, ...options} = {}) {
15
- const chosen = themes.get(theme) || themes.values().next().value;
16
- if (!chosen) throw new Error('BotUI: no theme is registered');
17
- return chosen.create(options);
18
- }
1
+ // Themes register here so a product can pick one by name.
2
+ //
3
+ // A theme is {name, zh, create(options), catalog()}. `create` returns an avatar
4
+ // with at least: el, update, setState, play, poke, express, look, gaze, blink,
5
+ // step, restart, suspend, resume, toSVG, dispose.
6
+ //
7
+ // Each theme registers itself when its module is imported, so a page only loads
8
+ // the themes it imports. The package entry imports the solids and makes them the
9
+ // default; without a default, the first registered theme is used.
10
+
11
+ const themes = new Map();
12
+ let fallback = null;
13
+
14
+ export function registerTheme(theme) { themes.set(theme.name, theme); return theme; }
15
+ /** Make a registered theme the one used when createAvatar is given no `theme`. */
16
+ export function setDefaultTheme(name) { if (themes.has(name)) fallback = name; return fallback; }
17
+ export const getTheme = name => themes.get(name) || null;
18
+ export const themeNames = () => [...themes.keys()];
19
+
20
+ /** Create an avatar. `theme` picks the theme by name; without it the default theme is used. */
21
+ export function createAvatar({theme, ...options} = {}) {
22
+ if (theme && !themes.has(theme) && typeof console !== 'undefined') console.warn(`BotUI: theme "${theme}" is not loaded; import '@kodax-ai/botui/themes/${theme}' first. Using the default.`);
23
+ const chosen = themes.get(theme) || themes.get(fallback) || themes.values().next().value;
24
+ if (!chosen) throw new Error('BotUI: no theme is registered');
25
+ return chosen.create(options);
26
+ }
package/docs/usage.md ADDED
@@ -0,0 +1,202 @@
1
+ # BotUI 接入说明
2
+
3
+ 给在自己的产品里使用 `@kodax-ai/botui` 的项目看。这份文件随 npm 包发布,在 `node_modules/@kodax-ai/botui/docs/usage.md`。每个主题的完整选项和细节见各自目录的 README(同样在包里:`themes/<主题名>/README.md`)。
4
+
5
+ BotUI 源码公开,但不是开源软件,使用前请先看最后一节“许可和水印”。
6
+
7
+ ## 1. 安装
8
+
9
+ ```bash
10
+ npm install @kodax-ai/botui
11
+ ```
12
+
13
+ 零依赖,原生 ES 模块,不需要构建步骤;任何支持 ES 模块的打包工具(Vite、webpack、Rollup、esbuild)都能直接用。TypeScript 类型随包提供。
14
+
15
+ 不用 npm 时可以从 CDN 引用,见第 5 节。
16
+
17
+ ## 2. 最小例子
18
+
19
+ ```js
20
+ import {createAvatar} from '@kodax-ai/botui';
21
+
22
+ const bot = createAvatar({identity: 'research-bot', size: 36});
23
+ document.querySelector('#sidebar').append(bot.el);
24
+
25
+ bot.setState('thinking'); // 开始干活
26
+ bot.setState('finished'); // 干完了
27
+ ```
28
+
29
+ - `identity` 是 Bot 的 ID。外形、颜色、眼型都由它稳定决定:同一个 ID 在任何页面、任何时候都是同一个样子,不同的 Bot 自然区分开。不想让它自动决定时,用 `shape`、`color`、`eyes` 指定。
30
+ - `bot.el` 是一个元素,放进页面就开始动;所有头像共用一个帧循环,页面里放几十个也没问题。
31
+ - 不再需要时调用 `bot.dispose()`。
32
+
33
+ ## 3. 选主题
34
+
35
+ 一共 9 个主题,选项、方法、状态、动作、眼神都一样,换主题不用改其他代码。
36
+
37
+ | 主题 | `theme` | 样子 |
38
+ |---|---|---|
39
+ | 立体家族 | `solids` | 带眼睛的立体(默认,36 种外形) |
40
+ | 点阵 | `matrix` | 彩色方块上的 5×5 点阵屏 |
41
+ | 活点 | `dots` | 五颗带弹簧的点,拉长、分裂、合并成表情 |
42
+ | 字符脸 | `kaomoji` | 打出来的字符脸 `o_o` `^_^` |
43
+ | 光标 | `caret` | 一个文本光标,弯成各种字形 |
44
+ | 光晕 | `orb` | 流动的柔光,加立体家族的眼睛 |
45
+ | 液面 | `liquid` | 玻璃杯里的液体,液面就是脸 |
46
+ | 像素生物 | `critter` | 像素画小生物,猫耳 / 熊耳 / 兔耳 / 天线 |
47
+ | 手绘 | `doodle` | 抖动的墨线手绘,带嘴 |
48
+
49
+ 颜色和眼型在所有主题里由同一套算法决定,所以同一个 Bot 换主题后还是同一种颜色、同一种眼睛。
50
+
51
+ **只用一个主题**:直接从主题入口引入,最省体积。
52
+
53
+ ```js
54
+ import {createAvatar} from '@kodax-ai/botui/themes/kaomoji';
55
+ const bot = createAvatar({identity: 'research-bot'});
56
+ ```
57
+
58
+ **一个产品里用几个主题,或让用户切换**:总入口只带默认的立体家族,其他主题引入一次即注册,之后用 `theme` 选择。
59
+
60
+ ```js
61
+ import {createAvatar} from '@kodax-ai/botui';
62
+ import '@kodax-ai/botui/themes/kaomoji';
63
+ import '@kodax-ai/botui/themes/critter';
64
+
65
+ createAvatar({theme: 'kaomoji', identity: 'a'});
66
+ createAvatar({theme: 'critter', identity: 'b'});
67
+ ```
68
+
69
+ `theme` 写了一个没引入的主题时,会在控制台警告并退回默认主题,不会报错。想改默认主题:`setDefaultTheme('kaomoji')`(该主题必须已引入)。`themeNames()` 列出已注册的主题。
70
+
71
+ ## 4. 在框架里用
72
+
73
+ 头像是普通的 DOM 元素,框架只需要在挂载时放进去、卸载时 `dispose()`。
74
+
75
+ ### React
76
+
77
+ ```jsx
78
+ import {useEffect, useRef} from 'react';
79
+ import {createAvatar} from '@kodax-ai/botui';
80
+
81
+ export function BotAvatar({identity, state = 'idle', size = 36}) {
82
+ const host = useRef(null), bot = useRef(null);
83
+ useEffect(() => {
84
+ bot.current = createAvatar({identity, size, state});
85
+ host.current.append(bot.current.el);
86
+ return () => bot.current.dispose();
87
+ }, [identity, size]);
88
+ useEffect(() => { bot.current?.setState(state); }, [state]);
89
+ return <span ref={host} style={{display: 'inline-block', width: size, height: size}} />;
90
+ }
91
+ ```
92
+
93
+ ### Vue 3
94
+
95
+ ```vue
96
+ <script setup>
97
+ import {onMounted, onBeforeUnmount, ref, watch} from 'vue';
98
+ import {createAvatar} from '@kodax-ai/botui';
99
+
100
+ const props = defineProps({identity: String, state: {type: String, default: 'idle'}, size: {type: Number, default: 36}});
101
+ const host = ref(null);
102
+ let bot;
103
+ onMounted(() => { bot = createAvatar({identity: props.identity, size: props.size, state: props.state}); host.value.append(bot.el); });
104
+ onBeforeUnmount(() => bot?.dispose());
105
+ watch(() => props.state, state => bot?.setState(state));
106
+ watch(() => [props.identity, props.size], ([identity, size]) => bot?.update({identity, size}));
107
+ </script>
108
+
109
+ <template><span ref="host" :style="{display: 'inline-block', width: size + 'px', height: size + 'px'}" /></template>
110
+ ```
111
+
112
+ ### 自定义元素(任何框架或纯 HTML)
113
+
114
+ ```js
115
+ import '@kodax-ai/botui/element.js';
116
+ ```
117
+
118
+ ```html
119
+ <bot-avatar identity="research-bot" size="36" state="thinking"></bot-avatar>
120
+ ```
121
+
122
+ 属性和选项一一对应(`eye-color`、`max-frame-rate` 这类用短横线写法)。布尔属性存在即打开:`static`、`emphasized`、`paused`、`inert`(不响应交互)、`untracked`(不跟随指针)。改属性立即生效,头像对象在 `element.avatar` 上。
123
+
124
+ 服务端渲染:模块可以在 Node 里导入(导入时不碰 DOM),但 `createAvatar` 需要浏览器 DOM,请只在客户端创建(React 的 `useEffect`、Vue 的 `onMounted` 里已经是这样)。
125
+
126
+ ## 5. 不用构建:CDN
127
+
128
+ ```html
129
+ <script type="module" src="https://cdn.jsdelivr.net/npm/@kodax-ai/botui@0.1/element.js"></script>
130
+ <bot-avatar identity="research-bot" size="36" state="thinking"></bot-avatar>
131
+ ```
132
+
133
+ 用默认以外的主题时,**先**加载主题脚本,再加载 `element.js`:
134
+
135
+ ```html
136
+ <script type="module" src="https://cdn.jsdelivr.net/npm/@kodax-ai/botui@0.1/themes/kaomoji/index.js"></script>
137
+ <script type="module" src="https://cdn.jsdelivr.net/npm/@kodax-ai/botui@0.1/element.js"></script>
138
+ <bot-avatar theme="kaomoji" identity="research-bot"></bot-avatar>
139
+ ```
140
+
141
+ ## 6. 把产品状态映射到头像
142
+
143
+ 最常用的是 `setState`。状态是持续的,直到换下一个。55 个状态分五组,完整列表在 `STATES`(每项带中文名 `zh` 和分组 `group`):
144
+
145
+ | 组 | 例子 | 什么时候用 |
146
+ |---|---|---|
147
+ | 工作流程 | `idle` `listening` `thinking` `streaming` `searching` `reading` `working` `coding` `running` `computing` `controlling` `asking` `blocked` `finished` `error` `sleeping` `waking` | 对应 Agent 的运行阶段 |
148
+ | 情绪反应 | `happy` `excited` `proud` `curious` `confused` `sad` `scared` `celebrating` `approving` `loving` 等 18 个 | 对用户操作的反应 |
149
+ | 信号 | `progress` `loading` `spawning` `dictating` `sending` `receiving` `uploading` `writing` `alerting` `powering-down` 等 13 个 | 表示一个具体的过程 |
150
+ | 产品场景 | `humming` `notifying` `dragging` `greeting` | 后台待命、有新消息、被拖动、欢迎 |
151
+ | 展示 | `logo` `showcase` `turntable` | 官网、关于页 |
152
+
153
+ 一个聊天 / Agent 产品的典型映射:
154
+
155
+ ```js
156
+ const MAP = {
157
+ queued: 'thinking', planning: 'thinking', toolCall: 'working', browsing: 'searching',
158
+ editingCode: 'coding', answering: 'streaming', needsInput: 'asking', waitingApproval: 'asking',
159
+ rateLimited: 'blocked', done: 'finished', failed: 'error', idleLong: 'sleeping',
160
+ };
161
+ bot.setState(MAP[event.type] ?? 'idle');
162
+ ```
163
+
164
+ 另外接受几个常见别名:`waiting` → `asking`、`pending` → `thinking`、`done` → `finished`、`failed` → `error`、`celebrate` → `celebrating`、`love` → `loving`。不认识的名字会被忽略,不会报错。
165
+
166
+ 其他常用方法(`STATES`、`ALIASES`、`setAttention` 从总入口导入;`GESTURES`、`EXPRESSIONS`、`COLORS` 这些表从主题入口导入,如 `@kodax-ai/botui/themes/solids`):
167
+
168
+ | 方法 | 作用 |
169
+ |---|---|
170
+ | `bot.update({progress: 0.4})` 配合 `setState('progress')` | 显示具体进度(0–1);不给进度时循环播放 |
171
+ | `bot.play('nod')` | 一次性动作(点头、摇头、挥手、跳等 17 个,见 `GESTURES`),叠加在当前状态上 |
172
+ | `bot.express('happy', 1500)` | 临时换一个眼神(21 个,见 `EXPRESSIONS`),毫秒后恢复 |
173
+ | `bot.nudge()` | 轻轻点一下头,适合用户每次按键时调用 |
174
+ | `bot.lookAt(x, y)` / `bot.look({x, y})` | 看向页面上的一点 / 一个方向;传 `null` 交还给指针 |
175
+ | `setAttention({x, y})` | 让页面上所有头像一起看向一点(比如输入框) |
176
+ | `bot.update({...})` | 改任意选项:`size`、`color`、`state`、`emphasized`、`identity`…… |
177
+ | `bot.toSVG()` | 当前帧导出成独立 SVG 字符串,用于 favicon、通知图标、导出 |
178
+
179
+ 常用选项:`size`(像素)、`scheme: 'light' | 'dark' | 'auto'`(所在背景,`auto` 跟随系统)、`emphasized`(标记当前正在说话的 Bot)、`label`(无障碍名称,不给时头像是装饰性的)、`static`(只显示静止姿态;系统开启“减少动态效果”时自动如此)、`interactive`、`track`。
180
+
181
+ ## 7. 包体积
182
+
183
+ 总入口只带默认主题,其他主题按需引入,没引入的主题不会进你的包。下面是各入口实际加载的全部源码(未压缩 / gzip,2026-11-28 的版本):
184
+
185
+ | 引入 | 未压缩 | gzip |
186
+ |---|---|---|
187
+ | `@kodax-ai/botui`(总入口,含立体家族) | 85 kB | 28 kB |
188
+ | `@kodax-ai/botui/themes/matrix` | 69 kB | 23 kB |
189
+ | 其他任一单独主题(dots、kaomoji、caret、orb、liquid、critter、doodle) | 76–83 kB | 25–27 kB |
190
+ | 总入口 + 任一其他主题 | 约 137–145 kB | 约 44–47 kB |
191
+ | 全部 9 个主题 + `element.js` | 211 kB | 63 kB |
192
+
193
+ 主题之间共享 `core/` 和点阵引擎,所以多引入一个主题只增加它自己的那一小部分。源码没有压缩,经过打包工具的压缩后还会更小。npm 包本身约 90 kB(解压后约 295 kB),包含全部主题。
194
+
195
+ ## 8. 许可和水印
196
+
197
+ BotUI 采用 [KodaX-AI BotUI License 1.0](../LICENSE),源码公开但不是开源软件。要点如下,以 LICENSE 原文为准:
198
+
199
+ - 任何使用都要保留 `LICENSE`、`NOTICE` 和代码、SVG 里的版权信息,并在产品的“关于”、文档或致谢处写明:`Bot avatars by BotUI © icetomoyo / KodaX-AI`。
200
+ - 个人、学习、非商业使用:免费,可以用 `watermark: false`(或属性 `no-watermark`)关掉水印。
201
+ - 商业或组织内使用(公司、学校、机构内部使用也算):免费,但必须保持水印开启、清晰可见、不被遮挡或改动。头像在 48 px 及以上时右下角显示 `KodaX-AI`,更小时不显示,但导出的 SVG 里始终带版权信息。
202
+ - 商用去水印需要与 KodaX-AI 另签书面授权,联系方式见 LICENSE。
package/element.js CHANGED
@@ -7,6 +7,11 @@
7
7
  // present: static, emphasized, paused, inert (no interaction), untracked (do
8
8
  // not follow the pointer), no-watermark (see LICENSE for when that is allowed).
9
9
  // The avatar itself is at `element.avatar`.
10
+ //
11
+ // The `theme` attribute picks a loaded theme. This file loads only the default
12
+ // (the solids); load any other theme with its own script first:
13
+ // <script type="module" src="https://cdn.jsdelivr.net/npm/@kodax-ai/botui@0.1/themes/dots/index.js"></script>
14
+ // <bot-avatar theme="dots" identity="research-bot"></bot-avatar>
10
15
 
11
16
  import {createAvatar} from './index.js';
12
17
 
package/index.d.ts CHANGED
@@ -100,6 +100,8 @@ export interface Theme {
100
100
 
101
101
  export function createAvatar(options?: AvatarOptions): Avatar;
102
102
  export function registerTheme(theme: Theme): Theme;
103
+ /** Make a registered theme the default for createAvatar without `theme`. Returns the default's name. */
104
+ export function setDefaultTheme(name: string): string | null;
103
105
  export function getTheme(name: string): Theme | null;
104
106
  export function themeNames(): string[];
105
107
  /** Make every avatar look at a client-space point, or pass null to stop. */
package/index.js CHANGED
@@ -5,13 +5,19 @@
5
5
  // sidebar.append(bot.el);
6
6
  // bot.setState('thinking');
7
7
  //
8
- // To ship a single theme, import it directly: '@kodax-ai/botui/themes/solids'.
8
+ // This entry brings only the default theme, the solids. Every other theme is
9
+ // loaded by importing it, which also registers it here by name:
10
+ //
11
+ // import '@kodax-ai/botui/themes/dots';
12
+ // createAvatar({theme: 'dots', identity: 'research-bot'});
13
+ //
14
+ // So a page only carries the themes it imports, however many there are.
9
15
 
10
- import {registerTheme} from './core/registry.js';
11
- import {theme as solids} from './themes/solids/index.js';
16
+ import {setDefaultTheme} from './core/registry.js';
17
+ import './themes/solids/index.js';
12
18
 
13
- registerTheme(solids);
19
+ setDefaultTheme('solids');
14
20
 
15
- export {createAvatar, registerTheme, getTheme, themeNames} from './core/registry.js';
21
+ export {createAvatar, registerTheme, setDefaultTheme, getTheme, themeNames} from './core/registry.js';
16
22
  export {STATES, STATE_GROUPS, ALIASES, resolveState} from './core/states.js';
17
23
  export {setAttention} from './core/core.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kodax-ai/botui",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "private": false,
5
5
  "description": "Animated Bot avatars for AI products. One core, one renderer per theme, no dependencies.",
6
6
  "license": "SEE LICENSE IN LICENSE",
@@ -47,12 +47,46 @@
47
47
  "types": "./index.d.ts",
48
48
  "default": "./themes/solids/index.js"
49
49
  },
50
+ "./themes/matrix": {
51
+ "types": "./themes/matrix/index.d.ts",
52
+ "default": "./themes/matrix/index.js"
53
+ },
54
+ "./themes/dots": {
55
+ "types": "./themes/dots/index.d.ts",
56
+ "default": "./themes/dots/index.js"
57
+ },
58
+ "./themes/kaomoji": {
59
+ "types": "./themes/kaomoji/index.d.ts",
60
+ "default": "./themes/kaomoji/index.js"
61
+ },
62
+ "./themes/caret": {
63
+ "types": "./themes/caret/index.d.ts",
64
+ "default": "./themes/caret/index.js"
65
+ },
66
+ "./themes/orb": {
67
+ "types": "./themes/orb/index.d.ts",
68
+ "default": "./themes/orb/index.js"
69
+ },
70
+ "./themes/liquid": {
71
+ "types": "./themes/liquid/index.d.ts",
72
+ "default": "./themes/liquid/index.js"
73
+ },
74
+ "./themes/critter": {
75
+ "types": "./themes/critter/index.d.ts",
76
+ "default": "./themes/critter/index.js"
77
+ },
78
+ "./themes/doodle": {
79
+ "types": "./themes/doodle/index.d.ts",
80
+ "default": "./themes/doodle/index.js"
81
+ },
50
82
  "./core": "./core/core.js",
51
83
  "./package.json": "./package.json"
52
84
  },
53
85
  "sideEffects": [
54
86
  "./index.js",
55
- "./element.js"
87
+ "./element.js",
88
+ "./core/core.js",
89
+ "./themes/*/index.js"
56
90
  ],
57
91
  "files": [
58
92
  "index.js",
@@ -60,8 +94,10 @@
60
94
  "element.js",
61
95
  "core",
62
96
  "themes/*/*.js",
97
+ "themes/*/*.d.ts",
63
98
  "!themes/*/playground.js",
64
99
  "themes/*/README.md",
100
+ "docs/usage.md",
65
101
  "LICENSE",
66
102
  "NOTICE"
67
103
  ]
@@ -0,0 +1,43 @@
1
+ # 光标 caret
2
+
3
+ 脸是一个文本光标,画在点阵主题的彩色方块上。空闲时像光标一样闪,一格一格跟着指针跳。每种表情都是同一笔弯成的字形(开心 `^`、闭眼 `_`、怀疑 `¬`、完成 ✓),弹簧带着它从一个字形弹到下一个。工作时像终端转圈,或者往右推、身后留下打好的字块。
4
+
5
+ 来自 avatar-lab 的草图方向 T(光标),2026-11-28 做成主题。
6
+
7
+ ## 用法
8
+
9
+ ```js
10
+ import {createAvatar} from '@kodax-ai/botui/themes/caret'; // 或相对路径 ./themes/caret/index.js
11
+
12
+ const bot = createAvatar({identity: 'research-bot', size: 36});
13
+ sidebar.append(bot.el);
14
+ bot.setState('thinking');
15
+ ```
16
+
17
+ 从总入口用:先 `import '@kodax-ai/botui/themes/caret'`,再 `createAvatar({theme: 'caret', ...})`。自定义元素:同样先加载主题脚本,再写 `<bot-avatar theme="caret" identity="research-bot"></bot-avatar>`。
18
+
19
+ ## 与点阵的关系
20
+
21
+ 方块、外形(圆角方、圆、六边形)、颜色、动作、记号、粒子、交互、水印都和 [点阵](../matrix/README.md) 一样,选项和方法也完全相同;同一个 Bot ID 在两个主题里是同一个外形、颜色和眼型。不同的只有脸:
22
+
23
+ | 项 | 光标 |
24
+ |---|---|
25
+ | 眼型 | `dot` 块光标、`bar` 竖线光标、`tall` 细长竖线 |
26
+ | 眼神 | 21 种都是一笔四点的字形:`happy` ^、`squint` >、`sad` ⌒、`skeptic` ¬、`cross` ×、`love` 红色 ♥、`sparkle` 来回转的十字 |
27
+ | 换表情 | 立即开始,四个点各自被弹簧拉过去 |
28
+ | 空闲 | 中性表情时像真光标一样闪(静止帧不闪),视线一格一格地跳 |
29
+ | 信号状态 | 思考像终端转圈(每次转 1/8)、输出往右推并留下字块、进度是填满的字块、完成 ✓、雷达扫线、上下行箭头、书写、感叹号、关机时先一条横线再一个暗点 |
30
+ | 戳一下 | 除了点阵的反应,光标的四个点会被弹乱再弹回来 |
31
+
32
+ ## 纯模型
33
+
34
+ `createCaret(options)` 与 `createMatrix` 用法相同,只是脸换成一条折线和几个字块。不碰 DOM、不用时钟和 `Math.random`,同样的输入永远得到同样的帧。
35
+
36
+ ## 文件
37
+
38
+ | 文件 | 内容 |
39
+ |---|---|
40
+ | `index.js` | 入口:`createAvatar`、`theme` 和各种表 |
41
+ | `avatar.js` | `CaretAvatar`:继承点阵的 `MatrixAvatar`,只换模型 |
42
+ | `engine.js` | 光标:字形表 `FACES`、屏幕表 `SCREENS`、弹簧 |
43
+ | `playground.html` | 试玩页(共用 `../solids/playground.js`) |
@@ -0,0 +1,10 @@
1
+ import {MatrixAvatar} from '../matrix/avatar.js';
2
+ import {createCaret} from './engine.js';
3
+
4
+ export {identityFrom} from '../matrix/avatar.js';
5
+
6
+ /** The caret avatar: the matrix avatar with a text cursor for a face. Options and methods as MatrixAvatar's. */
7
+ export class CaretAvatar extends MatrixAvatar {
8
+ static className = 'botui-caret';
9
+ createModel(options) { return createCaret(options); }
10
+ }
@@ -0,0 +1,143 @@
1
+ // The caret theme's model: the Bot is a text cursor, on the matrix theme's tile.
2
+ //
3
+ // One thick stroke with round ends. Idle, it blinks the way a cursor does and
4
+ // hops from column to column after the pointer. Every expression bends the same
5
+ // stroke into a glyph (^ for happy, _ when shut, ¬ for skeptic, a check when
6
+ // done), and it springs from one glyph to the next. Working, it spins like a
7
+ // terminal spinner and types: it pushes right and leaves blocks of text behind.
8
+ //
9
+ // Everything else (tile, motion, marks, particles, halo, handling) is the matrix
10
+ // model's; this file only draws the face. Pure, like it: no DOM, no clock, no
11
+ // Math.random, so the same Bot and the same inputs always give the same frames.
12
+ // Sketched in avatar-lab as direction T (光标), made a theme on 2026-11-28.
13
+
14
+ import {Matrix} from '../matrix/engine.js';
15
+ import {TAU, ease, clamp} from '../../core/core.js';
16
+
17
+ const {sin, cos, abs, max, min, floor, round, ceil} = Math;
18
+ const fixed = v => v.toFixed(2);
19
+ const STIFF = 420, DAMP = 26, KEYS = 4, COLUMN = 12;
20
+ const HEART = 'oklch(0.6 0.22 15)';
21
+
22
+ /** The cursor for each of the solids' eye styles: dot (圆点) a block, bar (竖条) a beam, tall (长条) a slim beam. */
23
+ const CURSOR = {dot: {w: 15, h: 13}, bar: {w: 8.5, h: 17}, tall: {w: 6, h: 20}};
24
+ export const caretEyes = Object.freeze(Object.keys(CURSOR));
25
+
26
+ // A glyph is four points the stroke runs through, a width, and `gap` to lift the pen between the 2nd and 3rd.
27
+ const G = (points, w, extra) => ({p: points, w, ...extra});
28
+ const line = (x1, y1, x2, y2, w, extra) => G([[x1, y1], [x1 + (x2 - x1) / 3, y1 + (y2 - y1) / 3], [x1 + (x2 - x1) * 2 / 3, y1 + (y2 - y1) * 2 / 3], [x2, y2]], w, extra);
29
+ const vee = (x1, y1, x2, y2, x3, y3, w, extra) => G([[x1, y1], [x2, y2], [x2, y2], [x3, y3]], w, extra);
30
+ const two = (a, b, w, extra) => G([...a, ...b], w, {gap: true, ...extra});
31
+ const spin = (a, r, w) => line(-r * cos(a), -r * sin(a), r * cos(a), r * sin(a), w);
32
+
33
+ /** The 21 expressions: (cursor size, t) -> glyph, around the centre of the screen. */
34
+ export const FACES = {
35
+ neutral: C => line(0, -C.h, 0, C.h, C.w),
36
+ wide: C => line(0, -C.h * 1.2, 0, C.h * 1.2, max(16, C.w * 1.8)),
37
+ happy: () => vee(-13, 6, 0, -7, 13, 6, 7),
38
+ closed: () => line(-12, 12, 12, 12, 7),
39
+ sleep: () => line(-10, 16, 10, 16, 6, {o: 0.6}),
40
+ squint: () => vee(-9, -11, 8, 0, -9, 11, 7),
41
+ sad: () => G([[-12, 9], [-6, 1], [6, 1], [12, 9]], 7),
42
+ focus: C => line(-4, -C.h, 4, C.h, C.w * 1.15),
43
+ angry: C => line(5, -C.h, -5, C.h, C.w * 1.3),
44
+ calm: () => G([[-12, -1], [-5, 6], [5, 6], [12, -1]], 7),
45
+ sleepy: C => line(0, 2, 0, 14, C.w),
46
+ shy: C => line(0, 7, 0, 14, C.w * 0.8),
47
+ inspecting: C => line(10, -C.h * 1.1, 10, C.h * 1.1, C.w),
48
+ confused: () => G([[-12, 2], [-4, -5], [4, 5], [12, -2]], 7),
49
+ wink: () => line(-9, 0, 9, 0, 7),
50
+ skeptic: () => G([[-12, -5], [6, -5], [12, -5], [12, 7]], 7),
51
+ cross: () => two([[-10, -10], [10, 10]], [[10, -10], [-10, 10]], 7),
52
+ surprised: () => line(0, 0, 0, 0, 24),
53
+ dizzy: (C, t) => spin(t * 9, 12, 7),
54
+ love: () => vee(-8, -5, 0, 5, 8, -5, 11, {fill: HEART}),
55
+ sparkle: (C, t) => { const a = floor(t * 3.5) % 2 ? TAU / 8 : 0; return two([[-12 * cos(a), -12 * sin(a)], [12 * cos(a), 12 * sin(a)]], [[12 * sin(a), -12 * cos(a)], [-12 * sin(a), 12 * cos(a)]], 6); },
56
+ };
57
+ // Glyphs already shut, so a blink leaves them alone.
58
+ const SHUT = new Set(['closed', 'sleep', 'wink', 'happy', 'calm', 'sad', 'skeptic', 'cross', 'confused', 'sparkle', 'love', 'squint', 'dizzy']);
59
+
60
+ // ---- the screen ----
61
+ // What the cursor does instead of a face: (C, t, progress, since) -> {glyph, keys}, or null for the face.
62
+ // `keys` are the blocks it has typed: [{x, w, o}], on a line below the middle.
63
+ const typed = (n, o = 0.5) => Array.from({length: KEYS}, (_, i) => ({x: -27 + i * 15, w: 11, o: i < n ? o : 0}));
64
+ const NONE = typed(0);
65
+ export const SCREENS = {
66
+ // A terminal spinner: the beam turns in eighths.
67
+ thinking: (C, t) => ({glyph: spin(TAU / 4 + floor(t * 7) * TAU / 8, 15, C.w), keys: NONE}),
68
+ // Pushes right, leaving typed blocks behind; then a new line.
69
+ streaming: (C, t) => { const step = floor(t * 6) % 6, n = min(KEYS, step); return {glyph: line(-27 + n * 15, -9, -27 + n * 15, 9, C.w * 0.85), keys: typed(n)}; },
70
+ finished: () => ({glyph: vee(-14, 1, -4, 11, 15, -10, 8), keys: NONE}),
71
+ orbit: (C, t) => ({glyph: spin(t * 6, 13, C.w), keys: NONE}),
72
+ radar: (C, t) => { const a = t * 2.6; return {glyph: line(0, 0, 17 * cos(a), 17 * sin(a), 6), keys: NONE}; },
73
+ progress: (C, t, progress) => { const n = round(clamp(progress ?? (t * 0.22) % 1, 0, 1) * KEYS); return {glyph: line(-27 + n * 15, -9, -27 + n * 15, 9, C.w * 0.85), keys: typed(KEYS, 0.18).map((k, i) => ({...k, o: i < n ? 1 : 0.18}))}; },
74
+ loading: (C, t) => ({glyph: line(0, -C.h, 0, C.h, C.w, {o: floor(t * 6) % 2 ? 1 : 0.15}), keys: NONE}),
75
+ spawning: (C, t, progress, since) => since >= 1 ? null : {glyph: line(0, -C.h * min(1, since * 1.5), 0, C.h * min(1, since * 1.5), C.w), keys: NONE},
76
+ dictating: (C, t) => { const h = 4 + 14 * abs(sin(t * 6) * cos(t * 2.1)); return {glyph: line(0, -h, 0, h, C.w), keys: NONE}; },
77
+ sending: (C, t) => { const u = (t * 1.6) % 1, x = -8 + 16 * u; return {glyph: vee(x - 6, -10, x + 5, 0, x - 6, 10, 7, {o: min(1, 4 * min(u, 1 - u) + 0.2)}), keys: NONE}; },
78
+ receiving: (C, t) => { const u = (t * 1.4) % 1, y = -8 + 16 * u; return {glyph: vee(-10, y - 6, 0, y + 5, 10, y - 6, 7, {o: min(1, 4 * min(u, 1 - u) + 0.2)}), keys: NONE}; },
79
+ uploading: (C, t) => { const u = (t * 1.4) % 1, y = 8 - 16 * u; return {glyph: vee(-10, y + 6, 0, y - 5, 10, y + 6, 7, {o: min(1, 4 * min(u, 1 - u) + 0.2)}), keys: NONE}; },
80
+ writing: (C, t) => { const n = floor(t * 2.5) % (KEYS + 2), k = min(KEYS, n); return {glyph: line(-27 + k * 15, -9, -27 + k * 15, 9, C.w * 0.85, {o: floor(t * 5) % 2 ? 1 : 0.4}), keys: typed(k, 0.8)}; },
81
+ alerting: (C, t) => { const k = 1 + 0.12 * max(0, sin(t * 7)); return {glyph: two([[0, -15 * k], [0, 3 * k]], [[0, 13 * k], [0, 13 * k]], 8 * k), keys: NONE}; },
82
+ bouncing: (C, t) => { const y = 12 - 26 * abs(sin(t * 4)); return {glyph: line(0, y - 4, 0, y + 4, 13), keys: NONE}; },
83
+ // A screen switching off: a line across, then a dim dot.
84
+ 'powering-down': (C, t, progress, since) => ({glyph: since < 0.6 ? line(-18, 0, 18, 0, 4) : line(0, 0, 0, 0, 6, {o: 0.45 + 0.25 * sin(t * 1.6)}), keys: NONE}),
85
+ };
86
+
87
+ class Caret extends Matrix {
88
+ faceLayout(ink, node) {
89
+ this.pen = {p: Array.from({length: 4}, () => [50, 50]), v: Array.from({length: 4}, () => [0, 0]), w: 8, vw: 0, o: 1};
90
+ this.keys = Array.from({length: KEYS}, () => ({x: 50, w: 0, o: 0}));
91
+ return [
92
+ ...this.keys.map(() => node('tile', 'rect', {height: 7, rx: 3.5, fill: ink.dark, opacity: 0})),
93
+ node('tile', 'path', {fill: 'none', 'stroke-linecap': 'round', 'stroke-linejoin': 'round', stroke: ink.dark}),
94
+ ];
95
+ }
96
+ /** As the matrix's, and the cursor jolts. */
97
+ poke(action, options) {
98
+ super.poke(action, options);
99
+ for (const v of this.pen.v) { v[0] += (this.rng() - 0.5) * 300; v[1] += (this.rng() - 0.5) * 300; }
100
+ return this;
101
+ }
102
+ /** Where the stroke and the typed blocks are heading. */
103
+ goal(yaw, pitch, facing, still) {
104
+ const C = CURSOR[this.eyes] || CURSOR.bar, k = this.size < 40 ? 1.12 : 1;
105
+ if (!facing) return {glyph: line(0, 0, 0, 0, 0, {o: 0}), keys: NONE, ox: 0, oy: 0, k};
106
+ const screen = !this.forced && SCREENS[this.state]?.(C, this.t, this.progress, this.since);
107
+ if (screen) return {...screen, ox: 0, oy: 0, k};
108
+ // The cursor hops a column at a time after the gaze, and drifts a little up and down.
109
+ const ox = round(clamp(-sin(yaw) * 1.5 + this.gx * 5, -1, 1)) * COLUMN, oy = clamp((pitch * 1.6 + this.gy * 5) * 5, -5, 5);
110
+ let name = this.shown;
111
+ const glyph = (FACES[name] || FACES.neutral)(C, this.t);
112
+ // A blink squeezes the stroke flat. Idle and neutral it also blinks like a cursor, except in a still frame.
113
+ const shut = SHUT.has(name) ? 1 : 0.15 + 0.85 * this.p.open;
114
+ let o = glyph.o ?? 1;
115
+ if (name === 'neutral' && this.state === 'idle' && !this.hover && !still) o *= (this.t % 1.06) < 0.62 ? 1 : 0.14;
116
+ return {glyph: {...glyph, p: glyph.p.map(([x, y]) => [x, y * shut]), o}, keys: NONE, ox, oy, k};
117
+ }
118
+ drawFace(nodes, n, {dt, ink, dim, yaw, pitch, facing}) {
119
+ const {glyph, keys, ox, oy, k} = this.goal(yaw, pitch, facing, dt === null), pen = this.pen;
120
+ const steps = dt === null ? 0 : max(1, ceil(dt / 0.0167 - 1e-6)), h = steps ? dt / steps : 0;
121
+ const to = glyph.p.map(([x, y]) => [50 + ox + x * k, 50 + oy + y * k]);
122
+ if (!steps) { pen.p = to.map(q => [...q]); pen.v = to.map(() => [0, 0]); pen.w = glyph.w * k; pen.vw = 0; pen.o = glyph.o ?? 1; }
123
+ else for (let s = 0; s < steps; s++) {
124
+ pen.p.forEach((q, i) => { for (const a of [0, 1]) { pen.v[i][a] += (-STIFF * (q[a] - to[i][a]) - DAMP * pen.v[i][a]) * h; q[a] += pen.v[i][a] * h; } });
125
+ pen.vw += (-STIFF * (pen.w - glyph.w * k) - DAMP * pen.vw) * h; pen.w += pen.vw * h;
126
+ // A cursor's blink is a hard on and off, not a fade.
127
+ pen.o = ease(pen.o, glyph.o ?? 1, h, 40);
128
+ }
129
+ keys.forEach((key, i) => {
130
+ const mine = this.keys[i];
131
+ if (!steps) Object.assign(mine, {x: key.x, w: key.w, o: key.o});
132
+ else { mine.x = ease(mine.x, key.x, dt, 30); mine.w = ease(mine.w, key.w, dt, 30); mine.o = ease(mine.o, key.o, dt, 18); }
133
+ });
134
+ const fade = 0.35 + 0.65 * dim;
135
+ for (const key of this.keys) Object.assign(nodes[n++], {x: fixed(50 + (key.x - key.w / 2) * k), y: fixed(50 + 13 * k), width: fixed(max(0, key.w * k)), opacity: fixed(clamp(key.o, 0, 1) * fade)});
136
+ const [a, b, c, d] = pen.p.map(([x, y]) => fixed(x) + ',' + fixed(y));
137
+ Object.assign(nodes[n++], {d: glyph.gap ? `M${a}L${b}M${c}L${d}` : `M${a}L${b}L${c}L${d}`, 'stroke-width': fixed(max(0.5, pen.w)), stroke: glyph.fill || ink.dark, opacity: fixed(clamp(pen.o, 0, 1) * fade)});
138
+ return n;
139
+ }
140
+ }
141
+
142
+ /** A caret model. Options as createMatrix's: identity, hue, chroma, light, dots, edge, shape, eyes, state, progress, size, emphasized. */
143
+ export const createCaret = options => new Caret(options);
@@ -0,0 +1,24 @@
1
+ // Types for '@kodax-ai/botui/themes/caret'.
2
+
3
+ import type {ColorName, EyeStyle, ExpressionName, IdentityPool, Theme} from '../../index.js';
4
+ import type {MatrixAvatar, MatrixOptions, MatrixShape, MatrixModel, MatrixTone} from '../matrix/index.js';
5
+
6
+ export type {Avatar, AvatarOptions, ColorName, EyeStyle, ExpressionName, GestureName, IdentityPool, StateName, StateAlias, StateGroup, Theme} from '../../index.js';
7
+ export type {MatrixOptions as CaretOptions, MatrixShape as CaretShape} from '../matrix/index.js';
8
+ export {setAttention, resolveState, STATES, STATE_GROUPS, ALIASES, COLORS, BIRTH_COLORS, HUES, EYE_STYLES, GESTURES} from '../../index.js';
9
+ export {SHAPES, GROUPS} from '../matrix/index.js';
10
+
11
+ export interface CaretAvatar extends MatrixAvatar {}
12
+ export const CaretAvatar: {new (options?: MatrixOptions): CaretAvatar};
13
+
14
+ export const theme: Theme;
15
+ export function createAvatar(options?: MatrixOptions): CaretAvatar;
16
+ export function identityFrom(identity: string, pool?: IdentityPool): {shape: MatrixShape; color: ColorName; eyes: EyeStyle};
17
+ export const EXPRESSIONS: Record<ExpressionName, Function>;
18
+
19
+ // The pure model: the matrix model with a text cursor for a face.
20
+ export function createCaret(options: MatrixTone & {identity?: string; shape: MatrixShape; eyes?: EyeStyle; state?: string; progress?: number | null; size?: number; emphasized?: boolean}): MatrixModel;
21
+ export const caretShapes: readonly MatrixShape[];
22
+ export const caretEyes: readonly EyeStyle[];
23
+ export const FACES: Record<ExpressionName, Function>;
24
+ export const SCREENS: Record<string, Function>;
@@ -0,0 +1,35 @@
1
+ // BotUI caret theme: a text cursor for a face, on a coloured tile.
2
+ //
3
+ // import {createAvatar} from './themes/caret/index.js';
4
+ // const bot = createAvatar({identity: 'research-bot', size: 36});
5
+ // sidebar.append(bot.el);
6
+ // bot.setState('thinking');
7
+
8
+ import {CaretAvatar} from './avatar.js';
9
+ import {COLORS, EYE_STYLES} from '../../core/identity.js';
10
+ import {STATES} from '../../core/states.js';
11
+ import {registerTheme} from '../../core/registry.js';
12
+ import {GESTURES} from '../../core/motion.js';
13
+ import {FACES} from './engine.js';
14
+ import {SHAPES} from '../matrix/engine.js';
15
+
16
+ export {CaretAvatar, identityFrom} from './avatar.js';
17
+ export {COLORS, BIRTH_COLORS, HUES, EYE_STYLES} from '../../core/identity.js';
18
+ export {STATES, STATE_GROUPS, ALIASES, resolveState} from '../../core/states.js';
19
+ export {setAttention} from '../../core/core.js';
20
+ export {GESTURES} from '../../core/motion.js';
21
+ export {SHAPES, GROUPS, matrixShapes as caretShapes} from '../matrix/engine.js';
22
+ export {createCaret, caretEyes, FACES, SCREENS} from './engine.js';
23
+ /** The expressions, by name; here each one is a glyph the cursor bends into (see FACES). */
24
+ export const EXPRESSIONS = FACES;
25
+
26
+ /** Create an avatar. Options as MatrixAvatar's. Append `.el` to the page. */
27
+ export function createAvatar(options) { return new CaretAvatar(options); }
28
+
29
+ /** This theme, in the form core/registry.js expects. */
30
+ export const theme = {
31
+ name: 'caret', zh: '光标', create: createAvatar,
32
+ catalog: () => ({shapes: SHAPES, colors: Object.keys(COLORS), eyes: Object.keys(EYE_STYLES), states: Object.keys(STATES), gestures: Object.keys(GESTURES), expressions: Object.keys(FACES)}),
33
+ };
34
+ // Importing a theme makes it available to createAvatar({theme}) from the package entry.
35
+ registerTheme(theme);