{
"tool_response": {
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Controller\/Api\/DocsController.php",
"oldString": " \/**\n * POST \/api\/v1\/docs\/chat\n * Chat mit Dokumentation (RAG).\n *\/\n public function chat(): void\n {\n try {\n $input = $this->getJsonInput();\n $question = trim($input['question'] ?? '');\n $model = $input['model'] ?? 'mistral';\n $limit = (int) ($input['limit'] ?? 5);\n\n if ($question === '') {\n $this->json(['success' => false, 'error' => 'Keine Frage angegeben'], 400);\n\n return;\n }\n\n \/\/ Get relevant chunks via semantic search\n $chunks = $this->syncService->search($question, $limit);\n\n if (empty($chunks)) {\n $this->json([\n 'success' => true,\n 'data' => [\n 'answer' => 'Leider konnte ich keine relevanten Informationen in der Dokumentation finden.',\n 'sources' => [],\n ],\n ]);\n\n return;\n }\n\n \/\/ Build context from chunks\n $context = $this->buildContext($chunks);\n\n \/\/ Generate answer using Ollama\n $ollama = new \\Infrastructure\\AI\\OllamaService();\n $prompt = $this->buildChatPrompt($question, $context);\n $answer = $ollama->generate($prompt, $model, 0.3);\n\n $sources = array_map(static fn ($chunk) => [\n 'id' => $chunk['doc_id'],\n 'path' => $chunk['path'],\n 'title' => $chunk['title'],\n 'score' => round($chunk['score'], 3),\n ], $chunks);\n\n $this->json([\n 'success' => true,\n 'data' => [\n 'answer' => $answer,\n 'sources' => $sources,\n ],\n ]);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n \/**\n * Build context from chunks.\n *\/\n private function buildContext(array $chunks): string\n {\n $parts = [];\n\n foreach ($chunks as $chunk) {\n $part = \"## {$chunk['title']}\\n\";\n $part .= \"Pfad: {$chunk['path']}\\n\";\n $part .= $chunk['content'];\n $parts[] = $part;\n }\n\n return implode(\"\\n\\n---\\n\\n\", $parts);\n }\n\n \/**\n * Build chat prompt.\n *\/\n private function buildChatPrompt(string $question, string $context): string\n {\n return <<<PROMPT\n Du bist ein Dokumentations-Assistent. Beantworte die Frage basierend auf dem bereitgestellten Kontext.\n\n KONTEXT:\n {$context}\n\n FRAGE:\n {$question}\n\n ANLEITUNG:\n - Antworte auf Deutsch\n - Sei präzise und hilfreich\n - Wenn der Kontext die Frage nicht beantwortet, sage das ehrlich\n - Verweise auf die relevanten Abschnitte der Dokumentation\n PROMPT;\n }\n}",
"newString": " \/**\n * POST \/api\/v1\/docs\/chat\n * Chat mit Dokumentation (RAG).\n *\/\n public function chat(): void\n {\n try {\n $input = $this->getJsonInput();\n $question = trim($input['question'] ?? '');\n $model = $input['model'] ?? 'mistral';\n $limit = (int) ($input['limit'] ?? 5);\n\n if ($question === '') {\n $this->json(['success' => false, 'error' => 'Keine Frage angegeben'], 400);\n\n return;\n }\n\n $result = $this->chatUseCase->execute($question, $model, $limit);\n\n $this->json([\n 'success' => true,\n 'data' => $result,\n ]);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n}",
"originalFile": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Controller\\Api;\n\nuse Framework\\Controller;\nuse Infrastructure\\Docs\\ChunkSyncService;\nuse Infrastructure\\Persistence\\DokumentationRepository;\nuse UseCases\\Docs\\DocumentationChatUseCase;\n\n\/**\n * DocsController - REST API für Dokumentationsverwaltung\n *\n * Endpoints:\n * - GET \/api\/v1\/docs - Liste aller Dokumente\n * - GET \/api\/v1\/docs\/{id} - Einzelnes Dokument\n * - GET \/api\/v1\/docs\/path\/{path} - Dokument nach Pfad\n * - POST \/api\/v1\/docs - Dokument erstellen\n * - PUT \/api\/v1\/docs\/{id} - Dokument aktualisieren\n * - DELETE \/api\/v1\/docs\/{id} - Dokument löschen\n * - GET \/api\/v1\/docs\/search - Semantic Search\n * - GET \/api\/v1\/docs\/hierarchy - Dokumentbaum\n * - POST \/api\/v1\/docs\/chat - Chat mit Dokumentation\n *\/\nclass DocsController extends Controller\n{\n private DokumentationRepository $repository;\n private ChunkSyncService $syncService;\n private DocumentationChatUseCase $chatUseCase;\n\n public function __construct(\n ?DokumentationRepository $repository = null,\n ?ChunkSyncService $syncService = null,\n ?DocumentationChatUseCase $chatUseCase = null\n ) {\n $this->repository = $repository ?? new DokumentationRepository();\n $this->syncService = $syncService ?? new ChunkSyncService();\n $this->chatUseCase = $chatUseCase ?? new DocumentationChatUseCase();\n }\n\n \/**\n * GET \/api\/v1\/docs\n * Liste aller Dokumente mit optionalen Filtern.\n *\/\n public function index(): void\n {\n try {\n $status = $this->getString('status');\n $parentId = $this->getInt('parent_id');\n $search = $this->getString('search');\n $limit = $this->getLimit(100, 50);\n $offset = $this->getInt('offset');\n\n $docs = $this->repository->findAll(\n status: $status ?: null,\n parentId: $parentId > 0 ? $parentId : null,\n search: $search ?: null,\n limit: $limit,\n offset: $offset\n );\n\n $total = $this->repository->count(\n status: $status ?: null,\n parentId: $parentId > 0 ? $parentId : null,\n search: $search ?: null\n );\n\n $this->json([\n 'success' => true,\n 'data' => $docs,\n 'meta' => [\n 'total' => $total,\n 'limit' => $limit,\n 'offset' => $offset,\n ],\n ]);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n \/**\n * GET \/api\/v1\/docs\/{id}\n * Einzelnes Dokument mit optionalen Kindern und Breadcrumb.\n *\/\n public function show(string $id): void\n {\n try {\n $includeChildren = $this->getString('include_children') === '1';\n $includeBreadcrumb = $this->getString('include_breadcrumb') === '1';\n\n $doc = $this->repository->findById((int) $id);\n\n if ($doc === null) {\n $this->json(['success' => false, 'error' => 'Dokument nicht gefunden'], 404);\n\n return;\n }\n\n $response = [\n 'success' => true,\n 'data' => $doc,\n ];\n\n if ($includeChildren) {\n $response['children'] = $this->repository->findChildren((int) $id);\n }\n\n if ($includeBreadcrumb) {\n $response['breadcrumb'] = $this->repository->getBreadcrumb((int) $id);\n }\n\n $this->json($response);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n \/**\n * GET \/api\/v1\/docs\/path\/{path}\n * Dokument nach Pfad.\n *\/\n public function showByPath(string $path): void\n {\n try {\n $fullPath = '\/' . ltrim($path, '\/');\n $doc = $this->repository->findByPath($fullPath);\n\n if ($doc === null) {\n $this->json(['success' => false, 'error' => 'Dokument nicht gefunden'], 404);\n\n return;\n }\n\n $this->json([\n 'success' => true,\n 'data' => $doc,\n ]);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n \/**\n * POST \/api\/v1\/docs\n * Neues Dokument erstellen.\n *\/\n public function store(): void\n {\n try {\n $input = $this->getJsonInput();\n\n $required = ['title', 'slug'];\n foreach ($required as $field) {\n if (empty($input[$field])) {\n $this->json(['success' => false, 'error' => \"Feld '$field' ist erforderlich\"], 400);\n\n return;\n }\n }\n\n $docId = $this->repository->create([\n 'title' => trim($input['title']),\n 'slug' => trim($input['slug']),\n 'content' => $input['content'] ?? '',\n 'description' => $input['description'] ?? null,\n 'parent_id' => $input['parent_id'] ?? null,\n 'status' => $input['status'] ?? 'draft',\n 'sort_order' => $input['sort_order'] ?? 0,\n ]);\n\n $doc = $this->repository->findById($docId);\n\n $this->json([\n 'success' => true,\n 'data' => $doc,\n 'message' => 'Dokument erstellt',\n ], 201);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n \/**\n * PUT \/api\/v1\/docs\/{id}\n * Dokument aktualisieren.\n *\/\n public function update(string $id): void\n {\n try {\n $doc = $this->repository->findById((int) $id);\n\n if ($doc === null) {\n $this->json(['success' => false, 'error' => 'Dokument nicht gefunden'], 404);\n\n return;\n }\n\n $input = $this->getJsonInput();\n\n $this->repository->update((int) $id, [\n 'title' => $input['title'] ?? $doc['title'],\n 'content' => $input['content'] ?? $doc['content'],\n 'description' => $input['description'] ?? $doc['description'],\n 'status' => $input['status'] ?? $doc['status'],\n ]);\n\n $updated = $this->repository->findById((int) $id);\n\n $this->json([\n 'success' => true,\n 'data' => $updated,\n 'message' => 'Dokument aktualisiert',\n ]);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n \/**\n * DELETE \/api\/v1\/docs\/{id}\n * Dokument löschen.\n *\/\n public function destroy(string $id): void\n {\n try {\n $doc = $this->repository->findById((int) $id);\n\n if ($doc === null) {\n $this->json(['success' => false, 'error' => 'Dokument nicht gefunden'], 404);\n\n return;\n }\n\n \/\/ Check for children\n $children = $this->repository->findChildren((int) $id);\n if (!empty($children)) {\n $this->json([\n 'success' => false,\n 'error' => 'Dokument hat Unterdokumente. Lösche diese zuerst.',\n ], 400);\n\n return;\n }\n\n $this->repository->delete((int) $id);\n\n $this->json([\n 'success' => true,\n 'message' => 'Dokument gelöscht',\n ]);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n \/**\n * GET \/api\/v1\/docs\/search\n * Semantic search über Dokumentation.\n *\/\n public function search(): void\n {\n try {\n $query = $this->getString('q');\n $limit = $this->getInt('limit', 5);\n $category = $this->getString('category');\n\n if ($query === '') {\n $this->json(['success' => false, 'error' => 'Keine Suchanfrage'], 400);\n\n return;\n }\n\n if ($category !== '') {\n $results = $this->syncService->searchByTaxonomy($query, $category, $limit);\n } else {\n $results = $this->syncService->search($query, $limit);\n }\n\n $this->json([\n 'success' => true,\n 'data' => $results,\n 'meta' => [\n 'query' => $query,\n 'limit' => $limit,\n 'count' => count($results),\n ],\n ]);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n \/**\n * GET \/api\/v1\/docs\/hierarchy\n * Vollständiger Dokumentationsbaum.\n *\/\n public function hierarchy(): void\n {\n try {\n $tree = $this->repository->getTree();\n\n $this->json([\n 'success' => true,\n 'data' => $tree,\n ]);\n } catch (\\Exception $e) {\n $this->jsonError($e->getMessage());\n }\n }\n\n \/**\n * POST \/api\/v1\/docs\/chat\n * Chat mit Dokumentation (RAG).\n *\/\n public function chat(): void\n {\n try {\n $input = $this->getJsonInput();\n $question = trim($input['question'] ?? '');\n $model = $input['model'] ?? 'mistral';\n $limit = (int) ($input['limit'] ?? 5);\n\n if ($question === '') {\n $this->json(['success' => false, 'error' => 'Keine Frage angegeben'], 400);\n\n return;\n }\n\n \/\/ Get relevant chunks via semantic search\n $chunks = $this->syncService->search($question, $limit);\n\n if (empty($chunks)) {\n $this->json([\n 'success' => true,\n 'data' => [\n ... [TRUNCATED-f280bd491577e670]",
"structuredPatch": [
{
"oldStart": 328,
"oldLines": 84,
"newStart": 328,
"newLines": 14,
"lines": [
" return;",
" }",
" ",
"- \/\/ Get relevant chunks via semantic search",
"- $chunks = $this->syncService->search($question, $limit);",
"+ $result = $this->chatUseCase->execute($question, $model, $limit);",
" ",
"- if (empty($chunks)) {",
"- $this->json([",
"- 'success' => true,",
"- 'data' => [",
"- 'answer' => 'Leider konnte ich keine relevanten Informationen in der Dokumentation finden.',",
"- 'sources' => [],",
"- ],",
"- ]);",
"-",
"- return;",
"- }",
"-",
"- \/\/ Build context from chunks",
"- $context = $this->buildContext($chunks);",
"-",
"- \/\/ Generate answer using Ollama",
"- $ollama = new \\Infrastructure\\AI\\OllamaService();",
"- $prompt = $this->buildChatPrompt($question, $context);",
"- $answer = $ollama->generate($prompt, $model, 0.3);",
"-",
"- $sources = array_map(static fn ($chunk) => [",
"- 'id' => $chunk['doc_id'],",
"- 'path' => $chunk['path'],",
"- 'title' => $chunk['title'],",
"- 'score' => round($chunk['score'], 3),",
"- ], $chunks);",
"-",
" $this->json([",
" 'success' => true,",
"- 'data' => [",
"- 'answer' => $answer,",
"- 'sources' => $sources,",
"- ],",
"+ 'data' => $result,",
" ]);",
" } catch (\\Exception $e) {",
" $this->jsonError($e->getMessage());",
" }",
" }",
"-",
"- \/**",
"- * Build context from chunks.",
"- *\/",
"- private function buildContext(array $chunks): string",
"- {",
"- $parts = [];",
"-",
"- foreach ($chunks as $chunk) {",
"- $part = \"## {$chunk['title']}\\n\";",
"- $part .= \"Pfad: {$chunk['path']}\\n\";",
"- $part .= $chunk['content'];",
"- $parts[] = $part;",
"- }",
"-",
"- return implode(\"\\n\\n---\\n\\n\", $parts);",
"- }",
"-",
"- \/**",
"- * Build chat prompt.",
"- *\/",
"- private function buildChatPrompt(string $question, string $context): string",
"- {",
"- return <<<PROMPT",
"- Du bist ein Dokumentations-Assistent. Beantworte die Frage basierend auf dem bereitgestellten Kontext.",
"-",
"- KONTEXT:",
"- {$context}",
"-",
"- FRAGE:",
"- {$question}",
"-",
"- ANLEITUNG:",
"- - Antworte auf Deutsch",
"- - Sei präzise und hilfreich",
"- - Wenn der Kontext die Frage nicht beantwortet, sage das ehrlich",
"- - Verweise auf die relevanten Abschnitte der Dokumentation",
"- PROMPT;",
"- }",
" }"
]
}
],
"userModified": false,
"replaceAll": false
}
}