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