From 0cabd6e65f6d35487a909d86d5aa33089e465015 Mon Sep 17 00:00:00 2001 From: Ryan Zhang Date: Fri, 20 Nov 2026 13:33:45 -0800 Subject: [PATCH 1/3] fix(notebook-migration): reject uploads whose language does not match the selected tab --- .../user-workflow.component.spec.ts | 183 +++++++++++++++++- .../user-workflow/user-workflow.component.ts | 135 +++++++++++-- 2 files changed, 304 insertions(+), 14 deletions(-) diff --git a/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.spec.ts b/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.spec.ts index 4a305fb9062..f814fd8593d 100644 --- a/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.spec.ts +++ b/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.spec.ts @@ -468,7 +468,7 @@ describe("SavedWorkflowSectionComponent", () => { expect(proceed).toBe(false); expect(errorSpy).toHaveBeenCalledWith( - "This notebook uses a python kernel, not R. Switch the notebook type to Python Notebook." + "This notebook uses a Python kernel, not R. Switch the notebook type to Python Notebook." ); expect(migration.sendToAIGenerateWorkflow).not.toHaveBeenCalled(); }); @@ -481,12 +481,185 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()({ name: "analysis.py" } as NzUploadFile, "gpt-4", "r"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith("Please upload an R Jupyter Notebook (.ipynb) or an R (.R) file."); + expect(errorSpy).toHaveBeenCalledWith("This is a Python file. Upload it under the Python File tab."); expect(migration.parseAndTagNotebook).not.toHaveBeenCalled(); expect(scriptSpy).not.toHaveBeenCalled(); }); }); + describe("language mismatches", () => { + function spies(notebook: unknown = { cells: [] }) { + const migration = TestBed.inject(NotebookMigrationService); + return { + notebookConvert: vi.spyOn(migration, "sendToAIGenerateWorkflow"), + notebookParse: vi.spyOn(migration, "parseAndTagNotebook").mockResolvedValue(notebook as any), + scriptParse: vi.spyOn(migration, "parseScriptFile"), + folderParse: vi.spyOn(migration, "parseFolder").mockRejectedValue(new Error("stop")), + errorSpy: vi.spyOn(TestBed.inject(NotificationService), "error").mockImplementation(() => {}), + }; + } + + function enableR() { + (TestBed.inject(GuiConfigService) as unknown as MockGuiConfigService).setConfig({ + rNotebookMigrationEnabled: true, + }); + } + + function pickedFile(relativePath: string): NzUploadFile { + const file = new File([""], relativePath.split("/").pop() ?? relativePath); + Object.defineProperty(file, "webkitRelativePath", { value: relativePath }); + return file as unknown as NzUploadFile; + } + + const rKernel = { cells: [], metadata: { kernelspec: { language: "R" } } }; + + it("points an R notebook uploaded as Python at the R notebook type", async () => { + enableR(); + const { notebookConvert, errorSpy } = spies(rKernel); + + const proceed = await getRequestImport()(ipynbFile, "gpt-4", "python"); + + expect(proceed).toBe(false); + expect(errorSpy).toHaveBeenCalledWith( + "This notebook uses an R kernel, not Python. Switch the notebook type to R Notebook." + ); + expect(notebookConvert).not.toHaveBeenCalled(); + }); + + it("says R is off instead of pointing at a disabled control", async () => { + const { notebookConvert, errorSpy } = spies(rKernel); + + await getRequestImport()(ipynbFile, "gpt-4", "python"); + + expect(errorSpy).toHaveBeenCalledWith( + "This notebook uses an R kernel, and R is not enabled on this deployment." + ); + expect(notebookConvert).not.toHaveBeenCalled(); + }); + + it("ignores case and surrounding space in the declared language", async () => { + const { errorSpy } = spies({ cells: [], metadata: { kernelspec: { language: " PYTHON " } } }); + + await getRequestImport()(ipynbFile, "gpt-4", "r"); + + expect(errorSpy).toHaveBeenCalledWith( + "This notebook uses a Python kernel, not R. Switch the notebook type to Python Notebook." + ); + }); + + it("falls back to language_info when there is no kernelspec", async () => { + enableR(); + const { errorSpy } = spies({ cells: [], metadata: { language_info: { name: "R" } } }); + + await getRequestImport()(ipynbFile, "gpt-4", "python"); + + expect(errorSpy).toHaveBeenCalledWith( + "This notebook uses an R kernel, not Python. Switch the notebook type to R Notebook." + ); + }); + + it("rejects a kernel in no supported language whichever type was chosen", async () => { + const { notebookConvert, errorSpy } = spies({ cells: [], metadata: { kernelspec: { language: "julia" } } }); + + await getRequestImport()(ipynbFile, "gpt-4", "python"); + await getRequestImport()(ipynbFile, "gpt-4", "r"); + + const message = "This notebook uses a julia kernel. Only Python and R notebooks can be converted."; + expect(errorSpy.mock.calls).toEqual([[message], [message]]); + expect(notebookConvert).not.toHaveBeenCalled(); + }); + + it("uses an before a kernel language starting with a vowel", async () => { + const { errorSpy } = spies({ cells: [], metadata: { kernelspec: { language: "ocaml" } } }); + + await getRequestImport()(ipynbFile, "gpt-4", "python"); + + expect(errorSpy).toHaveBeenCalledWith( + "This notebook uses an ocaml kernel. Only Python and R notebooks can be converted." + ); + }); + + it("points an R file uploaded under a Python tab at the R File tab", async () => { + enableR(); + const { scriptParse, errorSpy } = spies(); + + const proceed = await getRequestImport()({ name: "analysis.R" } as NzUploadFile, "gpt-4", "python"); + + expect(proceed).toBe(false); + expect(errorSpy).toHaveBeenCalledWith("This is an R file. Upload it under the R File tab."); + expect(scriptParse).not.toHaveBeenCalled(); + }); + + it("says R is off for an R file while R is disabled", async () => { + const { errorSpy } = spies(); + + await getRequestImport()({ name: "analysis.r" } as NzUploadFile, "gpt-4", "python"); + + expect(errorSpy).toHaveBeenCalledWith("This is an R file, and R is not enabled on this deployment."); + }); + + it("keeps the upload hint for an extension no language claims", async () => { + const { errorSpy } = spies(); + + await getRequestImport()({ name: "data.txt" } as NzUploadFile, "gpt-4", "r"); + + expect(errorSpy).toHaveBeenCalledWith("Please upload an R Jupyter Notebook (.ipynb) or an R (.R) file."); + }); + + it("points a folder of Python files on the R Folder tab at the Python Folder tab", async () => { + const { folderParse, errorSpy } = spies(); + + const proceed = await getRequestImport()( + [pickedFile("proj/main.py"), pickedFile("proj/README.md")], + "gpt-4", + "r" + ); + + expect(proceed).toBe(false); + expect(errorSpy).toHaveBeenCalledWith( + "This folder holds Python files, not R. Upload it under the Python Folder tab." + ); + expect(folderParse).not.toHaveBeenCalled(); + }); + + it("points a folder of R files on the Python Folder tab at the R Folder tab", async () => { + enableR(); + const { folderParse, errorSpy } = spies(); + + await getRequestImport()([pickedFile("proj/main.R")], "gpt-4", "python"); + + expect(errorSpy).toHaveBeenCalledWith( + "This folder holds R files, not Python. Upload it under the R Folder tab." + ); + expect(folderParse).not.toHaveBeenCalled(); + }); + + it("says R is off for a folder of R files while R is disabled", async () => { + const { errorSpy } = spies(); + + await getRequestImport()([pickedFile("proj/main.R")], "gpt-4", "python"); + + expect(errorSpy).toHaveBeenCalledWith("This folder holds R files, and R is not enabled on this deployment."); + }); + + it("leaves a folder with files in the selected language to parseFolder, even if it holds others too", async () => { + const { folderParse } = spies(); + + await getRequestImport()([pickedFile("proj/main.R"), pickedFile("proj/legacy.py")], "gpt-4", "r"); + + expect(folderParse).toHaveBeenCalled(); + }); + + it("leaves a folder with no source in any language to parseFolder's own message", async () => { + const { folderParse, errorSpy } = spies(); + + await getRequestImport()([pickedFile("proj/README.md")], "gpt-4", "r"); + + expect(folderParse).toHaveBeenCalled(); + expect(errorSpy).toHaveBeenCalledWith("stop"); + }); + }); + describe("R file input", () => { function mockRScriptGenerationSuccess() { const migration = TestBed.inject(NotebookMigrationService); @@ -673,7 +846,11 @@ describe("SavedWorkflowSectionComponent", () => { const { parseSpy, convertSpy } = mockFolderGenerationSuccess(); vi.spyOn(TestBed.inject(Router), "navigate").mockResolvedValue(true); - const proceed = await getRequestImport()(selection, "gpt-4", "r"); + const proceed = await getRequestImport()( + [pickedFile("proj/main.R"), pickedFile("proj/R/helpers.R")], + "gpt-4", + "r" + ); expect(parseSpy.mock.calls[0][1]).toBe("r"); expect(convertSpy).toHaveBeenCalledWith(assembled, "gpt-4", "r"); diff --git a/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.ts b/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.ts index 22946324803..bf8f52f6584 100644 --- a/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.ts +++ b/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.ts @@ -54,7 +54,9 @@ import { } from "../../../../workspace/service/notebook-migration/migration-llm"; import { FolderDocument, + folderRelativePath, folderRootName, + isMigratableSourcePath, pickedFileObject, pickedFilePath, } from "../../../../workspace/service/notebook-migration/folder-assembly"; @@ -76,20 +78,59 @@ import { FiltersInstructionsComponent } from "../filters-instructions/filters-in import { NzSelectComponent } from "ng-zorro-antd/select"; import { FormsModule } from "@angular/forms"; -// The single-file input each language accepts besides .ipynb. -const SCRIPT_INPUTS: Record = { +// What each language accepts, and where the modal takes it, so a mismatch can name the right place. +const MIGRATION_INPUTS: Record< + MigrationLanguage, + { + name: string; + article: string; + // As a notebook's kernelspec.language or language_info.name gives it, lowercased. + kernelLanguage: string; + extension: string; + uploadHint: string; + notebookType: string; + fileTab: string; + folderTab: string; + } +> = { python: { name: "Python", + article: "a", + kernelLanguage: "python", extension: "py", uploadHint: "Please upload a Jupyter Notebook (.ipynb) or a Python (.py) file.", + notebookType: "Python Notebook", + fileTab: "Python File", + folderTab: "Python Folder", }, r: { name: "R", + article: "an", + kernelLanguage: "r", extension: "R", uploadHint: "Please upload an R Jupyter Notebook (.ipynb) or an R (.R) file.", + notebookType: "R Notebook", + fileTab: "R File", + folderTab: "R Folder", }, }; +const MIGRATION_LANGUAGES = Object.keys(MIGRATION_INPUTS) as MigrationLanguage[]; + +/** The language a notebook declares, lowercased. Undefined when it declares none. */ +function notebookKernelLanguage(notebook: Notebook): string | undefined { + const declared = notebook.metadata?.["kernelspec"]?.language ?? notebook.metadata?.["language_info"]?.name; + return declared ? String(declared).trim().toLowerCase() : undefined; +} + +function languageOfKernel(kernelLanguage: string): MigrationLanguage | undefined { + return MIGRATION_LANGUAGES.find(language => MIGRATION_INPUTS[language].kernelLanguage === kernelLanguage); +} + +function languageOfExtension(extension: string | undefined): MigrationLanguage | undefined { + return MIGRATION_LANGUAGES.find(language => MIGRATION_INPUTS[language].extension.toLowerCase() === extension); +} + /** * Saved-workflow-section component contains information and functionality * of the saved workflows section: the list of workflows the user owns or has access to @@ -305,6 +346,21 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { return this.config.env.pythonNotebookMigrationEnabled; } + // Python needs no check: the import modal only opens while the migration flag is on. + private isLanguageEnabled(language: MigrationLanguage): boolean { + return language !== "r" || this.config.env.rNotebookMigrationEnabled; + } + + /** + * The message for an upload whose language differs from the selected one. `lead` states what was + * found; the rest points at where it belongs, or says it cannot go anywhere on this deployment. + */ + private languageMismatch(lead: string, found: MigrationLanguage, redirect: string): string { + return this.isLanguageEnabled(found) + ? `${lead}${redirect}` + : `${lead}, and ${MIGRATION_INPUTS[found].name} is not enabled on this deployment.`; + } + /** Open the AI-generate import modal, wiring its submit to generateWorkflowFromFile. */ public openAiGenerateModal(): void { this.modalService.create({ @@ -333,6 +389,11 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { language: MigrationLanguage ): Promise { if (Array.isArray(selection)) { + const mismatch = this.folderLanguageMismatch(selection, language); + if (mismatch) { + this.notificationService.error(mismatch); + return false; + } const generated = await this.generateFromFolder(selection, model, language); // Null means the step already told the user what went wrong. if (!generated) { @@ -341,10 +402,12 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { return this.saveAndOpenGenerated(this.deriveFolderName(selection), generated); } - const scriptInput = SCRIPT_INPUTS[language]; const fileExtension = selection.name.split(".").pop()?.toLowerCase(); - if (fileExtension !== "ipynb" && fileExtension !== scriptInput.extension.toLowerCase()) { - this.notificationService.error(scriptInput.uploadHint); + const fileLanguage = languageOfExtension(fileExtension); + if (fileExtension !== "ipynb" && fileLanguage !== language) { + this.notificationService.error( + fileLanguage ? this.fileLanguageMismatch(fileLanguage) : MIGRATION_INPUTS[language].uploadHint + ); return false; } @@ -374,11 +437,9 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { return null; } - const kernelLanguage = notebook.metadata?.["kernelspec"]?.language; - if (language === "r" && kernelLanguage && String(kernelLanguage).toLowerCase() !== "r") { - this.notificationService.error( - `This notebook uses a ${kernelLanguage} kernel, not R. Switch the notebook type to Python Notebook.` - ); + const mismatch = this.notebookLanguageMismatch(notebook, language); + if (mismatch) { + this.notificationService.error(mismatch); return null; } @@ -392,13 +453,65 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { } } + /** + * Why a notebook cannot be converted as the selected language, or null when it can. A notebook + * that declares no language is accepted; telling languages apart from code is left to the model. + */ + private notebookLanguageMismatch(notebook: Notebook, language: MigrationLanguage): string | null { + const kernelLanguage = notebookKernelLanguage(notebook); + if (kernelLanguage === undefined) { + return null; + } + const found = languageOfKernel(kernelLanguage); + if (found === undefined) { + const article = /^[aeiou]/.test(kernelLanguage) ? "an" : "a"; + const supported = MIGRATION_LANGUAGES.map(supportedLanguage => MIGRATION_INPUTS[supportedLanguage].name); + return `This notebook uses ${article} ${kernelLanguage} kernel. Only ${supported.join(" and ")} notebooks can be converted.`; + } + if (found === language) { + return null; + } + const { article, name, notebookType } = MIGRATION_INPUTS[found]; + return this.languageMismatch( + `This notebook uses ${article} ${name} kernel`, + found, + `, not ${MIGRATION_INPUTS[language].name}. Switch the notebook type to ${notebookType}.` + ); + } + + private fileLanguageMismatch(found: MigrationLanguage): string { + const { article, name, fileTab } = MIGRATION_INPUTS[found]; + return this.languageMismatch(`This is ${article} ${name} file`, found, `. Upload it under the ${fileTab} tab.`); + } + + /** + * Why a folder belongs under another tab, or null when it does not. Only a folder with no files in + * the selected language and some in another counts; anything else is left to parseFolder's checks. + */ + private folderLanguageMismatch(files: NzUploadFile[], language: MigrationLanguage): string | null { + const paths = files.map(file => folderRelativePath({ webkitRelativePath: pickedFilePath(file), name: file.name })); + if (paths.some(path => isMigratableSourcePath(path, language))) { + return null; + } + const found = MIGRATION_LANGUAGES.find(other => paths.some(path => isMigratableSourcePath(path, other))); + if (found === undefined) { + return null; + } + const { name, folderTab } = MIGRATION_INPUTS[found]; + return this.languageMismatch( + `This folder holds ${name} files`, + found, + `, not ${MIGRATION_INPUTS[language].name}. Upload it under the ${folderTab} tab.` + ); + } + /** Read and convert a script. The notebook comes back derived, since the upload had no cells. */ private async generateFromScript( file: NzUploadFile, model: string, language: MigrationLanguage ): Promise { - const { name, extension } = SCRIPT_INPUTS[language]; + const { name, extension } = MIGRATION_INPUTS[language]; let scriptSource: string; try { scriptSource = await this.notebookMigrationService.parseScriptFile(file as unknown as File); From bc4d4cac15c4b8d66a5d9ddb45994d71d06f313f Mon Sep 17 00:00:00 2001 From: Ryan Zhang Date: Fri, 20 Nov 2026 13:38:16 -0800 Subject: [PATCH 2/3] fix(notebook-migration): name the source language in script read errors --- .../user-workflow.component.spec.ts | 3 ++- .../user-workflow/user-workflow.component.ts | 2 +- .../service/notebook-migration/migration-llm.ts | 2 ++ .../notebook-migration.service.spec.ts | 13 +++++++++++-- .../notebook-migration.service.ts | 17 ++++++++++++----- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.spec.ts b/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.spec.ts index f814fd8593d..0588daba2ce 100644 --- a/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.spec.ts +++ b/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.spec.ts @@ -679,10 +679,11 @@ describe("SavedWorkflowSectionComponent", () => { ["analysis.R", "analysis.r"].forEach(name => { it(`converts ${name} as an R script`, async () => { - const { convertSpy } = mockRScriptGenerationSuccess(); + const { migration, convertSpy } = mockRScriptGenerationSuccess(); const proceed = await getRequestImport()({ name } as NzUploadFile, "gpt-4", "r"); + expect(migration.parseScriptFile).toHaveBeenCalledWith(expect.anything(), "r"); expect(convertSpy).toHaveBeenCalledWith("x <- 1\n", "gpt-4", "r"); expect(proceed).toBe(true); }); diff --git a/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.ts b/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.ts index bf8f52f6584..81e237c994c 100644 --- a/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.ts +++ b/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.ts @@ -514,7 +514,7 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { const { name, extension } = MIGRATION_INPUTS[language]; let scriptSource: string; try { - scriptSource = await this.notebookMigrationService.parseScriptFile(file as unknown as File); + scriptSource = await this.notebookMigrationService.parseScriptFile(file as unknown as File, language); } catch (error) { this.notificationService.error( `Failed to read the ${name} file. Please upload a valid, non-empty .${extension} file.` diff --git a/frontend/src/app/workspace/service/notebook-migration/migration-llm.ts b/frontend/src/app/workspace/service/notebook-migration/migration-llm.ts index 58cf17de068..fe596fe2723 100644 --- a/frontend/src/app/workspace/service/notebook-migration/migration-llm.ts +++ b/frontend/src/app/workspace/service/notebook-migration/migration-llm.ts @@ -175,6 +175,8 @@ const R_FOLDER_DOCUMENTATION: string[] = R_SCRIPT_DOCUMENTATION.map(doc => export type MigrationLanguage = "python" | "r"; +export const MIGRATION_LANGUAGE_NAMES: Record = { python: "Python", r: "R" }; + interface UdfOperator { type: string; // Overlaid on the schema defaults before the generated code and outputs. diff --git a/frontend/src/app/workspace/service/notebook-migration/notebook-migration.service.spec.ts b/frontend/src/app/workspace/service/notebook-migration/notebook-migration.service.spec.ts index d9d868409b1..99b380af158 100644 --- a/frontend/src/app/workspace/service/notebook-migration/notebook-migration.service.spec.ts +++ b/frontend/src/app/workspace/service/notebook-migration/notebook-migration.service.spec.ts @@ -591,7 +591,7 @@ describe("NotebookMigrationService", () => { }); }); - // parseScriptFile (reads an uploaded .py as text) + // parseScriptFile (reads an uploaded script as text) describe("parseScriptFile", () => { it("resolves the file's contents verbatim", async () => { const source = "import os\n\nprint(os.getcwd())\n"; @@ -600,7 +600,16 @@ describe("NotebookMigrationService", () => { }); it("rejects an empty file before it can cost an LLM round trip", async () => { - await expect(service.parseScriptFile(new File([""], "empty.py"))).rejects.toThrow(/empty/i); + await expect(service.parseScriptFile(new File([""], "empty.py"))).rejects.toThrow("The Python file is empty."); + }); + + it("names R in its errors for an R file", async () => { + await expect(service.parseScriptFile(new File([""], "empty.R"), "r")).rejects.toThrow("The R file is empty."); + + vi.spyOn(FileReader.prototype, "readAsText").mockImplementation(function (this: any) { + this.onerror?.(new ProgressEvent("error")); + }); + await expect(service.parseScriptFile(new File([""], "x.R"), "r")).rejects.toThrow("Failed to read the R file."); }); it("rejects a file holding only whitespace", async () => { diff --git a/frontend/src/app/workspace/service/notebook-migration/notebook-migration.service.ts b/frontend/src/app/workspace/service/notebook-migration/notebook-migration.service.ts index 0af785e1508..b139981d46b 100644 --- a/frontend/src/app/workspace/service/notebook-migration/notebook-migration.service.ts +++ b/frontend/src/app/workspace/service/notebook-migration/notebook-migration.service.ts @@ -19,7 +19,13 @@ import { Injectable } from "@angular/core"; import { AppSettings } from "../../../common/app-setting"; -import { MigrationLanguage, Notebook, NotebookMigrationLLM, SourceConversion } from "./migration-llm"; +import { + MIGRATION_LANGUAGE_NAMES, + MigrationLanguage, + Notebook, + NotebookMigrationLLM, + SourceConversion, +} from "./migration-llm"; import { buildFolderDocument, checkFolderByteSize, @@ -397,12 +403,13 @@ export class NotebookMigrationService { }); } - // Reads a .py file as text. Rejects on a file with nothing in it: an empty script would + // Reads a script as text. Rejects on a file with nothing in it: an empty script would // otherwise cost a full LLM round trip to produce an empty workflow. - public async parseScriptFile(file: File): Promise { - const source = await this.readFileAsText(file, "Python file"); + public async parseScriptFile(file: File, language: MigrationLanguage = "python"): Promise { + const name = MIGRATION_LANGUAGE_NAMES[language]; + const source = await this.readFileAsText(file, `${name} file`); if (source.trim() === "") { - throw new Error("The Python file is empty."); + throw new Error(`The ${name} file is empty.`); } return source; } From c60bf122c2b480efbe8a1077e988ef2b42ebe0f4 Mon Sep 17 00:00:00 2001 From: Ryan Zhang Date: Fri, 20 Nov 2026 13:55:17 -0800 Subject: [PATCH 3/3] fix(notebook-migration): keep migration error messages on screen longer --- .../user-workflow.component.spec.ts | 109 +++++++++++++----- .../user-workflow/user-workflow.component.ts | 29 +++-- 2 files changed, 97 insertions(+), 41 deletions(-) diff --git a/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.spec.ts b/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.spec.ts index 0588daba2ce..59113b6f32f 100644 --- a/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.spec.ts +++ b/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.spec.ts @@ -322,6 +322,8 @@ describe("SavedWorkflowSectionComponent", () => { describe("AI generate workflow (dashboard entry point)", () => { const ipynbFile = { name: "analysis.ipynb" } as NzUploadFile; + // Migration errors stay up longer than the default toast so their instructions can be read. + const MIGRATION_ERROR_OPTIONS = { nzDuration: 8000 }; const AI_BUTTON_SELECTOR = 'button[title="AI generate a workflow from source code"]'; // Opens the modal and returns the requestImport callback the component handed to it; calling @@ -419,7 +421,10 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()({ name: "data.txt" } as NzUploadFile, "gpt-4"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith("Please upload a Jupyter Notebook (.ipynb) or a Python (.py) file."); + expect(errorSpy).toHaveBeenCalledWith( + "Please upload a Jupyter Notebook (.ipynb) or a Python (.py) file.", + MIGRATION_ERROR_OPTIONS + ); expect(notebookSpy).not.toHaveBeenCalled(); expect(scriptSpy).not.toHaveBeenCalled(); }); @@ -468,7 +473,8 @@ describe("SavedWorkflowSectionComponent", () => { expect(proceed).toBe(false); expect(errorSpy).toHaveBeenCalledWith( - "This notebook uses a Python kernel, not R. Switch the notebook type to Python Notebook." + "This notebook uses a Python kernel, not R. Switch the notebook type to Python Notebook.", + MIGRATION_ERROR_OPTIONS ); expect(migration.sendToAIGenerateWorkflow).not.toHaveBeenCalled(); }); @@ -481,7 +487,10 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()({ name: "analysis.py" } as NzUploadFile, "gpt-4", "r"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith("This is a Python file. Upload it under the Python File tab."); + expect(errorSpy).toHaveBeenCalledWith( + "This is a Python file. Upload it under the Python File tab.", + MIGRATION_ERROR_OPTIONS + ); expect(migration.parseAndTagNotebook).not.toHaveBeenCalled(); expect(scriptSpy).not.toHaveBeenCalled(); }); @@ -521,7 +530,8 @@ describe("SavedWorkflowSectionComponent", () => { expect(proceed).toBe(false); expect(errorSpy).toHaveBeenCalledWith( - "This notebook uses an R kernel, not Python. Switch the notebook type to R Notebook." + "This notebook uses an R kernel, not Python. Switch the notebook type to R Notebook.", + MIGRATION_ERROR_OPTIONS ); expect(notebookConvert).not.toHaveBeenCalled(); }); @@ -532,7 +542,8 @@ describe("SavedWorkflowSectionComponent", () => { await getRequestImport()(ipynbFile, "gpt-4", "python"); expect(errorSpy).toHaveBeenCalledWith( - "This notebook uses an R kernel, and R is not enabled on this deployment." + "This notebook uses an R kernel, and R is not enabled on this deployment.", + MIGRATION_ERROR_OPTIONS ); expect(notebookConvert).not.toHaveBeenCalled(); }); @@ -543,7 +554,8 @@ describe("SavedWorkflowSectionComponent", () => { await getRequestImport()(ipynbFile, "gpt-4", "r"); expect(errorSpy).toHaveBeenCalledWith( - "This notebook uses a Python kernel, not R. Switch the notebook type to Python Notebook." + "This notebook uses a Python kernel, not R. Switch the notebook type to Python Notebook.", + MIGRATION_ERROR_OPTIONS ); }); @@ -554,7 +566,8 @@ describe("SavedWorkflowSectionComponent", () => { await getRequestImport()(ipynbFile, "gpt-4", "python"); expect(errorSpy).toHaveBeenCalledWith( - "This notebook uses an R kernel, not Python. Switch the notebook type to R Notebook." + "This notebook uses an R kernel, not Python. Switch the notebook type to R Notebook.", + MIGRATION_ERROR_OPTIONS ); }); @@ -565,7 +578,10 @@ describe("SavedWorkflowSectionComponent", () => { await getRequestImport()(ipynbFile, "gpt-4", "r"); const message = "This notebook uses a julia kernel. Only Python and R notebooks can be converted."; - expect(errorSpy.mock.calls).toEqual([[message], [message]]); + expect(errorSpy.mock.calls).toEqual([ + [message, MIGRATION_ERROR_OPTIONS], + [message, MIGRATION_ERROR_OPTIONS], + ]); expect(notebookConvert).not.toHaveBeenCalled(); }); @@ -575,7 +591,8 @@ describe("SavedWorkflowSectionComponent", () => { await getRequestImport()(ipynbFile, "gpt-4", "python"); expect(errorSpy).toHaveBeenCalledWith( - "This notebook uses an ocaml kernel. Only Python and R notebooks can be converted." + "This notebook uses an ocaml kernel. Only Python and R notebooks can be converted.", + MIGRATION_ERROR_OPTIONS ); }); @@ -586,7 +603,10 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()({ name: "analysis.R" } as NzUploadFile, "gpt-4", "python"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith("This is an R file. Upload it under the R File tab."); + expect(errorSpy).toHaveBeenCalledWith( + "This is an R file. Upload it under the R File tab.", + MIGRATION_ERROR_OPTIONS + ); expect(scriptParse).not.toHaveBeenCalled(); }); @@ -595,7 +615,10 @@ describe("SavedWorkflowSectionComponent", () => { await getRequestImport()({ name: "analysis.r" } as NzUploadFile, "gpt-4", "python"); - expect(errorSpy).toHaveBeenCalledWith("This is an R file, and R is not enabled on this deployment."); + expect(errorSpy).toHaveBeenCalledWith( + "This is an R file, and R is not enabled on this deployment.", + MIGRATION_ERROR_OPTIONS + ); }); it("keeps the upload hint for an extension no language claims", async () => { @@ -603,7 +626,10 @@ describe("SavedWorkflowSectionComponent", () => { await getRequestImport()({ name: "data.txt" } as NzUploadFile, "gpt-4", "r"); - expect(errorSpy).toHaveBeenCalledWith("Please upload an R Jupyter Notebook (.ipynb) or an R (.R) file."); + expect(errorSpy).toHaveBeenCalledWith( + "Please upload an R Jupyter Notebook (.ipynb) or an R (.R) file.", + MIGRATION_ERROR_OPTIONS + ); }); it("points a folder of Python files on the R Folder tab at the Python Folder tab", async () => { @@ -617,7 +643,8 @@ describe("SavedWorkflowSectionComponent", () => { expect(proceed).toBe(false); expect(errorSpy).toHaveBeenCalledWith( - "This folder holds Python files, not R. Upload it under the Python Folder tab." + "This folder holds Python files, not R. Upload it under the Python Folder tab.", + MIGRATION_ERROR_OPTIONS ); expect(folderParse).not.toHaveBeenCalled(); }); @@ -629,7 +656,8 @@ describe("SavedWorkflowSectionComponent", () => { await getRequestImport()([pickedFile("proj/main.R")], "gpt-4", "python"); expect(errorSpy).toHaveBeenCalledWith( - "This folder holds R files, not Python. Upload it under the R Folder tab." + "This folder holds R files, not Python. Upload it under the R Folder tab.", + MIGRATION_ERROR_OPTIONS ); expect(folderParse).not.toHaveBeenCalled(); }); @@ -639,7 +667,10 @@ describe("SavedWorkflowSectionComponent", () => { await getRequestImport()([pickedFile("proj/main.R")], "gpt-4", "python"); - expect(errorSpy).toHaveBeenCalledWith("This folder holds R files, and R is not enabled on this deployment."); + expect(errorSpy).toHaveBeenCalledWith( + "This folder holds R files, and R is not enabled on this deployment.", + MIGRATION_ERROR_OPTIONS + ); }); it("leaves a folder with files in the selected language to parseFolder, even if it holds others too", async () => { @@ -656,7 +687,7 @@ describe("SavedWorkflowSectionComponent", () => { await getRequestImport()([pickedFile("proj/README.md")], "gpt-4", "r"); expect(folderParse).toHaveBeenCalled(); - expect(errorSpy).toHaveBeenCalledWith("stop"); + expect(errorSpy).toHaveBeenCalledWith("stop", MIGRATION_ERROR_OPTIONS); }); }); @@ -697,7 +728,10 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()({ name: "analysis.R" } as NzUploadFile, "gpt-4", "r"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith("Failed to read the R file. Please upload a valid, non-empty .R file."); + expect(errorSpy).toHaveBeenCalledWith( + "Failed to read the R file. Please upload a valid, non-empty .R file.", + MIGRATION_ERROR_OPTIONS + ); expect(convertSpy).not.toHaveBeenCalled(); }); }); @@ -759,7 +793,8 @@ describe("SavedWorkflowSectionComponent", () => { expect(proceed).toBe(false); expect(errorSpy).toHaveBeenCalledWith( - "Failed to read the Python file. Please upload a valid, non-empty .py file." + "Failed to read the Python file. Please upload a valid, non-empty .py file.", + MIGRATION_ERROR_OPTIONS ); expect(convertSpy).not.toHaveBeenCalled(); }); @@ -773,7 +808,7 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()(pyFile, "gpt-4"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("simplify the script")); + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("simplify the script"), MIGRATION_ERROR_OPTIONS); }); it("reports a generation failure and resolves false", async () => { @@ -785,7 +820,10 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()(pyFile, "gpt-4"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith("Error while communicating with the LLM, check console for details."); + expect(errorSpy).toHaveBeenCalledWith( + "Error while communicating with the LLM, check console for details.", + MIGRATION_ERROR_OPTIONS + ); }); }); @@ -882,7 +920,10 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()(selection, "gpt-4"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith("No Python files were found in the selected folder."); + expect(errorSpy).toHaveBeenCalledWith( + "No Python files were found in the selected folder.", + MIGRATION_ERROR_OPTIONS + ); expect(convertSpy).not.toHaveBeenCalled(); }); @@ -895,8 +936,8 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()(selection, "gpt-4"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("cut off")); - expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("folder")); + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("cut off"), MIGRATION_ERROR_OPTIONS); + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("folder"), MIGRATION_ERROR_OPTIONS); }); it("falls back to a generic message when something other than an Error is thrown", async () => { @@ -907,7 +948,7 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()(selection, "gpt-4"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith("Failed to read the selected folder."); + expect(errorSpy).toHaveBeenCalledWith("Failed to read the selected folder.", MIGRATION_ERROR_OPTIONS); }); it("names the folder in the timeout message so the advice matches the input", async () => { @@ -919,7 +960,7 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()(selection, "gpt-4"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("simplify the folder")); + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("simplify the folder"), MIGRATION_ERROR_OPTIONS); }); it("names the workflow after the folder when the file arrives wrapped", async () => { @@ -957,7 +998,10 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()(ipynbFile, "gpt-4"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith("Failed to read the notebook file. Please upload a valid .ipynb file."); + expect(errorSpy).toHaveBeenCalledWith( + "Failed to read the notebook file. Please upload a valid .ipynb file.", + MIGRATION_ERROR_OPTIONS + ); expect(llmSpy).not.toHaveBeenCalled(); }); @@ -972,7 +1016,10 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()(ipynbFile, "gpt-4"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith("Error while communicating with the LLM, check console for details."); + expect(errorSpy).toHaveBeenCalledWith( + "Error while communicating with the LLM, check console for details.", + MIGRATION_ERROR_OPTIONS + ); expect(persist.createWorkflow).not.toHaveBeenCalled(); }); @@ -988,7 +1035,8 @@ describe("SavedWorkflowSectionComponent", () => { expect(proceed).toBe(false); expect(errorSpy).toHaveBeenCalledWith( - "Generation timed out after 10 minutes. Try again, choose a faster model, or simplify the notebook." + "Generation timed out after 10 minutes. Try again, choose a faster model, or simplify the notebook.", + MIGRATION_ERROR_OPTIONS ); expect(persist.createWorkflow).not.toHaveBeenCalled(); }); @@ -1008,7 +1056,10 @@ describe("SavedWorkflowSectionComponent", () => { const proceed = await getRequestImport()(ipynbFile, "gpt-4"); expect(proceed).toBe(false); - expect(errorSpy).toHaveBeenCalledWith("Failed to save the generated workflow, check console for details."); + expect(errorSpy).toHaveBeenCalledWith( + "Failed to save the generated workflow, check console for details.", + MIGRATION_ERROR_OPTIONS + ); expect(storeSpy).not.toHaveBeenCalled(); }); diff --git a/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.ts b/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.ts index 81e237c994c..06ded09cc90 100644 --- a/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.ts +++ b/frontend/src/app/dashboard/component/user/user-workflow/user-workflow.component.ts @@ -117,6 +117,8 @@ const MIGRATION_INPUTS: Record< const MIGRATION_LANGUAGES = Object.keys(MIGRATION_INPUTS) as MigrationLanguage[]; +const MIGRATION_ERROR_DURATION_MS = 8000; + /** The language a notebook declares, lowercased. Undefined when it declares none. */ function notebookKernelLanguage(notebook: Notebook): string | undefined { const declared = notebook.metadata?.["kernelspec"]?.language ?? notebook.metadata?.["language_info"]?.name; @@ -346,6 +348,11 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { return this.config.env.pythonNotebookMigrationEnabled; } + // Migration errors run to two instructional sentences, more than the default toast leaves time to read. + private migrationError(message: string): void { + this.notificationService.error(message, { nzDuration: MIGRATION_ERROR_DURATION_MS }); + } + // Python needs no check: the import modal only opens while the migration flag is on. private isLanguageEnabled(language: MigrationLanguage): boolean { return language !== "r" || this.config.env.rNotebookMigrationEnabled; @@ -391,7 +398,7 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { if (Array.isArray(selection)) { const mismatch = this.folderLanguageMismatch(selection, language); if (mismatch) { - this.notificationService.error(mismatch); + this.migrationError(mismatch); return false; } const generated = await this.generateFromFolder(selection, model, language); @@ -405,7 +412,7 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { const fileExtension = selection.name.split(".").pop()?.toLowerCase(); const fileLanguage = languageOfExtension(fileExtension); if (fileExtension !== "ipynb" && fileLanguage !== language) { - this.notificationService.error( + this.migrationError( fileLanguage ? this.fileLanguageMismatch(fileLanguage) : MIGRATION_INPUTS[language].uploadHint ); return false; @@ -432,14 +439,14 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { try { notebook = await this.notebookMigrationService.parseAndTagNotebook(file as unknown as File); } catch (error) { - this.notificationService.error("Failed to read the notebook file. Please upload a valid .ipynb file."); + this.migrationError("Failed to read the notebook file. Please upload a valid .ipynb file."); console.error("Notebook parse failed:", error); return null; } const mismatch = this.notebookLanguageMismatch(notebook, language); if (mismatch) { - this.notificationService.error(mismatch); + this.migrationError(mismatch); return null; } @@ -516,9 +523,7 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { try { scriptSource = await this.notebookMigrationService.parseScriptFile(file as unknown as File, language); } catch (error) { - this.notificationService.error( - `Failed to read the ${name} file. Please upload a valid, non-empty .${extension} file.` - ); + this.migrationError(`Failed to read the ${name} file. Please upload a valid, non-empty .${extension} file.`); console.error(`${name} file read failed:`, error); return null; } @@ -542,7 +547,7 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { folder = await this.notebookMigrationService.parseFolder(files.map(pickedFileObject), language); } catch (error) { // parseFolder names what was wrong with the selection, so its message is shown as-is. - this.notificationService.error(error instanceof Error ? error.message : "Failed to read the selected folder."); + this.migrationError(error instanceof Error ? error.message : "Failed to read the selected folder."); console.error("Folder read failed:", error); return null; } @@ -559,13 +564,13 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { // a faster model or trimming the input. private reportGenerationFailure(error: unknown, input: "notebook" | "script" | "folder"): void { if (error instanceof LlmResponseTruncatedError) { - this.notificationService.error(`${error.message} This ${input} may be too large to convert.`); + this.migrationError(`${error.message} This ${input} may be too large to convert.`); } else if (error instanceof LlmRequestTimeoutError) { - this.notificationService.error( + this.migrationError( `Generation timed out after ${error.minutes} minutes. Try again, choose a faster model, or simplify the ${input}.` ); } else { - this.notificationService.error("Error while communicating with the LLM, check console for details."); + this.migrationError("Error while communicating with the LLM, check console for details."); } console.error("LLM generation failed:", error); } @@ -591,7 +596,7 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { } wid = createdWorkflow.workflow.wid; } catch (error) { - this.notificationService.error("Failed to save the generated workflow, check console for details."); + this.migrationError("Failed to save the generated workflow, check console for details."); console.error("Saving the generated workflow failed:", error); return false; }