Skip to content

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

1.5 KB
Raw Blame
import { jsonSchemaNullable, type TriggerEventAction } from "@langfuse/shared";
import {
  logger,
  type PromptResult,
  EntityChangeQueue,
  QueueJobs,
  QueueName,
} from "@langfuse/shared/src/server";
import { v4 } from "uuid";

/**
 * Queue prompt change events for async processing using the generic EntityChangeQueue
 */
export const promptChangeEventSourcing = async (
  promptData: PromptResult | null,
  action: TriggerEventAction,
  user?: { id: string; name: string | null; email: string | null },
) => {
  if (!promptData) {
    return;
  }

  const event = {
    timestamp: new Date(),
    id: v4(),
    name: QueueJobs.EntityChangeJob as QueueJobs.EntityChangeJob,
    payload: {
      entityType: "prompt-version" as const,
      projectId: promptData.projectId,
      promptId: promptData.id,
      action: action,
      prompt: {
        ...promptData,
        prompt: jsonSchemaNullable.parse(promptData.prompt),
        config: jsonSchemaNullable.parse(promptData.config),
      },
      ...(user ? { user } : {}),
    },
  };
  try {
    // Queue the entity change event for async processing
    await EntityChangeQueue.getInstance()?.add(
      QueueName.EntityChangeQueue,
      event,
    );

    logger.debug(
      `Queued entity change event for prompt ${promptData.id} in project ${promptData.projectId} with action ${action}`,
    );
  } catch (error) {
    logger.error(
      `Failed to queue entity change event for prompt ${promptData.id} for project ${promptData.projectId}: ${error}`,
    );
    throw error;
  }
};