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

Aider robustness 2 #193

Merged
merged 2 commits into from
Nov 25, 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
22 changes: 13 additions & 9 deletions core/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from "node:fs";
import { v4 as uuidv4 } from "uuid";
import type { ContextItemId, IDE, IndexingProgressUpdate } from ".";
import type { ChatMessage, ContextItemId, IDE, IndexingProgressUpdate } from ".";
import { CompletionProvider } from "./autocomplete/completionProvider";
import { ConfigHandler } from "./config/ConfigHandler";
import {
Expand Down Expand Up @@ -383,7 +383,14 @@ export class Core {
});
break;
}
yield { content: next.value.content, citations: next.value?.citations };
// Assert that next.value is a ChatMessage
const chatMessage = next.value as ChatMessage;

yield {
content: chatMessage.content,
citations: chatMessage.citations
};

next = await gen.next();
}

Expand Down Expand Up @@ -444,7 +451,7 @@ export class Core {
const { accessToken, refreshToken } = msg.data || {};
const config = await this.configHandler.loadConfig();
const pearAIModels = config.models.filter(model => model instanceof PearAIServer) as PearAIServer[];
const aiderModels = config.models.filter(model => model instanceof Aider) as Aider[];
const aiderModel = config.models.find(model => model instanceof Aider) as Aider;

try {
if (pearAIModels.length > 0) {
Expand All @@ -457,13 +464,10 @@ export class Core {
console.warn(`Error resetting PearAI credentials: ${e}`);
return undefined;
}
// TODO @nang - handle this better
try {
if (aiderModels.length > 0) {
aiderModels.forEach(model => {
model.setPearAIAccessToken(accessToken);
model.setPearAIRefreshToken(refreshToken);
});
if (aiderModel) {
aiderModel.setPearAIAccessToken(accessToken);
aiderModel.setPearAIRefreshToken(refreshToken);
}
} catch (e) {
console.warn(`Error resetting PearAI credentials for aider: ${e}`);
Expand Down
17 changes: 7 additions & 10 deletions core/llm/llms/AiderLLM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,20 @@ class Aider extends BaseLLM {
content: escapeDollarSigns(newOutput),
};

// Safety check
// if (Aider.aiderProcess?.killed) {
// if (potentialCompletionTimeout) {
// clearTimeout(potentialCompletionTimeout);
// }
// this.setAiderState("stopped");
// break;
// }
if (Aider.aiderProcess?.state.state === "stopped" || Aider.aiderProcess?.state.state === "crashed") {
if (potentialCompletionTimeout) {
clearTimeout(potentialCompletionTimeout);
}
this.setAiderState({ state: "stopped" });
break;
}
}
}

// Clean up any remaining timeout
if (potentialCompletionTimeout) {
clearTimeout(potentialCompletionTimeout);
}

// Reset the output after capturing a complete response
if (Aider.aiderProcess) {
Aider.aiderProcess.aiderOutput = "";
}
Expand Down
4 changes: 2 additions & 2 deletions extensions/vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "pearai",
"publisher": "pearai",
"icon": "media/icon.png",
"version": "1.5.2",
"version": "1.5.3",
"repository": {
"type": "git",
"url": "https://github.com/trypear/pearai-submodule"
Expand Down
25 changes: 17 additions & 8 deletions extensions/vscode/src/integrations/aider/aiderProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,19 @@ export class AiderProcessManager {


public updateState(newState: Omit<AiderState, "timeStamp">) {
console.log(`Aider state changing from ${this._state.state} to ${newState.state}`);

// TODO: probably handle this better. Crashed happens because we send a SIGINT to the process needed to reset. @nang
if (this._state.state === "restarting" && newState.state === "crashed") {
console.log("Blocking state change from 'restarting' to 'crashed'");

return;
}
this._state = {
...newState,
timeStamp: Date.now()
};
vscode.commands.executeCommand("pearai.setAiderProcessState", this._state);
this.notifyStateChange();
}

private notifyStateChange() {
console.log(`Aider state changed to: ${this._state.state}`);
}

private captureAiderOutput(data: Buffer): void {
Expand All @@ -232,8 +235,10 @@ export class AiderProcessManager {
this.aiderOutput += cleanOutput;
}

async startAiderChat(model: string, apiKey: string | undefined): Promise<void> {
async startAiderChat(model: string, apiKey: string | undefined, isRestarting: boolean = false): Promise<void> {
if (!isRestarting) {
this.updateState({ state: "starting" });
}

try {
// Check if current workspace is a git repo
Expand Down Expand Up @@ -405,10 +410,14 @@ export class AiderProcessManager {

async resetSession(model: string, apiKey: string | undefined): Promise<void> {
console.log("Resetting Aider process...");
this.killAiderProcess();
if (this.aiderProcess) {
killAiderProcess(this.aiderProcess);
this.aiderProcess = null;
this.updateState({ state: "restarting" });
}

try {
await this.startAiderChat(model, apiKey);
await this.startAiderChat(model, apiKey, true);
console.log("Aider process reset successfully.");
} catch (error) {
console.error("Error resetting Aider process:", error);
Expand Down
14 changes: 5 additions & 9 deletions extensions/vscode/src/integrations/aider/aiderUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,13 @@ export async function aiderCtrlC(core: Core) {

export async function aiderResetSession(core: Core) {
const config = await core.configHandler.loadConfig();
const aiderModels = config.models.filter(
(model) => model instanceof Aider,
) as Aider[];
const aiderModel = config.models.find(
(model) => model instanceof Aider
) as Aider | undefined;

try {
if (aiderModels.length > 0) {
aiderModels.forEach((model) => {
if (Aider.aiderProcess) {
model.aiderResetSession(model.model, model.apiKey);
}
});
if (aiderModel && Aider.aiderProcess) {
aiderModel.aiderResetSession(aiderModel.model, aiderModel.apiKey);
}
} catch (e) {
console.warn(`Error resetting Aider session: ${e}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface AiderState {
state: "undefined" | "starting" | "uninstalled" | "ready" | "stopped" |"crashed" | "signedOut" | "notgitrepo";
state: "undefined" | "starting" | "uninstalled" | "ready" | "stopped" |"crashed" | "signedOut" | "notgitrepo" | "restarting";
timeStamp?: number;
}
2 changes: 1 addition & 1 deletion gui/src/integrations/aider/aidergui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ useEffect(() => {
if (aiderProcessState.state === "uninstalled" || aiderProcessState.state === "stopped" || aiderProcessState.state === "crashed") {
return <AiderManualInstallation />;
}
if (aiderProcessState.state === "starting") {
if (aiderProcessState.state === "starting" || aiderProcessState.state === "restarting") {
msg = (
<>
Spinning up PearAI Creator (Powered By aider), please give it a second...
Expand Down