@uncarved/jade 0.2.0 → 0.3.0

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 (224) hide show
  1. package/README.md +277 -0
  2. package/assets/logo.svg +1 -0
  3. package/bin/jade.js +15 -0
  4. package/dist/api.d.ts +5 -250
  5. package/dist/api.js +5 -7
  6. package/dist/api.js.map +1 -1
  7. package/dist/fetch.d.ts +8 -0
  8. package/dist/fetch.js +19 -17
  9. package/dist/fetch.js.map +1 -1
  10. package/dist/host.d.ts +12 -1
  11. package/dist/host.js +49 -1
  12. package/dist/host.js.map +1 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.js +1 -1
  15. package/dist/index.js.map +1 -1
  16. package/dist/main.js +28 -5
  17. package/dist/main.js.map +1 -1
  18. package/dist/remote.js +4 -0
  19. package/dist/remote.js.map +1 -1
  20. package/dist/session.d.ts +2 -0
  21. package/dist/session.js +133 -13
  22. package/dist/session.js.map +1 -1
  23. package/dist/source.d.ts +2 -10
  24. package/dist/source.js.map +1 -1
  25. package/dist/update.d.ts +8 -20
  26. package/dist/update.js +12 -1
  27. package/dist/update.js.map +1 -1
  28. package/package.json +6 -4
  29. package/plugins/approval/index.ts +246 -84
  30. package/plugins/approval/plugin.json +5 -1
  31. package/plugins/approval/policy.ts +27 -6
  32. package/plugins/approval/review.ts +1 -1
  33. package/plugins/ask/index.ts +2 -2
  34. package/plugins/budget/index.ts +1 -1
  35. package/plugins/checks/checks.ts +3 -2
  36. package/plugins/checks/index.ts +2 -5
  37. package/plugins/commands/index.ts +2 -44
  38. package/plugins/compact/compact.ts +15 -4
  39. package/plugins/compact/index.ts +13 -16
  40. package/plugins/continue/index.ts +1 -1
  41. package/plugins/cost/index.ts +143 -0
  42. package/plugins/cost/plugin.json +13 -0
  43. package/plugins/custom-commands/index.ts +85 -0
  44. package/plugins/custom-commands/plugin.json +8 -0
  45. package/plugins/editor/index.ts +88 -0
  46. package/plugins/editor/plugin.json +17 -0
  47. package/plugins/fs/fs.ts +110 -17
  48. package/plugins/fs/grep.ts +41 -11
  49. package/plugins/fs/index.ts +9 -7
  50. package/plugins/fs/renderers.ts +1 -1
  51. package/plugins/hooks/index.ts +152 -0
  52. package/plugins/hooks/plugin.json +14 -0
  53. package/plugins/keys/index.ts +2 -18
  54. package/plugins/mcp/index.ts +40 -12
  55. package/plugins/mcp/install.ts +139 -0
  56. package/plugins/mcp/mcp.ts +2 -1
  57. package/plugins/mcp/plugin.json +1 -1
  58. package/plugins/mcp/project.ts +37 -1
  59. package/plugins/memory/index.ts +4 -9
  60. package/plugins/memory/plugin.json +1 -1
  61. package/plugins/mentions/index.ts +33 -13
  62. package/plugins/models/configure.ts +258 -150
  63. package/plugins/models/context.ts +2 -2
  64. package/plugins/models/index.ts +50 -42
  65. package/plugins/models/plugin.json +1 -1
  66. package/plugins/models/provider.ts +22 -4
  67. package/plugins/models/saved.ts +8 -4
  68. package/plugins/modes/index.ts +2 -17
  69. package/plugins/plugins/index.ts +2 -2
  70. package/plugins/print/index.ts +60 -12
  71. package/plugins/print/plugin.json +1 -0
  72. package/plugins/prompts/index.ts +50 -14
  73. package/plugins/{memory → prompts}/notes.ts +21 -9
  74. package/plugins/prompts/plugin.json +8 -2
  75. package/plugins/prompts/prompts.ts +14 -8
  76. package/plugins/prompts/results.ts +63 -0
  77. package/plugins/prompts/sys.ts +16 -4
  78. package/plugins/renderers/index.ts +2 -43
  79. package/plugins/repeats/index.ts +1 -1
  80. package/plugins/schedule/index.ts +2 -2
  81. package/plugins/scratch/index.ts +2 -3
  82. package/plugins/screenshot/index.ts +26 -13
  83. package/plugins/screenshot/plugin.json +1 -1
  84. package/plugins/sessions/index.ts +2 -17
  85. package/plugins/shell/index.ts +56 -6
  86. package/plugins/shell/plugin.json +3 -1
  87. package/plugins/shell/renderers.ts +1 -1
  88. package/plugins/shell/service.ts +2 -13
  89. package/plugins/shell/shell.ts +31 -16
  90. package/plugins/skills/index.ts +172 -37
  91. package/plugins/skills/install.ts +212 -0
  92. package/plugins/skills/plugin.json +3 -2
  93. package/plugins/skills/project.ts +38 -1
  94. package/plugins/skills/skills.ts +28 -9
  95. package/plugins/steer/index.ts +142 -0
  96. package/plugins/steer/plugin.json +8 -0
  97. package/plugins/suggest/index.ts +16 -11
  98. package/plugins/suggest/plugin.json +1 -1
  99. package/plugins/task/index.ts +8 -16
  100. package/plugins/task/plugin.json +9 -2
  101. package/plugins/task/subagent.ts +119 -15
  102. package/plugins/todo/index.ts +4 -4
  103. package/plugins/tui/editor.ts +10 -2
  104. package/plugins/tui/frontend.ts +130 -25
  105. package/plugins/tui/index.ts +3 -8
  106. package/plugins/tui/markdown.ts +51 -6
  107. package/plugins/tui/mascot.ts +505 -0
  108. package/plugins/tui/mentions.ts +48 -8
  109. package/plugins/tui/plugin.json +2 -2
  110. package/plugins/tui/screen.ts +6 -0
  111. package/plugins/tui/transcript.ts +22 -3
  112. package/plugins/tui/tui.ts +323 -95
  113. package/plugins/tui/vendor.ts +16 -0
  114. package/plugins/undo/checkpoint.ts +26 -2
  115. package/plugins/undo/index.ts +86 -24
  116. package/plugins/undo/plugin.json +8 -2
  117. package/plugins/undo/snapshot.ts +129 -0
  118. package/plugins/update/index.ts +11 -15
  119. package/plugins/update/plugin.json +2 -1
  120. package/plugins/web/index.ts +2 -5
  121. package/plugins/web/renderers.ts +1 -1
  122. package/plugins/web/web.ts +6 -5
  123. package/plugins-dist/approval/index.js +232 -50
  124. package/plugins-dist/approval/plugin.json +5 -1
  125. package/plugins-dist/approval/policy.js +27 -5
  126. package/plugins-dist/ask/index.js +1 -1
  127. package/plugins-dist/budget/index.js +1 -1
  128. package/plugins-dist/checks/checks.js +2 -1
  129. package/plugins-dist/checks/index.js +1 -1
  130. package/plugins-dist/commands/index.js +1 -1
  131. package/plugins-dist/compact/compact.js +13 -2
  132. package/plugins-dist/compact/index.js +12 -8
  133. package/plugins-dist/continue/index.js +1 -1
  134. package/plugins-dist/cost/index.js +135 -0
  135. package/plugins-dist/cost/plugin.json +16 -0
  136. package/plugins-dist/custom-commands/index.js +86 -0
  137. package/plugins-dist/custom-commands/plugin.json +12 -0
  138. package/plugins-dist/editor/index.js +89 -0
  139. package/plugins-dist/editor/plugin.json +21 -0
  140. package/plugins-dist/fs/fs.js +106 -17
  141. package/plugins-dist/fs/grep.js +40 -12
  142. package/plugins-dist/fs/index.js +8 -5
  143. package/plugins-dist/hooks/index.js +145 -0
  144. package/plugins-dist/hooks/plugin.json +19 -0
  145. package/plugins-dist/keys/index.js +1 -1
  146. package/plugins-dist/mcp/index.js +41 -9
  147. package/plugins-dist/mcp/install.js +160 -0
  148. package/plugins-dist/mcp/mcp.js +2 -1
  149. package/plugins-dist/mcp/plugin.json +2 -1
  150. package/plugins-dist/mcp/project.js +42 -1
  151. package/plugins-dist/memory/index.js +3 -5
  152. package/plugins-dist/memory/plugin.json +1 -1
  153. package/plugins-dist/mentions/index.js +29 -11
  154. package/plugins-dist/models/configure.js +252 -151
  155. package/plugins-dist/models/context.js +2 -2
  156. package/plugins-dist/models/index.js +53 -16
  157. package/plugins-dist/models/plugin.json +1 -1
  158. package/plugins-dist/models/provider.js +4 -2
  159. package/plugins-dist/models/saved.js +9 -4
  160. package/plugins-dist/modes/index.js +1 -1
  161. package/plugins-dist/plugins/index.js +1 -1
  162. package/plugins-dist/print/index.js +62 -9
  163. package/plugins-dist/print/plugin.json +3 -0
  164. package/plugins-dist/prompts/index.js +49 -2
  165. package/plugins-dist/{memory → prompts}/notes.js +10 -7
  166. package/plugins-dist/prompts/plugin.json +8 -2
  167. package/plugins-dist/prompts/prompts.js +13 -8
  168. package/plugins-dist/prompts/results.js +60 -0
  169. package/plugins-dist/prompts/sys.js +15 -3
  170. package/plugins-dist/renderers/index.js +1 -1
  171. package/plugins-dist/repeats/index.js +1 -1
  172. package/plugins-dist/schedule/index.js +1 -1
  173. package/plugins-dist/scratch/index.js +1 -1
  174. package/plugins-dist/screenshot/index.js +25 -11
  175. package/plugins-dist/screenshot/plugin.json +1 -1
  176. package/plugins-dist/sessions/index.js +1 -1
  177. package/plugins-dist/shell/index.js +55 -3
  178. package/plugins-dist/shell/plugin.json +3 -1
  179. package/plugins-dist/shell/shell.js +26 -8
  180. package/plugins-dist/skills/index.js +184 -35
  181. package/plugins-dist/skills/install.js +178 -0
  182. package/plugins-dist/skills/plugin.json +6 -2
  183. package/plugins-dist/skills/project.js +43 -1
  184. package/plugins-dist/skills/skills.js +27 -10
  185. package/plugins-dist/steer/index.js +135 -0
  186. package/plugins-dist/steer/plugin.json +12 -0
  187. package/plugins-dist/suggest/index.js +15 -9
  188. package/plugins-dist/suggest/plugin.json +1 -1
  189. package/plugins-dist/task/index.js +7 -4
  190. package/plugins-dist/task/plugin.json +10 -2
  191. package/plugins-dist/task/subagent.js +106 -15
  192. package/plugins-dist/todo/index.js +3 -3
  193. package/plugins-dist/tui/editor.js +7 -2
  194. package/plugins-dist/tui/frontend.js +135 -18
  195. package/plugins-dist/tui/index.js +2 -1
  196. package/plugins-dist/tui/markdown.js +48 -6
  197. package/plugins-dist/tui/mascot.js +439 -0
  198. package/plugins-dist/tui/mentions.js +44 -6
  199. package/plugins-dist/tui/plugin.json +3 -2
  200. package/plugins-dist/tui/screen.js +2 -0
  201. package/plugins-dist/tui/transcript.js +19 -2
  202. package/plugins-dist/tui/tui.js +303 -83
  203. package/plugins-dist/tui/vendor.js +13822 -0
  204. package/plugins-dist/undo/checkpoint.js +28 -1
  205. package/plugins-dist/undo/index.js +92 -22
  206. package/plugins-dist/undo/plugin.json +10 -3
  207. package/plugins-dist/undo/snapshot.js +111 -0
  208. package/plugins-dist/update/index.js +9 -10
  209. package/plugins-dist/update/plugin.json +4 -1
  210. package/plugins-dist/web/index.js +1 -1
  211. package/plugins-dist/web/web.js +6 -5
  212. package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
  213. package/sdk/python/jade_plugin.py +8 -0
  214. package/src/api.ts +7 -262
  215. package/src/fetch.ts +18 -16
  216. package/src/host.ts +49 -1
  217. package/src/index.ts +1 -1
  218. package/src/main.ts +25 -6
  219. package/src/remote.ts +2 -0
  220. package/src/session.ts +134 -15
  221. package/src/source.ts +2 -11
  222. package/src/update.ts +16 -10
  223. package/plugins/undo/notes.ts +0 -43
  224. package/plugins-dist/undo/notes.js +0 -36
