🪢 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): keep LLM connection editor stable during list updates (#17576)
Nikita Kabardinf4dccff9b56d
diff --git a/web/src/features/public-api/components/LLMApiKeyList.clienttest.tsx b/web/src/features/public-api/components/LLMApiKeyList.clienttest.tsxnew file mode 100644index 0000000000..df251c3553--- /dev/null+++ b/web/src/features/public-api/components/LLMApiKeyList.clienttest.tsx@@ -0,0 +1,96 @@+import { fireEvent, render, screen, within } from "@testing-library/react";++import { LayerProvider } from "@/src/context/LayerContext/LayerContext";+import { api } from "@/src/utils/api";+import { LlmApiKeyList } from "./LLMApiKeyList";++vi.mock("@/src/components/layouts/header", () => ({+ default: () => null,+}));+vi.mock("@/src/features/rbac", () => ({+ useHasProjectAccess: () => true,+}));+vi.mock("@/src/features/posthog-analytics", () => ({+ usePostHogClientCapture: () => vi.fn(),+}));+vi.mock("@/src/ee/features/ui-customization/useUiCustomization", () => ({+ useUiCustomization: () => undefined,+}));+vi.mock("./CreateLLMApiKeyDialog", () => ({+ CreateLLMApiKeyDialog: () => null,+}));+vi.mock("./CreateLLMApiKeyForm", () => ({+ CreateLLMApiKeyForm: ({+ existingKey,+ onSuccess,+ }: {+ existingKey: { provider: string };+ onSuccess: () => void;+ }) => (+ <>+ <output>{existingKey.provider}</output>+ <input aria-label="API key" defaultValue="" />+ <button onClick={onSuccess}>Save connection</button>+ </>+ ),+}));+vi.mock("@/src/utils/api", () => ({+ api: {+ llmApiKey: {+ all: { useQuery: vi.fn() },+ delete: { useMutation: () => ({ isPending: false }) },+ },+ useUtils: () => ({ llmApiKey: { invalidate: vi.fn() } }),+ },+ reportNonTrpcError: vi.fn(),+}));++describe("LLM connection editing", () => {+ it("preserves the dialog and draft across list updates, then opens a fresh form for another connection", () => {+ const keys = ["First", "Second"].map((provider) => ({+ id: provider,+ provider,+ adapter: "openai",+ baseURL: null,+ displaySecretKey: "sk-...test",+ extraHeaderKeys: [] as string[],+ }));+ const query = {+ data: { data: keys },+ isLoading: false,+ isError: false,+ };+ vi.mocked(api.llmApiKey.all.useQuery).mockReturnValue(+ query as ReturnType<typeof api.llmApiKey.all.useQuery>,+ );++ const { rerender } = render(<LlmApiKeyList projectId="project" />, {+ wrapper: LayerProvider,+ });+ fireEvent.click(screen.getByRole("row", { name: "Edit First connection" }));+ const dialog = screen.getByRole("dialog");+ fireEvent.change(screen.getByRole("textbox", { name: "API key" }), {+ target: { value: "Unsaved draft" },+ });++ query.data = {+ data: keys.map((key) => ({ ...key, extraHeaderKeys: ["X-Test"] })),+ };+ rerender(<LlmApiKeyList projectId="project" />);++ expect(screen.getByRole("dialog")).toBe(dialog);+ expect(screen.getByRole("textbox", { name: "API key" })).toHaveValue(+ "Unsaved draft",+ );+ fireEvent.click(screen.getByRole("button", { name: "Save connection" }));+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();+ fireEvent.keyDown(+ screen.getByRole("row", { name: "Edit Second connection" }),+ { key: "Enter" },+ );+ expect(+ within(screen.getByRole("dialog")).getByText("Second"),+ ).toBeInTheDocument();+ expect(screen.getByRole("textbox", { name: "API key" })).toHaveValue("");+ });+});diff --git a/web/src/features/public-api/components/LLMApiKeyList.tsx b/web/src/features/public-api/components/LLMApiKeyList.tsxindex ad7215accd..f4a9248af5 100644--- a/web/src/features/public-api/components/LLMApiKeyList.tsx+++ b/web/src/features/public-api/components/LLMApiKeyList.tsx@@ -1,5 +1,5 @@import { useHasProjectAccess } from "@/src/features/rbac";-import { TrashIcon } from "lucide-react";+import { PencilIcon, TrashIcon } from "lucide-react";import { useState } from "react";import Header from "@/src/components/layouts/header";import { Button } from "@/src/components/ui/button";@@ -18,7 +18,6 @@ import { type RouterOutput } from "@/src/utils/types";type LlmApiKeyRow = RouterOutput["llmApiKey"]["all"]["data"][number];export function LlmApiKeyList(props: { projectId: string }) {- const [editingKeyId, setEditingKeyId] = useState<string | null>(null);const [open, setOpen] = useState(false);const hasAccess = useHasProjectAccess({@@ -47,7 +46,9 @@ export function LlmApiKeyList(props: { projectId: string }) {(key) => key.extraHeaderKeys.length > 0,);- const columns: LangfuseColumnDef<LlmApiKeyRow>[] = [+ const getColumns = (+ openDialog: (apiKey: LlmApiKeyRow) => void,+ ): LangfuseColumnDef<LlmApiKeyRow>[] => [{accessorKey: "provider",header: "Provider",@@ -95,18 +96,14 @@ export function LlmApiKeyList(props: { projectId: string }) {return (<div data-row-click-ignore className="flex justify-end space-x-2">{hasUpdateAccess && (- <UpdateLLMApiKeyDialog- apiKey={apiKey}- projectId={props.projectId}- open={editingKeyId === apiKey.id}- onOpenChange={(open: boolean) => {- if (open) {- setEditingKeyId(apiKey.id);- } else {- setEditingKeyId(null);- }- }}- />+ <Button+ variant="ghost"+ size="icon"+ aria-label={`Edit ${apiKey.provider} connection`}+ onClick={() => openDialog(apiKey)}+ >+ <PencilIcon className="h-4 w-4" />+ </Button>)}{hasDeleteAccess && (<DeleteApiKeyButton@@ -141,26 +138,32 @@ export function LlmApiKeyList(props: { projectId: string }) {Connect your LLM services to enable evaluations and playground features.Your provider will charge based on usage.</p>- <Card className="mb-4 overflow-auto">- <SimpleDataTable- columns={columns}- data={apiKeys.data?.data ?? []}- isLoading={apiKeys.isLoading}- noResults={- apiKeys.isError ? (- <span className="text-destructive">- Failed to load LLM connections. Please try again.- </span>- ) : (- "None"- )- }- bodyTone="muted"- rowVariant="primary-hover-static"- onRowClick={(apiKey) => setEditingKeyId(apiKey.id)}- getRowLabel={(apiKey) => `Edit ${apiKey.provider} connection`}- />- </Card>+ <UpdateLLMApiKeyDialog projectId={props.projectId}>+ {({ openDialog }) => (+ <Card className="mb-4 overflow-auto">+ <SimpleDataTable+ columns={getColumns(openDialog)}+ data={apiKeys.data?.data ?? []}+ isLoading={apiKeys.isLoading}+ noResults={+ apiKeys.isError ? (+ <span className="text-destructive">+ Failed to load LLM connections. Please try again.+ </span>+ ) : (+ "None"+ )+ }+ bodyTone="muted"+ rowVariant="primary-hover-static"+ onRowClick={(apiKey) => {+ if (hasUpdateAccess) openDialog(apiKey);+ }}+ getRowLabel={(apiKey) => `Edit ${apiKey.provider} connection`}+ />+ </Card>+ )}+ </UpdateLLMApiKeyDialog><CreateLLMApiKeyDialog open={open} setOpen={setOpen} /></div>);diff --git a/web/src/features/public-api/components/UpdateLLMApiKeyDialog.tsx b/web/src/features/public-api/components/UpdateLLMApiKeyDialog.tsxindex 9909af85d6..a5280b4618 100644--- a/web/src/features/public-api/components/UpdateLLMApiKeyDialog.tsx+++ b/web/src/features/public-api/components/UpdateLLMApiKeyDialog.tsx@@ -1,56 +1,46 @@-/* eslint-disable @repo/no-abstracted-overlay-trigger */-import { Button } from "@/src/components/ui/button";+import { type ComponentProps } from "react";+import { DialogController } from "@/src/components/design-system/DialogController/DialogController";import {- Dialog,DialogContent,DialogHeader,DialogTitle,- DialogTrigger,} from "@/src/components/ui/dialog";import { CreateLLMApiKeyForm } from "./CreateLLMApiKeyForm";import { useUiCustomization } from "@/src/ee/features/ui-customization/useUiCustomization";-import { PencilIcon } from "lucide-react";import { type RouterOutputs } from "@/src/utils/api";type LlmApiKeyListItem = RouterOutputs["llmApiKey"]["all"]["data"][number];export function UpdateLLMApiKeyDialog({- apiKey,projectId,- open,- onOpenChange,+ children,}: {- apiKey: LlmApiKeyListItem;projectId: string;- open: boolean;- onOpenChange: (open: boolean) => void;+ children: ComponentProps<+ typeof DialogController<LlmApiKeyListItem>+ >["children"];}) {const uiCustomization = useUiCustomization();return (- <Dialog open={open} onOpenChange={onOpenChange}>- <DialogTrigger asChild>- <Button variant="ghost" size="icon">- <PencilIcon className="h-4 w-4" />- </Button>- </DialogTrigger>- <DialogContent- className="max-h-[90%] min-w-[40vw] overflow-auto"- onClick={(e) => e.stopPropagation()}- >- <DialogHeader>- <DialogTitle>Update LLM Connection</DialogTitle>- </DialogHeader>- {open && (+ <DialogController<LlmApiKeyListItem>+ renderDialog={({ state: apiKey, closeDialog }) => (+ <DialogContent className="max-h-[90%] min-w-[40vw] overflow-auto">+ <DialogHeader>+ <DialogTitle>Update LLM Connection</DialogTitle>+ </DialogHeader><CreateLLMApiKeyForm+ key={apiKey.id}projectId={projectId}- onSuccess={() => onOpenChange(false)}+ onSuccess={closeDialog}customization={uiCustomization}mode="update"existingKey={apiKey}/>- )}- </DialogContent>- </Dialog>+ </DialogContent>+ )}+ >+ {children}+ </DialogController>);}