🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(web): open additional playground window if promt open and edited (#17274)
tobi_kl720b46a76e2a
diff --git a/web/src/features/playground/page/components/JumpToPlaygroundDropdownMenuController.tsx b/web/src/features/playground/page/components/JumpToPlaygroundDropdownMenuController.tsxindex 7d326177fc..fc63e53db0 100644--- a/web/src/features/playground/page/components/JumpToPlaygroundDropdownMenuController.tsx+++ b/web/src/features/playground/page/components/JumpToPlaygroundDropdownMenuController.tsx@@ -11,6 +11,11 @@ import {type PlaygroundTool,} from "@/src/features/playground/page/types";import { getMessagesFingerprint } from "@/src/features/playground/page/utils/messagesFingerprint";+import { resolveJumpTargetWindowId } from "@/src/features/playground/page/utils/resolveJumpTargetWindowId";+import {+ getWindowState,+ setWindowState,+} from "@/src/features/playground/page/storage/windowStorage";import { usePostHogClientCapture } from "@/src/features/posthog-analytics";import useProjectIdFromURL from "@/src/hooks/useProjectIdFromURL";import {@@ -32,7 +37,6 @@ import { normalizeInput, normalizeOutput } from "@/src/utils/chatml";import { extractTools } from "@/src/utils/chatml/extractTools";import { convertChatMlToPlayground } from "@/src/utils/chatml/playgroundConverter";import { api } from "@/src/utils/api";-import usePlaygroundCache from "@/src/features/playground/page/hooks/usePlaygroundCache";import {type MetadataDomainClient,type WithStringifiedMetadata,@@ -77,7 +81,7 @@ export const JumpToPlaygroundDropdownMenuController = (const router = useRouter();const capture = usePostHogClientCapture();const projectId = useProjectIdFromURL();- const { addWindowWithId, clearAllCache } = usePersistedWindowIds();+ const { windowIds, addWindowWithId, clearAllCache } = usePersistedWindowIds();const [includeOutput, setIncludeOutput] = useState(false);// Generate a stable window ID based on the source data@@ -86,7 +90,6 @@ export const JumpToPlaygroundDropdownMenuController = (const stableWindowId = useMemo(() => {return `playground-${props.source}-${sourceId}`;}, [props.source, sourceId]);- const { setPlaygroundCache } = usePlaygroundCache(stableWindowId);const apiKeys = api.llmApiKey.all.useQuery({@@ -139,12 +142,20 @@ export const JumpToPlaygroundDropdownMenuController = (return;}+ let targetWindowId = stableWindowId;+if (useFreshPlayground) {// Clear all existing playground data and reset to single windowclearAllCache(stableWindowId);} else {- // Add to existing playground- const addedWindowId = addWindowWithId(stableWindowId);+ targetWindowId = resolveJumpTargetWindowId({+ stableWindowId,+ openWindowIds: windowIds,+ incomingMessages: capturedState.messages,+ getCachedMessages: (windowId) => getWindowState(windowId)?.messages,+ });++ const addedWindowId = addWindowWithId(targetWindowId);if (!addedWindowId) {console.warn(@@ -157,9 +168,9 @@ export const JumpToPlaygroundDropdownMenuController = (// Use requestAnimationFrame to ensure the state update has been processedrequestAnimationFrame(() => {try {- setPlaygroundCache(capturedState);+ setWindowState(targetWindowId, capturedState);console.log(- `Cache saved for existing playground window ${stableWindowId}`,+ `Cache saved for existing playground window ${targetWindowId}`,);// Navigate after cache is successfully saveddiff --git a/web/src/features/playground/page/storage/windowStorage.ts b/web/src/features/playground/page/storage/windowStorage.tsindex ec8f106204..0b61d38ff9 100644--- a/web/src/features/playground/page/storage/windowStorage.ts+++ b/web/src/features/playground/page/storage/windowStorage.ts@@ -6,10 +6,6 @@ import {WINDOW_IDS_KEY,} from "./keys";-/**- * Retrieves the list of window IDs from sessionStorage.- * @returns An array of window IDs.- */export const getWindowIds = (): string[] | null => {const saved = sessionStorage.getItem(WINDOW_IDS_KEY);if (saved) {@@ -26,20 +22,11 @@ export const getWindowIds = (): string[] | null => {return null;};-/**- * Saves the list of window IDs to sessionStorage.- * @param ids - The array of window IDs to save.- */export const saveWindowIds = (ids: string[]): void => {sessionStorage.setItem(WINDOW_IDS_KEY, JSON.stringify(ids));};-/**- * Retrieves the cached state for a specific window.- * @param windowId - The ID of the window.- * @returns The cached state or null if not found.- */-const getWindowState = (windowId: string): PlaygroundCache | null => {+export const getWindowState = (windowId: string): PlaygroundCache | null => {const key = getCacheKey(windowId);const cachedState = sessionStorage.getItem(key);if (!cachedState) return null;@@ -52,11 +39,18 @@ const getWindowState = (windowId: string): PlaygroundCache | null => {}};-/**- * Clones the entire state (cache and model params) from a source window to a target window.- * @param sourceWindowId - The ID of the window to copy from.- * @param targetWindowId - The ID of the window to copy to.- */+export const setWindowState = (+ windowId: string,+ cache: PlaygroundCache,+): void => {+ const key = getCacheKey(windowId);+ if (cache === null) {+ sessionStorage.removeItem(key);+ return;+ }+ sessionStorage.setItem(key, JSON.stringify(cache));+};+export const cloneWindowState = (sourceWindowId: string,targetWindowId: string,@@ -95,19 +89,12 @@ export const cloneWindowState = (}};-/**- * Removes all storage associated with a single window.- * @param windowId - The ID of the window to remove.- */export const removeWindowState = (windowId: string): void => {sessionStorage.removeItem(getCacheKey(windowId));sessionStorage.removeItem(getModelNameKey(windowId));sessionStorage.removeItem(getModelProviderKey(windowId));};-/**- * Clears all playground-related data from storage.- */export const clearAllPlaygroundData = (): void => {const sessionKeysToRemove: string[] = [];for (let i = 0; i < sessionStorage.length; i++) {diff --git a/web/src/features/playground/page/utils/resolveJumpTargetWindowId.clienttest.ts b/web/src/features/playground/page/utils/resolveJumpTargetWindowId.clienttest.tsnew file mode 100644index 0000000000..812b718ed4--- /dev/null+++ b/web/src/features/playground/page/utils/resolveJumpTargetWindowId.clienttest.ts@@ -0,0 +1,89 @@+import { describe, expect, it } from "vitest";++import {+ ChatMessageRole,+ ChatMessageType,+ type ChatMessage,+ type PlaceholderMessage,+} from "@langfuse/shared";++import { resolveJumpTargetWindowId } from "./resolveJumpTargetWindowId";++const message = (content: string, id = "generated-on-hydration"): ChatMessage =>+ ({+ id,+ type: ChatMessageType.System,+ role: ChatMessageRole.System,+ content,+ }) as ChatMessage;++const stableWindowId = "playground-prompt-abc";+const incomingMessages = [message("You are a helpful assistant")];+const storedPrompt = [message("You are a helpful assistant", "other-id")];+const editedPrompt = [message("You are a pirate")];+const UUID = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";++const caches = (+ entries: Record<string, (ChatMessage | PlaceholderMessage)[]>,+) => ({+ openWindowIds: Object.keys(entries),+ getCachedMessages: (windowId: string) => entries[windowId],+});++describe("resolveJumpTargetWindowId", () => {+ it("addresses the stable window whenever nothing would be lost", () => {+ // Not open at all.+ expect(+ resolveJumpTargetWindowId({+ stableWindowId,+ openWindowIds: ["some-other-window"],+ incomingMessages,+ getCachedMessages: () => editedPrompt,+ }),+ ).toBe(stableWindowId);++ // Open and still holding the stored prompt. Message ids are assigned on+ // hydration, so they must not count towards the comparison.+ expect(+ resolveJumpTargetWindowId({+ stableWindowId,+ incomingMessages,+ ...caches({ [stableWindowId]: storedPrompt }),+ }),+ ).toBe(stableWindowId);++ // Open with nothing cached yet.+ expect(+ resolveJumpTargetWindowId({+ stableWindowId,+ openWindowIds: [stableWindowId],+ incomingMessages,+ getCachedMessages: () => undefined,+ }),+ ).toBe(stableWindowId);+ });++ it("mints a sibling once the stable window is edited, then jumps back into it", () => {+ const edited = { [stableWindowId]: editedPrompt };++ const siblingId = resolveJumpTargetWindowId({+ stableWindowId,+ incomingMessages,+ ...caches(edited),+ });++ expect(siblingId).toMatch(new RegExp(`^${stableWindowId}-${UUID}$`));++ expect(+ resolveJumpTargetWindowId({+ stableWindowId,+ incomingMessages,+ ...caches({+ ...edited,+ [`${stableWindowId}-def`]: storedPrompt,+ [siblingId]: storedPrompt,+ }),+ }),+ ).toBe(siblingId);+ });+});diff --git a/web/src/features/playground/page/utils/resolveJumpTargetWindowId.ts b/web/src/features/playground/page/utils/resolveJumpTargetWindowId.tsnew file mode 100644index 0000000000..5cefab4cf0--- /dev/null+++ b/web/src/features/playground/page/utils/resolveJumpTargetWindowId.ts@@ -0,0 +1,52 @@+import { v4 as uuidv4 } from "uuid";++import { type ChatMessage, type PlaceholderMessage } from "@langfuse/shared";++import { getMessagesFingerprint } from "./messagesFingerprint";++type ResolveJumpTargetWindowIdArgs = {+ stableWindowId: string;+ openWindowIds: string[];+ incomingMessages: (ChatMessage | PlaceholderMessage)[];+ getCachedMessages: (+ windowId: string,+ ) => (ChatMessage | PlaceholderMessage)[] | undefined;+};++const UUID_PATTERN =+ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;++/**+ * we track if a window exists for current prompt by having a stable window id + random string+ * we use UUID as random string because it's easy to validate against+ */+const isSiblingOf = (stableWindowId: string, windowId: string): boolean =>+ windowId.startsWith(`${stableWindowId}-`) &&+ UUID_PATTERN.test(windowId.slice(stableWindowId.length + 1));++export const resolveJumpTargetWindowId = ({+ stableWindowId,+ openWindowIds,+ incomingMessages,+ getCachedMessages,+}: ResolveJumpTargetWindowIdArgs): string => {+ if (!openWindowIds.includes(stableWindowId)) return stableWindowId;++ const incomingFingerprint = getMessagesFingerprint(incomingMessages);+ const holdsIncomingMessages = (windowId: string): boolean => {+ const cachedMessages = getCachedMessages(windowId);+ // Open but empty, so nothing to lose either.+ if (!cachedMessages) return true;++ return getMessagesFingerprint(cachedMessages) === incomingFingerprint;+ };++ if (holdsIncomingMessages(stableWindowId)) return stableWindowId;++ const reusableSiblingId = openWindowIds.find(+ (windowId) =>+ isSiblingOf(stableWindowId, windowId) && holdsIncomingMessages(windowId),+ );++ return reusableSiblingId ?? `${stableWindowId}-${uuidv4()}`;+};