mirror of
https://github.com/ArthurDanjou/artagents.git
synced 2026-01-23 08:30:28 +01:00
feat: add chat and file management APIs, implement chat loading and saving functionality
- Introduced new API endpoints for chat management including posting and retrieving chat messages. - Implemented file upload and deletion functionalities for chat and other files. - Added utility functions for streaming text and loading chat data. - Created TypeScript types for models and agents used in the application. - Configured TypeScript settings for server and project. - Added favicon and workspace configuration for pnpm.
This commit is contained in:
33
server/utils/chat.ts
Normal file
33
server/utils/chat.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
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/chats/${slug}.json`, {
|
||||
method: 'DELETE'
|
||||
})
|
||||
}
|
||||
13
server/utils/stream.ts
Normal file
13
server/utils/stream.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export async function streamToText(stream: ReadableStream<Uint8Array>) {
|
||||
const reader = stream.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let result = ''
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) break
|
||||
result += decoder.decode(value, { stream: true })
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user