@dexhq/canvas 0.14.0 → 0.15.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 (229) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/README.md +5 -3
  3. package/app/canvas/activity.tsx +8 -15
  4. package/app/canvas/anchoring.ts +3 -5
  5. package/app/canvas/canvas-input.ts +151 -14
  6. package/app/canvas/canvas-keys.ts +9 -0
  7. package/app/canvas/canvas-model.ts +5 -42
  8. package/app/canvas/canvas-panels.ts +1 -17
  9. package/app/canvas/canvas-scene.ts +7 -3
  10. package/app/canvas/canvas-ui.ts +2 -1
  11. package/app/canvas/canvas.css +61 -26
  12. package/app/canvas/change-notice.tsx +10 -1
  13. package/app/canvas/comment-layer.tsx +2 -1
  14. package/app/canvas/comments.tsx +204 -82
  15. package/app/canvas/element-picker.ts +200 -27
  16. package/app/canvas/engine/canvas-gestures.ts +72 -17
  17. package/app/canvas/engine/canvas-store.ts +17 -1
  18. package/app/canvas/engine/engine.css +6 -3
  19. package/app/canvas/find-bar.tsx +817 -0
  20. package/app/canvas/find-model.ts +135 -0
  21. package/app/canvas/flow-canvas.tsx +18 -2
  22. package/app/canvas/follow-pin.ts +26 -6
  23. package/app/canvas/frame-node.tsx +9 -9
  24. package/app/canvas/imported-reviews.tsx +12 -22
  25. package/app/canvas/layout.ts +27 -0
  26. package/app/canvas/live-page.ts +142 -19
  27. package/app/canvas/page-find.ts +220 -0
  28. package/app/canvas/page-style.ts +23 -0
  29. package/app/canvas/section-node.tsx +2 -0
  30. package/app/canvas/spec-threads.ts +24 -23
  31. package/app/canvas/thread-focus.tsx +14 -12
  32. package/app/canvas/use-activity.ts +0 -8
  33. package/app/canvas/use-agent-changes.tsx +34 -40
  34. package/app/canvas/use-canvas-clipboard.tsx +11 -14
  35. package/app/canvas/xray.ts +3 -12
  36. package/app/frame.tsx +5 -0
  37. package/app/i18n/locales/en/messages.po +88 -8
  38. package/app/i18n/locales/en/messages.ts +1 -1
  39. package/app/i18n/locales/pseudo/messages.po +88 -8
  40. package/app/i18n/locales/pseudo/messages.ts +1 -1
  41. package/app/icons/glyphs.tsx +4 -0
  42. package/app/main.tsx +3 -0
  43. package/app/pages/canvas.tsx +27 -8
  44. package/app/pages/commands-settings.tsx +16 -2
  45. package/app/shell/feedback-form.tsx +10 -21
  46. package/app/shell/feedback-link.tsx +2 -1
  47. package/app/shell/scaled-frame.tsx +21 -9
  48. package/app/types.ts +21 -205
  49. package/app/ui/file-drop.ts +128 -0
  50. package/app/ui/shadcn/input-group.tsx +2 -2
  51. package/dist/cli/banner.d.mts +4 -2
  52. package/dist/cli/banner.mjs +34 -11
  53. package/dist/cli/commands/app.mjs +19 -11
  54. package/dist/cli/commands/hook.mjs +2 -1
  55. package/dist/config/project-files.d.mts +6 -0
  56. package/dist/config/project-files.mjs +14 -2
  57. package/dist/config/renamed.d.mts +0 -2
  58. package/dist/config/renamed.mjs +1 -3
  59. package/dist/content/capture.d.mts +2 -2
  60. package/dist/content/capture.mjs +2 -2
  61. package/dist/content/content.d.mts +3 -141
  62. package/dist/content/content.mjs +7 -62
  63. package/dist/content/flow-specs.mjs +1 -1
  64. package/dist/content/prototype-summary.d.mts +3 -39
  65. package/dist/content/prototype-summary.mjs +2 -3
  66. package/dist/content/prototypes.mjs +1 -1
  67. package/dist/content/text-replace.d.mts +30 -0
  68. package/dist/content/text-replace.mjs +309 -0
  69. package/dist/content/thread-module.mjs +1 -1
  70. package/dist/content/threads.mjs +1 -1
  71. package/dist/paste/paste.mjs +1 -1
  72. package/dist/paths.d.mts +0 -2
  73. package/dist/paths.mjs +1 -3
  74. package/dist/server/agent-env.d.mts +5 -1
  75. package/dist/server/agent-env.mjs +7 -2
  76. package/dist/server/agent-session.d.mts +14 -1
  77. package/dist/server/agent-session.mjs +84 -19
  78. package/dist/server/build-pictures.mjs +1 -1
  79. package/dist/server/channel.d.mts +13 -24
  80. package/dist/server/channel.mjs +55 -37
  81. package/dist/server/claimed-presence.d.mts +3 -2
  82. package/dist/server/claimed-presence.mjs +15 -7
  83. package/dist/server/close-check.mjs +2 -2
  84. package/dist/server/comments/actions.d.mts +153 -0
  85. package/dist/server/comments/actions.mjs +154 -0
  86. package/dist/server/comments/routes.d.mts +25 -0
  87. package/dist/server/comments/routes.mjs +88 -0
  88. package/dist/server/comments/tools.d.mts +6 -0
  89. package/dist/server/comments/tools.mjs +154 -0
  90. package/dist/server/dev-server.mjs +27 -13
  91. package/dist/server/file-lock.d.mts +2 -2
  92. package/dist/server/file-lock.mjs +130 -36
  93. package/dist/server/mcp.d.mts +0 -2
  94. package/dist/server/mcp.mjs +7 -8
  95. package/dist/server/me.d.mts +1 -1
  96. package/dist/server/open-page.d.mts +22 -0
  97. package/dist/server/open-page.mjs +20 -2
  98. package/dist/server/outcome.d.mts +22 -0
  99. package/dist/server/outcome.mjs +8 -0
  100. package/dist/server/own-copies.mjs +2 -2
  101. package/dist/server/people.d.mts +1 -1
  102. package/dist/server/port.d.mts +12 -0
  103. package/dist/server/port.mjs +66 -14
  104. package/dist/server/presence.d.mts +4 -1
  105. package/dist/server/presence.mjs +2 -2
  106. package/dist/server/requests.d.mts +5 -1
  107. package/dist/server/requests.mjs +33 -48
  108. package/dist/server/studio-for.d.mts +5 -0
  109. package/dist/server/studio-for.mjs +17 -3
  110. package/dist/server/sync-install.d.mts +8 -12
  111. package/dist/server/sync-install.mjs +12 -30
  112. package/dist/server/thread-source.mjs +2 -2
  113. package/dist/server/tools/describe.d.mts +1 -1
  114. package/dist/server/tools/describe.mjs +1 -1
  115. package/dist/server/tools/feedback.d.mts +1 -1
  116. package/dist/server/tools/reading.mjs +13 -4
  117. package/dist/server/tools/show.d.mts +1 -1
  118. package/dist/server/tools/snapshot.d.mts +1 -1
  119. package/dist/server/tools/thread-list.mjs +1 -1
  120. package/dist/server/tools/watch.mjs +5 -3
  121. package/dist/server/vite.mjs +2 -2
  122. package/dist/server/web-app.mjs +4 -7
  123. package/dist/server/work-groups.mjs +1 -1
  124. package/dist/setup/create.d.mts +4 -2
  125. package/dist/setup/create.mjs +67 -17
  126. package/dist/setup/design-system-checks.mjs +3 -12
  127. package/dist/setup/desktop-app-script.mjs +4 -3
  128. package/dist/setup/desktop-app-start.d.mts +8 -2
  129. package/dist/setup/desktop-app-start.mjs +64 -29
  130. package/dist/setup/desktop-app.d.mts +8 -0
  131. package/dist/setup/desktop-app.mjs +19 -3
  132. package/dist/setup/detect.d.mts +0 -2
  133. package/dist/setup/detect.mjs +1 -3
  134. package/dist/setup/doctor.mjs +1 -1
  135. package/dist/setup/migrate/row-titles.mjs +1 -1
  136. package/dist/setup/project.d.mts +6 -1
  137. package/dist/setup/project.mjs +9 -9
  138. package/dist/setup/starters.d.mts +2 -0
  139. package/dist/setup/starters.mjs +7 -1
  140. package/dist/shared/annotations-core.mjs +1 -1
  141. package/dist/shared/canvas-core.mjs +1 -1
  142. package/dist/shared/canvas-file.d.mts +201 -0
  143. package/dist/shared/canvas-file.mjs +1 -0
  144. package/dist/shared/comments/routes.d.mts +53 -0
  145. package/dist/shared/comments/routes.mjs +37 -0
  146. package/dist/shared/feedback.d.mts +8 -0
  147. package/dist/shared/feedback.mjs +10 -2
  148. package/dist/shared/page-review.d.mts +2 -11
  149. package/dist/shared/page-review.mjs +4 -13
  150. package/dist/shared/presence-core.d.mts +11 -0
  151. package/dist/shared/presence-core.mjs +11 -2
  152. package/dist/shared/review-file.d.mts +5 -0
  153. package/dist/shared/review-file.mjs +23 -13
  154. package/dist/shared/server-page.mjs +1 -1
  155. package/dist/shared/signup.d.mts +2 -5
  156. package/dist/shared/signup.mjs +2 -35
  157. package/dist/shared/studio-events.d.mts +0 -6
  158. package/dist/shared/studio-events.mjs +1 -5
  159. package/dist/shared/studio-files.d.mts +12 -0
  160. package/dist/shared/studio-files.mjs +6 -1
  161. package/dist/shared/studio-routes.d.mts +16 -62
  162. package/dist/shared/studio-routes.mjs +1 -69
  163. package/dist/shared/threads-core.d.mts +2 -2
  164. package/dist/shared/threads-core.mjs +1 -1
  165. package/extension/dist/feedback.js +148 -138
  166. package/extension/dist/manifest.json +1 -1
  167. package/extension/dist/picker.js +146 -136
  168. package/package.json +2 -2
  169. package/src/cli/banner.ts +45 -11
  170. package/src/cli/commands/app.ts +23 -8
  171. package/src/cli/commands/hook.ts +4 -1
  172. package/src/config/project-files.ts +21 -6
  173. package/src/config/renamed.ts +0 -3
  174. package/src/content/capture.ts +6 -12
  175. package/src/content/content.ts +7 -241
  176. package/src/content/flow-specs.ts +2 -1
  177. package/src/content/prototype-summary.ts +4 -44
  178. package/src/content/review-pages.ts +2 -1
  179. package/src/content/text-replace.ts +449 -0
  180. package/src/paths.ts +0 -8
  181. package/src/server/agent-env.ts +12 -2
  182. package/src/server/agent-session.ts +124 -34
  183. package/src/server/channel.ts +94 -52
  184. package/src/server/claimed-presence.ts +26 -10
  185. package/src/server/comments/actions.ts +352 -0
  186. package/src/server/comments/routes.ts +176 -0
  187. package/src/server/comments/tools.ts +317 -0
  188. package/src/server/dev-server.ts +36 -15
  189. package/src/server/file-lock.ts +205 -54
  190. package/src/server/mcp.ts +13 -14
  191. package/src/server/me.ts +2 -5
  192. package/src/server/open-page.ts +54 -4
  193. package/src/server/outcome.ts +26 -0
  194. package/src/server/people.ts +2 -1
  195. package/src/server/port.ts +94 -20
  196. package/src/server/presence.ts +11 -3
  197. package/src/server/requests.ts +60 -70
  198. package/src/server/studio-for.ts +26 -2
  199. package/src/server/sync-install.ts +35 -26
  200. package/src/server/thread-place.ts +2 -4
  201. package/src/server/tools/reading.ts +16 -3
  202. package/src/server/tools/watch.ts +7 -7
  203. package/src/server/web-app.ts +11 -9
  204. package/src/setup/create.ts +91 -17
  205. package/src/setup/design-system-checks.ts +7 -15
  206. package/src/setup/desktop-app-script.ts +4 -3
  207. package/src/setup/desktop-app-start.ts +94 -26
  208. package/src/setup/desktop-app.ts +23 -2
  209. package/src/setup/detect.ts +0 -4
  210. package/src/setup/project.ts +9 -8
  211. package/src/setup/starters.ts +8 -1
  212. package/src/shared/canvas-file.ts +205 -0
  213. package/src/shared/comments/routes.ts +79 -0
  214. package/src/shared/feedback.ts +14 -4
  215. package/src/shared/page-review.ts +11 -16
  216. package/src/shared/presence-core.ts +16 -2
  217. package/src/shared/review-file.ts +28 -12
  218. package/src/shared/server-page.ts +2 -1
  219. package/src/shared/signup.ts +4 -28
  220. package/src/shared/studio-events.ts +0 -13
  221. package/src/shared/studio-files.ts +14 -0
  222. package/src/shared/studio-routes.ts +21 -64
  223. package/src/shared/threads-core.ts +2 -2
  224. package/dist/server/tools/close-reply.d.mts +0 -6
  225. package/dist/server/tools/close-reply.mjs +0 -138
  226. package/dist/server/tools/comment.d.mts +0 -6
  227. package/dist/server/tools/comment.mjs +0 -49
  228. package/src/server/tools/close-reply.ts +0 -271
  229. package/src/server/tools/comment.ts +0 -96
