feat: 支持 acp-link 包进行 acp 通用的 remote-control (#292)

* fix: 修复超时问题

* feat: 添加 acp-link 代码

* refactor: 样式重构完成

* feat: RCS 添加 ACP 后端支持

- 新增 ACP WebSocket handler (agent 注册、EventBus 订阅)
- 新增 relay handler (前端 WS → acp-link 透传 + EventBus inbound 转发)
- 新增 SSE event stream 供外部消费者订阅 channel group 事件
- ACP REST 接口无鉴权 (agents、channel-groups)
- WebSocket 端点保留 token 鉴权
- SPA 路由 /acp/ 指向 acp.html

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: 添加 ACP 专属前端界面

- 新增 /acp/ SPA 页面 (agent 列表 + 实时交互)
- Agent 列表按 channel group 分组,显示在线状态
- 通过 RCS WebSocket relay 与 agent 通信
- Vite multi-page 构建 (index.html + acp.html)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: acp-link 支持 RCS relay 双向通信

- rcs-upstream 新增 messageHandler 转发非控制消息
- server.ts 新增虚拟 WS + relay client state 处理 relay ACP 消息
- newSession/loadSession 补充 mcpServers 参数
- 连接成功后显示 ACP Dashboard URL

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: 移除 FileExplorer 及文件操作相关代码

- 删除 FileExplorer 组件
- ACPMain 移除 Files tab,仅保留 Chat 和 History
- client.ts 移除 listDir/readFile/onFileChanges 等方法
- types.ts 移除 FileItem/FileContent/FileChange 等类型

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: 修复类型问题

* feat: RCS 后端统一 ACP/Bridge 注册逻辑

- store: EnvironmentRecord 增加 capabilities 字段、storeFindEnvironmentByMachineName 复用逻辑
- store: 新增 storeGetSessionOwners,支持未绑定 session 自动 claim
- environment: registerEnvironment 支持 ACP 复用已有记录,返回 session_id
- session: resolveOwnedWebSessionId 支持无 owner session 自动绑定
- acp-ws-handler: 新增 handleIdentify 支持 REST+WS 两步注册
- acp routes: /acp/relay 和 /acp/agents 支持 UUID 认证
- event-bus: 增加 error 类型 payload 日志

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: acp-link 改 REST 注册 + WS identify 两步流程

- rcs-upstream: 新增 registerViaRest() 通过 POST /v1/environments/bridge 注册
- rcs-upstream: WS 连接后发送 identify 替代 register,携带 agentId
- rcs-upstream: 入口链接改为 /code/?sid=${sessionId} 实现用户绑定
- server: 修复心跳跳过 relay 虚拟连接的 bug
- server: maxSessions 配置传入 RCS upstream

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: 前端统一 Chat 组件 + ACP 聊天界面重构

- 新增 chat/ 组件: ChatView, ChatInput, MessageBubble, ToolCallGroup, PermissionPanel, SessionSidebar, CommandMenu
- ACPMain: 重构支持完整 ACP 协议交互(session/prompt/permission)
- rcs-chat-adapter: 统一 bridge session SSE 适配器
- ACPClient: 增强 session 管理、permission 流程、streaming 支持
- index.css: 新增 chat 相关样式、动画、布局
- useCommands: 新增快捷命令 hook

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: 删除 /acp/ 独立页面,ACP 聊天统一到 /code/:sessionId

- 删除 acp.html、acp-main.tsx 入口文件和 pages/acp/ 目录
- SessionDetail: ACP session 在同一页面渲染 ACPSessionDetail 组件
- App.tsx: ?sid= 参数自动调用 apiBind 绑定用户 UUID
- Dashboard: 统一 session 列表导航,ACP 显示紫色标签
- relay-client: 改用 UUID 认证替代 API token
- EnvironmentList: 显示 workerType 标签(ACP Agent / Claude Code)
- index.ts: 移除 /acp/ SPA 路由,vite.config 移除 acp 入口

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* build: 更新构建及测试修复

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-04-18 17:59:29 +08:00
committed by GitHub
parent 29cc74a170
commit 34154ee3f5
142 changed files with 17847 additions and 5577 deletions

View File

@@ -0,0 +1,334 @@
import { useState, useRef, useCallback, type KeyboardEvent, type ClipboardEvent } from "react";
import { cn } from "../../src/lib/utils";
import { Send, Square, Paperclip, Slash } from "lucide-react";
import type { ChatInputMessage, UserMessageImage } from "../../src/lib/types";
import type { AvailableCommand } from "../../src/acp/types";
import { CommandMenu } from "./CommandMenu";
import imageCompression from "browser-image-compression";
// 图片压缩配置
const IMAGE_COMPRESSION_OPTIONS = {
maxSizeMB: 2,
maxWidthOrHeight: 2048,
useWebWorker: true,
fileType: "image/jpeg" as const,
};
// =============================================================================
// Anthropic 风格聊天输入框 — 底部居中浮动卡片,橙色焦点环
// =============================================================================
interface ChatInputProps {
onSubmit: (message: ChatInputMessage) => void;
isLoading?: boolean;
onInterrupt?: () => void;
disabled?: boolean;
placeholder?: string;
/** 是否支持图片上传 */
supportsImages?: boolean;
/** Agent 提供的可用 slash 命令 */
commands?: AvailableCommand[];
className?: string;
}
export function ChatInput({
onSubmit,
isLoading = false,
onInterrupt,
disabled = false,
placeholder = "给 Claude 发送消息…",
supportsImages = false,
commands,
className,
}: ChatInputProps) {
const [text, setText] = useState("");
const [images, setImages] = useState<UserMessageImage[]>([]);
const [showCommandMenu, setShowCommandMenu] = useState(false);
const [commandFilter, setCommandFilter] = useState("");
const textareaRef = useRef<HTMLTextAreaElement>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
const handleSubmit = useCallback(() => {
const trimmed = text.trim();
if ((!trimmed && images.length === 0) || disabled) return;
onSubmit({ text: trimmed, images: images.length > 0 ? images : undefined });
setText("");
setImages([]);
setShowCommandMenu(false);
setCommandFilter("");
// 重置 textarea 高度
if (textareaRef.current) {
textareaRef.current.style.height = "auto";
}
}, [text, images, disabled, onSubmit]);
const handleKeyDown = useCallback(
(e: KeyboardEvent<HTMLTextAreaElement>) => {
if (showCommandMenu) {
if (e.key === "Escape") {
e.preventDefault();
setShowCommandMenu(false);
return;
}
// Let cmdk handle arrow keys and Enter for selection
// Tab also closes the menu
if (e.key === "Tab") {
e.preventDefault();
setShowCommandMenu(false);
return;
}
}
if (e.key === "Enter" && !e.shiftKey && !e.nativeEvent.isComposing) {
e.preventDefault();
if (isLoading) {
onInterrupt?.();
} else {
handleSubmit();
}
}
},
[handleSubmit, isLoading, onInterrupt, showCommandMenu],
);
const handleInput = useCallback((e: React.ChangeEvent<HTMLTextAreaElement>) => {
const value = e.target.value;
setText(value);
// 检测 slash 命令模式:仅在输入开头输入 / 时触发
if (value.startsWith("/") && commands && commands.length > 0) {
setShowCommandMenu(true);
setCommandFilter(value.slice(1).split(/\s/)[0] || "");
} else if (showCommandMenu) {
setShowCommandMenu(false);
setCommandFilter("");
}
// 自动调整高度
const el = e.target;
el.style.height = "auto";
el.style.height = Math.min(el.scrollHeight, 200) + "px";
}, [commands, showCommandMenu]);
// 粘贴图片
const handlePaste = useCallback(async (e: ClipboardEvent) => {
if (!supportsImages) return;
const files = Array.from(e.clipboardData.files).filter((f) => f.type.startsWith("image/"));
if (files.length === 0) return;
e.preventDefault();
const newImages = await processImageFiles(files);
setImages((prev) => [...prev, ...newImages]);
}, [supportsImages]);
// 选择文件
const handleFileSelect = useCallback(async () => {
if (!fileInputRef.current) return;
const files = fileInputRef.current.files;
if (!files || files.length === 0) return;
const newImages = await processImageFiles(Array.from(files));
setImages((prev) => [...prev, ...newImages]);
// 清空 input 以便重复选择
fileInputRef.current.value = "";
}, []);
const removeImage = useCallback((index: number) => {
setImages((prev) => prev.filter((_, i) => i !== index));
}, []);
const handleCommandSelect = useCallback((command: AvailableCommand) => {
setText(`/${command.name} `);
setShowCommandMenu(false);
setCommandFilter("");
textareaRef.current?.focus();
}, []);
const toggleCommandMenu = useCallback(() => {
if (showCommandMenu) {
setShowCommandMenu(false);
setCommandFilter("");
} else {
if (!text.startsWith("/")) {
setText("/" + text);
}
setShowCommandMenu(true);
setCommandFilter(text.startsWith("/") ? text.slice(1).split(/\s/)[0] || "" : "");
textareaRef.current?.focus();
}
}, [showCommandMenu, text]);
const canSend = (text.trim() || images.length > 0) && !disabled;
return (
<div className={cn("w-full max-w-3xl mx-auto px-3 sm:px-4 pb-4 pt-2", className)}>
<div className="relative">
{/* Slash command menu — floating above input */}
{showCommandMenu && commands && commands.length > 0 && (
<CommandMenu
commands={commands}
filter={commandFilter}
onSelect={handleCommandSelect}
onClose={() => {
setShowCommandMenu(false);
setCommandFilter("");
}}
className="absolute bottom-full left-0 right-0 mb-1 z-50"
/>
)}
<div className={cn(
"rounded-xl border border-border bg-surface-2 overflow-hidden",
"focus-within:border-brand/50 focus-within:shadow-[0_0_0_3px_rgba(217,119,87,0.15)] transition-all",
)}>
{/* 图片预览 */}
{images.length > 0 && (
<div className="flex flex-wrap gap-2 px-3 pt-3">
{images.map((img, i) => (
<div key={i} className="relative group">
<img
src={`data:${img.mimeType};base64,${img.data}`}
alt="附件"
className="h-14 w-14 object-cover rounded-lg border border-border"
/>
<button
type="button"
onClick={() => removeImage(i)}
className="absolute -top-1.5 -right-1.5 h-5 w-5 rounded-full bg-surface-2 border border-border flex items-center justify-center text-text-muted hover:text-text-primary text-xs opacity-0 group-hover:opacity-100 transition-opacity"
>
{"\u00D7"}
</button>
</div>
))}
</div>
)}
{/* 输入区域 — Anthropic 单行紧凑布局 */}
<div className="flex items-end gap-2 px-3 py-2.5">
{/* 左侧附件按钮 */}
{supportsImages && (
<>
<button
type="button"
onClick={() => fileInputRef.current?.click()}
className="flex-shrink-0 h-8 w-8 flex items-center justify-center rounded-lg text-text-muted hover:text-text-secondary hover:bg-surface-1/50 transition-colors"
disabled={disabled}
>
<Paperclip className="h-4 w-4" />
</button>
<input
ref={fileInputRef}
type="file"
accept="image/*"
multiple
className="hidden"
onChange={handleFileSelect}
/>
</>
)}
{/* Slash 命令按钮 */}
{commands && commands.length > 0 && (
<button
type="button"
onClick={toggleCommandMenu}
className={cn(
"flex-shrink-0 h-8 w-8 flex items-center justify-center rounded-lg transition-colors",
showCommandMenu
? "bg-brand/15 text-brand"
: "text-text-muted hover:text-text-secondary hover:bg-surface-1/50",
)}
disabled={disabled}
title="命令列表"
>
<Slash className="h-4 w-4" />
</button>
)}
{/* Textarea — Poppins font */}
<textarea
ref={textareaRef}
value={text}
onChange={handleInput}
onKeyDown={handleKeyDown}
onPaste={handlePaste}
placeholder={placeholder}
disabled={disabled}
rows={1}
className={cn(
"flex-1 resize-none border-none bg-transparent outline-none",
"text-sm text-text-primary placeholder:text-text-muted font-display",
"max-h-[200px] min-h-[24px] leading-normal",
)}
/>
{/* 右侧发送/取消按钮 */}
<button
type="button"
onClick={isLoading ? onInterrupt : handleSubmit}
disabled={!isLoading && !canSend}
className={cn(
"flex-shrink-0 h-8 w-8 flex items-center justify-center rounded-lg transition-all",
isLoading
? "bg-text-primary text-surface-2 hover:bg-text-secondary"
: canSend
? "bg-brand text-white hover:bg-brand-light hover:scale-[1.05] active:scale-[0.97]"
: "bg-surface-1 text-text-muted",
)}
>
{isLoading ? (
<Square className="h-3.5 w-3.5" fill="currentColor" />
) : (
<Send className="h-4 w-4" />
)}
</button>
</div>
</div>
</div>{/* end relative */}
{/* 提示文本 */}
<div className="text-center mt-1.5">
<span className="text-[11px] text-text-muted font-display">
Enter Shift+Enter
</span>
</div>
</div>
);
}
// =============================================================================
// 图片处理工具
// =============================================================================
async function processImageFiles(files: File[]): Promise<UserMessageImage[]> {
const results: UserMessageImage[] = [];
for (const file of files) {
try {
let blob: Blob = file;
let mimeType = file.type;
if (file.size > 2 * 1024 * 1024) {
const compressed = await imageCompression(file, IMAGE_COMPRESSION_OPTIONS);
blob = compressed;
mimeType = "image/jpeg";
}
const base64 = await new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
const result = reader.result as string;
const commaIdx = result.indexOf(",");
resolve(commaIdx >= 0 ? result.slice(commaIdx + 1) : result);
};
reader.onerror = () => reject(new Error("FileReader error"));
reader.readAsDataURL(blob);
});
results.push({ mimeType, data: base64 });
} catch (err) {
console.error("Failed to process image:", err);
}
}
return results;
}

