Skip to content

Commit b16a6a0

Browse files
authored
fix: revert changes to this file
1 parent 0b5d71f commit b16a6a0

1 file changed

Lines changed: 66 additions & 29 deletions

File tree

src/addons/addons/ai-integration/userscript.js

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,8 @@ import main from "./main.js";
55
import { saveSession, deleteSession, loadSessionsForProject, saveMetadata, loadMetadata } from "./helpers/db.js";
66
const {API_HOST} = require('../../../lib/brand.js');
77

8-
const AI_INTEGRATION = {
9-
AI_currently_blabbering: false,
10-
CodeChunks: [],
11-
AllCodeChunksEverAdded: [],
12-
processedCodeChunks: [],
13-
errorsDetected: [],
14-
15-
sessions: [],
16-
activeSessionId: null,
17-
nextSessionId: 1,
18-
currentProjectId: '0',
19-
20-
popupOpen: false,
21-
canUse: true,
22-
AIModels: [],
23-
};
24-
258
let authToken = {};
9+
2610
var mainWorkspace;
2711

2812
class ChatSession {
@@ -59,13 +43,68 @@ window.addEventListener('blockError', (event) => {
5943
document.AI_INTEGRATION.errorsDetected.push(event.detail);
6044
});
6145

62-
// TODO: possibly push this into the addons api?
63-
document.AI_INTEGRATION = AI_INTEGRATION;
46+
document.AI_INTEGRATION = { //probably the dumbest way to possibly do this, it just make debugging alot easier (will do it properly later)
47+
CodeChunks: [],
48+
AllCodeChunksEverAdded: [],
49+
processedCodeChunks: [],
50+
errorsDetected: [],
51+
52+
sessions: [],
53+
activeSessionId: null,
54+
nextSessionId: 1,
55+
currentProjectId: '0',
56+
57+
popupOpen: false,
58+
canUse: true,
59+
AIModels: [],
60+
};
61+
62+
document.AI_INTEGRATION.createNewSession = function() {
63+
const newId = this.nextSessionId++;
64+
const newSession = new ChatSession(newId, this.currentProjectId);
65+
this.sessions.push(newSession);
66+
this.activeSessionId = newId;
67+
68+
saveSession(newSession.toJSON());
69+
saveMetadata(`${this.currentProjectId}_nextSessionId`, this.nextSessionId);
70+
saveMetadata(`${this.currentProjectId}_activeSessionId`, this.activeSessionId);
71+
72+
return newSession;
73+
};
74+
75+
document.AI_INTEGRATION.getActiveSession = function() {
76+
if (!this.activeSessionId) return null;
77+
return this.sessions.find(s => s.id === this.activeSessionId);
78+
};
79+
80+
document.AI_INTEGRATION.closeSession = function(sessionId) {
81+
const sessionIndex = this.sessions.findIndex(s => s.id === sessionId);
82+
if (sessionIndex === -1) return;
83+
84+
this.sessions.splice(sessionIndex, 1);
85+
deleteSession(sessionId);
86+
87+
if (this.activeSessionId === sessionId) {
88+
if (this.sessions.length > 0) {
89+
const newActiveIndex = Math.max(0, sessionIndex - 1);
90+
this.activeSessionId = this.sessions[newActiveIndex].id;
91+
} else {
92+
this.activeSessionId = null;
93+
}
94+
saveMetadata(`${this.currentProjectId}_activeSessionId`, this.activeSessionId);
95+
}
96+
};
97+
98+
document.AI_INTEGRATION.updateAndSaveSession = function(session) {
99+
if (session) {
100+
saveSession(session.toJSON());
101+
}
102+
};
103+
104+
document.AI_INTEGRATION.saveActiveSessionMetadata = function() {
105+
saveMetadata(`${this.currentProjectId}_activeSessionId`, this.activeSessionId);
106+
};
64107

65-
// TODO: dont use global events
66-
window.addEventListener('blockError', (event) => {
67-
AI_INTEGRATION.errorsDetected.push(event.detail);
68-
});
69108

70109
function workspaceOverride() {
71110
if (typeof Blockly !== 'undefined') {
@@ -80,10 +119,9 @@ function workspaceOverride() {
80119
}
81120
workspaceOverride();
82121

83-
// TODO: this should use a global hook
84122
document.addEventListener("mousemove", (event) => {
85-
AI_INTEGRATION.X_COORDINATE = event.clientX;
86-
AI_INTEGRATION.Y_COORDINATE = event.clientY;
123+
document.AI_INTEGRATION.X_COORDINATE = event.clientX;
124+
document.AI_INTEGRATION.Y_COORDINATE = event.clientY;
87125
});
88126

89127
export default async function ({ addon, console }) {
@@ -144,7 +182,7 @@ export default async function ({ addon, console }) {
144182
authToken.openrouter = addon.settings.get("OpenRouterAPIKey");
145183

146184
if (authToken.gemini == "" && authToken.openrouter == "") {
147-
AI_INTEGRATION.canUse = false;
185+
document.AI_INTEGRATION.canUse = false;
148186
window.addEventListener('ai-button-clicked', function () {
149187
main.createBasePopup(2, "");
150188
});
@@ -165,7 +203,7 @@ export default async function ({ addon, console }) {
165203
}
166204
})
167205
.then(data => {
168-
AI_INTEGRATION.AIModels = data;
206+
document.AI_INTEGRATION.AIModels = data;
169207
helpers.updateAIModels(authToken.gemini, authToken.openrouter);
170208
})
171209
.catch(error => {
@@ -240,7 +278,6 @@ export default async function ({ addon, console }) {
240278
}
241279
});
242280

243-
// TODO: dont use global events
244281
window.addEventListener('ai-button-clicked', function () {
245282
main.startNewSessionWithPrompt(2, "");
246283
});

0 commit comments

Comments
 (0)