Skip to content

Commit 7271ea0

Browse files
fix(web): editing a queued message keeps its thread mention chips (pingdotgg#30889)
1 parent f28dc8e commit 7271ea0

4 files changed

Lines changed: 108 additions & 12 deletions

File tree

‎apps/web/src/components/chat/queuedMessageEdit.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
22
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3+
import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
34
import { beforeEach, describe, expect, it } from "vite-plus/test";
45
import {
56
DraftId,
@@ -86,6 +87,18 @@ describe("queued message file edits", () => {
8687
expect(store.getComposerDraft(editTarget)).toBeNull();
8788
});
8889

90+
it("removes a kept edit draft that held a thread mention", () => {
91+
const store = useComposerDraftStore.getState();
92+
store.setPrompt(editTarget, "Edited message");
93+
store.setThreadContexts(editTarget, [
94+
threadContextRecord(scopeThreadRef(environmentId, ThreadId.make("thread:other")), "Other"),
95+
]);
96+
expect(
97+
recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "Original message" }),
98+
).toBe("kept");
99+
expect(store.getComposerDraft(editTarget)).toBeNull();
100+
});
101+
89102
it("reports a removed queued attachment as an edit when the run leaves", () => {
90103
const store = useComposerDraftStore.getState();
91104
store.setPrompt(editTarget, "Original message");

‎apps/web/src/components/chat/queuedMessageEdit.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ export function recoverQueuedMessageEdit(input: {
2828
});
2929
if (recovery === "kept") {
3030
store.moveComposerPromptAndImages(input.editTarget, input.threadTarget);
31-
} else {
32-
store.clearComposerContent(input.editTarget);
3331
}
32+
store.clearComposerContent(input.editTarget);
3433
return recovery;
3534
}
3635

‎apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx‎

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ import {
99
} from "@t3tools/contracts";
1010
import { useImperativeHandle, type RefObject } from "react";
1111
import { describe, expect, it, vi } from "vite-plus/test";
12-
import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences";
12+
import {
13+
ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
14+
threadContextRecord,
15+
} from "@t3tools/shared/composerContextReferences";
1316
import { toastManager } from "../ui/toast";
1417

1518
import { act, render } from "../../../test/render";
1619
import { useComposerDraftStore, useComposerThreadDraft } from "../../composerDraftStore";
20+
import { formatInlineContextReference } from "../../lib/composerContextReferences";
21+
import {
22+
environmentContextRecord,
23+
environmentContextReference,
24+
threadContextReference,
25+
} from "../../lib/composerContextRecords";
1726
import { deriveComposerSendState } from "../ChatView.logic";
1827
import { ComposerPrimaryActions } from "./ComposerPrimaryActions";
1928
import { AppAtomRegistryProvider, appAtomRegistry } from "../../rpc/atomRegistry";
@@ -74,8 +83,8 @@ const RUN = RunId.make("run-1");
7483

7584
type HookResult = ReturnType<typeof useQueuedRunEdit>;
7685

77-
function projection(runs: ReadonlyArray<RunStatus>) {
78-
return { runs, messages: [] } as unknown as ThreadStructure;
86+
function projection(runs: ReadonlyArray<RunStatus>, messages: ReadonlyArray<unknown>) {
87+
return { runs, messages } as unknown as ThreadStructure;
7988
}
8089

8190
function setRuns(ref: ScopedThreadRef, status: string) {
@@ -86,13 +95,14 @@ function Harness(props: {
8695
readonly threadRef: ScopedThreadRef;
8796
readonly runStatus: string;
8897
readonly hookRef: RefObject<HookResult | null>;
98+
readonly messages: ReadonlyArray<unknown>;
8999
}) {
90100
const result = useQueuedRunEdit({
91101
environmentId: ENV,
92102
routeThreadRef: props.threadRef,
93103
threadDraftTarget: props.threadRef,
94104
activeThreadId: props.threadRef.threadId,
95-
serverProjection: projection([{ id: RUN, status: props.runStatus }]),
105+
serverProjection: projection([{ id: RUN, status: props.runStatus }], props.messages),
96106
promptRef: { current: "" },
97107
composerRef: { current: null },
98108
});
@@ -124,10 +134,15 @@ function Harness(props: {
124134
) : null;
125135
}
126136

127-
function view(threadRef: ScopedThreadRef, runStatus: string, hook: { current: HookResult | null }) {
137+
function view(
138+
threadRef: ScopedThreadRef,
139+
runStatus: string,
140+
hook: { current: HookResult | null },
141+
messages: ReadonlyArray<unknown> = [],
142+
) {
128143
return (
129144
<AppAtomRegistryProvider>
130-
<Harness threadRef={threadRef} runStatus={runStatus} hookRef={hook} />
145+
<Harness threadRef={threadRef} runStatus={runStatus} hookRef={hook} messages={messages} />
131146
</AppAtomRegistryProvider>
132147
);
133148
}
@@ -262,6 +277,56 @@ describe("useQueuedRunEdit", () => {
262277
);
263278
});
264279

280+
it("opens an edit with only the thread and machine records its text still mentions", async () => {
281+
const { hook } = setup();
282+
const thread = threadContextRecord(THREAD_B, "Support Any File Uploads");
283+
const machine = environmentContextRecord(ENV, "Build box");
284+
const unreferenced = threadContextRecord(THREAD_A, "Removed earlier");
285+
const messageId = MessageId.make("message-1");
286+
const text = `wait for ${formatInlineContextReference(threadContextReference(thread))} on ${formatInlineContextReference(environmentContextReference(machine))}`;
287+
const messages = [
288+
{ id: messageId, text, context: { version: 1, records: [thread, machine, unreferenced] } },
289+
];
290+
await using _screen = await render(view(THREAD_A, "queued", hook, messages));
291+
292+
await act(() => requireHook(hook).beginEdit({ runId: RUN, messageId, text, attachments: [] }));
293+
294+
const draft = useComposerDraftStore.getState().getComposerDraft(requireHook(hook).draftTarget);
295+
expect(draft?.prompt).toBe(text);
296+
expect(draft?.threadContexts).toEqual([thread]);
297+
expect(draft?.environmentContexts).toEqual([machine]);
298+
});
299+
300+
it("saves only the thread mentions still in the edited message", async () => {
301+
const { hook } = setup();
302+
mocks.editQueuedRun.mockResolvedValue({ _tag: "Success" });
303+
const kept = threadContextRecord(THREAD_B, "Kept");
304+
const removed = threadContextRecord(THREAD_A, "Removed");
305+
const messageId = MessageId.make("message-1");
306+
const text = [kept, removed]
307+
.map((record) => formatInlineContextReference(threadContextReference(record)))
308+
.join(" and ");
309+
const messages = [{ id: messageId, text, context: { version: 1, records: [kept, removed] } }];
310+
await using _screen = await render(view(THREAD_A, "queued", hook, messages));
311+
await act(() => requireHook(hook).beginEdit({ runId: RUN, messageId, text, attachments: [] }));
312+
const target = requireHook(hook).draftTarget;
313+
await act(() => useComposerDraftStore.getState().setThreadContexts(target, [kept]));
314+
315+
const draft = useComposerDraftStore.getState().getComposerDraft(target);
316+
await requireHook(hook).saveEdit(
317+
{ ...savedContent(draft?.prompt ?? ""), threadContexts: draft?.threadContexts ?? [] },
318+
() => {},
319+
);
320+
321+
expect(mocks.editQueuedRun).toHaveBeenCalledWith(
322+
expect.objectContaining({
323+
input: expect.objectContaining({
324+
edit: expect.objectContaining({ context: { version: 1, records: [kept] } }),
325+
}),
326+
}),
327+
);
328+
});
329+
265330
it("keeps an open edit when the user leaves the thread and comes back", async () => {
266331
const { hook } = setup();
267332
await using screen = await render(view(THREAD_A, "queued", hook));

‎apps/web/src/components/chat/useQueuedRunEdit.ts‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { ThreadStructure } from "@t3tools/client-runtime/state/shell";
77
import {
88
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
99
type ChatAttachment,
10+
type ComposerContextRecord,
1011
type EnvironmentContextRecord,
1112
type EnvironmentId,
1213
type MessageId,
@@ -40,6 +41,7 @@ import {
4041
startAttachmentUpload,
4142
} from "../../lib/attachmentUploadQueue";
4243
import { buildMessageContext } from "../../lib/composerContextRecords";
44+
import { collectInlineContextIds } from "../../lib/composerContextReferences";
4345
import { readFileAsDataUrl } from "../../lib/imageCompression";
4446
import type { TerminalContextDraft } from "../../lib/terminalContext";
4547
import type { ReviewCommentContext } from "../../reviewCommentContext";
@@ -85,6 +87,9 @@ interface QueuedRunEditContent {
8587
readonly environmentContexts: ReadonlyArray<EnvironmentContextRecord>;
8688
}
8789

90+
const isDraftOwnedRecord = (record: ComposerContextRecord) =>
91+
record.kind === "thread" || record.kind === "environment";
92+
8893
function queuedEditDraftTarget(routeThreadRef: ScopedThreadRef, runId: RunId) {
8994
return DraftId.make(`queued-edit:${scopedThreadKey(routeThreadRef)}:${runId}`);
9095
}
@@ -127,10 +132,12 @@ async function submitQueuedRunEdit(input: {
127132
readImage: readFileAsDataUrl,
128133
uploadFiles,
129134
});
135+
// The edit draft owns thread and machine mentions, so a chip removed while editing is dropped.
130136
const keptRecords = (editing.context?.records ?? []).filter(
131137
(record) =>
132-
!("attachmentId" in record) ||
133-
editing.existingAttachments.some((attachment) => attachment.id === record.attachmentId),
138+
!isDraftOwnedRecord(record) &&
139+
(!("attachmentId" in record) ||
140+
editing.existingAttachments.some((attachment) => attachment.id === record.attachmentId)),
134141
);
135142
const newRecords =
136143
buildMessageContext({
@@ -233,9 +240,22 @@ export function useQueuedRunEdit(input: {
233240
if (editing !== null && editing.runId !== request.runId) clearEditDraft(editing.runId);
234241
const target = queuedEditDraftTarget(routeThreadRef, request.runId);
235242
const text = stripAttachmentOnlyBootstrapPrompt(request.text);
243+
const context = serverProjection?.messages.find(
244+
(message) => message.id === request.messageId,
245+
)?.context;
246+
const referenced = new Set(collectInlineContextIds(text));
247+
const records = (context?.records ?? []).filter((record) => referenced.has(record.contextId));
236248
const store = useComposerDraftStore.getState();
237249
store.clearComposerContent(target);
238250
store.setPrompt(target, text);
251+
store.setThreadContexts(
252+
target,
253+
records.filter((record): record is ThreadContextRecord => record.kind === "thread"),
254+
);
255+
store.setEnvironmentContexts(
256+
target,
257+
records.filter((record): record is EnvironmentContextRecord => record.kind === "environment"),
258+
);
239259
const threadRef = { environmentId, threadId: activeThreadId };
240260
queuedRunEdits.begin(
241261
editKey,
@@ -245,8 +265,7 @@ export function useQueuedRunEdit(input: {
245265
messageId: request.messageId,
246266
originalText: text,
247267
existingAttachments: request.attachments,
248-
context: serverProjection?.messages.find((message) => message.id === request.messageId)
249-
?.context,
268+
context,
250269
},
251270
{
252271
runs: makeQueuedRunsAtom({

0 commit comments

Comments
 (0)