diff --git a/supabase/functions/generateChildren/index.ts b/supabase/functions/generateChildren/index.ts index 9b07652..48560f7 100644 --- a/supabase/functions/generateChildren/index.ts +++ b/supabase/functions/generateChildren/index.ts @@ -42,69 +42,63 @@ type Message = { } } -// Fetch previous scenarios from Supabase -async function getPreviousScenarios(startId) { - try { - const { data, error } = await supabase - .from('cards') - .select('id, leading_choice, content') - .eq('parent', startId); - - if (error) throw error; - - return data.map(d => ({ - role: d.leading_choice ? 'user' : 'assistant', - content: d.content?.text || '' - })); - } catch (error) { - console.error("Error fetching previous scenarios:", error); - throw new Error('Error fetching previous scenarios'); - } +async function getPreviousScenarios(startId: number) { // Line 46: Explicitly typed as 'number' + const { data, error } = await supabase + .from('cards') + .select('id, leading_choice, content') + .eq('parent', startId); + + if (error) throw error; + + return data.map(d => ({ + role: d.leading_choice ? 'user' : 'assistant', + content: d.content?.text || '' + })); } -// Function to handle the scenario expansion logic -async function expandScenario(scenarioToExpand) { - try { - const previousScenarios = await getPreviousScenarios(scenarioToExpand); - - const newScenarioMessages = [ - ...previousScenarios, - { role: 'user', content: 'new choice' }, // Example leading choice - ]; - - const newScenario = await generateScenario(newScenarioMessages); - - // Insert the new scenario into the database - const { data, error } = await supabase - .from('cards') - .insert([ - { - parent: scenarioToExpand, - leading_choice: 'Option A', - content: { text: newScenario }, - }, - ]); +async function expandScenario(scenarioToExpand: number) { // Line 60: Explicitly typed as 'number' + try { + const previousScenarios = await getPreviousScenarios(scenarioToExpand); + + const newScenarioMessages = [ + ...previousScenarios, + { role: 'user', content: 'new choice' }, // Example leading choice + ]; + + const newScenario = await generateScenario(newScenarioMessages); + + // Insert the new scenario into the database + const { data, error } = await supabase + .from('cards') + .insert([ + { + parent: scenarioToExpand, + leading_choice: 'Option A', + content: { text: newScenario }, + }, + ]); + + if (error) throw error; + + return data; + } catch (error) { + console.error("Error expanding scenario", error); + } + } - if (error) throw error; - return data; - } catch (error) { - console.error("Error expanding scenario:", error); - return null; - } -} + export default function App() { // Explicitly typing the state const [scenario, setScenario] = React.useState<{ content: { text: string } }[] | null>(null); - const [loading, setLoading] = React.useState(false); const handleGenerateScenario = async () => { - setLoading(true); - const result = await expandScenario(123); // Pass the ID of the scenario you want to expand - setScenario(result); - setLoading(false); + const result = await expandScenario(123); // Pass the ID of the scenario you want to expand + setScenario(result ?? []); // Ensure that 'result' is not undefined, use an empty array as fallback }; + +}; /* return ( @@ -120,4 +114,4 @@ export default function App() { ); */ -} + diff --git a/supabase/functions/generateScenario/index.ts b/supabase/functions/generateScenario/index.ts index 77902f4..66aaadc 100644 --- a/supabase/functions/generateScenario/index.ts +++ b/supabase/functions/generateScenario/index.ts @@ -4,8 +4,8 @@ import { z } from 'zod'; import Config from 'react-native-config'; // Set up Supabase Client -const supabaseUrl = Config.SUPABASE_URL; -const supabaseKey = Config.SUPABASE_KEY; +const supabaseUrl = Config.SUPABASE_URL as string; +const supabaseKey = Config.SUPABASE_KEY as string; const supabase = createClient(supabaseUrl, supabaseKey); // OpenAI Model Setup @@ -14,71 +14,76 @@ const SYSTEMPROMPT = "Create a brief current event scenario (2-3 sentences) for const OPENAI_MODEL = "gpt-4o-2024-08-06"; -// Function to generate the next scenario -async function generateScenario(messages) { - try { - const response = await axios.post( - 'https://api.openai.com/v1/chat/completions', - { - model: OPENAI_MODEL, - messages: [ - { role: "system", content: SYSTEMPROMPT }, - ...messages, - ] - }, - { headers: { 'Authorization': `Bearer ${OPENAI_API_KEY}` } } - ); - return response.data.choices[0].message.content; // Adjust according to OpenAI response format - } catch (error) { - console.error("Error generating scenario", error); +type Message = { + role: string; + content: string; + }; + + async function generateScenario(messages: Message[]) { + try { + const response = await axios.post( + 'https://api.openai.com/v1/chat/completions', + { + model: OPENAI_MODEL, + messages: [ + { role: "system", content: SYSTEMPROMPT }, + ...messages, + ] + }, + { headers: { 'Authorization': `Bearer ${OPENAI_API_KEY}` } } + ); + return response.data.choices[0].message.content; // Adjust according to OpenAI response format + } catch (error) { + console.error("Error generating scenario", error); + } + } +// Define the type for the startId parameter +async function getPreviousScenarios(startId: number) { + const { data, error } = await supabase + .from('cards') + .select('id, leading_choice, content') + .eq('parent', startId); + + if (error) throw error; + + return data.map(d => ({ + role: d.leading_choice ? 'user' : 'assistant', + content: d.content?.text || '' + })); } -} - -// Fetch previous scenarios from Supabase -async function getPreviousScenarios(startId) { - const { data, error } = await supabase - .from('cards') - .select('id, leading_choice, content') - .eq('parent', startId); - if (error) throw error; - - return data.map(d => ({ - role: d.leading_choice ? 'user' : 'assistant', - content: d.content?.text || '' - })); -} // Function to handle the scenario expansion logic -async function expandScenario(scenarioToExpand) { - try { - const previousScenarios = await getPreviousScenarios(scenarioToExpand); - - const newScenarioMessages = [ - ...previousScenarios, - { role: 'user', content: 'new choice' }, // Example leading choice - ]; - - const newScenario = await generateScenario(newScenarioMessages); - - // Insert the new scenario into the database - const { data, error } = await supabase - .from('cards') - .insert([ - { - parent: scenarioToExpand, - leading_choice: 'Option A', - content: { text: newScenario } - } - ]); - - if (error) throw error; - - return data; - } catch (error) { - console.error("Error expanding scenario", error); +// Define the type for scenarioToExpand as a number +async function expandScenario(scenarioToExpand: number) { + try { + const previousScenarios = await getPreviousScenarios(scenarioToExpand); + + const newScenarioMessages = [ + ...previousScenarios, + { role: 'user', content: 'new choice' }, // Example leading choice + ]; + + const newScenario = await generateScenario(newScenarioMessages); + + // Insert the new scenario into the database + const { data, error } = await supabase + .from('cards') + .insert([ + { + parent: scenarioToExpand, + leading_choice: 'Option A', + content: { text: newScenario } + } + ]); + + if (error) throw error; + + return data; + } catch (error) { + console.error("Error expanding scenario", error); + } } -} - + // Example usage: Call this to expand a scenario expandScenario(123); // Pass the ID of the scenario you want to expand