@dexhq/canvas 0.17.0 → 0.18.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 (174) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/app/canvas/canvas-commands.ts +2 -10
  3. package/app/canvas/canvas-context.tsx +11 -2
  4. package/app/canvas/canvas-document.ts +5 -1
  5. package/app/canvas/canvas-keys.ts +1 -1
  6. package/app/canvas/canvas-model.ts +195 -20
  7. package/app/canvas/canvas-scene.ts +176 -171
  8. package/app/canvas/canvas-ui.ts +4 -11
  9. package/app/canvas/canvas.css +159 -179
  10. package/app/canvas/change-pictures.tsx +156 -0
  11. package/app/canvas/comment-drafts.ts +31 -0
  12. package/app/canvas/comment-layer.tsx +125 -71
  13. package/app/canvas/comments.tsx +38 -2
  14. package/app/canvas/element-picker.ts +20 -0
  15. package/app/canvas/engine/canvas-gestures.ts +15 -3
  16. package/app/canvas/engine/canvas-store.ts +5 -0
  17. package/app/canvas/engine/canvas-view.tsx +22 -5
  18. package/app/canvas/find-bar.tsx +57 -41
  19. package/app/canvas/flow-canvas.tsx +147 -272
  20. package/app/canvas/frame-copies.ts +14 -17
  21. package/app/canvas/frame-drops.ts +15 -7
  22. package/app/canvas/frame-inspector.ts +192 -41
  23. package/app/canvas/frame-node.tsx +123 -75
  24. package/app/canvas/frame-resize.tsx +321 -83
  25. package/app/canvas/insert-hint.tsx +5 -15
  26. package/app/canvas/inspect-layer.tsx +73 -19
  27. package/app/canvas/label-action.tsx +13 -2
  28. package/app/canvas/layout.ts +233 -98
  29. package/app/canvas/live-page.ts +5 -0
  30. package/app/canvas/new-frame.ts +10 -25
  31. package/app/canvas/note-placing.ts +55 -84
  32. package/app/canvas/picked-box.tsx +5 -0
  33. package/app/canvas/picks-menu.tsx +3 -9
  34. package/app/canvas/readable-colour.ts +52 -0
  35. package/app/canvas/request-node.tsx +19 -10
  36. package/app/canvas/scrolling-page.ts +45 -0
  37. package/app/canvas/section-node.tsx +36 -53
  38. package/app/canvas/settle.ts +21 -0
  39. package/app/canvas/snapping.ts +96 -8
  40. package/app/canvas/spec-frame.tsx +8 -5
  41. package/app/canvas/spot-pins.tsx +15 -3
  42. package/app/canvas/stack-frame.tsx +13 -57
  43. package/app/canvas/stack-model.ts +5 -0
  44. package/app/canvas/stack-toggle.tsx +89 -0
  45. package/app/canvas/styles-layer.tsx +23 -43
  46. package/app/canvas/text-layer.tsx +26 -10
  47. package/app/canvas/thread-focus.tsx +18 -1
  48. package/app/canvas/tool-cursors.ts +2 -2
  49. package/app/canvas/toolbar-parts.tsx +48 -8
  50. package/app/canvas/toolbar.tsx +12 -10
  51. package/app/canvas/use-canvas-clipboard.tsx +13 -5
  52. package/app/canvas/use-reveal.ts +86 -0
  53. package/app/editor/extensions.ts +2 -0
  54. package/app/editor/line-break-caret.ts +57 -0
  55. package/app/git/branch-panel.tsx +61 -20
  56. package/app/i18n/locales/en/messages.po +64 -17
  57. package/app/i18n/locales/en/messages.ts +1 -1
  58. package/app/i18n/locales/pseudo/messages.po +64 -17
  59. package/app/i18n/locales/pseudo/messages.ts +1 -1
  60. package/app/icons/glyphs.tsx +2 -0
  61. package/app/live/studio-live.ts +29 -9
  62. package/app/pages/canvas.tsx +1 -1
  63. package/app/pages/commands-settings.tsx +70 -76
  64. package/app/pages/play-bar.tsx +6 -11
  65. package/app/pages/play.tsx +52 -33
  66. package/app/shell/feedback-form.tsx +29 -5
  67. package/app/ui/prompt-editor.tsx +2 -0
  68. package/app/ui/use-field-list.ts +90 -0
  69. package/dist/cli/commands/mcp.mjs +6 -4
  70. package/dist/cli/mcp-relay.mjs +10 -6
  71. package/dist/content/content.d.mts +26 -9
  72. package/dist/content/content.mjs +65 -8
  73. package/dist/content/screenshot.d.mts +2 -0
  74. package/dist/content/screenshot.mjs +3 -0
  75. package/dist/content/snapshot-client.d.mts +2 -0
  76. package/dist/content/snapshot-client.mjs +12 -1
  77. package/dist/inspector.js +96 -19
  78. package/dist/paste/paste.d.mts +5 -0
  79. package/dist/paste/paste.mjs +7 -2
  80. package/dist/server/agent-tools.d.mts +1 -0
  81. package/dist/server/agent-tools.mjs +4 -3
  82. package/dist/server/change-picture-store.d.mts +13 -0
  83. package/dist/server/change-picture-store.mjs +42 -0
  84. package/dist/server/change-pictures.d.mts +25 -0
  85. package/dist/server/change-pictures.mjs +104 -0
  86. package/dist/server/close-check.d.mts +11 -0
  87. package/dist/server/close-check.mjs +37 -5
  88. package/dist/server/comments/agent-actions.d.mts +8 -4
  89. package/dist/server/comments/agent-actions.mjs +16 -7
  90. package/dist/server/comments/close-tools.mjs +7 -5
  91. package/dist/server/comments/files.d.mts +5 -1
  92. package/dist/server/comments/hand-out.d.mts +1 -1
  93. package/dist/server/comments/hand-out.mjs +4 -1
  94. package/dist/server/comments/push.d.mts +19 -10
  95. package/dist/server/comments/push.mjs +45 -30
  96. package/dist/server/comments/studio-files.d.mts +14 -1
  97. package/dist/server/comments/studio-files.mjs +44 -31
  98. package/dist/server/comments/tools.d.mts +1 -1
  99. package/dist/server/comments/workspace.mjs +1 -0
  100. package/dist/server/dev-server.mjs +34 -0
  101. package/dist/server/own-copies.mjs +4 -23
  102. package/dist/server/requests.mjs +57 -3
  103. package/dist/server/response-budget.d.mts +2 -1
  104. package/dist/server/response-budget.mjs +4 -3
  105. package/dist/server/roster.d.mts +19 -8
  106. package/dist/server/roster.mjs +50 -22
  107. package/dist/server/studio-for.d.mts +7 -0
  108. package/dist/server/studio-for.mjs +23 -7
  109. package/dist/server/thread-forms.d.mts +3 -0
  110. package/dist/server/thread-forms.mjs +21 -1
  111. package/dist/server/tools/place-frame.d.mts +1 -0
  112. package/dist/server/tools/place-frame.mjs +33 -9
  113. package/dist/server/tools/result.mjs +2 -2
  114. package/dist/server/tools/snapshot.d.mts +1 -1
  115. package/dist/server/tools/snapshot.mjs +2 -1
  116. package/dist/server/tools/studio-context.d.mts +2 -1
  117. package/dist/server/tools/studio-context.mjs +2 -1
  118. package/dist/server/tools/watch.mjs +29 -12
  119. package/dist/server/work-groups.mjs +1 -1
  120. package/dist/shared/canvas-core.d.mts +39 -5
  121. package/dist/shared/canvas-core.mjs +60 -15
  122. package/dist/shared/canvas-file.d.mts +7 -4
  123. package/dist/shared/change-pictures-core.d.mts +6 -0
  124. package/dist/shared/change-pictures-core.mjs +5 -0
  125. package/dist/shared/page-looks.d.mts +12 -0
  126. package/dist/shared/page-looks.mjs +49 -0
  127. package/dist/shared/snapshot-clipboard.d.mts +6 -0
  128. package/dist/shared/studio-routes.d.mts +11 -0
  129. package/extension/dist/manifest.json +1 -1
  130. package/extension/dist/picker.js +114 -108
  131. package/package.json +4 -3
  132. package/src/cli/commands/mcp.ts +18 -8
  133. package/src/cli/mcp-relay.ts +13 -6
  134. package/src/content/content.ts +90 -16
  135. package/src/content/screenshot.ts +9 -1
  136. package/src/content/snapshot-client.ts +17 -0
  137. package/src/paste/paste.ts +18 -1
  138. package/src/server/agent-tools.ts +5 -4
  139. package/src/server/change-picture-store.ts +71 -0
  140. package/src/server/change-pictures.ts +244 -0
  141. package/src/server/close-check.ts +69 -6
  142. package/src/server/comments/agent-actions.ts +51 -12
  143. package/src/server/comments/close-tools.ts +20 -10
  144. package/src/server/comments/files.ts +10 -2
  145. package/src/server/comments/hand-out.ts +2 -2
  146. package/src/server/comments/push.ts +94 -46
  147. package/src/server/comments/studio-files.ts +77 -46
  148. package/src/server/comments/workspace.ts +1 -0
  149. package/src/server/dev-server.ts +70 -0
  150. package/src/server/own-copies.ts +5 -33
  151. package/src/server/requests.ts +84 -4
  152. package/src/server/response-budget.ts +5 -3
  153. package/src/server/roster.ts +78 -28
  154. package/src/server/studio-for.ts +32 -7
  155. package/src/server/thread-forms.ts +41 -1
  156. package/src/server/tools/place-frame.ts +71 -14
  157. package/src/server/tools/result.ts +2 -2
  158. package/src/server/tools/snapshot.ts +2 -1
  159. package/src/server/tools/studio-context.ts +24 -14
  160. package/src/server/tools/watch.ts +38 -13
  161. package/src/server/work-groups.ts +1 -1
  162. package/src/shared/canvas-core.ts +119 -27
  163. package/src/shared/canvas-file.ts +7 -4
  164. package/src/shared/change-pictures-core.ts +8 -0
  165. package/src/shared/page-looks.ts +133 -0
  166. package/src/shared/snapshot-clipboard.ts +6 -0
  167. package/src/shared/studio-routes.ts +7 -1
  168. package/templates/agents/dex-worker.md +9 -7
  169. package/templates/feedback-workflow.md +1 -1
  170. package/templates/methods/review.md +7 -6
  171. package/templates/rules/canvas-frames.md +8 -7
  172. package/templates/rules/canvas.md +10 -8
  173. package/templates/rules/prototypes.md +5 -3
  174. package/app/canvas/stack-node.tsx +0 -100
