aegisone 1.0.34 → 1.0.35

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 (226) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +1 -1
  3. package/SOURCE-OFFER.txt +9 -0
  4. package/bin/aegis.js +1 -322
  5. package/package.json +4 -5
  6. package/scripts/bump.mjs +1 -211
  7. package/scripts/bundle.js +1 -129
  8. package/scripts/predist.mjs +1 -976
  9. package/src/child.js +1 -285
  10. package/src/dispatch.js +1 -269
  11. package/src/doctor.js +1 -639
  12. package/src/host.js +1 -111
  13. package/src/permissions.js +1 -383
  14. package/src/probes.js +1 -159
  15. package/src/release.js +1 -409
  16. package/vendor/cli/package.json +3 -7
  17. package/vendor/cli/scripts/check.mjs +1 -52
  18. package/vendor/cli/scripts/demo.mjs +1 -94
  19. package/vendor/cli/scripts/predist.mjs +1 -75
  20. package/vendor/cli/scripts/runtests.mjs +1 -56
  21. package/vendor/cli/scripts/seal-gate.mjs +1 -74
  22. package/vendor/cli/src/agents.js +1 -139
  23. package/vendor/cli/src/app.js +1 -2957
  24. package/vendor/cli/src/art.js +1 -289
  25. package/vendor/cli/src/chatflow.js +1 -3023
  26. package/vendor/cli/src/checkpoint.js +1 -90
  27. package/vendor/cli/src/clipboard.js +1 -424
  28. package/vendor/cli/src/cloudsync.js +1 -528
  29. package/vendor/cli/src/commands.js +1 -4089
  30. package/vendor/cli/src/config.js +1 -581
  31. package/vendor/cli/src/connect.js +1 -349
  32. package/vendor/cli/src/credentials.js +1 -33
  33. package/vendor/cli/src/deps.js +1 -232
  34. package/vendor/cli/src/devrun.js +1 -187
  35. package/vendor/cli/src/diff.js +1 -333
  36. package/vendor/cli/src/diffview.js +1 -296
  37. package/vendor/cli/src/engine.js +1 -297
  38. package/vendor/cli/src/entitlement.js +1 -355
  39. package/vendor/cli/src/events.js +1 -707
  40. package/vendor/cli/src/export.js +1 -67
  41. package/vendor/cli/src/filepreview.js +1 -153
  42. package/vendor/cli/src/fingerprint.js +1 -592
  43. package/vendor/cli/src/format.js +1 -259
  44. package/vendor/cli/src/fuzzy.js +1 -119
  45. package/vendor/cli/src/history.js +1 -423
  46. package/vendor/cli/src/init.js +1 -175
  47. package/vendor/cli/src/input.js +1 -139
  48. package/vendor/cli/src/keys.js +1 -183
  49. package/vendor/cli/src/markdown.js +1 -306
  50. package/vendor/cli/src/models.js +1 -507
  51. package/vendor/cli/src/naming.js +1 -57
  52. package/vendor/cli/src/overlays.js +1 -462
  53. package/vendor/cli/src/panels.js +1 -1418
  54. package/vendor/cli/src/ping.js +1 -226
  55. package/vendor/cli/src/presence.js +1 -238
  56. package/vendor/cli/src/render.js +1 -516
  57. package/vendor/cli/src/screen.js +1 -518
  58. package/vendor/cli/src/screens.js +1 -896
  59. package/vendor/cli/src/secret.js +1 -59
  60. package/vendor/cli/src/securefs.js +1 -359
  61. package/vendor/cli/src/settings.js +1 -155
  62. package/vendor/cli/src/shared.js +1 -52
  63. package/vendor/cli/src/sharedpaths.js +1 -53
  64. package/vendor/cli/src/signup.js +1 -156
  65. package/vendor/cli/src/summarize.js +1 -95
  66. package/vendor/cli/src/sysdiag.js +1 -585
  67. package/vendor/cli/src/system.js +1 -40
  68. package/vendor/cli/src/theme.js +1 -570
  69. package/vendor/cli/src/title.js +1 -220
  70. package/vendor/cli/src/todos.js +1 -115
  71. package/vendor/cli/src/tokens.js +1 -263
  72. package/vendor/cli/src/topup.js +1 -210
  73. package/vendor/cli/src/update.js +1 -191
  74. package/vendor/cli/vendor/client/aegis.js +1 -2441
  75. package/vendor/cli/vendor/client/attest.js +1 -518
  76. package/vendor/cli/vendor/client/authorship-key.js +1 -410
  77. package/vendor/cli/vendor/client/auto-provision.js +1 -134
  78. package/vendor/cli/vendor/client/brain-catalog.js +1 -268
  79. package/vendor/cli/vendor/client/byok-catalog.js +1 -199
  80. package/vendor/cli/vendor/client/catalog-client.js +1 -487
  81. package/vendor/cli/vendor/client/conversation-export.js +1 -177
  82. package/vendor/cli/vendor/client/credentials.js +1 -494
  83. package/vendor/cli/vendor/client/device-fingerprint.js +1 -141
  84. package/vendor/cli/vendor/client/drive-connect.js +1 -110
  85. package/vendor/cli/vendor/client/drive-upload.js +1 -234
  86. package/vendor/cli/vendor/client/env-file.js +1 -1273
  87. package/vendor/cli/vendor/client/fingerprint.js +1 -1064
  88. package/vendor/cli/vendor/client/foreign-memory.js +1 -669
  89. package/vendor/cli/vendor/client/google-signin.js +1 -133
  90. package/vendor/cli/vendor/client/hooks.js +1 -430
  91. package/vendor/cli/vendor/client/model-discovery.js +1 -373
  92. package/vendor/cli/vendor/client/naming.js +1 -283
  93. package/vendor/cli/vendor/client/platform.js +1 -937
  94. package/vendor/cli/vendor/client/prefs.js +1 -351
  95. package/vendor/cli/vendor/client/presence-constants.js +1 -107
  96. package/vendor/cli/vendor/client/presence.js +1 -438
  97. package/vendor/cli/vendor/client/record-vault.js +3 -1055
  98. package/vendor/cli/vendor/client/session-store.js +1 -575
  99. package/vendor/cli/vendor/client/topup.js +1 -297
  100. package/vendor/cli/vendor/client/touch.js +1 -458
  101. package/vendor/cli/vendor/client/update.js +1 -763
  102. package/vendor/cli/vendor/client/user-models.js +1 -381
  103. package/vendor/cli/vendor/desktop/lib/local/agents.js +1 -105
  104. package/vendor/cli/vendor/desktop/lib/local/attachments.js +1 -276
  105. package/vendor/cli/vendor/desktop/lib/local/autonomous.js +1 -1045
  106. package/vendor/cli/vendor/desktop/lib/local/claude-pro.js +1 -1028
  107. package/vendor/cli/vendor/desktop/lib/local/context.js +1 -125
  108. package/vendor/cli/vendor/desktop/lib/local/engine.js +1 -3666
  109. package/vendor/cli/vendor/desktop/lib/local/entitlement.js +1 -660
  110. package/vendor/cli/vendor/desktop/lib/local/git-scope.js +1 -228
  111. package/vendor/cli/vendor/desktop/lib/local/local.js +1 -2145
  112. package/vendor/cli/vendor/desktop/lib/local/prompt.js +1 -280
  113. package/vendor/cli/vendor/desktop/lib/local/queue.js +1 -560
  114. package/vendor/cli/vendor/desktop/lib/local/session-rounds.js +1 -238
  115. package/vendor/cli/vendor/desktop/lib/local/spill.js +1 -75
  116. package/vendor/cli/vendor/desktop/lib/local/turn-guard.js +1 -300
  117. package/vendor/cli/vendor/desktop/lib/local/worktree-lock.js +1 -235
  118. package/vendor/cli/vendor/desktop/lib/settings.js +1 -890
  119. package/vendor/cli/vendor/desktop/renderer/usage.js +1 -1239
  120. package/vendor/cli/vendor/hosts/install.js +1 -638
  121. package/vendor/cli/vendor/hosts/openai-shim.js +1 -515
  122. package/vendor/cli/vendor/hosts/spec.js +1 -164
  123. package/vendor/cli/vendor/hosts/targets.js +1 -346
  124. package/vendor/cli/vendor/mcp/server.js +1 -221
  125. package/vendor/cli/vendor/mcp/tools.js +1 -408
  126. package/vendor/cli/vendor/one/src/child.js +1 -285
  127. package/vendor/cli/vendor/one/src/permissions.js +1 -383
  128. package/vendor/desktop/bin/aegis.js +1 -32
  129. package/vendor/desktop/lib/account-key.js +1 -56
  130. package/vendor/desktop/lib/auto.js +1 -461
  131. package/vendor/desktop/lib/avatar/companion.js +1 -698
  132. package/vendor/desktop/lib/avatar/cosmetics.js +1 -532
  133. package/vendor/desktop/lib/avatar/events.js +1 -344
  134. package/vendor/desktop/lib/avatar/holder-turn.js +1 -350
  135. package/vendor/desktop/lib/avatar/holders.js +1 -612
  136. package/vendor/desktop/lib/avatar/identity.js +1 -641
  137. package/vendor/desktop/lib/avatar/level.js +1 -368
  138. package/vendor/desktop/lib/avatar/mirror.js +1 -524
  139. package/vendor/desktop/lib/avatar/persona.js +1 -388
  140. package/vendor/desktop/lib/avatar/profile.js +1 -977
  141. package/vendor/desktop/lib/avatar/register.js +1 -307
  142. package/vendor/desktop/lib/avatar/store.js +1 -472
  143. package/vendor/desktop/lib/avatar/tiers.js +1 -38
  144. package/vendor/desktop/lib/avatar/transfer.js +1 -415
  145. package/vendor/desktop/lib/avatar/turn.js +1 -216
  146. package/vendor/desktop/lib/avatar/voice.js +1 -328
  147. package/vendor/desktop/lib/avatar/xp.js +1 -404
  148. package/vendor/desktop/lib/deep-link.js +1 -120
  149. package/vendor/desktop/lib/fingerprint.js +2 -347
  150. package/vendor/desktop/lib/google-connect.js +1 -323
  151. package/vendor/desktop/lib/local/agents.js +1 -105
  152. package/vendor/desktop/lib/local/attachments.js +1 -276
  153. package/vendor/desktop/lib/local/autonomous.js +1 -1045
  154. package/vendor/desktop/lib/local/claude-pro.js +1 -1028
  155. package/vendor/desktop/lib/local/context.js +1 -125
  156. package/vendor/desktop/lib/local/engine.js +1 -3666
  157. package/vendor/desktop/lib/local/entitlement.js +1 -660
  158. package/vendor/desktop/lib/local/git-scope.js +1 -228
  159. package/vendor/desktop/lib/local/local.js +1 -2145
  160. package/vendor/desktop/lib/local/prompt.js +1 -280
  161. package/vendor/desktop/lib/local/queue.js +1 -560
  162. package/vendor/desktop/lib/local/session-rounds.js +1 -238
  163. package/vendor/desktop/lib/local/spill.js +1 -75
  164. package/vendor/desktop/lib/local/telemetry.js +1 -470
  165. package/vendor/desktop/lib/local/turn-guard.js +1 -300
  166. package/vendor/desktop/lib/local/worktree-lock.js +1 -235
  167. package/vendor/desktop/lib/provider-key-mirror.js +1 -252
  168. package/vendor/desktop/lib/quick-launcher.js +1 -84
  169. package/vendor/desktop/lib/settings.js +1 -890
  170. package/vendor/desktop/lib/sync/memory-queue.js +1 -60
  171. package/vendor/desktop/lib/sync/persist-gate.js +1 -148
  172. package/vendor/desktop/lib/sync/sessions.js +1 -106
  173. package/vendor/desktop/lib/window-state.js +1 -66
  174. package/vendor/desktop/package.json +2 -4
  175. package/vendor/desktop/preload.js +1 -679
  176. package/vendor/desktop/renderer/app.js +1 -7250
  177. package/vendor/desktop/renderer/avatar/assemble.js +1 -335
  178. package/vendor/desktop/renderer/avatar/avatar.js +1 -380
  179. package/vendor/desktop/renderer/avatar/cards.js +1 -141
  180. package/vendor/desktop/renderer/avatar/hud.js +1 -206
  181. package/vendor/desktop/renderer/avatar/machine.js +1 -213
  182. package/vendor/desktop/renderer/avatar/pane.js +1 -439
  183. package/vendor/desktop/renderer/avatar/parts.js +1 -437
  184. package/vendor/desktop/renderer/budget.js +1 -172
  185. package/vendor/desktop/renderer/companion.js +1 -223
  186. package/vendor/desktop/renderer/diff.js +1 -359
  187. package/vendor/desktop/renderer/diffview.js +1 -336
  188. package/vendor/desktop/renderer/filepreview.js +1 -211
  189. package/vendor/desktop/renderer/fingerprint.js +3 -789
  190. package/vendor/desktop/renderer/markdown.js +1 -168
  191. package/vendor/desktop/renderer/provider-mark.js +1 -311
  192. package/vendor/desktop/renderer/provider-toolbar.js +1 -134
  193. package/vendor/desktop/renderer/providers.js +1 -259
  194. package/vendor/desktop/renderer/quick.js +1 -285
  195. package/vendor/desktop/renderer/stream-policy.js +1 -387
  196. package/vendor/desktop/renderer/transcript-view.js +1 -323
  197. package/vendor/desktop/renderer/usage.js +1 -1239
  198. package/vendor/desktop/vendor/aegis.js +1 -2441
  199. package/vendor/desktop/vendor/attest.js +1 -518
  200. package/vendor/desktop/vendor/authorship-key.js +1 -410
  201. package/vendor/desktop/vendor/auto-provision.js +1 -134
  202. package/vendor/desktop/vendor/brain-catalog.js +1 -268
  203. package/vendor/desktop/vendor/byok-catalog.js +1 -199
  204. package/vendor/desktop/vendor/catalog-client.js +1 -487
  205. package/vendor/desktop/vendor/conversation-export.js +1 -177
  206. package/vendor/desktop/vendor/credentials.js +1 -494
  207. package/vendor/desktop/vendor/device-fingerprint.js +1 -141
  208. package/vendor/desktop/vendor/drive-connect.js +1 -110
  209. package/vendor/desktop/vendor/drive-upload.js +1 -234
  210. package/vendor/desktop/vendor/env-file.js +1 -1273
  211. package/vendor/desktop/vendor/fingerprint.js +1 -1064
  212. package/vendor/desktop/vendor/foreign-memory.js +1 -669
  213. package/vendor/desktop/vendor/google-signin.js +1 -133
  214. package/vendor/desktop/vendor/hooks.js +1 -430
  215. package/vendor/desktop/vendor/model-discovery.js +1 -373
  216. package/vendor/desktop/vendor/naming.js +1 -283
  217. package/vendor/desktop/vendor/platform.js +1 -937
  218. package/vendor/desktop/vendor/prefs.js +1 -351
  219. package/vendor/desktop/vendor/presence-constants.js +1 -107
  220. package/vendor/desktop/vendor/presence.js +1 -438
  221. package/vendor/desktop/vendor/record-vault.js +3 -1055
  222. package/vendor/desktop/vendor/session-store.js +1 -575
  223. package/vendor/desktop/vendor/topup.js +1 -297
  224. package/vendor/desktop/vendor/touch.js +1 -458
  225. package/vendor/desktop/vendor/update.js +1 -763
  226. package/vendor/desktop/vendor/user-models.js +1 -381
