-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
328 lines (297 loc) · 12.4 KB
/
App.tsx
File metadata and controls
328 lines (297 loc) · 12.4 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import React, { useState, useEffect, useRef } from 'react';
import { Sidebar } from './components/Sidebar';
import { ChatPanel } from './components/ChatPanel';
import { useChat } from './hooks/useChat';
import type { ProjectFile, GeneratedFile, MemoryAtom, SessionState, BackgroundInsight } from './types';
import { DiffViewer } from './components/DiffViewer';
import { MemoryCrystal } from './components/MemoryCrystal';
import { backgroundCognitionService } from './services/backgroundCognitionService';
import { validateApiKey } from './services/geminiService';
import { CrystalIcon } from './components/icons';
// This is a mock. In a real app, you'd load these from the filesystem or an API.
const MOCK_PROJECT_FILES: ProjectFile[] = [
{ name: 'index.tsx', content: 'console.log("hello world");', language: 'typescript'},
{ name: 'App.tsx', content: 'function App() { return <div>Hello</div> }', language: 'typescript'},
{ name: 'package.json', content: '{ "name": "my-app" }', language: 'json'},
];
// Add this line to be able to use JSZip from window
declare const JSZip: any;
interface ApiKeySelectorProps {
onKeySelected: () => void;
}
const ApiKeySelector: React.FC<ApiKeySelectorProps> = ({ onKeySelected }) => {
const handleSelectKey = async () => {
if (window.aistudio) {
try {
await window.aistudio.openSelectKey();
// Assume success and notify the parent component to render the app.
// This handles the race condition mentioned in the guidelines.
onKeySelected();
} catch (e) {
console.error("Error opening API key selection:", e);
alert("There was an error opening the API key selection dialog. Please try again.");
}
} else {
alert("API key selection is not available in this environment.");
}
};
return (
<div className="flex items-center justify-center h-screen bg-gray-900 text-white">
<div className="max-w-md w-full bg-gray-800 p-8 rounded-lg shadow-2xl border border-gray-700 text-center">
<div className="flex justify-center text-cyan-400">
<CrystalIcon />
</div>
<h1 className="text-2xl font-bold text-cyan-400 mt-4">Welcome to Reflex Engine</h1>
<p className="text-gray-300 mt-4">
To use this application, you need to select a Google AI API key.
Your key will be used for API calls and associated billing.
</p>
<p className="text-gray-400 text-sm mt-2">
This application does not store your API key. It is managed securely by the environment.
For more information, see the{' '}
<a
href="https://ai.google.dev/gemini-api/docs/billing"
target="_blank"
rel="noopener noreferrer"
className="text-cyan-500 hover:underline"
>
billing documentation
</a>.
</p>
<button
onClick={handleSelectKey}
className="mt-8 w-full bg-cyan-600 text-white font-bold py-3 px-4 rounded-lg hover:bg-cyan-700 transition-colors focus:outline-none focus:ring-2 focus:ring-cyan-500 focus:ring-opacity-50"
>
Select API Key
</button>
</div>
</div>
);
};
type KeyState = 'unknown' | 'validating' | 'needs_selection' | 'ready' | 'error';
function App() {
const [keyState, setKeyState] = useState<KeyState>('unknown');
const [keyError, setKeyError] = useState<string | null>(null);
const chat = useChat(MOCK_PROJECT_FILES);
const [view, setView] = useState<'chat' | 'diff'>('chat');
const [isCrystalPanelVisible, setIsCrystalPanelVisible] = useState(false);
const [diffFiles, setDiffFiles] = useState<{file1: ProjectFile, file2: ProjectFile} | null>(null);
const isCognitionRunning = useRef(false);
useEffect(() => {
// This effect runs only once on mount to validate the API key.
const validate = async () => {
// Bypass validation if in a non-aistudio environment (for local dev)
if (!window.aistudio) {
console.warn("aistudio environment not detected. Assuming API_KEY is set and valid.");
setKeyState('ready');
return;
}
setKeyState('validating');
try {
const isValid = await validateApiKey();
if (isValid) {
setKeyState('ready');
} else {
setKeyState('needs_selection');
}
} catch (e: any) {
setKeyError(e.message);
setKeyState('error');
}
};
validate();
}, []);
// Background Cognition Loop
useEffect(() => {
if (keyState !== 'ready') return; // Don't run background tasks if the API key isn't ready
const cognitionInterval = setInterval(async () => {
if (isCognitionRunning.current || chat.isLoading) {
return; // Don't run if a cycle is already in progress or if the main chat is busy
}
// Ensure there's enough conversation to analyze
if (chat.messages.length < 2) {
return;
}
isCognitionRunning.current = true;
try {
const insight = await backgroundCognitionService.runCognitionCycle(chat.messages);
if (insight) {
chat.addBackgroundInsight(insight);
}
} catch (e) {
console.error("Error in background cognition interval:", e);
} finally {
isCognitionRunning.current = false;
}
}, 60000); // Run every 60 seconds
return () => clearInterval(cognitionInterval);
}, [keyState, chat.messages, chat.isLoading, chat.addBackgroundInsight]);
const handleCompareFiles = (filesToCompare: ProjectFile[]) => {
if (filesToCompare.length !== 2) {
alert("Please select exactly two files to compare.");
return;
}
setDiffFiles({ file1: filesToCompare[0], file2: filesToCompare[1] });
setView('diff');
};
const handleImportFiles = async (event: React.ChangeEvent<HTMLInputElement>) => {
const fileList = event.target.files;
if (!fileList) return;
const newFiles: ProjectFile[] = [];
// FIX: Iterate directly over the FileList. It's an iterable of File objects,
// which avoids the issue where `file` was being inferred as `unknown`.
for (const file of fileList) {
try {
const content = await file.text();
const language = file.name.split('.').pop() || 'plaintext';
newFiles.push({ name: file.name, content, language });
} catch (e) {
console.error("Error reading file:", file.name, e);
alert(`Could not read file: ${file.name}`);
}
}
chat.addFiles(newFiles);
// CRITICAL: Reset the input value to allow importing the same file again
event.target.value = '';
};
const handleImportState = async (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (!file) return;
if (file.type !== 'application/json') {
alert('Please select a valid JSON session file.');
event.target.value = '';
return;
}
try {
const content = await file.text();
const sessionState = JSON.parse(content) as SessionState;
// Basic validation
if (Array.isArray(sessionState.messages) && Array.isArray(sessionState.projectFiles) && Array.isArray(sessionState.contextFileNames)) {
chat.loadState(sessionState);
} else {
throw new Error("Invalid session file format. Missing required keys.");
}
} catch (e: any) {
console.error("Error importing session state:", e);
alert(`Could not import session file: ${e.message}`);
}
// Reset input to allow re-importing the same file
event.target.value = '';
};
const handleExportState = () => {
const sessionState: SessionState = {
messages: chat.messages,
projectFiles: chat.projectFiles,
contextFileNames: chat.contextFileNames,
selfNarrative: chat.selfNarrative,
};
const blob = new Blob([JSON.stringify(sessionState, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
a.href = url;
a.download = `reflex-session-${timestamp}.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
const handleExportAllGenerated = async (files: GeneratedFile[]) => {
if (files.length === 0) return;
const zip = new JSZip();
files.forEach(file => {
zip.file(file.name, file.content);
});
const zipBlob = await zip.generateAsync({ type: 'blob' });
const url = URL.createObjectURL(zipBlob);
const a = document.createElement('a');
a.href = url;
a.download = 'generated_files.zip';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
if (keyState === 'unknown' || keyState === 'validating') {
return (
<div className="flex flex-col items-center justify-center h-screen bg-gray-900 text-white">
<div className="text-cyan-400">
<CrystalIcon />
</div>
<h1 className="text-xl font-bold text-cyan-400 mt-4 animate-pulse">
{keyState === 'validating' ? 'Validating API Key...' : 'Initializing...'}
</h1>
</div>
);
}
if (keyState === 'error') {
return (
<div className="flex items-center justify-center h-screen bg-gray-900 text-white">
<div className="max-w-md w-full bg-red-900/50 p-8 rounded-lg border border-red-700 text-center">
<h1 className="text-2xl font-bold text-red-300">Initialization Failed</h1>
<p className="text-red-200 mt-4">{keyError}</p>
<button onClick={() => window.location.reload()} className="mt-8 w-full bg-red-600 text-white font-bold py-3 px-4 rounded-lg hover:bg-red-700 transition-colors focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-opacity-50">
Retry
</button>
</div>
</div>
);
}
if (keyState === 'needs_selection') {
return <ApiKeySelector onKeySelected={() => setKeyState('ready')} />;
}
const generatedFiles = chat.messages.flatMap(m => m.generatedFiles || []);
return (
<main className="flex h-screen bg-gray-900 text-white font-sans">
<Sidebar
projectFiles={chat.projectFiles}
generatedFiles={generatedFiles}
selfNarrative={chat.selfNarrative}
onImportFiles={handleImportFiles}
onImportState={handleImportState}
onDeleteFiles={chat.deleteFiles}
onCompareFiles={handleCompareFiles}
onToggleFileContext={chat.toggleProjectFileContext}
isFileInContext={chat.isFileInContext}
onExportAll={() => handleExportAllGenerated(generatedFiles)}
onExportState={handleExportState}
onWeaveNarrative={chat.weaveNarrative}
onShowCrystal={() => setIsCrystalPanelVisible(prev => !prev)}
isCrystalPanelVisible={isCrystalPanelVisible}
/>
<div className="flex-1 flex min-w-0">
{view === 'diff' && diffFiles ? (
<DiffViewer file1={diffFiles.file1} file2={diffFiles.file2} onExit={() => setView('chat')} />
) : (
<>
<div className="flex-1 flex flex-col min-w-0">
<ChatPanel
messages={chat.messages}
sendMessage={chat.sendMessage}
isLoading={chat.isLoading}
loadingStage={chat.loadingStage}
error={chat.error}
onToggleMessageContext={chat.toggleMessageContext}
onStopGeneration={chat.stopGeneration}
totalContextTokens={chat.totalContextTokens}
onToggleMessageCollapsed={chat.toggleMessageCollapsed}
onCollapseAll={chat.collapseAllMessages}
onExpandAll={chat.expandAllMessages}
onClearChat={chat.clearChat}
/>
</div>
{isCrystalPanelVisible && (
<div className="flex-shrink-0 w-2/5 min-w-[400px] max-w-[600px] flex flex-col border-l border-gray-700/50 bg-gray-800">
<MemoryCrystal
atoms={chat.messages}
onExit={() => setIsCrystalPanelVisible(false)}
/>
</div>
)}
</>
)}
</div>
</main>
);
}
export default App;