mirror of
https://github.com/ArthurDanjou/artagents.git
synced 2026-01-14 12:14:40 +01:00
38 lines
790 B
TypeScript
38 lines
790 B
TypeScript
import type { Message } from 'ai'
|
|
|
|
export async function loadChat(slug: string): Promise<Message[]> {
|
|
const { blobs } = await $fetch('/api/files')
|
|
|
|
if (!blobs.find(item => item.pathname === `chats/${slug}.json`)) {
|
|
await createChat(slug)
|
|
}
|
|
|
|
const data = await $fetch<string>(`/api/chats/${slug}`)
|
|
const dataString = JSON.stringify(data)
|
|
|
|
if (dataString === '[]')
|
|
return []
|
|
return JSON.parse(dataString)
|
|
}
|
|
|
|
async function createChat(slug: string) {
|
|
await $fetch('/api/chats', {
|
|
method: 'POST',
|
|
body: {
|
|
file: {
|
|
name: `${slug}.json`,
|
|
content: '[]',
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
export async function deleteChat(slug: string) {
|
|
await $fetch('/api/files', {
|
|
method: 'DELETE',
|
|
body: {
|
|
pathname: `chats/${slug}.json`,
|
|
},
|
|
})
|
|
}
|