-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathuseTaskCreation.ts
More file actions
208 lines (191 loc) · 6.15 KB
/
useTaskCreation.ts
File metadata and controls
208 lines (191 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { useAuthStore } from "@features/auth/stores/authStore";
import type { MessageEditorHandle } from "@features/message-editor/components/MessageEditor";
import { useTaskInputHistoryStore } from "@features/message-editor/stores/taskInputHistoryStore";
import {
contentToXml,
extractFilePaths,
} from "@features/message-editor/utils/content";
import { useSettingsStore } from "@features/settings/stores/settingsStore";
import { useCreateTask } from "@features/tasks/hooks/useTasks";
import { useConnectivity } from "@hooks/useConnectivity";
import type { WorkspaceMode } from "@main/services/workspace/schemas";
import { get } from "@renderer/di/container";
import { RENDERER_TOKENS } from "@renderer/di/tokens";
import { toast } from "@renderer/utils/toast";
import type { ExecutionMode, Task } from "@shared/types";
import { useNavigationStore } from "@stores/navigationStore";
import { logger } from "@utils/logger";
import { useCallback, useState } from "react";
import type { TaskCreationInput, TaskService } from "../service/service";
const log = logger.scope("task-creation");
interface UseTaskCreationOptions {
editorRef: React.RefObject<MessageEditorHandle | null>;
selectedDirectory: string;
selectedRepository?: string | null;
githubIntegrationId?: number;
workspaceMode: WorkspaceMode;
branch?: string | null;
editorIsEmpty: boolean;
executionMode?: ExecutionMode;
adapter?: "claude" | "codex";
model?: string;
reasoningLevel?: string;
environmentId?: string | null;
sandboxEnvironmentId?: string;
onTaskCreated?: (task: Task) => void;
}
interface UseTaskCreationReturn {
isCreatingTask: boolean;
canSubmit: boolean;
handleSubmit: () => void;
}
function prepareTaskInput(
content: Parameters<typeof contentToXml>[0],
options: {
selectedDirectory: string;
selectedRepository?: string | null;
githubIntegrationId?: number;
workspaceMode: WorkspaceMode;
branch?: string | null;
executionMode?: ExecutionMode;
adapter?: "claude" | "codex";
model?: string;
reasoningLevel?: string;
environmentId?: string | null;
sandboxEnvironmentId?: string;
},
): TaskCreationInput {
return {
content: contentToXml(content).trim(),
filePaths: extractFilePaths(content),
repoPath: options.selectedDirectory,
repository: options.selectedRepository,
githubIntegrationId: options.githubIntegrationId,
workspaceMode: options.workspaceMode,
branch: options.branch,
executionMode: options.executionMode,
adapter: options.adapter,
model: options.model,
reasoningLevel: options.reasoningLevel,
environmentId: options.environmentId ?? undefined,
sandboxEnvironmentId: options.sandboxEnvironmentId,
};
}
function getErrorTitle(failedStep: string): string {
const titles: Record<string, string> = {
repo_detection: "Failed to detect repository",
task_creation: "Failed to create task",
workspace_creation: "Failed to create workspace",
cloud_run: "Failed to start cloud execution",
agent_session: "Failed to start agent session",
};
return titles[failedStep] ?? "Task creation failed";
}
export function useTaskCreation({
editorRef,
selectedDirectory,
selectedRepository,
githubIntegrationId,
workspaceMode,
branch,
editorIsEmpty,
executionMode,
adapter,
model,
reasoningLevel,
environmentId,
sandboxEnvironmentId,
onTaskCreated,
}: UseTaskCreationOptions): UseTaskCreationReturn {
const [isCreatingTask, setIsCreatingTask] = useState(false);
const { navigateToTask } = useNavigationStore();
const { isAuthenticated } = useAuthStore();
const { invalidateTasks } = useCreateTask();
const { isOnline } = useConnectivity();
// Cloud mode can work with either selectedRepository (production) or selectedDirectory (dev testing)
const hasRequiredPath = !!selectedRepository || !!selectedDirectory;
const canSubmit =
!!editorRef.current &&
isAuthenticated &&
isOnline &&
hasRequiredPath &&
!isCreatingTask &&
!editorIsEmpty;
const handleSubmit = useCallback(async () => {
const editor = editorRef.current;
if (!canSubmit || !editor) return;
setIsCreatingTask(true);
try {
const content = editor.getContent();
log.info("Submitting task", { workspaceMode, selectedDirectory });
const plainText = editor.getText()?.trim();
if (plainText) {
useTaskInputHistoryStore.getState().addPrompt(plainText);
}
const input = prepareTaskInput(content, {
selectedDirectory,
selectedRepository,
githubIntegrationId,
workspaceMode,
branch,
executionMode,
adapter,
model,
reasoningLevel,
environmentId,
sandboxEnvironmentId,
});
if (executionMode) {
useSettingsStore.getState().setLastUsedInitialTaskMode(executionMode);
}
const taskService = get<TaskService>(RENDERER_TOKENS.TaskService);
const result = await taskService.createTask(input, (output) => {
invalidateTasks(output.task);
if (onTaskCreated) {
onTaskCreated(output.task);
} else {
navigateToTask(output.task);
}
editor.clear();
log.info("Task ready, navigated early", { taskId: output.task.id });
});
if (!result.success) {
const title = getErrorTitle(result.failedStep);
toast.error(title, { description: result.error });
log.error("Task creation failed", {
failedStep: result.failedStep,
error: result.error,
});
}
} catch (error) {
const description =
error instanceof Error ? error.message : "Unknown error";
toast.error("Failed to create task", { description });
log.error("Unexpected error during task creation", { error });
} finally {
setIsCreatingTask(false);
}
}, [
canSubmit,
editorRef,
selectedDirectory,
selectedRepository,
githubIntegrationId,
workspaceMode,
branch,
executionMode,
adapter,
model,
reasoningLevel,
environmentId,
sandboxEnvironmentId,
invalidateTasks,
navigateToTask,
onTaskCreated,
]);
return {
isCreatingTask,
canSubmit,
handleSubmit,
};
}