Skip to content

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

feat(annotation): optimistic updates on annotation form (#3859)

marliessophie1770579fa685
diff --git a/web/src/ee/features/annotation-queues/components/AnnotationQueueItemPage.tsx b/web/src/ee/features/annotation-queues/components/AnnotationQueueItemPage.tsx
index e223e4756f..da559e4fae 100644
--- a/web/src/ee/features/annotation-queues/components/AnnotationQueueItemPage.tsx
+++ b/web/src/ee/features/annotation-queues/components/AnnotationQueueItemPage.tsx
@@ -56,6 +56,7 @@ const AnnotateIOView = ({
const session = useSession();
const traceId = item.parentTraceId ?? item.objectId;
const projectId = router.query.projectId as string;
+ const [showSaving, setShowSaving] = useState(false);
const [showComments, setShowComments] = useSessionStorage(
`annotationQueueShowComments-${projectId}`,
false,
@@ -175,6 +176,8 @@ const AnnotateIOView = ({
type={item.objectType.toLowerCase() as "trace" | "observation"}
isSelectHidden
queueId={item.queueId}
+ showSaving={showSaving}
+ setShowSaving={setShowSaving}
actionButtons={
isLockedByOtherUser && isPresent(item.lockedByUser?.name) ? (
<div className="flex items-center justify-center rounded-sm border border-dark-red bg-light-red p-1">
diff --git a/web/src/features/scores/components/AnnotateDrawer.tsx b/web/src/features/scores/components/AnnotateDrawer.tsx
index 3f9e8d1075..3a0d280024 100644
--- a/web/src/features/scores/components/AnnotateDrawer.tsx
+++ b/web/src/features/scores/components/AnnotateDrawer.tsx
@@ -1,4 +1,4 @@
-import React, { useState } from "react";
+import React, { useState, useEffect } from "react";
import { Button } from "@/src/components/ui/button";
import { LockIcon, SquarePen, LoaderCircle } from "lucide-react";
import {
@@ -14,6 +14,10 @@ import Header from "@/src/components/layouts/header";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
import { AnnotateDrawerContent } from "@/src/features/scores/components/AnnotateDrawerContent";
+import { useIsMutating } from "@tanstack/react-query";
+import { z } from "zod";
+
+const mutationKeySchema = z.array(z.array(z.string()));
export function AnnotateDrawer({
traceId,
@@ -39,6 +43,7 @@ export function AnnotateDrawer({
hasGroupedButton?: boolean;
}) {
const [isDrawerOpen, setIsDrawerOpen] = useState(false);
+ const [showSaving, setShowSaving] = useState(false);
const capture = usePostHogClientCapture();
const hasAccess = useHasProjectAccess({
projectId,
@@ -56,6 +61,38 @@ export function AnnotateDrawer({
const configs = configsData.data?.configs ?? [];
+ // Validate if any of the scores mutations are in progress
+ const isMutating = useIsMutating({
+ predicate: (mutation) => {
+ const mutationKey = mutation.options.mutationKey;
+
+ const parsedMutationKey = mutationKeySchema.safeParse(mutationKey);
+ if (!parsedMutationKey.success) return false;
+
+ for (const key of parsedMutationKey.data) {
+ const parsedKey = key.join(".");
+ if (parsedKey === "scores.createAnnotationScore") return true;
+ if (parsedKey === "scores.updateAnnotationScore") return true;
+ if (parsedKey === "scores.deleteAnnotationScore") return true;
+ }
+
+ return false;
+ },
+ });
+
+ useEffect(() => {
+ if (isMutating > 0) {
+ setShowSaving(true);
+ } else {
+ // Add delay before setting showSaving to ensure loading state persists for a short time after the mutation key indicates completion, allowing for any pending operations to finish
+ const timer = setTimeout(() => {
+ setShowSaving(false);
+ }, 500); // 500ms delay
+
+ return () => clearTimeout(timer);
+ }
+ }, [isMutating]);
+
if (!hasAccess && variant === "badge") return null;
return (
@@ -64,7 +101,7 @@ export function AnnotateDrawer({
{variant === "button" ? (
<Button
variant="secondary"
- disabled={!hasAccess}
+ disabled={!hasAccess || showSaving}
className={hasGroupedButton ? "rounded-r-none" : ""}
onClick={() => {
setIsDrawerOpen(true);
@@ -81,6 +118,8 @@ export function AnnotateDrawer({
>
{!hasAccess ? (
<LockIcon className="mr-1.5 h-3 w-3" />
+ ) : showSaving ? (
+ <LoaderCircle className="mr-1.5 h-4 w-4 animate-spin text-muted-foreground" />
) : (
<SquarePen className="mr-1.5 h-4 w-4" />
)}
@@ -89,7 +128,7 @@ export function AnnotateDrawer({
) : (
<Button
className="h-6 rounded-full px-3 text-xs"
- disabled={!hasAccess}
+ disabled={!hasAccess || showSaving}
onClick={() => {
setIsDrawerOpen(true);
capture(
@@ -138,6 +177,9 @@ export function AnnotateDrawer({
projectId={projectId}
type={type}
source={source}
+ showSaving={showSaving}
+ setShowSaving={setShowSaving}
+ isDrawerOpen={isDrawerOpen}
/>
)}
</DrawerContent>
diff --git a/web/src/features/scores/components/AnnotateDrawerContent.tsx b/web/src/features/scores/components/AnnotateDrawerContent.tsx
index a387165928..4724fbf114 100644
--- a/web/src/features/scores/components/AnnotateDrawerContent.tsx
+++ b/web/src/features/scores/components/AnnotateDrawerContent.tsx
@@ -1,4 +1,4 @@
-import React, { useEffect, useRef, useState } from "react";
+import React, { useCallback, useEffect, useRef, useState } from "react";
import { Button } from "@/src/components/ui/button";
import {
MessageCircleMore,
@@ -175,6 +175,22 @@ function AnnotateHeader({
);
}
+function useCustomOptimistic<State, Action>(
+ initialState: State,
+ reducer: (state: State, action: Action) => State,
+): [State, (action: Action) => void] {
+ const [state, setState] = useState<State>(initialState);
+
+ const dispatch = useCallback(
+ (action: Action) => {
+ setState((currentState) => reducer(currentState, action));
+ },
+ [reducer],
+ );
+
+ return [state, dispatch];
+}
+
export function AnnotateDrawerContent({
traceId,
scores,
@@ -183,6 +199,9 @@ export function AnnotateDrawerContent({
setEmptySelectedConfigIds,
observationId,
projectId,
+ showSaving,
+ setShowSaving,
+ isDrawerOpen = true,
type = "trace",
source = "TraceDetail",
isSelectHidden = false,
@@ -196,6 +215,9 @@ export function AnnotateDrawerContent({
setEmptySelectedConfigIds: (ids: string[]) => void;
observationId?: string;
projectId: string;
+ showSaving: boolean;
+ setShowSaving: (showSaving: boolean) => void;
+ isDrawerOpen?: boolean;
type?: "trace" | "observation" | "session";
source?: "TraceDetail" | "SessionDetail";
isSelectHidden?: boolean;
@@ -204,7 +226,6 @@ export function AnnotateDrawerContent({
}) {
const capture = usePostHogClientCapture();
const router = useRouter();
- const [showSaving, setShowSaving] = useState(false);
const form = useForm<AnnotateFormSchemaType>({
resolver: zodResolver(AnnotateFormSchema),
@@ -219,6 +240,40 @@ export function AnnotateDrawerContent({
},
});
+ const [optimisticScores, setOptimisticScore] = useCustomOptimistic<
+ AnnotateFormSchemaType["scoreData"],
+ {
+ index: number;
+ value: number | null;
+ stringValue: string | null;
+ name?: string | null;
+ dataType?: ScoreDataType | null;
+ configId?: string | null;
+ }
+ >(form.getValues().scoreData, (state, updatedScore) => {
+ const stateCopy = state.map((score, idx) =>
+ idx === updatedScore.index
+ ? {
+ ...score,
+ value: updatedScore.value,
+ stringValue: updatedScore.stringValue ?? undefined,
+ }
+ : score,
+ );
+
+ if (updatedScore.index === stateCopy.length) {
+ const newScore = {
+ name: updatedScore.name ?? "",
+ dataType: updatedScore.dataType ?? ScoreDataType.NUMERIC,
+ configId: updatedScore.configId ?? undefined,
+ value: updatedScore.value,
+ stringValue: updatedScore.stringValue ?? undefined,
+ };
+ return [...stateCopy, newScore];
+ }
+ return stateCopy;
+ });
+
const { fields, remove, update, replace } = useFieldArray({
control: form.control,
name: "scoreData",
@@ -278,6 +333,8 @@ export function AnnotateDrawerContent({
utils.traces.invalidate(),
utils.sessions.invalidate(),
]);
+
+ if (!isDrawerOpen) setShowSaving(false);
},
});
@@ -306,6 +363,8 @@ export function AnnotateDrawerContent({
utils.traces.invalidate(),
utils.sessions.invalidate(),
]);
+
+ if (!isDrawerOpen) setShowSaving(false);
};
const mutCreateScores = api.scores.createAnnotationScore.useMutation({
@@ -316,6 +375,163 @@ export function AnnotateDrawerContent({
onSettled: onSettledUpsert,
});
+ const pendingCreates = useRef(new Map<number, Promise<APIScore>>());
+
+ async function handleScoreChange(
+ score: AnnotationScoreSchemaType,
+ index: number,
+ value: number,
+ stringValue: string | null,
+ ) {
+ // Optimistically update the UI
+ setOptimisticScore({
+ index,
+ value,
+ stringValue,
+ });
+
+ try {
+ // If we have an ID, straightforward update
+ if (!!score.scoreId) {
+ const validatedScore = UpdateAnnotationScoreData.parse({
+ id: score.scoreId,
+ projectId,
+ traceId,
+ name: score.name,
+ dataType: score.dataType,
+ configId: score.configId,
+ stringValue: stringValue ?? score.stringValue,
+ comment: score.comment,
+ observationId,
+ value,
+ queueId,
+ });
+
+ await mutUpdateScores.mutateAsync({
+ ...validatedScore,
+ });
+
+ capture("score:update", {
+ type: type,
+ source: source,
+ dataType: score.dataType,
+ });
+ } else {
+ const pendingCreate = pendingCreates.current.get(index);
+
+ if (pendingCreate) {
+ // Wait for the pending create to complete to get the ID
+ const createdScore = await pendingCreate;
+ const validatedScore = UpdateAnnotationScoreData.parse({
+ id: createdScore.id,
+ projectId,
+ traceId,
+ name: score.name,
+ dataType: score.dataType,
+ configId: score.configId,
+ stringValue: stringValue ?? score.stringValue,
+ comment: score.comment,
+ observationId,
+ value,
+ queueId,
+ });
+
+ await mutUpdateScores.mutateAsync({
+ ...validatedScore,
+ });
+
+ capture("score:update", {
+ type: type,
+ source: source,
+ dataType: score.dataType,
+ });
+ } else {
+ // If no pending create, straightforward create
+ const validatedScore = CreateAnnotationScoreData.parse({
+ projectId,
+ traceId,
+ name: score.name,
+ dataType: score.dataType,
+ configId: score.configId,
+ stringValue: stringValue ?? score.stringValue,
+ comment: score.comment,
+ observationId,
+ value,
+ queueId,
+ });
+
+ const createPromise = mutCreateScores.mutateAsync({
+ ...validatedScore,
+ });
+
+ capture("score:create", {
+ type: type,
+ source: source,
+ dataType: score.dataType,
+ });
+
+ pendingCreates.current.set(index, createPromise);
+
+ // Wait for creation and cleanup
+ const createdScore = await createPromise;
+ pendingCreates.current.delete(index);
+
+ // Update the form with the new ID
+ update(index, {
+ ...score,
+ scoreId: createdScore.id,
+ value: createdScore.value,
+ });
+ }
+ }
+ } catch (error) {
+ // Handle error and revert optimistic update
+ console.error(error);
+ setOptimisticScore({
+ index,
+ value: score.value ?? null,
+ stringValue: score.stringValue ?? null,
+ });
+ }
+ }
+
+ function handleOnBlur({
+ config,
+ field,
+ index,
+ score,
+ }: {
+ config: ValidatedScoreConfig;
+ field: ControllerRenderProps<
+ AnnotateFormSchemaType,
+ `scoreData.${number}.value`
+ >;
+ index: number;
+ score: AnnotationScoreSchemaType;
+ }): React.FocusEventHandler<HTMLInputElement> | undefined {
+ return async () => {
+ const { maxValue, minValue, dataType } = config;
+
+ if (isNumericDataType(dataType)) {
+ const formError = getFormError({
+ value: field.value,
+ maxValue,
+ minValue,
+ });
+ if (!!formError) {
+ form.setError(`scoreData.${index}.value`, formError);
+ return;
+ }
+ }
+
+ form.clearErrors(`scoreData.${index}.value`);
+
+ if (isPresent(field.value)) {
+ await handleScoreChange(score, index, Number(field.value), null);
+ }
+ };
+ }
+
useEffect(() => {
if (
mutUpdateScores.isLoading ||
@@ -324,16 +540,13 @@ export function AnnotateDrawerContent({
) {
setShowSaving(true);
} else {
- const timer = setTimeout(() => {
- setShowSaving(false);
- }, 300); // Keep saving message for 1 second after loading
-
- return () => clearTimeout(timer); // Cleanup timer on unmount or when loading state changes
+ setShowSaving(false);
}
}, [
mutUpdateScores.isLoading,
mutCreateScores.isLoading,
mutDeleteScore.isLoading,
+ setShowSaving,
]);
function handleOnCheckedChange(
@@ -359,6 +572,14 @@ export function AnnotateDrawerContent({
const index = fields.findIndex(({ configId }) => configId === id);
if (index === -1) {
+ setOptimisticScore({
+ index: fields.length,
+ value: null,
+ stringValue: null,
+ name,
+ dataType,
+ configId: id,
+ });
replace([
...fields,
{
@@ -388,59 +609,10 @@ export function AnnotateDrawerContent({
if (selectedCategory) {
const newValue = Number(selectedCategory.value);
- update(index, {
- ...score,
- value: newValue,
+ await handleScoreChange(score, index, newValue, stringValue);
+ form.setValue(`scoreData.${index}.value`, newValue, {
+ shouldValidate: true,
});
-
- if (!!stringValue) {
- if (!!score.scoreId) {
- const validatedScore = UpdateAnnotationScoreData.parse({
- id: score.scoreId,
- projectId,
- traceId,
- name: score.name,
- dataType: score.dataType,
- configId: score.configId,
- comment: score.comment,
- observationId,
- value: newValue,
- stringValue,
- queueId,
- });
-
- await mutUpdateScores.mutateAsync({
- ...validatedScore,
- });
- capture("score:update", {
- type: type,
- source: source,
- dataType: score.dataType,
- });
- } else {
- const validatedScore = CreateAnnotationScoreData.parse({
- projectId,
- traceId,
- name: score.name,
- dataType: score.dataType,
- configId: score.configId,
- comment: score.comment,
- observationId,
- value: newValue,
- stringValue,
- queueId,
- });
-
- await mutCreateScores.mutateAsync({
- ...validatedScore,
- });
- capture("score:create", {
- type: type,
- source: source,
- dataType: score.dataType,
- });
- }
- }
}
};
}
@@ -486,90 +658,6 @@ export function AnnotateDrawerContent({
};
}
- function handleOnBlur({
- config,
- field,
- index,
- score,
- }: {
- config: ValidatedScoreConfig;
- field: ControllerRenderProps<
- AnnotateFormSchemaType,
- `scoreData.${number}.value`
- >;
- index: number;
- score: AnnotationScoreSchemaType;
- }): React.FocusEventHandler<HTMLInputElement> | undefined {
- return async () => {
- const { maxValue, minValue, dataType } = config;
-
- if (isNumericDataType(dataType)) {
- const formError = getFormError({
- value: field.value,
- maxValue,
- minValue,
- });
- if (!!formError) {
- form.setError(`scoreData.${index}.value`, formError);
- return;
- }
- }
-
- form.clearErrors(`scoreData.${index}.value`);
-
- if (isPresent(field.value)) {
- if (!!score.scoreId) {
- const validatedScore = UpdateAnnotationScoreData.parse({
- id: score.scoreId,
- projectId,
- traceId,
- name: score.name,
- dataType: score.dataType,
- configId: score.configId,
- stringValue: score.stringValue,
- comment: score.comment,
- observationId,
- value: Number(field.value),
- queueId,
- });
-
- await mutUpdateScores.mutateAsync({
- ...validatedScore,
- });
-
- capture("score:update", {
- type: type,
- source: source,
- dataType: score.dataType,
- });
- } else {
- const validatedScore = CreateAnnotationScoreData.parse({
- projectId,
- traceId,
- name: score.name,
- dataType: score.dataType,
- configId: score.configId,
- stringValue: score.stringValue,
- comment: score.comment,
- observationId,
- value: Number(field.value),
- queueId,
- });
-
- await mutCreateScores.mutateAsync({
- ...validatedScore,
- });
-
- capture("score:create", {
- type: type,
- source: source,
- dataType: score.dataType,
- });
- }
- }
- };
- }
-
return (
<div className="mx-auto w-full overflow-y-auto md:max-h-full">
<DrawerHeader className="sticky top-0 z-10 rounded-sm bg-background">
@@ -606,9 +694,15 @@ export function AnnotateDrawerContent({
.map((config) => ({
key: config.id,
value: `${getScoreDataTypeIcon(config.dataType)} ${config.name}`,
- disabled: fields.some(
- (field) => !!field.scoreId && field.configId === config.id,
- ),
+ disabled:
+ fields.some(
+ (field) =>
+ !!field.scoreId && field.configId === config.id,
+ ) ||
+ optimisticScores.some(
+ (score) => score.configId === config.id && !!score.value,
+ ) ||
+ mutDeleteScore.isLoading,
isArchived: config.isArchived,
}))}
values={fields
@@ -824,15 +918,20 @@ export function AnnotateDrawerContent({
{isNumericDataType(score.dataType) ? (
<Input
{...field}
- value={field.value ?? ""}
+ value={
+ optimisticScores[index].value ?? ""
+ }
// manually manage controlled input state
onChange={(e) => {
const value = e.target.value;
- field.onChange(
- value === ""
- ? undefined
- : Number(value),
- );
+ const numValue =
+ value === "" ? null : Number(value);
+ setOptimisticScore({
+ index,
+ value: numValue,
+ stringValue: null,
+ });
+ field.onChange(numValue);
}}
type="number"
className="text-xs"
@@ -866,6 +965,10 @@ export function AnnotateDrawerContent({
renderSelect(categories) ? (
<Select
name={field.name}
+ value={
+ optimisticScores[index].stringValue ??
+ ""
+ }
defaultValue={score.stringValue}
disabled={config.isArchived}
onValueChange={handleOnValueChange(
@@ -896,6 +999,10 @@ export function AnnotateDrawerContent({
) : (
<ToggleGroup
type="single"
+ value={
+ optimisticScores[index].stringValue ??
+ ""
+ }
defaultValue={score.stringValue}
disabled={config.isArchived}
className={`grid grid-cols-${categories.length}`}
@@ -958,6 +1065,11 @@ export function AnnotateDrawerContent({
loading={mutDeleteScore.isLoading}
onClick={async () => {
if (score.scoreId) {
+ setOptimisticScore({
+ index,
+ value: null,
+ stringValue: null,
+ });
await mutDeleteScore.mutateAsync({
id: score.scoreId,
projectId,
@@ -984,9 +1096,20 @@ export function AnnotateDrawerContent({
className="px-0 pl-1"
title="Delete score from trace/observation"
disabled={isScoreUnsaved(score.scoreId)}
- loading={mutDeleteScore.isLoading}
+ loading={
+ mutDeleteScore.isLoading &&
+ !optimisticScores.some(
+ (s) => s.scoreId === score.scoreId,
+ ) &&
+ !isScoreUnsaved(score.scoreId)
+ }
onClick={async () => {
if (score.scoreId) {
+ setOptimisticScore({
+ index,
+ value: null,
+ stringValue: null,
+ });
await mutDeleteScore.mutateAsync({
id: score.scoreId,
projectId,
diff --git a/web/src/server/api/routers/scores.ts b/web/src/server/api/routers/scores.ts
index 726ca5221e..20c0781aaf 100644
--- a/web/src/server/api/routers/scores.ts
+++ b/web/src/server/api/routers/scores.ts
@@ -466,24 +466,23 @@ export const scoresRouter = createTRPCRouter({
// Audit log only if Postgres is not enabled, as we still run PG and CH ingestion in parallel on cloud
if (env.LANGFUSE_POSTGRES_INGESTION_ENABLED === "false") {
+ const updatedScore = {
+ ...clickhouseScore,
+ value: input.value ?? null,
+ stringValue: input.stringValue ?? null,
+ comment: input.comment ?? null,
+ authorUserId: ctx.session.user.id,
+ queueId: input.queueId ?? null,
+ };
await auditLog({
session: ctx.session,
resourceType: "score",
resourceId: input.id,
action: "update",
before: clickhouseScore,
- after: {
- ...clickhouseScore,
- timestamp: convertDateToClickhouseDateTime(
- clickhouseScore.timestamp,
- ),
- value: input.value !== null ? input.value : undefined,
- stringValue: input.stringValue,
- comment: input.comment,
- authorUserId: ctx.session.user.id,
- queueId: input.queueId,
- },
+ after: updatedScore,
});
+ return validateDbScore(updatedScore);
}
}
}