@dexhq/canvas 0.15.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (307) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/app/canvas/activity-parts.tsx +50 -6
  3. package/app/canvas/activity.tsx +2 -3
  4. package/app/canvas/agent-changes.ts +18 -8
  5. package/app/canvas/anchoring.ts +13 -3
  6. package/app/canvas/canvas-context.tsx +6 -2
  7. package/app/canvas/canvas-input.ts +35 -0
  8. package/app/canvas/canvas-model.ts +2 -2
  9. package/app/canvas/canvas-scene.ts +243 -24
  10. package/app/canvas/canvas-ui.ts +11 -2
  11. package/app/canvas/canvas.css +86 -3
  12. package/app/canvas/comments.tsx +66 -29
  13. package/app/canvas/element-link.ts +135 -0
  14. package/app/canvas/element-picker.ts +23 -0
  15. package/app/canvas/export-dialog.tsx +265 -0
  16. package/app/canvas/flow-canvas.tsx +65 -14
  17. package/app/canvas/frame-context-menu.tsx +61 -7
  18. package/app/canvas/frame-node.tsx +10 -11
  19. package/app/canvas/layout.ts +41 -22
  20. package/app/canvas/linked-element.tsx +196 -0
  21. package/app/canvas/live-page.ts +14 -0
  22. package/app/canvas/mentions.tsx +209 -0
  23. package/app/canvas/picked-box.tsx +21 -0
  24. package/app/canvas/removed-views-panel.tsx +4 -4
  25. package/app/canvas/request-node.tsx +151 -0
  26. package/app/canvas/review-panel.tsx +8 -3
  27. package/app/canvas/section-node.tsx +4 -133
  28. package/app/canvas/spec-margin.tsx +4 -1
  29. package/app/canvas/teammates-layer.tsx +205 -0
  30. package/app/canvas/text-layer.tsx +103 -17
  31. package/app/canvas/tool-cursors.ts +7 -6
  32. package/app/canvas/tool-icons.tsx +24 -56
  33. package/app/canvas/typing.ts +12 -0
  34. package/app/canvas/use-agent-changes.tsx +6 -2
  35. package/app/canvas/use-canvas-editor.ts +4 -2
  36. package/app/export/player-model.ts +159 -0
  37. package/app/export/player.tsx +198 -0
  38. package/app/export.html +31 -0
  39. package/app/export.tsx +9 -0
  40. package/app/frame/announce-refreshes.ts +21 -0
  41. package/app/frame/start-frame.tsx +50 -0
  42. package/app/frame/view-loads.ts +161 -0
  43. package/app/frame/views.ts +13 -49
  44. package/app/frame.tsx +2 -38
  45. package/app/i18n/locales/en/messages.po +145 -6
  46. package/app/i18n/locales/en/messages.ts +1 -1
  47. package/app/i18n/locales/pseudo/messages.po +144 -5
  48. package/app/i18n/locales/pseudo/messages.ts +1 -1
  49. package/app/icons/dex-glyphs.tsx +219 -0
  50. package/app/icons/glyphs.tsx +76 -80
  51. package/app/kit/use-flow.ts +2 -1
  52. package/app/live/canvas-teammates.ts +90 -0
  53. package/app/live/studio-api.ts +30 -20
  54. package/app/pages/canvas.tsx +18 -6
  55. package/app/pages/commands-settings.tsx +1 -1
  56. package/app/pages/render.tsx +39 -11
  57. package/app/shell/bell.tsx +15 -5
  58. package/app/shell/presence.tsx +123 -26
  59. package/app/shell/thread-list.tsx +25 -8
  60. package/app/shell/working-on.tsx +35 -3
  61. package/app/ui/prompt-editor.tsx +39 -11
  62. package/app/ui/reference.tsx +21 -8
  63. package/bin/sudio.mjs +1 -1
  64. package/dist/cli/commands/create.d.mts +5 -5
  65. package/dist/cli/commands/create.mjs +4 -4
  66. package/dist/cli/commands/export.d.mts +40 -0
  67. package/dist/cli/commands/export.mjs +66 -0
  68. package/dist/cli/commands/init.d.mts +2 -2
  69. package/dist/cli/commands/upgrade.mjs +1 -1
  70. package/dist/cli/help.d.mts +1 -1
  71. package/dist/cli/help.mjs +13 -7
  72. package/dist/cli/main.d.mts +42 -5
  73. package/dist/cli/main.mjs +1 -0
  74. package/dist/config/load.mjs +14 -2
  75. package/dist/config/project-files.d.mts +5 -0
  76. package/dist/config/project-files.mjs +10 -1
  77. package/dist/config/renamed.d.mts +7 -0
  78. package/dist/config/renamed.mjs +13 -1
  79. package/dist/content/commands.d.mts +11 -9
  80. package/dist/content/commands.mjs +22 -16
  81. package/dist/content/comments-log.mjs +2 -1
  82. package/dist/content/content.d.mts +11 -3
  83. package/dist/content/content.mjs +49 -15
  84. package/dist/content/element-words.d.mts +42 -0
  85. package/dist/content/element-words.mjs +112 -0
  86. package/dist/content/hosted-person.d.mts +31 -0
  87. package/dist/content/hosted-person.mjs +68 -0
  88. package/dist/content/merge-rules.d.mts +5 -0
  89. package/dist/content/merge-rules.mjs +17 -1
  90. package/dist/content/screenshot.mjs +9 -0
  91. package/dist/content/text-edit.d.mts +24 -14
  92. package/dist/content/text-edit.mjs +92 -81
  93. package/dist/content/text-replace.mjs +2 -26
  94. package/dist/content/thread-module.d.mts +2 -4
  95. package/dist/content/thread-module.mjs +4 -5
  96. package/dist/server/agents.d.mts +10 -0
  97. package/dist/server/agents.mjs +21 -6
  98. package/dist/server/canvas-presence.d.mts +22 -0
  99. package/dist/server/canvas-presence.mjs +160 -0
  100. package/dist/server/command-settings.mjs +1 -1
  101. package/dist/server/comments/actions.d.mts +0 -8
  102. package/dist/server/comments/actions.mjs +1 -9
  103. package/dist/server/comments/routes.mjs +10 -31
  104. package/dist/server/dev-server.mjs +35 -13
  105. package/dist/server/export/build.d.mts +15 -0
  106. package/dist/server/export/build.mjs +220 -0
  107. package/dist/server/export/page.d.mts +25 -0
  108. package/dist/server/export/page.mjs +40 -0
  109. package/dist/server/export/plugin.d.mts +31 -0
  110. package/dist/server/export/plugin.mjs +69 -0
  111. package/dist/server/export/run.d.mts +9 -0
  112. package/dist/server/export/run.mjs +61 -0
  113. package/dist/server/export/save.d.mts +7 -0
  114. package/dist/server/export/save.mjs +29 -0
  115. package/dist/server/hosted.d.mts +8 -0
  116. package/dist/server/hosted.mjs +78 -0
  117. package/dist/server/live-mirror.mjs +66 -10
  118. package/dist/server/live.mjs +28 -1
  119. package/dist/server/mcp.mjs +2 -0
  120. package/dist/server/me.d.mts +2 -0
  121. package/dist/server/me.mjs +8 -2
  122. package/dist/server/notices.d.mts +1 -1
  123. package/dist/server/notices.mjs +1 -1
  124. package/dist/server/own-copies.mjs +1 -1
  125. package/dist/server/people.d.mts +7 -2
  126. package/dist/server/people.mjs +6 -2
  127. package/dist/server/pictures.d.mts +1 -1
  128. package/dist/server/pictures.mjs +2 -2
  129. package/dist/server/prebundle.d.mts +32 -0
  130. package/dist/server/prebundle.mjs +115 -0
  131. package/dist/server/problems.d.mts +8 -0
  132. package/dist/server/problems.mjs +13 -0
  133. package/dist/server/requests.mjs +43 -30
  134. package/dist/server/source-stamp.d.mts +4 -3
  135. package/dist/server/source-stamp.mjs +60 -31
  136. package/dist/server/state.d.mts +19 -10
  137. package/dist/server/state.mjs +27 -18
  138. package/dist/server/thread-source.d.mts +8 -0
  139. package/dist/server/thread-source.mjs +24 -5
  140. package/dist/server/tools/export.d.mts +5 -0
  141. package/dist/server/tools/export.mjs +35 -0
  142. package/dist/server/tools/place-frame.mjs +16 -16
  143. package/dist/server/tools/thread-list.mjs +1 -1
  144. package/dist/server/vite.d.mts +9 -4
  145. package/dist/server/vite.mjs +63 -61
  146. package/dist/server/welcome.mjs +2 -1
  147. package/dist/setup/apply.d.mts +4 -0
  148. package/dist/setup/apply.mjs +5 -4
  149. package/dist/setup/create.mjs +5 -5
  150. package/dist/setup/design-system-checks.mjs +1 -6
  151. package/dist/setup/detect/workspace.d.mts +10 -10
  152. package/dist/setup/detect/workspace.mjs +10 -12
  153. package/dist/setup/doctor.d.mts +4 -1
  154. package/dist/setup/doctor.mjs +7 -5
  155. package/dist/setup/migrate/dex-folder.d.mts +61 -0
  156. package/dist/setup/migrate/dex-folder.mjs +544 -0
  157. package/dist/setup/migrate/files-folder.mjs +1 -1
  158. package/dist/setup/migrate/guides.mjs +1 -1
  159. package/dist/setup/migrate/migrations.mjs +4 -0
  160. package/dist/setup/migrate/setup-files.d.mts +5 -0
  161. package/dist/setup/migrate/setup-files.mjs +115 -0
  162. package/dist/setup/migrate/studio-state.d.mts +2 -2
  163. package/dist/setup/migrate/studio-state.mjs +4 -4
  164. package/dist/setup/placement.d.mts +1 -1
  165. package/dist/setup/placement.mjs +5 -4
  166. package/dist/setup/plan.d.mts +5 -5
  167. package/dist/setup/plan.mjs +8 -12
  168. package/dist/setup/project.mjs +1 -1
  169. package/dist/setup/prompts.d.mts +2 -1
  170. package/dist/setup/prompts.mjs +4 -3
  171. package/dist/setup/questions.mjs +3 -2
  172. package/dist/setup/starters.d.mts +1 -1
  173. package/dist/setup/starters.mjs +2 -2
  174. package/dist/setup/write.d.mts +15 -3
  175. package/dist/setup/write.mjs +23 -3
  176. package/dist/shared/canvas-core.d.mts +22 -7
  177. package/dist/shared/canvas-core.mjs +38 -10
  178. package/dist/shared/comments/routes.d.mts +3 -1
  179. package/dist/shared/comments/routes.mjs +4 -2
  180. package/dist/shared/export-core.d.mts +62 -0
  181. package/dist/shared/export-core.mjs +101 -0
  182. package/dist/shared/live-core.d.mts +2 -0
  183. package/dist/shared/live-core.mjs +6 -1
  184. package/dist/shared/live-presence.d.mts +57 -2
  185. package/dist/shared/live-presence.mjs +90 -4
  186. package/dist/shared/page-messages.d.mts +14 -1
  187. package/dist/shared/page-messages.mjs +30 -3
  188. package/dist/shared/people-core.d.mts +28 -0
  189. package/dist/shared/people-core.mjs +27 -1
  190. package/dist/shared/presence-core.d.mts +2 -0
  191. package/dist/shared/references-core.d.mts +4 -0
  192. package/dist/shared/references-core.mjs +12 -4
  193. package/dist/shared/studio-files.d.mts +9 -0
  194. package/dist/shared/studio-files.mjs +11 -2
  195. package/dist/shared/studio-routes.d.mts +13 -1
  196. package/dist/shared/threads-core.d.mts +27 -4
  197. package/dist/shared/threads-core.mjs +67 -12
  198. package/extension/dist/feedback.js +104 -294
  199. package/extension/dist/manifest.json +1 -1
  200. package/extension/dist/picker.js +104 -294
  201. package/package.json +2 -2
  202. package/src/cli/commands/create.ts +4 -4
  203. package/src/cli/commands/export.ts +78 -0
  204. package/src/cli/commands/upgrade.ts +1 -1
  205. package/src/cli/help.ts +13 -7
  206. package/src/cli/main.ts +1 -0
  207. package/src/config/load.ts +26 -2
  208. package/src/config/project-files.ts +15 -0
  209. package/src/config/renamed.ts +19 -1
  210. package/src/content/commands.ts +30 -18
  211. package/src/content/comments-log.ts +4 -1
  212. package/src/content/content.ts +64 -16
  213. package/src/content/element-words.ts +175 -0
  214. package/src/content/hosted-person.ts +107 -0
  215. package/src/content/merge-rules.ts +29 -0
  216. package/src/content/screenshot.ts +14 -0
  217. package/src/content/text-edit.ts +172 -105
  218. package/src/content/text-replace.ts +3 -32
  219. package/src/content/thread-module.ts +2 -4
  220. package/src/content/trash.ts +1 -1
  221. package/src/server/agents.ts +48 -17
  222. package/src/server/canvas-presence.ts +237 -0
  223. package/src/server/channel.ts +1 -1
  224. package/src/server/command-settings.ts +1 -1
  225. package/src/server/comments/actions.ts +0 -19
  226. package/src/server/comments/routes.ts +9 -54
  227. package/src/server/dev-server.ts +66 -26
  228. package/src/server/export/build.ts +310 -0
  229. package/src/server/export/page.ts +85 -0
  230. package/src/server/export/plugin.ts +138 -0
  231. package/src/server/export/run.ts +79 -0
  232. package/src/server/export/save.ts +38 -0
  233. package/src/server/hosted.ts +113 -0
  234. package/src/server/live-mirror.ts +93 -8
  235. package/src/server/live.ts +46 -1
  236. package/src/server/machine-update.ts +3 -3
  237. package/src/server/mcp.ts +2 -0
  238. package/src/server/me.ts +10 -1
  239. package/src/server/notices.ts +2 -2
  240. package/src/server/people.ts +11 -2
  241. package/src/server/pictures.ts +2 -2
  242. package/src/server/prebundle.ts +230 -0
  243. package/src/server/presence.ts +1 -1
  244. package/src/server/problems.ts +11 -0
  245. package/src/server/requests.ts +55 -40
  246. package/src/server/roster.ts +1 -1
  247. package/src/server/saved.ts +1 -1
  248. package/src/server/source-stamp.ts +118 -37
  249. package/src/server/state.ts +53 -31
  250. package/src/server/thread-source.ts +35 -3
  251. package/src/server/tools/export.ts +56 -0
  252. package/src/server/tools/place-frame.ts +23 -15
  253. package/src/server/tools/thread-list.ts +1 -1
  254. package/src/server/updates.ts +2 -2
  255. package/src/server/vite.ts +106 -110
  256. package/src/server/welcome.ts +3 -1
  257. package/src/setup/apply.ts +8 -5
  258. package/src/setup/create.ts +10 -10
  259. package/src/setup/design-system-checks.ts +1 -6
  260. package/src/setup/detect/workspace.ts +20 -21
  261. package/src/setup/doctor.ts +13 -6
  262. package/src/setup/migrate/dex-folder.ts +842 -0
  263. package/src/setup/migrate/files-folder.ts +1 -1
  264. package/src/setup/migrate/guides.ts +3 -1
  265. package/src/setup/migrate/migrations.ts +8 -2
  266. package/src/setup/migrate/setup-files.ts +198 -0
  267. package/src/setup/migrate/studio-state.ts +12 -10
  268. package/src/setup/own-app.ts +1 -1
  269. package/src/setup/placement.ts +7 -6
  270. package/src/setup/plan.ts +13 -18
  271. package/src/setup/project.ts +2 -2
  272. package/src/setup/prompts.ts +5 -4
  273. package/src/setup/questions.ts +6 -3
  274. package/src/setup/starters.ts +3 -3
  275. package/src/setup/write.ts +34 -3
  276. package/src/shared/canvas-core.ts +48 -9
  277. package/src/shared/comments/routes.ts +3 -1
  278. package/src/shared/export-core.ts +198 -0
  279. package/src/shared/live-core.ts +8 -0
  280. package/src/shared/live-presence.ts +171 -2
  281. package/src/shared/page-messages.ts +38 -2
  282. package/src/shared/people-core.ts +44 -0
  283. package/src/shared/presence-core.ts +2 -0
  284. package/src/shared/references-core.ts +22 -3
  285. package/src/shared/studio-files.ts +15 -1
  286. package/src/shared/studio-routes.ts +11 -1
  287. package/src/shared/threads-core.ts +132 -10
  288. package/templates/methods/explore.md +1 -1
  289. package/templates/methods/handoff.md +1 -1
  290. package/templates/methods/review.md +2 -1
  291. package/templates/rules/canvas-frames.md +6 -3
  292. package/templates/rules/commands.md +1 -0
  293. package/templates/rules/prototypes.md +1 -1
  294. package/templates/rules/studio.md +17 -11
  295. package/templates/skills/comprehend/SKILL.md +56 -0
  296. package/templates/skills/distill/SKILL.md +172 -0
  297. package/templates/skills/export-prototype/SKILL.md +35 -0
  298. package/templates/skills/riff/SKILL.md +21 -0
  299. package/templates/skills/riff-deep/SKILL.md +40 -0
  300. package/templates/skills/riff-max/SKILL.md +46 -0
  301. package/templates/skills/riff-state/SKILL.md +45 -0
  302. package/templates/skills/riff-up/SKILL.md +38 -0
  303. package/templates/skills/riff-wide/SKILL.md +40 -0
  304. package/templates/starters/standalone/README.md +1 -1
  305. package/templates/starters/turborepo/README.md +2 -2
  306. package/templates/starters/turborepo/apps/web/src/app.tsx +2 -2
  307. package/templates/starters/vite/README.md +1 -1
