u-foo 3.1.7 → 4.0.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 (193) hide show
  1. package/README.md +172 -13
  2. package/README.zh-CN.md +108 -13
  3. package/SKILLS/ufoo/SKILL.md +15 -3
  4. package/SKILLS/ufoo-bus/SKILL.md +21 -1
  5. package/bin/uclaude.js +4 -0
  6. package/bin/ucodex.js +4 -0
  7. package/bin/ufoo.js +3 -1
  8. package/dist/tui/darwin-arm64/ufoo-tui +0 -0
  9. package/dist/tui/darwin-x64/ufoo-tui +0 -0
  10. package/dist/tui/linux-arm64/ufoo-tui +0 -0
  11. package/dist/tui/linux-x64/ufoo-tui +0 -0
  12. package/package.json +10 -3
  13. package/scripts/postinstall-skills.js +53 -0
  14. package/scripts/postinstall.js +11 -16
  15. package/scripts/smoke-native-messages.js +214 -0
  16. package/src/agents/activity/activityStateWriter.js +37 -38
  17. package/src/agents/capabilities/agentManagement/index.js +4 -0
  18. package/src/agents/capabilities/agentManagement/service.js +113 -0
  19. package/src/agents/capabilities/agentRouting/index.js +4 -0
  20. package/src/agents/capabilities/coding/index.js +32 -0
  21. package/src/agents/capabilities/coordination/index.js +37 -0
  22. package/src/agents/capabilities/globalRouting/index.js +5 -0
  23. package/src/agents/capabilities/groups/index.js +3 -0
  24. package/src/agents/capabilities/index.js +25 -0
  25. package/src/agents/capabilities/planning/index.js +123 -0
  26. package/src/agents/capabilities/planning/main.js +28 -0
  27. package/src/agents/capabilities/projectDiscovery/index.js +3 -0
  28. package/src/agents/capabilities/scheduling/index.js +3 -0
  29. package/src/agents/capabilities/sharedMemory/index.js +4 -0
  30. package/src/agents/capabilities/skills/index.js +12 -0
  31. package/src/agents/capabilities/taskExecution/index.js +3 -0
  32. package/src/agents/capabilities/taskReports/index.js +6 -0
  33. package/src/agents/controller/controllerToolExecutor.js +6 -4
  34. package/src/agents/controller/loopRuntime.js +85 -221
  35. package/src/agents/controller/ufooAgent.js +1 -0
  36. package/src/agents/internal/internalRunner.js +103 -32
  37. package/src/agents/launch/launcher.js +88 -6
  38. package/src/agents/launch/nativeCapabilities.js +40 -0
  39. package/src/agents/launch/nativeMessages.js +362 -0
  40. package/src/agents/launch/notifier.js +13 -18
  41. package/src/agents/launch/readyDetector.js +26 -12
  42. package/src/agents/profiles/coding.js +6 -0
  43. package/src/agents/profiles/globalRouter.js +2 -0
  44. package/src/agents/profiles/main.js +5 -0
  45. package/src/agents/prompts/groupBootstrap.js +10 -0
  46. package/src/agents/prompts/mainAgent.js +39 -0
  47. package/src/agents/prompts/native/tasks.js +2 -2
  48. package/src/agents/prompts/native/ufoo.js +3 -0
  49. package/src/agents/providers/claudeEventTranslator.js +6 -1
  50. package/src/agents/providers/claudeThreadProvider.js +44 -2
  51. package/src/agents/providers/codexEventTranslator.js +47 -7
  52. package/src/agents/providers/codexThreadProvider.js +3 -2
  53. package/src/agents/providers/controllerJsonTransport.js +28 -0
  54. package/src/agents/providers/credentials/claude.js +10 -3
  55. package/src/agents/providers/nativeTransport.js +1135 -0
  56. package/src/agents/providers/runtimeConfig.js +186 -0
  57. package/src/agents/providers/thinkingLevels.js +132 -0
  58. package/src/agents/providers/transports/anthropicMessagesTransport.js +121 -0
  59. package/src/agents/providers/transports/index.js +10 -0
  60. package/src/agents/providers/transports/openaiChatTransport.js +117 -0
  61. package/src/agents/providers/transports/openaiResponsesTransport.js +120 -0
  62. package/src/agents/providers/transports/responsesProtocol.js +499 -0
  63. package/src/agents/providers/transports/systemBlocks.js +16 -0
  64. package/src/agents/providers/transports/transportContract.js +50 -0
  65. package/src/agents/providers/transports/visionBlocks.js +151 -0
  66. package/src/agents/providers/upstreamTransport.js +13 -4
  67. package/src/agents/runtime/composeCapabilities.js +89 -0
  68. package/src/agents/runtime/context/commandStore.js +43 -0
  69. package/src/agents/runtime/context/contextWindow.js +119 -0
  70. package/src/agents/runtime/context/fileLock.js +36 -0
  71. package/src/agents/runtime/context/runtimeStore.js +126 -0
  72. package/src/agents/runtime/context/sessionJournal.js +360 -0
  73. package/src/agents/runtime/context/sessionStore.js +57 -0
  74. package/src/agents/runtime/core/agentLoop.js +270 -0
  75. package/src/agents/runtime/core/stableJson.js +29 -0
  76. package/src/agents/runtime/core/toolLoopGuard.js +55 -0
  77. package/src/agents/runtime/createAgentRuntime.js +247 -0
  78. package/src/agents/runtime/index.js +14 -0
  79. package/src/agents/runtime/protocol/faultHarness.js +90 -0
  80. package/src/agents/runtime/protocol/materialize.js +107 -0
  81. package/src/agents/runtime/protocol/toolCallLedger.js +221 -0
  82. package/src/agents/runtime/tasks/resourceLease.js +66 -0
  83. package/src/agents/runtime/tasks/scheduler.js +73 -0
  84. package/src/agents/runtime/tools/registry.js +57 -0
  85. package/src/app/chat/ChatController.js +14 -3
  86. package/src/app/chat/agentDirectory.js +8 -2
  87. package/src/app/chat/commandExecutor.js +36 -46
  88. package/src/app/chat/commands.js +14 -7
  89. package/src/app/chat/daemonConnection.js +58 -5
  90. package/src/app/chat/daemonMessageRouter.js +61 -3
  91. package/src/app/chat/inputSubmitHandler.js +14 -2
  92. package/src/app/chat/launchRequestContext.js +42 -0
  93. package/src/app/chat/multiWindow/paneManager.js +85 -165
  94. package/src/app/chat/streamState.js +3 -3
  95. package/src/app/chat/transport.js +1 -1
  96. package/src/app/cli/busCoreCommands.js +9 -0
  97. package/src/app/cli/features/doctor.js +33 -0
  98. package/src/app/cli/features/skills.js +16 -4
  99. package/src/app/cli/reportCoreCommands.js +37 -0
  100. package/src/app/cli/run.js +40 -39
  101. package/src/code/agent.js +16 -4
  102. package/src/code/context/artifacts.js +12 -11
  103. package/src/code/context/assembler.js +2 -1
  104. package/src/code/context/promptLayers.js +1 -13
  105. package/src/code/context/stableJson.js +2 -27
  106. package/src/code/contextWindow.js +2 -117
  107. package/src/code/conversation/sessionJournal.js +3 -349
  108. package/src/code/internalThread.js +100 -0
  109. package/src/code/nativeRunner.js +36 -2667
  110. package/src/code/protocol/faultHarness.js +2 -88
  111. package/src/code/protocol/materialize.js +2 -105
  112. package/src/code/protocol/toolCallLedger.js +5 -215
  113. package/src/code/providers/anthropicMessagesTransport.js +2 -118
  114. package/src/code/providers/openaiChatTransport.js +2 -114
  115. package/src/code/providers/openaiResponsesTransport.js +2 -117
  116. package/src/code/providers/responsesProtocol.js +2 -497
  117. package/src/code/providers/transportContract.js +2 -48
  118. package/src/code/providers/visionBlocks.js +2 -149
  119. package/src/code/sessionStore.js +24 -96
  120. package/src/code/thinkingLevels.js +1 -130
  121. package/src/code/toolLoopGuard.js +3 -49
  122. package/src/code/tools/artifactRead.js +1 -1
  123. package/src/code/tools/executor.js +373 -0
  124. package/src/code/tools/specs.js +312 -0
  125. package/src/config.js +81 -67
  126. package/src/coordination/bus/deliveryQueue.js +16 -2
  127. package/src/coordination/bus/index.js +2 -6
  128. package/src/coordination/bus/inject.js +13 -5
  129. package/src/coordination/bus/nativeReceipts.js +100 -0
  130. package/src/coordination/bus/queue.js +1 -3
  131. package/src/coordination/bus/utils.js +7 -3
  132. package/src/coordination/history/agentSurface.js +142 -0
  133. package/src/coordination/history/controllerSummary.js +23 -0
  134. package/src/coordination/memory/index.js +5 -21
  135. package/src/coordination/report/store.js +103 -83
  136. package/src/coordination/state/agentsStore.js +57 -3
  137. package/src/coordination/state/fileLock.js +2 -0
  138. package/src/coordination/state/workspaceAccess.js +36 -0
  139. package/src/runtime/contracts/agentMode.js +17 -0
  140. package/src/runtime/contracts/eventContract.js +3 -0
  141. package/src/runtime/contracts/uiProtocol.js +4 -0
  142. package/src/runtime/daemon/agentHost.js +375 -0
  143. package/src/runtime/daemon/busBridge.js +396 -0
  144. package/src/runtime/daemon/claudeChannel.js +159 -0
  145. package/src/runtime/daemon/controlPlaneService.js +27 -17
  146. package/src/runtime/daemon/cronOps.js +25 -17
  147. package/src/runtime/daemon/deliveryScheduler.js +32 -5
  148. package/src/runtime/daemon/executionPaths.js +30 -0
  149. package/src/runtime/daemon/globalDaemon.js +28 -1
  150. package/src/runtime/daemon/groupOrchestrator.js +1 -0
  151. package/src/runtime/daemon/index.js +114 -344
  152. package/src/runtime/daemon/ipcServer.js +32 -10
  153. package/src/runtime/daemon/mcpConfigure.js +113 -20
  154. package/src/runtime/daemon/mcpHttpServer.js +27 -5
  155. package/src/runtime/daemon/mcpServer.js +7 -4
  156. package/src/runtime/daemon/mcpStdioProxy.js +127 -10
  157. package/src/runtime/daemon/ops.js +22 -17
  158. package/src/runtime/daemon/promptRequest.js +23 -4
  159. package/src/runtime/daemon/providerSessions.js +67 -33
  160. package/src/runtime/daemon/reportControlBus.js +59 -25
  161. package/src/runtime/daemon/status.js +4 -0
  162. package/src/runtime/privacy/redactor.js +2 -2
  163. package/src/runtime/projects/identity.js +2 -1
  164. package/src/runtime/projects/registry.js +98 -38
  165. package/src/tools/controllerSchemas.js +24 -0
  166. package/src/tools/handlers/dispatchMessage.js +5 -2
  167. package/src/tools/handlers/listAgents.js +3 -1
  168. package/src/tools/handlers/readBusSummary.js +3 -1
  169. package/src/tools/handlers/readProjectRegistry.js +29 -2
  170. package/src/tools/handlers/routeAgent.js +35 -0
  171. package/src/tools/index.js +1 -0
  172. package/src/tools/registry.js +2 -0
  173. package/src/tools/schemaFixtures.js +16 -3
  174. package/src/tools/tier1/routeAgent.js +2 -2
  175. package/src/tools/tier2/runtimeCoordination.js +38 -0
  176. package/src/tools/types.js +2 -0
  177. package/src/ui/agentPresentation.js +109 -0
  178. package/src/ui/agentSurface.js +270 -0
  179. package/src/ui/format/banner.js +8 -11
  180. package/src/ui/format/startupBanner.js +50 -0
  181. package/src/ui/multiPaneBusMirror.js +29 -3
  182. package/src/ui/rustChatHost.js +170 -438
  183. package/src/ui/rustMultiSession.js +225 -51
  184. package/src/ui/rustUcodeHost.js +20 -87
  185. package/src/ui/settingsBridge.js +7 -4
  186. package/src/app/chat/agentEnter.js +0 -70
  187. package/src/app/chat/multiWindow/index.js +0 -310
  188. package/src/app/chat/multiWindow/paneLayout.js +0 -84
  189. package/src/app/chat/multiWindow/renderer.js +0 -420
  190. package/src/app/chat/multiWindow/virtualTerminal.js +0 -327
  191. package/src/app/chat/multiWindow/vtFrame.js +0 -93
  192. package/src/ui/multiWindowHandoff.js +0 -232
  193. package/src/ui/ptyHandoff.js +0 -23
