feat(canvas): merged shell route with Svelte Flow graph, collapsible rail, hosted agent chat

This commit is contained in:
Benjiyaya
2026-09-06 05:15:00 +08:00
parent dd51b914a9
commit 86d99b86d9
8 changed files with 1304 additions and 93 deletions
@@ -0,0 +1,113 @@
<script lang="ts">
import type { Node, NodeProps } from '@xyflow/svelte';
import SafeMedia from '$lib/components/SafeMedia.svelte';
import Icon from '$lib/components/ui/Icon.svelte';
import { assetUrl } from '$lib/api';
import type { IconName } from '$lib/components/ui/icons';
interface EntityNodeData extends Record<string, unknown> {
canvasNodeId: number;
entityType: 'character' | 'location' | 'item' | 'scene';
title: string;
imagePath: string | null;
onOpen?: () => void;
}
type EntityNode = Node<EntityNodeData, 'entity'>;
function iconFor(entityType: EntityNodeData['entityType']): IconName {
switch (entityType) {
case 'character':
return 'sparkle';
case 'location':
return 'image';
case 'item':
return 'folder';
case 'scene':
return 'film';
}
}
let { data }: NodeProps<EntityNode> = $props();
const mediaSrc = $derived(assetUrl(data.imagePath));
</script>
<div
class="entity-node"
role="button"
tabindex="0"
onclick={() => data.onOpen?.()}
onkeydown={(e) => e.key === 'Enter' && data.onOpen?.()}
>
<header>
<Icon name={iconFor(data.entityType)} size={14} />
<span class="title" title={data.title}>{data.title}</span>
</header>
<div class="media">
{#if mediaSrc}
<SafeMedia src={mediaSrc} alt={data.title} kind="image" controls={false} />
{:else}
<span class="placeholder">{data.entityType}</span>
{/if}
</div>
</div>
<style>
.entity-node {
width: 240px;
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: 14px;
overflow: hidden;
cursor: pointer;
transition: border-color 0.15s;
}
.entity-node:hover {
border-color: #3a3a44;
}
/* Svelte Flow marks the selected node wrapper with .selected */
:global(.svelte-flow__node.selected) .entity-node {
border-color: var(--accent);
box-shadow:
0 0 0 1px var(--accent),
0 0 40px var(--accent-glow);
}
header {
display: flex;
align-items: center;
gap: 8px;
padding: 9px 12px;
color: var(--text-secondary);
}
.title {
flex: 1;
min-width: 0;
font-size: 12.5px;
font-weight: 500;
color: var(--text-primary);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.media {
height: 132px;
background: var(--bg-elevated);
display: flex;
align-items: center;
justify-content: center;
}
.media :global(img),
.media :global(video) {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
.placeholder {
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
color: var(--text-muted);
}
</style>
+8
View File
@@ -0,0 +1,8 @@
/* Canvas v2.1 scoped visual tokens (spec v2.0 §2) — canvas surfaces only.
The global --accent stays untouched for v1 pages. */
.canvas-root {
--accent: #4f46a5;
--accent-hover: #6157c9;
--accent-glow: rgba(99, 102, 241, 0.3);
--edge: #7dd3fc;
}
@@ -1,6 +1,6 @@
<script lang="ts">
interface Props {
active?: 'projects' | 'agents' | 'playground' | 'settings';
active?: 'projects' | 'agents' | 'canvas' | 'playground' | 'settings';
crumb?: string;
/** Optional status content rendered between the crumb and the nav. */
status?: import('svelte').Snippet;
@@ -24,6 +24,7 @@
</div>
{/if}
<nav class="top-nav" aria-label="Primary">
<a class="nav-link" class:active={active === 'canvas'} href="/canvas">Canvas</a>
<a class="nav-link" class:active={active === 'agents'} href="/agents">Agents</a>
<a class="nav-link" class:active={active === 'projects'} href="/projects">Projects</a>
<a class="nav-link" class:active={active === 'playground'} href="/playground">Playground</a>
@@ -9,10 +9,21 @@
onNewSandbox: () => void;
onNewProjectChat: (projectId: number) => void;
onDelete: (id: number) => void;
/** Canvas rail mode: render a 48px icon rail instead of the full list. */
collapsed?: boolean;
onToggleCollapse?: () => void;
}
let { sessions, activeId, onSelect, onNewSandbox, onNewProjectChat, onDelete }: Props =
$props();
let {
sessions,
activeId,
onSelect,
onNewSandbox,
onNewProjectChat,
onDelete,
collapsed = false,
onToggleCollapse,
}: Props = $props();
const sandbox = $derived(sessions.filter((s) => s.project_id == null));
const grouped = $derived.by(() => {
@@ -25,115 +36,164 @@
}
return [...map.values()];
});
const allSessions = $derived([...sandbox, ...grouped.flatMap((g) => g.items)]);
</script>
<aside class="sidebar">
<button type="button" class="new-chat" onclick={onNewSandbox}>
<Icon name="plus" size={14} />
New sandbox chat
</button>
{#if sessions.length === 0}
<p class="muted">No sessions yet.</p>
{/if}
{#if sandbox.length > 0}
<div class="group">
<div class="group-head">
<span class="group-title">Sandbox</span>
<span class="count">{sandbox.length}</span>
</div>
{#each sandbox as s (s.id)}
{#if collapsed}
<aside class="sidebar collapsed">
<button
type="button"
class="rail-btn"
onclick={() => onToggleCollapse?.()}
title="Expand sessions"
aria-label="Expand sessions"
>
<Icon name="drag" size={14} />
</button>
<button
type="button"
class="rail-btn"
onclick={onNewSandbox}
title="New sandbox chat"
aria-label="New sandbox chat"
>
<Icon name="plus" size={14} />
</button>
<div class="rail-dots">
{#each allSessions as s (s.id)}
<button
type="button"
class="item"
class="rail-dot"
class:active={s.id === activeId}
class:run={s.running || s.status === 'running'}
onclick={() => onSelect(s.id)}
>
<span class="dot" class:run={s.running || s.status === 'running'}></span>
<span class="title">{s.title}</span>
<span
class="del"
role="button"
tabindex="-1"
aria-label="Delete session"
onclick={(e) => {
e.stopPropagation();
onDelete(s.id);
}}
onkeydown={(e) => {
if (e.key === 'Enter') {
e.stopPropagation();
onDelete(s.id);
}
}}
>
<Icon name="trash" size={12} />
</span>
</button>
title={s.title}
aria-label={`Open ${s.title}`}
></button>
{/each}
</div>
{/if}
</aside>
{:else}
<aside class="sidebar">
{#if onToggleCollapse}
<button
type="button"
class="collapse-toggle"
onclick={() => onToggleCollapse?.()}
title="Collapse sessions"
aria-label="Collapse sessions"
>
<Icon name="drag" size={14} />
</button>
{/if}
<button type="button" class="new-chat" onclick={onNewSandbox}>
<Icon name="plus" size={14} />
New sandbox chat
</button>
{#each grouped as g (g.project?.id ?? 0)}
<div class="group">
<div class="group-head">
<span class="group-title" title={g.project?.title}>
{g.project?.title ?? `Project #${g.project?.id ?? '?'}`}
</span>
<span class="count">{g.items.length}</span>
<span
class="add-chat"
role="button"
tabindex="0"
aria-label="New chat for this project"
title="New chat for this project"
onclick={(e) => {
e.stopPropagation();
if (g.project) onNewProjectChat(g.project.id);
}}
onkeydown={(e) => {
if (e.key === 'Enter') {
{#if sessions.length === 0}
<p class="muted">No sessions yet.</p>
{/if}
{#if sandbox.length > 0}
<div class="group">
<div class="group-head">
<span class="group-title">Sandbox</span>
<span class="count">{sandbox.length}</span>
</div>
{#each sandbox as s (s.id)}
<button
type="button"
class="item"
class:active={s.id === activeId}
onclick={() => onSelect(s.id)}
>
<span class="dot" class:run={s.running || s.status === 'running'}></span>
<span class="title">{s.title}</span>
<span
class="del"
role="button"
tabindex="-1"
aria-label="Delete session"
onclick={(e) => {
e.stopPropagation();
onDelete(s.id);
}}
onkeydown={(e) => {
if (e.key === 'Enter') {
e.stopPropagation();
onDelete(s.id);
}
}}
>
<Icon name="trash" size={12} />
</span>
</button>
{/each}
</div>
{/if}
{#each grouped as g (g.project?.id ?? 0)}
<div class="group">
<div class="group-head">
<span class="group-title" title={g.project?.title}>
{g.project?.title ?? `Project #${g.project?.id ?? '?'}`}
</span>
<span class="count">{g.items.length}</span>
<span
class="add-chat"
role="button"
tabindex="0"
aria-label="New chat for this project"
title="New chat for this project"
onclick={(e) => {
e.stopPropagation();
if (g.project) onNewProjectChat(g.project.id);
}
}}
>
<Icon name="plus" size={12} />
</span>
</div>
{#each g.items as s (s.id)}
<button
type="button"
class="item"
class:active={s.id === activeId}
onclick={() => onSelect(s.id)}
>
<span class="dot" class:run={s.running || s.status === 'running'}></span>
<span class="title">{s.title}</span>
<span
class="del"
role="button"
tabindex="-1"
aria-label="Delete session"
onclick={(e) => {
e.stopPropagation();
onDelete(s.id);
}}
onkeydown={(e) => {
if (e.key === 'Enter') {
e.stopPropagation();
onDelete(s.id);
if (g.project) onNewProjectChat(g.project.id);
}
}}
>
<Icon name="trash" size={12} />
<Icon name="plus" size={12} />
</span>
</button>
{/each}
</div>
{/each}
</aside>
</div>
{#each g.items as s (s.id)}
<button
type="button"
class="item"
class:active={s.id === activeId}
onclick={() => onSelect(s.id)}
>
<span class="dot" class:run={s.running || s.status === 'running'}></span>
<span class="title">{s.title}</span>
<span
class="del"
role="button"
tabindex="-1"
aria-label="Delete session"
onclick={(e) => {
e.stopPropagation();
onDelete(s.id);
}}
onkeydown={(e) => {
if (e.key === 'Enter') {
e.stopPropagation();
onDelete(s.id);
}
}}
>
<Icon name="trash" size={12} />
</span>
</button>
{/each}
</div>
{/each}
</aside>
{/if}
<style>
.sidebar {
@@ -148,6 +208,90 @@
overflow-y: auto;
min-height: 0;
}
.sidebar.collapsed {
width: 48px;
padding: 10px 8px;
align-items: center;
gap: 8px;
}
.collapse-toggle {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 26px;
border: none;
border-radius: var(--radius-sm);
background: transparent;
color: var(--text-muted);
cursor: pointer;
margin-bottom: 4px;
}
.collapse-toggle:hover {
color: var(--text-primary);
background: var(--bg-elevated);
}
.collapse-toggle:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 1px;
}
.rail-btn {
display: flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
border: 1px dashed var(--border);
border-radius: var(--radius-sm);
background: transparent;
color: var(--text-secondary);
cursor: pointer;
}
.rail-btn:hover {
color: var(--text-primary);
border-color: var(--accent);
}
.rail-btn:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 1px;
}
.rail-dots {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
padding-top: 6px;
overflow-y: auto;
min-height: 0;
}
.rail-dot {
width: 10px;
height: 10px;
border-radius: 50%;
border: 1px solid var(--border);
background: var(--bg-elevated);
cursor: pointer;
padding: 0;
}
.rail-dot:hover {
border-color: var(--accent);
}
.rail-dot.active {
background: var(--accent);
border-color: var(--accent);
}
.rail-dot.run {
animation: rail-pulse 1.4s ease-in-out infinite;
}
@keyframes rail-pulse {
0%,
100% {
box-shadow: 0 0 0 0 var(--accent-glow);
}
50% {
box-shadow: 0 0 0 5px transparent;
}
}
.new-chat {
display: flex;
align-items: center;
@@ -0,0 +1,43 @@
<script lang="ts">
import { onMount } from 'svelte';
import { goto } from '$app/navigation';
import { agentApi, canvasApi } from '$lib/api';
let failed = $state('');
onMount(async () => {
try {
const sessions = await agentApi.listSessions();
const stored = Number(localStorage.getItem('calliope.agents.activeSession'));
const found =
sessions.find((s) => s.id === stored) ?? sessions[0] ?? (await agentApi.createSession({}));
const graph = found.project_id
? await canvasApi.ensureForProject(found.project_id)
: await canvasApi.ensureForSession(found.id);
goto(`/canvas/${graph.canvas.id}?session=${found.id}`, { replaceState: true });
} catch (err) {
failed = err instanceof Error ? err.message : 'Could not open canvas';
}
});
</script>
{#if failed}
<div class="redirect-error" role="alert">{failed}</div>
{:else}
<div class="redirecting">Opening canvas…</div>
{/if}
<style>
.redirecting,
.redirect-error {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
color: var(--text-secondary);
font-size: 14px;
}
.redirect-error {
color: #f87171;
}
</style>
+2
View File
@@ -0,0 +1,2 @@
export const ssr = false;
export const prerender = false;
@@ -0,0 +1,898 @@
<script lang="ts">
import { onMount } from 'svelte';
import { goto } from '$app/navigation';
import { page } from '$app/state';
import { toStore } from 'svelte/store';
import { createMutation, createQuery, useQueryClient } from '@tanstack/svelte-query';
import {
SvelteFlow,
Controls,
MiniMap,
Background,
BackgroundVariant,
type Node,
type Viewport,
} from '@xyflow/svelte';
import '@xyflow/svelte/dist/style.css';
import '$lib/canvas/tokens.css';
import AppHeader from '$lib/components/AppHeader.svelte';
import AgentChat from '$lib/components/agent/AgentChat.svelte';
import AgentComposer from '$lib/components/agent/AgentComposer.svelte';
import AgentSessionSidebar from '$lib/components/agent/AgentSessionSidebar.svelte';
import AgentPlanPanel from '$lib/components/agent/AgentPlanPanel.svelte';
import EntityNode from '$lib/canvas/EntityNode.svelte';
import Icon from '$lib/components/ui/Icon.svelte';
import {
agentApi,
canvasApi,
jobsApi,
playgroundApi,
projects,
workflows,
type AgentMessage,
type AgentPlan,
type AgentSession,
type AgentTask,
type Character,
type Item,
type Job,
type Location,
type Scene,
} from '$lib/api';
import type { AgentComposerPayload, WorkflowOption } from '$lib/agentComposer';
import { connectEvents } from '$lib/events';
import { toast } from '$lib/toast';
const client = useQueryClient();
const canvasId = Number(page.params.id);
if (!Number.isFinite(canvasId)) {
throw new Error('Invalid canvas id');
}
const RAIL_KEY = 'calliope.canvas.railCollapsed';
const CHAT_KEY = 'calliope.canvas.chatCollapsed';
const SESSION_KEY = 'calliope.agents.activeSession';
// ---- canvas graph ----------------------------------------------------
const canvasQuery = createQuery({
queryKey: ['canvas', canvasId],
queryFn: () => canvasApi.get(canvasId),
});
const canvas = $derived($canvasQuery.data?.canvas ?? null);
const canvasNodes = $derived($canvasQuery.data?.nodes ?? []);
const projectId = $derived(canvas?.project_id ?? null);
const assetsQuery = createQuery(
toStore(() => ({
queryKey: ['canvas-assets', projectId ?? 'none'],
queryFn: () => projects.getAssets(projectId!),
enabled: projectId != null,
})),
);
const scenesQuery = createQuery(
toStore(() => ({
queryKey: ['canvas-scenes', projectId ?? 'none'],
queryFn: () => projects.getScenes(projectId!),
enabled: projectId != null,
})),
);
function imagePathFor(
node: (typeof canvasNodes)[number],
characters: Character[],
locations: Location[],
items: Item[],
scenes: Scene[],
): string | null {
if (node.entity_type === 'character') {
const c = characters.find((x) => x.id === node.entity_id);
return c?.sheet_path ?? c?.portrait_path ?? null;
}
if (node.entity_type === 'location') {
const l = locations.find((x) => x.id === node.entity_id);
return l?.reference_image_path ?? null;
}
if (node.entity_type === 'item') {
const i = items.find((x) => x.id === node.entity_id);
return i?.reference_image_path ?? null;
}
if (node.entity_type === 'scene') {
const s = scenes.find((x) => x.id === node.entity_id);
return s?.env_image_path ?? null;
}
return null;
}
const nodeTypes = { entity: EntityNode };
// $state.raw: Svelte Flow v1 treats node arrays as immutable — it mutates
// internally, so proxies must never reach it.
let nodes = $state.raw<Node[]>([]);
let edges = $state.raw<[]>([]);
function openEntity(entityType: string, entityId: number) {
if (projectId == null) return;
if (entityType === 'scene') {
goto(`/project/${projectId}?stage=video&scene=${entityId}`);
} else {
goto(`/project/${projectId}?stage=assets`);
}
}
let syncVersion = $state(0);
let loadedGraphAt = $state(0);
$effect(() => {
// Rebuild the Svelte Flow graph whenever the persisted graph or any
// media source changes; drag positions win until the next refetch.
void syncVersion;
const graph = $canvasQuery.data;
if (!graph) {
loadedGraphAt = 0;
nodes = [];
return;
}
loadedGraphAt = Date.now();
const characters = $assetsQuery.data?.characters ?? [];
const locations = $assetsQuery.data?.locations ?? [];
const items = $assetsQuery.data?.items ?? [];
const scenes = $scenesQuery.data?.scenes ?? [];
nodes = graph.nodes.map((n) => ({
id: `cn-${n.id}`,
type: 'entity' as const,
position: { x: n.x, y: n.y },
data: {
canvasNodeId: n.id,
entityType: n.entity_type as 'character' | 'location' | 'item' | 'scene',
title: n.title ?? 'Untitled',
imagePath: imagePathFor(n, characters, locations, items, scenes),
onOpen: () => openEntity(n.entity_type!, n.entity_id!),
},
}));
});
async function persistNodePosition(node: Node) {
const canvasNodeId = (node.data as { canvasNodeId?: number }).canvasNodeId;
if (canvasNodeId == null) return;
try {
await canvasApi.patchNode(canvasId, canvasNodeId, {
x: node.position.x,
y: node.position.y,
});
} catch (err) {
toast.error(err instanceof Error ? err.message : 'Could not save position');
}
}
// ---- viewport persistence --------------------------------------------
let viewportTimeout: ReturnType<typeof setTimeout> | undefined;
let appliedStoredViewport = false;
let viewport = $state<Viewport>({ x: 0, y: 0, zoom: 1 });
function scheduleViewportSave(v: Viewport) {
clearTimeout(viewportTimeout);
viewportTimeout = setTimeout(() => {
void canvasApi
.patchCanvas(canvasId, { viewport_json: JSON.stringify(v) })
.catch(() => undefined);
}, 500);
}
$effect(() => {
const stored = canvas?.viewport_json;
if (appliedStoredViewport || !stored) return;
appliedStoredViewport = true;
try {
const parsed = JSON.parse(stored) as Viewport;
if (
typeof parsed?.x === 'number' &&
typeof parsed?.y === 'number' &&
typeof parsed?.zoom === 'number'
) {
viewport = parsed;
}
} catch {
// corrupt viewport_json — fitView default stands
}
});
// ---- sessions + chat (mirrors agents/+page.svelte) --------------------
let activeId = $state<number | null>(null);
let streaming = $state('');
let streamingReasoning = $state('');
let liveTools = $state<
{ tool: string; args?: Record<string, unknown> | null; result?: unknown; phase: 'running' | 'done' | 'error' }[]
>([]);
let livePlan = $state<AgentPlan | null>(null);
let composerDraft = $state('');
let composerNonce = $state(0);
let sessionScopeHandled = $state(false);
const sessionsQuery = createQuery({
queryKey: ['agent-sessions'],
queryFn: () => agentApi.listSessions(),
refetchInterval: 5000,
});
const sessions = $derived($sessionsQuery.data ?? []);
const activeSession = $derived(
activeId != null ? (sessions.find((s) => s.id === activeId) ?? null) : null,
);
const running = $derived(Boolean(activeSession?.running || activeSession?.status === 'running'));
const sessionQuery = createQuery(
toStore(() => ({
queryKey: ['agent-session', activeId],
queryFn: () => agentApi.getSession(activeId!),
enabled: activeId != null,
refetchInterval: () => (running ? 1500 : false),
})),
);
const messages = $derived($sessionQuery.data?.messages ?? []);
const plan = $derived(livePlan ?? ($sessionQuery.data?.plan ?? null));
const jobsQuery = createQuery(
toStore(() => ({
queryKey: ['agent-project-jobs', projectId ?? 'sandbox'],
queryFn: () => (projectId != null ? jobsApi.list(projectId) : playgroundApi.jobs()),
enabled: activeId != null,
refetchInterval: (q: { state: { data?: Job[] } }) => {
const list = q.state.data ?? [];
return list.some((j) => j.status === 'pending' || j.status === 'running')
? 2500
: false;
},
})),
);
const jobs = $derived($jobsQuery.data ?? []);
const workflowsQuery = createQuery({
queryKey: ['workflows'],
queryFn: workflows.list,
});
const enabledWorkflows = $derived<WorkflowOption[]>(
($workflowsQuery.data ?? [])
.filter((w) => w.is_enabled)
.map((w) => ({
id: w.id,
name: w.name,
kind: w.kind,
description: w.description,
is_enabled: w.is_enabled,
})),
);
// Initial session: ?session= param → stored → scope-matching default.
$effect(() => {
if (sessionScopeHandled || sessions.length === 0 || canvas == null) return;
sessionScopeHandled = true;
const paramSession = page.url.searchParams.get('session');
const pid = Number(paramSession);
if (paramSession != null && Number.isFinite(pid)) {
activeId = sessions.some((s) => s.id === pid) ? pid : null;
replaceUrlParam('session');
return;
}
const stored = Number(localStorage.getItem(SESSION_KEY));
if (Number.isFinite(stored) && sessions.some((s) => s.id === stored)) {
const s = sessions.find((x) => x.id === stored)!;
if ((s.project_id ?? null) === (canvas.project_id ?? null)) {
activeId = s.id;
return;
}
}
// Scope-matched default: project canvas → newest project session;
// sandbox canvas → newest sandbox session (or none).
if (canvas.project_id != null) {
const s = sessions.find((x) => x.project_id === canvas.project_id);
activeId = s?.id ?? null;
} else {
const s = sessions.find((x) => x.project_id == null);
activeId = s?.id ?? null;
}
});
function replaceUrlParam(key: string) {
const url = new URL(page.url);
url.searchParams.delete(key);
goto(url.pathname + (url.search || ''), {
replaceState: true,
keepFocus: true,
noScroll: true,
});
}
// Selecting a session from another scope navigates to that scope's canvas.
async function selectSession(id: number) {
const s = sessions.find((x) => x.id === id);
if (!s || !canvas) return;
const sameScope =
(s.project_id ?? null) === (canvas.project_id ?? null) ||
(canvas.project_id != null && s.project_id === canvas.project_id);
if (sameScope) {
activeId = id;
resetLiveViews();
return;
}
try {
const graph = s.project_id
? await canvasApi.ensureForProject(s.project_id)
: await canvasApi.ensureForSession(s.id);
goto(`/canvas/${graph.canvas.id}?session=${s.id}`);
} catch (err) {
toast.error(err instanceof Error ? err.message : 'Could not open that canvas');
}
}
function resetLiveViews() {
streaming = '';
streamingReasoning = '';
liveTools = [];
livePlan = null;
composerDraft = '';
composerNonce = 0;
}
function setComposerDraft(text: string) {
composerDraft = text;
composerNonce++;
}
function seedEmptySession(s: AgentSession) {
client.setQueryData(['agent-session', s.id], { ...s, messages: [], plan: null });
}
async function newSandbox() {
resetLiveViews();
try {
const s = await agentApi.createSession({});
seedEmptySession(s);
await client.invalidateQueries({ queryKey: ['agent-sessions'] });
const graph = await canvasApi.ensureForSession(s.id);
goto(`/canvas/${graph.canvas.id}?session=${s.id}`);
} catch (err) {
toast.error(err instanceof Error ? err.message : 'Could not create session');
}
}
async function newProjectChat(pid: number) {
resetLiveViews();
try {
const s = await agentApi.createSession({ project_id: pid });
seedEmptySession(s);
await client.invalidateQueries({ queryKey: ['agent-sessions'] });
if (canvas?.project_id === pid) {
activeId = s.id;
} else {
const graph = await canvasApi.ensureForProject(pid);
goto(`/canvas/${graph.canvas.id}?session=${s.id}`);
}
} catch (err) {
toast.error(err instanceof Error ? err.message : 'Could not create session');
}
}
async function removeSession(id: number) {
if (!confirm('Delete this chat and its message history?')) return;
try {
await agentApi.deleteSession(id);
if (activeId === id) activeId = null;
await client.invalidateQueries({ queryKey: ['agent-sessions'] });
toast.success('Session deleted');
} catch (err) {
toast.error(err instanceof Error ? err.message : 'Could not delete session');
}
}
const sendMutation = createMutation({
mutationFn: (payload: AgentComposerPayload) => agentApi.postMessage(activeId!, payload),
onMutate: () => {
streaming = '';
streamingReasoning = '';
liveTools = [];
livePlan = null;
if (activeId != null) {
client.setQueryData<AgentSession & { messages: AgentMessage[]; plan?: AgentPlan | null }>(
['agent-session', activeId],
(old) => (old ? { ...old, plan: null } : old),
);
}
},
onSuccess: async () => {
await client.invalidateQueries({ queryKey: ['agent-session', activeId] });
await client.invalidateQueries({ queryKey: ['agent-sessions'] });
},
onError: (err) => {
toast.error(err instanceof Error ? err.message : 'Failed to send');
},
});
async function cancelRun() {
if (activeId == null) return;
try {
await agentApi.cancel(activeId);
toast.info('Run cancelled');
} catch (err) {
toast.error(err instanceof Error ? err.message : 'Cancel failed');
}
}
async function handleSend(payload: AgentComposerPayload) {
if (activeId != null) {
$sendMutation.mutate(payload);
return;
}
try {
const s = await agentApi.createSession(canvas?.project_id ? { project_id: canvas.project_id } : {});
await client.invalidateQueries({ queryKey: ['agent-sessions'] });
activeId = s.id;
$sendMutation.mutate(payload);
} catch (err) {
toast.error(err instanceof Error ? err.message : 'Could not create session');
}
}
function upsertSession(s: AgentSession) {
client.setQueryData<AgentSession[]>(['agent-sessions'], (old) => {
const list = Array.isArray(old) ? old : [];
return list.some((x) => x.id === s.id)
? list.map((x) => (x.id === s.id ? { ...x, ...s } : x))
: [...list, s];
});
}
function appendMessage(sid: number, msg: AgentMessage) {
if (sid !== activeId) return;
client.setQueryData<AgentSession & { messages: AgentMessage[] }>(
['agent-session', sid],
(old) => {
if (!old || !Array.isArray(old.messages)) return old;
if (old.messages.some((m) => m.id === msg.id)) return old;
return { ...old, messages: [...old.messages, msg] };
},
);
}
onMount(() => {
const stop = connectEvents((ev) => {
if (ev.type === 'canvas.updated') {
if ((ev.data?.canvas_id as number | undefined) === canvasId) {
client.invalidateQueries({ queryKey: ['canvas', canvasId] });
}
} else if (ev.type === 'agent.session.updated') {
const s = ev.data?.session as AgentSession | undefined;
if (s) upsertSession(s);
} else if (ev.type === 'agent.message') {
const msg = ev.data?.message as AgentMessage | undefined;
if (msg) {
appendMessage(msg.session_id, msg);
if (msg.session_id === activeId) {
if (msg.role === 'tool') liveTools = [];
if (msg.role === 'assistant' && !msg.agent_name) {
streaming = '';
streamingReasoning = '';
}
}
}
client.invalidateQueries({ queryKey: ['agent-sessions'] });
} else if (ev.type === 'agent.token') {
if (ev.data?.session_id === activeId && !ev.data?.agent_name) {
streaming += String(ev.data?.content ?? '');
}
} else if (ev.type === 'agent.thinking') {
if (ev.data?.session_id === activeId && !ev.data?.agent_name) {
streamingReasoning += String(ev.data?.content ?? '');
}
} else if (ev.type === 'agent.tool') {
if (ev.data?.session_id !== activeId) return;
const name = String(ev.data?.tool ?? '');
const phase = String(ev.data?.phase ?? '');
if (phase === 'start') {
liveTools.push({
tool: name,
args: (ev.data?.args as Record<string, unknown>) ?? null,
phase: 'running',
});
} else if (phase === 'finish') {
const idx = [...liveTools]
.reverse()
.findIndex((t) => t.tool === name && t.phase === 'running');
if (idx >= 0) {
const real = liveTools.length - 1 - idx;
liveTools[real] = {
...liveTools[real],
result: ev.data?.result,
phase:
(ev.data?.result as { ok?: boolean } | undefined)?.ok === false
? 'error'
: 'done',
};
}
}
} else if (ev.type === 'agent.plan') {
if (ev.data?.session_id !== activeId) return;
const tasks = (ev.data?.tasks as AgentTask[] | undefined) ?? [];
livePlan = {
tasks: tasks.map((t) => ({ ...t, status: t.status ?? 'pending' })),
note: (ev.data?.note as string | null) ?? null,
};
} else if (ev.type === 'agent.task') {
if (ev.data?.session_id !== activeId) return;
const index = ev.data?.index;
const status = ev.data?.status as AgentTask['status'] | undefined;
if (typeof index === 'number' && status) {
livePlan = livePlan
? {
...livePlan,
tasks: livePlan.tasks.map((t, i) =>
i === index ? { ...t, status } : t,
),
}
: livePlan;
}
} else if (
ev.type === 'story.ready' ||
ev.type === 'job.created' ||
ev.type === 'job.started' ||
ev.type === 'job.completed' ||
ev.type === 'job.failed'
) {
client.invalidateQueries({ queryKey: ['agent-sessions'] });
client.invalidateQueries({ queryKey: ['agent-project-jobs'] });
client.invalidateQueries({ queryKey: ['playground-jobs'] });
// New story entities need seeding; finished jobs refresh media.
client.invalidateQueries({ queryKey: ['canvas', canvasId] });
client.invalidateQueries({ queryKey: ['canvas-assets'] });
client.invalidateQueries({ queryKey: ['canvas-scenes'] });
}
});
return stop;
});
// ---- collapsible panels ------------------------------------------------
let railCollapsed = $state(
typeof localStorage !== 'undefined' && localStorage.getItem(RAIL_KEY) === '1',
);
let chatCollapsed = $state(
typeof localStorage !== 'undefined' && localStorage.getItem(CHAT_KEY) === '1',
);
function toggleRail() {
railCollapsed = !railCollapsed;
localStorage.setItem(RAIL_KEY, railCollapsed ? '1' : '0');
}
function toggleChat() {
chatCollapsed = !chatCollapsed;
localStorage.setItem(CHAT_KEY, chatCollapsed ? '1' : '0');
}
// ---- title card ---------------------------------------------------------
let titleDraft = $state('');
let titleEditing = $state(false);
let savedFlash = $state(false);
$effect(() => {
if (!titleEditing) titleDraft = canvas?.title ?? '';
});
async function saveTitle() {
const next = titleDraft.trim();
titleEditing = false;
if (!canvas || !next || next === canvas.title) return;
try {
await canvasApi.patchCanvas(canvasId, { title: next });
await client.invalidateQueries({ queryKey: ['canvas', canvasId] });
savedFlash = true;
setTimeout(() => (savedFlash = false), 1400);
} catch (err) {
toast.error(err instanceof Error ? err.message : 'Rename failed');
}
}
const SUGGESTIONS: { label: string; prompt: string }[] = [
{
label: 'Create a new film',
prompt: 'Create a new film project and guide me through drafting the story.',
},
{
label: 'Draft a storyline',
prompt: 'Draft a storyline — beats, characters, environments, and misc. items.',
},
];
</script>
<div class="canvas-root shell">
<AppHeader active="canvas" />
{#if $canvasQuery.isError}
<div class="load-error" role="alert">
Could not load this canvas
{#if $canvasQuery.error instanceof Error}
— {$canvasQuery.error.message}
{/if}
</div>
{:else if canvas}
<div class="workspace">
<AgentSessionSidebar
{sessions}
{activeId}
onSelect={selectSession}
onNewSandbox={newSandbox}
onNewProjectChat={newProjectChat}
onDelete={removeSession}
collapsed={railCollapsed}
onToggleCollapse={toggleRail}
/>
<main class="flow-panel">
<div class="titlecard" data-testid="titlecard">
{#if titleEditing}
<input
class="title-input"
bind:value={titleDraft}
onblur={saveTitle}
onkeydown={(e) => {
if (e.key === 'Enter') saveTitle();
if (e.key === 'Escape') titleEditing = false;
}}
/>
{:else}
<button
type="button"
class="title-btn"
onclick={() => (titleEditing = true)}
title="Rename canvas"
>
{canvas.title}
</button>
{/if}
{#if savedFlash}
<span class="saved">saved</span>
{/if}
{#if canvas.project}
<a class="project-chip" href="/project/{canvas.project.id}">
<Icon name="folder" size={12} />
{canvas.project.title}
</a>
{:else}
<span class="sandbox-chip">
<Icon name="sparkle" size={12} />
Sandbox
</span>
{/if}
</div>
<SvelteFlow
bind:nodes
bind:edges
bind:viewport
{nodeTypes}
fitView
colorMode="dark"
minZoom={0.1}
onnodedragstop={(e) => {
const target = e.targetNode ?? e.nodes[e.nodes.length - 1];
if (target) persistNodePosition(target);
}}
onmoveend={(_event, vp) => scheduleViewportSave(vp)}
>
<Background variant={BackgroundVariant.Dots} gap={26} size={1} />
<Controls />
<MiniMap />
</SvelteFlow>
</main>
{#if !chatCollapsed}
<aside class="chat-panel">
<header class="chat-head">
<div class="chat-title-block">
{#if activeSession}
<span class="chat-title">{activeSession.title}</span>
{:else}
<span class="chat-title muted">No chat selected</span>
{/if}
</div>
<button
type="button"
class="chat-toggle"
onclick={toggleChat}
title="Collapse chat"
aria-label="Collapse chat"
>
<Icon name="chevron-right" size={14} />
</button>
</header>
<AgentChat
{messages}
{liveTools}
{streaming}
{streamingReasoning}
{jobs}
{running}
loading={activeId != null && $sessionQuery.isLoading}
suggestions={SUGGESTIONS}
onSuggestion={setComposerDraft}
/>
{#if running && plan && plan.tasks.length > 0}
<AgentPlanPanel {plan} />
{/if}
{#key activeId}
<AgentComposer
{running}
draft={composerDraft}
draftNonce={composerNonce}
workflows={enabledWorkflows}
onSend={handleSend}
onCancel={cancelRun}
/>
{/key}
</aside>
{:else}
<button
type="button"
class="chat-expand"
onclick={toggleChat}
title="Expand chat"
aria-label="Expand chat"
>
<Icon name="chevron-left" size={14} />
</button>
{/if}
</div>
{/if}
</div>
<style>
.shell {
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden;
}
.workspace {
flex: 1;
display: flex;
min-height: 0;
}
.flow-panel {
flex: 1;
min-width: 0;
position: relative;
}
.load-error {
padding: 24px;
color: var(--text-secondary);
border: 1px solid var(--border);
border-radius: var(--radius-md);
margin: 24px;
}
.titlecard {
position: absolute;
top: 14px;
left: 14px;
z-index: 5;
display: flex;
align-items: center;
gap: 10px;
padding: 8px 12px;
background: var(--bg-surface);
border: 1px solid var(--border);
border-radius: var(--radius-md);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
}
.title-btn {
border: none;
background: transparent;
color: var(--text-primary);
font-size: 14px;
font-weight: 600;
cursor: text;
padding: 2px 4px;
border-radius: var(--radius-sm);
max-width: 280px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.title-btn:hover {
background: var(--bg-elevated);
}
.title-input {
border: 1px solid var(--accent);
background: var(--bg-elevated);
color: var(--text-primary);
font-size: 14px;
font-weight: 600;
padding: 2px 6px;
border-radius: var(--radius-sm);
max-width: 280px;
outline: none;
}
.saved {
font-size: 10.5px;
color: var(--text-muted);
letter-spacing: 0.05em;
}
.project-chip,
.sandbox-chip {
display: inline-flex;
align-items: center;
gap: 6px;
font-size: 11.5px;
color: var(--text-secondary);
text-decoration: none;
padding: 3px 8px;
border: 1px solid var(--border);
border-radius: 999px;
white-space: nowrap;
}
.project-chip:hover {
border-color: var(--accent);
color: var(--text-primary);
}
.chat-panel {
width: 420px;
flex-shrink: 0;
border-left: 1px solid var(--border);
background: var(--bg-surface);
display: flex;
flex-direction: column;
min-height: 0;
}
.chat-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--space-sm);
padding: 10px 14px;
border-bottom: 1px solid var(--border);
flex-shrink: 0;
}
.chat-title-block {
min-width: 0;
}
.chat-title {
font-size: 13px;
font-weight: 600;
color: var(--text-primary);
}
.chat-title.muted {
color: var(--text-muted);
font-weight: 400;
}
.chat-toggle,
.chat-expand {
display: inline-flex;
align-items: center;
justify-content: center;
border: 1px solid var(--border);
background: var(--bg-surface);
color: var(--text-secondary);
border-radius: var(--radius-sm);
cursor: pointer;
}
.chat-toggle {
width: 26px;
height: 26px;
}
.chat-toggle:hover,
.chat-expand:hover {
color: var(--text-primary);
border-color: var(--accent);
}
.chat-expand {
width: 34px;
height: 34px;
position: absolute;
right: 14px;
top: 14px;
z-index: 5;
}
</style>
@@ -0,0 +1,2 @@
export const ssr = false;
export const prerender = false;