Skip to content

Commit

Permalink
Fix for multi-task and single-task lightspeed suggestions (#992)
Browse files Browse the repository at this point in the history
* Fix for multi-task and single-task lightspeed suggestions

*  Remove whitespaces from blank lines in between multi-task suggestion
*  Show user information message is model Id is invalid.
*  If file is under playbook folder don't trigger single-task
   suggestion in play context.
*  Add information message for user if both Github copilot for Ansible
   and Lightspeed setting in enabled.
*  Don't trigger single-task suggestion if `- name: <descrption>`
   pattern if found under `vars` context.
*  Remove `ansible_type` field from content matches display
   as it is no longer used.
*  If user is not logged in and lightspeed setting is enabled
   show login message for mulit-task suggestion trigger request.

* remove ansible_type field from content match params

* update log message

---------

Co-authored-by: Ansible-lightspeed-Bot <[email protected]>
  • Loading branch information
ganeshrn and Ansible-lightspeed-Bot authored Oct 19, 2023
1 parent 056a3a7 commit 5c3f39a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 23 deletions.
34 changes: 32 additions & 2 deletions src/features/lightspeed/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ export class LightSpeedAPI {
console.error("Ansible Lightspeed instance is not initialized.");
return {} as CompletionResponseParams;
}
console.log(
`[ansible-lightspeed] Completion request sent to lightspeed: ${JSON.stringify(
inputData
)}`
);
try {
const response = await axiosInstance.post(
LIGHTSPEED_SUGGESTION_COMPLETION_URL,
Expand Down Expand Up @@ -112,6 +117,23 @@ export class LightSpeedAPI {
vscode.window.showErrorMessage(
"Bad Request response. Please try again."
);
} else if (err?.response?.status === 403) {
const responseErrorData = <AxiosError<{ message?: string }>>(
err?.response?.data
);
if (
responseErrorData &&
responseErrorData.hasOwnProperty("message") &&
responseErrorData.message?.includes("WCA Model ID is invalid")
) {
vscode.window.showErrorMessage(
`Model ID "${this.settingsManager.settings.lightSpeedService.model}" is invalid. Please contact your administrator.`
);
} else {
vscode.window.showErrorMessage(
`User not authorized to access Ansible Lightspeed.`
);
}
} else if (err?.response?.status.toString().startsWith("5")) {
vscode.window.showErrorMessage(
"Ansible Lightspeed encountered an error. Try again after some time."
Expand Down Expand Up @@ -165,7 +187,11 @@ export class LightSpeedAPI {
if (Object.keys(inputData).length === 0) {
return {} as FeedbackResponseParams;
}

console.log(
`[ansible-lightspeed] Feedback request sent to lightspeed: ${JSON.stringify(
inputData
)}`
);
try {
const response = await axiosInstance.post(
LIGHTSPEED_SUGGESTION_FEEDBACK_URL,
Expand All @@ -177,7 +203,6 @@ export class LightSpeedAPI {
if (showInfoMessage) {
vscode.window.showInformationMessage("Thanks for your feedback!");
}
console.log(`Event sent to lightspeed: ${JSON.stringify(inputData)}}`);
return response.data;
} catch (error) {
const err = error as AxiosError;
Expand Down Expand Up @@ -217,6 +242,11 @@ export class LightSpeedAPI {
return {} as ContentMatchesResponseParams;
}
try {
console.log(
`[ansible-lightspeed] Content Match request sent to lightspeed: ${JSON.stringify(
inputData
)}`
);
const response = await axiosInstance.post(
LIGHTSPEED_SUGGESTION_CONTENT_MATCHES_URL,
inputData,
Expand Down
21 changes: 18 additions & 3 deletions src/features/lightspeed/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { LightspeedStatusBar } from "./statusBar";
import { IVarsFileContext } from "../../interfaces/lightspeed";
import { getCustomRolePaths, getCommonRoles } from "../utils/ansible";
import { watchRolesDirectory } from "./utils/watchers";
import { LightSpeedServiceSettings } from "../../interfaces/extensionSettings";

export class LightSpeedManager {
private context;
Expand Down Expand Up @@ -82,9 +83,10 @@ export class LightSpeedManager {
}

public async reInitialize(): Promise<void> {
const lightspeedEnabled = await vscode.workspace
.getConfiguration("ansible")
.get("lightspeed.enabled");
const lightspeedSettings = <LightSpeedServiceSettings>(
vscode.workspace.getConfiguration("ansible").get("lightspeed")
);
const lightspeedEnabled = lightspeedSettings.enabled;

if (!lightspeedEnabled) {
await this.resetContext();
Expand All @@ -94,6 +96,19 @@ export class LightSpeedManager {
} else {
this.lightSpeedAuthenticationProvider.initialize();
this.setContext();
if (lightspeedSettings.suggestions.enabled) {
const githubConfig = (<unknown>(
vscode.workspace.getConfiguration("github")
)) as {
copilot: { enable?: { ansible?: boolean } };
};
const copilotEnableForAnsible = githubConfig.copilot.enable?.ansible;
if (copilotEnableForAnsible) {
vscode.window.showInformationMessage(
"Please disable GitHub Copilot for Ansible Lightspeed file types to use Ansible Lightspeed."
);
}
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/features/lightspeed/contentMatchesWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ export class ContentMatchesWebview implements vscode.WebviewViewProvider {
<li>Path: ${contentMatchResponse.path}</li>
<li>Data Source: ${contentMatchResponse.data_source}</li>
<li>License: ${contentMatchResponse.license}</li>
<li>Ansible type: ${contentMatchResponse.ansible_type}</li>
<li>Score: ${contentMatchResponse.score}</li>
</ul>
</details>
Expand Down
12 changes: 5 additions & 7 deletions src/features/lightspeed/inlineSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,8 @@ export async function getInlineSuggestionItems(

let suggestionMatchType: LIGHTSPEED_SUGGESTION_TYPE | undefined = undefined;

let rhUserHasSeat =
const rhUserHasSeat =
await lightSpeedManager.lightSpeedAuthenticationProvider.rhUserHasSeat();
if (rhUserHasSeat === undefined) {
rhUserHasSeat = false;
}

const lineToExtractPrompt = document.lineAt(currentPosition.line - 1);
const taskMatchedPattern =
Expand Down Expand Up @@ -224,7 +221,7 @@ export async function getInlineSuggestionItems(
);

if (suggestionMatchType === "MULTI-TASK") {
if (!rhUserHasSeat) {
if (rhUserHasSeat === false) {
console.debug(
"[inline-suggestions] Multitask suggestions not supported for a non seat user."
);
Expand Down Expand Up @@ -254,7 +251,7 @@ export async function getInlineSuggestionItems(
}
if (
suggestionMatchType === "SINGLE-TASK" &&
!shouldRequestInlineSuggestions(parsedAnsibleDocument)
!shouldRequestInlineSuggestions(parsedAnsibleDocument, ansibleFileType)
) {
return [];
}
Expand Down Expand Up @@ -319,6 +316,7 @@ export async function getInlineSuggestionItems(
result.predictions.forEach((prediction) => {
let insertText = prediction;
insertText = adjustInlineSuggestionIndent(prediction, currentPosition);
insertText = insertText.replace(/^[ \t]+(?=\r?\n)/gm, "");
insertTexts.push(insertText);

const inlineSuggestionItem = new vscode.InlineCompletionItem(insertText);
Expand Down Expand Up @@ -359,7 +357,7 @@ async function requestInlineSuggest(
parsedAnsibleDocument: any,
documentUri: string,
activityId: string,
rhUserHasSeat: boolean,
rhUserHasSeat: boolean | undefined,
documentDirPath: string,
documentFilePath: string,
ansibleFileType: IAnsibleFileType
Expand Down
9 changes: 6 additions & 3 deletions src/features/lightspeed/lightSpeedOAuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,14 @@ export class LightSpeedAuthenticationProvider
return userAuth;
}

public async rhUserHasSeat(): Promise<boolean> {
public async rhUserHasSeat(): Promise<boolean | undefined> {
const authSession = await this.getLightSpeedAuthSession();
if (authSession?.rhUserHasSeat) {
if (authSession === undefined) {
return undefined;
} else if (authSession?.rhUserHasSeat) {
return true;
} else {
return false;
}
return false;
}
}
26 changes: 20 additions & 6 deletions src/features/lightspeed/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,36 @@ import {
} from "../../../definitions/lightspeed";

export function shouldRequestInlineSuggestions(
parsedAnsibleDocument: yaml.YAMLMap[]
parsedAnsibleDocument: yaml.YAMLMap[],
ansibleFileType: IAnsibleFileType
): boolean {
const lastObject = parsedAnsibleDocument[parsedAnsibleDocument.length - 1];
if (typeof lastObject !== "object") {
return false;
}
// for the last entry in list check if the inline suggestion
// triggered is in Ansible play context by checking for "hosts" keyword.

const objectKeys = Object.keys(lastObject);
const lastParentKey = objectKeys[objectKeys.length - 1];

// check if single-task trigger is in play context or not
if (lastParentKey === "name" && objectKeys.includes("hosts")) {
return false;
}

// check if single-task trigger is in vars context or not
if (lastParentKey === "vars" || lastParentKey === "vars_files") {
return false;
}

// for file identified as playbook, check single task trigger in task context
if (
objectKeys[objectKeys.length - 1] === "name" &&
objectKeys.includes("hosts")
ansibleFileType === "playbook" &&
!["tasks", "pre_tasks", "post_tasks", "handlers"].some((key) =>
objectKeys.includes(key)
)
) {
return false;
}

return true;
}

Expand Down
1 change: 0 additions & 1 deletion src/interfaces/lightspeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export interface IContentMatchParams {
path: string;
license: string;
data_source: string;
ansible_type: string;
score: number;
}

Expand Down

0 comments on commit 5c3f39a

Please sign in to comment.