@@ -1,420 +0,0 @@
1
- const BOX = { h: "─", v: "│", tl: "┌", tr: "┐", bl: "└", br: "┘", t: "┬", b: "┴", l: "├", r: "┤", x: "┼" };
2
- const {
3
- classifyChatLogLine,
4
- compactContinuationIndent,
5
- compactDividerLabel,
6
- } = require("../../../ui/chatLogModel");
7
-
8
- function createRenderer(options = {}) {
9
- const {
10
- write: rawWrite = process.stdout.write.bind(process.stdout),
11
- } = options;
12
-
13
- // Last-write caches used to skip redundant output. Both are reset by
14
- // clear()/clearRows() so a blanked region is always repainted.
15
- let slotCache = new Map();
16
- let paneSigs = new WeakMap();
17
-
18
- function write(data) {
19
- try { rawWrite(data); } catch {}
20
- }
21
-
22
- function writeIfChanged(slot, data, top, height) {
23
- const prev = slotCache.get(slot);
24
- if (prev && prev.data === data && prev.top === top) return;
25
- slotCache.set(slot, { data, top, height });
26
- write(data);
27
- }
28
-
29
- function paneSignature(pane, focused, extra) {
30
- return [pane.top, pane.left, pane.width, pane.height, focused ? 1 : 0, extra || ""].join("|");
31
- }
32
-
33
- function shouldSkipPane(vt, sig, force) {
34
- if (force || paneSigs.get(vt) !== sig) return false;
35
- return typeof vt.isDirty === "function" ? !vt.isDirty() : false;
36
- }
37
-
38
- function markPaneRendered(vt, sig) {
39
- paneSigs.set(vt, sig);
40
- if (vt && typeof vt.clearDirty === "function") vt.clearDirty();
41
- }
42
-
43
- function moveTo(row, col) {
44
- return `\x1b[${row + 1};${col + 1}H`;
45
- }
46
-
47
- function attrToAnsi(attr) {
48
- const parts = [];
49
- if (attr.bold) parts.push("1");
50
- if (attr.dim) parts.push("2");
51
- if (attr.italic) parts.push("3");
52
- if (attr.underline) parts.push("4");
53
- if (attr.inverse) parts.push("7");
54
- if (attr.fgRgb) {
55
- parts.push(`38;2;${attr.fgRgb[0]};${attr.fgRgb[1]};${attr.fgRgb[2]}`);
56
- } else if (attr.fg !== 7) {
57
- if (attr.fg < 8) parts.push(String(30 + attr.fg));
58
- else if (attr.fg < 16) parts.push(String(90 + attr.fg - 8));
59
- else parts.push(`38;5;${attr.fg}`);
60
- }
61
- if (attr.bgRgb) {
62
- parts.push(`48;2;${attr.bgRgb[0]};${attr.bgRgb[1]};${attr.bgRgb[2]}`);
63
- } else if (attr.bg > 0) {
64
- if (attr.bg < 8) parts.push(String(40 + attr.bg));
65
- else if (attr.bg < 16) parts.push(String(100 + attr.bg - 8));
66
- else parts.push(`48;5;${attr.bg}`);
67
- }
68
- return parts.length > 0 ? `\x1b[${parts.join(";")}m` : "";
69
- }
70
-
71
- function isWide(ch) {
72
- const code = ch.codePointAt(0);
73
- if (code < 0x1100) return false;
74
- return (
75
- (code >= 0x1100 && code <= 0x115f) ||
76
- (code >= 0x2e80 && code <= 0xa4cf && code !== 0x303f) ||
77
- (code >= 0xac00 && code <= 0xd7a3) ||
78
- (code >= 0xf900 && code <= 0xfaff) ||
79
- (code >= 0xfe10 && code <= 0xfe6f) ||
80
- (code >= 0xff01 && code <= 0xff60) ||
81
- (code >= 0xffe0 && code <= 0xffe6) ||
82
- (code >= 0x20000 && code <= 0x2fffd) ||
83
- (code >= 0x30000 && code <= 0x3fffd) ||
84
- (code >= 0x1f300 && code <= 0x1f9ff)
85
- );
86
- }
87
-
88
- function renderPane(vt, pane, focused, label, opts = {}) {
89
- const sig = paneSignature(pane, focused, label || "");
90
- if (shouldSkipPane(vt, sig, opts.force)) return;
91
- const { buffer, rows, cols, cursorRow, cursorCol } = vt.getScreen();
92
- const { top, left, width, height } = pane;
93
-
94
- let out = "";
95
- const borderColor = focused ? "\x1b[1;36m" : "\x1b[90m";
96
- const reset = "\x1b[0m";
97
-
98
- const labelText = label ? ` ${label} ` : "";
99
- const topLine = BOX.tl + labelText +
100
- BOX.h.repeat(Math.max(0, width - 2 - labelText.length)) + BOX.tr;
101
- out += moveTo(top, left) + borderColor + topLine + reset;
102
-
103
- const innerWidth = width - 2;
104
- const innerHeight = height - 2;
105
- for (let r = 0; r < innerHeight; r++) {
106
- out += moveTo(top + 1 + r, left) + borderColor + BOX.v + reset;
107
- const bufRow = r < rows ? buffer[r] : null;
108
- let lastAttr = "";
109
- let col = 0;
110
- let c = 0;
111
- while (col < innerWidth && c < (bufRow ? cols : 0)) {
112
- const cell = bufRow[c];
113
- if (cell.wideContinuation) {
114
- c++;
115
- continue;
116
- }
117
- const atCursor = focused && r === cursorRow && c === cursorCol;
118
- const w = isWide(cell.char) ? 2 : 1;
119
- if (col + w > innerWidth) break;
120
- const attr = atCursor ? { ...cell.attr, inverse: !cell.attr.inverse } : cell.attr;
121
- const ansi = attrToAnsi(attr);
122
- if (ansi !== lastAttr) {
123
- out += reset + ansi;
124
- lastAttr = ansi;
125
- }
126
- out += cell.char || " ";
127
- col += w;
128
- c++;
129
- }
130
- if (lastAttr) out += reset;
131
- if (col < innerWidth) out += " ".repeat(innerWidth - col);
132
- out += borderColor + BOX.v + reset;
133
- }
134
-
135
- const botLine = BOX.bl + BOX.h.repeat(Math.max(0, width - 2)) + BOX.br;
136
- out += moveTo(top + height - 1, left) + borderColor + botLine + reset;
137
-
138
- write(out);
139
- markPaneRendered(vt, sig);
140
- }
141
-
142
- function renderCells(cells, maxWidth, cursorCol = -1) {
143
- let out = "";
144
- let lastAttr = "";
145
- let col = 0;
146
- let c = 0;
147
- while (col < maxWidth && c < cells.length) {
148
- const cell = cells[c];
149
- if (cell.wideContinuation) {
150
- c++;
151
- continue;
152
- }
153
- const w = isWide(cell.char) ? 2 : 1;
154
- if (col + w > maxWidth) break;
155
- const atCursor = c === cursorCol;
156
- const attr = atCursor ? { ...cell.attr, inverse: !cell.attr.inverse } : cell.attr;
157
- const ansi = attrToAnsi(attr);
158
- if (ansi !== lastAttr) {
159
- out += "\x1b[0m" + ansi;
160
- lastAttr = ansi;
161
- }
162
- out += cell.char || " ";
163
- col += w;
164
- c++;
165
- }
166
- if (lastAttr) out += "\x1b[0m";
167
- if (col < maxWidth) out += " ".repeat(maxWidth - col);
168
- return out;
169
- }
170
-
171
- function renderPlainLine(text, width, color = "") {
172
- const reset = "\x1b[0m";
173
- const raw = String(text || "");
174
- const truncated = truncateVisible(raw, width);
175
- const pad = Math.max(0, width - visibleLength(truncated));
176
- return `${color}${truncated}${" ".repeat(pad)}${reset}`;
177
- }
178
-
179
- function renderInternalPane(vt, pane, focused, info = {}, opts = {}) {
180
- const extra = JSON.stringify([
181
- info.label || "", info.status || "", info.detail || "",
182
- info.input || "", info.cursor ?? "",
183
- ]);
184
- const sig = paneSignature(pane, focused, extra);
185
- if (shouldSkipPane(vt, sig, opts.force)) return;
186
- const { buffer, rows, cols } = vt.getScreen();
187
- const { top, left, width, height } = pane;
188
-
189
- let out = "";
190
- const borderColor = focused ? "\x1b[1;36m" : "\x1b[90m";
191
- const reset = "\x1b[0m";
192
- const cyan = "\x1b[36m";
193
- const gray = "\x1b[90m";
194
- const red = "\x1b[31m";
195
- const magenta = "\x1b[35m";
196
-
197
- const label = info.label || "";
198
- const labelText = label ? ` ${label} ` : "";
199
- const topLine = BOX.tl + labelText +
200
- BOX.h.repeat(Math.max(0, width - 2 - labelText.length)) + BOX.tr;
201
- out += moveTo(top, left) + borderColor + topLine + reset;
202
-
203
- const innerWidth = width - 2;
204
- const innerHeight = height - 2;
205
- const chromeRows = innerHeight >= 6 ? 3 : 0;
206
- const logHeight = Math.max(1, innerHeight - chromeRows);
207
- let lastContentRow = -1;
208
- for (let r = rows - 1; r >= 0; r--) {
209
- const line = buffer[r] || [];
210
- const hasContent = line.some((cell) => cell && !cell.wideContinuation && cell.char && cell.char !== " ");
211
- if (hasContent) {
212
- lastContentRow = r;
213
- break;
214
- }
215
- }
216
- const sourceStart = lastContentRow >= 0
217
- ? Math.max(0, lastContentRow - logHeight + 1)
218
- : Math.max(0, rows - logHeight);
219
-
220
- for (let r = 0; r < logHeight; r++) {
221
- const bufRow = buffer[sourceStart + r] || [];
222
- out += moveTo(top + 1 + r, left) + borderColor + BOX.v + reset;
223
- out += renderCells(bufRow, innerWidth);
224
- out += borderColor + BOX.v + reset;
225
- }
226
-
227
- if (chromeRows > 0) {
228
- const status = String(info.status || "ready").toLowerCase();
229
- const detail = String(info.detail || "").trim();
230
- const statusColor = status === "blocked" || status === "error" ? red : (status === "ready" || status === "idle" ? gray : cyan);
231
- const statusLabel = status === "idle" ? "ready" : status;
232
- const statusText = `ufoo · ${label || "agent"} · ${statusLabel}${detail ? ` · ${detail}` : ""}`;
233
- const input = String(info.input || "");
234
- const inputCursor = Number.isFinite(info.cursor) ? Math.max(0, Math.min(input.length, info.cursor)) : input.length;
235
- const before = input.slice(0, inputCursor);
236
- const cursorChar = inputCursor < input.length ? input[inputCursor] : " ";
237
- const after = inputCursor < input.length ? input.slice(inputCursor + 1) : "";
238
- const baseRow = top + 1 + logHeight;
239
-
240
- out += moveTo(baseRow, left) + borderColor + BOX.v + reset +
241
- renderPlainLine(statusText, innerWidth, statusColor) +
242
- borderColor + BOX.v + reset;
243
- out += moveTo(baseRow + 1, left) + borderColor + BOX.v + reset +
244
- gray + BOX.h.repeat(innerWidth) + reset +
245
- borderColor + BOX.v + reset;
246
- out += moveTo(baseRow + 2, left) + borderColor + BOX.v + reset +
247
- renderPlainLine(`${magenta}› ${reset}${before}\x1b[7m${cursorChar}${reset}${after}`, innerWidth) +
248
- borderColor + BOX.v + reset;
249
- }
250
-
251
- const botLine = BOX.bl + BOX.h.repeat(Math.max(0, width - 2)) + BOX.br;
252
- out += moveTo(top + height - 1, left) + borderColor + botLine + reset;
253
-
254
- write(out);
255
- markPaneRendered(vt, sig);
256
- }
257
-
258
- function stripControl(str) {
259
- return str.replace(/[\x00-\x08\x0a-\x1f]/g, "");
260
- }
261
-
262
- function visibleLength(str) {
263
- return str.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, "").length;
264
- }
265
-
266
- function truncateVisible(str, maxWidth) {
267
- let visible = 0;
268
- let i = 0;
269
- while (i < str.length && visible < maxWidth) {
270
- if (str[i] === "\x1b" && str[i + 1] === "[") {
271
- const start = i;
272
- i += 2;
273
- while (i < str.length && str[i] >= "\x20" && str[i] <= "\x3f") i++;
274
- if (i < str.length) i++;
275
- continue;
276
- }
277
- visible++;
278
- i++;
279
- }
280
- return str.slice(0, i);
281
- }
282
-
283
- function formatChatLogLine(raw = "", width = 80) {
284
- const row = classifyChatLogLine(stripControl(raw));
285
- const reset = "\x1b[0m";
286
- if (row.kind === "spacer") return " ".repeat(width);
287
- if (row.kind === "banner") return `\x1b[36;1m${truncateVisible(row.body, width)}${reset}`;
288
- if (row.kind === "divider") return `\x1b[90m${truncateVisible(` ${compactDividerLabel(row.body)}`, width)}${reset}`;
289
-
290
- const palette = {
291
- assistant: { marker: "\x1b[36m", speaker: "\x1b[37;1m", body: "" },
292
- agent: { marker: "\x1b[36m", speaker: "\x1b[36m", body: "" },
293
- error: { marker: "\x1b[31;1m", speaker: "\x1b[31m", body: "\x1b[31m" },
294
- success: { marker: "\x1b[32m", speaker: "\x1b[32m", body: "\x1b[32m" },
295
- meta: { marker: "\x1b[90m", speaker: "\x1b[90m", body: "\x1b[90m" },
296
- plain: { marker: "\x1b[90m", speaker: "\x1b[90m", body: "" },
297
- };
298
- const colors = palette[row.kind] || palette.plain;
299
- const speaker = row.speaker ? `${colors.speaker}${row.speaker}${reset}\x1b[90m · ${reset}` : "";
300
- const markerGlyph = row.kind === "agent" ? "●" : row.marker;
301
- const marker = markerGlyph
302
- ? `${colors.marker}${markerGlyph}${reset} `
303
- : " ";
304
- const body = row.kind === "plain"
305
- ? compactContinuationIndent(row.body || row.text || " ")
306
- : (row.body || row.text || " ");
307
- const line = `${marker}${speaker}${colors.body || ""}${body}${reset}`;
308
- return truncateVisible(line, width);
309
- }
310
-
311
- function renderChatLog(pane, lines) {
312
- const { top, left, width, height } = pane;
313
- const innerWidth = width - 1;
314
- let out = "";
315
- const dim = "\x1b[90m";
316
- const reset = "\x1b[0m";
317
-
318
- for (let r = 0; r < height; r++) {
319
- out += moveTo(top + r, left);
320
- const idx = lines.length - height + r;
321
- const raw = (idx >= 0 && idx < lines.length) ? lines[idx] || "" : "";
322
- const truncated = formatChatLogLine(raw, innerWidth);
323
- const pad = Math.max(0, innerWidth - visibleLength(truncated));
324
- out += truncated + reset + " ".repeat(pad);
325
- out += dim + BOX.v + reset;
326
- }
327
- writeIfChanged("chatlog", out, top, height);
328
- }
329
-
330
- function renderSeparator(pane, highlighted) {
331
- const { top, left, width } = pane;
332
- const reset = "\x1b[0m";
333
- const color = highlighted ? "\x1b[36m" : "\x1b[90m";
334
- writeIfChanged(`sep:${top}:${left}`, moveTo(top, left) + color + "─".repeat(width) + reset, top, 1);
335
- }
336
-
337
- function renderStatusLine(pane, text) {
338
- const { top, left, width } = pane;
339
- const reset = "\x1b[0m";
340
- const dim = "\x1b[90m";
341
- const truncated = truncateVisible(text || "", width);
342
- const pad = Math.max(0, width - visibleLength(truncated));
343
- writeIfChanged("status", moveTo(top, left) + dim + truncated + " ".repeat(pad) + reset, top, 1);
344
- }
345
-
346
- function renderDashboard(pane, lines) {
347
- const { top, left, width } = pane;
348
- const reset = "\x1b[0m";
349
- let out = "";
350
- for (let i = 0; i < lines.length; i++) {
351
- const line = lines[i] || "";
352
- const pad = Math.max(0, width - visibleLength(line));
353
- out += moveTo(top + i, left) + line + " ".repeat(pad) + reset;
354
- }
355
- writeIfChanged("dashboard", out, top, lines.length);
356
- }
357
-
358
- function renderInputPrompt(pane, prefix, draft, cursor) {
359
- const { top, left, width } = pane;
360
- const reset = "\x1b[0m";
361
- const cyan = "\x1b[36m";
362
- const inverse = "\x1b[7m";
363
- const prefixStr = prefix || "› ";
364
- const text = draft || "";
365
- const cursorPos = typeof cursor === "number" ? Math.max(0, Math.min(text.length, cursor)) : text.length;
366
- const before = text.slice(0, cursorPos);
367
- const cursorChar = cursorPos < text.length ? text[cursorPos] : " ";
368
- const after = cursorPos < text.length ? text.slice(cursorPos + 1) : "";
369
- const promptLine = cyan + prefixStr + reset + before + inverse + cursorChar + reset + after;
370
- const truncated = truncateVisible(promptLine, width);
371
- const pad = Math.max(0, width - visibleLength(truncated));
372
- writeIfChanged("input", moveTo(top, left) + truncated + " ".repeat(pad) + reset, top, 1);
373
- }
374
-
375
- function hideCursor() { write("\x1b[?25l"); }
376
- function showCursor() { write("\x1b[?25h"); }
377
- function clear() {
378
- slotCache = new Map();
379
- paneSigs = new WeakMap();
380
- write("\x1b[2J\x1b[H");
381
- }
382
-
383
- function clearRows(top, count, width, left = 0) {
384
- const rows = Math.max(0, Number(count) || 0);
385
- const cols = Math.max(0, Number(width) || 0);
386
- if (rows === 0 || cols === 0) return;
387
- // Blanked rows must be repainted on the next pass: drop every cached
388
- // slot overlapping the cleared range and reset pane signatures (kept
389
- // in a WeakMap, so they cannot be filtered selectively).
390
- for (const [slot, entry] of slotCache) {
391
- if (entry.top < top + rows && top < entry.top + entry.height) slotCache.delete(slot);
392
- }
393
- paneSigs = new WeakMap();
394
- let out = "";
395
- const blank = " ".repeat(cols);
396
- for (let i = 0; i < rows; i++) {
397
- out += moveTo(top + i, left) + blank;
398
- }
399
- write(out);
400
- }
401
-
402
- return {
403
- renderPane,
404
- renderInternalPane,
405
- renderChatLog,
406
- renderSeparator,
407
- renderStatusLine,
408
- renderDashboard,
409
- renderInputPrompt,
410
- hideCursor,
411
- showCursor,
412
- clear,
413
- clearRows,
414
- moveTo,
415
- write,
416
- visibleLength,
417
- };
418
- }
419
-
420
- module.exports = { createRenderer };