|
5 | 5 | } from "@trigger.dev/core/v3/schemas"; |
6 | 6 | import type { CursorPageResponse } from "@trigger.dev/core/v3/zodfetch"; |
7 | 7 |
|
| 8 | +const DEFAULT_MAX_TRACE_LINES = 500; |
| 9 | + |
8 | 10 | export function formatRun(run: RetrieveRunResponse): string { |
9 | 11 | const lines: string[] = []; |
10 | 12 |
|
@@ -170,23 +172,35 @@ function formatRelatedRuns(relatedRuns: RetrieveRunResponse["relatedRuns"]): str |
170 | 172 | return parts.length > 0 ? `Related: ${parts.join("; ")}` : null; |
171 | 173 | } |
172 | 174 |
|
173 | | -export function formatRunTrace(trace: RetrieveRunTraceResponseBody["trace"]): string { |
| 175 | +export function formatRunTrace( |
| 176 | + trace: RetrieveRunTraceResponseBody["trace"], |
| 177 | + maxTraceLines: number = DEFAULT_MAX_TRACE_LINES |
| 178 | +): string { |
174 | 179 | const lines: string[] = []; |
175 | 180 |
|
176 | 181 | lines.push(`Trace ID: ${trace.traceId}`); |
177 | 182 | lines.push(""); |
178 | 183 |
|
179 | 184 | // Format the root span and its children recursively |
180 | | - formatSpan(trace.rootSpan, lines, 0); |
| 185 | + const reachedMaxLines = formatSpan(trace.rootSpan, lines, 0, maxTraceLines); |
| 186 | + |
| 187 | + if (reachedMaxLines) { |
| 188 | + lines.push(`(truncated logs to ${maxTraceLines} lines)`); |
| 189 | + } |
181 | 190 |
|
182 | 191 | return lines.join("\n"); |
183 | 192 | } |
184 | 193 |
|
185 | 194 | function formatSpan( |
186 | 195 | span: RetrieveRunTraceResponseBody["trace"]["rootSpan"], |
187 | 196 | lines: string[], |
188 | | - depth: number |
189 | | -): void { |
| 197 | + depth: number, |
| 198 | + maxLines: number |
| 199 | +): boolean { |
| 200 | + if (lines.length >= maxLines) { |
| 201 | + return true; |
| 202 | + } |
| 203 | + |
190 | 204 | const indent = " ".repeat(depth); |
191 | 205 | const prefix = depth === 0 ? "└─" : "├─"; |
192 | 206 |
|
@@ -230,7 +244,7 @@ function formatSpan( |
230 | 244 | } |
231 | 245 |
|
232 | 246 | // Show output if it exists |
233 | | - if (span.data.output && Object.keys(span.data.output).length > 0) { |
| 247 | + if (span.data.output) { |
234 | 248 | lines.push( |
235 | 249 | `${indent} Output: ${JSON.stringify(span.data.output, null, 2).replace( |
236 | 250 | /\n/g, |
@@ -263,14 +277,20 @@ function formatSpan( |
263 | 277 |
|
264 | 278 | // Recursively format children |
265 | 279 | if (span.children) { |
266 | | - span.children.forEach((child, index) => { |
267 | | - formatSpan(child, lines, depth + 1); |
| 280 | + const reachedMaxLines = span.children.some((child, index) => { |
| 281 | + const reachedMaxLines = formatSpan(child, lines, depth + 1, maxLines); |
268 | 282 | // Add spacing between sibling spans (except for the last one) |
269 | | - if (index < span.children.length - 1) { |
| 283 | + if (index < span.children.length - 1 && !reachedMaxLines) { |
270 | 284 | lines.push(""); |
271 | 285 | } |
| 286 | + |
| 287 | + return reachedMaxLines; |
272 | 288 | }); |
| 289 | + |
| 290 | + return reachedMaxLines; |
273 | 291 | } |
| 292 | + |
| 293 | + return false; |
274 | 294 | } |
275 | 295 |
|
276 | 296 | function getStatusIndicator( |
|
0 commit comments