@@ -0,0 +1,30 @@
1
+ //#region src/content/text-replace.d.ts
2
+ /** What a replacement did, and how to put it back. */
3
+ export interface Replaced {
4
+ /** The files changed, relative to the folder. */
5
+ files: string[];
6
+ /** Files left as they were: changed since they were read, or unwritable. */
7
+ left?: number;
8
+ /** How many places were changed. */
9
+ replaced: number;
10
+ /** Text found but not changed: the new words cannot be written there. */
11
+ skipped: number;
12
+ /** Puts it all back (undoReplace), while nothing has changed since. */
13
+ undo?: string;
14
+ }
15
+ /**
16
+ * Change `find` to `to`, ignoring case, wherever the modules in `folder`
17
+ * write it as text that one of the `seen` texts (what the frames showed)
18
+ * holds. Each changed file can be put back with the `undo` it answers with.
19
+ */
20
+ export declare function replaceText(folder: string, find: string, to: string, seen: readonly string[]): Promise<Replaced>;
21
+ /**
22
+ * Put back a replacement: each file it changed, unless it has changed again
23
+ * since, which keeps the newer change. Answers with the files put back and
24
+ * the ones left, relative to `folder`.
25
+ */
26
+ export declare function undoReplace(folder: string, undo: string): Promise<{
27
+ kept: string[];
28
+ restored: string[];
29
+ }>;
30
+ //#endregion
@@ -0,0 +1,309 @@
1
+ import { parseJsx } from "./jsx-source.mjs";
2
+ import { join, relative } from "node:path";
3
+ import { randomUUID } from "node:crypto";
4
+ import { readFile, readdir, writeFile } from "node:fs/promises";
5
+ //#region src/content/text-replace.ts
6
+ const SOURCE_RE = /\.(?:tsx|ts|jsx|js)$/;
7
+ const SKIPPED = /* @__PURE__ */ new Set([
8
+ "comments",
9
+ "docs",
10
+ "node_modules"
11
+ ]);
12
+ const JSX_UNSAFE_RE = /[{}<>]/;
13
+ const SPACES_RE = /\s+/g;
14
+ const REGEX_CHARS_RE = /[.*+?^${}()|[\]\\]/g;
15
+ const MAX_LENGTH = 2e3;
16
+ /** How many replacements can still be put back, newest kept. */
17
+ const UNDO_KEPT = 20;
18
+ /** Calls whose strings are class names, or a module's path. */
19
+ const CODE_CALLS = /* @__PURE__ */ new Set([
20
+ "classNames",
21
+ "clsx",
22
+ "cn",
23
+ "cva",
24
+ "cx",
25
+ "require",
26
+ "tv",
27
+ "twMerge"
28
+ ]);
29
+ /** Props whose strings say how something looks or works, not what it says. */
30
+ const CODE_PROPS = /* @__PURE__ */ new Set([
31
+ "as",
32
+ "className",
33
+ "class",
34
+ "href",
35
+ "htmlFor",
36
+ "icon",
37
+ "id",
38
+ "key",
39
+ "name",
40
+ "rel",
41
+ "role",
42
+ "size",
43
+ "src",
44
+ "style",
45
+ "target",
46
+ "type",
47
+ "variant"
48
+ ]);
49
+ const COMPARISONS = /* @__PURE__ */ new Set([
50
+ "==",
51
+ "===",
52
+ "!=",
53
+ "!=="
54
+ ]);
55
+ const undos = /* @__PURE__ */ new Map();
56
+ async function sourceFiles(dir) {
57
+ let entries;
58
+ try {
59
+ entries = await readdir(dir, { withFileTypes: true });
60
+ } catch {
61
+ return [];
62
+ }
63
+ const files = [];
64
+ for (const entry of entries) {
65
+ if (SKIPPED.has(entry.name) || entry.isSymbolicLink()) continue;
66
+ const path = join(dir, entry.name);
67
+ if (entry.isDirectory()) files.push(...await sourceFiles(path));
68
+ else if (SOURCE_RE.test(entry.name)) files.push(path);
69
+ }
70
+ return files;
71
+ }
72
+ const words = (text) => text.replace(SPACES_RE, " ").trim();
73
+ const isNode = (value) => typeof value === "object" && value !== null && typeof value.type === "string";
74
+ const calleeName = (node) => {
75
+ const callee = node.callee;
76
+ if (callee?.type === "Identifier") return callee.name;
77
+ };
78
+ /** An object property's name, when it is written as a name or a string. */
79
+ const keyName = (node) => {
80
+ const named = node.key;
81
+ if (named?.type === "Identifier") return named.name;
82
+ return named?.type === "StringLiteral" ? named.value : void 0;
83
+ };
84
+ const propName = (node) => {
85
+ const name = node.name;
86
+ return name?.type === "JSXIdentifier" ? name.name : void 0;
87
+ };
88
+ const CALLS = /* @__PURE__ */ new Set(["CallExpression", "NewExpression"]);
89
+ const FUNCTIONS = /* @__PURE__ */ new Set([
90
+ "ArrowFunctionExpression",
91
+ "FunctionDeclaration",
92
+ "FunctionExpression"
93
+ ]);
94
+ /**
95
+ * What a call is given is the call's to read, not the page's words:
96
+ * useState("Save"), getItem(draft ? "Save" : "Old"), import("./save").
97
+ * A callback it is given is words again: what it renders shows.
98
+ */
99
+ const givenToCall = (parent, key) => CALLS.has(parent.type) && key === "arguments";
100
+ /** Whether a string at this place in the tree is code, not words. */
101
+ const isCode = (parent, key) => {
102
+ if (parent.type.startsWith("TS")) return key !== "expression";
103
+ switch (parent.type) {
104
+ case "ImportDeclaration":
105
+ case "ImportExpression":
106
+ case "SwitchCase": return true;
107
+ case "ExportAllDeclaration":
108
+ case "ExportNamedDeclaration": return key === "source";
109
+ case "ObjectProperty": return key === "key" || CODE_PROPS.has(keyName(parent) ?? "");
110
+ case "ObjectMethod":
111
+ case "ClassProperty":
112
+ case "MemberExpression":
113
+ case "OptionalMemberExpression": return key === "key" || key === "property";
114
+ case "BinaryExpression": return COMPARISONS.has(parent.operator);
115
+ case "CallExpression": return CODE_CALLS.has(calleeName(parent) ?? "");
116
+ case "JSXAttribute": {
117
+ const name = propName(parent) ?? "";
118
+ return CODE_PROPS.has(name) || name.startsWith("data-") || name.startsWith("aria-");
119
+ }
120
+ default: return false;
121
+ }
122
+ };
123
+ /** Every run of words in a module that is not code. */
124
+ function textRuns(code, filename) {
125
+ const program = parseJsx(code, filename);
126
+ if (!program) return [];
127
+ const runs = [];
128
+ const walk = (node, inCode) => {
129
+ for (const [key, value] of Object.entries(node)) {
130
+ if (key === "loc" || key === "extra" || key.endsWith("Comments")) continue;
131
+ for (const child of Array.isArray(value) ? value : [value]) if (isNode(child)) visit(child, node, key, inCode);
132
+ }
133
+ };
134
+ const visit = (node, parent, key, inCode) => {
135
+ let asCode = inCode || isCode(parent, key);
136
+ if (!asCode && givenToCall(parent, key)) asCode = "given";
137
+ if (asCode === "given" && FUNCTIONS.has(node.type)) asCode = false;
138
+ const { start, end } = node;
139
+ if (typeof start !== "number" || typeof end !== "number") {
140
+ walk(node, asCode);
141
+ return;
142
+ }
143
+ if (node.type === "JSXText") {
144
+ runs.push({
145
+ kind: "jsx",
146
+ part: false,
147
+ start,
148
+ end,
149
+ value: node.value
150
+ });
151
+ return;
152
+ }
153
+ if (asCode) {
154
+ walk(node, asCode);
155
+ return;
156
+ }
157
+ if (node.type === "StringLiteral") {
158
+ const quote = code[start];
159
+ runs.push({
160
+ attribute: parent.type === "JSXAttribute",
161
+ kind: quote,
162
+ part: parent.type === "BinaryExpression",
163
+ start: start + 1,
164
+ end: end - 1,
165
+ value: node.value
166
+ });
167
+ return;
168
+ }
169
+ if (node.type === "TemplateElement") {
170
+ const cooked = node.value?.cooked;
171
+ runs.push({
172
+ kind: "`",
173
+ part: true,
174
+ start,
175
+ end,
176
+ value: cooked ?? ""
177
+ });
178
+ return;
179
+ }
180
+ walk(node, asCode);
181
+ };
182
+ walk(program, false);
183
+ return runs;
184
+ }
185
+ /** The new words as they must be written in a run, or undefined if they cannot. */
186
+ const written = ({ attribute, kind }, value) => {
187
+ if (kind === "jsx") return JSX_UNSAFE_RE.test(value) ? void 0 : value;
188
+ if (attribute) return value.includes(kind) ? void 0 : value;
189
+ const escaped = value.replaceAll("\\", "\\\\").replaceAll(kind, `\\${kind}`);
190
+ return kind === "`" ? escaped.replaceAll("${", "\\${") : escaped;
191
+ };
192
+ const finder = (find) => new RegExp(find.replace(REGEX_CHARS_RE, "\\$&"), "gi");
193
+ /** One module with `find` changed to `to` in its runs the pages showed. */
194
+ function replaceIn(code, filename, find, to, seen) {
195
+ const matches = finder(find);
196
+ let replaced = 0;
197
+ let skipped = 0;
198
+ let next = code;
199
+ const runs = textRuns(code, filename).filter(({ part, value }) => {
200
+ const said = words(value);
201
+ return said.search(matches) !== -1 && (part ? seen.some((text) => text.includes(said)) : seen.includes(said));
202
+ }).sort((a, b) => b.start - a.start);
203
+ for (const run of runs) {
204
+ const raw = code.slice(run.start, run.end);
205
+ const value = written(run, to);
206
+ const count = raw.match(finder(find))?.length ?? 0;
207
+ if (value === void 0) {
208
+ skipped += count;
209
+ continue;
210
+ }
211
+ if (count === 0) {
212
+ skipped += 1;
213
+ continue;
214
+ }
215
+ next = next.slice(0, run.start) + raw.replace(finder(find), () => value) + next.slice(run.end);
216
+ replaced += count;
217
+ }
218
+ if (next !== code && !parseJsx(next, filename)) return {
219
+ next: code,
220
+ replaced: 0,
221
+ skipped: skipped + replaced
222
+ };
223
+ return {
224
+ next,
225
+ replaced,
226
+ skipped
227
+ };
228
+ }
229
+ /**
230
+ * Change `find` to `to`, ignoring case, wherever the modules in `folder`
231
+ * write it as text that one of the `seen` texts (what the frames showed)
232
+ * holds. Each changed file can be put back with the `undo` it answers with.
233
+ */
234
+ async function replaceText(folder, find, to, seen) {
235
+ const nothing = {
236
+ files: [],
237
+ replaced: 0,
238
+ skipped: 0
239
+ };
240
+ if (!find.trim() || find.length > MAX_LENGTH || to.length > MAX_LENGTH) return nothing;
241
+ const shown = seen.map(words);
242
+ const changed = [];
243
+ let skipped = 0;
244
+ for (const path of await sourceFiles(folder)) {
245
+ const before = await readFile(path, "utf8");
246
+ if (before.search(finder(find)) === -1) continue;
247
+ const result = replaceIn(before, path, find, to, shown);
248
+ skipped += result.skipped;
249
+ if (result.next !== before) changed.push({
250
+ path,
251
+ before,
252
+ after: result.next,
253
+ replaced: result.replaced
254
+ });
255
+ }
256
+ const saved = [];
257
+ let replaced = 0;
258
+ for (const change of changed) try {
259
+ if (await readFile(change.path, "utf8") === change.before) {
260
+ await writeFile(change.path, change.after);
261
+ saved.push(change);
262
+ replaced += change.replaced ?? 0;
263
+ }
264
+ } catch {}
265
+ const left = changed.length - saved.length;
266
+ if (saved.length === 0) return {
267
+ ...nothing,
268
+ skipped,
269
+ ...left > 0 ? { left } : {}
270
+ };
271
+ const undo = randomUUID();
272
+ undos.set(undo, saved);
273
+ for (const old of undos.keys()) {
274
+ if (undos.size <= UNDO_KEPT) break;
275
+ undos.delete(old);
276
+ }
277
+ return {
278
+ files: saved.map(({ path }) => relative(folder, path)),
279
+ ...left > 0 ? { left } : {},
280
+ replaced,
281
+ skipped,
282
+ undo
283
+ };
284
+ }
285
+ /**
286
+ * Put back a replacement: each file it changed, unless it has changed again
287
+ * since, which keeps the newer change. Answers with the files put back and
288
+ * the ones left, relative to `folder`.
289
+ */
290
+ async function undoReplace(folder, undo) {
291
+ const changed = undos.get(undo) ?? [];
292
+ undos.delete(undo);
293
+ const restored = [];
294
+ const kept = [];
295
+ for (const { path, before, after } of changed) {
296
+ const now = await readFile(path, "utf8").catch(() => void 0);
297
+ const name = relative(folder, path);
298
+ if (now === after) {
299
+ await writeFile(path, before);
300
+ restored.push(name);
301
+ } else kept.push(name);
302
+ }
303
+ return {
304
+ kept,
305
+ restored
306
+ };
307
+ }
308
+ //#endregion
309
+ export { replaceText, undoReplace };
@@ -1,6 +1,6 @@
1
1
  import { splitPrototypeName } from "../shared/prototypes-core.mjs";
