Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

2.1 KB
Raw Blame
import { useCallback, useEffect, useState } from 'react';

import {
  MAX_VISIBLE_VOICE_INPUT_HISTORY_ENTRIES,
  type VoiceInputDataSnapshot,
  type VoiceInputHistoryEntry,
} from '../../shared/voiceInputData';

export type { VoiceInputHistoryEntry };

export function getVoiceInputHistory(limit?: number): VoiceInputHistoryEntry[] {
  try {
    return window.electronAPI.voiceInput.getHistory(limit);
  } catch {
    return [];
  }
}

export function getVoiceInputHistoryForRefinement(): VoiceInputHistoryEntry[] {
  try {
    return window.electronAPI.voiceInput.getHistoryForRefinement();
  } catch {
    return [];
  }
}

export function recordVoiceInputHistory(text: string): string | null {
  try {
    return window.electronAPI.voiceInput.recordHistory(text);
  } catch {
    return null;
  }
}

export function updateVoiceInputHistoryEntry(id: string, text: string): void {
  try {
    window.electronAPI.voiceInput.updateHistoryEntry(id, text);
  } catch {
    // History is advisory context only; do not interrupt voice input on storage errors.
  }
}

export function deleteVoiceInputHistoryEntry(id: string): void {
  try {
    window.electronAPI.voiceInput.deleteHistoryEntry(id);
  } catch {
    // History is advisory context only; do not interrupt settings UI on storage errors.
  }
}

export function useVoiceInputHistory(): {
  entries: VoiceInputHistoryEntry[];
  deleteEntry: (id: string) => void;
} {
  const [entries, setEntries] = useState<VoiceInputHistoryEntry[]>(() =>
    getVoiceInputHistory(MAX_VISIBLE_VOICE_INPUT_HISTORY_ENTRIES),
  );

  const syncFromSnapshot = useCallback((snapshot?: VoiceInputDataSnapshot) => {
    setEntries(
      (snapshot?.history ?? getVoiceInputHistory())
        .slice(0, MAX_VISIBLE_VOICE_INPUT_HISTORY_ENTRIES),
    );
  }, []);

  const deleteEntry = useCallback((id: string) => {
    deleteVoiceInputHistoryEntry(id);
    setEntries(getVoiceInputHistory(MAX_VISIBLE_VOICE_INPUT_HISTORY_ENTRIES));
  }, []);

  useEffect(() => window.electronAPI.voiceInput.onDataChanged(syncFromSnapshot), [syncFromSnapshot]);

  return {
    entries,
    deleteEntry,
  };
}