Skip to content

Commit

Permalink
Merge branch 'main' into TamiTakamiya/update-wca-model-id-missing-errmsg
Browse files Browse the repository at this point in the history
  • Loading branch information
TamiTakamiya committed May 23, 2024
2 parents 44e4db1 + b1dc542 commit 6b9842e
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 129 deletions.
39 changes: 6 additions & 33 deletions packages/ansible-language-server/src/ansibleLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { getBaseUri } from "./utils/webUtils";
import {
ExplanationResponse,
GenerationResponse,
SummaryResponse,
} from "./interfaces/lightspeedApi";

/**
Expand Down Expand Up @@ -392,42 +391,14 @@ export class AnsibleLanguageService {
},
);

this.connection.onRequest(
"playbook/summary",
async (params): Promise<SummaryResponse> => {
const accessToken: string = params["accessToken"];
const URL: string = params["URL"];
const content: string = params["content"];

const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
};

const axiosInstance = axios.create({
baseURL: `${getBaseUri(URL)}/api/v0`,
headers: headers,
});

const result: SummaryResponse = await axiosInstance
.post("/ai/summaries/", {
content: content,
summaryId: uuidv4(),
})
.then((response) => {
return response.data;
});

return result;
},
);

this.connection.onRequest(
"playbook/generation",
async (params): Promise<GenerationResponse> => {
const accessToken: string = params["accessToken"];
const URL: string = params["URL"];
const content: string = params["content"];
const text: string = params["text"];
const createOutline: boolean = params["createOutline"];
const outline: string | undefined = params["outline"];

const headers = {
"Content-Type": "application/json",
Expand All @@ -441,7 +412,9 @@ export class AnsibleLanguageService {

const result: GenerationResponse = await axiosInstance
.post("/ai/generations/", {
content: content,
text,
createOutline,
outline,
generationId: uuidv4(),
})
.then((response) => {
Expand Down
10 changes: 2 additions & 8 deletions packages/ansible-language-server/src/interfaces/lightspeedApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@
* Interface for Lightspeed playbook generation/explanation APIs
*/
export interface GenerationResponse {
content: string;
format: string;
playbook: string;
outline?: string;
generationId: string;
}

export interface SummaryResponse {
content: string;
format: string;
summaryId: string;
}

