Spaces:
Sleeping
Sleeping
File size: 20,894 Bytes
4343907 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 |
<template>
<div class="modal-overlay" @click.self="$emit('close')">
<div class="chat-modal-content">
<div class="chat-header">
<div class="agent-info">
<div class="agent-avatar" :style="{ backgroundColor: agent.color || '#6B7280' }">
{{ agent.name?.charAt(0) || 'A' }}
</div>
<div>
<h2 class="agent-name">{{ agent.name }}</h2>
<p class="agent-status" :class="statusClass">
{{ getStatusText() }}
<span v-if="connectionError" class="error-indicator">β οΈ</span>
</p>
</div>
</div>
<!-- π Provider Switcher -->
<div class="provider-controls">
<div class="provider-switcher">
<button
class="provider-btn"
:class="{ active: currentProvider === 'colossus' }"
@click="currentProvider = 'colossus'"
title="Free but slower (15-60s)"
>
π€ colossus
<span class="provider-label">Free</span>
</button>
<button
class="provider-btn"
:class="{ active: currentProvider === 'openrouter' }"
@click="currentProvider = 'openrouter'"
title="Fast but costs money (1-3s)"
>
β‘ OpenRouter
<span class="provider-label">Fast</span>
</button>
</div>
</div>
<button class="close-button" @click="$emit('close')">
β
</button>
</div>
<!-- π NEW: Loading Progress Bar -->
<div v-if="sending && currentProvider === 'colossus'" class="loading-progress">
<div class="progress-bar">
<div class="progress-fill" :style="{ width: `${loadingProgress}%` }"></div>
</div>
<div class="progress-text">
{{ loadingMessage }} ({{ Math.ceil(loadingProgress/100 * 60) }}s / 60s)
</div>
<button v-if="loadingProgress > 50" @click="switchToOpenRouter" class="quick-switch-btn">
β‘ Switch to OpenRouter (Fast)
</button>
</div>
<div class="chat-body" ref="chatContainer">
<div v-if="messages.length === 0" class="empty-chat">
<div class="empty-icon">π¬</div>
<p>Start a conversation with {{ agent.name }}</p>
<div class="provider-info">
<p><strong>Current Provider:</strong>
<span :class="getProviderClass()">
{{ currentProvider === 'colossus' ? 'π€ colossus (Free, ~30-60s)' : 'β‘ OpenRouter (Fast, ~2s)' }}
</span>
</p>
<p v-if="currentProvider === 'colossus'" class="provider-warning">
β±οΈ <strong>Patience Required:</strong> colossus responses can take 30-60 seconds.
Use β‘ OpenRouter for faster responses.
</p>
</div>
<p v-if="connectionError" class="connection-warning">
β οΈ Connection issue detected. Messages may fail.
</p>
</div>
<div v-for="(message, index) in messages" :key="index" class="message" :class="message.type">
<div class="message-content" :class="{ 'error-message': message.error, 'timeout-message': message.timeout }">
<div class="message-header" v-if="message.provider">
<span class="provider-badge" :class="`provider-${message.provider}`">
{{ message.provider === 'colossus' ? 'π€' : 'β‘' }} {{ message.provider }}
</span>
<span v-if="message.performance" class="performance-info">
{{ message.performance.response_time }}s
<span v-if="message.performance.cost_usd" class="cost-info">
${{ message.performance.cost_usd.toFixed(4) }}
</span>
</span>
</div>
<div class="message-text">{{ message.text }}</div>
<div class="message-time">{{ formatTime(message.timestamp) }}</div>
<div v-if="message.error" class="error-details">
{{ message.errorDetails }}
<button @click="retryMessage(message)" class="retry-button">π Retry</button>
<button v-if="message.provider === 'colossus'" @click="retryWithOpenRouter(message)" class="switch-retry-button">
β‘ Try OpenRouter
</button>
</div>
</div>
</div>
</div>
<div class="chat-footer">
<div v-if="connectionError" class="connection-error">
<div class="error-message">
β οΈ Connection Error: {{ connectionError }}
<button @click="testConnection" class="test-connection-button">π§ Test Connection</button>
</div>
</div>
<form @submit.prevent="sendMessage">
<div class="message-input-container">
<input
v-model="newMessage"
type="text"
class="message-input"
:placeholder="`Send to ${currentProvider} (${currentProvider === 'colossus' ? 'Free, ~60s' : 'Fast, ~2s'})...`"
:disabled="sending || agent.status !== 'active'"
/>
<button
type="submit"
class="send-button"
:class="`provider-${currentProvider}`"
:disabled="!newMessage.trim() || sending || agent.status !== 'active'"
>
{{ sending ? 'β³' : (currentProvider === 'colossus' ? 'π€' : 'β‘') }}
</button>
</div>
</form>
</div>
</div>
</div>
</template>
<script>
import { ref, reactive, nextTick, computed, onUnmounted } from 'vue'
export default {
name: 'ChatModal',
props: {
agent: {
type: Object,
required: true
}
},
emits: ['close'],
setup(props) {
const newMessage = ref('')
const sending = ref(false)
const chatContainer = ref(null)
const connectionError = ref('')
const currentProvider = ref('colossus')
// π NEW: Loading progress tracking
const loadingProgress = ref(0)
const loadingMessage = ref('')
let progressInterval = null
let requestTimeout = null
const messages = reactive([
{
type: 'agent',
text: `Hello! I'm ${props.agent.name}. How can I assist you today?`,
timestamp: Date.now(),
provider: 'system'
}
])
const statusClass = computed(() => ({
'text-green-600': props.agent.status === 'active' && !connectionError.value,
'text-red-600': connectionError.value,
'text-gray-600': props.agent.status !== 'active'
}))
const getProviderClass = () => ({
'text-blue-600': currentProvider.value === 'colossus',
'text-green-600': currentProvider.value === 'openrouter'
})
const getStatusText = () => {
if (connectionError.value) return 'Connection Error'
return props.agent.status === 'active' ? 'Online' : 'Offline'
}
// π§ ENHANCED: Response parsing function
const parseResponse = (data) => {
console.log('π Parsing response:', data)
// Priority order for response text extraction
let responseText = null
let performance = null
if (data.error) {
return {
text: `β Error: ${data.error}`,
error: true,
errorDetails: data.error
}
}
// Try different response formats
if (data.response) {
if (typeof data.response === 'string') {
responseText = data.response
} else if (typeof data.response === 'object' && data.response.content) {
responseText = data.response.content
} else if (typeof data.response === 'object') {
responseText = JSON.stringify(data.response)
}
} else if (data.content) {
responseText = data.content
} else if (data.text) {
responseText = data.text
} else if (data.message) {
responseText = data.message
}
// Extract performance data
if (data.response_time || data.cost || data.model) {
performance = {
response_time: data.response_time || 0,
cost_usd: data.cost || 0,
model: data.model || 'unknown'
}
} else if (data.performance) {
performance = data.performance
}
return {
text: responseText || "β οΈ Received response but couldn't parse content",
error: false,
performance: performance
}
}
// π NEW: Progress tracking functions
const startProgressTracking = (provider) => {
loadingProgress.value = 0
if (provider === 'colossus') {
loadingMessage.value = 'Processing with colossus...'
progressInterval = setInterval(() => {
if (loadingProgress.value < 95) {
loadingProgress.value += 1.67 // 60 seconds / 60 intervals
}
}, 1000)
} else {
loadingMessage.value = 'Processing with OpenRouter...'
progressInterval = setInterval(() => {
if (loadingProgress.value < 90) {
loadingProgress.value += 15 // Faster for OpenRouter
}
}, 200)
}
}
const stopProgressTracking = () => {
if (progressInterval) {
clearInterval(progressInterval)
progressInterval = null
}
if (requestTimeout) {
clearTimeout(requestTimeout)
requestTimeout = null
}
loadingProgress.value = 0
}
const switchToOpenRouter = () => {
currentProvider.value = 'openrouter'
// Cancel current request and resend
if (sending.value && newMessage.value) {
// We can't easily cancel the ongoing request, but we can switch provider
stopProgressTracking()
sending.value = false
// Show switching message
messages.push({
type: 'system',
text: 'β‘ Switching to OpenRouter for faster response...',
timestamp: Date.now(),
provider: 'system'
})
}
}
const retryWithOpenRouter = (failedMessage) => {
currentProvider.value = 'openrouter'
if (failedMessage.originalText) {
newMessage.value = failedMessage.originalText
sendMessage()
}
}
const testConnection = async () => {
try {
const response = await fetch(`http://localhost:8000/api/v1/health`)
if (response.ok) {
connectionError.value = ''
} else {
connectionError.value = `Server returned ${response.status}`
}
} catch (error) {
connectionError.value = `Cannot reach server: ${error.message}`
}
}
// π ENHANCED: Send message with improved response parsing
const sendMessage = async () => {
if (!newMessage.value.trim() || sending.value) return
const userMessage = {
type: 'user',
text: newMessage.value,
timestamp: Date.now(),
provider: currentProvider.value,
originalText: newMessage.value // Store for retry
}
messages.push(userMessage)
const messageText = newMessage.value
newMessage.value = ''
sending.value = true
startProgressTracking(currentProvider.value)
await nextTick()
scrollToBottom()
try {
console.log(`π Sending to ${currentProvider.value}: ${messageText}`)
// π ENHANCED: Custom timeout based on provider
const timeoutDuration = currentProvider.value === 'colossus' ? 65000 : 15000 // 65s for colossus, 15s for OpenRouter
const controller = new AbortController()
requestTimeout = setTimeout(() => {
controller.abort()
}, timeoutDuration)
const endpoint = currentProvider.value === 'openrouter'
? `/api/v1/agents/${props.agent.id}/chat/openrouter`
: `/api/v1/agents/${props.agent.id}/chat`
const response = await fetch(`http://localhost:8000${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: messageText,
timestamp: new Date().toISOString() // π§ CRITICAL FIX: String timestamp instead of Date.now()
}),
signal: controller.signal
})
clearTimeout(requestTimeout)
console.log(`π‘ API Response Status:`, response.status, response.statusText)
if (response.ok) {
const data = await response.json()
console.log(`π¦ API Response Data:`, data)
// π§ ENHANCED: Use new parsing function
const parsed = parseResponse(data)
if (!parsed.error) {
connectionError.value = ''
} else {
connectionError.value = parsed.errorDetails || 'Response parsing error'
}
const agentMessage = {
type: 'agent',
text: parsed.text,
timestamp: Date.now(),
error: parsed.error,
errorDetails: parsed.errorDetails,
provider: currentProvider.value,
performance: parsed.performance,
rawResponse: data,
originalText: messageText // Store for retry
}
messages.push(agentMessage)
} else {
const errorText = await response.text()
console.error('API HTTP Error:', response.status, errorText)
connectionError.value = `HTTP ${response.status}: ${response.statusText}`
messages.push({
type: 'agent',
text: `β Server Error: ${response.status} ${response.statusText}`,
timestamp: Date.now(),
error: true,
errorDetails: errorText,
provider: currentProvider.value,
originalText: messageText
})
}
} catch (error) {
if (error.name === 'AbortError') {
const timeoutMsg = currentProvider.value === 'colossus'
? 'β±οΈ colossus timeout after 65 seconds. The server might be overloaded. Try β‘ OpenRouter for faster responses.'
: 'β±οΈ OpenRouter timeout after 15 seconds. Network issue detected.'
connectionError.value = `${currentProvider.value} timeout`
messages.push({
type: 'agent',
text: timeoutMsg,
timestamp: Date.now(),
error: true,
timeout: true,
errorDetails: 'Request timeout - server took too long to respond',
provider: currentProvider.value,
originalText: messageText
})
} else {
console.error('Chat network error:', error)
connectionError.value = `Network error: ${error.message}`
messages.push({
type: 'agent',
text: `β Connection Error: ${error.message}`,
timestamp: Date.now(),
error: true,
errorDetails: error.message,
provider: currentProvider.value,
originalText: messageText
})
}
} finally {
stopProgressTracking()
sending.value = false
await nextTick()
scrollToBottom()
}
}
const retryMessage = async (failedMessage) => {
if (!failedMessage.originalText) return
newMessage.value = failedMessage.originalText
await sendMessage()
}
const scrollToBottom = () => {
if (chatContainer.value) {
chatContainer.value.scrollTop = chatContainer.value.scrollHeight
}
}
const formatTime = (timestamp) => {
return new Date(timestamp).toLocaleTimeString('de-DE', {
hour: '2-digit',
minute: '2-digit'
})
}
// Cleanup on component unmount
onUnmounted(() => {
stopProgressTracking()
})
return {
newMessage,
sending,
messages,
chatContainer,
connectionError,
currentProvider,
loadingProgress,
loadingMessage,
statusClass,
getProviderClass,
getStatusText,
testConnection,
sendMessage,
retryMessage,
retryWithOpenRouter,
switchToOpenRouter,
formatTime
}
}
}
</script>
<style scoped>
.modal-overlay {
@apply fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50;
}
.chat-modal-content {
@apply bg-white rounded-lg shadow-xl w-full max-w-2xl mx-4 h-3/4 flex flex-col;
}
.chat-header {
@apply flex items-center justify-between p-4 border-b border-gray-200;
}
.agent-info {
@apply flex items-center space-x-3;
}
.agent-avatar {
@apply w-10 h-10 rounded-full flex items-center justify-center text-white font-medium;
}
.agent-name {
@apply font-semibold text-gray-900;
}
.agent-status {
@apply text-sm font-medium;
}
.error-indicator {
@apply text-orange-500;
}
/* Provider Controls */
.provider-controls {
@apply flex items-center space-x-3;
}
.provider-switcher {
@apply flex bg-gray-100 rounded-lg p-1;
}
.provider-btn {
@apply px-3 py-2 rounded-md text-sm font-medium transition-colors duration-200;
@apply text-gray-600 hover:text-gray-900 flex items-center space-x-1;
}
.provider-btn.active {
@apply bg-white text-gray-900 shadow-sm;
}
.provider-label {
@apply text-xs bg-gray-200 text-gray-600 px-1 rounded;
}
.provider-btn.active .provider-label {
@apply bg-blue-100 text-blue-700;
}
.close-button {
@apply text-gray-400 hover:text-gray-600 text-xl font-bold w-8 h-8 flex items-center justify-center;
}
/* π NEW: Loading Progress */
.loading-progress {
@apply bg-blue-50 border-b border-blue-200 p-4;
}
.progress-bar {
@apply w-full bg-blue-200 rounded-full h-3 mb-2;
}
.progress-fill {
@apply bg-blue-600 h-full rounded-full transition-all duration-1000;
}
.progress-text {
@apply text-sm text-blue-700 text-center mb-2;
}
.quick-switch-btn {
@apply w-full px-3 py-2 bg-green-600 text-white rounded-md text-sm font-medium;
@apply hover:bg-green-700 transition-colors duration-200;
}
.chat-body {
@apply flex-1 overflow-y-auto p-4 space-y-4;
}
.empty-chat {
@apply text-center py-12;
}
.empty-icon {
@apply text-4xl mb-4;
}
.provider-info {
@apply mt-4 p-3 bg-blue-50 rounded-lg;
}
.provider-warning {
@apply text-sm text-orange-600 mt-2 font-medium;
}
.connection-warning {
@apply text-sm text-orange-600 mt-2;
}
.message {
@apply flex;
}
.message.user {
@apply justify-end;
}
.message.agent {
@apply justify-start;
}
.message-content {
@apply max-w-xs lg:max-w-md px-4 py-2 rounded-lg;
}
.message.user .message-content {
@apply bg-blue-600 text-white;
}
.message.agent .message-content {
@apply bg-gray-200 text-gray-900;
}
.message-content.error-message {
@apply bg-red-100 border border-red-300 text-red-800;
}
.message-content.timeout-message {
@apply bg-orange-100 border border-orange-300 text-orange-800;
}
/* Provider-specific styling */
.message-header {
@apply flex items-center justify-between text-xs mb-2;
}
.provider-badge {
@apply px-2 py-1 rounded text-xs font-medium;
}
.provider-colossus {
@apply bg-blue-100 text-blue-700;
}
.provider-openrouter {
@apply bg-green-100 text-green-700;
}
.provider-comparison {
@apply bg-purple-100 text-purple-700;
}
.provider-system {
@apply bg-gray-100 text-gray-600;
}
.performance-info {
@apply text-gray-500;
}
.cost-info {
@apply text-green-600 font-medium;
}
.message-text {
@apply text-sm;
}
.message-time {
@apply text-xs opacity-70 mt-1;
}
.error-details {
@apply text-xs mt-2 text-red-600 border-t border-red-200 pt-2;
}
.retry-button {
@apply ml-2 text-xs bg-red-600 text-white px-2 py-1 rounded hover:bg-red-700;
}
.switch-retry-button {
@apply ml-1 text-xs bg-green-600 text-white px-2 py-1 rounded hover:bg-green-700;
}
.chat-footer {
@apply border-t border-gray-200 p-4;
}
.connection-error {
@apply mb-3 p-3 bg-red-50 border border-red-200 rounded-md;
}
.error-message {
@apply text-sm text-red-700 flex items-center justify-between;
}
.test-connection-button {
@apply text-xs bg-red-600 text-white px-2 py-1 rounded hover:bg-red-700 ml-2;
}
.message-input-container {
@apply flex space-x-2;
}
.message-input {
@apply flex-1 px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:bg-gray-100;
}
.send-button {
@apply px-4 py-2 text-white rounded-md hover:opacity-90 disabled:bg-gray-400 disabled:cursor-not-allowed;
}
.send-button.provider-colossus {
@apply bg-blue-600;
}
.send-button.provider-openrouter {
@apply bg-green-600;
}
</style>
|