@@ -5,24 +5,8 @@ import main from "./main.js";
55import { saveSession , deleteSession , loadSessionsForProject , saveMetadata , loadMetadata } from "./helpers/db.js" ;
66const { 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-
258let authToken = { } ;
9+
2610var mainWorkspace ;
2711
2812class 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
70109function workspaceOverride ( ) {
71110 if ( typeof Blockly !== 'undefined' ) {
@@ -80,10 +119,9 @@ function workspaceOverride() {
80119}
81120workspaceOverride ( ) ;
82121
83- // TODO: this should use a global hook
84122document . 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
89127export 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