@trans-ai/trans-code 0.10.3 → 0.10.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trans-ai/trans-code",
3
- "version": "0.10.3",
3
+ "version": "0.10.4",
4
4
  "description": "AI-powered multi-model coding CLI",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
package/src/main.tsx CHANGED
@@ -13,7 +13,7 @@ function Banner({ modelId, cwd, envLabel }: { modelId: string; cwd: string; envL
13
13
  const models = getAvailableModels();
14
14
  const current = models.find(m => m.id === modelId);
15
15
 
16
- const logo = bannerLines('T R A N S C O D E · Multi-model Agent');
16
+ const logo = bannerLines('T R A N S C O D E · Multi-Model Agent');
17
17
 
18
18
  return (
19
19
  <Box flexDirection="column" marginBottom={1}>
@@ -49,6 +49,7 @@ import {
49
49
  import { resolveSubmitInput } from './submit.js';
50
50
  import { submitSearchApiKey } from './search-setup.js';
51
51
  import { reduceQueue, shouldResumeQueue, type QueueAction } from './input-queue.js';
52
+ import { createStreamThrottler } from './stream-throttle.js';
52
53
  import { historyRule } from './history-rule.js';
53
54
  import { renderDiff } from './diff-render.js';
54
55
  import { createTurnStats, recordCall, renderTurnLog, shouldCollapse, summaryLine } from './turn-summary.js';
@@ -348,6 +349,21 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
348
349
  });
349
350
  }, []);
350
351
 
