File size: 15,677 Bytes
04f192c | 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 | <!--
SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project.
SPDX-License-Identifier: Apache-2.0
-->
<script setup lang="ts">
import {
IconChevronDown,
IconChevronUp,
IconCopy,
IconExclamationCircleFilled,
IconLoader2,
IconRefresh,
IconSquareRoundedCheckFilled,
IconTools,
IconUserFilled,
} from "@tabler/icons-vue";
import { useTimestamp } from "@vueuse/core";
import type { Dayjs } from "dayjs";
import dayjs from "dayjs";
import { computed, ref, watch } from "vue";
import type { Post, RetryingState } from "../store";
import ChatboxMarkdownContent from "./ChatboxMarkdownContent.vue";
const props = defineProps<{
posts: Post[];
retrying: RetryingState | null;
}>();
const itemRefs = ref<Record<string, HTMLElement>>({});
const hiddenReasoning = ref(new Set<number>());
const showToolCallDetails = ref(new Set<string>());
const copyStatus = ref<Record<number, boolean>>({}); // Track copy status for each post
const timestamp = useTimestamp();
// Live countdown for the 429 retry bar: reuse the reactive ``timestamp`` (updates
// ~each animation frame) so "waiting Ns" ticks down instead of freezing on a
// static backoff value. The start point resets on every new update:retry event.
const retryStartedAt = ref(0);
const remainingSeconds = computed(() => {
if (!props.retrying) return 0;
const elapsed = (timestamp.value - retryStartedAt.value) / 1000;
return Math.max(0, Math.ceil(props.retrying.wait - elapsed));
});
watch(
() => props.retrying,
(r) => {
if (r) retryStartedAt.value = Date.now();
},
);
defineExpose({
getElement,
});
function copyToClipboard(text: string, postIndex: number) {
navigator.clipboard
.writeText(text)
.then(() => {
// Set copy status to true (success)
copyStatus.value[postIndex] = true;
// Reset status after 2 seconds
setTimeout(() => {
copyStatus.value[postIndex] = false;
}, 2000);
})
.catch((err) => {
console.error("Failed to copy: ", err);
// Set copy status to false (error)
copyStatus.value[postIndex] = false;
});
}
function shouldShowHeader(message: Post, index: number): boolean {
if (index === 0 || isPost(message)) {
return true;
}
const previousItem = props.posts[index - 1];
if (!isPost(previousItem)) {
return true;
}
switch (message.type) {
case "user":
return previousItem.type !== "user";
case "agent":
return previousItem.type !== "agent" || message.role !== previousItem.role;
default:
return false;
}
}
function toggleReasoning(index: number) {
if (hiddenReasoning.value.has(index)) {
hiddenReasoning.value.delete(index);
} else {
hiddenReasoning.value.add(index);
}
}
function toggleToolResult(key: string) {
if (showToolCallDetails.value.has(key)) {
showToolCallDetails.value.delete(key);
} else {
showToolCallDetails.value.add(key);
}
}
function isPost(message: Post): boolean {
return !message.type.startsWith("error:") && !message.type.startsWith("system:");
}
function getAgentName(post: Post): string {
switch (post.type) {
case "agent":
const role = post.role.startsWith("agent:") ? post.role.replace("agent:", "") : post.role;
return role.charAt(0).toUpperCase() + role.slice(1).replace(/_/g, " ");
case "user":
return "You";
case "error:system":
return "System Error";
case "error:assistant":
return "Assistant Error";
default:
return "Unknown";
}
}
function formatTime(t: Dayjs): string {
return t.format("HH:mm");
}
function formatDiffSeconds(start: Dayjs, end?: Dayjs): string {
if (!end) {
end = dayjs(timestamp.value);
}
const sec = end.diff(start, "millisecond") / 1000;
const precision = sec < 10 ? 1 : 0;
return sec.toFixed(precision) + "s";
}
function getElement(id: string): HTMLElement | null {
return itemRefs.value[id] ?? null;
}
</script>
<template>
<div class="chatbox-react-posts flex flex-col w-full text-black dark:text-slate-100">
<template v-for="(post, index) in props.posts" :key="index">
<div
v-if="isPost(post)"
:ref="(el) => (itemRefs[post.id] = el as HTMLElement)"
class="flex flex-col w-full gap-1"
>
<div v-if="shouldShowHeader(post, index)" class="mt-2 flex items-center">
<div
v-if="post.type === 'agent'"
class="w-8 h-8 rounded-full flex items-center justify-center"
:class="{
'bg-blue-500 dark:bg-blue-600': getAgentName(post) == 'Leader Agent',
'bg-emerald-500 dark:bg-emerald-600': getAgentName(post) == 'Research Agent',
'bg-amber-500 dark:bg-amber-600': getAgentName(post) == 'Tool Agent',
}"
>
<img class="w-5 h-5 object-contain" src="../assets/copilotj.png" />
</div>
<div
v-else-if="post.type === 'user'"
class="w-8 h-8 rounded-full flex items-center justify-center overflow-hidden bg-violet-500"
>
<IconUserFilled class="w-6 h-6 rounded-full text-white" />
</div>
<span class="mx-2 pl-1 font-medium">
{{ post.type === "user" ? "You" : getAgentName(post) }}
</span>
</div>
<div class="flex-1 rounded-lg flex flex-col transition-all duration-300">
<!-- Thought -->
<div v-if="post.type === 'agent' && post.thought.length > 0" class="text-slate-600 dark:text-slate-300">
<div class="chatbox-react-posts_message-box flex">
<div class="w-10 flex">
<span class="chatbox-react-posts_message-hover-in mt-3 text-xs transition-all duration-75">
{{ formatTime(post.createAt) }}
</span>
</div>
<div
:class="[
'mt-1 p-2 rounded-lg flex-1 flex justify-between items-center gap-2 cursor-pointer transition hover:shadow',
'hover:bg-white hover:dark:bg-gray-800/80',
]"
@click="toggleReasoning(index)"
>
<div class="text-xs">
<span class="font-bold">Thought</span>
<span class="ml-2">take {{ formatDiffSeconds(post.createAt, post.content?.createAt) }}</span>
</div>
<component :is="hiddenReasoning.has(index) ? IconChevronUp : IconChevronDown" size="16" />
</div>
</div>
<div v-if="!hiddenReasoning.has(index)" class="pt-0 pb-2 rounded-lg flex flex-col gap-2 text-sm">
<template v-for="(item, itemIndex) in post.thought" :key="itemIndex">
<div class="chatbox-react-posts_message-box flex">
<div class="w-10 flex">
<span class="chatbox-react-posts_message-hover-in mt-1 text-xs transition-all duration-75">
{{ formatTime(item.createAt) }}
</span>
</div>
<!-- Reasoning -->
<ChatboxMarkdownContent
v-if="item.type === 'reasoning'"
:value="item.content"
size="sm"
class="ml-4"
/>
<!-- Action -->
<div
v-else-if="item.type === 'action'"
:class="[
'min-w-0 flex-1 ml-4 rounded-lg transition-all flex flex-row',
showToolCallDetails.has(`${index}-${itemIndex}`) ? ' bg-white/50 dark:bg-white/10' : '',
]"
>
<div
:class="[
'w-0 min-h-full border-l-6 border-r-6 rounded-l-lg transition-all cursor-pointer',
showToolCallDetails.has(`${index}-${itemIndex}`)
? 'border-gray-300 dark:border-gray-800'
: 'border-r-gray-500 dark:border-r-gray-400 border-l-transparent',
]"
@click="toggleToolResult(`${index}-${itemIndex}`)"
/>
<div
:class="[
'min-w-0 flex-1 rounded-r-lg transition',
showToolCallDetails.has(`${index}-${itemIndex}`) ? 'shadow hover:shadow-md' : 'hover:shadow',
]"
>
<div
:class="[
'p-2 rounded-r-lg flex flex-col gap-1 cursor-pointer',
'hover:bg-white/50 dark:hover:bg-white/10',
]"
@click="toggleToolResult(`${index}-${itemIndex}`)"
>
<div class="w-full flex items-center gap-2">
<template v-if="item.result">
<IconSquareRoundedCheckFilled
v-if="item.result.type === 'success'"
class="text-emerald-500"
/>
<IconExclamationCircleFilled v-if="item.result.type === 'error'" class="text-yellow-500" />
</template>
<IconLoader2 v-else-if="item.called" class="text-blue-500 animate-spin" />
<IconTools v-else />
<h4 class="font-bold">{{ item.content.tool.display_name }}</h4>
<div
class="min-w-0 flex-1 text-nowrap text-ellipsis overflow-hidden"
:title="item.content.tool.description"
>
{{ item.content.tool.description }}
</div>
</div>
</div>
<div v-if="showToolCallDetails.has(`${index}-${itemIndex}`)" class="p-3 pt-1 flex flex-col gap-2">
<div v-if="Object.keys(item.content.args).length">
<h5 class="text-xs text-slate-400">Arguments:</h5>
<p v-for="(v, k) in item.content.args" :key="k">
<strong>{{ k }}</strong
><span>: {{ v }}</span>
</p>
</div>
<div v-if="item.result">
<h5 class="text-xs text-slate-400">
{{ item.result.type === "success" ? "Result" : "Error" }}:
</h5>
<ChatboxMarkdownContent
class="chatbox-react-posts_action-item"
size="sm"
:value="item.result.data"
/>
</div>
</div>
</div>
</div>
</div>
</template>
</div>
</div>
<!-- Content -->
<div v-if="post.content" class="chatbox-react-posts_message-box flex mt-1">
<div class="w-10 flex">
<span class="chatbox-react-posts_message-hover-in mt-2 text-xs transition-all duration-75">
{{ formatTime(post.content.createAt) }}
</span>
</div>
<div class="w-full flex-1 flex flex-col">
<div
:class="[
'chatbox-react-posts_message-hover-shadow-md rounded-lg p-4 pl-5 flex transition-all duration-300 shadow',
post.type === 'user' ? 'bg-violet-100 dark:bg-violet-950/80' : 'bg-white dark:bg-gray-800/80',
{ 'animate-pulse': post.type == 'agent' && post.streaming },
]"
>
<ChatboxMarkdownContent :value="post.content.data" />
</div>
<!-- Copy Button -->
<div class="chatbox-react-posts_message-hover-in flex justify-start mt-1 ml-2">
<button
@click="copyToClipboard(post.content.data, index)"
:class="[
'flex items-center gap-1 text-xs transition',
'text-slate-700 hover:text-slate-900 dark:text-slate-400 dark:hover:text-slate-200',
]"
title="Copy to clipboard"
>
<IconCopy size="14" />
<span>{{ copyStatus[index] ? "Copied!" : "Copy" }}</span>
</button>
</div>
</div>
</div>
<!-- Thinking animation -->
<div
v-if="index === props.posts.length - 1 && post.type === 'agent' && post.streaming"
class="ml-12 flex items-center gap-2 text-sm text-slate-700 dark:text-slate-500 animate-pulse"
>
<p v-if="post.content">🤗 Answering...</p>
<p v-else>🤔 Thinking...</p>
</div>
<!-- 429 auto-retry feedback (#96) -->
<div
v-if="props.retrying && index === props.posts.length - 1"
class="ml-12 flex items-center gap-2 text-sm text-amber-600 dark:text-amber-400 animate-pulse"
>
<IconRefresh size="16" class="animate-spin" />
<span>
Rate limited — retrying ({{ props.retrying.attempt }}/{{ props.retrying.maxAttempts }}, waiting
{{ remainingSeconds }}s)…
</span>
</div>
</div>
</div>
<!-- Dialog -->
<div
v-else-if="post.type === 'system:dialog_completed'"
:ref="(el) => (itemRefs[post.id] = el as HTMLElement)"
class="chatbox-react-posts_message-box flex my-1"
>
<div class="w-10 flex">
<span class="chatbox-react-posts_message-hover-in text-xs transition-all duration-75">
{{ post.content && formatTime(post.content?.createAt) }}
</span>
</div>
<div class="flex-1 mx-4 flex items-center gap-4">
<div class="flex-1 border-b border-gray-400 dark:border-gray-600" />
<p class="text-center text-sm text-gray-400 dark:text-gray-600">Dialog {{ post.content?.data }} completed</p>
<div class="flex-1 border-b border-gray-400 dark:border-gray-600" />
</div>
</div>
<!-- System -->
<div
v-else
:ref="(el) => (itemRefs[post.id] = el as HTMLElement)"
class="chatbox-react-posts_message-box flex my-4"
>
<div class="w-10 flex">
<span class="chatbox-react-posts_message-hover-in mt-2 text-xs transition-all duration-75">
{{ post.content && formatTime(post.content?.createAt) }}
</span>
</div>
<div
:class="[
'flex-1 mx-16 rounded-md border p-2 text-sm transition-colors',
'text-red-800 dark:text-red-400 border-red-200 dark:border-red-500/30 bg-red-50 dark:bg-red-900/20',
'hover:border-red-300 dark:hover:border-red-500/50',
]"
>
<p class="text-center">{{ post.content?.data }}</p>
</div>
</div>
</template>
</div>
</template>
<style lang="postcss">
@reference "../style.css";
.chatbox-react-posts .chatbox-react-posts_message-box {
.chatbox-react-posts_message-hover-in {
opacity: 0;
}
&:hover .chatbox-react-posts_message-hover-in {
opacity: 0.5;
}
&:hover .chatbox-react-posts_message-hover-shadow-md {
@apply shadow-md;
}
}
</style>
|