AILIXIR Bot commited on
Commit
5bb46b2
·
1 Parent(s): ac4199a

Auto-sync: 5581a3c69a09940625f091c7f3399d882b5c76b9

Browse files
app/Http/Controllers/Api/ChemistryController.php CHANGED
@@ -72,7 +72,7 @@ class ChemistryController extends BaseController
72
  ]);
73
  }
74
 
75
- // ==================== Chat ====================
76
  public function chat(ChemistryChatRequest $request): JsonResponse
77
  {
78
  $user = Auth::user();
@@ -90,7 +90,20 @@ class ChemistryController extends BaseController
90
  ], 403);
91
  }
92
 
93
- $thread->update(['last_used_at' => now()]);
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  }
95
 
96
  $result = $this->chemistryService->chat(
@@ -166,7 +179,7 @@ class ChemistryController extends BaseController
166
  return $this->saveAndRespond($result, $user->id, 'docking', $request->input('docking_data'), $thread?->id);
167
  }
168
 
169
- // ==================== CSV (معدل) ====================
170
  public function uploadCsv(Request $request): JsonResponse
171
  {
172
  $user = Auth::user();
@@ -174,14 +187,14 @@ class ChemistryController extends BaseController
174
  $request->validate([
175
  'file' => 'required|file|mimes:csv,txt|max:2048',
176
  'analysis_type' => 'nullable|in:full,quick,admet,classify',
177
- 'thread_id' => 'required|string|max:255', // ← أصبح required
178
  ]);
179
 
180
  $file = $request->file('file');
181
  $analysisType = $request->input('analysis_type', 'full');
182
  $threadId = $request->input('thread_id');
183
 
184
- // ← Validate thread belongs to user
185
  $thread = ChemistryThread::where('user_id', $user->id)
186
  ->where('thread_id', $threadId)
187
  ->first();
@@ -211,7 +224,7 @@ class ChemistryController extends BaseController
211
 
212
  $job = ChemistryCsvJob::create([
213
  'user_id' => $user->id,
214
- 'chemistry_thread_id' => $thread->id, // ← جديد: ربط بالـ thread
215
  'job_id' => $apiJobId,
216
  'filename' => $file->getClientOriginalName(),
217
  'analysis_type' => $analysisType,
@@ -219,7 +232,7 @@ class ChemistryController extends BaseController
219
  'status' => 'queued',
220
  ]);
221
 
222
- // ← Update thread last_used_at
223
  $thread->update(['last_used_at' => now()]);
224
 
225
  return response()->json([
@@ -300,6 +313,7 @@ class ChemistryController extends BaseController
300
  ], 400);
301
  }
302
 
 
303
  if ($job->result_content) {
304
  return response($job->result_content)
305
  ->header('Content-Type', 'text/csv')
@@ -309,6 +323,7 @@ class ChemistryController extends BaseController
309
  $result = $this->chemistryService->getCsvResults($jobId);
310
 
311
  if (is_string($result)) {
 
312
  $job->update([
313
  'result_content' => $result,
314
  'result_file_path' => null,
@@ -329,6 +344,7 @@ class ChemistryController extends BaseController
329
  $query = $user->chemistryCsvJobs()
330
  ->orderBy('created_at', 'desc');
331
 
 
332
  if ($request->has('thread_id')) {
333
  $thread = ChemistryThread::where('user_id', $user->id)
334
  ->where('thread_id', $request->input('thread_id'))
@@ -410,7 +426,7 @@ class ChemistryController extends BaseController
410
  ]);
411
  }
412
 
413
- // ==================== Thread Messages ====================
414
  public function threadMessages(Request $request): JsonResponse
415
  {
416
  $user = Auth::user();
@@ -432,11 +448,13 @@ class ChemistryController extends BaseController
432
  ], 403);
433
  }
434
 
 
435
  $messages = ChemistryAnalysis::where('user_id', $user->id)
436
  ->where('chemistry_thread_id', $thread->id)
437
  ->orderBy('created_at', 'asc')
438
  ->get(['id', 'type', 'input_data', 'response', 'status', 'created_at']);
439
 
 
440
  $csvJobs = ChemistryCsvJob::where('user_id', $user->id)
441
  ->where('chemistry_thread_id', $thread->id)
442
  ->orderBy('created_at', 'asc')
@@ -465,6 +483,7 @@ class ChemistryController extends BaseController
465
  ],
