@nxxxsooo/opencode-annotate 0.0.0-stage → 0.6.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 +86 -0
- package/LICENSE +21 -0
- package/README.md +298 -2
- package/bridge/native-host.mjs +29 -0
- package/bridge/receiver.mjs +77 -0
- package/bridge/wire.mjs +38 -0
- package/browser/UPSTREAM.md +9 -0
- package/browser/capture.js +116 -0
- package/browser/icons/icon-128.png +0 -0
- package/browser/icons/icon-16.png +0 -0
- package/browser/icons/icon-32.png +0 -0
- package/browser/icons/icon-48.png +0 -0
- package/browser/icons/icon.svg +3 -0
- package/browser/inspector.js +125 -0
- package/browser/manifest.json +13 -0
- package/browser/panel.css +114 -0
- package/browser/panel.html +56 -0
- package/browser/panel.mjs +435 -0
- package/browser/protocol.mjs +45 -0
- package/browser/upstream/LICENSE +21 -0
- package/browser/upstream/annotate-1.0.0.js +1910 -0
- package/browser/worker.mjs +183 -0
- package/package.json +65 -4
- package/review.mjs +327 -0
- package/scripts/install-bridge.mjs +27 -0
- package/scripts/install-plugin.mjs +34 -0
- package/scripts/migrate-page-context.mjs +31 -0
- package/targets.mjs +32 -0
- package/theme.mjs +115 -0
- package/tui.ts +66 -0
- package/web-review.mjs +36 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createHash } from "node:crypto"
|
|
2
|
+
import { readFile, writeFile } from "node:fs/promises"
|
|
3
|
+
import { fileURLToPath } from "node:url"
|
|
4
|
+
import { safeUrl, validateReview } from "../browser/protocol.mjs"
|
|
5
|
+
|
|
6
|
+
export function migratePageContext(storage) {
|
|
7
|
+
const result = {}
|
|
8
|
+
for (const [key, page] of Object.entries(storage)) {
|
|
9
|
+
if (!key.startsWith("page:")) continue
|
|
10
|
+
if (!page?.page?.url || !Array.isArray(page.notes)) throw new Error(`Unrecognized old draft: ${key}`)
|
|
11
|
+
const source = { url: safeUrl(page.page.url), title: page.page.title || "Page" }
|
|
12
|
+
const hash = createHash("sha256").update(source.url).digest("hex")
|
|
13
|
+
for (const old of page.notes) {
|
|
14
|
+
const note = { id: old.id, revision: 1, kind: old.kind, comment: old.body,
|
|
15
|
+
createdAt: old.createdAt, updatedAt: old.updatedAt || old.createdAt,
|
|
16
|
+
anchor: { ...old.target, element: old.target.label || old.target.role || old.target.type,
|
|
17
|
+
styles: old.target.styles || "", accessibility: old.target.accessibility || "", fullPath: old.target.selector }, page: source }
|
|
18
|
+
validateReview({ version: 1, page: source, annotations: [note] })
|
|
19
|
+
const destination = `note:${hash}:${note.id}`
|
|
20
|
+
if (result[destination]) throw new Error("Duplicate note during migration")
|
|
21
|
+
result[destination] = note
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return result
|
|
25
|
+
}
|
|
26
|
+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
27
|
+
if (!process.argv[2] || !process.argv[3]) throw new Error("Usage: migrate-page-context.mjs input.json output.json")
|
|
28
|
+
const records = migratePageContext(JSON.parse(await readFile(process.argv[2], "utf8")))
|
|
29
|
+
await writeFile(process.argv[3], JSON.stringify(records, null, 2), { mode: 0o600, flag: "wx" })
|
|
30
|
+
console.log(`Prepared ${Object.keys(records).length} notes for import; source backup unchanged.`)
|
|
31
|
+
}
|
package/targets.mjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { stat } from "node:fs/promises"
|
|
2
|
+
import os from "node:os"
|
|
3
|
+
import path from "node:path"
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from "node:url"
|
|
5
|
+
|
|
6
|
+
const unquote = input => {
|
|
7
|
+
const value = input.trim()
|
|
8
|
+
return ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) ? value.slice(1, -1) : value
|
|
9
|
+
}
|
|
10
|
+
export const wantsBrowser = input => /^web(?: off)?$/.test(unquote(input)) || /\.html?$/i.test(unquote(input)) || /^[a-z][a-z0-9+.-]*:/i.test(unquote(input))
|
|
11
|
+
|
|
12
|
+
export async function browserTarget(input, directory) {
|
|
13
|
+
let value = unquote(input)
|
|
14
|
+
if (value === "web") return { type: "browser" }
|
|
15
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(value)) {
|
|
16
|
+
const url = new URL(value)
|
|
17
|
+
if (["http:", "https:"].includes(url.protocol)) {
|
|
18
|
+
if (url.username || url.password) throw new Error("Remove credentials from the page URL.")
|
|
19
|
+
if (/^chrome(webstore)?\.google\.com$/.test(url.hostname) || /\.pdf$/i.test(url.pathname)) throw new Error("This page cannot be annotated.")
|
|
20
|
+
return { type: "browser", url: url.href }
|
|
21
|
+
}
|
|
22
|
+
if (url.protocol !== "file:") throw new Error("Use an http, https, or local HTML target.")
|
|
23
|
+
value = fileURLToPath(url)
|
|
24
|
+
if (!/\.html?$/i.test(value)) throw new Error("File URLs must point to an HTML document.")
|
|
25
|
+
}
|
|
26
|
+
if (!/\.html?$/i.test(value)) return null
|
|
27
|
+
if (value.startsWith("~/")) value = path.join(os.homedir(), value.slice(2))
|
|
28
|
+
if (!path.isAbsolute(value) && !directory) throw new Error("Can't determine the session working directory.")
|
|
29
|
+
const file = path.resolve(directory || ".", value)
|
|
30
|
+
if (!(await stat(file)).isFile()) throw new Error("The HTML target is not a file.")
|
|
31
|
+
return { type: "browser", url: pathToFileURL(file).href }
|
|
32
|
+
}
|
package/theme.mjs
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { execFile } from "node:child_process"
|
|
2
|
+
import { writeFile } from "node:fs/promises"
|
|
3
|
+
import path from "node:path"
|
|
4
|
+
import { promisify } from "node:util"
|
|
5
|
+
|
|
6
|
+
const execute = promisify(execFile)
|
|
7
|
+
|
|
8
|
+
export function themeColors(theme) {
|
|
9
|
+
const roles = {
|
|
10
|
+
foreground: "text.base",
|
|
11
|
+
background: "background.base",
|
|
12
|
+
muted: "text.muted",
|
|
13
|
+
border: "border.base",
|
|
14
|
+
accent: "text.action.primary.selected",
|
|
15
|
+
heading: "markdown.heading",
|
|
16
|
+
code: "markdown.code",
|
|
17
|
+
link: "markdown.link",
|
|
18
|
+
"link-text": "markdown.linkText",
|
|
19
|
+
quote: "markdown.blockQuote",
|
|
20
|
+
strong: "markdown.strong",
|
|
21
|
+
emphasis: "markdown.emphasis",
|
|
22
|
+
"list-item": "markdown.listItem",
|
|
23
|
+
// OpenTUI styles ordered and unordered markers alike with markup.list.
|
|
24
|
+
"list-number": "markdown.listItem",
|
|
25
|
+
rule: "markdown.horizontalRule",
|
|
26
|
+
// Many themes leave action and feedback backgrounds transparent or equal to the
|
|
27
|
+
// page, so the send button uses the visible accent and feedback text colors.
|
|
28
|
+
send: "background.action.primary.focused",
|
|
29
|
+
sent: "text.feedback.success.base",
|
|
30
|
+
blocked: "text.feedback.warning.base",
|
|
31
|
+
comment: "text.feedback.warning.base",
|
|
32
|
+
"looks-good": "text.feedback.success.base",
|
|
33
|
+
delete: "text.feedback.error.base",
|
|
34
|
+
"comment-bg": "background.raised.high",
|
|
35
|
+
"looks-good-bg": "diff.background.added",
|
|
36
|
+
block: "background.raised.base",
|
|
37
|
+
toolbar: "background.raised.high",
|
|
38
|
+
cursor: "background.action.primary.focused",
|
|
39
|
+
"cursor-fg": "text.action.primary.focused",
|
|
40
|
+
selection: "background.action.primary.focused",
|
|
41
|
+
"selection-fg": "text.action.primary.focused",
|
|
42
|
+
}
|
|
43
|
+
const token = name => {
|
|
44
|
+
const color = name.split(".").reduce((value, key) => value?.[key], theme)
|
|
45
|
+
if (!color || [color.r, color.g, color.b, color.a].some(value =>
|
|
46
|
+
!Number.isFinite(value) || value < 0 || value > 1,
|
|
47
|
+
)) throw new Error(`OpenCode theme token ${name} is not a resolved RGBA color.`)
|
|
48
|
+
return color
|
|
49
|
+
}
|
|
50
|
+
const base = token("background.base")
|
|
51
|
+
const colors = Object.fromEntries(Object.entries(roles).map(([role, name]) => {
|
|
52
|
+
const color = token(name)
|
|
53
|
+
if (color.a === 0) return [role, "reset"]
|
|
54
|
+
if (color.a < 1 && base.a !== 1) {
|
|
55
|
+
throw new Error(`Can't resolve translucent theme token ${name} over a transparent terminal.`)
|
|
56
|
+
}
|
|
57
|
+
const hex = ["r", "g", "b"].map(channel =>
|
|
58
|
+
Math.round(255 * (color[channel] * color.a + base[channel] * (1 - color.a)))
|
|
59
|
+
.toString(16).padStart(2, "0"),
|
|
60
|
+
).join("")
|
|
61
|
+
return [role, `#${hex}`]
|
|
62
|
+
}))
|
|
63
|
+
const luminance = color => color.slice(1).match(/../g).map(value => {
|
|
64
|
+
const channel = Number.parseInt(value, 16) / 255
|
|
65
|
+
return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4
|
|
66
|
+
}).reduce((sum, channel, index) => sum + channel * [0.2126, 0.7152, 0.0722][index], 0)
|
|
67
|
+
const contrast = (one, two) => {
|
|
68
|
+
const [a, b] = [luminance(one), luminance(two)].sort((x, y) => x - y)
|
|
69
|
+
return (b + 0.05) / (a + 0.05)
|
|
70
|
+
}
|
|
71
|
+
const hex = (...items) => items.every(color => color.startsWith("#"))
|
|
72
|
+
// Some host themes use a pale focused label even on a pale accent. Keep their
|
|
73
|
+
// palette, but use the body foreground when it makes selected text more legible.
|
|
74
|
+
if (hex(colors.selection, colors["selection-fg"], colors.foreground)) {
|
|
75
|
+
const focused = contrast(colors.selection, colors["selection-fg"])
|
|
76
|
+
if (focused < 4.5 && contrast(colors.selection, colors.foreground) > focused) {
|
|
77
|
+
colors["selection-fg"] = colors.foreground
|
|
78
|
+
colors["cursor-fg"] = colors.foreground
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// One label colour serves the send, sent and blocked buttons: take whichever of
|
|
82
|
+
// the host's page or body colour stays most legible on all three.
|
|
83
|
+
const buttons = [colors.send, colors.sent, colors.blocked]
|
|
84
|
+
const labels = [colors.background, colors.foreground].filter(color => color.startsWith("#"))
|
|
85
|
+
if (hex(...buttons) && labels.length) {
|
|
86
|
+
colors["send-fg"] = labels.reduce((best, color) =>
|
|
87
|
+
Math.min(...buttons.map(button => contrast(button, color)))
|
|
88
|
+
> Math.min(...buttons.map(button => contrast(button, best))) ? color : best)
|
|
89
|
+
}
|
|
90
|
+
return colors
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export async function prepareTheme({ binary, theme, directory, env, signal }) {
|
|
94
|
+
const colors = themeColors(theme)
|
|
95
|
+
const config = async environment => execute(binary, ["config"], {
|
|
96
|
+
cwd: directory, env: environment, signal, timeout: 15000, maxBuffer: 1024 * 1024,
|
|
97
|
+
})
|
|
98
|
+
const { stdout } = await config(env)
|
|
99
|
+
// The fork's `config` command serializes its strict schema with [colors] last.
|
|
100
|
+
// Work on that normalized output, never try to parse or rewrite the user's TOML.
|
|
101
|
+
const marker = stdout.lastIndexOf("\n[colors]\n")
|
|
102
|
+
if (marker < 0) throw new Error("OpenCode colors require the color-capable Plannotator fork. Check the plugin's binary option.")
|
|
103
|
+
const filename = path.join(directory, "theme.toml")
|
|
104
|
+
const palette = Object.entries(colors).map(([role, value]) => `${role} = "${value}"`).join("\n")
|
|
105
|
+
await writeFile(filename, `${stdout.slice(0, marker)}\n\n[colors]\n${palette}\n`, { mode: 0o600 })
|
|
106
|
+
const themed = { ...env, PLANNOTATOR_TUI_CONFIG: filename }
|
|
107
|
+
const effective = await config(themed)
|
|
108
|
+
// Also catches launchers that overwrite PLANNOTATOR_TUI_CONFIG instead of honoring it.
|
|
109
|
+
for (const [role, value] of Object.entries(colors)) {
|
|
110
|
+
if (!new RegExp(`^${role} = "${value}"$`, "mi").test(effective.stdout)) {
|
|
111
|
+
throw new Error(`Plannotator did not apply ${role}. Its launcher must honor PLANNOTATOR_TUI_CONFIG.`)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return themed
|
|
115
|
+
}
|
package/tui.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Plugin } from "@opencode/plugin/tui"
|
|
2
|
+
import { createReviewCommand } from "./review.mjs"
|
|
3
|
+
|
|
4
|
+
export default Plugin.define({
|
|
5
|
+
id: "opencode-annotate",
|
|
6
|
+
setup(context) {
|
|
7
|
+
const review = createReviewCommand(context)
|
|
8
|
+
// 2.0.22's keymap.layer needs the mounted provider; newer hosts also allow setup().
|
|
9
|
+
const unmount = context.ui.slot({
|
|
10
|
+
append: "app",
|
|
11
|
+
render() {
|
|
12
|
+
context.keymap.layer(() => ({
|
|
13
|
+
mode: "global",
|
|
14
|
+
commands: [{
|
|
15
|
+
id: "opencode-annotate.annotate",
|
|
16
|
+
title: "Annotate reply or document",
|
|
17
|
+
description: "The latest completed reply, or a Markdown/text file path",
|
|
18
|
+
group: "Annotate",
|
|
19
|
+
palette: true,
|
|
20
|
+
slash: { name: "annotate", arguments: true },
|
|
21
|
+
run: review.run,
|
|
22
|
+
}, {
|
|
23
|
+
id: "opencode-annotate.annotate-last",
|
|
24
|
+
title: "Annotate last reply (TUI)",
|
|
25
|
+
description: "Annotate the latest completed reply; press q, then confirm to send",
|
|
26
|
+
group: "Annotate",
|
|
27
|
+
palette: true,
|
|
28
|
+
// Keep slash submission with a trailing space local to the command.
|
|
29
|
+
// Any supplied argument is deliberately ignored: this always means latest.
|
|
30
|
+
slash: { name: "annotate-last", arguments: true },
|
|
31
|
+
run: () => review.run(),
|
|
32
|
+
}, {
|
|
33
|
+
id: "opencode-annotate.annotate-pick",
|
|
34
|
+
title: "Annotate earlier text (TUI)",
|
|
35
|
+
description: "Choose a recent reply or progress update to annotate",
|
|
36
|
+
group: "Annotate",
|
|
37
|
+
palette: true,
|
|
38
|
+
slash: { name: "annotate-pick", arguments: true },
|
|
39
|
+
run: () => review.pick(),
|
|
40
|
+
}, {
|
|
41
|
+
id: "opencode-annotate.annotate-selection",
|
|
42
|
+
title: "Annotate selected text (TUI)",
|
|
43
|
+
description: "Open the message containing your last selection in this session",
|
|
44
|
+
group: "Annotate",
|
|
45
|
+
palette: true,
|
|
46
|
+
slash: { name: "annotate-selection", arguments: true },
|
|
47
|
+
run: () => review.selection(),
|
|
48
|
+
}, {
|
|
49
|
+
id: "opencode-annotate.annotate-web",
|
|
50
|
+
title: "Annotate webpage (browser)",
|
|
51
|
+
description: "Pair Chrome with this session, open a URL or HTML file, or off",
|
|
52
|
+
group: "Annotate",
|
|
53
|
+
palette: true,
|
|
54
|
+
slash: { name: "annotate-web", arguments: true },
|
|
55
|
+
run: review.web,
|
|
56
|
+
}],
|
|
57
|
+
}))
|
|
58
|
+
return null
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
return () => {
|
|
62
|
+
review.dispose()
|
|
63
|
+
unmount()
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
})
|
package/web-review.mjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { execFile } from "node:child_process"
|
|
2
|
+
import { promisify } from "node:util"
|
|
3
|
+
import { browserTarget } from "./targets.mjs"
|
|
4
|
+
import { createReceiver } from "./bridge/receiver.mjs"
|
|
5
|
+
|
|
6
|
+
const execute = promisify(execFile)
|
|
7
|
+
export function createWebReview(context, options = {}) {
|
|
8
|
+
const receivers = new Map()
|
|
9
|
+
let disposed = false
|
|
10
|
+
return {
|
|
11
|
+
async run(input, sessionID) {
|
|
12
|
+
if (input.trim() === "web off") {
|
|
13
|
+
await receivers.get(sessionID)?.close(); receivers.delete(sessionID)
|
|
14
|
+
context.ui.toast.show({ title: "Annotate web", message: "Browser pairing revoked for this session." })
|
|
15
|
+
return true
|
|
16
|
+
}
|
|
17
|
+
const session = await context.client.session.get({ sessionID })
|
|
18
|
+
const target = await browserTarget(input, session.location?.directory)
|
|
19
|
+
if (!target) return false
|
|
20
|
+
if (disposed) throw new Error("Annotation plugin is closed")
|
|
21
|
+
if (!receivers.has(sessionID)) {
|
|
22
|
+
const receiver = await createReceiver({ sessionID, title: session.title || sessionID, directory: session.location?.directory || "", client: context.client, ...options.receiver })
|
|
23
|
+
if (disposed) { await receiver.close(); return true }
|
|
24
|
+
receivers.set(sessionID, receiver)
|
|
25
|
+
}
|
|
26
|
+
if (target.url) {
|
|
27
|
+
if (options.open) await options.open(target.url)
|
|
28
|
+
else if (process.platform === "darwin") await execute("open", ["-a", "Google Chrome", target.url])
|
|
29
|
+
else throw new Error("Browser review currently requires macOS")
|
|
30
|
+
}
|
|
31
|
+
context.ui.toast.show({ title: "Annotate web", message: `Ready for browser pairing: ${session.title || sessionID}. Open the extension and select this session.`, duration: 10000 })
|
|
32
|
+
return true
|
|
33
|
+
},
|
|
34
|
+
async dispose() { disposed = true; await Promise.all([...receivers.values()].map(receiver => receiver.close())); receivers.clear() },
|
|
35
|
+
}
|
|
36
|
+
}
|