Skip to content

Commit

Permalink
Add GitHub Copilot prompt functionality #888
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Nov 13, 2024
1 parent 5f7f847 commit 65d430b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## [10.7.0] - 2024-xx-xx

### 🎨 Enhancements

- [#888](https://github.com/estruyf/vscode-front-matter/issues/888): Added the ability to prompt GitHub Copilot from a custom script/action

### 🐞 Fixes

## [10.6.0] - 2024-11-06 - [Release notes](https://beta.frontmatter.codes/updates/v10.6.0)

### 🎨 Enhancements
Expand Down
41 changes: 32 additions & 9 deletions src/helpers/CustomScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { ParsedFrontMatter } from '../parsers';
import { SETTING_CUSTOM_SCRIPTS } from '../constants';
import { evaluateCommand, existsAsync, getPlatform } from '../utils';
import { LocalizationKey, localize } from '../localization';
import { ScriptAction } from '../models';
import { Copilot } from '../services/Copilot';

export class CustomScript {
/**
Expand Down Expand Up @@ -267,12 +269,7 @@ export class CustomScript {
try {
const data: {
frontmatter?: { [key: string]: any };
fmAction?:
| 'open'
| 'copyMediaMetadata'
| 'copyMediaMetadataAndDelete'
| 'deleteMedia'
| 'fieldAction';
fmAction?: ScriptAction;
fmPath?: string;
fmSourcePath?: string;
fmDestinationPath?: string;
Expand Down Expand Up @@ -425,14 +422,31 @@ export class CustomScript {
const fullScript = `${command} "${scriptPath}" ${args}`;
Logger.info(localize(LocalizationKey.helpersCustomScriptExecuting, fullScript));

const output = await CustomScript.processExecution(fullScript, wsPath);
return output;
}

// Recursive function to process the execution of the script
private static async processExecution(fullScript: string, wsPath: string): Promise<string> {
const output: string = await CustomScript.executeScriptAsync(fullScript, wsPath);

try {
const data = JSON.parse(output);
if (data.questions) {
const { questions, fmAction, fmPrompt } = data as {
questions?: {
name: string;
message: string;
default?: string;
options?: string[];
}[];
fmAction?: ScriptAction;
fmPrompt?: any; // When the 'promptCopilot' action is used
};

if (questions) {
const answers: string[] = [];

for (const question of data.questions) {
for (const question of questions) {
if (question.name && question.message) {
let answer;
if (question.options) {
Expand Down Expand Up @@ -460,7 +474,16 @@ export class CustomScript {

if (answers.length > 0) {
const newScript = `${fullScript} ${answers.join(' ')}`;
return await CustomScript.executeScriptAsync(newScript, wsPath);
return await CustomScript.processExecution(newScript, wsPath);
}
} else if (fmAction) {
if (fmAction === 'promptCopilot' && fmPrompt) {
const response = await Copilot.promptCopilot(fmPrompt);
if (response) {
const promptResponse = `promptResponse="${response}"`;
const newScript = `${fullScript} ${promptResponse}`;
return await CustomScript.processExecution(newScript, wsPath);
}
}
}
} catch (error) {
Expand Down
7 changes: 7 additions & 0 deletions src/models/ScriptAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type ScriptAction =
| 'open'
| 'copyMediaMetadata'
| 'copyMediaMetadataAndDelete'
| 'deleteMedia'
| 'fieldAction'
| 'promptCopilot';
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export * from './Mode';
export * from './PanelSettings';
export * from './PostMessageData';
export * from './Project';
export * from './ScriptAction';
export * from './ShellSetting';
export * from './Snippets';
export * from './SortOrder';
Expand Down
28 changes: 28 additions & 0 deletions src/services/Copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,34 @@ Example: SEO, website optimization, digital marketing.`
}
}

/**
* Prompts the Copilot service with a given message and returns the response.
*
* @param {string} prompt - The message to send to the Copilot service.
* @returns {Promise<string | undefined>} - The response from the Copilot service, or undefined if no prompt is provided or an error occurs.
*
* @throws {Error} - Logs an error message if an exception occurs during the process.
*/
public static async promptCopilot(prompt: string): Promise<string | undefined> {
if (!prompt) {
return;
}

try {
const messages = [LanguageModelChatMessage.User(prompt)];

const chatResponse = await Copilot.getChatResponse(messages);
if (!chatResponse) {
return;
}

return chatResponse;
} catch (err) {
Logger.error(`Copilot:promptCopilot:: ${(err as Error).message}`);
return '';
}
}

/**
* Retrieves the chat response from the language model.
* @param messages - The chat messages to send to the language model.
Expand Down

0 comments on commit 65d430b

Please sign in to comment.