Skip to content

Commit ceacedd

Browse files
author
Jicheng Lu
committed
refine thinking section
1 parent 4707208 commit ceacedd

7 files changed

Lines changed: 153 additions & 9 deletions

File tree

package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"svelte-codemirror-editor": "^2.1.0",
7474
"svelte-collapse": "^0.1.2",
7575
"svelte-file-dropzone": "^2.0.2",
76+
"svelte-hero-icons": "^5.2.0",
7677
"svelte-i18n": "^4.0.0",
7778
"svelte-json-tree": "^2.2.0",
7879
"svelte-jsoneditor": "^3.11.0",

src/lib/common/spinners/Loader.svelte

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66
* disableDefaultStyles?: boolean,
77
* containerClasses?: string,
88
* containerStyles?: string,
9-
* size?: number
9+
* size?: number,
10+
* color?: string
1011
* }}
1112
*/
1213
let {
1314
disableDefaultStyles = false,
1415
containerClasses = '',
1516
containerStyles = '',
16-
size = 100
17+
size = 100,
18+
color = 'var(--bs-primary)'
1719
} = $props();
1820
</script>
1921

2022
<div
2123
class="{disableDefaultStyles ? '' : 'loader'} {containerClasses}"
2224
style={`${containerStyles}`}
2325
>
24-
<Circle {size} color="var(--bs-primary)" unit="px" duration="1s" />
26+
<Circle {size} {color} unit="px" duration="1s" />
2527
</div>

src/lib/helpers/types/conversationTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ IRichContent.prototype.language;
180180
* @property {boolean} is_dummy
181181
* @property {boolean} is_appended
182182
* @property {string} [indication]
183+
* @property {any} [meta_data]
183184
*/
184185

