@deepagents/toolbox 6.1.0 → 7.0.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.
@@ -0,0 +1,744 @@
1
+ // packages/toolbox/src/lib/hackernews-search.ts
2
+ import { tool } from "ai";
3
+ import { z } from "zod";
4
+
5
+ // packages/toolbox/src/lib/state.ts
6
+ function toState(options) {
7
+ return options.context;
8
+ }
9
+
10
+ // packages/toolbox/src/lib/hackernews-search.ts
11
+ function buildTags(options) {
12
+ const tags = [];
13
+ if (options.type && options.type !== "all") {
14
+ tags.push(options.type);
15
+ }
16
+ if (options.author) {
17
+ tags.push(`author_${options.author}`);
18
+ }
19
+ return tags.length > 0 ? tags.join(",") : void 0;
20
+ }
21
+ function buildNumericFilters(options) {
22
+ const filters = [];
23
+ if (options.timeFilter && options.timeFilter !== "all") {
24
+ const now = Math.floor(Date.now() / 1e3);
25
+ const timeMap = {
26
+ d: 86400,
27
+ // 1 day in seconds
28
+ w: 604800,
29
+ // 1 week
30
+ m: 2592e3,
31
+ // ~30 days
32
+ y: 31536e3
33
+ // ~365 days
34
+ };
35
+ const seconds = timeMap[options.timeFilter];
36
+ const timestamp = now - seconds;
37
+ filters.push(`created_at_i>${timestamp}`);
38
+ }
39
+ if (options.maxAgeHours !== void 0 && options.maxAgeHours > 0) {
40
+ const now = Math.floor(Date.now() / 1e3);
41
+ const seconds = options.maxAgeHours * 3600;
42
+ const timestamp = now - seconds;
43
+ filters.push(`created_at_i>${timestamp}`);
44
+ }
45
+ if (options.minPoints !== void 0 && options.minPoints > 0) {
46
+ filters.push(`points>=${options.minPoints}`);
47
+ }
48
+ if (options.minComments !== void 0 && options.minComments > 0) {
49
+ filters.push(`num_comments>=${options.minComments}`);
50
+ }
51
+ return filters.length > 0 ? filters.join(",") : void 0;
52
+ }
53
+ async function searchHackerNewsAPI(params) {
54
+ const {
55
+ query = "",
56
+ tags,
57
+ numericFilters,
58
+ sortBy = "relevance",
59
+ page = 0,
60
+ hitsPerPage = 20
61
+ } = params;
62
+ const endpoint = sortBy === "date" ? "http://hn.algolia.com/api/v1/search_by_date" : "http://hn.algolia.com/api/v1/search";
63
+ const urlParams = new URLSearchParams();
64
+ if (query) {
65
+ urlParams.set("query", query);
66
+ }
67
+ if (tags) {
68
+ urlParams.set("tags", tags);
69
+ }
70
+ if (numericFilters) {
71
+ urlParams.set("numericFilters", numericFilters);
72
+ }
73
+ urlParams.set("page", page.toString());
74
+ urlParams.set("hitsPerPage", Math.min(hitsPerPage, 1e3).toString());
75
+ const url = `${endpoint}?${urlParams.toString()}`;
76
+ try {
77
+ const response = await fetch(url, {
78
+ headers: {
79
+ "Content-Type": "application/json"
80
+ }
81
+ });
82
+ if (!response.ok) {
83
+ throw new Error(
84
+ `HN API error: ${response.status} ${response.statusText}`
85
+ );
86
+ }
87
+ const data = await response.json();
88
+ return data;
89
+ } catch (error) {
90
+ throw new Error(
91
+ `Failed to search HackerNews: ${error instanceof Error ? error.message : "Unknown error"}`
92
+ );
93
+ }
94
+ }
95
+ async function fetchHNItem(id) {
96
+ const url = `http://hn.algolia.com/api/v1/items/${id}`;
97
+ try {
98
+ const response = await fetch(url, {
99
+ headers: {
100
+ "Content-Type": "application/json"
101
+ }
102
+ });
103
+ if (!response.ok) {
104
+ throw new Error(
105
+ `HN API error: ${response.status} ${response.statusText}`
106
+ );
107
+ }
108
+ const data = await response.json();
109
+ return data;
110
+ } catch (error) {
111
+ throw new Error(
112
+ `Failed to fetch HN item: ${error instanceof Error ? error.message : "Unknown error"}`
113
+ );
114
+ }
115
+ }
116
+ async function fetchHNUser(username) {
117
+ const url = `http://hn.algolia.com/api/v1/users/${username}`;
118
+ try {
119
+ const response = await fetch(url, {
120
+ headers: {
121
+ "Content-Type": "application/json"
122
+ }
123
+ });
124
+ if (!response.ok) {
125
+ throw new Error(
126
+ `HN API error: ${response.status} ${response.statusText}`
127
+ );
128
+ }
129
+ const data = await response.json();
130
+ return data;
131
+ } catch (error) {
132
+ throw new Error(
133
+ `Failed to fetch HN user: ${error instanceof Error ? error.message : "Unknown error"}`
134
+ );
135
+ }
136
+ }
137
+ function formatDate(timestamp) {
138
+ return new Date(timestamp * 1e3).toLocaleString();
139
+ }
140
+ function formatHNLink(id) {
141
+ return `https://news.ycombinator.com/item?id=${id}`;
142
+ }
143
+ function truncateText(text, maxLength) {
144
+ if (text.length <= maxLength) return text;
145
+ return `${text.substring(0, maxLength)}...`;
146
+ }
147
+ function formatStoryItem(hit, index, page, hitsPerPage) {
148
+ const lines = [];
149
+ const itemNumber = page * hitsPerPage + index + 1;
150
+ lines.push(`
151
+ ${itemNumber}. ${hit.title || "(No title)"}`);
152
+ if (hit.url) {
153
+ lines.push(` URL: ${hit.url}`);
154
+ }
155
+ if (hit.points !== null) {
156
+ lines.push(
157
+ ` ${hit.points} points | by ${hit.author} | ${hit.num_comments || 0} comments`
158
+ );
159
+ } else {
160
+ lines.push(` by ${hit.author}`);
161
+ }
162
+ lines.push(` Date: ${formatDate(hit.created_at_i)}`);
163
+ if (hit.story_text) {
164
+ lines.push(` Text: ${truncateText(hit.story_text, 200)}`);
165
+ }
166
+ lines.push(` HN Link: ${formatHNLink(hit.objectID)}`);
167
+ return lines.join("\n");
168
+ }
169
+ function formatCommentItem(hit, index, page, hitsPerPage) {
170
+ const lines = [];
171
+ const itemNumber = page * hitsPerPage + index + 1;
172
+ lines.push(`
173
+ ${itemNumber}. Comment by ${hit.author}`);
174
+ if (hit.comment_text) {
175
+ lines.push(` ${truncateText(hit.comment_text, 300)}`);
176
+ }
177
+ lines.push(` Date: ${formatDate(hit.created_at_i)}`);
178
+ lines.push(` HN Link: ${formatHNLink(hit.objectID)}`);
179
+ return lines.join("\n");
180
+ }
181
+ function formatMixedItem(hit, index, page, hitsPerPage) {
182
+ const lines = [];
183
+ const itemNumber = page * hitsPerPage + index + 1;
184
+ const isStory = hit._tags.includes("story");
185
+ const isComment = hit._tags.includes("comment");
186
+ const type = isStory ? "Story" : "Comment";
187
+ lines.push(`
188
+ ${itemNumber}. [${type}] ${hit.title || "Comment"}`);
189
+ if (isStory) {
190
+ if (hit.url) {
191
+ lines.push(` URL: ${hit.url}`);
192
+ }
193
+ if (hit.points !== null) {
194
+ lines.push(` ${hit.points} points | ${hit.num_comments || 0} comments`);
195
+ }
196
+ if (hit.story_text) {
197
+ lines.push(` Text: ${truncateText(hit.story_text, 200)}`);
198
+ }
199
+ } else if (isComment && hit.comment_text) {
200
+ lines.push(` ${truncateText(hit.comment_text, 200)}`);
201
+ }
202
+ lines.push(` Date: ${formatDate(hit.created_at_i)}`);
203
+ lines.push(` HN Link: ${formatHNLink(hit.objectID)}`);
204
+ return lines.join("\n");
205
+ }
206
+ function formatSearchResults(response, options) {
207
+ const { hits, nbHits, page, nbPages } = response;
208
+ const { itemType, emptyMessage } = options;
209
+ if (hits.length === 0) {
210
+ return emptyMessage;
211
+ }
212
+ const formatItem = (hit, index) => {
213
+ switch (itemType) {
214
+ case "story":
215
+ return formatStoryItem(hit, index, page, response.hitsPerPage);
216
+ case "comment":
217
+ return formatCommentItem(hit, index, page, response.hitsPerPage);
218
+ case "mixed":
219
+ return formatMixedItem(hit, index, page, response.hitsPerPage);
220
+ }
221
+ };
222
+ const results = hits.map(formatItem);
223
+ const typeLabel = itemType === "mixed" ? "results" : `${itemType === "story" ? "stories" : "comments"}`;
224
+ const header = `Found ${nbHits} ${typeLabel} (showing page ${page + 1} of ${nbPages}):
225
+ `;
226
+ return header + results.join("\n");
227
+ }
228
+ function formatStoryResults(response) {
229
+ return formatSearchResults(response, {
230
+ itemType: "story",
231
+ emptyMessage: `No stories found for query: "${response.query}"`
232
+ });
233
+ }
234
+ function formatCommentResults(response) {
235
+ return formatSearchResults(response, {
236
+ itemType: "comment",
237
+ emptyMessage: `No comments found for query: "${response.query}"`
238
+ });
239
+ }
240
+ function formatAuthorResults(response) {
241
+ return formatSearchResults(response, {
242
+ itemType: "mixed",
243
+ emptyMessage: "No results found for this author"
244
+ });
245
+ }
246
+ function formatItemDetails(item) {
247
+ const lines = [];
248
+ if (item.type === "story") {
249
+ lines.push(`Story: ${item.title || "(No title)"}`);
250
+ if (item.url) {
251
+ lines.push(`URL: ${item.url}`);
252
+ }
253
+ lines.push(`By: ${item.author}`);
254
+ if (item.points !== void 0) {
255
+ lines.push(`Points: ${item.points}`);
256
+ }
257
+ lines.push(`Date: ${formatDate(item.created_at_i)}`);
258
+ if (item.text) {
259
+ lines.push(`
260
+ Text:
261
+ ${item.text}`);
262
+ }
263
+ lines.push(`
264
+ HN Link: ${formatHNLink(item.id)}`);
265
+ } else if (item.type === "comment") {
266
+ lines.push(`Comment by ${item.author}`);
267
+ if (item.story_title) {
268
+ lines.push(`On story: ${item.story_title}`);
269
+ }
270
+ lines.push(`Date: ${formatDate(item.created_at_i)}`);
271
+ if (item.text) {
272
+ lines.push(`
273
+ ${item.text}`);
274
+ }
275
+ lines.push(`
276
+ HN Link: ${formatHNLink(item.id)}`);
277
+ }
278
+ return lines.join("\n");
279
+ }
280
+ function formatUserProfile(user) {
281
+ const lines = [];
282
+ lines.push(`User: ${user.username}`);
283
+ lines.push(`Karma: ${user.karma.toLocaleString()}`);
284
+ lines.push(`Member since: ${formatDate(user.created_at_i)}`);
285
+ if (user.about) {
286
+ lines.push(`
287
+ About:
288
+ ${user.about}`);
289
+ }
290
+ lines.push(
291
+ `
292
+ Profile: https://news.ycombinator.com/user?id=${user.username}`
293
+ );
294
+ return lines.join("\n");
295
+ }
296
+ function formatJobItem(hit, index, page, hitsPerPage) {
297
+ const lines = [];
298
+ const itemNumber = page * hitsPerPage + index + 1;
299
+ lines.push(`
300
+ ${itemNumber}. ${hit.title || "(No title)"}`);
301
+ if (hit.url) {
302
+ lines.push(` URL: ${hit.url}`);
303
+ }
304
+ lines.push(` Posted by: ${hit.author}`);
305
+ lines.push(` Date: ${formatDate(hit.created_at_i)}`);
306
+ if (hit.story_text) {
307
+ lines.push(` Description: ${truncateText(hit.story_text, 300)}`);
308
+ }
309
+ lines.push(` HN Link: ${formatHNLink(hit.objectID)}`);
310
+ return lines.join("\n");
311
+ }
312
+ function formatJobResults(response) {
313
+ const { hits, nbHits, page, nbPages } = response;
314
+ if (hits.length === 0) {
315
+ return `No job postings found for query: "${response.query}"`;
316
+ }
317
+ const results = hits.map(
318
+ (hit, index) => formatJobItem(hit, index, page, response.hitsPerPage)
319
+ );
320
+ const header = `Found ${nbHits} job postings (showing page ${page + 1} of ${nbPages}):
321
+ `;
322
+ return header + results.join("\n");
323
+ }
324
+ var search_by_query = tool({
325
+ description: "Tool to search HackerNews stories by keywords and filters. Use when you need to find HN stories matching specific search criteria, time periods, or popularity thresholds.",
326
+ inputSchema: z.object({
327
+ query: z.string().describe(
328
+ 'Search query for story titles and content. Examples: "artificial intelligence", "python"'
329
+ ),
330
+ timeFilter: z.enum(["d", "w", "m", "y", "all"]).default("all").describe("Time filter: d=day, w=week, m=month, y=year, all=no filter"),
331
+ minPoints: z.number().int().min(0).optional().describe("Minimum points threshold"),
332
+ minComments: z.number().int().min(0).optional().describe("Minimum comments threshold"),
333
+ sortBy: z.enum(["relevance", "date"]).default("relevance").describe("Sort results by relevance or date"),
334
+ page: z.number().int().min(0).default(0).describe("Page number (0-indexed)"),
335
+ hitsPerPage: z.number().int().min(1).max(1e3).default(20).describe("Results per page (max 1000)")
336
+ }),
337
+ execute: async (input, options) => {
338
+ const context = toState(options);
339
+ context.hackernews_sources ??= [];
340
+ const numericFilters = buildNumericFilters({
341
+ timeFilter: input.timeFilter,
342
+ minPoints: input.minPoints,
343
+ minComments: input.minComments
344
+ });
345
+ const response = await searchHackerNewsAPI({
346
+ query: input.query,
347
+ tags: "story",
348
+ numericFilters,
349
+ sortBy: input.sortBy,
350
+ page: input.page,
351
+ hitsPerPage: input.hitsPerPage
352
+ });
353
+ fillContext(context, response);
354
+ return formatStoryResults(response);
355
+ }
356
+ });
357
+ var search_by_author = tool({
358
+ description: "Tool to search HackerNews content by author username. Use when you need to find all stories, comments, or both by a specific HN user.",
359
+ inputSchema: z.object({
360
+ author: z.string().describe('HackerNews username to search for. Examples: "pg", "tptacek"'),
361
+ type: z.enum(["story", "comment", "all"]).default("all").describe("Type of content: story, comment, or all"),
362
+ timeFilter: z.enum(["d", "w", "m", "y", "all"]).default("all").describe("Time filter: d=day, w=week, m=month, y=year, all=no filter"),
363
+ minComments: z.number().int().min(0).optional().describe("Minimum comments threshold (for stories only)"),
364
+ sortBy: z.enum(["relevance", "date"]).default("date").describe("Sort results by relevance or date"),
365
+ page: z.number().int().min(0).default(0).describe("Page number (0-indexed)"),
366
+ hitsPerPage: z.number().int().min(1).max(1e3).default(20).describe("Results per page (max 1000)")
367
+ }),
368
+ execute: async (input, options) => {
369
+ const context = toState(options);
370
+ context.hackernews_sources ??= [];
371
+ const tags = buildTags({ type: input.type, author: input.author });
372
+ const numericFilters = buildNumericFilters({
373
+ timeFilter: input.timeFilter,
374
+ minComments: input.minComments
375
+ });
376
+ const response = await searchHackerNewsAPI({
377
+ tags,
378
+ numericFilters,
379
+ sortBy: input.sortBy,
380
+ page: input.page,
381
+ hitsPerPage: input.hitsPerPage
382
+ });
383
+ fillContext(context, response);
384
+ return formatAuthorResults(response);
385
+ }
386
+ });
387
+ var get_story_item = tool({
388
+ description: "Tool to get detailed information about a specific HackerNews story by ID. Use when you need full details about a particular HN story including title, URL, author, points, and comments.",
389
+ inputSchema: z.object({
390
+ storyId: z.string().describe('HackerNews story ID. Example: "38709478"')
391
+ }),
392
+ execute: async (input, options) => {
393
+ const context = toState(options);
394
+ context.hackernews_sources ??= [];
395
+ const item = await fetchHNItem(input.storyId);
396
+ if (item.type !== "story") {
397
+ throw new Error(
398
+ `Item ${input.storyId} is not a story (type: ${item.type})`
399
+ );
400
+ }
401
+ context.hackernews_sources.push({
402
+ title: item.title || "Untitled",
403
+ hn_url: `https://news.ycombinator.com/item?id=${item.id}`,
404
+ story_url: item.url || ""
405
+ });
406
+ return formatItemDetails(item);
407
+ }
408
+ });
409
+ var get_story_comment = tool({
410
+ description: "Tool to get detailed information about a specific HackerNews comment by ID. Use when you need full details about a particular comment including text, author, and parent story information.",
411
+ inputSchema: z.object({
412
+ commentId: z.string().describe('HackerNews comment ID. Example: "38710123"')
413
+ }),
414
+ execute: async (input) => {
415
+ const item = await fetchHNItem(input.commentId);
416
+ if (item.type !== "comment") {
417
+ throw new Error(
418
+ `Item ${input.commentId} is not a comment (type: ${item.type})`
419
+ );
420
+ }
421
+ return formatItemDetails(item);
422
+ }
423
+ });
424
+ var get_front_page_stories = tool({
425
+ description: "Tool to get current HackerNews front page stories. Use when you need to see the latest trending and popular stories on HN.",
426
+ inputSchema: z.object({
427
+ page: z.number().int().min(0).default(0).describe("Page number (0-indexed)"),
428
+ hitsPerPage: z.number().int().min(1).max(50).default(30).describe("Results per page (max 50)")
429
+ }),
430
+ execute: async (input, options) => {
431
+ const context = toState(options);
432
+ context.hackernews_sources ??= [];
433
+ const response = await searchHackerNewsAPI({
434
+ tags: "front_page",
435
+ sortBy: "date",
436
+ page: input.page,
437
+ hitsPerPage: input.hitsPerPage
438
+ });
439
+ fillContext(context, response);
440
+ return formatStoryResults(response);
441
+ }
442
+ });
443
+ var get_story_comments = tool({
444
+ description: "Tool to get all comments for a specific HackerNews story. Use when you need to read the discussion and comments on a particular HN story.",
445
+ inputSchema: z.object({
446
+ storyId: z.string().describe('HackerNews story ID. Example: "38709478"'),
447
+ sortBy: z.enum(["relevance", "date"]).default("date").describe("Sort comments by relevance or date"),
448
+ page: z.number().int().min(0).default(0).describe("Page number (0-indexed)"),
449
+ hitsPerPage: z.number().int().min(1).max(1e3).default(50).describe("Results per page (max 1000)")
450
+ }),
451
+ execute: async (input) => {
452
+ const response = await searchHackerNewsAPI({
453
+ tags: `comment,story_${input.storyId}`,
454
+ sortBy: input.sortBy,
455
+ page: input.page,
456
+ hitsPerPage: input.hitsPerPage
457
+ });
458
+ return formatCommentResults(response);
459
+ }
460
+ });
461
+ var search_ask_hn = tool({
462
+ description: "Tool to search Ask HN posts - questions posed to the HackerNews community. Use when you need to find community questions and discussions on specific topics.",
463
+ inputSchema: z.object({
464
+ query: z.string().default("").describe(
465
+ 'Search query for Ask HN posts. Examples: "artificial intelligence", "career"'
466
+ ),
467
+ timeFilter: z.enum(["d", "w", "m", "y", "all"]).default("all").describe("Time filter: d=day, w=week, m=month, y=year, all=no filter"),
468
+ minPoints: z.number().int().min(0).optional().describe("Minimum points threshold"),
469
+ minComments: z.number().int().min(0).optional().describe("Minimum comments threshold"),
470
+ sortBy: z.enum(["relevance", "date"]).default("relevance").describe("Sort by relevance or date"),
471
+ page: z.number().int().min(0).default(0).describe("Page number (0-indexed)"),
472
+ hitsPerPage: z.number().int().min(1).max(1e3).default(20).describe("Results per page (max 1000)")
473
+ }),
474
+ execute: async (input, options) => {
475
+ const context = toState(options);
476
+ context.hackernews_sources ??= [];
477
+ const numericFilters = buildNumericFilters({
478
+ timeFilter: input.timeFilter,
479
+ minPoints: input.minPoints,
480
+ minComments: input.minComments
481
+ });
482
+ const response = await searchHackerNewsAPI({
483
+ query: input.query,
484
+ tags: "ask_hn",
485
+ numericFilters,
486
+ sortBy: input.sortBy,
487
+ page: input.page,
488
+ hitsPerPage: input.hitsPerPage
489
+ });
490
+ fillContext(context, response);
491
+ return formatStoryResults(response);
492
+ }
493
+ });
494
+ var search_show_hn = tool({
495
+ description: "Tool to search Show HN posts - projects and products shared with the HackerNews community. Use when you need to discover community projects, demos, or products on specific topics.",
496
+ inputSchema: z.object({
497
+ query: z.string().default("").describe(
498
+ 'Search query for Show HN posts. Examples: "web app", "open source"'
499
+ ),
500
+ timeFilter: z.enum(["d", "w", "m", "y", "all"]).default("all").describe("Time filter: d=day, w=week, m=month, y=year, all=no filter"),
501
+ minPoints: z.number().int().min(0).optional().describe("Minimum points threshold"),
502
+ minComments: z.number().int().min(0).optional().describe("Minimum comments threshold"),
503
+ sortBy: z.enum(["relevance", "date"]).default("relevance").describe("Sort by relevance or date"),
504
+ page: z.number().int().min(0).default(0).describe("Page number (0-indexed)"),
505
+ hitsPerPage: z.number().int().min(1).max(1e3).default(20).describe("Results per page (max 1000)")
506
+ }),
507
+ execute: async (input, options) => {
508
+ const context = toState(options);
509
+ context.hackernews_sources ??= [];
510
+ const numericFilters = buildNumericFilters({
511
+ timeFilter: input.timeFilter,
512
+ minPoints: input.minPoints,
513
+ minComments: input.minComments
514
+ });
515
+ const response = await searchHackerNewsAPI({
516
+ query: input.query,
517
+ tags: "show_hn",
518
+ numericFilters,
519
+ sortBy: input.sortBy,
520
+ page: input.page,
521
+ hitsPerPage: input.hitsPerPage
522
+ });
523
+ fillContext(context, response);
524
+ return formatStoryResults(response);
525
+ }
526
+ });
527
+ var search_jobs = tool({
528
+ description: "Tool to search HackerNews job postings. Use when you need to find tech job opportunities posted on HN.",
529
+ inputSchema: z.object({
530
+ query: z.string().default("").describe(
531
+ 'Search query for job postings. Examples: "remote", "machine learning"'
532
+ ),
533
+ timeFilter: z.enum(["d", "w", "m", "y", "all"]).default("all").describe("Time filter: d=day, w=week, m=month, y=year, all=no filter"),
534
+ sortBy: z.enum(["relevance", "date"]).default("date").describe("Sort by relevance or date"),
535
+ page: z.number().int().min(0).default(0).describe("Page number (0-indexed)"),
536
+ hitsPerPage: z.number().int().min(1).max(1e3).default(20).describe("Results per page (max 1000)")
537
+ }),
538
+ execute: async (input, options) => {
539
+ const context = toState(options);
540
+ context.hackernews_sources ??= [];
541
+ const numericFilters = buildNumericFilters({
542
+ timeFilter: input.timeFilter
543
+ });
544
+ const response = await searchHackerNewsAPI({
545
+ query: input.query,
546
+ tags: "job",
547
+ numericFilters,
548
+ sortBy: input.sortBy,
549
+ page: input.page,
550
+ hitsPerPage: input.hitsPerPage
551
+ });
552
+ fillContext(context, response);
553
+ return formatJobResults(response);
554
+ }
555
+ });
556
+ var search_polls = tool({
557
+ description: "Tool to search HackerNews polls - community surveys and voting. Use when you need to find community polls on various topics.",
558
+ inputSchema: z.object({
559
+ query: z.string().default("").describe('Search query for polls. Example: "programming language"'),
560
+ timeFilter: z.enum(["d", "w", "m", "y", "all"]).default("all").describe("Time filter: d=day, w=week, m=month, y=year, all=no filter"),
561
+ sortBy: z.enum(["relevance", "date"]).default("date").describe("Sort by relevance or date"),
562
+ page: z.number().int().min(0).default(0).describe("Page number (0-indexed)"),
563
+ hitsPerPage: z.number().int().min(1).max(1e3).default(20).describe("Results per page (max 1000)")
564
+ }),
565
+ execute: async (input, options) => {
566
+ const context = toState(options);
567
+ context.hackernews_sources ??= [];
568
+ const numericFilters = buildNumericFilters({
569
+ timeFilter: input.timeFilter
570
+ });
571
+ const response = await searchHackerNewsAPI({
572
+ query: input.query,
573
+ tags: "poll",
574
+ numericFilters,
575
+ sortBy: input.sortBy,
576
+ page: input.page,
577
+ hitsPerPage: input.hitsPerPage
578
+ });
579
+ fillContext(context, response);
580
+ return formatStoryResults(response);
581
+ }
582
+ });
583
+ var get_user_profile = tool({
584
+ description: "Tool to get HackerNews user profile information. Use when you need to view a user's karma, account creation date, and bio.",
585
+ inputSchema: z.object({
586
+ username: z.string().describe('HackerNews username. Example: "pg"')
587
+ }),
588
+ execute: async (input) => {
589
+ const user = await fetchHNUser(input.username);
590
+ return formatUserProfile(user);
591
+ }
592
+ });
593
+ var search_by_domain = tool({
594
+ description: "Tool to search HackerNews stories from a specific domain or website. Use when you need to find all HN posts from a particular domain or track discussions about content from specific websites.",
595
+ inputSchema: z.object({
596
+ domain: z.string().describe('Domain to search. Examples: "github.com", "arxiv.org"'),
597
+ timeFilter: z.enum(["d", "w", "m", "y", "all"]).default("all").describe("Time filter: d=day, w=week, m=month, y=year, all=no filter"),
598
+ minPoints: z.number().int().min(0).optional().describe("Minimum points threshold"),
599
+ minComments: z.number().int().min(0).optional().describe("Minimum comments threshold"),
600
+ sortBy: z.enum(["relevance", "date"]).default("date").describe("Sort by relevance or date"),
601
+ page: z.number().int().min(0).default(0).describe("Page number (0-indexed)"),
602
+ hitsPerPage: z.number().int().min(1).max(1e3).default(20).describe("Results per page (max 1000)")
603
+ }),
604
+ execute: async (input, options) => {
605
+ const context = toState(options);
606
+ context.hackernews_sources ??= [];
607
+ const numericFilters = buildNumericFilters({
608
+ timeFilter: input.timeFilter,
609
+ minPoints: input.minPoints,
610
+ minComments: input.minComments
611
+ });
612
+ const urlParams = new URLSearchParams();
613
+ urlParams.set("query", input.domain);
614
+ urlParams.set("restrictSearchableAttributes", "url");
615
+ urlParams.set("tags", "story");
616
+ if (numericFilters) {
617
+ urlParams.set("numericFilters", numericFilters);
618
+ }
619
+ urlParams.set("page", input.page.toString());
620
+ urlParams.set("hitsPerPage", input.hitsPerPage.toString());
621
+ const endpoint = input.sortBy === "date" ? "http://hn.algolia.com/api/v1/search_by_date" : "http://hn.algolia.com/api/v1/search";
622
+ const url = `${endpoint}?${urlParams.toString()}`;
623
+ const response = await fetch(url, {
624
+ headers: { "Content-Type": "application/json" }
625
+ });
626
+ if (!response.ok) {
627
+ throw new Error(
628
+ `HN API error: ${response.status} ${response.statusText}`
629
+ );
630
+ }
631
+ const data = await response.json();
632
+ fillContext(context, data);
633
+ return formatStoryResults(data);
634
+ }
635
+ });
636
+ var search_highly_discussed = tool({
637
+ description: "Tool to search for highly discussed HackerNews stories with many comments. Use when you need to find engaging or controversial discussions with significant community participation.",
638
+ inputSchema: z.object({
639
+ minComments: z.number().int().min(1).describe(
640
+ "Minimum number of comments (required). Example: 100 for highly discussed stories"
641
+ ),
642
+ query: z.string().default("").describe('Optional search query. Example: "AI"'),
643
+ timeFilter: z.enum(["d", "w", "m", "y", "all"]).default("all").describe("Time filter: d=day, w=week, m=month, y=year, all=no filter"),
644
+ minPoints: z.number().int().min(0).optional().describe("Minimum points threshold"),
645
+ sortBy: z.enum(["relevance", "date"]).default("date").describe("Sort by relevance or date"),
646
+ page: z.number().int().min(0).default(0).describe("Page number (0-indexed)"),
647
+ hitsPerPage: z.number().int().min(1).max(1e3).default(20).describe("Results per page (max 1000)")
648
+ }),
649
+ execute: async (input, options) => {
650
+ const context = toState(options);
651
+ context.hackernews_sources ??= [];
652
+ const numericFilters = buildNumericFilters({
653
+ timeFilter: input.timeFilter,
654
+ minPoints: input.minPoints,
655
+ minComments: input.minComments
656
+ });
657
+ const response = await searchHackerNewsAPI({
658
+ query: input.query,
659
+ tags: "story",
660
+ numericFilters,
661
+ sortBy: input.sortBy,
662
+ page: input.page,
663
+ hitsPerPage: input.hitsPerPage
664
+ });
665
+ fillContext(context, response);
666
+ return formatStoryResults(response);
667
+ }
668
+ });
669
+ var search_trending = tool({
670
+ description: "Tool to find currently trending HackerNews stories - recent posts with high engagement. Use when you need to discover what's hot right now on HN by combining recency with high points and comments.",
671
+ inputSchema: z.object({
672
+ minPoints: z.number().int().min(10).default(50).describe(
673
+ "Minimum points threshold. Example: 100 for highly upvoted stories"
674
+ ),
675
+ maxAgeHours: z.number().int().min(1).max(72).default(24).describe("Maximum age in hours. Example: 24 for stories from today"),
676
+ minComments: z.number().int().min(0).default(10).describe("Minimum comments threshold"),
677
+ sortBy: z.enum(["relevance", "date"]).default("date").describe("Sort by relevance or date"),
678
+ page: z.number().int().min(0).default(0).describe("Page number (0-indexed)"),
679
+ hitsPerPage: z.number().int().min(1).max(100).default(30).describe("Results per page (max 100)")
680
+ }),
681
+ execute: async (input, options) => {
682
+ const context = toState(options);
683
+ context.hackernews_sources ??= [];
684
+ const numericFilters = buildNumericFilters({
685
+ maxAgeHours: input.maxAgeHours,
686
+ minPoints: input.minPoints,
687
+ minComments: input.minComments
688
+ });
689
+ const response = await searchHackerNewsAPI({
690
+ tags: "story",
691
+ numericFilters,
692
+ sortBy: input.sortBy,
693
+ page: input.page,
694
+ hitsPerPage: input.hitsPerPage
695
+ });
696
+ fillContext(context, response);
697
+ return formatStoryResults(response);
698
+ }
699
+ });
700
+ function fillContext(context, result) {
701
+ result.hits.forEach((hit) => {
702
+ context.hackernews_sources.push({
703
+ title: hit.title,
704
+ hn_url: hit.objectID ? `https://news.ycombinator.com/item?id=${hit.objectID}` : void 0,
705
+ story_url: hit.url,
706
+ story_text: hit.story_text,
707
+ comment_text: hit.comment_text
708
+ });
709
+ });
710
+ }
711
+ var hackernewsTools = {
712
+ search_by_query,
713
+ search_by_author,
714
+ get_story_item,
715
+ get_story_comment,
716
+ get_front_page_stories,
717
+ get_story_comments,
718
+ search_ask_hn,
719
+ search_show_hn,
720
+ search_jobs,
721
+ search_polls,
722
+ get_user_profile,
723
+ search_by_domain,
724
+ search_highly_discussed,
725
+ search_trending
726
+ };
727
+ export {
728
+ get_front_page_stories,
729
+ get_story_comment,
730
+ get_story_comments,
731
+ get_story_item,
732
+ get_user_profile,
733
+ hackernewsTools,
734
+ search_ask_hn,
735
+ search_by_author,
736
+ search_by_domain,
737
+ search_by_query,
738
+ search_highly_discussed,
739
+ search_jobs,
740
+ search_polls,
741
+ search_show_hn,
742
+ search_trending
743
+ };
744
+ //# sourceMappingURL=hackernews-search.js.map