@@ -0,0 +1,439 @@
1
+ // 标题左边的吉祥物:一个玉做的团子,按当前在做的事换表情、带一点动画;还有终端标签页标题上的 jade 标志。
2
+ // 不碰 Ink:动画由 App 统一重画推动(见 tui.ts 的 tick),这里每次按时间算出这一帧。
3
+ //
4
+ // 画布 15×6 个像素,每格字符上下两个像素(▀ 前景是上面、背景是下面),6 个像素高正好三行,和右边三行文字并排。
5
+ // 左边 9 列是身体,空一列,右边 5 列放表情以外的小东西(思考的三个点、工具的转圈、提问的感叹号、做完的 ✓……都用像素画)。画布宽度不随表情变,标题右边的文字不会跟着左右跳。
6
+ //
7
+ // 精度只有这么多,细节靠颜色:身体按光照着色(左上亮、边缘暗、一点高光),眼睛是深色的形状,
8
+ // 每个像素按 4×4 次采样算出眼睛盖住了多少、和身体的颜色混合,所以眼睛可以不按整格移动、眨眼也有中间的几帧。
9
+ // 身体外面是终端背景,颜色未知、没法混合,所以轮廓只按整格变(只有做完后蹦那一下会变)、混色只在身体里面做。
10
+ // 视线、睁眼的程度、颜色在状态之间缓动(Mascot),不会一下跳过去。
11
+ export const JADE = "#00a86b";
12
+ /** 做完后笑多久 */
13
+ export const HAPPY_MS = 4000;
14
+ export function moodOf(s, now) {
15
+ if (s.asking)
16
+ return "asking";
17
+ const b = s.busy;
18
+ if (b === undefined)
19
+ return s.doneAt !== undefined && now - s.doneAt < HAPPY_MS ? "happy" : "idle";
20
+ if (b === "Interrupting")
21
+ return "stopping";
22
+ if (b === "Thinking")
23
+ return "thinking";
24
+ if (b.startsWith("Writing"))
25
+ return "writing";
26
+ if (b.startsWith("Running "))
27
+ return "tool";
28
+ return "waiting";
29
+ }
30
+ const rgb = (hex) => [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16));
31
+ const hex = (c) => `#${c.map((v) => Math.round(Math.max(0, Math.min(255, v))).toString(16).padStart(2, "0")).join("")}`;
32
+ const mix = (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];
33
+ /** 按 t(0–1)在一串颜色之间插值 */
34
+ const ramp = (stops, t) => {
35
+ const x = Math.max(0, Math.min(1, t)) * (stops.length - 1);
36
+ const i = Math.min(stops.length - 2, Math.floor(x));
37
+ return mix(stops[i], stops[i + 1], x - i);
38
+ };
39
+ /** 从暗到亮的玉色;中断时换成琥珀色那一套 */
40
+ const JADE_RAMP = ["#004a33", "#00744c", JADE, "#2ccc8a", "#8eeec0"].map(rgb);
41
+ const AMBER_RAMP = ["#5a3a00", "#9a6500", "#e0a526", "#f4c75a", "#fde4a0"].map(rgb);
42
+ const SPECULAR = rgb("#effff6");
43
+ const EYE = rgb("#0b2219");
44
+ const PALE = rgb("#b6f2d4");
45
+ const DEEP = rgb("#00744c");
46
+ const BANG = [rgb("#f5c518"), rgb("#fff1a8")];
47
+ const SWEAT = rgb("#8fd3ff");
48
+ /** 蹦起来时地上的影子:身体外面没法和终端背景混合,用一个暗的玉色 */
49
+ const SHADOW = mix(DEEP, EYE, 0.55);
50
+ // ---------- 形状 ----------
51
+ const W = 15;
52
+ /** 右边小东西的第一列:和身体之间空一列,不贴着 */
53
+ const SIDE = 10;
54
+ const H = 6;
55
+ /** 身体的轮廓(9 列),轮廓外是终端背景 */
56
+ const MASK = ["..#####..", ".#######.", "#########", "#########", "#########", ".#######."];
57
+ const CX = 4.5;
58
+ const SHAPES = {
59
+ rest: { mask: MASK, top: 0, bottom: 6, dy: 0 },
60
+ crouch: { mask: [".........", ".#######.", "#########", "#########", "#########", ".#######."], top: 1, bottom: 6, dy: 1 },
61
+ air: { mask: ["..#####..", ".#######.", "#########", "#########", ".#######.", "........."], top: 0, bottom: 5, dy: -1 },
62
+ };
63
+ /** 做完后的一跳:蹲下蓄力 → 跳起 → 落地压扁一下 → 站好(t 是进入 happy 之后的毫秒) */
64
+ const HOP = { crouch: 90, air: 380, land: 470 };
65
+ function shapeAt(mood, t) {
66
+ if (mood !== "happy" || t >= HOP.land)
67
+ return SHAPES.rest;
68
+ return t < HOP.crouch || t >= HOP.air ? SHAPES.crouch : SHAPES.air;
69
+ }
70
+ /**
71
+ * 两只眼睛:中心的 x、下沿的 y(像素坐标,像素 (x, y) 覆盖 [x, x+1)×[y, y+1)),宽 1、高 2 个像素。
72
+ * 停住的位置都正好对齐像素,眼睛是清楚的两块;只有移动、眨眼的中间几帧才会混出过渡色
73
+ */
74
+ const EYES_X = [2.5, 6.5];
75
+ const EYE_BOTTOM = 4;
76
+ const EYE_H = 2;
77
+ const smooth = (t) => {
78
+ const x = Math.max(0, Math.min(1, t));
79
+ return x * x * (3 - 2 * x);
80
+ };
81
+ const gauss = (dx, dy, s) => Math.exp(-(dx * dx + dy * dy) / (2 * s * s));
82
+ const REST = { gx: 0, gy: 0, open: 1, happy: 0, wink: 0, tint: 0, glow: 0 };
83
+ // ---------- 时间轴 ----------
84
+ const ease = (now, keys, period) => {
85
+ // keys:[时刻, 值],时刻在 [0, period) 里递增;相邻两个之间平滑过渡,最后一个过渡回第一个
86
+ const t = now % period;
87
+ for (let i = 0; i < keys.length; i++) {
88
+ const [t0, v0] = keys[i];
89
+ const [t1raw, v1] = keys[(i + 1) % keys.length];
90
+ const t1 = i + 1 < keys.length ? t1raw : t1raw + period;
91
+ if (t >= t0 && t < t1)
92
+ return v0 + (v1 - v0) * smooth((t - t0) / (t1 - t0));
93
+ }
94
+ return keys[0][1];
95
+ };
96
+ /** 关键帧之间停一会儿再动:每个值先停 hold 毫秒,再用 move 毫秒移到下一个 */
97
+ const steps = (values, hold, move) => {
98
+ const keys = [];
99
+ values.forEach((v, i) => {
100
+ keys.push([i * (hold + move), v], [i * (hold + move) + hold, v]);
101
+ });
102
+ return [keys, values.length * (hold + move)];
103
+ };
104
+ /** 每隔多久眨一次眼,眨一下多久 */
105
+ const BLINK_EVERY = 4200;
106
+ const BLINK_MS = 220;
107
+ /** 空闲时每隔多久往旁边瞟一眼,从什么时候到什么时候 */
108
+ const GLANCE_EVERY = 11_000;
109
+ const GLANCE = [7000, 8600];
110
+ /** 眨眼:返回睁眼程度的系数。每第三次连眨两下 */
111
+ function blink(now) {
112
+ const n = Math.floor(now / BLINK_EVERY);
113
+ const t = now % BLINK_EVERY;
114
+ const one = (t0) => (t >= t0 && t < t0 + BLINK_MS ? 1 - Math.sin((Math.PI * (t - t0)) / BLINK_MS) : 1);
115
+ return Math.min(one(0), n % 3 === 2 ? one(BLINK_MS + 90) : 1);
116
+ }
117
+ /** 做完后什么时候回过头眨眼(落地后先看着 ✓ 画出来) */
118
+ const WINK_AT = 1500;
119
+ /** 这个状态此刻想要的样子(Mascot 往这里缓动);t 是进入这个状态之后的毫秒,只有 happy 用 */
120
+ export function goal(mood, now, t = HAPPY_MS / 2) {
121
+ switch (mood) {
122
+ case "idle": {
123
+ const t = now % GLANCE_EVERY;
124
+ const look = t >= GLANCE[0] && t < GLANCE[1] ? -1 : 0;
125
+ return { ...REST, gx: look, open: blink(now) };
126
+ }
127
+ case "waiting": {
128
+ const [keys, period] = steps([0, -1, 0, 1], 600, 350);
129
+ return { ...REST, gx: ease(now, keys, period), open: blink(now) };
130
+ }
131
+ case "thinking": {
132
+ // 琢磨:眯起眼往左上方望,身体一点点亮起来(在攒想法);
133
+ // 想到了:睁开眼转向右上角,看着三个点一个个冒出来,攒的亮光随着点冒出来慢慢退掉
134
+ const gx = ease(now, [[0, -1], [THINK.ponder - 350, -1], [THINK.ponder, 1], [THINK.period - 350, 1]], THINK.period);
135
+ const open = ease(now, [[0, 0.5], [THINK.ponder - 350, 0.5], [THINK.ponder, 1], [THINK.period - 350, 1]], THINK.period);
136
+ const glow = ease(now, [[0, 0], [THINK.ponder, 0.75], [THINK.dot(2), 0]], THINK.period);
137
+ return { ...REST, gx, gy: -1, open: open * blink(now + 1700), glow };
138
+ }
139
+ case "writing": {
140
+ // 读着刚写的字:一行里从左往右扫,到头回来
141
+ const [keys, period] = steps([-1, 0, 1], 260, 140);
142
+ return { ...REST, gx: ease(now, keys, period), gy: 1, open: 0.5 + 0.5 * blink(now + 600) };
143
+ }
144
+ case "tool":
145
+ return { ...REST, gx: 1, open: blink(now + 900) };
146
+ case "asking":
147
+ return { ...REST, open: 1.5 * blink(now + 2500) };
148
+ case "stopping":
149
+ return { ...REST, open: 0, tint: 1 };
150
+ case "happy": {
151
+ // 只有眼睛,不加嘴和脸红:别的表情都只靠眼睛,加了反而不像同一个。
152
+ // 蹦的时候 ^ ^;落地那一下身体一亮,睁眼看右边的 ✓ 画出来;然后回过头,左眼 ^ 冲你眨一下
153
+ const glow = t >= HOP.air ? Math.exp(-(t - HOP.air) / 260) : 0;
154
+ if (t < HOP.land)
155
+ return { ...REST, happy: 1, glow };
156
+ if (t < WINK_AT)
157
+ return { ...REST, gx: 1, glow };
158
+ return { ...REST, wink: 1 };
159
+ }
160
+ }
161
+ }
162
+ /** 画一帧:返回 6 行、每行 15 个颜色("#rrggbb"),undefined 是透明(终端背景);t 同 goal */
163
+ export function paint(p, mood, now, t = HAPPY_MS / 2) {
164
+ const shape = shapeAt(mood, t);
165
+ const cy = (shape.top + shape.bottom) / 2;
166
+ const half = (shape.bottom - shape.top) / 2;
167
+ const ramp_ = (t) => mix(ramp(JADE_RAMP, t), ramp(AMBER_RAMP, t), p.tint);
168
+ const grid = Array.from({ length: H }, () => Array(W).fill(undefined));
169
+ // 工具运行时,一道光从左往右扫过身体
170
+ const sweep = mood === "tool" ? ((now % 1400) / 1400) * 16 - 4 : undefined;
171
+ for (let y = 0; y < H; y++)
172
+ for (let x = 0; x < 9; x++) {
173
+ if (shape.mask[y]?.[x] !== "#")
174
+ continue;
175
+ const nx = (x + 0.5 - CX) / 4.5;
176
+ const ny = (y + 0.5 - cy) / half;
177
+ const r = Math.max(Math.abs(nx), Math.abs(ny) * 0.9, Math.hypot(nx, ny) * 0.82);
178
+ let light = 0.62 - 0.3 * ny - 0.14 * nx - 0.32 * smooth((r - 0.55) / 0.45) + 0.18 * p.glow;
179
+ if (sweep !== undefined)
180
+ light += 0.22 * Math.exp(-(((x - y * 0.5 - sweep) / 0.8) ** 2));
181
+ let c = ramp_(light);
182
+ // 高光:左上角一个像素,旁边一圈淡淡的
183
+ c = mix(c, SPECULAR, 0.9 * gauss(x - 2, y - shape.top - 1, 0.45));
184
+ // 眼睛:4×4 次采样,算出盖住了多少
185
+ let cover = 0;
186
+ for (let sy = 0; sy < 4; sy++)
187
+ for (let sx = 0; sx < 4; sx++) {
188
+ const px = x + (sx + 0.5) / 4;
189
+ const py = y + (sy + 0.5) / 4;
190
+ cover += eyeAt(p, px, py - shape.dy) / 16;
191
+ }
192
+ c = mix(c, mix(EYE, ramp_(0.05), p.tint * 0.4), Math.min(1, cover));
193
+ grid[y][x] = c;
194
+ }
195
+ // 跳在空中时脚下一块影子
196
+ if (shape === SHAPES.air)
197
+ for (let x = 3; x < 6; x++)
198
+ grid[5][x] = SHADOW;
199
+ for (const [x, y, c] of extras(mood, now, t))
200
+ grid[y][x] = c;
201
+ return grid.map((row) => row.map((c) => (c ? hex(c) : undefined)));
202
+ }
203
+ /** 眼睛在 (px, py) 处的覆盖(0 或 1):普通的眼睛和 ^ 形的笑眼按 happy 混合(左眼还算上 wink) */
204
+ function eyeAt(p, px, py) {
205
+ let best = 0;
206
+ for (const [i, bx] of EYES_X.entries()) {
207
+ const happy = i === 0 ? Math.max(p.happy, p.wink) : p.happy;
208
+ // 笑分两步:先是下眼皮往上推,眼睛压成上面一格的一条线(连续的);
209
+ // 推到头之后一下子变成 ^(像素画按帧换形状,滑过去的中间几帧是糊的)
210
+ const smile = happy > 0.6;
211
+ // 两只都变成 ^ 时各往外一格:^ 有三格宽,不挪开的话两只里边的眼角只隔一格,连成一排锯齿;
212
+ // 只眨一只眼时另一只是普通的眼睛,不会连起来,^ 留在原位
213
+ // ^ 对齐整格:从别的表情缓过来时视线还没回正,不对齐的话 ^ 是糊的
214
+ const out = p.happy > 0.6 ? (i === 0 ? -1 : 1) : 0;
215
+ const ex = smile ? Math.round(bx + p.gx - 0.5) + 0.5 + out : bx + p.gx;
216
+ const bottom = smile ? Math.round(EYE_BOTTOM + p.gy) : EYE_BOTTOM + p.gy;
217
+ if (Math.abs(px - ex) < 0.5) {
218
+ // 闭眼是上眼皮往下:下沿不动,高度变小,最少留 1 像素的一条线
219
+ const lower = bottom - Math.min(1, happy / 0.6);
220
+ const upper = Math.min(bottom - Math.max(1, EYE_H * p.open), lower - 1);
221
+ if (py >= upper && py < lower)
222
+ best = 1;
223
+ }
224
+ if (smile && Math.abs(Math.abs(px - ex) - 1) < 0.5 && py >= bottom - 1 && py < bottom)
225
+ best = 1;
226
+ }
227
+ return best;
228
+ }
229
+ /** 右边的小东西:[列, 行, 颜色],列是 SIDE 起的 0–4 */
230
+ function extras(mood, now, t) {
231
+ return side(mood, now, t).map(([x, y, c]) => [SIDE + x, y, c]);
232
+ }
233
+ /** ✓ 的五个像素,按笔画的顺序:短的一笔往右下,长的一笔往右上 */
234
+ const CHECK = [
235
+ [0, 3],
236
+ [1, 4],
237
+ [2, 3],
238
+ [3, 2],
239
+ [4, 1],
240
+ ];
241
+ /** ✓ 落地后一笔画出来,每个像素隔多久;画完过一会儿一道光沿着笔画走一遍;最后按笔画的顺序暗下去消失 */
242
+ const CHECK_STEP = 55;
243
+ const CHECK_SHINE = 1900;
244
+ const CHECK_FADE = HAPPY_MS - 600;
245
+ function check(t) {
246
+ const white = rgb("#ffffff");
247
+ return CHECK.flatMap(([x, y], i) => {
248
+ const age = t - (HOP.land + i * CHECK_STEP);
249
+ if (age < 0)
250
+ return [];
251
+ // 刚画上的是白的笔尖,很快沉成淡玉色
252
+ let c = mix(PALE, white, Math.exp(-age / 120));
253
+ // 一道光:亮点沿着笔画走过去
254
+ c = mix(c, white, 0.85 * Math.exp(-(((t - CHECK_SHINE) / 60 - i) ** 2)));
255
+ // 消失:一个个暗下去再拿掉(外面是终端背景,没法渐隐)
256
+ const out = (t - CHECK_FADE - i * 70) / 280;
257
+ if (out >= 1)
258
+ return [];
259
+ if (out > 0)
260
+ c = mix(c, DEEP, out);
261
+ return [[x, y, c]];
262
+ });
263
+ }
264
+ const fade = (t) => ramp([DEEP, rgb(JADE), PALE], t);
265
+ /**
266
+ * 思考的节奏(一圈的毫秒数):先琢磨 ponder,然后三个点隔 gap 一个个冒出来;
267
+ * 三个都在了停一会儿,从 dim 起一起暗下去、拿掉,再接着琢磨
268
+ */
269
+ const THINK = {
270
+ period: 4800,
271
+ ponder: 1700,
272
+ gap: 550,
273
+ /** 第 i 个点冒出来的时刻 */
274
+ dot: (i) => THINK.ponder + 250 + i * THINK.gap,
275
+ dim: 4200,
276
+ /** 暗下去用多久:之后拿掉(外面是终端背景,没法渐隐) */
277
+ fade: 400,
278
+ };
279
+ /** 三个点:在右上方从左往右排成一排,和往右上看的眼睛同一高度附近,像省略号 */
280
+ const DOTS = [
281
+ [0, 1],
282
+ [2, 1],
283
+ [4, 1],
284
+ ];
285
+ function dots(now) {
286
+ const t = now % THINK.period;
287
+ const out = (t - THINK.dim) / THINK.fade;
288
+ if (out >= 1)
289
+ return [];
290
+ return DOTS.flatMap(([x, y], i) => {
291
+ const age = t - THINK.dot(i);
292
+ if (age < 0)
293
+ return [];
294
+ // 冒出来时白一下,很快沉成淡玉色
295
+ let c = mix(fade(0.85), rgb("#ffffff"), Math.exp(-age / 140));
296
+ if (out > 0)
297
+ c = mix(c, DEEP, out);
298
+ return [[x, y, c]];
299
+ });
300
+ }
301
+ function side(mood, now, t) {
302
+ switch (mood) {
303
+ case "happy":
304
+ return check(t);
305
+ case "thinking":
306
+ return dots(now);
307
+ case "writing": {
308
+ // 打字的点:一个个亮起来,亮过之后慢慢暗下去
309
+ const t = (now % 1200) / 1200;
310
+ return [0, 1, 2].flatMap((x) => {
311
+ const age = t - x * 0.18;
312
+ return age >= 0 && age < 0.75 ? [[x, 5, fade(1 - Math.max(0, age - 0.25) * 2)]] : [];
313
+ });
314
+ }
315
+ case "tool": {
316
+ // 彗星一样转圈:亮的头,后面跟着越来越暗的尾巴
317
+ const ring = [
318
+ [1, 1],
319
+ [2, 2],
320
+ [2, 3],
321
+ [1, 4],
322
+ [0, 3],
323
+ [0, 2],
324
+ ];
325
+ const head = Math.floor(now / 90) % ring.length;
326
+ return [0, 1, 2, 3].map((k) => {
327
+ const [x, y] = ring[(head - k + ring.length) % ring.length];
328
+ return [x, y, fade(1 - k / 3.2)];
329
+ });
330
+ }
331
+ case "asking": {
332
+ // 感叹号在两种黄之间呼吸
333
+ const c = mix(BANG[0], BANG[1], 0.5 + 0.5 * Math.sin(now / 160));
334
+ return [0, 1, 2, 4].map((y) => [1, y, c]);
335
+ }
336
+ case "stopping":
337
+ // 汗珠挂在头的右上角,贴着身体
338
+ return [
339
+ [-1, 0, SWEAT],
340
+ [-1, 1, mix(SWEAT, rgb("#ffffff"), 0.4)],
341
+ ];
342
+ default:
343
+ return [];
344
+ }
345
+ }
346
+ /**
347
+ * 有状态的动画:每次 frame 把 Pose 往当前状态想要的样子缓动一点,所以换状态时眼睛是滑过去的。
348
+ * 眨眼这种很快的动作直接用目标值,不缓动
349
+ */
350
+ export class Mascot {
351
+ pose = { ...REST };
352
+ #last;
353
+ #mood;
354
+ /** 进入当前状态的时刻:做完后那一套动作按它排时间 */
355
+ #since = 0;
356
+ frame(mood, now) {
357
+ if (mood !== this.#mood) {
358
+ this.#mood = mood;
359
+ this.#since = now;
360
+ }
361
+ const t = now - this.#since;
362
+ const want = goal(mood, now, t);
363
+ const dt = this.#last === undefined ? 1e9 : Math.max(0, now - this.#last);
364
+ this.#last = now;
365
+ const k = 1 - Math.exp(-dt / 110);
366
+ const p = this.pose;
367
+ for (const key of ["gx", "gy", "happy", "wink", "tint"])
368
+ p[key] += (want[key] - p[key]) * k;
369
+ p.open = want.open < p.open ? want.open : p.open + (want.open - p.open) * (1 - Math.exp(-dt / 50));
370
+ p.glow = want.glow;
371
+ return paint(p, mood, now, t);
372
+ }
373
+ /** 还在往目标缓动(空闲时也要接着重画几帧) */
374
+ moving(mood, now) {
375
+ const want = goal(mood, now, mood === this.#mood ? now - this.#since : 0);
376
+ const p = this.pose;
377
+ return ["gx", "gy", "happy", "wink", "tint", "open"].some((k) => Math.abs(want[k] - p[k]) > 0.02);
378
+ }
379
+ }
380
+ /** 把像素合成半格字符,每一行是若干段同色的字符 */
381
+ export function cells(grid) {
382
+ const out = [];
383
+ for (let y = 0; y < grid.length; y += 2) {
384
+ const top = grid[y];
385
+ const bottom = grid[y + 1] ?? [];
386
+ const line = [];
387
+ for (let x = 0; x < top.length; x++) {
388
+ const t = top[x];
389
+ const b = bottom[x];
390
+ const cell = !t && !b ? { text: " " }
391
+ : t === b ? { text: "█", fg: t }
392
+ : !t ? { text: "▄", fg: b }
393
+ : !b ? { text: "▀", fg: t }
394
+ : { text: "▀", fg: t, bg: b };
395
+ const last = line.at(-1);
396
+ if (last && last.fg === cell.fg && last.bg === cell.bg)
397
+ last.text += cell.text;
398
+ else
399
+ line.push(cell);
400
+ }
401
+ out.push(line);
402
+ }
403
+ return out;
404
+ }
405
+ /** 动画帧之间隔多久 */
406
+ export const FRAME_MS = 50;
407
+ /**
408
+ * 不在运行时,下一次需要重画还要多久(毫秒):缓动没结束、正在眨眼或瞟眼时按帧画,
409
+ * 否则等到下一次眨眼、瞟眼或笑完。运行中由 App 定时重画,用不到这个
410
+ */
411
+ export function nextChange(mood, now, doneAt, moving = false) {
412
+ if (moving)
413
+ return FRAME_MS;
414
+ if (mood === "asking" || mood === "happy") {
415
+ const end = mood === "happy" && doneAt !== undefined ? doneAt + HAPPY_MS - now : Infinity;
416
+ return Math.max(16, Math.min(FRAME_MS, end));
417
+ }
418
+ const tb = now % BLINK_EVERY;
419
+ // 眨眼(含连眨的第二下)期间按帧画
420
+ if (tb < 2 * BLINK_MS + 90)
421
+ return FRAME_MS;
422
+ const tg = now % GLANCE_EVERY;
423
+ const toGlance = tg < GLANCE[0] ? GLANCE[0] - tg : tg < GLANCE[1] ? GLANCE[1] - tg : GLANCE_EVERY - tg + GLANCE[0];
424
+ return Math.max(16, Math.min(BLINK_EVERY - tb, toGlance));
425
+ }
426
+ // ---------- 终端标题 ----------
427
+ /** jade 的标志:一块玉 */
428
+ export const LOGO = "◆";
429
+ const SPIN = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
430
+ /**
431
+ * 终端标签页标题:空闲时是标志,运行中换成转圈,等用户时加感叹号,在别的标签页里也能看出 jade 在做什么。
432
+ * dir 是当前目录的名字
433
+ */
434
+ export function title(mood, dir, now) {
435
+ const mark = mood === "asking" ? `${LOGO}!`
436
+ : mood === "idle" || mood === "happy" ? LOGO
437
+ : SPIN[Math.floor(now / 250) % SPIN.length];
438
+ return `${mark} jade · ${dir}`;
439
+ }
@@ -14,6 +14,24 @@ export function mentionAt(value, cursor) {
14
14
  }
15
15
  /** 补全后的文本:@ 引用加一个空格,含空白的路径加引号 */
16
16
  export const mentionText = (p) => (/\s/.test(p) ? `@"${p}"` : `@${p}`);
17
+ const prepared = new WeakMap();
18
+ function prepare(paths) {
19
+ let out = prepared.get(paths);
20
+ if (!out) {
21
+ out = paths.map((p) => {
22
+ const lower = p.toLowerCase();
23
+ const trimmed = lower.endsWith("/") ? lower.slice(0, -1) : lower;
24
+ return { lower, base: trimmed.slice(trimmed.lastIndexOf("/") + 1) };
25
+ });
26
+ prepared.set(paths, out);
27
+ }
28
+ return out;
29
+ }
30
+ /**
31
+ * 上一次排序时命中的全部候选(下标)。query 只是在后面追加了字符时,新的命中一定在这些里面
32
+ * (包含 q 加字符的也包含 q,按顺序出现的同理),只筛它们,不用再扫一遍全部路径
33
+ */
34
+ let last;
17
35
  /**
18
36
  * 按 query 给候选路径排序:文件名以 query 开头的最前,其次文件名包含、路径包含,
19
37
  * 最后是按顺序出现 query 每个字符的模糊匹配;同一档里短的在前
@@ -22,10 +40,11 @@ export function rankPaths(paths, query, limit) {
22
40
  const q = query.toLowerCase().replaceAll("\\", "/");
23
41
  if (!q)
24
42
  return paths.filter((p) => !p.slice(0, -1).includes("/")).slice(0, limit);
43
+ const info = prepare(paths);
44
+ const from = last && last.paths === paths && q.startsWith(last.q) ? last.hits : undefined;
25
45
  const scored = [];
26
- for (const p of paths) {
27
- const lower = p.toLowerCase();
28
- const base = lower.replace(/\/$/, "").split("/").pop();
46
+ const check = (i) => {
47
+ const { lower, base } = info[i];
29
48
  let score;
30
49
  if (lower.startsWith(q))
31
50
  score = 0;
@@ -38,11 +57,30 @@ export function rankPaths(paths, query, limit) {
38
57
  else if (subsequence(lower, q))
39
58
  score = 4;
40
59
  else
60
+ return;
61
+ scored.push({ i, score });
62
+ };
63
+ if (from)
64
+ for (const i of from)
65
+ check(i);
66
+ else
67
+ for (let i = 0; i < paths.length; i++)
68
+ check(i);
69
+ last = { paths, q, hits: scored.map((s) => s.i) };
70
+ const cmp = (a, b) => a.score - b.score || paths[a.i].length - paths[b.i].length || paths[a.i].localeCompare(paths[b.i]);
71
+ // 只要前 limit 条:命中常有上万条,全部排序(localeCompare 很慢)每按一个键要十几毫秒
72
+ const top = [];
73
+ for (const s of scored) {
74
+ if (top.length === limit && cmp(s, top[limit - 1]) >= 0)
41
75
  continue;
42
- scored.push({ p, score });
76
+ let at = top.length;
77
+ while (at > 0 && cmp(s, top[at - 1]) < 0)
78
+ at--;
79
+ top.splice(at, 0, s);
80
+ if (top.length > limit)
81
+ top.pop();
43
82
  }
44
- scored.sort((a, b) => a.score - b.score || a.p.length - b.p.length || a.p.localeCompare(b.p));
45
- return scored.slice(0, limit).map((s) => s.p);
83
+ return top.map((s) => paths[s.i]);
46
84
  }
47
85
  function subsequence(text, q) {
48
86
  let i = 0;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "jade/tui",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "api": "^1",
5
5
  "entry": "./index.js",
6
6
  "provides": [
@@ -16,7 +16,8 @@
16
16
  "modes@1",
17
17
  "models@1",
18
18
  "compact@1",
19
- "sessions@1"
19
+ "sessions@1",
20
+ "steer@1"
20
21
  ],
21
22
  "frontend": {
22
23
  "name": "tui",
@@ -90,6 +90,8 @@ export function safeScreen(screen) {
90
90
  set: (t) => screen.draft.set(plain(lf(t))),
91
91
  hint: (t) => screen.draft.hint(t === undefined ? undefined : plain(lf(t))),
92
92
  },
93
+ ...(screen.copy ? { copy: (t) => screen.copy(t) } : {}),
94
+ ...(screen.suspend ? { suspend: (fn) => screen.suspend(fn) } : {}),
93
95
  clear: () => screen.clear(),
94
96
  close: () => screen.close(),
95
97
  };
@@ -120,6 +120,8 @@ export function transcript(opts) {
120
120
  };
121
121
  /** 有 result renderer 的调用的参数,结果出来时交给它 */
122
122
  const inputs = new Map();
123
+ /** 等人工审批的调用的参数,回答(input.received)来了按它显示 */
124
+ const asks = new Map();
123
125
  /** 出过错的 renderer(插件 + 工具),只提示一次 */
124
126
  const failed = new Set();
125
127
  /** 调用插件的 renderer;没有、出错时返回 undefined(用默认的显示) */
@@ -187,6 +189,7 @@ export function transcript(opts) {
187
189
  calls.clear();
188
190
  befores.clear();
189
191
  inputs.clear();
192
+ asks.clear();
190
193
  self.lastEnd = undefined;
191
194
  },
192
195
  };
