mocode-ai 1.9.3 → 1.9.5

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.
@@ -6,7 +6,7 @@ import { ui } from '../theme.js';
6
6
  import * as content from '../content.js';
7
7
  import { t } from '../../i18n/index.js';
8
8
  import { state } from './state.js';
9
- import { esc, cup, getGeo, runningCaretPos, isStreamingPaused } from './screen.js';
9
+ import { esc, cup, getGeo, setRegion, runningCaretPos, isStreamingPaused } from './screen.js';
10
10
  const RUNNING_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
11
11
  // ── 状态行(两行式:spinner 行 + model 行)──
12
12
  const STATUS_SEP = ' ';
@@ -171,33 +171,202 @@ function formatLiveUsageChip(u) {
171
171
  const cacheTag = cached > 0 ? ` ↻ ${fmt(cached)}` : '';
172
172
  return `${ui.dim}↑ ${fmt(billable)} ↓ ${fmt(u.completionTokens)}${cacheTag}${ui.reset}`;
173
173
  }
174
- /** spinner 行上方的「虚拟空行」(contentBottom+1)。
175
- * - 有活跃 plan:显「plan: <summary> ▸ N. step」整行左对齐(yellow)
174
+ /** plan chip 步骤行的缩进(箭头 1 + 空格 1,步骤行对齐到摘要标题之后两列)。 */
175
+ const PLAN_STEP_INDENT = ' ';
176
+ /** 判断屏幕行是否落在 plan chip 占用的脚栏行(contentBottom+1 .. +planRows)。鼠标命中用。 */
177
+ export function isPlanRow(screenRow) {
178
+ if (!state.active || !state.base?.planSummary)
179
+ return false;
180
+ const g = getGeo();
181
+ return screenRow >= g.contentBottom + 1 && screenRow <= g.contentBottom + state.planRows;
182
+ }
183
+ /** 按显示宽度贪心折行(优先在空格断,宽字符/CJK 整字不拆)。plan 步骤详情用;纯展示,不处理 ANSI。 */
184
+ function wrapDisplayText(text, width) {
185
+ const out = [];
186
+ let line = '';
187
+ let lastSpace = -1; // line 内最后一个空格的 code-unit 下标
188
+ for (const ch of text.replace(/\s+/g, ' ').trim()) {
189
+ if (line && displayWidth(line) + displayWidth(ch) > width) {
190
+ if (lastSpace > 0) {
191
+ out.push(line.slice(0, lastSpace));
192
+ line = line.slice(lastSpace + 1);
193
+ }
194
+ else {
195
+ out.push(line);
196
+ line = '';
197
+ }
198
+ lastSpace = -1;
199
+ }
200
+ if (ch === ' ') {
201
+ if (line) {
202
+ lastSpace = line.length;
203
+ line += ch;
204
+ }
205
+ }
206
+ else {
207
+ line += ch;
208
+ }
209
+ }
210
+ if (line)
211
+ out.push(line);
212
+ return out;
213
+ }
214
+ /**
215
+ * spinner 行上方的「虚拟空行」(contentBottom+1)。
216
+ * - 有活跃 plan 且折叠:显「▸ plan: <summary> (N/M) ▸ 当前步」整行(yellow),过长截 1 行 + "…"
217
+ * - 有活跃 plan 且展开(点击标题行切换):首行「▾ plan: <标题> (N/M)」,下面逐行列全部步骤
218
+ * 「✓ N. label」(完成,dim green)/「▸ N. label」(当前,accent)/「· N. label」(待办,dim);
219
+ * 步骤带 detail 时行尾加 ▸ 提示可点,点击该步在下方缩进展开详情(dim,按宽折行),再点收起。
220
+ * 总行数按脚栏剩余高度(maxRows)钳制,放不下时末行显「… 还有 K 步」。
176
221
  * - 无活跃 plan:空(保留原分隔视觉,避免内容贴输入区)
177
- * - 过长(> cols):始终**截断保 1 行 + "…"**,不撑高脚栏、不拆 2 行——
178
- * 步进式任务标题/current step 通常是自然语言,即使切「 ▸ 」拆 2 行,第 2 行也大概率仍超长
179
- * (实测「第三步:实现核心业务模块。…」本身就比 cols 长),拆完依然覆盖 spinner/输入区,得不偿失。
180
- * 截断 + "…" 一行内永远不溢出,脚栏恒 6 行,spinner/输入/下线/model 行偏移稳定。
181
- * 这行在 DECSTBM 滚动区外([1, contentBottom]),稳定不滚。 */
182
- export function composePlanLines(status, cols) {
222
+ * 脚栏高度 = 5 + 输入行 + (planRows-1),spinner/输入/下线/model 行偏移稳定。
223
+ * 这组行在 DECSTBM 滚动区外([1, contentBottom]),稳定不滚。
224
+ * 副作用:重建 state.planRowStep(行→步骤号映射,鼠标点击路由用)。
225
+ * @param maxRows plan 区域允许占用的最大行数;不传时按屏幕高度保守钳(只防顶到屏顶)。
226
+ */
227
+ export function composePlanLines(status, cols, maxRows) {
183
228
  const plan = (status.planSummary ?? '').trim();
184
229
  if (!plan) {
185
230
  state.planRows = 1;
231
+ state.planExpanded = false;
232
+ state.planStateKey = undefined;
233
+ state.planRowStep = [];
186
234
  return [];
187
235
  }
188
- const w = displayWidth(stripAnsi(plan));
189
- if (w <= cols) {
236
+ // plan 身份变化(新计划 / 结算后新计划):自动收起展开态并清空步骤详情展开集,
237
+ // 避免把旧计划的多行展开套到新计划上。
238
+ if (status.planKey !== state.planStateKey) {
239
+ state.planStateKey = status.planKey;
240
+ state.planExpanded = false;
241
+ state.planOpenSteps.clear();
242
+ }
243
+ const collapsedLine = (caret) => {
244
+ const w = displayWidth(stripAnsi(plan)) + 2; // 箭头 + 空格
245
+ // 颜色必须套在 plan 文本上且在 caret 之后:caret 自带 reset,若 yellow 放在
246
+ // caret 前面会被它清掉,plan 文本就裸奔成默认白(展开态首行尤明显)。
247
+ if (w <= cols)
248
+ return `${caret} ${ui.yellow}${plan}${ui.reset}`;
249
+ return `${caret} ${ui.yellow}${truncateDisplay(plan, Math.max(1, cols - 3))}…${ui.reset}`;
250
+ };
251
+ if (!state.planExpanded) {
190
252
  state.planRows = 1;
191
- return [`${ui.yellow}${plan}${ui.reset}`];
253
+ state.planRowStep = [null];
254
+ return [collapsedLine(`${ui.dim}▸${ui.reset}`)];
192
255
  }
193
- // 溢出:截断为 1 行 + "…" 后缀(留 1 列空间给省略号)。
194
- state.planRows = 1;
195
- return [`${ui.yellow}${truncateDisplay(plan, Math.max(1, cols - 1))}…${ui.reset}`];
256
+ // ── 展开态:标题行 + 每步一行 + 点开的步骤渲染详情(钳高)──
257
+ const steps = status.planSteps ?? [];
258
+ // 不传 maxRows(drawStatusBar 路径):按屏幕高度保守钳,至少 2 行(标题 + 1 步);
259
+ // 传了(paintInput 路径,已知脚栏预算):精确钳,保证不覆盖 spinner/输入区。
260
+ const cap = Math.max(2, maxRows ?? Math.max(2, getGeo().rows - 5 - state.inputRows));
261
+ const header = collapsedLine(`${ui.accent}▾${ui.reset}`);
262
+ const lines = [header];
263
+ state.planRowStep = [null];
264
+ // 详情详情宽 = 减去详情缩进 4 列;先算好每个点开步骤的详情折行,供预算裁剪。
265
+ const detailIndent = ' ';
266
+ const detailOf = new Map();
267
+ for (const s of steps) {
268
+ if (s.detail && state.planOpenSteps.has(s.no)) {
269
+ detailOf.set(s.no, wrapDisplayText(s.detail, Math.max(8, cols - displayWidth(detailIndent))).map((l) => `${detailIndent}${ui.dim}${l}${ui.reset}`));
270
+ }
271
+ }
272
+ // 有步骤被裁掉时,末行要留给「… 还有 K 步」指示:先按「全部装得下」试探,
273
+ // 装不下则预扣 1 行给指示,再走一遍裁剪(保证 planRows ≤ cap)。
274
+ let budget = cap - 1;
275
+ const fits = (b) => {
276
+ let left = b;
277
+ const vis = [];
278
+ let hidden = 0;
279
+ for (const s of steps) {
280
+ const need = 1 + (detailOf.get(s.no)?.length ?? 0);
281
+ if (need <= left) {
282
+ vis.push(s);
283
+ left -= need;
284
+ }
285
+ else {
286
+ hidden = steps.length - vis.length;
287
+ break;
288
+ }
289
+ }
290
+ return { visible: vis, hidden };
291
+ };
292
+ let { visible, hidden } = fits(budget);
293
+ if (hidden > 0 && visible.length > 0) {
294
+ // 预扣指示行后重裁;若连一个步骤都装不下(极端矮终端),放弃指示行保步骤。
295
+ const retry = fits(budget - 1);
296
+ if (retry.visible.length > 0) {
297
+ visible = retry.visible;
298
+ hidden = retry.hidden;
299
+ budget -= 1;
300
+ }
301
+ }
302
+ for (const s of visible) {
303
+ const mark = s.done ? `${ui.green}✓${ui.reset}` : s.current ? `${ui.accent}▸${ui.reset}` : `${ui.dim}·${ui.reset}`;
304
+ // 前缀 = 缩进2 + 标记1 + 空格1 + "N. "(编号位数 + 2);label 吃掉剩余列,保证整行不 wrap。
305
+ const prefixW = displayWidth(PLAN_STEP_INDENT) + 1 + 1 + String(s.no).length + 2;
306
+ const open = detailOf.has(s.no);
307
+ const hasDetail = s.detail != null && s.detail !== '';
308
+ // 行尾可展开提示:有详情未展开 ▸ / 已展开 ▾(与标题行的箭头语义一致)。
309
+ const endCaret = !hasDetail ? '' : open ? ` ${ui.dim}▾${ui.reset}` : ` ${ui.dim}▸${ui.reset}`;
310
+ const labelW = Math.max(1, cols - prefixW - (hasDetail ? 2 : 0));
311
+ const label = truncateDisplay(s.label, labelW);
312
+ const color = s.done ? ui.dim : s.current ? ui.yellow : ui.dim;
313
+ lines.push(`${PLAN_STEP_INDENT}${mark} ${color}${s.no}. ${label}${ui.reset}${endCaret}`);
314
+ state.planRowStep.push(s.no);
315
+ const d = detailOf.get(s.no);
316
+ if (d) {
317
+ lines.push(...d);
318
+ for (let i = 0; i < d.length; i++)
319
+ state.planRowStep.push(s.no); // 点详情行=再点收起
320
+ }
321
+ }
322
+ if (hidden > 0) {
323
+ lines.push(`${PLAN_STEP_INDENT}${ui.dim}… ${t('status.planMoreSteps', { count: hidden })}${ui.reset}`);
324
+ state.planRowStep.push(null); // 指示行不可点
325
+ }
326
+ // 极端情况下 steps 为空(数据缺失):只有标题行,仍能点击收起。
327
+ state.planRows = Math.max(1, lines.length);
328
+ return lines;
329
+ }
330
+ /** plan 区域高度变化(展开/收起/结算)后的全幅重排回调,由 core.ts 注入
331
+ * (paintInput 同步重设 DECSTBM + 重画脚栏):statusbar 不能静态 import input-paint(循环依赖),
332
+ * 也不能用动态 import——异步窗口内 80ms 心跳会按新 planRows + 旧 footerH 画错行。 */
333
+ let planRelayout = null;
334
+ export function setPlanRelayoutHandler(fn) {
335
+ planRelayout = fn;
336
+ }
337
+ /** 切换 plan chip 展开/收起(鼠标点击标题行)。无活跃 plan / 无步骤数据时 no-op。
338
+ * 经注入的同步回调重设区域并重画整个脚栏;回调缺失时退化为 drawStatusBar。
339
+ * 收起时保留 planOpenSteps(重新展开还原各步详情展开状态),换计划才清空。 */
340
+ export function togglePlanExpanded() {
341
+ if (!state.active || !state.base?.planSummary)
342
+ return;
343
+ if (!state.planExpanded && !(state.base.planSteps && state.base.planSteps.length > 0))
344
+ return;
345
+ state.planExpanded = !state.planExpanded;
346
+ if (state.lastView && planRelayout)
347
+ planRelayout();
348
+ else
349
+ drawStatusBar();
350
+ }
351
+ /** 切换单个步骤的详情展开/收起(鼠标点击步骤行/详情行)。无详情数据时 no-op。 */
352
+ export function togglePlanDetail(stepNo) {
353
+ if (!state.active || !state.planExpanded)
354
+ return;
355
+ const step = state.base?.planSteps?.find((s) => s.no === stepNo);
356
+ if (!step?.detail)
357
+ return; // 无详情可看,点了也不给反应
358
+ if (state.planOpenSteps.has(stepNo))
359
+ state.planOpenSteps.delete(stepNo);
360
+ else
361
+ state.planOpenSteps.add(stepNo);
362
+ if (state.lastView && planRelayout)
363
+ planRelayout();
364
+ else
365
+ drawStatusBar();
196
366
  }
197
- /** 画状态行(plan 行 + spinner 行 + model 行,三行)。RUNNING 态 spinner 频繁调。
198
- * 行号(footerH=6 当 plan 单行;footerH=7 当 plan 撑 2 行):
199
- * plan 行 = contentBottom+1 (单行)
200
- * plan 第 2 行 = contentBottom+2 (双行,可选)
367
+ /** 画状态行(plan 行 + spinner 行 + model 行)。RUNNING 态 spinner 频繁调。
368
+ * 行号(footerH=5+inputRows+planRows-1):
369
+ * plan 行 1..N = contentBottom+1 .. +planRows (折叠 1 行;展开 = 标题 + 步骤行)
201
370
  * spinner 行 = contentBottom+1+planRows (● 空闲 / ⠹ 思考中… / etc)
202
371
  * 上线 = contentBottom+2+planRows (画在 paintInput)
203
372
  * 输入行 = contentBottom+3+planRows
@@ -209,20 +378,29 @@ export function drawStatusBar(status) {
209
378
  // 覆盖层(composer)打开中照常刷底栏:弹窗只占内容区(2..contentBottom-1),底栏行在其外不冲突;
210
379
  // 仅末尾光标归位改为弹窗自报位(见下),防 80ms 心跳把弹窗光标拽到底栏输入框。
211
380
  const s = status ?? { ...state.base, status: state.statusText, spinnerFrame: state.spinnerFrame };
381
+ const g0 = getGeo();
382
+ const planLines = composePlanLines(s, g0.cols);
383
+ // planRows 与当前 footerH 不符(plan 在运行中被结算/换新,composePlanLines 自动收起展开态等):
384
+ // 心跳路径不会走 paintInput 的 setRegion,这里重设区域并交回调全幅重排(含被吃掉/归还的内容末行),
385
+ // 不能只画 spinner/model——旧上分隔线、输入行会残留在新行位。
386
+ const needFooterH = 5 + state.inputRows + (state.planRows - 1);
387
+ if (needFooterH !== g0.footerH) {
388
+ setRegion(needFooterH);
389
+ planRelayout?.();
390
+ return;
391
+ }
212
392
  const g = getGeo();
213
- const planLines = composePlanLines(s, g.cols);
214
393
  const planRow1 = g.contentBottom + 1;
215
394
  const spinnerRow = g.contentBottom + 1 + state.planRows;
216
395
  const modelRow = g.rows; // 屏底:model 行
217
- // plan 可能占 1 或 2 行(过长自动撑开);spinner/model 行随之平移。
218
- // 一次写入:plan 1/2 行 + spinner 行 + model 行,末尾 cup 回续写位/输入框光标。
396
+ // 一次写入:plan 各行 + spinner 行 + model 行,末尾 cup 回续写位/输入框光标。
219
397
  let planBuf = '';
220
398
  for (let i = 0; i < planLines.length; i++) {
221
399
  planBuf += cup(planRow1 + i, 1) + esc.clearLine + planLines[i];
222
400
  }
223
- // 计划行从 2 行降到 1 行时,清掉残留的第 2 行(撑高后回退不留尾巴)。
224
- if (state.planRows === 1 && g.footerH > 6) {
225
- planBuf += cup(planRow1 + 1, 1) + esc.clearLine;
401
+ // 上轮 plan 占的行数比本轮多(步骤变少,展开态被钳短):清掉本轮不再使用的残留行,不留尾巴。
402
+ for (let r = planRow1 + planLines.length; r < spinnerRow; r++) {
403
+ planBuf += cup(r, 1) + esc.clearLine;
226
404
  }
227
405
  // plan 从非空变成空(已结算为 ## Done:/ notes 里无活跃 ## Plan: 段)时,清掉残留的 plan 行 1。
228
406
  // 否则上轮渲染的「plan: 标题 (N/M) ▸ 当前步」会卡在底栏「虚拟空行」位置直到下一次重启 REPL。
@@ -1 +1,2 @@
1
+ // 从 ui/layout.ts 提取的类型定义
1
2
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mocode-ai",
3
- "version": "1.9.3",
3
+ "version": "1.9.5",
4
4
  "description": "终端编码 agent:LLM + tool-call 循环 + 流式输出(含思考)+ 27 个工具,接任意 OpenAI 兼容后端。",
5
5
  "type": "module",
6
6
  "bin": {