Files
artagents/server/api/chat.post.ts
Arthur DANJOU c0b5539f12 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.
2025-04-14 12:19:30 +02:00

56 lines
1.3 KiB
TypeScript

import type { Message } from 'ai'
import { appendResponseMessages, appendClientMessage, streamText } from 'ai'
import { createWorkersAI } from 'workers-ai-provider'
import { loadChat } from '~~/server/utils/chat'
export default defineEventHandler(async (event) => {
const { id, message, model, agent } = await readBody(event)
console.log(model, agent)
const workersAI = createWorkersAI({ binding: hubAI() })
const previousMessages = await loadChat(id)
const messages = appendClientMessage({
messages: previousMessages,
message
})
const result = streamText({
model: workersAI('@cf/meta/llama-3.1-8b-instruct'),
// system, TODO: add system
// prompt, TODO: add prompt
messages,
// tools, TODO: add tools
async onFinish({ response }) {
await saveChat({
id,
messages: appendResponseMessages({
messages,
responseMessages: response.messages
})
})
}
})
result.consumeStream()
return result.toDataStreamResponse()
})
async function saveChat({ id, messages }: { id: string, messages: Message[] }) {
const hub = hubBlob()
await hub.delete(`chats/${id}.json`)
await $fetch('/api/chats', {
method: 'POST',
body: {
file: {
name: `${id}.json`,
content: JSON.stringify(messages, null, 2)
}
}
})
}