Skip to content

Commit

Permalink
basicly work
Browse files Browse the repository at this point in the history
  • Loading branch information
huangjien committed May 2, 2024
1 parent 95b72e6 commit fd6498b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
72 changes: 71 additions & 1 deletion extension.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -67,9 +99,13 @@ function activate(context) {
vscode.ViewColumn.One
);
panel.webview.html = `<br/><details><summary>${jobUrl}</summary><pre>${escapeHtml(info)}</pre></details><br/>`;
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);
});

});
Expand All @@ -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 = `<div>` + panel.webview.html + `<details><summary>${model}</summary><div>${html}</div></details></div>`;
}).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) {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -52,7 +52,7 @@
},
"jenkins-log-reader.aiModelUrl": {
"type": "string",
"default": "",
"default": "http://localhost:11434/v1",
"order": 3,
"description": "Local AI Model URL"
},
Expand Down Expand Up @@ -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"
}
}

0 comments on commit fd6498b

Please sign in to comment.