Files
t3code/apps/mobile/src/features/threads/use-composer-command-menu.ts
T

671 lines
22 KiB
TypeScript

import type {
EnvironmentId,
ProjectId,
ProviderInteractionMode,
ServerProvider,
ThreadId,
} from "@t3tools/contracts";
import { matchComposerThreadItems } from "@t3tools/client-runtime/composerThreadItems";
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models";
const EMPTY_THREAD_SHELLS: ReadonlyArray<EnvironmentThreadShell> = [];
import {
COMPOSER_CONTEXT_MAX_RECORDS,
PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS,
} from "@t3tools/contracts";
import { Alert } from "react-native";
import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences";
import { pullRequestComposerContext, threadComposerContext } from "../../lib/composerContext";
import { uuidv4 } from "../../lib/uuid";
import {
getComposerDraftSnapshot,
readComposerDraftSelection,
setComposerDraftContext,
} from "../../state/use-composer-drafts";
import { USAGE_LIMITS_COMMAND } from "@t3tools/shared/usageLimits";
import {
detectComposerTrigger,
replaceTextRange,
serializeComposerFileLink,
type ComposerTrigger,
} from "@t3tools/shared/composerTrigger";
import {
insertRankedSearchResult,
normalizeSearchQuery,
scoreQueryMatch,
} from "@t3tools/shared/searchRanking";
import {
dedupeProviderSkillsByName,
getProviderSkillsForSlashMenu,
getProviderSlashCommandsForSlashMenu,
isProviderSkillUserInvocable,
hasCompleteProviderWorkspaceSnapshot,
hasCurrentProviderWorkspaceSnapshot,
resolveProviderSkillsForCwd,
resolveProviderSlashCommandsForCwd,
} from "@t3tools/client-runtime/providerSkills";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import type { ComposerEditorSelection } from "../../components/ComposerEditor";
import { serverEnvironment } from "../../state/server";
import { useAtomCommand } from "../../state/use-atom-command";
import { useComposerPathSearch, useComposerPullRequestSearch } from "../../state/queries";
import type { ComposerCommandItem } from "./ComposerCommandPopover";
import { matchesSlashSkillQuery } from "./composerSlashSkillSearch";
const WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS = 10_000;
function composerSelectionAtEnd(draftMessage: string): ComposerEditorSelection {
return { start: draftMessage.length, end: draftMessage.length };
}
export function buildComposerSlashCommandItems(input: {
readonly query: string;
readonly atMessageStart: boolean;
readonly hasThread: boolean;
readonly hasCompactableConversation?: boolean;
/** Whether T3 itself offers /usage-limits for the selected provider. */
readonly offersUsageLimits?: boolean;
readonly allowInteractionMode: boolean;
readonly selectedProviderStatus: Pick<
ServerProvider,
"driver" | "slashCommands" | "showInteractionModeToggle"
> | null;
}): ComposerCommandItem[] {
const query = input.query.toLowerCase();
const allowInteractionMode =
input.allowInteractionMode && input.selectedProviderStatus?.showInteractionModeToggle !== false;
const builtIn = [
{
id: "cmd:model",
type: "slash-command",
command: "model",
label: "/model",
description: "Switch model",
},
{
id: "cmd:plan",
type: "slash-command",
command: "plan",
label: "/plan",
description: "Switch to plan mode",
},
{
id: "cmd:default",
type: "slash-command",
command: "default",
label: "/default",
description: "Switch to default mode",
},
] satisfies ComposerCommandItem[];
const items: ComposerCommandItem[] = builtIn.filter(
(item) => item.command.includes(query) && (item.command === "model" || allowInteractionMode),
);
// Providers expand commands only at the start of a message. T3 commands
// change local state and do not have this restriction.
if (!input.atMessageStart) return items;
for (const command of input.selectedProviderStatus?.slashCommands ?? []) {
if (!command.name.toLowerCase().includes(query)) continue;
if (command.name === "compact" && !input.hasCompactableConversation) continue;
// T3's own limits command is answered by the thread composer; New Task has
// nowhere to show it. A provider's same-named command is left alone.
if (command.name === USAGE_LIMITS_COMMAND.name && input.offersUsageLimits && !input.hasThread) {
continue;
}
if (
!input.hasThread &&
input.selectedProviderStatus?.driver === "codex" &&
command.name === "feedback"
) {
continue;
}
items.push({
id: `pcmd:${command.name}`,
type: "provider-slash-command",
command,
label: `/${command.name}`,
description: command.description ?? "",
});
}
return items;
}
export function resolveComposerCommandSelection(input: {
readonly draftMessage: string;
readonly trigger: Pick<ComposerTrigger, "rangeStart" | "rangeEnd">;
readonly item: ComposerCommandItem;
readonly allowInteractionMode: boolean;
}): {
readonly text: string;
readonly cursor: number;
readonly interactionMode: ProviderInteractionMode | null;
} {
const { draftMessage, trigger, item } = input;
if (
input.allowInteractionMode &&
item.type === "slash-command" &&
(item.command === "plan" || item.command === "default")
) {
return {
...replaceTextRange(draftMessage, trigger.rangeStart, trigger.rangeEnd, ""),
interactionMode: item.command,
};
}
let replacement = "";
if (item.type === "path") {
replacement = `${serializeComposerFileLink(item.path)} `;
} else if (item.type === "skill") {
replacement = `$${item.skill.name} `;
} else if (item.type === "slash-command") {
replacement = `/${item.command} `;
} else if (item.type === "provider-slash-command") {
replacement = `/${item.command.name} `;
}
return {
...replaceTextRange(draftMessage, trigger.rangeStart, trigger.rangeEnd, replacement),
interactionMode: null,
};
}
/** Shared autocomplete for thread composers and unsent new-task drafts. */
export function useComposerCommandMenu({
draftMessage,
ownerKey,
environmentId,
threadShells = EMPTY_THREAD_SHELLS,
currentThreadId = null,
projectCwd,
pullRequestProjectId = null,
pullRequestRepository = null,
selectedProviderStatus,
hasThread,
hasCompactableConversation,
offersUsageLimits = false,
enabled = true,
onChangeDraftMessage,
onUpdateInteractionMode,
onUsageLimits,
}: {
readonly draftMessage: string;
readonly ownerKey: string | null;
readonly environmentId: EnvironmentId | null;
/** Candidates for `@` thread suggestions; the caller reads them from the entity store. */
readonly threadShells?: ReadonlyArray<EnvironmentThreadShell>;
/** Left out of `@` thread suggestions: a thread is never context for itself. */
readonly currentThreadId?: ThreadId | null;
readonly projectCwd: string | null;
readonly pullRequestProjectId?: ProjectId | null;
readonly pullRequestRepository?: string | null;
readonly selectedProviderStatus: ServerProvider | null;
readonly hasThread: boolean;
readonly hasCompactableConversation: boolean;
/** Whether T3 itself offers /usage-limits for the selected provider. */
readonly offersUsageLimits?: boolean;
readonly enabled?: boolean;
readonly onChangeDraftMessage: (value: string) => void;
readonly onUpdateInteractionMode?: (mode: ProviderInteractionMode) => void;
/** Picking /usage-limits is the action itself; the draft keeps nothing of it. */
readonly onUsageLimits?: () => void;
}) {
const [selection, setSelection] = useState(() => composerSelectionAtEnd(draftMessage));
const previousOwnerKeyRef = useRef(ownerKey);
const onSelectionChange = useCallback((nextSelection: ComposerEditorSelection) => {
setSelection(nextSelection);
}, []);
useEffect(() => {
// An insert (attachment, terminal capture, review comment) rewrites the draft and records
// the caret that belongs after the new chip. Clamping alone would keep the old offset,
// which sits before it.
const inserted = ownerKey ? readComposerDraftSelection(ownerKey, draftMessage) : null;
if (inserted) {
setSelection((current) =>
current.start === inserted.start && current.end === inserted.end ? current : inserted,
);
return;
}
const end = draftMessage.length;
setSelection((current) => {
const start = Math.min(current.start, end);
const selectionEnd = Math.min(current.end, end);
if (start === current.start && selectionEnd === current.end) {
return current;
}
return { start, end: selectionEnd };
});
}, [draftMessage, ownerKey]);
useEffect(() => {
if (previousOwnerKeyRef.current === ownerKey) return;
previousOwnerKeyRef.current = ownerKey;
setSelection(composerSelectionAtEnd(draftMessage));
}, [draftMessage, ownerKey]);
const skills = useMemo(
() =>
selectedProviderStatus ? resolveProviderSkillsForCwd(selectedProviderStatus, projectCwd) : [],
[projectCwd, selectedProviderStatus],
);
const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, {
reportFailure: false,
});
const selectedProviderInstanceId = selectedProviderStatus?.instanceId;
const hasWorkspaceSnapshot = hasCompleteProviderWorkspaceSnapshot(
selectedProviderStatus,
projectCwd,
);
// The last scan this composer asked for. A request inside the TTL is not
// repeated, so a client clock ahead of the server's cannot loop rescans.
const workspaceRefreshKeyRef = useRef<{ key: string; requestedAt: number } | null>(null);
const [workspaceRefreshRetry, setWorkspaceRefreshRetry] = useState<{
key: string;
notBefore: number;
} | null>(null);
const workspaceRefreshScopeKey =
environmentId && projectCwd && selectedProviderInstanceId
? `${environmentId}:${selectedProviderInstanceId}:${projectCwd}`
: null;
const workspaceSlashCommandsPending =
selectedProviderStatus?.workspaceSnapshots?.some(
(snapshot) => snapshot.cwd === projectCwd && snapshot.slashCommandsPending === true,
) ?? false;
useEffect(() => {
if (
!workspaceSlashCommandsPending ||
!workspaceRefreshRetry ||
workspaceRefreshRetry.key !== workspaceRefreshScopeKey
)
return;
const timeout = setTimeout(
() => {
setWorkspaceRefreshRetry((current) => (current === workspaceRefreshRetry ? null : current));
},
Math.max(0, workspaceRefreshRetry.notBefore - Date.now()),
);
return () => clearTimeout(timeout);
}, [workspaceRefreshRetry, workspaceRefreshScopeKey, workspaceSlashCommandsPending]);
const hadWorkspaceSnapshotRef = useRef(false);
useEffect(() => {
if (hadWorkspaceSnapshotRef.current && !hasWorkspaceSnapshot) {
workspaceRefreshKeyRef.current = null;
setWorkspaceRefreshRetry(null);
}
hadWorkspaceSnapshotRef.current = hasWorkspaceSnapshot;
}, [hasWorkspaceSnapshot]);
useEffect(() => {
if (!environmentId || !projectCwd || !selectedProviderInstanceId) return;
const key = `${environmentId}:${selectedProviderInstanceId}:${projectCwd}`;
const now = Date.now();
const lastRequest = workspaceRefreshKeyRef.current;
if (
lastRequest?.key === key &&
now - lastRequest.requestedAt < PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS
)
return;
if (hasCurrentProviderWorkspaceSnapshot(selectedProviderStatus, projectCwd, now)) {
setWorkspaceRefreshRetry(null);
return;
}
const retry = workspaceRefreshRetry;
if (retry?.key === key && now < retry.notBefore) return;
const request = { key, requestedAt: now };
workspaceRefreshKeyRef.current = request;
const retryLater = () => {
if (workspaceRefreshKeyRef.current !== request) return;
workspaceRefreshKeyRef.current = null;
setWorkspaceRefreshRetry({
key,
notBefore: Date.now() + WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS,
});
};
void refreshProviders({
environmentId,
input: { instanceId: selectedProviderInstanceId, cwd: projectCwd },
}).then((result) => {
const refreshed =
result._tag === "Success" &&
hasCompleteProviderWorkspaceSnapshot(
result.value.providers.find(
(provider) => provider.instanceId === selectedProviderInstanceId,
),
projectCwd,
);
if (!refreshed) retryLater();
}, retryLater);
}, [
draftMessage,
environmentId,
projectCwd,
refreshProviders,
selectedProviderInstanceId,
selectedProviderStatus,
workspaceRefreshRetry,
]);
const trigger = useMemo(() => {
if (!enabled || selection.start !== selection.end) {
return null;
}
return detectComposerTrigger(draftMessage, selection.end);
}, [draftMessage, enabled, selection]);
const pathSearch = useComposerPathSearch({
environmentId,
cwd: trigger?.kind === "path" ? projectCwd : null,
query: trigger?.kind === "path" ? trigger.query : null,
});
const pullRequestSearch = useComposerPullRequestSearch({
environmentId,
projectId: pullRequestProjectId,
repository: pullRequestRepository,
query: trigger?.kind === "pull-request" ? trigger.query : null,
});
const items = useMemo<ComposerCommandItem[]>(() => {
if (!trigger) return [];
if (trigger.kind === "pull-request") {
return pullRequestSearch.entries.map((entry) => ({
id: `pr:${entry.projectId}:${entry.repository}:${entry.number}`,
type: "pull-request",
pullRequest: {
number: entry.number,
title: entry.title,
url: entry.url,
headBranch: entry.headBranch,
baseBranch: entry.baseBranch,
state: entry.state,
isDraft: entry.isDraft,
},
label: `#${entry.number}`,
description: `${entry.isDraft ? "Draft" : entry.state} · ${entry.title}`,
}));
}
if (trigger.kind === "slash-command") {
const q = trigger.query.toLowerCase();
const visibleSkills = getProviderSkillsForSlashMenu(skills, true);
const commandItems = buildComposerSlashCommandItems({
query: q,
atMessageStart: trigger.rangeStart === 0,
hasThread,
hasCompactableConversation,
offersUsageLimits,
allowInteractionMode: onUpdateInteractionMode !== undefined,
selectedProviderStatus: selectedProviderStatus
? {
...selectedProviderStatus,
slashCommands: getProviderSlashCommandsForSlashMenu(
resolveProviderSlashCommandsForCwd(selectedProviderStatus, projectCwd),
visibleSkills,
),
}
: null,
});
const skillItems = visibleSkills
.filter((skill) => matchesSlashSkillQuery(skill, q))
.map((skill) => ({
id: `skill:${skill.name}`,
type: "skill" as const,
skill,
label: `skill:${skill.name}`,
description: skill.shortDescription ?? skill.description ?? "",
}));
return [...commandItems, ...skillItems];
}
if (trigger.kind === "skill") {
const enabledSkills = dedupeProviderSkillsByName(skills.filter(isProviderSkillUserInvocable));
const normalizedQuery = normalizeSearchQuery(trigger.query, {
trimLeadingPattern: /^\p{Sc}+/u,
});
if (!normalizedQuery) {
return enabledSkills.slice(0, 20).map((skill) => ({
id: `skill:${skill.name}`,
type: "skill" as const,
skill,
label: skill.displayName ?? skill.name,
description: skill.shortDescription ?? skill.description ?? "",
}));
}
const ranked: Array<{
item: (typeof enabledSkills)[number];
score: number;
tieBreaker: string;
}> = [];
for (const skill of enabledSkills) {
const displayLabel = (skill.displayName ?? skill.name).toLowerCase();
const scores = [
scoreQueryMatch({
value: skill.name.toLowerCase(),
query: normalizedQuery,
exactBase: 0,
prefixBase: 2,
boundaryBase: 4,
includesBase: 6,
fuzzyBase: 100,
boundaryMarkers: ["-", "_", "/"],
}),
scoreQueryMatch({
value: displayLabel,
query: normalizedQuery,
exactBase: 1,
prefixBase: 3,
boundaryBase: 5,
includesBase: 7,
fuzzyBase: 110,
}),
scoreQueryMatch({
value: skill.shortDescription?.toLowerCase() ?? "",
query: normalizedQuery,
exactBase: 20,
prefixBase: 22,
boundaryBase: 24,
includesBase: 26,
}),
scoreQueryMatch({
value: skill.description?.toLowerCase() ?? "",
query: normalizedQuery,
exactBase: 30,
prefixBase: 32,
boundaryBase: 34,
includesBase: 36,
}),
].filter((score): score is number => score !== null);
if (scores.length > 0) {
insertRankedSearchResult(
ranked,
{
item: skill,
score: Math.min(...scores),
tieBreaker: `${displayLabel}\u0000${skill.name}`,
},
20,
);
}
}
return ranked.map(({ item: skill }) => ({
id: `skill:${skill.name}`,
type: "skill" as const,
skill,
label: skill.displayName ?? skill.name,
description: skill.shortDescription ?? skill.description ?? "",
}));
}
if (trigger.kind === "path") {
const threadItems = environmentId
? matchComposerThreadItems({
shells: threadShells,
environmentId,
excludeThreadId: currentThreadId,
query: trigger.query,
})
: [];
return [
...threadItems,
...pathSearch.entries.map((entry) => {
const parts = entry.path.split("/");
return {
id: `path:${entry.path}`,
type: "path" as const,
path: entry.path,
kind: entry.kind,
label: parts[parts.length - 1] ?? entry.path,
description: parts.length > 1 ? parts.slice(0, -1).join("/") : "",
};
}),
];
}
return [];
}, [
currentThreadId,
environmentId,
threadShells,
hasThread,
hasCompactableConversation,
onUpdateInteractionMode,
pathSearch.entries,
pullRequestSearch.entries,
projectCwd,
selectedProviderStatus,
skills,
trigger,
offersUsageLimits,
]);
const onSelect = useCallback(
(item: ComposerCommandItem) => {
if (!trigger) return;
if (item.type === "thread") {
if (!ownerKey || trigger.kind !== "path") return;
const shell = threadShells.find(
(candidate) =>
candidate.environmentId === item.thread.environmentId &&
candidate.id === item.thread.threadId,
);
if (!shell) return;
const record = threadComposerContext(item.thread, shell.title);
const existing = getComposerDraftSnapshot(ownerKey).context?.records ?? [];
const alreadyAttached = existing.some((entry) => entry.contextId === record.contextId);
if (!alreadyAttached && existing.length >= COMPOSER_CONTEXT_MAX_RECORDS) {
Alert.alert(
"Too many context items",
"Remove some context from the draft and try again.",
);
return;
}
const result = replaceTextRange(
draftMessage,
trigger.rangeStart,
trigger.rangeEnd,
`${formatComposerContextReference(record)} `,
);
onChangeDraftMessage(result.text);
if (!alreadyAttached) {
const draft = getComposerDraftSnapshot(ownerKey);
setComposerDraftContext(ownerKey, {
version: 1,
records: [...(draft.context?.records ?? []), record],
});
}
setSelection({ start: result.cursor, end: result.cursor });
return;
}
if (item.type === "pull-request") {
if (
!ownerKey ||
trigger.kind !== "pull-request" ||
!items.some((candidate) => candidate.id === item.id)
)
return;
const record = pullRequestComposerContext(item.pullRequest, uuidv4());
if (
(getComposerDraftSnapshot(ownerKey).context?.records.length ?? 0) >=
COMPOSER_CONTEXT_MAX_RECORDS
) {
Alert.alert(
"Too many context items",
"Remove some context from the draft and try again.",
);
return;
}
const result = replaceTextRange(
draftMessage,
trigger.rangeStart,
trigger.rangeEnd,
`${formatComposerContextReference(record)} `,
);
onChangeDraftMessage(result.text);
const draft = getComposerDraftSnapshot(ownerKey);
setComposerDraftContext(ownerKey, {
version: 1,
records: [...(draft.context?.records ?? []), record],
});
setSelection({ start: result.cursor, end: result.cursor });
return;
}
if (
item.type === "provider-slash-command" &&
item.command.name === USAGE_LIMITS_COMMAND.name &&
onUsageLimits
) {
const cleared = replaceTextRange(draftMessage, trigger.rangeStart, trigger.rangeEnd, "");
setSelection({ start: cleared.cursor, end: cleared.cursor });
onChangeDraftMessage(cleared.text);
onUsageLimits();
return;
}
const result = resolveComposerCommandSelection({
draftMessage,
trigger,
item,
allowInteractionMode:
onUpdateInteractionMode !== undefined &&
selectedProviderStatus?.showInteractionModeToggle !== false,
});
setSelection({ start: result.cursor, end: result.cursor });
onChangeDraftMessage(result.text);
if (result.interactionMode !== null) {
onUpdateInteractionMode?.(result.interactionMode);
}
},
[
draftMessage,
ownerKey,
items,
onChangeDraftMessage,
onUpdateInteractionMode,
onUsageLimits,
selectedProviderStatus?.showInteractionModeToggle,
threadShells,
trigger,
],
);
return {
selection,
onSelectionChange,
trigger,
items,
skills,
isLoading:
trigger?.kind === "pull-request" ? pullRequestSearch.isPending : pathSearch.isPending,
error:
trigger?.kind === "pull-request"
? pullRequestProjectId === null || pullRequestRepository === null
? "Pull requests are unavailable for this project."
: pullRequestSearch.error
: null,
onSelect,
};
}