@myrialabs/clopen 0.4.50 → 0.4.51

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 (457) hide show
  1. package/README.md +2 -1
  2. package/backend/chat/attachments.test.ts +106 -17
  3. package/backend/chat/attachments.ts +131 -34
  4. package/backend/chat/engine-handoff.ts +42 -6
  5. package/backend/chat/stream-manager.ts +41 -5
  6. package/backend/containers/actions.ts +22 -0
  7. package/backend/control/actions/projects.ts +93 -37
  8. package/backend/control/folders.test.ts +27 -2
  9. package/backend/control/index.ts +2 -1
  10. package/backend/database/migrations/056_support_mssql_driver.ts +7 -0
  11. package/backend/database/migrations/077_create_integration_db_links.ts +7 -0
  12. package/backend/database/migrations/086_single_owner.ts +66 -73
  13. package/backend/database/migrations/090_create_storage_tables.ts +83 -0
  14. package/backend/database/migrations/index.ts +11 -2
  15. package/backend/database/queries/integration-queries.ts +10 -0
  16. package/backend/database/queries/memory-queue-queries.ts +11 -0
  17. package/backend/database/queries/project-queries.ts +6 -0
  18. package/backend/database/queries/session-queries.ts +3 -0
  19. package/backend/database/utils/migration-runner.test.ts +196 -0
  20. package/backend/database/utils/migration-runner.ts +61 -16
  21. package/backend/database/utils/seeder-runner.ts +22 -16
  22. package/backend/engine/adapters/claude/stream.ts +5 -0
  23. package/backend/engine/adapters/cursor/environment.ts +1 -1
  24. package/backend/engine/adapters/qwen/session-store.ts +10 -1
  25. package/backend/engine/types.ts +7 -0
  26. package/backend/git/identity/cli.ts +35 -0
  27. package/backend/git/identity/env.test.ts +52 -1
  28. package/backend/git/identity/env.ts +69 -10
  29. package/backend/git/identity/index.ts +25 -6
  30. package/backend/git/identity/resolve.ts +2 -1
  31. package/backend/git/identity/scope-config.ts +13 -7
  32. package/backend/git-hosts/accounts.ts +106 -0
  33. package/backend/git-hosts/auth/client-ids.ts +34 -0
  34. package/backend/git-hosts/auth/connect.ts +110 -0
  35. package/backend/git-hosts/auth/endpoints.ts +130 -0
  36. package/backend/git-hosts/auth/flows.ts +355 -0
  37. package/backend/git-hosts/auth/local.ts +152 -0
  38. package/backend/git-hosts/ci.ts +18 -0
  39. package/backend/git-hosts/cli.test.ts +82 -0
  40. package/backend/git-hosts/cli.ts +342 -0
  41. package/backend/git-hosts/credentials.ts +173 -0
  42. package/backend/git-hosts/git-credential.ts +30 -0
  43. package/backend/git-hosts/health.ts +39 -0
  44. package/backend/git-hosts/http.ts +174 -0
  45. package/backend/git-hosts/index.ts +34 -0
  46. package/backend/git-hosts/providers/bitbucket.ts +186 -0
  47. package/backend/git-hosts/providers/gitea.ts +208 -0
  48. package/backend/git-hosts/providers/github.ts +270 -0
  49. package/backend/git-hosts/providers/gitlab-client.ts +70 -0
  50. package/backend/git-hosts/providers/gitlab.ts +285 -0
  51. package/backend/git-hosts/providers/locators.test.ts +68 -0
  52. package/backend/git-hosts/registry.ts +50 -0
  53. package/backend/git-hosts/service.ts +432 -0
  54. package/backend/git-hosts/types.ts +82 -0
  55. package/backend/git-hosts/urls.test.ts +42 -0
  56. package/backend/git-hosts/urls.ts +69 -0
  57. package/backend/index.ts +50 -10
  58. package/backend/integrations/accounts.ts +50 -3
  59. package/backend/integrations/registry/define.ts +9 -0
  60. package/backend/integrations/registry/index.ts +11 -0
  61. package/backend/integrations/registry/providers/bitbucket.ts +51 -0
  62. package/backend/integrations/registry/providers/gitea.ts +53 -0
  63. package/backend/integrations/registry/providers/github.ts +24 -7
  64. package/backend/integrations/registry/providers/gitlab.ts +57 -0
  65. package/backend/integrations/registry/types.ts +13 -1
  66. package/backend/mcp/internal/servers/browser-automation/actions/inspect.ts +115 -31
  67. package/backend/mcp/internal/servers/browser-automation/description.ts +5 -0
  68. package/backend/mentions/diff.ts +63 -0
  69. package/backend/mentions/index.ts +109 -0
  70. package/backend/mentions/mentions.test.ts +27 -0
  71. package/backend/mentions/shared.ts +82 -0
  72. package/backend/mentions/sources/ai-changes.ts +140 -0
  73. package/backend/mentions/sources/chats.ts +122 -0
  74. package/backend/mentions/sources/database.ts +211 -0
  75. package/backend/mentions/sources/files.ts +240 -0
  76. package/backend/mentions/sources/git.ts +308 -0
  77. package/backend/mentions/sources/notes-memory.ts +121 -0
  78. package/backend/mentions/sources/ops.ts +125 -0
  79. package/backend/mentions/sources/preview.ts +153 -0
  80. package/backend/mentions/sources/projects.ts +114 -0
  81. package/backend/mentions/sources/system.ts +135 -0
  82. package/backend/mentions/sources/terminal.ts +50 -0
  83. package/backend/mentions/types.ts +75 -0
  84. package/backend/preview/browser/bot-check-frames.test.ts +49 -0
  85. package/backend/preview/browser/bot-check-frames.ts +66 -0
  86. package/backend/preview/browser/browser-dialog-handler.ts +6 -5
  87. package/backend/preview/browser/browser-host-bridge.ts +4 -2
  88. package/backend/preview/browser/browser-native-ui-handler.ts +21 -0
  89. package/backend/preview/browser/browser-pool.ts +26 -17
  90. package/backend/preview/browser/browser-preview-service.ts +164 -2
  91. package/backend/preview/browser/browser-screenshot.test.ts +113 -0
  92. package/backend/preview/browser/browser-screenshot.ts +396 -0
  93. package/backend/preview/browser/browser-tab-manager.ts +35 -186
  94. package/backend/preview/browser/browser-video-capture.ts +12 -17
  95. package/backend/preview/browser/element-describe.ts +125 -0
  96. package/backend/preview/browser/page-binding.ts +141 -0
  97. package/backend/preview/browser/screenshot-files.test.ts +117 -0
  98. package/backend/preview/browser/screenshot-files.ts +134 -0
  99. package/backend/preview/browser/user-agent.test.ts +25 -0
  100. package/backend/preview/browser/user-agent.ts +100 -0
  101. package/backend/projects/engine-transcripts.ts +153 -0
  102. package/backend/projects/folder-guard.ts +55 -0
  103. package/backend/projects/move.test.ts +208 -0
  104. package/backend/projects/move.ts +277 -0
  105. package/backend/projects/project-cleanup.integration.test.ts +3 -1
  106. package/backend/projects/project-cleanup.ts +10 -0
  107. package/backend/storage/actions.ts +260 -0
  108. package/backend/storage/chats.test.ts +57 -0
  109. package/backend/storage/chats.ts +170 -0
  110. package/backend/storage/database.ts +105 -0
  111. package/backend/storage/engine-homes.ts +145 -0
  112. package/backend/storage/fs-size.ts +111 -0
  113. package/backend/storage/history.ts +39 -0
  114. package/backend/storage/policy.ts +156 -0
  115. package/backend/storage/preview-profiles.ts +169 -0
  116. package/backend/storage/purge.ts +158 -0
  117. package/backend/storage/scanner.ts +339 -0
  118. package/backend/storage/session-index.ts +208 -0
  119. package/backend/storage/snapshots.ts +99 -0
  120. package/backend/storage/storage-flow.test.ts +137 -0
  121. package/backend/storage/transcripts/aliases.ts +75 -0
  122. package/backend/storage/transcripts/claude-dedupe.test.ts +101 -0
  123. package/backend/storage/transcripts/claude-dedupe.ts +152 -0
  124. package/backend/storage/transcripts/claude.ts +155 -0
  125. package/backend/storage/transcripts/cursor.ts +165 -0
  126. package/backend/storage/transcripts/file-stores.ts +224 -0
  127. package/backend/storage/transcripts/index.ts +207 -0
  128. package/backend/storage/transcripts/opencode.ts +124 -0
  129. package/backend/storage/transcripts/references.ts +42 -0
  130. package/backend/storage/transcripts/types.ts +58 -0
  131. package/backend/storage/worktrees.ts +114 -0
  132. package/backend/terminal/ptykit.ts +25 -0
  133. package/backend/update/registry.ts +24 -0
  134. package/backend/update/release-notes.test.ts +110 -0
  135. package/backend/update/release-notes.ts +132 -0
  136. package/backend/update/restart.ts +159 -0
  137. package/backend/utils/env.test.ts +64 -0
  138. package/backend/utils/env.ts +23 -55
  139. package/backend/utils/port-utils.ts +13 -0
  140. package/backend/work/index.ts +13 -2
  141. package/backend/work/providers/github/actions-logs.ts +1 -1
  142. package/backend/work/providers/github/adapter.ts +32 -1
  143. package/backend/work/providers/github/client.ts +7 -3
  144. package/backend/work/providers/gitlab/adapter.ts +770 -0
  145. package/backend/work/providers/gitlab/system-notes.test.ts +24 -0
  146. package/backend/work/service.ts +60 -36
  147. package/backend/work/start-work.ts +1 -1
  148. package/backend/work/types.ts +4 -0
  149. package/backend/worktrees/index.ts +1 -0
  150. package/backend/worktrees/service.ts +23 -2
  151. package/backend/ws/chat/composer.ts +47 -6
  152. package/backend/ws/files/search.ts +1 -1
  153. package/backend/ws/files/write.ts +27 -0
  154. package/backend/ws/hosting/index.ts +231 -0
  155. package/backend/ws/index.ts +12 -2
  156. package/backend/ws/integrations/crud.ts +31 -5
  157. package/backend/ws/integrations/hosting-auth.ts +227 -0
  158. package/backend/ws/integrations/index.ts +2 -0
  159. package/backend/ws/mentions/index.ts +81 -0
  160. package/backend/ws/preview/browser/screenshot.ts +184 -0
  161. package/backend/ws/preview/index.ts +23 -0
  162. package/backend/ws/projects/crud.ts +66 -58
  163. package/backend/ws/sessions/crud.ts +7 -95
  164. package/backend/ws/snapshot/changes.ts +3 -3
  165. package/backend/ws/storage/index.ts +102 -0
  166. package/backend/ws/system/index.ts +7 -0
  167. package/backend/ws/system/operations.ts +125 -69
  168. package/backend/ws/work/context.ts +3 -4
  169. package/backend/ws/work/crud.ts +33 -0
  170. package/backend/ws/work/schemas.ts +4 -1
  171. package/bin/clopen.ts +15 -16
  172. package/dist/assets/{index-By6KdSLT.js → index-B9rHDvjg.js} +1 -1
  173. package/dist/assets/index-B9rHDvjg.js.br +0 -0
  174. package/dist/assets/index-B9rHDvjg.js.gz +0 -0
  175. package/dist/assets/{index-DV4TiDn1.js → index-DFlwOd7k.js} +1 -1
  176. package/dist/assets/index-DFlwOd7k.js.br +0 -0
  177. package/dist/assets/index-DFlwOd7k.js.gz +0 -0
  178. package/dist/assets/main-Bz_brxxS.css +1 -0
  179. package/dist/assets/main-Bz_brxxS.css.br +0 -0
  180. package/dist/assets/main-Bz_brxxS.css.gz +0 -0
  181. package/dist/assets/main-DRcX9SHx.js +1273 -0
  182. package/dist/assets/main-DRcX9SHx.js.br +0 -0
  183. package/dist/assets/main-DRcX9SHx.js.gz +0 -0
  184. package/dist/assets/pdf-DfwfQPLy.js +57 -0
  185. package/dist/assets/pdf-DfwfQPLy.js.br +0 -0
  186. package/dist/assets/pdf-DfwfQPLy.js.gz +0 -0
  187. package/dist/assets/pdf.worker.min-B5Yc7dP4.mjs +27 -0
  188. package/dist/assets/pdf.worker.min-B5Yc7dP4.mjs.br +0 -0
  189. package/dist/assets/pdf.worker.min-B5Yc7dP4.mjs.gz +0 -0
  190. package/dist/assets/pdf.worker.min-BOrSNwfp.js +1 -0
  191. package/dist/index.html +2 -2
  192. package/dist/index.html.br +0 -0
  193. package/dist/index.html.gz +0 -0
  194. package/dist/vendor/pdfjs/cmaps/78-EUC-H.bcmap +0 -0
  195. package/dist/vendor/pdfjs/cmaps/78-EUC-V.bcmap +0 -0
  196. package/dist/vendor/pdfjs/cmaps/78-H.bcmap +0 -0
  197. package/dist/vendor/pdfjs/cmaps/78-RKSJ-H.bcmap +0 -0
  198. package/dist/vendor/pdfjs/cmaps/78-RKSJ-V.bcmap +0 -0
  199. package/dist/vendor/pdfjs/cmaps/78-V.bcmap +0 -0
  200. package/dist/vendor/pdfjs/cmaps/78ms-RKSJ-H.bcmap +0 -0
  201. package/dist/vendor/pdfjs/cmaps/78ms-RKSJ-V.bcmap +0 -0
  202. package/dist/vendor/pdfjs/cmaps/83pv-RKSJ-H.bcmap +0 -0
  203. package/dist/vendor/pdfjs/cmaps/90ms-RKSJ-H.bcmap +0 -0
  204. package/dist/vendor/pdfjs/cmaps/90ms-RKSJ-V.bcmap +0 -0
  205. package/dist/vendor/pdfjs/cmaps/90msp-RKSJ-H.bcmap +0 -0
  206. package/dist/vendor/pdfjs/cmaps/90msp-RKSJ-V.bcmap +0 -0
  207. package/dist/vendor/pdfjs/cmaps/90pv-RKSJ-H.bcmap +0 -0
  208. package/dist/vendor/pdfjs/cmaps/90pv-RKSJ-V.bcmap +0 -0
  209. package/dist/vendor/pdfjs/cmaps/Add-H.bcmap +0 -0
  210. package/dist/vendor/pdfjs/cmaps/Add-RKSJ-H.bcmap +0 -0
  211. package/dist/vendor/pdfjs/cmaps/Add-RKSJ-V.bcmap +0 -0
  212. package/dist/vendor/pdfjs/cmaps/Add-V.bcmap +0 -0
  213. package/dist/vendor/pdfjs/cmaps/Adobe-CNS1-0.bcmap +0 -0
  214. package/dist/vendor/pdfjs/cmaps/Adobe-CNS1-1.bcmap +0 -0
  215. package/dist/vendor/pdfjs/cmaps/Adobe-CNS1-2.bcmap +0 -0
  216. package/dist/vendor/pdfjs/cmaps/Adobe-CNS1-3.bcmap +0 -0
  217. package/dist/vendor/pdfjs/cmaps/Adobe-CNS1-4.bcmap +0 -0
  218. package/dist/vendor/pdfjs/cmaps/Adobe-CNS1-5.bcmap +0 -0
  219. package/dist/vendor/pdfjs/cmaps/Adobe-CNS1-6.bcmap +0 -0
  220. package/dist/vendor/pdfjs/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
  221. package/dist/vendor/pdfjs/cmaps/Adobe-GB1-0.bcmap +0 -0
  222. package/dist/vendor/pdfjs/cmaps/Adobe-GB1-1.bcmap +0 -0
  223. package/dist/vendor/pdfjs/cmaps/Adobe-GB1-2.bcmap +0 -0
  224. package/dist/vendor/pdfjs/cmaps/Adobe-GB1-3.bcmap +0 -0
  225. package/dist/vendor/pdfjs/cmaps/Adobe-GB1-4.bcmap +0 -0
  226. package/dist/vendor/pdfjs/cmaps/Adobe-GB1-5.bcmap +0 -0
  227. package/dist/vendor/pdfjs/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
  228. package/dist/vendor/pdfjs/cmaps/Adobe-Japan1-0.bcmap +0 -0
  229. package/dist/vendor/pdfjs/cmaps/Adobe-Japan1-1.bcmap +0 -0
  230. package/dist/vendor/pdfjs/cmaps/Adobe-Japan1-2.bcmap +0 -0
  231. package/dist/vendor/pdfjs/cmaps/Adobe-Japan1-3.bcmap +0 -0
  232. package/dist/vendor/pdfjs/cmaps/Adobe-Japan1-4.bcmap +0 -0
  233. package/dist/vendor/pdfjs/cmaps/Adobe-Japan1-5.bcmap +0 -0
  234. package/dist/vendor/pdfjs/cmaps/Adobe-Japan1-6.bcmap +0 -0
  235. package/dist/vendor/pdfjs/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
  236. package/dist/vendor/pdfjs/cmaps/Adobe-Korea1-0.bcmap +0 -0
  237. package/dist/vendor/pdfjs/cmaps/Adobe-Korea1-1.bcmap +0 -0
  238. package/dist/vendor/pdfjs/cmaps/Adobe-Korea1-2.bcmap +0 -0
  239. package/dist/vendor/pdfjs/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
  240. package/dist/vendor/pdfjs/cmaps/B5-H.bcmap +0 -0
  241. package/dist/vendor/pdfjs/cmaps/B5-V.bcmap +0 -0
  242. package/dist/vendor/pdfjs/cmaps/B5pc-H.bcmap +0 -0
  243. package/dist/vendor/pdfjs/cmaps/B5pc-V.bcmap +0 -0
  244. package/dist/vendor/pdfjs/cmaps/CNS-EUC-H.bcmap +0 -0
  245. package/dist/vendor/pdfjs/cmaps/CNS-EUC-V.bcmap +0 -0
  246. package/dist/vendor/pdfjs/cmaps/CNS1-H.bcmap +0 -0
  247. package/dist/vendor/pdfjs/cmaps/CNS1-V.bcmap +0 -0
  248. package/dist/vendor/pdfjs/cmaps/CNS2-H.bcmap +0 -0
  249. package/dist/vendor/pdfjs/cmaps/CNS2-V.bcmap +3 -0
  250. package/dist/vendor/pdfjs/cmaps/ETHK-B5-H.bcmap +0 -0
  251. package/dist/vendor/pdfjs/cmaps/ETHK-B5-V.bcmap +0 -0
  252. package/dist/vendor/pdfjs/cmaps/ETen-B5-H.bcmap +0 -0
  253. package/dist/vendor/pdfjs/cmaps/ETen-B5-V.bcmap +0 -0
  254. package/dist/vendor/pdfjs/cmaps/ETenms-B5-H.bcmap +3 -0
  255. package/dist/vendor/pdfjs/cmaps/ETenms-B5-V.bcmap +0 -0
  256. package/dist/vendor/pdfjs/cmaps/EUC-H.bcmap +0 -0
  257. package/dist/vendor/pdfjs/cmaps/EUC-V.bcmap +0 -0
  258. package/dist/vendor/pdfjs/cmaps/Ext-H.bcmap +0 -0
  259. package/dist/vendor/pdfjs/cmaps/Ext-RKSJ-H.bcmap +0 -0
  260. package/dist/vendor/pdfjs/cmaps/Ext-RKSJ-V.bcmap +0 -0
  261. package/dist/vendor/pdfjs/cmaps/Ext-V.bcmap +0 -0
  262. package/dist/vendor/pdfjs/cmaps/GB-EUC-H.bcmap +0 -0
  263. package/dist/vendor/pdfjs/cmaps/GB-EUC-V.bcmap +0 -0
  264. package/dist/vendor/pdfjs/cmaps/GB-H.bcmap +6 -0
  265. package/dist/vendor/pdfjs/cmaps/GB-V.bcmap +0 -0
  266. package/dist/vendor/pdfjs/cmaps/GBK-EUC-H.bcmap +0 -0
  267. package/dist/vendor/pdfjs/cmaps/GBK-EUC-V.bcmap +0 -0
  268. package/dist/vendor/pdfjs/cmaps/GBK2K-H.bcmap +0 -0
  269. package/dist/vendor/pdfjs/cmaps/GBK2K-V.bcmap +0 -0
  270. package/dist/vendor/pdfjs/cmaps/GBKp-EUC-H.bcmap +0 -0
  271. package/dist/vendor/pdfjs/cmaps/GBKp-EUC-V.bcmap +0 -0
  272. package/dist/vendor/pdfjs/cmaps/GBT-EUC-H.bcmap +0 -0
  273. package/dist/vendor/pdfjs/cmaps/GBT-EUC-V.bcmap +0 -0
  274. package/dist/vendor/pdfjs/cmaps/GBT-H.bcmap +0 -0
  275. package/dist/vendor/pdfjs/cmaps/GBT-V.bcmap +0 -0
  276. package/dist/vendor/pdfjs/cmaps/GBTpc-EUC-H.bcmap +0 -0
  277. package/dist/vendor/pdfjs/cmaps/GBTpc-EUC-V.bcmap +0 -0
  278. package/dist/vendor/pdfjs/cmaps/GBpc-EUC-H.bcmap +0 -0
  279. package/dist/vendor/pdfjs/cmaps/GBpc-EUC-V.bcmap +0 -0
  280. package/dist/vendor/pdfjs/cmaps/H.bcmap +0 -0
  281. package/dist/vendor/pdfjs/cmaps/HKdla-B5-H.bcmap +0 -0
  282. package/dist/vendor/pdfjs/cmaps/HKdla-B5-V.bcmap +0 -0
  283. package/dist/vendor/pdfjs/cmaps/HKdlb-B5-H.bcmap +0 -0
  284. package/dist/vendor/pdfjs/cmaps/HKdlb-B5-V.bcmap +0 -0
  285. package/dist/vendor/pdfjs/cmaps/HKgccs-B5-H.bcmap +0 -0
  286. package/dist/vendor/pdfjs/cmaps/HKgccs-B5-V.bcmap +0 -0
  287. package/dist/vendor/pdfjs/cmaps/HKm314-B5-H.bcmap +0 -0
  288. package/dist/vendor/pdfjs/cmaps/HKm314-B5-V.bcmap +0 -0
  289. package/dist/vendor/pdfjs/cmaps/HKm471-B5-H.bcmap +0 -0
  290. package/dist/vendor/pdfjs/cmaps/HKm471-B5-V.bcmap +0 -0
  291. package/dist/vendor/pdfjs/cmaps/HKscs-B5-H.bcmap +0 -0
  292. package/dist/vendor/pdfjs/cmaps/HKscs-B5-V.bcmap +0 -0
  293. package/dist/vendor/pdfjs/cmaps/Hankaku.bcmap +0 -0
  294. package/dist/vendor/pdfjs/cmaps/Hiragana.bcmap +0 -0
  295. package/dist/vendor/pdfjs/cmaps/KSC-EUC-H.bcmap +0 -0
  296. package/dist/vendor/pdfjs/cmaps/KSC-EUC-V.bcmap +0 -0
  297. package/dist/vendor/pdfjs/cmaps/KSC-H.bcmap +0 -0
  298. package/dist/vendor/pdfjs/cmaps/KSC-Johab-H.bcmap +0 -0
  299. package/dist/vendor/pdfjs/cmaps/KSC-Johab-V.bcmap +0 -0
  300. package/dist/vendor/pdfjs/cmaps/KSC-V.bcmap +0 -0
  301. package/dist/vendor/pdfjs/cmaps/KSCms-UHC-H.bcmap +0 -0
  302. package/dist/vendor/pdfjs/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
  303. package/dist/vendor/pdfjs/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
  304. package/dist/vendor/pdfjs/cmaps/KSCms-UHC-V.bcmap +0 -0
  305. package/dist/vendor/pdfjs/cmaps/KSCpc-EUC-H.bcmap +0 -0
  306. package/dist/vendor/pdfjs/cmaps/KSCpc-EUC-V.bcmap +0 -0
  307. package/dist/vendor/pdfjs/cmaps/Katakana.bcmap +0 -0
  308. package/dist/vendor/pdfjs/cmaps/LICENSE +36 -0
  309. package/dist/vendor/pdfjs/cmaps/NWP-H.bcmap +0 -0
  310. package/dist/vendor/pdfjs/cmaps/NWP-V.bcmap +0 -0
  311. package/dist/vendor/pdfjs/cmaps/RKSJ-H.bcmap +0 -0
  312. package/dist/vendor/pdfjs/cmaps/RKSJ-V.bcmap +0 -0
  313. package/dist/vendor/pdfjs/cmaps/Roman.bcmap +0 -0
  314. package/dist/vendor/pdfjs/cmaps/UniCNS-UCS2-H.bcmap +0 -0
  315. package/dist/vendor/pdfjs/cmaps/UniCNS-UCS2-V.bcmap +0 -0
  316. package/dist/vendor/pdfjs/cmaps/UniCNS-UTF16-H.bcmap +0 -0
  317. package/dist/vendor/pdfjs/cmaps/UniCNS-UTF16-V.bcmap +0 -0
  318. package/dist/vendor/pdfjs/cmaps/UniCNS-UTF32-H.bcmap +0 -0
  319. package/dist/vendor/pdfjs/cmaps/UniCNS-UTF32-V.bcmap +0 -0
  320. package/dist/vendor/pdfjs/cmaps/UniCNS-UTF8-H.bcmap +0 -0
  321. package/dist/vendor/pdfjs/cmaps/UniCNS-UTF8-V.bcmap +0 -0
  322. package/dist/vendor/pdfjs/cmaps/UniGB-UCS2-H.bcmap +0 -0
  323. package/dist/vendor/pdfjs/cmaps/UniGB-UCS2-V.bcmap +0 -0
  324. package/dist/vendor/pdfjs/cmaps/UniGB-UTF16-H.bcmap +0 -0
  325. package/dist/vendor/pdfjs/cmaps/UniGB-UTF16-V.bcmap +0 -0
  326. package/dist/vendor/pdfjs/cmaps/UniGB-UTF32-H.bcmap +0 -0
  327. package/dist/vendor/pdfjs/cmaps/UniGB-UTF32-V.bcmap +0 -0
  328. package/dist/vendor/pdfjs/cmaps/UniGB-UTF8-H.bcmap +0 -0
  329. package/dist/vendor/pdfjs/cmaps/UniGB-UTF8-V.bcmap +0 -0
  330. package/dist/vendor/pdfjs/cmaps/UniJIS-UCS2-H.bcmap +0 -0
  331. package/dist/vendor/pdfjs/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
  332. package/dist/vendor/pdfjs/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
  333. package/dist/vendor/pdfjs/cmaps/UniJIS-UCS2-V.bcmap +0 -0
  334. package/dist/vendor/pdfjs/cmaps/UniJIS-UTF16-H.bcmap +0 -0
  335. package/dist/vendor/pdfjs/cmaps/UniJIS-UTF16-V.bcmap +0 -0
  336. package/dist/vendor/pdfjs/cmaps/UniJIS-UTF32-H.bcmap +0 -0
  337. package/dist/vendor/pdfjs/cmaps/UniJIS-UTF32-V.bcmap +0 -0
  338. package/dist/vendor/pdfjs/cmaps/UniJIS-UTF8-H.bcmap +0 -0
  339. package/dist/vendor/pdfjs/cmaps/UniJIS-UTF8-V.bcmap +0 -0
  340. package/dist/vendor/pdfjs/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
  341. package/dist/vendor/pdfjs/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
  342. package/dist/vendor/pdfjs/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
  343. package/dist/vendor/pdfjs/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
  344. package/dist/vendor/pdfjs/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
  345. package/dist/vendor/pdfjs/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
  346. package/dist/vendor/pdfjs/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
  347. package/dist/vendor/pdfjs/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
  348. package/dist/vendor/pdfjs/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
  349. package/dist/vendor/pdfjs/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
  350. package/dist/vendor/pdfjs/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
  351. package/dist/vendor/pdfjs/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
  352. package/dist/vendor/pdfjs/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
  353. package/dist/vendor/pdfjs/cmaps/UniKS-UCS2-H.bcmap +0 -0
  354. package/dist/vendor/pdfjs/cmaps/UniKS-UCS2-V.bcmap +0 -0
  355. package/dist/vendor/pdfjs/cmaps/UniKS-UTF16-H.bcmap +0 -0
  356. package/dist/vendor/pdfjs/cmaps/UniKS-UTF16-V.bcmap +0 -0
  357. package/dist/vendor/pdfjs/cmaps/UniKS-UTF32-H.bcmap +0 -0
  358. package/dist/vendor/pdfjs/cmaps/UniKS-UTF32-V.bcmap +0 -0
  359. package/dist/vendor/pdfjs/cmaps/UniKS-UTF8-H.bcmap +0 -0
  360. package/dist/vendor/pdfjs/cmaps/UniKS-UTF8-V.bcmap +0 -0
  361. package/dist/vendor/pdfjs/cmaps/V.bcmap +0 -0
  362. package/dist/vendor/pdfjs/cmaps/WP-Symbol.bcmap +0 -0
  363. package/dist/vendor/pdfjs/iccs/CGATS001Compat-v2-micro.icc +0 -0
  364. package/dist/vendor/pdfjs/iccs/LICENSE +116 -0
  365. package/dist/vendor/pdfjs/standard_fonts/FoxitDingbats.pfb +0 -0
  366. package/dist/vendor/pdfjs/standard_fonts/FoxitFixed.pfb +0 -0
  367. package/dist/vendor/pdfjs/standard_fonts/FoxitFixedBold.pfb +0 -0
  368. package/dist/vendor/pdfjs/standard_fonts/FoxitFixedBoldItalic.pfb +0 -0
  369. package/dist/vendor/pdfjs/standard_fonts/FoxitFixedItalic.pfb +0 -0
  370. package/dist/vendor/pdfjs/standard_fonts/FoxitSerif.pfb +0 -0
  371. package/dist/vendor/pdfjs/standard_fonts/FoxitSerifBold.pfb +0 -0
  372. package/dist/vendor/pdfjs/standard_fonts/FoxitSerifBoldItalic.pfb +0 -0
  373. package/dist/vendor/pdfjs/standard_fonts/FoxitSerifItalic.pfb +0 -0
  374. package/dist/vendor/pdfjs/standard_fonts/FoxitSymbol.pfb +0 -0
  375. package/dist/vendor/pdfjs/standard_fonts/LICENSE_FOXIT +27 -0
  376. package/dist/vendor/pdfjs/standard_fonts/LICENSE_LIBERATION +363 -0
  377. package/dist/vendor/pdfjs/standard_fonts/LiberationSans-Bold.ttf +0 -0
  378. package/dist/vendor/pdfjs/standard_fonts/LiberationSans-BoldItalic.ttf +0 -0
  379. package/dist/vendor/pdfjs/standard_fonts/LiberationSans-Italic.ttf +0 -0
  380. package/dist/vendor/pdfjs/standard_fonts/LiberationSans-Regular.ttf +0 -0
  381. package/dist/vendor/pdfjs/wasm/LICENSE_JBIG2 +196 -0
  382. package/dist/vendor/pdfjs/wasm/LICENSE_OPENJPEG +39 -0
  383. package/dist/vendor/pdfjs/wasm/LICENSE_PDFJS_JBIG2 +13 -0
  384. package/dist/vendor/pdfjs/wasm/LICENSE_PDFJS_OPENJPEG +22 -0
  385. package/dist/vendor/pdfjs/wasm/LICENSE_PDFJS_QCMS +19 -0
  386. package/dist/vendor/pdfjs/wasm/LICENSE_PDFJS_QUICKJS +21 -0
  387. package/dist/vendor/pdfjs/wasm/LICENSE_QCMS +21 -0
  388. package/dist/vendor/pdfjs/wasm/LICENSE_QUICKJS +22 -0
  389. package/dist/vendor/pdfjs/wasm/jbig2.wasm +0 -0
  390. package/dist/vendor/pdfjs/wasm/jbig2.wasm.br +0 -0
  391. package/dist/vendor/pdfjs/wasm/jbig2.wasm.gz +0 -0
  392. package/dist/vendor/pdfjs/wasm/jbig2_nowasm_fallback.js +2 -0
  393. package/dist/vendor/pdfjs/wasm/jbig2_nowasm_fallback.js.br +0 -0
  394. package/dist/vendor/pdfjs/wasm/jbig2_nowasm_fallback.js.gz +0 -0
  395. package/dist/vendor/pdfjs/wasm/openjpeg.wasm +0 -0
  396. package/dist/vendor/pdfjs/wasm/openjpeg.wasm.br +0 -0
  397. package/dist/vendor/pdfjs/wasm/openjpeg.wasm.gz +0 -0
  398. package/dist/vendor/pdfjs/wasm/openjpeg_nowasm_fallback.js +2 -0
  399. package/dist/vendor/pdfjs/wasm/openjpeg_nowasm_fallback.js.br +0 -0
  400. package/dist/vendor/pdfjs/wasm/openjpeg_nowasm_fallback.js.gz +0 -0
  401. package/dist/vendor/pdfjs/wasm/qcms_bg.wasm +0 -0
  402. package/dist/vendor/pdfjs/wasm/qcms_bg.wasm.br +0 -0
  403. package/dist/vendor/pdfjs/wasm/qcms_bg.wasm.gz +0 -0
  404. package/dist/vendor/pdfjs/wasm/quickjs-eval.js +16 -0
  405. package/dist/vendor/pdfjs/wasm/quickjs-eval.js.br +0 -0
  406. package/dist/vendor/pdfjs/wasm/quickjs-eval.js.gz +0 -0
  407. package/dist/vendor/pdfjs/wasm/quickjs-eval.wasm +0 -0
  408. package/dist/vendor/pdfjs/wasm/quickjs-eval.wasm.br +0 -0
  409. package/dist/vendor/pdfjs/wasm/quickjs-eval.wasm.gz +0 -0
  410. package/package.json +6 -7
  411. package/scripts/start.ts +21 -4
  412. package/shared/constants/binary-extensions.ts +20 -4
  413. package/shared/constants/support.test.ts +27 -0
  414. package/shared/constants/support.ts +79 -0
  415. package/shared/constants/tool-icons.ts +7 -1
  416. package/shared/types/database/schema.ts +6 -0
  417. package/shared/types/git-hosting.ts +241 -0
  418. package/shared/types/integrations.ts +51 -1
  419. package/shared/types/mentions.ts +96 -0
  420. package/shared/types/release-notes.ts +31 -0
  421. package/shared/types/storage.ts +175 -0
  422. package/shared/types/stores/settings.ts +16 -0
  423. package/shared/types/unified/index.ts +1 -0
  424. package/shared/types/unified/message.ts +26 -1
  425. package/shared/types/unified/tool.ts +4 -0
  426. package/shared/types/work.ts +6 -0
  427. package/shared/utils/attachment-mentions.test.ts +39 -0
  428. package/shared/utils/attachment-mentions.ts +100 -0
  429. package/shared/utils/context-document.test.ts +53 -0
  430. package/shared/utils/context-document.ts +226 -0
  431. package/shared/utils/device-frame.test.ts +73 -0
  432. package/shared/utils/device-frame.ts +320 -0
  433. package/shared/utils/fuzzy.ts +79 -0
  434. package/shared/utils/launcher-fingerprint.test.ts +73 -0
  435. package/shared/utils/launcher-fingerprint.ts +108 -0
  436. package/shared/utils/logger.ts +2 -0
  437. package/shared/utils/page-colors.test.ts +17 -0
  438. package/shared/utils/page-colors.ts +24 -0
  439. package/shared/utils/process-supervisor.test.ts +18 -1
  440. package/shared/utils/process-supervisor.ts +12 -23
  441. package/shared/utils/restart-intent.test.ts +57 -0
  442. package/shared/utils/restart-intent.ts +80 -0
  443. package/shared/utils/semver.test.ts +33 -0
  444. package/shared/utils/semver.ts +35 -0
  445. package/shared/utils/supervised-child.ts +44 -0
  446. package/shared/utils/supervisor-protocol.ts +20 -0
  447. package/.env.example +0 -9
  448. package/dist/assets/index-By6KdSLT.js.br +0 -0
  449. package/dist/assets/index-By6KdSLT.js.gz +0 -0
  450. package/dist/assets/index-DV4TiDn1.js.br +0 -0
  451. package/dist/assets/index-DV4TiDn1.js.gz +0 -0
  452. package/dist/assets/main-DIESTGTD.css +0 -1
  453. package/dist/assets/main-DIESTGTD.css.br +0 -0
  454. package/dist/assets/main-DIESTGTD.css.gz +0 -0
  455. package/dist/assets/main-Da3JKoua.js +0 -1240
  456. package/dist/assets/main-Da3JKoua.js.br +0 -0
  457. package/dist/assets/main-Da3JKoua.js.gz +0 -0
