message.php
- Pfad:
src/View/chat/partials/message.php - Namespace: -
- Zeilen: 84 | Größe: 3,923 Bytes
- Geändert: 2025-12-28 01:20:25 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 100
- Dependencies: 100 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Versionen 4
-
v4
2025-12-28 01:20 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v3
2025-12-27 13:56 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v2
2025-12-27 13:55 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v1
2025-12-27 13:55 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
Code
<?php
/**
* Chat Message Partial
*
* @var array $message Message data from database
* @var \Infrastructure\Formatting\ChatMessageFormatter|null $formatter Optional formatter instance
*/
$role = $message['role'] ?? 'user';
$content = $message['content'] ?? '';
// Fix #404: Handle double-encoded JSON sources
$sources = [];
if (!empty($message['sources'])) {
$decoded = json_decode($message['sources'], true);
if (is_array($decoded)) {
foreach ($decoded as $item) {
// Handle double-encoded JSON (each item is a JSON string)
$sources[] = is_string($item) ? json_decode($item, true) : $item;
}
}
}
// Fix #402: Use formatter for assistant messages
$formattedContent = $content;
if ($role === 'assistant') {
if (isset($formatter)) {
$formattedContent = $formatter->formatAnswer($content);
} else {
// Fallback: inline formatting for control tokens
$formattedContent = preg_replace('/<\/?end_of_turn>/i', '', $content);
$formattedContent = preg_replace('/<\/?start_of_turn>/i', '', $formattedContent);
$formattedContent = nl2br(htmlspecialchars($formattedContent));
}
}
$inputTokens = (int) ($message['tokens_input'] ?? 0);
$outputTokens = (int) ($message['tokens_output'] ?? 0);
$model = $message['model'] ?? 'claude-opus-4-5-20251101';
$isOllama = str_starts_with($model, 'ollama:');
$modelLabel = $isOllama ? substr($model, 7) : $model;
$cost = ($inputTokens * 0.000015) + ($outputTokens * 0.000075);
?>
<div class="chat-msg chat-msg--<?= htmlspecialchars($role) ?>">
<div class="chat-msg__content">
<?php if ($role === 'user'): ?>
<?= htmlspecialchars($content) ?>
<?php else: ?>
<?= $formattedContent ?>
<?php if (!empty($sources)): ?>
<?php $uniqueId = 'sources-' . ($message['id'] ?? uniqid()); ?>
<div class="chat-sources chat-sources--collapsed" id="<?= $uniqueId ?>">
<button type="button" class="chat-sources__toggle" onclick="toggleSources('<?= $uniqueId ?>')">
<span class="chat-sources__count"><?= count($sources) ?> Quelle<?= count($sources) > 1 ? 'n' : '' ?></span>
<span class="chat-sources__arrow">▼</span>
</button>
<div class="chat-sources__list">
<?php foreach ($sources as $source): ?>
<div class="source-item">
<div class="source-item__header">
<span class="source-item__title"><?= htmlspecialchars($source['title'] ?? 'Unbekannt') ?></span>
<span class="source-item__score"><?= round(($source['score'] ?? 0) * \Domain\Constants::PERCENT_FULL) ?>%</span>
</div>
<?php if (!empty($source['content'])): ?>
<div class="source-item__content">"<?= htmlspecialchars(mb_substr($source['content'], 0, 200)) ?><?= mb_strlen($source['content']) > 200 ? '...' : '' ?>"</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<div class="chat-msg__meta">
<span><?= htmlspecialchars($modelLabel) ?></span>
<?php if (!$isOllama && ($inputTokens > 0 || $outputTokens > 0)): ?>
<span class="chat-msg__tokens">↓<?= number_format($inputTokens) ?> ↑<?= number_format($outputTokens) ?></span>
<span class="chat-msg__cost">~$<?= number_format($cost, 4) ?></span>
<?php elseif ($isOllama): ?>
<span class="chat-msg__local">lokal</span>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</div>