Skip to content

Commit 3277655

Browse files
jakeleventhalclaude
andcommitted
feat(mobile): select, cite, and comment on assistant text
Long-press an assistant response, adjust the selection, and choose Cite to insert a quote into the composer. Tap a draft quote to add or edit its comment, copy it, jump to the source message, or remove it. Sent quotes open the same sheet read-only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent eb751fa commit 3277655

14 files changed

Lines changed: 339 additions & 27 deletions
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { ASSISTANT_CITATION_MAX_COMMENT_LENGTH, type AssistantCitation } from "@t3tools/contracts";
2+
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
3+
import { useState } from "react";
4+
import { Platform, ScrollView, TextInput, View } from "react-native";
5+
import { useSafeAreaInsets } from "react-native-safe-area-context";
6+
import { useOpenLinkedThread } from "../features/threads/useOpenLinkedThread";
7+
import { requestThreadMessageJump } from "../features/threads/threadMessageJump";
8+
import { copyTextWithHaptic } from "../lib/copyTextWithHaptic";
9+
import { AppText as Text } from "./ui/AppText";
10+
import { ListActionRow } from "./ui/ListActionRow";
11+
import { ListSection } from "./ui/ListSection";
12+
import { Sheet } from "./ui/Sheet";
13+
14+
const CONTEXT_CHARS = 80;
15+
16+
function contextBefore(prefix: string) {
17+
const flat = prefix.replace(/\s+/g, " ");
18+
if (flat.length <= CONTEXT_CHARS) return flat.trimStart();
19+
const cut = flat.slice(-CONTEXT_CHARS);
20+
return `…${cut.slice(cut.indexOf(" ") + 1)}`;
21+
}
22+
23+
function contextAfter(suffix: string) {
24+
const flat = suffix.replace(/\s+/g, " ");
25+
if (flat.length <= CONTEXT_CHARS) return flat.trimEnd();
26+
const cut = flat.slice(0, CONTEXT_CHARS);
27+
return `${cut.slice(0, cut.lastIndexOf(" "))}…`;
28+
}
29+
30+
export function AssistantCitationSheet(props: {
31+
readonly citation: AssistantCitation;
32+
readonly onClose: () => void;
33+
readonly onRemove?: () => void;
34+
readonly onCommentChange?: (comment: string) => void;
35+
}) {
36+
const openLinkedThread = useOpenLinkedThread();
37+
const insets = useSafeAreaInsets();
38+
const citation = props.citation;
39+
const [comment, setComment] = useState(citation.comment ?? "");
40+
const close = () => {
41+
if (props.onCommentChange && comment.trim() !== (citation.comment ?? "")) {
42+
props.onCommentChange(comment);
43+
}
44+
props.onClose();
45+
};
46+
return (
47+
<Sheet title="Quote" onClose={close}>
48+
<ScrollView
49+
contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
50+
contentContainerStyle={{
51+
paddingHorizontal: 20,
52+
paddingTop: 12,
53+
gap: 24,
54+
paddingBottom: Math.max(20, insets.bottom),
55+
}}
56+
>
57+
<View className="flex-row gap-3 rounded-2xl bg-grouped-card p-4">
58+
<View className="w-1 rounded-full bg-primary" />
59+
<Text selectable textRole="body" className="min-w-0 flex-1 text-foreground-muted">
60+
{contextBefore(citation.prefix)}
61+
<Text className="font-t3-medium text-foreground">{citation.text}</Text>
62+
{contextAfter(citation.suffix)}
63+
</Text>
64+
</View>
65+
{props.onCommentChange ? (
66+
<ListSection title="Comment">
67+
<TextInput
68+
accessibilityLabel="Comment on quote"
69+
placeholder="Add an optional comment…"
70+
placeholderTextColorClassName="accent-foreground-muted"
71+
multiline
72+
maxLength={ASSISTANT_CITATION_MAX_COMMENT_LENGTH}
73+
value={comment}
74+
onChangeText={setComment}
75+
className="min-h-24 px-4 py-3 text-base text-foreground"
76+
style={{ textAlignVertical: "top" }}
77+
/>
78+
</ListSection>
79+
) : citation.comment ? (
80+
<ListSection title="Comment">
81+
<Text selectable textRole="body" className="px-4 py-3 text-foreground">
82+
{citation.comment}
83+
</Text>
84+
</ListSection>
85+
) : null}
86+
<ListSection>
87+
<ListActionRow
88+
icon="arrow.up.right"
89+
label="Go to message"
90+
onPress={() => {
91+
close();
92+
requestThreadMessageJump(scopedThreadKey(citation), citation.messageId);
93+
openLinkedThread(citation);
94+
}}
95+
/>
96+
<ListActionRow
97+
icon="doc.on.doc"
98+
label="Copy quote"
99+
onPress={() => copyTextWithHaptic(citation.text)}
100+
/>
101+
{props.onRemove ? (
102+
<ListActionRow
103+
icon="trash"
104+
label="Remove from draft"
105+
tone="danger"
106+
onPress={props.onRemove}
107+
/>
108+
) : null}
109+
</ListSection>
110+
</ScrollView>
111+
</Sheet>
112+
);
113+
}