352
+ /**
353
+ * Frame-level coalescing for streaming output. Stock Ink re-renders
354
+ * synchronously per setState, so per-delta appends rewrite the whole
355
+ * history frame dozens of times per second — that is the flicker.
356
+ * Everything within a turn flows through here and is flushed at turn
357
+ * end (order preserved; ≤32ms latency, invisible).
358
+ */
359
+ const streamAppendRef = useRef<ReturnType<typeof createStreamThrottler> | null>(null);
360
+ if (streamAppendRef.current === null) {
361
+ streamAppendRef.current = createStreamThrottler(appendChatOutput, 32);
362
+ }
363
+ const streamAppend = (chunk: string): void => {
364
+ streamAppendRef.current?.push(chunk);
365
+ };
366
+
351
367
  /**
352
368
  * One shared compaction run for /resume and /compact: fast-model routing
353
369
  * (routing.compact → fallback current model) with the digest streamed
@@ -363,11 +379,13 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
363
379
  record,
364
380
  createModel(getRouting().compact ?? ctx.modelId).model,
365
381
  keepTurns,
366
- (delta) => appendChatOutput(delta),
382
+ (delta) => streamAppend(delta),
367
383
  );
384
+ streamAppendRef.current?.flush();
368
385
  appendChatOutput('\n\n');
369
386
  return outcome;
370
387
  } catch (error) {
388
+ streamAppendRef.current?.flush();
371
389
  appendChatOutput('\n\n');
372
390
  return {
373
391
  compressed: false,
@@ -510,11 +528,11 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
510
528
  toolSession: toolSessionRef.current,
511
529
  onEvent: event => {
512
530
  if (event.type === 'text') {
513
- appendChatOutput(lastWasTool ? `\n${event.text}` : event.text);
531
+ streamAppend(lastWasTool ? `\n${event.text}` : event.text);
514
532
  turnAssistantText += event.text;
515
533
  lastWasTool = false;
516
534
  } else if (event.type === 'max_turns_reached') {
517
- appendChatOutput(
535
+ streamAppend(
518
536
  `\n⚠ 已达轮次上限(${event.maxTurns}),任务未完成。输入"继续"接着做,或调大 TRANSCODE_MAX_TURNS。`,
519
537
  );
520
538
  lastWasTool = true;
@@ -532,10 +550,10 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
532
550
  );
533
551
  if (event.diff) {
534
552
  // Edits and writes stay visible immediately — rare and important.
535
- appendChatOutput(`\n⚙ ${title} ${marker}\n${renderDiff(event.diff)}`);
553
+ streamAppend(`\n⚙ ${title} ${marker}\n${renderDiff(event.diff)}`);
536
554
  } else if (event.name === 'todo_write' || event.name === 'dispatch_agent') {
537
555
  // Progress state and sub-agent reports: show immediately.
538
- appendChatOutput(`\n${event.output}`);
556
+ streamAppend(`\n${event.output}`);
539
557
  } else {
540
558
  bufferedLines.push(`\n⚙ ${title} ${marker}`);
541
559
  }
@@ -549,10 +567,10 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
549
567
  // Collapsed detail is kept (hiddenLines) so ctrl+o can reveal it.
550
568
  if (shouldCollapse(stats)) {
551
569
  const line = summaryLine(stats);
552
- if (line) appendChatOutput(`\n${line}`);
570
+ if (line) streamAppend(`\n${line}`);
553
571
  attachHiddenLines(bufferedLines);
554
572
  } else {
555
- for (const line of bufferedLines) appendChatOutput(line);
573
+ for (const line of bufferedLines) streamAppend(line);
556
574
  }
557
575
 
558
576
  messagesRef.current = result.messages.filter(
@@ -604,17 +622,19 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
604
622
  })();
605
623
  }
606
624
  if (controller.signal.aborted) {
607
- appendChatOutput('\n(cancelled)');
625
+ streamAppend('\n(cancelled)');
608
626
  } else {
609
627
  endedNaturally = true;
610
628
  }
611
629
  } catch (error) {
612
630
  const message = error instanceof Error ? error.message : String(error);
613
- appendChatOutput(`\n⚠ ${message}`);
631
+ streamAppend(`\n⚠ ${message}`);
614
632
  if (/api.?key/i.test(message)) {
615
- appendChatOutput(`\n(run "models" to switch model + set key, or export its env var)`);
633
+ streamAppend(`\n(run "models" to switch model + set key, or export its env var)`);
616
634
  }
617
635
  } finally {
636
+ // Drain any coalesced tail before busy resets and the queue continues.
637
+ streamAppendRef.current?.flush();
618
638
  setBusyBoth(false);
619
639
  abortRef.current = undefined;
620
640
  setLiveTool(null);
@@ -3,7 +3,7 @@ import { bannerLines } from './banner.js';
3
3
 
4
4
  describe('bannerLines', () => {
5
5
  test('renders a closed rectangle with aligned sides', () => {
6
- const lines = bannerLines('T R A N S C O D E · Multi-model Agent').split('\n');
6
+ const lines = bannerLines('T R A N S C O D E · Multi-Model Agent').split('\n');
7
7
  expect(lines.length).toBe(3);
8
8
  expect(lines[0]!.startsWith('┌')).toBe(true);
9
9
  expect(lines[0]!.endsWith('┐')).toBe(true);
@@ -0,0 +1,76 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import { createStreamThrottler } from './stream-throttle.js';
3
+
4
+ interface Harness {
5
+ push: (chunk: string) => void;
6
+ flush: () => void;
7
+ appended: string[];
8
+ tick: () => void;
9
+ pendingTimer: () => boolean;
10
+ }
11
+
12
+ function harness(intervalMs = 32): Harness {
13
+ const appended: string[] = [];
14
+ let timer: (() => void) | null = null;
15
+ const throttler = createStreamThrottler((text) => appended.push(text), intervalMs, {
16
+ setTimer: (fn) => {
17
+ timer = fn;
18
+ return 0;
19
+ },
20
+ clearTimer: () => {
21
+ timer = null;
22
+ },
23
+ });
24
+ return {
25
+ ...throttler,
26
+ appended,
27
+ tick: () => {
28
+ const fn = timer;
29
+ timer = null;
30
+ fn?.();
31
+ },
32
+ pendingTimer: () => timer !== null,
33
+ };
34
+ }
35
+
36
+ describe('createStreamThrottler', () => {
37
+ test('窗口内多个 delta 合并为一次追加(内容按序拼接)', () => {
38
+ const h = harness();
39
+ h.push('He');
40
+ h.push('llo ');
41
+ h.push('world');
42
+ expect(h.appended).toEqual([]);
43
+ h.tick();
44
+ expect(h.appended).toEqual(['Hello world']);
45
+ });
46
+
47
+ test('定时器窗口外的新 delta 开新窗口', () => {
48
+ const h = harness();
49
+ h.push('a');
50
+ h.tick();
51
+ h.push('b');
52
+ h.push('c');
53
+ h.tick();
54
+ expect(h.appended).toEqual(['a', 'bc']);
55
+ });
56
+
57
+ test('flush 立即排水并取消挂起的定时器;空缓冲 flush 无操作', () => {
58
+ const h = harness();
59
+ h.push('x');
60
+ h.push('y');
61
+ h.flush();
62
+ expect(h.appended).toEqual(['xy']);
63
+ expect(h.pendingTimer()).toBe(false);
64
+ h.flush();
65
+ expect(h.appended).toEqual(['xy']);
66
+ });
67
+
68
+ test('flush 后再 push 正常开新窗口', () => {
69
+ const h = harness();
70
+ h.push('1');
71
+ h.flush();
72
+ h.push('2');
73
+ h.tick();
74
+ expect(h.appended).toEqual(['1', '2']);
75
+ });
76
+ });
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Frame-level throttle for streaming output. Stock Ink re-renders
3
+ * synchronously on every setState — per-delta appends (dozens per second,
4
+ * each rewriting the whole history frame) are what makes the screen
5
+ * flicker. Coalescing deltas into one append per interval window cuts the
6
+ * render rate to frame level without changing output content or order.
7
+ */
8
+
9
+ export interface ThrottleTimers {
10
+ setTimer: (fn: () => void, ms: number) => unknown;
11
+ clearTimer: (handle: unknown) => void;
12
+ }
13
+
14
+ const REAL_TIMERS: ThrottleTimers = {
15
+ setTimer: (fn, ms) => setTimeout(fn, ms),
16
+ clearTimer: (handle) => clearTimeout(handle as ReturnType<typeof setTimeout>),
17
+ };
18
+
19
+ export function createStreamThrottler(
20
+ append: (text: string) => void,
21
+ intervalMs = 32,
22
+ timers: ThrottleTimers = REAL_TIMERS,
23
+ ): { push: (chunk: string) => void; flush: () => void } {
24
+ let buffer = '';
25
+ let pending: unknown = null;
26
+
27
+ const drain = (): void => {
28
+ if (pending !== null) {
29
+ timers.clearTimer(pending);
30
+ pending = null;
31
+ }
32
+ if (!buffer) return;
33
+ const text = buffer;
34
+ buffer = '';
35
+ append(text);
36
+ };
37
+
38
+ return {
39
+ push(chunk) {
40
+ buffer += chunk;
41
+ if (pending === null) {
42
+ pending = timers.setTimer(drain, intervalMs);
43
+ }
44
+ },
45
+ flush: drain,
46
+ };
47
+ }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Auto-generated from package.json by scripts/sync-version.ts (npm version hook)
2
- export const VERSION = '0.10.3';
2
+ export const VERSION = '0.10.4';