Files
archy/packages/app/vite-dev-chats.ts
T
Dorian 80aeefa4bf feat(chat): enhance chat functionality with song integration and UI improvements
- Updated ChatHeader and ChatMessage components to support song selection and display.
- Enhanced useContentPanel to handle both film and song content, allowing for richer interactions.
- Improved FilmCard and added SongCard components for better media representation.
- Refactored environment variables for better configuration management.
- Updated .gitignore to include development chat history.

Made-with: Cursor
2026-03-02 18:16:04 +00:00

69 lines
1.9 KiB
TypeScript

import type { Plugin, Connect } from 'vite'
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs'
import { resolve, dirname } from 'path'
const CHATS_DIR = '.dev'
const CHATS_FILE = 'chats.json'
function getChatsPath(root: string): string {
const dir = resolve(root, CHATS_DIR)
if (!existsSync(dir)) mkdirSync(dir, { recursive: true })
return resolve(dir, CHATS_FILE)
}
function readChats(filePath: string): string {
if (!existsSync(filePath)) return '{}'
try {
return readFileSync(filePath, 'utf-8')
} catch {
return '{}'
}
}
function writeChats(filePath: string, data: string): void {
const dir = dirname(filePath)
if (!existsSync(dir)) mkdirSync(dir, { recursive: true })
writeFileSync(filePath, data, 'utf-8')
}
export function devChatsPlugin(): Plugin {
let chatsPath = ''
return {
name: 'aiui-dev-chats',
apply: 'serve',
configResolved(config) {
chatsPath = getChatsPath(config.root)
},
configureServer(server) {
server.middlewares.use('/api/dev-chats', (req: Connect.IncomingMessage, res: any, next: () => void) => {
if (req.method === 'GET') {
const data = readChats(chatsPath)
res.setHeader('Content-Type', 'application/json')
res.end(data)
return
}
if (req.method === 'PUT') {
let body = ''
req.on('data', (chunk: Buffer) => { body += chunk.toString() })
req.on('end', () => {
try {
JSON.parse(body)
writeChats(chatsPath, body)
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ ok: true }))
} catch (e) {
res.writeHead(400, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: 'Invalid JSON' }))
}
})
return
}
next()
})
},
}
}