import { createClient } from '@supabase/supabase-js'; import * as dotenv from 'dotenv'; import * as path from 'path'; import * as fs from 'fs'; // Load environment variables const envLocalPath = path.resolve(process.cwd(), '../.env.local'); if (fs.existsSync(envLocalPath)) { console.log(`Loading environment from: ${envLocalPath}`); dotenv.config({ path: envLocalPath }); } else { console.log(`No .env.local file found at: ${envLocalPath}`); dotenv.config(); } async function associateToolsWithLLMs() { const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY; if (!supabaseUrl || !supabaseKey) { console.error('Missing Supabase environment variables'); process.exit(1); } const supabase = createClient(supabaseUrl, supabaseKey); const llmsCategoryId = 61; // Verify the LLMs category exists const { data: llmsCategory, error: categoryError } = await supabase .from('categories') .select('name') .eq('id', llmsCategoryId) .single(); if (categoryError || !llmsCategory) { console.error(`Category with ID ${llmsCategoryId} not found`); return; } console.log(`Found category: ${llmsCategoryId} (${llmsCategory.name})`); // Find potential LLM tools console.log('\nFinding potential LLM tools...'); const { data: llmTools, error: toolsError } = await supabase .from('tools') .select('id, name, published') .or('name.ilike.%llm%,name.ilike.%language model%,name.ilike.%gpt%,name.ilike.%claude%,name.ilike.%gemini%,name.ilike.%llama%'); if (toolsError || !llmTools || llmTools.length === 0) { console.log('No potential LLM tools found'); return; } console.log(`Found ${llmTools.length} potential LLM tools`); // Check which tools are already associated with the LLMs category const { data: existingAssociations } = await supabase .from('tools_categories') .select('tool_id') .eq('category_id', llmsCategoryId); const existingToolIds = new Set(existingAssociations?.map(a => a.tool_id) || []); // Prepare list of tools to associate const toolsToAssociate = llmTools .filter(tool => !existingToolIds.has(tool.id) && tool.published) .map(tool => ({ tool_id: tool.id, category_id: llmsCategoryId, is_primary: false, // Not setting as primary since they already have primary categories created_at: new Date().toISOString() })); console.log(`Found ${toolsToAssociate.length} tools to associate with the LLMs category`); if (toolsToAssociate.length === 0) { console.log('No new tools to associate'); return; } console.log('\nTools to be associated:'); for (const tool of llmTools.filter(t => !existingToolIds.has(t.id) && t.published)) { console.log(`- ${tool.name} (ID: ${tool.id})`); } console.log('\nTo associate these tools with the LLMs category, run with --execute flag'); console.log('Example: npx ts-node associate-tools-with-llms.ts --execute'); // Check if --execute flag is provided if (process.argv.includes('--execute')) { console.log('\n===== EXECUTING ASSOCIATION ====='); console.log(`Associating ${toolsToAssociate.length} tools with the LLMs category...`); // Insert in batches of 10 for (let i = 0; i < toolsToAssociate.length; i += 10) { const batch = toolsToAssociate.slice(i, i + 10); console.log(`Inserting batch ${i/10 + 1} (${batch.length} tools)...`); const { error } = await supabase .from('tools_categories') .insert(batch); if (error) { console.error(`Error inserting batch: ${error.message}`); } else { console.log(`Successfully inserted batch ${i/10 + 1}`); } } console.log('Association completed'); } } associateToolsWithLLMs().catch(console.error);