|
|
@@ -1,9 +1,22 @@
|
|
|
import type { StateCreator } from "zustand";
|
|
|
import type { ChatStore, CreateActions, AgentResponse } from "../../types";
|
|
|
import { fetchJson } from "../../../../hooks/use-api";
|
|
|
+import { bookKey } from "../message/runtime";
|
|
|
|
|
|
export const createCreateSlice: StateCreator<ChatStore, [], [], CreateActions> = (set, get) => ({
|
|
|
- setPendingBookArgs: (args) => set({ pendingBookArgs: args }),
|
|
|
+ setPendingBookArgs: (args) =>
|
|
|
+ set((state) => ({
|
|
|
+ pendingBookArgs: args,
|
|
|
+ sessions: state.activeSessionId
|
|
|
+ ? {
|
|
|
+ ...state.sessions,
|
|
|
+ [state.activeSessionId]: {
|
|
|
+ ...state.sessions[state.activeSessionId],
|
|
|
+ pendingBookArgs: args,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ : state.sessions,
|
|
|
+ })),
|
|
|
setBookCreating: (creating) => set({ bookCreating: creating }),
|
|
|
setCreateProgress: (progress) => set({ createProgress: progress }),
|
|
|
|
|
|
@@ -13,21 +26,62 @@ export const createCreateSlice: StateCreator<ChatStore, [], [], CreateActions> =
|
|
|
closeArtifact: () => set({ sidebarView: "panel", artifactFile: null, artifactChapter: null }),
|
|
|
setBookSummary: (summary) => set({ bookSummary: summary }),
|
|
|
|
|
|
- handleCreateBook: async (activeBookId) => {
|
|
|
- if (!get().pendingBookArgs) return null;
|
|
|
+ handleCreateBook: async (sessionId, activeBookId) => {
|
|
|
+ const session = get().sessions[sessionId];
|
|
|
+ const pendingArgs = session?.pendingBookArgs ?? get().pendingBookArgs;
|
|
|
+ if (!pendingArgs) return null;
|
|
|
|
|
|
set({ bookCreating: true });
|
|
|
try {
|
|
|
const data = await fetchJson<AgentResponse>("/agent", {
|
|
|
method: "POST",
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
- body: JSON.stringify({ instruction: "/create", activeBookId }),
|
|
|
+ body: JSON.stringify({ instruction: "/create", activeBookId, sessionId }),
|
|
|
});
|
|
|
const newBookId = data.session?.activeBookId ?? null;
|
|
|
- if (newBookId) get().bumpBookDataVersion();
|
|
|
+ if (newBookId) {
|
|
|
+ set((state) => {
|
|
|
+ const session = state.sessions[sessionId];
|
|
|
+ if (!session) {
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+
|
|
|
+ const previousIds = state.sessionIdsByBook[bookKey(session.bookId)] ?? [];
|
|
|
+ const nextIds = state.sessionIdsByBook[newBookId] ?? [];
|
|
|
+
|
|
|
+ return {
|
|
|
+ sessions: {
|
|
|
+ ...state.sessions,
|
|
|
+ [sessionId]: {
|
|
|
+ ...session,
|
|
|
+ bookId: newBookId,
|
|
|
+ pendingBookArgs: null,
|
|
|
+ },
|
|
|
+ },
|
|
|
+ sessionIdsByBook: {
|
|
|
+ ...state.sessionIdsByBook,
|
|
|
+ [bookKey(session.bookId)]: previousIds.filter((id) => id !== sessionId),
|
|
|
+ [newBookId]: nextIds.includes(sessionId) ? nextIds : [sessionId, ...nextIds],
|
|
|
+ },
|
|
|
+ };
|
|
|
+ });
|
|
|
+ get().bumpBookDataVersion();
|
|
|
+ }
|
|
|
+ set((state) => ({
|
|
|
+ pendingBookArgs: null,
|
|
|
+ sessions: state.sessions[sessionId]
|
|
|
+ ? {
|
|
|
+ ...state.sessions,
|
|
|
+ [sessionId]: {
|
|
|
+ ...state.sessions[sessionId],
|
|
|
+ pendingBookArgs: null,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ : state.sessions,
|
|
|
+ }));
|
|
|
return newBookId;
|
|
|
} catch (e) {
|
|
|
- get().addErrorMessage(e instanceof Error ? e.message : String(e));
|
|
|
+ get().addErrorMessage(sessionId, e instanceof Error ? e.message : String(e));
|
|
|
return null;
|
|
|
} finally {
|
|
|
set({ bookCreating: false });
|