export interface ExplanationResponse {
content: string;
format: string;
Expand Down
62 changes: 35 additions & 27 deletions src/features/lightspeed/playbookGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { getUri } from "../utils/getUri";
import { SettingsManager } from "../../settings";
import { isLightspeedEnabled } from "../../extension";
import { LightspeedUser } from "./lightspeedUser";
import {
GenerationResponse,
SummaryResponse,
} from "@ansible/ansible-language-server/src/interfaces/lightspeedApi";
import { GenerationResponse } from "@ansible/ansible-language-server/src/interfaces/lightspeedApi";

async function openNewPlaybookEditor(playbook: string) {
const options = {
Expand All @@ -32,7 +29,8 @@ async function openNewPlaybookEditor(playbook: string) {
}

async function generatePlaybook(
content: string,
text: string,
outline: string,
client: LanguageClient,
lightspeedAuthenticatedUser: LightspeedUser,
settingsManager: SettingsManager,
Expand All @@ -44,41 +42,46 @@ async function generatePlaybook(
panel.webview.postMessage({ command: "exception" });
}

const createOutline = false;
const playbook: GenerationResponse = await client.sendRequest(
"playbook/generation",
{
accessToken,
URL: settingsManager.settings.lightSpeedService.URL,
content,
text,
outline,
createOutline,
},
);

return playbook;
}

async function summarizeInput(
content: string,
async function generateOutline(
text: string,
client: LanguageClient,
lightspeedAuthenticatedUser: LightspeedUser,
settingsManager: SettingsManager,
panel: vscode.WebviewPanel,
): Promise<SummaryResponse> {
): Promise<GenerationResponse> {
const accessToken =
await lightspeedAuthenticatedUser.getLightspeedUserAccessToken();
if (!accessToken) {
panel.webview.postMessage({ command: "exception" });
}

const summary: SummaryResponse = await client.sendRequest(
"playbook/summary",
const createOutline = true;
const outline: GenerationResponse = await client.sendRequest(
"playbook/generation",
{
accessToken,
URL: settingsManager.settings.lightSpeedService.URL,
content,
text,
createOutline,
},
);

return summary;
return outline;
}

export async function showPlaybookGenerationPage(
Expand Down Expand Up @@ -113,28 +116,33 @@ export async function showPlaybookGenerationPage(
const command = message.command;
switch (command) {
case "generatePlaybook": {
const playbook = await generatePlaybook(
// TODO
message.content,
client,
lightspeedAuthenticatedUser,
settingsManager,
panel,
);
let playbook = message.playbook;

if (!playbook) {
const playbookResponse = await generatePlaybook(
message.text,
message.outline,
client,
lightspeedAuthenticatedUser,
settingsManager,
panel,
);
playbook = playbookResponse.playbook;
}

panel?.dispose();
await openNewPlaybookEditor(playbook.content);
await openNewPlaybookEditor(playbook);
break;
}
case "summarizeInput": {
const summary = await summarizeInput(
// TODO
message.content,
case "outline": {
const outline = await generateOutline(
message.text,
client,
lightspeedAuthenticatedUser,
settingsManager,
panel,
);
panel.webview.postMessage({ command: "summary", summary });
panel.webview.postMessage({ command: "outline", outline });
break;
}
case "thumbsUp":
Expand Down
56 changes: 34 additions & 22 deletions src/webview/apps/lightspeed/playbookGeneration/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ provideVSCodeDesignSystem().register(

const TEXTAREA_MAX_HEIGHT = 500;

let savedInput: string;
let savedInputHeight: string | undefined;
let savedSummary: string;
let savedText: string;
let savedTextHeight: string | undefined;
let savedOutline: string;
let savedPlaybook: string;
let outlineId: string | undefined;

const vscode = acquireVsCodeApi();
Expand All @@ -36,8 +37,8 @@ window.addEventListener("load", () => {

setListenerOnTextArea();

savedInput = "";
savedSummary = "";
savedText = "";
savedOutline = "";
});

window.addEventListener("message", (event) => {
Expand All @@ -49,7 +50,7 @@ window.addEventListener("message", (event) => {
element.focus();
break;
}
case "summary": {
case "outline": {
changeDisplay("spinnerContainer", "none");
changeDisplay("bigIconButtonContainer", "none");
changeDisplay("examplesContainer", "none");
Expand All @@ -62,12 +63,13 @@ window.addEventListener("message", (event) => {
updateThumbsUpDownButtons(false, false);

const element = document.getElementById("playbook-text-area") as TextArea;
savedSummary = element.value = message.summary.content;
outlineId = message.summary.summaryId;
savedOutline = element.value = message.outline.outline;
savedPlaybook = message.outline.playbook;
outlineId = message.outline.generationId;
resetTextAreaHeight();

const prompt = document.getElementById("prompt") as HTMLSpanElement;
prompt.textContent = savedInput;
prompt.textContent = savedText;

element.rows = 20;

Expand Down Expand Up @@ -101,8 +103,8 @@ function setListenerOnTextArea() {
const input = textArea.value;
submitButton.disabled = input.length === 0;

if (savedSummary) {
resetButton.disabled = savedSummary === input;
if (savedOutline) {
resetButton.disabled = savedOutline === input;
}

adjustTextAreaHeight();
Expand All @@ -120,18 +122,18 @@ function changeDisplay(className: string, displayState: string) {

async function submitInput() {
const element = document.getElementById("playbook-text-area") as TextArea;
savedInput = element.value;
savedInputHeight = getInputHeight();
savedText = element.value;
savedTextHeight = getInputHeight();

changeDisplay("spinnerContainer", "block");

vscode.postMessage({ command: "summarizeInput", content: savedInput });
vscode.postMessage({ command: "outline", text: savedText });
element.focus();
}

function reset() {
const element = document.getElementById("playbook-text-area") as TextArea;
element.value = savedSummary;
element.value = savedOutline;
element.focus();
}

Expand All @@ -145,22 +147,32 @@ function back() {
changeDisplay("promptContainer", "none");

const element = document.getElementById("playbook-text-area") as TextArea;
if (savedInput) {
element.value = savedInput;
if (savedText) {
element.value = savedText;
}
resetTextAreaHeight(savedInputHeight);
resetTextAreaHeight(savedTextHeight);
element.rows = 5;

element.focus();
}

async function generatePlaybook() {
const element = document.getElementById("playbook-text-area") as TextArea;
const content = element.value;
const text = savedText;
const outline = element.value;

// If user did not make any changes to the generated outline, use the saved playbook
// installed of calling the generations API again.
const playbook = savedOutline === outline ? savedPlaybook : undefined;

changeDisplay("spinnerContainer", "block");

vscode.postMessage({ command: "generatePlaybook", content });
vscode.postMessage({
command: "generatePlaybook",
text,
outline,
playbook,
});
}

function updateThumbsUpDownButtons(selectUp: boolean, selectDown: boolean) {
Expand Down Expand Up @@ -207,10 +219,10 @@ function getInputHeight(): string | undefined {
return textarea?.style.height;
}

function resetTextAreaHeight(savedInputHeight = "") {
function resetTextAreaHeight(savedTextHeight = "") {
const textarea = getTextAreaInShadowDOM();
if (textarea) {
textarea.style.height = savedInputHeight;
textarea.style.height = savedTextHeight;
}
}

Expand Down
25 changes: 20 additions & 5 deletions test/mockLightspeedServer/generations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@ export function generations(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
res: any,
) {
const createOutline = req.body.createOutline;
const generationId = req.body.generationId ? req.body.generationId : uuidv4();
const format = "yaml";
logger.info(req.body.content);
logger.info(req.body.text);
logger.info(req.body.outline);

// cSpell: disable
const content = `---
let outline: string | undefined = `Name: "Create an azure network..."
Description: "Create an azure network peering between VNET named VNET_1 and VNET named VNET_2"
This playbook will perform the following tass by this order:
1. Create VNET named VNET_1
2. Create VNET named VNET_2
3. Create virtual network peering
`;

const playbook = `---
# Create an azure network...
# Description: "Create an azure network peering between VNET named VNET_1 and VNET named VNET_2"
# This playbook will perform the following tass by this order:
Expand Down Expand Up @@ -50,9 +61,13 @@ export function generations(
`;
// cSpell: enable

if (!createOutline) {
outline = undefined;
}

return res.send({
content,
format,
playbook,
outline,
generationId,
});
}
Loading

0 comments on commit 6b9842e

Please sign in to comment.