Skip to content

Commit

Permalink
Fixed some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
NTh1nk committed Dec 20, 2024
1 parent 6f5e4a7 commit d3d0b51
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 116 deletions.
100 changes: 47 additions & 53 deletions supabase/functions/generateChildren/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
Expand All @@ -120,4 +114,4 @@ export default function App() {
</View>
);
*/
}

131 changes: 68 additions & 63 deletions supabase/functions/generateScenario/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit d3d0b51

Please sign in to comment.