@dexhq/canvas 0.13.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.
- package/CHANGELOG.md +53 -0
- package/README.md +21 -18
- package/app/canvas/activity.tsx +8 -15
- package/app/canvas/anchoring.ts +3 -5
- package/app/canvas/canvas-input.ts +151 -14
- package/app/canvas/canvas-keys.ts +9 -0
- package/app/canvas/canvas-model.ts +5 -42
- package/app/canvas/canvas-panels.ts +1 -17
- package/app/canvas/canvas-scene.ts +7 -3
- package/app/canvas/canvas-ui.ts +2 -1
- package/app/canvas/canvas.css +61 -26
- package/app/canvas/change-notice.tsx +10 -1
- package/app/canvas/comment-layer.tsx +2 -1
- package/app/canvas/comments.tsx +204 -82
- package/app/canvas/element-picker.ts +200 -27
- package/app/canvas/engine/canvas-gestures.ts +72 -17
- package/app/canvas/engine/canvas-store.ts +17 -1
- package/app/canvas/engine/engine.css +6 -3
- package/app/canvas/find-bar.tsx +817 -0
- package/app/canvas/find-model.ts +135 -0
- package/app/canvas/flow-canvas.tsx +18 -2
- package/app/canvas/follow-pin.ts +26 -6
- package/app/canvas/frame-node.tsx +43 -26
- package/app/canvas/imported-reviews.tsx +12 -22
- package/app/canvas/layout.ts +27 -0
- package/app/canvas/live-page.ts +142 -19
- package/app/canvas/page-find.ts +220 -0
- package/app/canvas/page-style.ts +23 -0
- package/app/canvas/section-node.tsx +2 -0
- package/app/canvas/spec-threads.ts +24 -23
- package/app/canvas/thread-focus.tsx +14 -12
- package/app/canvas/tool-cursors.ts +16 -16
- package/app/canvas/use-activity.ts +0 -8
- package/app/canvas/use-agent-changes.tsx +34 -40
- package/app/canvas/use-canvas-clipboard.tsx +11 -14
- package/app/canvas/xray.ts +3 -12
- package/app/frame.tsx +5 -0
- package/app/i18n/locales/en/messages.po +114 -12
- package/app/i18n/locales/en/messages.ts +1 -1
- package/app/i18n/locales/pseudo/messages.po +114 -12
- package/app/i18n/locales/pseudo/messages.ts +1 -1
- package/app/icons/{sets/lucide.tsx → glyphs.tsx} +7 -5
- package/app/icons/index.tsx +3 -3
- package/app/icons/types.ts +3 -10
- package/app/index.html +3 -3
- package/app/main.tsx +3 -0
- package/app/pages/canvas-settings.tsx +35 -0
- package/app/pages/canvas.tsx +27 -8
- package/app/pages/commands-settings.tsx +16 -2
- package/app/shell/about.tsx +5 -4
- package/app/shell/feedback-details.ts +0 -3
- package/app/shell/feedback-form.tsx +10 -21
- package/app/shell/feedback-link.tsx +2 -1
- package/app/shell/instructions-editor.tsx +1 -1
- package/app/shell/presence-style.ts +59 -0
- package/app/shell/presence.tsx +6 -3
- package/app/shell/scaled-frame.tsx +21 -9
- package/app/shell/settings-sections.ts +12 -0
- package/app/shell/sidebar.tsx +1 -1
- package/app/studio-ui.d.ts +0 -9
- package/app/types.ts +21 -205
- package/app/ui/file-drop.ts +128 -0
- package/app/ui/prompt-editor.tsx +1 -1
- package/app/ui/shadcn/input-group.tsx +2 -2
- package/dist/cli/banner.d.mts +4 -2
- package/dist/cli/banner.mjs +34 -11
- package/dist/cli/commands/app.mjs +19 -11
- package/dist/cli/commands/create.d.mts +26 -14
- package/dist/cli/commands/create.mjs +13 -7
- package/dist/cli/commands/dev.mjs +2 -3
- package/dist/cli/commands/hook.mjs +2 -1
- package/dist/cli/commands/init.d.mts +11 -6
- package/dist/cli/help.d.mts +1 -1
- package/dist/cli/help.mjs +3 -3
- package/dist/cli/main.d.mts +22 -12
- package/dist/config/index.d.mts +6 -5
- package/dist/config/project-files.d.mts +6 -3
- package/dist/config/project-files.mjs +14 -5
- package/dist/config/renamed.d.mts +0 -2
- package/dist/config/renamed.mjs +1 -3
- package/dist/config/schema.d.mts +0 -5
- package/dist/config/schema.mjs +0 -5
- package/dist/content/capture.d.mts +2 -2
- package/dist/content/capture.mjs +2 -2
- package/dist/content/content.d.mts +3 -141
- package/dist/content/content.mjs +7 -65
- package/dist/content/flow-specs.mjs +1 -1
- package/dist/content/prototype-summary.d.mts +3 -39
- package/dist/content/prototype-summary.mjs +2 -3
- package/dist/content/prototypes.mjs +1 -1
- package/dist/content/snapshot-client.mjs +1 -1
- package/dist/content/text-replace.d.mts +30 -0
- package/dist/content/text-replace.mjs +309 -0
- package/dist/content/thread-module.mjs +1 -1
- package/dist/content/threads.mjs +1 -1
- package/dist/paths.d.mts +0 -2
- package/dist/paths.mjs +1 -3
- package/dist/server/agent-env.d.mts +5 -1
- package/dist/server/agent-env.mjs +7 -2
- package/dist/server/agent-session.d.mts +14 -1
- package/dist/server/agent-session.mjs +84 -19
- package/dist/server/brief.mjs +3 -3
- package/dist/server/build-pictures.mjs +1 -1
- package/dist/server/channel.d.mts +19 -24
- package/dist/server/channel.mjs +70 -37
- package/dist/server/claimed-presence.d.mts +18 -0
- package/dist/server/claimed-presence.mjs +58 -0
- package/dist/server/close-check.mjs +2 -2
- package/dist/server/comments/actions.d.mts +153 -0
- package/dist/server/comments/actions.mjs +154 -0
- package/dist/server/comments/routes.d.mts +25 -0
- package/dist/server/comments/routes.mjs +88 -0
- package/dist/server/comments/tools.d.mts +6 -0
- package/dist/server/comments/tools.mjs +154 -0
- package/dist/server/dev-server.mjs +36 -13
- package/dist/server/file-errors.d.mts +7 -2
- package/dist/server/file-errors.mjs +31 -8
- package/dist/server/file-lock.d.mts +2 -2
- package/dist/server/file-lock.mjs +130 -36
- package/dist/server/mcp.d.mts +0 -2
- package/dist/server/mcp.mjs +7 -8
- package/dist/server/me.d.mts +1 -1
- package/dist/server/open-page.d.mts +22 -0
- package/dist/server/open-page.mjs +20 -2
- package/dist/server/outcome.d.mts +22 -0
- package/dist/server/outcome.mjs +8 -0
- package/dist/server/own-copies.mjs +2 -2
- package/dist/server/people.d.mts +1 -1
- package/dist/server/port.d.mts +12 -0
- package/dist/server/port.mjs +66 -14
- package/dist/server/presence.d.mts +4 -1
- package/dist/server/presence.mjs +2 -2
- package/dist/server/requests.d.mts +5 -1
- package/dist/server/requests.mjs +32 -47
- package/dist/server/studio-for.d.mts +5 -0
- package/dist/server/studio-for.mjs +18 -4
- package/dist/server/sync-install.d.mts +8 -12
- package/dist/server/sync-install.mjs +12 -30
- package/dist/server/thread-source.mjs +2 -2
- package/dist/server/tools/describe.d.mts +1 -1
- package/dist/server/tools/describe.mjs +1 -1
- package/dist/server/tools/feedback.d.mts +1 -1
- package/dist/server/tools/reading.mjs +13 -4
- package/dist/server/tools/show.d.mts +1 -1
- package/dist/server/tools/snapshot.d.mts +1 -1
- package/dist/server/tools/thread-list.mjs +1 -1
- package/dist/server/tools/watch.mjs +5 -3
- package/dist/server/vite.mjs +28 -31
- package/dist/server/web-app.mjs +7 -10
- package/dist/server/work-groups.mjs +1 -1
- package/dist/setup/apply.mjs +8 -14
- package/dist/setup/create.d.mts +4 -2
- package/dist/setup/create.mjs +118 -36
- package/dist/setup/design-system-checks.mjs +3 -12
- package/dist/setup/desktop-app-script.mjs +4 -3
- package/dist/setup/desktop-app-start.d.mts +8 -2
- package/dist/setup/desktop-app-start.mjs +64 -29
- package/dist/setup/desktop-app.d.mts +8 -0
- package/dist/setup/desktop-app.mjs +20 -4
- package/dist/setup/detect.d.mts +14 -7
- package/dist/setup/detect.mjs +56 -10
- package/dist/setup/doctor.mjs +2 -4
- package/dist/setup/existing-setup.d.mts +1 -3
- package/dist/setup/existing-setup.mjs +10 -11
- package/dist/setup/migrate/row-titles.mjs +1 -1
- package/dist/setup/own-app.d.mts +1 -2
- package/dist/setup/own-app.mjs +4 -5
- package/dist/setup/plan.d.mts +8 -6
- package/dist/setup/plan.mjs +64 -48
- package/dist/setup/project.d.mts +6 -17
- package/dist/setup/project.mjs +17 -61
- package/dist/setup/prompts.d.mts +5 -5
- package/dist/setup/prompts.mjs +28 -61
- package/dist/setup/questions.d.mts +12 -4
- package/dist/setup/questions.mjs +10 -25
- package/dist/setup/starters.d.mts +2 -0
- package/dist/setup/starters.mjs +26 -9
- package/dist/setup/workspace-setup.d.mts +1 -2
- package/dist/setup/workspace-setup.mjs +4 -6
- package/dist/setup/write.d.mts +15 -5
- package/dist/setup/write.mjs +35 -9
- package/dist/shared/annotations-core.mjs +1 -1
- package/dist/shared/canvas-core.mjs +1 -1
- package/dist/shared/canvas-file.d.mts +201 -0
- package/dist/shared/canvas-file.mjs +1 -0
- package/dist/shared/comments/routes.d.mts +53 -0
- package/dist/shared/comments/routes.mjs +37 -0
- package/dist/shared/feedback.d.mts +8 -0
- package/dist/shared/feedback.mjs +10 -2
- package/dist/shared/page-review.d.mts +2 -11
- package/dist/shared/page-review.mjs +4 -13
- package/dist/shared/presence-core.d.mts +11 -0
- package/dist/shared/presence-core.mjs +11 -2
- package/dist/shared/review-file.d.mts +5 -0
- package/dist/shared/review-file.mjs +23 -13
- package/dist/shared/server-page.mjs +7 -1
- package/dist/shared/signup.d.mts +2 -5
- package/dist/shared/signup.mjs +2 -35
- package/dist/shared/studio-events.d.mts +0 -6
- package/dist/shared/studio-events.mjs +1 -5
- package/dist/shared/studio-files.d.mts +12 -0
- package/dist/shared/studio-files.mjs +6 -1
- package/dist/shared/studio-routes.d.mts +16 -62
- package/dist/shared/studio-routes.mjs +1 -69
- package/dist/shared/threads-core.d.mts +2 -2
- package/dist/shared/threads-core.mjs +1 -1
- package/extension/dist/feedback.js +148 -138
- package/extension/dist/manifest.json +1 -1
- package/extension/dist/picker.js +146 -136
- package/package.json +3 -20
- package/src/cli/banner.ts +45 -11
- package/src/cli/commands/app.ts +23 -8
- package/src/cli/commands/create.ts +17 -9
- package/src/cli/commands/dev.ts +0 -3
- package/src/cli/commands/hook.ts +4 -1
- package/src/cli/help.ts +3 -3
- package/src/config/index.ts +6 -5
- package/src/config/project-files.ts +21 -11
- package/src/config/renamed.ts +0 -3
- package/src/config/schema.ts +0 -1
- package/src/content/capture.ts +6 -12
- package/src/content/content.ts +7 -244
- package/src/content/flow-specs.ts +2 -1
- package/src/content/prototype-summary.ts +4 -44
- package/src/content/review-pages.ts +2 -1
- package/src/content/snapshot-client.ts +1 -1
- package/src/content/text-replace.ts +449 -0
- package/src/paths.ts +0 -8
- package/src/server/agent-env.ts +12 -2
- package/src/server/agent-session.ts +124 -34
- package/src/server/brief.ts +8 -3
- package/src/server/channel.ts +121 -52
- package/src/server/claimed-presence.ts +93 -0
- package/src/server/comments/actions.ts +352 -0
- package/src/server/comments/routes.ts +176 -0
- package/src/server/comments/tools.ts +317 -0
- package/src/server/dev-server.ts +49 -15
- package/src/server/file-errors.ts +47 -13
- package/src/server/file-lock.ts +205 -54
- package/src/server/mcp.ts +13 -14
- package/src/server/me.ts +2 -5
- package/src/server/open-page.ts +54 -4
- package/src/server/outcome.ts +26 -0
- package/src/server/people.ts +2 -1
- package/src/server/port.ts +94 -20
- package/src/server/presence.ts +11 -3
- package/src/server/requests.ts +60 -70
- package/src/server/studio-for.ts +27 -3
- package/src/server/sync-install.ts +35 -26
- package/src/server/thread-place.ts +2 -4
- package/src/server/tools/reading.ts +16 -3
- package/src/server/tools/watch.ts +7 -7
- package/src/server/vite.ts +37 -40
- package/src/server/web-app.ts +14 -12
- package/src/setup/apply.ts +10 -16
- package/src/setup/create.ts +143 -37
- package/src/setup/design-system-checks.ts +7 -15
- package/src/setup/desktop-app-script.ts +4 -3
- package/src/setup/desktop-app-start.ts +94 -26
- package/src/setup/desktop-app.ts +24 -3
- package/src/setup/detect.ts +56 -14
- package/src/setup/doctor.ts +4 -4
- package/src/setup/existing-setup.ts +10 -12
- package/src/setup/own-app.ts +2 -5
- package/src/setup/plan.ts +93 -84
- package/src/setup/project.ts +25 -118
- package/src/setup/prompts.ts +44 -127
- package/src/setup/questions.ts +27 -29
- package/src/setup/starters.ts +35 -10
- package/src/setup/workspace-setup.ts +8 -18
- package/src/setup/write.ts +52 -9
- package/src/shared/canvas-file.ts +205 -0
- package/src/shared/comments/routes.ts +79 -0
- package/src/shared/feedback.ts +14 -4
- package/src/shared/page-review.ts +11 -16
- package/src/shared/presence-core.ts +16 -2
- package/src/shared/review-file.ts +28 -12
- package/src/shared/server-page.ts +11 -1
- package/src/shared/signup.ts +4 -28
- package/src/shared/studio-events.ts +0 -13
- package/src/shared/studio-files.ts +14 -0
- package/src/shared/studio-routes.ts +21 -64
- package/src/shared/threads-core.ts +2 -2
- package/templates/methods/review.md +2 -2
- package/templates/rules/canvas-frames.md +104 -0
- package/templates/rules/canvas.md +8 -142
- package/templates/rules/prototypes.md +1 -1
- package/templates/rules/snapshots.md +52 -0
- package/templates/starters/standalone/README.md +7 -7
- package/templates/starters/standalone/components.json +1 -1
- package/templates/starters/standalone/package.json +1 -2
- package/templates/starters/turborepo/README.md +6 -6
- package/templates/starters/turborepo/apps/web/components.json +1 -1
- package/templates/starters/turborepo/apps/web/package.json +1 -2
- package/templates/starters/turborepo/apps/web/src/app.tsx +4 -5
- package/templates/starters/turborepo/packages/ui/components.json +1 -1
- package/templates/starters/turborepo/packages/ui/package.json +1 -2
- package/templates/starters/vite/README.md +7 -6
- package/templates/starters/vite/components.json +1 -1
- package/templates/starters/vite/package.json +1 -2
- package/templates/starters/vite/src/app.tsx +3 -4
- package/templates/starters/vite/src/components/ui/accordion.tsx +71 -0
- package/templates/starters/vite/src/components/ui/attachment.tsx +1 -1
- package/templates/starters/vite/src/components/ui/badge.tsx +2 -2
- package/templates/starters/vite/src/components/ui/button.tsx +17 -12
- package/templates/starters/vite/src/components/ui/card.tsx +1 -1
- package/templates/starters/vite/src/components/ui/checkbox.tsx +28 -0
- package/templates/starters/vite/src/components/ui/dialog.tsx +5 -5
- package/templates/starters/vite/src/components/ui/dropdown-menu.tsx +17 -14
- package/templates/starters/vite/src/components/ui/field.tsx +3 -1
- package/templates/starters/vite/src/components/ui/input-group.tsx +5 -3
- package/templates/starters/vite/src/components/ui/input.tsx +1 -1
- package/templates/starters/vite/src/components/ui/label.tsx +2 -0
- package/templates/starters/vite/src/components/ui/native-select.tsx +3 -4
- package/templates/starters/vite/src/components/ui/popover.tsx +1 -1
- package/templates/starters/vite/src/components/ui/separator.tsx +2 -0
- package/templates/starters/vite/src/components/ui/spinner.tsx +3 -7
- package/templates/starters/vite/src/components/ui/switch.tsx +1 -1
- package/templates/starters/vite/src/components/ui/textarea.tsx +1 -1
- package/templates/starters/vite/src/components/ui/toggle.tsx +4 -2
- package/templates/starters/vite/src/index.css +533 -111
- package/app/icons/sets/hugeicons.tsx +0 -259
- package/app/icons/sets/phosphor.tsx +0 -251
- package/dist/server/icon-set.d.mts +0 -15
- package/dist/server/icon-set.mjs +0 -31
- package/dist/server/tools/close-reply.d.mts +0 -6
- package/dist/server/tools/close-reply.mjs +0 -138
- package/dist/server/tools/comment.d.mts +0 -6
- package/dist/server/tools/comment.mjs +0 -49
- package/dist/shared/icon-sets.d.mts +0 -9
- package/dist/shared/icon-sets.mjs +0 -21
- package/src/server/icon-set.ts +0 -56
- package/src/server/tools/close-reply.ts +0 -271
- package/src/server/tools/comment.ts +0 -96
- package/src/shared/icon-sets.ts +0 -23
- /package/app/public/{sudio-192.png → dex-192.png} +0 -0
- /package/app/public/{sudio-512.png → dex-512.png} +0 -0
- /package/app/public/{apple-touch-icon.png → dex-apple-touch-icon.png} +0 -0
- /package/app/public/{favicon-32.png → dex-icon-32.png} +0 -0
- /package/app/public/{favicon.svg → dex-icon.svg} +0 -0
- /package/app/public/{sudio-maskable-512.png → dex-maskable-512.png} +0 -0
|
@@ -4,15 +4,20 @@
|
|
|
4
4
|
// and another session cannot take it: the thread's 👀 says it was picked
|
|
5
5
|
// up. Claiming marks no view as being edited: the agent's edits do that
|
|
6
6
|
// (`dex hook`, presence.ts), so the canvas shows work only once there is
|
|
7
|
-
// some.
|
|
8
|
-
//
|
|
7
|
+
// some. The dev server marks under the session's id, Claude Code's hook
|
|
8
|
+
// under the id of the Claude session it serves, and both are the session's
|
|
9
|
+
// marks. They end with its work, as the heads-up's "working" ends with its
|
|
10
|
+
// claims: a thread it lets go clears its view, unless it holds another
|
|
11
|
+
// there; holding none clears every mark, and so does going back to `watch`
|
|
12
|
+
// with none. A session that ends lets go of the threads it had not finished.
|
|
9
13
|
|
|
10
14
|
import { randomUUID } from "node:crypto";
|
|
11
15
|
import type { ThreadAnswer, ThreadModule } from "../content/thread-module.ts";
|
|
12
16
|
import type { PresenceTarget } from "../shared/presence-core.ts";
|
|
13
|
-
import
|
|
14
|
-
AgentAction,
|
|
15
|
-
|
|
17
|
+
import {
|
|
18
|
+
type AgentAction,
|
|
19
|
+
claimOf,
|
|
20
|
+
type ThreadWithPrototype,
|
|
16
21
|
} from "../shared/threads-core.ts";
|
|
17
22
|
import { openPresence } from "./presence.ts";
|
|
18
23
|
|
|
@@ -70,30 +75,32 @@ export function openAgentSession(
|
|
|
70
75
|
threads: ThreadModule,
|
|
71
76
|
{
|
|
72
77
|
agent,
|
|
78
|
+
claudeSession,
|
|
73
79
|
id = newSessionId(),
|
|
74
80
|
presence = openPresence(root),
|
|
75
81
|
}: {
|
|
76
82
|
agent: () => string;
|
|
83
|
+
/**
|
|
84
|
+
* The Claude Code session it serves, when it does: Claude's hook marks
|
|
85
|
+
* that agent's edits under its id.
|
|
86
|
+
*/
|
|
87
|
+
claudeSession?: string;
|
|
77
88
|
id?: string;
|
|
78
89
|
presence?: SessionPresence;
|
|
79
90
|
}
|
|
80
91
|
) {
|
|
81
|
-
/**
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
} catch {
|
|
95
|
-
// Presence only shows the work; the thread has changed all the same.
|
|
96
|
-
}
|
|
92
|
+
/** Whom its marks are under: itself, and the Claude Code session it serves. */
|
|
93
|
+
const marksUnder =
|
|
94
|
+
claudeSession && claudeSession !== id ? [id, claudeSession] : [id];
|
|
95
|
+
/**
|
|
96
|
+
* Clear its marks, its sub-agents' too, on one view or doc or everywhere.
|
|
97
|
+
* Presence only shows the work: a clear that fails changes nothing, and
|
|
98
|
+
* marks expire.
|
|
99
|
+
*/
|
|
100
|
+
const clearMarks = async (where?: PresenceTarget): Promise<void> => {
|
|
101
|
+
await Promise.allSettled(
|
|
102
|
+
marksUnder.map((within) => presence.clear({ ...where, within }))
|
|
103
|
+
);
|
|
97
104
|
};
|
|
98
105
|
|
|
99
106
|
/** Threads this session claimed and has not yet closed or answered. */
|
|
@@ -113,6 +120,80 @@ export function openAgentSession(
|
|
|
113
120
|
/** Given to another server (mcp-handover.ts): this one lets go of nothing. */
|
|
114
121
|
let handedOver = false;
|
|
115
122
|
|
|
123
|
+
/** Where each thread it holds is on the canvas, once it has looked. */
|
|
124
|
+
const places = new Map<string, PresenceTarget | undefined>();
|
|
125
|
+
const placeOf = async (
|
|
126
|
+
threadId: string
|
|
127
|
+
): Promise<PresenceTarget | undefined> => {
|
|
128
|
+
if (!places.has(threadId)) {
|
|
129
|
+
const thread = await threads.find(threadId);
|
|
130
|
+
places.set(threadId, thread && presenceTargetOf(thread));
|
|
131
|
+
}
|
|
132
|
+
return places.get(threadId);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Let go of the threads it holds that a person closed or answered since
|
|
137
|
+
* it claimed them, which are no longer its own. One claimed meanwhile
|
|
138
|
+
* stays held.
|
|
139
|
+
*/
|
|
140
|
+
const letGoOfClosed = async (): Promise<void> => {
|
|
141
|
+
const ids = [...held];
|
|
142
|
+
if (ids.length === 0) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const own = new Set(
|
|
146
|
+
(await threads.list({ status: "acknowledged" }))
|
|
147
|
+
.filter((thread) => claimOf(thread)?.session === id)
|
|
148
|
+
.map((thread) => thread.id)
|
|
149
|
+
);
|
|
150
|
+
const closed = ids.filter((each) => !own.has(each));
|
|
151
|
+
if (closed.length > 0) {
|
|
152
|
+
holdingChange(() => {
|
|
153
|
+
for (const each of closed) {
|
|
154
|
+
held.delete(each);
|
|
155
|
+
places.delete(each);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/** Whether it holds a thread on `where`, as far as it knows. */
|
|
162
|
+
const holdsOn = async (where: PresenceTarget): Promise<boolean> =>
|
|
163
|
+
(await Promise.all([...held].map(placeOf))).some(
|
|
164
|
+
(on) => on?.slug === where.slug && on.target === where.target
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Its work on `thread` (or on nothing, waiting) is over: the thread's view
|
|
169
|
+
* shows nobody at work for it, unless it holds another there, and once
|
|
170
|
+
* it holds nothing, nothing it marked does (the file's shared code, other
|
|
171
|
+
* views it changed on the way). What it holds is looked at again first
|
|
172
|
+
* with `recheck`, or when another it holds would keep the view's marks,
|
|
173
|
+
* as a person may have closed it meanwhile. Handed over, its marks are
|
|
174
|
+
* the next server's.
|
|
175
|
+
*/
|
|
176
|
+
const workEnded = async (
|
|
177
|
+
thread: ThreadWithPrototype | undefined,
|
|
178
|
+
recheck: boolean
|
|
179
|
+
): Promise<void> => {
|
|
180
|
+
if (handedOver) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const where = thread && presenceTargetOf(thread);
|
|
184
|
+
if (recheck || (where && (await holdsOn(where)))) {
|
|
185
|
+
// Without the look, it ends what it knows to have ended.
|
|
186
|
+
await letGoOfClosed().catch(() => undefined);
|
|
187
|
+
}
|
|
188
|
+
if (held.size === 0) {
|
|
189
|
+
await clearMarks();
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (where && !(await holdsOn(where))) {
|
|
193
|
+
await clearMarks(where);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
116
197
|
/**
|
|
117
198
|
* An action on a thread, signed by this session: the thread after it, the
|
|
118
199
|
* problems that stopped it, or undefined when there is no such thread.
|
|
@@ -126,22 +207,26 @@ export function openAgentSession(
|
|
|
126
207
|
author: agent(),
|
|
127
208
|
session: id,
|
|
128
209
|
} as AgentAction);
|
|
129
|
-
if (answer?.thread) {
|
|
130
|
-
await clearOnClose(action, answer.thread);
|
|
131
|
-
}
|
|
132
210
|
// Closed, answered or let go, it is no longer this session's to release;
|
|
133
211
|
// nor is one a refused release names (a person answered first).
|
|
134
|
-
|
|
212
|
+
const letGo =
|
|
135
213
|
action.action === "release" ||
|
|
136
|
-
(answer?.thread && LETS_GO.has(action.action))
|
|
137
|
-
) {
|
|
214
|
+
Boolean(answer?.thread && LETS_GO.has(action.action));
|
|
215
|
+
if (letGo) {
|
|
216
|
+
// Done with one it held: the rest are looked at again. One it never
|
|
217
|
+
// held (closed straight from a list) changes nothing it holds.
|
|
218
|
+
const finished = held.has(threadId);
|
|
138
219
|
holdingChange(() => held.delete(threadId));
|
|
220
|
+
places.delete(threadId);
|
|
221
|
+
// Presence only shows the work: the thread has changed all the same.
|
|
222
|
+
await workEnded(answer?.thread, finished).catch(() => undefined);
|
|
139
223
|
}
|
|
140
224
|
return answer;
|
|
141
225
|
};
|
|
142
226
|
|
|
143
227
|
return {
|
|
144
228
|
id,
|
|
229
|
+
marksUnder,
|
|
145
230
|
act,
|
|
146
231
|
/**
|
|
147
232
|
* Claim a thread for this session: it is acknowledged (👀), and the
|
|
@@ -151,6 +236,7 @@ export function openAgentSession(
|
|
|
151
236
|
claim: async (threadId: string) => {
|
|
152
237
|
const answer = await act(threadId, { action: "acknowledge" });
|
|
153
238
|
if (answer?.thread) {
|
|
239
|
+
places.set(threadId, presenceTargetOf(answer.thread));
|
|
154
240
|
holdingChange(() => held.add(threadId));
|
|
155
241
|
}
|
|
156
242
|
return answer;
|
|
@@ -168,6 +254,14 @@ export function openAgentSession(
|
|
|
168
254
|
},
|
|
169
255
|
/** The threads the session holds now, for the server after this one. */
|
|
170
256
|
holding: (): string[] => [...held],
|
|
257
|
+
/**
|
|
258
|
+
* The agent waits for work (`watch`): holding none, it is at work
|
|
259
|
+
* nowhere, as when its turn ends, so nothing it marked shows. Holding
|
|
260
|
+
* some, its marks stay with them. A comment a person closed or answered
|
|
261
|
+
* while it held it is no longer its own, and counts for none.
|
|
262
|
+
*/
|
|
263
|
+
waits: (): Promise<void> =>
|
|
264
|
+
workEnded(undefined, true).catch(() => undefined),
|
|
171
265
|
/** Hear how many threads the session holds, each time that changes. */
|
|
172
266
|
onHoldingChange: (listener: (count: number) => void): void => {
|
|
173
267
|
holdingListeners.add(listener);
|
|
@@ -182,6 +276,7 @@ export function openAgentSession(
|
|
|
182
276
|
holdingChange(() => {
|
|
183
277
|
for (const threadId of given) {
|
|
184
278
|
held.delete(threadId);
|
|
279
|
+
places.delete(threadId);
|
|
185
280
|
}
|
|
186
281
|
});
|
|
187
282
|
},
|
|
@@ -196,13 +291,8 @@ export function openAgentSession(
|
|
|
196
291
|
await Promise.allSettled(
|
|
197
292
|
unfinished.map((threadId) => act(threadId, { action: "release" }))
|
|
198
293
|
);
|
|
199
|
-
if (handedOver) {
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
try {
|
|
203
|
-
await presence.clear({ session: id });
|
|
204
|
-
} catch {
|
|
205
|
-
// Marks expire on their own.
|
|
294
|
+
if (!handedOver) {
|
|
295
|
+
await clearMarks();
|
|
206
296
|
}
|
|
207
297
|
},
|
|
208
298
|
};
|
package/src/server/brief.ts
CHANGED
|
@@ -53,7 +53,7 @@ import { flowSpecs } from "../content/flow-specs.ts";
|
|
|
53
53
|
import { checkPrototype } from "../content/prototype-checks.ts";
|
|
54
54
|
import { prototypePaths } from "../content/prototypes.ts";
|
|
55
55
|
import { readText } from "../content/read-text.ts";
|
|
56
|
-
import { shownFrom } from "../paths.ts";
|
|
56
|
+
import { shownFrom, TEMPLATES_DIR } from "../paths.ts";
|
|
57
57
|
import { designSystemGuide } from "../setup/design-system-guide.ts";
|
|
58
58
|
import {
|
|
59
59
|
ALWAYS_ON,
|
|
@@ -352,12 +352,17 @@ async function guidesSection(
|
|
|
352
352
|
const when = always
|
|
353
353
|
? "Whatever the work: read it first"
|
|
354
354
|
: frontMatterValue(yaml, WHEN_RE);
|
|
355
|
-
|
|
355
|
+
const name = guide.own
|
|
356
|
+
? `\`${show(guide.file)}\` (the team's own)`
|
|
357
|
+
: `\`${guide.path}\``;
|
|
358
|
+
return `${name}${when ? `: ${words(when)}` : ""}`;
|
|
356
359
|
})
|
|
357
360
|
);
|
|
361
|
+
// The package's folder is named once, not on every entry: the brief is
|
|
362
|
+
// in every session, and the prefix was a tenth of it.
|
|
358
363
|
return [
|
|
359
364
|
"## Dex's rules and methods",
|
|
360
|
-
`Read one when the work is what it says.\n\n${listed(
|
|
365
|
+
`Read one when the work is what it says. Dex's are in \`${show(TEMPLATES_DIR)}/\`; the team's own are where they say.\n\n${listed(
|
|
361
366
|
entries.filter(Boolean),
|
|
362
367
|
(more) => `and ${more} in those folders`,
|
|
363
368
|
MAX_GUIDES
|
package/src/server/channel.ts
CHANGED
|
@@ -38,7 +38,9 @@ import {
|
|
|
38
38
|
} from "../shared/threads-core.ts";
|
|
39
39
|
import type { AgentSession } from "./agent-session.ts";
|
|
40
40
|
import { readCloudConfig } from "./cloud.ts";
|
|
41
|
+
import { claimComment } from "./comments/actions.ts";
|
|
41
42
|
import { yoursGroups } from "./own-copies.ts";
|
|
43
|
+
import { openPresence } from "./presence.ts";
|
|
42
44
|
import { firstOf, RESPONSE_BUDGETS } from "./response-budget.ts";
|
|
43
45
|
import { prepareState, studioState } from "./state.ts";
|
|
44
46
|
import { FOR_WORK, type Form, SHORT, shaped } from "./thread-forms.ts";
|
|
@@ -224,35 +226,55 @@ export function openReceipt({
|
|
|
224
226
|
/** A session's heartbeat file: who it is, and when it last beat. */
|
|
225
227
|
interface Heartbeat extends ConnectedAgent {
|
|
226
228
|
at: number;
|
|
229
|
+
/** The Claude Code session it serves, whose hook marks presence under it. */
|
|
230
|
+
claudeSession?: string;
|
|
227
231
|
/** The agent session's id, which signs its claims; missing from older ones. */
|
|
228
232
|
session?: string;
|
|
229
233
|
/** The Dex version the session runs; missing from older ones. */
|
|
230
234
|
version?: string;
|
|
231
235
|
}
|
|
232
236
|
|
|
233
|
-
/** What
|
|
234
|
-
type
|
|
237
|
+
/** What lets go of a gone session's threads: the studio's thread module. */
|
|
238
|
+
type SessionThreads = Pick<ThreadModule, "act" | "list">;
|
|
239
|
+
|
|
240
|
+
/** The ids a heartbeat's session works under: its own, and its Claude session's. */
|
|
241
|
+
const idsOf = ({ claudeSession, session }: Heartbeat): string[] =>
|
|
242
|
+
[session, claudeSession].filter((each): each is string => each !== undefined);
|
|
235
243
|
|
|
236
244
|
/**
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
245
|
+
* End the work of a session whose heartbeat stopped without its saying so,
|
|
246
|
+
* as a session that crashed or was killed never did (agent-session.ts):
|
|
247
|
+
* clear its marks and its Claude session's, so the canvas shows it at work
|
|
248
|
+
* nowhere, and, given `threads`, let go of the threads it still holds, so
|
|
249
|
+
* they wait for an agent again instead of showing its 👀 for good. Ids
|
|
250
|
+
* still `beating` are left alone: a session moved to another server
|
|
251
|
+
* (mcp-handover.ts) beats there under the same ids.
|
|
240
252
|
*/
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
253
|
+
const endGone = async (
|
|
254
|
+
root: string,
|
|
255
|
+
beat: Heartbeat,
|
|
256
|
+
beating: ReadonlySet<string>,
|
|
257
|
+
threads?: SessionThreads
|
|
258
|
+
): Promise<void> => {
|
|
259
|
+
const { agent, session } = beat;
|
|
260
|
+
if (!session || beating.has(session)) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const releases = threads
|
|
264
|
+
? (await threads.list({ status: "acknowledged" }))
|
|
265
|
+
.filter((thread) => claimOf(thread)?.session === session)
|
|
266
|
+
.map((thread) =>
|
|
267
|
+
threads.act(thread.id, { action: "release", author: agent, session })
|
|
268
|
+
)
|
|
269
|
+
: [];
|
|
270
|
+
const presence = openPresence(root);
|
|
271
|
+
await Promise.allSettled([
|
|
272
|
+
...releases,
|
|
273
|
+
...idsOf(beat)
|
|
274
|
+
.filter((each) => !beating.has(each))
|
|
275
|
+
.map((within) => presence.clear({ within })),
|
|
276
|
+
]);
|
|
277
|
+
};
|
|
256
278
|
|
|
257
279
|
/**
|
|
258
280
|
* Let go of the threads held by sessions that are no longer connected, so a
|
|
@@ -266,7 +288,7 @@ export const releaseGone =
|
|
|
266
288
|
*/
|
|
267
289
|
export async function releaseAbandoned(
|
|
268
290
|
root: string,
|
|
269
|
-
threads:
|
|
291
|
+
threads: SessionThreads,
|
|
270
292
|
own: string,
|
|
271
293
|
{
|
|
272
294
|
now = Date.now(),
|
|
@@ -342,15 +364,20 @@ export async function setWatching(root: string, on: boolean): Promise<void> {
|
|
|
342
364
|
* The agents connected to the studio over MCP, from their heartbeats: each
|
|
343
365
|
* one's name and whether it is a channel (feedback reaches it by itself).
|
|
344
366
|
* Heartbeats left by sessions that ended without saying so are cleared once
|
|
345
|
-
* they are `listeningMs` old at `now`,
|
|
367
|
+
* they are `listeningMs` old at `now`, each session's work ended first
|
|
368
|
+
* (endGone), its threads let go given `threads`.
|
|
346
369
|
*/
|
|
347
370
|
export async function connectedAgents(
|
|
348
371
|
root: string,
|
|
349
372
|
{
|
|
350
373
|
now = Date.now(),
|
|
351
374
|
listeningMs = LISTENING_MS,
|
|
352
|
-
|
|
353
|
-
}: {
|
|
375
|
+
threads,
|
|
376
|
+
}: {
|
|
377
|
+
listeningMs?: number;
|
|
378
|
+
now?: number;
|
|
379
|
+
threads?: SessionThreads;
|
|
380
|
+
} = {}
|
|
354
381
|
): Promise<ConnectedAgent[]> {
|
|
355
382
|
const dir = studioState(root).agents;
|
|
356
383
|
let names: string[] = [];
|
|
@@ -359,33 +386,71 @@ export async function connectedAgents(
|
|
|
359
386
|
} catch {
|
|
360
387
|
return [];
|
|
361
388
|
}
|
|
362
|
-
const
|
|
389
|
+
const beats: { beat: Heartbeat; file: string; name: string }[] = [];
|
|
363
390
|
for (const name of names) {
|
|
364
391
|
const file = join(dir, name);
|
|
365
392
|
try {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
channel: Boolean(beat.channel),
|
|
372
|
-
...pidOf(name),
|
|
373
|
-
...(beat.unproven && !beat.channel ? { unproven: true } : {}),
|
|
374
|
-
...(beat.watching ? { watching: true } : {}),
|
|
375
|
-
...(beat.holding ? { holding: beat.holding } : {}),
|
|
376
|
-
...outdatedOf(beat.version),
|
|
377
|
-
});
|
|
378
|
-
} else {
|
|
379
|
-
await onGone?.(beat).catch(() => undefined);
|
|
380
|
-
await rm(file, { force: true });
|
|
381
|
-
}
|
|
393
|
+
beats.push({
|
|
394
|
+
beat: JSON.parse(await readFile(file, "utf8")),
|
|
395
|
+
file,
|
|
396
|
+
name,
|
|
397
|
+
});
|
|
382
398
|
} catch {
|
|
383
399
|
// A heartbeat mid-write: it will be whole next time.
|
|
384
400
|
}
|
|
385
401
|
}
|
|
402
|
+
const isFresh = (beat: Heartbeat) => now - beat.at < listeningMs;
|
|
403
|
+
const beating = new Set(
|
|
404
|
+
beats.flatMap(({ beat }) => (isFresh(beat) ? idsOf(beat) : []))
|
|
405
|
+
);
|
|
406
|
+
const agents: ConnectedAgent[] = [];
|
|
407
|
+
for (const { beat, file, name } of beats) {
|
|
408
|
+
if (isFresh(beat)) {
|
|
409
|
+
// Sessions from before versions were written run an older one.
|
|
410
|
+
agents.push({
|
|
411
|
+
agent: beat.agent,
|
|
412
|
+
channel: Boolean(beat.channel),
|
|
413
|
+
...pidOf(name),
|
|
414
|
+
...(beat.unproven && !beat.channel ? { unproven: true } : {}),
|
|
415
|
+
...(beat.watching ? { watching: true } : {}),
|
|
416
|
+
...(beat.holding ? { holding: beat.holding } : {}),
|
|
417
|
+
...outdatedOf(beat.version),
|
|
418
|
+
});
|
|
419
|
+
} else {
|
|
420
|
+
await endGone(root, beat, beating, threads).catch(() => undefined);
|
|
421
|
+
await rm(file, { force: true });
|
|
422
|
+
}
|
|
423
|
+
}
|
|
386
424
|
return agents;
|
|
387
425
|
}
|
|
388
426
|
|
|
427
|
+
/**
|
|
428
|
+
* The agent sessions running on this machine now, by their heartbeats: not a
|
|
429
|
+
* teammate's, whose claims come from the workspace. Reads only; clearing old
|
|
430
|
+
* heartbeats is connectedAgents's.
|
|
431
|
+
*/
|
|
432
|
+
export async function localSessions(
|
|
433
|
+
root: string,
|
|
434
|
+
now = Date.now()
|
|
435
|
+
): Promise<Set<string>> {
|
|
436
|
+
const dir = studioState(root).agents;
|
|
437
|
+
const sessions = new Set<string>();
|
|
438
|
+
const names = await readdir(dir).catch(() => []);
|
|
439
|
+
for (const name of names.filter(isHeartbeat)) {
|
|
440
|
+
try {
|
|
441
|
+
const beat: Heartbeat = JSON.parse(
|
|
442
|
+
await readFile(join(dir, name), "utf8")
|
|
443
|
+
);
|
|
444
|
+
if (beat.session && now - beat.at < LISTENING_MS) {
|
|
445
|
+
sessions.add(beat.session);
|
|
446
|
+
}
|
|
447
|
+
} catch {
|
|
448
|
+
// A heartbeat mid-write: it will be whole next time.
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return sessions;
|
|
452
|
+
}
|
|
453
|
+
|
|
389
454
|
/** A heartbeat file's process, as it is named: "62653.json". */
|
|
390
455
|
const pidOf = (name: string): { pid?: number } => {
|
|
391
456
|
const digits = PID_FILE_RE.exec(name)?.[1];
|
|
@@ -439,9 +504,10 @@ const commandOf = (pid: number): Promise<string | undefined> =>
|
|
|
439
504
|
*/
|
|
440
505
|
export async function stopAgentServer(
|
|
441
506
|
root: string,
|
|
442
|
-
pid: number
|
|
507
|
+
pid: number,
|
|
508
|
+
threads?: SessionThreads
|
|
443
509
|
): Promise<void> {
|
|
444
|
-
const agent = (await connectedAgents(root)).find(
|
|
510
|
+
const agent = (await connectedAgents(root, { threads })).find(
|
|
445
511
|
(each) => each.pid === pid && each.outdated?.restartOnce
|
|
446
512
|
);
|
|
447
513
|
if (!agent) {
|
|
@@ -488,7 +554,7 @@ const everyTick: Tick = (check) => {
|
|
|
488
554
|
* when it changes: an agent connects, or its heartbeat stops and passes the
|
|
489
555
|
* listening window. A page hears the list as it is when it starts listening.
|
|
490
556
|
* Nothing is checked while no page listens. Given `threads`, the threads a
|
|
491
|
-
* gone session held are let go (
|
|
557
|
+
* gone session held are let go (endGone). `now` and `tick` are for tests.
|
|
492
558
|
*/
|
|
493
559
|
export function trackAgents(
|
|
494
560
|
root: string,
|
|
@@ -498,11 +564,10 @@ export function trackAgents(
|
|
|
498
564
|
threads,
|
|
499
565
|
}: {
|
|
500
566
|
now?: () => number;
|
|
501
|
-
threads?:
|
|
567
|
+
threads?: SessionThreads;
|
|
502
568
|
tick?: Tick;
|
|
503
569
|
} = {}
|
|
504
570
|
) {
|
|
505
|
-
const onGone = threads ? releaseGone(threads) : undefined;
|
|
506
571
|
const pages = new Set<(agents: ConnectedAgent[]) => void>();
|
|
507
572
|
// The list as the pages last heard it; undefined until the first check.
|
|
508
573
|
let agents: ConnectedAgent[] | undefined;
|
|
@@ -510,7 +575,7 @@ export function trackAgents(
|
|
|
510
575
|
let stopTicking: (() => void) | undefined;
|
|
511
576
|
|
|
512
577
|
const check = () => {
|
|
513
|
-
checking ??= connectedAgents(root, { now: now(),
|
|
578
|
+
checking ??= connectedAgents(root, { now: now(), threads })
|
|
514
579
|
.then((found) => {
|
|
515
580
|
if (JSON.stringify(found) !== JSON.stringify(agents)) {
|
|
516
581
|
agents = found;
|
|
@@ -591,11 +656,14 @@ export function startHeartbeat(
|
|
|
591
656
|
describe: () => ConnectedAgent,
|
|
592
657
|
{
|
|
593
658
|
beatMs = BEAT_MS,
|
|
659
|
+
claudeSession,
|
|
594
660
|
onEnd,
|
|
595
661
|
session,
|
|
596
662
|
write = writeWhole,
|
|
597
663
|
}: {
|
|
598
664
|
beatMs?: number;
|
|
665
|
+
/** The Claude Code session it serves, when it does. */
|
|
666
|
+
claudeSession?: string;
|
|
599
667
|
onEnd?: () => Promise<void>;
|
|
600
668
|
session?: string;
|
|
601
669
|
/** How a beat is written (for tests): whole, or not at all. */
|
|
@@ -621,6 +689,7 @@ export function startHeartbeat(
|
|
|
621
689
|
...describe(),
|
|
622
690
|
at: Date.now(),
|
|
623
691
|
...(session ? { session } : {}),
|
|
692
|
+
...(claudeSession ? { claudeSession } : {}),
|
|
624
693
|
version: runningVersion,
|
|
625
694
|
})
|
|
626
695
|
);
|
|
@@ -990,17 +1059,17 @@ export async function startChannel(
|
|
|
990
1059
|
}
|
|
991
1060
|
const claimed: ThreadWithPrototype[] = [];
|
|
992
1061
|
for (const thread of [...held.values()]) {
|
|
993
|
-
let answer: Awaited<ReturnType<
|
|
1062
|
+
let answer: Awaited<ReturnType<typeof claimComment>>;
|
|
994
1063
|
try {
|
|
995
|
-
answer = await session
|
|
1064
|
+
answer = await claimComment({ session }, thread.id);
|
|
996
1065
|
} catch {
|
|
997
1066
|
continue;
|
|
998
1067
|
}
|
|
999
1068
|
held.delete(thread.id);
|
|
1000
1069
|
sent.add(stamp(thread));
|
|
1001
|
-
if (answer
|
|
1002
|
-
sent.add(stamp(answer.
|
|
1003
|
-
claimed.push(answer.
|
|
1070
|
+
if (answer.value) {
|
|
1071
|
+
sent.add(stamp(answer.value));
|
|
1072
|
+
claimed.push(answer.value);
|
|
1004
1073
|
}
|
|
1005
1074
|
}
|
|
1006
1075
|
if (claimed.length === 0) {
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// An agent at work on a comment shows on the canvas once it edits the view or
|
|
2
|
+
// doc the comment is on, whichever agent it is. Claude Code marks its own
|
|
3
|
+
// edits through its hook (`dex hook`), but Codex, Cursor and the rest have no
|
|
4
|
+
// hook, so the dev server, which sees every file in a prototype change, marks
|
|
5
|
+
// the session on this machine holding a claimed thread there. The mark is the
|
|
6
|
+
// thread's own target, so closing the thread clears it (agent-session.ts);
|
|
7
|
+
// otherwise it expires as a hook's does. A thread closed while its mark is
|
|
8
|
+
// written would leave it behind, so the claims are looked at again after.
|
|
9
|
+
|
|
10
|
+
import { relative } from "node:path";
|
|
11
|
+
import type { ThreadModule } from "../content/thread-module.ts";
|
|
12
|
+
import { targetForPath } from "../shared/live-presence.ts";
|
|
13
|
+
import type { PresenceTarget } from "../shared/presence-core.ts";
|
|
14
|
+
import { claimOf } from "../shared/threads-core.ts";
|
|
15
|
+
import { presenceTargetOf } from "./agent-session.ts";
|
|
16
|
+
import type { PresenceMark, PresenceMatch } from "./presence.ts";
|
|
17
|
+
|
|
18
|
+
/** How often one claim's session is marked again while it edits. */
|
|
19
|
+
const REMARK_MS = 10_000;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Mark the agent sessions with a claimed thread on the view or doc `file`
|
|
23
|
+
* belongs to as working on it. `file` is absolute; `content` is the studio's
|
|
24
|
+
* content folder. `sessions` are the agent sessions running on this machine:
|
|
25
|
+
* a teammate's claim, synced from the workspace, is never marked here.
|
|
26
|
+
*/
|
|
27
|
+
export function claimedPresence({
|
|
28
|
+
clear,
|
|
29
|
+
content,
|
|
30
|
+
mark,
|
|
31
|
+
sessions,
|
|
32
|
+
threads,
|
|
33
|
+
now = Date.now,
|
|
34
|
+
}: {
|
|
35
|
+
clear: (match: PresenceMatch) => Promise<void>;
|
|
36
|
+
content: string;
|
|
37
|
+
mark: (entry: PresenceMark) => Promise<void>;
|
|
38
|
+
sessions: () => Promise<Set<string>>;
|
|
39
|
+
threads: Pick<ThreadModule, "list">;
|
|
40
|
+
now?: () => number;
|
|
41
|
+
}) {
|
|
42
|
+
const marked = new Map<string, number>();
|
|
43
|
+
/** The claimed threads on the view or doc edited. */
|
|
44
|
+
const claimsOn = async (edited: PresenceTarget) =>
|
|
45
|
+
(
|
|
46
|
+
await threads.list({ prototype: edited.slug, status: "acknowledged" })
|
|
47
|
+
).filter((thread) => presenceTargetOf(thread)?.target === edited.target);
|
|
48
|
+
return async (file: string): Promise<void> => {
|
|
49
|
+
const edited = targetForPath(relative(content, file).replaceAll("\\", "/"));
|
|
50
|
+
if (!edited || edited.target === "prototype") {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const claimed = await claimsOn(edited);
|
|
54
|
+
const time = now();
|
|
55
|
+
const marks: PresenceMark[] = [];
|
|
56
|
+
let local: Set<string> | undefined;
|
|
57
|
+
for (const thread of claimed) {
|
|
58
|
+
const claim = claimOf(thread);
|
|
59
|
+
const session = claim?.session;
|
|
60
|
+
if (!(claim && session)) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
// Each claim's first edit marks at once, even just after another
|
|
64
|
+
// claim on the same view was resolved and its mark cleared.
|
|
65
|
+
const key = [session, thread.id, edited.slug, edited.target].join(
|
|
66
|
+
"\u0001"
|
|
67
|
+
);
|
|
68
|
+
if (time - (marked.get(key) ?? Number.NEGATIVE_INFINITY) < REMARK_MS) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
local ??= await sessions();
|
|
72
|
+
if (!local.has(session)) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
marked.set(key, time);
|
|
76
|
+
marks.push({ ...edited, agent: claim.by, session });
|
|
77
|
+
}
|
|
78
|
+
if (marks.length === 0) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
await Promise.all(marks.map(mark));
|
|
82
|
+
// A claim closed since it was looked up has cleared its marks already,
|
|
83
|
+
// maybe before this one landed: clear the mark of a claim no longer held.
|
|
84
|
+
const holding = new Set(
|
|
85
|
+
(await claimsOn(edited)).map((thread) => claimOf(thread)?.session)
|
|
86
|
+
);
|
|
87
|
+
await Promise.all(
|
|
88
|
+
marks
|
|
89
|
+
.filter(({ session }) => !holding.has(session))
|
|
90
|
+
.map(({ session }) => clear({ ...edited, session }))
|
|
91
|
+
);
|
|
92
|
+
};
|
|
93
|
+
}
|