Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 60 additions & 60 deletions chatbot/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
useState,
} from "react";
import { useChat } from "./useChat";
import { useComposerDraft } from "./useComposerDraft.js";
import NativeApprovalPanel from "./NativeApprovalPanel";
import { AgentBrowserLinks } from "./agent-browser-links.js";
import ActivityTrace, { ReplyTimeline } from "./ActivityTrace";
Expand Down Expand Up @@ -108,6 +109,25 @@ function sidebarViewport() {
return window;
}
}
function navigateHistorySearch(event) {
if (event.nativeEvent.isComposing || event.nativeEvent.keyCode === 229) return;
const { key, target, currentTarget } = event;
if (!["Enter", "ArrowDown", "ArrowUp"].includes(key)) return;
const search = currentTarget.querySelector("input");
const results = Array.from(currentTarget.querySelectorAll(".search-results button"));
if (!results.length) return;
if (key === "Enter") {
if (target === search) {
event.preventDefault();
results[0].click();
}
return;
}
event.preventDefault();
const index = results.indexOf(target);
if (key === "ArrowDown") results[Math.min(index + 1, results.length - 1)].focus();
else (index < 1 ? search : results[index - 1]).focus();
}
function readSidebarWidth() {
// Also migrate standalone workbench loads, before the first React render.
if (localStorage.getItem(sidebarLayoutKey) !== sidebarLayoutVersion) {
Expand Down Expand Up @@ -331,11 +351,8 @@ export default function App() {
[expandedProjects, setExpandedProjects] = useState({}),
[pinnedOpen, setPinnedOpen] = useState(true),
[pins, setPins] = useState({}),
[text, setText] = useState(""),
[attachments, setAttachments] = useState([]),
[pendingAttachments, setPendingAttachments] = useState([]),
[imagePreview, setImagePreview] = useState(null),
[references, setReferences] = useState([]),
[issueMention, setIssueMention] = useState(readIssueMention),
[issueDraft, setIssueDraft] = useState(readIssueMention),
[mention, setMention] = useState(null),
Expand Down Expand Up @@ -363,6 +380,16 @@ export default function App() {
setIssueDraft(null);
setIssueMention(null);
chat.newChat();
closeNarrowSidebar();
}
function closeNarrowSidebar() {
if (window.innerWidth <= 850) setSidebar(false);
}
function openHistorySession(id) {
setSessionContext(null);
chat.open(id);
setQuery("");
closeNarrowSidebar();
}
const input = useRef(),
sessionPress = useRef(null),
Expand All @@ -373,14 +400,16 @@ export default function App() {
fileInput = useRef(),
scroll = useRef(),
follow = useRef(true),
draftReady = useRef(null),
previousArtifacts = useRef({ id: null, ids: new Set(), baselined: false }),
dialogInput = useRef(),
composerPreviews = useRef(new Map());
// A requirement handed over by the workbench drafts in its own slot so the
// project's unsent new-conversation draft and attachments stay untouched.
const newDraftSlot = issueDraft && issueDraft.projectId === chat.projectId ? "issue:" + issueDraft.issueId : "new:" + chat.projectId;
const draftKey = `agentflow.native-chat.draft.${chat.user?.id || "guest"}.${chat.activeId || newDraftSlot}`;
const draftPrefix = `agentflow.native-chat.draft.${chat.user?.id}.`;
const draftKey = chat.user ? draftPrefix + (chat.activeId || newDraftSlot) : null;
const { text, setText, attachments, setAttachments, references, setReferences, begin } =
useComposerDraft(draftKey, chat.setError);
useEffect(() => {
if (!chat.activeId) return;
setIssueDraft(null);
Expand Down Expand Up @@ -557,25 +586,6 @@ export default function App() {
window.addEventListener("storage", onStorage);
return () => window.removeEventListener("storage", onStorage);
}, []);
useEffect(() => {
if (!chat.user) return;
const value = readJSON(draftKey, {});
setText(value.text || "");
setAttachments(value.attachments || []);
setReferences(value.references || []);
draftReady.current = draftKey;
}, [draftKey, chat.user?.id]);
useEffect(() => {
if (!chat.user || draftReady.current !== draftKey) return;
try {
localStorage.setItem(
draftKey,
JSON.stringify({ text, attachments, references }),
);
} catch {
chat.setError("浏览器存储空间不足,草稿仍保留在当前页面。请移除较大的附件后重试。");
}
}, [text, attachments, references, draftKey, chat.user?.id]);
// A requirement handed over by the workbench ("创建并去对话澄清") opens a
// new conversation with the requirement already @-mentioned. It waits for
// the new conversation's draft to load so the draft does not overwrite it.
Expand All @@ -602,8 +612,7 @@ export default function App() {
!chat.user ||
chat.activeId ||
chat.projectId !== issueMention.projectId ||
newDraftSlot !== "issue:" + issueMention.issueId ||
draftReady.current !== draftKey
newDraftSlot !== "issue:" + issueMention.issueId
) return;
let current = true;
api(`/api/issues/${encodeURIComponent(issueMention.issueId)}`)
Expand Down Expand Up @@ -765,26 +774,23 @@ export default function App() {
closeMenu();
}
async function submit() {
if (uploading) return;
if (uploading || chat.submitting || !chat.ready || chat.machineSaving ||
(chat.activeId && chat.session?.id !== chat.activeId)) return;
const submission = begin();
if (!submission) return;
follow.current = true;
const ok = await chat.send({
text,
attachments,
references,
approvalMode: approval,
});
if (ok) {
setMention(null);
setText("");
setAttachments([]);
for (const a of attachments) releaseComposerPreview(a.id);
setReferences([]);
localStorage.removeItem(draftKey);
if (input.current) input.current.style.height = "";
} else {
// A failed first message may already have opened a new conversation,
// which loads that conversation's (empty) draft. Keep what was typed.
setText((current) => current || text);
setMention(null);
if (input.current) input.current.style.height = "";
let accepted = false;
try {
accepted = await chat.send({
...submission.sent,
approvalMode: approval,
onSessionCreated: (id, selected) => submission.bind(draftPrefix + id, selected),
});
} finally {
submission.finish(accepted);
if (accepted) for (const a of submission.sent.attachments) releaseComposerPreview(a.id);
}
}
function editQueued(item) {
Expand Down Expand Up @@ -1055,10 +1061,7 @@ export default function App() {
event.preventDefault();
return;
}
setSessionContext(null);
chat.open(s.id);
setQuery("");
if (innerWidth < 850) setSidebar(false);
openHistorySession(s.id);
}}
>
<span>{s.displayTitle || s.title || "新对话"}</span>
Expand Down Expand Up @@ -2062,12 +2065,9 @@ export default function App() {
}
}}
onKeyDown={(e) => {
if (e.nativeEvent.isComposing || e.nativeEvent.keyCode === 229) return;
if (mention && mentionPicker.current?.keyDown(e)) return;
if (
e.key === "Enter" &&
!e.shiftKey &&
!e.nativeEvent.isComposing
) {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
void submit();
}
Expand Down Expand Up @@ -2350,11 +2350,10 @@ export default function App() {
}
>
{dialog.type === "search" ? (
<>
<div onKeyDown={navigateHistorySearch}>
<label className="dialog-search">
<HarnessIcon name="search" size={17} />
<input
autoFocus
aria-label="搜索会话名称"
placeholder="搜索会话名称或项目"
value={query}
Expand All @@ -2372,9 +2371,8 @@ export default function App() {
<button
key={s.id}
onClick={() => {
chat.open(s.id);
openHistorySession(s.id);
closeDialog();
setQuery("");
}}
>
<HarnessIcon name="new-request" size={16} />
Expand All @@ -2390,10 +2388,12 @@ export default function App() {
</section>
))}
{!searchGroups.length ? (
<p className="end-label">没有匹配的会话</p>
<p className="end-label" role="status">
{query.trim() ? "没有匹配的会话,试试其他会话名称或项目名称。" : "还没有会话"}
</p>
) : null}
</div>
</>
</div>
) : dialog.type === "rename" ? (
<label className="field">
会话名称
Expand Down
16 changes: 12 additions & 4 deletions chatbot/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -2022,9 +2022,13 @@ small {
font-size: 13px;
}
.search-results > button:hover,
.search-results > section > button:hover {
.search-results > section > button:hover,
.search-results button:focus-visible {
background: var(--subtle);
}
.search-results button:focus-visible {
outline-offset: -2px;
}
.search-results > button > span,
.search-results > section > button > span {
display: flex;
Expand Down Expand Up @@ -3885,10 +3889,15 @@ a.draft-title {
flex: 1;
}

/* Narrow conversations (side panel open): keep send/stop reachable and let
the model and approval labels shrink first. */
/* The CLI, model and approval labels vary in width. Keep every control
reachable when their combined width exceeds the composer. */
.composer-toolbar {
min-width: 0;
flex-wrap: wrap;
row-gap: 4px;
}
.composer-toolbar > .send-button:first-of-type {
margin-inline-start: auto;
}
.composer-toolbar > * {
flex-shrink: 0;
Expand All @@ -3909,7 +3918,6 @@ a.draft-title {
@container (max-width: 360px) {
.composer-toolbar {
gap: 1px;
flex-wrap: wrap;
row-gap: 4px;
}
.chat-shell button.permission-selector {
Expand Down
Loading