466
  ]);
467
  }
 
468
  // ==================== Helper Methods ====================
469
  private function validateThread(int $userId, ?string $threadId): ?ChemistryThread
470
  {
 
72
  ]);
73
  }
74
 
75
+ // ==================== Chat (معدل: title من أول سؤال) ====================
76
  public function chat(ChemistryChatRequest $request): JsonResponse
77
  {
78
  $user = Auth::user();
 
90
  ], 403);
91
  }
92
 
93
+ // ← لو الـ title لسه "New Conversation"، غيريه لأول سؤال
94
+ if ($thread->title === 'New Conversation') {
95
+ $message = $request->input('message');
96
+ $newTitle = mb_strlen($message) > 50
97
+ ? mb_substr($message, 0, 50) . '...'
98
+ : $message;
99
+
100
+ $thread->update([
101
+ 'title' => $newTitle,
102
+ 'last_used_at' => now(),
103
+ ]);
104
+ } else {
105
+ $thread->update(['last_used_at' => now()]);
106
+ }
107
  }
108
 
109
  $result = $this->chemistryService->chat(
 
179
  return $this->saveAndRespond($result, $user->id, 'docking', $request->input('docking_data'), $thread?->id);
180
  }
181
 
182
+ // ==================== CSV (معدل: required thread_id) ====================
183
  public function uploadCsv(Request $request): JsonResponse
184
  {
185
  $user = Auth::user();
 
187
  $request->validate([
188
  'file' => 'required|file|mimes:csv,txt|max:2048',
189
  'analysis_type' => 'nullable|in:full,quick,admet,classify',
190
+ 'thread_id' => 'required|string|max:255',
191
  ]);
192
 
193
  $file = $request->file('file');
194
  $analysisType = $request->input('analysis_type', 'full');
195
  $threadId = $request->input('thread_id');
196
 
197
+ // Validate thread belongs to user
198
  $thread = ChemistryThread::where('user_id', $user->id)
199
  ->where('thread_id', $threadId)
200
  ->first();
 
224
 
225
  $job = ChemistryCsvJob::create([
226
  'user_id' => $user->id,
227
+ 'chemistry_thread_id' => $thread->id,
228
  'job_id' => $apiJobId,
229
  'filename' => $file->getClientOriginalName(),
230
  'analysis_type' => $analysisType,
 
232
  'status' => 'queued',
233
  ]);
234
 
235
+ // Update thread last_used_at
236
  $thread->update(['last_used_at' => now()]);
237
 
238
  return response()->json([
 
313
  ], 400);
314
  }
315
 
316
+ // ← Return content from DB if available
317
  if ($job->result_content) {
318
  return response($job->result_content)
319
  ->header('Content-Type', 'text/csv')
 
323
  $result = $this->chemistryService->getCsvResults($jobId);
324
 
325
  if (is_string($result)) {
326
+ // ← Store content in DB instead of file path
327
  $job->update([
328
  'result_content' => $result,
329
  'result_file_path' => null,
 
344
  $query = $user->chemistryCsvJobs()
345
  ->orderBy('created_at', 'desc');
346
 
347
+ // Optional: filter by thread_id
348
  if ($request->has('thread_id')) {
349
  $thread = ChemistryThread::where('user_id', $user->id)
350
  ->where('thread_id', $request->input('thread_id'))
 
426
  ]);
427
  }
428
 
429
+ // ==================== Thread Messages (جديد) ====================
430
  public function threadMessages(Request $request): JsonResponse
431
  {
432
  $user = Auth::user();
 
448
  ], 403);
449
  }
450
 
451
+ // Chat/analysis messages
452
  $messages = ChemistryAnalysis::where('user_id', $user->id)
453
  ->where('chemistry_thread_id', $thread->id)
454
  ->orderBy('created_at', 'asc')
455
  ->get(['id', 'type', 'input_data', 'response', 'status', 'created_at']);
456
 
457
+ // CSV jobs related to this thread
458
  $csvJobs = ChemistryCsvJob::where('user_id', $user->id)
459
  ->where('chemistry_thread_id', $thread->id)
460
  ->orderBy('created_at', 'asc')
 
483
  ],
484
  ]);
485
  }
486
+
487
  // ==================== Helper Methods ====================
488
  private function validateThread(int $userId, ?string $threadId): ?ChemistryThread
489
  {