@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
@@ -1,12 +1,14 @@
1
- // Where `dex dev` listens. When the project's port is taken it finds what
2
- // holds it: this project's own Dex (so there is nothing to do but open
3
- // it), another checkout's or project's, or some other process. Held by
4
- // anything but this project's Dex, it moves to the next free port, where
5
- // agents still find it (studio-for.ts). Asked for one port (STUDIO_PORT, as
6
- // the installed app asks for the address it opens), it keeps to that one
7
- // and stops. cli/banner.ts says which.
1
+ // Where `dex dev` listens. When the project's port is taken, on either
2
+ // address family, it finds what holds it: this project's own Dex (so
3
+ // there is nothing to do but open it), another checkout's or project's,
4
+ // or some other process. Held by anything but this project's Dex, it
5
+ // moves to the next free port, where agents still find it
6
+ // (studio-for.ts). Asked for one port (STUDIO_PORT, as the installed app
7
+ // asks for the address it opens), it keeps to that one and stops.
8
+ // cli/banner.ts says which.
8
9
 
9
10
  import { execFile } from "node:child_process";
11
+ import { connect } from "node:net";
10
12
  import { promisify } from "node:util";
11
13
  import type { ViteDevServer } from "vite";
12
14
  import {
@@ -20,6 +22,43 @@ const IN_USE_RE = /already in use/;
20
22
  const ASK_TIMEOUT_MS = 1000;
21
23
  const run = promisify(execFile);
22
24
 
25
+ /** Where `localhost` may reach a port: either family. */
26
+ const LOCALHOST_ADDRESSES = ["127.0.0.1", "::1"] as const;
27
+ /**
28
+ * How long a connection on loopback may take: one is made at once, but
29
+ * Windows takes seconds to refuse one.
30
+ */
31
+ const CONNECT_TIMEOUT_MS = 250;
32
+
33
+ /** Whether anything accepts a connection on `port` at `host`. */
34
+ const accepts = (port: number, host: string): Promise<boolean> =>
35
+ new Promise((answer) => {
36
+ const socket = connect({ port, host });
37
+ const done = (listens: boolean) => {
38
+ socket.destroy();
39
+ answer(listens);
40
+ };
41
+ socket.once("connect", () => done(true));
42
+ socket.once("error", () => done(false));
43
+ // No answer in time counts as none, a turn later: after this process
44
+ // was busy, the timer runs before a connection already made is heard.
45
+ socket.setTimeout(CONNECT_TIMEOUT_MS, () =>
46
+ setImmediate(() => done(false))
47
+ );
48
+ });
49
+
50
+ /**
51
+ * Whether anything listens on `port` where `localhost` may reach it, on
52
+ * either address family.
53
+ */
54
+ export async function heldOnLocalhost(port: number): Promise<boolean> {
55
+ const held = await Promise.all(
56
+ // A port out of range is no one's: Vite's listen says it is wrong.
57
+ LOCALHOST_ADDRESSES.map((host) => accepts(port, host).catch(() => false))
58
+ );
59
+ return held.includes(true);
60
+ }
61
+
23
62
  /** A web app answering on a port, as its manifest says. */
24
63
  interface AppOnPort {
25
64
  /** Its project, as a Dex names it in PROJECT_ID_HEADER. */
@@ -80,6 +119,9 @@ export async function lsofHolder(
80
119
  }
81
120
  }
82
121
 
122
+ /** A Dex on a port that isn't this project's, as a person is told of it. */
123
+ export const OTHER_DEX = "another checkout's Dex";
124
+
83
125
  /** What holds a port `dex dev` wanted. */
84
126
  export interface PortTaken {
85
127
  /**
@@ -133,9 +175,47 @@ export interface ListenOptions {
133
175
  root?: string;
134
176
  }
135
177
 
178
+ /**
179
+ * The port `server` is served on, once it listens. listenOnPort may have
180
+ * moved it off the configured one.
181
+ */
182
+ export function servedPort(server: ViteDevServer): number {
183
+ const address = server.httpServer?.address();
184
+ return typeof address === "object" && address
185
+ ? address.port
186
+ : server.config.server.port;
187
+ }
188
+
136
189
  /** How many ports past a taken one `dex dev` tries. */
137
190
  export const MOVE_TRIES = 20;
138
191
 
192
+ /**
193
+ * Listen on `port`: false when anything holds it. Vite's listen fails
194
+ * only for a holder of the very address it takes (the one `localhost`
195
+ * resolves to first, or STUDIO_HOST's). Yet a browser, and Dex itself,
196
+ * reach the studio at `localhost`, where a holder on the other family,
197
+ * or (on macOS) on a narrower or wider address, would answer instead:
198
+ * that is taken too. Any other failure closes the server and throws.
199
+ */
200
+ async function listenIfFree(
201
+ server: ViteDevServer,
202
+ port: number
203
+ ): Promise<boolean> {
204
+ if (await heldOnLocalhost(port)) {
205
+ return false;
206
+ }
207
+ try {
208
+ await server.listen(port);
209
+ return true;
210
+ } catch (error) {
211
+ if (error instanceof Error && IN_USE_RE.test(error.message)) {
212
+ return false;
213
+ }
214
+ await server.close();
215
+ throw error;
216
+ }
217
+ }
218
+
139
219
  /**
140
220
  * Listen on the configured port, or, while anything but this project's
141
221
  * Dex holds it, on the next free one: answers what held the port it moved
@@ -151,8 +231,7 @@ export async function listenOnPort(
151
231
  let movedFrom: PortTaken | undefined;
152
232
  for (let port = wanted; port < wanted + MOVE_TRIES; port += 1) {
153
233
  server.config.server.port = port;
154
- try {
155
- await server.listen(port);
234
+ if (await listenIfFree(server, port)) {
156
235
  // Vite restarts itself from its inline config (a config change):
157
236
  // there too, the port it took, never the taken one.
158
237
  server.config.inlineConfig.server = {
@@ -160,18 +239,13 @@ export async function listenOnPort(
160
239
  port,
161
240
  };
162
241
  return movedFrom;
163
- } catch (error) {
164
- if (!(error instanceof Error && IN_USE_RE.test(error.message))) {
165
- await server.close();
166
- throw error;
167
- }
168
- const taken = await whatHolds(port, id, holderOf);
169
- if (keepPort || taken.sameProject) {
170
- await server.close();
171
- throw new PortInUseError(taken);
172
- }
173
- movedFrom ??= taken;
174
242
  }
243
+ const taken = await whatHolds(port, id, holderOf);
244
+ if (keepPort || taken.sameProject) {
245
+ await server.close();
246
+ throw new PortInUseError(taken);
247
+ }
248
+ movedFrom ??= taken;
175
249
  }
176
250
  await server.close();
177
251
  throw new PortInUseError(movedFrom as PortTaken);
@@ -17,6 +17,7 @@ import {
17
17
  freshPresence,
18
18
  type PresenceEntry,
19
19
  type PresenceTarget,
20
+ underSession,
20
21
  } from "../shared/presence-core.ts";
21
22
  import { withLock } from "./file-lock.ts";
22
23
  import { legacyState, prepareState, studioState } from "./state.ts";
@@ -24,7 +25,10 @@ import { legacyState, prepareState, studioState } from "./state.ts";
24
25
  /** What to clear entries by: any field left out matches every entry. */
25
26
  export type PresenceMatch = Partial<
26
27
  Pick<PresenceEntry, "agent" | "session" | "slug" | "target">
27
- >;
28
+ > & {
29
+ /** A session: its own entries and its sub-agents' (workerSession). */
30
+ within?: string;
31
+ };
28
32
 
29
33
  /** A View, Doc or Prototype marked as worked on, and by whom. */
30
34
  export interface PresenceMark extends PresenceTarget {
@@ -47,8 +51,12 @@ export const isTeammate = fromRoom;
47
51
  const sameSlot = (a: PresenceEntry, b: PresenceEntry): boolean =>
48
52
  a.slug === b.slug && a.target === b.target && a.session === b.session;
49
53
 
50
- const matches = (entry: PresenceEntry, match: PresenceMatch): boolean =>
51
- Object.entries(match).every(
54
+ const matches = (
55
+ entry: PresenceEntry,
56
+ { within, ...fields }: PresenceMatch
57
+ ): boolean =>
58
+ (within === undefined || underSession(entry.session, within)) &&
59
+ Object.entries(fields).every(
52
60
  ([key, value]) =>
53
61
  value === undefined || entry[key as keyof PresenceEntry] === value
54
62
  );
@@ -16,8 +16,6 @@ import {
16
16
  saveCommand,
17
17
  } from "../content/commands.ts";
18
18
  import {
19
- type Canvas,
20
- type Contributor,
21
19
  createPrototype,
22
20
  createUntitledPrototype,
23
21
  currentPerson,
@@ -59,6 +57,7 @@ import {
59
57
  } from "../content/screenshot.ts";
60
58
  import type { SnapshotBody } from "../content/snapshot-client.ts";
61
59
  import { editText } from "../content/text-edit.ts";
60
+ import { replaceText, undoReplace } from "../content/text-replace.ts";
62
61
  import type { ThreadModule } from "../content/thread-module.ts";
63
62
  import {
64
63
  dismissThreadsOn,
@@ -89,6 +88,7 @@ import {
89
88
  type BuildSettings,
90
89
  } from "../shared/build-settings.ts";
91
90
  import type { Cell } from "../shared/canvas-core.ts";
91
+ import type { Canvas } from "../shared/canvas-file.ts";
92
92
  import {
93
93
  COMMAND_SWITCHES,
94
94
  type CommandsAnswer,
@@ -126,8 +126,10 @@ import {
126
126
  studioEvent,
127
127
  } from "../shared/studio-events.ts";
128
128
  import {
129
+ type Contributor,
129
130
  guidelinesPerson,
130
131
  isEditableMarkdown,
132
+ STUDIO_FILES,
131
133
  } from "../shared/studio-files.ts";
132
134
  import {
133
135
  type DesktopAppAnswer,
@@ -172,6 +174,7 @@ import {
172
174
  skillFolders,
173
175
  writeCommandMenu,
174
176
  } from "./command-settings.ts";
177
+ import { COMMENT_HANDLERS } from "./comments/routes.ts";
175
178
  import { type Feature, features } from "./features.ts";
176
179
  import {
177
180
  branchPrototypes,
@@ -466,7 +469,8 @@ type StudioPages = ReturnType<typeof studioPages>;
466
469
 
467
470
  interface RouteContext {
468
471
  body: RequestBody | undefined;
469
- desktop: DesktopAppHost;
472
+ /** Where this studio can make its app, asked as a desktop route answers. */
473
+ desktop: () => DesktopAppHost;
470
474
  events: EventHub;
471
475
  format: Formatter;
472
476
  launcher: AgentLauncher;
@@ -573,26 +577,31 @@ const emptyTheTrash = async ({ project }: RouteContext) => {
573
577
  return {};
574
578
  };
575
579
 
576
- const listConnectedAgents = async ({ project }: RouteContext) => ({
577
- agents: await connectedAgents(project.content),
580
+ const listConnectedAgents = async ({ project, threads }: RouteContext) => ({
581
+ agents: await connectedAgents(project.content, { threads }),
578
582
  });
579
583
 
580
584
  /** Only on this machine, where that process runs. */
581
- const stopOldAgent = async ({ project, body, request }: RouteContext) => {
585
+ const stopOldAgent = async ({
586
+ project,
587
+ body,
588
+ request,
589
+ threads,
590
+ }: RouteContext) => {
582
591
  onlyFromThisMachine(request, "Stop agents from the machine Dex runs on.");
583
592
  const pid = body?.pid;
584
593
  if (!(typeof pid === "number" && Number.isInteger(pid))) {
585
594
  throw new Problems(422, ["Say which agent to stop, by its process."]);
586
595
  }
587
596
  try {
588
- await stopAgentServer(project.content, pid);
597
+ await stopAgentServer(project.content, pid, threads);
589
598
  } catch (error) {
590
599
  if (error instanceof NotAnOldSession) {
591
600
  throw new Problems(409, [error.message]);
592
601
  }
593
602
  throw error;
594
603
  }
595
- return { agents: await connectedAgents(project.content) };
604
+ return { agents: await connectedAgents(project.content, { threads }) };
596
605
  };
597
606
 
598
607
  const listAgentSetups = async ({ project }: RouteContext) => ({
@@ -838,19 +847,20 @@ const desktopAnswer = async (
838
847
 
839
848
  const loadDesktopApp = ({ request, desktop }: RouteContext) => {
840
849
  fromThisMac(request, false);
841
- return desktopAnswer(desktop);
850
+ return desktopAnswer(desktop());
842
851
  };
843
852
 
844
853
  async function changeDesktopApp({ request, desktop, body }: RouteContext) {
845
854
  fromThisMac(request, true);
846
- if ("unavailable" in desktop) {
855
+ const host = desktop();
856
+ if ("unavailable" in host) {
847
857
  throw new Problems(409, [
848
- desktop.unavailable === "hosted"
858
+ host.unavailable === "hosted"
849
859
  ? "A hosted studio can't install an app."
850
860
  : "Installing outside the browser is macOS-only for now.",
851
861
  ]);
852
862
  }
853
- const { app, home, reveal, open, launchServices } = desktop;
863
+ const { app, home, reveal, open, launchServices } = host;
854
864
  switch (body?.action) {
855
865
  case "install": {
856
866
  const path = await refusable(() =>
@@ -874,7 +884,7 @@ async function changeDesktopApp({ request, desktop, body }: RouteContext) {
874
884
  'Send an action: "install", "open" or "remove".',
875
885
  ]);
876
886
  }
877
- return desktopAnswer(desktop);
887
+ return desktopAnswer(host);
878
888
  }
879
889
 
880
890
  const readWatch = async ({ project }: RouteContext) => ({
@@ -1026,6 +1036,35 @@ async function changeText({ project, body }: RouteContext) {
1026
1036
  return result;
1027
1037
  }
1028
1038
 
1039
+ /**
1040
+ * Find and replace on the canvas: { find, to, seen } changes the words in
1041
+ * the person's own folder of the file, where the frames showed them;
1042
+ * { undo } puts a replacement back.
1043
+ */
1044
+ async function replaceOnCanvas({ project, body }: RouteContext) {
1045
+ const { folder } = prototypeNamed(project.content, body ?? {});
1046
+ const own = join(
1047
+ folder,
1048
+ STUDIO_FILES.people,
1049
+ await whoChanges(project.content)
1050
+ );
1051
+ if (typeof body?.undo === "string") {
1052
+ return await undoReplace(own, body.undo);
1053
+ }
1054
+ const { find, to, seen } = body ?? {};
1055
+ if (
1056
+ !(typeof find === "string" && typeof to === "string" && Array.isArray(seen))
1057
+ ) {
1058
+ throw new Problems(400, ["Say what to find, and what to change it to."]);
1059
+ }
1060
+ return await replaceText(
1061
+ own,
1062
+ find,
1063
+ to,
1064
+ seen.filter((text): text is string => typeof text === "string")
1065
+ );
1066
+ }
1067
+
1029
1068
  const needsFeature = (name: Feature) => {
1030
1069
  if (!features()[name]) {
1031
1070
  throw new Problems(404, ["That is not on in this studio."]);
@@ -1204,60 +1243,6 @@ async function pictureOfView({
1204
1243
  const threadsPrototype = (content: string, url: URL): string =>
1205
1244
  prototypeOf(content, url).slug;
1206
1245
 
1207
- const listThreads = ({ project, url, threads }: RouteContext) =>
1208
- url.searchParams.has("slug")
1209
- ? threads.list({ prototype: threadsPrototype(project.content, url) })
1210
- : threads.list();
1211
-
1212
- /** A request for one prototype never changes another's thread. */
1213
- const isThreadOf = async (
1214
- threads: ThreadModule,
1215
- id: string,
1216
- prototype: string
1217
- ): Promise<boolean> => {
1218
- const thread = await threads.find(id);
1219
- return thread !== undefined && thread.slug === prototype;
1220
- };
1221
-
1222
- async function postThread({ project, url, body, threads }: RouteContext) {
1223
- const prototype = threadsPrototype(project.content, url);
1224
- if (!body) {
1225
- throw new Problems(400, ["Send a thread to start, or a change to one."]);
1226
- }
1227
- // Who they are is the studio's to say, never the page's: a message is its
1228
- // author's to edit by their slug, where names can be shared.
1229
- const { name: author, slug: authorId } = await readAuthor(project.content);
1230
- // The page's fields go to the thread module as they came: it finds no
1231
- // thread for an id it never gave, and checks a new thread
1232
- // (threadProblems) and a change (changeProblems) before keeping either.
1233
- const id = body.id as string | undefined;
1234
- if (id && !(await isThreadOf(threads, id, prototype))) {
1235
- throw new Problems(404, ["That thread is gone."]);
1236
- }
1237
- const result = id
1238
- ? await threads.respond(id, {
1239
- ...body,
1240
- author,
1241
- authorId,
1242
- } as Parameters<ThreadModule["respond"]>[1])
1243
- : await threads.start({
1244
- prototype,
1245
- target: body.target,
1246
- body: body.body,
1247
- for: body.for,
1248
- sketch: body.sketch,
1249
- author,
1250
- authorId,
1251
- } as Parameters<ThreadModule["start"]>[0]);
1252
- if (!result) {
1253
- throw new Problems(404, ["That thread is gone."]);
1254
- }
1255
- if (result.problems) {
1256
- throw new Problems(422, result.problems);
1257
- }
1258
- return result.thread;
1259
- }
1260
-
1261
1246
  const REVIEW_FILE_PROBLEMS: Record<ReviewFileProblem, string> = {
1262
1247
  "not-a-review": "This is not a Dex review.",
1263
1248
  newer: "This review is from a newer Dex: update to import it.",
@@ -2202,6 +2187,7 @@ type DeclaredRoute<Name extends StudioRouteName> =
2202
2187
  | ({ prototype?: false } & DeclaredMethods<RouteContext, Name>);
2203
2188
 
2204
2189
  const ROUTES: { [Name in StudioRouteName]: DeclaredRoute<Name> } = {
2190
+ ...COMMENT_HANDLERS,
2205
2191
  canvas: { prototype: true, GET: loadCanvas, PUT: saveCanvas },
2206
2192
  annotations: {
2207
2193
  prototype: true,
@@ -2219,7 +2205,6 @@ const ROUTES: { [Name in StudioRouteName]: DeclaredRoute<Name> } = {
2219
2205
  POST: trashRemovedViews,
2220
2206
  },
2221
2207
  trash: { DELETE: emptyTheTrash },
2222
- threads: { GET: listThreads, POST: postThread },
2223
2208
  reviews: { limit: MAX_REVIEW_BYTES, POST: importReviews },
2224
2209
  "spec-suggestion": { POST: acceptSpecSuggestion },
2225
2210
  instructions: { GET: loadInstructions, PUT: saveInstructions },
@@ -2233,6 +2218,7 @@ const ROUTES: { [Name in StudioRouteName]: DeclaredRoute<Name> } = {
2233
2218
  frame: { POST: addBlankFrame },
2234
2219
  show: { POST: showOnCanvas },
2235
2220
  text: { POST: changeText },
2221
+ replace: { POST: replaceOnCanvas },
2236
2222
  props: { POST: changeProps },
2237
2223
  insert: { POST: insertIntoView },
2238
2224
  screenshot: { POST: screenshotView },
@@ -2308,7 +2294,11 @@ async function answer(
2308
2294
  export interface StudioRequestOptions {
2309
2295
  agents?: AgentTracker;
2310
2296
  base?: string;
2311
- desktop?: DesktopAppHost;
2297
+ /**
2298
+ * Where the studio can make its app, asked each time: the dev server's
2299
+ * port is known only once it listens.
2300
+ */
2301
+ desktop?: () => DesktopAppHost;
2312
2302
  events: EventHub;
2313
2303
  format?: Formatter;
2314
2304
  launcher?: AgentLauncher;
@@ -2333,7 +2323,7 @@ export function studioRequests({
2333
2323
  agents = trackAgents(project.content, { threads }),
2334
2324
  server = {},
2335
2325
  base = "/",
2336
- desktop = { unavailable: "platform" },
2326
+ desktop = () => ({ unavailable: "platform" }),
2337
2327
  }: StudioRequestOptions): (
2338
2328
  request: IncomingMessage,
2339
2329
  response: ServerResponse
@@ -10,7 +10,13 @@
10
10
 
11
11
  import { RESTART_PATH } from "../shared/server-page.ts";
12
12
  import type { RestartStatus } from "../shared/update-core.ts";
13
- import { answersAs, lsofHolder, type PortTaken, whatHolds } from "./port.ts";
13
+ import {
14
+ answersAs,
15
+ lsofHolder,
16
+ OTHER_DEX,
17
+ type PortTaken,
18
+ whatHolds,
19
+ } from "./port.ts";
14
20
  import { projectId } from "./project-id.ts";
15
21
  import { type RosterEntry, readRoster } from "./roster.ts";
16
22
 
@@ -71,7 +77,7 @@ async function restartFailed(port: number): Promise<boolean> {
71
77
  /** What holds a port, as an agent is told: "another checkout's Dex". */
72
78
  function holderName({ by, pid }: PortTaken): string {
73
79
  if (by === "Dex") {
74
- return "another checkout's Dex";
80
+ return OTHER_DEX;
75
81
  }
76
82
  return pid ? `${by} (pid ${pid})` : String(by);
77
83
  }
@@ -136,6 +142,24 @@ export async function studioFor(
136
142
  };
137
143
  }
138
144
 
145
+ /**
146
+ * The port this checkout's Dex is on, also while it restarts there, or
147
+ * undefined when none is: what the app opens, and where it says to look.
148
+ */
149
+ export function ownPort(found: StudioFor): number | undefined {
150
+ switch (found.kind) {
151
+ case "running":
152
+ case "starting":
153
+ case "failed":
154
+ return found.port;
155
+ case "none":
156
+ case "elsewhere":
157
+ return;
158
+ default:
159
+ return found satisfies never;
160
+ }
161
+ }
162
+
139
163
  const START =
140
164
  "Start it with `npx dex dev` (or the project's own script, such as `npm run dex`), then try again.";
141
165
 
@@ -30,49 +30,58 @@ export interface SyncPlace {
30
30
  * work is in by then. Why Sync stopped comes back with where the machine
31
31
  * stands, rather than thrown. Run it inside the repository's turn.
32
32
  */
33
- export async function syncAndUpdate(
33
+ export const syncAndUpdate = (
34
34
  {
35
- content,
36
35
  keep,
37
36
  message,
38
- root,
39
- settings,
37
+ ...place
40
38
  }: SyncPlace & {
41
39
  keep?: SyncKeep;
42
40
  /** The commit's message, in place of one written from the changes. */
43
41
  message?: string;
44
42
  },
45
- { run = runStep, running }: { run?: StepRunner; running?: string } = {}
46
- ): Promise<{ machine: MachineState; stopped?: GitError }> {
47
- const before = await lockfilePrint(content);
48
- let stopped: GitError | undefined;
49
- try {
50
- await syncWork(content, settings, { keep, message, run });
51
- } catch (error) {
52
- if (!(error instanceof GitError)) {
53
- throw error;
54
- }
55
- stopped = error;
56
- }
57
- const machine = await updateMachine(
58
- { content, root },
59
- { before, run, ...(running ? { running } : {}) }
43
+ steps: UpdateSteps = {}
44
+ ): Promise<Updated> =>
45
+ thenUpdate(
46
+ place,
47
+ (run) => syncWork(place.content, place.settings, { keep, message, run }),
48
+ steps
60
49
  );
61
- return { machine, ...(stopped ? { stopped } : {}) };
62
- }
63
50
 
64
51
  /**
65
52
  * Pull from base, then bring the machine up to date with what it brought
66
53
  * in, as syncAndUpdate does for Sync. Run it inside the repository's turn.
67
54
  */
68
- export async function pullBaseAndUpdate(
69
- { content, keep, root, settings }: SyncPlace & { keep?: SyncKeep },
70
- { run = runStep, running }: { run?: StepRunner; running?: string } = {}
71
- ): Promise<{ machine: MachineState; stopped?: GitError }> {
55
+ export const pullBaseAndUpdate = (
56
+ { keep, ...place }: SyncPlace & { keep?: SyncKeep },
57
+ steps: UpdateSteps = {}
58
+ ): Promise<Updated> =>
59
+ thenUpdate(
60
+ place,
61
+ (run) => bringInBase(place.content, place.settings, { keep, run }),
62
+ steps
63
+ );
64
+
65
+ interface UpdateSteps {
66
+ run?: StepRunner;
67
+ running?: string;
68
+ }
69
+
70
+ interface Updated {
71
+ machine: MachineState;
72
+ stopped?: GitError;
73
+ }
74
+
75
+ /** Bring in the team's work, then update the machine, even if git stopped. */
76
+ async function thenUpdate(
77
+ { content, root }: SyncPlace,
78
+ bringIn: (run: StepRunner) => Promise<unknown>,
79
+ { run = runStep, running }: UpdateSteps
80
+ ): Promise<Updated> {
72
81
  const before = await lockfilePrint(content);
73
82
  let stopped: GitError | undefined;
74
83
  try {
75
- await bringInBase(content, settings, { keep, run });
84
+ await bringIn(run);
76
85
  } catch (error) {
77
86
  if (!(error instanceof GitError)) {
78
87
  throw error;
@@ -4,12 +4,10 @@
4
4
  // `close` says where a change landed.
5
5
 
6
6
  import { join } from "node:path";
7
- import {
8
- prototypeFrames,
9
- type ViewFrame,
10
- } from "../content/prototype-summary.ts";
7
+ import { prototypeFrames } from "../content/prototype-summary.ts";
11
8
  import { readText } from "../content/read-text.ts";
12
9
  import { isSpecPath, kinded } from "../shared/canvas-core.ts";
10
+ import type { ViewFrame } from "../shared/canvas-file.ts";
13
11
  import { prototypeFiles } from "../shared/prototypes-core.ts";
14
12
  import { quoteAt, resolveQuote } from "../shared/spec-quote.ts";
15
13
  import type { ChangePlace, ThreadTarget } from "../shared/threads-core.ts";
@@ -15,7 +15,8 @@ import {
15
15
  type FoundComponent,
16
16
  findComponents,
17
17
  } from "../component-catalogue.ts";
18
- import { whereThePersonIs } from "../open-page.ts";
18
+ import { atWorkOn, whereThePersonIs } from "../open-page.ts";
19
+ import { openPresence } from "../presence.ts";
19
20
  import { briefSummary } from "../prototype-brief.ts";
20
21
  import {
21
22
  cutText,
@@ -96,6 +97,7 @@ export function registerReadingTools(
96
97
  okAboutCode,
97
98
  refuse,
98
99
  root,
100
+ session,
99
101
  threads,
100
102
  withNotice,
101
103
  } = context;
@@ -263,9 +265,20 @@ export function registerReadingTools(
263
265
  annotations: { readOnlyHint: true },
264
266
  title: "Where the person is",
265
267
  description:
266
- 'Where the person is in Dex now: the file they have open (`prototype`, `fileTitle`), the `page` of it ("canvas"; "play", a section shown a frame at a time; "view", a view on a page of its own; or "file", its own page), the sections of the canvas in view (`flows`; none when they look at the canvas as a whole), the `frame` selected or playing, and when Dex last said so (`seen`, `ago`). `open` is false with a note when Dex is no longer on screen, or nobody has it open. Ask before starting on feedback, and again when the person says "this" or "here": work on what they are looking at first.',
268
+ 'Where the person is in Dex now: the file they have open (`prototype`, `fileTitle`), the `page` of it ("canvas"; "play", a section shown a frame at a time; "view", a view on a page of its own; or "file", its own page), the sections of the canvas in view (`flows`; none when they look at the canvas as a whole), the `frame` selected or playing, and when Dex last said so (`seen`, `ago`). `open` is false with a note when Dex is no longer on screen, or nobody has it open. `atWork` is who the canvas shows at work on the file now, the view drawn in blocks: on which view or doc (`prototype` is its shared code, so every view), whose mark it is (`you`: yours end as you close or let go of your comment, or wait in `watch` with none), when it was last marked, and when it `ends` at the latest. Ask before starting on feedback, and again when the person says "this" or "here": work on what they are looking at first.',
267
269
  inputSchema: {},
268
270
  },
269
- async () => ok(await whereThePersonIs(root))
271
+ async () => {
272
+ const where = await whereThePersonIs(root);
273
+ if (!("prototype" in where)) {
274
+ return ok(where);
275
+ }
276
+ const atWork = atWorkOn(
277
+ await openPresence(root).list(),
278
+ where.prototype,
279
+ session.marksUnder
280
+ );
281
+ return ok(atWork.length > 0 ? { ...where, atWork } : where);
282
+ }
270
283
  );
271
284
  }
@@ -7,6 +7,7 @@ import { z } from "zod";
7
7
  import type { ThreadFilter } from "../../content/thread-module.ts";
8
8
  import type { ThreadWithPrototype } from "../../shared/threads-core.ts";
9
9
  import { FEEDBACK_WORKFLOW, isWatching } from "../channel.ts";
10
+ import { claimComment } from "../comments/actions.ts";
10
11
  import { yoursGroups } from "../own-copies.ts";
11
12
  import { LOOP_HOW, WATCH_HOW } from "../push-notice.ts";
12
13
  import { plural, RESPONSE_BUDGETS, sizeOf } from "../response-budget.ts";
@@ -86,15 +87,12 @@ const groupsWith = (
86
87
  * Claim each thread for this session, in turn: the ones it won, as they
87
88
  * are after. One another session holds, or gone, is left out.
88
89
  */
89
- const claimAll = async (
90
- { session }: ToolContext,
91
- found: ThreadWithPrototype[]
92
- ) => {
90
+ const claimAll = async (context: ToolContext, found: ThreadWithPrototype[]) => {
93
91
  const claimed: ThreadWithPrototype[] = [];
94
92
  for (const thread of found) {
95
- const result = await session.claim(thread.id);
96
- if (result?.thread) {
97
- claimed.push(result.thread);
93
+ const result = await claimComment(context, thread.id);
94
+ if (result.value) {
95
+ claimed.push(result.value);
98
96
  }
99
97
  }
100
98
  return claimed;
@@ -342,6 +340,8 @@ export function registerWatchTool(
342
340
  if (ids) {
343
341
  return ok(await takeThreads(ids));
344
342
  }
343
+ // Waiting, an agent with nothing in hand is at work nowhere.
344
+ await context.session.waits();
345
345
  const filter: ThreadFilter = { prototype, status: "waiting" };
346
346
  let watching = true;
347
347
  // What is claimed during the call goes back, even what was claimed as