Spaces:
Sleeping
Sleeping
File size: 8,875 Bytes
dd5d9ad d433b55 50c8eb0 dd5d9ad cb5990d df3243b cb5990d dd5d9ad 7bf1507 50c8eb0 cb5990d 50c8eb0 7bf1507 a1a6daf c6ccfa4 7bf1507 a1a6daf dd5d9ad 7bf1507 a1a6daf 7bf1507 50c8eb0 cb5990d 50c8eb0 cb5990d 50c8eb0 cb5990d 50c8eb0 cb5990d 50c8eb0 cb5990d 50c8eb0 cb5990d 50c8eb0 cb5990d 50c8eb0 cb5990d 50c8eb0 dd5d9ad 50c8eb0 dd5d9ad a1a6daf dd5d9ad cb5990d dd5d9ad 7bf1507 dd5d9ad 7bf1507 02b74dc 7bf1507 02b74dc 7bf1507 02b74dc dd5d9ad c6ccfa4 7bf1507 c6ccfa4 cb5990d c6ccfa4 7bf1507 c6ccfa4 7bf1507 c6ccfa4 dd5d9ad 7bf1507 50c8eb0 7bf1507 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
<script lang="ts">
import { base } from "$app/paths";
import { page } from "$app/state";
import { goto } from "$app/navigation";
import CarbonCheckmark from "~icons/carbon/checkmark";
import CarbonTrashCan from "~icons/carbon/trash-can";
import CarbonClose from "~icons/carbon/close";
import CarbonEdit from "~icons/carbon/edit";
import CarbonChevronDown from "~icons/carbon/chevron-down";
import CarbonChevronRight from "~icons/carbon/chevron-right";
import type { ConvSidebar } from "$lib/types/ConvSidebar";
import { MessageRole } from "$lib/types/Message";
import EditConversationModal from "$lib/components/EditConversationModal.svelte";
import ConversationTreeGraph from "$lib/components/ConversationTreeGraph.svelte";
import { conversationTree } from "$lib/stores/conversationTree";
import { treeVisibility } from "$lib/stores/treeVisibility";
import type { TreeLayoutNode } from "$lib/utils/tree/layout";
import { buildTreeWithPositions } from "$lib/utils/tree/layout";
import { onDestroy } from "svelte";
interface Props {
conv: ConvSidebar;
readOnly?: true;
ondeleteConversation?: (id: string) => void;
oneditConversationTitle?: (payload: { id: string; title: string }) => void;
}
let { conv, readOnly, ondeleteConversation, oneditConversationTitle }: Props = $props();
let confirmDelete = $state(false);
let renameOpen = $state(false);
// Check if this is the active conversation with tree data
let isActiveWithTree = $derived(
conv.id === page.params.id && $conversationTree.conversationId === conv.id
);
// Initialize visibility for active conversation if not set
$effect(() => {
if (isActiveWithTree && $treeVisibility[conv.id.toString()] === undefined) {
// Default to visible for active conversation
treeVisibility.setVisible(conv.id.toString(), true);
}
});
let isVisible = $derived($treeVisibility[conv.id.toString()] ?? false);
let treeData = $state<{ nodes: TreeLayoutNode[]; width: number; height: number }>({
nodes: [],
width: 0,
height: 0
});
let treeUpdateTimeout: ReturnType<typeof setTimeout>;
// Update tree data when conversation or messages change
// Only update after messages are complete (have content)
// DEBOUNCED to prevent layout thrashing during streaming
$effect(() => {
if (isActiveWithTree && isVisible && $conversationTree.messages.length > 0) {
clearTimeout(treeUpdateTimeout);
treeUpdateTimeout = setTimeout(() => {
// Filter to only messages with content (streaming complete)
// For assistant messages with personas, check personaResponses
const completeMessages = $conversationTree.messages.filter(m => {
// Exclude system messages from tree visualization
if (m.from === MessageRole.System) {
return false;
}
if (m.from === MessageRole.Assistant && m.personaResponses && m.personaResponses.length > 0) {
// Check if at least one persona has content
return m.personaResponses.some(pr => pr.content && pr.content.trim().length > 0);
}
// For user messages, check main content field
return m.content && m.content.trim().length > 0;
});
if (completeMessages.length > 0) {
buildTreeWithPositions(
completeMessages,
$conversationTree.activeMessageId || undefined,
new Set($conversationTree.activePath)
).then((data) => {
treeData = data;
// Set CSS variable for sidebar width (tree width + padding)
// Allow expansion up to 800px for very wide trees
const sidebarWidth = Math.max(290, Math.min(800, data.width + 60)); // +60 for padding
document.documentElement.style.setProperty('--sidebar-width', `${sidebarWidth}px`);
}).catch((err) => {
console.error('Error building tree:', err);
});
}
}, 300); // 300ms debounce
} else {
treeData = { nodes: [], width: 0, height: 0 };
// Reset to default width if this was the active conversation
if (isActiveWithTree) {
document.documentElement.style.setProperty('--sidebar-width', '290px');
}
}
});
onDestroy(() => {
if (treeUpdateTimeout) clearTimeout(treeUpdateTimeout);
});
function handleTreeNodeClick(messageId: string, personaId?: string) {
const clickedMessage = $conversationTree.messages.find(m => m.id === messageId);
const personaParam = personaId ? `&personaId=${personaId}` : '';
if (!clickedMessage) {
console.error('Clicked message not found:', messageId);
goto(`${base}/conversation/${conv.id}?msgId=${messageId}&scrollTo=true${personaParam}`);
return;
}
const currentBranchState = $conversationTree.branchedFrom;
if (currentBranchState) {
const currentActivePath = new Set($conversationTree.activePath);
if (currentActivePath.has(messageId)) {
// Message is in active branch; preserve state
goto(`${base}/conversation/${conv.id}?msgId=${messageId}&scrollTo=true&keepBranch=true${personaParam}`);
return;
}
}
goto(`${base}/conversation/${conv.id}?msgId=${messageId}&scrollTo=true${personaParam}`);
}
</script>
<div class="flex flex-col">
<a
data-sveltekit-noscroll
onmouseleave={() => {
confirmDelete = false;
}}
onclick={(e) => {
// If clicking the active conversation, ensure tree is visible
if (isActiveWithTree && !isVisible) {
treeVisibility.setVisible(conv.id.toString(), true);
}
// Navigation happens automatically via href
}}
href="{base}/conversation/{conv.id}"
class="group flex h-[2.15rem] flex-none items-center gap-1.5 rounded-lg pl-2.5 pr-2 text-gray-600 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-700 max-sm:h-10
{conv.id === page.params.id ? 'bg-gray-100 dark:bg-gray-700' : ''}"
>
<div class="my-2 min-w-0 flex-1 truncate first-letter:uppercase">
<span>
{#if confirmDelete}
<span class="mr-1 font-semibold"> Delete? </span>
{/if}
{conv.title}
</span>
</div>
{#if !readOnly}
{#if confirmDelete}
<button
type="button"
class="flex h-5 w-5 items-center justify-center rounded md:hidden md:group-hover:flex"
title="Cancel delete action"
onclick={(e) => {
e.preventDefault();
confirmDelete = false;
}}
>
<CarbonClose class="text-xs text-gray-400 hover:text-gray-500 dark:hover:text-gray-300" />
</button>
<button
type="button"
class="flex h-5 w-5 items-center justify-center rounded md:hidden md:group-hover:flex"
title="Confirm delete action"
onclick={(e) => {
e.preventDefault();
confirmDelete = false;
ondeleteConversation?.(conv.id.toString());
}}
>
<CarbonCheckmark
class="text-xs text-gray-400 hover:text-gray-500 dark:hover:text-gray-300"
/>
</button>
{:else}
<button
type="button"
class="flex h-5 w-5 items-center justify-center rounded md:hidden md:group-hover:flex"
title={isVisible ? "Hide tree" : "Show tree"}
onclick={(e) => {
e.preventDefault();
e.stopPropagation();
if (isActiveWithTree) {
// Only toggle locally if it's the active conversation
treeVisibility.toggle(conv.id.toString());
} else {
// If inactive, navigate to it (Option A)
// This will naturally trigger the visibility effect to set it to true
goto(`${base}/conversation/${conv.id}`);
}
}}
>
{#if isActiveWithTree && isVisible}
<CarbonChevronDown class="text-xs text-gray-400 hover:text-gray-500 dark:hover:text-gray-300" />
{:else}
<CarbonChevronRight class="text-xs text-gray-400 hover:text-gray-500 dark:hover:text-gray-300" />
{/if}
</button>
<button
type="button"
class="flex h-5 w-5 items-center justify-center rounded md:hidden md:group-hover:flex"
title="Edit conversation title"
onclick={(e) => {
e.preventDefault();
renameOpen = true;
}}
>
<CarbonEdit class="text-xs text-gray-400 hover:text-gray-500 dark:hover:text-gray-300" />
</button>
<button
type="button"
class="flex h-5 w-5 items-center justify-center rounded md:hidden md:group-hover:flex"
title="Delete conversation"
onclick={(event) => {
event.preventDefault();
if (event.shiftKey) {
ondeleteConversation?.(conv.id.toString());
} else {
confirmDelete = true;
}
}}
>
<CarbonTrashCan
class="text-xs text-gray-400 hover:text-gray-500 dark:hover:text-gray-300"
/>
</button>
{/if}
{/if}
</a>
<!-- Tree view (only shown for active conversation) -->
{#if isActiveWithTree && treeData.nodes.length > 0}
<ConversationTreeGraph
{treeData}
onNodeClick={handleTreeNodeClick}
/>
{/if}
</div>
<!-- Edit title modal -->
{#if renameOpen}
<EditConversationModal
open={renameOpen}
title={conv.title}
onclose={() => (renameOpen = false)}
onsave={(payload) => {
renameOpen = false;
oneditConversationTitle?.({ id: conv.id.toString(), title: payload.title });
}}
/>
{/if}
|