@workover/cli 0.6.0 → 0.8.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/README.md +38 -0
- package/bin/workover.mjs +235 -8
- package/lib.mjs +63 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,7 @@ workover projects # the projects you can reach, with their ids
|
|
|
20
20
|
workover doc list --project 11
|
|
21
21
|
workover doc read 1234
|
|
22
22
|
workover rules list --project 11 # the style guide agents should write by
|
|
23
|
+
workover comments list 1234 # the feedback people left on a document
|
|
23
24
|
workover draft --org 3 --project 11 "Document the billing flow"
|
|
24
25
|
workover whoami # show the current sign-in
|
|
25
26
|
workover logout # sign out and revoke this device's token
|
|
@@ -46,6 +47,7 @@ workover projects # find the project id
|
|
|
46
47
|
workover doc list --project 11 # documents in it, with their ids
|
|
47
48
|
workover doc read 1234 # the current text
|
|
48
49
|
workover doc read 1234 --json # ...for a script
|
|
50
|
+
workover doc activity 1234 # who changed it, and how
|
|
49
51
|
|
|
50
52
|
workover doc create --project 11 --title "Billing" --file billing.md
|
|
51
53
|
cat billing.md | workover doc propose 1234 --note "tightened the intro"
|
|
@@ -87,6 +89,42 @@ is how images get deleted by something that thought it was filling in a blank. I
|
|
|
87
89
|
without that scope it is refused and `doc propose` is the way through. Every direct edit is recorded
|
|
88
90
|
in the workspace's audit log, since unlike a proposal it leaves no review trail of its own.
|
|
89
91
|
|
|
92
|
+
## Comments
|
|
93
|
+
|
|
94
|
+
The other half of coworking: the feedback a person left on the work, readable and answerable from
|
|
95
|
+
the terminal that produced it.
|
|
96
|
+
|
|
97
|
+
```sh
|
|
98
|
+
workover comments list 1234 # open threads on the document
|
|
99
|
+
workover comments list 1234 --all # ...including resolved ones
|
|
100
|
+
workover comments add 1234 --to 45 "Fixed — it now says annual."
|
|
101
|
+
workover comments resolve 45
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`list` shows open threads only, with each reply printed under the comment it answers. Resolved
|
|
105
|
+
threads are hidden rather than dropped — `--all` brings them back, and the count at the bottom says
|
|
106
|
+
how many are being left out, so a document never looks quieter than it is.
|
|
107
|
+
|
|
108
|
+
**Reply before you resolve.** A thread that closes with no answer tells the person who raised it
|
|
109
|
+
nothing except that their objection is gone, and reopening is the only way back. `resolve` and
|
|
110
|
+
`reopen` are the same act in both directions, and both are recorded in the document's activity log
|
|
111
|
+
with your name against them — which you can read:
|
|
112
|
+
|
|
113
|
+
```sh
|
|
114
|
+
workover doc activity 1234 # who changed this document, and how
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
That command exists because the claim needed to be checkable. The CLI used to say a resolve was in
|
|
118
|
+
the activity log while the route that shows it was refused to the token in your hand, which reads as
|
|
119
|
+
verified and was not. It lists the last changes newest first, naming the person AND the agent that
|
|
120
|
+
acted for them — an agent works with somebody's token, so `Alex Panagis · via Claude Code` is the
|
|
121
|
+
honest attribution and either half alone is misleading.
|
|
122
|
+
|
|
123
|
+
Reading, commenting and resolving all work with a normal `workover login` — no extra scope. Deleting
|
|
124
|
+
a comment does not: it is deliberately not reachable from a token at all. An edit or a resolve leaves
|
|
125
|
+
the thread there to be read and reopened, while a deletion leaves nothing behind to notice, so that
|
|
126
|
+
one stays something a person does in the browser.
|
|
127
|
+
|
|
90
128
|
## The style guide
|
|
91
129
|
|
|
92
130
|
Your workspace's **Style Guide** (Workover → Settings → Style Guide) is the set of rules your team
|
package/bin/workover.mjs
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// Workover CLI
|
|
3
|
-
//
|
|
2
|
+
// The Workover CLI. `login` / `logout` / `whoami` authenticate through the device-authorization
|
|
3
|
+
// flow (RFC 8628); everything else works on documents with the stored token: `projects`, `doc`
|
|
4
|
+
// (list/read/create/propose/write/search/tabs), `rules`, `comments`, `draft`, `draft-set`, `eval`.
|
|
5
|
+
//
|
|
6
|
+
// This header used to say "auth only for now", left over from the first version and never updated
|
|
7
|
+
// as the commands landed. On 2026-09-09 an agent read it, concluded the CLI could not edit
|
|
8
|
+
// documents, and reported the whole editing path as missing — while `doc write` had been shipping
|
|
9
|
+
// for weeks. A stale comment claiming LESS than the code does is read as a limitation and believed,
|
|
10
|
+
// because nobody grep-checks a file header. Keep this list current or delete it.
|
|
4
11
|
import {
|
|
5
12
|
readFileSync,
|
|
6
13
|
writeFileSync,
|
|
@@ -23,6 +30,7 @@ import {
|
|
|
23
30
|
documentText,
|
|
24
31
|
contentCommitted,
|
|
25
32
|
renderRulesBlock,
|
|
33
|
+
threadComments,
|
|
26
34
|
} from '../lib.mjs';
|
|
27
35
|
|
|
28
36
|
/*
|
|
@@ -73,10 +81,17 @@ const post = async (path, body) => {
|
|
|
73
81
|
return res.json().catch(() => ({}));
|
|
74
82
|
};
|
|
75
83
|
|
|
76
|
-
/**
|
|
77
|
-
|
|
84
|
+
/**
|
|
85
|
+
* An authenticated call against the signed-in API. GET unless a body is given, POST when one is,
|
|
86
|
+
* and whatever `method` names when the route wants something else.
|
|
87
|
+
*
|
|
88
|
+
* The verb used to be inferred from the presence of a body, which covers every route these commands
|
|
89
|
+
* had until now and cannot express a PATCH — resolving a comment sends a body to a route that is
|
|
90
|
+
* not a POST, and inferring would have silently called the wrong endpoint.
|
|
91
|
+
*/
|
|
92
|
+
const authedCall = async (cfg, path, body, { method } = {}) => {
|
|
78
93
|
const res = await fetch(`${cfg.api}${path}`, {
|
|
79
|
-
method: body ? 'POST' : 'GET',
|
|
94
|
+
method: method || (body ? 'POST' : 'GET'),
|
|
80
95
|
headers: {
|
|
81
96
|
authorization: `Bearer ${cfg.token}`,
|
|
82
97
|
...(body ? { 'content-type': 'application/json' } : {}),
|
|
@@ -722,6 +737,7 @@ const DOC_USAGE = [
|
|
|
722
737
|
' [--file <path> | piped stdin] [--json] [--tab <tabId>]',
|
|
723
738
|
' needs `workover login --write`; use it only when asked to',
|
|
724
739
|
' search "<query>" [--limit N] [--json] find documents by title/content',
|
|
740
|
+
' activity <docId> [--limit N] [--json] who changed this document, and how',
|
|
725
741
|
'',
|
|
726
742
|
' `workover projects` lists the project ids.',
|
|
727
743
|
'',
|
|
@@ -741,9 +757,17 @@ async function docCommand() {
|
|
|
741
757
|
// Usage before sign-in: someone who typed the command wrong is asking what the commands are, and
|
|
742
758
|
// answering "not signed in" sends them to fix the wrong thing.
|
|
743
759
|
if (
|
|
744
|
-
![
|
|
745
|
-
|
|
746
|
-
|
|
760
|
+
![
|
|
761
|
+
'list',
|
|
762
|
+
'read',
|
|
763
|
+
'create',
|
|
764
|
+
'propose',
|
|
765
|
+
'write',
|
|
766
|
+
'search',
|
|
767
|
+
'tabs',
|
|
768
|
+
'tab-new',
|
|
769
|
+
'activity',
|
|
770
|
+
].includes(sub)
|
|
747
771
|
) {
|
|
748
772
|
console.log(DOC_USAGE);
|
|
749
773
|
process.exit(sub ? 1 : 0);
|
|
@@ -1002,6 +1026,31 @@ async function docCommand() {
|
|
|
1002
1026
|
return;
|
|
1003
1027
|
}
|
|
1004
1028
|
|
|
1029
|
+
if (sub === 'activity') {
|
|
1030
|
+
const docId = docIdArg(positionals, 'workover doc activity <docId>');
|
|
1031
|
+
const limit = rowId(flags.limit) ?? 20;
|
|
1032
|
+
const rows = (await authedCall(cfg, `/project/doc/${docId}/activity`)) || [];
|
|
1033
|
+
const shown = rows.slice(0, limit);
|
|
1034
|
+
if (asJson) return console.log(JSON.stringify(shown, null, 2));
|
|
1035
|
+
if (!shown.length) {
|
|
1036
|
+
return console.log(`\n Nothing recorded against document #${docId} yet.\n`);
|
|
1037
|
+
}
|
|
1038
|
+
console.log('');
|
|
1039
|
+
for (const r of shown) {
|
|
1040
|
+
// Person AND agent, never one or the other: an agent acts with somebody's token, so crediting
|
|
1041
|
+
// only the agent hides who authorised it and only the person hides that a machine wrote it.
|
|
1042
|
+
const who = r.actorName || (r.actorId ? `user ${r.actorId}` : 'someone');
|
|
1043
|
+
const via = r.agentName ? ` · via ${r.agentName}` : '';
|
|
1044
|
+
const when = String(r.createdAt || '').slice(0, 16).replace('T', ' ');
|
|
1045
|
+
console.log(` ${when} ${String(r.action || '').padEnd(20)} ${who}${via}`);
|
|
1046
|
+
if (r.detail) console.log(` ${stripHtml(r.detail)}`);
|
|
1047
|
+
}
|
|
1048
|
+
console.log(
|
|
1049
|
+
`\n ${shown.length} most recent${rows.length > shown.length ? ` of ${rows.length}` : ''}, newest first.\n`,
|
|
1050
|
+
);
|
|
1051
|
+
return;
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1005
1054
|
if (sub === 'search') {
|
|
1006
1055
|
const query = positionals.join(' ').trim();
|
|
1007
1056
|
if (!query) {
|
|
@@ -1026,6 +1075,180 @@ async function docCommand() {
|
|
|
1026
1075
|
|
|
1027
1076
|
}
|
|
1028
1077
|
|
|
1078
|
+
const COMMENTS_USAGE = [
|
|
1079
|
+
'',
|
|
1080
|
+
' Usage: workover comments <command>',
|
|
1081
|
+
'',
|
|
1082
|
+
' list <docId> [--all] [--json] the feedback on a document,',
|
|
1083
|
+
' open threads only unless --all',
|
|
1084
|
+
' add <docId> "<text>" leave a comment',
|
|
1085
|
+
' [--to <commentId>] ...as a reply in that thread',
|
|
1086
|
+
' [--quote "<text>"] ...against the passage it is about',
|
|
1087
|
+
' resolve <commentId> [--json] mark a thread dealt with',
|
|
1088
|
+
' reopen <commentId> [--json] put it back',
|
|
1089
|
+
'',
|
|
1090
|
+
' Resolve the thread you actually answered, and say what you did first —',
|
|
1091
|
+
' `comments add <docId> --to <commentId> "..."` then `comments resolve`. A',
|
|
1092
|
+
' thread that closes with no reply tells the person who raised it nothing,',
|
|
1093
|
+
' and reopening is the only way back.',
|
|
1094
|
+
'',
|
|
1095
|
+
].join('\n');
|
|
1096
|
+
|
|
1097
|
+
/**
|
|
1098
|
+
* One comment: who and when on the first line, then the passage it is about, then the note itself.
|
|
1099
|
+
*
|
|
1100
|
+
* `label` carries the id and the shape (a thread opens with `#45`, an answer to it with `↳ #47`),
|
|
1101
|
+
* so a reply is visibly an answer rather than a second opinion that happens to be indented. Dates
|
|
1102
|
+
* are the ISO prefix rather than anything locale-aware — the CLI's output gets pasted into issues
|
|
1103
|
+
* and diffed, and a date that renders differently per machine is a false change.
|
|
1104
|
+
*/
|
|
1105
|
+
const printComment = (c, { label, pad }) => {
|
|
1106
|
+
const who = c.authorName || `user ${c.authorId}`;
|
|
1107
|
+
const when = String(c.createdAt || '').slice(0, 10);
|
|
1108
|
+
const mark = c.resolved ? ' (resolved)' : '';
|
|
1109
|
+
console.log(`${label}${who}${when ? ` · ${when}` : ''}${mark}`);
|
|
1110
|
+
// The quoted passage first: it is what the comment is ABOUT, and the note rarely repeats it.
|
|
1111
|
+
if (c.quote) console.log(`${pad}on “${stripHtml(c.quote)}”`);
|
|
1112
|
+
for (const line of String(c.body || '').split('\n')) {
|
|
1113
|
+
console.log(`${pad}${line}`);
|
|
1114
|
+
}
|
|
1115
|
+
};
|
|
1116
|
+
|
|
1117
|
+
/**
|
|
1118
|
+
* Read, answer and close the comments on a document.
|
|
1119
|
+
*
|
|
1120
|
+
* The half of coworking that was missing. An agent could read a document and propose edits to it,
|
|
1121
|
+
* and could not see the objection somebody left on the work it had just done — so every review
|
|
1122
|
+
* round trip went back through the browser, and a person had to retype the feedback into the
|
|
1123
|
+
* terminal for the agent that caused it.
|
|
1124
|
+
*/
|
|
1125
|
+
async function commentsCommand() {
|
|
1126
|
+
const sub = process.argv[3];
|
|
1127
|
+
// Usage before sign-in, matching `doc`: someone who typed the command wrong is asking what the
|
|
1128
|
+
// commands are, and answering "not signed in" sends them to fix the wrong thing.
|
|
1129
|
+
if (!['list', 'add', 'resolve', 'reopen'].includes(sub)) {
|
|
1130
|
+
console.log(COMMENTS_USAGE);
|
|
1131
|
+
process.exit(sub ? 1 : 0);
|
|
1132
|
+
}
|
|
1133
|
+
const { flags, positionals } = parseArgs(process.argv.slice(4), {
|
|
1134
|
+
booleans: ['json', 'all'],
|
|
1135
|
+
});
|
|
1136
|
+
const cfg = requireSignIn();
|
|
1137
|
+
const asJson = flags.json === true;
|
|
1138
|
+
|
|
1139
|
+
if (sub === 'resolve' || sub === 'reopen') {
|
|
1140
|
+
const resolved = sub === 'resolve';
|
|
1141
|
+
const commentId = rowId(positionals[0]);
|
|
1142
|
+
if (commentId === null) {
|
|
1143
|
+
console.log(`\n Usage: workover comments ${sub} <commentId>`);
|
|
1144
|
+
console.log(' The comment id — `workover comments list <docId>` prints it.\n');
|
|
1145
|
+
process.exit(1);
|
|
1146
|
+
}
|
|
1147
|
+
const res = await authedCall(
|
|
1148
|
+
cfg,
|
|
1149
|
+
`/project/comments/${commentId}`,
|
|
1150
|
+
{ resolved },
|
|
1151
|
+
{ method: 'PATCH' },
|
|
1152
|
+
);
|
|
1153
|
+
if (asJson) return console.log(JSON.stringify(res, null, 2));
|
|
1154
|
+
console.log(
|
|
1155
|
+
`\n \x1b[32m✓\x1b[0m ${resolved ? 'Resolved' : 'Reopened'} comment #${commentId}.`,
|
|
1156
|
+
);
|
|
1157
|
+
// Both facts named because they are why this is allowed to happen unattended: the act is
|
|
1158
|
+
// attributable, and it is reversible.
|
|
1159
|
+
//
|
|
1160
|
+
// The activity line used to be printed WITHOUT `doc activity` existing, so the CLI asserted the
|
|
1161
|
+
// act was in the log while the only route that could show it was refused to the token in the
|
|
1162
|
+
// caller's hand. Either the claim is checkable or it should not be made; this prints the command
|
|
1163
|
+
// that checks it.
|
|
1164
|
+
console.log(
|
|
1165
|
+
` Recorded as ${resolved ? 'resolved' : 'reopened'} in the document's activity log.`,
|
|
1166
|
+
);
|
|
1167
|
+
console.log(
|
|
1168
|
+
` \`workover comments ${resolved ? 'reopen' : 'resolve'} ${commentId}\` undoes it.\n`,
|
|
1169
|
+
);
|
|
1170
|
+
return;
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
if (sub === 'add') {
|
|
1174
|
+
const usage = 'workover comments add <docId> "<text>" [--to <commentId>]';
|
|
1175
|
+
const docId = docIdArg(positionals, usage);
|
|
1176
|
+
// The text is everything after the document id, so an unquoted sentence still arrives whole.
|
|
1177
|
+
let body = positionals.slice(1).join(' ').trim();
|
|
1178
|
+
// `--file` only when asked for. `doc` reads piped stdin without being told, which is right for
|
|
1179
|
+
// a document body; a comment is a sentence somebody types, and silently waiting on stdin would
|
|
1180
|
+
// read as a command that had hung.
|
|
1181
|
+
if (!body && typeof flags.file === 'string') body = (await readBody(flags)).trim();
|
|
1182
|
+
if (!body) {
|
|
1183
|
+
console.log(`\n Usage: ${usage}`);
|
|
1184
|
+
console.log(' Or --file <path> for a longer note.\n');
|
|
1185
|
+
process.exit(1);
|
|
1186
|
+
}
|
|
1187
|
+
const parentId = flags.to == null ? null : rowId(flags.to);
|
|
1188
|
+
if (flags.to != null && parentId === null) {
|
|
1189
|
+
console.log('\n ✗ --to must be a comment id. List them: workover comments list <docId>\n');
|
|
1190
|
+
process.exit(1);
|
|
1191
|
+
}
|
|
1192
|
+
const created = await authedCall(cfg, `/project/doc/${docId}/comments`, {
|
|
1193
|
+
body,
|
|
1194
|
+
...(parentId === null ? {} : { parentId }),
|
|
1195
|
+
...(typeof flags.quote === 'string' ? { quote: flags.quote } : {}),
|
|
1196
|
+
});
|
|
1197
|
+
if (asJson) return console.log(JSON.stringify(created, null, 2));
|
|
1198
|
+
console.log(
|
|
1199
|
+
`\n \x1b[32m✓\x1b[0m ${parentId === null ? 'Commented on' : `Replied in thread #${parentId} on`} document #${docId}${created?.id ? ` (#${created.id})` : ''}.`,
|
|
1200
|
+
);
|
|
1201
|
+
console.log(' Everyone on the document can see it, and the thread stays open until');
|
|
1202
|
+
console.log(' somebody resolves it.\n');
|
|
1203
|
+
return;
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
// list
|
|
1207
|
+
const docId = docIdArg(positionals, 'workover comments list <docId>');
|
|
1208
|
+
const rows = (await authedCall(cfg, `/project/doc/${docId}/comments`)) || [];
|
|
1209
|
+
const all = threadComments(rows, { includeResolved: true });
|
|
1210
|
+
const threads = flags.all === true ? all : all.filter((t) => !t.resolved);
|
|
1211
|
+
const hidden = all.length - threads.length;
|
|
1212
|
+
|
|
1213
|
+
if (asJson) return console.log(JSON.stringify(threads, null, 2));
|
|
1214
|
+
|
|
1215
|
+
if (!threads.length) {
|
|
1216
|
+
console.log(
|
|
1217
|
+
hidden
|
|
1218
|
+
? `\n No open comments on document #${docId} — ${hidden} resolved (--all shows them).\n`
|
|
1219
|
+
: `\n No comments on document #${docId}.\n`,
|
|
1220
|
+
);
|
|
1221
|
+
return;
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
console.log('');
|
|
1225
|
+
for (const t of threads) {
|
|
1226
|
+
printComment(t, { label: ` #${String(t.id).padEnd(6)} `, pad: ' ' });
|
|
1227
|
+
for (const r of t.replies) {
|
|
1228
|
+
console.log('');
|
|
1229
|
+
printComment(r, { label: ` ↳ #${r.id} `, pad: ' ' });
|
|
1230
|
+
}
|
|
1231
|
+
console.log('');
|
|
1232
|
+
}
|
|
1233
|
+
/*
|
|
1234
|
+
* The tally has to describe what was actually printed, in both modes.
|
|
1235
|
+
*
|
|
1236
|
+
* Counting only the open ones reads as an undercount under `--all`, where the resolved threads
|
|
1237
|
+
* are right there on the screen; mentioning what is hidden is the whole point without it. So the
|
|
1238
|
+
* sentence changes rather than the number being reused.
|
|
1239
|
+
*/
|
|
1240
|
+
const open = threads.filter((t) => !t.resolved).length;
|
|
1241
|
+
const shownResolved = threads.length - open;
|
|
1242
|
+
const plural = (n) => (n === 1 ? '' : 's');
|
|
1243
|
+
const tally = hidden
|
|
1244
|
+
? `${open} open thread${plural(open)}, ${hidden} resolved (--all shows them)`
|
|
1245
|
+
: shownResolved
|
|
1246
|
+
? `${threads.length} thread${plural(threads.length)}, ${shownResolved} resolved`
|
|
1247
|
+
: `${open} open thread${plural(open)}`;
|
|
1248
|
+
console.log(` ${tally}.`);
|
|
1249
|
+
console.log(` Reply with \`workover comments add ${docId} --to <commentId> "..."\`.\n`);
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1029
1252
|
/** The project ids every `doc` command asks for. */
|
|
1030
1253
|
async function projects() {
|
|
1031
1254
|
const cfg = requireSignIn();
|
|
@@ -1050,6 +1273,7 @@ else if (cmd === 'draft-set') await draftSet();
|
|
|
1050
1273
|
else if (cmd === 'eval') await evaluateDocs();
|
|
1051
1274
|
else if (cmd === 'doc') await docCommand();
|
|
1052
1275
|
else if (cmd === 'rules') await rulesCommand();
|
|
1276
|
+
else if (cmd === 'comments') await commentsCommand();
|
|
1053
1277
|
else if (cmd === 'projects') await projects();
|
|
1054
1278
|
else {
|
|
1055
1279
|
console.log('\n Workover CLI\n');
|
|
@@ -1061,9 +1285,12 @@ else {
|
|
|
1061
1285
|
console.log(' --rules also allow changing the style guide');
|
|
1062
1286
|
console.log(' --client "<name>" what to call this token in your history');
|
|
1063
1287
|
console.log(' doc Work on documents: list, read, create, propose, write, search');
|
|
1288
|
+
console.log(' plus activity, to see who changed one and how');
|
|
1064
1289
|
console.log(' plus tabs and tab-new for documents with several bodies');
|
|
1065
1290
|
console.log(' workover doc create --project 11 --title "Billing" --file b.md');
|
|
1066
1291
|
console.log(' workover doc read 123 · workover doc propose 123 --file new.md');
|
|
1292
|
+
console.log(' comments Read, answer and resolve feedback on a document');
|
|
1293
|
+
console.log(' workover comments list 123 · workover comments resolve 45');
|
|
1067
1294
|
console.log(' rules The style guide agents should write by: list, export, add');
|
|
1068
1295
|
console.log(' workover rules export --project 11 >> AGENTS.md');
|
|
1069
1296
|
console.log(' projects List the projects you can reach, with their ids');
|
package/lib.mjs
CHANGED
|
@@ -208,3 +208,66 @@ export const renderRulesBlock = (rules) => {
|
|
|
208
208
|
'<<<END_WORKSPACE_CONTENT_RULES>>>',
|
|
209
209
|
].join('\n');
|
|
210
210
|
};
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Group a document's flat comment rows into threads, oldest conversation first.
|
|
214
|
+
*
|
|
215
|
+
* The API returns one row per comment with `parentId` pointing at whatever it answers, which is the
|
|
216
|
+
* right shape to store and the wrong shape to read: a reply printed between two unrelated top-level
|
|
217
|
+
* comments is not visibly a reply to either.
|
|
218
|
+
*
|
|
219
|
+
* `resolved` is read off the ROOT of a thread, never off each row. It is a per-row column, but a
|
|
220
|
+
* thread is the unit a person actually resolves — so filtering row by row would hide a resolved
|
|
221
|
+
* reply inside an open thread, leaving an answer with no question, and would print open replies
|
|
222
|
+
* under a resolved root, reopening a conversation nobody reopened.
|
|
223
|
+
*
|
|
224
|
+
* Two shapes are flattened rather than trusted, because both make comments DISAPPEAR from a
|
|
225
|
+
* terminal while they still exist in the app, which is the one failure a reviewer cannot catch:
|
|
226
|
+
*
|
|
227
|
+
* - A reply whose parent is not in the list is promoted to a thread of its own. The parent may have
|
|
228
|
+
* been deleted — its author or the document's owner can — and swallowing every answer to it would
|
|
229
|
+
* drop the objection silently.
|
|
230
|
+
* - A reply to a reply is attached to the thread's root. The schema threads one level deep, so
|
|
231
|
+
* nothing should be nested twice; filing it under a comment that is itself a reply would put it
|
|
232
|
+
* in a bucket nothing ever reads. The walk is bounded, so a row whose parent chain loops ends up
|
|
233
|
+
* as its own root instead of hanging.
|
|
234
|
+
*/
|
|
235
|
+
export const threadComments = (comments, { includeResolved = false } = {}) => {
|
|
236
|
+
const rows = (comments || []).filter((c) => c && c.id != null);
|
|
237
|
+
const byId = new Map(rows.map((c) => [c.id, c]));
|
|
238
|
+
|
|
239
|
+
const rootOf = (comment) => {
|
|
240
|
+
let node = comment;
|
|
241
|
+
const seen = new Set();
|
|
242
|
+
while (node.parentId != null && !seen.has(node.id)) {
|
|
243
|
+
seen.add(node.id);
|
|
244
|
+
const parent = byId.get(node.parentId);
|
|
245
|
+
if (!parent || parent.id === node.id) break;
|
|
246
|
+
node = parent;
|
|
247
|
+
}
|
|
248
|
+
return node;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
// Oldest first, so a thread reads top to bottom the way it was written. `id` breaks ties: two
|
|
252
|
+
// comments can share a timestamp, and an unstable order would shuffle them between runs.
|
|
253
|
+
const order = (a, b) =>
|
|
254
|
+
String(a.createdAt || '').localeCompare(String(b.createdAt || '')) ||
|
|
255
|
+
Number(a.id) - Number(b.id);
|
|
256
|
+
|
|
257
|
+
const roots = [];
|
|
258
|
+
const repliesByRoot = new Map();
|
|
259
|
+
for (const c of rows) {
|
|
260
|
+
const root = rootOf(c);
|
|
261
|
+
if (root === c) {
|
|
262
|
+
roots.push(c);
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
if (!repliesByRoot.has(root.id)) repliesByRoot.set(root.id, []);
|
|
266
|
+
repliesByRoot.get(root.id).push(c);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return roots
|
|
270
|
+
.filter((r) => includeResolved || !r.resolved)
|
|
271
|
+
.sort(order)
|
|
272
|
+
.map((r) => ({ ...r, replies: (repliesByRoot.get(r.id) || []).sort(order) }));
|
|
273
|
+
};
|