@@ -0,0 +1,175 @@
1
+ // The words a JSX element writes where it is: the one piece of text between
2
+ // its tags (beside its icons and other elements), or, for a component with
3
+ // nothing between its tags, the strings it is given as props that say
4
+ // something rather than how it looks or works. The dev server stamps an
5
+ // element that writes words with its place (source-stamp.ts); the Text tool
6
+ // changes them there (text-edit.ts). An element a callback renders, a list's
7
+ // row, is never stamped: its words show once for every item.
8
+
9
+ import { createHash } from "node:crypto";
10
+ import type { types } from "@babel/core";
11
+
12
+ /** Props whose strings say how something looks or works, not what it says. */
13
+ export const CODE_PROPS = new Set([
14
+ "as",
15
+ "className",
16
+ "class",
17
+ "href",
18
+ "htmlFor",
19
+ "icon",
20
+ "id",
21
+ "key",
22
+ "name",
23
+ "rel",
24
+ "role",
25
+ "size",
26
+ "src",
27
+ "style",
28
+ "target",
29
+ "type",
30
+ "variant",
31
+ ]);
32
+
33
+ /** Whether a JSX prop's string is code (a class, a link, a test id), not words. */
34
+ export const isCodeProp = (name: string): boolean =>
35
+ CODE_PROPS.has(name) || name.startsWith("data-") || name.startsWith("aria-");
36
+
37
+ const HOST_RE = /^[a-z]/;
38
+ const SPACES_RE = /\s+/g;
39
+
40
+ /** Words as a page shows them: spaced once, whatever the code's lines. */
41
+ export const words = (text: string): string =>
42
+ text.replace(SPACES_RE, " ").trim();
43
+
44
+ /** Elements whose text is code, not words a page shows. */
45
+ const CODE_TAGS = new Set(["script", "style"]);
46
+
47
+ /** Words as written in the code: the node that holds them, and what they say. */
48
+ export interface Written {
49
+ /**
50
+ * A prop's string written in JSX (`label="Save"`), where a backslash
51
+ * escapes nothing; else text between tags, or a string in braces.
52
+ */
53
+ attribute?: boolean;
54
+ node: types.JSXText | types.StringLiteral;
55
+ /** What they say, as the page has them (entities and escapes read). */
56
+ value: string;
57
+ }
58
+
59
+ /** Whether an element is the page's own tag (`<h1>`), not a component. */
60
+ export const isHostElement = ({ openingElement: { name } }: types.JSXElement) =>
61
+ name.type === "JSXIdentifier" && HOST_RE.test(name.name);
62
+
63
+ const said = (value: string) => value.trim() !== "";
64
+
65
+ /**
66
+ * The words between an element's tags, when they are one piece: one text,
67
+ * or one string in braces, beside anything else it holds. None for text in
68
+ * pieces (`Due {days} days`), or for a style's or a script's.
69
+ */
70
+ export function childWords(element: types.JSXElement): Written | undefined {
71
+ const { name } = element.openingElement;
72
+ if (name.type === "JSXIdentifier" && CODE_TAGS.has(name.name)) {
73
+ return;
74
+ }
75
+ const pieces: Written[] = [];
76
+ for (const child of element.children) {
77
+ if (child.type === "JSXText" && said(child.value)) {
78
+ pieces.push({ node: child, value: child.value });
79
+ } else if (
80
+ child.type === "JSXExpressionContainer" &&
81
+ child.expression.type === "StringLiteral" &&
82
+ said(child.expression.value)
83
+ ) {
84
+ pieces.push({ node: child.expression, value: child.expression.value });
85
+ }
86
+ }
87
+ return pieces.length === 1 ? pieces[0] : undefined;
88
+ }
89
+
90
+ /** Whether an element holds anything between its tags but space. */
91
+ const holdsAnything = ({ children }: types.JSXElement) =>
92
+ children.some((child) => child.type !== "JSXText" || said(child.value));
93
+
94
+ /**
95
+ * The strings a component with nothing between its tags is given as props
96
+ * that say something: `title="Collections"` or `title={"Collections"}`, not
97
+ * a class or a variant. One that holds anything shows that instead, and a
98
+ * page's own tags show none: their props are not text.
99
+ */
100
+ export function propWords(element: types.JSXElement): Written[] {
101
+ if (isHostElement(element) || holdsAnything(element)) {
102
+ return [];
103
+ }
104
+ const written: Written[] = [];
105
+ for (const prop of element.openingElement.attributes) {
106
+ if (
107
+ prop.type !== "JSXAttribute" ||
108
+ prop.name.type !== "JSXIdentifier" ||
109
+ isCodeProp(prop.name.name)
110
+ ) {
111
+ continue;
112
+ }
113
+ const { value } = prop;
114
+ if (value?.type === "StringLiteral" && said(value.value)) {
115
+ written.push({ attribute: true, node: value, value: value.value });
116
+ } else if (
117
+ value?.type === "JSXExpressionContainer" &&
118
+ value.expression.type === "StringLiteral" &&
119
+ said(value.expression.value)
120
+ ) {
121
+ written.push({ node: value.expression, value: value.expression.value });
122
+ }
123
+ }
124
+ return written;
125
+ }
126
+
127
+ const CALLS = new Set([
128
+ "CallExpression",
129
+ "NewExpression",
130
+ "OptionalCallExpression",
131
+ ]);
132
+ const CALLBACKS = new Set(["ArrowFunctionExpression", "FunctionExpression"]);
133
+
134
+ /**
135
+ * The elements a callback renders, such as a list's rows in `items.map(…)`:
136
+ * written once, shown once for each item.
137
+ */
138
+ export function repeatedElements(
139
+ program: types.Program
140
+ ): Set<types.JSXElement> {
141
+ const repeated = new Set<types.JSXElement>();
142
+ const walk = (node: unknown, inCallback: boolean) => {
143
+ if (Array.isArray(node)) {
144
+ for (const child of node) {
145
+ walk(child, inCallback);
146
+ }
147
+ return;
148
+ }
149
+ if (!node || typeof node !== "object") {
150
+ return;
151
+ }
152
+ const record = node as { type?: unknown } & Record<string, unknown>;
153
+ if (inCallback && record.type === "JSXElement") {
154
+ repeated.add(record as unknown as types.JSXElement);
155
+ }
156
+ const isCall = CALLS.has(String(record.type));
157
+ for (const [key, value] of Object.entries(record)) {
158
+ if (key === "loc" || key === "extra" || key.endsWith("Comments")) {
159
+ continue;
160
+ }
161
+ const given =
162
+ isCall &&
163
+ key === "arguments" &&
164
+ Array.isArray(value) &&
165
+ value.some((argument) => CALLBACKS.has(argument?.type));
166
+ walk(value, inCallback || given);
167
+ }
168
+ };
169
+ walk(program.body, false);
170
+ return repeated;
171
+ }
172
+
173
+ /** Which version of a file a page was served, as its places say it. */
174
+ export const textVersion = (code: string): string =>
175
+ createHash("sha1").update(code).digest("hex").slice(0, 12);
@@ -0,0 +1,107 @@
1
+ // Dex in the browser, hosted for a workspace (STUDIO_HOSTED): one Dex, many
2
+ // people. The cloud's preview host signs each person in and says who they are
3
+ // on every request and socket, in a header no browser can set (the host
4
+ // replaces it). Each request then runs as its person: who "you" are (your
5
+ // name, your folder, what you sign) comes from here instead of git and the
6
+ // GitHub CLI of the machine Dex runs on. A Dex on someone's own machine is
7
+ // never hosted, and ignores the header.
8
+
9
+ import { AsyncLocalStorage } from "node:async_hooks";
10
+ import { randomBytes, timingSafeEqual } from "node:crypto";
11
+
12
+ /** Someone signed in to the workspace, using Dex in their browser. */
13
+ export interface HostedPerson {
14
+ color?: string;
15
+ email: string;
16
+ /** Their account's id in the workspace service. */
17
+ id: string;
18
+ name: string;
19
+ /** Viewers look and comment; editors change files too. */
20
+ role: "owner" | "editor" | "viewer";
21
+ }
22
+
23
+ const ROLES = new Set(["owner", "editor", "viewer"]);
24
+ const MAX_FIELD = 320;
25
+
26
+ const text = (value: unknown): string | undefined =>
27
+ typeof value === "string" && value.length > 0 && value.length <= MAX_FIELD
28
+ ? value
29
+ : undefined;
30
+
31
+ /** Whether this Dex runs hosted, for many people in their browsers. */
32
+ export const isHosted = (): boolean => process.env.STUDIO_HOSTED === "1";
33
+
34
+ /** The person a header names, or undefined when it names no one properly. */
35
+ export function personFromHeader(
36
+ header: string | string[] | undefined
37
+ ): HostedPerson | undefined {
38
+ if (typeof header !== "string") {
39
+ return;
40
+ }
41
+ let fields: Record<string, unknown>;
42
+ try {
43
+ fields = JSON.parse(Buffer.from(header, "base64url").toString("utf8"));
44
+ } catch {
45
+ return;
46
+ }
47
+ const id = text(fields.id);
48
+ const name = text(fields.name);
49
+ const email = text(fields.email);
50
+ const role = fields.role;
51
+ if (!(id && name && email && typeof role === "string" && ROLES.has(role))) {
52
+ return;
53
+ }
54
+ return {
55
+ id,
56
+ name,
57
+ email,
58
+ role: role as HostedPerson["role"],
59
+ ...(text(fields.color) ? { color: text(fields.color) } : {}),
60
+ };
61
+ }
62
+
63
+ /**
64
+ * Dex's own browser, capturing frames and thumbnails from this server: it
65
+ * has no person, so it comes in with a key only this process knows, and only
66
+ * looks (a viewer).
67
+ */
68
+ export const CAPTURE_HEADER = "x-dex-capture";
69
+ export const CAPTURE_PERSON: HostedPerson = {
70
+ id: "dex-capture",
71
+ name: "Dex",
72
+ email: "capture@dex.invalid",
73
+ role: "viewer",
74
+ };
75
+
76
+ // On globalThis, so the dev server and the capture code share one key even
77
+ // when the build puts them in different chunks.
78
+ const KEY = Symbol.for("dex.capture-key");
79
+ const captureKey = (): string => {
80
+ const holder = globalThis as unknown as Record<symbol, string | undefined>;
81
+ holder[KEY] ??= randomBytes(32).toString("base64url");
82
+ return holder[KEY];
83
+ };
84
+
85
+ /** The headers Dex's own browser sends this server when hosted, if any. */
86
+ export const captureHeaders = (): Record<string, string> | undefined =>
87
+ isHosted() ? { [CAPTURE_HEADER]: captureKey() } : undefined;
88
+
89
+ /** Whether a request is Dex's own browser, by its key. */
90
+ export function isCapture(header: string | string[] | undefined): boolean {
91
+ if (typeof header !== "string") {
92
+ return false;
93
+ }
94
+ const said = Buffer.from(header);
95
+ const key = Buffer.from(captureKey());
96
+ return said.length === key.length && timingSafeEqual(said, key);
97
+ }
98
+
99
+ const current = new AsyncLocalStorage<HostedPerson>();
100
+
101
+ /** Run `work` as `person`: everything it awaits sees them as "you". */
102
+ export const asPerson = <T>(person: HostedPerson, work: () => T): T =>
103
+ current.run(person, work);
104
+
105
+ /** Who the request being answered is from, when hosted. */
106
+ export const hostedPerson = (): HostedPerson | undefined =>
107
+ isHosted() ? current.getStore() : undefined;
@@ -32,6 +32,35 @@ export const MERGE_RULES: Record<string, string[]> = {
32
32
  "": [`/contributors.json merge=${CONTRIBUTORS_MERGE}`],
33
33
  };
