1414 * 2. Have a local Worker running on `localhost:8787` with the following code (also requires adding a binding in the Wrangler config file):
1515 *
1616 * ```
17- * export interface Env {
18- * AI: Ai;
19- * }
20-
21- * export default {
22- * async fetch(request, env): Promise<Response> {
23- * const response = await env.AI.run("@cf /facebook/bart-large-cnn", {
24- * input_text: await request.text(),
25- * max_length: 60
26- * });
27- * return Response.json(response.summary);
28- * },
29- * } satisfies ExportedHandler<Env>;
17+ export interface Env {
18+ AI: Ai;
19+ }
20+
21+ export default {
22+ async fetch(request, env): Promise<Response> {
23+
24+ const input_text = await request.text()
25+
26+ const messages = [
27+ { role: "system", content: "You are an assistant who helps summarize long chunks of text." },
28+ { role: "system", content: "You help generate optimized SEO descriptions that are - at most - 60 words. These also convey the most important points of the page and contain keywords." },
29+ { role: "system", content: "In your response, provide no content other than the summary text."},
30+ {
31+ role: "user",
32+ content: input_text,
33+ },
34+ ];
35+ const response = await env.AI.run("@cf/meta/llama-3.3-70b-instruct-fp8-fast", { messages });
36+
37+ return Response.json(response.response);
38+ },
39+ } satisfies ExportedHandler<Env>;
3040 * ```
31- * 3. Run `npx tsx bin/generate-descriptions.ts --pcx-content-type $TYPE` to generate the descriptions.
41+ * 3. Run `npx tsx bin/generate-descriptions.ts [options]` to generate the descriptions.
42+ *
43+ * Available options:
44+ * - `--pcx-content-type $TYPE`: Filter by content type (e.g., tutorial, overview)
45+ * - `--product $PRODUCT`: Filter by product folder (e.g., workers, pages, r2)
3246 *
3347 */
3448
@@ -166,18 +180,22 @@ async function updateFrontmatter(
166180function parseArgs ( ) {
167181 const args = process . argv . slice ( 2 ) ;
168182 let pcxContentType : string | undefined ;
183+ let product : string | undefined ;
169184 let showHelp = false ;
170185
171186 for ( let i = 0 ; i < args . length ; i ++ ) {
172187 if ( args [ i ] === "--pcx-content-type" && i + 1 < args . length ) {
173188 pcxContentType = args [ i + 1 ] ;
174189 i ++ ; // Skip the next argument as it's the value
190+ } else if ( args [ i ] === "--product" && i + 1 < args . length ) {
191+ product = args [ i + 1 ] ;
192+ i ++ ; // Skip the next argument as it's the value
175193 } else if ( args [ i ] === "--help" || args [ i ] === "-h" ) {
176194 showHelp = true ;
177195 }
178196 }
179197
180- return { pcxContentType, showHelp } ;
198+ return { pcxContentType, product , showHelp } ;
181199}
182200
183201/**
@@ -189,13 +207,14 @@ Usage: npx tsx bin/generate-descriptions.ts [options]
189207
190208Options:
191209 --pcx-content-type <type> Filter MDX files by pcx_content_type (e.g., overview, tutorial, navigation)
210+ --product <product> Filter MDX files by product folder (e.g., workers, pages, r2)
192211 --help, -h Show this help message
193212` ) ;
194213}
195214
196215async function main ( ) {
197216 // Parse command line arguments
198- const { pcxContentType, showHelp } = parseArgs ( ) ;
217+ const { pcxContentType, product , showHelp } = parseArgs ( ) ;
199218
200219 if ( showHelp ) {
201220 showUsage ( ) ;
@@ -205,6 +224,10 @@ async function main() {
205224 if ( pcxContentType ) {
206225 console . log ( `Filtering by pcx_content_type: ${ pcxContentType } ` ) ;
207226 }
227+
228+ if ( product ) {
229+ console . log ( `Filtering by product: ${ product } ` ) ;
230+ }
208231 try {
209232 // Find all MDX files in the docs directory
210233 const mdxFiles = await globby ( "**/*.mdx" , {
@@ -213,21 +236,45 @@ async function main() {
213236 } ) ;
214237 console . log ( `Found ${ mdxFiles . length } MDX files in the docs directory` ) ;
215238
216- // Filter files by pcx_content_type if specified
239+ // Filter files by product if specified
217240 let filteredMdxFiles = mdxFiles ;
241+ if ( product ) {
242+ const productPath = path . join ( DOCS_DIR , product ) ;
243+
244+ // Check if the product directory exists
245+ try {
246+ await fs . access ( productPath ) ;
247+ } catch ( error ) {
248+ console . error (
249+ `Product directory not found: ${ productPath } -- ${ error } ` ,
250+ ) ;
251+ process . exit ( 1 ) ;
252+ }
253+
254+ // Filter files by product
255+ filteredMdxFiles = mdxFiles . filter ( ( file ) => {
256+ return file . startsWith ( productPath ) ;
257+ } ) ;
258+ console . log (
259+ `Filtered to ${ filteredMdxFiles . length } MDX files in product: ${ product } ` ,
260+ ) ;
261+ }
262+
263+ // Further filter files by pcx_content_type if specified
218264 if ( pcxContentType ) {
219- filteredMdxFiles = [ ] ;
220- for ( const mdxFile of mdxFiles ) {
265+ const contentTypeFiltered = [ ] ;
266+ for ( const mdxFile of filteredMdxFiles ) {
221267 try {
222268 const content = await fs . readFile ( mdxFile , "utf-8" ) ;
223269 const { data : frontmatter } = matter ( content ) ;
224270 if ( frontmatter . pcx_content_type === pcxContentType ) {
225- filteredMdxFiles . push ( mdxFile ) ;
271+ contentTypeFiltered . push ( mdxFile ) ;
226272 }
227273 } catch ( error ) {
228274 console . error ( `Error reading ${ mdxFile } :` , error ) ;
229275 }
230276 }
277+ filteredMdxFiles = contentTypeFiltered ;
231278 console . log (
232279 `Filtered to ${ filteredMdxFiles . length } MDX files with pcx_content_type: ${ pcxContentType } ` ,
233280 ) ;
@@ -306,6 +353,14 @@ async function main() {
306353
307354 console . log ( "\n--- Summary ---" ) ;
308355 console . log ( `Total MDX files: ${ mdxFiles . length } ` ) ;
356+ if ( product ) {
357+ console . log ( `Files in product '${ product } ': ${ filteredMdxFiles . length } ` ) ;
358+ }
359+ if ( pcxContentType ) {
360+ console . log (
361+ `Files with pcx_content_type '${ pcxContentType } ': ${ filteredMdxFiles . length } ` ,
362+ ) ;
363+ }
309364 console . log ( `Updated: ${ updatedCount } ` ) ;
310365 console . log ( `Skipped (already had description): ${ skippedExistingCount } ` ) ;
311366 console . log ( `Skipped (description unchanged): ${ skippedUnchangedCount } ` ) ;
0 commit comments