Protokoll #15980

ID15980
Zeitstempel2025-12-25 16:58:22.865452
Clientroot
IP145.224.72.140
Modellclaude-sonnet-4-20250514
Statuscompleted
Tokens4,428 (Input: 670, Output: 3,758)
Dauer126 ms
Request-Zeit2025-12-25 16:58:22.865452
Response-Zeit2025-12-25 16:58:22.991843

Request

{
    "event": "PreToolUse",
    "tool_name": "Edit",
    "tool_input": {
        "file_path": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Infrastructure\/Docs\/ChunkAnalysisService.php",
        "old_string": "    \/**\n     * Analyzes a single chunk.\n     *\n     * @return array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>}\n     *\/\n    public function analyzeChunk(int $chunkId): array\n    {\n        $chunk = $this->getChunk($chunkId);\n\n        if ($chunk === null) {\n            throw new RuntimeException(\"Chunk #{$chunkId} not found\");\n        }\n\n        \/\/ Mark as processing\n        $this->updateStatus($chunkId, 'processing');\n\n        try {\n            \/\/ Get document context\n            $docContext = $this->getDocumentContext((int) $chunk['dokumentation_id']);\n\n            \/\/ Perform analysis\n            $analysis = $this->performAnalysis($chunk, $docContext);\n\n            \/\/ Store results\n            $this->storeAnalysisResults($chunkId, $analysis);\n\n            return $analysis;\n        } catch (RuntimeException $e) {\n            $this->updateStatus($chunkId, 'failed', $e->getMessage());\n\n            throw $e;\n        }\n    }",
        "new_string": "    \/**\n     * Analyzes a single chunk.\n     *\n     * @return array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>}\n     *\/\n    public function analyzeChunk(int $chunkId): array\n    {\n        return $this->process($chunkId);\n    }\n\n    \/**\n     * Processes a single chunk (implements ChunkProcessorInterface).\n     *\n     * @return array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>}\n     *\/\n    public function process(int $chunkId): array\n    {\n        $chunk = $this->repository->findById($chunkId);\n\n        if ($chunk === null) {\n            throw new RuntimeException(\"Chunk #{$chunkId} not found\");\n        }\n\n        \/\/ Mark as processing\n        $this->repository->updateStatus($chunkId, 'processing');\n\n        try {\n            \/\/ Get document context\n            $docContext = $this->repository->getDocumentContext((int) $chunk['dokumentation_id']);\n\n            \/\/ Perform analysis via ChunkAnalyzer\n            $analysis = $this->analyzer->analyze($chunk, $docContext);\n\n            \/\/ Store results\n            $this->repository->storeAnalysisResults($chunkId, $analysis, self::TAXONOMY_MODEL);\n\n            return $analysis;\n        } catch (RuntimeException $e) {\n            $this->repository->updateStatus($chunkId, 'failed', $e->getMessage());\n\n            throw $e;\n        }\n    }"
    }
}

Response

