diff --git a/web/backend/api/session.go b/web/backend/api/session.go index 054b78b73..0143f5737 100644 --- a/web/backend/api/session.go +++ b/web/backend/api/session.go @@ -460,6 +460,9 @@ func visibleSessionMessages(messages []providers.Message, toolFeedbackMaxArgsLen for _, msg := range messages { switch msg.Role { + case "tool": + continue + case "user": if sessionMessageVisible(msg) { transcript = append(transcript, sessionChatMessage{ @@ -501,7 +504,18 @@ func visibleSessionMessages(messages []providers.Message, toolFeedbackMaxArgsLen } } - return transcript + return filterSessionChatMessages(transcript) +} + +func filterSessionChatMessages(messages []sessionChatMessage) []sessionChatMessage { + filtered := messages[:0] + for _, msg := range messages { + if msg.Role != "user" && msg.Role != "assistant" { + continue + } + filtered = append(filtered, msg) + } + return filtered } func assistantMessageTransientThought(msg providers.Message) bool { @@ -528,22 +542,16 @@ func visibleAssistantToolSummaryMessages( messages := make([]sessionChatMessage, 0, len(toolCalls)) for _, tc := range toolCalls { - name := tc.Name - argsJSON := "" - if tc.Function != nil { - if name == "" { - name = tc.Function.Name - } - argsJSON = tc.Function.Arguments - } - + name, argsJSON := toolCallNameAndArguments(tc) if strings.TrimSpace(name) == "" { continue } - - if strings.TrimSpace(argsJSON) == "" && len(tc.Arguments) > 0 { - if encodedArgs, err := json.Marshal(tc.Arguments); err == nil { - argsJSON = string(encodedArgs) + if name == "web_search" || name == "web_fetch" { + continue + } + if name == "message" { + if _, ok := parseMessageToolContent(argsJSON); ok { + continue } } @@ -568,36 +576,53 @@ func visibleAssistantToolMessages(toolCalls []providers.ToolCall) []sessionChatM messages := make([]sessionChatMessage, 0, len(toolCalls)) for _, tc := range toolCalls { - name := tc.Name - argsJSON := "" - if tc.Function != nil { - if name == "" { - name = tc.Function.Name - } - argsJSON = tc.Function.Arguments + name, argsJSON := toolCallNameAndArguments(tc) + if name != "message" { + continue } - - switch name { - case "message": - var args struct { - Content string `json:"content"` - } - if err := json.Unmarshal([]byte(argsJSON), &args); err != nil { - continue - } - if strings.TrimSpace(args.Content) == "" { - continue - } - messages = append(messages, sessionChatMessage{ - Role: "assistant", - Content: args.Content, - }) + content, ok := parseMessageToolContent(argsJSON) + if !ok { + continue } + messages = append(messages, sessionChatMessage{ + Role: "assistant", + Content: content, + }) } return messages } +func toolCallNameAndArguments(tc providers.ToolCall) (string, string) { + name := tc.Name + argsJSON := "" + if tc.Function != nil { + if name == "" { + name = tc.Function.Name + } + argsJSON = tc.Function.Arguments + } + if strings.TrimSpace(argsJSON) == "" && len(tc.Arguments) > 0 { + if encodedArgs, err := json.Marshal(tc.Arguments); err == nil { + argsJSON = string(encodedArgs) + } + } + return name, argsJSON +} + +func parseMessageToolContent(argsJSON string) (string, bool) { + var args struct { + Content string `json:"content"` + } + if err := json.Unmarshal([]byte(argsJSON), &args); err != nil { + return "", false + } + if strings.TrimSpace(args.Content) == "" { + return "", false + } + return args.Content, true +} + // sessionsDir resolves the path to the gateway's session storage directory. // It reads the workspace from config, falling back to ~/.picoclaw/workspace. func (h *Handler) sessionsDir() (string, error) { diff --git a/web/backend/api/session_test.go b/web/backend/api/session_test.go index e40a8c77c..f6c643bde 100644 --- a/web/backend/api/session_test.go +++ b/web/backend/api/session_test.go @@ -346,7 +346,7 @@ func TestHandleGetSession_OmitsTransientThoughtMessages(t *testing.T) { } } -func TestHandleGetSession_ReconstructsVisibleMessageToolOutput(t *testing.T) { +func TestHandleGetSession_ReconstructsVisibleMessageToolOutputWithoutDuplicateSummary(t *testing.T) { configPath, cleanup := setupOAuthTestEnv(t) defer cleanup() @@ -402,14 +402,19 @@ func TestHandleGetSession_ReconstructsVisibleMessageToolOutput(t *testing.T) { if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { t.Fatalf("Unmarshal() error = %v", err) } - if len(resp.Messages) != 3 { - t.Fatalf("len(resp.Messages) = %d, want 3", len(resp.Messages)) + if len(resp.Messages) != 2 { + t.Fatalf("len(resp.Messages) = %d, want 2", len(resp.Messages)) } - if !strings.Contains(resp.Messages[1].Content, "`message`") { - t.Fatalf("tool summary message = %#v, want message tool summary", resp.Messages[1]) + if resp.Messages[0].Role != "user" || resp.Messages[0].Content != "test" { + t.Fatalf("first message = %#v, want user/test", resp.Messages[0]) } - if resp.Messages[2].Role != "assistant" || resp.Messages[2].Content != "visible tool output" { - t.Fatalf("assistant message = %#v, want visible tool output", resp.Messages[2]) + if resp.Messages[1].Role != "assistant" || resp.Messages[1].Content != "visible tool output" { + t.Fatalf("assistant message = %#v, want visible tool output", resp.Messages[1]) + } + for _, msg := range resp.Messages { + if msg.Role == "tool" || strings.Contains(msg.Content, "`message`") { + t.Fatalf("unexpected raw tool or duplicate message-tool summary: %#v", msg) + } } } @@ -468,17 +473,17 @@ func TestHandleGetSession_PreservesFinalAssistantReplyAfterMessageToolOutput(t * if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { t.Fatalf("Unmarshal() error = %v", err) } - if len(resp.Messages) != 4 { - t.Fatalf("len(resp.Messages) = %d, want 4", len(resp.Messages)) + if len(resp.Messages) != 3 { + t.Fatalf("len(resp.Messages) = %d, want 3", len(resp.Messages)) } - if !strings.Contains(resp.Messages[1].Content, "`message`") { - t.Fatalf("tool summary message = %#v, want message tool summary", resp.Messages[1]) + if resp.Messages[0].Role != "user" || resp.Messages[0].Content != "test" { + t.Fatalf("first message = %#v, want user/test", resp.Messages[0]) } - if resp.Messages[2].Role != "assistant" || resp.Messages[2].Content != "visible tool output" { - t.Fatalf("interim assistant message = %#v, want visible tool output", resp.Messages[2]) + if resp.Messages[1].Role != "assistant" || resp.Messages[1].Content != "visible tool output" { + t.Fatalf("interim assistant message = %#v, want visible tool output", resp.Messages[1]) } - if resp.Messages[3].Role != "assistant" || resp.Messages[3].Content != "final assistant reply" { - t.Fatalf("final assistant message = %#v, want final assistant reply", resp.Messages[3]) + if resp.Messages[2].Role != "assistant" || resp.Messages[2].Content != "final assistant reply" { + t.Fatalf("final assistant message = %#v, want final assistant reply", resp.Messages[2]) } } @@ -535,8 +540,8 @@ func TestHandleListSessions_MessageCountUsesVisibleTranscript(t *testing.T) { if len(items) != 1 { t.Fatalf("len(items) = %d, want 1", len(items)) } - if items[0].MessageCount != 3 { - t.Fatalf("items[0].MessageCount = %d, want 3", items[0].MessageCount) + if items[0].MessageCount != 2 { + t.Fatalf("items[0].MessageCount = %d, want 2", items[0].MessageCount) } } @@ -567,6 +572,7 @@ func TestHandleGetSession_PreservesToolSummaryAndAssistantContent(t *testing.T) }, }, }, + {Role: "tool", Content: "raw read_file result", ToolCallID: "call_1"}, } { if err := store.AddFullMessage(nil, sessionKey, msg); err != nil { t.Fatalf("AddFullMessage() error = %v", err) @@ -606,6 +612,11 @@ func TestHandleGetSession_PreservesToolSummaryAndAssistantContent(t *testing.T) if resp.Messages[2].Role != "assistant" || resp.Messages[2].Content != "model final reply" { t.Fatalf("assistant message = %#v, want model final reply", resp.Messages[2]) } + for _, msg := range resp.Messages { + if msg.Role == "tool" || strings.Contains(msg.Content, "raw read_file result") { + t.Fatalf("unexpected raw tool result in history: %#v", msg) + } + } } func TestHandleGetSession_UsesConfiguredToolFeedbackMaxArgsLength(t *testing.T) { diff --git a/web/frontend/src/components/chat/assistant-message.tsx b/web/frontend/src/components/chat/assistant-message.tsx index 55b7b9bf6..5c2235982 100644 --- a/web/frontend/src/components/chat/assistant-message.tsx +++ b/web/frontend/src/components/chat/assistant-message.tsx @@ -1,4 +1,10 @@ -import { IconBrain, IconCheck, IconCopy } from "@tabler/icons-react" +import { + IconBrain, + IconCheck, + IconChevronDown, + IconCopy, +} from "@tabler/icons-react" +import { useAtom } from "jotai" import { useState } from "react" import { useTranslation } from "react-i18next" import ReactMarkdown from "react-markdown" @@ -10,6 +16,7 @@ import remarkGfm from "remark-gfm" import { Button } from "@/components/ui/button" import { formatMessageTime } from "@/hooks/use-pico-chat" import { cn } from "@/lib/utils" +import { showThoughtsAtom } from "@/store/chat" interface AssistantMessageProps { content: string @@ -24,6 +31,7 @@ export function AssistantMessage({ }: AssistantMessageProps) { const { t } = useTranslation() const [isCopied, setIsCopied] = useState(false) + const [isExpanded, setIsExpanded] = useAtom(showThoughtsAtom) const formattedTimestamp = timestamp !== "" ? formatMessageTime(timestamp) : "" @@ -36,64 +44,76 @@ export function AssistantMessage({ return (
-
-
- PicoClaw - {isThought && ( - - - {t("chat.reasoningLabel")} - - )} - {formattedTimestamp && ( - <> - - {formattedTimestamp} - - )} + {!isThought && ( +
+
+ PicoClaw + {formattedTimestamp && ( + <> + + {formattedTimestamp} + + )} +
-
+ )}
-
- setIsExpanded(!isExpanded)} > - {content} - -
- +
+ + {t("chat.reasoningLabel")} +
+ +
+ )} + {(!isThought || isExpanded) && ( +
+ + {content} + +
+ )} + {!isThought && ( + + )}
) diff --git a/web/frontend/src/components/chat/chat-composer.tsx b/web/frontend/src/components/chat/chat-composer.tsx index 58612d846..cb3016842 100644 --- a/web/frontend/src/components/chat/chat-composer.tsx +++ b/web/frontend/src/components/chat/chat-composer.tsx @@ -4,6 +4,11 @@ import { useTranslation } from "react-i18next" import TextareaAutosize from "react-textarea-autosize" import { Button } from "@/components/ui/button" +import { + Tooltip, + TooltipContent, + TooltipTrigger, +} from "@/components/ui/tooltip" import { cn } from "@/lib/utils" import type { ChatAttachment } from "@/store/chat" @@ -57,8 +62,8 @@ export function ChatComposer({ } return ( -
-
+
+
{attachments.length > 0 && (
{attachments.map((attachment, index) => ( @@ -93,17 +98,12 @@ export function ChatComposer({ disabled={!canInput} title={disabledMessage || undefined} className={cn( - "placeholder:text-muted-foreground/50 max-h-[200px] min-h-[60px] resize-none border-0 bg-transparent px-2 py-1 text-[15px] shadow-none transition-colors focus-visible:ring-0 focus-visible:outline-none dark:bg-transparent", + "placeholder:text-muted-foreground/50 max-h-[200px] min-h-[64px] resize-none border-0 bg-transparent px-2 py-1 text-[15px] shadow-none transition-colors focus-visible:ring-0 focus-visible:outline-none dark:bg-transparent", !canInput && "cursor-not-allowed", )} minRows={1} maxRows={8} /> - {!canInput && disabledMessage && ( -
- {disabledMessage} -
- )}
@@ -122,15 +122,28 @@ export function ChatComposer({
{canInput ? ( - + + + + + + + + {t("chat.sendHint")} + + ) : null}
diff --git a/web/frontend/src/components/chat/chat-page.tsx b/web/frontend/src/components/chat/chat-page.tsx index 4129d812a..c117be0b7 100644 --- a/web/frontend/src/components/chat/chat-page.tsx +++ b/web/frontend/src/components/chat/chat-page.tsx @@ -153,7 +153,7 @@ export function ChatPage() { }) const syncScrollState = (element: HTMLDivElement) => { - const { scrollTop, scrollHeight, clientHeight } = element + const { clientHeight, scrollHeight, scrollTop } = element setHasScrolled(scrollTop > 0) setIsAtBottom(scrollHeight - scrollTop <= clientHeight + 10) } @@ -294,7 +294,7 @@ export function ChatPage() {
{messages.length === 0 && !isTyping && ( diff --git a/web/frontend/src/components/chat/typing-indicator.tsx b/web/frontend/src/components/chat/typing-indicator.tsx index 98580963d..df138553c 100644 --- a/web/frontend/src/components/chat/typing-indicator.tsx +++ b/web/frontend/src/components/chat/typing-indicator.tsx @@ -21,10 +21,7 @@ export function TypingIndicator() { return (
-
- PicoClaw -
-
+
diff --git a/web/frontend/src/components/chat/user-message.tsx b/web/frontend/src/components/chat/user-message.tsx index 96119a534..8bfdf24c9 100644 --- a/web/frontend/src/components/chat/user-message.tsx +++ b/web/frontend/src/components/chat/user-message.tsx @@ -1,3 +1,4 @@ +import { cn } from "@/lib/utils" import type { ChatAttachment } from "@/store/chat" interface UserMessageProps { @@ -7,6 +8,7 @@ interface UserMessageProps { export function UserMessage({ content, attachments = [] }: UserMessageProps) { const hasText = content.trim().length > 0 + const isCommand = content.trim().startsWith("/") const imageAttachments = attachments.filter( (attachment) => attachment.type === "image", ) @@ -27,8 +29,24 @@ export function UserMessage({ content, attachments = [] }: UserMessageProps) { )} {hasText && ( -
- {content} +
+ {isCommand ? ( +
+ + ❯ + + {content} +
+ ) : ( + content + )}
)}
diff --git a/web/frontend/src/components/ui/tooltip.tsx b/web/frontend/src/components/ui/tooltip.tsx index 757f05b03..6e71ad55f 100644 --- a/web/frontend/src/components/ui/tooltip.tsx +++ b/web/frontend/src/components/ui/tooltip.tsx @@ -30,10 +30,13 @@ function TooltipTrigger({ function TooltipContent({ className, + arrowClassName, sideOffset = 0, children, ...props -}: React.ComponentProps) { +}: React.ComponentProps & { + arrowClassName?: string +}) { return ( {children} - + ) diff --git a/web/frontend/src/i18n/locales/en.json b/web/frontend/src/i18n/locales/en.json index c96d4b71b..2c51cc6d7 100644 --- a/web/frontend/src/i18n/locales/en.json +++ b/web/frontend/src/i18n/locales/en.json @@ -38,7 +38,7 @@ "chat": { "welcome": "How can I help you today?", "welcomeDesc": "Ask me about weather, settings, or any other tasks. I'm here to assist you.", - "placeholder": "Start a new message...\nPress Enter to send, Shift + Enter for a new line", + "placeholder": "Start a new message...", "disabledPlaceholder": { "gatewayUnknown": "Unable to chat: Gateway status is still being checked. Please wait, then refresh the page or restart Launcher if needed.", "gatewayStarting": "Unable to chat: Gateway is starting. Wait for startup to complete, then try again.", @@ -60,6 +60,7 @@ "step4": "Almost there..." }, "reasoningLabel": "Reasoning", + "toolLabel": "Tool", "history": "History", "noHistory": "No chat history yet", "historyLoadFailed": "Failed to load chat history", @@ -72,6 +73,8 @@ "notConnected": "Gateway is not running. Start it to chat.", "noModel": "No default model configured. Go to Models page to set one." }, + "sendMessage": "Send message", + "sendHint": "Press Enter to send\nShift + Enter for a new line", "attachImage": "Add images", "removeImage": "Remove image", "uploadedImage": "Uploaded image", diff --git a/web/frontend/src/i18n/locales/zh.json b/web/frontend/src/i18n/locales/zh.json index 4a9e59cf4..11827fc8a 100644 --- a/web/frontend/src/i18n/locales/zh.json +++ b/web/frontend/src/i18n/locales/zh.json @@ -38,7 +38,7 @@ "chat": { "welcome": "今天我能为您做些什么?", "welcomeDesc": "您可以询问我天气、设置或其他任何任务,我随时为您效劳。", - "placeholder": "输入新消息...\n按 Enter 发送,Shift + Enter 换行", + "placeholder": "输入新消息...", "disabledPlaceholder": { "gatewayUnknown": "无法对话:网关状态仍在检测中。请稍候重试,如仍无效请刷新页面或重启 Launcher。", "gatewayStarting": "无法对话:网关正在启动。请等待启动完成后重试。", @@ -60,6 +60,7 @@ "step4": "马上就好..." }, "reasoningLabel": "思考", + "toolLabel": "工具", "history": "历史记录", "noHistory": "暂无对话历史", "historyLoadFailed": "加载历史记录失败", @@ -72,6 +73,8 @@ "notConnected": "服务未运行,请先启动以进行对话。", "noModel": "未设置默认模型,请前往模型页面进行配置。" }, + "sendMessage": "发送消息", + "sendHint": "按 Enter 发送\nShift + Enter 换行", "attachImage": "添加图片", "removeImage": "移除图片", "uploadedImage": "已上传图片", diff --git a/web/frontend/src/store/chat.ts b/web/frontend/src/store/chat.ts index 2c6f70610..c3b44f348 100644 --- a/web/frontend/src/store/chat.ts +++ b/web/frontend/src/store/chat.ts @@ -48,6 +48,8 @@ const DEFAULT_CHAT_STATE: ChatStoreState = { export const chatAtom = atom(DEFAULT_CHAT_STATE) +export const showThoughtsAtom = atom(true) + const store = getDefaultStore() export function getChatState() {