All files / stores persona.ts

6.25% Statements 2/32
0% Branches 0/2
16.66% Functions 1/6
6.25% Lines 2/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                                                  1x 1x                                                                                                                      
import { defineStore } from 'pinia'
import request from '@/utils/request'
 
export interface Persona {
  id: number
  owner_id: number
  name: string
  title: string
  bio: string
  theories: string[]
  stance: string
  system_prompt: string
  is_public: boolean
}
 
interface CreatePersonaData {
    name: string;
    title?: string;
    bio?: string;
    theories?: string[];
    stance?: string;
    system_prompt?: string;
    is_public?: boolean;
}
 
export const usePersonaStore = defineStore('persona', {
  state: () => ({
    personas: [] as Persona[],
    loading: false
  }),
  actions: {
    async fetchPersonas(ownerId?: number) {
      this.loading = true
      try {
        const params = ownerId ? { owner_id: ownerId } : {}
        const res = await request.get('/personas/', { params })
        this.personas = res.data
      } catch (error) {
        console.error('Failed to fetch personas:', error)
        this.personas = []
      } finally {
        this.loading = false
      }
    },
    async createPersona(data: CreatePersonaData) {
      this.loading = true
      try {
        await request.post('/personas/', data)
        await this.fetchPersonas()
      } catch (error) {
        console.error('Failed to create persona:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    async createPresetPersonas() {
      try {
        await request.post('/personas/batch/preset')
        await this.fetchPersonas()
      } catch (error) {
        console.error('Failed to create preset personas:', error)
        throw error
      }
    },
    async updatePersona(id: number, data: Partial<CreatePersonaData>) {
      try {
        await request.put(`/personas/${id}`, data)
        await this.fetchPersonas()
      } catch (error) {
        console.error('Failed to update persona:', error)
        throw error
      }
    },
    async deletePersona(id: number) {
      try {
        await request.delete(`/personas/${id}`)
        await this.fetchPersonas()
      } catch (error) {
        console.error('Failed to delete persona:', error)
        throw error
      }
    }
  }
})