|
| 1 | +import { json } from "@remix-run/server-runtime"; |
| 2 | +import { BatchId } from "@trigger.dev/core/v3/isomorphic"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { $replica } from "~/db.server"; |
| 5 | +import { extractAISpanData } from "~/components/runs/v3/ai"; |
| 6 | +import { createLoaderApiRoute } from "~/services/routeBuilders/apiBuilder.server"; |
| 7 | +import { resolveEventRepositoryForStore } from "~/v3/eventRepository/index.server"; |
| 8 | +import { getTaskEventStoreTableForRun } from "~/v3/taskEventStore.server"; |
| 9 | + |
| 10 | +const ParamsSchema = z.object({ |
| 11 | + runId: z.string(), |
| 12 | + spanId: z.string(), |
| 13 | +}); |
| 14 | + |
| 15 | +export const loader = createLoaderApiRoute( |
| 16 | + { |
| 17 | + params: ParamsSchema, |
| 18 | + allowJWT: true, |
| 19 | + corsStrategy: "all", |
| 20 | + findResource: (params, auth) => { |
| 21 | + return $replica.taskRun.findFirst({ |
| 22 | + where: { |
| 23 | + friendlyId: params.runId, |
| 24 | + runtimeEnvironmentId: auth.environment.id, |
| 25 | + }, |
| 26 | + }); |
| 27 | + }, |
| 28 | + shouldRetryNotFound: true, |
| 29 | + authorization: { |
| 30 | + action: "read", |
| 31 | + resource: (run) => ({ |
| 32 | + runs: run.friendlyId, |
| 33 | + tags: run.runTags, |
| 34 | + batch: run.batchId ? BatchId.toFriendlyId(run.batchId) : undefined, |
| 35 | + tasks: run.taskIdentifier, |
| 36 | + }), |
| 37 | + superScopes: ["read:runs", "read:all", "admin"], |
| 38 | + }, |
| 39 | + }, |
| 40 | + async ({ params, resource: run, authentication }) => { |
| 41 | + const eventRepository = resolveEventRepositoryForStore(run.taskEventStore); |
| 42 | + const eventStore = getTaskEventStoreTableForRun(run); |
| 43 | + |
| 44 | + const span = await eventRepository.getSpan( |
| 45 | + eventStore, |
| 46 | + authentication.environment.id, |
| 47 | + params.spanId, |
| 48 | + run.traceId, |
| 49 | + run.createdAt, |
| 50 | + run.completedAt ?? undefined |
| 51 | + ); |
| 52 | + |
| 53 | + if (!span) { |
| 54 | + return json({ error: "Span not found" }, { status: 404 }); |
| 55 | + } |
| 56 | + |
| 57 | + // Duration is nanoseconds from ClickHouse (Postgres store is deprecated) |
| 58 | + const durationMs = span.duration / 1_000_000; |
| 59 | + |
| 60 | + const aiData = |
| 61 | + span.properties && typeof span.properties === "object" |
| 62 | + ? extractAISpanData(span.properties as Record<string, unknown>, durationMs) |
| 63 | + : undefined; |
| 64 | + |
| 65 | + const triggeredRuns = await $replica.taskRun.findMany({ |
| 66 | + take: 50, |
| 67 | + select: { |
| 68 | + friendlyId: true, |
| 69 | + taskIdentifier: true, |
| 70 | + status: true, |
| 71 | + createdAt: true, |
| 72 | + }, |
| 73 | + where: { |
| 74 | + runtimeEnvironmentId: authentication.environment.id, |
| 75 | + parentSpanId: params.spanId, |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + const properties = |
| 80 | + span.properties && |
| 81 | + typeof span.properties === "object" && |
| 82 | + Object.keys(span.properties as Record<string, unknown>).length > 0 |
| 83 | + ? (span.properties as Record<string, unknown>) |
| 84 | + : undefined; |
| 85 | + |
| 86 | + return json( |
| 87 | + { |
| 88 | + spanId: span.spanId, |
| 89 | + parentId: span.parentId, |
| 90 | + runId: run.friendlyId, |
| 91 | + message: span.message, |
| 92 | + isError: span.isError, |
| 93 | + isPartial: span.isPartial, |
| 94 | + isCancelled: span.isCancelled, |
| 95 | + level: span.level, |
| 96 | + startTime: span.startTime, |
| 97 | + durationMs, |
| 98 | + properties, |
| 99 | + events: span.events?.length ? span.events : undefined, |
| 100 | + entityType: span.entity.type ?? undefined, |
| 101 | + ai: aiData |
| 102 | + ? { |
| 103 | + model: aiData.model, |
| 104 | + provider: aiData.provider, |
| 105 | + operationName: aiData.operationName, |
| 106 | + inputTokens: aiData.inputTokens, |
| 107 | + outputTokens: aiData.outputTokens, |
| 108 | + totalTokens: aiData.totalTokens, |
| 109 | + cachedTokens: aiData.cachedTokens, |
| 110 | + reasoningTokens: aiData.reasoningTokens, |
| 111 | + inputCost: aiData.inputCost, |
| 112 | + outputCost: aiData.outputCost, |
| 113 | + totalCost: aiData.totalCost, |
| 114 | + tokensPerSecond: aiData.tokensPerSecond, |
| 115 | + msToFirstChunk: aiData.msToFirstChunk, |
| 116 | + durationMs: aiData.durationMs, |
| 117 | + finishReason: aiData.finishReason, |
| 118 | + responseText: aiData.responseText, |
| 119 | + } |
| 120 | + : undefined, |
| 121 | + triggeredRuns: |
| 122 | + triggeredRuns.length > 0 |
| 123 | + ? triggeredRuns.map((r) => ({ |
| 124 | + runId: r.friendlyId, |
| 125 | + taskIdentifier: r.taskIdentifier, |
| 126 | + status: r.status, |
| 127 | + createdAt: r.createdAt, |
| 128 | + })) |
| 129 | + : undefined, |
| 130 | + }, |
| 131 | + { status: 200 } |
| 132 | + ); |
| 133 | + } |
| 134 | +); |
0 commit comments