2
- import { legacyFile, legacyNameOf } from "./prototypes.mjs";
3
2
  import { AGENT_ACTIONS, agentChange, changeProblems, describeThread, statusFilter, threadProblems } from "../shared/threads-core.mjs";
3
+ import { legacyFile, legacyNameOf } from "./prototypes.mjs";
4
4
  import { reviewFor } from "../shared/review-file.mjs";
5
5
  import { fileStore, openStore } from "./store.mjs";
6
6
  //#region src/content/thread-module.ts
@@ -1,8 +1,8 @@
1
+ import { THREAD_ID_RE, changeConflicts, changeThread, newThread } from "../shared/threads-core.mjs";
1
2
  import { withLock } from "../server/file-lock.mjs";
2
3
  import { prepareState, studioState } from "../server/state.mjs";
3
4
  import { askForMerges } from "./merge-rules.mjs";
4
5
  import { listPrototypes, prototypePaths } from "./prototypes.mjs";
5
- import { THREAD_ID_RE, changeConflicts, changeThread, newThread } from "../shared/threads-core.mjs";
6
6
  import { changeEvent, eventLine, foldEvents, parseEvents, snapshotEvent, startEvent } from "./comments-log.mjs";
7
7
  import { reviewEvents } from "./review-import.mjs";
