From fd6498b7ecb13b433957e9b68fdc7d398efcb20a Mon Sep 17 00:00:00 2001 From: huangjien Date: Thu, 2 May 2024 21:32:59 +0100 Subject: [PATCH] basicly work --- extension.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++- package.json | 7 ++--- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/extension.js b/extension.js index a85e15d..bd9daa8 100644 --- a/extension.js +++ b/extension.js @@ -1,7 +1,11 @@ // The module 'vscode' contains the VS Code extensibility API + + // Import the module and reference it with the alias vscode in your code below const vscode = require('vscode'); const axios = require('axios'); +const OpenAI = require('openai') +const Showdown = require('showdown') async function fetchJenkinsLog(jobUrl, username, apiToken) { const auth = Buffer.from(`${username}:${apiToken}`).toString('base64'); @@ -34,6 +38,9 @@ function activate(context) { let disposable = vscode.commands.registerCommand( 'jenkins-log-reader.readJenkinsLog', async () => { + + const converter = new Showdown.Converter(); + // You could prompt the user for these or use configuration settings // const jobUrl = vscode.workspace // .getConfiguration() @@ -52,12 +59,37 @@ function activate(context) { return; } + const localAiUrl = vscode.workspace + .getConfiguration() + .get('jenkins-log-reader.aiModelUrl'); + + const model = vscode.workspace + .getConfiguration() + .get('jenkins-log-reader.aiModel'); + + const prompt = vscode.workspace + .getConfiguration() + .get('jenkins-log-reader.aiPrompt'); + + if (!localAiUrl || !model) { + vscode.window.showInformationMessage( + 'Please configure your Local AI settings.' + ); + return; + } + + let localAi = new OpenAI.OpenAI({ + baseURL: localAiUrl, + apiKey: model + }); + await vscode.window.showInputBox({ placeHolder: 'Enter the Jenkins job URL, e.g., http://jenkins.local/job/my-job' }).then((jobUrl) => { if (!jobUrl) { return; } + fetchJenkinsLog(jobUrl, username, apiToken).then(log => { if (log) { const info = keepLongTail(log) @@ -67,9 +99,13 @@ function activate(context) { vscode.ViewColumn.One ); panel.webview.html = `
${jobUrl}
${escapeHtml(info)}

`; + const promptString = prompt.replace('$PROMPT$', info); // analyse with local AI - // await OpenAI.chat.completion + const longRunTask = aiAnalyse(localAi, model, promptString, panel, converter); + showStatusBarProgress(longRunTask); } + }).catch((err) => { + vscode.window.showErrorMessage(err.message); }); }); @@ -80,6 +116,40 @@ function activate(context) { context.subscriptions.push(disposable); } +function showStatusBarProgress(task) { + vscode.window.withProgress( + { + location: vscode.ProgressLocation.Notification, + title: 'Processing AI Analysis...', + cancellable: true, // Set to true if you want to allow cancelling the task + }, + () => { + return task; // The progress UI will show until this Promise resolves + } + ); +} + + +async function aiAnalyse(localAi, model, promptString, panel, converter) { + localAi.chat.completions.create({ + model: model, + messages: [{ role: 'assistant', content: promptString }], + temperature: 0.8, + max_tokens: 8192, + }).then(data => { + return JSON.stringify(data); + }).then(data => { + const evalContent = JSON.parse(data); + console.log(evalContent) + const infomation = evalContent.choices[0]['message']['content']; + console.log(infomation); + const html = converter.makeHtml(infomation); + panel.webview.html = `
` + panel.webview.html + `
${model}
${html}
`; + }).catch((err) => { + vscode.window.showErrorMessage(err.message); + }); +} + // sometimes, the log is too long. we believe that 5k should be enough. function keepLongTail(inputString) { if (inputString.length > 5120) { diff --git a/package.json b/package.json index 22d6840..5cadfe0 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "jenkins-log-reader", "displayName": "jenkins-log-reader", "description": "read jenkins log, analyse with local AI.", - "version": "0.0.12", + "version": "0.0.13", "engines": { "vscode": "^1.88.0" }, @@ -52,7 +52,7 @@ }, "jenkins-log-reader.aiModelUrl": { "type": "string", - "default": "", + "default": "http://localhost:11434/v1", "order": 3, "description": "Local AI Model URL" }, @@ -110,6 +110,7 @@ "axios": "^1.6.8", "crypto": "^1.0.1", "husky": "^9.0.11", - "openai": "^4.40.0" + "openai": "^4.40.0", + "showdown": "^2.1.0" } }