<?php
// conversations.php - View and manage moved conversations
session_start();
// Handle conversation operations
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
switch ($action) {
case 'delete_conversation':
$convId = $_POST['conversation_id'] ?? '';
if ($convId && isset($_SESSION['conversations'][$convId])) {
unset($_SESSION['conversations'][$convId]);
}
break;
case 'restore_conversation':
$convId = $_POST['conversation_id'] ?? '';
if ($convId && isset($_SESSION['conversations'][$convId])) {
// Add messages back to main chat
$messages = $_SESSION['conversations'][$convId]['messages'];
foreach ($messages as $message) {
$_SESSION['chat_log'][] = $message;
}
// Remove from conversations
unset($_SESSION['conversations'][$convId]);
// Redirect to main chat
header('Location: index.php');
exit;
}
break;
}
// Redirect to prevent form resubmission
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
function h($s) { return htmlspecialchars($s ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); }
// Render markdown helper (simplified version)
function render_content($text, $isUser = false) {
if ($isUser) {
return '<div class="md-p">' . nl2br(h($text)) . '</div>';
}
// Simple markdown rendering for assistant messages
$text = str_replace(["\r\n", "\r"], "\n", $text);
$pat = '/```([a-zA-Z0-9_\-]+)?\s*\n([\s\S]*?)```/m';
$html=''; $pos=0;
while (preg_match($pat,$text,$m,PREG_OFFSET_CAPTURE,$pos)) {
$start=$m[0][1]; $len=strlen($m[0][0]); $lang=$m[1][0]??''; $code=$m[2][0]??'';
$before = substr($text,$pos,$start-$pos);
if ($before!=='') $html.='<div class="md-p">'.nl2br(h($before)).'</div>';
$html.='<div class="code-sep"></div>';
$html.='<div class="codeblock">'.($lang?'<div class="code-lang">'.h($lang).'</div>':'')
.'<pre><code>'.h($code).'</code></pre></div>';
$pos = $start+$len;
}
if ($pos < strlen($text)) {
$tail = substr($text,$pos);
if ($tail!=='') $html.='<div class="md-p">'.nl2br(h($tail)).'</div>';
}
if ($html==='') $html = '<div class="md-p">'.nl2br(h($text)).'</div>';
return $html;
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Conversations Manager</title>
<style>
:root{
--bg:#0f1115; --panel:#151823; --ink:#e8e8e8; --muted:#9aa0a6; --br:#252a36;
--accent:#2f6feb; --bot:#2a2f3a; --sep:#2b3242; --code:#0b0e14;
--danger: #ef4444; --success: #10b981;
}
*{box-sizing:border-box}
html,body{height:100%;margin:0}
body{background:var(--bg);color:var(--ink);font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial,sans-serif;display:flex;flex-direction:column}
.header{height:56px;background:var(--panel);border-bottom:1px solid var(--br);display:flex;align-items:center;justify-content:space-between;padding:0 16px}
.title{font-weight:600;font-size:1.1rem}
.actions{display:flex;gap:8px}
.btn{background:#1b1f2a;color:var(--ink);border:1px solid var(--br);border-radius:10px;padding:8px 16px;cursor:pointer;font-size:0.9rem;text-decoration:none;display:inline-flex;align-items:center;gap:6px}
.btn:hover{border-color:var(--accent)}
.btn-danger{background:var(--danger);border-color:var(--danger);color:white}
.btn-success{background:var(--success);border-color:var(--success);color:white}
.main{flex:1;overflow:auto;padding:20px}
.container{max-width:1200px;margin:0 auto}
.conversations-grid{display:grid;gap:20px}
.conversation-card{background:var(--panel);border:1px solid var(--br);border-radius:12px;overflow:hidden}
.conversation-header{padding:16px;border-bottom:1px solid var(--br);display:flex;justify-content:space-between;align-items:center}
.conversation-title{font-weight:600;font-size:1rem}
.conversation-meta{font-size:0.85rem;color:var(--muted)}
.conversation-actions{display:flex;gap:8px}
.conversation-content{max-height:400px;overflow-y:auto;padding:16px}
.msg{display:flex;gap:10px;align-items:flex-start;margin-bottom:12px}
.msg .bubble{max-width:min(780px,92%);padding:12px 14px;border-radius:14px;border:1px solid var(--br);background:var(--bot)}
.msg.user{justify-content:flex-end}
.msg.user .bubble{background:#0e2240;border-color:#17345a}
.msg.artifact .bubble{background:linear-gradient(135deg,rgba(47,111,235,0.1) 0%,rgba(99,102,241,0.05) 100%);border-color:var(--accent)}
.badge{font-size:.75rem;color:var(--muted);margin-bottom:4px}
.md-p{margin:.4rem 0;line-height:1.45}
.code-sep{height:10px;border-top:3px double var(--sep);margin:.8rem 0}
.codeblock{border:1px solid var(--sep);border-radius:10px;background:var(--code)}
.code-lang{font-size:.8rem;color:#cbd5e1;padding:.4rem .6rem;border-bottom:1px solid var(--sep);background:#0f1521;border-top-left-radius:10px;border-top-right-radius:10px}
pre{margin:0;padding:.75rem;overflow:auto;white-space:pre;color:#e6edf3}
.empty-state{text-align:center;padding:60px 20px;color:var(--muted)}
.empty-state h3{margin-bottom:8px}
</style>
</head>
<body>
<div class="header">
<div class="title">Conversations Manager</div>
<div class="actions">
<a href="index.php" class="btn">← Back to Chat</a>
</div>
</div>
<div class="main">
<div class="container">
<?php if (empty($_SESSION['conversations'])): ?>
<div class="empty-state">
<h3>No saved conversations</h3>
<p>Messages moved from the main chat will appear here.</p>
</div>
<?php else: ?>
<div class="conversations-grid">
<?php foreach ($_SESSION['conversations'] as $convId => $conversation): ?>
<div class="conversation-card">
<div class="conversation-header">
<div>
<div class="conversation-title"><?= h($conversation['title']) ?></div>
<div class="conversation-meta">
<?= count($conversation['messages']) ?> messages •
Created <?= date('M j, Y g:i A', $conversation['created']) ?>
</div>
</div>
<div class="conversation-actions">
<form method="post" style="margin:0;display:inline">
<input type="hidden" name="action" value="restore_conversation">
<input type="hidden" name="conversation_id" value="<?= h($convId) ?>">
<button type="submit" class="btn btn-success" onclick="return confirm('Restore this conversation to main chat?')">
Restore
</button>
</form>
<form method="post" style="margin:0;display:inline">
<input type="hidden" name="action" value="delete_conversation">
<input type="hidden" name="conversation_id" value="<?= h($convId) ?>">
<button type="submit" class="btn btn-danger" onclick="return confirm('Permanently delete this conversation?')">
Delete
</button>
</form>
</div>
</div>
<div class="conversation-content">
<?php foreach ($conversation['messages'] as $message):
$isUser = $message['role'] === 'user';
$isArtifact = $message['role'] === 'artifact';
?>
<?php if ($isArtifact): ?>
<div class="msg artifact">
<div class="bubble">
<div class="badge">Artifact</div>
<div class="content">
<strong><?= h($message['title']) ?></strong>
<div style="margin-top:8px;font-size:0.9rem;color:var(--muted)">
<?= h(substr($message['content'], 0, 200)) ?><?= strlen($message['content']) > 200 ? '...' : '' ?>
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="msg <?= $isUser ? 'user' : '' ?>">
<div class="bubble">
<div class="badge"><?= $isUser ? 'You' : 'Assistant' ?></div>
<div class="content">
<?= render_content($message['content'], $isUser) ?>
</div>
</div>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
</body>
</html>