@@ -0,0 +1,53 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import {
3
+ buildContextDocument,
4
+ contextFileSlug,
5
+ countLines,
6
+ readContextHeader,
7
+ readContextHeaderFromBase64,
8
+ textToBase64
9
+ } from './context-document';
10
+
11
+ describe('context documents', () => {
12
+ it('carry what they are in their first line', () => {
13
+ const document = buildContextDocument({
14
+ kind: 'git-commit',
15
+ label: 'fix: stop the leak\n',
16
+ detail: 'abc123',
17
+ capturedAt: '2026-11-27T00:00:00.000Z',
18
+ facts: [['Commit', 'abc123'], ['Empty', null]],
19
+ sections: [{ title: 'Commit', body: 'diff --git a b', fence: 'diff' }]
20
+ });
21
+ expect(document.fileName).toBe('git-commit-fix-stop-the-leak.md');
22
+ expect(document.source).toEqual({ kind: 'git-commit', label: 'fix: stop the leak', detail: 'abc123', capturedAt: '2026-11-27T00:00:00.000Z' });
23
+ expect(readContextHeader(document.text)).toEqual(document.source);
24
+ expect(document.text).toContain('# Git commit: fix: stop the leak');
25
+ expect(document.text).toContain('- Commit: abc123');
26
+ expect(document.text).not.toContain('Empty');
27
+ expect(document.text).toContain('```diff\ndiff --git a b\n```');
28
+ });
29
+
30
+ it('survive a label that would end the comment and a body that holds a fence', () => {
31
+ const document = buildContextDocument({ kind: 'note', label: 'a --> b', sections: [{ body: '```js\nx\n```', fence: 'md' }] });
32
+ expect(document.text.split('\n')[0].endsWith(' -->')).toBe(true);
33
+ expect(document.text.split('\n')[0].indexOf('-->')).toBe(document.text.split('\n')[0].length - 3);
34
+ expect(readContextHeader(document.text)?.label).toBe('a --> b');
35
+ expect(document.text).toContain('````md\n```js\nx\n```\n````');
36
+ });
37
+
38
+ it('are told apart from any other file by their bytes', () => {
39
+ const document = buildContextDocument({ kind: 'terminal', label: 'Ünïcode tab', sections: [{ body: 'x'.repeat(20_000) }] });
40
+ expect(readContextHeaderFromBase64(textToBase64(document.text), 'text/markdown')?.label).toBe('Ünïcode tab');
41
+ expect(readContextHeaderFromBase64(textToBase64('# just notes'), 'text/markdown')).toBeNull();
42
+ expect(readContextHeaderFromBase64(textToBase64(document.text), 'image/png')).toBeNull();
43
+ expect(readContextHeader('<!-- clopen-context {"kind":"nope","label":"x"} -->')).toBeNull();
44
+ });
45
+
46
+ it('name files and lines the way people read them', () => {
47
+ expect(contextFileSlug(' ')).toBe('context');
48
+ expect(contextFileSlug('src/App.svelte:10-20')).toBe('src-app-svelte-10-20');
49
+ expect(countLines('')).toBe('0 lines');
50
+ expect(countLines('a\n')).toBe('1 line');
51
+ expect(countLines('a\nb\nc')).toBe('3 lines');
52
+ });
53
+ });
@@ -0,0 +1,226 @@
1
+ /**
2
+ * Context documents — the file a mention is turned into.
3
+ *
4
+ * A snapshot is plain markdown the model can read with its own tools, and its
5
+ * first line is a comment saying what it is:
6
+ *
7
+ * <!-- clopen-context {"kind":"terminal","label":"dev server",…} -->
8
+ *
9
+ * The header travels with the bytes, so every path an attachment already takes
10
+ * — the draft, the queue, an edit, a message returned to the box — carries it
11
+ * without knowing about it. The composer and the timeline read it to draw the
12
+ * card; the server reads it to label the attachment for the engine.
13
+ */
14
+
15
+ import type { ContextKind, ContextSource } from '$shared/types/mentions';
16
+
17
+ const HEADER_START = '<!-- clopen-context ';
18
+ const HEADER_END = ' -->';
19
+
20
+ /** The media type every context document is written in. */
21
+ export const CONTEXT_MEDIA_TYPE = 'text/markdown';
22
+
23
+ /** How a kind is named in a label: "Attachment @2: terminal output …". */
24
+ export const CONTEXT_NOUNS: Record<ContextKind, string> = {
25
+ file: 'project file',
26
+ folder: 'folder listing',
27
+ code: 'code selection',
28
+ terminal: 'terminal output',
29
+ 'terminal-selection': 'terminal selection',
30
+ 'git-changes': 'git working-tree changes',
31
+ 'git-staged': 'git staged changes',
32
+ 'git-file': 'git file diff',
33
+ 'git-commit': 'git commit',
34
+ 'git-branch': 'git branch diff',
35
+ 'git-stash': 'git stash',
36
+ 'git-conflicts': 'git merge conflicts',
37
+ 'preview-tab': 'preview tab',
38
+ 'preview-element': 'preview page element',
39
+ 'preview-console': 'preview console log',
40
+ 'chat-session': 'chat transcript',
41
+ 'chat-message': 'chat message',
42
+ project: 'project overview',
43
+ 'ai-changes': 'changes made by the AI',
44
+ 'work-item': 'issue or pull request',
45
+ note: 'note',
46
+ memory: 'memory',
47
+ 'db-connection': 'database schema',
48
+ 'db-table': 'database table',
49
+ 'db-result': 'database query result',
50
+ deployment: 'deployment',
51
+ container: 'container',
52
+ port: 'listening port',
53
+ 'env-file': 'environment file'
54
+ };
55
+
56
+ /** Kinds that point at a real file instead of carrying a snapshot of it. */
57
+ export function isReferenceKind(kind: ContextKind): boolean {
58
+ return kind === 'file';
59
+ }
60
+
61
+ export interface ContextSection {
62
+ /** A `##` heading; omitted for the lead section. */
63
+ title?: string;
64
+ body: string;
65
+ /** Fenced as code in this language; `''` fences without one. Omit for prose. */
66
+ fence?: string;
67
+ }
68
+
69
+ export interface ContextDocumentInput {
70
+ kind: ContextKind;
71
+ label: string;
72
+ detail?: string;
73
+ path?: string;
74
+ /** `- key: value` lines under the title. */
75
+ facts?: Array<[string, string | number | null | undefined]>;
76
+ sections?: ContextSection[];
77
+ /** Defaults to now. */
78
+ capturedAt?: string;
79
+ }
80
+
81
+ export interface ContextDocument {
82
+ fileName: string;
83
+ text: string;
84
+ source: ContextSource;
85
+ }
86
+
87
+ /** A fence long enough that nothing inside the body can close it. */
88
+ function fenceFor(body: string): string {
89
+ let longest = 0;
90
+ for (const match of body.matchAll(/`{3,}/g)) longest = Math.max(longest, match[0].length);
91
+ return '`'.repeat(Math.max(3, longest + 1));
92
+ }
93
+
94
+ /** `name` as a file name: lowercase words joined by dashes, never empty. */
95
+ export function contextFileSlug(name: string): string {
96
+ const slug = name
97
+ .normalize('NFKD')
98
+ .replace(/[^\w\s.-]+/g, ' ')
99
+ .trim()
100
+ .replace(/[\s.]+/g, '-')
101
+ .replace(/-+/g, '-')
102
+ .toLowerCase()
103
+ .slice(0, 60)
104
+ .replace(/^-|-$/g, '');
105
+ return slug || 'context';
106
+ }
107
+
108
+ /** Build the document a mention attaches. */
109
+ export function buildContextDocument(input: ContextDocumentInput): ContextDocument {
110
+ const source: ContextSource = {
111
+ kind: input.kind,
112
+ // The header has to fit in the bytes a reader probes (see below).
113
+ label: input.label.replace(/\s+/g, ' ').trim().slice(0, 200),
114
+ ...(input.detail && { detail: input.detail.slice(0, 200) }),
115
+ ...(input.path && { path: input.path }),
116
+ capturedAt: input.capturedAt ?? new Date().toISOString()
117
+ };
118
+
119
+ const lines: string[] = [
120
+ // `-->` inside the JSON would end the comment early.
121
+ `${HEADER_START}${JSON.stringify(source).replace(/-->/g, '--\\u003e')}${HEADER_END}`,
122
+ `# ${capitalise(CONTEXT_NOUNS[input.kind])}: ${source.label}`,
123
+ ''
124
+ ];
125
+
126
+ const facts = (input.facts ?? []).filter(([, value]) => value !== null && value !== undefined && value !== '');
127
+ if (input.detail) facts.unshift(['Detail', input.detail]);
128
+ facts.push(['Captured', source.capturedAt]);
129
+ for (const [key, value] of facts) lines.push(`- ${key}: ${value}`);
130
+
131
+ for (const section of input.sections ?? []) {
132
+ lines.push('');
133
+ if (section.title) lines.push(`## ${section.title}`, '');
134
+ if (section.fence === undefined) {
135
+ lines.push(section.body.trimEnd() || '_Empty._');
136
+ } else {
137
+ const fence = fenceFor(section.body);
138
+ lines.push(`${fence}${section.fence}`, section.body.replace(/\n+$/, ''), fence);
139
+ }
140
+ }
141
+
142
+ return {
143
+ fileName: `${input.kind}-${contextFileSlug(source.label)}.md`,
144
+ text: `${lines.join('\n')}\n`,
145
+ source
146
+ };
147
+ }
148
+
149
+ /**
150
+ * A mentioned project file. The document only names it: the engine is pointed
151
+ * at the file itself (see backend/chat/attachments), so it reads the version
152
+ * on disk when it runs, not a copy from when it was mentioned.
153
+ */
154
+ export function fileReferenceDocument(path: string): ContextDocument {
155
+ return buildContextDocument({ kind: 'file', label: path, path, facts: [['Path', path]] });
156
+ }
157
+
158
+ function capitalise(text: string): string {
159
+ return text.charAt(0).toUpperCase() + text.slice(1);
160
+ }
161
+
162
+ /** What a document says it is, or null for any other file. */
163
+ export function readContextHeader(text: string): ContextSource | null {
164
+ if (!text.startsWith(HEADER_START)) return null;
165
+ const end = text.indexOf(HEADER_END, HEADER_START.length);
166
+ if (end === -1) return null;
167
+ try {
168
+ const parsed = JSON.parse(text.slice(HEADER_START.length, end)) as Partial<ContextSource>;
169
+ if (typeof parsed.kind !== 'string' || !(parsed.kind in CONTEXT_NOUNS)) return null;
170
+ if (typeof parsed.label !== 'string') return null;
171
+ return {
172
+ kind: parsed.kind as ContextKind,
173
+ label: parsed.label,
174
+ ...(typeof parsed.detail === 'string' && parsed.detail && { detail: parsed.detail }),
175
+ ...(typeof parsed.path === 'string' && parsed.path && { path: parsed.path }),
176
+ capturedAt: typeof parsed.capturedAt === 'string' ? parsed.capturedAt : ''
177
+ };
178
+ } catch {
179
+ return null;
180
+ }
181
+ }
182
+
183
+ /** Enough of a document's start to hold its header. */
184
+ export const CONTEXT_HEADER_PROBE_BYTES = 4096;
185
+
186
+ /**
187
+ * The header of a document held as base64, decoding only its start. Works on
188
+ * any attachment: a file that is not a context document answers null.
189
+ */
190
+ export function readContextHeaderFromBase64(base64: string, mediaType: string): ContextSource | null {
191
+ if (mediaType !== CONTEXT_MEDIA_TYPE && mediaType !== 'text/plain') return null;
192
+ try {
193
+ // Four base64 characters are three bytes; cut on a whole group.
194
+ const head = base64.slice(0, Math.ceil((CONTEXT_HEADER_PROBE_BYTES * 4) / 3 / 4) * 4);
195
+ const binary = atob(head);
196
+ const bytes = new Uint8Array(binary.length);
197
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
198
+ return readContextHeader(new TextDecoder().decode(bytes));
199
+ } catch {
200
+ return null;
201
+ }
202
+ }
203
+
204
+ /** UTF-8 text as base64, the form attachments travel in. */
205
+ export function textToBase64(text: string): string {
206
+ const bytes = new TextEncoder().encode(text);
207
+ let binary = '';
208
+ for (let i = 0; i < bytes.length; i += 0x8000) binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
209
+ return btoa(binary);
210
+ }
211
+
212
+ /** What a context attachment is, as a card names it: "Terminal output". */
213
+ export function contextNoun(kind: ContextKind): string {
214
+ return capitalise(CONTEXT_NOUNS[kind]);
215
+ }
216
+
217
+ /** How the picker and the viewer name a context attachment: "Terminal output · dev server". */
218
+ export function describeContext(source: ContextSource): string {
219
+ return `${contextNoun(source.kind)} · ${source.label}`;
220
+ }
221
+
222
+ /** A line count as people write it: "1,240 lines". */
223
+ export function countLines(text: string): string {
224
+ const lines = text ? text.replace(/\n$/, '').split('\n').length : 0;
225
+ return `${lines.toLocaleString('en-US')} ${lines === 1 ? 'line' : 'lines'}`;
226
+ }
@@ -0,0 +1,73 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import { drawDeviceFrame, frameDrawInput, hasDeviceFrame } from './device-frame';
3
+
4
+ /** A 2D context that accepts every call, for running the drawing without a browser. */
5
+ function fakeContext(): unknown {
6
+ const gradient = { addColorStop() {} };
7
+ return new Proxy(
8
+ {},
9
+ {
10
+ get(_target, key) {
11
+ if (key === 'createLinearGradient' || key === 'createRadialGradient') return () => gradient;
12
+ if (key === 'measureText') return () => ({ width: 10 });
13
+ return () => {};
14
+ },
15
+ set: () => true
16
+ }
17
+ );
18
+ }
19
+
20
+ function withFakeDocument<T>(run: () => T): T {
21
+ const scope = globalThis as unknown as { document?: unknown };
22
+ const previous = scope.document;
23
+ scope.document = {
24
+ createElement: () => ({ width: 0, height: 0, getContext: () => fakeContext() })
25
+ };
26
+ try {
27
+ return run();
28
+ } finally {
29
+ scope.document = previous;
30
+ }
31
+ }
32
+
33
+ const phone = { deviceSize: 'mobile', viewport: { width: 402, height: 874 }, deviceModel: 'iphone-16' } as const;
34
+
35
+ describe('device frame', () => {
36
+ test('only devices with a mockup get a frame', () => {
37
+ expect(hasDeviceFrame(phone)).toBe(true);
38
+ expect(hasDeviceFrame({ deviceSize: 'responsive', viewport: { width: 900, height: 700 }, deviceModel: null })).toBe(false);
39
+ expect(frameDrawInput({ deviceSize: 'responsive', viewport: { width: 900, height: 700 }, deviceModel: null })).toBeNull();
40
+ });
41
+
42
+ test('ink follows the page colours it sits on', () => {
43
+ const input = frameDrawInput({ ...phone, pageColors: { statusBar: '#0f172a', background: '#ffffff' } });
44
+ expect(input?.statusInk).toBe('#fff');
45
+ expect(input?.backgroundInk).toBe('#111113');
46
+ });
47
+
48
+ test('the canvas is the whole mockup, at the image’s scale', () => {
49
+ const input = frameDrawInput(phone)!;
50
+ const canvas = withFakeDocument(() => drawDeviceFrame({ width: 804, height: 1748 } as never, input));
51
+ expect(canvas?.width).toBe(Math.round(input.layout.footprint.width * 2));
52
+ expect(canvas?.height).toBe(Math.round(input.layout.footprint.height * 2));
53
+ });
54
+
55
+ // The server ships the function into a browser page as source text. Rebuilt
56
+ // from that text alone, it must still run: anything it reached outside its
57
+ // own body would be a ReferenceError there.
58
+ test('runs from its source text alone, as the server sends it', () => {
59
+ const rebuilt = new Function(`return (${drawDeviceFrame.toString()})`)() as typeof drawDeviceFrame;
60
+ for (const spec of [
61
+ phone,
62
+ { deviceSize: 'mobile', viewport: { width: 412, height: 923 }, deviceModel: 'pixel-9' },
63
+ { deviceSize: 'mobile', viewport: { width: 375, height: 667 }, deviceModel: 'iphone-se' },
64
+ { deviceSize: 'tablet', viewport: { width: 834, height: 1194 }, deviceModel: 'ipad-pro-11' },
65
+ { deviceSize: 'laptop', viewport: { width: 1440, height: 900 }, deviceModel: 'macbook-air-13' },
66
+ { deviceSize: 'desktop', viewport: { width: 1920, height: 1080 }, deviceModel: 'monitor-fhd' }
67
+ ] as const) {
68
+ const input = frameDrawInput(spec)!;
69
+ const canvas = withFakeDocument(() => rebuilt(spec.viewport as never, input));
70
+ expect(canvas).not.toBeNull();
71
+ }
72
+ });
73
+ });
@@ -0,0 +1,320 @@
1
+ /**
2
+ * Device mockup, drawn into a canvas around a screenshot.
3
+ *
4
+ * Reads the same `FrameLayout` the live frame is built from, so a framed
5
+ * screenshot puts the page exactly where the preview shows it — beside the
6
+ * status bar and the home indicator, never under them. The finish is the
7
+ * dark one the light theme shows: it reads on any background the image ends
8
+ * up on, which a light device on a light document would not.
9
+ *
10
+ * One renderer for both sides. The viewer calls it directly; the server has
11
+ * no canvas, so it runs the very same function inside a page of the preview
12
+ * browser — which is why `drawDeviceFrame` must stay self-contained: it is
13
+ * shipped as source text, and anything it reached outside its own body would
14
+ * not be there.
15
+ */
16
+
17
+ import {
18
+ getFrameLayout,
19
+ type DeviceSize,
20
+ type FrameLayout,
21
+ type PageColors,
22
+ type ViewportSize
23
+ } from '$shared/constants/preview';
24
+ import { foregroundFor } from './page-colors';
25
+
26
+ export interface FrameSpec {
27
+ deviceSize: DeviceSize;
28
+ viewport: ViewportSize;
29
+ deviceModel: string | null;
30
+ pageColors?: PageColors | null;
31
+ }
32
+
33
+ /** Everything the drawing needs, already resolved — plain data, so it can cross into a page. */
34
+ export interface FrameDrawInput {
35
+ layout: FrameLayout;
36
+ viewportWidth: number;
37
+ statusBar: string;
38
+ statusInk: string;
39
+ background: string;
40
+ backgroundInk: string;
41
+ }
42
+
43
+ /** Whether a screenshot of this device would get a mockup at all. */
44
+ export function hasDeviceFrame(spec: Pick<FrameSpec, 'deviceSize' | 'viewport' | 'deviceModel'>): boolean {
45
+ return getFrameLayout(spec.deviceSize, spec.viewport, true, spec.deviceModel).kind !== 'none';
46
+ }
47
+
48
+ /** The drawing input for a device, or null when it has no mockup. */
49
+ export function frameDrawInput(spec: FrameSpec): FrameDrawInput | null {
50
+ const layout = getFrameLayout(spec.deviceSize, spec.viewport, true, spec.deviceModel);
51
+ if (layout.kind === 'none') return null;
52
+ const statusBar = spec.pageColors?.statusBar ?? '#ffffff';
53
+ const background = spec.pageColors?.background ?? '#ffffff';
54
+ return {
55
+ layout,
56
+ viewportWidth: spec.viewport.width,
57
+ statusBar,
58
+ statusInk: foregroundFor(statusBar),
59
+ background,
60
+ backgroundInk: foregroundFor(background)
61
+ };
62
+ }
63
+
64
+ /**
65
+ * The image inside its device, on a transparent background.
66
+ *
67
+ * SELF-CONTAINED: no reference to anything outside this function body.
68
+ */
69
+ export function drawDeviceFrame(
70
+ image: CanvasImageSource & { width: number; height: number },
71
+ input: FrameDrawInput
72
+ ): HTMLCanvasElement | null {
73
+ type Rect = { x: number; y: number; width: number; height: number };
74
+ type Corners = [number, number, number, number];
75
+ type Finish = { hi: string; mid: string; lo: string };
76
+
77
+ const FINISH: Record<string, Finish> = {
78
+ default: { hi: '#6b6b70', mid: '#48484d', lo: '#2a2a2e' },
79
+ aluminum: { hi: '#6b6b70', mid: '#48484d', lo: '#2a2a2e' },
80
+ titanium: { hi: '#5e5d5b', mid: '#3e3d3b', lo: '#222221' },
81
+ 'space-black': { hi: '#4a4a4f', mid: '#2e2e32', lo: '#18181b' },
82
+ plastic: { hi: '#2f2f33', mid: '#232326', lo: '#161618' }
83
+ };
84
+ const SYSTEM_FONT = "-apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, sans-serif";
85
+ const ANDROID_FONT = "Roboto, 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif";
86
+
87
+ const { layout } = input;
88
+ const scale = image.width / input.viewportWidth;
89
+ const canvas = document.createElement('canvas');
90
+ canvas.width = Math.round(layout.footprint.width * scale);
91
+ canvas.height = Math.round(layout.footprint.height * scale);
92
+ const ctx = canvas.getContext('2d');
93
+ if (!ctx) return null;
94
+ ctx.scale(scale, scale);
95
+
96
+ const roundedRect = (rect: Rect, radius: Corners | number = 0) => {
97
+ ctx.beginPath();
98
+ ctx.roundRect(rect.x, rect.y, rect.width, rect.height, Array.isArray(radius) ? radius : [radius]);
99
+ };
100
+ const fill = (rect: Rect, style: string | CanvasGradient, radius: Corners | number = 0) => {
101
+ roundedRect(rect, radius);
102
+ ctx.fillStyle = style;
103
+ ctx.fill();
104
+ };
105
+ const metal = (rect: Rect, finish: Finish) => {
106
+ const gradient = ctx.createLinearGradient(rect.x, rect.y, rect.x + rect.width * 0.45, rect.y + rect.height);
107
+ gradient.addColorStop(0, finish.hi);
108
+ gradient.addColorStop(0.4, finish.mid);
109
+ gradient.addColorStop(1, finish.lo);
110
+ return gradient;
111
+ };
112
+ const lens = (cx: number, cy: number, size: number) => {
113
+ const gradient = ctx.createRadialGradient(cx - size * 0.15, cy - size * 0.15, 0, cx, cy, size / 2);
114
+ gradient.addColorStop(0, '#2b3a4d');
115
+ gradient.addColorStop(0.6, '#0b0f15');
116
+ gradient.addColorStop(1, '#000');
117
+ ctx.beginPath();
118
+ ctx.arc(cx, cy, size / 2, 0, Math.PI * 2);
119
+ ctx.fillStyle = gradient;
120
+ ctx.fill();
121
+ };
122
+
123
+ const now = new Date();
124
+ const shortTime = now
125
+ .toLocaleTimeString(undefined, { hour: 'numeric', minute: '2-digit' })
126
+ .replace(/\s?(AM|PM|am|pm)/, '')
127
+ .trim();
128
+ const fullTime = now.toLocaleTimeString(undefined, { hour: 'numeric', minute: '2-digit' });
129
+
130
+ /** Signal bars, wifi and battery, right-aligned to `right`, centred on `cy`. */
131
+ const statusGlyphs = (right: number, cy: number, unit: number, colour: string) => {
132
+ ctx.fillStyle = colour;
133
+ ctx.strokeStyle = colour;
134
+
135
+ const batteryW = unit * 2.1;
136
+ const batteryH = unit;
137
+ const batteryX = right - batteryW;
138
+ ctx.lineWidth = Math.max(1, unit * 0.09);
139
+ roundedRect({ x: batteryX, y: cy - batteryH / 2, width: batteryW - unit * 0.18, height: batteryH }, unit * 0.25);
140
+ ctx.stroke();
141
+ fill({ x: batteryX + unit * 0.16, y: cy - batteryH / 2 + unit * 0.16, width: batteryW - unit * 0.5, height: batteryH - unit * 0.32 }, colour, unit * 0.12);
142
+ fill({ x: right - unit * 0.12, y: cy - unit * 0.2, width: unit * 0.12, height: unit * 0.4 }, colour, unit * 0.05);
143
+
144
+ const wifiCx = batteryX - unit * 1.1;
145
+ ctx.lineWidth = unit * 0.2;
146
+ ctx.lineCap = 'round';
147
+ for (const r of [0.35, 0.7, 1.05]) {
148
+ ctx.beginPath();
149
+ ctx.arc(wifiCx, cy + unit * 0.55, unit * r, Math.PI * 1.25, Math.PI * 1.75);
150
+ ctx.stroke();
151
+ }
152
+
153
+ const barsRight = wifiCx - unit * 1.1;
154
+ for (let index = 0; index < 4; index++) {
155
+ const height = unit * (0.35 + index * 0.22);
156
+ fill({ x: barsRight - (3 - index) * unit * 0.32 - unit * 0.22, y: cy + unit * 0.5 - height, width: unit * 0.22, height }, colour, unit * 0.05);
157
+ }
158
+ };
159
+
160
+ const drawStatusBar = (bar: Rect) => {
161
+ ctx.textBaseline = 'middle';
162
+ if (layout.os === 'macos') {
163
+ fill(bar, 'rgba(245, 245, 247, 0.96)');
164
+ const size = layout.kind === 'laptop' ? 13 : 12;
165
+ const cy = bar.y + bar.height / 2;
166
+ ctx.fillStyle = '#111113';
167
+ // The apple, as a plain dot of ink — a logo is not worth a path here.
168
+ ctx.beginPath();
169
+ ctx.arc(bar.x + 22, cy, size * 0.42, 0, Math.PI * 2);
170
+ ctx.fill();
171
+ ctx.font = `700 ${size}px ${SYSTEM_FONT}`;
172
+ ctx.fillText('Safari', bar.x + 40, cy);
173
+ let x = bar.x + 40 + ctx.measureText('Safari').width + 18;
174
+ ctx.font = `500 ${size}px ${SYSTEM_FONT}`;
175
+ for (const item of ['File', 'Edit', 'View', 'History', 'Bookmarks', 'Window', 'Help']) {
176
+ ctx.fillText(item, x, cy);
177
+ x += ctx.measureText(item).width + 18;
178
+ }
179
+ const clock = `${now.toLocaleDateString(undefined, { weekday: 'short', day: 'numeric', month: 'short' })} ${fullTime}`;
180
+ ctx.textAlign = 'right';
181
+ ctx.fillText(clock, bar.x + bar.width - 16, cy);
182
+ ctx.textAlign = 'left';
183
+ return;
184
+ }
185
+
186
+ fill(bar, input.statusBar);
187
+ const android = layout.os === 'android';
188
+ const phone = layout.kind === 'phone';
189
+ const size = phone ? (android ? 14 : 16) : 12;
190
+ ctx.fillStyle = input.statusInk;
191
+ ctx.font = `${android ? 500 : 600} ${size}px ${android ? ANDROID_FONT : SYSTEM_FONT}`;
192
+
193
+ // Phones with an island centre their items on the island's band, not the bar.
194
+ const cy = layout.island ? layout.island.y + layout.island.height / 2 : bar.y + bar.height / 2;
195
+ const inset = phone ? (layout.island ? Math.max(24, (bar.width - layout.island.width) / 4 - 20) : 18) : 22;
196
+ ctx.textAlign = 'left';
197
+ ctx.fillText(shortTime, bar.x + inset, cy);
198
+ statusGlyphs(bar.x + bar.width - inset, cy, phone ? 11 : 9, input.statusInk);
199
+ };
200
+
201
+ const drawTaskbar = (bar: Rect) => {
202
+ fill(bar, 'rgba(238, 240, 244, 0.96)');
203
+ const cy = bar.y + bar.height / 2;
204
+ const logo = 18;
205
+ const cx = bar.x + bar.width / 2 - 60;
206
+ const half = logo / 2 - 0.5;
207
+ for (const [dx, dy] of [[0, 0], [half + 1, 0], [0, half + 1], [half + 1, half + 1]]) {
208
+ fill({ x: cx + dx, y: cy - logo / 2 + dy, width: half, height: half }, '#0a84ff');
209
+ }
210
+ ['#f2b600', '#1f6feb', '#7c3aed'].forEach((colour, index) => {
211
+ fill({ x: cx + 40 + index * 34, y: cy - 11, width: 22, height: 22 }, colour, 5);
212
+ });
213
+ ctx.fillStyle = '#111113';
214
+ ctx.font = `12px ${ANDROID_FONT}`;
215
+ ctx.textAlign = 'right';
216
+ ctx.textBaseline = 'middle';
217
+ ctx.fillText(fullTime, bar.x + bar.width - 14, cy - 7);
218
+ ctx.fillText(now.toLocaleDateString(), bar.x + bar.width - 14, cy + 8);
219
+ ctx.textAlign = 'left';
220
+ };
221
+
222
+ const drawNavBar = (bar: Rect & { direction: 'horizontal' | 'vertical' }) => {
223
+ fill(bar, input.background);
224
+ ctx.strokeStyle = input.backgroundInk;
225
+ ctx.lineWidth = 1.8;
226
+ ctx.lineJoin = 'round';
227
+
228
+ // Back, home, recents — left to right, or bottom to top when turned.
229
+ const vertical = bar.direction === 'vertical';
230
+ const length = vertical ? bar.height : bar.width;
231
+ const [back, home, recents] = [0.25, 0.5, 0.75].map((fraction) =>
232
+ vertical
233
+ ? { x: bar.x + bar.width / 2, y: bar.y + length * (1 - fraction) }
234
+ : { x: bar.x + length * fraction, y: bar.y + bar.height / 2 }
235
+ );
236
+
237
+ ctx.beginPath();
238
+ ctx.moveTo(back.x + 4, back.y - 5.5);
239
+ ctx.lineTo(back.x + 4, back.y + 5.5);
240
+ ctx.lineTo(back.x - 4.5, back.y);
241
+ ctx.closePath();
242
+ ctx.stroke();
243
+
244
+ ctx.beginPath();
245
+ ctx.arc(home.x, home.y, 6, 0, Math.PI * 2);
246
+ ctx.stroke();
247
+
248
+ roundedRect({ x: recents.x - 5.5, y: recents.y - 5.5, width: 11, height: 11 }, 1.5);
249
+ ctx.stroke();
250
+ };
251
+
252
+ const finish = FINISH[layout.finish ?? 'default'] ?? FINISH.default;
253
+
254
+ // Stand and deck sit behind the body.
255
+ if (layout.neck) fill(layout.neck, metal(layout.neck, finish));
256
+ if (layout.foot) fill(layout.foot, metal(layout.foot, finish), layout.foot.height / 2);
257
+ for (const foot of layout.feet) fill(foot, finish.lo, 2);
258
+ if (layout.base) fill(layout.base, metal(layout.base, finish), [2, 2, layout.base.height, layout.base.height]);
259
+ if (layout.scoop) fill(layout.scoop, finish.lo, [0, 0, layout.scoop.height, layout.scoop.height]);
260
+
261
+ for (const button of layout.buttons) {
262
+ const leading = button.edge === 'left' || button.edge === 'top';
263
+ fill(button, finish.mid, leading ? [3, 1, 1, 3] : [1, 3, 3, 1]);
264
+ }
265
+
266
+ fill(layout.body, metal(layout.body, finish), layout.bodyRadius);
267
+ const glass = ctx.createLinearGradient(0, layout.glass.y, 0, layout.glass.y + layout.glass.height);
268
+ glass.addColorStop(0, '#0d0d10');
269
+ glass.addColorStop(1, '#050506');
270
+ fill(layout.glass, glass, layout.glassRadius);
271
+
272
+ // Everything on the display is clipped to its rounded corners.
273
+ ctx.save();
274
+ roundedRect(layout.display, layout.displayRadius);
275
+ ctx.clip();
276
+ fill(layout.display, input.background);
277
+
278
+ if (layout.statusBar) drawStatusBar(layout.statusBar);
279
+
280
+ ctx.save();
281
+ roundedRect(layout.screen, layout.screenRadius);
282
+ ctx.clip();
283
+ ctx.drawImage(image, layout.screen.x, layout.screen.y, layout.screen.width, layout.screen.height);
284
+ ctx.restore();
285
+
286
+ if (layout.bottomBar && layout.os === 'windows') drawTaskbar(layout.bottomBar);
287
+ if (layout.homeIndicator) fill(layout.homeIndicator, input.backgroundInk, layout.homeIndicator.height / 2);
288
+ if (layout.navBar) drawNavBar(layout.navBar);
289
+ if (layout.island) fill(layout.island, '#000', layout.island.height / 2);
290
+ if (layout.notch) fill(layout.notch, '#000', [0, 0, layout.notch.height * 0.6, layout.notch.height * 0.6]);
291
+ if (layout.punchHole) {
292
+ const hole = layout.punchHole;
293
+ lens(hole.x + hole.width / 2, hole.y + hole.height / 2, hole.width);
294
+ }
295
+ if (layout.camera && (layout.island || layout.notch)) lens(layout.camera.x, layout.camera.y, layout.camera.size);
296
+ ctx.restore();
297
+
298
+ // Lenses, the earpiece and the home button live in the bezel.
299
+ if (layout.camera && !layout.island && !layout.notch) lens(layout.camera.x, layout.camera.y, layout.camera.size);
300
+ if (layout.earpiece) fill(layout.earpiece, '#1a1a1d', layout.earpiece.height / 2);
301
+ if (layout.homeButton) {
302
+ const button = layout.homeButton;
303
+ ctx.beginPath();
304
+ ctx.arc(button.x + button.width / 2, button.y + button.height / 2, button.width / 2, 0, Math.PI * 2);
305
+ ctx.strokeStyle = finish.hi;
306
+ ctx.lineWidth = 2;
307
+ ctx.stroke();
308
+ }
309
+
310
+ return canvas;
311
+ }
312
+
313
+ /** The image inside its device, or null when the device has no mockup. In a browser context. */
314
+ export function renderInDeviceFrame(
315
+ image: CanvasImageSource & { width: number; height: number },
316
+ spec: FrameSpec
317
+ ): HTMLCanvasElement | null {
318
+ const input = frameDrawInput(spec);
319
+ return input ? drawDeviceFrame(image, input) : null;
320
+ }