Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

1.4 KB
Raw Blame
import { Attachment } from '@/types/attachment'
import { create } from 'zustand'

type AttachmentSummary = Pick<Attachment, 'name' | 'size'>

type AttachmentIngestionState = {
  isModalOpen: boolean
  currentAttachment: AttachmentSummary | null
  currentIndex: number
  totalCount: number
  resolver: ((choice: 'inline' | 'embeddings' | undefined) => void) | null
  showPrompt: (
    attachment: AttachmentSummary,
    currentIndex: number,
    totalCount: number
  ) => Promise<'inline' | 'embeddings' | undefined>
  choose: (choice: 'inline' | 'embeddings') => void
  cancel: () => void
}

export const useAttachmentIngestionPrompt = create<AttachmentIngestionState>()(
  (set, get) => ({
    isModalOpen: false,
    currentAttachment: null,
    currentIndex: 0,
    totalCount: 0,
    resolver: null,
    showPrompt: async (attachment, currentIndex, totalCount) => {
      return new Promise<'inline' | 'embeddings' | undefined>((resolve) => {
        set({
          isModalOpen: true,
          currentAttachment: attachment,
          currentIndex,
          totalCount,
          resolver: resolve,
        })
      })
    },
    choose: (choice) => {
      const { resolver } = get()
      resolver?.(choice)
      set({ isModalOpen: false, currentAttachment: null, resolver: null })
    },
    cancel: () => {
      const { resolver } = get()
      resolver?.(undefined)
      set({ isModalOpen: false, currentAttachment: null, resolver: null })
    },
  })
)