Skip to content

Commit 5ff503b

Browse files
author
Bruno Quaresma
authored
fix: force task to be created with latest version (#19923)
Fixes #19744
1 parent acc0890 commit 5ff503b

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

‎site/src/pages/TasksPage/TaskPrompt.tsx‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,12 @@ const CreateTaskForm: FC<CreateTaskFormProps> = ({ templates, onSuccess }) => {
186186

187187
const createTaskMutation = useMutation({
188188
mutationFn: async ({ prompt }: CreateTaskMutationFnProps) =>
189-
API.experimental.createTask(user.id, {
189+
createTaskWithLatestTemplateVersion(
190190
prompt,
191-
template_version_id: selectedTemplate.active_version_id,
192-
template_version_preset_id: selectedPresetId,
193-
}),
191+
user.id,
192+
selectedTemplate.id,
193+
selectedPresetId,
194+
),
194195
onSuccess: async (task) => {
195196
await queryClient.invalidateQueries({ queryKey: ["tasks"] });
196197
onSuccess(task);
@@ -427,3 +428,21 @@ function sortByDefault(a: Preset, b: Preset) {
427428
// Otherwise, sort alphabetically by name
428429
return a.Name.localeCompare(b.Name);
429430
}
431+
432+
// TODO: Enforce task creation to always use the latest active template version.
433+
// During task creation, the active version might change between template load
434+
// and user action. Since handling this in the FE cannot guarantee correctness,
435+
// we should move the logic to the BE after the experimental phase.
436+
async function createTaskWithLatestTemplateVersion(
437+
prompt: string,
438+
userId: string,
439+
templateId: string,
440+
presetId: string | undefined,
441+
): Promise<Task> {
442+
const template = await API.getTemplate(templateId);
443+
return API.experimental.createTask(userId, {
444+
prompt,
445+
template_version_id: template.active_version_id,
446+
template_version_preset_id: presetId,
447+
});
448+
}

‎site/src/pages/TasksPage/TasksPage.stories.tsx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,12 @@ export const CreateTaskSuccessfully: Story = {
245245
}),
246246
},
247247
beforeEach: () => {
248+
const activeVersionId = `${MockTemplate.active_version_id}-latest`;
248249
spyOn(API, "getTemplates").mockResolvedValue([MockTemplate]);
250+
spyOn(API, "getTemplate").mockResolvedValue({
251+
...MockTemplate,
252+
active_version_id: activeVersionId,
253+
});
249254
spyOn(API.experimental, "getTasks")
250255
.mockResolvedValueOnce(MockTasks)
251256
.mockResolvedValue([MockNewTaskData, ...MockTasks]);
@@ -262,6 +267,17 @@ export const CreateTaskSuccessfully: Story = {
262267
await userEvent.click(submitButton);
263268
});
264269

270+
await step("Uses latest template version", () => {
271+
expect(API.experimental.createTask).toHaveBeenCalledWith(
272+
MockUserOwner.id,
273+
{
274+
prompt: MockNewTaskData.prompt,
275+
template_version_id: `${MockTemplate.active_version_id}-latest`,
276+
template_version_preset_id: undefined,
277+
},
278+
);
279+
});
280+
265281
await step("Displays success message", async () => {
266282
const body = within(canvasElement.ownerDocument.body);
267283
const successMessage = await body.findByText(/task created/i);
@@ -281,6 +297,7 @@ export const CreateTaskError: Story = {
281297
decorators: [withGlobalSnackbar],
282298
beforeEach: () => {
283299
spyOn(API, "getTemplates").mockResolvedValue([MockTemplate]);
300+
spyOn(API, "getTemplate").mockResolvedValue(MockTemplate);
284301
spyOn(API.experimental, "getTasks").mockResolvedValue(MockTasks);
285302
spyOn(API.experimental, "createTask").mockRejectedValue(
286303
mockApiError({

0 commit comments

Comments
 (0)