@myrialabs/clopen 0.4.47 → 0.4.48

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 (325) hide show
  1. package/README.md +11 -11
  2. package/backend/artifacts/project/sources.ts +1 -1
  3. package/backend/auth/auth-service.ts +52 -358
  4. package/backend/auth/onboarding.test.ts +2 -3
  5. package/backend/auth/permissions.test.ts +10 -20
  6. package/backend/auth/permissions.ts +4 -290
  7. package/backend/auth/rate-limiter.test.ts +8 -9
  8. package/backend/auth/rate-limiter.ts +0 -2
  9. package/backend/auth/tokens.ts +1 -8
  10. package/backend/bootstrap.ts +7 -0
  11. package/backend/chat/attachments.test.ts +88 -0
  12. package/backend/chat/attachments.ts +159 -0
  13. package/backend/chat/engine-handoff.test.ts +0 -2
  14. package/backend/chat/stream-manager.ts +62 -62
  15. package/backend/chat/turn-answer.test.ts +64 -0
  16. package/backend/chat/turn-answer.ts +125 -0
  17. package/backend/chat-apps/awaiting.test.ts +39 -0
  18. package/backend/chat-apps/awaiting.ts +49 -0
  19. package/backend/chat-apps/conversation.ts +746 -0
  20. package/backend/chat-apps/inbound.ts +324 -0
  21. package/backend/chat-apps/index.ts +309 -0
  22. package/backend/chat-apps/link-events.ts +20 -0
  23. package/backend/chat-apps/media.test.ts +62 -0
  24. package/backend/chat-apps/media.ts +208 -0
  25. package/backend/chat-apps/messages.test.ts +229 -0
  26. package/backend/chat-apps/messages.ts +365 -0
  27. package/backend/chat-apps/outbound.ts +243 -0
  28. package/backend/chat-apps/pairing.test.ts +45 -0
  29. package/backend/chat-apps/pairing.ts +74 -0
  30. package/backend/chat-apps/platforms/index.ts +20 -0
  31. package/backend/chat-apps/platforms/telegram.test.ts +35 -0
  32. package/backend/chat-apps/platforms/telegram.ts +191 -0
  33. package/backend/chat-apps/platforms/types.ts +114 -0
  34. package/backend/chat-apps/questions.ts +133 -0
  35. package/backend/chat-apps/runtime.ts +572 -0
  36. package/backend/chat-apps/storage.test.ts +201 -0
  37. package/backend/chat-apps/turns.test.ts +45 -0
  38. package/backend/chat-apps/turns.ts +208 -0
  39. package/backend/containers/pty.ts +3 -11
  40. package/backend/control/actions/accounts.ts +89 -0
  41. package/backend/control/actions/files.ts +116 -0
  42. package/backend/control/actions/models.ts +188 -0
  43. package/backend/control/actions/projects.ts +272 -0
  44. package/backend/control/actions/sessions.ts +280 -0
  45. package/backend/control/actions/skills.ts +111 -0
  46. package/backend/control/control.test.ts +169 -0
  47. package/backend/control/define.ts +20 -0
  48. package/backend/control/folders.test.ts +67 -0
  49. package/backend/control/index.ts +213 -0
  50. package/backend/control/invoke.ts +44 -0
  51. package/backend/control/links.ts +35 -0
  52. package/backend/control/paging.ts +26 -0
  53. package/backend/control/types.ts +117 -0
  54. package/backend/control/workspace.ts +127 -0
  55. package/backend/database/migrations/084_create_channel_links.ts +71 -0
  56. package/backend/database/migrations/085_add_home_projects.ts +36 -0
  57. package/backend/database/migrations/086_single_owner.test.ts +170 -0
  58. package/backend/database/migrations/086_single_owner.ts +418 -0
  59. package/backend/database/migrations/index.ts +21 -0
  60. package/backend/database/queries/auth-queries.ts +7 -85
  61. package/backend/database/queries/channel-queries.ts +270 -0
  62. package/backend/database/queries/db-client-connection-queries.ts +18 -56
  63. package/backend/database/queries/file-share-queries.ts +5 -23
  64. package/backend/database/queries/git-identity-queries.ts +31 -42
  65. package/backend/database/queries/graph-queries.ts +1 -1
  66. package/backend/database/queries/index.ts +13 -1
  67. package/backend/database/queries/message-queries.test.ts +146 -0
  68. package/backend/database/queries/message-queries.ts +42 -1
  69. package/backend/database/queries/permission-set-queries.ts +1 -1
  70. package/backend/database/queries/profile-queries.ts +1 -1
  71. package/backend/database/queries/project-mcp-trust-queries.ts +1 -1
  72. package/backend/database/queries/project-queries.ts +50 -63
  73. package/backend/database/queries/session-queries.ts +28 -56
  74. package/backend/database/queries/ssh-connection-queries.ts +18 -41
  75. package/backend/db-client/integrations/projector.ts +0 -5
  76. package/backend/engine/adapters/claude/message-converter.test.ts +47 -0
  77. package/backend/engine/adapters/claude/message-converter.ts +25 -2
  78. package/backend/engine/adapters/cline/message-converter.ts +0 -1
  79. package/backend/engine/adapters/cline/stream.ts +11 -15
  80. package/backend/engine/adapters/codex/message-converter.ts +0 -2
  81. package/backend/engine/adapters/copilot/message-converter.ts +1 -2
  82. package/backend/engine/adapters/cursor/message-converter.ts +0 -1
  83. package/backend/engine/adapters/opencode/message-converter.ts +2 -4
  84. package/backend/engine/adapters/opencode/stream.ts +11 -0
  85. package/backend/engine/adapters/parent-scope.test.ts +38 -0
  86. package/backend/engine/adapters/parent-scope.ts +44 -0
  87. package/backend/engine/adapters/pi/message-converter.ts +0 -1
  88. package/backend/engine/adapters/pi/stream.ts +4 -5
  89. package/backend/engine/adapters/qwen/message-converter.ts +0 -2
  90. package/backend/engine/bootstrap-default-engine.ts +2 -2
  91. package/backend/engine/docs/adapter-contract.md +8 -1
  92. package/backend/engine/install-runner.ts +8 -17
  93. package/backend/files/file-shares.ts +20 -44
  94. package/backend/files/file-size-limit.ts +3 -3
  95. package/backend/files/image-processing.ts +1 -1
  96. package/backend/files/os-clipboard.ts +2 -2
  97. package/backend/git/git-executor.ts +25 -5
  98. package/backend/git/git-service.ts +420 -19
  99. package/backend/git/identity/credential-helper.test.ts +2 -7
  100. package/backend/git/identity/import-local.test.ts +11 -16
  101. package/backend/git/identity/import-local.ts +12 -12
  102. package/backend/git/identity/index.ts +10 -24
  103. package/backend/git/identity/resolve.ts +13 -14
  104. package/backend/git/identity/scope-config.ts +33 -28
  105. package/backend/git/identity/service.ts +44 -47
  106. package/backend/git/op-lock.test.ts +111 -0
  107. package/backend/git/op-lock.ts +180 -0
  108. package/backend/git/remote-sync.test.ts +249 -0
  109. package/backend/host/metrics.ts +1 -1
  110. package/backend/http/bearer-auth.ts +1 -2
  111. package/backend/http/files-download.test.ts +8 -9
  112. package/backend/http/files-download.ts +2 -2
  113. package/backend/http/files-shared.test.ts +62 -89
  114. package/backend/http/files-shared.ts +2 -2
  115. package/backend/http/files-upload.test.ts +3 -7
  116. package/backend/http/files-upload.ts +3 -3
  117. package/backend/http/notes-images.test.ts +15 -38
  118. package/backend/http/notes-images.ts +6 -8
  119. package/backend/http/ssh-sftp.ts +5 -10
  120. package/backend/index.ts +21 -7
  121. package/backend/integrations/accounts.ts +61 -3
  122. package/backend/integrations/index.ts +2 -2
  123. package/backend/integrations/registry/define.ts +15 -0
  124. package/backend/integrations/registry/index.ts +2 -0
  125. package/backend/integrations/registry/providers/telegram.ts +54 -0
  126. package/backend/integrations/registry/types.ts +21 -1
  127. package/backend/integrations/webhooks/gateway.ts +7 -6
  128. package/backend/integrations/webhooks/verify.test.ts +30 -0
  129. package/backend/integrations/webhooks/verify.ts +4 -0
  130. package/backend/mcp/external/project.ts +1 -1
  131. package/backend/mcp/internal/remote-server.test.ts +1 -3
  132. package/backend/mcp/internal/remote-server.ts +4 -4
  133. package/backend/mcp/internal/servers/browser-automation/actions/dialogs.ts +67 -0
  134. package/backend/mcp/internal/servers/browser-automation/actions/index.ts +3 -1
  135. package/backend/mcp/internal/servers/browser-automation/actions/input.ts +5 -5
  136. package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +157 -28
  137. package/backend/mcp/internal/servers/browser-automation/context.ts +1 -0
  138. package/backend/mcp/internal/servers/browser-automation/description.ts +5 -2
  139. package/backend/mcp/internal/servers/browser-automation/format.ts +24 -1
  140. package/backend/mcp/internal/servers/browser-automation/runner.ts +68 -3
  141. package/backend/memory/redact.ts +1 -1
  142. package/backend/notes/access.test.ts +28 -29
  143. package/backend/notes/access.ts +6 -11
  144. package/backend/permissions/service.ts +1 -1
  145. package/backend/preview/browser/browser-clipboard.test.ts +46 -0
  146. package/backend/preview/browser/browser-clipboard.ts +67 -0
  147. package/backend/preview/browser/browser-dialog-handler.ts +95 -6
  148. package/backend/preview/browser/browser-host-bridge.ts +9 -0
  149. package/backend/preview/browser/browser-interaction-handler.ts +316 -86
  150. package/backend/preview/browser/browser-mcp-control.ts +34 -0
  151. package/backend/preview/browser/browser-pool.ts +67 -6
  152. package/backend/preview/browser/browser-preview-service.ts +361 -25
  153. package/backend/preview/browser/browser-tab-manager.ts +66 -23
  154. package/backend/preview/browser/browser-video-capture.ts +92 -5
  155. package/backend/preview/browser/element-locator.ts +190 -17
  156. package/backend/preview/browser/keyboard-chords.test.ts +68 -0
  157. package/backend/preview/browser/keyboard-chords.ts +132 -0
  158. package/backend/preview/browser/paste.ts +81 -0
  159. package/backend/preview/browser/process-usage.ts +142 -0
  160. package/backend/preview/browser/profile-lock.ts +209 -0
  161. package/backend/preview/browser/scripts/host-bridge.ts +59 -0
  162. package/backend/preview/browser/scripts/video-stream.ts +58 -0
  163. package/backend/preview/browser/types.ts +40 -2
  164. package/backend/preview/browser/viewport.test.ts +45 -0
  165. package/backend/preview/browser/viewport.ts +96 -0
  166. package/backend/profiles/service.ts +2 -2
  167. package/backend/project/status-manager.ts +43 -172
  168. package/backend/projects/home.ts +61 -0
  169. package/backend/projects/project-cleanup.integration.test.ts +2 -2
  170. package/backend/projects/project-cleanup.ts +0 -8
  171. package/backend/skills/service.ts +2 -3
  172. package/backend/snapshot/helpers.ts +0 -1
  173. package/backend/snapshot/restore.ts +252 -0
  174. package/backend/ssh/ptykit.ts +1 -2
  175. package/backend/terminal/ptykit.ts +5 -8
  176. package/backend/tunnel/public-origin.ts +31 -20
  177. package/backend/utils/user-helpers.ts +0 -55
  178. package/backend/utils/ws.ts +61 -154
  179. package/backend/worktrees/branching/engine.ts +3 -16
  180. package/backend/worktrees/branching/index.ts +1 -1
  181. package/backend/worktrees/branching/projector.ts +1 -1
  182. package/backend/worktrees/branching/service.ts +13 -25
  183. package/backend/ws/access.ts +12 -21
  184. package/backend/ws/artifacts/generate.ts +2 -2
  185. package/backend/ws/auth/device.ts +16 -71
  186. package/backend/ws/auth/index.ts +4 -22
  187. package/backend/ws/auth/login.test.ts +14 -60
  188. package/backend/ws/auth/login.ts +24 -124
  189. package/backend/ws/auth/status.ts +0 -1
  190. package/backend/ws/chat/composer.ts +118 -118
  191. package/backend/ws/chat/stream.ts +64 -47
  192. package/backend/ws/chat-apps/index.ts +117 -0
  193. package/backend/ws/containers/actions.ts +11 -25
  194. package/backend/ws/containers/index.ts +3 -4
  195. package/backend/ws/containers/list.ts +10 -36
  196. package/backend/ws/containers/logs.ts +2 -2
  197. package/backend/ws/containers/tunnel.ts +1 -2
  198. package/backend/ws/db-client/access.ts +2 -16
  199. package/backend/ws/db-client/accounts.ts +5 -15
  200. package/backend/ws/db-client/connections.ts +17 -25
  201. package/backend/ws/db-client/context.ts +1 -1
  202. package/backend/ws/db-client/env.ts +7 -10
  203. package/backend/ws/db-client/index.ts +2 -2
  204. package/backend/ws/db-client/io.ts +5 -5
  205. package/backend/ws/db-client/query-history-retention.test.ts +8 -8
  206. package/backend/ws/db-client/query.ts +18 -17
  207. package/backend/ws/db-client/schema.ts +13 -13
  208. package/backend/ws/db-client/structure.ts +35 -35
  209. package/backend/ws/db-client/supabase.ts +4 -21
  210. package/backend/ws/deployments/context.ts +1 -1
  211. package/backend/ws/deployments/crud.ts +0 -5
  212. package/backend/ws/engine/access.ts +7 -20
  213. package/backend/ws/engine/claude/accounts.ts +4 -4
  214. package/backend/ws/engine/cline/accounts.ts +5 -5
  215. package/backend/ws/engine/codex/accounts.ts +3 -3
  216. package/backend/ws/engine/pi/accounts.ts +5 -5
  217. package/backend/ws/files/clipboard.ts +8 -27
  218. package/backend/ws/files/image-edit.ts +6 -6
  219. package/backend/ws/files/index.ts +1 -1
  220. package/backend/ws/files/path-access.test.ts +32 -131
  221. package/backend/ws/files/path-access.ts +8 -87
  222. package/backend/ws/files/read.ts +15 -19
  223. package/backend/ws/files/reveal.ts +3 -3
  224. package/backend/ws/files/search.ts +7 -7
  225. package/backend/ws/files/share.ts +9 -13
  226. package/backend/ws/files/state.ts +9 -12
  227. package/backend/ws/files/watch.ts +3 -3
  228. package/backend/ws/files/write.ts +30 -31
  229. package/backend/ws/git/branch.ts +80 -59
  230. package/backend/ws/git/commit.ts +63 -44
  231. package/backend/ws/git/conflict.ts +16 -18
  232. package/backend/ws/git/identity-context.ts +8 -17
  233. package/backend/ws/git/identity.ts +17 -27
  234. package/backend/ws/git/index.ts +3 -1
  235. package/backend/ws/git/op-state.ts +139 -0
  236. package/backend/ws/git/remote.ts +241 -141
  237. package/backend/ws/git/repo-context.ts +67 -0
  238. package/backend/ws/git/staging.ts +18 -28
  239. package/backend/ws/git/status.ts +5 -2
  240. package/backend/ws/index.ts +5 -1
  241. package/backend/ws/instructions/crud.ts +1 -1
  242. package/backend/ws/integrations/crud.ts +1 -3
  243. package/backend/ws/mcp/crud.ts +3 -5
  244. package/backend/ws/mcp/tools.ts +1 -1
  245. package/backend/ws/memory/crud.ts +14 -46
  246. package/backend/ws/memory/index.ts +2 -2
  247. package/backend/ws/messages/crud.ts +29 -8
  248. package/backend/ws/notes/index.ts +33 -39
  249. package/backend/ws/permissions/crud.ts +3 -4
  250. package/backend/ws/ports/index.ts +1 -2
  251. package/backend/ws/ports/kill.ts +5 -10
  252. package/backend/ws/ports/list.ts +4 -10
  253. package/backend/ws/preview/access.ts +1 -1
  254. package/backend/ws/preview/browser/interact.ts +52 -79
  255. package/backend/ws/preview/browser/native-ui.ts +24 -0
  256. package/backend/ws/preview/browser/stats.ts +98 -1
  257. package/backend/ws/preview/browser/tab-info.ts +15 -0
  258. package/backend/ws/preview/browser/tab.ts +19 -4
  259. package/backend/ws/preview/index.ts +27 -0
  260. package/backend/ws/profiles/crud.ts +6 -8
  261. package/backend/ws/project-artifacts/index.ts +30 -31
  262. package/backend/ws/projects/crud.ts +33 -35
  263. package/backend/ws/projects/index.ts +3 -8
  264. package/backend/ws/projects/info.ts +3 -3
  265. package/backend/ws/projects/overview.ts +19 -30
  266. package/backend/ws/projects/status.ts +17 -57
  267. package/backend/ws/sessions/crud.ts +34 -45
  268. package/backend/ws/sessions/index.ts +1 -1
  269. package/backend/ws/settings/crud.ts +1 -1
  270. package/backend/ws/skills/crud.ts +3 -4
  271. package/backend/ws/skills/index.ts +1 -1
  272. package/backend/ws/snapshot/changes.ts +7 -7
  273. package/backend/ws/snapshot/restore.ts +11 -295
  274. package/backend/ws/snapshot/timeline.ts +2 -4
  275. package/backend/ws/ssh/access.ts +6 -21
  276. package/backend/ws/ssh/connections.ts +21 -31
  277. package/backend/ws/ssh/forwards.ts +12 -12
  278. package/backend/ws/ssh/sftp.ts +34 -34
  279. package/backend/ws/ssh/tunnel.ts +1 -2
  280. package/backend/ws/stack/install.ts +7 -17
  281. package/backend/ws/subagents/crud.ts +1 -1
  282. package/backend/ws/tunnel/share.ts +0 -2
  283. package/backend/ws/user/crud.ts +1 -59
  284. package/backend/ws/user/index.ts +1 -1
  285. package/backend/ws/work/context.ts +1 -1
  286. package/backend/ws/work/crud.ts +0 -5
  287. package/backend/ws/workspace/state.ts +10 -13
  288. package/backend/ws/worktrees/branching.ts +7 -36
  289. package/backend/ws/worktrees/crud.ts +2 -2
  290. package/bin/clopen.ts +10 -11
  291. package/dist/assets/{index-CwuKQelV.js → index-CONlwAXZ.js} +1 -1
  292. package/dist/assets/{index-BTcR1byF.js → index-DJzwt-hq.js} +1 -1
  293. package/dist/assets/main-BUiUL3eZ.css +1 -0
  294. package/dist/assets/main-nBOb9YOA.js +1240 -0
  295. package/dist/index.html +2 -2
  296. package/package.json +2 -1
  297. package/shared/constants/attachments.ts +14 -0
  298. package/shared/constants/engine-tools.ts +1 -1
  299. package/shared/constants/preview.test.ts +259 -0
  300. package/shared/constants/preview.ts +1018 -57
  301. package/shared/constants/tool-icons.ts +5 -0
  302. package/shared/types/containers/container.ts +1 -1
  303. package/shared/types/database/schema.ts +9 -6
  304. package/shared/types/git-identity.ts +1 -1
  305. package/shared/types/git.ts +116 -0
  306. package/shared/types/integrations.ts +7 -0
  307. package/shared/types/stores/settings.ts +1 -1
  308. package/shared/types/unified/index.ts +1 -1
  309. package/shared/types/unified/message.ts +0 -7
  310. package/shared/types/unified/stream.ts +2 -4
  311. package/shared/types/unified/tool.ts +15 -1
  312. package/shared/utils/ask-user-question.test.ts +319 -0
  313. package/shared/utils/ask-user-question.ts +154 -0
  314. package/shared/utils/home-project.ts +25 -0
  315. package/shared/utils/logger.ts +2 -0
  316. package/shared/utils/ws-server.ts +32 -7
  317. package/backend/auth/session-invalidation.test.ts +0 -112
  318. package/backend/auth/session-invalidation.ts +0 -77
  319. package/backend/project/status-manager.test.ts +0 -32
  320. package/backend/ws/auth/invites.ts +0 -94
  321. package/backend/ws/auth/users.ts +0 -112
  322. package/backend/ws/projects/presence.ts +0 -46
  323. package/backend/ws/stack/access.ts +0 -31
  324. package/dist/assets/main-BiDNMWb3.css +0 -1
  325. package/dist/assets/main-BlhKWz2B.js +0 -1241
