Skip to content
Closed
Binary file added apps/mobile/assets/context-chip-icons/quote.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions apps/mobile/scripts/sync-context-chip-icons.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const lucideIcons = [
"message-circle",
"messages-square",
"mouse-pointer-click",
"quote",
"server",
"terminal",
];
Expand Down
20 changes: 20 additions & 0 deletions apps/mobile/src/features/threads/ThreadFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand All @@ -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 (
<AssistantMarkdownBlocks
Expand All @@ -1159,6 +1172,7 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: {
renderImage={props.renderImage}
skills={props.skills}
models={models}
onCite={onCite}
/>
);
});
Expand All @@ -1172,6 +1186,9 @@ function AssistantMarkdownBlocks(props: {
readonly renderImage: MarkdownImageRenderer;
readonly skills?: ReadonlyArray<SelectableMarkdownSkill> | undefined;
readonly models: ReturnType<typeof useModelMentions>;
readonly onCite:
| ((selection: { readonly text: string; readonly start: number }) => void)
| undefined;
}) {
const blocks = useMemo(
() =>
Expand Down Expand Up @@ -1215,6 +1232,7 @@ function AssistantMarkdownBlocks(props: {
textStyle={props.markdownStyles.nativeTextStyle}
{...props.linkHandlers}
renderImage={props.renderImage}
onCite={props.onCite}
/>
);
});
Expand Down Expand Up @@ -2236,6 +2254,8 @@ function renderFeedEntry(
<AssistantMarkdownContent
markdown={renderedText}
environmentId={props.environmentId}
citeThreadId={message.role === "assistant" ? props.threadId : undefined}
citeMessageId={message.role === "assistant" ? message.id : undefined}
markdownStyles={styles}
linkHandlers={props.markdownLinkHandlers}
onUseArtifactTemplate={props.onUseArtifactTemplate}
Expand Down
62 changes: 62 additions & 0 deletions apps/mobile/src/features/threads/citeAssistantText.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { EnvironmentId, MessageId, RunId, ThreadId } from "@t3tools/contracts";
import { collectAssistantCitations } from "@t3tools/shared/assistantCitations";
import { Atom } from "effect/reactivity";
import { describe, expect, it, vi } from "vite-plus/test";

import type { TextInputDialogRequest } from "../../components/ui/Dialog";
import { appAtomRegistry } from "../../state/atom-registry";
import { beginQueuedRunEdit, queuedEditDraftKey } from "../../state/queued-run-edit";
import { getComposerDraftSnapshot } from "../../state/use-composer-drafts";
import { citeAssistantText } from "./citeAssistantText";

const prompts: TextInputDialogRequest[] = [];
vi.mock("../../components/ui/Dialog", () => ({
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<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | 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?"]]);
});
});
76 changes: 76 additions & 0 deletions apps/mobile/src/features/threads/citeAssistantText.ts
Original file line number Diff line number Diff line change
@@ -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("");
}
1 change: 1 addition & 0 deletions apps/mobile/src/lib/composerChipPresentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 27 additions & 0 deletions apps/mobile/src/lib/composerContext.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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) =>
Expand Down
16 changes: 14 additions & 2 deletions apps/mobile/src/lib/composerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -194,13 +198,14 @@ export function composerContextEditorTokens(
modelTokens: ReadonlyArray<ComposerModelMentionOccurrence> = [],
) {
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),
);
Expand All @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions apps/mobile/src/lib/contextChipIcons.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
15 changes: 14 additions & 1 deletion apps/mobile/src/native/EnrichedMarkdownText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -439,6 +451,7 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
onLinkLongPress={onLinkLongPress}
linkContextMenuItems={linkContextMenuItems}
linkPillContent={linkPillContent}
contextMenuItems={citeMenuItems}
/>
</View>
);
Expand Down
2 changes: 2 additions & 0 deletions apps/mobile/src/native/SelectableMarkdownText.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
8 changes: 5 additions & 3 deletions apps/mobile/src/native/T3ComposerEditor.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions apps/mobile/src/native/T3ComposerEditor.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading