| 1 | import { create } from 'zustand' |
| 2 | import type { |
| 3 | HtmlEditorAiHistoryMessage, |
| 4 | HtmlEditorAiIntent, |
| 5 | HtmlEditorAiMessage, |
| 6 | HtmlEditorAiPlan |
| 7 | } from '../lib/ipc' |
| 8 | |
| 9 | interface HtmlEditorAiState { |
| 10 | enabled: boolean |
| 11 | input: string |
| 12 | messages: HtmlEditorAiHistoryMessage[] |
| 13 | isSending: boolean |
| 14 | error: string | null |
| 15 | intent: HtmlEditorAiIntent | null |
| 16 | pendingPlan: HtmlEditorAiPlan | null |
| 17 | requiresConfirmation: boolean |
| 18 | setEnabled: (enabled: boolean) => void |
| 19 | setInput: (input: string) => void |
| 20 | addMessage: (message: HtmlEditorAiMessage) => void |
| 21 | setMessages: (messages: HtmlEditorAiHistoryMessage[]) => void |
| 22 | setSending: (isSending: boolean) => void |
| 23 | setError: (error: string | null) => void |
| 24 | setPlan: (args: { |
| 25 | intent: HtmlEditorAiIntent |
| 26 | plan: HtmlEditorAiPlan | null |
| 27 | requiresConfirmation: boolean |
| 28 | }) => void |
| 29 | clearConversation: () => void |
| 30 | reset: () => void |
| 31 | } |
| 32 | |
| 33 | const initialState = { |
| 34 | enabled: false, |
| 35 | input: '', |
| 36 | messages: [], |
| 37 | isSending: false, |
| 38 | error: null, |
| 39 | intent: null, |
| 40 | pendingPlan: null, |
| 41 | requiresConfirmation: false |
| 42 | } |
| 43 | |
| 44 | export const useHtmlEditorAiStore = create<HtmlEditorAiState>((set) => ({ |
| 45 | ...initialState, |
| 46 | setEnabled: (enabled) => set({ enabled }), |
| 47 | setInput: (input) => set({ input }), |
| 48 | addMessage: (message) => |
| 49 | set((state) => ({ |
| 50 | messages: [ |
| 51 | ...state.messages, |
| 52 | { ...message, id: crypto.randomUUID(), createdAt: Date.now() } |
| 53 | ].slice(-48) |
| 54 | })), |
| 55 | setMessages: (messages) => set({ messages: messages.slice(-48) }), |
| 56 | setSending: (isSending) => set({ isSending }), |
| 57 | setError: (error) => set({ error }), |
| 58 | setPlan: ({ intent, plan, requiresConfirmation }) => |
| 59 | set({ intent, pendingPlan: plan, requiresConfirmation }), |
| 60 | clearConversation: () => |
| 61 | set({ |
| 62 | input: '', |
| 63 | messages: [], |
| 64 | isSending: false, |
| 65 | error: null, |
| 66 | intent: null, |
| 67 | pendingPlan: null, |
| 68 | requiresConfirmation: false |
| 69 | }), |
| 70 | reset: () => set(initialState) |
| 71 | })) |
| 72 |