@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
@@ -0,0 +1,130 @@
1
+ // The aura theme's model: a glowing orb with the solids' eyes, on the matrix theme's tile.
2
+ //
3
+ // Inside the tile, three soft blobs of light in neighbouring hues drift around
4
+ // each other. The state sets their mood: they swirl fast while it works, slow
5
+ // down as it gets sleepy, run warm when it is angry, cool when it is sad, and
6
+ // spread apart when it celebrates. In front, two eyes drawn exactly like the
7
+ // solids' (every expression, every eye style), a mouth while it talks, and a
8
+ // ring that fills for progress.
9
+ //
10
+ // Everything else (outline, motion, marks, particles, halo, handling) is the
11
+ // matrix model's. Pure, like it: no DOM, no clock, no Math.random. The one
12
+ // exception is a counter in the gradient ids, so two orbs on a page never share one.
13
+ // Sketched in avatar-lab as direction AC+ (光晕带眼睛), made a theme on 2026-11-28.
14
+
15
+ import {Matrix} from '../matrix/engine.js';
16
+ import {eyeShape} from '../solids/face.js';
17
+ import {EYE_STYLES} from '../../core/identity.js';
18
+ import {TAU, ease, clamp} from '../../core/core.js';
19
+
20
+ const {sin, cos, abs, max, min} = Math;
21
+ const fixed = v => v.toFixed(2);
22
+ const E = 44, RING = 44, BLOBS = 3, HEART = 'oklch(0.6 0.22 15)', BLUSH = 'oklch(0.74 0.17 15)';
23
+ let made = 0;
24
+
25
+ /** The orb's eyes are the solids': dot (圆点), bar (竖条), tall (长条). */
26
+ export const orbEyes = Object.freeze(Object.keys(EYE_STYLES));
27
+
28
+ // How the light moves in each state: speed of the swirl, how far apart the hues are, a hue to lean toward, brightness.
29
+ const CALM = {speed: 0.6, spread: 40, glow: 1};
30
+ const BUSY = {speed: 2.2, spread: 50, glow: 1.1};
31
+ export const MOODS = {
32
+ idle: CALM, listening: {speed: 0.9, spread: 40, glow: 1.15},
33
+ thinking: BUSY, searching: BUSY, reading: BUSY, working: BUSY, coding: BUSY, running: BUSY, computing: BUSY, controlling: BUSY, loading: BUSY, orbit: BUSY, radar: BUSY, progress: BUSY,
34
+ streaming: {speed: 1.6, spread: 45, glow: 1.15}, writing: {speed: 1.4, spread: 45, glow: 1.05}, dictating: {speed: 1.4, spread: 45, glow: 1.1},
35
+ sending: {speed: 1.8, spread: 45, glow: 1.1}, receiving: {speed: 1.8, spread: 45, glow: 1.1}, uploading: {speed: 1.8, spread: 45, glow: 1.1},
36
+ excited: {speed: 2.6, spread: 90, glow: 1.25}, celebrating: {speed: 2.6, spread: 110, glow: 1.25}, laughing: {speed: 2.2, spread: 80, glow: 1.2}, happy: {speed: 1.2, spread: 70, glow: 1.15},
37
+ finished: {speed: 1, spread: 60, glow: 1.2}, approving: {speed: 1, spread: 60, glow: 1.15}, proud: {speed: 0.9, spread: 60, glow: 1.15},
38
+ angry: {speed: 2.4, spread: 25, glow: 1.1, lean: 25}, error: {speed: 1.2, spread: 20, glow: 0.9, lean: 25}, alerting: {speed: 2, spread: 25, glow: 1.2, lean: 45},
39
+ loving: {speed: 0.9, spread: 30, glow: 1.15, lean: 0}, shy: {speed: 0.7, spread: 30, glow: 1, lean: 10},
40
+ sad: {speed: 0.35, spread: 20, glow: 0.8, lean: 250}, scared: {speed: 2.4, spread: 20, glow: 0.85, lean: 260},
41
+ sleeping: {speed: 0.15, spread: 15, glow: 0.65}, drowsy: {speed: 0.3, spread: 20, glow: 0.8}, bored: {speed: 0.25, spread: 15, glow: 0.8},
42
+ blocked: {speed: 0.2, spread: 10, glow: 0.6}, 'powering-down': {speed: 0.1, spread: 5, glow: 0.45},
43
+ };
44
+ // States it talks in: the mouth opens and closes.
45
+ const TALK = {streaming: 9, dictating: 7, humming: 4, laughing: 11, greeting: 6};
46
+
47
+ class Orb extends Matrix {
48
+ tileLayout(ink, node) {
49
+ const id = `botui-orb-${++made}`, [outline] = super.tileLayout(ink, node);
50
+ this.ids = {clip: id + '-clip', blob: i => `${id}-b${i}`};
51
+ this.swirl = this.rng() * 10; this.mood = {...CALM, lean: null, mix: 0};
52
+ const defs = node('tile', 'defs'), clip = node('tile', 'clipPath', {id: this.ids.clip}, defs);
53
+ const shape = node('tile', 'path', {d: outline.attrs.d}, clip);
54
+ const grads = Array.from({length: BLOBS}, (_, i) => {
55
+ const grad = node('tile', 'radialGradient', {id: this.ids.blob(i)}, defs);
56
+ return [grad, node('tile', 'stop', {offset: '0'}, grad), node('tile', 'stop', {offset: '0.55'}, grad), node('tile', 'stop', {offset: '1', 'stop-opacity': 0}, grad)];
57
+ }).flat();
58
+ const inside = node('tile', 'g', {'clip-path': `url(#${this.ids.clip})`});
59
+ return [
60
+ defs, clip, shape, ...grads, outline, inside,
61
+ ...Array.from({length: BLOBS}, (_, i) => node('tile', 'circle', {fill: `url(#${this.ids.blob(i)})`}, inside)),
62
+ node('tile', 'ellipse', {fill: '#fff', cx: 38, cy: 30, rx: 11, ry: 6, transform: 'rotate(-30 38 30)'}, inside),
63
+ node('tile', 'path', {fill: 'none', 'stroke-linecap': 'round', 'stroke-width': 3.2, d: ''}),
64
+ ];
65
+ }
66
+ drawTile(nodes, n, {dt, ink, dim}) {
67
+ const want = MOODS[this.state] || CALM, mood = this.mood, tone = this.tone;
68
+ // Ease from one mood to the next; the lean hue blends in and out.
69
+ if (dt === null) Object.assign(mood, want, {lean: want.lean ?? null, mix: want.lean == null ? 0 : 1});
70
+ else {
71
+ for (const key of ['speed', 'spread', 'glow']) mood[key] = ease(mood[key], want[key], dt, 3);
72
+ if (want.lean != null) mood.lean = want.lean;
73
+ mood.mix = ease(mood.mix, want.lean == null ? 0 : 1, dt, 3);
74
+ this.swirl += dt * mood.speed;
75
+ }
76
+ n += 3;
77
+ // The three lights: the Bot's hue and two neighbours, one lighter and one deeper.
78
+ const chroma = max(0.02, tone.chroma ?? 0.15) * (0.7 + 0.3 * this.p.sat), base = tone.light ?? 0.74;
79
+ const lean = mood.lean ?? tone.hue, toward = ((lean - tone.hue + 540) % 360) - 180;
80
+ const hues = [0, -mood.spread, mood.spread].map(d => (tone.hue + toward * mood.mix * 0.85 + d + 360) % 360);
81
+ const lights = [min(0.95, base + 0.13), clamp(base - 0.02, 0.3, 0.9), clamp(base - 0.12, 0.25, 0.85)].map(l => clamp(l * (0.82 + 0.18 * mood.glow), 0.2, 0.96));
82
+ for (let i = 0; i < BLOBS; i++) {
83
+ const colour = `oklch(${lights[i].toFixed(3)} ${(chroma * (i ? 1.25 : 0.9) + 0.02).toFixed(3)} ${hues[i].toFixed(1)})`;
84
+ nodes[n++]; Object.assign(nodes[n++], {'stop-color': colour, 'stop-opacity': fixed(min(1, 0.95 * mood.glow))});
85
+ Object.assign(nodes[n++], {'stop-color': colour, 'stop-opacity': fixed(min(1, 0.55 * mood.glow))}); Object.assign(nodes[n++], {'stop-color': colour});
86
+ }
87
+ Object.assign(nodes[n++], {fill: ink.body, stroke: ink.edge});
88
+ n++;
89
+ for (let i = 0; i < BLOBS; i++) {
90
+ const a = this.swirl * (1 + 0.3 * i) + i * TAU / BLOBS, r = 13 + 4 * sin(this.swirl * 0.7 + i * 2);
91
+ Object.assign(nodes[n++], {cx: fixed(50 + r * cos(a)), cy: fixed(50 + r * 0.85 * sin(a)), r: fixed(26 + 3 * sin(this.swirl * 1.3 + i))});
92
+ }
93
+ Object.assign(nodes[n++], {opacity: fixed(0.2 * dim)});
94
+ // The progress ring, from the top, clockwise.
95
+ const ring = nodes[n++], u = this.state === 'progress' ? clamp(this.progress ?? (this.t * 0.22) % 1, 0, 1) : 0;
96
+ if (u <= 0.001) Object.assign(ring, {d: '', opacity: 0});
97
+ else {
98
+ const a = -TAU / 4 + min(u, 0.9999) * TAU, big = u > 0.5 ? 1 : 0;
99
+ Object.assign(ring, {d: `M50,${50 - RING}A${RING},${RING} 0 ${big},1 ${fixed(50 + RING * cos(a))},${fixed(50 + RING * sin(a))}`, stroke: ink.accent, opacity: 1});
100
+ }
101
+ return n;
102
+ }
103
+ faceLayout(ink, node) {
104
+ return [
105
+ ...[-1, 1].map(() => node('tile', 'ellipse', {rx: 5.5, ry: 3, fill: BLUSH, opacity: 0})),
106
+ ...[-1, 1].map(() => node('tile', 'path', {'stroke-linecap': 'round', 'stroke-linejoin': 'round', fill: 'none', stroke: ink.dark})),
107
+ node('tile', 'ellipse', {fill: ink.dark, rx: 0, ry: 0}),
108
+ ];
109
+ }
110
+ drawFace(nodes, n, {ink, dim, yaw, pitch, facing}) {
111
+ const p = this.p, style = EYE_STYLES[this.eyes] || EYE_STYLES.tall, k = this.size < 40 ? 1.15 : 1;
112
+ const ox = clamp((-sin(yaw) * 1.5 + this.gx * 5) * 7, -9, 9), oy = clamp((pitch * 1.6 + this.gy * 5) * 5, -6, 6), fade = 0.35 + 0.65 * dim;
113
+ for (const side of [-1, 1]) Object.assign(nodes[n++], {cx: fixed(50 + side * 25 + ox), cy: fixed(59 + oy), opacity: fixed(facing ? 0.8 * p.blush : 0)});
114
+ for (const side of [-1, 1]) {
115
+ const node = nodes[n++];
116
+ if (!facing) { Object.assign(node, {d: '', opacity: 0}); continue; }
117
+ const eye = eyeShape(this.shown, side, style, p.open), cx = 50 + side * style.gap * E * k + ox, cy = 47 + oy;
118
+ const d = eye.lines.map(line => line.map(([x, y], i) => (i ? 'L' : 'M') + fixed(cx + x * E * k) + ',' + fixed(cy + y * E * k)).join('') + (eye.fill ? 'Z' : '')).join('');
119
+ const colour = this.shown === 'love' ? HEART : ink.dark;
120
+ Object.assign(node, {d, 'stroke-width': fixed(eye.w * E * k), stroke: colour, fill: eye.fill ? colour : 'none', opacity: fixed(fade)});
121
+ }
122
+ // A mouth only while it talks.
123
+ const talk = !this.forced && facing ? TALK[this.state] : 0, open = talk ? abs(sin(this.t * talk)) * (0.6 + 0.4 * abs(sin(this.t * talk * 0.37))) : 0;
124
+ Object.assign(nodes[n++], {cx: fixed(50 + ox), cy: fixed(62 + oy), rx: fixed(talk ? 4.2 * k : 0), ry: fixed(talk ? 0.8 + 4 * open * k : 0), opacity: fixed(fade)});
125
+ return n;
126
+ }
127
+ }
128
+
129
+ /** An orb model. Options as createMatrix's: identity, hue, chroma, light, dots, edge, shape, eyes, state, progress, size, emphasized. */
130
+ export const createOrb = options => new Orb(options);
@@ -0,0 +1,24 @@
1
+ // Types for '@kodax-ai/botui/themes/orb'.
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 OrbOptions, MatrixShape as OrbShape} 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 OrbAvatar extends MatrixAvatar {}
12
+ export const OrbAvatar: {new (options?: MatrixOptions): OrbAvatar};
13
+
14
+ export const theme: Theme;
15
+ export function createAvatar(options?: MatrixOptions): OrbAvatar;
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 light drifting inside and the solids' eyes.
20
+ export function createOrb(options: MatrixTone & {identity?: string; shape: MatrixShape; eyes?: EyeStyle; state?: string; progress?: number | null; size?: number; emphasized?: boolean}): MatrixModel;
21
+ export const orbShapes: readonly MatrixShape[];
22
+ export const orbEyes: readonly EyeStyle[];
23
+ export const FACES: Record<ExpressionName, Function>;
24
+ export const MOODS: Record<string, {speed: number; spread: number; glow: number; lean?: number}>;
@@ -0,0 +1,36 @@
1
+ // BotUI aura theme: a glowing orb with eyes.
2
+ //
3
+ // import {createAvatar} from './themes/orb/index.js';
4
+ // const bot = createAvatar({identity: 'research-bot', size: 36});
5
+ // sidebar.append(bot.el);
6
+ // bot.setState('thinking');
7
+
8
+ import {OrbAvatar} 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 {EXPRESSIONS as FACES} from '../solids/face.js';
14
+ import {SHAPES} from '../matrix/engine.js';
15
+
16
+ export {OrbAvatar, 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 orbShapes} from '../matrix/engine.js';
22
+ export {createOrb, orbEyes, MOODS} from './engine.js';
23
+ export {EXPRESSIONS as FACES} from '../solids/face.js';
24
+ /** The expressions, by name: the solids' eyes (see FACES). */
25
+ export const EXPRESSIONS = FACES;
26
+
27
+ /** Create an avatar. Options as MatrixAvatar's. Append `.el` to the page. */
28
+ export function createAvatar(options) { return new OrbAvatar(options); }
29
+
30
+ /** This theme, in the form core/registry.js expects. */
31
+ export const theme = {
32
+ name: 'orb', zh: '光晕', create: createAvatar,
33
+ catalog: () => ({shapes: SHAPES, colors: Object.keys(COLORS), eyes: Object.keys(EYE_STYLES), states: Object.keys(STATES), gestures: Object.keys(GESTURES), expressions: Object.keys(FACES)}),
34
+ };
35
+ // Importing a theme makes it available to createAvatar({theme}) from the package entry.
36
+ registerTheme(theme);
@@ -91,9 +91,9 @@ bot.setState('thinking');
91
91
  | `avatar.js` | `SolidAvatar`:选项、交互、物理、渲染 |
92
92
  | `geometry.js` | 36 个立体的网格 |
93
93
  | `face.js` | 眼型和眼神 |
94
- | `motion.js` | 每个状态的姿态、动作、空闲小动作 |
94
+ | `../../core/motion.js` | 每个状态的姿态、动作、空闲小动作(移到了 `core/`,所有主题共用这一份) |
95
95
  | `symbols.js` | `signals: 'morph'` 时身体变成的符号 |
96
- | `playground.html` | 试玩页 |
96
+ | `playground.html`、`playground.js` | 试玩页。`playground.js` 加载所在页面目录的 `index.js`,点阵主题共用它 |
97
97
  | `sheet.html`、`filmstrip.html` | 全家福和连续帧,核对造型和动作用 |
98
98
  | `pose.html` | 核对默认姿态:可切换侧转、俯仰和是否跟随鼠标 |
99
99