Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

feat(workbench): chat text-selection quote/ask toolbar

cyh511a87c290c2
diff --git a/ui/src/components/workbench/ChatPage.tsx b/ui/src/components/workbench/ChatPage.tsx
index 71f442454..61b17436e 100644
--- a/ui/src/components/workbench/ChatPage.tsx
+++ b/ui/src/components/workbench/ChatPage.tsx
@@ -15,8 +15,10 @@ import { isProxyMediaUrl } from '../../lib/mediaProxy';
import { localPath, type ShowPageLinkInfo } from '../../lib/showPageLinks';
import { formatLocalDateTime } from '../../lib/relativeTime';
import { useFileDrop } from '../../lib/useFileDrop';
+import { quoteText } from '../../lib/quoteText';
import { AgentRoutePicker } from './AgentRoutePicker';
import { ShowPageShareControl } from './ShowPageShareControl';
+import { SelectionQuoteToolbar } from './SelectionQuoteToolbar';
import { InstallHint } from '../InstallHint';
import { Button } from '../ui/button';
import { ChatImage } from '../ui/chat-image';
@@ -146,6 +148,28 @@ export const ChatPage: React.FC = () => {
composerRef.current?.insertSessionReference(refSessionId, title),
[],
);
+
+ // Chat-selection toolbar actions. "Quote" appends the quoted selection to the
+ // current composer; "Ask in a new session" forks this session and seeds the
+ // fork's draft with the same quote, then navigates to it.
+ const quoteSelectionToComposer = useCallback(
+ (text: string) => composerRef.current?.appendText(quoteText(text)),
+ [],
+ );
+ const askInNewSession = useCallback(
+ async (text: string) => {
+ if (!sessionId) return;
+ try {
+ const forked = await api.forkSession(sessionId);
+ if (!forked?.id) return;
+ await api.setSessionDraft(forked.id, quoteText(text));
+ navigate(`/chat/${encodeURIComponent(forked.id)}`);
+ } catch {
+ // forkSession / setSessionDraft surface their own errors via the toast layer.
+ }
+ },
+ [sessionId, api, navigate],
+ );
// Null target hides that sidebar action unless the composer is actually
// mounted + insertable: a chat is open (sessionId), its session has loaded
// (before that ChatPage shows a loader — the composer isn't rendered yet), and
@@ -1384,6 +1408,8 @@ export const ChatPage: React.FC = () => {
highlightedId={highlightedId}
messageFontSize={messageFontSize}
onQuickReply={handleQuickReply}
+ onQuoteSelection={quoteSelectionToComposer}
+ onAskInNewSession={askInNewSession}
/>
<QueueStrip queue={queue} onRemove={removeQueued} onSendNow={sendQueueNow} />
{/* key by session so the composer remounts per session — its draft-seeding
@@ -1723,6 +1749,10 @@ interface TranscriptProps {
highlightedId: string | null;
messageFontSize: number;
onQuickReply: (messageId: string, choice: string) => boolean | void | Promise<boolean | void>;
+ // Chat-selection toolbar: quote the selection into the composer, or fork +
+ // ask in a new session seeded with the quote.
+ onQuoteSelection: (text: string) => void;
+ onAskInNewSession: (text: string) => void;
}
const Transcript: React.FC<TranscriptProps> = ({
@@ -1741,6 +1771,8 @@ const Transcript: React.FC<TranscriptProps> = ({
highlightedId,
messageFontSize,
onQuickReply,
+ onQuoteSelection,
+ onAskInNewSession,
}) => {
const { t } = useTranslation();
const forkSourceSessionId =
@@ -2030,7 +2062,10 @@ const Transcript: React.FC<TranscriptProps> = ({
}
return (
<div className="relative flex min-h-0 flex-1 flex-col">
- <div ref={scrollRef} onScroll={handleScroll} className="min-h-0 flex-1 overflow-y-auto px-4 py-5 [overflow-anchor:none] md:px-8">
+ <SelectionQuoteToolbar containerRef={scrollRef} onQuote={onQuoteSelection} onAskInNew={onAskInNewSession} />
+ {/* [-webkit-touch-callout:none] suppresses the iOS selection callout so our
+ toolbar is the selection UI on mobile (we re-offer Copy there). */}
+ <div ref={scrollRef} onScroll={handleScroll} className="min-h-0 flex-1 overflow-y-auto px-4 py-5 [overflow-anchor:none] [-webkit-touch-callout:none] md:px-8">
<div ref={contentRef} className="mx-auto flex w-full max-w-[1080px] flex-col gap-3">
{forkSourceBanner}
{loadingOlder && (
diff --git a/ui/src/components/workbench/Composer.tsx b/ui/src/components/workbench/Composer.tsx
index 4f4dc7842..cc153bebe 100644
--- a/ui/src/components/workbench/Composer.tsx
+++ b/ui/src/components/workbench/Composer.tsx
@@ -130,6 +130,10 @@ export interface ComposerHandle {
* session" from the sidebar). No-op when the mention editor isn't active
* (the plain-textarea home composer). */
insertSessionReference: (sessionId: string, title?: string | null) => void;
+ /** Append text to the end of the composer (e.g. a quoted chat selection),
+ * with a separating space when the composer is non-empty. No-op when the
+ * mention editor isn't active (the plain-textarea home composer). */
+ appendText: (text: string) => void;
}
// The chat-style input row: an auto-growing textarea + a Send/Stop icon button,
@@ -343,6 +347,7 @@ export const Composer = forwardRef<ComposerHandle, ComposerProps>(function Compo
// carries the stable sessionId → serializes to `#<id>` + a session ref.
mentionRef.current?.insertMention('#', title?.trim() || refSessionId, { sessionId: refSessionId });
},
+ appendText: (text: string) => mentionRef.current?.append(text),
}));
const startRecording = async () => {
diff --git a/ui/src/components/workbench/SelectionQuoteToolbar.tsx b/ui/src/components/workbench/SelectionQuoteToolbar.tsx
new file mode 100644
index 000000000..526bfcbe2
--- /dev/null
+++ b/ui/src/components/workbench/SelectionQuoteToolbar.tsx
@@ -0,0 +1,129 @@
+import React, { useEffect, useState } from 'react';
+import { createPortal } from 'react-dom';
+import { useTranslation } from 'react-i18next';
+import { Check, Copy, GitFork, TextQuote } from 'lucide-react';
+
+import { copyTextToClipboard } from '../../lib/utils';
+
+type SelectionState = { text: string; top: number; bottom: number; left: number };
+
+const TOOLBAR_H = 38;
+const GAP = 8;
+
+// A floating toolbar that appears over a text selection inside the chat
+// transcript. "Quote" appends the (quoted) selection to the current composer;
+// "Ask in a new session" forks + prefills the fork's draft; "Copy" (mobile
+// only) replaces the native callout that the transcript suppresses there.
+export const SelectionQuoteToolbar: React.FC<{
+ containerRef: React.RefObject<HTMLDivElement | null>;
+ onQuote: (text: string) => void;
+ onAskInNew: (text: string) => void;
+}> = ({ containerRef, onQuote, onAskInNew }) => {
+ const { t } = useTranslation();
+ const [sel, setSel] = useState<SelectionState | null>(null);
+ const [copied, setCopied] = useState(false);
+
+ useEffect(() => {
+ const container = containerRef.current;
+ if (!container) return;
+ let timer = 0;
+ const recompute = () => {
+ const selection = window.getSelection();
+ if (!selection || selection.isCollapsed || selection.rangeCount === 0) {
+ setSel(null);
+ return;
+ }
+ const text = selection.toString().trim();
+ const range = selection.getRangeAt(0);
+ if (!text || !container.contains(range.commonAncestorContainer)) {
+ setSel(null);
+ return;
+ }
+ const rect = range.getBoundingClientRect();
+ if (!rect.width && !rect.height) {
+ setSel(null);
+ return;
+ }
+ setSel({ text, top: rect.top, bottom: rect.bottom, left: rect.left + rect.width / 2 });
+ };
+ // Debounce so the toolbar appears when the selection settles, not on every
+ // intermediate range while dragging the selection / handles.
+ const onSelectionChange = () => {
+ window.clearTimeout(timer);
+ timer = window.setTimeout(recompute, 150);
+ };
+ // A scrolled transcript makes the cached rect stale — hide immediately.
+ const onScroll = () => {
+ window.clearTimeout(timer);
+ setSel(null);
+ };
+ document.addEventListener('selectionchange', onSelectionChange);
+ container.addEventListener('scroll', onScroll, { passive: true });
+ return () => {
+ window.clearTimeout(timer);
+ document.removeEventListener('selectionchange', onSelectionChange);
+ container.removeEventListener('scroll', onScroll);
+ };
+ }, [containerRef]);
+
+ if (!sel) return null;
+
+ const dismiss = () => {
+ window.getSelection()?.removeAllRanges();
+ setSel(null);
+ setCopied(false);
+ };
+ const runQuote = () => {
+ onQuote(sel.text);
+ dismiss();
+ };
+ const runAskInNew = () => {
+ onAskInNew(sel.text);
+ dismiss();
+ };
+ const runCopy = () => {
+ const text = sel.text;
+ void copyTextToClipboard(text).then((ok) => {
+ if (ok) {
+ setCopied(true);
+ window.setTimeout(dismiss, 800);
+ }
+ });
+ };
+
+ const above = sel.top > TOOLBAR_H + GAP + 8;
+ const top = above ? sel.top - TOOLBAR_H - GAP : sel.bottom + GAP;
+ const left = Math.min(Math.max(sel.left, 96), window.innerWidth - 96);
+
+ const itemClass =
+ 'flex items-center gap-1.5 px-3 py-2 text-[13px] font-medium text-foreground transition-colors hover:bg-foreground/[0.06]';
+
+ return createPortal(
+ <div
+ role="toolbar"
+ // Keep the selection alive when the toolbar is pressed (desktop). On touch
+ // the handlers use the captured `sel.text`, so a collapse is harmless too.
+ onMouseDown={(e) => e.preventDefault()}
+ style={{ position: 'fixed', top, left, transform: 'translateX(-50%)', zIndex: 60 }}
+ className="flex items-center overflow-hidden rounded-lg border border-border-strong bg-surface-2 shadow-[0_12px_30px_-8px_rgba(0,0,0,0.7)]"
+ >
+ <button type="button" className={itemClass} onClick={runQuote}>
+ <TextQuote className="size-3.5 text-muted" />
+ {t('chat.selection.quote')}
+ </button>
+ <span className="h-5 w-px bg-border" />
+ <button type="button" className={itemClass} onClick={runAskInNew}>
+ <GitFork className="size-3.5 text-muted" />
+ {t('chat.selection.askInNew')}
+ </button>
+ {/* Copy is mobile-only: the transcript suppresses the native iOS callout
+ there, so we re-offer copy ourselves. Desktop keeps Cmd/Ctrl+C. */}
+ <span className="h-5 w-px bg-border md:hidden" />
+ <button type="button" className={`${itemClass} md:hidden`} onClick={runCopy}>
+ {copied ? <Check className="size-3.5 text-mint" /> : <Copy className="size-3.5 text-muted" />}
+ {t('chat.selection.copy')}
+ </button>
+ </div>,
+ document.body,
+ );
+};
diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.json
index 808f36b3c..374361bbb 100644
--- a/ui/src/i18n/en.json
+++ b/ui/src/i18n/en.json
@@ -1777,6 +1777,11 @@
"chat": {
"back": "Back",
"untitled": "Untitled session",
+ "selection": {
+ "quote": "Quote",
+ "askInNew": "Ask in a new session",
+ "copy": "Copy"
+ },
"showPage": {
"open": "Visualize",
"backToChat": "Back to chat",
diff --git a/ui/src/i18n/zh.json b/ui/src/i18n/zh.json
index 269a33c92..10e59efca 100644
--- a/ui/src/i18n/zh.json
+++ b/ui/src/i18n/zh.json
@@ -1776,6 +1776,11 @@
"chat": {
"back": "返回",
"untitled": "未命名会话",
+ "selection": {
+ "quote": "引用",
+ "askInNew": "在新会话询问",
+ "copy": "复制"
+ },
"showPage": {
"open": "可视化",
"backToChat": "回聊天",
diff --git a/ui/src/lib/quoteText.ts b/ui/src/lib/quoteText.ts
new file mode 100644
index 000000000..e2af4a7a4
--- /dev/null
+++ b/ui/src/lib/quoteText.ts
@@ -0,0 +1,11 @@
+// Wrap a chat selection in quotes for the composer / a forked draft. Text that
+// contains any CJK ideograph (pure Chinese OR mixed Chinese+Latin) uses the
+// Chinese corner brackets 「」; pure Latin/ASCII text uses straight double
+// quotes. Mixed text counts as Chinese — any CJK character flips to 「」.
+// Ranges: CJK Ext A (3400–4DBF), CJK Unified (4E00–9FFF), CJK Compat (F900–FAFF).
+const CJK = /[㐀-䶿一-鿿豈-﫿]/;
+
+export function quoteText(text: string): string {
+ const trimmed = text.trim();
+ return CJK.test(trimmed) ? `「${trimmed}」` : `"${trimmed}"`;
+}