@@ -0,0 +1,125 @@
1
+ /**
2
+ * The answer a turn ended with — what a chat app shows as "the reply".
3
+ *
4
+ * A turn is a user message followed by assistant messages, tool calls and tool
5
+ * results; what reads as the answer is the text of the LAST assistant message
6
+ * that has any (the earlier ones are the "let me look at…" between tools).
7
+ * `head_summary` is not this: it is cut to 200 characters with code removed,
8
+ * fit for a session list, not for a conversation.
9
+ *
10
+ * Walks back from HEAD only as far as the turn's own user message, one row at
11
+ * a time, so a long session is not loaded whole to read its last reply.
12
+ */
13
+
14
+ import { messageQueries, sessionQueries } from '$backend/database/queries';
15
+ import { isInternalToolMessage } from '$backend/snapshot/helpers';
16
+ import type { UnifiedMessage } from '$shared/types/unified';
17
+
18
+ /** A turn is never this long; the bound only protects against a corrupt parent chain. */
19
+ const MAX_WALK = 2000;
20
+
21
+ export interface TurnAnswer {
22
+ /** The answer's Markdown, or '' when the turn produced no text. */
23
+ text: string;
24
+ /** The error the turn ended with, when it failed — never part of `text`. */
25
+ error: string | null;
26
+ /** HEAD when read — the message a reply to this answer continues from. */
27
+ endMessageId: string | null;
28
+ /** The turn's own user message. */
29
+ userMessageId: string | null;
30
+ }
31
+
32
+ /**
33
+ * Where a turn starts: any message the user sent — including one that is only
34
+ * a photo, which a snapshot checkpoint (text required) would not count.
35
+ */
36
+ function isTurnStart(row: { data: string }): boolean {
37
+ try {
38
+ const message = JSON.parse(row.data) as UnifiedMessage;
39
+ return message.type === 'user' && !isInternalToolMessage(message);
40
+ } catch {
41
+ return false;
42
+ }
43
+ }
44
+
45
+ /** The stream records a failure as an assistant message with this prefix. */
46
+ const ERROR_PREFIX = '**Error:** ';
47
+
48
+ function textOf(message: UnifiedMessage): string {
49
+ if (message.type !== 'assistant' || !Array.isArray(message.content)) return '';
50
+ return message.content
51
+ .filter((block) => block.type === 'text' && typeof block.text === 'string')
52
+ .map((block) => (block as { text: string }).text)
53
+ .join('\n\n')
54
+ .trim();
55
+ }
56
+
57
+ export function lastTurnAnswer(chatSessionId: string): TurnAnswer {
58
+ const head = sessionQueries.getHead(chatSessionId);
59
+ if (!head) return { text: '', error: null, endMessageId: null, userMessageId: null };
60
+
61
+ let error: string | null = null;
62
+ let currentId: string | null = head;
63
+ for (let steps = 0; currentId && steps < MAX_WALK; steps++) {
64
+ const row = messageQueries.getById(currentId);
65
+ if (!row) break;
66
+ if (isTurnStart(row)) return { text: '', error, endMessageId: head, userMessageId: row.id };
67
+ try {
68
+ const message = JSON.parse(row.data) as UnifiedMessage;
69
+ // Sub-agent output carries the tool call it answers; it is not the reply.
70
+ const isSubAgent = message.type === 'assistant' && message.parent?.toolUseId != null;
71
+ const text = isSubAgent ? '' : textOf(message);
72
+ // The failure the turn ended with is reported apart; what the turn said
73
+ // before it is still the answer so far.
74
+ if (text.startsWith(ERROR_PREFIX) && error === null && steps === 0) {
75
+ error = text.slice(ERROR_PREFIX.length).trim();
76
+ } else if (text) {
77
+ return { text, error, endMessageId: head, userMessageId: findTurnStart(row.parent_message_id ?? null) };
78
+ }
79
+ } catch {
80
+ // Unparseable row — skip it like any message without text.
81
+ }
82
+ currentId = row.parent_message_id ?? null;
83
+ }
84
+ return { text: '', error, endMessageId: head, userMessageId: null };
85
+ }
86
+
87
+ function findTurnStart(fromId: string | null): string | null {
88
+ let currentId = fromId;
89
+ for (let steps = 0; currentId && steps < MAX_WALK; steps++) {
90
+ const row = messageQueries.getById(currentId);
91
+ if (!row) return null;
92
+ if (isTurnStart(row)) return row.id;
93
+ currentId = row.parent_message_id ?? null;
94
+ }
95
+ return null;
96
+ }
97
+
98
+ /** The text of a user message as the user wrote it — what /retry and an edit resend. */
99
+ export function userMessageText(messageId: string): string | null {
100
+ const row = messageQueries.getById(messageId);
101
+ if (!row) return null;
102
+ try {
103
+ const message = JSON.parse(row.data) as UnifiedMessage;
104
+ if (message.type !== 'user' || !Array.isArray(message.content)) return null;
105
+ return message.content
106
+ .filter((block) => block.type === 'text' && typeof (block as { text?: string }).text === 'string')
107
+ .map((block) => (block as { text: string }).text)
108
+ .join('\n')
109
+ .trim() || null;
110
+ } catch {
111
+ return null;
112
+ }
113
+ }
114
+
115
+ /** Cut Markdown at a paragraph boundary near `max`, so a quoted excerpt never ends mid-fence. */
116
+ export function excerptMarkdown(markdown: string, max: number): string {
117
+ const trimmed = markdown.trim();
118
+ if (trimmed.length <= max) return trimmed;
119
+ const window = trimmed.slice(0, max);
120
+ const cut = window.lastIndexOf('\n\n');
121
+ let excerpt = cut > max / 2 ? window.slice(0, cut) : window;
122
+ // An odd number of fences means the cut landed inside a code block — close it.
123
+ if ((excerpt.match(/^\s*```/gm) ?? []).length % 2 === 1) excerpt += '\n```';
124
+ return `${excerpt.trimEnd()}\n\n…`;
125
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * "The next thing you type is for this": one per chat, newest wins, used
3
+ * once, and it lapses.
4
+ */
5
+
6
+ import { describe, it, expect, afterEach, setSystemTime } from 'bun:test';
7
+ import { clearAwaiting, hasAwaiting, isAwaiting, resetAwaiting, setAwaiting, takeAwaiting } from './awaiting';
8
+
9
+ afterEach(() => {
10
+ resetAwaiting();
11
+ setSystemTime();
12
+ });
13
+
14
+ describe('awaiting typed words', () => {
15
+ it('is taken once, and the newest ask wins', () => {
16
+ setAwaiting('link', { kind: 'question', rowId: 'q1' });
17
+ setAwaiting('link', { kind: 'ask', rowId: 'a1' });
18
+ expect(isAwaiting('link', 'q1')).toBe(false);
19
+ expect(takeAwaiting('link')).toEqual({ kind: 'ask', rowId: 'a1' });
20
+ expect(takeAwaiting('link')).toBeNull();
21
+ });
22
+
23
+ it('knows whether the chat is waiting at all', () => {
24
+ expect(hasAwaiting('link')).toBe(false);
25
+ setAwaiting('link', { kind: 'ask', rowId: 'a1' });
26
+ expect(hasAwaiting('link')).toBe(true);
27
+ expect(hasAwaiting('other-link')).toBe(false);
28
+ });
29
+
30
+ it('lapses, and a command clears it', () => {
31
+ setAwaiting('link', { kind: 'question', rowId: 'q1' });
32
+ setSystemTime(new Date(Date.now() + 11 * 60 * 1000));
33
+ expect(takeAwaiting('link')).toBeNull();
34
+ setSystemTime();
35
+ setAwaiting('link', { kind: 'question', rowId: 'q1' });
36
+ clearAwaiting('link');
37
+ expect(takeAwaiting('link')).toBeNull();
38
+ });
39
+ });
@@ -0,0 +1,49 @@
1
+ /**
2
+ * "The next thing you type is for this." A chat has no text field next to a
3
+ * button, so when a button needs words (a question's "Other…", a new folder's
4
+ * name), the bot asks and the chat's NEXT plain message is the answer,
5
+ * instead of a prompt for the session. Replying to the asking message works
6
+ * too, and is the only way once something newer was asked.
7
+ *
8
+ * One per chat, newest wins, and it lapses: a message typed an hour later is
9
+ * a message, not a late answer. A command clears it.
10
+ */
11
+
12
+ export type Awaiting = { kind: 'question'; rowId: string } | { kind: 'ask'; rowId: string };
13
+
14
+ const AWAIT_TTL_MS = 10 * 60 * 1000;
15
+
16
+ const awaiting = new Map<string, Awaiting & { at: number }>();
17
+
18
+ export function setAwaiting(linkId: string, entry: Awaiting): void {
19
+ awaiting.set(linkId, { ...entry, at: Date.now() });
20
+ }
21
+
22
+ /** What the chat's next plain message answers, if anything. Asking consumes it. */
23
+ export function takeAwaiting(linkId: string): Awaiting | null {
24
+ const entry = awaiting.get(linkId);
25
+ awaiting.delete(linkId);
26
+ if (!entry || Date.now() - entry.at > AWAIT_TTL_MS) return null;
27
+ return entry.kind === 'question' ? { kind: 'question', rowId: entry.rowId } : { kind: 'ask', rowId: entry.rowId };
28
+ }
29
+
30
+ /** Is the chat waiting to type into this row? */
31
+ export function isAwaiting(linkId: string, rowId: string): boolean {
32
+ const entry = awaiting.get(linkId);
33
+ return Boolean(entry && entry.rowId === rowId && Date.now() - entry.at <= AWAIT_TTL_MS);
34
+ }
35
+
36
+ /** Is the chat waiting for typed words at all? */
37
+ export function hasAwaiting(linkId: string): boolean {
38
+ const entry = awaiting.get(linkId);
39
+ return Boolean(entry && Date.now() - entry.at <= AWAIT_TTL_MS);
40
+ }
41
+
42
+ export function clearAwaiting(linkId: string): void {
43
+ awaiting.delete(linkId);
44
+ }
45
+
46
+ /** Tests only. */
47
+ export function resetAwaiting(): void {
48
+ awaiting.clear();
49
+ }