View File

@@ -0,0 +1,166 @@
import type { ThreadEntry, ToolCallEntry } from "../../src/lib/types";
import { cn } from "../../src/lib/utils";
import { UserBubble, AssistantBubble } from "./MessageBubble";
import { ToolCallGroup } from "./ToolCallGroup";
import { Conversation, ConversationContent, ConversationEmptyState, ConversationScrollButtons } from "../ai-elements/conversation";
// =============================================================================
// 统一聊天视图 — Anthropic 编辑式排版
// 无气泡间距,用垂直 rhythm 区分消息块
// =============================================================================
interface ChatViewProps {
entries: ThreadEntry[];
isLoading?: boolean;
onPermissionRespond?: (requestId: string, optionId: string | null, optionKind: string | null) => void;
emptyTitle?: string;
emptyDescription?: string;
}
export function ChatView({
entries,
isLoading = false,
onPermissionRespond,
emptyTitle = "开始对话",
emptyDescription = "输入消息开始聊天",
}: ChatViewProps) {
// 将相邻的 ToolCallEntry 合并为一组
const grouped = groupToolCalls(entries);
const hasMessages = entries.length > 0;
// 检查是否正在加载(最后一个条目是用户消息)
const showThinking = isLoading && entries.length > 0 && entries[entries.length - 1]?.type === "user_message";
return (
<Conversation className="flex-1">
<ConversationContent>
{!hasMessages ? (
<ConversationEmptyState
title={emptyTitle}
description={emptyDescription}
/>
) : (
<>
{grouped.map((item, i) => {
if (item.type === "single") {
return (
<div key={`entry-${i}`} className={cn(entrySpacing(entries, i))}>
<EntryRenderer entry={item.entry} isLoading={isLoading} onPermissionRespond={onPermissionRespond} />
</div>
);
}
// 工具调用组 — 紧贴在助手消息下方
return (
<div key={`group-${i}`} className="-mt-2">
<ToolCallGroup entries={item.entries} onPermissionRespond={onPermissionRespond} />
</div>
);
})}
{/* 思考指示器 — Anthropic 打字动画 */}
{showThinking && (
<div className="flex gap-3 items-start">
<div className="w-7 h-7 rounded-lg bg-brand/10 flex items-center justify-center flex-shrink-0">
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true">
<path d="M7 2L12 12H2L7 2Z" fill="var(--color-brand)" opacity=".85" />
</svg>
</div>
<div className="flex items-center gap-1 pt-1">
<span className="chat-typing-indicator" aria-hidden="true">
<span></span><span></span><span></span>
</span>
</div>
</div>
)}
</>
)}
<ConversationScrollButtons hasUserMessages={entries.some((e) => e.type === "user_message")} />
</ConversationContent>
</Conversation>
);
}
// =============================================================================
// 间距逻辑 — 用户消息前后间距大,工具调用紧贴
// =============================================================================
function entrySpacing(entries: ThreadEntry[], index: number): string {
const entry = entries[index];
// 用户消息前面多留白
if (entry?.type === "user_message") {
return "pt-6 pb-2";
}
// 助手消息后面多留白(除非紧跟工具调用)
if (entry?.type === "assistant_message") {
const next = entries[index + 1];
if (next?.type === "tool_call") {
return "pt-2 pb-1";
}
return "pt-2 pb-4";
}
return "py-1";
}
// =============================================================================
// 单条目渲染器
// =============================================================================
function EntryRenderer({
entry,
isLoading,
onPermissionRespond,
}: {
entry: ThreadEntry;
isLoading: boolean;
onPermissionRespond?: (requestId: string, optionId: string | null, optionKind: string | null) => void;
}) {
switch (entry.type) {
case "user_message":
return <UserBubble entry={entry} />;
case "assistant_message":
return <AssistantBubble entry={entry} isStreaming={isLoading} />;
case "tool_call":
return (
<ToolCallGroup
entries={[entry as ToolCallEntry]}
onPermissionRespond={onPermissionRespond}
/>
);
default:
return null;
}
}
// =============================================================================
// 工具调用分组逻辑
// =============================================================================
type GroupedItem =
| { type: "single"; entry: ThreadEntry }
| { type: "tool_group"; entries: ToolCallEntry[] };
function groupToolCalls(entries: ThreadEntry[]): GroupedItem[] {
const result: GroupedItem[] = [];
let currentToolGroup: ToolCallEntry[] = [];
const flushToolGroup = () => {
if (currentToolGroup.length === 1) {
result.push({ type: "single", entry: currentToolGroup[0] });
} else if (currentToolGroup.length > 1) {
result.push({ type: "tool_group", entries: currentToolGroup });
}
currentToolGroup = [];
};
for (const entry of entries) {
if (entry.type === "tool_call") {
currentToolGroup.push(entry);
} else {
flushToolGroup();
result.push({ type: "single", entry });
}
}
flushToolGroup();
return result;
}

