agent-canvas 0.9.0-dev.1 → 0.9.0-dev.11
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/README.md +10 -0
- package/bin/lib/commands/review-remote.ts +186 -0
- package/bin/lib/commands/review.ts +88 -25
- package/bin/lib/instructions/app-review.md +31 -0
- package/daemon/build.ts +2 -0
- package/daemon/client/App.tsx +1 -1
- package/daemon/client/FileViewer.tsx +1 -1
- package/daemon/dist/client.css +2 -2
- package/daemon/dist/review.js +33 -3
- package/daemon/review-widget/anchor.ts +21 -4
- package/daemon/review-widget/browser.ts +46 -0
- package/daemon/review-widget/client.ts +23 -1
- package/daemon/review-widget/index.tsx +69 -11
- package/daemon/review-widget/remote-client.ts +117 -0
- package/daemon/review-widget/source.ts +43 -7
- package/daemon/review-widget/style.ts +51 -0
- package/daemon/review-widget/styles.css +30 -0
- package/daemon/review-widget/tsconfig.json +2 -1
- package/daemon/review-widget/widget.tsx +115 -17
- package/daemon/src/review/markdown.ts +6 -1
- package/daemon/src/review/model.ts +13 -1
- package/daemon/src/review/proxy.ts +30 -3
- package/daemon/src/review/remote.ts +112 -0
- package/daemon/src/review/sealing.ts +35 -0
- package/daemon/src/review/server.ts +16 -1
- package/daemon/src/review/store.ts +20 -28
- package/daemon/src/review/threads.ts +63 -0
- package/daemon/src/server.ts +1 -1
- package/daemon/tsconfig.json +1 -1
- package/package.json +23 -6
- package/skills/canvas/SKILL.md +2 -1
- package/types/bin/agent-canvas.d.ts +2 -0
- package/types/bin/lib/commands/daemon.d.ts +1 -0
- package/types/bin/lib/commands/exec.d.ts +7 -0
- package/types/bin/lib/commands/fetch.d.ts +1 -0
- package/types/bin/lib/commands/install.d.ts +1 -0
- package/types/bin/lib/commands/instructions.d.ts +1 -0
- package/types/bin/lib/commands/push.d.ts +1 -0
- package/types/bin/lib/commands/review-remote.d.ts +52 -0
- package/types/bin/lib/commands/review.d.ts +1 -0
- package/types/bin/lib/commands/watch.d.ts +1 -0
- package/types/bin/lib/config.d.ts +9 -0
- package/types/bin/lib/daemon-lifecycle.d.ts +4 -0
- package/types/bin/lib/helpers.d.ts +13 -0
- package/types/daemon/build.d.ts +1 -0
- package/types/daemon/client/App.d.ts +1 -0
- package/types/daemon/client/CompareView.d.ts +17 -0
- package/types/daemon/client/FileBrowser.d.ts +4 -0
- package/types/daemon/client/FileViewer.d.ts +6 -0
- package/types/daemon/client/OverviewView.d.ts +18 -0
- package/types/daemon/client/ReviewerIdentityDialog.d.ts +17 -0
- package/types/daemon/client/RevisionSelect.d.ts +10 -0
- package/types/daemon/client/SessionSwitcher.d.ts +6 -0
- package/types/daemon/client/ShareDialog.d.ts +30 -0
- package/types/daemon/client/apiSchemas.d.ts +1311 -0
- package/types/daemon/client/canvas-runtime.d.ts +1 -0
- package/types/daemon/client/clientApi.d.ts +112 -0
- package/types/daemon/client/components/SecretInput.d.ts +10 -0
- package/types/daemon/client/components/index.d.ts +2 -0
- package/types/daemon/client/domDiff.d.ts +37 -0
- package/types/daemon/client/errorMessage.d.ts +1 -0
- package/types/daemon/client/exportMarkdown.d.ts +1 -0
- package/types/daemon/client/lcs.d.ts +2 -0
- package/types/daemon/client/shareCrypto.d.ts +40 -0
- package/types/daemon/client/unifiedDiff.d.ts +26 -0
- package/types/daemon/langMap.d.ts +2 -0
- package/types/daemon/review-widget/anchor.d.ts +18 -0
- package/types/daemon/review-widget/browser.d.ts +6 -0
- package/types/daemon/review-widget/client.d.ts +38 -0
- package/types/daemon/review-widget/index.d.ts +1 -0
- package/types/daemon/review-widget/markup.d.ts +4 -0
- package/types/daemon/review-widget/remote-client.d.ts +29 -0
- package/types/daemon/review-widget/screenshot.d.ts +8 -0
- package/types/daemon/review-widget/source.d.ts +17 -0
- package/types/daemon/review-widget/style.d.ts +7 -0
- package/types/daemon/review-widget/widget.d.ts +7 -0
- package/types/daemon/src/components.d.ts +10 -0
- package/types/daemon/src/handlers/api.d.ts +11 -0
- package/types/daemon/src/handlers/files.d.ts +3 -0
- package/types/daemon/src/handlers/static.d.ts +2 -0
- package/types/daemon/src/handlers/uploads.d.ts +3 -0
- package/types/daemon/src/paths.d.ts +12 -0
- package/types/daemon/src/remote-feedback.d.ts +4 -0
- package/types/daemon/src/review/markdown.d.ts +3 -0
- package/types/daemon/src/review/model.d.ts +1089 -0
- package/types/daemon/src/review/proxy.d.ts +30 -0
- package/types/daemon/src/review/remote.d.ts +258 -0
- package/types/daemon/src/review/sealing.d.ts +11 -0
- package/types/daemon/src/review/server.d.ts +24 -0
- package/types/daemon/src/review/store.d.ts +34 -0
- package/types/daemon/src/review/threads.d.ts +35 -0
- package/types/daemon/src/server.d.ts +7 -0
- package/types/daemon/src/share.d.ts +98 -0
- package/types/daemon/src/stubFetch.d.ts +2 -0
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { ReviewAnchor } from '../src/review/model'
|
|
2
|
+
import { browserOf } from './browser'
|
|
2
3
|
import { contextOf, htmlOf } from './markup'
|
|
4
|
+
import { createdInLibrary } from './source'
|
|
5
|
+
import { styleOf } from './style'
|
|
3
6
|
|
|
4
7
|
const MAX_TEXT = 120
|
|
5
8
|
const QUOTE_CONTEXT = 40
|
|
@@ -10,6 +13,14 @@ function shorten(text: string, max = MAX_TEXT): string {
|
|
|
10
13
|
return collapsed.length > max ? `${collapsed.slice(0, max - 1)}…` : collapsed
|
|
11
14
|
}
|
|
12
15
|
|
|
16
|
+
/** The element's text with a space between text nodes: adjacent inline elements otherwise run their words together. */
|
|
17
|
+
function textOf(element: Element): string {
|
|
18
|
+
const walker = element.ownerDocument.createTreeWalker(element, NodeFilter.SHOW_TEXT)
|
|
19
|
+
const parts: string[] = []
|
|
20
|
+
for (let node = walker.nextNode(); node !== null; node = walker.nextNode()) parts.push(node.textContent ?? '')
|
|
21
|
+
return parts.join(' ')
|
|
22
|
+
}
|
|
23
|
+
|
|
13
24
|
const IMPLICIT_ROLES: Record<string, string> = {
|
|
14
25
|
button: 'button',
|
|
15
26
|
select: 'combobox',
|
|
@@ -65,7 +76,8 @@ export function nameOf(element: Element): string | undefined {
|
|
|
65
76
|
const role = roleOf(element)
|
|
66
77
|
const namedByContent = role === 'button' || role === 'link' || role === 'heading' || role === 'cell' || role === 'columnheader' || role === 'tab'
|
|
67
78
|
|| role === 'menuitem' || role === 'option'
|
|
68
|
-
|
|
79
|
+
const text = textOf(element)
|
|
80
|
+
if (namedByContent && text.trim() !== '') return shorten(text)
|
|
69
81
|
return undefined
|
|
70
82
|
}
|
|
71
83
|
|
|
@@ -114,8 +126,9 @@ export function componentsOf(element: Element): string[] {
|
|
|
114
126
|
while (isRecord(fiber) && names.length < 40) {
|
|
115
127
|
// Bundlers suffix a name that collides in their scope (`Card` → `Card13`); the source still says `Card`.
|
|
116
128
|
const name = componentName(fiber['type'])?.replace(/(?<=[a-z])\d+$/, '')
|
|
117
|
-
// Context providers wrap whole apps and say nothing about where the element is.
|
|
118
|
-
|
|
129
|
+
// Context providers wrap whole apps and say nothing about where the element is; nor do a library's own wrappers.
|
|
130
|
+
const telling = name !== undefined && /^[A-Z]/.test(name) && !name.endsWith('Provider') && !createdInLibrary(fiber)
|
|
131
|
+
if (telling && names[0] !== name) names.unshift(name)
|
|
119
132
|
fiber = fiber['return']
|
|
120
133
|
}
|
|
121
134
|
return names.slice(-MAX_COMPONENTS)
|
|
@@ -144,10 +157,12 @@ function quoteOf(element: Element, range: Range): ReviewAnchor['quote'] {
|
|
|
144
157
|
export function describe(element: Element, url: string, range?: Range): ReviewAnchor {
|
|
145
158
|
const role = roleOf(element)
|
|
146
159
|
const name = nameOf(element)
|
|
147
|
-
const text =
|
|
160
|
+
const text = shorten(textOf(element))
|
|
148
161
|
const components = componentsOf(element)
|
|
149
162
|
const quote = range === undefined ? undefined : quoteOf(element, range)
|
|
150
163
|
const context = contextOf(element)
|
|
164
|
+
const view = element.ownerDocument.defaultView
|
|
165
|
+
const style = view === null ? '' : styleOf(view.getComputedStyle(element))
|
|
151
166
|
return {
|
|
152
167
|
url,
|
|
153
168
|
selector: selectorOf(element),
|
|
@@ -157,6 +172,8 @@ export function describe(element: Element, url: string, range?: Range): ReviewAn
|
|
|
157
172
|
...(components.length === 0 ? {} : { components }),
|
|
158
173
|
...(quote === undefined ? {} : { quote }),
|
|
159
174
|
...(context === undefined ? {} : { context }),
|
|
175
|
+
...(view === null ? {} : { browser: browserOf(view) }),
|
|
176
|
+
...(style === '' ? {} : { style }),
|
|
160
177
|
html: htmlOf(element),
|
|
161
178
|
}
|
|
162
179
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ReviewAnchor } from '../src/review/model'
|
|
2
|
+
|
|
3
|
+
type ReviewBrowser = NonNullable<ReviewAnchor['browser']>
|
|
4
|
+
|
|
5
|
+
const MAX_UNKNOWN = 100
|
|
6
|
+
|
|
7
|
+
/** Most specific first: Edge and Opera also name Chrome, and Chrome also names Safari. */
|
|
8
|
+
const BROWSERS: ReadonlyArray<readonly [string, RegExp]> = [
|
|
9
|
+
['Edge', /\bEdg(?:e|A|iOS)?\/(\d+)/],
|
|
10
|
+
['Opera', /\bOPR\/(\d+)/],
|
|
11
|
+
['Firefox', /\b(?:Firefox|FxiOS)\/(\d+)/],
|
|
12
|
+
['Chrome', /\b(?:Chrome|CriOS)\/(\d+)/],
|
|
13
|
+
['Safari', /\bVersion\/(\d+)[^ ]* (?:Mobile\/\S+ )?Safari\//],
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
/** Most specific first: Android also names Linux, and iOS names Mac OS X. */
|
|
17
|
+
const SYSTEMS: ReadonlyArray<readonly [string, RegExp]> = [
|
|
18
|
+
['Android', /\bAndroid\b/],
|
|
19
|
+
['iOS', /\b(?:iPhone|iPad|iPod)\b/],
|
|
20
|
+
['Windows', /\bWindows\b/],
|
|
21
|
+
['macOS', /\bMac OS X\b/],
|
|
22
|
+
['Linux', /\bLinux\b/],
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
/** Browser, major version and operating system, e.g. `Chrome 141 · Linux`; the rest of a user agent is noise. */
|
|
26
|
+
export function browserName(userAgent: string): string {
|
|
27
|
+
let browser: string | undefined
|
|
28
|
+
for (const [name, pattern] of BROWSERS) {
|
|
29
|
+
const version = pattern.exec(userAgent)?.[1]
|
|
30
|
+
if (version !== undefined) {
|
|
31
|
+
browser = `${name} ${version}`
|
|
32
|
+
break
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const system = SYSTEMS.find(([, pattern]) => pattern.test(userAgent))?.[0]
|
|
36
|
+
const parts = [browser, system].filter((part) => part !== undefined)
|
|
37
|
+
return parts.length === 0 ? userAgent.slice(0, MAX_UNKNOWN) : parts.join(' · ')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function browserOf(view: Window): ReviewBrowser {
|
|
41
|
+
return {
|
|
42
|
+
name: browserName(view.navigator.userAgent),
|
|
43
|
+
viewport: { width: view.innerWidth, height: view.innerHeight, scale: view.devicePixelRatio },
|
|
44
|
+
colorScheme: view.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light',
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -7,6 +7,7 @@ export interface ReviewEvents {
|
|
|
7
7
|
threads(threads: readonly ReviewThread[]): void
|
|
8
8
|
agentWatching(watching: boolean): void
|
|
9
9
|
connected(connected: boolean): void
|
|
10
|
+
reload(path: string): void
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
@@ -24,13 +25,29 @@ function readFrame(data: unknown): ReviewBrowserFrame | undefined {
|
|
|
24
25
|
}
|
|
25
26
|
if (!isRecord(frame)) return undefined
|
|
26
27
|
if (frame['type'] === 'agent-status' && typeof frame['watching'] === 'boolean') return { type: 'agent-status', watching: frame['watching'] }
|
|
28
|
+
if (frame['type'] === 'reload' && typeof frame['path'] === 'string') return { type: 'reload', path: frame['path'] }
|
|
27
29
|
if (frame['type'] !== 'threads') return undefined
|
|
28
30
|
const parsed = threadListSchema.safeParse(frame)
|
|
29
31
|
return parsed.success ? { type: 'threads', threads: parsed.data.threads } : undefined
|
|
30
32
|
}
|
|
31
33
|
|
|
34
|
+
/** Where the widget keeps threads: the local daemon, or a remote project on the share worker. */
|
|
35
|
+
export interface ReviewClient {
|
|
36
|
+
/** The presence label while the widget cannot reach its store. */
|
|
37
|
+
readonly offlineLabel: string
|
|
38
|
+
connect(events: ReviewEvents): void
|
|
39
|
+
close(): void
|
|
40
|
+
createThread(anchor: ReviewAnchor, body: string, send: boolean, screenshot?: string): Promise<unknown>
|
|
41
|
+
comment(threadId: string, body: string, send: boolean): Promise<unknown>
|
|
42
|
+
send(threadId: string): Promise<unknown>
|
|
43
|
+
sendNotes(): Promise<unknown>
|
|
44
|
+
setResolved(threadId: string, resolved: boolean): Promise<unknown>
|
|
45
|
+
delete(threadId: string): Promise<unknown>
|
|
46
|
+
}
|
|
47
|
+
|
|
32
48
|
/** The widget's side of the daemon: thread mutations over HTTP, the thread list and agent presence over a WebSocket. */
|
|
33
|
-
export class ReviewClient {
|
|
49
|
+
export class DaemonReviewClient implements ReviewClient {
|
|
50
|
+
readonly offlineLabel = 'Daemon offline'
|
|
34
51
|
private socket: WebSocket | null = null
|
|
35
52
|
private closed = false
|
|
36
53
|
|
|
@@ -51,6 +68,7 @@ export class ReviewClient {
|
|
|
51
68
|
const frame = readFrame(event.data)
|
|
52
69
|
if (frame?.type === 'threads') events.threads(frame.threads)
|
|
53
70
|
if (frame?.type === 'agent-status') events.agentWatching(frame.watching)
|
|
71
|
+
if (frame?.type === 'reload') events.reload(frame.path)
|
|
54
72
|
}
|
|
55
73
|
socket.onclose = () => {
|
|
56
74
|
events.connected(false)
|
|
@@ -75,6 +93,10 @@ export class ReviewClient {
|
|
|
75
93
|
return this.request('POST', `/threads/${threadId}/send`)
|
|
76
94
|
}
|
|
77
95
|
|
|
96
|
+
sendNotes(): Promise<unknown> {
|
|
97
|
+
return this.request('POST', '/notes/send')
|
|
98
|
+
}
|
|
99
|
+
|
|
78
100
|
setResolved(threadId: string, resolved: boolean): Promise<unknown> {
|
|
79
101
|
return this.request('POST', `/threads/${threadId}/${resolved ? 'resolve' : 'reopen'}`)
|
|
80
102
|
}
|
|
@@ -1,21 +1,50 @@
|
|
|
1
1
|
import { render } from 'preact'
|
|
2
|
+
import { importShareKey } from '../client/shareCrypto'
|
|
2
3
|
import { PROJECT_RE } from '../src/review/model'
|
|
3
|
-
import {
|
|
4
|
+
import { formatReviewCredentials, parseReviewCredentials, REVIEW_FRAGMENT_PARAM, type ReviewCredentials } from '../src/review/remote'
|
|
5
|
+
import { DaemonReviewClient, type ReviewClient } from './client'
|
|
6
|
+
import { RemoteReviewClient } from './remote-client'
|
|
4
7
|
import styles from './styles.css' with { type: 'text' }
|
|
5
8
|
import { ReviewWidget } from './widget'
|
|
6
9
|
|
|
7
10
|
const HOST_TAG = 'agent-canvas-review'
|
|
11
|
+
const REMOTE_KEY = 'agent-canvas-review:remote'
|
|
12
|
+
const FORGET = 'off'
|
|
8
13
|
|
|
9
14
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
15
|
+
* The reviewer's link carries the remote project and its key in the fragment, which never reaches a server. The page
|
|
16
|
+
* keeps them for its origin, so navigation and later visits keep the widget; `#agent-canvas-review=off` forgets them.
|
|
12
17
|
*/
|
|
13
|
-
function
|
|
14
|
-
const
|
|
15
|
-
if (
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
function takeRemoteCredentials(): ReviewCredentials | null {
|
|
19
|
+
const fromLink = new URLSearchParams(location.hash.slice(1)).get(REVIEW_FRAGMENT_PARAM)
|
|
20
|
+
if (fromLink !== null) {
|
|
21
|
+
// The key must not stay in the address bar, the history, or the page URL a thread records.
|
|
22
|
+
history.replaceState(history.state, '', `${location.pathname}${location.search}`)
|
|
23
|
+
if (fromLink === FORGET) {
|
|
24
|
+
try {
|
|
25
|
+
localStorage.removeItem(REMOTE_KEY)
|
|
26
|
+
} catch {}
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
const credentials = parseReviewCredentials(fromLink)
|
|
30
|
+
if (credentials === null) {
|
|
31
|
+
console.error('agent-canvas review: the review link is malformed')
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
localStorage.setItem(REMOTE_KEY, formatReviewCredentials(credentials))
|
|
36
|
+
} catch {}
|
|
37
|
+
return credentials
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const stored = localStorage.getItem(REMOTE_KEY)
|
|
41
|
+
return stored === null ? null : parseReviewCredentials(stored)
|
|
42
|
+
} catch {
|
|
43
|
+
return null
|
|
18
44
|
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function mountWidget(client: ReviewClient): void {
|
|
19
48
|
if (document.querySelector(HOST_TAG) !== null) return
|
|
20
49
|
|
|
21
50
|
const pageStyle = document.createElement('style')
|
|
@@ -28,9 +57,38 @@ function mount(script: HTMLScriptElement): void {
|
|
|
28
57
|
const style = document.createElement('style')
|
|
29
58
|
style.textContent = styles
|
|
30
59
|
root.append(style)
|
|
31
|
-
render(<ReviewWidget client={
|
|
60
|
+
render(<ReviewWidget client={client} host={host} />, root)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* - `<script src="http://localhost:19400/assets/review.js" data-project="my-app"></script>`: the local daemon keeps the
|
|
65
|
+
* threads.
|
|
66
|
+
* - `<script src="https://<share worker>/assets/review.js"></script>`: a remote project on the share worker, mounted
|
|
67
|
+
* only for a reviewer who opened its link.
|
|
68
|
+
*/
|
|
69
|
+
async function mount(script: HTMLScriptElement): Promise<void> {
|
|
70
|
+
const origin = new URL(script.src).origin
|
|
71
|
+
const project = script.dataset['project']
|
|
72
|
+
if (project !== undefined) {
|
|
73
|
+
if (!PROJECT_RE.test(project)) {
|
|
74
|
+
console.error(`agent-canvas review: data-project must match ${PROJECT_RE}, got "${project}"`)
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
mountWidget(new DaemonReviewClient(origin, project))
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
const credentials = takeRemoteCredentials()
|
|
81
|
+
if (credentials === null) return
|
|
82
|
+
mountWidget(new RemoteReviewClient(origin, credentials.projectId, await importShareKey(credentials.key)))
|
|
32
83
|
}
|
|
33
84
|
|
|
34
85
|
const script = document.currentScript
|
|
35
|
-
if (script instanceof HTMLScriptElement)
|
|
36
|
-
|
|
86
|
+
if (script instanceof HTMLScriptElement) {
|
|
87
|
+
void mount(script)
|
|
88
|
+
// A link opened on a page already showing loads no new document; reloading lets it mount or forget the project.
|
|
89
|
+
if (script.dataset['project'] === undefined) {
|
|
90
|
+
window.addEventListener('hashchange', () => {
|
|
91
|
+
if (new URLSearchParams(location.hash.slice(1)).has(REVIEW_FRAGMENT_PARAM)) location.reload()
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
} else console.error('agent-canvas review: load review.js with a classic <script> tag')
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import type { ReviewAnchor, ReviewThread } from '../src/review/model'
|
|
3
|
+
import { KEEPALIVE_PING, type SealedThread, sealedThreadSchema, sealedThreadsSchema } from '../src/review/remote'
|
|
4
|
+
import { openThread, sealAnchor, sealText } from '../src/review/sealing'
|
|
5
|
+
import type { ReviewClient, ReviewEvents } from './client'
|
|
6
|
+
|
|
7
|
+
const KEEPALIVE_MS = 30_000
|
|
8
|
+
|
|
9
|
+
const frameSchema = z.discriminatedUnion('type', [
|
|
10
|
+
z.object({ type: z.literal('threads'), threads: z.array(sealedThreadSchema) }),
|
|
11
|
+
z.object({ type: z.literal('agent-status'), watching: z.boolean() }),
|
|
12
|
+
])
|
|
13
|
+
const errorSchema = z.object({ error: z.string() })
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The widget's side of a remote review project on the share worker. Everything the reviewer writes is sealed with the
|
|
17
|
+
* project key before it leaves the page, and every thread the worker sends is opened here.
|
|
18
|
+
*/
|
|
19
|
+
export class RemoteReviewClient implements ReviewClient {
|
|
20
|
+
readonly offlineLabel = 'Offline'
|
|
21
|
+
private socket: WebSocket | null = null
|
|
22
|
+
private keepalive: ReturnType<typeof setInterval> | null = null
|
|
23
|
+
private closed = false
|
|
24
|
+
/** Opening threads is asynchronous; only the newest list may reach the widget. */
|
|
25
|
+
private generation = 0
|
|
26
|
+
|
|
27
|
+
constructor(private readonly workerOrigin: string, private readonly projectId: string, private readonly key: CryptoKey) {}
|
|
28
|
+
|
|
29
|
+
connect(events: ReviewEvents): void {
|
|
30
|
+
const socket = new WebSocket(`${this.workerOrigin.replace(/^http/, 'ws')}/review/${this.projectId}/ws`)
|
|
31
|
+
this.socket = socket
|
|
32
|
+
socket.onopen = () => {
|
|
33
|
+
events.connected(true)
|
|
34
|
+
this.keepalive = setInterval(() => socket.send(KEEPALIVE_PING), KEEPALIVE_MS)
|
|
35
|
+
void this.request('GET', '/threads').then((body) => this.publish(sealedThreadsSchema.parse(body).threads, events))
|
|
36
|
+
}
|
|
37
|
+
socket.onmessage = (event) => {
|
|
38
|
+
if (typeof event.data !== 'string' || event.data === 'pong') return
|
|
39
|
+
let data: unknown
|
|
40
|
+
try {
|
|
41
|
+
data = JSON.parse(event.data)
|
|
42
|
+
} catch {
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
const frame = frameSchema.safeParse(data)
|
|
46
|
+
if (!frame.success) return
|
|
47
|
+
if (frame.data.type === 'threads') this.publish(frame.data.threads, events)
|
|
48
|
+
else events.agentWatching(frame.data.watching)
|
|
49
|
+
}
|
|
50
|
+
socket.onclose = () => {
|
|
51
|
+
if (this.keepalive !== null) clearInterval(this.keepalive)
|
|
52
|
+
events.connected(false)
|
|
53
|
+
if (!this.closed) setTimeout(() => this.connect(events), 2000)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
close(): void {
|
|
58
|
+
this.closed = true
|
|
59
|
+
this.socket?.close()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async createThread(anchor: ReviewAnchor, body: string, send: boolean, screenshot?: string): Promise<unknown> {
|
|
63
|
+
return this.request('POST', '/threads', {
|
|
64
|
+
anchor: await sealAnchor(this.key, anchor),
|
|
65
|
+
body: await sealText(this.key, body),
|
|
66
|
+
send,
|
|
67
|
+
...(screenshot === undefined ? {} : { screenshot: await sealText(this.key, screenshot) }),
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async comment(threadId: string, body: string, send: boolean): Promise<unknown> {
|
|
72
|
+
return this.request('POST', `/threads/${threadId}/comments`, { body: await sealText(this.key, body), send })
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
send(threadId: string): Promise<unknown> {
|
|
76
|
+
return this.request('POST', `/threads/${threadId}/send`)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
sendNotes(): Promise<unknown> {
|
|
80
|
+
return this.request('POST', '/notes/send')
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
setResolved(threadId: string, resolved: boolean): Promise<unknown> {
|
|
84
|
+
return this.request('POST', `/threads/${threadId}/${resolved ? 'resolve' : 'reopen'}`)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
delete(threadId: string): Promise<unknown> {
|
|
88
|
+
return this.request('DELETE', `/threads/${threadId}`)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A thread that does not open was not sealed with this project's key; the reviewer never sees it. */
|
|
92
|
+
private publish(sealed: readonly SealedThread[], events: ReviewEvents): void {
|
|
93
|
+
const generation = ++this.generation
|
|
94
|
+
void Promise.allSettled(sealed.map((thread) => openThread(this.key, thread))).then((results) => {
|
|
95
|
+
if (generation !== this.generation) return
|
|
96
|
+
const threads: ReviewThread[] = []
|
|
97
|
+
for (const result of results) {
|
|
98
|
+
if (result.status === 'fulfilled') threads.push(result.value)
|
|
99
|
+
else console.warn('agent-canvas review: a thread did not open with this project key')
|
|
100
|
+
}
|
|
101
|
+
events.threads(threads)
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private async request(method: string, path: string, body?: unknown): Promise<unknown> {
|
|
106
|
+
const response = await fetch(`${this.workerOrigin}/review/${this.projectId}${path}`, {
|
|
107
|
+
method,
|
|
108
|
+
...(body === undefined ? {} : { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }),
|
|
109
|
+
})
|
|
110
|
+
const result: unknown = await response.json()
|
|
111
|
+
if (!response.ok) {
|
|
112
|
+
const error = errorSchema.safeParse(result)
|
|
113
|
+
throw new Error(error.success ? error.data.error : `Request failed (${response.status})`)
|
|
114
|
+
}
|
|
115
|
+
return result
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { originalPositionFor, TraceMap } from '@jridgewell/trace-mapping'
|
|
2
|
-
import type
|
|
2
|
+
import { MAX_SOURCE_FILE, type ReviewSource } from '../src/review/model'
|
|
3
3
|
|
|
4
4
|
/** A frame of a development stack, still in the bundle the page loaded. */
|
|
5
5
|
export interface BundleFrame {
|
|
@@ -19,13 +19,20 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
|
|
19
19
|
return typeof value === 'object' && value !== null
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
/** React Refresh assigns an anonymous component to `_c`, `_c2`, …, which then becomes its name. */
|
|
23
|
+
const REFRESH_TEMPORARY = /^_c\d*$/
|
|
24
|
+
|
|
22
25
|
function nameOf(type: unknown): string | undefined {
|
|
23
26
|
if (typeof type === 'string') return type
|
|
24
|
-
if (typeof type === 'function')
|
|
27
|
+
if (typeof type === 'function') {
|
|
28
|
+
return type.name === '' || REFRESH_TEMPORARY.test(type.name) ? undefined : type.name.replace(/(?<=[a-z])\d+$/, '')
|
|
29
|
+
}
|
|
25
30
|
if (isRecord(type)) return nameOf(type['type']) ?? nameOf(type['render'])
|
|
26
31
|
return undefined
|
|
27
32
|
}
|
|
28
33
|
|
|
34
|
+
const LIBRARY = /\/node_modules\//
|
|
35
|
+
|
|
29
36
|
/** The first application frame of a stack: where the JSX was written. */
|
|
30
37
|
function creationFrame(stack: string): Omit<BundleFrame, 'label'> | undefined {
|
|
31
38
|
for (const line of stack.split('\n').slice(1)) {
|
|
@@ -54,13 +61,22 @@ export function bundleFrames(element: Element): BundleFrame[] {
|
|
|
54
61
|
const stack = isRecord(fiber['_debugStack']) ? fiber['_debugStack']['stack'] : undefined
|
|
55
62
|
if (typeof stack !== 'string') continue
|
|
56
63
|
const frame = creationFrame(stack)
|
|
57
|
-
|
|
64
|
+
// An element a dependency created is not the reviewer's code to change.
|
|
65
|
+
if (frame === undefined || LIBRARY.test(frame.url)) continue
|
|
58
66
|
const owner = isRecord(fiber['_debugOwner']) ? nameOf(fiber['_debugOwner']['type']) : undefined
|
|
59
67
|
frames.push({ label: owner === undefined ? `<${name}>` : `<${name}> in ${owner}`, ...frame })
|
|
60
68
|
}
|
|
61
69
|
return frames
|
|
62
70
|
}
|
|
63
71
|
|
|
72
|
+
/** Whether a dependency, not the application, created the fiber's element; unknown without a development stack. */
|
|
73
|
+
export function createdInLibrary(fiber: Record<string, unknown>): boolean {
|
|
74
|
+
const stack = isRecord(fiber['_debugStack']) ? fiber['_debugStack']['stack'] : undefined
|
|
75
|
+
if (typeof stack !== 'string') return false
|
|
76
|
+
const frame = creationFrame(stack)
|
|
77
|
+
return frame !== undefined && LIBRARY.test(frame.url)
|
|
78
|
+
}
|
|
79
|
+
|
|
64
80
|
const traceMaps = new Map<string, Promise<TraceMap | null>>()
|
|
65
81
|
|
|
66
82
|
async function loadTraceMap(scriptUrl: string): Promise<TraceMap | null> {
|
|
@@ -70,7 +86,17 @@ async function loadTraceMap(scriptUrl: string): Promise<TraceMap | null> {
|
|
|
70
86
|
const mapUrl = new URL(reference, scriptUrl).href
|
|
71
87
|
const map: unknown = await fetch(mapUrl).then((response) => response.json())
|
|
72
88
|
if (!isRecord(map) || map['version'] !== 3) return null
|
|
73
|
-
return new TraceMap(JSON.stringify(map), mapUrl)
|
|
89
|
+
return new TraceMap(JSON.stringify(map), sourcesBase(map, mapUrl, scriptUrl))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The URL a map's relative `sources` resolve against. An inline `data:` map has no location of its own: Vite names
|
|
94
|
+
* the module's absolute path in `file`; otherwise the sources sit next to the script.
|
|
95
|
+
*/
|
|
96
|
+
function sourcesBase(map: Record<string, unknown>, mapUrl: string, scriptUrl: string): string {
|
|
97
|
+
if (!mapUrl.startsWith('data:')) return mapUrl
|
|
98
|
+
const file = map['file']
|
|
99
|
+
return typeof file === 'string' && file.startsWith('/') ? new URL(file, 'file:///').href : scriptUrl
|
|
74
100
|
}
|
|
75
101
|
|
|
76
102
|
function traceMapFor(scriptUrl: string): Promise<TraceMap | null> {
|
|
@@ -82,8 +108,15 @@ function traceMapFor(scriptUrl: string): Promise<TraceMap | null> {
|
|
|
82
108
|
return map
|
|
83
109
|
}
|
|
84
110
|
|
|
85
|
-
|
|
86
|
-
|
|
111
|
+
/** A file path when the map names one; a source on the dev server becomes its path from the server's root. */
|
|
112
|
+
function sourcePath(source: string, scriptUrl: string): string {
|
|
113
|
+
if (!URL.canParse(source)) return source
|
|
114
|
+
const url = new URL(source)
|
|
115
|
+
if (url.protocol === 'file:') return decodeURIComponent(url.pathname)
|
|
116
|
+
if (url.origin !== new URL(scriptUrl).origin) return source
|
|
117
|
+
const path = decodeURIComponent(url.pathname)
|
|
118
|
+
// Vite serves files outside its root under `/@fs/<absolute path>`.
|
|
119
|
+
return path.startsWith('/@fs/') ? path.slice('/@fs'.length) : path.slice(1)
|
|
87
120
|
}
|
|
88
121
|
|
|
89
122
|
async function resolveFrame(frame: BundleFrame): Promise<ReviewSource | null> {
|
|
@@ -92,7 +125,10 @@ async function resolveFrame(frame: BundleFrame): Promise<ReviewSource | null> {
|
|
|
92
125
|
// Stack columns count from 1, source map columns from 0.
|
|
93
126
|
const position = originalPositionFor(map, { line: frame.line, column: frame.column - 1 })
|
|
94
127
|
if (position.source === null || position.line === null) return null
|
|
95
|
-
|
|
128
|
+
const file = sourcePath(position.source, frame.url)
|
|
129
|
+
// One unusable source must not fail the whole thread against the schema.
|
|
130
|
+
if (file.length > MAX_SOURCE_FILE) return null
|
|
131
|
+
return { label: frame.label, file, line: position.line, column: position.column + 1 }
|
|
96
132
|
}
|
|
97
133
|
|
|
98
134
|
/** Source positions of the frames, through the bundles' source maps; frames a map does not cover are left out. */
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { MAX_STYLE } from '../src/review/model'
|
|
2
|
+
|
|
3
|
+
type ComputedStyle = Pick<CSSStyleDeclaration, 'getPropertyValue'>
|
|
4
|
+
|
|
5
|
+
const TRANSPARENT = 'rgba(0, 0, 0, 0)'
|
|
6
|
+
|
|
7
|
+
/** Four sides in CSS shorthand order, collapsed the way a person writes them: `8px`, `8px 12px`, … */
|
|
8
|
+
function sides(style: ComputedStyle, property: 'margin' | 'padding'): string {
|
|
9
|
+
const [top, right, bottom, left] = ['top', 'right', 'bottom', 'left'].map((side) => style.getPropertyValue(`${property}-${side}`))
|
|
10
|
+
if (top === right && right === bottom && bottom === left) return top ?? ''
|
|
11
|
+
if (top === bottom && right === left) return `${top} ${right}`
|
|
12
|
+
if (right === left) return `${top} ${right} ${bottom}`
|
|
13
|
+
return `${top} ${right} ${bottom} ${left}`
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function gapOf(style: ComputedStyle): string {
|
|
17
|
+
const row = style.getPropertyValue('row-gap')
|
|
18
|
+
const column = style.getPropertyValue('column-gap')
|
|
19
|
+
return row === column ? row : `${row} ${column}`
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The computed values that explain an element's box, layout and type, as CSS declarations. Initial values are left
|
|
24
|
+
* out; flex and grid properties only appear on flex and grid containers.
|
|
25
|
+
*/
|
|
26
|
+
export function styleOf(style: ComputedStyle): string {
|
|
27
|
+
const display = style.getPropertyValue('display')
|
|
28
|
+
const flex = display.endsWith('flex')
|
|
29
|
+
const grid = display.endsWith('grid')
|
|
30
|
+
const declarations: Array<readonly [string, string, boolean]> = [
|
|
31
|
+
['display', display, true],
|
|
32
|
+
['position', style.getPropertyValue('position'), style.getPropertyValue('position') !== 'static'],
|
|
33
|
+
['flex-direction', style.getPropertyValue('flex-direction'), flex],
|
|
34
|
+
['flex-wrap', style.getPropertyValue('flex-wrap'), flex && style.getPropertyValue('flex-wrap') !== 'nowrap'],
|
|
35
|
+
['grid-template-columns', style.getPropertyValue('grid-template-columns'), grid],
|
|
36
|
+
['align-items', style.getPropertyValue('align-items'), flex || grid],
|
|
37
|
+
['justify-content', style.getPropertyValue('justify-content'), flex || grid],
|
|
38
|
+
['gap', gapOf(style), (flex || grid) && gapOf(style) !== 'normal'],
|
|
39
|
+
['width', style.getPropertyValue('width'), true],
|
|
40
|
+
['height', style.getPropertyValue('height'), true],
|
|
41
|
+
['margin', sides(style, 'margin'), sides(style, 'margin') !== '0px'],
|
|
42
|
+
['padding', sides(style, 'padding'), sides(style, 'padding') !== '0px'],
|
|
43
|
+
['font-size', style.getPropertyValue('font-size'), true],
|
|
44
|
+
['line-height', style.getPropertyValue('line-height'), true],
|
|
45
|
+
['font-weight', style.getPropertyValue('font-weight'), true],
|
|
46
|
+
['color', style.getPropertyValue('color'), true],
|
|
47
|
+
['background-color', style.getPropertyValue('background-color'), style.getPropertyValue('background-color') !== TRANSPARENT],
|
|
48
|
+
]
|
|
49
|
+
const text = declarations.filter(([, value, shown]) => shown && value !== '').map(([property, value]) => `${property}: ${value}`).join('; ')
|
|
50
|
+
return text.length > MAX_STYLE ? `${text.slice(0, MAX_STYLE - 1)}…` : text
|
|
51
|
+
}
|
|
@@ -268,6 +268,14 @@ textarea:focus {
|
|
|
268
268
|
border-bottom: 1px solid var(--border);
|
|
269
269
|
}
|
|
270
270
|
|
|
271
|
+
.send-notes {
|
|
272
|
+
padding: 0 12px 8px;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
.send-notes button {
|
|
276
|
+
width: 100%;
|
|
277
|
+
}
|
|
278
|
+
|
|
271
279
|
.tabs button.active {
|
|
272
280
|
background: var(--bg-muted);
|
|
273
281
|
font-weight: 600;
|
|
@@ -297,6 +305,10 @@ textarea:focus {
|
|
|
297
305
|
gap: 8px;
|
|
298
306
|
}
|
|
299
307
|
|
|
308
|
+
.thread.resolved {
|
|
309
|
+
opacity: 0.65;
|
|
310
|
+
}
|
|
311
|
+
|
|
300
312
|
.thread.current {
|
|
301
313
|
border-color: var(--accent);
|
|
302
314
|
}
|
|
@@ -357,3 +369,21 @@ textarea:focus {
|
|
|
357
369
|
color: var(--warn);
|
|
358
370
|
font-size: 12px;
|
|
359
371
|
}
|
|
372
|
+
|
|
373
|
+
.reload-pending {
|
|
374
|
+
display: flex;
|
|
375
|
+
align-items: center;
|
|
376
|
+
gap: 8px;
|
|
377
|
+
padding: 6px 8px;
|
|
378
|
+
border-radius: 6px;
|
|
379
|
+
background: var(--bg-muted);
|
|
380
|
+
font-size: 12px;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
.reload-pending button {
|
|
384
|
+
flex: none;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
.panel > .error {
|
|
388
|
+
padding: 0 12px 8px;
|
|
389
|
+
}
|