mirror of
https://github.com/open-webui/computer.git
synced 2026-11-22 21:50:43 -05:00
refac
This commit is contained in:
1 parent
48db0611a8
commit
10ecaae589
8 files changed
+110
-3
No files matched your search
@@ -25,6 +25,7 @@ export interface ChatInfo {
|
||||
current_message_id: string | null;
|
||||
created_at: number;
|
||||
updated_at: number;
|
||||
last_read_at: number | null;
|
||||
is_active?: boolean;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
let wsChatsHasMore = $state<Map<string, boolean>>(new Map());
|
||||
let wsChatsLoading = $state<Set<string>>(new Set());
|
||||
let currentPath = $derived($page.url.searchParams.get('workspace'));
|
||||
let currentChatId = $derived($page.url.searchParams.get('chatId'));
|
||||
|
||||
function toggleWorkspaceExpand(path: string) {
|
||||
const next = new Set(expandedWorkspaces);
|
||||
@@ -163,7 +164,20 @@
|
||||
title?: string;
|
||||
delta?: string;
|
||||
workspace?: string;
|
||||
last_read_at?: number;
|
||||
}) {
|
||||
if (typeof data.last_read_at === 'number') {
|
||||
wsChatsCache = new Map(
|
||||
[...wsChatsCache].map(([path, chats]) => [
|
||||
path,
|
||||
chats.map((chat) =>
|
||||
chat.id === data.chat_id ? { ...chat, last_read_at: data.last_read_at! } : chat
|
||||
)
|
||||
])
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const isNew = !seenChatIds.has(data.chat_id);
|
||||
seenChatIds.add(data.chat_id);
|
||||
if (!data.done && !data.title && !isNew) return;
|
||||
@@ -314,6 +328,7 @@
|
||||
{#each chats as chat (chat.id)}
|
||||
<ChatItem
|
||||
{chat}
|
||||
isSelected={chat.id === currentChatId}
|
||||
onclick={() => openChat(chat.id, ws.path)}
|
||||
onmenu={(e) => openChatMenu(e, chat.id, ws.path)}
|
||||
/>
|
||||
|
||||
@@ -383,10 +383,16 @@
|
||||
// ── Load chat from DB ───────────────────────────────────────
|
||||
|
||||
let loadGeneration = 0;
|
||||
let loadedChatId = $state<string | null>(null);
|
||||
|
||||
function markChatRead(id: string) {
|
||||
socketStore.getSocket()?.emit('chat:read', { chat_id: id });
|
||||
}
|
||||
|
||||
async function loadChat(id: string) {
|
||||
if (chatId && chatId !== id) stopTtsPlayback();
|
||||
chatId = id;
|
||||
loadedChatId = null;
|
||||
const gen = ++loadGeneration;
|
||||
// Only show loading spinner on initial load (no messages yet).
|
||||
// On reloads (e.g. after cancel/done), keep the DOM intact to preserve scroll position.
|
||||
@@ -416,6 +422,7 @@
|
||||
chatTitle = data.chat.title;
|
||||
updateTab(tabId, id, data.chat.title);
|
||||
}
|
||||
loadedChatId = id;
|
||||
} finally {
|
||||
if (isInitialLoad && gen === loadGeneration) loading = false;
|
||||
}
|
||||
@@ -756,6 +763,22 @@
|
||||
return () => unregisterStreamingChat(chatId!);
|
||||
});
|
||||
|
||||
// ── Persist read state separately from visibility tracking ──
|
||||
|
||||
$effect(() => {
|
||||
if (!chatId || loadedChatId !== chatId || !active) return;
|
||||
const id = chatId;
|
||||
const send = () => markChatRead(id);
|
||||
send();
|
||||
return socketStore.on('connect', send);
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (!chatId || !active) return;
|
||||
const id = chatId;
|
||||
return () => markChatRead(id);
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (!chatId || !tabId) return;
|
||||
const send = () =>
|
||||
|
||||
@@ -10,11 +10,17 @@
|
||||
|
||||
interface Props {
|
||||
chat: ChatInfo;
|
||||
isSelected?: boolean;
|
||||
onclick: () => void;
|
||||
/** Optional menu button click handler */
|
||||
onmenu?: (e: MouseEvent) => void;
|
||||
}
|
||||
let { chat, onclick, onmenu }: Props = $props();
|
||||
let { chat, isSelected = false, onclick, onmenu }: Props = $props();
|
||||
let unread = $derived(
|
||||
!isSelected &&
|
||||
!chat.is_active &&
|
||||
(chat.last_read_at === null || chat.updated_at > chat.last_read_at)
|
||||
);
|
||||
|
||||
function formatTime(ts: number): string {
|
||||
const now = Date.now();
|
||||
@@ -47,7 +53,14 @@
|
||||
{#if chat.is_active}
|
||||
<Spinner size={10} borderWidth={1.5} class="opacity-50" />
|
||||
{/if}
|
||||
<span class="flex-1 text-xs text-gray-500 dark:text-gray-500 truncate min-w-0">{chat.title}</span>
|
||||
{#if unread}
|
||||
<span class="size-1.5 shrink-0 rounded-full bg-sky-500" aria-hidden="true"></span>
|
||||
{/if}
|
||||
<span
|
||||
class="flex-1 text-xs truncate min-w-0 {unread
|
||||
? 'font-medium text-gray-900 dark:text-gray-100'
|
||||
: 'text-gray-500 dark:text-gray-500'}">{chat.title}</span
|
||||
>
|
||||
<span class="text-[0.625rem] text-gray-300 dark:text-gray-700 shrink-0 tabular-nums"
|
||||
>{formatTime(chat.updated_at)}</span
|
||||
>
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
"""add chat last read at
|
||||
|
||||
Revision ID: 0004
|
||||
Revises: 0003
|
||||
Create Date: 2026-07-11
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = "0004"
|
||||
down_revision = "0003"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("chats", sa.Column("last_read_at", sa.BigInteger(), nullable=True))
|
||||
op.execute("UPDATE chats SET last_read_at = updated_at")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column("chats", "last_read_at")
|
||||
@@ -29,6 +29,7 @@ class Chat(Base):
|
||||
meta = Column(JSON, nullable=True) # {model_id, connection_id, pinned, archived, ...}
|
||||
created_at = Column(BigInteger, nullable=False)
|
||||
updated_at = Column(BigInteger, nullable=False)
|
||||
last_read_at = Column(BigInteger, nullable=True)
|
||||
|
||||
# ── Class methods ────────────────────────────────────────
|
||||
|
||||
@@ -61,6 +62,7 @@ class Chat(Base):
|
||||
meta=meta,
|
||||
created_at=created_at,
|
||||
updated_at=created_at,
|
||||
last_read_at=created_at,
|
||||
)
|
||||
db.add(chat)
|
||||
await db.commit()
|
||||
@@ -109,6 +111,18 @@ class Chat(Base):
|
||||
await db.commit()
|
||||
return result.rowcount > 0
|
||||
|
||||
@staticmethod
|
||||
async def update_last_read_at(chat_id: str, user_id: str, last_read_at: int) -> bool:
|
||||
"""Mark a chat read without changing its activity timestamp."""
|
||||
async with await get_db() as db:
|
||||
result = await db.execute(
|
||||
update(Chat)
|
||||
.where(Chat.id == chat_id, Chat.user_id == user_id)
|
||||
.values(last_read_at=last_read_at)
|
||||
)
|
||||
await db.commit()
|
||||
return result.rowcount > 0
|
||||
|
||||
@staticmethod
|
||||
async def delete(chat_id: str) -> bool:
|
||||
async with await get_db() as db:
|
||||
|
||||
@@ -180,6 +180,7 @@ async def list_chats(
|
||||
"current_message_id": chat.current_message_id,
|
||||
"created_at": chat.created_at,
|
||||
"updated_at": listing_updated_at,
|
||||
"last_read_at": chat.last_read_at,
|
||||
"is_active": chat.id in active_ids,
|
||||
}
|
||||
)
|
||||
@@ -402,6 +403,7 @@ async def get_chat(
|
||||
"current_message_id": chat.current_message_id,
|
||||
"created_at": chat.created_at,
|
||||
"updated_at": chat.updated_at,
|
||||
"last_read_at": chat.last_read_at,
|
||||
},
|
||||
"messages": [_message_dict(m) for m in messages],
|
||||
"tasks": tasks,
|
||||
|
||||
+16
-1
@@ -9,7 +9,8 @@ from __future__ import annotations
|
||||
|
||||
import socketio
|
||||
|
||||
from cptr.utils.config import check_access
|
||||
from cptr.models import Chat
|
||||
from cptr.utils.config import check_access, now_ms
|
||||
from cptr.env import CORS_ALLOWED_ORIGINS
|
||||
|
||||
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins=CORS_ALLOWED_ORIGINS)
|
||||
@@ -77,6 +78,20 @@ async def on_chat_view(sid, data):
|
||||
del _chat_view_sids[(user_id, chat_id)]
|
||||
|
||||
|
||||
@sio.on("chat:read")
|
||||
async def on_chat_read(sid, data):
|
||||
"""Persist a user's explicit read acknowledgement for a chat."""
|
||||
session = await sio.get_session(sid)
|
||||
user_id = session.get("user_id")
|
||||
chat_id = data.get("chat_id") if isinstance(data, dict) else None
|
||||
if not user_id or not isinstance(chat_id, str):
|
||||
return
|
||||
|
||||
last_read_at = now_ms()
|
||||
if await Chat.update_last_read_at(chat_id, user_id, last_read_at):
|
||||
await emit_to_user(user_id, {"chat_id": chat_id, "last_read_at": last_read_at})
|
||||
|
||||
|
||||
async def emit_to_user(user_id: str, data: dict):
|
||||
"""Send events:chat to all of a user's connected tabs/windows."""
|
||||
for sid in list(_user_sids.get(user_id, set())):
|
||||
|
||||
Reference in new issue
Block a user