@sixb/app 0.2.4 → 0.2.6

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 (306) hide show
  1. package/assets/connector-icons/README.md +1 -0
  2. package/assets/connector-icons/ctm.png +0 -0
  3. package/dist/agent/activity-label.d.ts.map +1 -1
  4. package/dist/agent/agent-chat.d.ts +3 -1
  5. package/dist/agent/agent-chat.d.ts.map +1 -1
  6. package/dist/agent/agent-panel.d.ts.map +1 -1
  7. package/dist/agent/artifact-panel.d.ts +3 -1
  8. package/dist/agent/artifact-panel.d.ts.map +1 -1
  9. package/dist/agent/components/agent-threads.d.ts.map +1 -1
  10. package/dist/agent/components/composer-place.d.ts.map +1 -1
  11. package/dist/agent/components/composer-settings.d.ts +1 -1
  12. package/dist/agent/components/composer-settings.d.ts.map +1 -1
  13. package/dist/agent/components/composer.d.ts +22 -5
  14. package/dist/agent/components/composer.d.ts.map +1 -1
  15. package/dist/agent/components/computer-place.d.ts +24 -6
  16. package/dist/agent/components/computer-place.d.ts.map +1 -1
  17. package/dist/agent/components/context-meter.d.ts +1 -1
  18. package/dist/agent/components/context-meter.d.ts.map +1 -1
  19. package/dist/agent/components/conversation-panel.d.ts +5 -2
  20. package/dist/agent/components/conversation-panel.d.ts.map +1 -1
  21. package/dist/agent/components/message-parts.d.ts +2 -1
  22. package/dist/agent/components/message-parts.d.ts.map +1 -1
  23. package/dist/agent/components/message-view.d.ts +13 -3
  24. package/dist/agent/components/message-view.d.ts.map +1 -1
  25. package/dist/agent/components/pending-messages.d.ts +20 -0
  26. package/dist/agent/components/pending-messages.d.ts.map +1 -0
  27. package/dist/agent/components/sandbox-recovery.d.ts +11 -2
  28. package/dist/agent/components/sandbox-recovery.d.ts.map +1 -1
  29. package/dist/agent/components/threads-button.d.ts +1 -1
  30. package/dist/agent/components/threads-button.d.ts.map +1 -1
  31. package/dist/agent/components/transcript.d.ts +13 -0
  32. package/dist/agent/components/transcript.d.ts.map +1 -1
  33. package/dist/agent/conversation.d.ts +7 -6
  34. package/dist/agent/conversation.d.ts.map +1 -1
  35. package/dist/agent/delegation/interpret.d.ts +5 -0
  36. package/dist/agent/delegation/interpret.d.ts.map +1 -1
  37. package/dist/agent/delegation/use-children.d.ts.map +1 -1
  38. package/dist/agent/format.d.ts +7 -0
  39. package/dist/agent/format.d.ts.map +1 -1
  40. package/dist/agent/hooks/thread-queries.d.ts +1548 -0
  41. package/dist/agent/hooks/thread-queries.d.ts.map +1 -0
  42. package/dist/agent/hooks/use-agent-conversation.d.ts +51 -6
  43. package/dist/agent/hooks/use-agent-conversation.d.ts.map +1 -1
  44. package/dist/agent/hooks/use-thread-lists.d.ts +80 -3
  45. package/dist/agent/hooks/use-thread-lists.d.ts.map +1 -1
  46. package/dist/agent/hooks/use-thread-place.d.ts +49 -0
  47. package/dist/agent/hooks/use-thread-place.d.ts.map +1 -0
  48. package/dist/agent/hooks/use-thread-stream.d.ts.map +1 -1
  49. package/dist/agent/i18n/en.d.ts +107 -6
  50. package/dist/agent/i18n/en.d.ts.map +1 -1
  51. package/dist/agent/i18n/fr.d.ts.map +1 -1
  52. package/dist/agent/i18n/index.d.ts +180 -12
  53. package/dist/agent/i18n/index.d.ts.map +1 -1
  54. package/dist/agent/inbox-handoff.d.ts +10 -0
  55. package/dist/agent/inbox-handoff.d.ts.map +1 -0
  56. package/dist/agent/live-run.d.ts +8 -1
  57. package/dist/agent/live-run.d.ts.map +1 -1
  58. package/dist/agent/model-selection.d.ts +3 -2
  59. package/dist/agent/model-selection.d.ts.map +1 -1
  60. package/dist/agent/moves.d.ts +40 -0
  61. package/dist/agent/moves.d.ts.map +1 -0
  62. package/dist/agent/search/interpret.d.ts +101 -0
  63. package/dist/agent/search/interpret.d.ts.map +1 -0
  64. package/dist/agent/search/search-tool-view.d.ts +10 -0
  65. package/dist/agent/search/search-tool-view.d.ts.map +1 -0
  66. package/dist/agent/thread-reader-state.d.ts +30 -0
  67. package/dist/agent/thread-reader-state.d.ts.map +1 -0
  68. package/dist/agent/thread-state.d.ts +26 -0
  69. package/dist/agent/thread-state.d.ts.map +1 -0
  70. package/dist/agent/tool-views/built-in.d.ts.map +1 -1
  71. package/dist/agent/tool-views/registry.d.ts +5 -6
  72. package/dist/agent/tool-views/registry.d.ts.map +1 -1
  73. package/dist/agent/types.d.ts +9 -1
  74. package/dist/agent/types.d.ts.map +1 -1
  75. package/dist/agent/view-file/view-file-tool-view.d.ts +2 -1
  76. package/dist/agent/view-file/view-file-tool-view.d.ts.map +1 -1
  77. package/dist/artifacts/card-preview.d.ts.map +1 -1
  78. package/dist/artifacts/editor/block-handle.d.ts +20 -0
  79. package/dist/artifacts/editor/block-handle.d.ts.map +1 -0
  80. package/dist/artifacts/editor/blocks.d.ts +34 -0
  81. package/dist/artifacts/editor/blocks.d.ts.map +1 -0
  82. package/dist/artifacts/editor/callout-view.d.ts +7 -0
  83. package/dist/artifacts/editor/callout-view.d.ts.map +1 -0
  84. package/dist/artifacts/editor/document-editor.d.ts +30 -0
  85. package/dist/artifacts/editor/document-editor.d.ts.map +1 -0
  86. package/dist/artifacts/editor/figure-view.d.ts +12 -0
  87. package/dist/artifacts/editor/figure-view.d.ts.map +1 -0
  88. package/dist/artifacts/editor/find-blocks.d.ts +6 -0
  89. package/dist/artifacts/editor/find-blocks.d.ts.map +1 -0
  90. package/dist/artifacts/editor/markdown-document.d.ts +12 -0
  91. package/dist/artifacts/editor/markdown-document.d.ts.map +1 -0
  92. package/dist/artifacts/editor/messages.d.ts +4 -0
  93. package/dist/artifacts/editor/messages.d.ts.map +1 -0
  94. package/dist/artifacts/editor/schema.d.ts +21 -0
  95. package/dist/artifacts/editor/schema.d.ts.map +1 -0
  96. package/dist/artifacts/editor/selection-toolbar.d.ts +35 -0
  97. package/dist/artifacts/editor/selection-toolbar.d.ts.map +1 -0
  98. package/dist/artifacts/editor/slash-menu.d.ts +43 -0
  99. package/dist/artifacts/editor/slash-menu.d.ts.map +1 -0
  100. package/dist/artifacts/editor/upload.d.ts +12 -0
  101. package/dist/artifacts/editor/upload.d.ts.map +1 -0
  102. package/dist/artifacts/file-source.d.ts +5 -0
  103. package/dist/artifacts/file-source.d.ts.map +1 -1
  104. package/dist/artifacts/frame-editor/bundle.d.ts +7 -0
  105. package/dist/artifacts/frame-editor/bundle.d.ts.map +1 -0
  106. package/dist/artifacts/frame-editor/commands.d.ts +93 -0
  107. package/dist/artifacts/frame-editor/commands.d.ts.map +1 -0
  108. package/dist/artifacts/frame-editor/entry.d.ts +2 -0
  109. package/dist/artifacts/frame-editor/entry.d.ts.map +1 -0
  110. package/dist/artifacts/frame-editor/frame.d.ts +5 -0
  111. package/dist/artifacts/frame-editor/frame.d.ts.map +1 -0
  112. package/dist/artifacts/frame-editor/keys.d.ts +8 -0
  113. package/dist/artifacts/frame-editor/keys.d.ts.map +1 -0
  114. package/dist/artifacts/frame-editor/protocol.d.ts +186 -0
  115. package/dist/artifacts/frame-editor/protocol.d.ts.map +1 -0
  116. package/dist/artifacts/frame-editor/schema.d.ts +34 -0
  117. package/dist/artifacts/frame-editor/schema.d.ts.map +1 -0
  118. package/dist/artifacts/frame-editor/style.d.ts +2 -0
  119. package/dist/artifacts/frame-editor/style.d.ts.map +1 -0
  120. package/dist/artifacts/frame.d.ts +2 -2
  121. package/dist/artifacts/frame.d.ts.map +1 -1
  122. package/dist/artifacts/live/boards.d.ts +2 -0
  123. package/dist/artifacts/live/boards.d.ts.map +1 -1
  124. package/dist/artifacts/live/document.d.ts +32 -0
  125. package/dist/artifacts/live/document.d.ts.map +1 -0
  126. package/dist/artifacts/live/header.d.ts +4 -4
  127. package/dist/artifacts/live/header.d.ts.map +1 -1
  128. package/dist/artifacts/live/presence.d.ts.map +1 -1
  129. package/dist/artifacts/live/session.d.ts +2 -0
  130. package/dist/artifacts/live/session.d.ts.map +1 -1
  131. package/dist/artifacts/live/view.d.ts +4 -3
  132. package/dist/artifacts/live/view.d.ts.map +1 -1
  133. package/dist/artifacts/messages.d.ts +226 -2
  134. package/dist/artifacts/messages.d.ts.map +1 -1
  135. package/dist/artifacts/pdf.d.ts +8 -0
  136. package/dist/artifacts/pdf.d.ts.map +1 -0
  137. package/dist/artifacts/queries.d.ts +80 -0
  138. package/dist/artifacts/queries.d.ts.map +1 -1
  139. package/dist/artifacts/references/references.d.ts +3 -3
  140. package/dist/artifacts/references/references.d.ts.map +1 -1
  141. package/dist/artifacts/renderers/canvas/board-editor.d.ts.map +1 -1
  142. package/dist/artifacts/renderers/canvas/comment-pins.d.ts +4 -2
  143. package/dist/artifacts/renderers/canvas/comment-pins.d.ts.map +1 -1
  144. package/dist/artifacts/renderers/canvas/comments-panel.d.ts.map +1 -1
  145. package/dist/artifacts/renderers/canvas/comments.d.ts +2 -1
  146. package/dist/artifacts/renderers/canvas/comments.d.ts.map +1 -1
  147. package/dist/artifacts/renderers/designed-document.d.ts +28 -0
  148. package/dist/artifacts/renderers/designed-document.d.ts.map +1 -0
  149. package/dist/artifacts/renderers/document-tree.d.ts +2 -0
  150. package/dist/artifacts/renderers/document-tree.d.ts.map +1 -1
  151. package/dist/artifacts/renderers/document.d.ts +9 -1
  152. package/dist/artifacts/renderers/document.d.ts.map +1 -1
  153. package/dist/artifacts/renderers/live-frame.d.ts +145 -0
  154. package/dist/artifacts/renderers/live-frame.d.ts.map +1 -0
  155. package/dist/artifacts/renderers/page-editor.d.ts +16 -0
  156. package/dist/artifacts/renderers/page-editor.d.ts.map +1 -0
  157. package/dist/artifacts/renderers/page.d.ts +10 -1
  158. package/dist/artifacts/renderers/page.d.ts.map +1 -1
  159. package/dist/artifacts/viewer.d.ts.map +1 -1
  160. package/dist/{chunk-pxd07kvx.js → chunk-096d52h2.js} +1 -1
  161. package/dist/{chunk-yrcm1pbg.js → chunk-94d7enwc.js} +491 -2128
  162. package/dist/chunk-94d7enwc.js.map +27 -0
  163. package/dist/{chunk-970s5nkn.js → chunk-ezntd3yy.js} +1 -1
  164. package/dist/{chunk-kymx9y5e.js → chunk-qjw5r2vk.js} +12729 -5044
  165. package/dist/chunk-qjw5r2vk.js.map +242 -0
  166. package/dist/chunk-y51c4tsz.js +11 -0
  167. package/dist/chunk-y51c4tsz.js.map +10 -0
  168. package/dist/createCustomApp.d.ts +8 -1
  169. package/dist/createCustomApp.d.ts.map +1 -1
  170. package/dist/index.d.ts +4 -2
  171. package/dist/index.d.ts.map +1 -1
  172. package/dist/index.js +13 -6
  173. package/dist/index.js.map +5 -5
  174. package/dist/pages/console/data/accounts.d.ts +94 -0
  175. package/dist/pages/console/data/accounts.d.ts.map +1 -0
  176. package/dist/pages/console/data/connectors.d.ts.map +1 -1
  177. package/dist/pages/console/data/live-updates.d.ts.map +1 -1
  178. package/dist/pages/console/data/messages.d.ts +52 -0
  179. package/dist/pages/console/data/messages.d.ts.map +1 -1
  180. package/dist/pages/console/data/shared.d.ts.map +1 -1
  181. package/dist/pages/console/data/sync-button.d.ts.map +1 -1
  182. package/dist/pages/console/data/syncs.d.ts.map +1 -1
  183. package/dist/pages/settings/usage/breakdown.d.ts.map +1 -1
  184. package/dist/pages/settings/usage/page.d.ts +4 -1
  185. package/dist/pages/settings/usage/page.d.ts.map +1 -1
  186. package/dist/pages/settings/usage/spend-model.d.ts +43 -19
  187. package/dist/pages/settings/usage/spend-model.d.ts.map +1 -1
  188. package/dist/pages.js +23044 -22570
  189. package/dist/pages.js.map +17 -16
  190. package/dist/server.d.ts +1 -1
  191. package/dist/server.d.ts.map +1 -1
  192. package/dist/server.js +40 -3
  193. package/dist/server.js.map +3 -3
  194. package/dist/shell/search-command.d.ts.map +1 -1
  195. package/package.json +22 -5
  196. package/src/agent/activity-label.ts +3 -0
  197. package/src/agent/agent-chat.tsx +58 -39
  198. package/src/agent/agent-execution-trace.tsx +2 -2
  199. package/src/agent/agent-panel.tsx +6 -4
  200. package/src/agent/agent.css +254 -0
  201. package/src/agent/artifact-panel.tsx +77 -22
  202. package/src/agent/components/agent-threads.tsx +42 -2
  203. package/src/agent/components/composer-place.tsx +2 -7
  204. package/src/agent/components/composer-settings.tsx +20 -10
  205. package/src/agent/components/composer.tsx +342 -104
  206. package/src/agent/components/computer-place.tsx +415 -70
  207. package/src/agent/components/context-meter.tsx +7 -3
  208. package/src/agent/components/conversation-panel.tsx +99 -54
  209. package/src/agent/components/message-parts.tsx +367 -59
  210. package/src/agent/components/message-view.tsx +428 -9
  211. package/src/agent/components/pending-messages.tsx +342 -0
  212. package/src/agent/components/sandbox-recovery.tsx +36 -17
  213. package/src/agent/components/threads-button.tsx +3 -2
  214. package/src/agent/components/transcript.tsx +198 -38
  215. package/src/agent/conversation.ts +219 -55
  216. package/src/agent/delegation/interpret.ts +31 -7
  217. package/src/agent/format.ts +52 -0
  218. package/src/agent/hooks/thread-queries.ts +75 -0
  219. package/src/agent/hooks/use-agent-conversation.ts +801 -211
  220. package/src/agent/hooks/use-thread-lists.ts +72 -7
  221. package/src/agent/hooks/use-thread-place.ts +239 -0
  222. package/src/agent/hooks/use-thread-stream.ts +10 -0
  223. package/src/agent/i18n/en.ts +150 -27
  224. package/src/agent/i18n/fr.ts +148 -31
  225. package/src/agent/inbox-handoff.ts +47 -0
  226. package/src/agent/live-run.ts +61 -2
  227. package/src/agent/model-selection.ts +3 -2
  228. package/src/agent/moves.ts +174 -0
  229. package/src/agent/react-router.tsx +2 -2
  230. package/src/agent/search/interpret.ts +367 -0
  231. package/src/agent/search/search-tool-view.tsx +235 -0
  232. package/src/agent/thread-reader-state.ts +75 -0
  233. package/src/agent/thread-state.ts +114 -0
  234. package/src/agent/tool-views/built-in.tsx +16 -0
  235. package/src/agent/tool-views/registry.tsx +28 -2
  236. package/src/agent/types.ts +13 -0
  237. package/src/agent/view-file/view-file-tool-view.tsx +33 -19
  238. package/src/artifacts/artifacts.css +398 -0
  239. package/src/artifacts/card-preview.tsx +6 -1
  240. package/src/artifacts/editor/block-handle.tsx +278 -0
  241. package/src/artifacts/editor/blocks.ts +216 -0
  242. package/src/artifacts/editor/callout-view.tsx +31 -0
  243. package/src/artifacts/editor/document-editor.tsx +484 -0
  244. package/src/artifacts/editor/figure-view.tsx +66 -0
  245. package/src/artifacts/editor/find-blocks.ts +19 -0
  246. package/src/artifacts/editor/markdown-document.tsx +72 -0
  247. package/src/artifacts/editor/messages.ts +4 -0
  248. package/src/artifacts/editor/schema.ts +159 -0
  249. package/src/artifacts/editor/selection-toolbar.tsx +364 -0
  250. package/src/artifacts/editor/slash-menu.tsx +251 -0
  251. package/src/artifacts/editor/upload.ts +40 -0
  252. package/src/artifacts/file-source.ts +11 -0
  253. package/src/artifacts/frame-editor/bundle.ts +8 -0
  254. package/src/artifacts/frame-editor/commands.ts +565 -0
  255. package/src/artifacts/frame-editor/entry.ts +6 -0
  256. package/src/artifacts/frame-editor/frame.ts +847 -0
  257. package/src/artifacts/frame-editor/generated/editor.txt +149 -0
  258. package/src/artifacts/frame-editor/generated.d.ts +5 -0
  259. package/src/artifacts/frame-editor/keys.ts +323 -0
  260. package/src/artifacts/frame-editor/protocol.ts +151 -0
  261. package/src/artifacts/frame-editor/schema.ts +293 -0
  262. package/src/artifacts/frame-editor/style.ts +58 -0
  263. package/src/artifacts/frame.tsx +11 -3
  264. package/src/artifacts/live/boards.ts +46 -3
  265. package/src/artifacts/live/document.ts +89 -0
  266. package/src/artifacts/live/header.tsx +9 -14
  267. package/src/artifacts/live/presence.ts +5 -1
  268. package/src/artifacts/live/session.ts +9 -2
  269. package/src/artifacts/live/view.ts +14 -8
  270. package/src/artifacts/messages.ts +138 -2
  271. package/src/artifacts/pdf.ts +50 -0
  272. package/src/artifacts/references/references.tsx +46 -11
  273. package/src/artifacts/renderers/canvas/board-editor.tsx +11 -144
  274. package/src/artifacts/renderers/canvas/comment-pins.tsx +5 -0
  275. package/src/artifacts/renderers/canvas/comments-panel.tsx +58 -1
  276. package/src/artifacts/renderers/canvas/comments.ts +8 -4
  277. package/src/artifacts/renderers/designed-document.tsx +277 -0
  278. package/src/artifacts/renderers/document-tree.ts +9 -0
  279. package/src/artifacts/renderers/document.tsx +13 -102
  280. package/src/artifacts/renderers/live-frame.ts +163 -0
  281. package/src/artifacts/renderers/page-editor.tsx +613 -0
  282. package/src/artifacts/renderers/page.tsx +21 -4
  283. package/src/artifacts/viewer.tsx +87 -41
  284. package/src/createCustomApp.ts +57 -3
  285. package/src/index.ts +10 -0
  286. package/src/pages/console/data/accounts.tsx +509 -0
  287. package/src/pages/console/data/connectors.tsx +7 -15
  288. package/src/pages/console/data/live-updates.ts +2 -0
  289. package/src/pages/console/data/messages.ts +79 -0
  290. package/src/pages/console/data/projections.tsx +4 -0
  291. package/src/pages/console/data/shared.tsx +1 -0
  292. package/src/pages/console/data/sync-button.tsx +56 -16
  293. package/src/pages/console/data/syncs.tsx +66 -41
  294. package/src/pages/console/ontology/lineage/trail.tsx +3 -0
  295. package/src/pages/section-ui.tsx +1 -1
  296. package/src/pages/settings/usage/breakdown.tsx +5 -2
  297. package/src/pages/settings/usage/filter-menu.tsx +1 -1
  298. package/src/pages/settings/usage/page.tsx +32 -13
  299. package/src/pages/settings/usage/spend-chart.tsx +2 -2
  300. package/src/pages/settings/usage/spend-model.ts +118 -50
  301. package/src/server.ts +1 -0
  302. package/src/shell/search-command.tsx +2 -0
  303. package/dist/chunk-kymx9y5e.js.map +0 -211
  304. package/dist/chunk-yrcm1pbg.js.map +0 -34
  305. /package/dist/{chunk-pxd07kvx.js.map → chunk-096d52h2.js.map} +0 -0
  306. /package/dist/{chunk-970s5nkn.js.map → chunk-ezntd3yy.js.map} +0 -0
