Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling and other improvements #56

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/lib/agent/Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { typeRealistically } = require('../../util/realisticTyping');
const vscode = require('vscode');
const { getProvider } = require('./providers/providerInstance');
const { speak } = require('../../util/speak');
const config = require('../../../config');

class Agent {
constructor() {
Expand All @@ -16,25 +15,37 @@ class Agent {
this.isNewPrompt = false;
this.promptingTemplate =
'Dan says: {prompt}\nCurrent code in the editor:\n```\n{currentCode}\n```';
this.isStreaming = false;
}

/**
* Prompt something to the AI
* @param {string} input The prompt to be processed
*/
prompt(input) {
if (this.isStreaming || this.actionsQueue.length > 0) {
return vscode.window.showErrorMessage(
'Please wait for the current prompt to finish processing before sending another one.'
);
}
const editor = vscode.window.activeTextEditor;

this.isNewPrompt = true;
const prompt = this.promptingTemplate
.replace('{prompt}', input)
.replace('{currentCode}', editor.document.getText());
this.provider.queryStream(prompt, (response) =>
this.consumeStream(response)
);
this.isStreaming = true;
this.provider
.queryStream(prompt, (response) => this.consumeStream(response))
.then((out) => {
this.isStreaming = false;
if (out.blocked) {
vscode.window.showErrorMessage(`Prompt blocked: ${out.blockReason}`);
}
});
}

async consumeStream(response) {
consumeStream(response) {
const text = response.response;
const event = response.event;

Expand Down
11 changes: 11 additions & 0 deletions src/lib/agent/providers/geminiProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,23 @@ class GeminiProvider extends ModelProvider {
const result = await this.chat.sendMessageStream(prompt);
let fullResponse = '';
for await (const chunk of result.stream) {
if (chunk.promptFeedback?.blockReason) {
// prompt blocked
this.messageHistory.pop();
return {
blocked: true,
blockReason: chunk.promptFeedback.blockReason,
};
}
const text = chunk.text();
fullResponse += text;
await process({ response: text, event: 'output' });
}
await process({ response: '', event: 'done' });
this.messageHistory.push({ role: 'assistant', content: fullResponse });
return {
success: true,
};
}
}

Expand Down