refactor: 增加多agent设置
This commit is contained in:
parent
71960aed6d
commit
22042f469a
|
|
@ -32,6 +32,10 @@ const navItems = [
|
|||
path: '/conversations',
|
||||
icon: `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path></svg>`
|
||||
},
|
||||
{
|
||||
path: '/chat-rooms',
|
||||
icon: `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path><circle cx="9" cy="7" r="4"></circle><path d="M23 21v-2a4 4 0 0 0-3-3.87"></path><path d="M16 3.13a4 4 0 0 1 0 7.75"></path></svg>`
|
||||
},
|
||||
{
|
||||
path: '/tools',
|
||||
icon: `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"></path></svg>`
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
<template>
|
||||
<div class="message-bubble" :class="[message.role]">
|
||||
<div v-if="message.role === 'user'" class="avatar">user</div>
|
||||
<div v-else class="avatar">Luxx</div>
|
||||
<div class="message-bubble" :class="[message.role, { 'room-msg': message.room_id }]">
|
||||
<div class="avatar" :style="avatarStyle">{{ avatarText }}</div>
|
||||
<div class="message-container">
|
||||
<div v-if="message.sender_name || message.round_number" class="sender-info">
|
||||
<span v-if="message.sender_name" class="sender-name" :style="{ color: message.sender_color }">{{ message.sender_name }}</span>
|
||||
<span v-if="message.round_number" class="round-tag">R{{ message.round_number }}</span>
|
||||
</div>
|
||||
<!-- File attachments list -->
|
||||
<div v-if="message.attachments && message.attachments.length > 0" class="attachments-list">
|
||||
<div v-for="(file, index) in message.attachments" :key="index" class="attachment-item">
|
||||
|
|
@ -61,6 +64,20 @@ defineEmits(['delete', 'regenerate'])
|
|||
|
||||
const messageRef = ref(null)
|
||||
|
||||
const avatarStyle = computed(() => {
|
||||
if (props.message.sender_color && props.message.role === 'assistant') {
|
||||
return { background: props.message.sender_color }
|
||||
}
|
||||
return {}
|
||||
})
|
||||
|
||||
const avatarText = computed(() => {
|
||||
if (props.message.sender_name && props.message.role === 'assistant') {
|
||||
return props.message.sender_name.charAt(0)
|
||||
}
|
||||
return props.message.role === 'user' ? 'user' : 'Luxx'
|
||||
})
|
||||
|
||||
const renderedContent = computed(() => {
|
||||
const text = props.message.content || props.message.text || ''
|
||||
if (!text) return ''
|
||||
|
|
@ -136,4 +153,24 @@ const regenerateIcon = `<svg viewBox="0 0 24 24" width="14" height="14" fill="no
|
|||
font-size: 12px;
|
||||
color: var(--text-tertiary);
|
||||
}
|
||||
|
||||
.sender-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.sender-name {
|
||||
font-size: 0.8rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.round-tag {
|
||||
font-size: 0.65rem;
|
||||
background: var(--bg-secondary);
|
||||
padding: 1px 6px;
|
||||
border-radius: 4px;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
</style>
|
||||
|
|
|
|||
|
|
@ -32,6 +32,12 @@ const routes = [
|
|||
component: () => import('../views/ToolsView.vue'),
|
||||
meta: { requiresAuth: true }
|
||||
},
|
||||
{
|
||||
path: '/chat-rooms',
|
||||
name: 'ChatRooms',
|
||||
component: () => import('../views/ChatRoomView.vue'),
|
||||
meta: { requiresAuth: true }
|
||||
},
|
||||
// 首页重定向
|
||||
{
|
||||
path: '/home',
|
||||
|
|
|
|||
|
|
@ -44,7 +44,9 @@ export const authAPI = {
|
|||
logout: () => api.post('/auth/logout'),
|
||||
getMe: () => api.get('/auth/me'),
|
||||
listUsers: () => api.get('/auth/users'),
|
||||
updateUserPermission: (userId, data) => api.put(`/auth/users/${userId}`, data)
|
||||
updateUserPermission: (userId, data) => api.put(`/auth/users/${userId}`, data),
|
||||
getSettings: () => api.get('/auth/settings'),
|
||||
updateSettings: (data) => api.put('/auth/settings', data)
|
||||
}
|
||||
|
||||
// ============ 会话接口 ============
|
||||
|
|
@ -84,4 +86,31 @@ export const providersAPI = {
|
|||
test: (id) => api.post(`/providers/${id}/test`)
|
||||
}
|
||||
|
||||
// ============ Agent 接口 ============
|
||||
|
||||
export const agentsAPI = {
|
||||
list: () => api.get('/agents/'),
|
||||
create: (data) => api.post('/agents/', data),
|
||||
get: (id) => api.get(`/agents/${id}`),
|
||||
update: (id, data) => api.put(`/agents/${id}`, data),
|
||||
delete: (id) => api.delete(`/agents/${id}`)
|
||||
}
|
||||
|
||||
// ============ 聊天室接口 ============
|
||||
|
||||
export const chatRoomsAPI = {
|
||||
list: (params) => api.get('/chat-rooms/', { params }),
|
||||
create: (data) => api.post('/chat-rooms/', data),
|
||||
get: (id) => api.get(`/chat-rooms/${id}`),
|
||||
update: (id, data) => api.put(`/chat-rooms/${id}`, data),
|
||||
delete: (id) => api.delete(`/chat-rooms/${id}`),
|
||||
getMessages: (id) => api.get(`/chat-rooms/${id}/messages`),
|
||||
start: (id) => `/api/chat-rooms/${id}/start`,
|
||||
stop: (id) => api.post(`/chat-rooms/${id}/stop`),
|
||||
reset: (id) => api.post(`/chat-rooms/${id}/reset`),
|
||||
addAgent: (roomId, data) => api.post(`/chat-rooms/${roomId}/agents`, data),
|
||||
updateAgent: (roomId, agentId, data) => api.put(`/chat-rooms/${roomId}/agents/${agentId}`, data),
|
||||
deleteAgent: (roomId, agentId) => api.delete(`/chat-rooms/${roomId}/agents/${agentId}`)
|
||||
}
|
||||
|
||||
export default api
|
||||
|
|
|
|||
|
|
@ -0,0 +1,850 @@
|
|||
<script setup>
|
||||
import { ref, computed, onMounted, onUnmounted, nextTick, watch } from 'vue'
|
||||
import { chatRoomsAPI, providersAPI, agentsAPI } from '../utils/api.js'
|
||||
import MessageBubble from '../components/MessageBubble.vue'
|
||||
|
||||
// ============ Sidebar tab state ============
|
||||
const sidebarTab = ref('rooms') // 'agents' | 'rooms' | 'roomAgents'
|
||||
|
||||
// ============ Agent pool state ============
|
||||
const agentPool = ref([])
|
||||
const agentPoolLoading = ref(false)
|
||||
const editingAgent = ref(null) // null | 'new' | agent object
|
||||
const agentForm = ref({})
|
||||
const agentSaving = ref(false)
|
||||
|
||||
// Computed models from selected provider
|
||||
const availableModels = computed(() => {
|
||||
let models = []
|
||||
if (!agentForm.value.provider_id) {
|
||||
models = providers.value.map(p => `${p.name}: ${p.default_model}`)
|
||||
} else {
|
||||
const p = providers.value.find(p => p.id === agentForm.value.provider_id)
|
||||
if (p) models = [`${p.name}: ${p.default_model}`]
|
||||
}
|
||||
// Always include current model if it's a custom value not in the list
|
||||
if (agentForm.value.model && !models.some(m => m.endsWith(`: ${agentForm.value.model}`))) {
|
||||
models.push(agentForm.value.model)
|
||||
}
|
||||
return models
|
||||
})
|
||||
|
||||
// Model display value for dropdown
|
||||
const modelDisplayValue = computed({
|
||||
get() {
|
||||
if (!agentForm.value.model) return ''
|
||||
const found = availableModels.value.find(m => m.endsWith(`: ${agentForm.value.model}`))
|
||||
return found || agentForm.value.model
|
||||
},
|
||||
set(val) {
|
||||
agentForm.value.model = val.includes(': ') ? val.split(': ')[1] : val
|
||||
}
|
||||
})
|
||||
|
||||
// ============ Room list state ============
|
||||
const rooms = ref([])
|
||||
const roomsLoading = ref(false)
|
||||
const providers = ref([])
|
||||
const showCreate = ref(false)
|
||||
const creating = ref(false)
|
||||
const newRoom = ref({ title: '', task: '', max_rounds: 5, agent_ids: [] })
|
||||
const showAddToRoom = ref(false)
|
||||
|
||||
// ============ Selected room state ============
|
||||
const selectedId = ref(null)
|
||||
const room = ref(null)
|
||||
const messages = ref([])
|
||||
const messagesLoading = ref(false)
|
||||
const streaming = ref(false)
|
||||
const error = ref('')
|
||||
const messagesContainer = ref(null)
|
||||
|
||||
// Room agent editing
|
||||
const editingRoomAgent = ref(null)
|
||||
const roomAgentForm = ref({})
|
||||
const roomAgentSaving = ref(false)
|
||||
|
||||
let abortController = null
|
||||
|
||||
const statusMap = {
|
||||
idle: { label: '就绪', class: 'status-idle' },
|
||||
running: { label: '进行中', class: 'status-running' },
|
||||
paused: { label: '已暂停', class: 'status-paused' },
|
||||
completed: { label: '已完成', class: 'status-completed' },
|
||||
error: { label: '错误', class: 'status-error' }
|
||||
}
|
||||
|
||||
const roomAgents = computed(() => room.value?.agents || [])
|
||||
const canEditRoom = computed(() => room.value?.status !== 'running' && !streaming.value)
|
||||
|
||||
// ============ Agent Pool ============
|
||||
|
||||
async function loadAgentPool() {
|
||||
agentPoolLoading.value = true
|
||||
try {
|
||||
const res = await agentsAPI.list()
|
||||
agentPool.value = res.data || []
|
||||
} catch (e) {
|
||||
console.error('Failed to load agents:', e)
|
||||
} finally {
|
||||
agentPoolLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function onProviderChange() {
|
||||
const p = providers.value.find(p => p.id === agentForm.value.provider_id)
|
||||
agentForm.value.model = p ? p.default_model : ''
|
||||
}
|
||||
|
||||
function startCreateAgent() {
|
||||
editingAgent.value = 'new'
|
||||
agentForm.value = {
|
||||
name: '', role: '', provider_id: null, model: '',
|
||||
system_prompt: 'You are a helpful AI assistant.', color: randomColor()
|
||||
}
|
||||
}
|
||||
|
||||
function startEditAgent(agent) {
|
||||
editingAgent.value = agent
|
||||
agentForm.value = { ...agent }
|
||||
}
|
||||
|
||||
function cancelAgentEdit() {
|
||||
editingAgent.value = null
|
||||
agentForm.value = {}
|
||||
}
|
||||
|
||||
async function saveAgent() {
|
||||
if (!agentForm.value.name) return
|
||||
agentSaving.value = true
|
||||
try {
|
||||
if (editingAgent.value === 'new') {
|
||||
await agentsAPI.create(agentForm.value)
|
||||
} else {
|
||||
await agentsAPI.update(editingAgent.value.id, agentForm.value)
|
||||
}
|
||||
cancelAgentEdit()
|
||||
await loadAgentPool()
|
||||
} catch (e) {
|
||||
console.error('Failed to save agent:', e)
|
||||
} finally {
|
||||
agentSaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteAgentFromPool(agent) {
|
||||
if (!confirm(`删除 Agent「${agent.name}」?它将仅从 Agent 池中移除,已加入房间的副本不受影响。`)) return
|
||||
try {
|
||||
await agentsAPI.delete(agent.id)
|
||||
cancelAgentEdit()
|
||||
await loadAgentPool()
|
||||
} catch (e) {
|
||||
console.error('Failed to delete agent:', e)
|
||||
}
|
||||
}
|
||||
|
||||
// ============ Room list ============
|
||||
|
||||
async function loadRooms() {
|
||||
roomsLoading.value = true
|
||||
try {
|
||||
const res = await chatRoomsAPI.list()
|
||||
rooms.value = res.data?.items || []
|
||||
} catch (e) {
|
||||
console.error('Failed to load rooms:', e)
|
||||
} finally {
|
||||
roomsLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadProviders() {
|
||||
try {
|
||||
const res = await providersAPI.list()
|
||||
providers.value = res.data || []
|
||||
} catch (e) {
|
||||
console.error('Failed to load providers:', e)
|
||||
}
|
||||
}
|
||||
|
||||
async function createRoom() {
|
||||
if (!newRoom.value.title || !newRoom.value.task) return
|
||||
creating.value = true
|
||||
try {
|
||||
// Build agents list from selected agent_ids
|
||||
const agents = newRoom.value.agent_ids
|
||||
.map(id => agentPool.value.find(a => a.id === id))
|
||||
.filter(Boolean)
|
||||
.map(a => ({ agent_id: a.id }))
|
||||
|
||||
const res = await chatRoomsAPI.create({
|
||||
title: newRoom.value.title,
|
||||
task: newRoom.value.task,
|
||||
max_rounds: newRoom.value.max_rounds,
|
||||
agents
|
||||
})
|
||||
showCreate.value = false
|
||||
resetNewRoom()
|
||||
await loadRooms()
|
||||
const created = res.data
|
||||
if (created?.id) selectRoom(created.id)
|
||||
} catch (e) {
|
||||
console.error('Failed to create room:', e)
|
||||
} finally {
|
||||
creating.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function resetNewRoom() {
|
||||
newRoom.value = { title: '', task: '', max_rounds: 5, agent_ids: [] }
|
||||
}
|
||||
|
||||
function toggleAgentInNewRoom(agentId) {
|
||||
const idx = newRoom.value.agent_ids.indexOf(agentId)
|
||||
if (idx >= 0) {
|
||||
newRoom.value.agent_ids.splice(idx, 1)
|
||||
} else {
|
||||
newRoom.value.agent_ids.push(agentId)
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteRoom(id) {
|
||||
if (!confirm('确定删除此聊天室?')) return
|
||||
try {
|
||||
await chatRoomsAPI.delete(id)
|
||||
if (selectedId.value === id) {
|
||||
selectedId.value = null
|
||||
room.value = null
|
||||
messages.value = []
|
||||
}
|
||||
await loadRooms()
|
||||
} catch (e) {
|
||||
console.error('Failed to delete room:', e)
|
||||
}
|
||||
}
|
||||
|
||||
// ============ Select room ============
|
||||
|
||||
async function selectRoom(id) {
|
||||
if (streaming.value) {
|
||||
if (abortController) abortController.abort()
|
||||
streaming.value = false
|
||||
}
|
||||
selectedId.value = id
|
||||
sidebarTab.value = 'roomAgents'
|
||||
error.value = ''
|
||||
editingRoomAgent.value = null
|
||||
messagesLoading.value = true
|
||||
try {
|
||||
const [roomRes, msgRes] = await Promise.all([
|
||||
chatRoomsAPI.get(id),
|
||||
chatRoomsAPI.getMessages(id)
|
||||
])
|
||||
room.value = roomRes.data
|
||||
messages.value = msgRes.data?.messages || []
|
||||
if (msgRes.data?.room) room.value = msgRes.data.room
|
||||
await nextTick()
|
||||
scrollToBottom()
|
||||
} catch (e) {
|
||||
console.error('Failed to load room:', e)
|
||||
error.value = '加载失败'
|
||||
} finally {
|
||||
messagesLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function scrollToBottom() {
|
||||
if (messagesContainer.value) {
|
||||
messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
|
||||
}
|
||||
}
|
||||
|
||||
// ============ SSE Streaming ============
|
||||
|
||||
async function startRoom() {
|
||||
if (!selectedId.value || streaming.value) return
|
||||
streaming.value = true
|
||||
error.value = ''
|
||||
abortController = new AbortController()
|
||||
|
||||
try {
|
||||
const token = localStorage.getItem('access_token')
|
||||
const url = chatRoomsAPI.start(selectedId.value)
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' },
|
||||
signal: abortController.signal
|
||||
})
|
||||
if (!response.ok) {
|
||||
const err = await response.json().catch(() => ({}))
|
||||
throw new Error(err.message || `HTTP ${response.status}`)
|
||||
}
|
||||
const reader = response.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let buffer = ''
|
||||
while (true) {
|
||||
const { done, value } = await reader.read()
|
||||
if (value) buffer += decoder.decode(value, { stream: true })
|
||||
if (done) break
|
||||
const lines = buffer.split('\n')
|
||||
buffer = lines.pop() || ''
|
||||
let currentEvent = ''
|
||||
for (const line of lines) {
|
||||
if (line.startsWith('event: ')) currentEvent = line.slice(7).trim()
|
||||
else if (line.startsWith('data: ')) {
|
||||
try { handleSSEEvent(currentEvent, JSON.parse(line.slice(6))) } catch (e) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
if (e.name !== 'AbortError') error.value = e.message
|
||||
} finally {
|
||||
streaming.value = false
|
||||
if (selectedId.value) {
|
||||
const res = await chatRoomsAPI.get(selectedId.value)
|
||||
room.value = res.data
|
||||
await loadRooms()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function handleSSEEvent(event, data) {
|
||||
switch (event) {
|
||||
case 'message': messages.value.push(data); nextTick(scrollToBottom); break
|
||||
case 'room_started': room.value = { ...room.value, status: 'running' }; break
|
||||
case 'room_completed': room.value = { ...room.value, status: 'completed' }; break
|
||||
case 'room_paused': room.value = { ...room.value, status: 'paused' }; break
|
||||
case 'round_start': room.value = { ...room.value, current_round: data.round }; break
|
||||
case 'agent_error': error.value = `Agent ${data.agent} 出错: ${data.error}`; break
|
||||
case 'error': error.value = data.content || '未知错误'; break
|
||||
}
|
||||
}
|
||||
|
||||
async function stopRoom() {
|
||||
try {
|
||||
if (abortController) { abortController.abort(); abortController = null }
|
||||
await chatRoomsAPI.stop(selectedId.value)
|
||||
room.value = { ...room.value, status: 'paused' }
|
||||
streaming.value = false
|
||||
} catch (e) { console.error('Failed to stop room:', e) }
|
||||
}
|
||||
|
||||
async function resetRoom() {
|
||||
if (!confirm('确定重置?将清除所有消息')) return
|
||||
try {
|
||||
await chatRoomsAPI.reset(selectedId.value)
|
||||
messages.value = []
|
||||
const res = await chatRoomsAPI.get(selectedId.value)
|
||||
room.value = res.data
|
||||
} catch (e) { console.error('Failed to reset room:', e) }
|
||||
}
|
||||
|
||||
// ============ Room Agent Management ============
|
||||
|
||||
async function addAgentToRoom(agentFromPool) {
|
||||
if (!canEditRoom.value) return
|
||||
try {
|
||||
await chatRoomsAPI.addAgent(selectedId.value, { agent_id: agentFromPool.id })
|
||||
const res = await chatRoomsAPI.get(selectedId.value)
|
||||
room.value = res.data
|
||||
} catch (e) {
|
||||
console.error('Failed to add agent:', e)
|
||||
}
|
||||
}
|
||||
|
||||
function startEditRoomAgent(agent) {
|
||||
editingRoomAgent.value = agent
|
||||
roomAgentForm.value = { ...agent }
|
||||
}
|
||||
|
||||
function cancelRoomAgentEdit() {
|
||||
editingRoomAgent.value = null
|
||||
roomAgentForm.value = {}
|
||||
}
|
||||
|
||||
async function saveRoomAgent() {
|
||||
roomAgentSaving.value = true
|
||||
try {
|
||||
await chatRoomsAPI.updateAgent(selectedId.value, editingRoomAgent.value.id, roomAgentForm.value)
|
||||
cancelRoomAgentEdit()
|
||||
const res = await chatRoomsAPI.get(selectedId.value)
|
||||
room.value = res.data
|
||||
} catch (e) {
|
||||
console.error('Failed to save room agent:', e)
|
||||
} finally {
|
||||
roomAgentSaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteRoomAgent(agent) {
|
||||
if (!confirm(`从房间移除 Agent「${agent.name}」?`)) return
|
||||
try {
|
||||
await chatRoomsAPI.deleteAgent(selectedId.value, agent.id)
|
||||
cancelRoomAgentEdit()
|
||||
const res = await chatRoomsAPI.get(selectedId.value)
|
||||
room.value = res.data
|
||||
} catch (e) {
|
||||
console.error('Failed to remove agent:', e)
|
||||
}
|
||||
}
|
||||
|
||||
function randomColor() {
|
||||
const colors = ['#2563eb', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6', '#ec4899', '#06b6d4', '#84cc16']
|
||||
return colors[Math.floor(Math.random() * colors.length)]
|
||||
}
|
||||
|
||||
watch(messages, () => { nextTick(scrollToBottom) }, { deep: true })
|
||||
|
||||
onMounted(() => {
|
||||
loadAgentPool()
|
||||
loadRooms()
|
||||
loadProviders()
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
if (abortController) abortController.abort()
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="page-container chat-rooms">
|
||||
<div class="rooms-layout">
|
||||
<!-- Left sidebar -->
|
||||
<aside class="rooms-sidebar">
|
||||
<!-- Tab bar -->
|
||||
<div class="sidebar-tabs">
|
||||
<button class="tab-btn" :class="{ active: sidebarTab === 'rooms' }" @click="sidebarTab = 'rooms'">
|
||||
💬 聊天室
|
||||
</button>
|
||||
<button class="tab-btn" :class="{ active: sidebarTab === 'agents' }" @click="sidebarTab = 'agents'">
|
||||
🤖 Agent 池
|
||||
</button>
|
||||
<button v-if="room" class="tab-btn" :class="{ active: sidebarTab === 'roomAgents' }" @click="sidebarTab = 'roomAgents'">
|
||||
🏠 {{ room.title }}
|
||||
</button>
|
||||
<button class="tab-btn tab-btn-add" title="新建 Agent" @click="startCreateAgent">
|
||||
+
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Agent Pool tab -->
|
||||
<div v-if="sidebarTab === 'agents'" class="sidebar-tab-content">
|
||||
<div class="sidebar-header">
|
||||
<span class="sidebar-title">Agent 池</span>
|
||||
</div>
|
||||
<div v-if="agentPoolLoading" class="sidebar-loading"><div class="spinner-small"></div></div>
|
||||
<div v-else-if="agentPool.length === 0" class="sidebar-empty">暂无 Agent,点击 + 创建</div>
|
||||
<div v-else class="pool-list">
|
||||
<div v-for="agent in agentPool" :key="agent.id"
|
||||
class="pool-item"
|
||||
@click="startEditAgent(agent)"
|
||||
>
|
||||
<span class="agent-dot" :style="{ background: agent.color }">{{ agent.name.charAt(0) }}</span>
|
||||
<div class="agent-info">
|
||||
<span class="agent-name" :style="{ color: agent.color }">{{ agent.name }}</span>
|
||||
<span v-if="agent.role" class="agent-role">{{ agent.role }}</span>
|
||||
<span class="agent-model">{{ agent.model || 'default' }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Room list tab -->
|
||||
<div v-if="sidebarTab === 'rooms'" class="sidebar-tab-content">
|
||||
<div class="sidebar-header">
|
||||
<span class="sidebar-title">聊天室</span>
|
||||
<button class="btn-new-room" @click="showCreate = true">+</button>
|
||||
</div>
|
||||
<div v-if="roomsLoading" class="sidebar-loading"><div class="spinner-small"></div></div>
|
||||
<div v-else-if="rooms.length === 0" class="sidebar-empty">暂无聊天室</div>
|
||||
<div v-else class="room-list">
|
||||
<div v-for="r in rooms" :key="r.id" class="room-item" :class="{ active: selectedId === r.id }" @click="selectRoom(r.id)">
|
||||
<div class="room-item-header">
|
||||
<span class="room-item-title">{{ r.title }}</span>
|
||||
<span class="status-dot" :class="statusMap[r.status]?.class" :title="statusMap[r.status]?.label"></span>
|
||||
</div>
|
||||
<div class="room-item-meta">
|
||||
<span class="meta-text">
|
||||
<span v-for="a in (r.agents || []).slice(0, 4)" :key="a.id" class="mini-dot" :style="{ background: a.color }" :title="a.name"></span>
|
||||
{{ r.agents?.length || 0 }} Agents
|
||||
</span>
|
||||
<span class="meta-text">{{ r.current_round }}/{{ r.max_rounds }}</span>
|
||||
<div class="room-item-actions" @click.stop>
|
||||
<button @click="deleteRoom(r.id)" class="btn-icon-sm" title="删除">
|
||||
<svg viewBox="0 0 24 24" width="12" height="12" fill="none" stroke="currentColor" stroke-width="2"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path></svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Room agents tab (shown when a room is selected) -->
|
||||
<div v-if="sidebarTab === 'roomAgents' && room" class="sidebar-tab-content">
|
||||
<div class="sidebar-header">
|
||||
<button class="btn-back" @click="sidebarTab = 'rooms'; selectedId = null; room = null">←</button>
|
||||
<span class="sidebar-title">{{ room.title }}</span>
|
||||
<button v-if="canEditRoom" class="btn-new-room" @click="showAddToRoom = true">+</button>
|
||||
</div>
|
||||
<div class="room-agent-list">
|
||||
<div v-for="agent in roomAgents" :key="agent.id"
|
||||
class="agent-row"
|
||||
:class="{ editing: editingRoomAgent?.id === agent.id }"
|
||||
@click="startEditRoomAgent(agent)"
|
||||
>
|
||||
<span class="agent-dot" :style="{ background: agent.color }">{{ agent.name.charAt(0) }}</span>
|
||||
<div class="agent-info">
|
||||
<span class="agent-name" :style="{ color: agent.color }">{{ agent.name }}</span>
|
||||
<span v-if="agent.role" class="agent-role">{{ agent.role }}</span>
|
||||
<span class="agent-model">{{ agent.model || 'default' }}</span>
|
||||
</div>
|
||||
<button v-if="canEditRoom" class="btn-del-agent" @click.stop="deleteRoomAgent(agent)">
|
||||
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
|
||||
</button>
|
||||
</div>
|
||||
<div v-if="roomAgents.length === 0" class="no-agents">
|
||||
<p>暂无 Agent</p>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Room agent edit form -->
|
||||
<div v-if="editingRoomAgent" class="agent-form">
|
||||
<div class="agent-form-header">
|
||||
<span>编辑房间 Agent</span>
|
||||
<button class="btn-close-xs" @click="cancelRoomAgentEdit">×</button>
|
||||
</div>
|
||||
<div class="agent-form-body">
|
||||
<div class="form-row"><input v-model="roomAgentForm.name" placeholder="名称" class="fi" /><input type="color" v-model="roomAgentForm.color" class="fi-color" /></div>
|
||||
<div class="form-row">
|
||||
<select v-model="roomAgentForm.provider_id" class="fi">
|
||||
<option :value="null">默认 Provider</option>
|
||||
<option v-for="p in providers" :key="p.id" :value="p.id">{{ p.name }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row"><input v-model="roomAgentForm.model" placeholder="模型" class="fi" /></div>
|
||||
<div class="form-row"><textarea v-model="roomAgentForm.system_prompt" rows="2" placeholder="系统提示词" class="fi"></textarea></div>
|
||||
<div class="form-row" style="justify-content: flex-end; gap: 0.4rem;">
|
||||
<button class="btn-xs btn-secondary" @click="cancelRoomAgentEdit">取消</button>
|
||||
<button class="btn-xs btn-primary" @click="saveRoomAgent" :disabled="!roomAgentForm.name || roomAgentSaving">{{ roomAgentSaving ? '...' : '保存' }}</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sidebar-room-info">
|
||||
<div class="info-item"><span class="info-label">任务</span><p class="info-value">{{ room.task }}</p></div>
|
||||
<div class="info-item"><span class="info-label">轮次</span><p class="info-value">{{ room.max_rounds }}</p></div>
|
||||
</div>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<!-- Right content -->
|
||||
<main class="rooms-content">
|
||||
<div v-if="!room" class="empty-content">
|
||||
<div class="empty-icon">
|
||||
<svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
|
||||
<path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path><circle cx="9" cy="7" r="4"></circle>
|
||||
<path d="M23 21v-2a4 4 0 0 0-3-3.87"></path><path d="M16 3.13a4 4 0 0 1 0 7.75"></path>
|
||||
</svg>
|
||||
</div>
|
||||
<p>选择一个聊天室或创建新的</p>
|
||||
</div>
|
||||
|
||||
<div v-else class="room-detail">
|
||||
<div class="room-toolbar">
|
||||
<div class="toolbar-info">
|
||||
<h3>{{ room.title }}</h3>
|
||||
<div class="toolbar-badges">
|
||||
<span class="status-badge" :class="statusMap[room.status]?.class">{{ statusMap[room.status]?.label }}</span>
|
||||
<span class="round-badge" v-if="room.current_round > 0">R{{ room.current_round }}/{{ room.max_rounds }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="toolbar-actions">
|
||||
<button v-if="!streaming && room.status !== 'running'" class="btn-ctrl btn-start" @click="startRoom">
|
||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg>
|
||||
{{ room.status === 'paused' ? '继续' : '开始' }}
|
||||
</button>
|
||||
<button v-if="streaming || room.status === 'running'" class="btn-ctrl btn-stop" @click="stopRoom">
|
||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><rect x="6" y="4" width="4" height="16"></rect><rect x="14" y="4" width="4" height="16"></rect></svg>
|
||||
停止
|
||||
</button>
|
||||
<button v-if="room.status !== 'running' && messages.length > 0" class="btn-ctrl btn-reset" @click="resetRoom">
|
||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="23 4 23 10 17 10"></polyline><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"></path></svg>
|
||||
重置
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="error" class="error-bar">{{ error }}<button @click="error = ''">×</button></div>
|
||||
<div class="chat-messages" ref="messagesContainer">
|
||||
<div v-if="messagesLoading" class="loading-messages"><div class="spinner-small"></div><span>加载中...</span></div>
|
||||
<div v-else-if="messages.length === 0" class="chat-empty"><p>点击「开始」启动多 Agent 对话</p></div>
|
||||
<div v-else>
|
||||
<MessageBubble v-for="msg in messages" :key="msg.id" :message="msg" />
|
||||
</div>
|
||||
<div v-if="streaming" class="streaming-hint"><div class="spinner-sm"></div><span>Agent 正在思考...</span></div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<!-- Create room modal -->
|
||||
<div v-if="showCreate" class="modal-overlay" @click.self="showCreate = false">
|
||||
<div class="modal">
|
||||
<div class="modal-head"><h3>新建聊天室</h3><button class="btn-close" @click="showCreate = false">×</button></div>
|
||||
<div class="modal-body">
|
||||
<div class="fg"><label>标题</label><input v-model="newRoom.title" placeholder="项目架构设计讨论" /></div>
|
||||
<div class="fg"><label>任务描述</label><textarea v-model="newRoom.task" rows="3" placeholder="描述需要 Agent 讨论的问题..."></textarea></div>
|
||||
<div class="fg"><label>最大轮次</label><input v-model.number="newRoom.max_rounds" type="number" min="1" max="20" /></div>
|
||||
<div class="agents-select">
|
||||
<label class="agents-select-label">选择 Agent</label>
|
||||
<div v-if="agentPool.length === 0" class="no-agents-hint">请先在左侧 Agent 池中创建 Agent</div>
|
||||
<div v-else class="agents-checkboxes">
|
||||
<label v-for="a in agentPool" :key="a.id" class="agent-checkbox" :class="{ checked: newRoom.agent_ids.includes(a.id) }">
|
||||
<input type="checkbox" :value="a.id" v-model="newRoom.agent_ids" />
|
||||
<span class="agent-dot" :style="{ background: a.color }">{{ a.name.charAt(0) }}</span>
|
||||
<span class="agent-checkbox-name" :style="{ color: a.color }">{{ a.name }}</span>
|
||||
<span class="agent-checkbox-model">{{ a.model || 'default' }}</span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-foot">
|
||||
<button class="btn-secondary" @click="showCreate = false">取消</button>
|
||||
<button class="btn-primary" @click="createRoom" :disabled="!newRoom.title || !newRoom.task || newRoom.agent_ids.length === 0 || creating">{{ creating ? '创建中...' : '创建' }}</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Add agent to room modal -->
|
||||
<div v-if="showAddToRoom" class="modal-overlay" @click.self="showAddToRoom = false">
|
||||
<div class="modal">
|
||||
<div class="modal-head"><h3>添加 Agent 到房间</h3><button class="btn-close" @click="showAddToRoom = false">×</button></div>
|
||||
<div class="modal-body">
|
||||
<div v-if="agentPool.length === 0" class="no-agents-hint">请先在左侧 Agent 池中创建 Agent</div>
|
||||
<div v-else class="agents-checkboxes">
|
||||
<div v-for="a in agentPool" :key="a.id" class="agent-pick-row" @click="addAgentToRoom(a); showAddToRoom = false">
|
||||
<span class="agent-dot" :style="{ background: a.color }">{{ a.name.charAt(0) }}</span>
|
||||
<span class="agent-checkbox-name" :style="{ color: a.color }">{{ a.name }}</span>
|
||||
<span class="agent-checkbox-model">{{ a.model || 'default' }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Agent create/edit modal -->
|
||||
<div v-if="editingAgent" class="modal-overlay" @click.self="cancelAgentEdit">
|
||||
<div class="modal">
|
||||
<div class="modal-head"><h3>{{ editingAgent === 'new' ? '新建 Agent' : '编辑 Agent' }}</h3><button class="btn-close" @click="cancelAgentEdit">×</button></div>
|
||||
<div class="modal-body">
|
||||
<div class="fg"><label>名称</label><input v-model="agentForm.name" placeholder="如: 架构师" /></div>
|
||||
<div class="fg"><label>角色</label><input v-model="agentForm.role" placeholder="如: 架构师、开发者、审查员" /></div>
|
||||
<div class="fg-row">
|
||||
<div class="fg" style="flex: 1;">
|
||||
<label>Provider</label>
|
||||
<select v-model="agentForm.provider_id" @change="onProviderChange">
|
||||
<option :value="null">默认 Provider</option>
|
||||
<option v-for="p in providers" :key="p.id" :value="p.id">{{ p.name }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="fg fg-color">
|
||||
<label>颜色</label>
|
||||
<div class="color-picker-wrap">
|
||||
<input type="color" v-model="agentForm.color" class="color-input" />
|
||||
<span class="color-preview" :style="{ background: agentForm.color }"></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="fg">
|
||||
<label>模型</label>
|
||||
<select v-model="modelDisplayValue">
|
||||
<option value="">自动选择</option>
|
||||
<option v-for="m in availableModels" :key="m" :value="m">{{ m }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="fg"><label>系统提示词</label><textarea v-model="agentForm.system_prompt" rows="3" placeholder="定义 Agent 的角色和行为..."></textarea></div>
|
||||
</div>
|
||||
<div class="modal-foot">
|
||||
<button v-if="editingAgent !== 'new'" class="btn-danger" @click="deleteAgentFromPool(editingAgent)">删除</button>
|
||||
<div style="flex: 1;"></div>
|
||||
<button class="btn-secondary" @click="cancelAgentEdit">取消</button>
|
||||
<button class="btn-primary" @click="saveAgent" :disabled="!agentForm.name || agentSaving">{{ agentSaving ? '保存中...' : '保存' }}</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
/* Layout */
|
||||
.page-container { padding: 0 !important; overflow: hidden; height: 100% !important; min-height: 0; display: flex; flex-direction: column; }
|
||||
.chat-rooms { height: 100% !important; }
|
||||
.rooms-layout { display: flex; gap: 1rem; height: 100%; flex: 1; min-height: 0; }
|
||||
|
||||
/* ===== Left Sidebar ===== */
|
||||
.rooms-sidebar { width: 22%; min-width: 240px; max-width: 320px; background: var(--bg-primary); border: 1px solid var(--border-light); border-radius: 12px; display: flex; flex-direction: column; overflow: hidden; }
|
||||
|
||||
/* Tab bar */
|
||||
.sidebar-tabs { display: flex; border-bottom: 1px solid var(--border-light); flex-shrink: 0; background: var(--bg-secondary); }
|
||||
.tab-btn { flex: 1; min-width: 0; padding: 0.55rem 0.3rem; background: none; border: none; border-bottom: 2px solid transparent; font-size: 0.72rem; color: var(--text-secondary); cursor: pointer; transition: all 0.2s; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||||
.tab-btn:hover { color: var(--text-primary); }
|
||||
.tab-btn.active { color: var(--accent-primary); border-bottom-color: var(--accent-primary); }
|
||||
.tab-btn-add { flex: 0 0 auto; width: 34px; padding: 0.55rem 0; font-size: 1.1rem; font-weight: 600; color: var(--accent-primary); border-bottom-color: transparent; }
|
||||
.tab-btn-add:hover { color: white; background: var(--accent-primary); }
|
||||
|
||||
/* Tab content */
|
||||
.sidebar-tab-content { display: flex; flex-direction: column; flex: 1; min-height: 0; overflow: hidden; }
|
||||
|
||||
/* Shared section styles */
|
||||
.sidebar-section { display: flex; flex-direction: column; overflow: hidden; border-bottom: 1px solid var(--border-light); }
|
||||
.sidebar-section:last-child { border-bottom: none; }
|
||||
|
||||
/* Pool & room list scrollable */
|
||||
.pool-list, .room-list, .room-agent-list { flex: 1; overflow-y: auto; }
|
||||
.pool-list { padding: 0.3rem 0.5rem; }
|
||||
.room-agent-list { padding: 0.3rem 0.5rem; }
|
||||
|
||||
.sidebar-header { display: flex; justify-content: space-between; align-items: center; padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--border-light); flex-shrink: 0; }
|
||||
.btn-back { background: none; border: none; color: var(--text-secondary); cursor: pointer; font-size: 1rem; padding: 0 0.3rem 0 0; line-height: 1; }
|
||||
.btn-back:hover { color: var(--accent-primary); }
|
||||
.sidebar-title { font-size: 0.8rem; font-weight: 700; color: var(--text-primary); }
|
||||
.btn-new-room { width: 26px; height: 26px; background: var(--accent-primary); color: white; border: none; border-radius: 6px; cursor: pointer; font-size: 0.95rem; font-weight: 600; display: flex; align-items: center; justify-content: center; transition: all 0.2s; line-height: 1; }
|
||||
.btn-new-room:hover { background: var(--accent-primary-hover); }
|
||||
|
||||
.sidebar-loading, .sidebar-empty { display: flex; align-items: center; justify-content: center; padding: 1.5rem; color: var(--text-secondary); font-size: 0.8rem; }
|
||||
|
||||
/* Agent pool list */
|
||||
.pool-item { display: flex; align-items: center; gap: 0.6rem; padding: 0.5rem 0.6rem; border-radius: 10px; cursor: pointer; transition: all 0.2s ease; border: 1px solid transparent; }
|
||||
.pool-item:hover, .pool-item.editing { background: var(--bg-secondary); border-color: var(--border-light); box-shadow: 0 2px 8px rgba(0,0,0,0.08); }
|
||||
.room-item { padding: 0.55rem 0.75rem; border-bottom: 1px solid var(--border-light); cursor: pointer; transition: all 0.15s; }
|
||||
.room-item:hover { background: var(--bg-hover); }
|
||||
.room-item.active { background: var(--accent-primary-light); border-left: 3px solid var(--accent-primary); }
|
||||
.room-item-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.2rem; }
|
||||
.room-item-title { font-size: 0.78rem; font-weight: 500; color: var(--text-primary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
|
||||
.status-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
|
||||
.status-idle { background: var(--text-secondary); }
|
||||
.status-running { background: #22c55e; animation: pulse 1.5s ease-in-out infinite; }
|
||||
.status-paused { background: #f59e0b; }
|
||||
.status-completed { background: #3b82f6; }
|
||||
.status-error { background: #ef4444; }
|
||||
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }
|
||||
.room-item-meta { display: flex; align-items: center; gap: 0.75rem; font-size: 0.7rem; color: var(--text-secondary); }
|
||||
.meta-text { display: flex; align-items: center; gap: 3px; }
|
||||
.mini-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
|
||||
.room-item-actions { margin-left: auto; }
|
||||
.btn-icon-sm { background: transparent; border: none; cursor: pointer; padding: 2px; border-radius: 3px; color: var(--text-secondary); transition: all 0.15s; display: flex; align-items: center; }
|
||||
.btn-icon-sm:hover { color: #ef4444; background: rgba(239,68,68,0.1); }
|
||||
|
||||
/* Shared agent row */
|
||||
.agent-row, .pool-item { display: flex; align-items: center; gap: 0.6rem; padding: 0.5rem 0.6rem; border-radius: 10px; cursor: pointer; transition: all 0.2s ease; border: 1px solid transparent; }
|
||||
.agent-row:hover, .agent-row.editing { background: var(--bg-secondary); border-color: var(--border-light); box-shadow: 0 2px 8px rgba(0,0,0,0.08); }
|
||||
.agent-dot { width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 0.75rem; font-weight: 700; color: white; flex-shrink: 0; box-shadow: 0 2px 8px rgba(0,0,0,0.15); transition: transform 0.2s ease; }
|
||||
.agent-row:hover .agent-dot, .pool-item:hover .agent-dot { transform: scale(1.1); }
|
||||
.agent-info { flex: 1; min-width: 0; }
|
||||
.agent-name { font-size: 0.8rem; font-weight: 600; display: block; }
|
||||
.agent-role { font-size: 0.65rem; color: var(--text-secondary); display: block; margin-top: 1px; }
|
||||
.agent-model { font-size: 0.6rem; color: var(--text-secondary); display: block; margin-top: 1px; opacity: 0.7; }
|
||||
.btn-del-agent { background: none; border: none; color: var(--text-secondary); cursor: pointer; padding: 2px; opacity: 0; transition: all 0.15s; }
|
||||
.agent-row:hover .btn-del-agent { opacity: 1; }
|
||||
.btn-del-agent:hover { color: #ef4444; }
|
||||
.no-agents { text-align: center; padding: 0.8rem 0; color: var(--text-secondary); font-size: 0.75rem; }
|
||||
.no-agents p { margin: 0; }
|
||||
|
||||
/* Room agent list */
|
||||
|
||||
/* Sidebar inline form */
|
||||
.btn-close-xs { background: none; border: none; font-size: 1.1rem; cursor: pointer; color: var(--text-secondary); padding: 0; line-height: 1; transition: color 0.15s; }
|
||||
.btn-close-xs:hover { color: var(--text-primary); }
|
||||
.form-row { margin-bottom: 0.5rem; display: flex; gap: 0.4rem; }
|
||||
.fi { width: 100%; padding: 0.45rem 0.6rem; border: 1px solid var(--border-light); border-radius: 8px; background: var(--bg-primary); color: var(--text-primary); font-size: 0.8rem; transition: all 0.2s ease; }
|
||||
.fi:focus { outline: none; border-color: var(--accent-primary); box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1); }
|
||||
.fi-color { width: 32px !important; height: 32px; padding: 2px !important; border-radius: 6px; cursor: pointer; flex-shrink: 0; }
|
||||
textarea.fi { resize: vertical; min-height: 50px; }
|
||||
|
||||
/* Sidebar room info */
|
||||
.sidebar-room-info { border-top: 1px solid var(--border-light); padding: 0.5rem 0.6rem; flex-shrink: 0; }
|
||||
.info-item { margin-bottom: 0.2rem; }
|
||||
.info-label { font-size: 0.6rem; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.5px; }
|
||||
.info-value { font-size: 0.75rem; color: var(--text-primary); margin: 0.1rem 0 0; line-height: 1.4; }
|
||||
|
||||
/* ===== Right Content ===== */
|
||||
.rooms-content { flex: 1; background: var(--bg-primary); border: 1px solid var(--border-light); border-radius: 12px; display: flex; flex-direction: column; overflow: hidden; min-height: 0; }
|
||||
.empty-content { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-secondary); }
|
||||
.empty-content .empty-icon { opacity: 0.4; margin-bottom: 0.75rem; }
|
||||
.empty-content p { font-size: 0.9rem; }
|
||||
|
||||
.room-detail { display: flex; flex-direction: column; height: 100%; overflow: hidden; }
|
||||
.room-toolbar { display: flex; align-items: center; gap: 0.75rem; padding: 0.5rem 1rem; border-bottom: 1px solid var(--border-light); flex-shrink: 0; }
|
||||
.toolbar-info { flex: 1; min-width: 0; }
|
||||
.toolbar-info h3 { font-size: 0.95rem; font-weight: 600; color: var(--text-primary); margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||||
.toolbar-badges { display: flex; gap: 0.5rem; align-items: center; margin-top: 2px; }
|
||||
.status-badge { font-size: 0.65rem; padding: 0.1rem 0.4rem; border-radius: 8px; font-weight: 600; }
|
||||
.round-badge { font-size: 0.65rem; color: var(--text-secondary); }
|
||||
|
||||
.toolbar-actions { display: flex; gap: 0.4rem; align-items: center; }
|
||||
.btn-ctrl { display: flex; align-items: center; gap: 0.3rem; padding: 0.35rem 0.75rem; border: none; border-radius: 6px; font-size: 0.8rem; cursor: pointer; font-weight: 500; }
|
||||
.btn-start { background: #22c55e; color: white; }
|
||||
.btn-start:hover { background: #16a34a; }
|
||||
.btn-stop { background: #ef4444; color: white; }
|
||||
.btn-stop:hover { background: #dc2626; }
|
||||
.btn-reset { background: var(--bg-secondary); color: var(--text-secondary); border: 1px solid var(--border-light); }
|
||||
.btn-reset:hover { border-color: var(--accent-primary); color: var(--accent-primary); }
|
||||
|
||||
.error-bar { padding: 0.5rem 1rem; background: rgba(239,68,68,0.1); color: #ef4444; font-size: 0.8rem; display: flex; justify-content: space-between; align-items: center; }
|
||||
.error-bar button { background: none; border: none; color: #ef4444; cursor: pointer; font-size: 1.2rem; }
|
||||
|
||||
.chat-messages { flex: 1; overflow-y: auto; padding: 0.75rem; }
|
||||
.chat-empty { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--text-secondary); font-size: 0.85rem; }
|
||||
.loading-messages { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 2rem; color: var(--text-secondary); font-size: 0.85rem; }
|
||||
.spinner-small { width: 24px; height: 24px; border: 3px solid var(--border-light); border-top-color: var(--accent-primary); border-radius: 50%; animation: spin 1s linear infinite; margin: 0 auto 0.5rem; }
|
||||
.spinner-sm { width: 16px; height: 16px; border: 2px solid var(--border-light); border-top-color: var(--accent-primary); border-radius: 50%; animation: spin 0.8s linear infinite; }
|
||||
@keyframes spin { to { transform: rotate(360deg); } }
|
||||
.streaming-hint { display: flex; align-items: center; gap: 0.5rem; padding: 0.75rem; color: var(--text-secondary); font-size: 0.8rem; }
|
||||
|
||||
/* ===== Buttons ===== */
|
||||
.btn-xs { padding: 0.2rem 0.5rem; font-size: 0.7rem; border-radius: 4px; border: none; cursor: pointer; }
|
||||
.btn-xs.btn-primary { background: var(--accent-primary); color: white; }
|
||||
.btn-xs.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
|
||||
.btn-xs.btn-secondary { background: var(--bg-secondary); color: var(--text-secondary); border: 1px solid var(--border-light); }
|
||||
.btn-xs.btn-danger { background: var(--danger-bg, rgba(239,68,68,0.1)); color: #ef4444; }
|
||||
.btn-primary { padding: 0.5rem 1rem; background: var(--accent-primary); color: white; border: none; border-radius: 6px; cursor: pointer; font-size: 0.85rem; transition: all 0.2s; }
|
||||
.btn-primary:hover { background: var(--accent-primary-hover); }
|
||||
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
|
||||
.btn-secondary { padding: 0.5rem 1rem; background: var(--bg-secondary); color: var(--text-primary); border: 1px solid var(--border-light); border-radius: 6px; cursor: pointer; font-size: 0.85rem; }
|
||||
.btn-danger { padding: 0.5rem 1rem; background: var(--danger-bg, rgba(239,68,68,0.1)); color: #ef4444; border: 1px solid rgba(239,68,68,0.3); border-radius: 6px; cursor: pointer; font-size: 0.85rem; }
|
||||
.btn-danger:hover { background: rgba(239,68,68,0.2); }
|
||||
|
||||
/* ===== Modal ===== */
|
||||
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 1000; }
|
||||
.modal { background: var(--bg-primary); border-radius: 16px; width: 480px; max-width: 95vw; max-height: 85vh; overflow-y: auto; }
|
||||
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 1rem 1.25rem; border-bottom: 1px solid var(--border-light); }
|
||||
.modal-head h3 { margin: 0; font-size: 1rem; }
|
||||
.btn-close { background: none; border: none; font-size: 1.4rem; cursor: pointer; color: var(--text-secondary); padding: 0; line-height: 1; }
|
||||
.modal-body { padding: 1.25rem; }
|
||||
.modal-foot { display: flex; justify-content: flex-end; gap: 0.5rem; padding: 1rem 1.25rem; border-top: 1px solid var(--border-light); }
|
||||
|
||||
.fg { margin-bottom: 1.25rem; }
|
||||
.fg label { display: block; font-size: 0.75rem; font-weight: 600; color: var(--text-secondary); margin-bottom: 0.4rem; text-transform: uppercase; letter-spacing: 0.5px; }
|
||||
.fg input, .fg textarea, .fg select { width: 100%; padding: 0.6rem 0.875rem; border: 1px solid var(--border-light); border-radius: 10px; background: var(--bg-secondary); color: var(--text-primary); font-size: 0.875rem; box-sizing: border-box; transition: all 0.2s ease; }
|
||||
.fg input:focus, .fg textarea:focus, .fg select:focus { outline: none; border-color: var(--accent-primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1); }
|
||||
.fg textarea { resize: vertical; min-height: 80px; }
|
||||
.fg-row { display: flex; gap: 0.75rem; margin-bottom: 1.25rem; }
|
||||
.fg-color { width: 70px; flex-shrink: 0; }
|
||||
.fg-color label { margin-bottom: 0.4rem; display: block; }
|
||||
.color-picker-wrap { position: relative; display: flex; align-items: center; gap: 0.5rem; height: 42px; padding: 4px 10px; border: 1px solid var(--border-light); border-radius: 10px; background: var(--bg-secondary); transition: all 0.2s ease; }
|
||||
.color-picker-wrap:hover { border-color: var(--accent-primary); }
|
||||
.color-input { width: 28px !important; height: 28px !important; padding: 0 !important; border: none !important; border-radius: 6px; cursor: pointer; background: none; flex-shrink: 0; }
|
||||
.color-preview { width: 28px; height: 28px; border-radius: 6px; flex-shrink: 0; border: 1px solid rgba(0,0,0,0.1); box-shadow: 0 2px 6px rgba(0,0,0,0.1); }
|
||||
|
||||
/* Agent selection in create room modal */
|
||||
.agents-select { margin-top: 0.5rem; }
|
||||
.agents-select-label { font-size: 0.8rem; font-weight: 600; color: var(--text-primary); display: block; margin-bottom: 0.5rem; }
|
||||
.no-agents-hint { color: var(--text-secondary); font-size: 0.8rem; padding: 0.5rem 0; }
|
||||
.agents-checkboxes { display: flex; flex-direction: column; gap: 0.4rem; max-height: 200px; overflow-y: auto; padding: 0.25rem; }
|
||||
.agent-checkbox { display: flex; align-items: center; gap: 0.75rem; padding: 0.6rem 0.75rem; border-radius: 10px; cursor: pointer; transition: all 0.2s ease; border: 1px solid transparent; }
|
||||
.agent-checkbox:hover { background: var(--bg-secondary); border-color: var(--border-light); box-shadow: 0 2px 8px rgba(0,0,0,0.06); }
|
||||
.agent-checkbox.checked { background: var(--accent-primary-light); border-color: var(--accent-primary); }
|
||||
.agent-checkbox input { display: none; }
|
||||
.agent-checkbox-name { font-size: 0.85rem; font-weight: 600; }
|
||||
.agent-checkbox-model { font-size: 0.7rem; color: var(--text-secondary); margin-left: auto; }
|
||||
|
||||
/* Agent pick row in add-to-room modal */
|
||||
.agent-pick-row { display: flex; align-items: center; gap: 0.75rem; padding: 0.7rem 0.8rem; border-radius: 10px; cursor: pointer; transition: all 0.2s ease; border: 1px solid transparent; }
|
||||
.agent-pick-row:hover { background: var(--bg-secondary); border-color: var(--border-light); box-shadow: 0 2px 8px rgba(0,0,0,0.06); }
|
||||
</style>
|
||||
|
|
@ -10,26 +10,34 @@
|
|||
<span class="section-icon">👤</span>
|
||||
<span class="section-text">用户信息</span>
|
||||
</div>
|
||||
<div class="settings-card">
|
||||
<div v-if="loadingUser" class="loading-small"><div class="spinner-small"></div>加载中...</div>
|
||||
<template v-else>
|
||||
<div class="settings-row">
|
||||
<div class="row-label">
|
||||
<span class="label-icon">用户名</span>
|
||||
</div>
|
||||
<div class="row-value">{{ userForm.username || '-' }}</div>
|
||||
</div>
|
||||
<div class="settings-row">
|
||||
<div class="row-label">
|
||||
<span class="label-icon">邮箱</span>
|
||||
</div>
|
||||
<div class="row-value">{{ userForm.email || '-' }}</div>
|
||||
</div>
|
||||
<div class="settings-row actions">
|
||||
<button @click="openUserModal" class="btn-action">编辑资料</button>
|
||||
<button @click="handleLogout" class="btn-action btn-logout">退出登录</button>
|
||||
</div>
|
||||
</template>
|
||||
<div v-if="loadingUser" class="loading-small"><div class="spinner-small"></div>加载中...</div>
|
||||
<div v-else class="settings-table-container">
|
||||
<table class="settings-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="setting-key-col">项目</th>
|
||||
<th>值</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td class="setting-key-col"><div class="setting-label">用户名</div></td>
|
||||
<td>{{ userForm.username || '-' }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="setting-key-col"><div class="setting-label">邮箱</div></td>
|
||||
<td>{{ userForm.email || '-' }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="2" class="table-footer">
|
||||
<button @click="openUserModal" class="btn-op">编辑资料</button>
|
||||
<button @click="handleLogout" class="btn-op btn-danger">退出登录</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
@ -39,19 +47,29 @@
|
|||
<span class="section-icon">🎨</span>
|
||||
<span class="section-text">外观</span>
|
||||
</div>
|
||||
<div class="settings-card">
|
||||
<div class="settings-row">
|
||||
<div class="row-label">
|
||||
<span class="row-title">夜间模式</span>
|
||||
<span class="row-desc">切换深色/浅色主题</span>
|
||||
</div>
|
||||
<div class="row-value">
|
||||
<label class="switch" @click.prevent="toggleTheme">
|
||||
<input type="checkbox" v-model="isDark" />
|
||||
<span class="slider"></span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="settings-table-container">
|
||||
<table class="settings-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="setting-key-col">设置项</th>
|
||||
<th>值</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td class="setting-key-col">
|
||||
<div class="setting-label">夜间模式</div>
|
||||
<div class="setting-desc">切换深色/浅色主题</div>
|
||||
</td>
|
||||
<td>
|
||||
<label class="switch" @click.prevent="toggleTheme">
|
||||
<input type="checkbox" v-model="isDark" />
|
||||
<span class="slider"></span>
|
||||
</label>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
@ -61,65 +79,67 @@
|
|||
<span class="section-icon">🤖</span>
|
||||
<span class="section-text">模型设置</span>
|
||||
</div>
|
||||
<div class="settings-card">
|
||||
<div class="settings-row">
|
||||
<div class="row-label">
|
||||
<span class="row-title">默认 Provider</span>
|
||||
<span class="row-desc">选择默认使用的 LLM Provider</span>
|
||||
</div>
|
||||
<div class="row-value">
|
||||
<select v-model="modelSettings.default_provider" class="inline-select" @change="saveDefaultProvider">
|
||||
<option :value="null" disabled>选择 Provider</option>
|
||||
<option v-for="p in providers" :key="p.id" :value="p.id">
|
||||
{{ p.name }} ({{ p.default_model }})
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="settings-row">
|
||||
<div class="row-label">温度 (Temperature)</div>
|
||||
<div class="row-value">
|
||||
<input v-model.number="modelSettings.temperature" type="number" min="0" max="2" step="0.1" class="inline-input" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="settings-row">
|
||||
<div class="row-label">最大 Tokens</div>
|
||||
<div class="row-value">
|
||||
<input v-model.number="modelSettings.max_tokens" type="number" min="100" max="32000" class="inline-input" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="settings-row">
|
||||
<div class="row-label">
|
||||
<span class="row-title">推理模式</span>
|
||||
<span class="row-desc">使用 CoT 推理,消耗更多 token 但更准确</span>
|
||||
</div>
|
||||
<div class="row-value">
|
||||
<label class="switch" @click.prevent="modelSettings.thinking_enabled = !modelSettings.thinking_enabled">
|
||||
<input v-model="modelSettings.thinking_enabled" type="checkbox" />
|
||||
<span class="slider"></span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="settings-row actions">
|
||||
<button @click="saveModelSettings" class="btn-primary">保存设置</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 系统提示词 -->
|
||||
<div class="settings-section">
|
||||
<div class="section-title">
|
||||
<span class="section-icon">💬</span>
|
||||
<span class="section-text">系统提示词</span>
|
||||
</div>
|
||||
<div class="settings-card">
|
||||
<div class="settings-row full">
|
||||
<textarea v-model="modelSettings.system_prompt" rows="4" placeholder="You are a helpful assistant."></textarea>
|
||||
<span class="hint-block">设置默认系统提示词,可在新建会话时覆盖</span>
|
||||
</div>
|
||||
<div class="settings-row actions">
|
||||
<button @click="saveSystemPrompt" class="btn-primary">保存提示词</button>
|
||||
</div>
|
||||
<div class="settings-table-container">
|
||||
<table class="settings-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="setting-key-col">设置项</th>
|
||||
<th>值</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td class="setting-key-col">
|
||||
<div class="setting-label">默认 Provider</div>
|
||||
<div class="setting-desc">选择默认使用的 LLM Provider</div>
|
||||
</td>
|
||||
<td>
|
||||
<select v-model="modelSettings.default_provider_id" class="inline-select" @change="saveDefaultProvider">
|
||||
<option :value="null" disabled>选择 Provider</option>
|
||||
<option v-for="p in providers" :key="p.id" :value="p.id">
|
||||
{{ p.name }} ({{ p.default_model }})
|
||||
</option>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="setting-key-col"><div class="setting-label">温度 (Temperature)</div></td>
|
||||
<td><input v-model.number="modelSettings.temperature" type="number" min="0" max="2" step="0.1" class="inline-input" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="setting-key-col"><div class="setting-label">最大 Tokens</div></td>
|
||||
<td><input v-model.number="modelSettings.max_tokens" type="number" min="100" max="32000" class="inline-input" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="setting-key-col">
|
||||
<div class="setting-label">推理模式</div>
|
||||
<div class="setting-desc">使用 CoT 推理,消耗更多 token 但更准确</div>
|
||||
</td>
|
||||
<td>
|
||||
<label class="switch" @click.prevent="modelSettings.thinking_enabled = !modelSettings.thinking_enabled">
|
||||
<input v-model="modelSettings.thinking_enabled" type="checkbox" />
|
||||
<span class="slider"></span>
|
||||
</label>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="setting-key-col">
|
||||
<div class="setting-label">系统提示词</div>
|
||||
<div class="setting-desc">设置默认系统提示词,可在新建会话时覆盖</div>
|
||||
</td>
|
||||
<td>
|
||||
<textarea v-model="modelSettings.system_prompt" rows="3" placeholder="You are a helpful assistant." class="table-textarea"></textarea>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="2" class="table-footer">
|
||||
<button @click="saveModelSettings" class="btn-primary">保存设置</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
@ -132,7 +152,7 @@
|
|||
|
||||
<div v-if="loading" class="loading"><div class="spinner"></div>加载中...</div>
|
||||
<div v-else-if="error" class="error">{{ error }}</div>
|
||||
<div v-else-if="providers.length" class="settings-table-container">
|
||||
<div v-else class="settings-table-container">
|
||||
<table class="settings-table">
|
||||
<thead>
|
||||
<tr>
|
||||
|
|
@ -174,15 +194,15 @@
|
|||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="4" class="table-footer">
|
||||
<button @click="showModal = true" class="btn-primary">+ 添加 Provider</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<!-- 添加按钮 -->
|
||||
<div class="settings-card">
|
||||
<div class="settings-row actions">
|
||||
<button @click="showModal = true" class="btn-primary">+ 添加 Provider</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 用户管理 (仅管理员可见) -->
|
||||
|
|
@ -342,12 +362,13 @@ const loadingUsers = ref(false)
|
|||
const usersError = ref('')
|
||||
|
||||
const modelSettings = ref({
|
||||
default_provider: null,
|
||||
default_provider_id: null,
|
||||
temperature: 0.7,
|
||||
max_tokens: 8192,
|
||||
thinking_enabled: false,
|
||||
system_prompt: 'You are a helpful assistant.'
|
||||
})
|
||||
const settingsLoaded = ref(false)
|
||||
|
||||
const fetchUserInfo = async () => {
|
||||
loadingUser.value = true
|
||||
|
|
@ -391,13 +412,18 @@ const updateUser = async () => {
|
|||
}
|
||||
|
||||
const saveModelSettings = async () => {
|
||||
localStorage.setItem('modelSettings', JSON.stringify(modelSettings.value))
|
||||
alert('模型设置已保存')
|
||||
}
|
||||
|
||||
const saveSystemPrompt = async () => {
|
||||
localStorage.setItem('defaultSystemPrompt', modelSettings.value.system_prompt)
|
||||
alert('系统提示词已保存')
|
||||
try {
|
||||
await authAPI.updateSettings({
|
||||
default_provider_id: modelSettings.value.default_provider_id,
|
||||
temperature: modelSettings.value.temperature,
|
||||
max_tokens: modelSettings.value.max_tokens,
|
||||
thinking_enabled: modelSettings.value.thinking_enabled,
|
||||
system_prompt: modelSettings.value.system_prompt
|
||||
})
|
||||
alert('设置已保存')
|
||||
} catch (e) {
|
||||
alert('保存失败: ' + e.message)
|
||||
}
|
||||
}
|
||||
|
||||
const providers = ref([])
|
||||
|
|
@ -421,16 +447,31 @@ const fetchProviders = async () => {
|
|||
const res = await providersAPI.list()
|
||||
if (res.success) {
|
||||
providers.value = res.data.providers || []
|
||||
const defaultProvider = providers.value.find(p => p.is_default)
|
||||
if (defaultProvider && !modelSettings.value.default_provider) {
|
||||
modelSettings.value.default_provider = defaultProvider.id
|
||||
}
|
||||
}
|
||||
else throw new Error(res.message)
|
||||
} catch (e) { error.value = e.message }
|
||||
finally { loading.value = false }
|
||||
}
|
||||
|
||||
const fetchSettings = async () => {
|
||||
try {
|
||||
const res = await authAPI.getSettings()
|
||||
if (res.success && res.data) {
|
||||
modelSettings.value = {
|
||||
default_provider_id: res.data.default_provider_id,
|
||||
temperature: res.data.temperature,
|
||||
max_tokens: res.data.max_tokens,
|
||||
thinking_enabled: res.data.thinking_enabled,
|
||||
system_prompt: res.data.system_prompt
|
||||
}
|
||||
settingsLoaded.value = true
|
||||
}
|
||||
} catch (e) {
|
||||
// 首次使用可能没有设置,用默认值
|
||||
console.warn('获取设置失败,使用默认值:', e)
|
||||
}
|
||||
}
|
||||
|
||||
const closeModal = () => {
|
||||
showModal.value = false
|
||||
editing.value = null
|
||||
|
|
@ -526,18 +567,10 @@ const toggleEnabled = async (p) => {
|
|||
|
||||
const saveDefaultProvider = async () => {
|
||||
try {
|
||||
// 取消所有 Provider 的默认状态
|
||||
for (const p of providers.value) {
|
||||
if (p.is_default && p.id !== modelSettings.value.default_provider) {
|
||||
await providersAPI.update(p.id, { is_default: false })
|
||||
}
|
||||
}
|
||||
// 设置选中的 Provider 为默认
|
||||
if (modelSettings.value.default_provider) {
|
||||
await providersAPI.update(modelSettings.value.default_provider, { is_default: true })
|
||||
}
|
||||
await fetchProviders()
|
||||
} catch (e) { alert('设置默认 Provider 失败: ' + e.message) }
|
||||
await authAPI.updateSettings({ default_provider_id: modelSettings.value.default_provider_id })
|
||||
} catch (e) {
|
||||
alert('设置默认 Provider 失败: ' + e.message)
|
||||
}
|
||||
}
|
||||
|
||||
const fetchUsers = async () => {
|
||||
|
|
@ -577,9 +610,10 @@ const getPermissionName = (level) => {
|
|||
|
||||
onMounted(() => {
|
||||
fetchUserInfo()
|
||||
fetchSettings()
|
||||
fetchProviders()
|
||||
fetchUsers()
|
||||
|
||||
|
||||
// 检查是否是管理员
|
||||
const userData = localStorage.getItem('user')
|
||||
if (userData) {
|
||||
|
|
@ -588,19 +622,6 @@ onMounted(() => {
|
|||
isAdmin.value = user.permission_level === 4
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
const savedSettings = localStorage.getItem('modelSettings')
|
||||
if (savedSettings) {
|
||||
try {
|
||||
const parsed = JSON.parse(savedSettings)
|
||||
modelSettings.value = { ...modelSettings.value, ...parsed }
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
const savedPrompt = localStorage.getItem('defaultSystemPrompt')
|
||||
if (savedPrompt) {
|
||||
modelSettings.value.system_prompt = savedPrompt
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
|
|
@ -614,25 +635,9 @@ onMounted(() => {
|
|||
.section-icon { font-size: 1rem; }
|
||||
.section-text { font-size: 1rem; font-weight: 700; color: var(--text-primary); }
|
||||
|
||||
/* 设置卡片 */
|
||||
.settings-card { background: var(--bg-primary); border: 1px solid var(--border-light); border-radius: 12px; overflow: hidden; }
|
||||
|
||||
/* 设置行 */
|
||||
.settings-row { display: flex; align-items: center; padding: 0.85rem 1rem; border-bottom: 1px solid var(--border-light); }
|
||||
.settings-row:last-child { border-bottom: none; }
|
||||
.settings-row.full { flex-direction: column; align-items: stretch; }
|
||||
.settings-row.actions { justify-content: flex-end; gap: 0.5rem; background: var(--bg-secondary); }
|
||||
.row-label { min-width: 140px; color: var(--text-secondary); font-size: 0.85rem; flex-shrink: 0; }
|
||||
.row-title { display: block; font-weight: 500; color: var(--text-primary); font-size: 0.9rem; }
|
||||
.row-desc { display: block; font-size: 0.75rem; color: var(--text-secondary); margin-top: 0.15rem; }
|
||||
.row-value { flex: 1; display: flex; align-items: center; justify-content: flex-end; }
|
||||
.row-value .switch { margin-left: auto; }
|
||||
|
||||
/* 内联输入框 */
|
||||
.inline-select, .inline-input { padding: 0.5rem 0.75rem; border: 1px solid var(--border-input); border-radius: 6px; background: var(--bg-input); color: var(--text-primary); font-size: 0.85rem; }
|
||||
.inline-input { width: 120px; }
|
||||
textarea { width: 100%; padding: 0.75rem; border: 1px solid var(--border-input); border-radius: 8px; background: var(--bg-input); color: var(--text-primary); font-size: 0.85rem; resize: vertical; min-height: 80px; box-sizing: border-box; }
|
||||
.hint-block { font-size: 0.75rem; color: var(--text-tertiary); margin-top: 0.5rem; }
|
||||
|
||||
/* 表格容器 */
|
||||
.settings-table-container { background: var(--bg-primary); border: 1px solid var(--border-light); border-radius: 12px; overflow: hidden; }
|
||||
|
|
@ -647,6 +652,19 @@ textarea { width: 100%; padding: 0.75rem; border: 1px solid var(--border-input);
|
|||
.info-col { width: 60%; min-width: 200px; }
|
||||
.switch-col { text-align: center; width: 80px; }
|
||||
.ops-col { width: 15%; min-width: 180px; text-align: center; }
|
||||
.setting-key-col { width: 25%; min-width: 160px; }
|
||||
|
||||
/* 设置项标签 */
|
||||
.setting-label { font-weight: 500; color: var(--text-primary); font-size: 0.9rem; }
|
||||
.setting-desc { font-size: 0.75rem; color: var(--text-secondary); margin-top: 0.15rem; }
|
||||
|
||||
/* 表格内 textarea */
|
||||
.table-textarea { width: 100%; padding: 0.5rem; border: 1px solid var(--border-input); border-radius: 6px; background: var(--bg-input); color: var(--text-primary); font-size: 0.85rem; resize: vertical; min-height: 60px; box-sizing: border-box; }
|
||||
|
||||
/* 表格底部 */
|
||||
.table-footer { text-align: right; padding: 0.75rem 1rem; background: var(--bg-secondary); border-top: 1px solid var(--border-light); }
|
||||
|
||||
/* 空行提示 */
|
||||
|
||||
/* Provider 单元格 */
|
||||
.provider-name { font-weight: 600; font-size: 0.9rem; color: var(--text-primary); }
|
||||
|
|
@ -659,8 +677,6 @@ textarea { width: 100%; padding: 0.75rem; border: 1px solid var(--border-input);
|
|||
.info-item { font-size: 0.8rem; color: var(--text-primary); word-break: break-all; }
|
||||
.info-item.sub { font-size: 0.75rem; color: var(--text-secondary); margin-top: 0.2rem; }
|
||||
|
||||
/* 开关样式已移至全局 style.css */
|
||||
|
||||
/* 操作按钮 */
|
||||
.ops-buttons { display: flex; flex-wrap: nowrap; gap: 0.5rem; }
|
||||
.btn-op { padding: 0.4rem 0.75rem; background: var(--bg-secondary); border: 1px solid var(--border-light); border-radius: 6px; font-size: 0.8rem; color: var(--text-secondary); cursor: pointer; transition: all 0.2s; white-space: nowrap; }
|
||||
|
|
@ -668,12 +684,6 @@ textarea { width: 100%; padding: 0.75rem; border: 1px solid var(--border-input);
|
|||
.btn-op.btn-danger { color: var(--danger-color); border-color: var(--danger-bg); }
|
||||
.btn-op.btn-danger:hover { background: var(--danger-bg); }
|
||||
|
||||
/* 用户信息区域按钮 */
|
||||
.btn-action { padding: 0.45rem 0.9rem; background: var(--bg-secondary); border: 1px solid var(--border-light); border-radius: 6px; font-size: 0.8rem; color: var(--text-primary); cursor: pointer; transition: all 0.2s; }
|
||||
.btn-action:hover { background: var(--bg-hover); border-color: var(--accent-primary); }
|
||||
.btn-action.btn-logout { color: var(--danger-color); }
|
||||
.btn-action.btn-logout:hover { background: var(--danger-bg); border-color: var(--danger-color); }
|
||||
|
||||
/* 主要按钮 */
|
||||
.btn-primary { padding: 0.5rem 1rem; background: var(--accent-primary); color: white; border: none; border-radius: 6px; font-size: 0.85rem; cursor: pointer; transition: all 0.2s; }
|
||||
.btn-primary:hover { background: var(--accent-primary-hover); }
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ logger = logging.getLogger(__name__)
|
|||
async def lifespan(app: FastAPI):
|
||||
"""Application lifespan manager"""
|
||||
# Import all models to ensure they are registered with Base
|
||||
from luxx.models import User, Conversation, Message, Project, LLMProvider # noqa
|
||||
from luxx.models import User, Conversation, Message, Project, LLMProvider, ChatRoom, RoomAgent # noqa
|
||||
init_db()
|
||||
|
||||
# Create default test user if not exists
|
||||
|
|
|
|||
217
luxx/models.py
217
luxx/models.py
|
|
@ -18,21 +18,19 @@ class LLMProvider(Base):
|
|||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False)
|
||||
name: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
provider_type: Mapped[str] = mapped_column(String(50), nullable=False, default="openai") # openai, deepseek, glm, etc.
|
||||
provider_type: Mapped[str] = mapped_column(String(50), nullable=False, default="openai")
|
||||
base_url: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
api_key: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
default_model: Mapped[str] = mapped_column(String(100), nullable=False, default="gpt-4")
|
||||
max_tokens: Mapped[int] = mapped_column(Integer, default=8192) # 默认 8192
|
||||
max_tokens: Mapped[int] = mapped_column(Integer, default=8192)
|
||||
is_default: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=local_now, onupdate=local_now)
|
||||
|
||||
# Relationships
|
||||
user: Mapped["User"] = relationship("User", backref="llm_providers")
|
||||
|
||||
def to_dict(self, include_key: bool = False):
|
||||
"""Convert to dictionary, optionally include API key"""
|
||||
result = {
|
||||
"id": self.id,
|
||||
"user_id": self.user_id,
|
||||
|
|
@ -51,6 +49,37 @@ class LLMProvider(Base):
|
|||
return result
|
||||
|
||||
|
||||
class UserSettings(Base):
|
||||
"""Per-user settings model"""
|
||||
__tablename__ = "user_settings"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), unique=True, nullable=False)
|
||||
default_provider_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("llm_providers.id"), nullable=True)
|
||||
temperature: Mapped[float] = mapped_column(Float, default=0.7)
|
||||
max_tokens: Mapped[int] = mapped_column(Integer, default=8192)
|
||||
thinking_enabled: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
system_prompt: Mapped[str] = mapped_column(Text, default="You are a helpful assistant.")
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=local_now, onupdate=local_now)
|
||||
|
||||
user: Mapped["User"] = relationship("User", backref="settings")
|
||||
default_provider: Mapped[Optional["LLMProvider"]] = relationship("LLMProvider")
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"user_id": self.user_id,
|
||||
"default_provider_id": self.default_provider_id,
|
||||
"temperature": self.temperature,
|
||||
"max_tokens": self.max_tokens,
|
||||
"thinking_enabled": self.thinking_enabled,
|
||||
"system_prompt": self.system_prompt,
|
||||
"created_at": self.created_at.isoformat() if self.created_at else None,
|
||||
"updated_at": self.updated_at.isoformat() if self.updated_at else None
|
||||
}
|
||||
|
||||
|
||||
class Project(Base):
|
||||
"""Project model"""
|
||||
__tablename__ = "projects"
|
||||
|
|
@ -62,7 +91,6 @@ class Project(Base):
|
|||
created_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=local_now, onupdate=local_now)
|
||||
|
||||
# Relationships
|
||||
user: Mapped["User"] = relationship("User", backref="projects")
|
||||
|
||||
|
||||
|
|
@ -75,12 +103,11 @@ class User(Base):
|
|||
email: Mapped[Optional[str]] = mapped_column(String(120), unique=True, nullable=True)
|
||||
password_hash: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
||||
role: Mapped[str] = mapped_column(String(20), default="user")
|
||||
permission_level: Mapped[int] = mapped_column(Integer, default=1) # 1=READ_ONLY, 2=WRITE, 3=EXECUTE, 4=ADMIN
|
||||
workspace_path: Mapped[Optional[str]] = mapped_column(String(500), nullable=True) # 用户工作空间路径
|
||||
permission_level: Mapped[int] = mapped_column(Integer, default=1)
|
||||
workspace_path: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
||||
|
||||
# Relationships
|
||||
conversations: Mapped[List["Conversation"]] = relationship(
|
||||
"Conversation", back_populates="user", cascade="all, delete-orphan"
|
||||
)
|
||||
|
|
@ -115,11 +142,11 @@ class Conversation(Base):
|
|||
created_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=local_now, onupdate=local_now)
|
||||
|
||||
# Relationships
|
||||
user: Mapped["User"] = relationship("User", back_populates="conversations")
|
||||
provider: Mapped[Optional["LLMProvider"]] = relationship("LLMProvider")
|
||||
messages: Mapped[List["Message"]] = relationship(
|
||||
"Message", back_populates="conversation", cascade="all, delete-orphan"
|
||||
"Message", back_populates="conversation", cascade="all, delete-orphan",
|
||||
primaryjoin="Conversation.id == foreign(Message.conversation_id)"
|
||||
)
|
||||
|
||||
def to_dict(self):
|
||||
|
|
@ -142,84 +169,184 @@ class Conversation(Base):
|
|||
class Message(Base):
|
||||
"""Message model
|
||||
|
||||
content 字段统一使用 JSON 格式存储:
|
||||
|
||||
**User 消息:**
|
||||
{
|
||||
"text": "用户输入的文本内容",
|
||||
"attachments": [
|
||||
{"name": "utils.py", "extension": "py", "content": "..."}
|
||||
]
|
||||
}
|
||||
|
||||
**Assistant 消息:**
|
||||
{
|
||||
"steps": [ // 有序步骤,用于渲染(主要数据源)
|
||||
{"id": "step-0", "index": 0, "type": "thinking", "content": "..."},
|
||||
{"id": "step-1", "index": 1, "type": "text", "content": "..."},
|
||||
{"id": "step-2", "index": 2, "type": "tool_call", "id_ref": "call_xxx", "name": "...", "arguments": "..."},
|
||||
{"id": "step-3", "index": 3, "type": "tool_result", "id_ref": "call_xxx", "name": "...", "content": "..."}
|
||||
]
|
||||
}
|
||||
|
||||
注意:to_dict() 返回时会从 steps 动态计算 text 和 content 字段。
|
||||
同时服务于普通会话和聊天室:
|
||||
- 普通会话:conversation_id 非空,room_id 为空
|
||||
- 聊天室:room_id 非空,conversation_id 为空,sender_name/sender_color/round_number 有值
|
||||
"""
|
||||
__tablename__ = "messages"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
conversation_id: Mapped[str] = mapped_column(String(64), ForeignKey("conversations.id"), nullable=False)
|
||||
role: Mapped[str] = mapped_column(String(16), nullable=False) # user, assistant, system, tool
|
||||
conversation_id: Mapped[Optional[str]] = mapped_column(String(64), ForeignKey("conversations.id"), nullable=True)
|
||||
room_id: Mapped[Optional[str]] = mapped_column(String(64), ForeignKey("chat_rooms.id"), nullable=True)
|
||||
role: Mapped[str] = mapped_column(String(16), nullable=False)
|
||||
content: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
||||
token_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
usage: Mapped[Optional[str]] = mapped_column(Text, nullable=True) # JSON string for usage info
|
||||
usage: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
# 聊天室专属字段(普通会话为空)
|
||||
sender_name: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
||||
sender_color: Mapped[Optional[str]] = mapped_column(String(7), nullable=True, default="#2563eb")
|
||||
round_number: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
||||
|
||||
# Relationships
|
||||
conversation: Mapped["Conversation"] = relationship("Conversation", back_populates="messages")
|
||||
conversation: Mapped[Optional["Conversation"]] = relationship("Conversation", back_populates="messages")
|
||||
room: Mapped[Optional["ChatRoom"]] = relationship("ChatRoom", back_populates="messages")
|
||||
|
||||
def to_dict(self):
|
||||
"""Convert to dictionary, extracting process_steps for frontend"""
|
||||
import json
|
||||
|
||||
result = {
|
||||
"id": self.id,
|
||||
"conversation_id": self.conversation_id,
|
||||
"room_id": self.room_id,
|
||||
"role": self.role,
|
||||
"token_count": self.token_count,
|
||||
"created_at": self.created_at.isoformat() if self.created_at else None
|
||||
}
|
||||
|
||||
# Parse usage JSON
|
||||
if self.usage:
|
||||
try:
|
||||
result["usage"] = json.loads(self.usage)
|
||||
except json.JSONDecodeError:
|
||||
result["usage"] = None
|
||||
|
||||
# Parse content JSON
|
||||
# 聊天室专属字段
|
||||
if self.sender_name:
|
||||
result["sender_name"] = self.sender_name
|
||||
if self.sender_color:
|
||||
result["sender_color"] = self.sender_color
|
||||
if self.round_number is not None:
|
||||
result["round_number"] = self.round_number
|
||||
|
||||
try:
|
||||
content_obj = json.loads(self.content) if self.content else {}
|
||||
except json.JSONDecodeError:
|
||||
# Legacy plain text content
|
||||
result["content"] = self.content
|
||||
result["text"] = self.content
|
||||
result["attachments"] = []
|
||||
result["process_steps"] = []
|
||||
return result
|
||||
|
||||
# Extract steps as process_steps for frontend rendering
|
||||
steps = content_obj.get("steps", [])
|
||||
result["process_steps"] = steps
|
||||
|
||||
# Extract text from steps (concatenate all text type steps)
|
||||
text_content = "".join(
|
||||
s.get("content", "") for s in steps
|
||||
if s.get("type") == "text"
|
||||
)
|
||||
result["text"] = text_content
|
||||
result["content"] = text_content # Alias for convenience
|
||||
|
||||
# Extract attachments
|
||||
result["content"] = text_content
|
||||
result["attachments"] = content_obj.get("attachments", [])
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# ============ Chat Room Models ============
|
||||
|
||||
class Agent(Base):
|
||||
"""Standalone reusable Agent template"""
|
||||
__tablename__ = "agents"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False)
|
||||
name: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
role: Mapped[str] = mapped_column(String(255), nullable=False, default="")
|
||||
provider_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("llm_providers.id"), nullable=True)
|
||||
model: Mapped[str] = mapped_column(String(100), nullable=False, default="")
|
||||
system_prompt: Mapped[str] = mapped_column(Text, nullable=False, default="You are a helpful AI assistant.")
|
||||
color: Mapped[str] = mapped_column(String(7), nullable=False, default="#2563eb")
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=local_now, onupdate=local_now)
|
||||
|
||||
user: Mapped["User"] = relationship("User", backref="agents")
|
||||
provider: Mapped[Optional["LLMProvider"]] = relationship("LLMProvider")
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"user_id": self.user_id,
|
||||
"name": self.name,
|
||||
"role": self.role,
|
||||
"provider_id": self.provider_id,
|
||||
"model": self.model,
|
||||
"system_prompt": self.system_prompt,
|
||||
"color": self.color,
|
||||
"created_at": self.created_at.isoformat() if self.created_at else None,
|
||||
"updated_at": self.updated_at.isoformat() if self.updated_at else None
|
||||
}
|
||||
|
||||
|
||||
class ChatRoom(Base):
|
||||
"""Multi-agent chat room model"""
|
||||
__tablename__ = "chat_rooms"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id"), nullable=False)
|
||||
title: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
task: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
||||
status: Mapped[str] = mapped_column(String(20), nullable=False, default="idle") # idle, running, paused, completed, error
|
||||
max_rounds: Mapped[int] = mapped_column(Integer, default=5)
|
||||
current_round: Mapped[int] = mapped_column(Integer, default=0)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=local_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, default=local_now, onupdate=local_now)
|
||||
|
||||
user: Mapped["User"] = relationship("User", backref="chat_rooms")
|
||||
agents: Mapped[List["RoomAgent"]] = relationship(
|
||||
"RoomAgent", back_populates="room", cascade="all, delete-orphan", order_by="RoomAgent.turn_order"
|
||||
)
|
||||
messages: Mapped[List["Message"]] = relationship(
|
||||
"Message", back_populates="room", cascade="all, delete-orphan",
|
||||
primaryjoin="ChatRoom.id == foreign(Message.room_id)",
|
||||
order_by="Message.created_at"
|
||||
)
|
||||
|
||||
def to_dict(self, include_messages: bool = False):
|
||||
result = {
|
||||
"id": self.id,
|
||||
"user_id": self.user_id,
|
||||
"title": self.title,
|
||||
"task": self.task,
|
||||
"status": self.status,
|
||||
"max_rounds": self.max_rounds,
|
||||
"current_round": self.current_round,
|
||||
"created_at": self.created_at.isoformat() if self.created_at else None,
|
||||
"updated_at": self.updated_at.isoformat() if self.updated_at else None,
|
||||
"agents": [a.to_dict() for a in self.agents]
|
||||
}
|
||||
if include_messages:
|
||||
result["messages"] = [m.to_dict() for m in self.messages]
|
||||
return result
|
||||
|
||||
|
||||
class RoomAgent(Base):
|
||||
"""Agent assignment in a chat room (links Agent to Room with room-specific config)"""
|
||||
__tablename__ = "room_agents"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
room_id: Mapped[str] = mapped_column(String(64), ForeignKey("chat_rooms.id"), nullable=False)
|
||||
agent_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("agents.id"), nullable=True)
|
||||
name: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
role: Mapped[str] = mapped_column(String(255), nullable=False, default="")
|
||||
provider_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("llm_providers.id"), nullable=True)
|
||||
model: Mapped[str] = mapped_column(String(100), nullable=False, default="")
|
||||
system_prompt: Mapped[str] = mapped_column(Text, nullable=False, default="You are a helpful AI assistant.")
|
||||
color: Mapped[str] = mapped_column(String(7), nullable=False, default="#2563eb")
|
||||
turn_order: Mapped[int] = mapped_column(Integer, default=0)
|
||||
|
||||
room: Mapped["ChatRoom"] = relationship("ChatRoom", back_populates="agents")
|
||||
provider: Mapped[Optional["LLMProvider"]] = relationship("LLMProvider")
|
||||
agent: Mapped[Optional["Agent"]] = relationship("Agent")
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"room_id": self.room_id,
|
||||
"agent_id": self.agent_id,
|
||||
"name": self.name,
|
||||
"role": self.role,
|
||||
"provider_id": self.provider_id,
|
||||
"model": self.model,
|
||||
"system_prompt": self.system_prompt,
|
||||
"color": self.color,
|
||||
"turn_order": self.turn_order
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
"""API routes module"""
|
||||
from fastapi import APIRouter
|
||||
|
||||
from luxx.routes import auth, conversations, messages, tools, providers
|
||||
from luxx.routes import auth, conversations, messages, tools, providers, chat_rooms, agents
|
||||
|
||||
|
||||
api_router = APIRouter()
|
||||
|
|
@ -12,3 +12,5 @@ api_router.include_router(conversations.router)
|
|||
api_router.include_router(messages.router)
|
||||
api_router.include_router(tools.router)
|
||||
api_router.include_router(providers.router)
|
||||
api_router.include_router(chat_rooms.router)
|
||||
api_router.include_router(agents.router)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,143 @@
|
|||
"""Standalone Agent CRUD routes"""
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from luxx.database import get_db
|
||||
from luxx.models import Agent, LLMProvider, User
|
||||
from luxx.routes.auth import get_current_user
|
||||
from luxx.utils.helpers import success_response, error_response
|
||||
|
||||
router = APIRouter(prefix="/agents", tags=["Agents"])
|
||||
|
||||
|
||||
class AgentCreate(BaseModel):
|
||||
name: str
|
||||
role: str = ""
|
||||
provider_id: Optional[int] = None
|
||||
model: str = ""
|
||||
system_prompt: str = "You are a helpful AI assistant."
|
||||
color: str = "#2563eb"
|
||||
|
||||
|
||||
class AgentUpdate(BaseModel):
|
||||
name: Optional[str] = None
|
||||
role: Optional[str] = None
|
||||
provider_id: Optional[int] = None
|
||||
model: Optional[str] = None
|
||||
system_prompt: Optional[str] = None
|
||||
color: Optional[str] = None
|
||||
|
||||
|
||||
@router.get("/", response_model=dict)
|
||||
def list_agents(
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""List all agents for current user"""
|
||||
agents = db.query(Agent).filter(
|
||||
Agent.user_id == current_user.id
|
||||
).order_by(Agent.updated_at.desc()).all()
|
||||
return success_response(data=[a.to_dict() for a in agents])
|
||||
|
||||
|
||||
@router.post("/", response_model=dict)
|
||||
def create_agent(
|
||||
data: AgentCreate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Create a new agent"""
|
||||
model = data.model
|
||||
provider_id = data.provider_id
|
||||
if provider_id and not model:
|
||||
provider = db.query(LLMProvider).filter(
|
||||
LLMProvider.id == provider_id,
|
||||
LLMProvider.user_id == current_user.id
|
||||
).first()
|
||||
if provider:
|
||||
model = provider.default_model
|
||||
if not model:
|
||||
default_provider = db.query(LLMProvider).filter(
|
||||
LLMProvider.user_id == current_user.id,
|
||||
LLMProvider.is_default == True
|
||||
).first()
|
||||
if default_provider:
|
||||
provider_id = default_provider.id
|
||||
model = default_provider.default_model
|
||||
if not model:
|
||||
model = "gpt-4"
|
||||
|
||||
agent = Agent(
|
||||
user_id=current_user.id,
|
||||
name=data.name,
|
||||
role=data.role,
|
||||
provider_id=provider_id,
|
||||
model=model,
|
||||
system_prompt=data.system_prompt,
|
||||
color=data.color
|
||||
)
|
||||
db.add(agent)
|
||||
db.commit()
|
||||
db.refresh(agent)
|
||||
return success_response(data=agent.to_dict(), message="Agent created")
|
||||
|
||||
|
||||
@router.get("/{agent_id}", response_model=dict)
|
||||
def get_agent(
|
||||
agent_id: int,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Get agent details"""
|
||||
agent = db.query(Agent).filter(
|
||||
Agent.id == agent_id,
|
||||
Agent.user_id == current_user.id
|
||||
).first()
|
||||
if not agent:
|
||||
return error_response("Agent not found", 404)
|
||||
return success_response(data=agent.to_dict())
|
||||
|
||||
|
||||
@router.put("/{agent_id}", response_model=dict)
|
||||
def update_agent(
|
||||
agent_id: int,
|
||||
data: AgentUpdate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Update an agent"""
|
||||
agent = db.query(Agent).filter(
|
||||
Agent.id == agent_id,
|
||||
Agent.user_id == current_user.id
|
||||
).first()
|
||||
if not agent:
|
||||
return error_response("Agent not found", 404)
|
||||
|
||||
update_data = data.dict(exclude_unset=True)
|
||||
for key, value in update_data.items():
|
||||
setattr(agent, key, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(agent)
|
||||
return success_response(data=agent.to_dict(), message="Agent updated")
|
||||
|
||||
|
||||
@router.delete("/{agent_id}", response_model=dict)
|
||||
def delete_agent(
|
||||
agent_id: int,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Delete an agent"""
|
||||
agent = db.query(Agent).filter(
|
||||
Agent.id == agent_id,
|
||||
Agent.user_id == current_user.id
|
||||
).first()
|
||||
if not agent:
|
||||
return error_response("Agent not found", 404)
|
||||
|
||||
db.delete(agent)
|
||||
db.commit()
|
||||
return success_response(message="Agent deleted")
|
||||
|
|
@ -6,7 +6,7 @@ from sqlalchemy.orm import Session
|
|||
from pydantic import BaseModel
|
||||
|
||||
from luxx.database import get_db
|
||||
from luxx.models import User
|
||||
from luxx.models import User, UserSettings
|
||||
from luxx.utils.helpers import (
|
||||
hash_password,
|
||||
verify_password,
|
||||
|
|
@ -49,6 +49,15 @@ class UserPermissionUpdate(BaseModel):
|
|||
permission_level: int
|
||||
|
||||
|
||||
class SettingsUpdate(BaseModel):
|
||||
"""User settings update model"""
|
||||
default_provider_id: int | None = None
|
||||
temperature: float | None = None
|
||||
max_tokens: int | None = None
|
||||
thinking_enabled: bool | None = None
|
||||
system_prompt: str | None = None
|
||||
|
||||
|
||||
class TokenResponse(BaseModel):
|
||||
"""Token response model"""
|
||||
access_token: str
|
||||
|
|
@ -167,3 +176,40 @@ def update_user(user_id: int, data: UserPermissionUpdate, admin_user: User = Dep
|
|||
db.commit()
|
||||
|
||||
return success_response(data=user.to_dict(), message="User permission updated")
|
||||
|
||||
|
||||
def _get_or_create_settings(db: Session, user_id: int) -> UserSettings:
|
||||
"""Get or create user settings"""
|
||||
settings = db.query(UserSettings).filter(UserSettings.user_id == user_id).first()
|
||||
if not settings:
|
||||
settings = UserSettings(user_id=user_id)
|
||||
db.add(settings)
|
||||
db.commit()
|
||||
db.refresh(settings)
|
||||
return settings
|
||||
|
||||
|
||||
@router.get("/settings", response_model=dict)
|
||||
def get_settings(current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
|
||||
"""Get current user settings"""
|
||||
settings = _get_or_create_settings(db, current_user.id)
|
||||
return success_response(data=settings.to_dict())
|
||||
|
||||
|
||||
@router.put("/settings", response_model=dict)
|
||||
def update_settings(
|
||||
data: SettingsUpdate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Update current user settings"""
|
||||
settings = _get_or_create_settings(db, current_user.id)
|
||||
|
||||
update_data = data.dict(exclude_unset=True)
|
||||
for key, value in update_data.items():
|
||||
setattr(settings, key, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(settings)
|
||||
|
||||
return success_response(data=settings.to_dict(), message="Settings updated")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,462 @@
|
|||
"""Chat room routes for multi-agent conversations"""
|
||||
from typing import Optional, List
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi.responses import StreamingResponse
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime
|
||||
|
||||
from luxx.database import get_db, SessionLocal
|
||||
from luxx.models import ChatRoom, RoomAgent, Agent, Message, LLMProvider, User
|
||||
from luxx.routes.auth import get_current_user
|
||||
from luxx.services.chat_room import orchestrator
|
||||
from luxx.utils.helpers import generate_id, success_response, error_response, paginate
|
||||
|
||||
router = APIRouter(prefix="/chat-rooms", tags=["Chat Rooms"])
|
||||
|
||||
|
||||
# ============ Request Models ============
|
||||
|
||||
class AgentConfig(BaseModel):
|
||||
agent_id: Optional[int] = None # Link to existing Agent
|
||||
name: str = ""
|
||||
role: str = ""
|
||||
provider_id: Optional[int] = None
|
||||
model: str = ""
|
||||
system_prompt: str = "You are a helpful AI assistant."
|
||||
color: str = "#2563eb"
|
||||
|
||||
|
||||
class ChatRoomCreate(BaseModel):
|
||||
title: str
|
||||
task: str
|
||||
max_rounds: int = 5
|
||||
agents: List[AgentConfig] = []
|
||||
|
||||
|
||||
class ChatRoomUpdate(BaseModel):
|
||||
title: Optional[str] = None
|
||||
task: Optional[str] = None
|
||||
max_rounds: Optional[int] = None
|
||||
status: Optional[str] = None
|
||||
|
||||
|
||||
class AgentCreate(BaseModel):
|
||||
agent_id: Optional[int] = None # Link to existing Agent
|
||||
name: str = ""
|
||||
role: str = ""
|
||||
provider_id: Optional[int] = None
|
||||
model: str = ""
|
||||
system_prompt: str = "You are a helpful AI assistant."
|
||||
color: str = "#2563eb"
|
||||
|
||||
|
||||
class AgentUpdate(BaseModel):
|
||||
name: Optional[str] = None
|
||||
role: Optional[str] = None
|
||||
provider_id: Optional[int] = None
|
||||
model: Optional[str] = None
|
||||
system_prompt: Optional[str] = None
|
||||
color: Optional[str] = None
|
||||
turn_order: Optional[int] = None
|
||||
|
||||
|
||||
# ============ Room CRUD ============
|
||||
|
||||
@router.get("/", response_model=dict)
|
||||
def list_rooms(
|
||||
page: int = 1,
|
||||
page_size: int = 20,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""List chat rooms"""
|
||||
query = db.query(ChatRoom).filter(ChatRoom.user_id == current_user.id)
|
||||
result = paginate(query.order_by(ChatRoom.updated_at.desc()), page, page_size)
|
||||
return success_response(data={
|
||||
"items": [r.to_dict() for r in result["items"]],
|
||||
"total": result["total"],
|
||||
"page": result["page"],
|
||||
"page_size": result["page_size"]
|
||||
})
|
||||
|
||||
|
||||
@router.post("/", response_model=dict)
|
||||
def create_room(
|
||||
data: ChatRoomCreate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Create a chat room with agents"""
|
||||
room = ChatRoom(
|
||||
id=generate_id("room"),
|
||||
user_id=current_user.id,
|
||||
title=data.title,
|
||||
task=data.task,
|
||||
max_rounds=data.max_rounds
|
||||
)
|
||||
db.add(room)
|
||||
db.flush()
|
||||
|
||||
for i, agent_cfg in enumerate(data.agents):
|
||||
# If agent_id provided, copy config from existing Agent
|
||||
if agent_cfg.agent_id:
|
||||
existing = db.query(Agent).filter(
|
||||
Agent.id == agent_cfg.agent_id,
|
||||
Agent.user_id == current_user.id
|
||||
).first()
|
||||
if existing:
|
||||
name = agent_cfg.name or existing.name
|
||||
role = agent_cfg.role or existing.role
|
||||
provider_id = agent_cfg.provider_id or existing.provider_id
|
||||
model = agent_cfg.model or existing.model
|
||||
system_prompt = agent_cfg.system_prompt if agent_cfg.system_prompt != "You are a helpful AI assistant." else existing.system_prompt
|
||||
color = agent_cfg.color if agent_cfg.color != "#2563eb" else existing.color
|
||||
agent_id = existing.id
|
||||
else:
|
||||
return error_response(f"Agent {agent_cfg.agent_id} not found", 404)
|
||||
else:
|
||||
name = agent_cfg.name or f"Agent {i+1}"
|
||||
role = agent_cfg.role
|
||||
provider_id = agent_cfg.provider_id
|
||||
model = agent_cfg.model
|
||||
system_prompt = agent_cfg.system_prompt
|
||||
color = agent_cfg.color
|
||||
agent_id = None
|
||||
|
||||
# Resolve model from provider if not specified
|
||||
if provider_id and not model:
|
||||
provider = db.query(LLMProvider).filter(
|
||||
LLMProvider.id == provider_id,
|
||||
LLMProvider.user_id == current_user.id
|
||||
).first()
|
||||
if provider:
|
||||
model = provider.default_model
|
||||
if not model:
|
||||
default_provider = db.query(LLMProvider).filter(
|
||||
LLMProvider.user_id == current_user.id,
|
||||
LLMProvider.is_default == True
|
||||
).first()
|
||||
if default_provider:
|
||||
provider_id = default_provider.id
|
||||
model = default_provider.default_model
|
||||
if not model:
|
||||
model = "gpt-4"
|
||||
|
||||
agent = RoomAgent(
|
||||
room_id=room.id,
|
||||
agent_id=agent_id,
|
||||
name=name,
|
||||
role=role,
|
||||
provider_id=provider_id,
|
||||
model=model,
|
||||
system_prompt=system_prompt,
|
||||
color=color,
|
||||
turn_order=i
|
||||
)
|
||||
db.add(agent)
|
||||
|
||||
db.commit()
|
||||
db.refresh(room)
|
||||
return success_response(data=room.to_dict(include_messages=False), message="Room created")
|
||||
|
||||
|
||||
@router.get("/{room_id}", response_model=dict)
|
||||
def get_room(
|
||||
room_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Get room details with agents"""
|
||||
room = db.query(ChatRoom).filter(
|
||||
ChatRoom.id == room_id,
|
||||
ChatRoom.user_id == current_user.id
|
||||
).first()
|
||||
if not room:
|
||||
return error_response("Room not found", 404)
|
||||
|
||||
result = room.to_dict(include_messages=False)
|
||||
# Also get message count
|
||||
msg_count = db.query(Message).filter(Message.room_id == room_id).count()
|
||||
result["message_count"] = msg_count
|
||||
return success_response(data=result)
|
||||
|
||||
|
||||
@router.put("/{room_id}", response_model=dict)
|
||||
def update_room(
|
||||
room_id: str,
|
||||
data: ChatRoomUpdate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Update room"""
|
||||
room = db.query(ChatRoom).filter(
|
||||
ChatRoom.id == room_id,
|
||||
ChatRoom.user_id == current_user.id
|
||||
).first()
|
||||
if not room:
|
||||
return error_response("Room not found", 404)
|
||||
|
||||
if room.status == "running":
|
||||
return error_response("Cannot update a running room", 400)
|
||||
|
||||
update_data = data.dict(exclude_unset=True)
|
||||
for key, value in update_data.items():
|
||||
setattr(room, key, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(room)
|
||||
return success_response(data=room.to_dict(), message="Room updated")
|
||||
|
||||
|
||||
@router.delete("/{room_id}", response_model=dict)
|
||||
def delete_room(
|
||||
room_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Delete room"""
|
||||
room = db.query(ChatRoom).filter(
|
||||
ChatRoom.id == room_id,
|
||||
ChatRoom.user_id == current_user.id
|
||||
).first()
|
||||
if not room:
|
||||
return error_response("Room not found", 404)
|
||||
|
||||
if room.status == "running":
|
||||
return error_response("Cannot delete a running room. Stop it first.", 400)
|
||||
|
||||
db.delete(room)
|
||||
db.commit()
|
||||
return success_response(message="Room deleted")
|
||||
|
||||
|
||||
# ============ Room Actions ============
|
||||
|
||||
@router.post("/{room_id}/start")
|
||||
async def start_room(
|
||||
room_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Start the multi-agent conversation as SSE stream"""
|
||||
room = db.query(ChatRoom).filter(
|
||||
ChatRoom.id == room_id,
|
||||
ChatRoom.user_id == current_user.id
|
||||
).first()
|
||||
if not room:
|
||||
return error_response("Room not found", 404)
|
||||
|
||||
if room.status == "running":
|
||||
return error_response("Room is already running", 400)
|
||||
|
||||
async def event_generator():
|
||||
async for sse_str in orchestrator.run_room(room_id):
|
||||
yield sse_str
|
||||
|
||||
return StreamingResponse(
|
||||
event_generator(),
|
||||
media_type="text/event-stream",
|
||||
headers={
|
||||
"Cache-Control": "no-cache",
|
||||
"Connection": "keep-alive",
|
||||
"X-Accel-Buffering": "no"
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/{room_id}/stop", response_model=dict)
|
||||
def stop_room(
|
||||
room_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Stop a running room"""
|
||||
room = db.query(ChatRoom).filter(
|
||||
ChatRoom.id == room_id,
|
||||
ChatRoom.user_id == current_user.id
|
||||
).first()
|
||||
if not room:
|
||||
return error_response("Room not found", 404)
|
||||
|
||||
orchestrator.cancel(room_id)
|
||||
room.status = "paused"
|
||||
db.commit()
|
||||
return success_response(message="Room stopped")
|
||||
|
||||
|
||||
@router.post("/{room_id}/reset", response_model=dict)
|
||||
def reset_room(
|
||||
room_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Reset room to initial state, clearing all messages"""
|
||||
room = db.query(ChatRoom).filter(
|
||||
ChatRoom.id == room_id,
|
||||
ChatRoom.user_id == current_user.id
|
||||
).first()
|
||||
if not room:
|
||||
return error_response("Room not found", 404)
|
||||
|
||||
if room.status == "running":
|
||||
return error_response("Cannot reset a running room", 400)
|
||||
|
||||
# Delete all messages in this room
|
||||
db.query(Message).filter(Message.room_id == room_id).delete()
|
||||
room.status = "idle"
|
||||
room.current_round = 0
|
||||
db.commit()
|
||||
return success_response(message="Room reset")
|
||||
|
||||
|
||||
# ============ Messages ============
|
||||
|
||||
@router.get("/{room_id}/messages", response_model=dict)
|
||||
def get_room_messages(
|
||||
room_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Get all messages in a room"""
|
||||
room = db.query(ChatRoom).filter(
|
||||
ChatRoom.id == room_id,
|
||||
ChatRoom.user_id == current_user.id
|
||||
).first()
|
||||
if not room:
|
||||
return error_response("Room not found", 404)
|
||||
|
||||
messages = db.query(Message).filter(
|
||||
Message.room_id == room_id
|
||||
).order_by(Message.created_at).all()
|
||||
|
||||
return success_response(data={
|
||||
"messages": [m.to_dict() for m in messages],
|
||||
"room": room.to_dict()
|
||||
})
|
||||
|
||||
|
||||
# ============ Agent CRUD ============
|
||||
|
||||
@router.post("/{room_id}/agents", response_model=dict)
|
||||
def add_agent(
|
||||
room_id: str,
|
||||
data: AgentCreate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Add an agent to a room"""
|
||||
room = db.query(ChatRoom).filter(
|
||||
ChatRoom.id == room_id,
|
||||
ChatRoom.user_id == current_user.id
|
||||
).first()
|
||||
if not room:
|
||||
return error_response("Room not found", 404)
|
||||
|
||||
if room.status == "running":
|
||||
return error_response("Cannot modify agents while room is running", 400)
|
||||
|
||||
# Get max turn_order
|
||||
max_order = db.query(RoomAgent).filter(
|
||||
RoomAgent.room_id == room_id
|
||||
).count()
|
||||
|
||||
# If agent_id provided, copy from existing Agent
|
||||
if data.agent_id:
|
||||
existing = db.query(Agent).filter(
|
||||
Agent.id == data.agent_id,
|
||||
Agent.user_id == current_user.id
|
||||
).first()
|
||||
if not existing:
|
||||
return error_response(f"Agent {data.agent_id} not found", 404)
|
||||
name = data.name or existing.name
|
||||
role = data.role or existing.role
|
||||
provider_id = data.provider_id or existing.provider_id
|
||||
model = data.model or existing.model
|
||||
system_prompt = data.system_prompt if data.system_prompt != "You are a helpful AI assistant." else existing.system_prompt
|
||||
color = data.color if data.color != "#2563eb" else existing.color
|
||||
agent_id = existing.id
|
||||
else:
|
||||
name = data.name or f"Agent {max_order + 1}"
|
||||
role = data.role
|
||||
provider_id = data.provider_id
|
||||
model = data.model
|
||||
system_prompt = data.system_prompt
|
||||
color = data.color
|
||||
agent_id = None
|
||||
|
||||
model = model
|
||||
if provider_id and not model:
|
||||
provider = db.query(LLMProvider).filter(LLMProvider.id == provider_id).first()
|
||||
if provider:
|
||||
model = provider.default_model
|
||||
if not model:
|
||||
model = "gpt-4"
|
||||
|
||||
agent = RoomAgent(
|
||||
room_id=room_id,
|
||||
agent_id=agent_id,
|
||||
name=name,
|
||||
role=role,
|
||||
provider_id=provider_id,
|
||||
model=model,
|
||||
system_prompt=system_prompt,
|
||||
color=color,
|
||||
turn_order=max_order
|
||||
)
|
||||
db.add(agent)
|
||||
db.commit()
|
||||
db.refresh(agent)
|
||||
return success_response(data=agent.to_dict(), message="Agent added")
|
||||
|
||||
|
||||
@router.put("/{room_id}/agents/{agent_id}", response_model=dict)
|
||||
def update_agent(
|
||||
room_id: str,
|
||||
agent_id: int,
|
||||
data: AgentUpdate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Update an agent"""
|
||||
agent = db.query(RoomAgent).filter(
|
||||
RoomAgent.id == agent_id,
|
||||
RoomAgent.room_id == room_id
|
||||
).first()
|
||||
if not agent:
|
||||
return error_response("Agent not found", 404)
|
||||
|
||||
room = db.query(ChatRoom).filter(ChatRoom.id == room_id).first()
|
||||
if room and room.status == "running":
|
||||
return error_response("Cannot modify agents while room is running", 400)
|
||||
|
||||
update_data = data.dict(exclude_unset=True)
|
||||
for key, value in update_data.items():
|
||||
setattr(agent, key, value)
|
||||
|
||||
db.commit()
|
||||
return success_response(data=agent.to_dict(), message="Agent updated")
|
||||
|
||||
|
||||
@router.delete("/{room_id}/agents/{agent_id}", response_model=dict)
|
||||
def delete_agent(
|
||||
room_id: str,
|
||||
agent_id: int,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Remove an agent from a room"""
|
||||
agent = db.query(RoomAgent).filter(
|
||||
RoomAgent.id == agent_id,
|
||||
RoomAgent.room_id == room_id
|
||||
).first()
|
||||
if not agent:
|
||||
return error_response("Agent not found", 404)
|
||||
|
||||
room = db.query(ChatRoom).filter(ChatRoom.id == room_id).first()
|
||||
if room and room.status == "running":
|
||||
return error_response("Cannot remove agents while room is running", 400)
|
||||
|
||||
db.delete(agent)
|
||||
db.commit()
|
||||
return success_response(message="Agent removed")
|
||||
|
|
@ -0,0 +1,288 @@
|
|||
"""Multi-agent chat room service.
|
||||
|
||||
Orchestrates multiple agents taking turns to discuss and solve a task.
|
||||
Each agent uses its own LLM provider/model and system prompt.
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
import asyncio
|
||||
import traceback
|
||||
from typing import List, Dict, Any, AsyncGenerator, Optional
|
||||
|
||||
from luxx.database import SessionLocal
|
||||
from luxx.models import ChatRoom, RoomAgent, Message, LLMProvider
|
||||
from luxx.services.llm_client import LLMClient
|
||||
from luxx.services.stream_context import StreamState, StepType
|
||||
from luxx.services.events import sse_event
|
||||
from luxx.utils.helpers import generate_id
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ChatRoomOrchestrator:
|
||||
"""Orchestrates multi-agent conversations in a chat room."""
|
||||
|
||||
def __init__(self):
|
||||
self._running_rooms: Dict[str, asyncio.Task] = {}
|
||||
|
||||
def is_running(self, room_id: str) -> bool:
|
||||
return room_id in self._running_rooms and not self._running_rooms[room_id].done()
|
||||
|
||||
def cancel(self, room_id: str):
|
||||
task = self._running_rooms.get(room_id)
|
||||
if task and not task.done():
|
||||
task.cancel()
|
||||
|
||||
async def run_room(
|
||||
self,
|
||||
room_id: str,
|
||||
db_session=None
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""Run a chat room: agents take turns discussing the task."""
|
||||
db = db_session or SessionLocal()
|
||||
own_session = db_session is None
|
||||
|
||||
try:
|
||||
room = db.query(ChatRoom).filter(ChatRoom.id == room_id).first()
|
||||
if not room:
|
||||
yield sse_event("error", {"content": "Room not found"})
|
||||
return
|
||||
|
||||
agents = db.query(RoomAgent).filter(
|
||||
RoomAgent.room_id == room_id
|
||||
).order_by(RoomAgent.turn_order).all()
|
||||
|
||||
if not agents:
|
||||
yield sse_event("error", {"content": "No agents in room"})
|
||||
return
|
||||
|
||||
room.status = "running"
|
||||
db.commit()
|
||||
|
||||
# Yield room started event
|
||||
yield sse_event("room_started", {"room_id": room_id, "task": room.task})
|
||||
|
||||
# Build conversation history from existing messages
|
||||
history = self._load_history(room_id, db)
|
||||
|
||||
# If no messages yet, add the task as the initial user message
|
||||
if not history:
|
||||
task_msg = Message(
|
||||
id=generate_id("msg"),
|
||||
room_id=room_id,
|
||||
role="user",
|
||||
content=json.dumps({"text": room.task}, ensure_ascii=False),
|
||||
sender_name="用户",
|
||||
sender_color="#10b981",
|
||||
round_number=0
|
||||
)
|
||||
db.add(task_msg)
|
||||
db.commit()
|
||||
history.append({"role": "user", "content": room.task})
|
||||
yield sse_event("message", task_msg.to_dict())
|
||||
|
||||
# Run rounds
|
||||
for round_num in range(room.current_round + 1, room.max_rounds + 1):
|
||||
room.current_round = round_num
|
||||
db.commit()
|
||||
|
||||
yield sse_event("round_start", {
|
||||
"round": round_num,
|
||||
"max_rounds": room.max_rounds
|
||||
})
|
||||
|
||||
for agent in agents:
|
||||
try:
|
||||
async for event in self._agent_turn(
|
||||
room_id, agent, history, round_num, db
|
||||
):
|
||||
yield event
|
||||
except asyncio.CancelledError:
|
||||
room.status = "paused"
|
||||
db.commit()
|
||||
yield sse_event("room_paused", {"room_id": room_id, "round": round_num})
|
||||
return
|
||||
except Exception as e:
|
||||
logger.error(f"Agent {agent.name} error: {e}\n{traceback.format_exc()}")
|
||||
yield sse_event("agent_error", {
|
||||
"agent": agent.name,
|
||||
"error": str(e)
|
||||
})
|
||||
|
||||
yield sse_event("round_end", {"round": round_num})
|
||||
|
||||
# Completed
|
||||
room.status = "completed"
|
||||
db.commit()
|
||||
yield sse_event("room_completed", {
|
||||
"room_id": room_id,
|
||||
"total_rounds": room.max_rounds
|
||||
})
|
||||
|
||||
except asyncio.CancelledError:
|
||||
room = db.query(ChatRoom).filter(ChatRoom.id == room_id).first()
|
||||
if room:
|
||||
room.status = "paused"
|
||||
db.commit()
|
||||
yield sse_event("room_paused", {"room_id": room_id})
|
||||
except Exception as e:
|
||||
logger.error(f"Room error: {e}\n{traceback.format_exc()}")
|
||||
room = db.query(ChatRoom).filter(ChatRoom.id == room_id).first()
|
||||
if room:
|
||||
room.status = "error"
|
||||
db.commit()
|
||||
yield sse_event("error", {"content": str(e)})
|
||||
finally:
|
||||
if own_session:
|
||||
db.close()
|
||||
self._running_rooms.pop(room_id, None)
|
||||
|
||||
async def _agent_turn(
|
||||
self,
|
||||
room_id: str,
|
||||
agent: RoomAgent,
|
||||
history: List[Dict],
|
||||
round_num: int,
|
||||
db
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""Execute one agent's turn in the conversation."""
|
||||
# Get LLM client for this agent
|
||||
llm, max_tokens = self._create_llm_client(agent, db)
|
||||
if not llm:
|
||||
yield sse_event("agent_error", {
|
||||
"agent": agent.name,
|
||||
"error": "No LLM provider configured"
|
||||
})
|
||||
return
|
||||
|
||||
model = agent.model or llm.default_model or "gpt-4"
|
||||
|
||||
# Build messages for this agent
|
||||
messages = self._build_agent_messages(agent, history)
|
||||
|
||||
# Call LLM (non-streaming for simplicity in multi-agent context)
|
||||
try:
|
||||
response = await llm.async_sync_call(
|
||||
model=model,
|
||||
messages=messages,
|
||||
temperature=0.7,
|
||||
max_tokens=max_tokens or 2000
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"LLM call failed for {agent.name}: {e}")
|
||||
yield sse_event("agent_error", {
|
||||
"agent": agent.name,
|
||||
"error": f"LLM call failed: {str(e)}"
|
||||
})
|
||||
return
|
||||
|
||||
content = response.get("content", "")
|
||||
usage = response.get("usage", {})
|
||||
token_count = usage.get("total_tokens", len(content) // 4)
|
||||
|
||||
# Build steps for storage (compatible with Message content format)
|
||||
steps = [{"id": "step-0", "index": 0, "type": "text", "content": content}]
|
||||
content_json = {"steps": steps}
|
||||
|
||||
# Save message
|
||||
msg = Message(
|
||||
id=generate_id("msg"),
|
||||
room_id=room_id,
|
||||
role="assistant",
|
||||
content=json.dumps(content_json, ensure_ascii=False),
|
||||
token_count=token_count,
|
||||
usage=json.dumps(usage) if usage else None,
|
||||
sender_name=agent.name,
|
||||
sender_color=agent.color,
|
||||
round_number=round_num
|
||||
)
|
||||
db.add(msg)
|
||||
db.commit()
|
||||
|
||||
# Update history
|
||||
history.append({"role": "assistant", "content": content, "sender": agent.name})
|
||||
|
||||
# Yield message event
|
||||
msg_dict = msg.to_dict()
|
||||
yield sse_event("message", msg_dict)
|
||||
|
||||
# Close client
|
||||
await llm.close()
|
||||
|
||||
def _create_llm_client(self, agent: RoomAgent, db) -> tuple:
|
||||
"""Create LLM client for an agent."""
|
||||
if agent.provider_id:
|
||||
provider = db.query(LLMProvider).filter(
|
||||
LLMProvider.id == agent.provider_id
|
||||
).first()
|
||||
if provider:
|
||||
client = LLMClient(
|
||||
api_key=provider.api_key,
|
||||
api_url=provider.base_url,
|
||||
model=agent.model or provider.default_model,
|
||||
provider_type=provider.provider_type
|
||||
)
|
||||
return client, provider.max_tokens
|
||||
|
||||
return None, None
|
||||
|
||||
def _build_agent_messages(self, agent: RoomAgent, history: List[Dict]) -> List[Dict]:
|
||||
"""Build the message list for an agent's LLM call."""
|
||||
messages = [{"role": "system", "content": agent.system_prompt}]
|
||||
|
||||
for h in history:
|
||||
role = h.get("role", "user")
|
||||
content = h.get("content", "")
|
||||
sender = h.get("sender", "")
|
||||
|
||||
if role == "user":
|
||||
messages.append({"role": "user", "content": content})
|
||||
elif role == "assistant":
|
||||
# Prefix with sender name so the agent knows who said what
|
||||
prefix = f"[{sender}]: " if sender else ""
|
||||
messages.append({"role": "assistant", "content": prefix + content})
|
||||
|
||||
return messages
|
||||
|
||||
def _load_history(self, room_id: str, db) -> List[Dict]:
|
||||
"""Load conversation history from existing room messages."""
|
||||
messages = db.query(Message).filter(
|
||||
Message.room_id == room_id
|
||||
).order_by(Message.created_at).all()
|
||||
|
||||
history = []
|
||||
for msg in messages:
|
||||
# Extract text from message content
|
||||
text = self._extract_text(msg.content)
|
||||
entry = {"role": msg.role, "content": text}
|
||||
if msg.sender_name and msg.role == "assistant":
|
||||
entry["sender"] = msg.sender_name
|
||||
history.append(entry)
|
||||
|
||||
return history
|
||||
|
||||
@staticmethod
|
||||
def _extract_text(content: str) -> str:
|
||||
"""Extract text from message content JSON."""
|
||||
if not content:
|
||||
return ""
|
||||
try:
|
||||
parsed = json.loads(content)
|
||||
if isinstance(parsed, dict):
|
||||
# Try steps-based format
|
||||
steps = parsed.get("steps", [])
|
||||
if steps:
|
||||
return "".join(
|
||||
s.get("content", "") for s in steps
|
||||
if s.get("type") == "text"
|
||||
)
|
||||
# Try simple text format
|
||||
if "text" in parsed:
|
||||
return parsed["text"]
|
||||
return content
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
return content
|
||||
|
||||
|
||||
# Singleton orchestrator
|
||||
orchestrator = ChatRoomOrchestrator()
|
||||
Loading…
Reference in New Issue