Skip to content

Commit 35bc117

Browse files
committed
fix(server): honor explicit skill invocations across providers
Skills picked from the composer menu are now dispatched explicitly to every provider instead of being left as plain `$name` text that only some harnesses happen to recognize. Rebased onto main. Upstream extracted the mobile command menu into `useComposerCommandMenu`, so the composer's skill-range tracking now lives beside it: the hook reports the range of an inserted skill and `ThreadComposer` keeps the draft's ranges in sync. Review follow-ups are folded into this commit because replaying them across that refactor produced no meaningful intermediate states.
1 parent 30175a8 commit 35bc117

50 files changed

Lines changed: 2029 additions & 99 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type {
22
EnvironmentId,
3+
ExplicitSkillInvocation,
34
MessageId,
45
ModelSelection,
56
OrchestrationThreadShell,
67
ProviderInteractionMode,
78
RuntimeMode,
89
ServerConfig as T3ServerConfig,
910
} from "@t3tools/contracts";
11+
import { updateExplicitSkillInvocationsForTextEdit } from "@t3tools/shared/explicitSkillInvocations";
1012
import { StackActions, useFocusEffect, useNavigation } from "@react-navigation/native";
1113
import type { ReactNode } from "react";
1214
import {
@@ -88,6 +90,7 @@ export const COMPOSER_EXPANDED_CHROME = 156;
8890

8991
export interface ThreadComposerProps {
9092
readonly draftMessage: string;
93+
readonly draftSkillInvocations: ReadonlyArray<ExplicitSkillInvocation>;
9194
readonly draftAttachments: ReadonlyArray<DraftComposerAttachment>;
9295
readonly placeholder: string;
9396
readonly contentMaxWidth?: number;
@@ -107,7 +110,10 @@ export interface ThreadComposerProps {
107110
readonly environmentId: EnvironmentId;
108111
readonly projectCwd: string | null;
109112
readonly editorRef?: RefObject<ComposerEditorHandle | null>;
110-
readonly onChangeDraftMessage: (value: string) => void;
113+
readonly onChangeDraftMessage: (
114+
value: string,
115+
skillInvocations: ReadonlyArray<ExplicitSkillInvocation>,
116+
) => void;
111117
readonly onPickDraftMedia: () => Promise<void>;
112118
readonly onPickDraftFiles: () => Promise<void>;
113119
readonly onNativePasteImages: (uris: ReadonlyArray<string>) => Promise<void>;
@@ -303,8 +309,20 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
303309
const settingsRoutePresentedRef = useRef(false);
304310
const wasExpandedBeforePreviewRef = useRef(false);
305311
const inFlightThreadIdsRef = useRef(new Set<string>());
312+
const skillInvocationsRef = useRef<ExplicitSkillInvocation[]>([...props.draftSkillInvocations]);
313+
const previousDraftMessageRef = useRef(props.draftMessage);
306314
const { onExpandedChange } = props;
307315

316+
useEffect(() => {
317+
previousDraftMessageRef.current = props.draftMessage;
318+
skillInvocationsRef.current = [...props.draftSkillInvocations];
319+
}, [
320+
props.draftMessage,
321+
props.draftSkillInvocations,
322+
props.environmentId,
323+
props.selectedThread.id,
324+
]);
325+
308326
const [previewImageUri, setPreviewImageUri] = useState<string | null>(null);
309327
const hasContent = props.draftMessage.trim().length > 0 || props.draftAttachments.length > 0;
310328
const showStopAction =
@@ -332,21 +350,42 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
332350
}, [props.serverConfig, props.selectedThread.modelSelection.instanceId]);
333351
const composerOwnerKey = scopedThreadKey(props.environmentId, props.selectedThread.id);
334352

353+
const { onChangeDraftMessage } = props;
354+
// Keeps the draft's `$skill` ranges aligned with the text they point at, and
355+
// records the range when the command menu inserts a skill.
356+
const handleDraftMessageChange = useCallback(
357+
(nextMessage: string, addedInvocation?: ExplicitSkillInvocation) => {
358+
const previousMessage = previousDraftMessageRef.current;
359+
skillInvocationsRef.current = updateExplicitSkillInvocationsForTextEdit({
360+
previousText: previousMessage,
361+
nextText: nextMessage,
362+
invocations: skillInvocationsRef.current,
363+
});
364+
if (addedInvocation) {
365+
skillInvocationsRef.current.push(addedInvocation);
366+
skillInvocationsRef.current.sort((left, right) => left.start - right.start);
367+
}
368+
previousDraftMessageRef.current = nextMessage;
369+
onChangeDraftMessage(nextMessage, skillInvocationsRef.current);
370+
},
371+
[onChangeDraftMessage],
372+
);
373+
335374
const composerMenu = useComposerCommandMenu({
336375
draftMessage: props.draftMessage,
337376
ownerKey: composerOwnerKey,
338377
environmentId: props.environmentId,
339378
projectCwd: props.projectCwd,
340379
selectedProviderStatus,
341380
hasThread: true,
342-
onChangeDraftMessage: props.onChangeDraftMessage,
381+
onChangeDraftMessage: handleDraftMessageChange,
343382
onUpdateInteractionMode: props.onUpdateInteractionMode,
344383
});
345384
const voiceInput = useVoiceInputController({
346385
ownerKey: composerOwnerKey,
347386
draftMessage: props.draftMessage,
348387
selection: composerMenu.selection,
349-
onChangeDraftMessage: props.onChangeDraftMessage,
388+
onChangeDraftMessage: handleDraftMessageChange,
350389
onChangeSelection: composerMenu.onSelectionChange,
351390
});
352391
const voicePresentation = resolveVoiceComposerPresentation(
@@ -406,6 +445,8 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
406445
if (messageId === null) {
407446
return;
408447
}
448+
skillInvocationsRef.current = [];
449+
previousDraftMessageRef.current = "";
409450
// Sending a prompt starts agent work: arm the lock-screen card while the
410451
// app is foregrounded and the activity token can be registered. Armed
411452
// after the send so its preference read and native Activity start don't
@@ -614,7 +655,7 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
614655
readOnly={voiceInput.freezesEditor}
615656
skills={selectedProviderStatus?.skills ?? []}
616657
selection={composerMenu.selection}
617-
onChangeText={props.onChangeDraftMessage}
658+
onChangeText={handleDraftMessageChange}
618659
onSelectionChange={composerMenu.onSelectionChange}
619660
onPasteImages={(uris) => void props.onNativePasteImages(uris)}
620661
placeholder={props.placeholder}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { HeaderHeightContext } from "@react-navigation/elements";
1010
import type {
1111
ApprovalRequestId,
1212
EnvironmentId,
13+
ExplicitSkillInvocation,
1314
MessageId,
1415
ModelSelection,
1516
OrchestrationThreadShell,
@@ -20,6 +21,7 @@ import type {
2021
ThreadId,
2122
UserInputQuestion,
2223
} from "@t3tools/contracts";
24+
import { updateExplicitSkillInvocationsForTextEdit } from "@t3tools/shared/explicitSkillInvocations";
2325
import * as Haptics from "expo-haptics";
2426
import {
2527
memo,
@@ -105,6 +107,7 @@ export interface ThreadDetailScreenProps {
105107
readonly activePendingUserInputAnswers: Record<string, string | ReadonlyArray<string>> | null;
106108
readonly respondingUserInputId: ApprovalRequestId | null;
107109
readonly draftMessage: string;
110+
readonly draftSkillInvocations: ReadonlyArray<ExplicitSkillInvocation>;
108111
readonly draftAttachments: ReadonlyArray<DraftComposerAttachment>;
109112
readonly connectionStateLabel: EnvironmentConnectionPhase;
110113
/** Message sync status for the selected thread (drives the composer status pill). */
@@ -120,7 +123,10 @@ export interface ThreadDetailScreenProps {
120123
readonly usesAutomaticContentInsets?: boolean;
121124
readonly onHeaderMaterialVisibilityChange?: (visible: boolean) => void;
122125
readonly onOpenConnectionEditor: () => void;
123-
readonly onChangeDraftMessage: (value: string) => void;
126+
readonly onChangeDraftMessage: (
127+
value: string,
128+
skillInvocations: ReadonlyArray<ExplicitSkillInvocation>,
129+
) => void;
124130
readonly onPickDraftMedia: () => Promise<void>;
125131
readonly onPickDraftFiles: () => Promise<void>;
126132
readonly onNativePasteImages: (uris: ReadonlyArray<string>) => Promise<void>;
@@ -626,14 +632,21 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
626632
const nextDraft = appendCodexArtifactTemplateUsePrompt(currentDraft, template);
627633
if (nextDraft !== currentDraft) {
628634
draftMessageRef.current = nextDraft;
629-
props.onChangeDraftMessage(nextDraft);
635+
props.onChangeDraftMessage(
636+
nextDraft,
637+
updateExplicitSkillInvocationsForTextEdit({
638+
previousText: currentDraft,
639+
nextText: nextDraft,
640+
invocations: props.draftSkillInvocations,
641+
}),
642+
);
630643
}
631644
requestAnimationFrame(() => {
632645
composerEditorRef.current?.focus();
633646
composerEditorRef.current?.setSelection({ start: nextDraft.length, end: nextDraft.length });
634647
});
635648
},
636-
[props.onChangeDraftMessage],
649+
[props.draftSkillInvocations, props.onChangeDraftMessage],
637650
);
638651

639652
const handleScrollToEnd = useCallback(() => {
@@ -798,6 +811,7 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
798811
<ThreadComposer
799812
editorRef={composerEditorRef}
800813
draftMessage={props.draftMessage}
814+
draftSkillInvocations={props.draftSkillInvocations}
801815
draftAttachments={props.draftAttachments}
802816
placeholder="Ask the repo agent, or run a command…"
803817
contentMaxWidth={contentMaxWidth}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ function ThreadRouteContent(
781781
activePendingUserInputAnswers={requests.activePendingUserInputAnswers}
782782
respondingUserInputId={requests.respondingUserInputId}
783783
draftMessage={composer.draftMessage}
784+
draftSkillInvocations={composer.draftSkillInvocations}
784785
draftAttachments={composer.draftAttachments}
785786
connectionStateLabel={routeConnectionState}
786787
threadSyncStatus={selectedThreadDetailState.status}

‎apps/mobile/src/features/threads/use-composer-command-menu.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { EnvironmentId, ProviderInteractionMode, ServerProvider } from "@t3tools/contracts";
1+
import type {
2+
EnvironmentId,
3+
ExplicitSkillInvocation,
4+
ProviderInteractionMode,
5+
ServerProvider,
6+
} from "@t3tools/contracts";
27
import {
38
detectComposerTrigger,
49
replaceTextRange,
@@ -39,7 +44,12 @@ export function useComposerCommandMenu({
3944
readonly selectedProviderStatus: ServerProvider | null;
4045
readonly hasThread: boolean;
4146
readonly enabled?: boolean;
42-
readonly onChangeDraftMessage: (value: string) => void;
47+
// Inserting a skill reports the range it occupies so callers that track
48+
// explicit invocations can record it with the same edit.
49+
readonly onChangeDraftMessage: (
50+
value: string,
51+
addedSkillInvocation?: ExplicitSkillInvocation,
52+
) => void;
4353
readonly onUpdateInteractionMode?: (mode: ProviderInteractionMode) => void;
4454
}) {
4555
const [selection, setSelection] = useState(() => composerSelectionAtEnd(draftMessage));
@@ -276,7 +286,16 @@ export function useComposerCommandMenu({
276286
replacement,
277287
);
278288
setSelection({ start: result.cursor, end: result.cursor });
279-
onChangeDraftMessage(result.text);
289+
onChangeDraftMessage(
290+
result.text,
291+
item.type === "skill"
292+
? {
293+
name: item.skill.name,
294+
start: trigger.rangeStart,
295+
end: trigger.rangeStart + item.skill.name.length + 1,
296+
}
297+
: undefined,
298+
);
280299
},
281300
[draftMessage, onChangeDraftMessage, onUpdateInteractionMode, trigger],
282301
);

‎apps/mobile/src/state/thread-outbox-model.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell
77
import {
88
CommandId,
99
EnvironmentId,
10+
ExplicitSkillInvocation,
1011
IsoDateTime,
1112
MessageId,
1213
ModelSelection,
@@ -25,7 +26,7 @@ import { DraftComposerAttachmentSchema } from "../lib/composer-image-schema";
2526
import type { DraftComposerAttachment } from "../lib/composerImages";
2627
import { scopedThreadKey } from "../lib/scopedEntities";
2728

28-
const THREAD_OUTBOX_SCHEMA_VERSION = 3;
29+
const THREAD_OUTBOX_SCHEMA_VERSION = 4;
2930
const THREAD_OUTBOX_MAX_RETRY_DELAY_MS = 16_000;
3031

3132
const QueuedThreadCreationSchema = Schema.Struct({
@@ -41,12 +42,13 @@ const QueuedThreadCreationSchema = Schema.Struct({
4142
});
4243

4344
export const QueuedThreadMessageSchema = Schema.Struct({
44-
schemaVersion: Schema.Literals([1, 2, THREAD_OUTBOX_SCHEMA_VERSION]),
45+
schemaVersion: Schema.Literals([1, 2, 3, THREAD_OUTBOX_SCHEMA_VERSION]),
4546
environmentId: EnvironmentId,
4647
threadId: ThreadId,
4748
messageId: MessageId,
4849
commandId: CommandId,
4950
text: Schema.String,
51+
skillInvocations: Schema.optional(Schema.Array(ExplicitSkillInvocation)),
5052
attachments: Schema.Array(DraftComposerAttachmentSchema),
5153
modelSelection: Schema.optional(ModelSelection),
5254
runtimeMode: Schema.optional(RuntimeMode),
@@ -76,6 +78,7 @@ export interface QueuedThreadMessage {
7678
readonly messageId: MessageId;
7779
readonly commandId: CommandId;
7880
readonly text: string;
81+
readonly skillInvocations?: ReadonlyArray<typeof ExplicitSkillInvocation.Type>;
7982
readonly attachments: ReadonlyArray<DraftComposerAttachment>;
8083
readonly modelSelection?: ModelSelectionType;
8184
readonly runtimeMode?: RuntimeModeType;

‎apps/mobile/src/state/thread-outbox.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ describe("thread outbox", () => {
116116
},
117117
runtimeMode: "approval-required",
118118
interactionMode: "plan",
119+
skillInvocations: [{ name: "review", start: 0, end: 7 }],
119120
} satisfies QueuedThreadMessage;
120121

121122
expect(decodeQueuedThreadMessage(encodeQueuedThreadMessage(selectedMessage))).toEqual(

‎apps/mobile/src/state/use-composer-drafts.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,38 @@ describe("mobile composer drafts", () => {
866866
});
867867
});
868868

869+
it("persists explicit skill ranges and clears them with sent content", () => {
870+
const draftKey = "environment-1:thread-1";
871+
const draft = decodePersistedComposerDrafts({
872+
schemaVersion: 1,
873+
drafts: {
874+
[draftKey]: {
875+
text: "$review this",
876+
skillInvocations: [{ name: "review", start: 0, end: 7 }],
877+
attachments: [],
878+
},
879+
},
880+
})[draftKey];
881+
882+
expect(draft?.skillInvocations).toEqual([{ name: "review", start: 0, end: 7 }]);
883+
expect(clearComposerDraftContentState({ [draftKey]: draft! }, draftKey)).toEqual({});
884+
});
885+
886+
it("restores skill ranges when a failed send merges into matching text", () => {
887+
const draftKey = "environment-1:thread-1";
888+
const merged = mergeComposerDraftContentState(
889+
{ [draftKey]: { text: "$review this", attachments: [] } },
890+
draftKey,
891+
{
892+
text: "$review this",
893+
skillInvocations: [{ name: "review", start: 0, end: 7 }],
894+
attachments: [],
895+
},
896+
);
897+
898+
expect(merged[draftKey]?.skillInvocations).toEqual([{ name: "review", start: 0, end: 7 }]);
899+
});
900+
869901
it("clears sent content without clearing the selected model or workspace", () => {
870902
const draftKey = "environment-1:thread-1";
871903
const draft: ComposerDraft = {

0 commit comments

Comments
 (0)