@@ -1,389 +1,3 @@
1
1
  // SPDX-License-Identifier: AGPL-3.0-or-later
2
2
  // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
- // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
- // Conveying a modified copy, or running one as a network service, owes the
5
- // corresponding source under this licence (§5/§13) — see LICENSING.md.
6
- 'use strict';
7
-
8
- /**
9
- * Pure decisions behind a live streaming turn: whether the transcript is
10
- * allowed to follow the stream, and whether a rejection means the user asked
11
- * to stop. Kept out of app.js — like budget.js — so both rules are
12
- * unit-testable from plain Node without a DOM or window.aegis. app.js only
13
- * calls into this.
14
- *
15
- * Both rules exist because a running turn used to own the transcript: it
16
- * re-pinned the view on every chunk (so earlier turns could not be read) and
17
- * it reported a deliberate stop as a failure (so interrupting lost the answer
18
- * already on screen).
19
- */
20
-
21
- /** Distance from the bottom still counted as "following the stream". */
22
- const STICK_SLOP_PX = 48;
23
-
24
- /**
25
- * Is the view at (or effectively at) the tail? A few pixels of slack absorbs
26
- * sub-pixel layout and a scrollbar that appears mid-stream; without it the
27
- * follow silently stops a fraction short.
28
- */
29
- function nearBottom(metrics, slop) {
30
- const m = metrics || {};
31
- const budget = Number.isFinite(slop) ? slop : STICK_SLOP_PX;
32
- const scrollHeight = Number(m.scrollHeight) || 0;
33
- const scrollTop = Number(m.scrollTop) || 0;
34
- const clientHeight = Number(m.clientHeight) || 0;
35
- return scrollHeight - scrollTop - clientHeight <= budget;
36
- }
37
-
38
- /**
39
- * May the transcript be scrolled to the bottom right now? `force` is for the
40
- * cases where the view genuinely must move (the user just sent, or a card is
41
- * blocking the turn); otherwise the reader's explicit scroll-away wins.
42
- *
43
- * The flag is checked before the measurement on purpose: a mid-stream reflow
44
- * can momentarily measure as "at the bottom" while the reader is nowhere near
45
- * it, which is exactly the case where re-pinning feels like a hijack.
46
- */
47
- function shouldFollow(metrics, opts) {
48
- const o = opts || {};
49
- if (o.force) return true;
50
- if (o.userScrolledUp) return false;
51
- return nearBottom(metrics, o.slop);
52
- }
53
-
54
- /**
55
- * Signatures of a real abort, as each layer spells it: Chromium's fetch
56
- * ("The user aborted a request."), undici/Node ("This operation was aborted"),
57
- * and an AbortSignal's own reason ("signal is aborted without reason").
58
- *
59
- * Deliberately NOT a bare /abort/i. A dropped socket surfaces as
60
- * "ECONNABORTED: connection aborted by peer" or "socket hang up", which
61
- * contains the same word but is a genuine transport failure — matching it
62
- * would relabel a real error as a deliberate stop and strand the user with a
63
- * silently truncated answer.
64
- */
65
- const ABORT_SIGNATURES = [
66
- /AbortError/,
67
- /\boperation was aborted\b/i,
68
- /\buser aborted\b/i,
69
- /\brequest\s+aborted\b/i,
70
- /\bsignal is aborted\b/i,
71
- /\baborted without reason\b/i,
72
- ];
73
-
74
- /**
75
- * Did the user ask for this turn to stop? The abort travels back over IPC,
76
- * which rebuilds the Error object and drops `name`/`code` — the renderer only
77
- * sees a wrapped message. So the caller's own flag is the primary signal, and
78
- * the signatures above are a backstop for an abort this renderer did not
79
- * initiate.
80
- */
81
- function isCancellation(err, opts) {
82
- const o = opts || {};
83
- if (o.userStopped) return true;
84
- if (!err) return false;
85
- if (err.name === 'AbortError' || err.code === 'ABORT_ERR') return true;
86
- const msg = typeof err === 'string' ? err : err.message;
87
- if (typeof msg !== 'string') return false;
88
- return ABORT_SIGNATURES.some((re) => re.test(msg));
89
- }
90
-
91
- /**
92
- * Should a stop request reach the turn running right now?
93
- *
94
- * `runningTurn` is the token of the turn in progress (null when idle) and
95
- * `stoppedTurn` is the token already asked to stop. The abort is not
96
- * instantaneous — the transport has to unwind before `send()`'s catch runs — so
97
- * a second press lands on a turn that is still nominally running. That second
98
- * press is the *same* intent and must not re-enter.
99
- *
100
- * The two writers keep the pair consistent: app.js only ever assigns
101
- * `stoppedTurn = runningTurn`, and claiming a new turn sets `stoppedTurn = null`
102
- * alongside `runningTurn = myTurn`. So `stoppedTurn` is never some *other*
103
- * turn's number, and `runningTurn !== stoppedTurn` means exactly "not already
104
- * stopping". What keeps a finished turn's stop off its successor is that
105
- * invariant plus the catch's `stoppedTurn === myTurn` — not a refusal here. A
106
- * press on a live successor is legitimate and does reach it.
107
- */
108
- function stopAppliesTo(runningTurn, stoppedTurn) {
109
- if (runningTurn == null) return false;
110
- return runningTurn !== stoppedTurn;
111
- }
112
-
113
- /**
114
- * What a cancelled turn leaves on screen, and what it honestly is.
115
- *
116
- * `streamedText || reasoningText` collapsed two different outcomes into one
117
- * string: a turn cut off after real answer text, and a turn cut off with
118
- * deliberation but no answer. The second is not a failure and the user should
119
- * keep it, but folding it in as if it were the answer loses the distinction —
120
- * so the case is named here and the caller labels it. With neither, the
121
- * placeholder is still returned rather than an empty bubble.
122
- */
123
- function salvageTurn(input) {
124
- const i = input || {};
125
- const streamed = typeof i.streamedText === 'string' ? i.streamedText : '';
126
- const reasoning = typeof i.reasoningText === 'string' ? i.reasoningText : '';
127
- if (streamed) return { text: streamed, reasoning: reasoning, kind: 'answer' };
128
- if (reasoning) return { text: reasoning, reasoning: '', kind: 'reasoning-only' };
129
- return { text: '(stopped before any output)', reasoning: '', kind: 'empty' };
130
- }
131
-
132
- /**
133
- * What a salvaged turn may contribute to the history that gets replayed —
134
- * as opposed to what it shows on screen.
135
- *
136
- * These were the same string, and on a cancelled turn that was a loop.
137
- * `salvageTurn` promotes the deliberation to `text` so the bubble is not
138
- * empty, which is right for the screen; but that same string was pushed into
139
- * `threadMessages` and written through `sync.append`, so the next turn fed the
140
- * model its own truncated chain-of-thought back as its prior reply. A model
141
- * handed an unfinished sentence finishes it — the same words every time,
142
- * because the same incomplete prefix went back every time. It survived a
143
- * reopen, and `openSession` rebuilds `threadMessages` from the stored rows, so
144
- * the loop outlived the session that started it.
145
- *
146
- * So the screen keeps the deliberation (labelled `reasoning only`) and the
147
- * history gets an annotation instead. The annotation is deliberate rather than
148
- * a blank: a stopped turn is a real exchange and the row still carries the
149
- * ledger fields the rolling meter folds — only the prose is replaced, so
150
- * billing and the token total are unchanged.
151
- */
152
- function replayableContent(kind, text) {
153
- if (kind === 'reasoning-only') return '(stopped: reasoning only)';
154
- if (kind === 'empty') return '(stopped before any output)';
155
- return text;
156
- }
157
-
158
- /**
159
- * The history a turn is actually replayed with: the same rows, minus anything
160
- * that is not the model's own prose, with runs of one role folded together.
161
- *
162
- * Both halves are needed at once, and each one alone breaks the other.
163
- * Keeping the annotations replayed a label as if the model had said it — the
164
- * loop `replayableContent` exists to stop. Dropping them left two `user` rows
165
- * adjacent whenever the turn between them contributed nothing (a cancel, an
166
- * empty response), and strict providers reject a non-alternating thread
167
- * outright: the fix for the loop would have turned the next turn into a 400 on
168
- * exactly the messages that needed it most. So the run is folded instead —
169
- * nothing that is not prose reaches the wire, and the roles still alternate.
170
- *
171
- * A runaway of empty rows disappears for the same reason: a message with no
172
- * content is not a message, and the provider would reject the empty block.
173
- * A leading assistant row goes too — there is no user request for it to
174
- * answer, so replaying it as the start of the conversation is a thread that
175
- * begins with the model talking to itself. The current prompt travels
176
- * separately (`prompt`, appended after `messages`), so nothing of the user's
177
- * is lost by either drop.
178
- *
179
- * Display and the ledger are untouched by design: openSession draws the
180
- * bubbles from `msgs` and folds the meter with `rollMessages(msgs)`, both
181
- * before this is called, so only what goes back to the model changes.
182
- */
183
- /**
184
- * Reduce a content value to text. A string passes through; an OpenAI
185
- * content-part array becomes its text parts with `[image]` standing in for each
186
- * picture. Mirrors flattenContent() in lib/local/attachments.js, which the
187
- * main side uses for the same reason (that module is main-side and this file
188
- * has no require).
189
- *
190
- * @param {string|Array|null|undefined} content
191
- * @returns {string}
192
- */
193
- function flattenParts(content) {
194
- if (content == null) return '';
195
- if (typeof content === 'string') return content;
196
- if (!Array.isArray(content)) return '';
197
- const out = [];
198
- for (const part of content) {
199
- if (!part || typeof part !== 'object') continue;
200
- if (typeof part.text === 'string') out.push(part.text);
201
- else if (part.type === 'image_url') out.push('[image]');
202
- }
203
- return out.join('\n');
204
- }
205
-
206
- /**
207
- * Coalesce two same-role content values (this function merges adjacent user
208
- * rows, which strict providers reject). Stays a string when both sides are
209
- * strings — the common case — and becomes a parts array when either side is
210
- * one, with the two texts joined and any image parts kept.
211
- *
212
- * @param {string|Array} prev
213
- * @param {string|Array} next
214
- * @returns {string|Array}
215
- */
216
- function joinContent(prev, next) {
217
- if (typeof prev === 'string' && typeof next === 'string') return `${prev}\n\n${next}`;
218
- const merged = [];
219
- const push = (value) => {
220
- if (typeof value === 'string') {
221
- if (value.trim()) merged.push({ type: 'text', text: value });
222
- return;
223
- }
224
- if (!Array.isArray(value)) return;
225
- for (const part of value) {
226
- if (!part || typeof part !== 'object') continue;
227
- if (part.type === 'image_url') merged.push(part);
228
- else if (typeof part.text === 'string') merged.push({ type: 'text', text: part.text });
229
- }
230
- };
231
- push(prev);
232
- push(next);
233
- // Two text parts are joined into one, so a merged row does not read as a
234
- // stack of fragments; images keep their position after the prose.
235
- const texts = merged.filter((p) => p.type === 'text').map((p) => p.text);
236
- const images = merged.filter((p) => p.type === 'image_url');
237
- const out = [];
238
- if (texts.length) out.push({ type: 'text', text: texts.join('\n\n') });
239
- out.push(...images);
240
- if (out.length === 1 && out[0].type === 'text') return out[0].text;
241
- return out;
242
- }
243
-
244
- function replayHistory(messages) {
245
- const out = [];
246
- for (const m of Array.isArray(messages) ? messages : []) {
247
- if (!m) continue;
248
- // Same rows openSession always replayed: user and assistant. A `system`
249
- // row was never part of the renderer's thread and is not promoted to one
250
- // here.
251
- if (m.role !== 'user' && m.role !== 'assistant') continue;
252
- // `content` may be an OpenAI content-part ARRAY: a dropped picture makes
253
- // the user row `[{type:'text'}, {type:'image_url'}]` (see the ATTACH block
254
- // in app.js). Reading only strings dropped such a row ENTIRELY — the
255
- // prompt that came with the picture included — so the turn vanished from
256
- // the replayed history and the model was asked a follow-up about a
257
- // conversation it could no longer see.
258
- //
259
- // PASSED THROUGH, not flattened. Flattening here would make a picture
260
- // visible only on the turn it was dropped: the next turn replays history,
261
- // and `[image]` is a text marker, not pixels. The parts travel as parts so
262
- // the model can still see what was sent earlier; the context trimmer bills
263
- // each image a flat IMAGE_COST (lib/local/context.js), so keeping them is
264
- // accounted for rather than free.
265
- const content =
266
- typeof m.content === 'string'
267
- ? m.content
268
- : Array.isArray(m.content)
269
- ? m.content
270
- : typeof m.text === 'string'
271
- ? m.text
272
- : '';
273
- const asText = Array.isArray(content) ? flattenParts(content) : content;
274
- if (!asText.trim()) continue;
275
- if (m.role === 'assistant' && isPlaceholderContent(asText)) continue;
276
- const last = out[out.length - 1];
277
- if (last && last.role === m.role) last.content = joinContent(last.content, content);
278
- else out.push({ role: m.role, content });
279
- }
280
- while (out.length && out[0].role === 'assistant') out.shift();
281
- return out;
282
- }
283
-
284
- /** True for the annotations above — rows that must never be replayed as prose. */
285
- function isPlaceholderContent(content) {
286
- const t = typeof content === 'string' ? content.trim() : '';
287
- if (!t) return false;
288
- return (
289
- t === '(stopped: reasoning only)' ||
290
- t === '(stopped before any output)' ||
291
- t === '(empty response)' ||
292
- t === '(no response)'
293
- );
294
- }
295
-
296
- /** The smallest stream growth, in characters, that earns a live-meter recompute.
297
- *
298
- * It was 256, and that number was the start-of-turn lag this rule exists to
299
- * avoid: the counter's first paint required a quarter kilobyte of text — ~64
300
- * tokens, several seconds into a slow turn — so the bubble opened with no
301
- * figure at all and then jumped to it, while the CLI's own tail
302
- * (`cli/src/chatflow.js`'s spinner, `↓${fmtTokens(state.streamed)}`) and the
303
- * reference engine both have a count climbing from the first frame. 32 chars
304
- * (~8 tokens) is a step the eye reads as continuous at the start of a reply,
305
- * which is the whole point of a live count.
306
- *
307
- * The cost bound is NOT this number — it is the proportional term below. At
308
- * 100k chars the step is 1/32 of the stream (3125), so lowering this floor
309
- * leaves the long-reply recompute count unchanged; it only buys paints in the
310
- * first ~1k characters, where each estimate is over a string too short to
311
- * matter. */
312
- const LIVE_METER_MIN_GROWTH = 32;
313
-
314
- /** …and the fraction of the whole stream that must have arrived since the last
315
- * one: a recompute comes due once the stream has grown by 1/this. */
316
- const LIVE_METER_GROWTH_FRACTION = 32;
317
-
318
- /**
319
- * Whether the live token meter is due to be recomputed, given how much of the
320
- * stream it last measured and how much of the stream has arrived now.
321
- *
322
- * The meter is paint-driven — `renderRollMeter` is called from inside the rAF
323
- * painter — and each recompute estimates the WHOLE accumulated reply plus
324
- * reasoning trace. `estimateTokens` counts code points by spreading the string
325
- * into an array (`[...String(text)].length`), so one estimate is linear in the
326
- * stream AND allocates a character-per-entry array. Per frame that is linear
327
- * per frame, i.e. quadratic over the turn, and it was measured on the thread
328
- * the user is trying to scroll: 0.37ms a frame on a 20k-char reply (2.2% of a
329
- * 60fps budget), 1.5ms at 100k (9%), 7.7ms at 400k (46%).
330
- *
331
- * The step is proportional rather than a fixed interval so the lag is bounded
332
- * at both ends. A short reply still updates every `LIVE_METER_MIN_GROWTH`
333
- * characters, so the number visibly moves; a long one updates once per 1/32 of
334
- * itself, which at the CLI's 4-chars-per-token rate means the figure can sit at
335
- * most ~3% below the truth before its next step — invisible on a token counter.
336
- *
337
- * Nothing about the settled number depends on this cadence: the turn's
338
- * committed value comes from `foldRoll`'s single estimate over the finished
339
- * text, which every exit path writes (success, stop, and error), and a reopened
340
- * thread is rebuilt by `rollMessages`. This only decides how often the preview
341
- * is allowed to cost anything.
342
- */
343
- function liveMeterDue(measured, streamChars) {
344
- const now = Number(streamChars) || 0;
345
- // Nothing has been painted yet this turn. The cursor is per-turn and starts
346
- // UNSET (see app.js `let meterChars = null`), and an unset cursor must not be
347
- // read as "measured an empty stream" — that made the first paint wait out the
348
- // whole step below, which is the start-of-turn dead zone. The first text to
349
- // arrive paints, so the figure is on screen from the first chunk rather than
350
- // from the first 32nd of the reply.
351
- if (measured === null || measured === undefined) return now > 0;
352
- const grown = now - (Number(measured) || 0);
353
- if (grown <= 0) return false;
354
- const step = Math.max(LIVE_METER_MIN_GROWTH, Math.floor(now / LIVE_METER_GROWTH_FRACTION));
355
- return grown >= step;
356
- }
357
-
358
- /**
359
- * The status glyph for one tool-activity line.
360
- *
361
- * Only an explicit `true` is a ✓. A call with no `ok` at all is a call whose
362
- * result never arrived — the tool-call-mid-abort case — and drawing it as ✓
363
- * claimed success for work the turn never finished. It gets its own mark
364
- * instead of ✗, because interrupted is not the same as failed.
365
- */
366
- function toolMark(tool) {
367
- const t = tool || {};
368
- if (t.ok === true) return '✓';
369
- if (t.ok === false) return '✗';
370
- return '⊘';
371
- }
372
-
373
- if (typeof module !== 'undefined' && module.exports) {
374
- module.exports = {
375
- nearBottom,
376
- shouldFollow,
377
- isCancellation,
378
- stopAppliesTo,
379
- salvageTurn,
380
- replayableContent,
381
- isPlaceholderContent,
382
- replayHistory,
383
- liveMeterDue,
384
- toolMark,
385
- STICK_SLOP_PX,
386
- LIVE_METER_MIN_GROWTH,
387
- LIVE_METER_GROWTH_FRACTION,
388
- };
389
- }
3
+ "use strict";const STICK_SLOP_PX=48;function nearBottom(t,e){const n=t||{},o=Number.isFinite(e)?e:STICK_SLOP_PX;return(Number(n.scrollHeight)||0)-(Number(n.scrollTop)||0)-(Number(n.clientHeight)||0)<=o}function shouldFollow(t,e){const n=e||{};return!!n.force||!n.userScrolledUp&&nearBottom(t,n.slop)}const ABORT_SIGNATURES=[/AbortError/,/\boperation was aborted\b/i,/\buser aborted\b/i,/\brequest\s+aborted\b/i,/\bsignal is aborted\b/i,/\baborted without reason\b/i];function isCancellation(t,e){if((e||{}).userStopped)return!0;if(!t)return!1;if("AbortError"===t.name||"ABORT_ERR"===t.code)return!0;const n="string"==typeof t?t:t.message;return"string"==typeof n&&ABORT_SIGNATURES.some((t=>t.test(n)))}function stopAppliesTo(t,e){return null!=t&&t!==e}function salvageTurn(t){const e=t||{},n="string"==typeof e.streamedText?e.streamedText:"",o="string"==typeof e.reasoningText?e.reasoningText:"";return n?{text:n,reasoning:o,kind:"answer"}:o?{text:o,reasoning:"",kind:"reasoning-only"}:{text:"(stopped before any output)",reasoning:"",kind:"empty"}}function replayableContent(t,e){return"reasoning-only"===t?"(stopped: reasoning only)":"empty"===t?"(stopped before any output)":e}function flattenParts(t){if(null==t)return"";if("string"==typeof t)return t;if(!Array.isArray(t))return"";const e=[];for(const n of t)n&&"object"==typeof n&&("string"==typeof n.text?e.push(n.text):"image_url"===n.type&&e.push("[image]"));return e.join("\n")}function joinContent(t,e){if("string"==typeof t&&"string"==typeof e)return`${t}\n\n${e}`;const n=[],o=t=>{if("string"!=typeof t){if(Array.isArray(t))for(const e of t)e&&"object"==typeof e&&("image_url"===e.type?n.push(e):"string"==typeof e.text&&n.push({type:"text",text:e.text}))}else t.trim()&&n.push({type:"text",text:t})};o(t),o(e);const r=n.filter((t=>"text"===t.type)).map((t=>t.text)),s=n.filter((t=>"image_url"===t.type)),i=[];return r.length&&i.push({type:"text",text:r.join("\n\n")}),i.push(...s),1===i.length&&"text"===i[0].type?i[0].text:i}function replayHistory(t){const e=[];for(const n of Array.isArray(t)?t:[]){if(!n)continue;if("user"!==n.role&&"assistant"!==n.role)continue;const t="string"==typeof n.content||Array.isArray(n.content)?n.content:"string"==typeof n.text?n.text:"",o=Array.isArray(t)?flattenParts(t):t;if(!o.trim())continue;if("assistant"===n.role&&isPlaceholderContent(o))continue;const r=e[e.length-1];r&&r.role===n.role?r.content=joinContent(r.content,t):e.push({role:n.role,content:t})}for(;e.length&&"assistant"===e[0].role;)e.shift();return e}function isPlaceholderContent(t){const e="string"==typeof t?t.trim():"";return!!e&&("(stopped: reasoning only)"===e||"(stopped before any output)"===e||"(empty response)"===e||"(no response)"===e)}const LIVE_METER_MIN_GROWTH=32,LIVE_METER_GROWTH_FRACTION=32;function liveMeterDue(t,e){const n=Number(e)||0;if(null==t)return n>0;const o=n-(Number(t)||0);return!(o<=0)&&o>=Math.max(32,Math.floor(n/32))}function toolMark(t){const e=t||{};return!0===e.ok?"✓":!1===e.ok?"✗":"⊘"}"undefined"!=typeof module&&module.exports&&(module.exports={nearBottom:nearBottom,shouldFollow:shouldFollow,isCancellation:isCancellation,stopAppliesTo:stopAppliesTo,salvageTurn:salvageTurn,replayableContent:replayableContent,isPlaceholderContent:isPlaceholderContent,replayHistory:replayHistory,liveMeterDue:liveMeterDue,toolMark:toolMark,STICK_SLOP_PX:STICK_SLOP_PX,LIVE_METER_MIN_GROWTH:32,LIVE_METER_GROWTH_FRACTION:32});