‎apps/mobile/src/components/ComposerEditor.tsx‎

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import { useEffect, useState } from "react";
77
import type { EnvironmentId } from "@t3tools/contracts";
88
import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";
99
import { collectComposerContextReferences } from "@t3tools/shared/composerContextReferences";
10+
import {
11+
collectAssistantCitations,
12+
serializeAssistantCitation,
13+
withAssistantCitationComment,
14+
} from "@t3tools/shared/assistantCitations";
15+
import { AssistantCitationSheet } from "./AssistantCitationSheet";
1016
import { ComposerEditor as NativeComposerEditor } from "../native/T3ComposerEditor";
1117
import type { ComposerEditorProps as NativeComposerEditorProps } from "../native/T3ComposerEditor";
1218
import {
@@ -164,13 +170,42 @@ export function ComposerEditor({
164170
const selectedReference = selected
165171
? collectComposerContextReferences(selected.source)[0]
166172
: undefined;
173+
const selectedCitation = selected
174+
? collectAssistantCitations(selected.source)[0]?.citation
175+
: undefined;
167176
const selectedSkillName = selected?.source.match(/^\p{Sc}(.+)$/u)?.[1];
168177
const selectedSkill = selectedSkillName
169178
? props.skills?.find((skill) => skill.name === selectedSkillName)
170179
: undefined;
171180
const record = draft.context?.records.find(
172181
(entry) => entry.contextId === selectedReference?.contextId,
173182
);
183+
const removeSelected =
184+
props.readOnly || props.editable === false
185+
? undefined
186+
: () => {
187+
if (!selected) return;
188+
if (props.value.slice(selected.start, selected.end) === selected.source) {
189+
changeText(props.value.slice(0, selected.start) + props.value.slice(selected.end));
190+
props.onSelectionChange?.({ start: selected.start, end: selected.start });
191+
}
192+
setSelected(null);
193+
};
194+
const commentSelectedCitation =
195+
props.readOnly || props.editable === false || !selected || !selectedCitation
196+
? undefined
197+
: (comment: string) => {
198+
if (props.value.slice(selected.start, selected.end) !== selected.source) return;
199+
const next = serializeAssistantCitation(
200+
withAssistantCitationComment(selectedCitation, comment),
201+
);
202+
const text =
203+
props.value.slice(0, selected.start) + next + props.value.slice(selected.end);
204+
const cursor = selected.start + next.length;
205+
if (draftKey)
206+
rememberComposerDraftSelection(draftKey, text, { start: cursor, end: cursor });
207+
changeText(text);
208+
};
174209
return (
175210
<>
176211
{/* Reset revision history with the recreated iOS view without aborting context imports. */}
@@ -214,6 +249,14 @@ export function ComposerEditor({
214249
{importing ? (
215250
<Text className="py-2 text-xs text-foreground-muted">Copying context…</Text>
216251
) : null}
252+
{selectedCitation ? (
253+
<AssistantCitationSheet
254+
citation={selectedCitation}
255+
onClose={() => setSelected(null)}
256+
onRemove={removeSelected}
257+
onCommentChange={commentSelectedCitation}
258+
/>
259+
) : null}
217260
{selected && (selectedReference || selectedSkill) ? (
218261
<ComposerContextSheet
219262
label={
@@ -252,19 +295,7 @@ export function ComposerEditor({
252295
records={draft.context?.records}
253296
attachments={draft.attachments}
254297
onClose={() => setSelected(null)}
255-
onRemove={
256-
props.readOnly || props.editable === false
257-
? undefined
258-
: () => {
259-
if (props.value.slice(selected.start, selected.end) === selected.source) {
260-
changeText(
261-
props.value.slice(0, selected.start) + props.value.slice(selected.end),
262-
);
263-
props.onSelectionChange?.({ start: selected.start, end: selected.start });
264-
}
265-
setSelected(null);
266-
}
267-
}
298+
onRemove={removeSelected}
268299
/>
269300
) : null}
270301
</>

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ import { useAppearancePreferences } from "../settings/appearance/AppearancePrefe
124124
import { collectProviderUsageLimits } from "@t3tools/shared/usageLimits";
125125
import type { ComposerEditorHandle } from "../../components/ComposerEditor";
126126
import type { StatusTone } from "../../components/StatusPill";
127-
import { getComposerDraftSnapshot } from "../../state/use-composer-drafts";
127+
import {
128+
captureComposerDraftInsertion,
129+
getComposerDraftSnapshot,
130+
insertComposerDraftText,
131+
} from "../../state/use-composer-drafts";
132+
import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
133+
import type { AssistantCitation } from "@t3tools/contracts";
128134
import { RenderErrorBoundary, RenderFailureView } from "../../components/RenderErrorBoundary";
129135
import {
130136
ANDROID_READING_MAX_WIDTH,
@@ -1196,6 +1202,24 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
11961202
composerEditorRef.current?.blur();
11971203
};
11981204

1205+
const canCiteAssistantText =
1206+
props.composerDraftKey !== null &&
1207+
!isProviderSubagent &&
1208+
!composerSlotHidden &&
1209+
!props.isSavingQueuedEdit &&
1210+
!props.draftMode?.interactionLocked;
1211+
const handleCiteAssistantText = (citation: AssistantCitation) => {
1212+
if (!canCiteAssistantText || props.composerDraftKey === null) return;
1213+
const target = captureComposerDraftInsertion(props.composerDraftKey);
1214+
const prefix = target.start > 0 && !/\s/.test(target.text[target.start - 1]!) ? " " : "";
1215+
insertComposerDraftText(
1216+
props.composerDraftKey,
1217+
`${prefix}${serializeAssistantCitation(citation)} `,
1218+
target,
1219+
);
1220+
composerEditorRef.current?.focus();
1221+
};
1222+
11991223
const handleUseArtifactTemplate = (template: CodexArtifactTemplate) => {
12001224
const draftMessage =
12011225
props.composerDraftKey === null ? "" : getComposerDraftSnapshot(props.composerDraftKey).text;
@@ -1332,6 +1356,7 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
13321356
onEndFollowEnabledChange={handleEndFollowEnabledChange}
13331357
skills={selectedProviderSkills}
13341358
onUseArtifactTemplate={handleUseArtifactTemplate}
1359+
onCiteAssistantText={canCiteAssistantText ? handleCiteAssistantText : undefined}
13351360
/>
13361361
</RenderErrorBoundary>
13371362
</View>

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Button } from "../../components/ui/Button";
22
import * as Dialog from "../../components/ui/Dialog";
3+
import { ASSISTANT_CITATION_MAX_TEXT_LENGTH, type AssistantCitation } from "@t3tools/contracts";
34
import { useOpenPullRequest } from "../../native/useOpenPullRequest";
45
import { useWorkspaceSettledContentWidth } from "../layout/workspace-content-width";
56
import { ThreadMarkdownVideo, ThreadMediaVisibleContext } from "./ThreadMarkdownVideo";
@@ -54,7 +55,6 @@ import {
5455
type ScheduledTaskId,
5556
type ScopedThreadRef,
5657
} from "@t3tools/contracts";
57-
import { renderAssistantCitationsAsText } from "@t3tools/shared/assistantCitations";
5858
import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";
5959
import { parseThreadLinkHref } from "@t3tools/shared/threadLinks";
6060
import { PROMPT_LINK_MAX_PROMPT_LENGTH } from "@t3tools/shared/promptLinks";
@@ -365,6 +365,7 @@ export interface ThreadFeedProps {
365365
readonly onEndFollowEnabledChange?: (enabled: boolean) => void;
366366
readonly skills?: ReadonlyArray<SelectableMarkdownSkill>;
367367
readonly onUseArtifactTemplate?: (template: CodexArtifactTemplate) => void;
368+
readonly onCiteAssistantText?: (citation: AssistantCitation) => void;
368369
}
369370

370371
async function waitForThreadShell(
@@ -900,6 +901,8 @@ function ArtifactTemplateCard(props: {
900901
const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: {
901902
readonly markdown: string;
902903
readonly environmentId: EnvironmentId;
904+
readonly citationSource?: Pick<AssistantCitation, "threadId" | "messageId">;
905+
readonly onCiteAssistantText?: (citation: AssistantCitation) => void;
903906
readonly markdownStyles: MarkdownStyleSet;
904907
readonly linkHandlers: MarkdownLinkHandlers;
905908
readonly onUseArtifactTemplate?: ((template: CodexArtifactTemplate) => void) | undefined;
@@ -934,6 +937,32 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: {
934937
markdown={markdown}
935938
skills={props.skills}
936939
textStyle={props.markdownStyles.nativeTextStyle}
940+
onCiteText={
941+
props.onCiteAssistantText && props.citationSource
942+
? (text) => {
943+
if (!text.trim()) return;
944+
if (text.length > ASSISTANT_CITATION_MAX_TEXT_LENGTH) {
945+
Dialog.alert(
946+
"Quote is too long",
947+
`Select up to ${ASSISTANT_CITATION_MAX_TEXT_LENGTH.toLocaleString()} characters to cite.`,
948+
);
949+
return;
950+
}
951+
// Native offsets are local to Markdown blocks; navigation resolves the quote in its message.
952+
if (!props.citationSource) return;
953+
props.onCiteAssistantText?.({
954+
version: 1,
955+
environmentId: props.environmentId,
956+
...props.citationSource,
957+
text,
958+
start: 0,
959+
end: text.replace(/\s+/g, " ").length,
960+
prefix: "",
961+
suffix: "",
962+
});
963+
}
964+
: undefined
965+
}
937966
{...linkHandlers}
938967
renderImage={props.renderImage}
939968
/>
@@ -1161,7 +1190,7 @@ function ThreadAutomationMessageRow(props: {
11611190
<View className="px-3.5 pb-2.5">
11621191
<MarkdownImageAvailableWidthContext value={props.contentMaxWidth}>
11631192
<UserMessageContent
1164-
text={renderAssistantCitationsAsText(props.text)}
1193+
text={props.text}
11651194
environmentId={props.environmentId}
11661195
context={props.context}
11671196
markdownStyles={props.markdownStyles}
@@ -1300,6 +1329,7 @@ function renderFeedEntry(
13001329
ThreadFeedProps,
13011330
| "environmentId"
13021331
| "onUseArtifactTemplate"
1332+
| "onCiteAssistantText"
13031333
| "skills"
13041334
| "dispatchingMessageId"
13051335
| "onEditPendingMessage"
@@ -1509,7 +1539,7 @@ function renderFeedEntry(
15091539
const { message } = entry;
15101540
const isUser = message.role === "user";
15111541
const presentation = resolveUserMessagePresentation(message);
1512-
const renderedText = renderAssistantCitationsAsText(presentation.text);
1542+
const renderedText = presentation.text;
15131543
const styles = isUser ? markdownStyles.user : markdownStyles.assistant;
15141544
const timestampLabel = formatMessageTime(isUser ? message.createdAt : message.updatedAt);
15151545
const attachments = message.attachments ?? [];
@@ -1820,6 +1850,8 @@ function renderFeedEntry(
18201850
<AssistantMarkdownContent
18211851
markdown={renderedText}
18221852
environmentId={props.environmentId}
1853+
citationSource={{ threadId: props.threadId, messageId: message.id }}
1854+
onCiteAssistantText={props.onCiteAssistantText}
18231855
markdownStyles={styles}
18241856
linkHandlers={props.markdownLinkHandlers}
18251857
onUseArtifactTemplate={props.onUseArtifactTemplate}
@@ -2694,6 +2726,7 @@ export const ThreadFeed = memo(function ThreadFeed({
26942726
// Include turn completion so unchanged message rows reveal their footer and spacing
26952727
// even when the final message update arrives before the turn settles.
26962728
const listAppearanceData = {
2729+
onCiteAssistantText: props.onCiteAssistantText,
26972730
worktreeSetup: props.worktreeSetup,
26982731
setupWorkingStartedAt: props.setupWorkingStartedAt,
26992732
dispatchingMessageId: props.dispatchingMessageId,
@@ -3143,6 +3176,7 @@ export const ThreadFeed = memo(function ThreadFeed({
31433176
dispatchingMessageId: props.dispatchingMessageId,
31443177
onEditPendingMessage: props.onEditPendingMessage,
31453178
onUseArtifactTemplate: props.onUseArtifactTemplate,
3179+
onCiteAssistantText: props.onCiteAssistantText,
31463180
threadId: props.threadId,
31473181
copiedRowId,
31483182
expandedWorkRows,

‎apps/mobile/src/lib/composerChipPresentation.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const CONTEXT_CHIP_PRESENTATIONS = {
3434
icon: "mouse-pointer-click",
3535
},
3636
"review-comment": { accent: "#8a70dd", icon: "message-circle" },
37+
citation: { accent: "#8a70dd", icon: "message-circle" },
3738
"pull-request": {
3839
accent: "#7079e4",
3940
icon: "git-pull-request-arrow",

0 commit comments

Comments
 (0)