@vite-hub/comments 0.0.0 → 0.3.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/LICENSE +201 -0
- package/README.md +291 -0
- package/dist/anchor.d.ts +43 -0
- package/dist/anchor.js +133 -0
- package/dist/client-diagnostics-CcFpkbAk.js +40 -0
- package/dist/client.d.ts +176 -0
- package/dist/client.js +738 -0
- package/dist/console-h4KlhIik.d.ts +332 -0
- package/dist/context-BqIsmYP1.js +61 -0
- package/dist/database/postgres.d.ts +1402 -0
- package/dist/database/postgres.js +81 -0
- package/dist/database-CA1aHKN7.d.ts +1495 -0
- package/dist/database.d.ts +12 -0
- package/dist/database.js +2 -0
- package/dist/define-43mMR39K.js +89 -0
- package/dist/define-B3TF9zzn.d.ts +204 -0
- package/dist/errors-4gelGVmK.js +57 -0
- package/dist/errors-C6qptPoG.js +135 -0
- package/dist/errors-CyQyKAvy.d.ts +185 -0
- package/dist/fold-name-B7Zb3-MF.js +7 -0
- package/dist/identity-CBOzgxSf.js +39 -0
- package/dist/index-DNhy69Wt.d.ts +83 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +196 -0
- package/dist/install-BcMPgPk_.js +1154 -0
- package/dist/install-W9ghhGoh.d.ts +36 -0
- package/dist/installed-B-PfERBU.js +30 -0
- package/dist/invariant-D8_xBqmZ.js +22 -0
- package/dist/migrate-B0k_0swk.d.ts +54 -0
- package/dist/migrate-BDQ8xqUs.js +289 -0
- package/dist/object-Da1zyPCR.js +11 -0
- package/dist/operations-D4A1Fcgf.d.ts +160 -0
- package/dist/operations-DMMIfYPW.js +1159 -0
- package/dist/operations-GLeOqgMt.d.ts +257 -0
- package/dist/operations.d.ts +3 -0
- package/dist/operations.js +3 -0
- package/dist/overlay/client-DmsSMOzi.js +4 -0
- package/dist/overlay/dist-BoHMOFax.js +14 -0
- package/dist/overlay/dist-yIZmvSaU.js +1 -0
- package/dist/overlay/overlay.js +201 -0
- package/dist/page/loader.js +142 -0
- package/dist/page-B41ozN63.js +40 -0
- package/dist/page-DtbMOEMd.d.ts +22 -0
- package/dist/page-tools-shared-NHUjKI8E.js +117 -0
- package/dist/path--zk6PEMz.js +18 -0
- package/dist/plugin-PS8PMYus.js +74 -0
- package/dist/realtime.d.ts +12 -0
- package/dist/realtime.js +47 -0
- package/dist/rpc-HBcWtg0T.d.ts +17 -0
- package/dist/rpc-RKPAusHp.js +153 -0
- package/dist/rpc.d.ts +2 -0
- package/dist/rpc.js +2 -0
- package/dist/runtime/auth.d.ts +8 -0
- package/dist/runtime/auth.js +18 -0
- package/dist/runtime/connect/node.d.ts +1 -0
- package/dist/runtime/connect/node.js +2 -0
- package/dist/runtime/connect.d.ts +1 -0
- package/dist/runtime/connect.js +2 -0
- package/dist/runtime/console.d.ts +5 -0
- package/dist/runtime/console.js +64 -0
- package/dist/runtime/index.d.ts +270 -0
- package/dist/runtime/index.js +1113 -0
- package/dist/server-D8k-0GXe.js +72 -0
- package/dist/server.d.ts +20 -0
- package/dist/server.js +7 -0
- package/dist/service-CFVm23k7.d.ts +158 -0
- package/dist/service-GHhhyQj4.js +609 -0
- package/dist/session-DVx-1Tym.d.ts +14 -0
- package/dist/session-EkudzUmm.js +503 -0
- package/dist/testing.d.ts +30 -0
- package/dist/testing.js +187 -0
- package/dist/tokens-DSbwkVSy.js +58 -0
- package/dist/types-BIS0hCEl.d.ts +124 -0
- package/dist/types-CZujrMFd.d.ts +322 -0
- package/dist/vite.d.ts +308 -0
- package/dist/vite.js +1660 -0
- package/package.json +153 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# @vite-hub/comments
|
|
2
|
+
|
|
3
|
+
`@vite-hub/comments` lets a team comment on a live ViteHub site, Figma style. People pin comments to any element of any page, reply, mention each other, and resolve threads. Everyone on the same page sees the pins, the people, and their cursors live. Where an element renders a Content field, the same person can edit its text in place, and the edit saves a Content draft.
|
|
4
|
+
|
|
5
|
+
## Turn it on
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @vite-hub/comments
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// vite.config.ts
|
|
13
|
+
import { defineConfig } from "vite"
|
|
14
|
+
import comments from "@vite-hub/comments/vite"
|
|
15
|
+
|
|
16
|
+
export default defineConfig({ plugins: [comments()] })
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
There is nothing else to set up. `comments()` adds Nitro as the server unless the app has it, serves the Comments API, and keeps threads in `.vitehub/data/comments.sqlite`, a file that it opens with Node's built-in `node:sqlite` and creates the tables in. On the dev server, anyone may comment as a guest. Open any page with `?vitehub`.
|
|
20
|
+
|
|
21
|
+
A production build connects to `DATABASE_URL`, `TURSO_DATABASE_URL`, or a Cloudflare D1 binding `DB`, whichever the server has. Or point it somewhere: `comments({ database: "d1:COMMENTS" })`, `"env:COMMENTS_DATABASE_URL"`, or `"file:./data/comments.sqlite"` on Node. Comments creates its tables there on first use, and moves the tables of an earlier version before its first query, so there is no migration to run.
|
|
22
|
+
|
|
23
|
+
### Sign in with GitHub
|
|
24
|
+
|
|
25
|
+
Create a GitHub App, install it on the repository, and list `https://<site>/api/_vitehub/comments/auth/callback/github` as its callback URL. Then set `VITEHUB_GITHUB_CLIENT_ID`, `VITEHUB_GITHUB_CLIENT_SECRET`, and `VITEHUB_SECRET` on the server. People who can push to the repository may comment. The session is an encrypted cookie, so the server stores no users.
|
|
26
|
+
|
|
27
|
+
### In a ViteHub app
|
|
28
|
+
|
|
29
|
+
[`vite-hub`](https://www.npmjs.com/package/vite-hub) wires the package for you, with any ViteHub Database, presence over Realtime, Content edits in place, the Console, and MCP tools.
|
|
30
|
+
|
|
31
|
+
```ts [server/comments.ts]
|
|
32
|
+
// server/comments.ts
|
|
33
|
+
import { defineComments } from "vite-hub/comments"
|
|
34
|
+
|
|
35
|
+
export const comments = defineComments({
|
|
36
|
+
// Who may read, comment, and resolve. Without it, a production build lets in only people signed in with GitHub who
|
|
37
|
+
// can push to the repository, and people that the Console admits to Comments; everyone may on the dev server.
|
|
38
|
+
authorize: ({ user }) => user?.role === "editor",
|
|
39
|
+
})
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Without the file, `vitehub({ comments: true })` turns Comments on with `defineComments()`, and so does an object such as `{ database: "team" }`. The Console does not turn it on. `comments: false` turns it off. Docs sites of `@vite-hub/docs` on `vite-hub/nuxt` set it by default; `docs: { comments: false }` turns it off.
|
|
43
|
+
|
|
44
|
+
ViteHub then adds the `vitehub_comment_threads`, `vitehub_comment_messages`, `vitehub_comment_counters`, and `vitehub_comment_forge` tables to the default Database (or the only one, or `defineComments({ database: "name" })`; without a Database Definition, the implicit `default` Database of `@vite-hub/database`), serves `/api/_vitehub/comments/**`, and adds a Comments section to the Console. Apply the tables with `vitehub database generate` and `vitehub database migrate`.
|
|
45
|
+
|
|
46
|
+
A ViteHub Database with the tables of the earlier live-site model must move them once in a migration: see [Upgrade to one model](https://vitehub.dev/docs/comments/configure#upgrade-to-one-model). Until then every Comments request fails with `COMMENTS_R0019`, whose `fix` names `commentsMigrationSql({ from: "live" })`. `migrateComments(db)` from `@vite-hub/comments/database` moves them in place and keeps the old tables as a backup.
|
|
47
|
+
|
|
48
|
+
Comments is a ViteHub page tool. Open any page with `?vitehub`, or `?vitehub=comments`. The server adds the page tools loader to that page. The loader calls `GET /_vitehub/page/session`, which allows Comments when `authorize` allows `read` or offers GitHub sign-in or account switching, and sets the `vitehub-tools` cookie so later pages open in comment mode too. The loader then draws the ViteHub tools pill at the bottom of the page and imports the overlay. The overlay draws its toolbar in its segment of the pill, and its pins, cursors, and cards in `<vitehub-comments>`. The pill's **×** button turns the page tools off, so the toolbar has no close button of its own. A small ViteHub hexagon then stays at the pill's place, on every page, and pressing it turns them on again. The cookie is a hint, never a credential: every Comments route checks `authorize` again. A request without the hint gets the page with a short inline bootstrap that loads the loader in the browser only when the hint is there, so prerendered pages that a host serves as static files work too. Opening the Console calls the same session route, so the Console's people see the toolbar on the app's pages. The session route and the loader do not need the Console.
|
|
49
|
+
|
|
50
|
+
A single page app whose `index.html` Vite serves or builds gets a short inline check in that file. It imports the loader in the browser when the page has the query or the cookie, so a built `index.html` that a host serves as a static file also gets the tools. When the app has a Vite `base` or a Nuxt `app.baseURL`, such as `/docs/`, every path follows it: `/docs/_vitehub/page/session`, `/docs/_vitehub/page/assets/loader.js`, and `/docs/api/_vitehub/comments/**`. The loader and the overlay find the base from their own URL, including the overlay's Realtime presence and live update connections.
|
|
51
|
+
|
|
52
|
+
Custom `identify` names the user of every surface, and `authorize` decides; Comments then reads neither the Auth session nor the Console's access. Without `identify`, when the app has the Console, its Comments section controls access, and opening the Console enables the toolbar on the app's pages. Otherwise Comments reads the Auth session.
|
|
53
|
+
|
|
54
|
+
`authorize` gets `console: true` when the Console admitted the caller to its Comments section: the comment operations of the Console, the CLI, and ViteHub MCP, and `commentsMcpTools(comments, { console: true })`. With `identify`, these calls have no app user, so `authorize` must allow them itself:
|
|
55
|
+
|
|
56
|
+
```ts fragment
|
|
57
|
+
// Console callers, and the app's editors on the page.
|
|
58
|
+
authorize: ({ console, user }) => console === true || user?.role === "editor",
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
By default the overlay comments on the page itself, `{ kind: "page", id: "/pricing" }`. `page` maps a page to a subject of your own. The server decides: the overlay reads the subject from the session route, and a thread that the overlay starts must be on that subject. `authorize` still runs on it. A `page` given to the API, as the `?page=` query of a route such as `GET /threads?page=/pricing` or in the body of `POST /threads`, means that same subject.
|
|
62
|
+
|
|
63
|
+
```ts fragment
|
|
64
|
+
import { defineCommentSubject, defineComments } from "vite-hub/comments"
|
|
65
|
+
|
|
66
|
+
export const comments = defineComments({
|
|
67
|
+
subjects: [defineCommentSubject({ kind: "drop", scope: id => `drop:${id}` })],
|
|
68
|
+
page: async ({ url }) => {
|
|
69
|
+
const id = url.pathname.match(/^\/d\/([^/]+)/)?.[1]
|
|
70
|
+
return id ? { kind: "drop", id, revision: await latestRevision(id) } : undefined
|
|
71
|
+
},
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`url` has the origin and the normalized path, without query and hash. Return `undefined` or `null` for the page itself. With `revision`, new threads from the overlay start on the version that the person saw: the overlay sends the `revision` that `/session` gave for the page, and the server keeps it when it is not newer than the one `page` gives now. A missing or newer revision becomes the one from `page`.
|
|
76
|
+
|
|
77
|
+
Console `comments:read` access allows reading threads and joining presence. Writes also require full `comments` access and the Comments policy.
|
|
78
|
+
|
|
79
|
+
Press `C` and click the page to start a thread, or press `B` to draw on the page with a marker, a pen, arrows, or text labels and comment with a picture of it. Press `E` to edit text that `contentField()` marks:
|
|
80
|
+
|
|
81
|
+
```vue
|
|
82
|
+
<script setup lang="ts">
|
|
83
|
+
import { contentField } from "vite-hub/content/client"
|
|
84
|
+
</script>
|
|
85
|
+
|
|
86
|
+
<template>
|
|
87
|
+
<h1 v-bind="contentField('about', 'hero.title')">{{ page.hero.title }}</h1>
|
|
88
|
+
<span v-bind="contentField('customers', customer.id, 'name')">{{ customer.name }}</span>
|
|
89
|
+
</template>
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
An edit reads the draft, changes one string field, and saves the document with `ifDigest`. It needs `content.can({ action: "write" })`. Publishing stays in the Content publish flow.
|
|
93
|
+
|
|
94
|
+
## Server API
|
|
95
|
+
|
|
96
|
+
Every method returns `[error, data]`.
|
|
97
|
+
|
|
98
|
+
```ts fragment
|
|
99
|
+
import { comments } from "~/server/comments"
|
|
100
|
+
|
|
101
|
+
const [error, threads] = await comments.list({ page: "/pricing", status: "open" })
|
|
102
|
+
// threads[0].messages[0].body, threads[0].subject: { kind: "page", id: "/pricing" }
|
|
103
|
+
await comments.reply(threadId, { body: "Fixed in the draft", author: { id: "agent", name: "Docs Agent" } })
|
|
104
|
+
await comments.resolve(threadId, { by: { id: "agent", name: "Docs Agent" } })
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Post to the pull request
|
|
108
|
+
|
|
109
|
+
On a preview, `forge: { provider: "github" }` posts each thread to the pull request that the build previews: one summary comment that lists every thread, then one comment per comment, with the page, the element, the components, and the browser. Resolving a thread hides its comment. It posts with the GitHub App of `VITEHUB_GITHUB_APP_ID` and `VITEHUB_GITHUB_PRIVATE_KEY`, or as each person: people who signed in with GitHub post as themselves, and `token(author)` returns the token for any other sign-in. Each thread's `forge` field says whether it posted.
|
|
110
|
+
|
|
111
|
+
## Deployment and security boundaries
|
|
112
|
+
|
|
113
|
+
- Threads live only in the Database. Presence rooms hold no document, and connections are read-only. The server binds each person's presence to the user that `identify` or the Auth session resolves.
|
|
114
|
+
- Pins anchor to the Content field first, then a CSS path checked against the element text, then the text alone, then the page point. A pin whose element is gone shows dashed.
|
|
115
|
+
- A thread change tells the other tabs of the page through the Realtime events room, and they read the threads again. Changes from the server API reach open pages when they refetch: on focus and every 30 seconds.
|
|
116
|
+
- Without a Realtime authority, such as on Vercel, comments work without presence or cursors.
|
|
117
|
+
- Write routes compare `Origin` with the request origin, and honour `X-Forwarded-Proto` and `X-Forwarded-Host` from a proxy that ends TLS.
|
|
118
|
+
- Page context stores only the URL origin and pathname, validated against the request origin and thread page. Query parameters, URL credentials, and fragments are discarded. Nuxt component files come from its resolved component registry.
|
|
119
|
+
- Guests can comment only when `authorize` allows `user: null`. They name themselves in the browser, and their names and IDs are display data only. Guests cannot delete comments or threads. `defineComments({ rateLimit: { limit: 5, window: "1m" } })` limits the writes of each guest address.
|
|
120
|
+
- The overlay is a custom element with a shadow root, so the page's CSS does not reach it.
|
|
121
|
+
|
|
122
|
+
## Public imports
|
|
123
|
+
|
|
124
|
+
| Import | Purpose |
|
|
125
|
+
| --- | --- |
|
|
126
|
+
| `@vite-hub/comments` | `defineComments()` and types. `installComments()` is in `@vite-hub/comments/server`. |
|
|
127
|
+
| `@vite-hub/comments/client` | Framework-free client, transport types, `createAnchor()`, and `resolveAnchor()`. |
|
|
128
|
+
| `@vite-hub/comments/testing` | In-memory client transport for tests and examples. |
|
|
129
|
+
| `@vite-hub/comments/anchor` | DOM anchoring: `createAnchor()`, `placeAnchor()`, `selectorOf()`, `labelOf()`. |
|
|
130
|
+
| `@vite-hub/comments/database` | `commentsSchema`, the Drizzle tables, and `commentsTables`, the statements that create them. |
|
|
131
|
+
| `@vite-hub/comments/realtime` | `commentsRealtime()`, the presence room Realtime Definition. |
|
|
132
|
+
| `@vite-hub/comments/runtime` | `createCommentsHandler()` and `defineCommentsPlugin()` for a manual Nitro setup; `commentsMcpTools()` for an app's own MCP server. |
|
|
133
|
+
| `@vite-hub/comments/vite` | `comments()`, the plugin for apps without `vite-hub`, and `hubComments()`, the integration that `vitehub()` adds. |
|
|
134
|
+
| `@vite-hub/comments/runtime/connect` | `connectDatabase()` of `@vite-hub/database/connect`, for the files that `comments()` generates. `/node` also opens SQLite files. |
|
|
135
|
+
|
|
136
|
+
Generated files under `.vitehub/nitro/comments` are inspectable build output, not application imports.
|
|
137
|
+
|
|
138
|
+
`createCommentsHandler(options)` returns a Fetchable, `{ fetch(request) }`. Mount it as a Nitro or H3 route at `/api/_vitehub/comments/**`, or call `handler.fetch(request)` yourself. Every outcome is a `Response`, also the errors. The handler gives the Request to `identify` as `event`, and reads cookies, the Cloudflare bindings, and `waitUntil` from its Standard Context, `request["~standard"]`. In a ViteHub app, Nitro sets it. For a Request from elsewhere, the handler sets it.
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { createCommentsHandler } from "@vite-hub/comments/runtime";
|
|
142
|
+
import { comments } from "./server/comments";
|
|
143
|
+
|
|
144
|
+
const handler = createCommentsHandler({ comments, realtime: false });
|
|
145
|
+
const response = await handler.fetch(new Request("https://site.test/api/_vitehub/comments/threads?page=/"));
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
`commentsMcpTools(comments)` returns tools with `call(input, { user })`, where your server authenticates `user` or passes `null` for an anonymous caller. With a request `event`, such as the Request or an H3 event, a configured Comments `identify` or session reader takes precedence over `user`. The `page` filter of `comments_list_threads` and of `comments.threads` takes a path such as `/pricing` or a full URL of the page. When `authorize` hides threads from the caller, the list says how many and why, and its structured output has a `hidden` count. See [Fix comments with an Agent](https://vitehub.dev/docs/comments/configure#fix-comments-with-an-agent).
|
|
149
|
+
|
|
150
|
+
## Learn more
|
|
151
|
+
|
|
152
|
+
- [Comments](https://vitehub.dev/docs/comments) covers access, guests, mentions, hosts, and errors.
|
|
153
|
+
- [Content](https://vitehub.dev/docs/content) covers collections, drafts, and publishing.
|
|
154
|
+
- [Realtime](https://vitehub.dev/docs/realtime) covers room authorities.
|
|
155
|
+
|
|
156
|
+
### Floating window
|
|
157
|
+
|
|
158
|
+
Comments loads when its pill button is first used. Its thread list and replies open in the shared page-tools window. Expand or minimize it without replacing the reply form. Navigation updates the thread list to the current page. The pill asks before turning off tools with an unsent comment.
|
|
159
|
+
|
|
160
|
+
Select **Edit**, then a marked field, to edit plain text in place. A field that is not plain text says to edit it in the Console at console.vitehub.dev. The Content write and publish rules do not change.
|
|
161
|
+
|
|
162
|
+
## One model
|
|
163
|
+
|
|
164
|
+
The overlay, the server API, the comment operations, the browser client, the Console, and MCP share one model: threads on a subject, numbered `n` in a scope, with Markdown `messages`, mentions as ids, `metadata`, an optional subject `revision`, and attachments. One `authorize` policy decides on every surface.
|
|
165
|
+
|
|
166
|
+
`server/comments.ts` exports one definition, as `comments` or as the default export. Either form runs every surface: the overlay and the Comments API, presence rooms, the MCP tools, and the comment operations of the Console, the CLI, and MCP. See [Comment operations](https://vitehub.dev/docs/comments/operations/get-started). Threads live in `commentsSchema` from `/database`, or `commentsPostgresSchema` from `/database/postgres` with `vitehub({ comments: { dialect: "postgresql" } })`.
|
|
167
|
+
|
|
168
|
+
## Browser client
|
|
169
|
+
|
|
170
|
+
Figma-style comments for your own running app. A signed-in team member drops a pin on any spot of a page, and the team replies in the thread.
|
|
171
|
+
|
|
172
|
+
This package holds what the server and the UI share:
|
|
173
|
+
|
|
174
|
+
- `@vite-hub/comments/client` exports the browser types: `CommentThread`, `CommentMessage`, `CommentAnchor`, `CommentSubject`, `CommentUser`, `CommentStatus`, and the `CommentsTransport` contract.
|
|
175
|
+
- `@vite-hub/comments/client` has no framework. It makes anchors, finds them again, and calls the comment operations through a transport.
|
|
176
|
+
- `@vite-hub/comments/testing` has `createMemoryTransport()` for tests and docs examples.
|
|
177
|
+
|
|
178
|
+
The Vue parts are in `@vite-hub/ui`. Use `createRestTransport()` from `@vite-hub/comments/client` for the Comments API, which guests and share-link readers can use too, or `createRpcTransport()` from `@vite-hub/comments/rpc` for the operations RPC on a development server. A browser cannot sign Console calls, so the RPC transport works only with `console: { auth: false }` or without the `console` option. `transport.attachmentUrl(key)` of the REST transport is the URL of an attachment for people who may read its thread. The overlay uses the anchoring API of `@vite-hub/comments/anchor`, which writes `element` anchors with placement hints.
|
|
179
|
+
|
|
180
|
+
## Anchors
|
|
181
|
+
|
|
182
|
+
An anchor says where a comment sits, so the pin can find the spot again after the layout changes.
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
import { createAnchor, resolveAnchor } from "@vite-hub/comments/client";
|
|
186
|
+
|
|
187
|
+
document.addEventListener("click", (event) => {
|
|
188
|
+
// From a click: the element under the point, plus where in its box the click landed.
|
|
189
|
+
const anchor = createAnchor({ x: event.clientX, y: event.clientY });
|
|
190
|
+
// { kind: "element", selector: "#pricing > div > button", x: 480, y: 310 }
|
|
191
|
+
|
|
192
|
+
const position = resolveAnchor(anchor);
|
|
193
|
+
if (position.detached) {
|
|
194
|
+
// The element is gone, hidden, or no longer holds the quote.
|
|
195
|
+
} else {
|
|
196
|
+
position.x; // The pin position in client coordinates.
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
| Kind | Made from | Keeps |
|
|
202
|
+
| --- | --- | --- |
|
|
203
|
+
| `page` | `"page"` | Nothing. The whole page. |
|
|
204
|
+
| `element` | an element, or a point | The element selector and a point in thousandths of its box. |
|
|
205
|
+
| `text` | a `Selection` or a `Range` | The selector of the nearest element, and the quote as `exact`, `prefix`, `suffix`. |
|
|
206
|
+
| `area` | an element and `options.area` | The selector and a rectangle in thousandths. |
|
|
207
|
+
| `image` | an `img`, with a point or `options.area` | The same as `element` or `area`, on an image. |
|
|
208
|
+
|
|
209
|
+
The selector uses the first of these that names exactly one element: `data-comment-id`, `id`, `data-testid`. Else it is a short path such as `#plans > div > p:nth-of-type(2)`. Ids that a framework makes up, such as React's `:r1:`, `radix-…`, `headlessui-…`, or Vue's `v-1`, and ids that are mostly digits or hashes, are skipped. Add `data-comment-id` to an element that must keep its comments when the markup changes.
|
|
210
|
+
|
|
211
|
+
`resolveAnchor()` never guesses. A selector that names more than one element is ambiguous, so the anchor is detached. A text anchor tries every match and takes the one that holds the quote. A quote that occurs more than once needs enough text around it to match, else the anchor is detached. A resolved anchor has `clipped: true` when a scroll container or an `overflow: hidden` ancestor cuts the spot off. Hide the pin then.
|
|
212
|
+
|
|
213
|
+
Offsets are in thousandths of the element box: `500` is the middle. A pin follows its element when the window resizes.
|
|
214
|
+
|
|
215
|
+
## Client
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
import { createAnchor, createCommentsClient, createRestTransport } from "@vite-hub/comments/client";
|
|
219
|
+
|
|
220
|
+
const client = createCommentsClient({
|
|
221
|
+
transport: createRestTransport(), // the Comments API; see Transport below
|
|
222
|
+
user: { id: "ana", name: "Ana Ruiz" },
|
|
223
|
+
query: { page: location.pathname },
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
const stop = client.start(); // Loads the threads, then keeps them fresh.
|
|
227
|
+
await client.create({
|
|
228
|
+
subject: { kind: "page", id: location.pathname },
|
|
229
|
+
anchor: createAnchor(document.querySelector("#pricing button")!),
|
|
230
|
+
body: "This button is too small.",
|
|
231
|
+
});
|
|
232
|
+
client.threads; // The cached threads, ordered by number.
|
|
233
|
+
client.onChange(() => console.log(`${client.threads.length} threads`));
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
To retry a create safely, give it a `requestId`, such as `crypto.randomUUID()`, and keep it for that draft. A second create with the same `requestId` in the same scope returns the first thread and creates no new thread. Only the author of the first thread gets it back, for the same subject, and only when that author may still read it. Anything else fails: the Operations and `POST /threads` answer `409` with `COMMENTS_R0022`, and the server API `comments.create()` returns `COMMENTS_INVALID_INPUT` with the reason. Make a new `requestId` for each new comment.
|
|
237
|
+
|
|
238
|
+
`start()` listens to the transport's `subscribe`, such as Realtime. Without `subscribe` it refreshes when the window gets focus. A list that comes back late never undoes a thread that was created, changed, or deleted while it was on its way, and an event that is older than the stored thread is ignored. A transport that lost its connection sends `{ type: "reconnected" }` when it has it again, and the client lists the threads once more.
|
|
239
|
+
|
|
240
|
+
Without an explicit query `limit`, refresh loads every page. Multi-page refreshes require two matching scans before replacing the cache. If three scans cannot agree because the list keeps changing, refresh raises `COMMENTS_R0015` and preserves the previous cache; refresh again to retry. An explicit `limit` makes one request.
|
|
241
|
+
|
|
242
|
+
### Transport
|
|
243
|
+
|
|
244
|
+
A transport is the seam to the server. Each method has the name and the input of one `comments.*` operation.
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
import type { CommentCreateInput, CommentDeleteInput, CommentEditInput, CommentEvent, CommentGetInput, CommentListQuery, CommentReplyInput, CommentStatusInput, CommentThread, CommentUser, CommentUsersInput } from "@vite-hub/comments"
|
|
248
|
+
|
|
249
|
+
interface CommentsTransport {
|
|
250
|
+
supportsAttachments?: boolean; // Defaults to true for custom transports.
|
|
251
|
+
list(query: CommentListQuery): Promise<CommentThread[]>;
|
|
252
|
+
get(input: CommentGetInput): Promise<CommentThread | null>; // { id } or { scope, n }
|
|
253
|
+
create(input: CommentCreateInput): Promise<CommentThread>;
|
|
254
|
+
reply(input: CommentReplyInput): Promise<CommentThread>;
|
|
255
|
+
edit(input: CommentEditInput): Promise<CommentThread>;
|
|
256
|
+
delete(input: CommentDeleteInput): Promise<CommentThread | null>;
|
|
257
|
+
resolve(input: CommentStatusInput): Promise<CommentThread>;
|
|
258
|
+
reopen(input: CommentStatusInput): Promise<CommentThread>;
|
|
259
|
+
users(input: CommentUsersInput): Promise<CommentUser[]>;
|
|
260
|
+
subscribe?(listener: (event: CommentEvent) => void): () => void;
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
A mutation returns the thread as it is afterwards. `delete` returns `null` when the thread is gone.
|
|
265
|
+
|
|
266
|
+
Transports accept attachment keys by default, including the shipped `createRpcTransport()`. Custom transports that reject attachment keys should set `supportsAttachments: false` to disable file controls in comment surfaces.
|
|
267
|
+
|
|
268
|
+
`createMemoryTransport()` keeps threads in memory. Use it in tests and docs examples:
|
|
269
|
+
|
|
270
|
+
```ts
|
|
271
|
+
import { createCommentsClient } from "@vite-hub/comments/client";
|
|
272
|
+
import { createMemoryTransport } from "@vite-hub/comments/testing";
|
|
273
|
+
|
|
274
|
+
const ana = { id: "ana", name: "Ana Ruiz" };
|
|
275
|
+
const transport = createMemoryTransport({ user: ana });
|
|
276
|
+
const client = createCommentsClient({ transport, user: ana });
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
`transport.as(user)` gives another person on the same store, and `transport.reconnect()` sends `reconnected` to the listeners.
|
|
280
|
+
|
|
281
|
+
## Errors
|
|
282
|
+
|
|
283
|
+
Errors have a stable code, a `why`, and a `fix`.
|
|
284
|
+
|
|
285
|
+
| Code | Meaning |
|
|
286
|
+
| --- | --- |
|
|
287
|
+
| `COMMENTS_R0001` | `createAnchor()` found nothing to anchor to. |
|
|
288
|
+
| `COMMENTS_R0002` | The thread does not exist. |
|
|
289
|
+
| `COMMENTS_R0003` | The message does not exist in the thread. |
|
|
290
|
+
| `COMMENTS_R0004` | Only the author can edit or delete a message. |
|
|
291
|
+
| `COMMENTS_R0005` | A comment needs some text. |
|
package/dist/anchor.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { D as ElementAnchor } from "./types-CZujrMFd.js";
|
|
2
|
+
import "./types-BIS0hCEl.js";
|
|
3
|
+
//#region src/anchor.d.ts
|
|
4
|
+
/** The anchor of a pin that the overlay drops: an element anchor with every hint that `placeAnchor()` uses. */
|
|
5
|
+
export type PinAnchor = ElementAnchor & Required<Pick<ElementAnchor, "label" | "page" | "viewport">>;
|
|
6
|
+
/** The attribute that Content's `contentField()` writes on an element that renders a field. */
|
|
7
|
+
export declare const fieldAttribute = "data-content-field";
|
|
8
|
+
/**
|
|
9
|
+
* CSS path of an element: tag and `:nth-of-type` per level, at most 10 levels, from the closest ancestor with an id or
|
|
10
|
+
* a Content field. Positional only, so `label` checks that it still finds the same element.
|
|
11
|
+
*/
|
|
12
|
+
export declare function selectorOf(element: Element): string;
|
|
13
|
+
/** Tag and the first 40 characters of the element text. */
|
|
14
|
+
export declare function labelOf(element: Element): string;
|
|
15
|
+
/** The closest element with a Content field, from the element itself up. */
|
|
16
|
+
export declare function fieldElementOf(element: Element): Element | undefined;
|
|
17
|
+
/** The anchor of a point on an element, in client coordinates. */
|
|
18
|
+
export declare function createAnchor(element: Element, clientX: number, clientY: number): PinAnchor;
|
|
19
|
+
export type PlacedAnchor =
|
|
20
|
+
/** On the anchored element. */
|
|
21
|
+
{
|
|
22
|
+
state: "exact";
|
|
23
|
+
element: Element;
|
|
24
|
+
x: number;
|
|
25
|
+
y: number;
|
|
26
|
+
} |
|
|
27
|
+
/** At the page point, because the element is gone. The overlay shows the pin as approximate. */
|
|
28
|
+
{
|
|
29
|
+
state: "moved";
|
|
30
|
+
x: number;
|
|
31
|
+
y: number;
|
|
32
|
+
} |
|
|
33
|
+
/** The element exists but has no box, such as inside a closed menu. */
|
|
34
|
+
{
|
|
35
|
+
state: "hidden";
|
|
36
|
+
element: Element;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Where an anchor is now, in client coordinates: the Content field, then the CSS path if its label still matches,
|
|
40
|
+
* then any element of that tag with the same label, then the page point. An anchor without a label trusts the path.
|
|
41
|
+
*/
|
|
42
|
+
export declare function placeAnchor(anchor: ElementAnchor): PlacedAnchor;
|
|
43
|
+
//#endregion
|