Skip to content

Commit

Permalink
Pass code into prompt for compilation error
Browse files Browse the repository at this point in the history
TODO:
1. Pass code into prompt for active chatting.
2. Improve the chat user interface.
  • Loading branch information
wlicb committed Jun 2, 2024
1 parent cf8bac6 commit ca041fe
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 34 deletions.
7 changes: 5 additions & 2 deletions src/common/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function getRandomCommentWhenLowHealth() {
}
}

export async function getRandomCommentWhenCompilationError() {
export async function getRandomCommentWhenCompilationError(code: string) {
const encouragementMessages = [
`Don't worry, we can fix this! 🛠️`,
`Errors are steps to success! 🚀`,
Expand All @@ -61,16 +61,18 @@ export async function getRandomCommentWhenCompilationError() {
];
let prompt = "You are a virtual pet for students to learn programming. You should talk in a cute way and give the student emotional support and encouragement. Please keep your response within 20 words.";
prompt += `The student just had a compilation error. Please give the student some positive feedback.`;
prompt += `Here is the student's code: ${code}. The student may ask you about the code, but please do not provide solutions directly. Please give indirect hints, such as where to look for the bugs.`;
const randomMessage = encouragementMessages[Math.floor(Math.random() * encouragementMessages.length)];
const aiMessage = await getMessageFromAI(prompt);
console.log(prompt);
if (aiMessage === "") {
return randomMessage;
} else {
return aiMessage;
}
}

export async function getRandomCommentWhenCompilationSuccess() {
export async function getRandomCommentWhenCompilationSuccess(code: string) {
const encouragementMessages = [
`Great job! 🎉`,
`You did it! 🚀`,
Expand All @@ -85,6 +87,7 @@ export async function getRandomCommentWhenCompilationSuccess() {
];
let prompt = "You are a virtual pet for students to learn programming. You should talk in a cute way and give the student emotional support and encouragement. Please keep your response within 20 words.";
prompt += `The student just succeeded in compiling his code. Please give the student some positive feedback.`;
prompt += `Here is the student's code: ${code}. The student may ask you about the code, but please do not provide solutions directly. Please give indirect hints, such as where to improve.`;
const randomMessage = encouragementMessages[Math.floor(Math.random() * encouragementMessages.length)];
const aiMessage = await getMessageFromAI(prompt);
if (aiMessage === "") {
Expand Down
2 changes: 1 addition & 1 deletion src/common/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ async function runCompilationTask(filePath: string): Promise<number> {

vscode.tasks.executeTask(task).then(undefined, reject);
});
}
}
30 changes: 9 additions & 21 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,30 +483,23 @@ export function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(
vscode.commands.registerCommand('vscode-pets.compile', async () => {
const code = getEditorText();
let codeText = "";
if (code !== undefined) {
codeText = code;
}
doCompile()?.then(compileResult => {
//console.log("Compile result is ", compileResult);
const panel = getPetPanel();
if (panel !== undefined) {
panel.handleCompileResult(compileResult);
panel.handleCompileResult(compileResult, codeText);
}
}).catch(err => {
console.log(err);
});
}),
);

context.subscriptions.push(
vscode.commands.registerCommand('vscode-pets.get-code-text', async () => {
const text = getEditorText();
if (text !== undefined) {
const panel = getPetPanel();
if (panel !== undefined) {
panel.handleGetCodeTextResult(text);
}
}
}),
)

context.subscriptions.push(
vscode.commands.registerCommand('vscode-pets.roll-call', async () => {
const panel = getPetPanel();
Expand Down Expand Up @@ -857,8 +850,7 @@ interface IPetPanel {
setThrowWithMouse(newThrowWithMouse: boolean): void;
updateExperience(difference: number): void;
updateHealth(difference: number): void;
handleGetCodeTextResult(text: string): void;
handleCompileResult(result: number): void;
handleCompileResult(result: number, code: string): void;
updateHealthTimer(timer: Date): void;
}

Expand Down Expand Up @@ -997,12 +989,8 @@ class PetWebviewContainer implements IPetPanel {

}

public handleCompileResult(result: number): void {
void this.getWebview().postMessage({ command: 'handle-compile-result', result: result });
}

public handleGetCodeTextResult(text: string): void {
void this.getWebview().postMessage({ command: 'handle-code-text-result', result: text });
public handleCompileResult(result: number, code: string): void {
void this.getWebview().postMessage({ command: 'handle-compile-result', result: result, code: code });
}

protected getWebview(): vscode.Webview {
Expand Down
8 changes: 4 additions & 4 deletions src/panel/basepettype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,10 @@ export abstract class BasePetType implements IPetType {
}
}

async onCompilationError() {
async onCompilationError(code: string) {
let returnMsg = "";
try {
const msg = await getRandomCommentWhenCompilationError();
const msg = await getRandomCommentWhenCompilationError(code);
this.showSpeechBubble(msg, 2000);
returnMsg = "(Compilation Failed) " + msg;
} catch (err) {
Expand All @@ -485,10 +485,10 @@ export abstract class BasePetType implements IPetType {
return returnMsg;
}

async onCompilationSuccess() {
async onCompilationSuccess(code: string) {
let returnMsg = "";
try {
const msg = await getRandomCommentWhenCompilationSuccess();
const msg = await getRandomCommentWhenCompilationSuccess(code);
this.showSpeechBubble(msg, 2000);
returnMsg = "(Compilation Succeeded) " + msg;
} catch (err) {
Expand Down
9 changes: 5 additions & 4 deletions src/panel/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,11 +708,12 @@ export function petPanelApp(
break;
case 'handle-compile-result':
var pets = allPets.pets;
var result = message.result;
var randomPet = pets[Math.floor(Math.random() * pets.length)];
const result = message.result;
const code = message.code;
const randomPet = pets[Math.floor(Math.random() * pets.length)];

if (result === 0) {
randomPet.pet.onCompilationSuccess().then(msg => {
randomPet.pet.onCompilationSuccess(code).then(msg => {
if (msg !== "") {
displayMessage("", msg);
storeMessage("", msg);
Expand All @@ -731,7 +732,7 @@ export function petPanelApp(
});;
});
} else {
randomPet.pet.onCompilationError().then(msg => {
randomPet.pet.onCompilationError(code).then(msg => {
if (msg !== "") {
displayMessage("", msg);
storeMessage("", msg);
Expand Down
4 changes: 2 additions & 2 deletions src/panel/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export interface IPetType {
setExperience(value: number, showMessage: boolean): Promise<string>;
setLevel(value: number): void;

onCompilationError(): Promise<string>;
onCompilationSuccess(): Promise<string>;
onCompilationError(code: string): Promise<string>;
onCompilationSuccess(code: string): Promise<string>;


showSpeechBubble(message: string, duration: number): void;
Expand Down

0 comments on commit ca041fe

Please sign in to comment.