31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
import { Task } from '../types';
|
|
|
|
export const generatePlan = async (goal: string): Promise<Task[]> => {
|
|
try {
|
|
const response = await fetch('/api/generate-plan', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ goal }),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({ error: 'Failed to generate plan from server.' }));
|
|
throw new Error(errorData.error || `Server responded with status ${response.status}`);
|
|
}
|
|
|
|
const tasks: Omit<Task, 'status'>[] = await response.json();
|
|
|
|
// Add the 'status' property to each task, same as before.
|
|
return tasks.map(task => ({
|
|
...task,
|
|
status: 'todo',
|
|
}));
|
|
} catch (error) {
|
|
console.error("Error generating plan:", error);
|
|
const message = error instanceof Error ? error.message : "An unknown error occurred.";
|
|
throw new Error(`Failed to generate a plan. ${message}`);
|
|
}
|
|
};
|