Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(playground): allow saving new prompts after editing in playground (#11648)

Nimar3bf32dd98143
diff --git a/web/src/features/playground/page/hooks/usePlaygroundCache.ts b/web/src/features/playground/page/hooks/usePlaygroundCache.ts
index 57dbbfe0cb..4961a8bbcd 100644
--- a/web/src/features/playground/page/hooks/usePlaygroundCache.ts
+++ b/web/src/features/playground/page/hooks/usePlaygroundCache.ts
@@ -2,57 +2,41 @@ import { useEffect, useState } from "react";
import { type PlaygroundCache } from "../types";
import { getCacheKey } from "../storage/keys";
+const readCache = (key: string): PlaygroundCache => {
+ if (typeof window === "undefined") return null;
+ const saved = sessionStorage.getItem(key);
+ if (!saved) return null;
+ try {
+ return JSON.parse(saved);
+ } catch {
+ sessionStorage.removeItem(key);
+ return null;
+ }
+};
+
/**
* Hook for managing playground cache with window isolation support.
- * Relies on storage utilities for key generation and persistence.
- *
- * @param windowId - Optional window identifier for state isolation.
- * @returns Object with playgroundCache state and setPlaygroundCache function.
+ * Cache is read synchronously to avoid race conditions with effects
+ * that depend on playgroundCache being available on first render.
*/
export default function usePlaygroundCache(windowId?: string) {
- const [cache, setCache] = useState<PlaygroundCache>(null);
+ const cacheKey = getCacheKey(windowId ?? "");
+ const [cache, setCache] = useState<PlaygroundCache>(() =>
+ readCache(cacheKey),
+ );
- const playgroundCacheKey = getCacheKey(windowId ?? "");
+ // Re-read when key changes (windowId change)
+ useEffect(() => {
+ setCache(readCache(cacheKey));
+ }, [cacheKey]);
- /**
- * Set playground cache for this specific window.
- * Stores the cache in sessionStorage with a window-specific key.
- *
- * @param cache - PlaygroundCache object to store, or null to clear
- */
- const setPlaygroundCache = (cache: PlaygroundCache) => {
- if (cache === null) {
- sessionStorage.removeItem(playgroundCacheKey);
+ const setPlaygroundCache = (newCache: PlaygroundCache) => {
+ if (newCache === null) {
+ sessionStorage.removeItem(cacheKey);
} else {
- sessionStorage.setItem(playgroundCacheKey, JSON.stringify(cache));
+ sessionStorage.setItem(cacheKey, JSON.stringify(newCache));
}
};
- /**
- * Load cached state from sessionStorage on mount and when windowId changes
- * Attempts to parse JSON and handles errors gracefully
- */
- useEffect(() => {
- const savedCache = sessionStorage.getItem(playgroundCacheKey);
- if (savedCache) {
- try {
- setCache(JSON.parse(savedCache));
- } catch (e) {
- console.error(
- `Failed to parse playground cache for window ${windowId}`,
- e,
- );
- // Clear corrupted cache
- sessionStorage.removeItem(playgroundCacheKey);
- setCache(null);
- }
- } else {
- setCache(null);
- }
- }, [playgroundCacheKey, windowId]);
-
- return {
- playgroundCache: cache,
- setPlaygroundCache: setPlaygroundCache,
- };
+ return { playgroundCache: cache, setPlaygroundCache };
}
diff --git a/web/src/features/prompts/components/NewPromptForm/index.tsx b/web/src/features/prompts/components/NewPromptForm/index.tsx
index fae001e394..729eac240c 100644
--- a/web/src/features/prompts/components/NewPromptForm/index.tsx
+++ b/web/src/features/prompts/components/NewPromptForm/index.tsx
@@ -207,7 +207,7 @@ export const NewPromptForm: React.FC<NewPromptFormProps> = (props) => {
formId,
projectId: projectId ?? "",
form,
- enabled: Boolean(projectId),
+ enabled: Boolean(projectId) && !shouldLoadPlaygroundCache,
onDraftRestored: (draft) => {
// Restore chat messages if present
if (