diff --git a/apps/web/src/app/(console)/copilot/page.tsx b/apps/web/src/app/(console)/copilot/page.tsx new file mode 100644 index 0000000..c54ab11 --- /dev/null +++ b/apps/web/src/app/(console)/copilot/page.tsx @@ -0,0 +1,9 @@ +import { CopilotPanel } from '@/features/copilot'; + +export default function CopilotPage() { + return ( +
+ +
+ ); +} diff --git a/apps/web/src/app/globals.css b/apps/web/src/app/globals.css index de296d9..6f8d409 100644 --- a/apps/web/src/app/globals.css +++ b/apps/web/src/app/globals.css @@ -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; + } } diff --git a/apps/web/src/components/console-shell.tsx b/apps/web/src/components/console-shell.tsx index a01d347..cef47fd 100644 --- a/apps/web/src/components/console-shell.tsx +++ b/apps/web/src/components/console-shell.tsx @@ -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 ( -
+
跳转到主要内容 @@ -158,7 +160,7 @@ export function ConsoleShell({ children }: { children: ReactNode }) { {children}
- + {copilotPage ? null : }
); } diff --git a/apps/web/src/features/copilot-composer.tsx b/apps/web/src/features/copilot-composer.tsx new file mode 100644 index 0000000..5d91dae --- /dev/null +++ b/apps/web/src/features/copilot-composer.tsx @@ -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 ( +
  • + {/* eslint-disable-next-line @next/next/no-img-element */} + {picture.name} + {picture.status === 'uploading' ? 上传中 : null} + {picture.status === 'error' ? ( + + 失败 + + ) : null} + +
  • + ); +} + +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; + hint: string; +}) { + const area = useRef(null); + const picker = useRef(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 ( +
    { + 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 ?
    松开即可添加图片
    : null} + {pictures.length ? ( +
      + {pictures.map(picture => ( + onPicturesChange(pictures.filter(item => item.key !== picture.key))} + /> + ))} +
    + ) : null} +
    + + { + const files = [...(event.target.files ?? [])]; + event.target.value = ''; + void accept(files); + }} + /> + +