Skip to content

Commit 331ae26

Browse files
nang-devnang-dev2
andcommitted
Aider robustness 2 (#193)
* Fixed build * Added working --------- Co-authored-by: nang-dev <[email protected]>
1 parent 64f9000 commit 331ae26

File tree

8 files changed

+47
-41
lines changed

8 files changed

+47
-41
lines changed

core/core.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from "node:fs";
22
import { v4 as uuidv4 } from "uuid";
3-
import type { ContextItemId, IDE, IndexingProgressUpdate } from ".";
3+
import type { ChatMessage, ContextItemId, IDE, IndexingProgressUpdate } from ".";
44
import { CompletionProvider } from "./autocomplete/completionProvider";
55
import { ConfigHandler } from "./config/ConfigHandler";
66
import {
@@ -383,7 +383,14 @@ export class Core {
383383
});
384384
break;
385385
}
386-
yield { content: next.value.content, citations: next.value?.citations };
386+
// Assert that next.value is a ChatMessage
387+
const chatMessage = next.value as ChatMessage;
388+
389+
yield {
390+
content: chatMessage.content,
391+
citations: chatMessage.citations
392+
};
393+
387394
next = await gen.next();
388395
}
389396

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

449456
try {
450457
if (pearAIModels.length > 0) {
@@ -457,13 +464,10 @@ export class Core {
457464
console.warn(`Error resetting PearAI credentials: ${e}`);
458465
return undefined;
459466
}
460-
// TODO @nang - handle this better
461467
try {
462-
if (aiderModels.length > 0) {
463-
aiderModels.forEach(model => {
464-
model.setPearAIAccessToken(accessToken);
465-
model.setPearAIRefreshToken(refreshToken);
466-
});
468+
if (aiderModel) {
469+
aiderModel.setPearAIAccessToken(accessToken);
470+
aiderModel.setPearAIRefreshToken(refreshToken);
467471
}
468472
} catch (e) {
469473
console.warn(`Error resetting PearAI credentials for aider: ${e}`);

core/llm/llms/AiderLLM.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,20 @@ class Aider extends BaseLLM {
198198
content: escapeDollarSigns(newOutput),
199199
};
200200

201-
// Safety check
202-
// if (Aider.aiderProcess?.killed) {
203-
// if (potentialCompletionTimeout) {
204-
// clearTimeout(potentialCompletionTimeout);
205-
// }
206-
// this.setAiderState("stopped");
207-
// break;
208-
// }
201+
if (Aider.aiderProcess?.state.state === "stopped" || Aider.aiderProcess?.state.state === "crashed") {
202+
if (potentialCompletionTimeout) {
203+
clearTimeout(potentialCompletionTimeout);
204+
}
205+
this.setAiderState({ state: "stopped" });
206+
break;
207+
}
209208
}
210209
}
211210

212-
// Clean up any remaining timeout
213211
if (potentialCompletionTimeout) {
214212
clearTimeout(potentialCompletionTimeout);
215213
}
216214

217-
// Reset the output after capturing a complete response
218215
if (Aider.aiderProcess) {
219216
Aider.aiderProcess.aiderOutput = "";
220217
}

extensions/vscode/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/vscode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "pearai",
44
"publisher": "pearai",
55
"icon": "media/icon.png",
6-
"version": "1.5.2",
6+
"version": "1.5.3",
77
"repository": {
88
"type": "git",
99
"url": "https://github.com/trypear/pearai-submodule"

extensions/vscode/src/integrations/aider/aiderProcess.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,19 @@ export class AiderProcessManager {
208208

209209

210210
public updateState(newState: Omit<AiderState, "timeStamp">) {
211+
console.log(`Aider state changing from ${this._state.state} to ${newState.state}`);
212+
213+
// TODO: probably handle this better. Crashed happens because we send a SIGINT to the process needed to reset. @nang
214+
if (this._state.state === "restarting" && newState.state === "crashed") {
215+
console.log("Blocking state change from 'restarting' to 'crashed'");
216+
217+
return;
218+
}
211219
this._state = {
212220
...newState,
213221
timeStamp: Date.now()
214222
};
215223
vscode.commands.executeCommand("pearai.setAiderProcessState", this._state);
216-
this.notifyStateChange();
217-
}
218-
219-
private notifyStateChange() {
220-
console.log(`Aider state changed to: ${this._state.state}`);
221224
}
222225

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

235-
async startAiderChat(model: string, apiKey: string | undefined): Promise<void> {
238+
async startAiderChat(model: string, apiKey: string | undefined, isRestarting: boolean = false): Promise<void> {
239+
if (!isRestarting) {
236240
this.updateState({ state: "starting" });
241+
}
237242

238243
try {
239244
// Check if current workspace is a git repo
@@ -405,10 +410,14 @@ export class AiderProcessManager {
405410

406411
async resetSession(model: string, apiKey: string | undefined): Promise<void> {
407412
console.log("Resetting Aider process...");
408-
this.killAiderProcess();
413+
if (this.aiderProcess) {
414+
killAiderProcess(this.aiderProcess);
415+
this.aiderProcess = null;
416+
this.updateState({ state: "restarting" });
417+
}
409418

410419
try {
411-
await this.startAiderChat(model, apiKey);
420+
await this.startAiderChat(model, apiKey, true);
412421
console.log("Aider process reset successfully.");
413422
} catch (error) {
414423
console.error("Error resetting Aider process:", error);

extensions/vscode/src/integrations/aider/aiderUtil.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,13 @@ export async function aiderCtrlC(core: Core) {
113113

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

120120
try {
121-
if (aiderModels.length > 0) {
122-
aiderModels.forEach((model) => {
123-
if (Aider.aiderProcess) {
124-
model.aiderResetSession(model.model, model.apiKey);
125-
}
126-
});
121+
if (aiderModel && Aider.aiderProcess) {
122+
aiderModel.aiderResetSession(aiderModel.model, aiderModel.apiKey);
127123
}
128124
} catch (e) {
129125
console.warn(`Error resetting Aider session: ${e}`);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export interface AiderState {
2-
state: "undefined" | "starting" | "uninstalled" | "ready" | "stopped" |"crashed" | "signedOut" | "notgitrepo";
2+
state: "undefined" | "starting" | "uninstalled" | "ready" | "stopped" |"crashed" | "signedOut" | "notgitrepo" | "restarting";
33
timeStamp?: number;
44
}

gui/src/integrations/aider/aidergui.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ useEffect(() => {
271271
if (aiderProcessState.state === "uninstalled" || aiderProcessState.state === "stopped" || aiderProcessState.state === "crashed") {
272272
return <AiderManualInstallation />;
273273
}
274-
if (aiderProcessState.state === "starting") {
274+
if (aiderProcessState.state === "starting" || aiderProcessState.state === "restarting") {
275275
msg = (
276276
<>
277277
Spinning up PearAI Creator (Powered By aider), please give it a second...

0 commit comments

Comments
 (0)