Skip to content

Commit b18c9e9

Browse files
Miriadresearch
andcommitted
fix: address PR review — dynamic year, typed YouTube response, nullable parseJsonOutput
- Replace hardcoded "2025" in YouTube queries with dynamic year - Add YouTubeSearchItem/YouTubeSearchResponse/YouTubeVideoStats interfaces - Change parseJsonOutput to return T | null instead of {} as T - Update all callers to use optional chaining for null safety Co-authored-by: research <research@miriad.systems>
1 parent 09c5034 commit b18c9e9

2 files changed

Lines changed: 33 additions & 17 deletions

File tree

lib/services/research.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ async function runNotebookLM(
167167
}
168168
}
169169

170-
/** Parse JSON from CLI stdout, with a fallback for non-JSON output. */
171-
function parseJsonOutput<T = unknown>(stdout: string): T {
170+
/** Parse JSON from CLI stdout, with a fallback for non-JSON output. Returns null for empty input. */
171+
function parseJsonOutput<T = unknown>(stdout: string): T | null {
172172
const trimmed = stdout.trim();
173173
if (!trimmed) {
174-
return {} as T;
174+
return null;
175175
}
176176
try {
177177
return JSON.parse(trimmed) as T;
@@ -530,7 +530,7 @@ export async function conductResearch(
530530
const createResult = parseJsonOutput<{ id?: string; notebook_id?: string }>(
531531
createOutput
532532
);
533-
const notebookId = createResult.id || createResult.notebook_id || "";
533+
const notebookId = createResult?.id || createResult?.notebook_id || "";
534534

535535
if (!notebookId) {
536536
throw new Error(
@@ -625,7 +625,7 @@ export async function conductResearch(
625625

626626
// Parse artifact results
627627
const mindMap = mindMapResult
628-
? (parseJsonOutput<{ content?: string }>(mindMapResult).content ??
628+
? (parseJsonOutput<{ content?: string }>(mindMapResult)?.content ??
629629
mindMapResult.trim())
630630
: undefined;
631631

@@ -635,7 +635,7 @@ export async function conductResearch(
635635
const briefing = briefingParsed?.content ?? briefingResult?.trim() ?? "";
636636

637637
const dataTable = dataTableResult
638-
? (parseJsonOutput<{ content?: string }>(dataTableResult).content ??
638+
? (parseJsonOutput<{ content?: string }>(dataTableResult)?.content ??
639639
dataTableResult.trim())
640640
: undefined;
641641

@@ -665,7 +665,7 @@ export async function conductResearch(
665665
text?: string;
666666
}>(result);
667667
answers[key] =
668-
parsed.answer || parsed.response || parsed.text || result.trim();
668+
parsed?.answer || parsed?.response || parsed?.text || result.trim();
669669
}
670670
}
671671

lib/services/trend-discovery.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,28 @@ function parseFeedItems(xml: string): FeedItem[] {
427427
// Source 1: Hacker News
428428
// ---------------------------------------------------------------------------
429429

430+
/** YouTube Data API v3 search response shape. */
431+
interface YouTubeSearchItem {
432+
id: { videoId: string };
433+
snippet: {
434+
title: string;
435+
publishedAt: string;
436+
channelTitle: string;
437+
};
438+
}
439+
440+
interface YouTubeSearchResponse {
441+
items?: YouTubeSearchItem[];
442+
}
443+
444+
/** YouTube Data API v3 video statistics response shape. */
445+
interface YouTubeVideoStats {
446+
items?: Array<{
447+
id: string;
448+
statistics: { viewCount?: string };
449+
}>;
450+
}
451+
430452
interface HNItem {
431453
id: number;
432454
title?: string;
@@ -654,7 +676,8 @@ async function fetchYouTube(
654676
Date.now() - lookbackDays * 86400 * 1000,
655677
).toISOString();
656678

657-
const queries = ["web development 2025", "nextjs react tutorial", "AI coding tools"];
679+
const currentYear = new Date().getFullYear();
680+
const queries = [`web development ${currentYear}`, "nextjs react tutorial", "AI coding tools"];
658681
const results = await Promise.allSettled(
659682
queries.map(async (q) => {
660683
const params = new URLSearchParams({
@@ -673,15 +696,8 @@ async function fetchYouTube(
673696
console.warn(`${LOG_PREFIX} YouTube search failed: ${res.status}`);
674697
return [];
675698
}
676-
const data = await res.json();
677-
return (data.items ?? []) as Array<{
678-
id: { videoId: string };
679-
snippet: {
680-
title: string;
681-
publishedAt: string;
682-
channelTitle: string;
683-
};
684-
}>;
699+
const data: YouTubeSearchResponse = await res.json();
700+
return (data.items ?? []);
685701
}),
686702
);
687703

0 commit comments

Comments
 (0)