-
Notifications
You must be signed in to change notification settings - Fork 894
Expand file tree
/
Copy pathhackermind.js
More file actions
80 lines (73 loc) · 2.86 KB
/
Copy pathhackermind.js
File metadata and controls
80 lines (73 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* omnisearch hackermind — search Hacker News stories & comments for omnisearch.
*
* No login. Uses the public HN Algolia API, which indexes stories and
* comments. Good for researching what the tech/startup community is saying
* about a topic, product, or problem.
*/
import { cli, Strategy } from '@agentrhq/webcmd/registry';
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@agentrhq/webcmd/errors';
const API = 'https://hn.algolia.com/api/v1/search';
function requireQuery(value) {
const s = String(value ?? '').trim();
if (!s) throw new ArgumentError('a search query is required');
return s;
}
cli({
site: 'omnisearch',
name: 'hackermind',
tags: ['search'],
access: 'read',
description: "Search Hacker News stories & comments (no login)",
domain: 'hn.algolia.com',
strategy: Strategy.PUBLIC,
browser: false,
args: [
{ name: 'query', required: true, positional: true, help: 'Topic, product, or problem to research' },
{ name: 'limit', type: 'int', default: 20, help: 'Number of results' },
{
name: 'scope',
default: 'story',
help: 'What to search: story (headlines) or comment (reply text)',
choices: ['story', 'comment'],
},
],
columns: ['rank', 'id', 'objectType', 'title', 'author', 'score', 'commentCount', 'createdAt', 'url'],
func: async (kwargs) => {
const query = requireQuery(kwargs.query);
const raw = Number(kwargs.limit ?? 20);
if (!Number.isInteger(raw) || raw <= 0) {
throw new ArgumentError('limit must be a positive integer');
}
const limit = Math.min(raw, 100);
const scope = String(kwargs.scope ?? 'story');
const url = new URL(API);
url.searchParams.set('query', query);
url.searchParams.set('tags', scope === 'comment' ? 'comment' : 'story');
url.searchParams.set('hitsPerPage', String(limit));
let json;
try {
const res = await fetch(url);
if (!res.ok) throw new CommandExecutionError(`HN Algolia request failed: HTTP ${res.status}`);
json = await res.json();
} catch (err) {
if (err instanceof CommandExecutionError) throw err;
throw new CommandExecutionError(`HN Algolia request failed: ${err instanceof Error ? err.message : String(err)}`);
}
const hits = Array.isArray(json?.hits) ? json.hits : [];
if (!hits.length) {
throw new EmptyResultError('omnisearch/hackermind', `no results for "${query}"`);
}
return hits.slice(0, limit).map((h, index) => ({
rank: index + 1,
id: h.objectID,
objectType: scope,
title: String(h.title ?? h.story_title ?? h.comment_text ?? '').replace(/<[^>]+>/g, '').trim(),
author: String(h.author ?? ''),
score: h.points ?? 0,
commentCount: h.num_comments ?? 0,
createdAt: String(h.created_at ?? ''),
url: String(h.url ?? `https://news.ycombinator.com/item?id=${h.objectID}`),
}));
},
});