Repository navigation
Expand file tree
/
Copy pathgithub-podcast-tools.ts
More file actions
140 lines (120 loc) · 4.34 KB
/
Copy pathgithub-podcast-tools.ts
File metadata and controls
140 lines (120 loc) · 4.34 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { defineTool } from "@github/copilot-sdk";
import { Parser } from "htmlparser2";
import { z } from "zod";
const feedUrl = "https://feeds.simplecast.com/ioCY0vfY";
export type EpisodeBrief = {
episodeNumber: number | null;
title: string;
published: string;
duration: string;
description: string;
episodeUrl: string;
sourceFeedUrl: string;
};
export const episodeTool = defineTool("get_github_podcast_episode", {
description: "Gets a GitHub Podcast episode from the official RSS feed.",
parameters: z.object({
episodeTitle: z.string().optional().describe("Optional GitHub Podcast episode title. Omit for the latest episode."),
}),
handler: async ({ episodeTitle }) => getEpisode(episodeTitle),
});
export const latestEpisodesTool = defineTool("get_latest_github_podcast_episodes", {
description: "Gets the ten newest GitHub Podcast episodes from the official RSS feed.",
parameters: z.object({}),
handler: async () => getLatestEpisodes(),
});
export async function getLatestEpisodes(): Promise<EpisodeBrief[]> {
const items = await getItems();
return items.slice(0, 10).map(toEpisodeBrief);
}
export async function getEpisode(episodeTitle?: string): Promise<EpisodeBrief> {
const items = await getItems();
const item = !episodeTitle
? items[0]
: items.find((candidate) => value(candidate, "title").toLowerCase() === episodeTitle.toLowerCase());
if (!item) {
throw new Error(!episodeTitle
? "The GitHub Podcast RSS feed contained no episodes."
: `Episode "${episodeTitle}" was not found in the GitHub Podcast RSS feed.`);
}
return toEpisodeBrief(item);
}
export async function pickEpisode(episodes: EpisodeBrief[]): Promise<EpisodeBrief> {
if (episodes.length === 0) {
throw new Error("No GitHub Podcast episodes are available to select.");
}
console.log("\nChoose a GitHub Podcast episode:");
episodes.forEach((episode, index) => console.log(` ${index + 1}. ${episode.title}`));
const { readIndex } = await import("./model-selector.js");
return episodes[await readIndex("Episode [1]: ", episodes.length, 0)]!;
}
async function getItems(): Promise<string[]> {
const response = await fetch(feedUrl, { signal: AbortSignal.timeout(10_000) });
if (!response.ok) {
throw new Error(`Failed to fetch The GitHub Podcast RSS feed: ${response.status}`);
}
const xml = await response.text();
return [...xml.matchAll(/<item\b[\s\S]*?<\/item>/g)].map((match) => match[0]);
}
function toEpisodeBrief(item: string): EpisodeBrief {
return {
episodeNumber: getEpisodeNumber(item),
title: value(item, "title"),
published: value(item, "pubDate"),
duration: valueByLocalName(item, "duration") || "Unknown",
description: htmlToPlainText(value(item, "description")),
episodeUrl: value(item, "link"),
sourceFeedUrl: feedUrl,
};
}
function getEpisodeNumber(item: string): number | null {
const title = value(item, "title");
const separator = title.indexOf(":");
if (separator <= 0) {
return null;
}
const parsed = Number.parseInt(title.slice(0, separator), 10);
return Number.isInteger(parsed) ? parsed : null;
}
function value(item: string, name: string): string {
const match = item.match(new RegExp(`<${name}[^>]*>([\\s\\S]*?)<\\/${name}>`));
return decodeXml(match?.[1]?.trim() ?? "");
}
function valueByLocalName(item: string, localName: string): string {
const match = item.match(new RegExp(`<(?:[^:>]+:)?${localName}[^>]*>([\\s\\S]*?)<\\/(?:[^:>]+:)?${localName}>`));
return decodeXml(match?.[1]?.trim() ?? "");
}
function htmlToPlainText(value: string): string {
const text: string[] = [];
let ignoredDepth = 0;
const parser = new Parser({
onopentag(name) {
if (ignoredDepth > 0 || name === "script" || name === "style") {
ignoredDepth++;
}
},
onclosetag() {
if (ignoredDepth > 0) {
ignoredDepth--;
}
},
ontext(value) {
if (ignoredDepth === 0) {
text.push(value);
}
},
}, { decodeEntities: true });
parser.end(value);
return text.join("").trim();
}
function decodeXml(value: string): string {
// XML decoding preserves CDATA; description HTML is parsed in its own context.
const text: string[] = [];
const parser = new Parser({
ontext(value) {
text.push(value);
},
}, { xmlMode: true, decodeEntities: true });
parser.end(value);
return text.join("");
}