Skip to content

Commit cfa1ebe

Browse files
FjHehfj
andauthored
feat(chat): add input history navigation and message copy (#101)
* feat(chat): add input history navigation and message copy * style(chat): fix Prettier formatting in ChatMessage.vue * refactor(chat): clean up copy button timer leak on unmount --------- Co-authored-by: hfj <hfj@qq.com>
1 parent b675028 commit cfa1ebe

3 files changed

Lines changed: 175 additions & 15 deletions

File tree

‎data-agent-frontend/src/views/chat/ChatView.vue‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
// 已发过消息 = 绑定已落库 = 不可再切换数据源(锁定语义)。
5656
const isBound = computed(() => messages.value.length > 0);
5757
58+
// 用户在当前会话中发过的消息文本(用于输入框上下键历史导航)
59+
const userMessages = computed(() =>
60+
messages.value.filter(m => m.role === 'user').map(m => m.content),
61+
);
62+
5863
// 未绑定的旧会话实际跟随全局激活源,展示其名字以免用户误以为可自由选源。
5964
const activeDatasourceName = computed(() => {
6065
if (datasourceId.value != null) return null;
@@ -247,8 +252,10 @@
247252
</div>
248253

249254
<ChatInput
255+
:key="sessionId"
250256
:is-streaming="isStreaming"
251257
:pending-question="pendingQuestion"
258+
:user-messages="userMessages"
252259
@send="handleSend"
253260
@stop="stopStreaming"
254261
/>

‎data-agent-frontend/src/views/chat/components/ChatInput.vue‎

Lines changed: 103 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
-->
1717

1818
<script setup lang="ts">
19-
import { ref, nextTick } from 'vue';
19+
import { ref, computed, nextTick, watch } from 'vue';
2020
2121
import type { PendingQuestion } from '@/composables/useAgentChat';
2222
2323
const props = defineProps<{
2424
isStreaming: boolean;
2525
pendingQuestion: PendingQuestion | null;
26+
// Previous user messages from the chat, used as input history for Up/Down navigation.
27+
// Ordered oldest → newest.
28+
userMessages: string[];
2629
}>();
2730
2831
const emit = defineEmits<{
@@ -31,28 +34,113 @@
3134
}>();
3235
3336
const inputText = ref('');
34-
const textareaRef = ref<{ style: { height: string }; scrollHeight: number }>();
37+
const textareaRef = ref<{
38+
value: string;
39+
style: { height: string };
40+
scrollHeight: number;
41+
selectionStart: number;
42+
selectionEnd: number;
43+
}>();
44+
45+
// -1 = not browsing; 0 = newest user message, increasing = older
46+
const historyIndex = ref(-1);
47+
48+
// Full original message at the current history position (used by Tab confirmation)
49+
const historyFull = computed(() => {
50+
if (historyIndex.value < 0) return '';
51+
const msgs = props.userMessages;
52+
const idx = msgs.length - 1 - historyIndex.value;
53+
return idx >= 0 && idx < msgs.length ? msgs[idx] : '';
54+
});
55+
56+
// Truncated version shown as placeholder: max 20 chars or cut at first newline,
57+
// whichever comes first. Appends "…" whenever anything was dropped.
58+
const historyPreview = computed(() => {
59+
const raw = historyFull.value;
60+
if (!raw) return null;
61+
const nl = raw.indexOf('\n');
62+
const cut = nl === -1 ? raw.length : nl;
63+
const limit = Math.min(50, cut);
64+
const truncated = raw.slice(0, limit);
65+
return truncated.length < raw.length ? truncated + '…' : truncated;
66+
});
67+
68+
watch(inputText, () => {
69+
nextTick(autoResize);
70+
});
3571
3672
function handleSend() {
3773
const text = inputText.value.trim();
3874
if (!text || props.isStreaming) return;
75+
historyIndex.value = -1;
3976
emit('send', text);
4077
inputText.value = '';
41-
nextTick(() => {
42-
if (textareaRef.value) {
43-
textareaRef.value.style.height = 'auto';
44-
}
45-
});
4678
}
4779
48-
function handleKeydown(e: { key: string; shiftKey: boolean; preventDefault: () => void }) {
80+
function handleKeydown(e: {
81+
key: string;
82+
shiftKey: boolean;
83+
ctrlKey: boolean;
84+
altKey: boolean;
85+
metaKey: boolean;
86+
preventDefault: () => void;
87+
}) {
4988
if (e.key === 'Enter' && !e.shiftKey) {
5089
e.preventDefault();
5190
if (props.isStreaming) {
5291
emit('stop');
5392
} else {
5493
handleSend();
5594
}
95+
return;
96+
}
97+
98+
const hasModifier = e.shiftKey || e.ctrlKey || e.altKey || e.metaKey;
99+
if (hasModifier) return;
100+
101+
const el = textareaRef.value;
102+
if (!el) return;
103+
104+
// History preview via Up/Down:
105+
// - When input is empty → always works
106+
// - When input has text → only when cursor is at the boundary
107+
// (Up at start of text, Down at end of text)
108+
// The history item shows as placeholder (not real value); Tab to confirm.
109+
110+
if (e.key === 'ArrowUp') {
111+
if (props.userMessages.length === 0) return;
112+
const len = el.value.length;
113+
const cursorAtStart = el.selectionStart === 0 && el.selectionEnd === 0;
114+
if (len > 0 && !cursorAtStart) return;
115+
e.preventDefault();
116+
const next = historyIndex.value === -1 ? 0 : historyIndex.value + 1;
117+
historyIndex.value = Math.min(next, props.userMessages.length - 1);
118+
return;
119+
}
120+
121+
if (e.key === 'ArrowDown') {
122+
if (historyIndex.value === -1) return;
123+
const len = el.value.length;
124+
const cursorAtEnd = el.selectionStart === len && el.selectionEnd === len;
125+
if (len > 0 && !cursorAtEnd) return;
126+
e.preventDefault();
127+
historyIndex.value = Math.max(historyIndex.value - 1, -1);
128+
return;
129+
}
130+
131+
// Tab: confirm the current history preview, filling the full original text into the input
132+
if (e.key === 'Tab' && historyPreview.value !== null) {
133+
e.preventDefault();
134+
inputText.value = historyFull.value;
135+
historyIndex.value = -1;
136+
nextTick(autoResize);
137+
return;
138+
}
139+
140+
// Escape: dismiss the history preview
141+
if (e.key === 'Escape' && historyPreview.value !== null) {
142+
e.preventDefault();
143+
historyIndex.value = -1;
56144
}
57145
}
58146
@@ -75,15 +163,16 @@
75163
v-model="inputText"
76164
class="chat-input__textarea"
77165
:placeholder="
78-
props.pendingQuestion && !isStreaming
79-
? '输入你的回答,Enter 发送...'
80-
: isStreaming
81-
? '正在回复中...'
82-
: '输入消息,Enter 发送,Shift+Enter 换行'
166+
historyPreview !== null
167+
? historyPreview + ' (Tab 确认)'
168+
: props.pendingQuestion && !isStreaming
169+
? '输入你的回答,Enter 发送...'
170+
: isStreaming
171+
? '正在回复中...'
172+
: '输入消息,Enter 发送,Shift+Enter 换行'
83173
"
84174
:disabled="isStreaming"
85175
rows="1"
86-
@input="autoResize"
87176
@keydown="handleKeydown"
88177
/>
89178
<el-button

‎data-agent-frontend/src/views/chat/components/ChatMessage.vue‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
-->
1717

1818
<script setup lang="ts">
19-
import { computed } from 'vue';
19+
import { computed, ref, onUnmounted } from 'vue';
2020
import { marked } from 'marked';
2121
import type { ChatMessage as ChatMessageType } from '@/composables/useAgentChat';
2222
import TracePanel from './TracePanel.vue';
@@ -46,10 +46,37 @@
4646
4747
const renderedText = computed(() => marked.parse(contentParts.value.text) as string);
4848
const renderedSummary = computed(() => marked.parse(contentParts.value.summary) as string);
49+
50+
const copied = ref(false);
51+
let resetTimer: ReturnType<typeof setTimeout> | null = null;
52+
async function copyMessage() {
53+
try {
54+
await navigator.clipboard.writeText(props.message.content);
55+
copied.value = true;
56+
if (resetTimer) clearTimeout(resetTimer);
57+
resetTimer = setTimeout(() => {
58+
copied.value = false;
59+
}, 2000);
60+
} catch {
61+
// Clipboard API may fail in insecure contexts; ignore.
62+
}
63+
}
64+
65+
onUnmounted(() => {
66+
if (resetTimer) clearTimeout(resetTimer);
67+
});
4968
</script>
5069

5170
<template>
5271
<div class="chat-message" :class="`chat-message--${message.role}`">
72+
<div v-if="message.role === 'user'" class="chat-message__actions">
73+
<button type="button" class="chat-message__copy-btn" title="复制" @click.stop="copyMessage">
74+
<el-icon :size="16">
75+
<Check v-if="copied" />
76+
<CopyDocument v-else />
77+
</el-icon>
78+
</button>
79+
</div>
5380
<div class="chat-message__bubble">
5481
<TracePanel
5582
v-if="message.role === 'agent'"
@@ -88,6 +115,7 @@
88115
89116
.chat-message--user {
90117
justify-content: flex-end;
118+
align-items: center;
91119
}
92120
93121
.chat-message--agent {
@@ -257,6 +285,42 @@
257285
opacity: 0.7;
258286
}
259287
288+
.chat-message__actions {
289+
flex-shrink: 0;
290+
display: flex;
291+
align-items: center;
292+
padding-right: 8px;
293+
}
294+
295+
.chat-message__copy-btn {
296+
display: flex;
297+
align-items: center;
298+
justify-content: center;
299+
width: 28px;
300+
height: 28px;
301+
padding: 0;
302+
border: none;
303+
border-radius: 6px;
304+
background: transparent;
305+
color: var(--app-text-secondary);
306+
opacity: 0;
307+
cursor: pointer;
308+
transition:
309+
opacity 0.15s,
310+
background-color 0.15s,
311+
color 0.15s;
312+
}
313+
314+
.chat-message--user:hover .chat-message__copy-btn {
315+
opacity: 0.6;
316+
}
317+
318+
.chat-message__copy-btn:hover {
319+
opacity: 1 !important;
320+
color: var(--app-text-primary);
321+
background: var(--app-bg-hover);
322+
}
323+
260324
@keyframes blink {
261325
0%,
262326
100% {

0 commit comments

Comments
 (0)