Newer
Older
navi-1 / webclient / src / stores / chat / recall.js
@Eugene Sukhodolskiy Eugene Sukhodolskiy 23 hours ago 1 KB webclient: split the chat store into area modules
import * as api from '@/api/index.js'
import { useSessionsStore } from '@/stores/sessions.js'

/** The scheduled recall of the active session: banner state and its actions. */

export function createRecallActions(state) {
  const { currentId, recall } = state

  async function loadRecall(id = currentId.value) {
    if (!id) {
      recall.value = null
      return
    }
    try {
      const data = await api.getSessionRecall(id)
      recall.value = data?.recall ?? null
    } catch {
      recall.value = null
    }
  }

  // Cancelling and skipping live next to the banner and the dialog that both
  // offer them, so neither component talks to the API on its own.
  async function cancelRecall() {
    if (!currentId.value) return false
    try {
      await api.cancelSessionRecall(currentId.value)
      recall.value = null
      useSessionsStore().updateRecallStatus(currentId.value, false)
      return true
    } catch (e) {
      console.error('cancel recall failed', e)
      return false
    }
  }

  async function skipRecall() {
    if (!currentId.value) return false
    try {
      await api.skipSessionRecall(currentId.value)
      await loadRecall()
      return true
    } catch (e) {
      console.error('skip recall failed', e)
      return false
    }
  }

  function onRecallUpdate(data) {
    if (!data.session_id) return
    const sessionsStore = useSessionsStore()
    const hasRecall = data.status === 'pending'
    sessionsStore.updateRecallStatus(data.session_id, hasRecall)
    if (data.session_id === currentId.value) {
      if (hasRecall) {
        // Partial update from event — refetch for full details
        loadRecall(data.session_id)
      } else {
        recall.value = null
      }
    }
  }

  return { loadRecall, cancelRecall, skipRecall, onRecallUpdate }
}