Files
jackwener__opencli/clis/lesswrong/comments.ts
Xule Lin d2051cdab7 feat(lesswrong): add LessWrong adapter (#773)
* feat(lesswrong): add LessWrong adapter

15 commands for the LessWrong rationality and AI alignment community:
- Post listings: curated, frontpage, new, top, top-week/month/year
- Content: read (full post), comments, shortform (quick takes)
- Discovery: tag, tags, sequences
- Users: user (profile), user-posts

All commands use the public GraphQL API (no browser required).
Time-filtered views use the `after` date parameter.
Tag lookup resolves slugs to IDs via the `tagBySlug` view.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: move lesswrong adapter to clis/ (post PR #782 restructure)

New adapter files were created at src/clis/lesswrong/ but PR #782 moved
all adapters to root clis/. Move to correct location.

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: jackwener <jakevingoo@gmail.com>
2026-04-05 03:21:48 +08:00

79 lines
2.2 KiB
TypeScript

import { cli, Strategy } from '../../registry.js';
import { EmptyResultError } from '../../errors.js';
import {
DOMAIN,
SITE,
gqlEscape,
gqlRequest,
parsePostId,
stripHtml,
} from './_helpers.js';
cli({
site: SITE,
name: 'comments',
description: 'Top comments on a post',
domain: DOMAIN,
strategy: Strategy.PUBLIC,
browser: false,
args: [
{
name: 'url-or-id',
type: 'string',
required: true,
positional: true,
help: 'Post URL or LessWrong post ID',
},
{ name: 'limit', type: 'int', default: 5, help: 'Number of comments' },
],
columns: ['rank', 'score', 'author', 'text'],
func: async (_page, kwargs) => {
const postId = gqlEscape(parsePostId(String(kwargs['url-or-id'])));
const limit = Number(kwargs.limit ?? 5);
// Fetch post title and comments in parallel
const [postData, commentsData] = await Promise.all([
gqlRequest(`query PostTitle {
post(input: {selector: {documentId: "${postId}"}}) {
result { _id title slug }
}
}`),
gqlRequest(`query Comments {
comments(input: {terms: {view: "postCommentsTop", postId: "${postId}", limit: ${limit}}}) {
results { _id user { displayName } baseScore htmlBody postedAt }
}
}`),
]);
const post = postData?.post?.result;
if (!post?._id) {
throw new EmptyResultError('lesswrong comments', `Post "${postId}" not found`);
}
const comments = (commentsData?.comments?.results ?? []) as Array<Record<string, unknown>>;
const rows: Array<Record<string, unknown>> = [];
// First row: post context
rows.push({
rank: '',
score: '',
author: '',
text: `Comments on: ${post.title ?? 'Untitled'} (https://${DOMAIN}/posts/${post._id}/${post.slug})`,
});
for (let i = 0; i < comments.length; i++) {
const item = comments[i];
const user = item.user as { displayName?: string } | undefined;
const raw = stripHtml((item.htmlBody as string) ?? '');
rows.push({
rank: i + 1,
score: (item.baseScore as number) ?? 0,
author: user?.displayName ?? 'Unknown',
text: raw.length > 500 ? `${raw.slice(0, 500)}...` : raw,
});
}
return rows;
},
});