Skip to content

Commit beec3c8

Browse files
refactor(web): the composer send reads its own settings and draft instead of taking them from ChatView (#23095)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent ac23b74 commit beec3c8

4 files changed

Lines changed: 265 additions & 60 deletions

File tree

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,15 +2983,8 @@ export default function ChatView(props: ChatViewProps) {
29832983
sendEnvMode,
29842984
startFromOrigin,
29852985
localCheckoutBranchMismatch,
2986-
settings,
2987-
clientSettingsHydrated,
2988-
serverConfig,
2989-
environmentById,
29902986
logicalProjectEnvironments,
29912987
providerInstanceEntries,
2992-
supportsAttachmentUploads,
2993-
referencedThreadContexts,
2994-
composerHasNonPromptContent,
29952988
composerRef,
29962989
composerDraftTarget,
29972990
promptRef,
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3+
import { describe, expect, it } from "vite-plus/test";
4+
import { formatInlineContextReference } from "../../lib/composerContextReferences";
5+
import { threadContextRecord, threadContextReference } from "../../lib/composerContextRecords";
6+
import {
7+
approvalRedirectNotice,
8+
loadBalancingSendWarning,
9+
multipleModelSendBlock,
10+
referencedThreadContexts,
11+
} from "./composerSend.logic";
12+
13+
const local = EnvironmentId.make("local");
14+
const remote = EnvironmentId.make("remote");
15+
const localThread = threadContextRecord(scopeThreadRef(local, ThreadId.make("a")), "Local");
16+
const remoteThread = threadContextRecord(scopeThreadRef(remote, ThreadId.make("b")), "Remote");
17+
18+
describe("referencedThreadContexts", () => {
19+
it("keeps only the attached threads the prompt still links", () => {
20+
const prompt = `see ${formatInlineContextReference(threadContextReference(remoteThread))}`;
21+
expect(
22+
referencedThreadContexts({ prompt, threadContexts: [localThread, remoteThread] }),
23+
).toEqual([remoteThread]);
24+
});
25+
});
26+
27+
describe("loadBalancingSendWarning", () => {
28+
it("asks the user to wait while resource checks run", () => {
29+
expect(
30+
loadBalancingSendWarning({
31+
pending: true,
32+
threadContexts: [localThread, remoteThread],
33+
candidateEnvironmentIds: [],
34+
}).title,
35+
).toBe("Checking machine resources");
36+
});
37+
38+
it("blames the attached threads when no machine can read all of them", () => {
39+
expect(
40+
loadBalancingSendWarning({
41+
pending: false,
42+
threadContexts: [localThread, remoteThread],
43+
candidateEnvironmentIds: [local, remote],
44+
}).description,
45+
).toMatch(/No machine can read every attached thread/);
46+
});
47+
48+
it("blames resources when one machine can read every attached thread", () => {
49+
expect(
50+
loadBalancingSendWarning({
51+
pending: false,
52+
threadContexts: [localThread],
53+
candidateEnvironmentIds: [remote, local],
54+
}).description,
55+
).toMatch(/No eligible machine has available resources/);
56+
});
57+
});
58+
59+
describe("multipleModelSendBlock", () => {
60+
const ready = {
61+
selectionCount: 2,
62+
serverSupportsWorktreeBootstrap: true,
63+
isLocalDraftThread: true,
64+
isGitRepo: true,
65+
branch: "main",
66+
};
67+
68+
it("lets a new Git draft with a branch and models start", () => {
69+
expect(multipleModelSendBlock(ready)).toBeNull();
70+
});
71+
72+
it("asks for a server update before checking the draft", () => {
73+
expect(
74+
multipleModelSendBlock({
75+
...ready,
76+
serverSupportsWorktreeBootstrap: false,
77+
isGitRepo: false,
78+
}),
79+
).toBe("server-outdated");
80+
});
81+
82+
it.each([
83+
{ isLocalDraftThread: false },
84+
{ isGitRepo: false },
85+
{ branch: null },
86+
{ selectionCount: 0 },
87+
])("needs a new Git thread with a branch and models (%o)", (override) => {
88+
expect(multipleModelSendBlock({ ...ready, ...override })).toBe("needs-new-git-thread");
89+
});
90+
});
91+
92+
describe("approvalRedirectNotice", () => {
93+
const settled = {
94+
draftChanged: false,
95+
declined: true,
96+
approvalResponding: false,
97+
approvalArrived: false,
98+
};
99+
100+
it("says the decline went through when the draft changed after it", () => {
101+
expect(approvalRedirectNotice({ ...settled, draftChanged: true })).toBe(
102+
"Approval declined. Your message changed, so it wasn't sent.",
103+
);
104+
expect(approvalRedirectNotice({ ...settled, draftChanged: true, declined: false })).toBe(
105+
"Your message changed, so it wasn't sent.",
106+
);
107+
});
108+
109+
it("explains an approval already being answered", () => {
110+
expect(
111+
approvalRedirectNotice({ ...settled, declined: false, approvalResponding: true }),
112+
).toMatch(/already being answered/);
113+
});
114+
115+
it("explains an approval that arrived during the decline", () => {
116+
expect(approvalRedirectNotice({ ...settled, approvalArrived: true })).toMatch(
117+
/Another approval arrived/,
118+
);
119+
});
120+
121+
it("stays quiet when the decline failed or the user left the thread", () => {
122+
expect(approvalRedirectNotice({ ...settled, declined: false })).toBeNull();
123+
expect(approvalRedirectNotice(settled)).toBeNull();
124+
});
125+
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import type { EnvironmentId, ThreadContextRecord } from "@t3tools/contracts";
2+
import { collectInlineContextIds } from "../../lib/composerContextReferences";
3+
4+
/** Attached threads the prompt still links to; replacing linked text leaves stale records. */
5+
export function referencedThreadContexts(draft: {
6+
readonly prompt: string;
7+
readonly threadContexts: ReadonlyArray<ThreadContextRecord>;
8+
}): ReadonlyArray<ThreadContextRecord> {
9+
if (draft.threadContexts.length === 0) return [];
10+
const referenced = new Set(collectInlineContextIds(draft.prompt));
11+
return draft.threadContexts.filter((record) => referenced.has(record.contextId));
12+
}
13+
14+
/** Why a send waits for the user to pick a machine when no machine was chosen automatically. */
15+
export function loadBalancingSendWarning(input: {
16+
readonly pending: boolean;
17+
readonly threadContexts: ReadonlyArray<ThreadContextRecord>;
18+
readonly candidateEnvironmentIds: ReadonlyArray<EnvironmentId>;
19+
}) {
20+
if (input.pending) {
21+
return {
22+
title: "Checking machine resources",
23+
description: "Resource checks are still running. You can choose a machine in the composer.",
24+
};
25+
}
26+
const noMachineReadsEveryThread =
27+
input.threadContexts.length > 0 &&
28+
!input.candidateEnvironmentIds.some((environmentId) =>
29+
input.threadContexts.every((record) => record.environmentId === environmentId),
30+
);
31+
return {
32+
title: "Choose a machine to continue",
33+
description: noMachineReadsEveryThread
34+
? "No machine can read every attached thread. Choose a machine in the composer or remove the threads."
35+
: "No eligible machine has available resources. Choose a machine in the composer to override.",
36+
};
37+
}
38+
39+
/**
40+
* Why a multiple-model send cannot start: the server predates worktree bootstrap, or the
41+
* draft is not a new thread on a Git branch with at least one model.
42+
*/
43+
export function multipleModelSendBlock(input: {
44+
readonly selectionCount: number;
45+
readonly serverSupportsWorktreeBootstrap: boolean;
46+
readonly isLocalDraftThread: boolean;
47+
readonly isGitRepo: boolean;
48+
readonly branch: string | null;
49+
}): "server-outdated" | "needs-new-git-thread" | null {
50+
if (!input.serverSupportsWorktreeBootstrap) return "server-outdated";
51+
if (
52+
!input.isLocalDraftThread ||
53+
!input.isGitRepo ||
54+
!input.branch ||
55+
input.selectionCount === 0
56+
) {
57+
return "needs-new-git-thread";
58+
}
59+
return null;
60+
}
61+
62+
/** Why a message sent over pending approvals did not go out, or null to stop quietly. */
63+
export function approvalRedirectNotice(input: {
64+
readonly draftChanged: boolean;
65+
readonly declined: boolean;
66+
readonly approvalResponding: boolean;
67+
readonly approvalArrived: boolean;
68+
}): string | null {
69+
if (input.draftChanged) {
70+
return input.declined
71+
? "Approval declined. Your message changed, so it wasn't sent."
72+
: "Your message changed, so it wasn't sent.";
73+
}
74+
if (input.approvalResponding) {
75+
return "An approval is already being answered, so your message wasn't sent.";
76+
}
77+
if (input.declined && input.approvalArrived) {
78+
return "Another approval arrived, so your message wasn't sent.";
79+
}
80+
return null;
81+
}

0 commit comments

Comments
 (0)