zyratlo commented on code in PR #5262: URL: https://github.com/apache/texera/pull/5262#discussion_r3482795556
########## frontend/src/app/workspace/service/notebook-migration/notebook-migration.service.ts: ########## @@ -0,0 +1,212 @@ +/** + * 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: { [key: string]: any }; + operator_to_cell: { [key: string]: any }; +} + +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 = new NotebookMigrationLLM(this.config, this.workflowUtilService); + // initialize() defaults to the user's Texera JWT via AuthService.getAccessToken(). + 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(); + } + } Review Comment: The missing `apiKey` parameter is intentional. The `/api/chat/*` LiteLLM proxy is now `@RolesAllowed("REGULAR","ADMIN")` (#5421), so authentication uses the caller's Texera JWT, not a user-supplied key. `initialize()` defaults its access token to `AuthService.getAccessToken()`, so the service no longer threads a key through. The old `apiKey: "dummy"` path now returns 401 and was removed during reconciliation with the updated LLM client (#5260). The lifecycle point is valid and fixed: `verifyConnection()` now runs inside the outer try, so close() always runs in the finally, including on a verification failure. Refactored in [abe65b9](https://github.com/apache/texera/pull/5262/commits/abe65b99f9605d9cc8b5a135cc1d1eb6229ac79f) -- 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]
