From b2cba70f797c0ac074c31c3453509e2abdc251da Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Fri, 3 Oct 2025 18:56:40 +0000 Subject: [PATCH] saving diagrams --- .../002_add_diagram_text_to_profiles.sql | 1 + generate_diagram.php | 16 +- generate_roles.php | 50 +++++ generate_tools.php | 50 +++++ profile.php | 202 +++++++++++++++--- save_diagram.php | 44 ++++ 6 files changed, 334 insertions(+), 29 deletions(-) create mode 100644 db/migrations/002_add_diagram_text_to_profiles.sql create mode 100644 generate_roles.php create mode 100644 generate_tools.php create mode 100644 save_diagram.php diff --git a/db/migrations/002_add_diagram_text_to_profiles.sql b/db/migrations/002_add_diagram_text_to_profiles.sql new file mode 100644 index 0000000..2b566ce --- /dev/null +++ b/db/migrations/002_add_diagram_text_to_profiles.sql @@ -0,0 +1 @@ +ALTER TABLE `gtm_profiles` ADD `diagram_mermaid_text` TEXT NULL DEFAULT NULL; \ No newline at end of file diff --git a/generate_diagram.php b/generate_diagram.php index 55267ef..1b82f03 100644 --- a/generate_diagram.php +++ b/generate_diagram.php @@ -106,6 +106,7 @@ file_put_contents($log_file, $log_entry, FILE_APPEND); // Clean up the response and extract the Mermaid code $diagram = trim($diagram); +$is_error = false; // Regex to find Mermaid code block, with or without backticks and optional "mermaid" label if (preg_match('/```(?:mermaid)?\s*(graph\s(TD|LR|BT|RL);?[\s\S]*?)```|^(graph\s(TD|LR|BT|RL);?[\s\S]*)/', $diagram, $matches)) { @@ -116,8 +117,19 @@ if (preg_match('/```(?:mermaid)?\s*(graph\s(TD|LR|BT|RL);?[\s\S]*?)```|^(graph\s // If no match, log the bad response and set an error for the user error_log("Invalid AI response format: " . $diagram); $diagram = "graph TD; A[Error: Invalid AI response format.];"; + $is_error = true; } +// --- Auto-save the generated diagram to the database --- +if (!$is_error) { + try { + $pdo = db(); + $stmt = $pdo->prepare("UPDATE gtm_profiles SET diagram_mermaid_text = ? WHERE id = ?"); + $stmt->execute([$diagram, $profile_id]); + } catch (PDOException $e) { + // Log the error, but don't block the user. The diagram was generated, just not saved. + error_log("DB Error saving diagram for profile {$profile_id}: " . $e->getMessage()); + } +} - -echo json_encode(['diagram' => $diagram]); +echo json_encode(['diagram' => $diagram]); \ No newline at end of file diff --git a/generate_roles.php b/generate_roles.php new file mode 100644 index 0000000..47b7721 --- /dev/null +++ b/generate_roles.php @@ -0,0 +1,50 @@ + 'Profile ID is missing.']); + exit; +} + +try { + $pdo = db(); + $stmt = $pdo->prepare("SELECT * FROM gtm_profiles WHERE id = ?"); + $stmt->execute([$profile_id]); + $profile = $stmt->fetch(PDO::FETCH_ASSOC); + + if (!$profile) { + echo json_encode(['error' => 'Profile not found.']); + exit; + } + + // Simulate AI-powered recommendations based on profile data + $recommendations = '