Branchwell

AI Resource Pro / website / scripts / associate-tools-with-llms.ts

1import { createClient } from '@supabase/supabase-js';
2import * as dotenv from 'dotenv';
3import * as path from 'path';
4import * as fs from 'fs';
5
6// Load environment variables
7const envLocalPath = path.resolve(process.cwd(), '../.env.local');
8if (fs.existsSync(envLocalPath)) {
9 console.log(`Loading environment from: ${envLocalPath}`);
10 dotenv.config({ path: envLocalPath });
11} else {
12 console.log(`No .env.local file found at: ${envLocalPath}`);
13 dotenv.config();
14}
15
16async function associateToolsWithLLMs() {
17 const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
18 const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
19
20 if (!supabaseUrl || !supabaseKey) {
21 console.error('Missing Supabase environment variables');
22 process.exit(1);
23 }
24
25 const supabase = createClient(supabaseUrl, supabaseKey);
26 const llmsCategoryId = 61;
27
28 // Verify the LLMs category exists
29 const { data: llmsCategory, error: categoryError } = await supabase
30 .from('categories')
31 .select('name')
32 .eq('id', llmsCategoryId)
33 .single();
34
35 if (categoryError || !llmsCategory) {
36 console.error(`Category with ID ${llmsCategoryId} not found`);
37 return;
38 }
39
40 console.log(`Found category: ${llmsCategoryId} (${llmsCategory.name})`);
41
42 // Find potential LLM tools
43 console.log('\nFinding potential LLM tools...');
44 const { data: llmTools, error: toolsError } = await supabase
45 .from('tools')
46 .select('id, name, published')
47 .or('name.ilike.%llm%,name.ilike.%language model%,name.ilike.%gpt%,name.ilike.%claude%,name.ilike.%gemini%,name.ilike.%llama%');
48
49 if (toolsError || !llmTools || llmTools.length === 0) {
50 console.log('No potential LLM tools found');
51 return;
52 }
53
54 console.log(`Found ${llmTools.length} potential LLM tools`);
55
56 // Check which tools are already associated with the LLMs category
57 const { data: existingAssociations } = await supabase
58 .from('tools_categories')
59 .select('tool_id')
60 .eq('category_id', llmsCategoryId);
61
62 const existingToolIds = new Set(existingAssociations?.map(a => a.tool_id) || []);
63
64 // Prepare list of tools to associate
65 const toolsToAssociate = llmTools
66 .filter(tool => !existingToolIds.has(tool.id) && tool.published)
67 .map(tool => ({
68 tool_id: tool.id,
69 category_id: llmsCategoryId,
70 is_primary: false, // Not setting as primary since they already have primary categories
71 created_at: new Date().toISOString()
72 }));
73
74 console.log(`Found ${toolsToAssociate.length} tools to associate with the LLMs category`);
75
76 if (toolsToAssociate.length === 0) {
77 console.log('No new tools to associate');
78 return;
79 }
80
81 console.log('\nTools to be associated:');
82 for (const tool of llmTools.filter(t => !existingToolIds.has(t.id) && t.published)) {
83 console.log(`- ${tool.name} (ID: ${tool.id})`);
84 }
85
86 console.log('\nTo associate these tools with the LLMs category, run with --execute flag');
87 console.log('Example: npx ts-node associate-tools-with-llms.ts --execute');
88
89 // Check if --execute flag is provided
90 if (process.argv.includes('--execute')) {
91 console.log('\n===== EXECUTING ASSOCIATION =====');
92 console.log(`Associating ${toolsToAssociate.length} tools with the LLMs category...`);
93
94 // Insert in batches of 10
95 for (let i = 0; i < toolsToAssociate.length; i += 10) {
96 const batch = toolsToAssociate.slice(i, i + 10);
97 console.log(`Inserting batch ${i/10 + 1} (${batch.length} tools)...`);
98
99 const { error } = await supabase
100 .from('tools_categories')
101 .insert(batch);
102
103 if (error) {
104 console.error(`Error inserting batch: ${error.message}`);
105 } else {
106 console.log(`Successfully inserted batch ${i/10 + 1}`);
107 }
108 }
109
110 console.log('Association completed');
111 }
112}
113
114associateToolsWithLLMs().catch(console.error);