{
"tool_response": {
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Controller\/Api\/ChatController.php",
"oldString": " \/**\n * Get database connection\n *\/\n private function getDatabase(): \\PDO\n {\n $credentials = $this->loadCredentials();\n\n return new \\PDO(\n 'mysql:host=localhost;dbname=ki_content;charset=utf8mb4',\n 'root',\n $credentials['db_password'],\n [\\PDO::ATTR_ERRMODE => \\PDO::ERRMODE_EXCEPTION]\n );\n }\n\n \/**\n * Load credentials from file\n *\/\n private function loadCredentials(): array\n {\n $file = '\/var\/www\/docs\/credentials\/credentials.md';\n $content = file_get_contents($file);\n\n $password = '';\n foreach (explode(\"\\n\", $content) as $line) {\n if (str_contains($line, 'MariaDB') && str_contains($line, 'root')) {\n $parts = explode('|', $line);\n if (count($parts) >= 4) {\n $password = trim($parts[3]);\n break;\n }\n }\n }\n\n return ['db_password' => $password];\n }\n}",
"newString": " \/**\n * Get database connection for dokumentation tables (ki_dev)\n *\/\n private function getDatabase(): \\PDO\n {\n return \\Infrastructure\\Config\\DatabaseFactory::dev();\n }\n}",
"originalFile": "<?php\n\nnamespace Controller\\Api;\n\nuse Framework\\Controller;\nuse Infrastructure\\AI\\AIConfig;\nuse Infrastructure\\AI\\ChatService;\nuse Infrastructure\\AI\\OllamaService;\nuse Infrastructure\\AI\\QdrantService;\n\nclass ChatController extends Controller\n{\n private ChatService $chatService;\n private OllamaService $ollamaService;\n private QdrantService $qdrantService;\n\n public function __construct()\n {\n $config = AIConfig::fromCredentialsFile();\n $this->chatService = $config->createChatService();\n $this->ollamaService = $config->createOllamaService();\n $this->qdrantService = $config->createQdrantService();\n }\n\n \/**\n * POST \/api\/v1\/chat\n * Handle chat message\n *\/\n public function send(): void\n {\n $input = json_decode(file_get_contents('php:\/\/input'), true);\n $question = trim($input['message'] ?? '');\n\n if ($question === '') {\n $this->json(['error' => 'Keine Frage angegeben'], 400);\n\n return;\n }\n\n try {\n $result = $this->askChat($question);\n $this->json($result);\n } catch (\\Exception $e) {\n $this->json(['error' => $e->getMessage()], 500);\n }\n }\n\n \/**\n * GET \/api\/v1\/chat\/search\n * Search for relevant chunks\n *\/\n public function search(): void\n {\n $query = trim($_GET['q'] ?? '');\n $limit = (int) ($_GET['limit'] ?? 5);\n\n if ($query === '') {\n $this->json(['error' => 'Keine Suchanfrage'], 400);\n\n return;\n }\n\n try {\n $results = $this->searchChunks($query, $limit);\n $this->json(['results' => $results]);\n } catch (\\Exception $e) {\n $this->json(['error' => $e->getMessage()], 500);\n }\n }\n\n \/**\n * GET \/api\/v1\/chat\/stats\n * Get chat\/document statistics (Doc2Vector Pipeline)\n *\/\n public function stats(): void\n {\n try {\n $pdo = $this->getDatabase();\n\n $stats = [\n 'dokumente' => (int) $pdo->query('SELECT COUNT(*) FROM dokumentation WHERE depth = 0')->fetchColumn(),\n 'seiten' => (int) $pdo->query('SELECT COUNT(*) FROM dokumentation WHERE depth > 0')->fetchColumn(),\n 'chunks' => (int) $pdo->query('SELECT COUNT(*) FROM dokumentation_chunks')->fetchColumn(),\n 'tokens' => (int) $pdo->query('SELECT COALESCE(SUM(token_count), 0) FROM dokumentation_chunks')->fetchColumn(),\n 'analyzed' => (int) $pdo->query(\"SELECT COUNT(*) FROM dokumentation_chunks WHERE analysis_status = 'completed'\")->fetchColumn(),\n 'synced' => (int) $pdo->query('SELECT COUNT(*) FROM dokumentation_chunks WHERE qdrant_id IS NOT NULL')->fetchColumn(),\n ];\n\n $this->json($stats);\n } catch (\\Exception $e) {\n $this->json(['error' => $e->getMessage()], 500);\n }\n }\n\n \/**\n * Ask chat question using ChatService\n *\n * @param string $question User's question\n *\n * @return array<string, mixed> Chat response with answer and sources\n *\n * @throws \\RuntimeException If chat service fails\n *\/\n private function askChat(string $question): array\n {\n try {\n \/\/ Use dokumentation_chunks collection for RAG\n return $this->chatService->chat($question, 'claude-opus-4-5-20251101', 'dokumentation_chunks', 5);\n } catch (\\RuntimeException $e) {\n throw new \\RuntimeException('Chat-Service konnte nicht ausgeführt werden: ' . $e->getMessage(), 0, $e);\n }\n }\n\n \/**\n * Search for similar chunks using vector search\n *\n * @param string $query Search query\n * @param int $limit Maximum number of results\n *\n * @return array<int, array{id: int|string, score: float, payload: array<string, mixed>}> Search results\n *\n * @throws \\RuntimeException If search fails\n *\/\n private function searchChunks(string $query, int $limit): array\n {\n try {\n $queryEmbedding = $this->ollamaService->getEmbedding($query);\n\n \/\/ Use dokumentation_chunks collection\n return $this->qdrantService->search($queryEmbedding, 'dokumentation_chunks', $limit);\n } catch (\\RuntimeException $e) {\n throw new \\RuntimeException('Suche konnte nicht ausgeführt werden: ' . $e->getMessage(), 0, $e);\n }\n }\n\n \/**\n * Get database connection\n *\/\n private function getDatabase(): \\PDO\n {\n $credentials = $this->loadCredentials();\n\n return new \\PDO(\n 'mysql:host=localhost;dbname=ki_content;charset=utf8mb4',\n 'root',\n $credentials['db_password'],\n [\\PDO::ATTR_ERRMODE => \\PDO::ERRMODE_EXCEPTION]\n );\n }\n\n \/**\n * Load credentials from file\n *\/\n private function loadCredentials(): array\n {\n $file = '\/var\/www\/docs\/credentials\/credentials.md';\n $content = file_get_contents($file);\n\n $password = '';\n foreach (explode(\"\\n\", $content) as $line) {\n if (str_contains($line, 'MariaDB') && str_contains($line, 'root')) {\n $parts = explode('|', $line);\n if (count($parts) >= 4) {\n $password = trim($parts[3]);\n break;\n }\n }\n }\n\n return ['db_password' => $password];\n }\n}\n",
"structuredPatch": [
{
"oldStart": 134,
"oldLines": 39,
"newStart": 134,
"newLines": 10,
"lines": [
" }",
" ",
" \/**",
"- * Get database connection",
"+ * Get database connection for dokumentation tables (ki_dev)",
" *\/",
" private function getDatabase(): \\PDO",
" {",
"- $credentials = $this->loadCredentials();",
"-",
"- return new \\PDO(",
"- 'mysql:host=localhost;dbname=ki_content;charset=utf8mb4',",
"- 'root',",
"- $credentials['db_password'],",
"- [\\PDO::ATTR_ERRMODE => \\PDO::ERRMODE_EXCEPTION]",
"- );",
"+ return \\Infrastructure\\Config\\DatabaseFactory::dev();",
" }",
"-",
"- \/**",
"- * Load credentials from file",
"- *\/",
"- private function loadCredentials(): array",
"- {",
"- $file = '\/var\/www\/docs\/credentials\/credentials.md';",
"- $content = file_get_contents($file);",
"-",
"- $password = '';",
"- foreach (explode(\"\\n\", $content) as $line) {",
"- if (str_contains($line, 'MariaDB') && str_contains($line, 'root')) {",
"- $parts = explode('|', $line);",
"- if (count($parts) >= 4) {",
"- $password = trim($parts[3]);",
"- break;",
"- }",
"- }",
"- }",
"-",
"- return ['db_password' => $password];",
"- }",
" }"
]
}
],
"userModified": false,
"replaceAll": false
}
}