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-8910-f673554f5aca4863e1ee8d652df3ba8520b847a1
in repository https://gitbox.apache.org/repos/asf/texera.git

commit 8b89d0c863ca94c7670cb4b0e7e475ee5eef1376
Author: Ryan Zhang <[email protected]>
AuthorDate: Thu Oct 29 19:27:48 2026 +0000

    refactor(notebook-migration): select prompts and operator type by source 
language (#8910)
    
    ### What changes were proposed in this PR?
    
    This is the first of three PRs adding R support to the migration tool
    (#8009). It is a pure refactor with no behavior change: Python
    conversions send the same prompts and build the same operators as
    before.
    
    The migration tool hardcodes Python in three places that R needs to
    vary: the documentation and prompts sent to the model, the operator type
    built from each generated UDF (`PythonUDFV2`), and the kernelspec of the
    notebook derived for a script. This PR gathers those into one
    `MigrationPromptSet` per source language, held in a `PROMPT_SETS` table
    that only has a Python entry for now.
    
    * `convertNotebookToWorkflow` and `convertScriptToWorkflow` take a
    `language` argument, defaulting to `"python"`, and read everything from
    the set.
    * `buildWorkflow`, `requestConversion` and `finishConversion` receive
    the operator type and notebook metadata as arguments instead of
    hardcoding them.
    * The three Python documentation arrays move from private statics on the
    class to module level constants, unchanged, so the table can reference
    them.
    * The folder path uses the Python set explicitly. R folders come in a
    later PR.
    
    `MigrationLanguage` is `"python"` only in this PR. The next PR widens it
    to `"python" | "r"` and adds the R set.
    
    ### Any related issues, documentation, discussions?
    
    Part of #8009
    Parent-issue #4301
    
    The plan for R support is in
    https://github.com/apache/texera/discussions/8866. The follow up PRs add
    the R prompts and the R option in the import modal. They are stacked on
    this one.
    
    ### How was this PR tested?
    
    Three new tests in `migration-llm.spec.ts` pin the Python output so
    later changes cannot alter it silently:
    
    * The notebook path sends exactly the existing system prelude, workflow
    prompt and mapping prompt.
    * The script path does the same with the script prelude and prompts.
    * Generated operators are built as `PythonUDFV2`.
    
    The existing notebook, script and folder tests pass unchanged. Coverage
    of `migration-llm.ts` is 100% of lines; the one uncovered branch is the
    `@Injectable()` decorator, as before this change. The full frontend
    suite passes.
    
    ### Was this PR authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Code (Claude Opus 5.5)
---
 .../notebook-migration/migration-llm.spec.ts       |  84 ++++++++++++
 .../service/notebook-migration/migration-llm.ts    | 145 ++++++++++++++-------
 2 files changed, 179 insertions(+), 50 deletions(-)

diff --git 
a/frontend/src/app/workspace/service/notebook-migration/migration-llm.spec.ts 
b/frontend/src/app/workspace/service/notebook-migration/migration-llm.spec.ts
index b8c311c919..4503417a1c 100644
--- 
a/frontend/src/app/workspace/service/notebook-migration/migration-llm.spec.ts
+++ 
b/frontend/src/app/workspace/service/notebook-migration/migration-llm.spec.ts
@@ -26,6 +26,21 @@ import {
   LlmRequestTimeoutError,
 } from "./migration-llm";
 import { buildFolderDocument } from "./folder-assembly";
+import {
+  TEXERA_OVERVIEW,
+  TUPLE_DOCUMENTATION,
+  TABLE_DOCUMENTATION,
+  OPERATOR_DOCUMENTATION,
+  EXAMPLE_OF_GOOD_CONVERSION,
+  VISUALIZER_DOCUMENTATION,
+  UDF_INPUT_PORT_DOCUMENTATION,
+  EXAMPLE_OF_MULTIPLE_UDF_CONVERSION,
+  EXAMPLE_OF_MULTIPLE_UDF_CONVERSION_SCRIPT,
+  WORKFLOW_PROMPT,
+  MAPPING_PROMPT,
+  SCRIPT_WORKFLOW_PROMPT,
+  SCRIPT_MAPPING_PROMPT,
+} from "./migration-prompts";
 import { GuiConfigService } from "../../../common/service/gui-config.service";
 import { WorkflowUtilService } from 
"../workflow-graph/util/workflow-util.service";
 import { AuthService } from "../../../common/service/user/auth.service";
@@ -316,6 +331,75 @@ describe("NotebookMigrationLLM", () => {
     });
   });
 