185186
/**

src/lib/scss/custom/pages/_chat.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@
293293
.dropdown-menu {
294294
box-shadow: $box-shadow;
295295
border: 1px solid var(--#{$prefix}border-color);
296-
top: 10px !important;
296+
top: auto !important;
297+
bottom: 100% !important;
297298
}
298299
}
299300

src/routes/chat/[agentId]/[conversationId]/chat-box.svelte

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,11 @@
578578
dialogs.push({
579579
...message,
580580
is_chat_message: false,
581-
is_dummy: true
581+
is_dummy: true,
582+
meta_data: {
583+
...(message.meta_data || {}),
584+
thinking_text: message.meta_data?.thinking_text || ''
585+
}
582586
});
583587
}
584588
refresh();
@@ -595,7 +599,15 @@
595599
&& lastMsg?.is_dummy
596600
) {
597601
setTimeout(() => {
598-
dialogs[dialogs.length - 1].text += message.text;
602+
const lastDialog = dialogs[dialogs.length - 1];
603+
const thinkingText = message.meta_data?.thinking_text || '';
604+
if (thinkingText) {
605+
if (!lastDialog.meta_data) {
606+
lastDialog.meta_data = { thinking_text: '' };
607+
}
608+
lastDialog.meta_data.thinking_text += thinkingText;
609+
}
610+
lastDialog.text += message.text;
599611
refreshDialogs();
600612
}, 0);
601613
}
@@ -624,8 +636,21 @@
624636
}
625637
626638
try {
639+
const lastDialog = dialogs[dialogs.length - 1];
640+
const thinkingText = item.meta_data?.thinking_text || '';
641+
if (thinkingText) {
642+
if (!lastDialog.meta_data) {
643+
lastDialog.meta_data = { thinking_text: '' };
644+
}
645+
for (const tt of thinkingText) {
646+
lastDialog.meta_data.thinking_text += tt;
647+
refreshDialogs();
648+
await delay(10);
649+
}
650+
}
651+
627652
for (const char of item.text) {
628-
dialogs[dialogs.length - 1].text += char;
653+
lastDialog.text += char;
629654
refreshDialogs();
630655
await delay(10);
631656
}

src/routes/chat/[agentId]/[conversationId]/rich-content/rc-message.svelte

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import Markdown from "$lib/common/markdown/Markdown.svelte";
33
import { RichType } from "$lib/helpers/enums";
44
import RcJsInterpreter from "./rc-js-interpreter.svelte";
5+
import { Icon, Sparkles } from "svelte-hero-icons";
6+
import Loader from "$lib/common/spinners/Loader.svelte";
57
68
/**
79
* @type {{
@@ -19,13 +21,51 @@
1921
} = $props();
2022
2123
let text = $derived(message?.rich_content?.message?.text || message?.text || '');
24+
let thinkingText = $derived(message?.meta_data?.thinking_text || '');
25+
let isStillThinking = $derived(!!thinkingText && !text);
26+
let isThinkingExpanded = $state(false);
27+
let isThinkingAutoControlled = $state(true);
28+
29+
$effect(() => {
30+
if (!isThinkingAutoControlled) {
31+
return;
32+
}
33+
34+
if (thinkingText && !text) {
35+
isThinkingExpanded = true;
36+
} else if (text && thinkingText) {
37+
isThinkingExpanded = false;
38+
isThinkingAutoControlled = false;
39+
}
40+
});
2241
</script>
2342
24-
{#if text}
43+
{#if text || thinkingText}
2544
<div
2645
class={`ctext-wrap bg-primary ${containerClasses}`}
2746
style={`${containerStyles}`}
2847
>
48+
{#if thinkingText}
49+
<div class="thinking-section">
50+
<button
51+
class="thinking-toggle"
52+
onclick={() => isThinkingExpanded = !isThinkingExpanded}
53+
>
54+
<span class="thinking-sparkle"><Icon src={Sparkles} solid size="16" /></span>
55+
<span>{'Thinking'}</span>
56+
{#if isStillThinking}
57+
<Loader disableDefaultStyles size={14} color="#4285f4" containerStyles="display: flex; align-items: center;" />
58+
{:else}
59+
<span class="thinking-chevron" class:expanded={isThinkingExpanded}></span>
60+
{/if}
61+
</button>
62+
{#if isThinkingExpanded}
63+
<div class="thinking-content">
64+
<Markdown containerStyles="color: #444 !important;" text={thinkingText} rawText />
65+
</div>
66+
{/if}
67+
</div>
68+
{/if}
2969
<div class="flex-shrink-0 align-self-center">
3070
{#if message?.rich_content?.message?.rich_type === RichType.ProgramCode
3171
&& message?.rich_content?.message?.language === 'javascript'}
@@ -35,4 +75,56 @@
3575
{/if}
3676
</div>
3777
</div>
38-
{/if}
78+
{/if}
79+
80+
<style>
81+
.thinking-section {
82+
margin-bottom: 8px;
83+
}
84+
85+
.thinking-toggle {
86+
display: flex;
87+
align-items: center;
88+
gap: 8px;
89+
background: none;
90+
border: none;
91+
color: #555;
92+
cursor: pointer;
93+
padding: 4px 0;
94+
font-size: 0.9em;
95+
}
96+
97+
.thinking-toggle:hover {
98+
color: #333;
99+
}
100+
101+
.thinking-sparkle {
102+
color: #4285f4;
103+
display: flex;
104+
align-items: center;
105+
}
106+
107+
.thinking-chevron {
108+
font-size: 1em;
109+
transition: transform 0.2s ease;
110+
display: inline-block;
111+
}
112+
113+
.thinking-chevron.expanded {
114+
transform: rotate(90deg);
115+
}
116+
117+
.thinking-content {
118+
font-size: 0.9em;
119+
padding: 4px 0 4px 16px;
120+
border-left: 1px solid #888;
121+
margin-top: 4px;
122+
max-height: 200px;
123+
overflow-y: auto;
124+
scrollbar-width: none;
125+
}
126+
127+
.thinking-content::-webkit-scrollbar {
128+
display: none;
129+
}
130+
</style>

0 commit comments

Comments
 (0)