@@ -287,8 +290,10 @@ export function transcript(opts) {
287
290
  calls.set(d.toolUseId, full);
288
291
  if (opts.renderer?.(name)?.renderer.result)
289
292
  inputs.set(d.toolUseId, d.input);
290
- if (d.decision === "ask")
293
+ if (d.decision === "ask") {
294
+ asks.set(d.toolUseId, d.input);
291
295
  return;
296
+ }
292
297
  // 执行前交给 renderer 的 before(例如按文件当时的内容算 diff);重新显示历史时文件已经变了,不调用
293
298
  if (d.decision === "allow" && !replaying) {
294
299
  const before = customSync(name, (r) => r.before?.(d.input));
@@ -318,6 +323,16 @@ export function transcript(opts) {
318
323
  }
319
324
  return;
320
325
  }
326
+ case "input.received": {
327
+ // 对等待审批的调用的回答:和关卡直接决定的一样显示
328
+ const d = e.data;
329
+ if (d.kind !== "approval")
330
+ return;
331
+ const input = asks.get(d.toolUseId);
332
+ asks.delete(d.toolUseId);
333
+ const decided = { toolUseId: d.toolUseId, decision: d.approved ? "allow" : "deny", input, ...(d.approved ? {} : { reason: d.reason }) };
334
+ return renderEvent({ ...e, type: "tool.decided", data: decided });
335
+ }
321
336
  case "tool.completed": {
322
337
  const d = e.data;
323
338
  void countUsage(e);
@@ -376,7 +391,9 @@ export function transcript(opts) {
376
391
  else if (d.reason === "aborted")
377
392
  screen.log(yellow("[interrupted]"));
378
393
  else if (d.reason === "max_tokens") {
379
- const limit = process.env.JADE_MAX_TOKENS ? `${process.env.JADE_MAX_TOKENS} tokens` : "the provider's default";
394
+ // 不读 process.env:写在 $JADE_HOME/.env 里的只有 models 插件知道
395
+ const max = opts.maxTokens?.();
396
+ const limit = max ? `${max} tokens` : "the provider's default";
380
397
  screen.log(yellow(`[reply cut off at the output limit (${limit}); raise it with /model if this keeps happening]`));
381
398
  }
382
399
  else if (d.reason !== "end")