8
8
  import { existsSync } from "node:fs";
@@ -1,6 +1,6 @@
1
+ import { MAX_SIDE } from "../content/capture.mjs";
1
2
  import { isSnapshotClipboard, snapshotFrom } from "../shared/snapshot-clipboard.mjs";
2
3
  import { fontBringer } from "./fonts.mjs";
3
- import { MAX_SIDE } from "../content/capture.mjs";
4
4
  import { PASTED_IMAGE_TYPES } from "../shared/pasted-image.mjs";
5
5
  import { imageDataUrl, withImages } from "../shared/snapshot-images.mjs";
6
6
  import { isFigmaClipboard } from "./figma/decode.mjs";
package/dist/paths.d.mts CHANGED
@@ -23,8 +23,6 @@ export declare const STUDIO_REACT: {
23
23
  range: string;
24
24
  major: number;
25
25
  };
26
- /** The range the studio asks of a peer dependency: "^2.1.10". */
27
- export declare const studioPeer: (name: string) => string | undefined;
28
26
  /**
29
27
  * The highest major a version or range allows ("^18 || ^19" → 19), or
30
28
  * undefined when it names none ("catalog:", "latest").
package/dist/paths.mjs CHANGED
@@ -36,8 +36,6 @@ const STUDIO_REACT = (() => {
36
36
  major: Number(range.match(MAJOR_RE)?.[0])
37
37
  };
38
38
  })();
39
- /** The range the studio asks of a peer dependency: "^2.1.10". */
40
- const studioPeer = (name) => JSON.parse(readFileSync(join(PACKAGE_ROOT, "package.json"), "utf8")).peerDependencies?.[name];
41
39
  /**
42
40
  * The highest major a version or range allows ("^18 || ^19" → 19), or
43
41
  * undefined when it names none ("catalog:", "latest").
@@ -135,4 +133,4 @@ function installedDex(from, fallback) {
135
133
  }
136
134
  }
137
135
  //#endregion
138
- export { APP_DIR, EXTENSION_DIR, PACKAGE_NAME, PACKAGE_ROOT, STUDIO_REACT, TEMPLATES_DIR, compareVersions, highestMajor, installedBin, installedDex, isDirectory, isFile, isOlder, posix, readTemplate, runningVersion, shownFrom, studioPeer, studioVersion };
136
+ export { APP_DIR, EXTENSION_DIR, PACKAGE_NAME, PACKAGE_ROOT, STUDIO_REACT, TEMPLATES_DIR, compareVersions, highestMajor, installedBin, installedDex, isDirectory, isFile, isOlder, posix, readTemplate, runningVersion, shownFrom, studioVersion };
@@ -7,6 +7,10 @@ export declare const withoutAgentSession: (env: NodeJS.ProcessEnv) => NodeJS.Pro
7
7
  * profile sets the rest, as in any new window. However the terminal app was
8
8
  * started, even by an agent's session, nothing of that session reaches what
9
9
  * runs. A shell of the same kind stays open once `command` ends.
10
+ *
11
+ * `runtime`, the Node running Dex, goes first on that shell's PATH: a
12
+ * profile may not put Node there at all (nvm loaded only in some shells),
13
+ * and `dex`, its hooks and its MCP server all run on it.
10
14
  */