34
34
 
35
+ /**
36
+ * A .gitattributes with each merge Sudio named (OLD_MERGES) named as Dex
37
+ * names it, and a line that leaves the same as one already there taken out.
38
+ */
39
+ export function withNewMerges(text: string): string {
40
+ const renames: [string, string][] = [
41
+ [OLD_MERGES.spec, SPEC_MERGE],
42
+ [OLD_MERGES.contributors, CONTRIBUTORS_MERGE],
43
+ ];
44
+ const lines = text.split("\n");
45
+ const kept = new Set(lines.map((line) => line.trim()));
46
+ return lines
47
+ .flatMap((line) => {
48
+ const renamed = renames.reduce(
49
+ (each, [old, now]) => each.replace(`merge=${old}`, `merge=${now}`),
50
+ line
51
+ );
52
+ if (renamed === line) {
53
+ return [line];
54
+ }
55
+ if (kept.has(renamed.trim())) {
56
+ return [];
57
+ }
58
+ kept.add(renamed.trim());
59
+ return [renamed];
60
+ })
61
+ .join("\n");
62
+ }
63
+
35
64
  /** Add the merge rules each .gitattributes does not have yet. */
36
65
  export async function askForMerges(root: string): Promise<void> {
37
66
  for (const [folder, rules] of Object.entries(MERGE_RULES)) {
@@ -8,6 +8,7 @@ import type { Browser, BrowserType, Page, Request } from "playwright";
8
8
  import { VIEWPORTS } from "../shared/canvas-core.ts";
9
9
  import { VIEW_RENDERED } from "../shared/page-messages.ts";
10
10
  import { viewInFrame } from "../shared/prototypes-core.ts";
11
+ import { captureHeaders } from "./hosted-person.ts";
11
12
 
12
13
  const SETTLE_MS = 600;
13
14
  /**
@@ -380,6 +381,19 @@ async function capture(
380
381
  deviceScaleFactor: scale,
381
382
  });
382
383
  try {
384
+ // Hosted, this server lets in only people, and Dex's own browser by its
385
+ // key: sent to this server alone, never to a font or image elsewhere.
386
+ const headers = captureHeaders();
387
+ if (headers) {
388
+ const origin = new URL(site).origin;
389
+ await page.route(
390
+ (url) => url.origin === origin,
391
+ (route) =>
392
+ route.continue({
393
+ headers: { ...route.request().headers(), ...headers },
394
+ })
395
+ );
396
+ }
383
397
  await page.addInitScript(
384
398
  (theme) => {
385
399
  localStorage.setItem("theme", theme);
@@ -1,134 +1,201 @@
1
- // Editing a prototype's text on the canvas, with the Type tool. A change is
2
- // made in the prototype's code when the text is written there exactly once,
3
- // whole: as the text between tags, or as a whole string. Anything else (text
4
- // built from data, written twice, or wrapped across lines) is not certain
5
- // enough to change by hand, so the canvas asks the agent instead.
1
+ // Editing a prototype's text on the canvas, with the Text tool: the words are
2
+ // changed in the code where the element writes them. The page says which
3
+ // element it is by its place, which the dev server stamps on every element
4
+ // that writes words (source-stamp.ts) with the version of the file it served,
5
+ // and the words there that read as the page showed them are changed. Anything
6
+ // else (text a variable, a prop, a list or a sum makes, words written twice,
7
+ // or code changed since the page loaded) is not certain enough to change by
8
+ // hand, so the canvas asks the agent instead. Only the person's own files are
9
+ // changed, and only in turn with the file's other changes.
6
10
 
7
- import { readdir, readFile, writeFile } from "node:fs/promises";
8
- import { join, relative } from "node:path";
11
+ import { readFile } from "node:fs/promises";
12
+ import { isAbsolute, join, relative } from "node:path";
13
+ import type { types } from "@babel/core";
14
+ import { posix } from "../paths.ts";
15
+ import { queued } from "./content.ts";
16
+ import {
17
+ childWords,
18
+ propWords,
19
+ textVersion,
20
+ type Written,
21
+ words,
22
+ } from "./element-words.ts";
23
+ import { replaceFile } from "./file-turns.ts";
24
+ import { parseJsx } from "./jsx-source.ts";
25
+ import { applyEdits, type Edit, openPlace } from "./prop-edit.ts";
26
+ import { prototypePaths } from "./prototypes.ts";
9
27
 
10
- const SOURCE_RE = /\.(?:tsx|ts|jsx|js)$/;
11
- const SKIPPED = new Set(["comments", "docs", "node_modules"]);
12
- const QUOTES = new Set(['"', "'", "`"]);
13
- const JSX_UNSAFE_RE = /[{}<>]/;
14
- const SPACE_RE = /\s/;
15
28
  const MAX_LENGTH = 2000;
29
+ /** A place's file, and what follows it: line, column, and the file's version. */
30
+ const PLACE_RE = /^(.+):\d+:\d+#(\w+)$/;
31
+ /** What JSX reads in text between tags: a tag, braces, an entity, lines. */
32
+ const READ_IN_TEXT_RE = /[{}<>\n\r]|&(?:#\d+|#x[\da-f]+|\w+);/i;
33
+ /** What JSX reads in a prop's string, besides its quote. */
34
+ const READ_IN_PROP_RE = /[\n\r]|&(?:#\d+|#x[\da-f]+|\w+);/i;
35
+ const NBSP_RE = / /g;
16
36
 
17
- /** Where text is written whole: inside quotes, or between tags. */
18
- export type WholeMatch =
19
- | { index: number; kind: "string"; quote: string }
20
- | { index: number; kind: "jsx" };
37
+ const CANNOT_TELL = "Dex can't tell where in the code that text is written.";
38
+ const CHANGED = "the code has changed since the frame loaded.";
39
+ const MORE_THAN_ONCE = "That text is written more than once there.";
40
+ const SOMEONE_ELSES = "That view is someone else's.";
21
41
 
22
- /** The file changed, relative to the prototype, or why it was not. */
42
+ /** The file changed, from the person's own folder, or why it was not. */
23
43
  export type TextEdit = { file: string } | { reason: string };
24
44
 
25
- async function sourceFiles(dir: string): Promise<string[]> {
26
- const files: string[] = [];
27
- for (const entry of await readdir(dir, { withFileTypes: true })) {
28
- if (SKIPPED.has(entry.name)) {
29
- continue;
30
- }
31
- const path = join(dir, entry.name);
32
- if (entry.isDirectory()) {
33
- files.push(...(await sourceFiles(path)));
34
- } else if (SOURCE_RE.test(entry.name)) {
35
- files.push(path);
36
- }
37
- }
38
- return files;
45
+ /** Where an edit may change code. */
46
+ export interface TextScope {
47
+ /** The studio's content folder: places are written from it. */
48
+ content: string;
49
+ /** The person's own folder in the file: nothing outside it changes. */
50
+ own: string;
51
+ /** The file, whose turn the change takes. */
52
+ slug: string;
39
53
  }
40
54
 
41
- /** The first character before `index` that is not white space. */
42
- const before = (text: string, index: number): string | undefined => {
43
- let at = index - 1;
44
- while (at >= 0 && SPACE_RE.test(text[at])) {
45
- at -= 1;
46
- }
47
- return text[at];
48
- };
55
+ /** Text changed on the canvas, and where it is. */
56
+ export interface TextChange {
57
+ /** The text as the page showed it. */
58
+ from?: string;
59
+ /**
60
+ * The element's place as the page has it stamped, with the version of the
61
+ * file it was served ("files/<slug>/people/<person>/views/home.tsx:12:5#…").
62
+ */
63
+ place?: string;
64
+ to?: string;
65
+ }
49
66
 
50
- /** The first character from `index` that is not white space. */
51
- const after = (text: string, index: number): string | undefined => {
52
- let at = index;
53
- while (at < text.length && SPACE_RE.test(text[at])) {
54
- at += 1;
55
- }
56
- return text[at];
67
+ /** Words found in a module, ready to change. */
68
+ interface Found {
69
+ code: string;
70
+ file: string;
71
+ written: Written;
72
+ }
73
+
74
+ const isInside = (folder: string, file: string) => {
75
+ const inside = relative(folder, file);
76
+ return inside !== "" && !inside.startsWith("..") && !isAbsolute(inside);
57
77
  };
58
78
 
59
- /** Where `value` is written whole in `text`: a string, or text between tags. */
60
- export function wholeMatches(text: string, value: string): WholeMatch[] {
61
- const found: WholeMatch[] = [];
62
- let index = text.indexOf(value);
63
- while (index !== -1) {
64
- const end = index + value.length;
65
- const quote = text[index - 1];
66
- if (QUOTES.has(quote) && text[end] === quote) {
67
- found.push({ index, kind: "string", quote });
68
- } else if (
69
- [">", "}"].includes(before(text, index) ?? "") &&
70
- ["<", "{"].includes(after(text, end) ?? "")
71
- ) {
72
- found.push({ index, kind: "jsx" });
73
- }
74
- index = text.indexOf(value, index + 1);
79
+ /** The words an element writes that read as `said`: between its tags, else one prop. */
80
+ function wordsOf(element: types.JSXElement, said: string): Written | string {
81
+ const child = childWords(element);
82
+ if (child && words(child.value) === said) {
83
+ return child;
75
84
  }
76
- return found;
85
+ const props = propWords(element).filter(({ value }) => words(value) === said);
86
+ if (props.length === 1) {
87
+ return props[0];
88
+ }
89
+ return props.length > 1 ? MORE_THAN_ONCE : CANNOT_TELL;
77
90
  }
78
91
 
79
- /** The replacement as it must be written, or undefined if it cannot be. */
80
- const written = (match: WholeMatch, value: string): string | undefined => {
81
- if (match.kind === "jsx") {
82
- return JSX_UNSAFE_RE.test(value) ? undefined : value;
92
+ /** The words written at an element's place, in the person's own files. */
93
+ async function atPlace(
94
+ { content, own, slug }: TextScope,
95
+ place: string | undefined,
96
+ said: string
97
+ ): Promise<Found | { reason: string }> {
98
+ const [, path = "", version] = PLACE_RE.exec(place ?? "") ?? [];
99
+ if (!version) {
100
+ return { reason: CANNOT_TELL };
101
+ }
102
+ const file = join(content, path);
103
+ if (
104
+ isInside(prototypePaths(content, slug).folder, file) &&
105
+ !isInside(own, file)
106
+ ) {
107
+ return { reason: SOMEONE_ELSES };
83
108
  }
84
- const escaped = value
85
- .replaceAll("\\", "\\\\")
86
- .replaceAll(match.quote, `\\${match.quote}`);
87
- return match.quote === "`" ? escaped.replaceAll("${", "\\${") : escaped;
109
+ const opened = await openPlace(content, own, place);
110
+ if (typeof opened === "string") {
111
+ return { reason: opened };
112
+ }
113
+ if (textVersion(opened.code) !== version) {
114
+ return { reason: CHANGED };
115
+ }
116
+ const written = wordsOf(opened.element, said);
117
+ return typeof written === "string"
118
+ ? { reason: written }
119
+ : { code: opened.code, file: opened.file, written };
120
+ }
121
+
122
+ /** A string in quotes, as JavaScript reads it. */
123
+ const quoted = (value: string, quote: string) => {
124
+ const json = JSON.stringify(value);
125
+ return quote === "'"
126
+ ? `'${json.slice(1, -1).replaceAll('\\"', '"').replaceAll("'", "\\'")}'`
127
+ : json;
88
128
  };
89
129
 
130
+ /** The edit that writes `value` in place of the words found. */
131
+ function rewrite(
132
+ code: string,
133
+ { attribute, node }: Written,
134
+ value: string
135
+ ): Edit {
136
+ const start = node.start ?? 0;
137
+ const end = node.end ?? start;
138
+ const quote = code[start] ?? '"';
139
+ if (node.type === "JSXText") {
140
+ // Between tags: the space around the words stays, as it reads.
141
+ const raw = code.slice(start, end);
142
+ const text = READ_IN_TEXT_RE.test(value)
143
+ ? `{${JSON.stringify(value)}}`
144
+ : value;
145
+ return {
146
+ start: start + (raw.length - raw.trimStart().length),
147
+ end: end - (raw.length - raw.trimEnd().length),
148
+ text,
149
+ };
150
+ }
151
+ if (attribute) {
152
+ // A prop's string escapes nothing: one JSX would read goes in braces.
153
+ const plain = !(value.includes(quote) || READ_IN_PROP_RE.test(value));
154
+ return {
155
+ start,
156
+ end,
157
+ text: plain ? `${quote}${value}${quote}` : `{${JSON.stringify(value)}}`,
158
+ };
159
+ }
160
+ return { start, end, text: quoted(value, quote) };
161
+ }
162
+
90
163
  /**
91
- * Change `from` to `to` in a prototype's code. Returns { file } when changed,
164
+ * Change the text an element on the canvas shows from `from` to `to` in the
165
+ * person's own code, at the element's place. Answers { file } when changed,
92
166
  * or { reason } when the agent should do it.
93
167
  */
94
168
  export async function editText(
95
- folder: string,
96
- from: string | undefined,
97
- to: string | undefined
169
+ scope: TextScope,
170
+ { from, place, to }: TextChange
98
171
  ): Promise<TextEdit> {
99
- const original = from?.trim() ?? "";
100
- const next = to?.trim() ?? "";
101
- if (!(original && next) || next.length > MAX_LENGTH) {
172
+ const said = words(from ?? "");
173
+ // What the person typed: a space the page kept with a no-break space is
174
+ // a space.
175
+ const next = (to ?? "").replace(NBSP_RE, " ").trim();
176
+ if (!(said && next) || next.length > MAX_LENGTH) {
102
177
  return { reason: "There is no text to change." };
103
178
  }
104
- if (original === next) {
179
+ if (said === words(next)) {
105
180
  return { reason: "The text did not change." };
106
181
  }
107
- const found: { file: string; text: string; match: WholeMatch }[] = [];
108
- for (const file of await sourceFiles(folder)) {
109
- const text = await readFile(file, "utf8");
110
- for (const match of wholeMatches(text, original)) {
111
- found.push({ file, text, match });
182
+ return await queued(scope.content, scope.slug, async () => {
183
+ const found = await atPlace(scope, place, said);
184
+ if ("reason" in found) {
185
+ return found;
112
186
  }
113
- }
114
- if (found.length !== 1) {
115
- return {
116
- reason:
117
- found.length === 0
118
- ? "That text is not written as it is in the code."
119
- : "That text is written in more than one place.",
120
- };
121
- }
122
- const [{ file, text, match }] = found;
123
- const value = written(match, next);
124
- if (value === undefined) {
125
- return { reason: "The new text has characters code would read." };
126
- }
127
- await writeFile(
128
- file,
129
- text.slice(0, match.index) +
130
- value +
131
- text.slice(match.index + original.length)
132
- );
133
- return { file: relative(folder, file) };
187
+ const { code, file, written } = found;
188
+ const changed = applyEdits(code, [rewrite(code, written, next)]);
189
+ // Never a change that would not run.
190
+ if (!parseJsx(changed, file)) {
191
+ return { reason: "The new text would break the view." };
192
+ }
193
+ // Agents change views without waiting: one changed since it was read
194
+ // keeps their change.
195
+ if ((await readFile(file, "utf8").catch(() => undefined)) !== code) {
196
+ return { reason: CHANGED };
197
+ }
198
+ await replaceFile(file, changed);
199
+ return { file: posix(relative(scope.own, file)) };
200
+ });
134
201
  }