forked from onlook-dev/onlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
86 lines (74 loc) · 2.55 KB
/
index.ts
File metadata and controls
86 lines (74 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import type { ProjectsManager } from '@/lib/projects';
import { type ParsedError, compareErrors } from '@onlook/utility';
import { autorun, makeAutoObservable } from 'mobx';
import type { EditorEngine } from '..';
export class ErrorManager {
private webviewIdToError: Record<string, ParsedError[]> = {};
private terminalErrors: ParsedError[] = [];
shouldShowErrors: boolean = false;
constructor(
private editorEngine: EditorEngine,
private projectsManager: ProjectsManager,
) {
makeAutoObservable(this);
autorun(() => {
if (this.shouldAutoFixErrors) {
this.sendFixError();
}
});
}
get errors() {
return [...this.terminalErrors];
}
get shouldAutoFixErrors() {
return this.errors.length > 0 && !this.editorEngine.chat.isWaiting;
}
async sendFixError() {
if (this.errors.length > 0) {
const res = await this.editorEngine.chat.sendFixErrorToAi(this.errors);
if (res) {
this.removeErrorsFromMap(this.errors);
}
}
}
removeErrorsFromMap(errors: ParsedError[]) {
for (const [webviewId, existingErrors] of Object.entries(this.webviewIdToError)) {
this.webviewIdToError[webviewId] = existingErrors.filter(
(existing) => !errors.some((error) => compareErrors(existing, error)),
);
}
}
errorByWebviewId(webviewId: string) {
return this.webviewIdToError[webviewId];
}
addError(webviewId: string, event: Electron.ConsoleMessageEvent) {
if (event.sourceId?.includes('localhost')) {
return;
}
const error: ParsedError = {
sourceId: event.sourceId,
type: 'webview',
content: event.message,
};
const existingErrors = this.webviewIdToError[webviewId] || [];
if (!existingErrors.some((e) => compareErrors(e, error))) {
this.webviewIdToError[webviewId] = [...existingErrors, error];
}
}
addTerminalError(message: string) {
const error: ParsedError = {
sourceId: 'terminal',
type: 'terminal',
content: message,
};
const existingErrors = this.terminalErrors || [];
if (!existingErrors.some((e) => compareErrors(e, error))) {
this.terminalErrors = [...existingErrors, error];
}
this.shouldShowErrors = true;
}
clear() {
this.webviewIdToError = {};
this.terminalErrors = [];
}
}