opencode/packages/gui-plugins/src/browser/comment.ts

98 lines
3.4 KiB
TypeScript

import { Browser } from "@opencode/plugin-browser/rpc"
import { uuid } from "@opencode/util/uuid"
import { Schema } from "effect"
import type { ComposerNote } from "@opencode/plugin/gui"
import type { PaneElement } from "./ipc"
/** A comment on a picked element. The ref is absent once the page navigated. */
export type ElementComment = {
origin: string
tabID: Browser.TabID
url: string
element: Omit<PaneElement, "rect" | "ref"> & { ref?: Browser.Ref }
subtitle?: string
comment: string
}
/** The tab a note's href names, and its element ref while the note is live. */
type NoteHref = { readonly tabID: Browser.TabID; readonly ref?: Browser.Ref }
const isTab = Schema.is(Browser.TabID)
const isRef = Schema.is(Browser.Ref)
/**
* The composer note for a comment. A ref names an element only inside the desktop process that picked
* it; a later process can hand the same ref to another element, so only the live note carries it.
*/
export function commentNote(input: ElementComment): ComposerNote {
const ref = input.element.ref
const note: ComposerNote = {
type: "note",
origin: input.origin,
commentID: uuid(),
label: input.element.label,
title: input.element.selector || input.element.label,
subtitle: input.subtitle,
icon: "select-element",
subject: subject(input),
comment: input.comment,
href: input.tabID,
}
return ref ? { ...note, live: { subject: subject(input, ref), href: `${input.tabID}#${ref}` } } : note
}
/** The composer note for a comment on a whole page; its chip shows the page's tab again. */
export function pageNote(input: {
origin: string
tabID: Browser.TabID
url: string
title: string
label: string
comment: string
}): ComposerNote {
// Page-provided strings are quoted so they read as data, not as part of the user's request.
const title = input.title ? ` titled ${JSON.stringify(input.title)}` : ""
return {
type: "note",
origin: input.origin,
commentID: uuid(),
label: input.label,
icon: "globe",
subject: `the page in browser tab ${input.tabID} at ${input.url}${title}`,
comment: input.comment,
href: input.tabID,
}
}
/** The tab a note's href names, and its element ref while the note is live. */
export function readHref(href: string): NoteHref | undefined {
const [tabID, ref] = href.split("#")
if (!isTab(tabID)) return
return ref && isRef(ref) ? { tabID, ref } : { tabID }
}
function subject(input: ElementComment, ref?: string) {
const element = input.element
// Page-provided strings are quoted so they read as data, not as part of the user's request.
const details = [
element.role ? `role ${element.role}` : undefined,
element.name ? `accessible name ${JSON.stringify(element.name)}` : undefined,
element.text && element.text !== element.name ? `text ${JSON.stringify(element.text.slice(0, 80))}` : undefined,
// A selector too long to keep is empty rather than cut into invalid syntax.
element.selector
? `selector ${JSON.stringify(element.selector)}${element.selector.includes(" >>> ") ? ' (">>>" enters a shadow root)' : ""}`
: undefined,
ref
? `browser ref @${ref}, usable as ref in any browser tool including browser.evaluate until the page navigates`
: undefined,
].filter((detail) => detail !== undefined)
return `the ${JSON.stringify(element.label)} element in browser tab ${input.tabID} at ${input.url}${details.length ? ` (${details.join("; ")})` : ""}`
}