{
    "tool_response": {
        "filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/Infrastructure\/Docs\/ChunkAnalysisService.php",
        "oldString": "    \/**\n     * Analyzes a single chunk.\n     *\n     * @return array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>}\n     *\/\n    public function analyzeChunk(int $chunkId): array\n    {\n        $chunk = $this->getChunk($chunkId);\n\n        if ($chunk === null) {\n            throw new RuntimeException(\"Chunk #{$chunkId} not found\");\n        }\n\n        \/\/ Mark as processing\n        $this->updateStatus($chunkId, 'processing');\n\n        try {\n            \/\/ Get document context\n            $docContext = $this->getDocumentContext((int) $chunk['dokumentation_id']);\n\n            \/\/ Perform analysis\n            $analysis = $this->performAnalysis($chunk, $docContext);\n\n            \/\/ Store results\n            $this->storeAnalysisResults($chunkId, $analysis);\n\n            return $analysis;\n        } catch (RuntimeException $e) {\n            $this->updateStatus($chunkId, 'failed', $e->getMessage());\n\n            throw $e;\n        }\n    }",
        "newString": "    \/**\n     * Analyzes a single chunk.\n     *\n     * @return array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>}\n     *\/\n    public function analyzeChunk(int $chunkId): array\n    {\n        return $this->process($chunkId);\n    }\n\n    \/**\n     * Processes a single chunk (implements ChunkProcessorInterface).\n     *\n     * @return array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>}\n     *\/\n    public function process(int $chunkId): array\n    {\n        $chunk = $this->repository->findById($chunkId);\n\n        if ($chunk === null) {\n            throw new RuntimeException(\"Chunk #{$chunkId} not found\");\n        }\n\n        \/\/ Mark as processing\n        $this->repository->updateStatus($chunkId, 'processing');\n\n        try {\n            \/\/ Get document context\n            $docContext = $this->repository->getDocumentContext((int) $chunk['dokumentation_id']);\n\n            \/\/ Perform analysis via ChunkAnalyzer\n            $analysis = $this->analyzer->analyze($chunk, $docContext);\n\n            \/\/ Store results\n            $this->repository->storeAnalysisResults($chunkId, $analysis, self::TAXONOMY_MODEL);\n\n            return $analysis;\n        } catch (RuntimeException $e) {\n            $this->repository->updateStatus($chunkId, 'failed', $e->getMessage());\n\n            throw $e;\n        }\n    }",
        "originalFile": "<?php\n\ndeclare(strict_types=1);\n\nnamespace Infrastructure\\Docs;\n\n\/\/ @responsibility: Orchestriert Chunk-Analyse (koordiniert ChunkAnalyzer + ChunkRepository)\n\nuse RuntimeException;\n\nfinal class ChunkAnalysisService implements ChunkProcessorInterface\n{\n    private const string TAXONOMY_MODEL = 'gemma3:4b-it-qat';\n    private const int BATCH_SIZE = 10;\n\n    public function __construct(\n        private ChunkRepository $repository,\n        private ChunkAnalyzer $analyzer\n    ) {\n    }\n\n    \/**\n     * Analyzes a single chunk.\n     *\n     * @return array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>}\n     *\/\n    public function analyzeChunk(int $chunkId): array\n    {\n        $chunk = $this->getChunk($chunkId);\n\n        if ($chunk === null) {\n            throw new RuntimeException(\"Chunk #{$chunkId} not found\");\n        }\n\n        \/\/ Mark as processing\n        $this->updateStatus($chunkId, 'processing');\n\n        try {\n            \/\/ Get document context\n            $docContext = $this->getDocumentContext((int) $chunk['dokumentation_id']);\n\n            \/\/ Perform analysis\n            $analysis = $this->performAnalysis($chunk, $docContext);\n\n            \/\/ Store results\n            $this->storeAnalysisResults($chunkId, $analysis);\n\n            return $analysis;\n        } catch (RuntimeException $e) {\n            $this->updateStatus($chunkId, 'failed', $e->getMessage());\n\n            throw $e;\n        }\n    }\n\n    \/**\n     * Analyzes all pending chunks in batches.\n     *\n     * @return array{analyzed: int, failed: int, errors: array<string>}\n     *\/\n    public function analyzeAllPending(int $limit = 100): array\n    {\n        $results = ['analyzed' => 0, 'failed' => 0, 'errors' => []];\n\n        $chunks = $this->getPendingChunks($limit);\n\n        foreach ($chunks as $chunk) {\n            try {\n                $this->analyzeChunk((int) $chunk['id']);\n                $results['analyzed']++;\n\n                \/\/ Progress output\n                if ($results['analyzed'] % self::BATCH_SIZE === 0) {\n                    echo \"Analyzed {$results['analyzed']} chunks...\\n\";\n                }\n            } catch (RuntimeException $e) {\n                $results['failed']++;\n                $results['errors'][] = \"Chunk #{$chunk['id']}: \" . $e->getMessage();\n            }\n        }\n\n        return $results;\n    }\n\n    \/**\n     * Performs the actual LLM analysis.\n     *\n     * @param array<string, mixed> $chunk\n     * @param array<string, mixed> $docContext\n     * @return array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>}\n     *\/\n    private function performAnalysis(array $chunk, array $docContext): array\n    {\n        $content = $chunk['content_clean'] ?? $chunk['content'];\n        $headingPath = $this->decodeJsonArray($chunk['heading_path'] ?? null);\n\n        \/\/ Build context\n        $context = sprintf(\n            \"Dokument: %s\\nPfad: %s\\nAbschnitt: %s\\n\\nInhalt:\\n%s\",\n            $docContext['title'],\n            $docContext['path'],\n            implode(' > ', $headingPath),\n            $content\n        );\n\n        \/\/ Combined analysis prompt for efficiency\n        $prompt = $this->buildAnalysisPrompt($context);\n\n        $response = $this->callLlmWithRetry($prompt, self::TAXONOMY_MODEL);\n        $analysis = $this->parseAnalysisResponse($response);\n\n        \/\/ Fallback: If no taxonomy, derive from document path\n        if (empty($analysis['taxonomy'])) {\n            $analysis['taxonomy'] = $this->deriveTaxonomyFromPath($docContext['path']);\n        }\n\n        return $analysis;\n    }\n\n    \/**\n     * Builds the analysis prompt.\n     *\/\n    private function buildAnalysisPrompt(string $context): string\n    {\n        return <<<PROMPT\n            Analysiere den folgenden technischen Dokumentationsabschnitt und extrahiere strukturierte Informationen.\n\n            {$context}\n\n            Antworte NUR mit einem JSON-Objekt in diesem exakten Format (keine Erklärungen):\n            {\n              \"taxonomy\": [\"Hauptkategorie\", \"Unterkategorie\", \"Thema\"],\n              \"entities\": [\n                {\"name\": \"Entitätsname\", \"type\": \"TECHNOLOGY|CONCEPT|CONFIG|COMMAND|SERVICE\"}\n              ],\n              \"keywords\": [\"keyword1\", \"keyword2\", \"keyword3\"]\n            }\n\n            Regeln:\n            - taxonomy: Hierarchische Klassifikation (3 Ebenen: Bereich > Modul > Thema)\n            - entities: Wichtige Technologien, Konzepte, Konfigurationen, Befehle, Dienste\n            - keywords: 3-5 relevante Suchbegriffe\n            - Antworte NUR mit dem JSON, keine anderen Texte\n            PROMPT;\n    }\n\n    \/**\n     * Calls the LLM with retry logic.\n     *\/\n    private function callLlmWithRetry(string $prompt, string $model): string\n    {\n        $lastError = new RuntimeException('No attempts made');\n\n        for ($attempt = 1; $attempt <= self::MAX_RETRIES; $attempt++) {\n            try {\n                return $this->ollama->generate($prompt, $model);\n            } catch (RuntimeException $e) {\n                $lastError = $e;\n                if ($attempt < self::MAX_RETRIES) {\n                    usleep(500000 * $attempt); \/\/ Progressive backoff\n                }\n            }\n        }\n\n        throw new RuntimeException('LLM call failed after ' . self::MAX_RETRIES . ' attempts: ' . $lastError->getMessage());\n    }\n\n    \/**\n     * Parses the LLM response into structured data.\n     *\n     * @return array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>}\n     *\/\n    private function parseAnalysisResponse(string $response): array\n    {\n        $default = [\n            'taxonomy' => [],\n            'entities' => [],\n            'keywords' => [],\n        ];\n\n        \/\/ Extract JSON from response (handle markdown code blocks)\n        $json = $response;\n        if (preg_match('\/```(?:json)?\\s*([\\s\\S]*?)\\s*```\/', $response, $matches)) {\n            $json = $matches[1];\n        } elseif (preg_match('\/\\{[\\s\\S]*\\}\/', $response, $matches)) {\n            $json = $matches[0];\n        }\n\n        $decoded = json_decode($json, true);\n\n        if (!is_array($decoded)) {\n            return $default;\n        }\n\n        return [\n            'taxonomy' => $this->validateArray($decoded['taxonomy'] ?? [], 'string'),\n            'entities' => $this->validateEntities($decoded['entities'] ?? []),\n            'keywords' => $this->validateArray($decoded['keywords'] ?? [], 'string'),\n        ];\n    }\n\n    \/**\n     * Validates an array of strings.\n     *\n     * @param mixed $arr\n     * @return array<string>\n     *\/\n    private function validateArray(mixed $arr, string $type): array\n    {\n        if (!is_array($arr)) {\n            return [];\n        }\n\n        return array_values(array_filter($arr, static fn ($item): bool => is_string($item) && trim($item) !== ''));\n    }\n\n    \/**\n     * Validates entities array.\n     *\n     * @param mixed $entities\n     * @return array<array{name: string, type: string}>\n     *\/\n    private function validateEntities(mixed $entities): array\n    {\n        if (!is_array($entities)) {\n            return [];\n        }\n\n        $result = [];\n        foreach ($entities as $entity) {\n            if (is_array($entity) && isset($entity['name']) && is_string($entity['name'])) {\n                $result[] = [\n                    'name' => trim($entity['name']),\n                    'type' => isset($entity['type']) && is_string($entity['type']) ? strtoupper($entity['type']) : 'OTHER',\n                ];\n            }\n        }\n\n        return $result;\n    }\n\n    \/**\n     * Derives taxonomy from document path.\n     *\n     * @return array<string>\n     *\/\n    private function deriveTaxonomyFromPath(string $path): array\n    {\n        $parts = array_filter(explode('\/', trim($path, '\/')));\n\n        \/\/ Map common paths to categories\n        $mapping = [\n            'server' => 'Server',\n            'modul' => 'Module',\n            'anwendungen' => 'Anwendungen',\n            'mcp' => 'MCP-Server',\n            'ki-tasks' => 'KI-Tasks',\n        ];\n\n        $taxonomy = [];\n        foreach ($parts as $part) {\n            $taxonomy[] = $mapping[$part] ?? ucfirst($part);\n        }\n\n        return array_slice($taxonomy, 0, 3);\n    }\n\n    \/**\n     * Stores analysis results in the database.\n     *\n     * @param array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>} $analysis\n     *\/\n    private function storeAnalysisResults(int $chunkId, array $analysis): void\n    {\n        $taxonomyPath = $analysis['taxonomy'];\n        $taxonomyCategory = !empty($taxonomyPath) ? $taxonomyPath[0] : null;\n\n        $sql = \"UPDATE dokumentation_chunks SET\n                taxonomy_category = :category,\n                taxonomy_path = :taxonomy,\n                entities = :entities,\n                keywords = :keywords,\n                analysis_model = :model,\n                analysis_status = 'completed',\n                analysis_error = NULL,\n                analyzed_at = NOW()\n                WHERE id = :id\";\n\n        $stmt = $this->pdo->prepare($sql);\n        $stmt->execute([\n            'id' => $chunkId,\n            'category' => $taxonomyCategory,\n            'taxonomy' => json_encode($taxonomyPath),\n            'entities' => json_encode($analysis['entities']),\n            'keywords' => json_encode($analysis['keywords']),\n            'model' => self::TAXONOMY_MODEL,\n        ]);\n    }\n\n    \/**\n     * Updates chunk status.\n     *\/\n    private function updateStatus(int $chunkId, string $status, ?string $error = null): void\n    {\n        $sql = 'UPDATE dokumentation_chunks SET analysis_status = :status, analysis_error = :error WHERE id = :id';\n        $stmt = $this->pdo->prepare($sql);\n        $stmt->execute(['id' => $chunkId, 'status' => $status, 'error' => $error]);\n    }\n\n    \/**\n     * Gets a chunk by ID.\n     *\n     * @return array<string, mixed>|null\n     *\/\n    private fu... [TRUNCATED-2d33c68f918813b6]",
        "structuredPatch": [
            {
                "oldStart": 26,
                "oldLines": 28,
                "newStart": 26,
                "newLines": 38,
                "lines": [
                    "      *\/",
                    "     public function analyzeChunk(int $chunkId): array",
                    "     {",
                    "-        $chunk = $this->getChunk($chunkId);",
                    "+        return $this->process($chunkId);",
                    "+    }",
                    " ",
                    "+    \/**",
                    "+     * Processes a single chunk (implements ChunkProcessorInterface).",
                    "+     *",
                    "+     * @return array{taxonomy: array<string>, entities: array<array{name: string, type: string}>, keywords: array<string>}",
                    "+     *\/",
                    "+    public function process(int $chunkId): array",
                    "+    {",
                    "+        $chunk = $this->repository->findById($chunkId);",
                    "+",
                    "         if ($chunk === null) {",
                    "             throw new RuntimeException(\"Chunk #{$chunkId} not found\");",
                    "         }",
                    " ",
                    "         \/\/ Mark as processing",
                    "-        $this->updateStatus($chunkId, 'processing');",
                    "+        $this->repository->updateStatus($chunkId, 'processing');",
                    " ",
                    "         try {",
                    "             \/\/ Get document context",
                    "-            $docContext = $this->getDocumentContext((int) $chunk['dokumentation_id']);",
                    "+            $docContext = $this->repository->getDocumentContext((int) $chunk['dokumentation_id']);",
                    " ",
                    "-            \/\/ Perform analysis",
                    "-            $analysis = $this->performAnalysis($chunk, $docContext);",
                    "+            \/\/ Perform analysis via ChunkAnalyzer",
                    "+            $analysis = $this->analyzer->analyze($chunk, $docContext);",
                    " ",
                    "             \/\/ Store results",
                    "-            $this->storeAnalysisResults($chunkId, $analysis);",
                    "+            $this->repository->storeAnalysisResults($chunkId, $analysis, self::TAXONOMY_MODEL);",
                    " ",
                    "             return $analysis;",
                    "         } catch (RuntimeException $e) {",
                    "-            $this->updateStatus($chunkId, 'failed', $e->getMessage());",
                    "+            $this->repository->updateStatus($chunkId, 'failed', $e->getMessage());",
                    " ",
                    "             throw $e;",
                    "         }"
                ]
            }
        ],
        "userModified": false,
        "replaceAll": false
    }
}
← Vorheriger Zur Liste Nächster →