✨ Rebuild the Copilot panel as a chat box a customer can use

The panel was a form that submitted a question and printed an answer. It is now
a conversation: a bubble per message, a picture strip, a folded reasoning line,
a progress sentence in the customer's words, and cards that ask for confirmation
instead of repeating the answer.

The input box is the part that was missing. A paperclip opens the file picker,
the whole box accepts a dropped file, and a pasted screenshot becomes an
attachment too; each one shows as a thumbnail with a remove button, and the send
button waits until nothing is still uploading. Enter sends, Shift+Enter breaks
the line, and the box grows to five lines.

The panel also has a page of its own at /copilot, and the docked panel steps
aside there so the same conversation is never rendered twice.

Colour, spacing and radii follow one small set of tokens (teal #0f766e, 12px
cards and bubbles, 8px buttons) so the panel reads as part of the console.
This commit is contained in:
wishforge 2026-11-09 19:07:55 -08:00
parent 20c5ff1691
commit c959b96f54
6 changed files with 1040 additions and 366 deletions

View File

@ -0,0 +1,9 @@
import { CopilotPanel } from '@/features/copilot';
export default function CopilotPage() {
return (
<div className="copilot-full">
<CopilotPanel variant="page" />
</div>
);
}

View File

@ -3279,6 +3279,9 @@ fieldset:disabled label {
}
}
/* Copilot: one conversation, shown as a docked panel or as the page at /copilot.
Brand colour is the console's teal (#0f766e); bubbles are square-ish cards, not speech balloons,
because every answer here is a short report rather than a chat line. */
.copilot-toggle {
display: inline-flex;
align-items: center;
@ -3286,15 +3289,20 @@ fieldset:disabled label {
min-height: 34px;
padding: 5px 10px;
border: 1px solid var(--border);
border-radius: 6px;
border-radius: 8px;
color: var(--teal-dark);
background: white;
white-space: nowrap;
}
.copilot-toggle.active,
.copilot-toggle:hover {
background: var(--teal-pale);
border-color: #b8dcd3;
background: #f0fdfa;
border-color: #99d7cb;
}
.copilot-panel {
display: flex;
flex-direction: column;
min-height: 0;
}
.copilot-sidebar {
position: fixed;
@ -3302,9 +3310,7 @@ fieldset:disabled label {
width: 520px;
max-width: 100vw;
z-index: 45;
display: flex;
flex-direction: column;
background: #fbfcfe;
background: #fbfcfd;
border-left: 1px solid var(--border);
box-shadow: -8px 0 28px #1020310a;
transform: translateX(100%);
@ -3315,92 +3321,177 @@ fieldset:disabled label {
transform: translateX(0);
visibility: visible;
}
.copilot-page {
position: relative;
width: 100%;
max-width: 800px;
margin: 0 auto;
height: min(78vh, 860px);
background: white;
border: 1px solid var(--border);
border-radius: 16px;
box-shadow: 0 2px 8px rgba(15, 23, 42, 0.06);
overflow: hidden;
}
.copilot-full {
display: flex;
justify-content: center;
padding: 8px 0 24px;
}
.copilot-header {
min-height: 65px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 12px 16px;
border-bottom: 1px solid var(--border);
background: white;
}
.copilot-header > div,
.copilot-header span {
.copilot-header > div:first-child {
display: flex;
align-items: center;
gap: 10px;
min-width: 0;
}
.copilot-brand {
display: inline-flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
border-radius: 10px;
color: #0f766e;
background: #f0fdfa;
border: 1px solid #ccfbf1;
}
.copilot-header span {
align-items: flex-start;
flex-direction: column;
gap: 0;
min-width: 0;
}
.copilot-header strong {
display: block;
font-size: 14px;
line-height: 1.3;
}
.copilot-header small {
display: block;
color: var(--muted);
font-size: 12px;
}
.copilot-header-actions {
display: flex;
align-items: center;
gap: 4px;
}
.copilot-project {
display: flex;
gap: 8px;
padding: 12px 14px;
padding: 10px 14px;
border-bottom: 1px solid var(--border);
background: #fbfcfd;
}
.copilot-project select {
min-width: 0;
flex: 1;
height: 34px;
padding: 0 10px;
border: 1px solid var(--border);
border-radius: 8px;
background: white;
}
.copilot-messages {
flex: 1;
min-height: 0;
overflow-y: auto;
padding: 16px 14px;
padding: 18px 16px 8px;
scroll-behavior: smooth;
}
.copilot-welcome {
text-align: center;
color: var(--muted);
padding: 38px 20px;
padding: 30px 16px 12px;
}
.copilot-welcome svg {
color: var(--teal);
margin-bottom: 12px;
.copilot-welcome-icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: 44px;
height: 44px;
margin-bottom: 10px;
border-radius: 14px;
color: #0f766e;
background: #f0fdfa;
border: 1px solid #ccfbf1;
}
.copilot-welcome h2 {
color: var(--ink);
margin-bottom: 8px;
font-size: 16px;
margin-bottom: 6px;
}
.copilot-welcome p {
font-size: 13px;
}
.copilot-message {
display: flex;
align-items: flex-start;
gap: 8px;
margin-bottom: 16px;
overflow-wrap: anywhere;
}
.copilot-message.user {
display: flex;
justify-content: flex-end;
}
.copilot-message.user span {
max-width: 90%;
padding: 9px 12px;
background: var(--teal-pale);
border-radius: 10px 10px 2px 10px;
white-space: pre-wrap;
.copilot-avatar {
display: inline-flex;
align-items: center;
justify-content: center;
flex: 0 0 28px;
width: 28px;
height: 28px;
margin-top: 2px;
border-radius: 9px;
color: #0f766e;
background: #f0fdfa;
border: 1px solid #ccfbf1;
}
.copilot-message.assistant {
padding: 12px;
.copilot-bubble {
max-width: 88%;
padding: 11px 14px;
border-radius: 12px;
font-size: 13.5px;
line-height: 1.65;
}
.copilot-message.assistant .copilot-bubble {
background: #f0fdfa;
color: #10312c;
border: 1px solid #d8f3ec;
}
.copilot-message.user .copilot-bubble {
background: white;
border: 1px solid var(--border);
border-radius: 8px;
border-radius: 12px 12px 4px 12px;
}
.copilot-message.error {
border-color: #e8bfc2;
.copilot-message.user .copilot-bubble p {
margin: 0;
white-space: pre-wrap;
}
.copilot-message.is-error .copilot-bubble {
background: #fff7f7;
border-color: #f3c9cb;
}
.copilot-answer {
white-space: pre-wrap;
margin-bottom: 10px;
margin: 0 0 8px;
}
.copilot-answer:last-child {
margin-bottom: 0;
}
.copilot-kind {
display: inline-flex;
align-items: center;
gap: 5px;
padding: 2px 7px;
border-radius: 12px;
padding: 2px 8px;
margin-top: 4px;
border-radius: 999px;
background: #eef2f6;
color: #42546a;
font-size: 11px;
@ -3410,127 +3501,221 @@ fieldset:disabled label {
color: #9d343c;
}
.copilot-kind.feature {
background: var(--teal-pale);
color: var(--teal-dark);
background: #eafaf6;
color: #0f766e;
}
.copilot-sources {
margin: 12px 0;
padding: 10px;
background: #f6f8fb;
border-radius: 6px;
margin: 10px 0 0;
padding: 10px 12px;
background: #ffffffcc;
border: 1px solid #d8f3ec;
border-radius: 10px;
}
.copilot-sources > strong {
display: block;
font-size: 12px;
color: #0f766e;
margin-bottom: 2px;
}
.copilot-source {
padding: 7px 0;
border-top: 1px solid var(--border);
border-top: 1px solid #e6f6f1;
}
.copilot-source:first-of-type {
border-top: 0;
}
.copilot-source > a {
display: inline-flex;
align-items: center;
gap: 4px;
color: var(--teal-dark);
font-size: 12.5px;
}
.copilot-source small {
display: block;
color: var(--muted);
}
.copilot-wish-draft {
display: grid;
gap: 12px;
margin-top: 12px;
}
.copilot-wish-draft label {
display: grid;
gap: 5px;
font-size: 12px;
font-weight: 600;
}
.copilot-wish-draft textarea {
font-weight: 400;
width: 100%;
}
.copilot-bug-context {
padding: 10px;
background: #fff8f2;
border-radius: 6px;
}
.copilot-bug-context ol {
padding-left: 20px;
margin: 5px 0 10px;
}
.grill-card {
border: 1px solid #c9ded9;
border-radius: 7px;
padding: 10px;
margin: 0;
}
.grill-card legend {
padding: 0 5px;
font-weight: 600;
}
.grill-card > small {
display: block;
color: var(--muted);
margin-bottom: 8px;
}
.grill-card > div {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
.grill-card button {
border: 1px solid var(--border);
.copilot-card {
margin-top: 12px;
background: white;
border-radius: 5px;
padding: 5px 9px;
border: 1px solid #dbe7e4;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(15, 23, 42, 0.06);
overflow: hidden;
}
.grill-card button.selected {
border-color: var(--teal);
background: var(--teal-pale);
color: var(--teal-dark);
.copilot-card > header {
display: flex;
align-items: center;
gap: 6px;
padding: 10px 14px;
color: #0f766e;
background: #f0fdfa;
border-bottom: 1px solid #d8f3ec;
font-size: 13px;
}
.copilot-card-row {
padding: 10px 14px;
border-bottom: 1px solid #f0f4f8;
background: #fbfdfd;
}
.copilot-card-row h4 {
margin: 0 0 4px;
font-size: 11.5px;
font-weight: 600;
letter-spacing: 0.02em;
color: var(--muted);
}
.copilot-card-row p,
.copilot-card-row ul,
.copilot-card-row ol {
margin: 0 0 6px;
white-space: pre-wrap;
font-size: 13px;
}
.copilot-card-row ul,
.copilot-card-row ol {
padding-left: 18px;
}
.copilot-card-row > :last-child {
margin-bottom: 0;
}
.copilot-card > footer {
padding: 12px 14px;
background: white;
}
.copilot-primary {
width: 100%;
min-height: 38px;
border: 0;
border-radius: 8px;
color: white;
background: #0f766e;
font-weight: 600;
}
.copilot-primary:hover:not(:disabled) {
background: #115e59;
}
.copilot-secondary {
display: inline-flex;
align-items: center;
gap: 6px;
min-height: 32px;
padding: 5px 12px;
border: 1px solid var(--border);
border-radius: 8px;
background: white;
color: var(--ink);
}
.copilot-wish-link {
justify-content: center;
}
.copilot-composer {
padding: 12px 14px 14px;
border-top: 1px solid var(--border);
background: white;
}
.copilot-composer textarea {
width: 100%;
min-height: 40px;
max-height: 160px;
overflow-y: auto;
resize: none;
}
.copilot-composer > div {
.copilot-choice {
margin: 0;
padding: 10px 14px;
border: 0;
border-bottom: 1px solid #f0f4f8;
}
.copilot-choice legend {
padding: 0;
font-size: 12.5px;
font-weight: 600;
color: var(--ink);
}
.copilot-choice > div {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
flex-wrap: wrap;
gap: 7px;
margin-top: 8px;
}
.copilot-composer small {
.copilot-choice button {
padding: 6px 12px;
border: 1px solid #d7e3e0;
border-radius: 999px;
background: white;
font-size: 12.5px;
color: var(--ink);
}
.copilot-choice button:hover {
border-color: #99d7cb;
}
.copilot-choice button.selected {
border-color: #0f766e;
background: #f0fdfa;
color: #0f766e;
font-weight: 600;
}
.copilot-quick-replies {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 10px;
}
.copilot-quick-replies button {
padding: 5px 12px;
border: 1px solid #d7e3e0;
border-radius: 999px;
background: white;
font-size: 12px;
color: var(--teal-dark);
}
.copilot-quick-replies button:hover {
border-color: #99d7cb;
background: #f0fdfa;
}
.copilot-welcome .copilot-quick-replies {
justify-content: center;
margin-top: 14px;
}
.copilot-thinking {
margin-bottom: 8px;
padding: 8px 10px;
background: #ffffffb3;
border: 1px solid #d8f3ec;
border-radius: 10px;
font-size: 12px;
color: var(--muted);
}
.copilot-thinking summary {
cursor: pointer;
color: #0f766e;
font-weight: 600;
}
.copilot-thinking p {
margin: 6px 0 0;
white-space: pre-wrap;
}
.copilot-error {
color: var(--red);
margin-top: 8px;
margin: 8px 0 0;
}
.copilot-error-card {
margin-top: 4px;
padding: 10px 12px;
background: #fff7f7;
border: 1px solid #f3c9cb;
border-radius: 10px;
}
.copilot-error-card p {
margin: 0 0 8px;
color: #9d343c;
font-size: 13px;
}
.copilot-progress {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--muted);
font-size: 12px;
margin-bottom: 8px;
font-size: 12.5px;
margin: 0 0 6px;
}
.copilot-progress::before {
content: '';
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--teal);
background: #0f766e;
animation: copilot-pulse 1.2s ease-in-out infinite;
}
@keyframes copilot-pulse {
@ -3544,11 +3729,8 @@ fieldset:disabled label {
}
.copilot-cursor {
display: inline-block;
width: 2px;
height: 1em;
margin-left: 2px;
vertical-align: -2px;
background: var(--teal-dark);
color: #0f766e;
animation: copilot-blink 1s step-end infinite;
}
@keyframes copilot-blink {
@ -3556,94 +3738,181 @@ fieldset:disabled label {
opacity: 0;
}
}
.copilot-thinking {
margin-bottom: 10px;
padding: 8px 10px;
background: #f6f8fb;
border-radius: 6px;
font-size: 12px;
color: var(--muted);
}
.copilot-thinking summary {
cursor: pointer;
color: var(--ink);
font-weight: 600;
}
.copilot-thinking p {
margin-top: 6px;
white-space: pre-wrap;
}
.copilot-quick-replies {
.copilot-message-pictures {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 10px;
margin: 8px 0 0;
padding: 0;
list-style: none;
}
.copilot-quick-replies button {
.copilot-message-pictures img {
width: 84px;
height: 84px;
object-fit: cover;
border-radius: 8px;
border: 1px solid var(--border);
}
.copilot-composer {
position: relative;
margin: 8px 14px 14px;
padding: 8px 10px 6px;
background: white;
border: 1px solid var(--border);
border-radius: 14px;
padding: 5px 11px;
font-size: 12px;
color: var(--teal-dark);
box-shadow: 0 2px 8px rgba(15, 23, 42, 0.05);
transition:
border-color 0.15s ease,
box-shadow 0.15s ease;
}
.copilot-quick-replies button:hover {
border-color: #b8dcd3;
background: var(--teal-pale);
.copilot-composer:focus-within {
border-color: #5ec2b0;
box-shadow: 0 0 0 3px #0f766e1f;
}
.copilot-welcome .copilot-quick-replies {
justify-content: center;
.copilot-composer.is-dragging {
border-color: #0f766e;
border-style: dashed;
background: #f0fdfa;
}
.copilot-card-goal {
padding: 10px;
background: #f6f8fb;
border-radius: 6px;
}
.copilot-card-goal strong {
display: block;
margin-bottom: 4px;
font-size: 12px;
}
.copilot-card-goal p,
.copilot-card-goal ul {
margin: 0;
white-space: pre-wrap;
}
.copilot-card-goal ul {
padding-left: 18px;
}
.wiki-source {
background: white;
border: 1px solid var(--border);
border-radius: var(--radius);
overflow: hidden;
}
.wiki-source > pre {
margin: 0;
padding: 24px;
white-space: pre-wrap;
overflow-wrap: anywhere;
line-height: 1.75;
}
.wiki-source footer {
padding: 18px 24px;
border-top: 1px solid var(--border);
background: #f8fafc;
}
.wiki-source footer div {
.copilot-drop-hint {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
gap: 6px;
margin-top: 8px;
align-items: center;
justify-content: center;
border-radius: 14px;
background: #f0fdfae6;
color: #0f766e;
font-weight: 600;
z-index: 2;
pointer-events: none;
}
.wiki-source footer a {
color: var(--teal-dark);
font-family: 'SFMono-Regular', Consolas, monospace;
font-size: 12px;
.copilot-thumbs {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin: 2px 0 8px;
padding: 0;
list-style: none;
}
.copilot-thumb {
position: relative;
width: 64px;
height: 64px;
border-radius: 10px;
overflow: hidden;
border: 1px solid var(--border);
background: #f1f5f9;
}
.copilot-thumb img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
.copilot-thumb.error {
border-color: #f3c9cb;
}
.copilot-thumb-state {
position: absolute;
inset: auto 0 0 0;
padding: 1px 4px;
font-size: 10px;
text-align: center;
color: white;
background: #0f766ecc;
}
.copilot-thumb.error .copilot-thumb-state {
background: #be444ccc;
}
.copilot-thumb > button {
position: absolute;
top: 2px;
right: 2px;
display: inline-flex;
align-items: center;
justify-content: center;
width: 18px;
height: 18px;
padding: 0;
border: 0;
border-radius: 50%;
color: white;
background: #0f172acc;
}
.copilot-thumb > button:hover {
background: #0f172a;
}
.copilot-input-row {
display: flex;
align-items: flex-end;
gap: 6px;
}
.copilot-attach {
display: inline-flex;
align-items: center;
justify-content: center;
flex: 0 0 auto;
width: 32px;
height: 32px;
border: 0;
border-radius: 8px;
color: var(--muted);
background: transparent;
}
.copilot-attach:hover:not(:disabled) {
color: #0f766e;
background: #f0fdfa;
}
.copilot-input-row textarea {
flex: 1;
min-height: 32px;
max-height: 160px;
padding: 6px 2px;
border: 0;
outline: none;
background: transparent;
resize: none;
line-height: 1.55;
}
.copilot-send {
display: inline-flex;
align-items: center;
justify-content: center;
flex: 0 0 auto;
width: 32px;
height: 32px;
border: 0;
border-radius: 50%;
color: white;
background: #0f766e;
}
.copilot-send:hover:not(:disabled) {
background: #115e59;
}
.copilot-send.is-stop {
background: #425466;
}
.copilot-input-foot {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
padding: 4px 2px 0;
font-size: 11px;
color: var(--muted);
}
.copilot-input-foot > span {
display: inline-flex;
align-items: center;
gap: 4px;
}
.copilot-input-notice {
color: #9d343c;
}
@media (min-width: 1101px) {
.console.copilot-open .main-shell {
margin-right: 400px;
margin-right: 520px;
}
.console.copilot-open .main-content {
padding-left: 22px;
@ -3668,4 +3937,8 @@ fieldset:disabled label {
.copilot-sidebar {
width: 100vw;
}
.copilot-page {
height: calc(100vh - 140px);
border-radius: 12px;
}
}

View File

@ -69,8 +69,10 @@ export function ConsoleShell({ children }: { children: ReactNode }) {
name?: string;
}>('/session');
const current = navigation.find(([href]) => (href === '/' ? pathname === '/' : pathname.startsWith(href)));
// The Copilot has its own page: the docked panel would otherwise render the same conversation twice.
const copilotPage = pathname === '/copilot' || pathname.startsWith('/copilot/');
return (
<div className={`console ${copilotOpen ? 'copilot-open' : ''}`}>
<div className={`console ${copilotOpen && !copilotPage ? 'copilot-open' : ''}`}>
<a className="skip-link" href="#main">
跳转到主要内容
</a>
@ -158,7 +160,7 @@ export function ConsoleShell({ children }: { children: ReactNode }) {
{children}
</main>
</div>
<CopilotSidebar open={copilotOpen} onClose={toggleCopilot} />
{copilotPage ? null : <CopilotSidebar open={copilotOpen} onClose={toggleCopilot} />}
</div>
);
}

View File

@ -0,0 +1,211 @@
'use client';
import { ArrowUp, ImagePlus, Paperclip, Square, X } from 'lucide-react';
import { useCallback, useEffect, useRef, useState } from 'react';
/**
* What a picture is doing on its way to the question: chosen, uploading, ready, or refused.
* `id` is the server's handle and is the only part the question needs to send.
*/
export type PendingPicture = {
key: string;
name: string;
size: number;
contentType: string;
preview: string;
status: 'uploading' | 'ready' | 'error';
id?: string;
error?: string;
};
export const PICTURE_TYPES = ['image/png', 'image/jpeg', 'image/webp'];
export const MAX_PICTURES = 3;
export const MAX_PICTURE_BYTES = 4 * 1024 * 1024;
/** The reason a file cannot be attached, in words a customer can act on. */
export function pictureProblem(file: { type: string; size: number }, count: number) {
if (count >= MAX_PICTURES) return `一次最多附上 ${MAX_PICTURES} 张图片`;
if (!PICTURE_TYPES.includes(file.type)) return '只支持 PNG、JPEG 或 WebP 图片';
if (file.size > MAX_PICTURE_BYTES) return '每张图片不能超过 4MB';
if (file.size === 0) return '这张图片是空的';
return '';
}
function Thumbnail({ picture, onRemove }: { picture: PendingPicture; onRemove: () => void }) {
return (
<li className={`copilot-thumb ${picture.status}`}>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={picture.preview} alt={picture.name} />
{picture.status === 'uploading' ? <span className="copilot-thumb-state">上传中</span> : null}
{picture.status === 'error' ? (
<span className="copilot-thumb-state" title={picture.error}>
失败
</span>
) : null}
<button type="button" onClick={onRemove} aria-label={`移除 ${picture.name}`}>
<X size={12} />
</button>
</li>
);
}
export function CopilotComposer({
value,
onChange,
onSend,
onStop,
streaming,
disabled,
pictures,
onPicturesChange,
onPickFiles,
hint,
}: {
value: string;
onChange: (value: string) => void;
onSend: () => void;
onStop: () => void;
streaming: boolean;
disabled: boolean;
pictures: PendingPicture[];
onPicturesChange: (pictures: PendingPicture[]) => void;
/** Uploads the chosen files and resolves once each one has settled. */
onPickFiles: (files: File[]) => Promise<void>;
hint: string;
}) {
const area = useRef<HTMLTextAreaElement>(null);
const picker = useRef<HTMLInputElement>(null);
const [dragging, setDragging] = useState(false);
const [notice, setNotice] = useState('');
// The box grows with the text and stops at five lines, exactly like a chat box should.
useEffect(() => {
const element = area.current;
if (!element) return;
element.style.height = 'auto';
element.style.height = `${Math.min(element.scrollHeight, 160)}px`;
}, [value]);
const accept = useCallback(
async (files: File[]) => {
const images = files.filter(file => file.type.startsWith('image/') || file.name.match(/\.(png|jpe?g|webp)$/i));
if (!images.length) {
setNotice('只支持 PNG、JPEG 或 WebP 图片');
return;
}
setNotice('');
await onPickFiles(images);
},
[onPickFiles],
);
const uploading = pictures.some(picture => picture.status === 'uploading');
return (
<div
className={`copilot-composer ${dragging ? 'is-dragging' : ''}`}
onDragOver={event => {
if (!event.dataTransfer.types.includes('Files')) return;
event.preventDefault();
setDragging(true);
}}
onDragLeave={event => {
if (event.currentTarget.contains(event.relatedTarget as Node)) return;
setDragging(false);
}}
onDrop={event => {
if (!event.dataTransfer.files.length) return;
event.preventDefault();
setDragging(false);
void accept([...event.dataTransfer.files]);
}}
>
{dragging ? <div className="copilot-drop-hint">松开即可添加图片</div> : null}
{pictures.length ? (
<ul className="copilot-thumbs">
{pictures.map(picture => (
<Thumbnail
key={picture.key}
picture={picture}
onRemove={() => onPicturesChange(pictures.filter(item => item.key !== picture.key))}
/>
))}
</ul>
) : null}
<div className="copilot-input-row">
<button
type="button"
className="copilot-attach"
aria-label="添加图片"
title="添加图片(也可拖入或粘贴)"
disabled={disabled || streaming || pictures.length >= MAX_PICTURES}
onClick={() => picker.current?.click()}
>
<Paperclip size={17} />
</button>
<input
ref={picker}
type="file"
accept={PICTURE_TYPES.join(',')}
multiple
hidden
onChange={event => {
const files = [...(event.target.files ?? [])];
event.target.value = '';
void accept(files);
}}
/>
<label htmlFor="copilot-question" className="sr-only">
询问项目 Copilot
</label>
<textarea
id="copilot-question"
ref={area}
rows={1}
maxLength={8000}
value={value}
disabled={disabled}
onChange={event => onChange(event.target.value)}
onPaste={event => {
const files = [...event.clipboardData.files];
if (!files.length) return;
event.preventDefault();
void accept(files);
}}
onKeyDown={event => {
if (event.key === 'Enter' && !event.shiftKey && !event.nativeEvent.isComposing) {
event.preventDefault();
onSend();
}
}}
placeholder="问用法、报问题或说需求…可以贴截图(Enter 发送,Shift+Enter 换行)"
/>
{streaming ? (
<button type="button" className="copilot-send is-stop" onClick={onStop} aria-label="停止回答">
<Square size={14} fill="currentColor" />
</button>
) : (
<button
type="button"
className="copilot-send"
onClick={onSend}
disabled={disabled || uploading || !value.trim()}
aria-label="发送"
>
<ArrowUp size={17} />
</button>
)}
</div>
<div className="copilot-input-foot">
<span>
{notice ? (
<span className="copilot-input-notice">{notice}</span>
) : (
<>
<ImagePlus size={12} /> {hint}
</>
)}
</span>
<span className="copilot-input-count">{value.length ? `${value.length}/8000` : ''}</span>
</div>
</div>
);
}

View File

@ -47,6 +47,8 @@ export function useCopilotStream() {
sessionId: string;
content: string;
clientId: string;
/** Ids of the pictures uploaded for this question, in the order they were shown. */
attachments?: string[];
context?: { pathname?: string; runId?: string };
onDone?: () => void;
}) => {
@ -61,6 +63,7 @@ export function useCopilotStream() {
body: JSON.stringify({
content: input.content,
clientId: input.clientId,
...(input.attachments?.length ? { attachments: input.attachments } : {}),
...(input.context ? { context: input.context } : {}),
}),
cache: 'no-store',

View File

@ -1,18 +1,21 @@
'use client';
import { BookOpen, Bot, Bug, ExternalLink, Lightbulb, Plus, RefreshCw, Send, Square, X } from 'lucide-react';
import { BookOpen, Bot, Bug, ExternalLink, Lightbulb, Maximize2, Plus, RefreshCw, Sparkles, X } from 'lucide-react';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { useEffect, useRef, useState } from 'react';
import { withAnswers } from '@wishforge/contracts/wish';
import type { CopilotMessage, CopilotSession } from '@wishforge/contracts';
import type { CopilotAttachment, CopilotMessage, CopilotSession } from '@wishforge/contracts';
import { api, messageOf, mutateApi, useResource } from '@/lib/api';
import { browserId } from '@/lib/browser-id';
import type { Project } from '@/lib/types';
import { Button, ErrorNotice, Notice } from '@/components/ui';
import { ErrorNotice, Notice } from '@/components/ui';
import { useCopilotStream } from './copilot-stream';
import { CopilotComposer, MAX_PICTURES, PICTURE_TYPES, pictureProblem, type PendingPicture } from './copilot-composer';
const kindLabels = { faq: '使用说明', bug: '疑似 Bug', feature: '需求', unknown: '待补充' } as const;
const sessionKey = (projectId: string) => `wishforge.copilot.session.${projectId}`;
/** What a customer is most likely to want to say first, one click each. */
const scenes = ['你能帮我做什么?', '某个按钮点了没反应', '想把首页那句介绍改个说法'];
function Sources({ message }: { message: { sources?: CopilotMessage['sources']; knowledgeVersion?: string } }) {
if (!message.sources?.length) return null;
@ -31,10 +34,35 @@ function Sources({ message }: { message: { sources?: CopilotMessage['sources'];
);
}
/** The pictures a question was sent with, still visible after a reload. */
function PictureStrip({
pictures,
projectId,
sessionId,
}: {
pictures: CopilotAttachment[];
projectId: string;
sessionId: string;
}) {
const url = (id: string) => `/api/projects/${projectId}/copilot/sessions/${sessionId}/attachments/${id}`;
return (
<ul className="copilot-message-pictures">
{pictures.map(picture => (
<li key={picture.id}>
<a href={url(picture.id)} target="_blank" rel="noreferrer" title={picture.name}>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={url(picture.id)} alt={picture.name} />
</a>
</li>
))}
</ul>
);
}
/**
* The Wish the Copilot drafted, shown as one card to confirm. The customer picks between the
* options the Copilot offered, or types nothing at all: the card already says what will change and
* how it will be checked. Only the confirm button creates the Wish.
* The Wish the Copilot drafted, shown as one card to confirm. The card never repeats the answer
* above it: it says what will change (only when that is a different sentence), how it will be
* checked, and what the customer may want to pick before confirming.
*/
function WishDraft({
message,
@ -47,8 +75,6 @@ function WishDraft({
sessionId: string;
onCreated: () => void;
}) {
// The card is the confirmation, not a second copy of the answer. When the Copilot did not write a
// separate goal, the answer above already says what will change, so the card must not repeat it.
const statedGoal = message.goal?.trim() ?? '';
const baseGoal = statedGoal || message.content;
const repeatsAnswer = statedGoal === message.content.trim();
@ -96,10 +122,14 @@ function WishDraft({
}
};
return (
<div className="copilot-wish-draft">
<div className="copilot-card">
<header>
<Sparkles size={15} />
<strong>{message.kind === 'bug' ? '确认这次修复' : '确认这个需求'}</strong>
</header>
{message.kind === 'bug' ? (
<div className="copilot-bug-context">
<strong>复现信息</strong>
<section className="copilot-card-row">
<h4>复现信息</h4>
{message.reproductionSteps?.length ? (
<ol>
{message.reproductionSteps.map(step => (
@ -109,22 +139,22 @@ function WishDraft({
) : (
<p>还没提供复现步骤,工程师会再确认。</p>
)}
<strong>当时的情况</strong>
<h4>当时的情况</h4>
<p>{message.contextSummary || '没有额外说明。'}</p>
</div>
</section>
) : null}
{statedGoal && !repeatsAnswer ? (
<div className="copilot-card-goal">
<strong>要做的改动</strong>
<section className="copilot-card-row">
<h4>要做的改动</h4>
<p>{goal}</p>
</div>
</section>
) : null}
<div className="copilot-card-goal">
<strong>做完怎么确认</strong>
<section className="copilot-card-row">
<h4>做完怎么确认</h4>
<ul>{criteria.length ? criteria.map(item => <li key={item}>{item}</li>) : <li>改动完成即可。</li>}</ul>
</div>
</section>
{message.questions?.map((question, questionIndex) => (
<fieldset className="grill-card" key={question.question}>
<fieldset className="copilot-choice" key={question.question}>
<legend>{question.question}</legend>
<div>
{question.options.map((option, optionIndex) => (
@ -146,15 +176,17 @@ function WishDraft({
{error}
</p>
) : null}
{created || message.wish ? (
<Link className="button button-secondary copilot-wish-link" href={(created ?? message.wish)!.url}>
查看这次改动 <ExternalLink size={14} />
</Link>
) : (
<Button type="button" busy={busy} disabled={!goal} onClick={() => void submit()}>
确认,开始处理
</Button>
)}
<footer>
{created || message.wish ? (
<Link className="button button-secondary copilot-wish-link" href={(created ?? message.wish)!.url}>
查看这次改动 <ExternalLink size={14} />
</Link>
) : (
<button type="button" className="copilot-primary" disabled={busy || !goal} onClick={() => void submit()}>
{busy ? '正在提交…' : '确认,开始处理'}
</button>
)}
</footer>
</div>
);
}
@ -175,39 +207,46 @@ function StreamingAnswer({
}) {
if (!state.streaming && !state.content && !state.error && !state.thinking) return null;
return (
<article className={`copilot-message assistant${state.error ? ' error' : ''}`}>
{state.thinking ? (
<details className="copilot-thinking">
<summary>思考过程</summary>
<p>{state.thinking}</p>
</details>
) : null}
{state.progress ? (
<p className="copilot-progress" role="status">
{state.progress}
</p>
) : null}
{state.content ? (
<p className="copilot-answer">
{state.content}
{state.streaming ? <span className="copilot-cursor" aria-hidden /> : null}
</p>
) : null}
{state.streaming && !state.content && !state.progress ? (
<p className="copilot-progress" role="status">
正在准备回答…
</p>
) : null}
{state.error ? (
<>
<p role="alert" className="copilot-error">
{state.error}
<article className={`copilot-message assistant${state.error ? ' is-error' : ''}`}>
<span className="copilot-avatar" aria-hidden>
<Bot size={15} />
</span>
<div className="copilot-bubble">
{state.thinking ? (
<details className="copilot-thinking">
<summary>思考过程</summary>
<p>{state.thinking}</p>
</details>
) : null}
{state.progress ? (
<p className="copilot-progress" role="status">
{state.progress}
</p>
<button type="button" className="button button-secondary" onClick={onRetry}>
<RefreshCw size={14} /> 重试
</button>
</>
) : null}
) : null}
{state.content ? (
<p className="copilot-answer">
{state.content}
{state.streaming ? (
<span className="copilot-cursor" aria-hidden>
▊
</span>
) : null}
</p>
) : null}
{state.streaming && !state.content && !state.progress ? (
<p className="copilot-progress" role="status">
正在准备回答…
</p>
) : null}
{state.error ? (
<div className="copilot-error-card">
<p role="alert">{state.error}</p>
<button type="button" className="copilot-secondary" onClick={onRetry}>
<RefreshCw size={14} /> 重试
</button>
</div>
) : null}
</div>
</article>
);
}
@ -218,53 +257,100 @@ function Message({
sessionId,
onCreated,
onQuickReply,
onRetry,
}: {
message: CopilotMessage;
projectId: string;
sessionId: string;
onCreated: () => void;
onQuickReply: (reply: string) => void;
onRetry: () => void;
}) {
if (message.role === 'user')
return (
<div className="copilot-message user">
<span>{message.content}</span>
<div className="copilot-bubble">
<p>{message.content}</p>
{message.attachments?.length ? (
<PictureStrip pictures={message.attachments} projectId={projectId} sessionId={sessionId} />
) : null}
</div>
</div>
);
if (message.status === 'error')
return (
<article className="copilot-message assistant is-error">
<span className="copilot-avatar" aria-hidden>
<Bot size={15} />
</span>
<div className="copilot-bubble">
<div className="copilot-error-card">
<p role="alert">{message.content || '这次没能回答上,请重试。'}</p>
<button type="button" className="copilot-secondary" onClick={onRetry}>
<RefreshCw size={14} /> 重试
</button>
</div>
</div>
</article>
);
return (
<article className={`copilot-message assistant ${message.status}`}>
{message.status === 'pending' ? <p className="copilot-progress">正在查找资料…</p> : null}
{message.status !== 'pending' ? <p className="copilot-answer">{message.content}</p> : null}
{message.kind ? (
<span className={`copilot-kind ${message.kind}`}>
{message.kind === 'bug' ? (
<Bug size={12} />
) : message.kind === 'feature' ? (
<Lightbulb size={12} />
) : (
<BookOpen size={12} />
)}
{kindLabels[message.kind]}
</span>
) : null}
<Sources message={message} />
{message.status === 'complete' && ['bug', 'feature'].includes(message.kind ?? '') ? (
<WishDraft message={message} projectId={projectId} sessionId={sessionId} onCreated={onCreated} />
) : null}
{message.status === 'complete' && message.quickReplies?.length ? (
<div className="copilot-quick-replies">
{message.quickReplies.map(reply => (
<button type="button" key={reply} onClick={() => onQuickReply(reply)}>
{reply}
</button>
))}
</div>
) : null}
<span className="copilot-avatar" aria-hidden>
<Bot size={15} />
</span>
<div className="copilot-bubble">
{message.status === 'pending' ? (
<p className="copilot-progress" role="status">
正在查找资料…
</p>
) : (
<>
<p className="copilot-answer">{message.content}</p>
{message.kind ? (
<span className={`copilot-kind ${message.kind}`}>
{message.kind === 'bug' ? (
<Bug size={12} />
) : message.kind === 'feature' ? (
<Lightbulb size={12} />
) : (
<BookOpen size={12} />
)}
{kindLabels[message.kind]}
</span>
) : null}
<Sources message={message} />
{message.status === 'complete' && ['bug', 'feature'].includes(message.kind ?? '') ? (
<WishDraft message={message} projectId={projectId} sessionId={sessionId} onCreated={onCreated} />
) : null}
{message.status === 'complete' && message.quickReplies?.length ? (
<div className="copilot-quick-replies">
{message.quickReplies.map(reply => (
<button type="button" key={reply} onClick={() => onQuickReply(reply)}>
{reply}
</button>
))}
</div>
) : null}
</>
)}
</div>
</article>
);
}
export function CopilotSidebar({ open, onClose }: { open: boolean; onClose: () => void }) {
/**
* The conversation itself. It is rendered twice: as the side panel that follows the customer around
* the console, and as the full page at `/copilot` for a longer conversation.
*/
export function CopilotPanel({
variant = 'drawer',
open = true,
onClose,
}: {
variant?: 'drawer' | 'page';
open?: boolean;
onClose?: () => void;
}) {
const pathname = usePathname();
const projects = useResource<Project[]>('/projects');
const routeProject = pathname.match(/^\/projects\/([^/]+)/)?.[1];
@ -275,23 +361,25 @@ export function CopilotSidebar({ open, onClose }: { open: boolean; onClose: () =
const [error, setError] = useState('');
const [pendingUser, setPendingUser] = useState('');
const [lastPrompt, setLastPrompt] = useState('');
const composer = useRef<HTMLTextAreaElement>(null);
const [pictures, setPictures] = useState<PendingPicture[]>([]);
const stream = useCopilotStream();
const streaming = stream.state.streaming;
const scroller = useRef<HTMLDivElement>(null);
useEffect(() => {
setChosenProject(current => current || localStorage.getItem('wishforge.copilot.project') || '');
}, []);
useEffect(() => {
if (routeProject) {
if (routeProject && variant === 'drawer') {
localStorage.setItem('wishforge.copilot.project', routeProject);
setChosenProject(routeProject);
}
}, [routeProject]);
}, [routeProject, variant]);
useEffect(() => {
if (projectId) localStorage.setItem('wishforge.copilot.project', projectId);
setSessionId(projectId ? (localStorage.getItem(sessionKey(projectId)) ?? '') : '');
setError('');
setPendingUser('');
setPictures([]);
stream.reset();
// A project switch starts a fresh conversation view; the stored sessions are per project.
// eslint-disable-next-line react-hooks/exhaustive-deps
@ -308,39 +396,135 @@ export function CopilotSidebar({ open, onClose }: { open: boolean; onClose: () =
const timer = window.setInterval(() => void sessionResource.mutate(), 3000);
return () => window.clearInterval(timer);
}, [pending, open, streaming, sessionResource.mutate]);
const messages = sessionResource.data?.projectId === projectId ? sessionResource.data.messages : [];
// One answer, one place. While it streams the local copy is the only one; once the stored message
// is in the transcript that becomes the answer, so the streamed text steps aside.
const streamedText = stream.state.content.trim();
const answerAlreadyStored =
Boolean(streamedText) &&
messages.some(message => message.role === 'assistant' && message.content.trim() === streamedText);
const showStream =
streaming || Boolean(stream.state.error) || (Boolean(stream.state.content) && !answerAlreadyStored);
// A new message scrolls itself into view, so the answer is never written below the fold.
const transcript = messages.length + (pendingUser ? 1 : 0);
useEffect(() => {
const element = composer.current;
if (!element) return;
element.style.height = 'auto';
element.style.height = `${Math.min(element.scrollHeight, 160)}px`;
}, [input]);
const element = scroller.current;
if (element) element.scrollTop = element.scrollHeight;
}, [transcript, stream.state.content, stream.state.progress]);
const ensureSession = async () => {
if (sessionId) return sessionId;
if (!projectId) throw new Error('请先选择一个项目。');
const created = await api<CopilotSession>(`/projects/${projectId}/copilot/sessions`, { method: 'POST' });
localStorage.setItem(sessionKey(projectId), created.id);
setSessionId(created.id);
return created.id;
};
const upload = async (file: File, id: string) => {
const response = await fetch(
`/api/projects/${projectId}/copilot/sessions/${id}/attachments?name=${encodeURIComponent(file.name)}`,
{
method: 'POST',
headers: { 'Content-Type': file.type },
body: file,
cache: 'no-store',
credentials: 'same-origin',
},
);
const payload = await response.json().catch(() => null);
if (!response.ok) throw new Error(payload?.error?.message ?? '图片没能上传,请重试。');
return payload.data as { id: string; name: string; size: number; contentType: string; url: string };
};
const pickFiles = async (files: File[]) => {
if (!projectId) {
setError('请先选择一个项目。');
return;
}
const room = MAX_PICTURES - pictures.length;
if (room <= 0) {
setError(`一次最多附上 ${MAX_PICTURES} 张图片。`);
return;
}
const chosen = files.slice(0, room).map(file => {
const picture: PendingPicture = {
key: `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
name: file.name,
size: file.size,
contentType: file.type,
preview: URL.createObjectURL(file),
status: 'uploading' as const,
};
const problem = pictureProblem(file, pictures.length);
return { file, problem, picture };
});
if (files.length > room) setError(`一次最多附上 ${MAX_PICTURES} 张图片,多出的没有加上。`);
setPictures(current => [
...current,
...chosen.map(({ picture, problem }) =>
problem ? { ...picture, status: 'error' as const, error: problem } : picture,
),
]);
// The session has to exist before a picture can belong to it; creating it here also keeps the
// upload and the question in the same conversation.
let id = '';
try {
id = await ensureSession();
} catch (failure) {
setPictures(current =>
current.map(item =>
chosen.some(({ picture }) => picture.key === item.key) && item.status === 'uploading'
? { ...item, status: 'error' as const, error: messageOf(failure) }
: item,
),
);
return;
}
for (const { file, problem, picture } of chosen) {
if (problem) continue;
try {
const stored = await upload(file, id);
setPictures(current =>
current.map(item => (item.key === picture.key ? { ...item, status: 'ready', id: stored.id } : item)),
);
} catch (failure) {
setPictures(current =>
current.map(item =>
item.key === picture.key ? { ...item, status: 'error', error: messageOf(failure) } : item,
),
);
}
}
};
const newSession = () => {
if (projectId) localStorage.removeItem(sessionKey(projectId));
setSessionId('');
setError('');
setPendingUser('');
setPictures([]);
stream.reset();
};
const send = async (text?: string) => {
const content = (text ?? input).trim();
if (!projectId || !content || streaming) return;
if (pictures.some(picture => picture.status === 'uploading')) {
setError('图片还在上传,稍等一下再发送。');
return;
}
const attached = pictures.flatMap(picture => (picture.status === 'ready' && picture.id ? [picture.id] : []));
setError('');
setInput('');
setPendingUser(content);
setLastPrompt(content);
const sent = pictures;
setPictures([]);
try {
let id = sessionId;
if (!id) {
const created = await api<CopilotSession>(`/projects/${projectId}/copilot/sessions`, { method: 'POST' });
id = created.id;
localStorage.setItem(sessionKey(projectId), id);
setSessionId(id);
}
const id = await ensureSession();
await stream.start({
projectId,
sessionId: id,
content,
clientId: browserId(),
...(attached.length ? { attachments: attached } : {}),
context: { pathname },
onDone: () => {
setPendingUser('');
@ -354,42 +538,56 @@ export function CopilotSidebar({ open, onClose }: { open: boolean; onClose: () =
});
} catch (failure) {
setPendingUser('');
setPictures(sent);
setError(messageOf(failure));
stream.reset();
}
};
const messages = sessionResource.data?.projectId === projectId ? sessionResource.data.messages : [];
// One answer, one place. While it streams the local copy is the only one; once the stored message
// is in the transcript that becomes the answer, so the streamed text steps aside.
const streamedText = stream.state.content.trim();
const answerAlreadyStored =
Boolean(streamedText) &&
messages.some(message => message.role === 'assistant' && message.content.trim() === streamedText);
const showStream =
streaming || Boolean(stream.state.error) || (Boolean(stream.state.content) && !answerAlreadyStored);
return (
<aside
className={`copilot-sidebar ${open ? 'is-open' : ''}`}
className={`copilot-panel ${variant === 'drawer' ? `copilot-sidebar ${open ? 'is-open' : ''}` : 'copilot-page'}`}
aria-label="AI Copilot"
inert={!open ? true : undefined}
inert={variant === 'drawer' && !open ? true : undefined}
>
<div className="copilot-header">
<div>
<Bot size={19} />
<span className="copilot-brand" aria-hidden>
<Bot size={17} />
</span>
<span>
<strong>项目 Copilot</strong>
<small>问项目、报问题、提需求</small>
</span>
</div>
<button className="icon-button" onClick={onClose} aria-label="收起 AI Copilot">
<X size={18} />
</button>
<div className="copilot-header-actions">
<button
type="button"
className="icon-button"
onClick={newSession}
disabled={!projectId}
aria-label="新建对话"
title="新建对话"
>
<Plus size={17} />
</button>
{variant === 'drawer' ? (
<Link className="icon-button" href="/copilot" aria-label="在新页面打开" title="在新页面打开">
<Maximize2 size={16} />
</Link>
) : null}
{variant === 'drawer' && onClose ? (
<button className="icon-button" onClick={onClose} aria-label="收起 AI Copilot">
<X size={18} />
</button>
) : null}
</div>
</div>
<div className="copilot-project">
<select
aria-label="Copilot 项目"
value={projectId}
disabled={Boolean(routeProject)}
disabled={variant === 'drawer' && Boolean(routeProject)}
onChange={event => {
localStorage.setItem('wishforge.copilot.project', event.target.value);
setChosenProject(event.target.value);
@ -402,25 +600,18 @@ export function CopilotSidebar({ open, onClose }: { open: boolean; onClose: () =
</option>
))}
</select>
<button
className="icon-button"
onClick={newSession}
disabled={!projectId}
aria-label="新建 Copilot 会话"
title="新建会话"
>
<Plus size={17} />
</button>
</div>
<div className="copilot-messages" aria-live="polite">
<div className="copilot-messages" aria-live="polite" ref={scroller}>
{!projectId ? <Notice>选择一个项目后,可以询问它的用法、报告问题或提出需求。</Notice> : null}
{projectId && !messages.length && !showStream && !sessionResource.isLoading ? (
<div className="copilot-welcome">
<Bot size={28} />
<h2>先问这个项目</h2>
<p>说清楚在哪一步、点了什么、本来想看到什么,我就能查资料帮你定位。</p>
<span className="copilot-welcome-icon">
<Sparkles size={22} />
</span>
<h2>你好!我是 WishForge 助手 👋</h2>
<p>可以帮你定位问题、提改进建议。试试这些:</p>
<div className="copilot-quick-replies">
{['你能帮我做什么?', '某个按钮点了没反应', '想把首页那句介绍改个说法'].map(scene => (
{scenes.map(scene => (
<button type="button" key={scene} onClick={() => void send(scene)}>
{scene}
</button>
@ -436,11 +627,14 @@ export function CopilotSidebar({ open, onClose }: { open: boolean; onClose: () =
sessionId={sessionId}
onCreated={() => void sessionResource.mutate()}
onQuickReply={reply => void send(reply)}
onRetry={() => void send(lastPrompt)}
/>
))}
{pendingUser && !messages.some(message => message.role === 'user' && message.content === pendingUser) ? (
<div className="copilot-message user">
<span>{pendingUser}</span>
<div className="copilot-bubble">
<p>{pendingUser}</p>
</div>
</div>
) : null}
<StreamingAnswer state={stream.state} onRetry={() => void send(lastPrompt)} />
@ -449,48 +643,30 @@ export function CopilotSidebar({ open, onClose }: { open: boolean; onClose: () =
) : null}
{error ? (
<p role="alert" className="copilot-error">
{error}。可刷新或重新打开侧栏恢复会话。
{error}
</p>
) : null}
</div>
<form
className="copilot-composer"
onSubmit={event => {
event.preventDefault();
void send();
}}
>
<label htmlFor="copilot-question" className="sr-only">
询问项目 Copilot
</label>
<textarea
id="copilot-question"
ref={composer}
rows={1}
maxLength={8000}
value={input}
onChange={event => setInput(event.target.value)}
onKeyDown={event => {
if (event.key === 'Enter' && !event.shiftKey && !event.nativeEvent.isComposing) {
event.preventDefault();
void send();
}
}}
placeholder="问用法、报问题或说需求…(Enter 发送,Shift+Enter 换行)"
/>
<div>
<small>{streaming ? '正在回答,可随时停止' : '回答只依据本项目的资料'}</small>
{streaming ? (
<Button type="button" variant="secondary" onClick={() => stream.stop()}>
<Square size={13} /> 停止
</Button>
) : (
<Button disabled={!projectId || !input.trim()}>
<Send size={15} /> 发送
</Button>
)}
</div>
</form>
<CopilotComposer
value={input}
onChange={setInput}
onSend={() => void send()}
onStop={() => stream.stop()}
streaming={streaming}
disabled={!projectId}
pictures={pictures}
onPicturesChange={setPictures}
onPickFiles={pickFiles}
hint={`可贴图或拖入图片(PNG/JPEG/WebP,最多 ${MAX_PICTURES} 张)`}
/>
</aside>
);
}
/** The panel the console keeps docked on the right. */
export function CopilotSidebar({ open, onClose }: { open: boolean; onClose: () => void }) {
return <CopilotPanel variant="drawer" open={open} onClose={onClose} />;
}
export const copilotPictureTypes = PICTURE_TYPES;
export type { PendingPicture };