Skip to content

Commit b8d5c37

Browse files
committed
fix: preserve dictation context and session lifecycle
1 parent 47562fe commit b8d5c37

6 files changed

Lines changed: 40 additions & 29 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3570,6 +3570,7 @@ export default function ChatView(props: ChatViewProps) {
35703570
) : null}
35713571
{!composerMounted ? null : (
35723572
<ChatComposer
3573+
getShortcutContext={getShortcutContextStable}
35733574
canOperateThread={canOperateThread}
35743575
reportedModelSelection={reportedModelSelection}
35753576
multipleModelSelections={multipleModelSelections}

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@ import { attachmentUploadBlockReason } from "../../lib/attachmentUploadState";
137137
import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
138138
import { isCommandPaletteOpen } from "../../commandPaletteBus";
139139
import { isInSideChatPane } from "../../lib/sideChatPane";
140-
import { getTerminalFocusOwner } from "../../lib/terminalFocus";
141-
import { resolveShortcutCommand, shortcutLabelForCommand } from "../../keybindings";
140+
import {
141+
resolveShortcutCommand,
142+
shortcutLabelForCommand,
143+
type ShortcutMatchContext,
144+
} from "../../keybindings";
142145
import {
143146
type TerminalContextDraft,
144147
type TerminalContextSelection,
@@ -356,6 +359,7 @@ export interface ChatComposerHandle {
356359
}
357360

358361
export interface ChatComposerProps {
362+
getShortcutContext: (target: EventTarget | null) => Partial<ShortcutMatchContext>;
359363
composerDraftTarget: ScopedThreadRef | DraftId;
360364
environmentId: EnvironmentId;
361365
canOperateThread: boolean;
@@ -955,11 +959,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
955959
start: inserted.start,
956960
end: inserted.end + (closed.cursor - selection.start),
957961
};
958-
const previous = promptRef.current;
959-
applyPromptReplacement(0, previous.length, closed.text, {
960-
expandedCursorAfterReplace: closed.cursor,
961-
focusEditorAfterReplace: false,
962-
});
962+
onPromptChange(
963+
closed.text,
964+
collapseExpandedComposerCursor(closed.text, closed.cursor, isLiteralPendingAnswer),
965+
closed.cursor,
966+
isLiteralPendingAnswer,
967+
isLiteralPendingAnswer ? [] : collectInlineContextIds(closed.text),
968+
);
963969
},
964970
onCommitted: () => {
965971
composerEditorRef.current?.focusAt(composerCursor);
@@ -2422,11 +2428,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
24222428
? event.target instanceof Node && composerFormRef.current?.contains(event.target) === true
24232429
: !isInSideChatPane(event.target)) &&
24242430
resolveShortcutCommand(event, keybindings, {
2425-
context: {
2426-
terminalFocus: getTerminalFocusOwner() !== null,
2427-
terminalOpen,
2428-
modelPickerOpen: isComposerModelPickerOpen,
2429-
},
2431+
context: props.getShortcutContext(event.target),
24302432
}) === "composer.dictate",
24312433
});
24322434

‎apps/web/src/components/settings/DictationSettings.tsx‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import { useEnvironmentScope } from "~/state/session";
2525
import { useAtomCommand } from "~/state/use-atom-command";
2626
import { Button } from "../ui/button";
2727
import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
28+
import { Switch } from "../ui/switch";
2829
import { toastManager } from "../ui/toast";
2930
import { EnvironmentRow, environmentTransportLabel } from "./EnvironmentRow";
3031
import { FoldedSettingsSection } from "./FoldedSettingsSection";
31-
import { ScopedSwitch } from "./ScopedSwitch";
3232
import { useSettingsScope } from "./SettingsScopeContext";
3333
import { SettingsRow, SettingsSection } from "./settingsLayout";
3434
import { searchableSetting } from "./settingsSearch";
@@ -84,7 +84,7 @@ const modelDetail = (model: DictationModel) =>
8484