+  describe("Python prompts", () => {
+    const workflowResponse = JSON.stringify({ code: { UDF1: "# UDF1" }, edges: 
[], outputs: {} });
+    const documentation = [
+      TEXERA_OVERVIEW,
+      TUPLE_DOCUMENTATION,
+      TABLE_DOCUMENTATION,
+      OPERATOR_DOCUMENTATION,
+      EXAMPLE_OF_GOOD_CONVERSION,
+      VISUALIZER_DOCUMENTATION,
+      UDF_INPUT_PORT_DOCUMENTATION,
+      EXAMPLE_OF_MULTIPLE_UDF_CONVERSION,
+    ];
+
+    // Every call shares one messages array, so the first call's argument 
holds the whole conversation.
+    function contentsFor(role: string): string[] {
+      return (callModelSpy.mock.calls[0][0] as { role: string; content: string 
}[])
+        .filter(message => message.role === role)
+        .map(message => message.content);
+    }
+
+    it("sends the notebook prelude and prompts unchanged", async () => {
+      mockResponses(workflowResponse, JSON.stringify({ UDF1: ["CELL1"] }));
+
+      await makeLLM().convertNotebookToWorkflow({ cells: [codeCell("CELL1", 
"print(1)")] });
+
+      expect(contentsFor("system")).toEqual(documentation);
+      const [workflowPrompt, mappingPrompt] = contentsFor("user");
+      expect(workflowPrompt.startsWith(`${WORKFLOW_PROMPT}\n`)).toBe(true);
+      expect(mappingPrompt).toBe(MAPPING_PROMPT);
+    });
+
+    it("sends the script prelude and prompts unchanged", async () => {
+      mockResponses(workflowResponse, JSON.stringify({ UDF1: [[1, 1]] }));
+
+      await makeLLM().convertScriptToWorkflow("print(1)");
+
+      expect(contentsFor("system")).toEqual(
+        documentation.map(doc =>
+          doc === EXAMPLE_OF_MULTIPLE_UDF_CONVERSION ? 
EXAMPLE_OF_MULTIPLE_UDF_CONVERSION_SCRIPT : doc
+        )
+      );
+      const [workflowPrompt, mappingPrompt] = contentsFor("user");
+      
expect(workflowPrompt.startsWith(`${SCRIPT_WORKFLOW_PROMPT}\n`)).toBe(true);
+      expect(mappingPrompt).toBe(SCRIPT_MAPPING_PROMPT);
+    });
+
+    it("builds PythonUDFV2 operators", async () => {
+      mockResponses(workflowResponse, JSON.stringify({ UDF1: [[1, 1]] }));
+
+      const { workflowJSON } = await 
makeLLM().convertScriptToWorkflow("print(1)");
+
+      
expect(stubUtil.getNewOperatorPredicate).toHaveBeenCalledWith("PythonUDFV2", 
"UDF1");
+      expect(workflowJSON.operators[0].operatorType).toBe("PythonUDFV2");
+    });
+
+    it("gives each derived notebook its own metadata", async () => {
+      const llm = makeLLM();
+      mockResponses(workflowResponse, JSON.stringify({ UDF1: [[1, 1]] }));
+      const first = await llm.convertScriptToWorkflow("print(1)");
+      first.notebook.metadata!["kernelspec"].name = "changed";
+
+      mockResponses(workflowResponse, JSON.stringify({ UDF1: [[1, 1]] }));
+      const second = await llm.convertScriptToWorkflow("print(1)");
+
+      expect(second.notebook.metadata).not.toBe(first.notebook.metadata);
+      expect(second.notebook.metadata!["kernelspec"].name).toBe("python3");
+    });
+  });
+
   describe("parseJsonResponse", () => {
     // parseJsonResponse is private; cast to access it directly for focused 
coverage.
     const parse = (raw: string) => (makeLLM() as any).parseJsonResponse(raw, 
"workflow");
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 6a98e21503..f20a4c5700 100644
--- a/frontend/src/app/workspace/service/notebook-migration/migration-llm.ts
+++ b/frontend/src/app/workspace/service/notebook-migration/migration-llm.ts
@@ -102,7 +102,7 @@ function numberScriptLines(source: string): string {
 
 // Wrap derived cells as a notebook. The nbformat fields are what make it 
openable in Jupyter,
 // and metadata.uuid is the join key the stored mapping is expressed in, same 
as for a real .ipynb.
-function toDerivedNotebook(cells: DerivedCell[]): Notebook {
+function toDerivedNotebook(cells: DerivedCell[], metadata: 
Notebook["metadata"]): Notebook {
   return {
     cells: cells.map(cell => ({
       cell_type: "code",
@@ -111,15 +111,66 @@ function toDerivedNotebook(cells: DerivedCell[]): 
Notebook {
       outputs: [],
       execution_count: null,
     })),
-    metadata: {
-      kernelspec: { display_name: "Python 3", language: "python", name: 
"python3" },
-      language_info: { name: "python" },
-    },
+    metadata: structuredClone(metadata),
     nbformat: 4,
     nbformat_minor: 4,
   };
 }
 
+const PYTHON_NOTEBOOK_DOCUMENTATION: string[] = [
+  TEXERA_OVERVIEW,
+  TUPLE_DOCUMENTATION,
+  TABLE_DOCUMENTATION,
+  OPERATOR_DOCUMENTATION,
+  EXAMPLE_OF_GOOD_CONVERSION,
+  VISUALIZER_DOCUMENTATION,
+  UDF_INPUT_PORT_DOCUMENTATION,
+  EXAMPLE_OF_MULTIPLE_UDF_CONVERSION,
+];
+
+// The script prelude differs in exactly one entry. The notebook worked 
example is written in
+// `# START CELL1` form, and a system-message example of that weight would 
push the model to
+// answer in cell ids for an input that has no cells. The notebook array is 
left untouched so
+// existing conversions see byte-identical context.
+const PYTHON_SCRIPT_DOCUMENTATION: string[] = 
PYTHON_NOTEBOOK_DOCUMENTATION.map(doc =>
+  doc === EXAMPLE_OF_MULTIPLE_UDF_CONVERSION ? 
EXAMPLE_OF_MULTIPLE_UDF_CONVERSION_SCRIPT : doc
+);
+
+// Differs from the script prelude in the same single entry: its worked 
example shows banner
+// lines, an entry point calling into other files, and definitions inlined 
rather than imported.
+const PYTHON_FOLDER_DOCUMENTATION: string[] = 
PYTHON_SCRIPT_DOCUMENTATION.map(doc =>
+  doc === EXAMPLE_OF_MULTIPLE_UDF_CONVERSION_SCRIPT ? 
EXAMPLE_OF_MULTIPLE_UDF_CONVERSION_FOLDER : doc
+);
+
+export type MigrationLanguage = "python";
+
+interface MigrationPromptSet {
+  notebookDocumentation: string[];
+  notebookWorkflowPrompt: string;
+  notebookMappingPrompt: string;
+  scriptDocumentation: string[];
+  scriptWorkflowPrompt: string;
+  scriptMappingPrompt: string;
+  operatorType: string;
+  notebookMetadata: Notebook["metadata"];
+}
+
+const PROMPT_SETS: Record<MigrationLanguage, MigrationPromptSet> = {
+  python: {
+    notebookDocumentation: PYTHON_NOTEBOOK_DOCUMENTATION,
+    notebookWorkflowPrompt: WORKFLOW_PROMPT,
+    notebookMappingPrompt: MAPPING_PROMPT,
+    scriptDocumentation: PYTHON_SCRIPT_DOCUMENTATION,
+    scriptWorkflowPrompt: SCRIPT_WORKFLOW_PROMPT,
+    scriptMappingPrompt: SCRIPT_MAPPING_PROMPT,
+    operatorType: "PythonUDFV2",
+    notebookMetadata: {
+      kernelspec: { display_name: "Python 3", language: "python", name: 
"python3" },
+      language_info: { name: "python" },
+    },
+  },
+};
+
 /**
  * Wraps a single LLM chat session that converts a Jupyter notebook or a 
Python script into
  * a Texera workflow plus a cell<->operator mapping.
@@ -175,31 +226,6 @@ export class NotebookMigrationLLM {
   private messages: ModelMessage[] = [];
   private initialized = false;
 
-  private static readonly DOCUMENTATION: string[] = [
-    TEXERA_OVERVIEW,
-    TUPLE_DOCUMENTATION,
-    TABLE_DOCUMENTATION,
-    OPERATOR_DOCUMENTATION,
-    EXAMPLE_OF_GOOD_CONVERSION,
-    VISUALIZER_DOCUMENTATION,
-    UDF_INPUT_PORT_DOCUMENTATION,
-    EXAMPLE_OF_MULTIPLE_UDF_CONVERSION,
-  ];
-
-  // The script prelude differs in exactly one entry. The notebook worked 
example is written in
-  // `# START CELL1` form, and a system-message example of that weight would 
push the model to
-  // answer in cell ids for an input that has no cells. The notebook array is 
left untouched so
-  // existing conversions see byte-identical context.
-  private static readonly SCRIPT_DOCUMENTATION: string[] = 
NotebookMigrationLLM.DOCUMENTATION.map(doc =>
-    doc === EXAMPLE_OF_MULTIPLE_UDF_CONVERSION ? 
EXAMPLE_OF_MULTIPLE_UDF_CONVERSION_SCRIPT : doc
-  );
-
-  // Differs from the script prelude in the same single entry: its worked 
example shows banner
-  // lines, an entry point calling into other files, and definitions inlined 
rather than imported.
-  private static readonly FOLDER_DOCUMENTATION: string[] = 
NotebookMigrationLLM.SCRIPT_DOCUMENTATION.map(doc =>
-    doc === EXAMPLE_OF_MULTIPLE_UDF_CONVERSION_SCRIPT ? 
EXAMPLE_OF_MULTIPLE_UDF_CONVERSION_FOLDER : doc
-  );
-
   constructor(
     private config: GuiConfigService,
     private workflowUtilService: WorkflowUtilService
@@ -220,7 +246,7 @@ export class NotebookMigrationLLM {
    * conversation. Used by initialize() and at the start of each conversion, 
which is where
    * the input-specific variant is chosen.
    */
-  private seedDocumentation(documentation: string[] = 
NotebookMigrationLLM.DOCUMENTATION): void {
+  private seedDocumentation(documentation: string[] = 
PYTHON_NOTEBOOK_DOCUMENTATION): void {
     this.messages = documentation.map(
       (doc): ModelMessage => ({
         role: "system",
@@ -361,15 +387,16 @@ export class NotebookMigrationLLM {
   /**
    * Send a Jupyter Notebook to be converted into a workflow and mapping.
    */
-  public async convertNotebookToWorkflow(notebook: Notebook): Promise<string> {
+  public async convertNotebookToWorkflow(notebook: Notebook, language: 
MigrationLanguage = "python"): Promise<string> {
     this.assertEnabled();
     if (!this.initialized) {
       throw new Error("LLM session not initialized");
     }
 
+    const prompts = PROMPT_SETS[language];
     // Reset to the documentation prelude so a prior conversion's 
prompts/responses
     // don't leak into this one. The two sendPrompt calls below still share 
history.
-    this.seedDocumentation(NotebookMigrationLLM.DOCUMENTATION);
+    this.seedDocumentation(prompts.notebookDocumentation);
 
     const codeCells = notebook.cells.filter(cell => cell.cell_type === "code");
 
@@ -389,12 +416,12 @@ export class NotebookMigrationLLM {
       })
       .join("\n\n");
 
-    const workflow = await 
this.sendPrompt(`${WORKFLOW_PROMPT}\n${notebookString}`);
-    const mapping = await this.sendPrompt(MAPPING_PROMPT);
+    const workflow = await 
this.sendPrompt(`${prompts.notebookWorkflowPrompt}\n${notebookString}`);
+    const mapping = await this.sendPrompt(prompts.notebookMappingPrompt);
 
     // Remove ```json blocks and parse
     const udfLLMResponse = this.parseJsonResponse(workflow, "workflow");
-    const { workflowJSON, udfIdToOperatorId } = 
this.buildWorkflow(udfLLMResponse);
+    const { workflowJSON, udfIdToOperatorId } = 
this.buildWorkflow(udfLLMResponse, prompts.operatorType);
 
     // The notebook path keys its mapping on the cell uuids embedded in the 
prompt.
     const parsedMapping: Record<string, string[]> = 
this.parseJsonResponse(mapping, "mapping");
@@ -411,22 +438,32 @@ export class NotebookMigrationLLM {
    * rather than cell markers, and the model is asked which line ranges became 
which UDF; the
    * cells are then derived from that answer instead of arriving with the 
input.
    */
-  public async convertScriptToWorkflow(source: string): 
Promise<SourceConversion> {
+  public async convertScriptToWorkflow(
+    source: string,
+    language: MigrationLanguage = "python"
+  ): Promise<SourceConversion> {
     this.assertEnabled();
     if (!this.initialized) {
       throw new Error("LLM session not initialized");
     }
 
+    const prompts = PROMPT_SETS[language];
     const { workflowJSON, udfIdToOperatorId, reportedRanges } = await 
this.requestConversion(
-      NotebookMigrationLLM.SCRIPT_DOCUMENTATION,
-      SCRIPT_WORKFLOW_PROMPT,
-      SCRIPT_MAPPING_PROMPT,
+      prompts.scriptDocumentation,
+      prompts.scriptWorkflowPrompt,
+      prompts.scriptMappingPrompt,
+      prompts.operatorType,
       source
     );
 
     // segmentScript reconciles whatever the model reported, so a malformed 
range degrades the
     // mapping rather than discarding a workflow that already cost a full 
conversion.
-    return this.finishConversion(workflowJSON, udfIdToOperatorId, 
segmentScript(source, reportedRanges));
+    return this.finishConversion(
+      workflowJSON,
+      udfIdToOperatorId,
+      segmentScript(source, reportedRanges),
+      prompts.notebookMetadata
+    );
   }
 
   /**
@@ -444,10 +481,12 @@ export class NotebookMigrationLLM {
       throw new Error("LLM session not initialized");
     }
 
+    const prompts = PROMPT_SETS.python;
     const { workflowJSON, udfIdToOperatorId, workflowResponse, reportedRanges 
} = await this.requestConversion(
-      NotebookMigrationLLM.FOLDER_DOCUMENTATION,
+      PYTHON_FOLDER_DOCUMENTATION,
       FOLDER_WORKFLOW_PROMPT,
       FOLDER_MAPPING_PROMPT,
+      prompts.operatorType,
       document.source,
       // The layout is prompt text, never part of the numbered document: 
numbering it would shift
       // every line the model reports, and the segmenter would emit it as a 
cell of directory
@@ -465,7 +504,8 @@ export class NotebookMigrationLLM {
     return this.finishConversion(
       workflowJSON,
       udfIdToOperatorId,
-      entryPoint ? scopeSegmentationToSpan(segmentation, entryPoint) : 
segmentation
+      entryPoint ? scopeSegmentationToSpan(segmentation, entryPoint) : 
segmentation,
+      prompts.notebookMetadata
     );
   }
 
@@ -478,6 +518,7 @@ export class NotebookMigrationLLM {
     documentation: string[],
     workflowPrompt: string,
     mappingPrompt: string,
+    operatorType: string,
     source: string,
     preamble?: string
   ): Promise<{
@@ -493,7 +534,7 @@ export class NotebookMigrationLLM {
     const mapping = await this.sendPrompt(mappingPrompt);
 
     const workflowResponse = this.parseJsonResponse(workflow, "workflow");
-    const { workflowJSON, udfIdToOperatorId } = 
this.buildWorkflow(workflowResponse);
+    const { workflowJSON, udfIdToOperatorId } = 
this.buildWorkflow(workflowResponse, operatorType);
 
     return {
       workflowJSON,
@@ -507,12 +548,13 @@ export class NotebookMigrationLLM {
   private finishConversion(
     workflowJSON: WorkflowJSON,
     udfIdToOperatorId: Record<string, string>,
-    segmentation: ScriptSegmentation
+    segmentation: ScriptSegmentation,
+    notebookMetadata: Notebook["metadata"]
   ): SourceConversion {
     return {
       workflowJSON,
       workflowNotebookMapping: 
this.buildCombinedMapping(segmentation.udfToCellUuids, udfIdToOperatorId),
-      notebook: toDerivedNotebook(segmentation.cells),
+      notebook: toDerivedNotebook(segmentation.cells, notebookMetadata),
     };
   }
 
@@ -524,7 +566,10 @@ export class NotebookMigrationLLM {
    *
    * Returns the workflow together with the UDF id -> operatorID index the 
mapping is built from.
    */
-  private buildWorkflow(udfLLMResponse: any): {
+  private buildWorkflow(
+    udfLLMResponse: any,
+    operatorType: string
+  ): {
     workflowJSON: WorkflowJSON;
     udfIdToOperatorId: Record<string, string>;
   } {
@@ -556,9 +601,9 @@ export class NotebookMigrationLLM {
         }));
       }
 
-      // Build the operator from the live PythonUDFV2 schema so the 
operatorVersion, ports, and
-      // property defaults track the backend definition, then overlay the 
generated code/outputs.
-      const base = 
this.workflowUtilService.getNewOperatorPredicate("PythonUDFV2", udfId);
+      // Build the operator from the live schema so the operatorVersion, 
ports, and property
+      // defaults track the backend definition, then overlay the generated 
code/outputs.
+      const base = 
this.workflowUtilService.getNewOperatorPredicate(operatorType, udfId);
       const operator: OperatorPredicate = {
         ...base,
         operatorProperties: {

Reply via email to