Spaces:
Build error
Build error
File size: 9,438 Bytes
2c11877 |
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 |
/**
* @license
* SPDX-License-Identifier: Apache-2.0
*/
/* tslint:disable */
import React, {useCallback, useEffect, useState} from 'react';
import {GeneratedContent} from './components/GeneratedContent';
import {Icon} from './components/Icon';
import {ParametersPanel} from './components/ParametersPanel';
import {Window} from './components/Window';
import {APP_DEFINITIONS_CONFIG, INITIAL_MAX_HISTORY_LENGTH} from './constants';
import {streamAppContent} from './services/geminiService';
import {AppDefinition, InteractionData} from './types';
const DesktopView: React.FC<{onAppOpen: (app: AppDefinition) => void}> = ({
onAppOpen,
}) => (
<div className="flex flex-wrap content-start p-4">
{APP_DEFINITIONS_CONFIG.map((app) => (
<Icon key={app.id} app={app} onInteract={() => onAppOpen(app)} />
))}
</div>
);
const App: React.FC = () => {
const [activeApp, setActiveApp] = useState<AppDefinition | null>(null);
const [previousActiveApp, setPreviousActiveApp] =
useState<AppDefinition | null>(null);
const [llmContent, setLlmContent] = useState<string>('');
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
const [interactionHistory, setInteractionHistory] = useState<
InteractionData[]
>([]);
const [isParametersOpen, setIsParametersOpen] = useState<boolean>(false);
const [currentMaxHistoryLength, setCurrentMaxHistoryLength] =
useState<number>(INITIAL_MAX_HISTORY_LENGTH);
// Statefulness feature state
const [isStatefulnessEnabled, setIsStatefulnessEnabled] =
useState<boolean>(false);
const [appContentCache, setAppContentCache] = useState<
Record<string, string>
>({});
const [currentAppPath, setCurrentAppPath] = useState<string[]>([]); // For UI graph statefulness
const internalHandleLlmRequest = useCallback(
async (historyForLlm: InteractionData[], maxHistoryLength: number) => {
if (historyForLlm.length === 0) {
setError('No interaction data to process.');
return;
}
setIsLoading(true);
setError(null);
let accumulatedContent = '';
// Clear llmContent before streaming new content only if not loading from cache
// This is now handled before this function is called (in handleAppOpen/handleInteraction)
// setLlmContent(''); // Removed from here, set by caller if needed
try {
const stream = streamAppContent(historyForLlm, maxHistoryLength);
for await (const chunk of stream) {
accumulatedContent += chunk;
setLlmContent((prev) => prev + chunk);
}
} catch (e: any) {
setError('Failed to stream content from the API.');
console.error(e);
accumulatedContent = `<div class="p-4 text-red-600 bg-red-100 rounded-md">Error loading content.</div>`;
setLlmContent(accumulatedContent);
} finally {
setIsLoading(false);
// Caching logic is now in useEffect watching llmContent, isLoading, activeApp, currentAppPath etc.
}
},
[],
);
// Effect to cache content when loading finishes and statefulness is enabled
useEffect(() => {
if (
!isLoading &&
currentAppPath.length > 0 &&
isStatefulnessEnabled &&
llmContent
) {
const cacheKey = currentAppPath.join('__');
// Update cache if content is different or not yet cached for this path
if (appContentCache[cacheKey] !== llmContent) {
setAppContentCache((prevCache) => ({
...prevCache,
[cacheKey]: llmContent,
}));
}
}
}, [
llmContent,
isLoading,
currentAppPath,
isStatefulnessEnabled,
appContentCache,
]);
const handleInteraction = useCallback(
async (interactionData: InteractionData) => {
if (interactionData.id === 'app_close_button') {
// This specific ID might not be generated by LLM
handleCloseAppView(); // Use existing close logic
return;
}
const newHistory = [
interactionData,
...interactionHistory.slice(0, currentMaxHistoryLength - 1),
];
setInteractionHistory(newHistory);
const newPath = activeApp
? [...currentAppPath, interactionData.id]
: [interactionData.id];
setCurrentAppPath(newPath);
const cacheKey = newPath.join('__');
setLlmContent('');
setError(null);
if (isStatefulnessEnabled && appContentCache[cacheKey]) {
setLlmContent(appContentCache[cacheKey]);
setIsLoading(false);
} else {
internalHandleLlmRequest(newHistory, currentMaxHistoryLength);
}
},
[
interactionHistory,
internalHandleLlmRequest,
activeApp,
currentMaxHistoryLength,
currentAppPath,
isStatefulnessEnabled,
appContentCache,
],
);
const handleAppOpen = (app: AppDefinition) => {
const initialInteraction: InteractionData = {
id: app.id,
type: 'app_open',
elementText: app.name,
elementType: 'icon',
appContext: app.id,
};
const newHistory = [initialInteraction];
setInteractionHistory(newHistory);
const appPath = [app.id];
setCurrentAppPath(appPath);
const cacheKey = appPath.join('__');
if (isParametersOpen) {
setIsParametersOpen(false);
}
setActiveApp(app);
setLlmContent('');
setError(null);
if (isStatefulnessEnabled && appContentCache[cacheKey]) {
setLlmContent(appContentCache[cacheKey]);
setIsLoading(false);
} else {
internalHandleLlmRequest(newHistory, currentMaxHistoryLength);
}
};
const handleCloseAppView = () => {
setActiveApp(null);
setLlmContent('');
setError(null);
setInteractionHistory([]);
setCurrentAppPath([]);
};
const handleToggleParametersPanel = () => {
setIsParametersOpen((prevIsOpen) => {
const nowOpeningParameters = !prevIsOpen;
if (nowOpeningParameters) {
// Store the currently active app (if any) so it can be restored,
// or null if no app is active (desktop view).
setPreviousActiveApp(activeApp);
setActiveApp(null); // Clear active app to show parameters panel
setLlmContent('');
setError(null);
// Interaction history and current path are not cleared here,
// as they might be relevant if the user returns to an app.
} else {
// Closing parameters panel - always go back to desktop view
setPreviousActiveApp(null); // Clear any stored previous app
setActiveApp(null); // Ensure desktop view
setLlmContent('');
setError(null);
setInteractionHistory([]); // Clear history when returning to desktop from parameters
setCurrentAppPath([]); // Clear app path
}
return nowOpeningParameters;
});
};
const handleUpdateHistoryLength = (newLength: number) => {
setCurrentMaxHistoryLength(newLength);
// Trim interaction history if new length is shorter
setInteractionHistory((prev) => prev.slice(0, newLength));
};
const handleSetStatefulness = (enabled: boolean) => {
setIsStatefulnessEnabled(enabled);
if (!enabled) {
setAppContentCache({});
}
};
const windowTitle = isParametersOpen
? 'Gemini Computer'
: activeApp
? activeApp.name
: 'Gemini Computer';
const contentBgColor = '#ffffff';
const handleMasterClose = () => {
if (isParametersOpen) {
handleToggleParametersPanel();
} else if (activeApp) {
handleCloseAppView();
}
};
return (
<div className="bg-white w-full min-h-screen flex items-center justify-center p-4">
<Window
title={windowTitle}
onClose={handleMasterClose}
isAppOpen={!!activeApp && !isParametersOpen}
appId={activeApp?.id}
onToggleParameters={handleToggleParametersPanel}
onExitToDesktop={handleCloseAppView}
isParametersPanelOpen={isParametersOpen}>
<div
className="w-full h-full"
style={{backgroundColor: contentBgColor}}>
{isParametersOpen ? (
<ParametersPanel
currentLength={currentMaxHistoryLength}
onUpdateHistoryLength={handleUpdateHistoryLength}
onClosePanel={handleToggleParametersPanel}
isStatefulnessEnabled={isStatefulnessEnabled}
onSetStatefulness={handleSetStatefulness}
/>
) : !activeApp ? (
<DesktopView onAppOpen={handleAppOpen} />
) : (
<>
{isLoading && llmContent.length === 0 && (
<div className="flex justify-center items-center h-full">
<div className="animate-spin rounded-full h-16 w-16 border-t-2 border-b-2 border-blue-500"></div>
</div>
)}
{error && (
<div className="p-4 text-red-600 bg-red-100 rounded-md">
{error}
</div>
)}
{(!isLoading || llmContent) && (
<GeneratedContent
htmlContent={llmContent}
onInteract={handleInteraction}
appContext={activeApp.id}
isLoading={isLoading}
/>
)}
</>
)}
</div>
</Window>
</div>
);
};
export default App;
|