The best way to control Claude Code, Codex and more from any computer or phone

main
ts 178 lines 6.27 KB
Raw
1 import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell";
2 import type { EnvironmentId, MessageId, ProjectId } from "@t3tools/contracts";
3
4 import { msg } from "gt-react-native";
5 import { m, type MessageResolver } from "../i18n/messages";
6 import { deriveThreadTitleFromPrompt } from "../lib/projectThreadStartTurn";
7 import {
8 isQueuedThreadCreationSendable,
9 resolveThreadOutboxDeliveryAction,
10 type QueuedThreadCreation,
11 type QueuedThreadMessage,
12 } from "./thread-outbox-model";
13 import { isNewTaskDraftKey } from "./new-task-draft-key";
14 import type { ComposerDraft } from "./use-composer-drafts";
15 import { resolveNewTaskDraftWorkspace } from "../features/threads/new-task-context-presentation";
16
17 export type PendingNewTask = PendingQueuedTask | PendingDraftTask;
18
19 export interface PendingQueuedTask {
20 readonly kind: "pending";
21 readonly key: string;
22 readonly environmentId: EnvironmentId;
23 readonly projectId: ProjectId;
24 readonly projectTitle: string | undefined;
25 readonly projectCwd: string | undefined;
26 readonly branch: string | null;
27 readonly title: string;
28 readonly createdAt: string;
29 readonly message: QueuedThreadMessage;
30 readonly creation: QueuedThreadCreation;
31 }
32
33 export interface PendingDraftTask {
34 readonly kind: "draft";
35 readonly key: string;
36 readonly environmentId: EnvironmentId;
37 readonly projectId: ProjectId;
38 readonly projectTitle: undefined;
39 readonly projectCwd: undefined;
40 readonly title: string;
41 readonly createdAt: string;
42 readonly draftKey: string;
43 readonly draft: ComposerDraft;
44 }
45
46 function composerDraftHasUserContent(draft: ComposerDraft): boolean {
47 return draft.text.trim().length > 0 || draft.attachments.length > 0;
48 }
49
50 function draftTitle(draft: ComposerDraft, resolve: MessageResolver): string {
51 if (draft.text.trim().length > 0) {
52 return deriveThreadTitleFromPrompt(draft.text);
53 }
54 return resolve(
55 msg("{count, plural, one {{count} attachment} other {{count} attachments}}", {
56 count: draft.attachments.length,
57 }),
58 );
59 }
60
61 /** A project's new-task defaults, as useNewTaskProjectDefaults resolves them. */
62 export interface PendingDraftProjectDefaults {
63 readonly canChooseWorkspace: boolean;
64 readonly defaultMode: "local" | "worktree";
65 readonly defaultModeSettled: boolean;
66 }
67
68 /**
69 * True when the draft's branch is derived from refs rather than stored, so the list must load
70 * refs to show it.
71 */
72 export function pendingDraftBranchNeedsRefs(
73 draft: ComposerDraft,
74 defaults: PendingDraftProjectDefaults,
75 ): boolean {
76 const selection = defaults.canChooseWorkspace ? draft.workspaceSelection : undefined;
77 return selection === undefined ? defaults.defaultMode === "worktree" : selection.branch === null;
78 }
79
80 /** The branch a draft will start on, derived the same way the new-task flow and send path do. */
81 export function resolvePendingDraftBranch(input: {
82 readonly draft: ComposerDraft;
83 readonly defaults: PendingDraftProjectDefaults;
84 readonly isGitRepo: boolean;
85 readonly branches: Parameters<typeof resolveNewTaskDraftWorkspace>[0]["branches"];
86 readonly projectCwd: string | null;
87 }): string | null {
88 return resolveNewTaskDraftWorkspace({
89 selection: input.draft.workspaceSelection,
90 canChooseWorkspace: input.defaults.canChooseWorkspace,
91 defaultMode: input.defaults.defaultMode,
92 defaultModeSettled: input.defaults.defaultModeSettled,
93 isGitRepo: input.isGitRepo,
94 branches: input.branches,
95 projectCwd: input.projectCwd,
96 }).branch;
97 }
98
99 export interface ThreadOutboxSendGate {
100 readonly connectedEnvironmentIds: ReadonlySet<EnvironmentId>;
101 readonly operableEnvironmentIds: ReadonlySet<EnvironmentId>;
102 readonly shellStatuses: ReadonlyMap<EnvironmentId, EnvironmentShellStatus>;
103 readonly retryingMessageIds: Readonly<Partial<Record<MessageId, true>>>;
104 }
105
106 /** The outbox drain's gate for a creation: sendable, connected, shell live, operate granted, not backing off. */
107 export function isQueuedCreationSending(
108 message: QueuedThreadMessage,
109 gate: ThreadOutboxSendGate,
110 ): boolean {
111 const environmentId = message.environmentId;
112 return (
113 isQueuedThreadCreationSendable(message) &&
114 gate.retryingMessageIds[message.messageId] !== true &&
115 gate.operableEnvironmentIds.has(environmentId) &&
116 resolveThreadOutboxDeliveryAction({
117 isCreation: true,
118 threadExists: false,
119 shellStatus: gate.shellStatuses.get(environmentId) ?? "empty",
120 environmentConnected: gate.connectedEnvironmentIds.has(environmentId),
121 }) === "send"
122 );
123 }
124
125 /** Render callers pass `useMessages()` so draft titles follow a language switch. */
126 export function buildPendingNewTasks(
127 input: {
128 readonly queuedMessages: ReadonlyArray<QueuedThreadMessage>;
129 readonly drafts: Readonly<Record<string, ComposerDraft>>;
130 readonly sendGate: ThreadOutboxSendGate;
131 },
132 resolve: MessageResolver = m,
133 ): ReadonlyArray<PendingNewTask> {
134 const tasks: PendingNewTask[] = [];
135 for (const message of input.queuedMessages) {
136 // Listing a creation already being sent would flash an "Unsent" row.
137 if (!message.creation || isQueuedCreationSending(message, input.sendGate)) {
138 continue;
139 }
140 tasks.push({
141 kind: "pending",
142 key: `pending-task:${message.messageId}`,
143 environmentId: message.environmentId,
144 projectId: message.creation.projectId,
145 projectTitle: message.creation.projectTitle,
146 projectCwd: message.creation.projectCwd,
147 branch: message.creation.branch,
148 title: deriveThreadTitleFromPrompt(message.text),
149 createdAt: message.createdAt,
150 message,
151 creation: message.creation,
152 });
153 }
154 for (const [draftKey, draft] of Object.entries(input.drafts)) {
155 if (!isNewTaskDraftKey(draftKey) || !draft.project || !composerDraftHasUserContent(draft)) {
156 continue;
157 }
158 tasks.push({
159 kind: "draft",
160 key: `draft-task:${draftKey}`,
161 environmentId: draft.project.environmentId,
162 projectId: draft.project.projectId,
163 projectTitle: undefined,
164 projectCwd: undefined,
165 title: draftTitle(draft, resolve),
166 createdAt: draft.project.createdAt,
167 draftKey,
168 draft,
169 });
170 }
171 tasks.sort((left, right) => {
172 if (left.kind !== right.kind) {
173 return left.kind === "draft" ? -1 : 1;
174 }
175 return right.createdAt.localeCompare(left.createdAt) || left.key.localeCompare(right.key);
176 });
177 return tasks;
178 }