11
- export declare const inFreshShell: (command: string) => string;
15
+ export declare const inFreshShell: (command: string, runtime?: string) => string;
12
16
  //#endregion
@@ -1,3 +1,4 @@
1
+ import { dirname } from "node:path";
1
2
  //#region src/server/agent-env.ts
2
3
  const SESSION_MARKERS = [
3
4
  "CLAUDECODE",
@@ -37,10 +38,14 @@ const quoted = (value) => `'${value.replaceAll("'", "'\\''")}'`;
37
38
  * profile sets the rest, as in any new window. However the terminal app was
38
39
  * started, even by an agent's session, nothing of that session reaches what
39
40
  * runs. A shell of the same kind stays open once `command` ends.
41
+ *
42
+ * `runtime`, the Node running Dex, goes first on that shell's PATH: a
43
+ * profile may not put Node there at all (nvm loaded only in some shells),
44
+ * and `dex`, its hooks and its MCP server all run on it.
40
45
  */
41
- const inFreshShell = (command) => {
46
+ const inFreshShell = (command, runtime = process.execPath) => {
42
47
  const kept = KEPT_FOR_LOGIN.map((name) => `${name}="$${name}"`).join(" ");
43
- const then = `${command}; exec "$SHELL" -l`;
48
+ const then = `${`export PATH=${quoted(dirname(runtime))}:"$PATH"`}; ${command}; exec "$SHELL" -l`;
44
49
  return `exec /usr/bin/env -i ${kept} "$SHELL" -lic ${quoted(then)}`;
45
50
  };
46
51
  //#endregion
@@ -24,12 +24,18 @@ export declare const presenceTargetOf: (thread: ThreadWithPrototype) => Presence
24
24
  * acting on `threads`. Presence is best effort: a mark that fails never
25
25
  * fails the action.
26
26
  */
27
- export declare function openAgentSession(root: string, threads: ThreadModule, { agent, id, presence }: {
27
+ export declare function openAgentSession(root: string, threads: ThreadModule, { agent, claudeSession, id, presence }: {
28
28
  agent: () => string;
29
+ /**
30
+ * The Claude Code session it serves, when it does: Claude's hook marks
31
+ * that agent's edits under its id.
32
+ */
33
+ claudeSession?: string;
29
34
  id?: string;
30
35
  presence?: SessionPresence;
31
36
  }): {
32
37
  id: string;
38
+ marksUnder: string[];
33
39
  act: (threadId: string, action: UnsignedAction) => Promise<ThreadAnswer | undefined>;
34
40
  /**
35
41
  * Claim a thread for this session: it is acknowledged (👀), and the
@@ -44,6 +50,13 @@ export declare function openAgentSession(root: string, threads: ThreadModule, {
44
50
  adopt: (threadIds: string[]) => void;
45
51
  /** The threads the session holds now, for the server after this one. */
46
52
  holding: () => string[];
53
+ /**
54
+ * The agent waits for work (`watch`): holding none, it is at work
55
+ * nowhere, as when its turn ends, so nothing it marked shows. Holding
56
+ * some, its marks stay with them. A comment a person closed or answered
57
+ * while it held it is no longer its own, and counts for none.
58
+ */
59
+ waits: () => Promise<void>;
47
60
  /** Hear how many threads the session holds, each time that changes. */
48
61
  onHoldingChange: (listener: (count: number) => void) => void;
49
62
  /**
@@ -1,3 +1,4 @@
1
+ import { claimOf } from "../shared/threads-core.mjs";
1
2
  import { openPresence } from "./presence.mjs";
2
3
  import { randomUUID } from "node:crypto";
3
4
  //#region src/server/agent-session.ts
@@ -34,17 +35,19 @@ const presenceTargetOf = (thread) => {
34
35
  * acting on `threads`. Presence is best effort: a mark that fails never
35
36
  * fails the action.
36
37
  */
37
- function openAgentSession(root, threads, { agent, id = newSessionId(), presence = openPresence(root) }) {
38
- /** Once the thread is resolved or dismissed, clear this session's marks on its view. */
39
- const clearOnClose = async (action, thread) => {
40
- const where = presenceTargetOf(thread);
41
- if (!where) return;
42
- try {
43
- if (action.action === "resolve" || action.action === "dismiss") await presence.clear({
44
- ...where,
45
- session: id
46
- });
47
- } catch {}
38
+ function openAgentSession(root, threads, { agent, claudeSession, id = newSessionId(), presence = openPresence(root) }) {
39
+ /** Whom its marks are under: itself, and the Claude Code session it serves. */
40
+ const marksUnder = claudeSession && claudeSession !== id ? [id, claudeSession] : [id];
41
+ /**
42
+ * Clear its marks, its sub-agents' too, on one view or doc or everywhere.
43
+ * Presence only shows the work: a clear that fails changes nothing, and
44
+ * marks expire.
45
+ */
46
+ const clearMarks = async (where) => {
47
+ await Promise.allSettled(marksUnder.map((within) => presence.clear({
48
+ ...where,
49
+ within
50
+ })));
48
51
  };
49
52
  /** Threads this session claimed and has not yet closed or answered. */
50
53
  const held = /* @__PURE__ */ new Set();
@@ -58,6 +61,53 @@ function openAgentSession(root, threads, { agent, id = newSessionId(), presence
58
61
  };
59
62
  /** Given to another server (mcp-handover.ts): this one lets go of nothing. */
60
63
  let handedOver = false;
64
+ /** Where each thread it holds is on the canvas, once it has looked. */
65
+ const places = /* @__PURE__ */ new Map();
66
+ const placeOf = async (threadId) => {
67
+ if (!places.has(threadId)) {
68
+ const thread = await threads.find(threadId);
69
+ places.set(threadId, thread && presenceTargetOf(thread));
70
+ }
71
+ return places.get(threadId);
72
+ };
73
+ /**
74
+ * Let go of the threads it holds that a person closed or answered since
75
+ * it claimed them, which are no longer its own. One claimed meanwhile
76
+ * stays held.
77
+ */
78
+ const letGoOfClosed = async () => {
79
+ const ids = [...held];
80
+ if (ids.length === 0) return;
81
+ const own = new Set((await threads.list({ status: "acknowledged" })).filter((thread) => claimOf(thread)?.session === id).map((thread) => thread.id));
82
+ const closed = ids.filter((each) => !own.has(each));
83
+ if (closed.length > 0) holdingChange(() => {
84
+ for (const each of closed) {
85
+ held.delete(each);
86
+ places.delete(each);
87
+ }
88
+ });
89
+ };
90
+ /** Whether it holds a thread on `where`, as far as it knows. */
91
+ const holdsOn = async (where) => (await Promise.all([...held].map(placeOf))).some((on) => on?.slug === where.slug && on.target === where.target);
92
+ /**
93
+ * Its work on `thread` (or on nothing, waiting) is over: the thread's view
94
+ * shows nobody at work for it, unless it holds another there, and once
95
+ * it holds nothing, nothing it marked does (the file's shared code, other
96
+ * views it changed on the way). What it holds is looked at again first
97
+ * with `recheck`, or when another it holds would keep the view's marks,
98
+ * as a person may have closed it meanwhile. Handed over, its marks are
99
+ * the next server's.
100
+ */
101
+ const workEnded = async (thread, recheck) => {
102
+ if (handedOver) return;
103
+ const where = thread && presenceTargetOf(thread);
104
+ if (recheck || where && await holdsOn(where)) await letGoOfClosed().catch(() => void 0);
105
+ if (held.size === 0) {
106
+ await clearMarks();
107
+ return;
108
+ }
109
+ if (where && !await holdsOn(where)) await clearMarks(where);
110
+ };
61
111
  /**
62
112
  * An action on a thread, signed by this session: the thread after it, the
63
113
  * problems that stopped it, or undefined when there is no such thread.
@@ -68,12 +118,17 @@ function openAgentSession(root, threads, { agent, id = newSessionId(), presence
68
118
  author: agent(),
69
119
  session: id
70
120
  });
71
- if (answer?.thread) await clearOnClose(action, answer.thread);
72
- if (action.action === "release" || answer?.thread && LETS_GO.has(action.action)) holdingChange(() => held.delete(threadId));
121
+ if (action.action === "release" || Boolean(answer?.thread && LETS_GO.has(action.action))) {
122
+ const finished = held.has(threadId);
123
+ holdingChange(() => held.delete(threadId));
124
+ places.delete(threadId);
125
+ await workEnded(answer?.thread, finished).catch(() => void 0);
126
+ }
73
127
  return answer;
74
128
  };
75
129
  return {
76
130
  id,
131
+ marksUnder,
77
132
  act,
78
133
  /**
79
134
  * Claim a thread for this session: it is acknowledged (👀), and the
@@ -82,7 +137,10 @@ function openAgentSession(root, threads, { agent, id = newSessionId(), presence
82
137
  */
83
138
  claim: async (threadId) => {
84
139
  const answer = await act(threadId, { action: "acknowledge" });
85
- if (answer?.thread) holdingChange(() => held.add(threadId));
140
+ if (answer?.thread) {
141
+ places.set(threadId, presenceTargetOf(answer.thread));
142
+ holdingChange(() => held.add(threadId));
143
+ }
86
144
  return answer;
87
145
  },
88
146
  /**
@@ -96,6 +154,13 @@ function openAgentSession(root, threads, { agent, id = newSessionId(), presence
96
154
  },
97
155
  /** The threads the session holds now, for the server after this one. */
98
156
  holding: () => [...held],
157
+ /**
158
+ * The agent waits for work (`watch`): holding none, it is at work
159
+ * nowhere, as when its turn ends, so nothing it marked shows. Holding
160
+ * some, its marks stay with them. A comment a person closed or answered
161
+ * while it held it is no longer its own, and counts for none.
162
+ */
163
+ waits: () => workEnded(void 0, true).catch(() => void 0),
99
164
  /** Hear how many threads the session holds, each time that changes. */
100
165
  onHoldingChange: (listener) => {
101
166
  holdingListeners.add(listener);
@@ -108,7 +173,10 @@ function openAgentSession(root, threads, { agent, id = newSessionId(), presence
108
173
  handOver: (given) => {
109
174
  handedOver = true;
110
175
  holdingChange(() => {
111
- for (const threadId of given) held.delete(threadId);
176
+ for (const threadId of given) {
177
+ held.delete(threadId);
178
+ places.delete(threadId);
179
+ }
112
180
  });
113
181
  },
114
182
  /**
@@ -120,10 +188,7 @@ function openAgentSession(root, threads, { agent, id = newSessionId(), presence
120
188
  const unfinished = [...held];
121
189
  holdingChange(() => held.clear());
122
190
  await Promise.allSettled(unfinished.map((threadId) => act(threadId, { action: "release" })));
123
- if (handedOver) return;
124
- try {
125
- await presence.clear({ session: id });
126
- } catch {}
191
+ if (!handedOver) await clearMarks();
127
192
  }
128
193
  };
129
194
  }
@@ -1,6 +1,6 @@
1
1
  import { prototypePage } from "../shared/prototypes-core.mjs";
2
- import { listPrototypes } from "../content/prototypes.mjs";
3
2
  import { canvasFrames, kinded, viewFramePage } from "../shared/canvas-core.mjs";
3
+ import { listPrototypes } from "../content/prototypes.mjs";
4
4
  import { contentType } from "./content-type.mjs";
5
5
  import { readCanvas } from "../content/content.mjs";
6
6
  import { INSTALL_BROWSER, browserInstalled, closeScreenshotBrowser, pictureOfFrame } from "../content/screenshot.mjs";