8585
// Scope model installation to each selected environment.
8686
export function DictationSettingsSection() {
87-
const { scope, environment, connectedEnvironments } = useSettingsScope();
87+
const { scope, environment, connectedEnvironments, targets } = useSettingsScope();
8888
const environmentId =
8989
environment?.connection.phase === "connected" && environment.serverConfig !== null
9090
? environment.environmentId
@@ -93,6 +93,9 @@ export function DictationSettingsSection() {
9393
// A server from before dictation has no dictation methods, so its subscription fails outright.
9494
const outdated = environmentId !== null && state === null && error !== null;
9595
const settings = useScopedSettings((unified) => unified.dictation);
96+
const engineMixed = targets.some(
97+
(target) => target.settings.dictation.engine !== settings.engine,
98+
);
9699
const updateSettings = useUpdateScopedSettings();
97100
const canConfigure = useEnvironmentScope(environmentId, AuthSettingsWriteScope);
98101
const projectScope = scope.kind === "project" || scope.kind === "checkout";
@@ -116,9 +119,9 @@ export function DictationSettingsSection() {
116119
: "Transcribe dictation on this environment with a local speech model. Audio is not sent to any other service."
117120
}
118121
control={
119-
<ScopedSwitch
120-
settingKeys={["dictation"]}
121-
checked={settings.engine === "local"}
122+
<Switch
123+
mixed={engineMixed}
124+
checked={!engineMixed && settings.engine === "local"}
122125
disabled={locked}
123126
aria-label="Dictation"
124127
onCheckedChange={(checked) =>
@@ -277,12 +280,10 @@ function InactiveDictationModels(props: {
277280
const [removing, setRemoving] = useState<DictationModel["id"] | null>(null);
278281
if (!state?.supported) return null;
279282
const inactive = state.models.filter(
280-
// The environment keeps its own active model even when another environment's
281-
// selection is shown; removing it would be a no-op on the server.
282283
(model) =>
283284
model.install._tag === "installed" &&
284-
model.id !== props.selectedModel &&
285-
model.id !== state.settings.localModel,
285+
(state.settings.engine === "off" ||
286+
(model.id !== props.selectedModel && model.id !== state.settings.localModel)),
286287
);
287288
if (inactive.length === 0) return null;
288289

‎apps/web/src/dictation/streamingDictation.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export function createStreamingDictation(input: {
163163
throw new VoiceTranscriptionError("unavailable", "No environment can transcribe.");
164164
}
165165
// A start that never answers must not hold dictation open after a cancel.
166-
const failure = await Promise.race([current.ready, rejectOnAbort(signal)]);
166+
const failure = await Promise.race([current.ready, current.lost, rejectOnAbort(signal)]);
167167
if (failure !== null) throw new VoiceTranscriptionError("preparation-failed", failure);
168168
const sessionId = current.sessionId;
169169
if (signal.aborted || sessionId === null) close(current);
@@ -227,17 +227,16 @@ export function createStreamingDictation(input: {
227227
dependencies: {
228228
recorder,
229229
getTranscriber: () => (input.resolveEnvironmentId() === null ? null : transcriber),
230-
// Opening the microphone is the browser's permission prompt. The session
231-
// request goes out first so the network round trip overlaps it, and
232-
// capture starts the moment the microphone is live.
230+
// Permission prompts can outlive server sessions, so request a session only after consent.
233231
requestPermission: async () => {
234232
const environmentId = input.resolveEnvironmentId();
235233
close(session);
236-
session = environmentId === null ? null : begin(environmentId);
234+
session = null;
237235
const access = await capture.open();
238236
if (!access.granted) {
239237
return { granted: false, canAskAgain: true, message: access.message };
240238
}
239+
session = environmentId === null ? null : begin(environmentId);
241240
await capture.prepare();
242241
const current = session;
243242
captureStartedAt = Date.now();

‎packages/shared/src/inlineSkills.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ describe("replaceSpokenSkills", () => {
1515
expect([...matchInlineSkills(text, skills)].map((match) => match.skill.name)).toEqual([
1616
"review",
1717
]);
18+
const sentences = replaceSpokenSkills("Use the review skill. Then run tests.", skills);
19+
expect(sentences).toBe("Use $review. Then run tests.");
20+
expect([...matchInlineSkills(sentences, skills)].map((match) => match.skill.name)).toEqual([
21+
"review",
22+
]);
1823
});
1924

2025
it("matches multi-word names, split words, and small recognition slips", () => {
2126
expect(replaceSpokenSkills("potato mode skill, fix the bug", skills)).toBe(
22-
"$poteto-mode fix the bug",
27+
"$poteto-mode, fix the bug",
2328
);
2429
expect(replaceSpokenSkills("run skill post plan read on it", skills)).toBe(
2530
"run $postplan-read on it",

‎packages/shared/src/inlineSkills.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function formatProviderSkillDisplayName(
2222
}
2323

2424
const SKILL_TOKEN_REGEX =
25-
/(^|\s)\p{Sc}(?![0-9][0-9_]*(?:[kKmMbBtT]|[eE][0-9]+)?(?:\s|$))(?=[a-zA-Z0-9:_-]*[a-zA-Z])([a-zA-Z0-9][a-zA-Z0-9:_-]*)(?=\s|$)/gu;
25+
/(^|\s)\p{Sc}(?![0-9][0-9_]*(?:[kKmMbBtT]|[eE][0-9]+)?(?:\s|$))(?=[a-zA-Z0-9:_-]*[a-zA-Z])([a-zA-Z0-9][a-zA-Z0-9:_-]*)(?=\s|[.,!?;:]|$)/gu;
2626

2727
export function* matchInlineSkills<Skill extends InlineSkill>(
2828
text: string,
@@ -131,7 +131,10 @@ export function replaceSpokenSkills(text: string, skills: readonly InlineSkill[]
131131
if (words[first]!.start < consumedUntil) continue;
132132
replacements.push({
133133
start: words[first]!.start,
134-
end: words[match.last]!.end,
134+
end:
135+
words[match.last]!.start +
136+
text.slice(words[match.last]!.start, words[match.last]!.end).replace(/[^\p{L}\p{N}]+$/u, "")
137+
.length,
135138
token: `$${match.skill.name}`,
136139
});
137140
consumedUntil = words[match.last]!.end;

0 commit comments

Comments
 (0)