everydaytok commited on
Commit
a276d1e
·
verified ·
1 Parent(s): 122a2a0

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +24 -1
app.js CHANGED
@@ -78,6 +78,26 @@ const callAI = async (history, input, contextData, systemPrompt, projectContext,
78
  } catch (e) { return { text: "<notification>AI Unreachable</notification>", usage: {} }; }
79
  };
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  function extractCommands(text) {
82
  const commands = [];
83
  const parse = (regex, type, isJson = true) => {
@@ -123,7 +143,10 @@ async function executeCommands(userId, projectId, commands) {
123
  for (const cmd of commands) {
124
  try {
125
  if (cmd.type === 'create_thrust') {
126
- const { data: thrust } = await supabase.from('thrusts').insert({ lead_id: projectId, title: cmd.payload.title, markdown_content: cmd.payload.markdown_content, status: 'active' }).select().single();
 
 
 
127
  if (thrust && cmd.payload.tasks && cmd.payload.tasks.length > 0) {
128
  const tasks = cmd.payload.tasks.map(t => ({ thrust_id: thrust.id, title: t }));
129
  await supabase.from('thrust_tasks').insert(tasks);
 
78
  } catch (e) { return { text: "<notification>AI Unreachable</notification>", usage: {} }; }
79
  };
80
 
81
+ // --- THRUST CLEANUP HELPER ---
82
+ async function clearPreviousThrusts(projectId) {
83
+ try {
84
+ // Deleting the thrust automatically deletes all its tasks
85
+ // because of the 'on delete cascade' rule in Supabase.
86
+ const { error } = await supabase
87
+ .from('thrusts')
88
+ .delete()
89
+ .eq('lead_id', projectId);
90
+
91
+ if (error) {
92
+ console.error(`[DB Error] Failed to clear old thrusts for ${projectId}:`, error.message);
93
+ } else {
94
+ console.log(`🧹 Cleared previous thrusts for project ${projectId}`);
95
+ }
96
+ } catch (e) {
97
+ console.error("Error in clearPreviousThrusts:", e.message);
98
+ }
99
+ }
100
+
101
  function extractCommands(text) {
102
  const commands = [];
103
  const parse = (regex, type, isJson = true) => {
 
143
  for (const cmd of commands) {
144
  try {
145
  if (cmd.type === 'create_thrust') {
146
+ // 👉 ADD THIS LINE: Wipe out the old thrust and tasks first
147
+ await clearPreviousThrusts(projectId);
148
+
149
+ const { data: thrust } = await supabase.from('thrusts').insert({ lead_id: projectId, title: cmd.payload.title, markdown_content: cmd.payload.markdown_content, status: 'active' }).select().single();
150
  if (thrust && cmd.payload.tasks && cmd.payload.tasks.length > 0) {
151
  const tasks = cmd.payload.tasks.map(t => ({ thrust_id: thrust.id, title: t }));
152
  await supabase.from('thrust_tasks').insert(tasks);