Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions console-ui/__tests__/store-messages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { beforeEach, describe, expect, it } from "vitest";
import { useStore, type Chat, type Message } from "@/lib/store";

const message = (id: string, content: string): Message => ({ id, content, role: "assistant", timestamp: 0 });

beforeEach(() => {
localStorage.clear();
useStore.setState({ chats: [], activeChatId: null });
});

describe("chat message state", () => {
it("preserves other chats and messages while applying updates and ordered token batches", () => {
const untouched = message("other", "untouched");
const otherChat: Chat = { id: "other-chat", title: "Other", createdAt: 0, messages: [message("reply", "other chat")] };
useStore.setState({ chats: [
{ id: "active", title: "Active", createdAt: 0, messages: [message("reply", "Hello"), untouched] },
otherChat,
] });
const actions = useStore.getState();
actions.appendToMessage("active", "reply", " world");
actions.appendToThinking("active", "reply", "First");
actions.appendToThinking("active", "reply", " second");
actions.updateMessage("active", "reply", { streaming: false, tokenCount: 3 });
const chats = useStore.getState().chats;
expect(chats[0].messages[0]).toMatchObject({ content: "Hello world", thinking: "First second", streaming: false, tokenCount: 3 });
expect(chats[0].messages[1]).toBe(untouched);
expect(chats[1]).toBe(otherChat);
});

it("does not create messages for unknown chat or message identifiers", () => {
const chat: Chat = { id: "active", title: "Active", createdAt: 0, messages: [message("reply", "keep")] };
useStore.setState({ chats: [chat] });
useStore.getState().appendToMessage("missing", "reply", "lost");
useStore.getState().updateMessage("active", "missing", { content: "lost" });
expect(useStore.getState().chats).toEqual([chat]);
});

it("keeps images and streaming in memory but excludes them from persisted history", () => {
const id = useStore.getState().createChat();
useStore.getState().addMessage(id, { ...message("reply", "visible"), streaming: true, images: ["data:image/png;base64,AAA"] });
useStore.getState().appendToMessage(id, "reply", " token");
const current = useStore.getState().chats[0].messages[0];
expect(current.streaming).toBe(true);
expect(current.images).toHaveLength(1);
const persisted = JSON.parse(localStorage.getItem("darkbloom-store")!).state.chats[0].messages[0];
expect(persisted).toMatchObject({ content: "visible token", streaming: false });
expect(persisted.images).toBeUndefined();
});
});
9 changes: 1 addition & 8 deletions console-ui/src/lib/chat-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,5 @@ export function toApiMessages(
function findNewestImageMessageIndex(
messages: Pick<Message, "images">[]
): number {
let index = messages.length;
for (const message of [...messages].reverse()) {
index -= 1;
if (message.images && message.images.length > 0) {
return index;
}
}
return -1;
return messages.findLastIndex((message) => Boolean(message.images?.length));
}
49 changes: 11 additions & 38 deletions console-ui/src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ function generateId(): string {
return Math.random().toString(36).slice(2, 10) + Date.now().toString(36);
}

/** Update one message without replacing other chats or their message objects. */
function mapMessage(chats: Chat[], chatId: string, msgId: string, update: (message: Message) => Message): Chat[] {
return chats.map((chat) => chat.id === chatId ? {
...chat,
messages: chat.messages.map((message) => message.id === msgId ? update(message) : message),
} : chat);
}

export const useStore = create<AppState>()(
persist(
(set, get) => ({
Expand Down Expand Up @@ -113,48 +121,13 @@ export const useStore = create<AppState>()(
})),

updateMessage: (chatId, msgId, update) =>
set((s) => ({
chats: s.chats.map((c) =>
c.id === chatId
? {
...c,
messages: c.messages.map((m) =>
m.id === msgId ? { ...m, ...update } : m
),
}
: c
),
})),
set((s) => ({ chats: mapMessage(s.chats, chatId, msgId, (message) => ({ ...message, ...update })) })),

appendToMessage: (chatId, msgId, token) =>
set((s) => ({
chats: s.chats.map((c) =>
c.id === chatId
? {
...c,
messages: c.messages.map((m) =>
m.id === msgId ? { ...m, content: m.content + token } : m
),
}
: c
),
})),
set((s) => ({ chats: mapMessage(s.chats, chatId, msgId, (message) => ({ ...message, content: message.content + token })) })),

appendToThinking: (chatId, msgId, token) =>
set((s) => ({
chats: s.chats.map((c) =>
c.id === chatId
? {
...c,
messages: c.messages.map((m) =>
m.id === msgId
? { ...m, thinking: (m.thinking || "") + token }
: m
),
}
: c
),
})),
set((s) => ({ chats: mapMessage(s.chats, chatId, msgId, (message) => ({ ...message, thinking: (message.thinking || "") + token })) })),

setSelectedModel: (model) => set({ selectedModel: model }),
setModels: (models) => {
Expand Down
Loading