|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { Workunit } from "@hpcc-js/comms"; |
| 3 | +import { isPlatformConnected } from "../../../hpccplatform/session"; |
| 4 | +import { reporter } from "../../../telemetry"; |
| 5 | +import localize from "../../../util/localize"; |
| 6 | +import { createServiceOptions, logToolEvent, requireConnectedSession, throwIfCancellationRequested } from "../utils/index"; |
| 7 | + |
| 8 | +export interface IGetWorkunitECLParameters { |
| 9 | + wuid: string; |
| 10 | +} |
| 11 | + |
| 12 | +export class GetWorkunitECLTool implements vscode.LanguageModelTool<IGetWorkunitECLParameters> { |
| 13 | + async invoke(options: vscode.LanguageModelToolInvocationOptions<IGetWorkunitECLParameters>, token: vscode.CancellationToken) { |
| 14 | + reporter?.sendTelemetryEvent("lmTool.invoke", { tool: "getWorkunitECL" }); |
| 15 | + const params = options.input; |
| 16 | + |
| 17 | + const wuid = typeof params.wuid === "string" ? params.wuid.trim() : ""; |
| 18 | + if (wuid.length === 0) { |
| 19 | + throw new vscode.LanguageModelError(localize("WUID is required"), { cause: "invalid_parameters" }); |
| 20 | + } |
| 21 | + |
| 22 | + logToolEvent("getWorkunitECL", "invoke start", { wuid }); |
| 23 | + |
| 24 | + const session = requireConnectedSession(); |
| 25 | + const opts = await createServiceOptions(session); |
| 26 | + |
| 27 | + try { |
| 28 | + const wu = Workunit.attach(opts, wuid); |
| 29 | + await wu.refresh(); |
| 30 | + |
| 31 | + throwIfCancellationRequested(token); |
| 32 | + |
| 33 | + const parts: vscode.LanguageModelTextPart[] = []; |
| 34 | + |
| 35 | + const detailsUrl = session.wuDetailsUrl(wu.Wuid); |
| 36 | + parts.push(new vscode.LanguageModelTextPart(localize("ECL Source for Workunit {0}:", wuid))); |
| 37 | + |
| 38 | + const summary = localize( |
| 39 | + "Workunit {0} on {1} ({2})", |
| 40 | + wu.Wuid, |
| 41 | + wu.Cluster || localize("unknown cluster"), |
| 42 | + wu.State || localize("unknown state") |
| 43 | + ); |
| 44 | + parts.push(new vscode.LanguageModelTextPart(summary)); |
| 45 | + if (detailsUrl) { |
| 46 | + parts.push(new vscode.LanguageModelTextPart(`${localize("ECL Watch URL:")} ${detailsUrl}`)); |
| 47 | + } |
| 48 | + |
| 49 | + throwIfCancellationRequested(token); |
| 50 | + const eclArchive = await wu.fetchArchive().catch(() => ""); |
| 51 | + |
| 52 | + if (eclArchive) { |
| 53 | + parts.push(new vscode.LanguageModelTextPart(localize("\nECL Archive (XML format):"))); |
| 54 | + parts.push(new vscode.LanguageModelTextPart("```xml\n" + eclArchive + "\n```")); |
| 55 | + } else { |
| 56 | + parts.push(new vscode.LanguageModelTextPart(localize("No ECL archive available for this workunit."))); |
| 57 | + } |
| 58 | + |
| 59 | + logToolEvent("getWorkunitECL", "invoke success", { |
| 60 | + wuid: wu.Wuid, |
| 61 | + state: wu.State, |
| 62 | + hasECL: !!eclArchive, |
| 63 | + }); |
| 64 | + |
| 65 | + return new vscode.LanguageModelToolResult(parts); |
| 66 | + } catch (error) { |
| 67 | + const errorMessage = error instanceof Error ? error.message : String(error); |
| 68 | + logToolEvent("getWorkunitECL", "invoke failed", { wuid, error: errorMessage }); |
| 69 | + throw new vscode.LanguageModelError( |
| 70 | + localize("Failed to fetch workunit ECL: {0}", errorMessage), |
| 71 | + { cause: error } |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + async prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<IGetWorkunitECLParameters>, _token: vscode.CancellationToken) { |
| 77 | + const connected = isPlatformConnected(); |
| 78 | + const wuid = typeof options.input.wuid === "string" ? options.input.wuid.trim() : ""; |
| 79 | + |
| 80 | + return { |
| 81 | + invocationMessage: connected |
| 82 | + ? localize("Fetching ECL source for workunit {0}", wuid || localize("(unspecified)")) |
| 83 | + : localize("Cannot fetch: HPCC Platform not connected"), |
| 84 | + confirmationMessages: connected ? undefined : { |
| 85 | + title: localize("HPCC Platform not connected"), |
| 86 | + message: new vscode.MarkdownString(localize("This tool requires an active HPCC connection.")), |
| 87 | + } |
| 88 | + }; |
| 89 | + } |
| 90 | +} |
0 commit comments