mengw15 commented on code in PR #5262: URL: https://github.com/apache/texera/pull/5262#discussion_r3483437611
########## frontend/src/app/workspace/service/notebook-migration/notebook-migration.service.ts: ########## @@ -0,0 +1,223 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { Injectable } from "@angular/core"; +import { AppSettings } from "../../../common/app-setting"; +import { Notebook, NotebookMigrationLLM } from "./migration-llm"; +import { HttpClient, HttpHeaders } from "@angular/common/http"; +import { NotificationService } from "src/app/common/service/notification/notification.service"; +import { GuiConfigService } from "../../../common/service/gui-config.service"; +import { WorkflowUtilService } from "../workflow-graph/util/workflow-util.service"; +import { catchError, firstValueFrom, map, Observable, of } from "rxjs"; + +interface LiteLLMModel { + id: string; + object: string; + created: number; + owned_by: string; +} + +interface LiteLLMModelsResponse { + data: LiteLLMModel[]; + object: string; +} + +interface MappingContent { + cell_to_operator: Record<string, string[]>; + operator_to_cell: Record<string, string[]>; +} + +interface StoreNotebookResponse { + success: boolean; + message: string; +} + +@Injectable({ + providedIn: "root", +}) +export class NotebookMigrationService { + private mapping: { [key: string]: MappingContent } = {}; + + constructor( + private http: HttpClient, + private notificationService: NotificationService, + private config: GuiConfigService, + private workflowUtilService: WorkflowUtilService + ) {} + + private get enabled(): boolean { + return this.config.env.pythonNotebookMigrationEnabled; + } + + public getAvailableModels(): Observable<{ name: string }[]> { + if (!this.enabled) return of([]); + return this.http.get<LiteLLMModelsResponse>(`${AppSettings.getApiEndpoint()}/models`).pipe( + map(response => + response.data.map(model => ({ + name: model.id, + })) + ), + catchError((err: unknown) => { + console.error("Failed to fetch models", err); + return of([]); + }) + ); + } + + public async sendToAIGenerateWorkflow(notebookContent: Notebook, modelType: string) { + if (!this.enabled) throw new Error("Notebook migration feature is disabled"); + const migrationLLM = this.createMigrationLLM(); + // initialize() defaults to the user's Texera JWT via AuthService.getAccessToken(). + // The outer try/finally guarantees close() runs for the whole lifecycle, + // including a verifyConnection failure. + try { + migrationLLM.initialize(modelType); + + const isValid = await migrationLLM.verifyConnection(); + if (!isValid) { + throw new Error("Unable to authenticate with or reach the LLM backend"); + } + + try { + const result = await migrationLLM.convertNotebookToWorkflow(notebookContent); + const parsedResult = JSON.parse(result); + const workflowContent = parsedResult.workflowJSON; + const mappingContent = parsedResult.workflowNotebookMapping; + return { workflowContent, mappingContent }; + } catch (error) { + console.error("Error converting notebook:", error); + throw error; + } + } finally { + migrationLLM.close(); + } + } + + // Factory seam for the LLM client. Extracted so specs can override it to supply + // a fake, keeping the real NotebookMigrationLLM (and its `ai` transport) out of + // the test module graph. A new instance is created per conversion. + protected createMigrationLLM(): NotebookMigrationLLM { + return new NotebookMigrationLLM(this.config, this.workflowUtilService); + } + + public async sendNotebookToJupyter(notebookData: Notebook) { + if (!this.enabled) return 0; + const jupyterAPIUrl = `${AppSettings.getApiEndpoint()}/notebook-migration/set-notebook`; + + const requestBody = { + notebookName: "notebook.ipynb", + notebookData: notebookData, + }; Review Comment: the notebookName here is hardcoded. which could be fine with version 1 design, if we want to go with global notebook migration service, here need some change ########## frontend/src/app/workspace/service/notebook-migration/notebook-migration.service.ts: ########## @@ -0,0 +1,223 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { Injectable } from "@angular/core"; +import { AppSettings } from "../../../common/app-setting"; +import { Notebook, NotebookMigrationLLM } from "./migration-llm"; +import { HttpClient, HttpHeaders } from "@angular/common/http"; +import { NotificationService } from "src/app/common/service/notification/notification.service"; +import { GuiConfigService } from "../../../common/service/gui-config.service"; +import { WorkflowUtilService } from "../workflow-graph/util/workflow-util.service"; +import { catchError, firstValueFrom, map, Observable, of } from "rxjs"; + +interface LiteLLMModel { + id: string; + object: string; + created: number; + owned_by: string; +} + +interface LiteLLMModelsResponse { + data: LiteLLMModel[]; + object: string; +} + +interface MappingContent { + cell_to_operator: Record<string, string[]>; + operator_to_cell: Record<string, string[]>; +} + +interface StoreNotebookResponse { + success: boolean; + message: string; +} + +@Injectable({ + providedIn: "root", +}) +export class NotebookMigrationService { + private mapping: { [key: string]: MappingContent } = {}; + + constructor( + private http: HttpClient, + private notificationService: NotificationService, + private config: GuiConfigService, + private workflowUtilService: WorkflowUtilService + ) {} + + private get enabled(): boolean { + return this.config.env.pythonNotebookMigrationEnabled; + } + + public getAvailableModels(): Observable<{ name: string }[]> { + if (!this.enabled) return of([]); + return this.http.get<LiteLLMModelsResponse>(`${AppSettings.getApiEndpoint()}/models`).pipe( + map(response => + response.data.map(model => ({ + name: model.id, + })) + ), + catchError((err: unknown) => { + console.error("Failed to fetch models", err); + return of([]); + }) + ); + } + + public async sendToAIGenerateWorkflow(notebookContent: Notebook, modelType: string) { + if (!this.enabled) throw new Error("Notebook migration feature is disabled"); + const migrationLLM = this.createMigrationLLM(); + // initialize() defaults to the user's Texera JWT via AuthService.getAccessToken(). + // The outer try/finally guarantees close() runs for the whole lifecycle, + // including a verifyConnection failure. + try { + migrationLLM.initialize(modelType); + + const isValid = await migrationLLM.verifyConnection(); + if (!isValid) { + throw new Error("Unable to authenticate with or reach the LLM backend"); + } + + try { + const result = await migrationLLM.convertNotebookToWorkflow(notebookContent); + const parsedResult = JSON.parse(result); + const workflowContent = parsedResult.workflowJSON; + const mappingContent = parsedResult.workflowNotebookMapping; + return { workflowContent, mappingContent }; + } catch (error) { + console.error("Error converting notebook:", error); + throw error; + } + } finally { + migrationLLM.close(); + } + } + + // Factory seam for the LLM client. Extracted so specs can override it to supply + // a fake, keeping the real NotebookMigrationLLM (and its `ai` transport) out of + // the test module graph. A new instance is created per conversion. + protected createMigrationLLM(): NotebookMigrationLLM { + return new NotebookMigrationLLM(this.config, this.workflowUtilService); + } + + public async sendNotebookToJupyter(notebookData: Notebook) { + if (!this.enabled) return 0; + const jupyterAPIUrl = `${AppSettings.getApiEndpoint()}/notebook-migration/set-notebook`; + + const requestBody = { + notebookName: "notebook.ipynb", + notebookData: notebookData, + }; + + const headers = new HttpHeaders({ + "Content-Type": "application/json", + }); + + try { + await firstValueFrom(this.http.post(jupyterAPIUrl, requestBody, { headers })); + this.notificationService.success("Notebook successfully sent to Jupyter"); + return 1; + } catch (error) { + console.error("Error sending notebook to pod: ", error); + const message = error instanceof Error ? error.message : String(error); + this.notificationService.error("Error sending notebook to Jupyter: " + message); + return 0; + } + } + + public async getJupyterURL(): Promise<string | null> { Review Comment: do we need this function? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
