HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /home/panomity.de/vr.panomity.com/api/ollama-context.php
<?php
/**
 * Ollama Context API für CMS4VR
 * 
 * Gibt den kombinierten Kontext für eine Scene zurück.
 * Bei leerem Szenen-Kontext wird automatisch eine Bildbeschreibung erzeugt.
 */

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
    http_response_code(405);
    echo json_encode(['error' => 'Method not allowed']);
    exit;
}

// Datenbank-Verbindung
$dbPath = __DIR__ . '/../admin/data/vrm.db';
if (!file_exists($dbPath)) {
    http_response_code(500);
    echo json_encode(['error' => 'Database not found']);
    exit;
}

$db = new SQLite3($dbPath);
require_once __DIR__ . '/ollama_context_helper.php';
ensure_panos_ollama_context_column($db);

// Parameter
$panoId = isset($_GET['pano_id']) ? (int)$_GET['pano_id'] : 0;
$language = isset($_GET['language']) ? $_GET['language'] : 'de';

if ($panoId <= 0) {
    http_response_code(400);
    echo json_encode(['error' => 'Invalid pano_id']);
    exit;
}

// Pano-Kontext abrufen (sprachabhängig: en aus panos.ollama_context, sonst pano_ai_context)
$panoContext = read_pano_context($db, $panoId, $language);

// Auto-Fallback: fehlenden Szenen-Kontext direkt erzeugen
if (trim($panoContext) === '') {
    $generated = generate_ollama_context_for_pano($db, $panoId, $language, false);
    if (!empty($generated['success']) && !empty($generated['context'])) {
        $panoContext = (string)$generated['context'];
    }
}

// Category-Kontext abrufen
$categoryContext = '';
$stmt = $db->prepare('
    SELECT cp.ollama_context 
    FROM pano_category_connections pcc
    JOIN category_phrases cp ON cp.categories_id = pcc.category_id
    JOIN languages l ON l.id = cp.languages_id
    WHERE pcc.pano_id = :pano_id AND l.short = :language
    LIMIT 1
');
$stmt->bindValue(':pano_id', $panoId, SQLITE3_INTEGER);
$stmt->bindValue(':language', $language, SQLITE3_TEXT);
$result = $stmt->execute();
if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
    $categoryContext = $row['ollama_context'] ?: '';
}

// Kontexte kombinieren und Redundanzen entfernen
$combinedContext = '';
$contextParts = [];

if ($panoContext) {
    $contextParts[] = trim($panoContext);
}
if ($categoryContext) {
    $contextParts[] = trim($categoryContext);
}

// Redundanzen entfernen: Zeilen die in beiden Kontexten vorkommen nur einmal behalten
if (count($contextParts) > 1) {
    $lines1 = explode("\n", $contextParts[0]);
    $lines2 = explode("\n", $contextParts[1]);
    
    $uniqueLines = [];
    $seenLines = [];
    
    // Ersten Kontext hinzufügen
    foreach ($lines1 as $line) {
        $trimmed = trim($line);
        if ($trimmed && !isset($seenLines[strtolower($trimmed)])) {
            $uniqueLines[] = $trimmed;
            $seenLines[strtolower($trimmed)] = true;
        }
    }
    
    // Zweiten Kontext hinzufügen (nur neue Zeilen)
    foreach ($lines2 as $line) {
        $trimmed = trim($line);
        if ($trimmed && !isset($seenLines[strtolower($trimmed)])) {
            $uniqueLines[] = $trimmed;
            $seenLines[strtolower($trimmed)] = true;
        }
    }
    
    $combinedContext = implode("\n", $uniqueLines);
} else {
    $combinedContext = implode("\n", $contextParts);
}

$db->close();

echo json_encode([
    'context' => $combinedContext,
    'pano_context' => $panoContext,
    'category_context' => $categoryContext,
    'pano_id' => $panoId
]);