diff --git a/apps/mobile/assets/context-chip-icons/quote.png b/apps/mobile/assets/context-chip-icons/quote.png new file mode 100644 index 0000000000000..0e180813c3ca5 Binary files /dev/null and b/apps/mobile/assets/context-chip-icons/quote.png differ diff --git a/apps/mobile/scripts/sync-context-chip-icons.mjs b/apps/mobile/scripts/sync-context-chip-icons.mjs index 7b86766708763..6322b4debf082 100644 --- a/apps/mobile/scripts/sync-context-chip-icons.mjs +++ b/apps/mobile/scripts/sync-context-chip-icons.mjs @@ -44,6 +44,7 @@ const lucideIcons = [ "message-circle", "messages-square", "mouse-pointer-click", + "quote", "server", "terminal", ]; diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index b2e947c5ab0e9..48bb68c857262 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -61,6 +61,7 @@ import { } from "@t3tools/contracts"; import type { ToolCallVisibility } from "@t3tools/contracts/settings"; import { renderAssistantCitationsAsText } from "@t3tools/shared/assistantCitations"; +import { citeAssistantText } from "./citeAssistantText"; import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard"; import { hostedThreadLink, parseThreadLinkHref } from "@t3tools/shared/threadLinks"; import { PROMPT_LINK_MAX_PROMPT_LENGTH } from "@t3tools/shared/promptLinks"; @@ -1135,6 +1136,9 @@ function ArtifactTemplateCard(props: { const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: { readonly markdown: string; readonly environmentId: EnvironmentId; + /** Set for assistant replies, which offer Cite on selected text. */ + readonly citeThreadId?: ThreadId | undefined; + readonly citeMessageId?: MessageId | undefined; readonly markdownStyles: MarkdownStyleSet; readonly linkHandlers: MarkdownLinkHandlers; readonly onUseArtifactTemplate?: ((template: CodexArtifactTemplate) => void) | undefined; @@ -1149,6 +1153,15 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: { linkCustomization: (href) => withChipIcon(props.linkHandlers.linkCustomization?.(href), threadIcons.get(href)), }; + const { environmentId, citeThreadId: threadId, citeMessageId: messageId } = props; + const onCite = useMemo( + () => + threadId && messageId + ? (selection: { readonly text: string; readonly start: number }) => + citeAssistantText({ environmentId, threadId, messageId }, selection) + : undefined, + [environmentId, threadId, messageId], + ); return ( ); }); @@ -1172,6 +1186,9 @@ function AssistantMarkdownBlocks(props: { readonly renderImage: MarkdownImageRenderer; readonly skills?: ReadonlyArray | undefined; readonly models: ReturnType; + readonly onCite: + | ((selection: { readonly text: string; readonly start: number }) => void) + | undefined; }) { const blocks = useMemo( () => @@ -1215,6 +1232,7 @@ function AssistantMarkdownBlocks(props: { textStyle={props.markdownStyles.nativeTextStyle} {...props.linkHandlers} renderImage={props.renderImage} + onCite={props.onCite} /> ); }); @@ -2236,6 +2254,8 @@ function renderFeedEntry( ({ + alert: vi.fn(), + prompt: (request: TextInputDialogRequest) => prompts.push(request), +})); + +vi.mock("expo-file-system", () => ({ + Directory: class { + create() {} + }, + File: class { + exists = false; + create() {} + write() {} + moveSync() {} + }, + Paths: { document: { uri: "file:///documents" } }, +})); + +describe("citeAssistantText", () => { + it("cites into the thread draft when the queued edit ends while the prompt is open", () => { + prompts.length = 0; + const environmentId = EnvironmentId.make("env-cite"); + const threadId = ThreadId.make("thread-cite"); + const threadKey = `${environmentId}:${threadId}`; + const runId = RunId.make("run-cite"); + const runs = Atom.make | null>([ + { id: runId, status: "queued" }, + ]); + beginQueuedRunEdit( + threadKey, + { runId, messageId: MessageId.make("queued"), originalText: "", existingAttachments: [] }, + runs, + ); + + citeAssistantText( + { environmentId, threadId, messageId: MessageId.make("reply") }, + { text: "complete", start: 0 }, + ); + appAtomRegistry.set(runs, [{ id: runId, status: "running" }]); + prompts[0]!.onConfirm("Why?"); + + expect(getComposerDraftSnapshot(queuedEditDraftKey(threadKey, runId)).text).toBe(""); + expect( + collectAssistantCitations(getComposerDraftSnapshot(threadKey).text).map(({ citation }) => [ + citation.text, + citation.comment, + ]), + ).toEqual([["complete", "Why?"]]); + }); +}); diff --git a/apps/mobile/src/features/threads/citeAssistantText.ts b/apps/mobile/src/features/threads/citeAssistantText.ts new file mode 100644 index 0000000000000..418e6cc101ed4 --- /dev/null +++ b/apps/mobile/src/features/threads/citeAssistantText.ts @@ -0,0 +1,76 @@ +import { + ASSISTANT_CITATION_MAX_COMMENT_LENGTH, + ASSISTANT_CITATION_MAX_TEXT_LENGTH, + type EnvironmentId, + type MessageId, + type ThreadId, +} from "@t3tools/contracts"; +import { + serializeAssistantCitation, + withAssistantCitationComment, +} from "@t3tools/shared/assistantCitations"; + +import * as Dialog from "../../components/ui/Dialog"; +import { scopedThreadKey } from "../../lib/scopedEntities"; +import { + captureComposerDraftInsertion, + insertComposerDraftText, +} from "../../state/use-composer-drafts"; +import { getQueuedRunEdit, queuedEditDraftKey } from "../../state/queued-run-edit"; + +export function citeAssistantText( + source: { + readonly environmentId: EnvironmentId; + readonly threadId: ThreadId; + readonly messageId: MessageId; + }, + selection: { readonly text: string; readonly start: number }, +): void { + const { text } = selection; + if (!text.trim()) return; + if (text.length > ASSISTANT_CITATION_MAX_TEXT_LENGTH) { + Dialog.alert("Selection is too long to cite", "Select a shorter passage and try again."); + return; + } + const start = Math.max(0, selection.start); + // Native offsets are per rendered block, not per message, and carry no surrounding context; + // web resolves the quote by its text when the offsets do not line up. + const citation = { + version: 1 as const, + ...source, + text, + start, + end: start + text.replace(/\s+/g, " ").length, + prefix: "", + suffix: "", + }; + const askForComment = (initialValue: string) => + Dialog.prompt({ + title: "Cite", + message: "Add a comment about this quote (optional).", + placeholder: "Comment", + initialValue, + confirmText: "Cite", + onConfirm: (comment) => { + if (comment.trim().length > ASSISTANT_CITATION_MAX_COMMENT_LENGTH) { + Dialog.alert("Comment is too long", "Shorten the comment and try again.", [ + { text: "OK", onPress: () => askForComment(comment) }, + ]); + return; + } + // Resolved on confirm: a queued edit can end while the prompt is open. + const threadKey = scopedThreadKey(source.environmentId, source.threadId); + const edit = getQueuedRunEdit(threadKey); + const draftKey = edit === null ? threadKey : queuedEditDraftKey(threadKey, edit.runId); + const insertion = captureComposerDraftInsertion(draftKey); + const before = insertion.text.slice(0, insertion.start); + const lead = before.length > 0 && !/\s$/.test(before) ? " " : ""; + insertComposerDraftText( + draftKey, + `${lead}${serializeAssistantCitation(withAssistantCitationComment(citation, comment))} `, + insertion, + ); + }, + }); + askForComment(""); +} diff --git a/apps/mobile/src/lib/composerChipPresentation.ts b/apps/mobile/src/lib/composerChipPresentation.ts index 067e3aea2ecf7..7efc99fc04bce 100644 --- a/apps/mobile/src/lib/composerChipPresentation.ts +++ b/apps/mobile/src/lib/composerChipPresentation.ts @@ -39,6 +39,7 @@ const CONTEXT_CHIP_PRESENTATIONS = { icon: "git-pull-request-arrow", }, skill: { accent: "#b261be", icon: "skill" }, + citation: { icon: "quote" }, thread: { accent: "#009c96", icon: "messages-square", diff --git a/apps/mobile/src/lib/composerContext.test.ts b/apps/mobile/src/lib/composerContext.test.ts index 2e1ee64831d08..7a2c1c6ca91af 100644 --- a/apps/mobile/src/lib/composerContext.test.ts +++ b/apps/mobile/src/lib/composerContext.test.ts @@ -1,8 +1,12 @@ import { ComposerContextId, + EnvironmentId, + MessageId, + ThreadId, type OrchestrationMessageContext, type ServerProviderSkill, } from "@t3tools/contracts"; +import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations"; import { collectComposerInlineTokens } from "@t3tools/shared/composerInlineTokens"; import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT, @@ -156,6 +160,29 @@ describe("mobile composer context", () => { expect(restore("deleted")?.records ?? []).toEqual([]); expect(createComposerContextHistory()(source)?.records).toEqual([]); }); + it("chips a cited assistant quote as one token, hiding mentions inside its link", () => { + const quote = serializeAssistantCitation({ + version: 1, + environmentId: EnvironmentId.make("env"), + threadId: ThreadId.make("thread"), + messageId: MessageId.make("message"), + text: "Run $deploy with model:codex/gpt-6.1 ", + start: 0, + end: 37, + prefix: "", + suffix: "", + comment: "Why this one?", + }); + const text = `See ${quote} please`; + expect( + composerContextEditorTokens( + text, + collectComposerInlineTokens(text), + new Set(["deploy"]), + collectComposerModelTokens(text, new Set(["model:codex/gpt-6.1"])), + ).map((token) => [token.type, token.value, token.start, token.end]), + ).toEqual([["citation", "Why this one?", 4, 4 + quote.length]]); + }); it("chips a model reference only once it is complete and names a known model", () => { const known = new Set(["model:codex/gpt-6.1"]); const modelTokens = (text: string) => diff --git a/apps/mobile/src/lib/composerContext.ts b/apps/mobile/src/lib/composerContext.ts index 53494209362af..30c37ab74ba87 100644 --- a/apps/mobile/src/lib/composerContext.ts +++ b/apps/mobile/src/lib/composerContext.ts @@ -4,6 +4,10 @@ import { } from "@t3tools/client-runtime/composer-model-mentions"; import { isProviderSkillUserInvocable } from "@t3tools/client-runtime/providerSkills"; import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; +import { + assistantCitationLabel, + collectAssistantCitations, +} from "@t3tools/shared/assistantCitations"; import { filePreviewKind } from "@t3tools/shared/filePreview"; import { videoMimeType } from "@t3tools/shared/video"; import { @@ -194,13 +198,14 @@ export function composerContextEditorTokens( modelTokens: ReadonlyArray = [], ) { const references = collectComposerContextReferences(text); + const citations = collectAssistantCitations(text); const inlineTokens = tokens.filter( (token) => // `$HOME` and other `$name` text that is not one of the provider's skills stays prose. (token.type !== "skill" || knownSkills.has(token.value)) && - !references.some((ref) => token.start < ref.end && token.end > ref.start), + ![...references, ...citations].some((ref) => token.start < ref.end && token.end > ref.start), ); - const taken = [...inlineTokens, ...references, ...composerMathRanges(text)]; + const taken = [...inlineTokens, ...references, ...citations, ...composerMathRanges(text)]; const models = modelTokens.filter( (mention) => !taken.some((range) => mention.start < range.end && mention.end > range.start), ); @@ -216,6 +221,13 @@ export function composerContextEditorTokens( value: mention.model, ...mention, })), + ...citations.map(({ citation, source, start, end }) => ({ + type: "citation" as const, + value: assistantCitationLabel(citation), + source, + start, + end, + })), ].sort((a, b) => a.start - b.start); } diff --git a/apps/mobile/src/lib/contextChipIcons.generated.ts b/apps/mobile/src/lib/contextChipIcons.generated.ts index 170fe0c776fc1..683e65adf1538 100644 --- a/apps/mobile/src/lib/contextChipIcons.generated.ts +++ b/apps/mobile/src/lib/contextChipIcons.generated.ts @@ -12,6 +12,7 @@ export const CONTEXT_CHIP_ICON_SOURCES = { "message-circle": require("../../assets/context-chip-icons/message-circle.png"), "messages-square": require("../../assets/context-chip-icons/messages-square.png"), "mouse-pointer-click": require("../../assets/context-chip-icons/mouse-pointer-click.png"), + quote: require("../../assets/context-chip-icons/quote.png"), server: require("../../assets/context-chip-icons/server.png"), skill: require("../../assets/context-chip-icons/skill.png"), terminal: require("../../assets/context-chip-icons/terminal.png"), diff --git a/apps/mobile/src/native/EnrichedMarkdownText.tsx b/apps/mobile/src/native/EnrichedMarkdownText.tsx index 86bf8021bdd0f..25622e701c251 100644 --- a/apps/mobile/src/native/EnrichedMarkdownText.tsx +++ b/apps/mobile/src/native/EnrichedMarkdownText.tsx @@ -218,7 +218,19 @@ function codeBlockElement( } export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) { - const { renderImage, resolveImageSource, fileEmbedLabel } = props; + const { renderImage, resolveImageSource, fileEmbedLabel, onCite } = props; + const citeMenuItems = useMemo( + () => + onCite && [ + { + text: "Cite", + icon: "quote.bubble", + onPress: ({ text, selection }: { text: string; selection: { start: number } }) => + onCite({ text, start: selection.start }), + }, + ], + [onCite], + ); const { themeAppearance, mathRenderingEnabled } = useAppearancePreferences(); const { markdown, fileHrefs } = useMemo(() => { if (!fileEmbedLabel) { @@ -439,6 +451,7 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) { onLinkLongPress={onLinkLongPress} linkContextMenuItems={linkContextMenuItems} linkPillContent={linkPillContent} + contextMenuItems={citeMenuItems} /> ); diff --git a/apps/mobile/src/native/SelectableMarkdownText.types.ts b/apps/mobile/src/native/SelectableMarkdownText.types.ts index 63f8429c386fd..1295be40df6fa 100644 --- a/apps/mobile/src/native/SelectableMarkdownText.types.ts +++ b/apps/mobile/src/native/SelectableMarkdownText.types.ts @@ -94,6 +94,8 @@ export interface SelectableMarkdownTextProps { readonly checked: boolean; readonly text: string; }) => void; + /** Adds Cite to the text selection menu. `start` is an offset into this block's rendered text. */ + readonly onCite?: (selection: { readonly text: string; readonly start: number }) => void; readonly marginTop?: number; readonly marginBottom?: number; readonly writingDirection?: "ltr" | "rtl"; diff --git a/apps/mobile/src/native/T3ComposerEditor.ios.tsx b/apps/mobile/src/native/T3ComposerEditor.ios.tsx index 437bcda4d012d..3c563958f9f91 100644 --- a/apps/mobile/src/native/T3ComposerEditor.ios.tsx +++ b/apps/mobile/src/native/T3ComposerEditor.ios.tsx @@ -214,9 +214,11 @@ export function ComposerEditor({ ? (skillLabels.get(token.value) ?? token.value) : model ? model.label - : token.type === "context" - ? `${token.label}${props.context?.records.some((record) => record.contextId === token.contextId) ? "" : " · unavailable"}` - : basename(token.value), + : token.type === "citation" + ? token.value + : token.type === "context" + ? `${token.label}${props.context?.records.some((record) => record.contextId === token.contextId) ? "" : " · unavailable"}` + : basename(token.value), detail: token.type === "context" ? composerChipSizeSuffix(record) : "", iconUri: model ? modelChipIconUri(model) : contextChipIconUri(icon), tintsIcon: model ? modelChipIcon(model).tintsIcon : icon.kind === "chip", diff --git a/apps/mobile/src/native/T3ComposerEditor.native.tsx b/apps/mobile/src/native/T3ComposerEditor.native.tsx index 5aabd8e0d6071..4ecf0cbd6225e 100644 --- a/apps/mobile/src/native/T3ComposerEditor.native.tsx +++ b/apps/mobile/src/native/T3ComposerEditor.native.tsx @@ -215,9 +215,11 @@ export function ComposerEditor({ ? (skillLabels.get(token.value) ?? token.value) : model ? model.label - : token.type === "context" - ? `${token.label}${props.context?.records.some((record) => record.contextId === token.contextId) ? "" : " · unavailable"}` - : basename(token.value), + : token.type === "citation" + ? token.value + : token.type === "context" + ? `${token.label}${props.context?.records.some((record) => record.contextId === token.contextId) ? "" : " · unavailable"}` + : basename(token.value), detail: token.type === "context" ? composerChipSizeSuffix(record) : "", iconUri: model ? modelChipIconUri(model) : contextChipIconUri(icon), tintsIcon: model ? modelChipIcon(model).tintsIcon : icon.kind === "chip",