View File

@@ -0,0 +1,113 @@
import { useMemo, useRef, useEffect } from "react";
import {
Command,
CommandList,
CommandEmpty,
CommandGroup,
CommandItem,
} from "../ui/command";
import { cn } from "../../src/lib/utils";
import type { AvailableCommand } from "../../src/acp/types";
// =============================================================================
// Slash command picker — floating above ChatInput
// =============================================================================
interface CommandMenuProps {
commands: AvailableCommand[];
/** Text after "/" used for filtering */
filter: string;
onSelect: (command: AvailableCommand) => void;
onClose: () => void;
className?: string;
}
/**
* Fuzzy match — checks if all query chars appear in order in the text.
* Same algorithm as ModelSelectorPicker.
*/
function fuzzyMatch(query: string, text: string): boolean {
if (!query) return true;
const lowerQuery = query.toLowerCase();
const lowerText = text.toLowerCase();
let queryIdx = 0;
for (let i = 0; i < lowerText.length && queryIdx < lowerQuery.length; i++) {
if (lowerText[i] === lowerQuery[queryIdx]) {
queryIdx++;
}
}
return queryIdx === lowerQuery.length;
}
export function CommandMenu({
commands,
filter,
onSelect,
onClose,
className,
}: CommandMenuProps) {
const containerRef = useRef<HTMLDivElement>(null);
// Close on outside click
useEffect(() => {
const handleClick = (e: MouseEvent) => {
if (containerRef.current && !containerRef.current.contains(e.target as Node)) {
onClose();
}
};
document.addEventListener("mousedown", handleClick);
return () => document.removeEventListener("mousedown", handleClick);
}, [onClose]);
// Filter commands by current input
const filtered = useMemo(() => {
if (!filter) return commands;
return commands.filter(
(cmd) => fuzzyMatch(filter, cmd.name) || fuzzyMatch(filter, cmd.description),
);
}, [commands, filter]);
return (
<div
ref={containerRef}
className={cn(
"rounded-xl border border-border bg-surface-2 shadow-lg",
className,
)}
>
<Command shouldFilter={false}>
<CommandList className="max-h-[320px]">
<CommandEmpty className="text-xs text-text-muted font-display py-3">
</CommandEmpty>
<CommandGroup>
{filtered.map((cmd) => (
<CommandItem
key={cmd.name}
value={cmd.name}
onSelect={() => onSelect(cmd)}
className={cn(
"flex items-center gap-2 px-3 py-2 cursor-pointer",
"rounded-lg mx-1",
"data-[selected=true]:bg-brand/8 data-[selected=true]:text-text-primary",
)}
>
<span className="text-sm font-display font-medium text-brand">
/{cmd.name}
</span>
<span className="text-xs text-text-muted truncate flex-1">
{cmd.description}
</span>
{cmd.input?.hint && (
<span className="text-[10px] text-text-muted italic">
{cmd.input.hint}
</span>
)}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</div>
);
}

View File

@@ -0,0 +1,113 @@
import type { UserMessageEntry, AssistantMessageEntry, UserMessageImage } from "../../src/lib/types";
import { cn, esc } from "../../src/lib/utils";
import { MessageResponse } from "../ai-elements/message";
import { Reasoning, ReasoningTrigger, ReasoningContent } from "../ai-elements/reasoning";
// =============================================================================
// 用户消息 — 右对齐,深色反转背景,无气泡边框
// Anthropic: right-aligned, inverted dark bg, rounded-xl with bottom-right notch
// =============================================================================
interface UserBubbleProps {
entry: UserMessageEntry;
}
export function UserBubble({ entry }: UserBubbleProps) {
return (
<div className="flex justify-end">
<div className="max-w-[85%] sm:max-w-[75%]">
{/* 图片附件 */}
{entry.images && entry.images.length > 0 && (
<div className="flex flex-wrap gap-2 mb-2 justify-end">
{entry.images.map((img, i) => (
<ImageThumbnail key={i} image={img} />
))}
</div>
)}
{/* 文本内容 */}
{entry.content && (
<div className="rounded-2xl rounded-br-md bg-bg-inverted px-4 py-2.5 text-sm text-text-inverted whitespace-pre-wrap font-display leading-relaxed">
{esc(entry.content)}
</div>
)}
</div>
</div>
);
}
// =============================================================================
// 助手消息 — 左对齐,无背景卡片,编辑式排版
// Anthropic: avatar + plain text, no bubble/card wrapper, serif body font
// =============================================================================
interface AssistantBubbleProps {
entry: AssistantMessageEntry;
isStreaming?: boolean;
}
export function AssistantBubble({ entry, isStreaming }: AssistantBubbleProps) {
return (
<div className="flex gap-3 items-start">
{/* Orange triangle avatar */}
<div className="w-7 h-7 rounded-lg bg-brand/10 flex items-center justify-center flex-shrink-0 mt-0.5">
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true">
<path d="M7 2L12 12H2L7 2Z" fill="var(--color-brand)" opacity=".85" />
</svg>
</div>
{/* 内容 — 无卡片背景,直接排版 */}
<div className="flex-1 min-w-0 space-y-3">
{/* Sender label */}
<span className="text-sm font-medium text-text-primary font-display">Claude</span>
{entry.chunks.map((chunk, i) => {
if (chunk.type === "thought") {
const isLastChunk = i === entry.chunks.length - 1;
const isThoughtStreaming = isStreaming && isLastChunk;
return (
<Reasoning key={i} isStreaming={isThoughtStreaming}>
<ReasoningTrigger />
<ReasoningContent>
<div className="text-sm text-text-secondary">
{chunk.text}
</div>
</ReasoningContent>
</Reasoning>
);
}
// 普通消息块 — 直接输出,无包裹卡片
return (
<div key={i} className="message-content text-text-primary leading-loose">
<MessageResponse>{chunk.text}</MessageResponse>
</div>
);
})}
</div>
</div>
);
}
// =============================================================================
// 图片缩略图 — 点击放大
// =============================================================================
function ImageThumbnail({ image }: { image: UserMessageImage }) {
const dataUrl = `data:${image.mimeType};base64,${image.data}`;
return (
<button
type="button"
className="rounded-lg overflow-hidden border border-border hover:border-brand/40 transition-colors cursor-pointer"
onClick={() => {
// 简单的点击放大 — 在新标签页打开图片
const w = window.open("");
if (w) {
w.document.write(`<img src="${dataUrl}" style="max-width:100%;max-height:100%" />`);
}
}}
>
<img
src={dataUrl}
alt="用户上传的图片"
className="h-20 w-20 object-cover"
/>
</button>
);
}

View File

@@ -0,0 +1,76 @@
import type { PendingPermission } from "../../src/lib/types";
import { cn } from "../../src/lib/utils";
import { ShieldAlert, Check, X } from "lucide-react";
// =============================================================================
// 权限请求面板 — 固定在输入框上方Anthropic warm token style
// =============================================================================
interface PermissionPanelProps {
requests: PendingPermission[];
onRespond?: (requestId: string, approved: boolean) => void;
className?: string;
}
export function PermissionPanel({ requests, onRespond, className }: PermissionPanelProps) {
if (requests.length === 0) return null;
return (
<div className={cn("w-full max-w-3xl mx-auto px-4", className)}>
<div className="space-y-2">
{requests.map((req) => (
<PermissionCard
key={req.requestId}
request={req}
onRespond={onRespond}
/>
))}
</div>
</div>
);
}
// =============================================================================
// 单个权限卡片 — warm warning tokens + left-border accent
// =============================================================================
interface PermissionCardProps {
request: PendingPermission;
onRespond?: (requestId: string, approved: boolean) => void;
}
function PermissionCard({ request, onRespond }: PermissionCardProps) {
return (
<div className="flex items-center gap-3 rounded-xl border border-warning-border/30 border-l-3 border-l-warning-border bg-warning-bg/50 px-4 py-3">
<ShieldAlert className="h-5 w-5 text-warning-text flex-shrink-0" />
<div className="flex-1 min-w-0">
<div className="text-sm font-medium text-warning-text">
{request.toolName}
</div>
{request.description && (
<div className="text-xs text-warning-text/80 mt-0.5 truncate">
{request.description}
</div>
)}
</div>
<div className="flex items-center gap-2 flex-shrink-0">
<button
type="button"
onClick={() => onRespond?.(request.requestId, true)}
className="h-8 px-3 rounded-lg bg-brand text-white text-xs font-medium hover:bg-brand-light transition-colors flex items-center gap-1.5"
>
<Check className="h-3.5 w-3.5" />
</button>
<button
type="button"
onClick={() => onRespond?.(request.requestId, false)}
className="h-8 px-3 rounded-lg border border-warning-border/30 text-warning-text text-xs font-medium hover:bg-warning-bg transition-colors flex items-center gap-1.5"
>
<X className="h-3.5 w-3.5" />
</button>
</div>
</div>
);
}

View File

@@ -0,0 +1,141 @@
import { cn } from "../../src/lib/utils";
import { Plus, MessageSquare, ChevronLeft, ChevronRight } from "lucide-react";
import { useState } from "react";
import type { SessionListItem } from "../../src/lib/types";
// =============================================================================
// 会话侧边栏 — Anthropic 分段式:今天/昨天/更早 + 橙色活跃态
// =============================================================================
interface SessionSidebarProps {
sessions: SessionListItem[];
activeId?: string | null;
onSelect?: (id: string) => void;
onNew?: () => void;
className?: string;
}
export function SessionSidebar({
sessions,
activeId,
onSelect,
onNew,
className,
}: SessionSidebarProps) {
const [collapsed, setCollapsed] = useState(false);
// 按日期分组
const groups = groupByRecency(sessions);
return (
<div
className={cn(
"hidden md:flex flex-col border-r border-border bg-surface-1 transition-all duration-200",
collapsed ? "w-12" : "w-64",
className,
)}
>
{/* 头部 */}
<div className="flex items-center justify-between px-3 py-3 border-b border-border">
{!collapsed && (
<span className="text-xs font-display font-medium text-text-muted uppercase tracking-wider"></span>
)}
<div className="flex items-center gap-1">
{!collapsed && onNew && (
<button
type="button"
onClick={onNew}
className="h-7 w-7 flex items-center justify-center rounded-lg text-text-muted hover:text-brand hover:bg-brand/10 transition-colors"
>
<Plus className="h-4 w-4" />
</button>
)}
<button
type="button"
onClick={() => setCollapsed(!collapsed)}
className="h-7 w-7 flex items-center justify-center rounded-lg text-text-muted hover:text-text-primary hover:bg-surface-2 transition-colors"
>
{collapsed ? (
<ChevronRight className="h-4 w-4" />
) : (
<ChevronLeft className="h-4 w-4" />
)}
</button>
</div>
</div>
{/* 会话列表 — 分段 */}
{!collapsed && (
<nav className="flex-1 overflow-y-auto py-2" aria-label="历史会话">
{groups.map((group) => (
<div key={group.label}>
<div className="px-3 py-1.5">
<span className="text-[10px] font-display font-medium uppercase tracking-widest text-text-muted">
{group.label}
</span>
</div>
{group.sessions.map((session) => (
<button
key={session.id}
type="button"
onClick={() => onSelect?.(session.id)}
className={cn(
"w-full flex items-center gap-2 px-3 py-2 text-left transition-colors border-l-2",
session.id === activeId
? "bg-brand/10 text-text-primary border-l-brand"
: "text-text-secondary hover:bg-surface-1/50 hover:text-text-primary border-l-transparent",
)}
title={session.title || session.id}
>
<MessageSquare className="h-3.5 w-3.5 shrink-0 text-text-muted" />
<span className="text-sm font-display truncate">
{session.title || session.id.slice(0, 8)}
</span>
</button>
))}
</div>
))}
{sessions.length === 0 && (
<div className="flex items-center justify-center py-8">
<span className="text-xs text-text-muted font-display"></span>
</div>
)}
</nav>
)}
</div>
);
}
// =============================================================================
// 按日期分组
// =============================================================================
interface SessionGroup {
label: string;
sessions: SessionListItem[];
}
function groupByRecency(sessions: SessionListItem[]): SessionGroup[] {
const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const yesterday = new Date(today.getTime() - 86400000);
const groups: SessionGroup[] = [
{ label: "今天", sessions: [] },
{ label: "昨天", sessions: [] },
{ label: "更早", sessions: [] },
];
for (const session of sessions) {
const date = session.updatedAt ? new Date(session.updatedAt) : new Date(0);
if (date >= today) {
groups[0].sessions.push(session);
} else if (date >= yesterday) {
groups[1].sessions.push(session);
} else {
groups[2].sessions.push(session);
}
}
return groups.filter((g) => g.sessions.length > 0);
}

View File

@@ -0,0 +1,206 @@
import { useState } from "react";
import type { ToolCallEntry, ToolCallData } from "../../src/lib/types";
import { cn } from "../../src/lib/utils";
import { ToolPermissionButtons } from "../ai-elements/permission-request";
// =============================================================================
// 工具调用折叠组 — Anthropic: subtle card, left-border accent, compact layout
// =============================================================================
interface ToolCallGroupProps {
entries: ToolCallEntry[];
onPermissionRespond?: (requestId: string, optionId: string | null, optionKind: string | null) => void;
}
export function ToolCallGroup({ entries, onPermissionRespond }: ToolCallGroupProps) {
const [expanded, setExpanded] = useState(false);
if (entries.length === 0) return null;
// 单个工具调用
if (entries.length === 1) {
return (
<div className="pl-10">
<SingleToolCard
tool={entries[0].toolCall}
onPermissionRespond={onPermissionRespond}
/>
</div>
);
}
// 多个工具调用 — 折叠组
const summary = buildSummary(entries);
return (
<div className="pl-10">
<div className="rounded-lg border border-border border-l-3 border-l-brand/50 bg-surface-2/50 overflow-hidden">
{/* 折叠头 */}
<button
type="button"
className="w-full flex items-center gap-2 px-3 py-2 text-sm text-text-secondary hover:bg-surface-1/50 transition-colors"
onClick={() => setExpanded(!expanded)}
>
<svg
width="12"
height="12"
viewBox="0 0 12 12"
fill="none"
className={cn("transition-transform text-text-muted", expanded && "rotate-90")}
>
<path d="M4 2L8 6L4 10" stroke="currentColor" strokeWidth="1.5" fill="none" />
</svg>
<span className="text-xs text-text-muted font-display">{summary}</span>
</button>
{/* 展开内容 */}
{expanded && (
<div className="border-t border-border divide-y divide-border">
{entries.map((entry, i) => (
<SingleToolCard
key={entry.toolCall.id || i}
tool={entry.toolCall}
compact
onPermissionRespond={onPermissionRespond}
/>
))}
</div>
)}
</div>
</div>
);
}
// =============================================================================
// 单个工具卡片 — compact, left-accent, inline status
// =============================================================================
interface SingleToolCardProps {
tool: ToolCallData;
compact?: boolean;
onPermissionRespond?: (requestId: string, optionId: string | null, optionKind: string | null) => void;
}
function SingleToolCard({ tool, compact, onPermissionRespond }: SingleToolCardProps) {
const [expanded, setExpanded] = useState(!compact);
const statusIcon = (() => {
switch (tool.status) {
case "running":
return <span className="text-status-running text-[10px]">&#9654;</span>;
case "complete":
return <span className="text-status-active text-[10px]">&#10003;</span>;
case "error":
return <span className="text-status-error text-[10px]">&#10005;</span>;
case "waiting_for_confirmation":
return <span className="text-brand text-[10px]">&#9083;</span>;
case "canceled":
return <span className="text-text-muted text-[10px]">&#8212;</span>;
case "rejected":
return <span className="text-status-error text-[10px]">&#10005;</span>;
default:
return null;
}
})();
const hasOutput = tool.status !== "running" && tool.status !== "waiting_for_confirmation" && (tool.rawOutput || tool.content);
return (
<div className={cn("px-3 py-2", compact && "py-1.5")}>
{/* 标题行 — 单行紧凑 */}
<div
className="flex items-center gap-1.5 cursor-pointer group"
onClick={() => setExpanded(!expanded)}
>
{statusIcon}
<span className="text-xs font-display font-medium text-text-secondary group-hover:text-text-primary transition-colors truncate">
{tool.title}
</span>
{tool.status === "running" && (
<span className="text-[10px] text-status-running animate-pulse">running</span>
)}
</div>
{/* 权限请求按钮 */}
{tool.status === "waiting_for_confirmation" && tool.permissionRequest && (
<div className="mt-1.5 ml-4">
<ToolPermissionButtons
requestId={tool.permissionRequest.requestId}
options={tool.permissionRequest.options}
onRespond={onPermissionRespond || (() => {})}
/>
</div>
)}
{/* 展开详情 */}
{expanded && (
<div className="mt-1.5 ml-4 space-y-1.5">
{tool.rawInput && Object.keys(tool.rawInput).length > 0 && (
<div>
<pre className="text-[11px] bg-surface-1 rounded-md p-2 overflow-x-auto font-mono max-h-36 text-text-secondary">
{truncate(JSON.stringify(tool.rawInput, null, 2), 2000)}
</pre>
</div>
)}
{hasOutput && (
<div>
<pre className={cn(
"text-[11px] rounded-md p-2 overflow-x-auto font-mono max-h-36",
tool.status === "error" ? "bg-status-error/10 text-status-error" : "bg-surface-1 text-text-secondary",
)}>
{formatOutput(tool)}
</pre>
</div>
)}
</div>
)}
</div>
);
}
// =============================================================================
// 工具函数
// =============================================================================
/** 构建统计摘要 */
function buildSummary(entries: ToolCallEntry[]): string {
const toolCounts = new Map<string, number>();
for (const entry of entries) {
const name = simplifyToolName(entry.toolCall.title);
toolCounts.set(name, (toolCounts.get(name) || 0) + 1);
}
const parts: string[] = [];
for (const [name, count] of toolCounts) {
parts.push(count === 1 ? name : `${count}${name}`);
}
if (parts.length === 0) return `${entries.length} 个工具调用`;
if (parts.length === 1) return parts[0];
return `${entries.length} 个工具: ${parts.join("、")}`;
}
/** 简化工具名称 */
function simplifyToolName(title: string): string {
const match = title.match(/^(\w+)/);
return match ? match[1] : title;
}
/** 格式化工具输出 */
function formatOutput(tool: ToolCallData): string {
if (tool.content && tool.content.length > 0) {
const texts = tool.content
.filter((c): c is Extract<typeof c, { type: "content" }> => c.type === "content")
.filter((c) => c.content.type === "text" && "text" in c.content)
.map((c) => (c.content as { text: string }).text);
if (texts.length > 0) return truncate(texts.join("\n"), 2000);
}
if (tool.rawOutput && Object.keys(tool.rawOutput).length > 0) {
return truncate(JSON.stringify(tool.rawOutput, null, 2), 2000);
}
return "";
}
function truncate(str: string, max: number): string {
return str.length > max ? str.slice(0, max) + "..." : str;
}

View File

@@ -0,0 +1,7 @@
export { ChatView } from "./ChatView";
export { UserBubble, AssistantBubble } from "./MessageBubble";
export { ToolCallGroup } from "./ToolCallGroup";
export { ChatInput } from "./ChatInput";
export { PermissionPanel } from "./PermissionPanel";
export { SessionSidebar } from "./SessionSidebar";
export { CommandMenu } from "./CommandMenu";