shapewall 0.0.0-stage → 0.2.2
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 +139 -0
- package/Dockerfile.agent +45 -0
- package/LICENSE +202 -0
- package/NOTICE +12 -0
- package/README.md +651 -2
- package/THIRD_PARTY.md +165 -0
- package/bin/shapewall.js +29 -0
- package/dist/agent/package-lock.json +8384 -0
- package/dist/client/assets/BoardChat-D5SFd8HE.js +28 -0
- package/dist/client/assets/BuzzDialog-HWXBy52W.js +1 -0
- package/dist/client/assets/ChecksPanel-DTdImWoD.js +1 -0
- package/dist/client/assets/History-CEv6b-av.js +1 -0
- package/dist/client/assets/LocalSettings-C4vriEVQ.js +1 -0
- package/dist/client/assets/Management-DCfhcEUY.js +1 -0
- package/dist/client/assets/QuickConnect-DQGgUsl1.js +1 -0
- package/dist/client/assets/ShareDialog-C_plIto0.js +2 -0
- package/dist/client/assets/app-update-dialog-2hPz_N9G.js +1 -0
- package/dist/client/assets/circle-x-BZMPi8Zf.js +1 -0
- package/dist/client/assets/index-ByQRKVit.css +2 -0
- package/dist/client/assets/index-DweQ8Qtu.js +29 -0
- package/dist/client/assets/refresh-cw-CRow0Nbq.js +1 -0
- package/dist/client/assets/runtime-Bft6x_6E.js +4 -0
- package/dist/client/assets/square-A8EnIYLR.js +1 -0
- package/dist/client/assets/tts-dist-DYlBQmwG.js +18 -0
- package/dist/client/assets/tts-en-g2p-BPUtBFGG.js +1 -0
- package/dist/client/assets/tts-ort-wasm-simd-threaded-qh98TCA_.wasm +0 -0
- package/dist/client/assets/tts-ort.wasm.bundle.min-rEkcs_ow.js +12 -0
- package/dist/client/assets/tts-rolldown-runtime-D8NudN84.js +1 -0
- package/dist/client/assets/tts-spacy-en-tokenizer-6SNMl--A.json +1 -0
- package/dist/client/assets/tts-tts.worker-Bzm6aWbv.js +1 -0
- package/dist/client/assets/tts-us_gold-DcjeNMH1.json +92589 -0
- package/dist/client/assets/tts-us_silver-C964mEB5.json +93363 -0
- package/dist/client/assets/ui-CLbws0rx.js +41 -0
- package/dist/client/build-info.json +1 -0
- package/dist/client/favicon.svg +7 -0
- package/dist/client/icons/apple-touch-icon.png +0 -0
- package/dist/client/icons/icon-192.png +0 -0
- package/dist/client/icons/icon-512.png +0 -0
- package/dist/client/icons/maskable-512.png +0 -0
- package/dist/client/index.html +25 -0
- package/dist/client/manifest.webmanifest +15 -0
- package/dist/client/sw.js +53 -0
- package/dist/client/theme-boot.js +27 -0
- package/dist/console/assets/LocalAccountsApp-BQCcmj3x.js +1 -0
- package/dist/console/assets/LocalConsole-Bb2FUDs7.js +1 -0
- package/dist/console/assets/app-update-dialog-DIeYepLZ.js +1 -0
- package/dist/console/assets/console-C7uJLLOn.js +51 -0
- package/dist/console/assets/console-R8IBY3Am.css +2 -0
- package/dist/console/build-info.json +1 -0
- package/dist/console/console.html +21 -0
- package/dist/console/favicon.svg +7 -0
- package/dist/console/icons/apple-touch-icon.png +0 -0
- package/dist/console/icons/icon-192.png +0 -0
- package/dist/console/icons/icon-512.png +0 -0
- package/dist/console/icons/maskable-512.png +0 -0
- package/dist/console/manifest.webmanifest +15 -0
- package/dist/console/theme-boot.js +27 -0
- package/docs/agent-hardening.md +342 -0
- package/docs/agent-recipes.md +187 -0
- package/docs/agent-workflow.md +320 -0
- package/docs/agents.md +894 -0
- package/docs/architecture.md +242 -0
- package/docs/buzz.md +140 -0
- package/docs/capture-warnings.md +57 -0
- package/docs/config.md +489 -0
- package/docs/deployment.md +289 -0
- package/docs/design-system.md +162 -0
- package/docs/github.md +107 -0
- package/docs/hosted.md +764 -0
- package/docs/images/board-overview.png +0 -0
- package/docs/images/journeys.png +0 -0
- package/docs/images/live-comment.png +0 -0
- package/docs/images/phone-comment.png +0 -0
- package/docs/images/phone-overview.png +0 -0
- package/docs/images/variant.png +0 -0
- package/docs/interaction-audits.md +40 -0
- package/docs/journeys.md +137 -0
- package/docs/local-accounts.md +80 -0
- package/docs/notifications.md +237 -0
- package/docs/page-checks.md +69 -0
- package/docs/performance.md +174 -0
- package/docs/starter-agent.md +325 -0
- package/docs/updates.md +87 -0
- package/examples/audits/interaction.audit.json +17 -0
- package/package.json +132 -4
- package/src/accounts/avatar.js +121 -0
- package/src/accounts/local.js +464 -0
- package/src/agent/acp/command.js +204 -0
- package/src/agent/acp/detect.js +75 -0
- package/src/agent/acp/sandbox.js +160 -0
- package/src/agent/agent-health.js +75 -0
- package/src/agent/backends/acp.js +1297 -0
- package/src/agent/backends/index.js +61 -0
- package/src/agent/backends/openai.js +308 -0
- package/src/agent/composer.js +221 -0
- package/src/agent/config.js +338 -0
- package/src/agent/context.js +191 -0
- package/src/agent/control.js +122 -0
- package/src/agent/errors.js +17 -0
- package/src/agent/feedback-loop.js +137 -0
- package/src/agent/manage.js +94 -0
- package/src/agent/next-steps.js +51 -0
- package/src/agent/owner-mcp.js +38 -0
- package/src/agent/plain-text.js +52 -0
- package/src/agent/ratings-core.js +213 -0
- package/src/agent/ratings.js +100 -0
- package/src/agent/reply-text.js +67 -0
- package/src/agent/routes.js +1065 -0
- package/src/agent/run.js +326 -0
- package/src/agent/settings-routes.js +704 -0
- package/src/agent/showable.js +23 -0
- package/src/agent/starter/approvals.js +181 -0
- package/src/agent/starter/build-image.js +90 -0
- package/src/agent/starter/cli.js +163 -0
- package/src/agent/starter/disk.js +53 -0
- package/src/agent/starter/doctor.js +180 -0
- package/src/agent/starter/egress-proxy.js +351 -0
- package/src/agent/starter/gate.js +90 -0
- package/src/agent/starter/loop.js +114 -0
- package/src/agent/starter/model-relay.js +289 -0
- package/src/agent/starter/pause.js +14 -0
- package/src/agent/starter/probe.js +272 -0
- package/src/agent/starter/prompt.js +41 -0
- package/src/agent/starter/push-guard.js +146 -0
- package/src/agent/starter/rpc.js +164 -0
- package/src/agent/starter/runtime.js +287 -0
- package/src/agent/starter/sandbox-main.js +121 -0
- package/src/agent/starter/settings.js +290 -0
- package/src/agent/starter/source.js +69 -0
- package/src/agent/starter/supervisor.js +755 -0
- package/src/agent/starter/sweep.js +46 -0
- package/src/agent/starter/tools.js +524 -0
- package/src/agent/starter/watch.js +84 -0
- package/src/agent/starter/worktree.js +409 -0
- package/src/agent/text-diff.js +210 -0
- package/src/agent/thread-history.js +223 -0
- package/src/agent/threads.js +399 -0
- package/src/agent/tools.js +200 -0
- package/src/agent/turn-changes.js +424 -0
- package/src/agent/undo-state.js +26 -0
- package/src/agent/user-settings.js +308 -0
- package/src/auth/record.js +375 -0
- package/src/broken-links.js +164 -0
- package/src/buzz/client.js +75 -0
- package/src/buzz/config.js +151 -0
- package/src/buzz/environment.js +13 -0
- package/src/buzz/gateway.js +192 -0
- package/src/buzz/routes.js +61 -0
- package/src/buzz/signer-child.js +205 -0
- package/src/buzz/store.js +89 -0
- package/src/buzz/transport.js +60 -0
- package/src/buzz/verify.js +140 -0
- package/src/capture/actions.js +159 -0
- package/src/capture/capture.js +776 -0
- package/src/capture/dom-names.js +189 -0
- package/src/capture/element-map.js +509 -0
- package/src/capture/live.js +827 -0
- package/src/capture/media-scheme.js +317 -0
- package/src/capture/network-summary.js +86 -0
- package/src/capture/pacing.js +34 -0
- package/src/capture/requests.js +167 -0
- package/src/capture/sanitize.js +752 -0
- package/src/checks/accessibility.js +134 -0
- package/src/checks/budgets.js +58 -0
- package/src/checks/cli.js +55 -0
- package/src/checks/comparison.js +113 -0
- package/src/checks/environments.js +32 -0
- package/src/checks/flow-config.js +82 -0
- package/src/checks/flow-origin.js +53 -0
- package/src/checks/flows.js +398 -0
- package/src/checks/lighthouse-auth.js +145 -0
- package/src/checks/lighthouse-findings.js +130 -0
- package/src/checks/lighthouse.js +395 -0
- package/src/checks/robots.js +85 -0
- package/src/checks/routes.js +52 -0
- package/src/checks/run-control.js +259 -0
- package/src/checks/runner.js +724 -0
- package/src/checks/schema.js +133 -0
- package/src/checks/seo-analysis.js +443 -0
- package/src/checks/seo-probe.js +190 -0
- package/src/checks/seo-snapshot.js +99 -0
- package/src/checks/store.js +214 -0
- package/src/checks/transport.js +47 -0
- package/src/cli/args.js +319 -0
- package/src/cli/capture.js +79 -0
- package/src/cli/commands.js +92 -0
- package/src/cli/comments.js +84 -0
- package/src/cli/help.js +243 -0
- package/src/cli/init.js +214 -0
- package/src/cli/misc.js +64 -0
- package/src/cli/notify.js +87 -0
- package/src/cli/open.js +22 -0
- package/src/cli/serve.js +274 -0
- package/src/cli/variant.js +82 -0
- package/src/cli/watch.js +70 -0
- package/src/cli.js +130 -0
- package/src/comment-fields.js +76 -0
- package/src/comments-cli.js +208 -0
- package/src/config-reload.js +100 -0
- package/src/config.js +1061 -0
- package/src/devices-cli.js +21 -0
- package/src/diff.js +95 -0
- package/src/discover.js +284 -0
- package/src/doctor.js +295 -0
- package/src/event-log.js +268 -0
- package/src/events-cli.js +102 -0
- package/src/events.js +113 -0
- package/src/export.js +119 -0
- package/src/feedback.js +246 -0
- package/src/github.js +551 -0
- package/src/hosted/LICENSE.md +105 -0
- package/src/hosted/access-cli.js +78 -0
- package/src/hosted/admin.js +682 -0
- package/src/hosted/agent-approvals.js +163 -0
- package/src/hosted/agent-policy.js +189 -0
- package/src/hosted/app.js +510 -0
- package/src/hosted/auth-routes.js +540 -0
- package/src/hosted/auth.js +536 -0
- package/src/hosted/backup.js +483 -0
- package/src/hosted/board-context.js +35 -0
- package/src/hosted/board-routes.js +486 -0
- package/src/hosted/board-view.js +94 -0
- package/src/hosted/clean-worker.js +24 -0
- package/src/hosted/cleaner.js +89 -0
- package/src/hosted/comments.js +355 -0
- package/src/hosted/console-routes.js +401 -0
- package/src/hosted/cookies.js +18 -0
- package/src/hosted/crypto.js +80 -0
- package/src/hosted/db.js +496 -0
- package/src/hosted/device-routes.js +175 -0
- package/src/hosted/device.js +233 -0
- package/src/hosted/events.js +63 -0
- package/src/hosted/guards.js +172 -0
- package/src/hosted/guest-view.js +65 -0
- package/src/hosted/http.js +39 -0
- package/src/hosted/import.js +300 -0
- package/src/hosted/mail.js +73 -0
- package/src/hosted/mcp-http.js +71 -0
- package/src/hosted/membership.js +114 -0
- package/src/hosted/operator.js +478 -0
- package/src/hosted/ops-cli.js +345 -0
- package/src/hosted/pages.js +146 -0
- package/src/hosted/permissions.js +113 -0
- package/src/hosted/presence.js +372 -0
- package/src/hosted/projects.js +124 -0
- package/src/hosted/public.js +109 -0
- package/src/hosted/push.js +622 -0
- package/src/hosted/quotas.js +115 -0
- package/src/hosted/ratelimit.js +96 -0
- package/src/hosted/rebind.js +316 -0
- package/src/hosted/retention.js +128 -0
- package/src/hosted/server.js +260 -0
- package/src/hosted/setup-link.js +64 -0
- package/src/hosted/setup.js +323 -0
- package/src/hosted/shares.js +444 -0
- package/src/hosted/tailscale.js +204 -0
- package/src/hosted/tenancy.js +301 -0
- package/src/hosted/token-api.js +455 -0
- package/src/hosted/token-events.js +60 -0
- package/src/hosted/tokens.js +57 -0
- package/src/hosted/turnstile.js +49 -0
- package/src/hosted/usercontent.js +154 -0
- package/src/hosted/zip.js +155 -0
- package/src/invocation.js +58 -0
- package/src/journey-paths.js +282 -0
- package/src/journeys.js +189 -0
- package/src/links.js +106 -0
- package/src/local-projects.js +182 -0
- package/src/lock.js +193 -0
- package/src/mcp.js +417 -0
- package/src/mentions.js +151 -0
- package/src/net.js +15 -0
- package/src/notify/config.js +275 -0
- package/src/notify/index.js +301 -0
- package/src/notify/lease.js +73 -0
- package/src/notify/messages.js +212 -0
- package/src/notify/run.js +174 -0
- package/src/pixeldiff.js +49 -0
- package/src/png.js +143 -0
- package/src/reactions.js +111 -0
- package/src/remote/cli.js +133 -0
- package/src/remote/credentials.js +62 -0
- package/src/remote/ids.js +12 -0
- package/src/remote/login.js +155 -0
- package/src/remote/remote-mcp.js +237 -0
- package/src/remote/remote-watch.js +291 -0
- package/src/remote/remote.js +313 -0
- package/src/remote/share-state.js +78 -0
- package/src/remote/share.js +369 -0
- package/src/remote/terminal.js +13 -0
- package/src/serve-state.js +64 -0
- package/src/server/app.js +824 -0
- package/src/server/devices.js +186 -0
- package/src/server/local-console.js +28 -0
- package/src/server/local-user.js +59 -0
- package/src/server/pairing-routes.js +159 -0
- package/src/server/share-routes.js +89 -0
- package/src/server/tailscale.js +97 -0
- package/src/sitemap.js +431 -0
- package/src/store.js +983 -0
- package/src/tokens.js +11 -0
- package/src/update/routes.js +245 -0
- package/src/watch.js +403 -0
- package/src/which.js +58 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// A small pool of worker threads that clean pushed files (clean-worker.js).
|
|
2
|
+
// At most `size` files are cleaned at once and at most `maxQueue` wait; a
|
|
3
|
+
// file that takes longer than `timeoutMs` (a decompression bomb, say) has
|
|
4
|
+
// its worker terminated and replaced. Workers are unref'd, so an idle pool
|
|
5
|
+
// never keeps the process alive.
|
|
6
|
+
|
|
7
|
+
import { Worker } from 'node:worker_threads'
|
|
8
|
+
import { PushError } from './push.js'
|
|
9
|
+
|
|
10
|
+
const WORKER_URL = new URL('./clean-worker.js', import.meta.url)
|
|
11
|
+
|
|
12
|
+
export function createCleaner({ size = 2, maxQueue = 32, timeoutMs = 30_000, memoryMb = 512 } = {}) {
|
|
13
|
+
const idle = []
|
|
14
|
+
const busy = new Set()
|
|
15
|
+
const queue = []
|
|
16
|
+
let nextId = 1
|
|
17
|
+
let closed = false
|
|
18
|
+
|
|
19
|
+
function spawn() {
|
|
20
|
+
const worker = new Worker(WORKER_URL, { resourceLimits: { maxOldGenerationSizeMb: memoryMb } })
|
|
21
|
+
worker.unref()
|
|
22
|
+
worker.on('message', (message) => finish(worker, message))
|
|
23
|
+
worker.on('error', () => crash(worker))
|
|
24
|
+
worker.on('exit', () => crash(worker))
|
|
25
|
+
return worker
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function crash(worker) {
|
|
29
|
+
const job = worker.job
|
|
30
|
+
if (!job) {
|
|
31
|
+
const index = idle.indexOf(worker)
|
|
32
|
+
if (index !== -1) idle.splice(index, 1)
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
worker.job = null
|
|
36
|
+
busy.delete(worker)
|
|
37
|
+
clearTimeout(job.timer)
|
|
38
|
+
job.reject(new PushError(422, 'clean_failed', 'The file could not be cleaned.'))
|
|
39
|
+
pump()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function finish(worker, message) {
|
|
43
|
+
const job = worker.job
|
|
44
|
+
if (!job || job.id !== message.id) return
|
|
45
|
+
worker.job = null
|
|
46
|
+
clearTimeout(job.timer)
|
|
47
|
+
busy.delete(worker)
|
|
48
|
+
idle.push(worker)
|
|
49
|
+
if (message.ok) job.resolve(Buffer.from(message.body.buffer, message.body.byteOffset, message.body.byteLength))
|
|
50
|
+
else job.reject(new PushError(message.status, message.code, message.message))
|
|
51
|
+
pump()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function pump() {
|
|
55
|
+
while (!closed && queue.length && busy.size < size) {
|
|
56
|
+
const job = queue.shift()
|
|
57
|
+
const worker = idle.pop() || spawn()
|
|
58
|
+
worker.job = job
|
|
59
|
+
busy.add(worker)
|
|
60
|
+
job.timer = setTimeout(() => {
|
|
61
|
+
job.reject(new PushError(422, 'clean_timeout', 'The file took too long to clean.'))
|
|
62
|
+
worker.job = null
|
|
63
|
+
busy.delete(worker)
|
|
64
|
+
worker.terminate()
|
|
65
|
+
pump()
|
|
66
|
+
}, timeoutMs)
|
|
67
|
+
job.timer.unref?.()
|
|
68
|
+
worker.postMessage({ id: job.id, kind: job.kind, body: job.body, base: job.base })
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Resolves with the cleaned Buffer, or rejects with a PushError.
|
|
73
|
+
function clean(kind, body, base = null) {
|
|
74
|
+
if (closed) return Promise.reject(new PushError(503, 'closing', 'The server is shutting down.'))
|
|
75
|
+
if (queue.length >= maxQueue) return Promise.reject(new PushError(503, 'busy', 'The server is busy cleaning files; try again shortly.'))
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
queue.push({ id: nextId++, kind, body, base, resolve, reject })
|
|
78
|
+
pump()
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function close() {
|
|
83
|
+
closed = true
|
|
84
|
+
for (const job of queue.splice(0)) job.reject(new PushError(503, 'closing', 'The server is shutting down.'))
|
|
85
|
+
await Promise.all([...idle.splice(0), ...busy].map((worker) => worker.terminate()))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return { clean, close, stats: () => ({ busy: busy.size, idle: idle.length, queued: queue.length }) }
|
|
89
|
+
}
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
// Comments of hosted boards, in SQLite. Same shape and rules as the local
|
|
2
|
+
// store (src/store.js): the same validation, ids per board, replies,
|
|
3
|
+
// agent status (with compare-and-set), reopen counts and fix references.
|
|
4
|
+
// What changes: the author of a person's comment is their account name, set
|
|
5
|
+
// by the server, and every query is scoped through the tenant handle.
|
|
6
|
+
|
|
7
|
+
import { commentMentions } from '../mentions.js'
|
|
8
|
+
import { publicReactions, toggleReaction } from '../reactions.js'
|
|
9
|
+
import { GUEST_SHARE } from './guest-view.js'
|
|
10
|
+
import { AGENT_STATUSES, AUTHOR_MAX, COMMENT_BODY_MAX, CommentError, cleanFixRef, currentVersion, validateCommentInput } from '../store.js'
|
|
11
|
+
|
|
12
|
+
export { GUEST_SHARE, guestView } from './guest-view.js'
|
|
13
|
+
|
|
14
|
+
// Which account or token wrote a reply ({ user, token }), stored with it but
|
|
15
|
+
// kept off the JSON (a symbol key): agent views use it to decide trust.
|
|
16
|
+
export const REPLY_AUTHOR = Symbol('replyAuthor')
|
|
17
|
+
|
|
18
|
+
const COMMENTS_MAX = 5000
|
|
19
|
+
const REPLIES_MAX = 200
|
|
20
|
+
|
|
21
|
+
function cleanBody(value) {
|
|
22
|
+
const body = typeof value === 'string' ? value.replace(/\r\n/g, '\n').trim() : ''
|
|
23
|
+
if (!body) throw new CommentError(400, 'empty_comment', 'Write something first.')
|
|
24
|
+
if (body.length > COMMENT_BODY_MAX)
|
|
25
|
+
throw new CommentError(400, 'comment_too_long', `Comments are limited to ${COMMENT_BODY_MAX} characters.`)
|
|
26
|
+
return body
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function cleanAuthor(value, fallback = 'agent') {
|
|
30
|
+
const author = typeof value === 'string' ? value.replace(/\s+/g, ' ').trim().slice(0, AUTHOR_MAX) : ''
|
|
31
|
+
return author || fallback
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function cleanAgentStatus(value) {
|
|
35
|
+
if (value === null || AGENT_STATUSES.includes(value)) return value
|
|
36
|
+
throw new CommentError(400, 'invalid_agent_status', `Agent status must be one of ${AGENT_STATUSES.join(', ')} (or null to clear).`)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function parseNumber(value) {
|
|
40
|
+
const id = typeof value === 'number' ? value : /^[1-9][0-9]{0,15}$/.test(String(value)) ? Number(value) : NaN
|
|
41
|
+
if (!Number.isSafeInteger(id) || id <= 0) throw new CommentError(404, 'unknown_comment', 'That comment does not exist.')
|
|
42
|
+
return id
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Imported local reactions carry counts, never claimed account identities.
|
|
46
|
+
function combinedReactions(current, imported) {
|
|
47
|
+
const known = publicReactions(current)
|
|
48
|
+
for (const count of parseList(imported)) {
|
|
49
|
+
const existing = known.find((item) => item.emoji === count.emoji)
|
|
50
|
+
if (existing) existing.count += count.count
|
|
51
|
+
else known.push({ ...count, by: [] })
|
|
52
|
+
}
|
|
53
|
+
return known
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// A row in the shape the board and agents know.
|
|
57
|
+
export function rowToComment(row) {
|
|
58
|
+
const stored = JSON.parse(row.replies || '[]')
|
|
59
|
+
const replies = stored.map(({ share: _share, user, token, reactions, ...reply }) => ({
|
|
60
|
+
...reply,
|
|
61
|
+
reactions: combinedReactions(reactions, JSON.stringify(reply.importedReactions || [])),
|
|
62
|
+
[REPLY_AUTHOR]: { user: user || null, token: token || null },
|
|
63
|
+
}))
|
|
64
|
+
const comment = {
|
|
65
|
+
id: row.number,
|
|
66
|
+
...(row.imported_by
|
|
67
|
+
? {
|
|
68
|
+
imported: true,
|
|
69
|
+
...(row.import_author_kind === 'agent' ? { authorAgent: true } : row.import_author_kind === 'guest' ? { authorGuest: true } : {}),
|
|
70
|
+
importedBy: row.imported_by,
|
|
71
|
+
importedAt: row.imported_at,
|
|
72
|
+
...(row.import_orphan ? { orphanReason: 'page no longer on the board' } : {}),
|
|
73
|
+
}
|
|
74
|
+
: {}),
|
|
75
|
+
pageId: row.page_id,
|
|
76
|
+
viewport: row.viewport,
|
|
77
|
+
x: row.x,
|
|
78
|
+
y: row.y,
|
|
79
|
+
body: row.body,
|
|
80
|
+
author: row.author,
|
|
81
|
+
// Hosted only: the account handle beside the display name, and a flag for
|
|
82
|
+
// comments a token wrote (an agent), so names alone never decide either.
|
|
83
|
+
...(row.author_handle ? { authorHandle: row.author_handle } : {}),
|
|
84
|
+
...(row.author_token_id || row.import_author_kind === 'agent' ? { authorAgent: true } : {}),
|
|
85
|
+
// A guest of a share link: untrusted text from outside the workspace.
|
|
86
|
+
...(row.author_share_id || row.import_author_kind === 'guest' ? { authorGuest: true } : {}),
|
|
87
|
+
...(row.anchor ? { anchor: JSON.parse(row.anchor) } : {}),
|
|
88
|
+
captureVersion: row.capture_version || null,
|
|
89
|
+
status: row.status,
|
|
90
|
+
createdAt: row.created_at,
|
|
91
|
+
updatedAt: row.updated_at,
|
|
92
|
+
resolvedAt: row.resolved_at || null,
|
|
93
|
+
resolution: row.resolution || null,
|
|
94
|
+
replies,
|
|
95
|
+
agentStatus: AGENT_STATUSES.includes(row.agent_status) ? row.agent_status : null,
|
|
96
|
+
agentStatusAt: row.agent_status_at || null,
|
|
97
|
+
reopenCount: row.reopen_count || 0,
|
|
98
|
+
reopenedAt: row.reopened_at || null,
|
|
99
|
+
...(row.fix_ref ? { fixRef: row.fix_ref } : {}),
|
|
100
|
+
editedAt: row.edited_at || null,
|
|
101
|
+
reactions: combinedReactions(parseList(row.reactions), row.imported_reactions),
|
|
102
|
+
}
|
|
103
|
+
// Guests never @mention anyone: their text must not wake a watcher
|
|
104
|
+
// ("watch --mention agent") or page a person.
|
|
105
|
+
comment.mentions = commentMentions({
|
|
106
|
+
body: comment.authorGuest ? '' : comment.body,
|
|
107
|
+
replies: replies.filter((reply) => !reply.guest),
|
|
108
|
+
})
|
|
109
|
+
comment[GUEST_SHARE] = { comment: row.author_share_id || null, replies: stored.map((reply) => reply.share || null) }
|
|
110
|
+
return comment
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function parseList(text) {
|
|
114
|
+
try {
|
|
115
|
+
const value = JSON.parse(text || '[]')
|
|
116
|
+
return Array.isArray(value) ? value : []
|
|
117
|
+
} catch {
|
|
118
|
+
return []
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const SCOPE = 'workspace_id = :ws AND project_id = :project AND board_id = :board'
|
|
123
|
+
|
|
124
|
+
export function createCommentStore(t, projectId, boardId) {
|
|
125
|
+
const key = { project: projectId, board: boardId }
|
|
126
|
+
const rowOf = (number) => t.get(`SELECT * FROM comments WHERE ${SCOPE} AND number = :number`, { ...key, number })
|
|
127
|
+
|
|
128
|
+
function list({ status = null, mention = null } = {}) {
|
|
129
|
+
const wanted = mention ? String(mention).replace(/^@/, '').toLowerCase() : null
|
|
130
|
+
return t
|
|
131
|
+
.all(`SELECT * FROM comments WHERE ${SCOPE} ORDER BY created_at DESC, number DESC`, key)
|
|
132
|
+
.map(rowToComment)
|
|
133
|
+
.filter((comment) => !status || comment.status === status)
|
|
134
|
+
.filter((comment) => !wanted || comment.mentions.includes(wanted))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function counts() {
|
|
138
|
+
const out = {}
|
|
139
|
+
for (const row of t.all(`SELECT page_id, status, COUNT(*) AS n FROM comments WHERE ${SCOPE} GROUP BY page_id, status`, key)) {
|
|
140
|
+
out[row.page_id] ||= { open: 0, resolved: 0 }
|
|
141
|
+
out[row.page_id][row.status] = row.n
|
|
142
|
+
}
|
|
143
|
+
return out
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function get(number) {
|
|
147
|
+
const row = rowOf(parseNumber(number))
|
|
148
|
+
if (!row) throw new CommentError(404, 'unknown_comment', 'That comment does not exist.')
|
|
149
|
+
return row
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// `author` is { name, userId, handle } for a person, { name, tokenId } for
|
|
153
|
+
// a token, or { name, shareId } for a share link's guest.
|
|
154
|
+
function create(config, board, input, author, now = new Date()) {
|
|
155
|
+
const clean = validateCommentInput(config, board, input)
|
|
156
|
+
const total = t.get(`SELECT COUNT(*) AS n FROM comments WHERE ${SCOPE}`, key).n
|
|
157
|
+
if (total >= COMMENTS_MAX) throw new CommentError(409, 'too_many_comments', `A board holds at most ${COMMENTS_MAX} comments.`)
|
|
158
|
+
const counter = t.get('SELECT next_comment FROM boards WHERE workspace_id = :ws AND project_id = :project AND board_id = :board', key)
|
|
159
|
+
if (!counter) throw new CommentError(404, 'unknown_board', 'No such board.')
|
|
160
|
+
const number = counter.next_comment
|
|
161
|
+
t.run('UPDATE boards SET next_comment = :next WHERE workspace_id = :ws AND project_id = :project AND board_id = :board', {
|
|
162
|
+
...key,
|
|
163
|
+
next: number + 1,
|
|
164
|
+
})
|
|
165
|
+
const at = now.toISOString()
|
|
166
|
+
t.run(
|
|
167
|
+
`INSERT INTO comments (workspace_id, project_id, board_id, number, page_id, viewport, x, y, body, author, author_handle,
|
|
168
|
+
author_user_id, author_token_id, author_share_id, anchor, capture_version, status, created_at, updated_at)
|
|
169
|
+
VALUES (:ws, :project, :board, :number, :page, :viewport, :x, :y, :body, :author, :handle, :user, :token, :share, :anchor, :version,
|
|
170
|
+
'open', :at, :at)`,
|
|
171
|
+
{
|
|
172
|
+
...key,
|
|
173
|
+
number,
|
|
174
|
+
page: clean.pageId,
|
|
175
|
+
viewport: clean.viewport,
|
|
176
|
+
x: clean.x,
|
|
177
|
+
y: clean.y,
|
|
178
|
+
body: clean.body,
|
|
179
|
+
author: author.name,
|
|
180
|
+
handle: author.handle || null,
|
|
181
|
+
user: author.userId || null,
|
|
182
|
+
token: author.tokenId || null,
|
|
183
|
+
share: author.shareId || null,
|
|
184
|
+
anchor: clean.anchor ? JSON.stringify(clean.anchor) : null,
|
|
185
|
+
version: clean.pageId.startsWith('variant:') ? null : currentVersion(config, board.id, clean.pageId, clean.viewport),
|
|
186
|
+
at,
|
|
187
|
+
},
|
|
188
|
+
)
|
|
189
|
+
return rowToComment(rowOf(number))
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function save(row, now) {
|
|
193
|
+
t.run(
|
|
194
|
+
`UPDATE comments SET body = :body, status = :status, resolution = :resolution, fix_ref = :fix, replies = :replies,
|
|
195
|
+
agent_status = :agent, agent_status_at = :agentAt, reopen_count = :reopens, reopened_at = :reopenedAt,
|
|
196
|
+
resolved_at = :resolvedAt, edited_at = :editedAt, updated_at = :at
|
|
197
|
+
WHERE ${SCOPE} AND number = :number`,
|
|
198
|
+
{
|
|
199
|
+
...key,
|
|
200
|
+
number: row.number,
|
|
201
|
+
body: row.body,
|
|
202
|
+
status: row.status,
|
|
203
|
+
resolution: row.resolution ?? null,
|
|
204
|
+
fix: row.fix_ref ?? null,
|
|
205
|
+
replies: row.replies,
|
|
206
|
+
agent: row.agent_status ?? null,
|
|
207
|
+
agentAt: row.agent_status_at ?? null,
|
|
208
|
+
reopens: row.reopen_count || 0,
|
|
209
|
+
reopenedAt: row.reopened_at ?? null,
|
|
210
|
+
resolvedAt: row.resolved_at ?? null,
|
|
211
|
+
editedAt: row.edited_at ?? null,
|
|
212
|
+
at: now.toISOString(),
|
|
213
|
+
},
|
|
214
|
+
)
|
|
215
|
+
return rowToComment(rowOf(row.number))
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const setAgent = (row, value, now) => {
|
|
219
|
+
if ((row.agent_status ?? null) === value) return
|
|
220
|
+
row.agent_status = value
|
|
221
|
+
row.agent_status_at = value ? now.toISOString() : null
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Same semantics as store.updateComment.
|
|
225
|
+
function update(number, changes, now = new Date()) {
|
|
226
|
+
if (!changes || typeof changes !== 'object') throw new CommentError(400, 'invalid_change', 'Nothing to change.')
|
|
227
|
+
const body = changes.body !== undefined ? cleanBody(changes.body) : undefined
|
|
228
|
+
if (changes.status !== undefined && !['open', 'resolved'].includes(changes.status)) {
|
|
229
|
+
throw new CommentError(400, 'invalid_status', 'Status must be open or resolved.')
|
|
230
|
+
}
|
|
231
|
+
if (changes.resolution !== undefined && changes.resolution !== null && typeof changes.resolution !== 'string') {
|
|
232
|
+
throw new CommentError(400, 'invalid_resolution', 'The resolution note must be a string.')
|
|
233
|
+
}
|
|
234
|
+
const agentStatus = changes.agentStatus === undefined ? undefined : cleanAgentStatus(changes.agentStatus)
|
|
235
|
+
const fixRef = changes.fixRef === undefined ? undefined : cleanFixRef(changes.fixRef)
|
|
236
|
+
const row = { ...get(number) }
|
|
237
|
+
if (body !== undefined && body !== row.body) {
|
|
238
|
+
t.run(
|
|
239
|
+
`UPDATE comments SET dispatch_hash = NULL, dispatch_by = NULL, dispatch_at = NULL, run_approved = 0 WHERE ${SCOPE} AND number = :number`,
|
|
240
|
+
{ ...key, number: row.number },
|
|
241
|
+
)
|
|
242
|
+
row.body = body
|
|
243
|
+
row.edited_at = now.toISOString()
|
|
244
|
+
}
|
|
245
|
+
if (fixRef !== undefined) row.fix_ref = fixRef || null
|
|
246
|
+
if (changes.status !== undefined && changes.status !== row.status) {
|
|
247
|
+
row.status = changes.status
|
|
248
|
+
row.resolved_at = changes.status === 'resolved' ? now.toISOString() : null
|
|
249
|
+
if (changes.status === 'open') {
|
|
250
|
+
row.resolution = null
|
|
251
|
+
row.reopen_count = (row.reopen_count || 0) + 1
|
|
252
|
+
row.reopened_at = now.toISOString()
|
|
253
|
+
if (agentStatus === undefined) setAgent(row, null, now)
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (agentStatus !== undefined) setAgent(row, agentStatus, now)
|
|
257
|
+
if (typeof changes.resolution === 'string' && row.status === 'resolved') {
|
|
258
|
+
row.resolution = changes.resolution.trim().slice(0, COMMENT_BODY_MAX) || null
|
|
259
|
+
}
|
|
260
|
+
return save(row, now)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// `author`: { name, handle } for a person, { name, agent: true } for a
|
|
264
|
+
// token, { name, guest: true } for a share link's guest.
|
|
265
|
+
function reply(number, input, author, now = new Date()) {
|
|
266
|
+
const body = cleanBody(input?.body)
|
|
267
|
+
const row = { ...get(number) }
|
|
268
|
+
const replies = JSON.parse(row.replies || '[]')
|
|
269
|
+
if (replies.length >= REPLIES_MAX) throw new CommentError(409, 'too_many_replies', `A comment holds at most ${REPLIES_MAX} replies.`)
|
|
270
|
+
const entry = {
|
|
271
|
+
id: Math.max(0, ...replies.map((item) => Number(item.id) || 0)) + 1,
|
|
272
|
+
author: author.name,
|
|
273
|
+
...(author.handle ? { handle: author.handle } : {}),
|
|
274
|
+
...(author.agent ? { agent: true } : {}),
|
|
275
|
+
...(author.guest ? { guest: true } : {}),
|
|
276
|
+
...(author.guest && author.shareId ? { share: author.shareId } : {}),
|
|
277
|
+
...(!author.guest && author.userId ? { user: author.userId } : {}),
|
|
278
|
+
...(author.agent && author.tokenId ? { token: author.tokenId } : {}),
|
|
279
|
+
body,
|
|
280
|
+
createdAt: now.toISOString(),
|
|
281
|
+
}
|
|
282
|
+
row.replies = JSON.stringify([...replies, entry])
|
|
283
|
+
// The reply as everyone sees it: which link, account or token wrote it
|
|
284
|
+
// stays stored only.
|
|
285
|
+
const shown = { ...entry }
|
|
286
|
+
delete shown.share
|
|
287
|
+
delete shown.user
|
|
288
|
+
delete shown.token
|
|
289
|
+
return { comment: save(row, now), reply: shown }
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Compare-and-set like store.setAgentStatus: with onlyFrom, changes only an
|
|
293
|
+
// open comment whose status is one of them.
|
|
294
|
+
function setAgentStatus(number, value, { onlyFrom = null } = {}, now = new Date()) {
|
|
295
|
+
const next = cleanAgentStatus(value)
|
|
296
|
+
if (onlyFrom && (!Array.isArray(onlyFrom) || onlyFrom.some((item) => item !== null && !AGENT_STATUSES.includes(item)))) {
|
|
297
|
+
throw new CommentError(400, 'invalid_agent_status', 'onlyFrom: a list of agent statuses (null for none).')
|
|
298
|
+
}
|
|
299
|
+
const row = { ...get(number) }
|
|
300
|
+
const current = AGENT_STATUSES.includes(row.agent_status) ? row.agent_status : null
|
|
301
|
+
if ((onlyFrom && (row.status !== 'open' || !onlyFrom.includes(current))) || current === next) {
|
|
302
|
+
return { comment: rowToComment(row), changed: false }
|
|
303
|
+
}
|
|
304
|
+
setAgent(row, next, now)
|
|
305
|
+
return { comment: save(row, now), changed: true }
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// A person's emoji reaction on the comment (replyId null) or a reply,
|
|
309
|
+
// added or taken away. `person` is { name, handle, userId }; the account id
|
|
310
|
+
// is stored to match the person again and never shown. Keeps updated_at:
|
|
311
|
+
// a reaction is not work on the comment.
|
|
312
|
+
function react(number, { emoji, replyId = null }, person) {
|
|
313
|
+
const row = get(number)
|
|
314
|
+
const who = { name: person.name, ...(person.handle ? { handle: person.handle } : {}), user: person.userId }
|
|
315
|
+
const run = (list) => {
|
|
316
|
+
try {
|
|
317
|
+
return toggleReaction(list, emoji, who)
|
|
318
|
+
} catch (error) {
|
|
319
|
+
throw new CommentError(error.code === 'too_many_reactions' ? 409 : 400, error.code || 'invalid_reaction', error.message)
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
let added
|
|
323
|
+
if (replyId === null || replyId === undefined) {
|
|
324
|
+
const result = run(parseList(row.reactions))
|
|
325
|
+
added = result.added
|
|
326
|
+
t.run(`UPDATE comments SET reactions = :reactions WHERE ${SCOPE} AND number = :number`, {
|
|
327
|
+
...key,
|
|
328
|
+
number: row.number,
|
|
329
|
+
reactions: JSON.stringify(result.reactions),
|
|
330
|
+
})
|
|
331
|
+
} else {
|
|
332
|
+
const replies = JSON.parse(row.replies || '[]')
|
|
333
|
+
const index = replies.findIndex((reply) => Number(reply.id) === Number(replyId))
|
|
334
|
+
if (index < 0) throw new CommentError(404, 'unknown_reply', 'That reply does not exist.')
|
|
335
|
+
const result = run(replies[index].reactions)
|
|
336
|
+
added = result.added
|
|
337
|
+
const rest = { ...replies[index] }
|
|
338
|
+
delete rest.reactions
|
|
339
|
+
replies[index] = result.reactions.length ? { ...rest, reactions: result.reactions } : rest
|
|
340
|
+
t.run(`UPDATE comments SET replies = :replies WHERE ${SCOPE} AND number = :number`, {
|
|
341
|
+
...key,
|
|
342
|
+
number: row.number,
|
|
343
|
+
replies: JSON.stringify(replies),
|
|
344
|
+
})
|
|
345
|
+
}
|
|
346
|
+
return { comment: rowToComment(rowOf(row.number)), added }
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function remove(number) {
|
|
350
|
+
const row = get(number)
|
|
351
|
+
t.run(`DELETE FROM comments WHERE ${SCOPE} AND number = :number`, { ...key, number: row.number })
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
return { list, counts, get, create, update, reply, react, setAgentStatus, remove }
|
|
355
|
+
}
|