Skip to content

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

feat(annotation-queues): add support for batch adding observations to annotation queues (#8261)

marliessophie8c8c488e4d93
diff --git a/packages/shared/src/features/batchAction/types.ts b/packages/shared/src/features/batchAction/types.ts
index f4ba0b9bb6..7ac6104692 100644
--- a/packages/shared/src/features/batchAction/types.ts
+++ b/packages/shared/src/features/batchAction/types.ts
@@ -15,6 +15,7 @@ const ActionIdSchema = z.enum([
"trace-delete",
"trace-add-to-annotation-queue",
"session-add-to-annotation-queue",
+ "observation-add-to-annotation-queue",
]);
export type ActionId = z.infer<typeof ActionIdSchema>;
diff --git a/packages/shared/src/server/queues.ts b/packages/shared/src/server/queues.ts
index b35621efb7..82d07e5da5 100644
--- a/packages/shared/src/server/queues.ts
+++ b/packages/shared/src/server/queues.ts
@@ -125,6 +125,15 @@ export const BatchActionProcessingEventSchema = z.discriminatedUnion(
targetId: z.string().optional(),
type: z.enum(BatchActionType),
}),
+ z.object({
+ actionId: z.literal("observation-add-to-annotation-queue"),
+ projectId: z.string(),
+ query: BatchActionQuerySchema,
+ tableName: z.enum(BatchTableNames),
+ cutoffCreatedAt: z.date(),
+ targetId: z.string().optional(),
+ type: z.enum(BatchActionType),
+ }),
z.object({
actionId: z.literal("eval-create"),
targetObject: z.enum(["trace", "dataset"]),
diff --git a/web/src/components/table/use-cases/observations.tsx b/web/src/components/table/use-cases/observations.tsx
index 6b97f8c451..8fd56e2581 100644
--- a/web/src/components/table/use-cases/observations.tsx
+++ b/web/src/components/table/use-cases/observations.tsx
@@ -1,7 +1,7 @@
import { api } from "@/src/utils/api";
import { DataTable } from "@/src/components/table/data-table";
import { DataTableToolbar } from "@/src/components/table/data-table-toolbar";
-import { useEffect, useMemo } from "react";
+import { useEffect, useMemo, useState } from "react";
import { TokenUsageBadge } from "@/src/components/token-usage-badge";
import { NumberParam, useQueryParams, withDefault } from "use-query-params";
import { useQueryFilterState } from "@/src/features/filters/hooks/useFilterState";
@@ -15,6 +15,8 @@ import {
BatchExportTableName,
type ObservationType,
TableViewPresetTableName,
+ AnnotationQueueObjectType,
+ BatchActionType,
} from "@langfuse/shared";
import { cn } from "@/src/utils/tailwind";
import { LevelColors } from "@/src/components/level-colors";
@@ -43,7 +45,7 @@ import {
convertSelectedEnvironmentsToFilter,
} from "@/src/hooks/use-environment-filter";
import { Badge } from "@/src/components/ui/badge";
-import { type Row } from "@tanstack/react-table";
+import { type RowSelectionState, type Row } from "@tanstack/react-table";
import TableIdOrName from "@/src/components/table/table-id";
import { ItemBadge } from "@/src/components/ItemBadge";
import { Skeleton } from "@/src/components/ui/skeleton";
@@ -55,6 +57,11 @@ import { useTableViewManager } from "@/src/components/table/table-view-presets/h
import { useRouter } from "next/router";
import { useFullTextSearch } from "@/src/components/table/use-cases/useFullTextSearch";
import { type PeekViewProps } from "@/src/components/table/peek/hooks/usePeekView";
+import { TableSelectionManager } from "@/src/features/table/components/TableSelectionManager";
+import { useSelectAll } from "@/src/features/table/hooks/useSelectAll";
+import { showSuccessToast } from "@/src/features/notifications/showSuccessToast";
+import { TableActionMenu } from "@/src/features/table/components/TableActionMenu";
+import { type TableAction } from "@/src/features/table/types";
export type ObservationsTableRow = {
// Shown by default
@@ -117,10 +124,12 @@ export default function ObservationsTable({
const { viewId } = router.query;
const { setDetailPageList } = useDetailPageLists();
-
+ const [selectedRows, setSelectedRows] = useState<RowSelectionState>({});
const { searchQuery, searchType, setSearchQuery, setSearchType } =
useFullTextSearch();
+ const { selectAll, setSelectAll } = useSelectAll(projectId, "observations");
+
const [paginationState, setPaginationState] = useQueryParams({
pageIndex: withDefault(NumberParam, 0),
pageSize: withDefault(NumberParam, 50),
@@ -272,6 +281,19 @@ export default function ObservationsTable({
},
);
+ const addToQueueMutation = api.annotationQueueItems.createMany.useMutation({
+ onSuccess: (data) => {
+ showSuccessToast({
+ title: "Observations added to queue",
+ description: `Selected observations will be added to queue "${data.queueName}". This may take a minute.`,
+ link: {
+ href: `/project/${projectId}/annotation-queues/${data.queueId}`,
+ text: `View queue "${data.queueName}"`,
+ },
+ });
+ },
+ });
+
useEffect(() => {
if (generations.isSuccess) {
setDetailPageList(
@@ -302,7 +324,54 @@ export default function ObservationsTable({
);
};
+ const { selectActionColumn } = TableSelectionManager<ObservationsTableRow>({
+ projectId,
+ tableName: "observations",
+ setSelectedRows,
+ });
+
+ const handleAddToAnnotationQueue = async ({
+ projectId,
+ targetId,
+ }: {
+ projectId: string;
+ targetId: string;
+ }) => {
+ const selectedGenerationIds = Object.keys(selectedRows).filter(
+ (generationId) =>
+ generations.data?.generations.map((g) => g.id).includes(generationId),
+ );
+
+ await addToQueueMutation.mutateAsync({
+ projectId,
+ objectIds: selectedGenerationIds,
+ objectType: AnnotationQueueObjectType.OBSERVATION,
+ queueId: targetId,
+ isBatchAction: selectAll,
+ query: {
+ filter: filterState,
+ orderBy: orderByState,
+ },
+ });
+ setSelectedRows({});
+ };
+
+ const tableActions: TableAction[] = [
+ {
+ id: "observation-add-to-annotation-queue",
+ type: BatchActionType.Create,
+ label: "Add to Annotation Queue",
+ description: "Add selected observations to an annotation queue.",
+ targetLabel: "Annotation Queue",
+ execute: handleAddToAnnotationQueue,
+ accessCheck: {
+ scope: "annotationQueues:CUD",
+ },
+ },
+ ];
+
const columns: LangfuseColumnDef<ObservationsTableRow>[] = [
+ selectActionColumn,
{
accessorKey: "startTime",
id: "startTime",
@@ -975,7 +1044,7 @@ export default function ObservationsTable({
setRowHeight={setRowHeight}
selectedOption={selectedOption}
setDateRangeAndOption={setDateRangeAndOption}
- actionButtons={
+ actionButtons={[
<BatchExportTableButton
{...{
projectId,
@@ -986,13 +1055,37 @@ export default function ObservationsTable({
}}
tableName={BatchExportTableName.Observations}
key="batchExport"
- />
- }
+ />,
+ Object.keys(selectedRows).filter((generationId) =>
+ generations.data?.generations
+ .map((g) => g.id)
+ .includes(generationId),
+ ).length > 0 ? (
+ <TableActionMenu
+ key="observations-multi-select-actions"
+ projectId={projectId}
+ actions={tableActions}
+ tableName={BatchExportTableName.Observations}
+ />
+ ) : null,
+ ]}
environmentFilter={{
values: selectedEnvironments,
onValueChange: setSelectedEnvironments,
options: environmentOptions.map((env) => ({ value: env })),
}}
+ multiSelect={{
+ selectAll,
+ setSelectAll,
+ selectedRowIds: Object.keys(selectedRows).filter((generationId) =>
+ generations.data?.generations
+ .map((g) => g.id)
+ .includes(generationId),
+ ),
+ setRowSelection: setSelectedRows,
+ totalCount,
+ ...paginationState,
+ }}
/>
<DataTable
tableName={"observations"}
@@ -1018,6 +1111,8 @@ export default function ObservationsTable({
onChange: setPaginationState,
state: paginationState,
}}
+ rowSelection={selectedRows}
+ setRowSelection={setSelectedRows}
setOrderBy={setOrderByState}
orderBy={orderByState}
columnOrder={columnOrder}
diff --git a/web/src/features/annotation-queues/server/annotationQueueItems.ts b/web/src/features/annotation-queues/server/annotationQueueItems.ts
index 5680660289..7a9aee3997 100644
--- a/web/src/features/annotation-queues/server/annotationQueueItems.ts
+++ b/web/src/features/annotation-queues/server/annotationQueueItems.ts
@@ -34,7 +34,7 @@ const isItemLocked = (item: AnnotationQueueItem) => {
};
const MAP_OBJECT_TYPE_TO_ACTION_PROPS: Record<
- "TRACE" | "SESSION",
+ AnnotationQueueObjectType,
{ actionId: ActionId; tableName: BatchTableNames }
> = {
[AnnotationQueueObjectType.TRACE]: {
@@ -45,6 +45,10 @@ const MAP_OBJECT_TYPE_TO_ACTION_PROPS: Record<
actionId: "session-add-to-annotation-queue",
tableName: BatchExportTableName.Sessions,
},
+ [AnnotationQueueObjectType.OBSERVATION]: {
+ actionId: "observation-add-to-annotation-queue",
+ tableName: BatchExportTableName.Observations,
+ },
};
export const queueItemRouter = createTRPCRouter({
diff --git a/web/src/features/table/components/targetOptionsQueryMap.tsx b/web/src/features/table/components/targetOptionsQueryMap.tsx
index 0216e8aafc..83c499164e 100644
--- a/web/src/features/table/components/targetOptionsQueryMap.tsx
+++ b/web/src/features/table/components/targetOptionsQueryMap.tsx
@@ -4,4 +4,6 @@ export const targetOptionsQueryMap = {
"trace-add-to-annotation-queue": api.annotationQueues.allNamesAndIds.useQuery,
"session-add-to-annotation-queue":
api.annotationQueues.allNamesAndIds.useQuery,
+ "observation-add-to-annotation-queue":
+ api.annotationQueues.allNamesAndIds.useQuery,
} as const;
diff --git a/worker/src/features/batchAction/handleBatchActionJob.ts b/worker/src/features/batchAction/handleBatchActionJob.ts
index 96f0a0d7da..2a397c299e 100644
--- a/worker/src/features/batchAction/handleBatchActionJob.ts
+++ b/worker/src/features/batchAction/handleBatchActionJob.ts
@@ -20,6 +20,7 @@ import { processClickhouseTraceDelete } from "../traces/processClickhouseTraceDe
import { env } from "../../env";
import { Job } from "bullmq";
import {
+ processAddObservationsToQueue,
processAddSessionsToQueue,
processAddTracesToQueue,
} from "./processAddToQueue";
@@ -69,6 +70,14 @@ async function processActionChunk(
);
break;
+ case "observation-add-to-annotation-queue":
+ await processAddObservationsToQueue(
+ projectId,
+ chunkIds,
+ targetId as string,
+ );
+ break;
+
case "score-delete":
await processClickhouseScoreDelete(projectId, chunkIds);
break;
@@ -145,6 +154,7 @@ export const handleBatchActionJob = async (
actionId === "trace-delete" ||
actionId === "trace-add-to-annotation-queue" ||
actionId === "session-add-to-annotation-queue" ||
+ actionId === "observation-add-to-annotation-queue" ||
actionId === "score-delete"
) {
const { projectId, tableName, query, cutoffCreatedAt, targetId, type } =
diff --git a/worker/src/features/batchAction/processAddToQueue.ts b/worker/src/features/batchAction/processAddToQueue.ts
index bbd6098324..505dd67fdd 100644
--- a/worker/src/features/batchAction/processAddToQueue.ts
+++ b/worker/src/features/batchAction/processAddToQueue.ts
@@ -92,3 +92,29 @@ export const processAddSessionsToQueue = async (
throw e;
}
};
+
+export const processAddObservationsToQueue = async (
+ projectId: string,
+ observationIds: string[],
+ targetId: string,
+) => {
+ logger.info(
+ `Adding observations ${JSON.stringify(observationIds)} to annotation queue ${targetId} in project ${projectId}`,
+ );
+
+ try {
+ await addToQueue({
+ projectId,
+ objectIds: observationIds,
+ objectType: AnnotationQueueObjectType.OBSERVATION,
+ targetId,
+ });
+ } catch (e) {
+ logger.error(
+ `Error adding observations ${JSON.stringify(observationIds)} to annotation queue ${targetId} in project ${projectId}`,
+ e,
+ );
+ traceException(e);
+ throw e;
+ }
+};