Open History to see the last commit
1import type { EnvironmentId, ThreadContextRecord } from "@t3tools/contracts";
2import { msg } from "gt-react";
3import { m, type MessageResolver } from "../../i18n/messages";
4import { collectInlineContextIds } from "../../lib/composerContextReferences";
5import type { ComposerThreadDraftState } from "../../composerDraftStore";
6
7/** Compare user content, including edited image bytes, without treating upload metadata as an edit. */
8export function composerDraftContentChanged(
9 before: ComposerThreadDraftState | null,
10 after: ComposerThreadDraftState | null,
11): boolean {
12 if (before === after) return false;
13 if (!before || !after) return true;
14 const sameIds = (left: ReadonlyArray<{ id: string }>, right: ReadonlyArray<{ id: string }>) =>
15 left.length === right.length && left.every((item, index) => item.id === right[index]?.id);
16 return (
17 before.prompt !== after.prompt ||
18 !sameIds(before.images, after.images) ||
19 before.images.some((image, index) => image.file !== after.images[index]?.file) ||
20 !sameIds(before.files, after.files) ||
21 before.terminalContexts !== after.terminalContexts ||
22 before.previewAnnotations !== after.previewAnnotations ||
23 before.reviewComments !== after.reviewComments ||
24 before.threadContexts !== after.threadContexts ||
25 before.environmentContexts !== after.environmentContexts ||
26 before.replyTo !== after.replyTo
27 );
28}
29
30/** Attached threads the prompt still links to; replacing linked text leaves stale records. */
31export function referencedThreadContexts(draft: {
32 readonly prompt: string;
33 readonly threadContexts: ReadonlyArray<ThreadContextRecord>;
34}): ReadonlyArray<ThreadContextRecord> {
35 if (draft.threadContexts.length === 0) return [];
36 const referenced = new Set(collectInlineContextIds(draft.prompt));
37 return draft.threadContexts.filter((record) => referenced.has(record.contextId));
38}
39
40/** Why a send waits for the user to pick a machine when no machine was chosen automatically. */
41export function loadBalancingSendWarning(
42 input: {
43 readonly pending: boolean;
44 readonly threadContexts: ReadonlyArray<ThreadContextRecord>;
45 readonly candidateEnvironmentIds: ReadonlyArray<EnvironmentId>;
46 },
47 resolve: MessageResolver = m,
48) {
49 if (input.pending) {
50 return {
51 title: resolve(msg("Checking machine resources")),
52 description: resolve(
53 msg("Resource checks are still running. You can choose a machine in the composer."),
54 ),
55 };
56 }
57 const noMachineReadsEveryThread =
58 input.threadContexts.length > 0 &&
59 !input.candidateEnvironmentIds.some((environmentId) =>
60 input.threadContexts.every((record) => record.environmentId === environmentId),
61 );
62 return {
63 title: resolve(msg("Choose a machine to continue")),
64 description: noMachineReadsEveryThread
65 ? resolve(
66 msg(
67 "No machine can read every attached thread. Choose a machine in the composer or remove the threads.",
68 ),
69 )
70 : resolve(
71 msg(
72 "No eligible machine has available resources. Choose a machine in the composer to override.",
73 ),
74 ),
75 };
76}
77
78/**
79 * Why a multiple-model send cannot start: the server predates worktree bootstrap, or the
80 * draft is not a new thread on a Git branch with at least one model.
81 */
82export function multipleModelSendBlock(input: {
83 readonly selectionCount: number;
84 readonly serverSupportsWorktreeBootstrap: boolean;
85 readonly isLocalDraftThread: boolean;
86 readonly isGitRepo: boolean;
87 readonly branch: string | null;
88}): "server-outdated" | "needs-new-git-thread" | null {
89 if (!input.serverSupportsWorktreeBootstrap) return "server-outdated";
90 if (
91 !input.isLocalDraftThread ||
92 !input.isGitRepo ||
93 !input.branch ||
94 input.selectionCount === 0
95 ) {
96 return "needs-new-git-thread";
97 }
98 return null;
99}
100
101/** Why a message sent over pending approvals did not go out, or null to stop quietly. */
102export function approvalRedirectNotice(
103 input: {
104 readonly draftChanged: boolean;
105 readonly declined: boolean;
106 readonly approvalResponding: boolean;
107 readonly approvalArrived: boolean;
108 },
109 resolve: MessageResolver = m,
110): string | null {
111 if (input.draftChanged) {
112 return input.declined
113 ? resolve(msg("Approval declined. Your message changed, so it wasn't sent."))
114 : resolve(msg("Your message changed, so it wasn't sent."));
115 }
116 if (input.approvalResponding) {
117 return resolve(msg("An approval is already being answered, so your message wasn't sent."));
118 }
119 if (input.declined && input.approvalArrived) {
120 return resolve(msg("Another approval arrived, so your message wasn't sent."));
121 }
122 return null;
123}