18-vm/api/chat.php
Flatlogic Bot 00a5c5a638 Version 1
2026-01-19 00:06:34 +00:00

110 lines
4.0 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
require_once __DIR__ . '/../ai/LocalAIApi.php';
header('Content-Type: application/json');
$input = json_decode(file_get_contents('php://input'), true);
if (isset($input['action']) && $input['action'] === 'get_past_timesheets') {
try {
$pdo = db();
$stmt = $pdo->query("SELECT DATE(created_at) as timesheet_date, JSON_ARRAYAGG(JSON_OBJECT('id', id, 'task', task, 'project', project, 'output_type', output_type, 'duration', duration, 'timestamp', DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s'))) as activities FROM activity_log GROUP BY timesheet_date ORDER BY timesheet_date DESC");
$past_timesheets = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode(['past_timesheets' => $past_timesheets]);
} catch (PDOException $e) {
http_response_code(500);
error_log("DB Error: " . $e->getMessage());
echo json_encode(['error' => 'Database error while fetching past timesheets']);
}
exit;
}
if (isset($input['action']) && $input['action'] === 'manual_log') {
$task_description = $input['type'] . ': ' . $input['task'];
$project = $input['project'];
// For manual entries, we can set a duration of 0 or make it an optional field.
$duration_formatted = '00:00:00';
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO activity_log (task, project, output_type, duration) VALUES (?, ?, ?, ?)");
$stmt->execute([$task_description, $project, 'Human', $duration_formatted]);
$new_log_id = $pdo->lastInsertId();
$log_stmt = $pdo->prepare("SELECT *, DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') as timestamp FROM activity_log WHERE id = ?");
$log_stmt->execute([$new_log_id]);
$new_log_entry = $log_stmt->fetch(PDO::FETCH_ASSOC);
echo json_encode(['new_log_entry' => $new_log_entry]);
} catch (PDOException $e) {
http_response_code(500);
error_log("DB Error: " . $e->getMessage());
echo json_encode(['error' => 'Database error while logging manual entry']);
}
exit;
}
$user_message = $input['message'] ?? '';
if (empty($user_message)) {
echo json_encode(['error' => 'Empty message']);
exit;
}
$start_time = microtime(true);
// Use the LocalAIApi to get a response from the AI
$ai_response_data = LocalAIApi::createResponse([
'input' => [
['role' => 'system', 'content' => 'You are a helpful assistant.'],
['role' => 'user', 'content' => $user_message],
],
]);
if (empty($ai_response_data['success'])) {
http_response_code(500);
error_log('AI API Error: ' . ($ai_response_data['error'] ?? 'Unknown error'));
echo json_encode(['error' => 'Failed to get response from AI', 'details' => $ai_response_data['error'] ?? '']);
exit;
}
$ai_message = LocalAIApi::extractText($ai_response_data);
if ($ai_message === '') {
$decoded = LocalAIApi::decodeJsonFromResponse($ai_response_data);
$ai_message = $decoded ? json_encode($decoded, JSON_UNESCAPED_UNICODE) : 'Empty response from AI.';
}
$end_time = microtime(true);
$duration_seconds = round($end_time - $start_time);
$duration_formatted = gmdate("H:i:s", $duration_seconds);
$project_name = 'Chat Interaction';
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO activity_log (task, project, output_type, duration) VALUES (?, ?, ?, ?)");
$stmt->execute([$ai_message, $project_name, 'AI', $duration_formatted]);
// Fetch the new log entry to return it
$new_log_id = $pdo->lastInsertId();
$log_stmt = $pdo->prepare("SELECT *, DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') as timestamp FROM activity_log WHERE id = ?");
$log_stmt->execute([$new_log_id]);
$new_log_entry = $log_stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
http_response_code(500);
error_log("DB Error: " . $e->getMessage());
echo json_encode(['error' => 'Database error while logging']);
exit;
}
echo json_encode([
'reply' => $ai_message,
'new_log_entry' => $new_log_entry
]);