Skip to content

Commit

Permalink
AAP-30427: VS Code Extension settings page to take a new custom_promp…
Browse files Browse the repository at this point in the history
…t field (ansible#1545)
  • Loading branch information
manstis committed Oct 14, 2024
1 parent 5c40c83 commit 16ff1a6
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 1 deletion.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,18 @@
"type": "string",
"markdownDescription": "Model ID to override your organization's default model. This setting is only applicable to commercial users with an Ansible Lightspeed seat assignment.",
"order": 4
},
"ansible.lightspeed.playbookGenerationCustomPrompt": {
"scope": "resource",
"type": "string",
"markdownDescription": "Custom Prompt for Playbook generation. This setting is only applicable to commercial users with an Ansible Lightspeed seat assignment.",
"order": 5
},
"ansible.lightspeed.playbookExplanationCustomPrompt": {
"scope": "resource",
"type": "string",
"markdownDescription": "Custom Prompt for Playbook explanation. This setting is only applicable to commercial users with an Ansible Lightspeed seat assignment.",
"order": 6
}
}
},
Expand Down
13 changes: 13 additions & 0 deletions src/features/lightspeed/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ export class LightSpeedAPI {
return {} as ExplanationResponseParams;
}
try {
const customPrompt =
lightSpeedManager.settingsManager.settings.lightSpeedService
.playbookExplanationCustomPrompt;
if (customPrompt && customPrompt.length > 0) {
inputData.customPrompt = customPrompt;
}
const requestData = {
...inputData,
metadata: { ansibleExtensionVersion: this._extensionVersion },
Expand Down Expand Up @@ -336,6 +342,13 @@ export class LightSpeedAPI {
return {} as GenerationResponseParams;
}
try {
const customPrompt =
lightSpeedManager.settingsManager.settings.lightSpeedService
.playbookGenerationCustomPrompt;
if (customPrompt && customPrompt.length > 0) {
inputData.customPrompt = customPrompt;
}

const requestData = {
...inputData,
metadata: { ansibleExtensionVersion: this._extensionVersion },
Expand Down
7 changes: 7 additions & 0 deletions src/features/lightspeed/playbookExplanation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ export const playbookExplanation = async (extensionUri: vscode.Uri) => {
markdown = response.content;
if (markdown.length === 0) {
markdown = "### No explanation provided.";
const customPrompt =
lightSpeedManager.settingsManager.settings.lightSpeedService
.playbookExplanationCustomPrompt ?? "";
if (customPrompt.length > 0) {
markdown +=
"\n\nYou may want to consider amending your custom prompt.";
}
}
const html_snippet = marked.parse(markdown) as string;
currentPanel.setContent(html_snippet, true);
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/lightspeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export interface ISuggestionDetails {

export interface GenerationRequestParams {
text: string;
customPrompt?: string;
outline?: string;
generationId: string;
createOutline: boolean;
Expand All @@ -156,6 +157,7 @@ export interface GenerationResponseParams {

export interface ExplanationRequestParams {
content: string;
customPrompt?: string;
explanationId: string;
}

Expand Down
17 changes: 16 additions & 1 deletion test/mockLightspeedServer/explanations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function explanations(
res: any,
) {
const playbook = req.body.content;
const customPrompt = req.body.customPrompt;
const explanationId = req.body.explanationId
? req.body.explanationId
: uuidv4();
Expand Down Expand Up @@ -38,8 +39,18 @@ export function explanations(
});
}

// Special case to replicate a broken custom prompt
if (customPrompt && customPrompt === "custom prompt broken") {
logger.info("Returning 400. Custom prompt is invalid");
return res.status(400).send({
code: "validation",
message: "invalid",
detail: { customPrompt: "custom prompt is invalid" },
});
}

// cSpell: disable
const content = `
let content = `
## Playbook Overview and Structure
This playbook creates an Azure Virtual Network (VNET) with the name "VNET_1"
Expand Down Expand Up @@ -83,6 +94,10 @@ the following parameters:
`;
// cSpell: enable

if (customPrompt) {
content += "\nCustom prompt explanation.";
}

return res.send({
content,
format,
Expand Down
15 changes: 15 additions & 0 deletions test/mockLightspeedServer/generations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function generations(
res: any,
) {
const text = req.body.text;
const customPrompt = req.body.customPrompt;
const createOutline = req.body.createOutline;
const generationId = req.body.generationId ? req.body.generationId : uuidv4();
const wizardId = req.body.wizardId;
Expand Down Expand Up @@ -46,6 +47,16 @@ export function generations(
});
}

// Special case to replicate a broken custom prompt
if (customPrompt && customPrompt === "custom prompt broken") {
logger.info("Returning 400. Custom prompt is invalid");
return res.status(400).send({
code: "validation",
message: "invalid",
detail: { customPrompt: "custom prompt is invalid" },
});
}

// cSpell: disable
let outline: string | undefined = `1. Create VNET named VNET_1
2. Create VNET named VNET_2
Expand Down Expand Up @@ -98,6 +109,10 @@ export function generations(
outline += "\n4. Some extra step.";
}

if (customPrompt) {
outline += "\n5. Custom prompt step.";
}

return res.send({
playbook,
outline,
Expand Down
Loading

0 comments on commit 16ff1a6

Please sign in to comment.