@@ -0,0 +1,484 @@
1
+ import { createHeadingSlugger, isArtifactAddress } from "@sixb/core/artifacts"
2
+ import { blocksToMarkdown, LIVE_FILES, markdownToBlocks } from "@sixb/core/artifacts/live"
3
+ import { toast } from "@sixb/ui/components"
4
+ import { cn } from "@sixb/ui/lib/utils"
5
+ import { type Editor, Extension } from "@tiptap/core"
6
+ import { Collaboration } from "@tiptap/extension-collaboration"
7
+ import { CollaborationCaret } from "@tiptap/extension-collaboration-caret"
8
+ import { Placeholder } from "@tiptap/extensions"
9
+ import { Fragment, type Node as ProseMirrorNode, Slice } from "@tiptap/pm/model"
10
+ import { Plugin, type Transaction } from "@tiptap/pm/state"
11
+ import { Decoration, DecorationSet } from "@tiptap/pm/view"
12
+ import { EditorContent, ReactNodeViewRenderer, useEditor } from "@tiptap/react"
13
+ import { useEffect, useMemo, useRef, useState } from "react"
14
+ import type { Awareness } from "y-protocols/awareness"
15
+ import type * as Y from "yjs"
16
+ import { BlockHandle } from "./block-handle"
17
+ import { blockTypes } from "./blocks"
18
+ import { CalloutView } from "./callout-view"
19
+ import { FigureContext, FigureView } from "./figure-view"
20
+ import type { EditorMessages } from "./messages"
21
+ import { CalloutNode, documentSchemaExtensions, Figure } from "./schema"
22
+ import { SelectionToolbar } from "./selection-toolbar"
23
+ import { SlashCommand, SlashMenu, SlashMenuStore } from "./slash-menu"
24
+
25
+ // A Markdown document edited as a page of notes: click anywhere and type. It edits the live
26
+ // document's blocks, so everyone editing it at once sees each other's words and carets, and the
27
+ // server writes them back as the document's Markdown. It reads as the document page does, with
28
+ // the same type and column.
29
+
30
+ export interface DocumentEditorProps {
31
+ /** `document.md` as the live document holds it. */
32
+ readonly fragment: Y.XmlFragment
33
+ readonly awareness: Awareness
34
+ /** The reader may change it. */
35
+ readonly editable: boolean
36
+ /** Where one of its image paths is served from. */
37
+ readonly imageSource: (src: string) => string | null
38
+ readonly messages: EditorMessages
39
+ /** The artifact's theme, as the document page applies it. */
40
+ readonly theme?: { readonly className?: string; readonly style?: React.CSSProperties }
41
+ /**
42
+ * Stores an image a person adds as a file of the document, and says where its blocks point to
43
+ * it; throws, saying why, when it cannot. Without it, images are not added.
44
+ */
45
+ readonly uploadImage?: (file: File) => Promise<{ readonly src: string; readonly alt: string }>
46
+ /** The section to open at: the slug of its heading, as the document page names it. */
47
+ readonly part?: string
48
+ }
49
+
50
+ export function DocumentEditor({
51
+ fragment,
52
+ awareness,
53
+ editable,
54
+ imageSource,
55
+ messages,
56
+ theme,
57
+ uploadImage,
58
+ part,
59
+ }: DocumentEditorProps) {
60
+ const picker = useRef<HTMLInputElement | null>(null)
61
+ const editorRef = useRef<Editor | null>(null)
62
+ const types = useMemo(
63
+ () =>
64
+ blockTypes(messages.blocks, uploadImage ? { pickImage: () => picker.current?.click() } : {}),
65
+ [messages.blocks, uploadImage]
66
+ )
67
+
68
+ /** Adds images where `at` is, else where the selection is, each as a figure of its own. */
69
+ const addImages = async (files: readonly File[], at?: number) => {
70
+ const editor = editorRef.current
71
+ if (!editor || !uploadImage) return
72
+ // Where they go moves with what anyone changes while they upload.
73
+ let pos = at ?? editor.state.selection.to
74
+ const follow = ({ transaction }: { readonly transaction: Transaction }) => {
75
+ pos = transaction.mapping.map(pos)
76
+ }
77
+ editor.on("transaction", follow)
78
+ try {
79
+ for (const file of files) {
80
+ try {
81
+ const { src, alt } = await uploadImage(file)
82
+ const figure = { type: "image", attrs: { src, alt, title: null } }
83
+ editor
84
+ .chain()
85
+ .insertContentAt(Math.min(pos, editor.state.doc.content.size), figure)
86
+ .focus()
87
+ .run()
88
+ pos = editor.state.selection.to
89
+ } catch (error) {
90
+ toast.error(
91
+ error instanceof Error && error.message ? error.message : messages.imageFailed
92
+ )
93
+ }
94
+ }
95
+ } finally {
96
+ editor.off("transaction", follow)
97
+ }
98
+ }
99
+ const slash = useMemo(() => new SlashMenuStore(), [])
100
+ const [linkRequest, setLinkRequest] = useState(0)
101
+
102
+ // A picture publishing takes: another artifact's, or one of this artifact's files.
103
+ const isOwnImage = (src: string) => {
104
+ const files = fragment.doc?.getMap<unknown>(LIVE_FILES)
105
+ if (isArtifactAddress(src)) return true
106
+ const path = artifactPath(src)
107
+ return path !== null && files?.has(path) === true
108
+ }
109
+
110
+ const editor = useEditor(
111
+ {
112
+ editable,
113
+ extensions: [
114
+ ...documentSchemaExtensions({
115
+ callout: CalloutNode.extend({
116
+ addNodeView: () => ReactNodeViewRenderer(CalloutView),
117
+ }),
118
+ }).map((extension) =>
119
+ extension.name === "image"
120
+ ? Figure.extend({ addNodeView: () => ReactNodeViewRenderer(FigureView) })
121
+ : extension
122
+ ),
123
+ Collaboration.configure({ fragment }),
124
+ CollaborationCaret.configure({
125
+ provider: { awareness },
126
+ // Who someone is, the server says: what this reader says of itself is replaced.
127
+ user: {},
128
+ render: caret,
129
+ selectionRender: (user: { readonly color?: string }) => ({
130
+ nodeName: "span",
131
+ class: "sixb-caret-selection",
132
+ style: `--caret: ${user.color ?? "#888"}`,
133
+ }),
134
+ }),
135
+ Placeholder.configure({
136
+ includeChildren: true,
137
+ showOnlyCurrent: true,
138
+ placeholder: ({ editor, node, pos }) => placeholder(editor, node, pos, messages),
139
+ }),
140
+ SlashCommand(slash, () => types),
141
+ BlockShortcuts.configure({ onLink: () => setLinkRequest((count) => count + 1) }),
142
+ SoftBreaks,
143
+ ],
144
+ editorProps: {
145
+ attributes: {
146
+ class: "prose sixb-document sixb-editor",
147
+ spellcheck: "true",
148
+ "aria-multiline": "true",
149
+ role: "textbox",
150
+ },
151
+ // An image dropped or pasted is added where it lands.
152
+ handleDrop: (view, event, _slice, moved) => {
153
+ const files = imagesOf(event.dataTransfer)
154
+ if (moved || files.length === 0 || !uploadImage) return false
155
+ event.preventDefault()
156
+ const at = view.posAtCoords({ left: event.clientX, top: event.clientY })
157
+ void addImages(files, at?.pos)
158
+ return true
159
+ },
160
+ // What is pasted or dropped from elsewhere keeps only the artifact's own pictures: the
161
+ // document could not be saved with any other.
162
+ transformPasted: (slice) => {
163
+ const content = withOwnImages(slice.content, isOwnImage)
164
+ if (content === slice.content) return slice
165
+ const open = Slice.maxOpen(content)
166
+ return new Slice(
167
+ content,
168
+ Math.min(slice.openStart, open.openStart),
169
+ Math.min(slice.openEnd, open.openEnd)
170
+ )
171
+ },
172
+ // Markdown pasted as text becomes its blocks; copied, blocks are Markdown again.
173
+ handlePaste: (view, event) => {
174
+ const files = imagesOf(event.clipboardData)
175
+ if (files.length > 0 && uploadImage) {
176
+ void addImages(files)
177
+ return true
178
+ }
179
+ const text = event.clipboardData?.getData("text/plain") ?? ""
180
+ if (event.clipboardData?.getData("text/html") || !looksLikeMarkdown(text)) return false
181
+ // Code is pasted as it is.
182
+ if (view.state.selection.$from.parent.type.spec.code) return false
183
+ const content = markdownToBlocks(text).content ?? []
184
+ // Words alone go into the line they are pasted in; blocks, as blocks.
185
+ const [only] = content
186
+ const inline = content.length === 1 && only?.type === "paragraph"
187
+ // Markdown trims a paragraph's spaces; words pasted into a line keep theirs.
188
+ const space = (match: RegExpMatchArray | null) =>
189
+ match ? [{ type: "text", text: match[0] }] : []
190
+ const nodes = inline
191
+ ? [
192
+ ...space(text.match(/^[ \t]+/)),
193
+ ...(only.content ?? []),
194
+ ...space(text.match(/[ \t]+$/)),
195
+ ]
196
+ : content
197
+ try {
198
+ const fragment = withOwnImages(Fragment.fromJSON(view.state.schema, nodes), isOwnImage)
199
+ view.dispatch(view.state.tr.replaceSelection(Slice.maxOpen(fragment)).scrollIntoView())
200
+ return true
201
+ } catch {
202
+ return false
203
+ }
204
+ },
205
+ clipboardTextSerializer: (slice) => {
206
+ const plain = () => slice.content.textBetween(0, slice.content.size, "\n\n", "\n")
207
+ const json = slice.content.toJSON() as { readonly type: string }[] | null
208
+ if (!json?.length) return ""
209
+ const inline = slice.content.firstChild?.isInline === true
210
+ const blocks = inline ? [{ type: "paragraph", content: json }] : json
211
+ try {
212
+ // A slice of list items or cells writes nothing as Markdown alone: its words, then.
213
+ return blocksToMarkdown({ type: "doc", content: blocks as never }).trimEnd() || plain()
214
+ } catch {
215
+ return plain()
216
+ }
217
+ },
218
+ },
219
+ },
220
+ [fragment, awareness, types]
221
+ )
222
+ editorRef.current = editor
223
+
224
+ useEffect(() => {
225
+ if (editor && editor.isEditable !== editable) editor.setEditable(editable)
226
+ }, [editor, editable])
227
+
228
+ // A link to one of its sections opens there, as the document page does.
229
+ useEffect(() => {
230
+ if (!editor || !part) return
231
+ const slug = createHeadingSlugger()
232
+ let found: number | null = null
233
+ editor.state.doc.forEach((node, pos) => {
234
+ if (found !== null || node.type.name !== "heading") return
235
+ if (slug(node.textContent.replace(/\s+/g, " ").trim()) === part) found = pos
236
+ })
237
+ if (found === null) return
238
+ const dom = editor.view.nodeDOM(found)
239
+ if (dom instanceof HTMLElement) dom.scrollIntoView({ block: "start" })
240
+ }, [editor, part])
241
+
242
+ return (
243
+ <FigureContext.Provider value={{ source: imageSource, caption: messages.caption }}>
244
+ <div
245
+ data-sixb-artifact=""
246
+ className={cn(
247
+ "@container min-h-0 flex-1 overflow-y-auto bg-background text-foreground",
248
+ theme?.className
249
+ )}
250
+ style={theme?.style}
251
+ >
252
+ <div className="sixb-document-page">
253
+ <article className="relative">
254
+ <EditorContent editor={editor} />
255
+ {editor && editable ? (
256
+ <>
257
+ <BlockHandle editor={editor} types={types} messages={messages} />
258
+ <SelectionToolbar
259
+ editor={editor}
260
+ types={types}
261
+ messages={messages}
262
+ linkRequest={linkRequest}
263
+ />
264
+ </>
265
+ ) : null}
266
+ </article>
267
+ </div>
268
+ </div>
269
+ <SlashMenu store={slash} messages={messages} />
270
+ {uploadImage ? (
271
+ <input
272
+ ref={picker}
273
+ type="file"
274
+ accept="image/*"
275
+ multiple
276
+ hidden
277
+ onChange={(event) => {
278
+ const files = [...(event.target.files ?? [])]
279
+ event.target.value = ""
280
+ void addImages(files)
281
+ }}
282
+ />
283
+ ) : null}
284
+ </FigureContext.Provider>
285
+ )
286
+ }
287
+
288
+ /** The images among the files a drop or a paste carries. */
289
+ function imagesOf(data: DataTransfer | null | undefined): File[] {
290
+ return [...(data?.files ?? [])].filter((file) => file.type.startsWith("image/"))
291
+ }
292
+
293
+ /**
294
+ * `content` without the pictures `own` does not take, nor the Markdown kept as written that shows
295
+ * one; the same fragment when it holds none. A block of blocks they leave empty gets what it must
296
+ * hold.
297
+ */
298
+ function withOwnImages(content: Fragment, own: (src: string) => boolean): Fragment {
299
+ let changed = false
300
+ const kept: ProseMirrorNode[] = []
301
+ content.forEach((node) => {
302
+ const shown =
303
+ node.type.name === "image"
304
+ ? [String(node.attrs.src ?? "")]
305
+ : node.type.name === "markdown" || node.type.name === "markdownInline"
306
+ ? imageSources(String(node.attrs.source ?? ""))
307
+ : []
308
+ if (!shown.every(own)) {
309
+ changed = true
310
+ return
311
+ }
312
+
313
+ const inner = node.isLeaf ? node.content : withOwnImages(node.content, own)
314
+ if (inner === node.content) {
315
+ kept.push(node)
316
+ return
317
+ }
318
+ changed = true
319
+ // Words that were pictures alone go with them.
320
+ if (node.isTextblock && inner.size === 0) return
321
+ const rebuilt = node.type.createAndFill(node.attrs, inner, node.marks)
322
+ if (rebuilt) kept.push(rebuilt)
323
+ })
324
+ return changed ? Fragment.from(kept) : content
325
+ }
326
+
327
+ /** The pictures Markdown or HTML written as it was shows: `![…](src)` and `<img src>`. */
328
+ function imageSources(source: string): string[] {
329
+ const pictures = source.matchAll(
330
+ /!\[[^\]]*\]\(\s*<?([^\s)>]*)|<img\b[^>]*?\bsrc\s*=\s*["']?([^"'\s>]*)/gi
331
+ )
332
+ return [...pictures].map((match) => match[1] ?? match[2] ?? "")
333
+ }
334
+
335
+ /**
336
+ * The file of the artifact a picture's path names, as publishing reads it; null for a path that
337
+ * cannot name one: an address elsewhere, a `data:` image, or a path from the root.
338
+ */
339
+ function artifactPath(src: string): string | null {
340
+ if (src === "" || src.startsWith("/") || /^[a-z][a-z\d+.-]*:/i.test(src)) return null
341
+ let path = src.split(/[?#]/)[0] ?? ""
342
+ try {
343
+ path = decodeURI(path)
344
+ } catch {
345
+ // Kept as written: it simply names no file.
346
+ }
347
+ const segments: string[] = []
348
+ for (const segment of path.split("/")) {
349
+ if (segment === "..") segments.pop()
350
+ else if (segment !== "" && segment !== ".") segments.push(segment)
351
+ }
352
+ return segments.join("/")
353
+ }
354
+
355
+ /** Another editor's caret: a line in their color, their name above it. */
356
+ function caret(user: { readonly name?: string; readonly color?: string }): HTMLElement {
357
+ const element = document.createElement("span")
358
+ element.className = "sixb-caret"
359
+ element.style.setProperty("--caret", user.color ?? "#888")
360
+ const label = document.createElement("span")
361
+ label.className = "sixb-caret-label"
362
+ label.textContent = user.name || "·"
363
+ element.append(label)
364
+ return element
365
+ }
366
+
367
+ /** What an empty block says it is for, while the caret is in it. */
368
+ function placeholder(
369
+ editor: Editor,
370
+ node: ProseMirrorNode,
371
+ pos: number,
372
+ messages: EditorMessages
373
+ ): string {
374
+ if (node.type.name === "heading") {
375
+ return pos === 0 ? messages.titlePlaceholder : messages.heading(Number(node.attrs.level))
376
+ }
377
+ if (node.type.name !== "paragraph") return ""
378
+ const parent = editor.state.doc.resolve(pos).parent.type.name
379
+ if (parent === "listItem") return messages.listItem
380
+ if (parent === "taskItem") return messages.taskItem
381
+ if (pos === 0 && editor.state.doc.childCount === 1) return messages.titlePlaceholder
382
+ return messages.placeholder
383
+ }
384
+
385
+ /** Whether pasted text reads as Markdown: a heading, a list, a quote, a fence, a table, a link. */
386
+ function looksLikeMarkdown(text: string): boolean {
387
+ return (
388
+ /^(#{1,6} |[-*+] |\d+\. |> |```|\|.+\|$|- \[[ x]\] )/m.test(text) ||
389
+ /\*\*[^*\n]+\*\*|\[[^\]\n]+\]\([^)\n]+\)/.test(text)
390
+ )
391
+ }
392
+
393
+ /**
394
+ * A line break within a paragraph's Markdown, as the Agent writes them: the document page reads
395
+ * it as a space, and so does the editor, while the text keeps it, so the Markdown written back
396
+ * is the same. Code keeps its lines.
397
+ */
398
+ const SoftBreaks = Extension.create({
399
+ name: "softBreaks",
400
+ addProseMirrorPlugins() {
401
+ return [
402
+ new Plugin<DecorationSet>({
403
+ state: {
404
+ init: (_, state) => softBreaks(state.doc),
405
+ apply: (transaction, set) => (transaction.docChanged ? softBreaks(transaction.doc) : set),
406
+ },
407
+ props: {
408
+ decorations(state) {
409
+ return this.getState(state)
410
+ },
411
+ },
412
+ }),
413
+ ]
414
+ },
415
+ })
416
+
417
+ function softBreaks(doc: ProseMirrorNode): DecorationSet {
418
+ const breaks: Decoration[] = []
419
+ doc.descendants((node, pos) => {
420
+ if (node.type.spec.code) return false
421
+ if (!node.isText) return true
422
+
423
+ for (const match of (node.text ?? "").matchAll(/\r?\n|\r/g)) {
424
+ const at = pos + match.index
425
+ breaks.push(Decoration.inline(at, at + match[0].length, { class: "sixb-soft-break" }))
426
+ }
427
+ return false
428
+ })
429
+ return DecorationSet.create(doc, breaks)
430
+ }
431
+
432
+ /**
433
+ * Keys for whole blocks: ⌘D duplicates the one the caret is in, ⌥⇧↑ and ⌥⇧↓ move it, ⌘K opens
434
+ * the link field over the selection.
435
+ */
436
+ const BlockShortcuts = Extension.create<{ readonly onLink: () => void }>({
437
+ name: "blockShortcuts",
438
+ addOptions: () => ({ onLink: () => {} }),
439
+ addKeyboardShortcuts() {
440
+ const current = () => {
441
+ const { $from } = this.editor.state.selection
442
+ if ($from.depth < 1) return null
443
+ const pos = $from.before(1)
444
+ const node = this.editor.state.doc.nodeAt(pos)
445
+ return node ? { node, pos } : null
446
+ }
447
+ const move = (by: -1 | 1) => {
448
+ const at = current()
449
+ if (!at) return false
450
+ const index = this.editor.state.doc.resolve(at.pos).index()
451
+ const sibling = this.editor.state.doc.maybeChild(index + by)
452
+ if (!sibling) return true
453
+ const offset = this.editor.state.selection.from - at.pos
454
+ const target = by === 1 ? at.pos + sibling.nodeSize : at.pos - sibling.nodeSize
455
+ return this.editor
456
+ .chain()
457
+ .command(({ tr }) => {
458
+ tr.delete(at.pos, at.pos + at.node.nodeSize)
459
+ tr.insert(target, at.node)
460
+ return true
461
+ })
462
+ .setTextSelection(target + offset)
463
+ .scrollIntoView()
464
+ .run()
465
+ }
466
+ return {
467
+ "Mod-d": () => {
468
+ const at = current()
469
+ if (!at) return false
470
+ return this.editor
471
+ .chain()
472
+ .insertContentAt(at.pos + at.node.nodeSize, at.node.toJSON())
473
+ .run()
474
+ },
475
+ "Alt-Shift-ArrowUp": () => move(-1),
476
+ "Alt-Shift-ArrowDown": () => move(1),
477
+ "Mod-k": () => {
478
+ if (this.editor.state.selection.empty) return false
479
+ this.options.onLink()
480
+ return true
481
+ },
482
+ }
483
+ },
484
+ })
@@ -0,0 +1,66 @@
1
+ import { isArtifactAddress } from "@sixb/core/artifacts"
2
+ import { cn } from "@sixb/ui/lib/utils"
3
+ import { type NodeViewProps, NodeViewWrapper } from "@tiptap/react"
4
+ import { ImageOff } from "lucide-react"
5
+ import { createContext, useContext, useEffect, useState } from "react"
6
+ import { ArtifactEmbed } from "../references/references"
7
+
8
+ /** Where a document's image path is served from, and what an empty caption says. */
9
+ export const FigureContext = createContext<{
10
+ readonly source: (src: string) => string | null
11
+ readonly caption: string
12
+ }>({ source: () => null, caption: "" })
13
+
14
+ /**
15
+ * A figure as it reads: the image, and its caption, which is typed into in place. The caption is
16
+ * the image's title, as Markdown keeps it.
17
+ */
18
+ export function FigureView({ node, updateAttributes, selected, editor }: NodeViewProps) {
19
+ const { source: resolve, caption: captionPlaceholder } = useContext(FigureContext)
20
+ const src = String(node.attrs.src ?? "")
21
+ const embedded = isArtifactAddress(src)
22
+ const url = /^(https?:|data:image\/)/.test(src) ? src : resolve(src)
23
+ const [caption, setCaption] = useState(String(node.attrs.title ?? ""))
24
+ useEffect(() => setCaption(String(node.attrs.title ?? "")), [node.attrs.title])
25
+
26
+ return (
27
+ <NodeViewWrapper as="figure" className={cn("sixb-editor-figure", selected && "is-selected")}>
28
+ <div data-drag-handle="" className="sixb-editor-figure-frame">
29
+ {embedded ? (
30
+ // Another artifact, shown as the page shows it; its alt text is its caption.
31
+ <div contentEditable={false}>
32
+ <ArtifactEmbed address={src.trim()} caption={String(node.attrs.alt ?? "")} />
33
+ </div>
34
+ ) : url ? (
35
+ <img src={url} alt={String(node.attrs.alt ?? "")} draggable={false} />
36
+ ) : (
37
+ <div className="flex aspect-[16/7] flex-col items-center justify-center gap-2 rounded-[var(--radius)] bg-muted text-sm text-muted-foreground">
38
+ <ImageOff className="size-5" />
39
+ <span className="font-mono text-xs">{src}</span>
40
+ </div>
41
+ )}
42
+ </div>
43
+ {!embedded && (editor.isEditable || caption) ? (
44
+ <input
45
+ className="sixb-editor-caption"
46
+ value={caption}
47
+ placeholder={captionPlaceholder}
48
+ readOnly={!editor.isEditable}
49
+ aria-label={String(node.attrs.alt ?? "")}
50
+ onChange={(event) => setCaption(event.target.value)}
51
+ onBlur={() => {
52
+ const title = caption.trim() || null
53
+ if (title !== (node.attrs.title ?? null)) updateAttributes({ title })
54
+ }}
55
+ onKeyDown={(event) => {
56
+ if (event.key === "Enter") {
57
+ event.preventDefault()
58
+ event.currentTarget.blur()
59
+ editor.commands.focus()
60
+ }
61
+ }}
62
+ />
63
+ ) : null}
64
+ </NodeViewWrapper>
65
+ )
66
+ }
@@ -0,0 +1,19 @@
1
+ /** The kinds of block a query finds, best first: by label, then by keyword. */
2
+ export function findBlockTypes<
3
+ Type extends { readonly label: string; readonly keywords: readonly string[] },
4
+ >(types: readonly Type[], query: string): Type[] {
5
+ const wanted = query.trim().toLowerCase()
6
+ if (!wanted) return [...types]
7
+ const score = (type: Type) => {
8
+ const label = type.label.toLowerCase()
9
+ if (label.startsWith(wanted)) return 0
10
+ if (label.includes(wanted)) return 1
11
+ if (type.keywords.some((keyword) => keyword.startsWith(wanted))) return 2
12
+ return -1
13
+ }
14
+ return types
15
+ .map((type) => ({ type, rank: score(type) }))
16
+ .filter(({ rank }) => rank >= 0)
17
+ .sort((left, right) => left.rank - right.rank)
18
+ .map(({ type }) => type)
19
+ }
@@ -0,0 +1,72 @@
1
+ import { LIVE_FILES } from "@sixb/core/artifacts/live"
2
+ import { useCallback, useEffect, useRef } from "react"
3
+ import * as Y from "yjs"
4
+ import { useLiveArtifactState, useViewerLiveArtifact } from "../live/hooks"
5
+ import { useArtifactMessages } from "../messages"
6
+ import { type ArtifactVersion, versionImageSource } from "../queries"
7
+ import { DocumentRenderer } from "../renderers/document"
8
+ import { useArtifactThemeAttributes } from "../theme"
9
+ import { DocumentEditor } from "./document-editor"
10
+ import { useImageUpload } from "./upload"
11
+
12
+ /**
13
+ * A Markdown document's body: once the live document holds its blocks, the editor, which
14
+ * everyone who may change it types into as it is shown; until then, and for an older version, the
15
+ * document page.
16
+ */
17
+ export function MarkdownDocument({
18
+ version,
19
+ latestVersion,
20
+ part,
21
+ }: {
22
+ readonly version: ArtifactVersion
23
+ readonly latestVersion: number
24
+ readonly part?: string
25
+ }) {
26
+ const messages = useArtifactMessages()
27
+ const theme = useArtifactThemeAttributes(version)
28
+ const live = useViewerLiveArtifact()
29
+ const state = useLiveArtifactState(live)
30
+ // An image added since the version was read shows from the reader's own copy until one holds it.
31
+ const previews = useRef(new Map<string, string>())
32
+ useEffect(() => {
33
+ const kept = previews.current
34
+ return () => {
35
+ for (const url of kept.values()) URL.revokeObjectURL(url)
36
+ }
37
+ }, [])
38
+ const imageSource = useCallback(
39
+ (src: string) =>
40
+ previews.current.get(src) ?? versionImageSource(version, src)?.inlineUrl ?? null,
41
+ [version]
42
+ )
43
+ const upload = useImageUpload(live, version)
44
+ const uploadImage = useCallback(
45
+ async (file: File) => {
46
+ const added = await upload(file)
47
+ previews.current.set(added.src, URL.createObjectURL(file))
48
+ return added
49
+ },
50
+ [upload]
51
+ )
52
+
53
+ const fragment =
54
+ live && state.synced && version.version === latestVersion
55
+ ? live.doc.getMap(LIVE_FILES).get(version.entry)
56
+ : null
57
+ if (live && fragment instanceof Y.XmlFragment) {
58
+ return (
59
+ <DocumentEditor
60
+ fragment={fragment}
61
+ awareness={live.awareness}
62
+ editable={state.access === "edit" && !state.left}
63
+ imageSource={imageSource}
64
+ messages={messages.editor}
65
+ theme={theme}
66
+ {...(part ? { part } : {})}
67
+ {...(state.access === "edit" ? { uploadImage } : {})}
68
+ />
69
+ )
70
+ }
71
+ return <DocumentRenderer version={version} latestVersion={latestVersion} part={part} />
72
+ }
@@ -0,0 +1,4 @@
1
+ import type { ArtifactMessages } from "../messages"
2
+
3
+ /** The editor's words, in the reader's locale. */
4
+ export type EditorMessages = ArtifactMessages["editor"]