This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-9578-728d5cd7039257acfb54dfeef73f87db053e21f6 in repository https://gitbox.apache.org/repos/asf/texera.git
commit 3ed01463a0ae537234d00e703e5fe13817a385c3 Author: Ryan Zhang <[email protected]> AuthorDate: Mon Nov 23 23:55:49 2026 +0000 feat(notebook-migration): reject uploads whose language does not match the selected tab (#9578) ### What changes were proposed in this PR? The import modal decides which prompts a conversion uses from where the user uploads: the Python Notebook / R Notebook switch on the Jupyter Notebook tab, or the Python File, Python Folder, R File and R Folder tabs. Until now only one mismatch was checked, and its message could point users to the wrong place. An R notebook uploaded as a Python Notebook went through to the Python prompts and came back as a workflow of broken Python UDFs. **Mismatch checks.** The dashboard now checks every upload against the selected language before any model call: * Notebooks: the language comes from `metadata.kernelspec.language`, falling back to `metadata.language_info.name`, ignoring case and surrounding spaces. A notebook that declares neither is accepted as before. * Files: the extension decides. An extension no language claims keeps today's upload hint. * Folders: a folder with no files in the selected language but some in another is pointed at the right tab. Any other folder, including a mixed one, goes on to `parseFolder` and its existing checks. | Upload | Message | | --- | --- | | R notebook as a Python Notebook | This notebook uses an R kernel, not Python. Switch the notebook type to R Notebook. | | Python notebook as an R Notebook | This notebook uses a Python kernel, not R. Switch the notebook type to Python Notebook. | | Any other kernel, either type | This notebook uses a julia kernel. Only Python and R notebooks can be converted. | | R file on a Python tab | This is an R file. Upload it under the R File tab. | | Python file on an R tab | This is a Python file. Upload it under the Python File tab. | | Python folder on R Folder | This folder holds Python files, not R. Upload it under the Python Folder tab. | | R folder on Python Folder | This folder holds R files, not Python. Upload it under the R Folder tab. | When the right place is an R option and R is not enabled on the deployment, the message says so instead of pointing at a disabled control, for example "This notebook uses an R kernel, and R is not enabled on this deployment." The checks are driven by the per language table the dispatch already used, now extended with each language's kernel name, notebook type and tab names, so a later language gets them without new branches. **Script read errors.** `parseScriptFile` takes the language, so an empty or unreadable R file is reported as "The R file is empty." or "Failed to read the R file." rather than naming Python. **Error duration.** Migration errors now stay on screen for 8 seconds instead of ng-zorro's default 3, since most of them are two sentences of instructions. Errors elsewhere in the app keep the default. #### Demo video https://drive.google.com/file/d/1BkMJHuZqcvIPArxia73Dtqp3th-_y2kn/view?usp=sharing ### Any related issues, documentation, discussions? Closes #8929 Parent-issue #4301 ### How was this PR tested? * `user-workflow.component.spec.ts`, a new "language mismatches" block of 14 tests covering every row above: R and Python notebooks on the wrong type with R on and off, case and spacing in the declared language, the `language_info` fallback, an unsupported kernel on both types with the right article, R and Python files on the wrong tab with R on and off, an unknown extension, folders in either direction with R on and off, and a mixed folder and a folder with no source both reaching `parseFolder`. Existing assertions were updated to the new wording and to expect the 8 second duration, and the R folder test from #9427 now sends R files. * `notebook-migration.service.spec.ts`: the exact Python and R messages from `parseScriptFile`. The full frontend suite passes, and every new line and branch is covered. Checked against a local stack by driving the modal's own upload and submit with files carrying the mock inputs' kernels, extensions and folder paths. With R off, the four R uploads on Python options each showed the R is not enabled message. With R on, all seven mismatches showed the messages above, and a matching R notebook passed the checks and went on to the model call. No mismatch made a model request. An error toast stayed visible for about 9 seconds, the 8 second duration plus its fade out. ### Was this PR authored or co-authored using generative AI tooling? Generated-by: Claude Code (Claude Opus 5.5) --------- Co-authored-by: Meng Wang <[email protected]> --- .../user-workflow/user-workflow.component.spec.ts | 325 +++++++++++++++++++-- .../user/user-workflow/user-workflow.component.ts | 163 +++++++++-- .../service/notebook-migration/migration-llm.ts | 2 + .../notebook-migration.service.spec.ts | 13 +- .../notebook-migration.service.ts | 17 +- 5 files changed, 474 insertions(+), 46 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 4a305fb906..7e59aaa3ab 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,12 +487,270 @@ 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.", + MIGRATION_ERROR_OPTIONS + ); 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.", + MIGRATION_ERROR_OPTIONS + ); + 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.", + MIGRATION_ERROR_OPTIONS + ); + 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.", + MIGRATION_ERROR_OPTIONS + ); + }); + + 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.", + MIGRATION_ERROR_OPTIONS + ); + }); + + it("falls back to language_info when the kernelspec language is empty", async () => { + enableR(); + const { notebookConvert, errorSpy } = spies({ + cells: [], + metadata: { kernelspec: { language: "" }, 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.", + MIGRATION_ERROR_OPTIONS + ); + expect(notebookConvert).not.toHaveBeenCalled(); + }); + + it("falls back to language_info when the kernelspec language is only whitespace", async () => { + const { errorSpy } = spies({ + cells: [], + metadata: { kernelspec: { language: " " }, language_info: { name: "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.", + MIGRATION_ERROR_OPTIONS + ); + }); + + it("skips a declared language that is not a string instead of throwing", async () => { + enableR(); + const { notebookConvert, errorSpy } = spies({ + cells: [], + metadata: { kernelspec: { language: "R" }, language_info: { name: JSON.parse('{"toString": null}') } }, + }); + + 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.", + MIGRATION_ERROR_OPTIONS + ); + expect(notebookConvert).not.toHaveBeenCalled(); + }); + + it("accepts a notebook whose declared languages are all blank", async () => { + const { notebookConvert, errorSpy } = spies({ + cells: [], + metadata: { kernelspec: { language: " " }, language_info: { name: "" } }, + }); + notebookConvert.mockRejectedValue(new Error("reached the model")); + + await getRequestImport()(ipynbFile, "gpt-4", "python"); + + expect(notebookConvert).toHaveBeenCalled(); + expect(errorSpy).not.toHaveBeenCalledWith(expect.stringContaining("kernel"), expect.anything()); + }); + + 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, MIGRATION_ERROR_OPTIONS], + [message, MIGRATION_ERROR_OPTIONS], + ]); + 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.", + MIGRATION_ERROR_OPTIONS + ); + }); + + 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.", + MIGRATION_ERROR_OPTIONS + ); + 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.", + MIGRATION_ERROR_OPTIONS + ); + }); + + 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.", + MIGRATION_ERROR_OPTIONS + ); + }); + + 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.", + MIGRATION_ERROR_OPTIONS + ); + 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.", + MIGRATION_ERROR_OPTIONS + ); + 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.", + MIGRATION_ERROR_OPTIONS + ); + }); + + 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", MIGRATION_ERROR_OPTIONS); + }); + }); + describe("R file input", () => { function mockRScriptGenerationSuccess() { const migration = TestBed.inject(NotebookMigrationService); @@ -506,10 +770,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); }); @@ -523,7 +788,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(); }); }); @@ -585,7 +853,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(); }); @@ -599,7 +868,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 () => { @@ -611,7 +880,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 + ); }); }); @@ -673,7 +945,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"); @@ -704,7 +980,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(); }); @@ -717,8 +996,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 () => { @@ -729,7 +1008,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 () => { @@ -741,7 +1020,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 () => { @@ -779,7 +1058,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(); }); @@ -794,7 +1076,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(); }); @@ -810,7 +1095,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(); }); @@ -830,7 +1116,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 2294632480..a9e201f7a0 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,64 @@ 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<MigrationLanguage, { name: string; extension: string; uploadHint: string }> = { +// 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[]; + +const MIGRATION_ERROR_DURATION_MS = 8000; + +/** The language a notebook declares, lowercased. Undefined when it declares none. */ +function notebookKernelLanguage(notebook: Notebook): string | undefined { + // Each field is normalized first, so a blank or non-string kernelspec still falls back to + // language_info. Metadata is unvalidated JSON, and coercing an arbitrary object can throw. + return [notebook.metadata?.["kernelspec"]?.language, notebook.metadata?.["language_info"]?.name] + .map(value => (typeof value === "string" ? value.trim().toLowerCase() : "")) + .find(value => value !== ""); +} + +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 +351,26 @@ 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; + } + + /** + * 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<NotebookImportModalComponent, NotebookImportModalData>({ @@ -333,6 +399,11 @@ export class UserWorkflowComponent implements AfterViewInit, OnDestroy { language: MigrationLanguage ): Promise<boolean> { if (Array.isArray(selection)) { + const mismatch = this.folderLanguageMismatch(selection, language); + if (mismatch) { + this.migrationError(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 +412,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.migrationError( + fileLanguage ? this.fileLanguageMismatch(fileLanguage) : MIGRATION_INPUTS[language].uploadHint + ); return false; } @@ -369,16 +442,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 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.migrationError(mismatch); return null; } @@ -392,20 +463,70 @@ 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<GeneratedWorkflowContent | null> { - 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); + 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; } @@ -429,7 +550,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; } @@ -446,13 +567,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); } @@ -478,7 +599,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; } 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 58cf17de06..fe596fe272 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<MigrationLanguage, string> = { 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 d9d868409b..99b380af15 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 0af785e150..b139981d46 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<string> { - const source = await this.readFileAsText(file, "Python file"); + public async parseScriptFile(file: File, language: MigrationLanguage = "python"): Promise<string> { + 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; }