@@ -99,6 +99,7 @@ import {
99
99
  } from "../shared/build-settings.ts";
100
100
  import type { Cell } from "../shared/canvas-core.ts";
101
101
  import type { Canvas } from "../shared/canvas-file.ts";
102
+ import { isMoment } from "../shared/change-pictures-core.ts";
102
103
  import {
103
104
  COMMAND_SWITCHES,
104
105
  type CommandsAnswer,
@@ -154,6 +155,7 @@ import {
154
155
  type StudioMethodOf,
155
156
  type StudioRouteName,
156
157
  } from "../shared/studio-routes.ts";
158
+ import { THREAD_ID_RE } from "../shared/threads-core.ts";
157
159
  import { agentsPlace } from "./agents-place.ts";
158
160
  import {
159
161
  isAllowedRequest,
@@ -163,6 +165,10 @@ import {
163
165
  } from "./allowed-request.ts";
164
166
  import { AvatarRefusedError, removeAvatar, writeAvatar } from "./avatars.ts";
165
167
  import { readBuildSettings, writeBuildSettings } from "./build-settings.ts";
168
+ import {
169
+ type ChangePictureStore,
170
+ changePictureStore,
171
+ } from "./change-picture-store.ts";
166
172
  import { notesSince } from "./changelog.ts";
167
173
  import {
168
174
  codebaseSkills,
@@ -258,6 +264,7 @@ import { zipFolder } from "./zip.ts";
258
264
  const PREFIX = "/__studio/";
259
265
  const UPDATE_WAIT_MS = 3000;
260
266
  const TRAILING_SLASH_RE = /\/$/u;
267
+ const BASE64_RE = /^[A-Za-z0-9+/]*={0,2}$/u;
261
268
  const METHODS = ["GET", "POST", "PUT", "DELETE"] as const;
262
269
  const WITH_BODY = new Set<string>(["POST", "PUT"]);
263
270
  export const MAX_BODY_BYTES = 1_000_000;
@@ -492,6 +499,7 @@ type StudioPages = ReturnType<typeof studioPages>;
492
499
 
493
500
  interface RouteContext {
494
501
  body: RequestBody | undefined;
502
+ changePictures: ChangePictureStore;
495
503
  /** This studio's app, asked as a desktop route answers. */
496
504
  desktop: () => ProjectDesktop;
497
505
  events: EventHub;
@@ -1020,6 +1028,7 @@ async function pasteSnapshots(context: RouteContext) {
1020
1028
  from: pasted.from,
1021
1029
  saved: saved.map(({ name, frame }) => ({ name, frame })),
1022
1030
  images: pasted.images,
1031
+ ...(pasted.sizedByScripts && { sizedByScripts: pasted.sizedByScripts }),
1023
1032
  ...(large && { large: true as const }),
1024
1033
  };
1025
1034
  }
@@ -1274,9 +1283,73 @@ async function screenshotView(context: RouteContext) {
1274
1283
  }
1275
1284
 
1276
1285
  async function snapshotOfView(context: RouteContext): Promise<SnapshotBody> {
1277
- const { image, problems } = await shotOf(context);
1286
+ const { image, looks = { blank: false }, problems } = await shotOf(context);
1287
+
1288
+ return { image: image.toString("base64"), looks, problems };
1289
+ }
1290
+
1291
+ const threadIdIn = (url: URL): string => {
1292
+ const id = url.searchParams.get("thread") ?? "";
1293
+
1294
+ if (!THREAD_ID_RE.test(id)) {
1295
+ throw new Problems(400, ["Say which comment, by its id."]);
1296
+ }
1297
+
1298
+ return id;
1299
+ };
1300
+
1301
+ const momentIn = (url: URL) => {
1302
+ const moment = url.searchParams.get("moment");
1303
+
1304
+ if (!isMoment(moment)) {
1305
+ throw new Problems(400, ["Say which picture: before or after."]);
1306
+ }
1307
+
1308
+ return moment;
1309
+ };
1310
+
1311
+ const loadChangePictures = ({ changePictures, url }: RouteContext) =>
1312
+ changePictures.kept(threadIdIn(url));
1313
+
1314
+ function serveChangePicture({ changePictures, response, url }: RouteContext) {
1315
+ const image = changePictures.image(threadIdIn(url), momentIn(url));
1316
+
1317
+ if (!image) {
1318
+ throw new Problems(404, ["That comment has no such picture."]);
1319
+ }
1320
+
1321
+ response.setHeader("Cache-Control", "no-store");
1322
+ response.setHeader("X-Content-Type-Options", "nosniff");
1278
1323
 
1279
- return { image: image.toString("base64"), problems };
1324
+ return new Reply(200, image, "image/png");
1325
+ }
1326
+
1327
+ const PNG_SIGNATURE = Buffer.from([
1328
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
1329
+ ]);
1330
+
1331
+ function keepChangePicture({ body, changePictures, url }: RouteContext) {
1332
+ const data = body?.data;
1333
+
1334
+ if (typeof data !== "string" || !BASE64_RE.test(data)) {
1335
+ throw new Problems(400, ["Send the picture's bytes in base64 as data."]);
1336
+ }
1337
+
1338
+ const image = Buffer.from(data, "base64");
1339
+
1340
+ if (!image.subarray(0, PNG_SIGNATURE.length).equals(PNG_SIGNATURE)) {
1341
+ throw new Problems(415, ["A comment's picture is a PNG."]);
1342
+ }
1343
+
1344
+ changePictures.keep(threadIdIn(url), momentIn(url), image);
1345
+
1346
+ return {};
1347
+ }
1348
+
1349
+ function dropChangePicture({ changePictures, url }: RouteContext) {
1350
+ changePictures.drop(threadIdIn(url), momentIn(url));
1351
+
1352
+ return {};
1280
1353
  }
1281
1354
 
1282
1355
  const pictures = framePictures();
@@ -2131,8 +2204,6 @@ async function saveTeamDevices({ project, body, events }: RouteContext) {
2131
2204
  return { devices: saved.devices, problems: [] };
2132
2205
  }
2133
2206
 
2134
- const BASE64_RE = /^[A-Za-z0-9+/]*={0,2}$/u;
2135
-
2136
2207
  /**
2137
2208
  * The icon is served under its own address, so it is never a page: opened
2138
2209
  * directly, an SVG runs nothing and loads nothing.
@@ -2512,6 +2583,13 @@ const ROUTES = {
2512
2583
  screenshot: { POST: screenshotView },
2513
2584
  snapshot: { POST: snapshotOfView },
2514
2585
  picture: { POST: pictureOfView },
2586
+ "change-pictures": { GET: loadChangePictures },
2587
+ "change-picture": {
2588
+ limit: MAX_CAPTURE_BYTES,
2589
+ GET: serveChangePicture,
2590
+ PUT: keepChangePicture,
2591
+ DELETE: dropChangePicture,
2592
+ },
2515
2593
  agents: { GET: listConnectedAgents },
2516
2594
  "agent-stop": { POST: stopOldAgent },
2517
2595
  edited: { GET: ({ project }) => lastEdited(project.content) },
@@ -2626,6 +2704,7 @@ export function studioRequests({
2626
2704
  ) => Promise<boolean> {
2627
2705
  const pages = studioPages({ project, events, threads, agents });
2628
2706
  const pageBase = base.replace(TRAILING_SLASH_RE, "");
2707
+ const changePictures = changePictureStore();
2629
2708
 
2630
2709
  return async (request, response) => {
2631
2710
  const url = new URL(request.url ?? "/", "http://studio.local");
@@ -2649,6 +2728,7 @@ export function studioRequests({
2649
2728
  const route: Route = ROUTES[name];
2650
2729
 
2651
2730
  const answered = await answer(route, {
2731
+ changePictures,
2652
2732
  project,
2653
2733
  threads,
2654
2734
  events,
@@ -59,10 +59,12 @@ export const RESPONSE_BUDGETS = {
59
59
  notice: NOTICE_BUDGET,
60
60
  } as const;
61
61
 
62
- /** What a value comes to as a tool returns it: JSON, indented. */
62
+ /** A value as a tool sends it, and as its size is measured: compact JSON. */
63
+ // oxlint-disable-next-line anti-slop/no-unknown-parameters -- writes any value as JSON
64
+ export const jsonText = (value: unknown): string => JSON.stringify(value) ?? "";
65
+
63
66
  // oxlint-disable-next-line anti-slop/no-unknown-parameters -- measures any value as JSON
64
- export const sizeOf = (value: unknown): number =>
65
- JSON.stringify(value, null, 2)?.length ?? 0;
67
+ export const sizeOf = (value: unknown): number => jsonText(value).length;
66
68
 
67
69
  const count = (number: number): string => number.toLocaleString("en");
68
70
 
@@ -37,12 +37,19 @@ export interface RosterEntry {
37
37
  role: RosterRole;
38
38
  /** When it joined. */
39
39
  since: string;
40
- /** When its process started, as ps says: which process `pid` was. */
40
+ /**
41
+ * When its process started, as an older Dex wrote it: as ps said it in
42
+ * the time zone and language that Dex ran in (see processNow).
43
+ */
41
44
  started?: string;
45
+ /** When its process started, as startedAt says: which process `pid` was. */
46
+ startedUtc?: string;
42
47
  /** The supervisor that runs a development server, when one does. */
43
48
  supervisor?: number;
44
- /** When the supervisor's process started, as ps says. */
49
+ /** When the supervisor's process started, as an older Dex wrote it. */
45
50
  supervisorStarted?: string;
51
+ /** When the supervisor's process started, as startedAt says. */
52
+ supervisorStartedUtc?: string;
46
53
  /** The Dex it runs. */
47
54
  version?: string;
48
55
  }
@@ -75,16 +82,20 @@ export function isAlive(pid: number): boolean {
75
82
  }
76
83
 
77
84
  /**
78
- * When process `pid` started, as ps says: with its pid, which process it
79
- * is. Undefined where there is no ps (Windows) or it can't say.
85
+ * When process `pid` started, as ps says it in the time zone and language
86
+ * `env` names. Undefined where there is no ps (Windows) or it can't say.
80
87
  */
81
- export async function startedAt(pid: number): Promise<string | undefined> {
88
+ async function psStart(
89
+ pid: number,
90
+ env: NodeJS.ProcessEnv
91
+ ): Promise<string | undefined> {
82
92
  if (process.platform === "win32") {
83
93
  return;
84
94
  }
85
95
 
86
96
  try {
87
97
  const { stdout } = await run("ps", ["-o", "lstart=", "-p", String(pid)], {
98
+ env,
88
99
  timeout: PS_MS,
89
100
  });
90
101
 
@@ -95,22 +106,55 @@ export async function startedAt(pid: number): Promise<string | undefined> {
95
106
  }
96
107
 
97
108
  /**
98
- * Whether `pid` is still the process that started at `started`. Without
99
- * ps (Windows), whether it is running, and the port says the rest.
109
+ * When process `pid` started, as ps says: with its pid, which process it
110
+ * is. Asked in UTC and the C locale: ps writes the time in the time zone
111
+ * and language of whoever asks, and the Dexes on one roster start from
112
+ * different ones (a person's terminal, an agent's app), which must all read
113
+ * one start the same. Undefined where there is no ps (Windows) or it can't
114
+ * say.
115
+ */
116
+ export const startedAt = (pid: number): Promise<string | undefined> =>
117
+ psStart(pid, { ...process.env, LC_ALL: "C", TZ: "UTC" });
118
+
119
+ /** What a roster entry's pid is now, against the start it joined with. */
120
+ type ProcessNow = "joined" | "another" | "unknown";
121
+
122
+ /**
123
+ * Whether `pid` is still the process that joined with start `utc` (as
124
+ * startedAt says), or `local` (an older Dex's): "another" once it stopped
125
+ * or its start differs, "unknown" while that can't be told. An older Dex
126
+ * wrote its start as ps said it in the time zone and language it ran in,
127
+ * so it is asked of ps here as that Dex asked it, which agrees only when
128
+ * both run in the same: when it doesn't, nothing can tell. Without ps
129
+ * (Windows), "joined" while it runs, and the port says the rest.
100
130
  */
101
- async function stillThe(
131
+ async function processNow(
102
132
  pid: number,
103
- started: string | undefined
104
- ): Promise<boolean> {
133
+ utc: string | undefined,
134
+ local?: string
135
+ ): Promise<ProcessNow> {
105
136
  if (!isAlive(pid)) {
106
- return false;
137
+ return "another";
107
138
  }
108
139
 
109
140
  if (process.platform === "win32") {
110
- return true;
141
+ return "joined";
111
142
  }
112
143
 
113
- return started !== undefined && (await startedAt(pid)) === started;
144
+ if (utc !== undefined) {
145
+ const now = await startedAt(pid);
146
+
147
+ if (now === undefined) {
148
+ return "unknown";
149
+ }
150
+
151
+ return now === utc ? "joined" : "another";
152
+ }
153
+
154
+ const here =
155
+ local === undefined ? undefined : await psStart(pid, process.env);
156
+
157
+ return here !== undefined && here === local ? "joined" : "unknown";
114
158
  }
115
159
 
116
160
  /** What a process in the roster can change about itself, or end. */
@@ -142,8 +186,8 @@ export async function joinRoster(
142
186
  ...entry,
143
187
  pid,
144
188
  since: new Date().toISOString(),
145
- ...(started && { started }),
146
- ...(supervisorStarted && { supervisorStarted }),
189
+ ...(started && { startedUtc: started }),
190
+ ...(supervisorStarted && { supervisorStartedUtc: supervisorStarted }),
147
191
  };
148
192
 
149
193
  const file = state.rosterEntry(kept.pid);
@@ -210,9 +254,7 @@ export async function readRoster(content: string): Promise<RosterEntry[]> {
210
254
  }
211
255
 
212
256
  // Its pid is another process's now: the Dex stopped.
213
- const now = entry.started && (await startedAt(pid));
214
-
215
- if (now && now !== entry.started) {
257
+ if ((await processNow(pid, entry.startedUtc)) === "another") {
216
258
  await rm(join(folder, name), { force: true });
217
259
  continue;
218
260
  }
@@ -297,10 +339,10 @@ export async function askOthersToRestart(
297
339
  /**
298
340
  * Stop other Dexes: ask every other development server for the project
299
341
  * `id` names (projectId) to stop, through its supervisor when it has one,
300
- * as Ctrl+C does. Only one still the process that joined (an entry whose
301
- * pid is another process's now is dropped) and answering on its port as
302
- * this project's Dex; nothing else is signalled. Answers how many were
303
- * asked.
342
+ * as Ctrl+C does. Only one known to be the process that joined (an entry
343
+ * whose pid is another process's now is dropped; one that can't be told
344
+ * stays, unasked) and answering on its port as this project's Dex; nothing
345
+ * else is signalled. Answers how many were asked.
304
346
  */
305
347
  export async function stopOthers(
306
348
  content: string,
@@ -311,26 +353,34 @@ export async function stopOthers(
311
353
  let asked = 0;
312
354
 
313
355
  for (const entry of others) {
314
- const { pid, port, started, supervisor, supervisorStarted } = entry;
356
+ const { pid, port, supervisor } = entry;
357
+ const now = await processNow(pid, entry.startedUtc, entry.started);
315
358
 
316
- if (!(await stillThe(pid, started))) {
359
+ if (now === "another") {
317
360
  await rm(studioState(content).rosterEntry(pid), { force: true });
318
361
  continue;
319
362
  }
320
363
 
321
- // A server that doesn't answer as this project's is left alone.
364
+ // A server that may not be the one that joined, or doesn't answer as
365
+ // this project's, is left alone.
322
366
  // SAFETY: otherServers keeps only entries with a port.
323
- if (!(await answersAs(port as number, id))) {
367
+ if (now === "unknown" || !(await answersAs(port as number, id))) {
324
368
  continue;
325
369
  }
326
370
 
327
371
  // Only a supervisor whose start was recorded, and still matches: where
328
372
  // that can't be checked (Windows), the server answering as this
329
373
  // project's is signalled instead.
374
+ const recorded = entry.supervisorStartedUtc ?? entry.supervisorStarted;
375
+
330
376
  const supervised =
331
377
  supervisor !== undefined &&
332
- supervisorStarted !== undefined &&
333
- (await stillThe(supervisor, supervisorStarted));
378
+ recorded !== undefined &&
379
+ (await processNow(
380
+ supervisor,
381
+ entry.supervisorStartedUtc,
382
+ entry.supervisorStarted
383
+ )) === "joined";
334
384
 
335
385
  try {
336
386
  process.kill(supervised ? supervisor : pid, "SIGTERM");
@@ -186,25 +186,41 @@ const START =
186
186
  const ports = (tried: number[]): string =>
187
187
  tried.map((port) => `:${port}`).join(", ");
188
188
 
189
- /** Why there is no Dex to ask, in words a person can act on. */
190
- export function notRunning(
189
+ export interface NotRunning {
190
+ todo: string;
191
+ why: string;
192
+ }
193
+
194
+ export function whyNotRunning(
191
195
  found: Exclude<StudioFor, { kind: "running" }>
192
- ): string {
196
+ ): NotRunning {
193
197
  switch (found.kind) {
194
198
  case "none": {
195
- return `Dex isn't running for this checkout (tried ${ports(found.tried)}). ${START}`;
199
+ return {
200
+ why: `Dex isn't running for this checkout (tried ${ports(found.tried)}).`,
201
+ todo: START,
202
+ };
196
203
  }
197
204
 
198
205
  case "elsewhere": {
199
- return `Dex isn't running for this checkout (tried ${ports(found.tried)}; :${found.port} is ${found.holder}). Start this checkout's with \`npx dex dev\`, which moves on to a free port, then try again.`;
206
+ return {
207
+ why: `Dex isn't running for this checkout (tried ${ports(found.tried)}; :${found.port} is ${found.holder}).`,
208
+ todo: "Start this checkout's with `npx dex dev`, which moves on to a free port, then try again.",
209
+ };
200
210
  }
201
211
 
202
212
  case "starting": {
203
- return `Dex for this checkout is restarting on :${found.port}. Try again in a moment.`;
213
+ return {
214
+ why: `Dex for this checkout is restarting on :${found.port}.`,
215
+ todo: "Try again in a moment.",
216
+ };
204
217
  }
205
218
 
206
219
  case "failed": {
207
- return `Dex for this checkout stopped just after it restarted. Its log and Try again are at http://localhost:${found.port}; once it is running, try again.`;
220
+ return {
221
+ why: "Dex for this checkout stopped just after it restarted.",
222
+ todo: `Its log and Try again are at http://localhost:${found.port}; once it is running, try again.`,
223
+ };
208
224
  }
209
225
 
210
226
  default: {
@@ -212,3 +228,12 @@ export function notRunning(
212
228
  }
213
229
  }
214
230
  }
231
+
232
+ /** Why there is no Dex to ask, in words a person can act on. */
233
+ export function notRunning(
234
+ found: Exclude<StudioFor, { kind: "running" }>
235
+ ): string {
236
+ const { why, todo } = whyNotRunning(found);
237
+
238
+ return `${why} ${todo}`;
239
+ }
@@ -14,6 +14,8 @@
14
14
  // and the latest, and a wireframe says how many boxes it has. Never cut:
15
15
  // a thread's id, where it is, and that someone asked.
16
16
 
17
+ import { isDeepStrictEqual } from "node:util";
18
+
17
19
  import { cutText, plural, sizeOf } from "./response-budget.ts";
18
20
 
19
21
  /** A thread as describeAt gives it. */
@@ -81,7 +83,6 @@ const messagesOf = (described: Described): Message[] =>
81
83
  // SAFETY: a Described thread's messages are the Messages shaped from it.
82
84
  Array.isArray(described.messages) ? (described.messages as Message[]) : [];
83
85
 
84
- /** The first message and the latest ones, each cut, and how many are between. */
85
86
  /** The messages a form keeps, and how many it leaves out. */
86
87
  interface SomeMessages {
87
88
  left: number;
@@ -212,6 +213,45 @@ export function describedIn(described: Described, form: Form): ThreadInForm {
212
213
  return { thread, cut };
213
214
  }
214
215
 
216
+ const PLACE_FIELDS = [
217
+ "file",
218
+ "folder",
219
+ "canvas",
220
+ "section",
221
+ "frame",
222
+ "uid",
223
+ "state",
224
+ "viewport",
225
+ "flowSpec",
226
+ "moreFlowSpecs",
227
+ ] as const;
228
+
229
+ /** Where two or more threads are alike, to say once: a field one has not is not shared. */
230
+ export function sharedPlace(threads: Described[]): Described {
231
+ const [first, ...rest] = threads;
232
+
233
+ if (!first || rest.length === 0) {
234
+ return {};
235
+ }
236
+
237
+ return Object.fromEntries(
238
+ PLACE_FIELDS.flatMap((key) =>
239
+ first[key] !== undefined &&
240
+ rest.every((thread) => isDeepStrictEqual(thread[key], first[key]))
241
+ ? [[key, first[key]]]
242
+ : []
243
+ )
244
+ );
245
+ }
246
+
247
+ export const withoutSaid = (thread: Described, said: Described): Described =>
248
+ Object.fromEntries(
249
+ Object.entries(thread).filter(
250
+ ([key, value]) =>
251
+ !(Object.hasOwn(said, key) && isDeepStrictEqual(value, said[key]))
252
+ )
253
+ );
254
+
215
255
  /**
216
256
  * A thread's description in the fullest of `forms` that comes within
217
257
  * `room`; in the last of them when none does.
@@ -4,13 +4,19 @@
4
4
  import { defineTool } from "@dexhq/harness/tools";
5
5
  import { z } from "zod";
6
6
 
7
- import { joinCurrentPerson, placeViewFrame } from "../../content/content.ts";
7
+ import {
8
+ frameKeyOn,
9
+ joinCurrentPerson,
10
+ placeViewFrame,
11
+ readCanvas,
12
+ } from "../../content/content.ts";
8
13
  import { resolvePrototype } from "../../content/prototypes.ts";
9
14
  import type { ThreadModule } from "../../content/thread-module.ts";
10
15
  import {
11
- type Cell,
16
+ type AskedCell,
12
17
  type PageSize,
13
18
  requestCell,
19
+ whereInRow,
14
20
  } from "../../shared/canvas-core.ts";
15
21
  import { NOT_RECOGNISED } from "../../shared/studio-routes.ts";
16
22
  import type { ToolContext } from "./context.ts";
@@ -23,19 +29,39 @@ const LARGE_CANVAS_NOTE =
23
29
 
24
30
  /**
25
31
  * Where `place_frame` puts a frame: a view request's cell, by its thread,
26
- * at the size the person gave it; or the cell given.
32
+ * at the size the person gave it; right of a frame; or the cell given, its
33
+ * step left out for the end of its row.
27
34
  */
28
35
  async function placeFor(
29
36
  threads: ThreadModule,
30
37
  input: {
31
38
  above?: string;
32
39
  prototype: string;
40
+ rightOf?: string;
33
41
  row?: string;
34
42
  section?: string;
35
43
  step?: number;
36
44
  thread?: string;
37
45
  }
38
- ): Promise<{ cell: Cell; size?: PageSize } | { problem: string }> {
46
+ ): Promise<
47
+ | { cell: AskedCell | { rightOf: string }; size?: PageSize }
48
+ | { problem: string }
49
+ > {
50
+ if (input.rightOf) {
51
+ const { above, row, section, step, thread } = input;
52
+
53
+ if (
54
+ [above, row, section, step, thread].some((given) => given !== undefined)
55
+ ) {
56
+ return {
57
+ problem:
58
+ "Give rightOf, or the cell (a thread, or a section with its row or step), not both: rightOf finds the frame's cell as the canvas is now.",
59
+ };
60
+ }
61
+
62
+ return { cell: { rightOf: input.rightOf } };
63
+ }
64
+
39
65
  if (input.thread) {
40
66
  const thread = await threads.find(input.thread);
41
67
 
@@ -60,10 +86,10 @@ async function placeFor(
60
86
  return { cell: requestCell(thread.target), size: thread.target.size };
61
87
  }
62
88
 
63
- if (!(input.section && input.step)) {
89
+ if (!input.section) {
64
90
  return {
65
91
  problem:
66
- "Say where the frame goes: the thread of a request for a new view, or a section and step (and a row, for an existing row).",
92
+ "Say where the frame goes: the thread of a request for a new view, a frame to go right of (rightOf), or a section (with a row and step, or neither for the end of its main path).",
67
93
  };
68
94
  }
69
95
 
@@ -72,6 +98,11 @@ async function placeFor(
72
98
  };
73
99
  }
74
100
 
101
+ /** What `place_frame` says of each frame: where it went, or why it did not. */
102
+ type FrameAnswer =
103
+ | { added: boolean; frame: string; uid?: string; view: string }
104
+ | { problem: string; view: string };
105
+
75
106
  /** The `place_frame` tool. */
76
107
  export function placeFrameTool({
77
108
  refuse,
@@ -89,7 +120,7 @@ export function placeFrameTool({
89
120
  },
90
121
  title: "Put views on the canvas",
91
122
  description:
92
- "Add frames showing views you built to a file's canvas, several at once, each in a cell (a row and a step of a section): give the thread of a request for a new view (its target names the cell, and the size when the person set one), or the cell itself (section, step, and row for an existing row; leave row out for a new row at the end of the section, or give `above` for one right above that row). A cell's frames are one stack: with `stack`, the frame goes on top of the stack already in that cell, a variant of that step; without it, a frame already in the cell moves one step right, with the frames packed after it, and the new frame takes the cell. Every column starts at its row's top: never write `beside`. canvas.json is changed for you, safely while other agents change it too: do not edit it by hand for this. If a cell already shows the view, its frame is returned as it is. Returns each frame's key (\"section/row/frame\") and its uid, which picks follow.",
123
+ "Add frames showing views you built to a file's canvas, several at once, each in a cell (a row and a step of a section): give the thread of a request for a new view (its target names the cell, and the size when the person set one); or `rightOf`, a frame's uid or key, for the step right after that frame in its row, wherever the person has moved it since (in someone else's row, your own row right below it); use it to put a frame right of another, never a step worked out from an earlier read; or the cell itself (section, step, and row for an existing row; leave step out for the free step at the end of the row; leave row out for a new row at the end of the section, or give `above` for one right above that row; leave both out for the end of the section's first row, its main path). A cell's frames are one stack: with `stack`, the frame goes on top of the stack already in that cell, a variant of that step; without it, a frame already in the cell moves one step right, with the frames packed after it, and the new frame takes the cell. Every column starts at its row's top: never write `beside`. canvas.json is changed for you, safely while other agents change it too: do not edit it by hand for this. If a cell already shows the view, its frame is returned as it is, and so is the view's frame already in the row past `rightOf`, or anywhere in it with no step. Returns each frame's key (\"section/row/frame\"), its uid, which picks follow, and, once all are placed, its step and `rightOf`, the key of the frame on its left in its row (none at the row's start): say where it went from these.",
93
124
  inputSchema: {
94
125
  prototype: PROTOTYPE,
95
126
  frames: z
@@ -103,6 +134,13 @@ export function placeFrameTool({
103
134
  .string()
104
135
  .optional()
105
136
  .describe("A request for a new view: its cell is used."),
137
+ rightOf: z
138
+ .string()
139
+ .min(1)
140
+ .optional()
141
+ .describe(
142
+ 'A frame\'s uid or key ("section/row/frame"): the frame goes on the step right after it, in its row, where it is now. Leave out thread, section, row, above and step.'
143
+ ),
106
144
  section: z.string().optional().describe("The section's id."),
107
145
  row: z
108
146
  .string()
@@ -119,12 +157,14 @@ export function placeFrameTool({
119
157
  .int()
120
158
  .min(1)
121
159
  .optional()
122
- .describe("The column, from 1."),
160
+ .describe(
161
+ "The step, from 1; leave out for the free step at the end of the row."
162
+ ),
123
163
  stack: z
124
164
  .boolean()
125
165
  .optional()
126
166
  .describe(
127
- "With a row and a step: put the frame on top of the stack in that cell, rather than moving what is there one step right."
167
+ "With a row and a step, or rightOf: put the frame on top of the stack in that cell, rather than moving what is there one step right."
128
168
  ),
129
169
  })
130
170
  )
@@ -148,8 +188,7 @@ export function placeFrameTool({
148
188
  return refuse(NOT_RECOGNISED);
149
189
  }
150
190
 
151
- // oxlint-disable-next-line anti-slop/no-unsafe-dictionary-type -- each frame's answer: its view and what was placed or refused, sent as JSON
152
- const frames: Record<string, unknown>[] = [];
191
+ const frames: FrameAnswer[] = [];
153
192
  let large = false;
154
193
 
155
194
  // One at a time: each changes canvas.json.
@@ -183,11 +222,29 @@ export function placeFrameTool({
183
222
  });
184
223
  }
185
224
 
186
- if (frames.every((frame) => "problem" in frame)) {
187
- return refuse(frames.map((frame) => frame.problem).join(" "));
225
+ const problems = frames.flatMap((answer) =>
226
+ "problem" in answer ? [answer.problem] : []
227
+ );
228
+
229
+ if (problems.length === frames.length) {
230
+ return refuse(problems.join(" "));
188
231
  }
189
232
 
190
- const result = json({ frames });
233
+ // Where each is once all are placed: a later one may have moved an
234
+ // earlier one along.
235
+ const placedAll = await readCanvas(slug, root);
236
+
237
+ const answers = frames.map((answer) => {
238
+ if ("problem" in answer) {
239
+ return answer;
240
+ }
241
+
242
+ const key = frameKeyOn(placedAll, answer) ?? answer.frame;
243
+
244
+ return { ...answer, frame: key, ...whereInRow(placedAll, key) };
245
+ });
246
+
247
+ const result = json({ frames: answers });
191
248
 
192
249
  return withNotice(
193
250
  large
@@ -3,14 +3,14 @@
3
3
 
4
4
  import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
5
5
 
6
- import { firstOf } from "../response-budget.ts";
6
+ import { firstOf, jsonText } from "../response-budget.ts";
7
7
 
8
8
  /** Words as a part of a tool's result. */
9
9
  export const textPart = (text: string) => ({ type: "text" as const, text });
10
10
 
11
11
  // oxlint-disable-next-line anti-slop/no-unknown-parameters -- a tool's result, any value, sent as JSON
12
12
  export const json = (value: unknown): CallToolResult => ({
13
- content: [{ type: "text", text: JSON.stringify(value, null, 2) }],
13
+ content: [{ type: "text", text: jsonText(value) }],
14
14
  });
15
15
 
16
16
  /** A result with a line after its answer: what the answer left out. */