Repository navigation
Expand file tree
/
Copy pathdevOutput.ts
More file actions
453 lines (377 loc) · 15.5 KB
/
Copy pathdevOutput.ts
File metadata and controls
453 lines (377 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import { formatDurationMilliseconds } from "@trigger.dev/core/v3";
import { DEFAULT_DEV_BRANCH } from "@trigger.dev/core/v3/utils/gitBranch";
import type { ResolvedConfig } from "@trigger.dev/core/v3/build";
import {
createTaskMetadataFailedErrorStack,
DuplicateTaskIdsError,
WebhookIndexingError,
formatErrorCauses,
TaskIndexingImportError,
TaskMetadataParseError,
taskRunErrorEnhancer,
taskRunErrorToString,
} from "@trigger.dev/core/v3/errors";
import type { TaskRunError } from "@trigger.dev/core/v3/schemas";
import { TaskRunErrorCodes } from "@trigger.dev/core/v3/schemas";
import type { DevCommandOptions } from "../commands/dev.js";
import { isAgent } from "../utilities/agent.js";
import {
aiHelpLink,
chalkError,
chalkGrey,
chalkLink,
chalkRun,
chalkSuccess,
chalkTask,
chalkWarning,
chalkWorker,
cliLink,
isLinksSupported,
prettyError,
prettyPrintDate,
prettyWarning,
} from "../utilities/cliOutput.js";
import type { EventBusEventArgs } from "../utilities/eventBus.js";
import { eventBus } from "../utilities/eventBus.js";
import { logger } from "../utilities/logger.js";
import { formatScheduleWarnings } from "../utilities/scheduleWarnings.js";
import type { Socket } from "socket.io-client";
import { BundleError } from "../build/bundle.js";
import type { BackgroundWorker } from "./backgroundWorker.js";
import { analyzeWorker } from "../utilities/analyze.js";
import { SCHEDULE_PLAN_LIMIT_HEADER, SchedulePlanLimitError } from "./errors.js";
export type DevOutputOptions = {
name: string | undefined;
branch?: string;
dashboardUrl: string;
config: ResolvedConfig;
args: DevCommandOptions;
};
export function startDevOutput(options: DevOutputOptions) {
const { branch, dashboardUrl, config } = options;
const baseUrl = `${dashboardUrl}/projects/v3/${config.project}`;
// The version new runs execute on. A failed build leaves the previous worker running.
let workerVersion: string | undefined;
let printedAgentLinks = false;
// A person can follow this link to the dashboard's AI help. An agent already has the error.
const askAiForHelp = (query: string) => {
if (!isAgent) {
aiHelpLink({ dashboardUrl, project: config.project, query });
}
};
const logBuildFailed = (willRebuild: boolean) => {
const bullet = chalkError("○");
const failed = chalkError("Build failed");
if (!willRebuild) {
logger.log(`${bullet} ${failed}${chalkGrey(". Fix the errors above and run dev again.")}`);
return;
}
const runsText = workerVersion ? `, runs still use ${workerVersion}` : "";
logger.log(
`${bullet} ${failed}${chalkGrey(
`${runsText}. Fix the errors above and it will rebuild automatically.`
)}`
);
};
const rebuildStarted = (...[target]: EventBusEventArgs<"rebuildStarted">) => {
logger.log(chalkGrey("○ Rebuilding local worker…"));
};
const buildStarted = (...[target]: EventBusEventArgs<"buildStarted">) => {
logger.log(chalkGrey("○ Building local worker…"));
};
const buildFailed = (...[target, error]: EventBusEventArgs<"buildFailed">) => {
const errorText = error instanceof Error ? error.message : "Unknown error";
const stack = error instanceof Error ? error.stack : undefined;
let issues: string[] = [];
if (error instanceof BundleError) {
issues = error.issues?.map((issue) => `${issue.text} (${issue.location?.file})`) ?? [];
}
askAiForHelp(`Build failed:\n ${errorText}\n${issues.join("\n")}\n${stack}`);
// Only the first build emits this, and the dev command exits after it
logBuildFailed(false);
};
const rebuildFailed = () => {
logBuildFailed(true);
};
const workerSkipped = () => {
logger.log(
chalkGrey(
workerVersion
? `○ No changes detected, keeping local worker ${workerVersion}`
: "○ No changes detected, skipping build…"
)
);
};
const backgroundWorkerInitialized = (
...[worker]: EventBusEventArgs<"backgroundWorkerInitialized">
) => {
analyzeWorker(worker, options.args.analyze, options.args.disableWarnings);
if (!options.args.disableWarnings && worker.warnings.length > 0) {
prettyWarning("Schedule policy applied", formatScheduleWarnings(worker.warnings));
}
const logParts: string[] = [];
const testUrl = `${dashboardUrl}/projects/v3/${config.project}/test?environment=dev`;
const runsUrl = `${dashboardUrl}/projects/v3/${config.project}/runs?envSlug=dev`;
const pipe = chalkGrey("|");
const bullet = chalkGrey("○");
const arrow = chalkGrey("->");
logParts.push(bullet);
const testLink = chalkLink(cliLink("Test tasks", testUrl));
const runsLink = chalkLink(cliLink("View runs", runsUrl));
const runtime = chalkGrey(`[${worker.build.runtime}]`);
const workerStarted = chalkGrey(
`Local worker ready on branch: ${branch ?? DEFAULT_DEV_BRANCH}`
);
workerVersion = worker.serverWorker!.version;
logParts.push(workerStarted, runtime, arrow, chalkWorker(workerVersion));
if (isLinksSupported && !isAgent) {
logParts.push(pipe, testLink, pipe, runsLink);
}
logger.log(logParts.join(" "));
if (isAgent) {
logAgentWorkerDetails(worker, { testUrl, runsUrl, includeLinks: !printedAgentLinks });
printedAgentLinks = true;
}
};
const backgroundWorkerIndexingError = (
...[buildManifest, error]: EventBusEventArgs<"backgroundWorkerIndexingError">
) => {
if (error instanceof TaskIndexingImportError) {
let errorText = "";
for (const importError of error.importErrors) {
prettyError(
`Could not import ${importError.file}`,
importError.stack ?? importError.message
);
errorText += `Could not import ${importError.file}:\n ${
importError.stack ?? importError.message
}\n`;
}
askAiForHelp(errorText);
} else if (error instanceof TaskMetadataParseError) {
const errorStack = createTaskMetadataFailedErrorStack({
version: "v1",
zodIssues: error.zodIssues,
tasks: error.tasks,
});
prettyError(`Could not parse task metadata`, errorStack);
askAiForHelp(`Could not parse task metadata:\n ${errorStack}`);
} else if (error instanceof DuplicateTaskIdsError) {
const body = error.collisions
.map(({ id, filePaths }) => {
const distinct = Array.from(new Set(filePaths));
return distinct.length === 1
? `${chalkTask(id)} was defined more than once in ${distinct[0]}`
: `${chalkTask(id)} was defined in:\n${distinct.map((f) => ` ${f}`).join("\n")}`;
})
.join("\n\n");
prettyError(
"Duplicate task ids detected",
`${body}\n\nTask ids must be unique across your project (including scheduled tasks). Please rename one of them.`,
cliLink("View the task docs", "https://trigger.dev/docs/tasks/overview")
);
askAiForHelp(`Duplicate task ids: ${error.collisions.map((c) => c.id).join(", ")}`);
} else if (error instanceof WebhookIndexingError) {
const collisions = error.collisions.map(({ id, filePaths }) => {
const distinct = Array.from(new Set(filePaths));
return distinct.length === 1
? `Endpoint ${chalkTask(id)} was defined more than once in ${distinct[0]}`
: `Endpoint ${chalkTask(id)} was defined in:\n${distinct.map((f) => ` ${f}`).join("\n")}`;
});
const body = [...collisions, ...error.problems].join("\n\n");
prettyError(
"Webhook endpoints could not be indexed",
`${body}\n\nEndpoint ids must be unique across your project, and every subscriber needs an endpoint defined with webhooks.endpoint.define().`,
cliLink("View the webhook docs", "https://trigger.dev/docs/webhooks/overview")
);
askAiForHelp(`Webhook indexing errors: ${error.message}`);
} else if (error instanceof SchedulePlanLimitError) {
prettyError(SCHEDULE_PLAN_LIMIT_HEADER, error.message);
// The dev command exits after this error, so there's no rebuild to wait for
return;
} else {
const errorText = error instanceof Error ? error.message : "Unknown error";
const stack = error instanceof Error ? error.stack : undefined;
prettyError(`Build failed: ${errorText}`, stack);
askAiForHelp(`Build failed:\n ${errorText}\n${stack}`);
}
logBuildFailed(true);
};
const runStarted = (...[worker, execution]: EventBusEventArgs<"runStarted">) => {
if (!worker.serverWorker) {
return;
}
// ○ Mar 27 09:17:25.653 -> View logs | 20240326.20 | create-avatar | run_slufhjdfiv8ejnrkw9dsj.1
const logsUrl = `${baseUrl}/runs/${execution.run.id}`;
const pipe = chalkGrey("|");
const bullet = chalkGrey("○");
const link = chalkLink(cliLink("View logs", logsUrl));
const timestampPrefix = chalkGrey(prettyPrintDate(execution.attempt.startedAt));
const workerPrefix = chalkWorker(worker.serverWorker.version);
const taskPrefix = chalkTask(execution.task.id);
const runId = chalkRun(`${execution.run.id}.${execution.attempt.number}`);
const logParts = [bullet, timestampPrefix, chalkGrey("->")];
if (isLinksSupported && !isAgent) {
logParts.push(link, pipe);
}
logParts.push(workerPrefix, pipe, taskPrefix, pipe, runId);
if (isAgent) {
logParts.push(pipe, logsUrl);
}
logger.log(logParts.join(" "));
};
const runCompleted = (
...[worker, execution, completion, durationMs]: EventBusEventArgs<"runCompleted">
) => {
const retryingText = chalkGrey(
!completion.ok && completion.skippedRetrying
? " (retrying skipped)"
: !completion.ok && completion.retry !== undefined
? ` (retrying in ${completion.retry.delay}ms)`
: ""
);
const resultText = !completion.ok
? completion.error.type === "INTERNAL_ERROR" &&
(completion.error.code === TaskRunErrorCodes.TASK_EXECUTION_ABORTED ||
completion.error.code === TaskRunErrorCodes.TASK_RUN_CANCELLED)
? chalkWarning("Cancelled")
: `${chalkError("Error")}${retryingText}`
: chalkSuccess("Success");
const errorText = !completion.ok
? formatErrorLog(completion.error)
: "retry" in completion
? `retry in ${completion.retry}ms`
: "";
const elapsedText = chalkGrey(
`(${formatDurationMilliseconds(durationMs, { style: "short" })})`
);
const timestampPrefix = chalkGrey(prettyPrintDate());
const logsUrl = `${baseUrl}/runs/${execution.run.id}`;
const pipe = chalkGrey("|");
const bullet = chalkGrey("○");
const link = chalkLink(cliLink("View logs", logsUrl));
const workerPrefix = chalkWorker(worker.serverWorker!.version);
const taskPrefix = chalkTask(execution.task.id);
const runId = chalkRun(`${execution.run.id}.${execution.attempt.number}`);
const logParts = [bullet, timestampPrefix, chalkGrey("->")];
if (isLinksSupported && !isAgent) {
logParts.push(link, pipe);
}
logParts.push(workerPrefix, pipe, taskPrefix, pipe, runId, pipe, resultText);
logger.log(`${logParts.join(" ")} ${elapsedText}${errorText}`);
if (isAgent && completion.ok) {
const output = formatRunOutput(completion.output, completion.outputType);
if (output !== undefined) {
logger.log(`${chalkGrey(" Output:")} ${output}`);
}
}
};
const socketConnectionDisconnected = (reason: Socket.DisconnectReason) => {
logger.log(chalkGrey(`○ Connection was lost: ${reason}`));
};
const socketConnectionReconnected = (reason: string) => {
logger.log(chalkGrey(`○ Connection was restored`));
};
eventBus.on("rebuildStarted", rebuildStarted);
eventBus.on("buildStarted", buildStarted);
eventBus.on("buildFailed", buildFailed);
eventBus.on("rebuildFailed", rebuildFailed);
eventBus.on("workerSkipped", workerSkipped);
eventBus.on("backgroundWorkerInitialized", backgroundWorkerInitialized);
eventBus.on("runStarted", runStarted);
eventBus.on("runCompleted", runCompleted);
eventBus.on("backgroundWorkerIndexingError", backgroundWorkerIndexingError);
eventBus.on("socketConnectionDisconnected", socketConnectionDisconnected);
eventBus.on("socketConnectionReconnected", socketConnectionReconnected);
return () => {
eventBus.off("rebuildStarted", rebuildStarted);
eventBus.off("buildStarted", buildStarted);
eventBus.off("buildFailed", buildFailed);
eventBus.off("rebuildFailed", rebuildFailed);
eventBus.off("workerSkipped", workerSkipped);
eventBus.off("backgroundWorkerInitialized", backgroundWorkerInitialized);
eventBus.off("runStarted", runStarted);
eventBus.off("runCompleted", runCompleted);
eventBus.off("backgroundWorkerIndexingError", backgroundWorkerIndexingError);
eventBus.off("socketConnectionDisconnected", socketConnectionDisconnected);
eventBus.off("socketConnectionReconnected", socketConnectionReconnected);
};
}
const MAX_AGENT_TASK_IDS = 50;
const MAX_AGENT_OUTPUT_LENGTH = 1_000;
// Agents can't click terminal links or open the dashboard, so they get the task ids and plain URLs
function logAgentWorkerDetails(
worker: BackgroundWorker,
{ testUrl, runsUrl, includeLinks }: { testUrl: string; runsUrl: string; includeLinks: boolean }
) {
const taskIds = worker.manifest?.tasks.map((task) => task.id) ?? [];
const shownTaskIds = taskIds.slice(0, MAX_AGENT_TASK_IDS);
const hiddenCount = taskIds.length - shownTaskIds.length;
logger.log(
chalkGrey(
` Tasks: ${shownTaskIds.length > 0 ? shownTaskIds.join(", ") : "none"}${
hiddenCount > 0 ? `, and ${hiddenCount} more` : ""
}`
)
);
if (includeLinks) {
logger.log(chalkGrey(` Test tasks: ${testUrl}`));
logger.log(chalkGrey(` View runs: ${runsUrl}`));
}
}
/** A run's output as one line, or undefined when the run didn't return anything. */
export function formatRunOutput(
output: string | undefined,
outputType: string
): string | undefined {
if (output === undefined) {
return undefined;
}
if (outputType === "application/store") {
return "(too large to show here, view it in the dashboard)";
}
let text = output;
if (outputType === "application/super+json") {
try {
// Show the plain JSON part, without superjson's type metadata
text = JSON.stringify(JSON.parse(output).json);
} catch {
// Show the raw output
}
}
if (text.length <= MAX_AGENT_OUTPUT_LENGTH) {
return text;
}
return `${text.slice(0, MAX_AGENT_OUTPUT_LENGTH)}… (${
text.length - MAX_AGENT_OUTPUT_LENGTH
} more characters)`;
}
function formatErrorLog(error: TaskRunError) {
switch (error.type) {
case "INTERNAL_ERROR": {
// Already labelled "Cancelled"
if (
error.code === TaskRunErrorCodes.TASK_EXECUTION_ABORTED ||
error.code === TaskRunErrorCodes.TASK_RUN_CANCELLED
) {
return "";
}
const enhancedError = taskRunErrorEnhancer(error);
const link = enhancedError.link
? `\n${enhancedError.link.name}: ${enhancedError.link.href}`
: "";
const stackTrace = error.stackTrace ? `\n${error.stackTrace}` : "";
return `\n\n${chalkError("X Error:")} ${taskRunErrorToString(enhancedError)}${link}${stackTrace}\n`;
}
case "STRING_ERROR": {
return `\n\n${chalkError("X Error:")} ${error.raw}\n`;
}
case "CUSTOM_ERROR": {
return `\n\n${chalkError("X Error:")} ${error.raw}\n`;
}
case "BUILT_IN_ERROR": {
const stackTrace = error.stackTrace.replace(/^Error: /, chalkError("X Error: "));
return `\n\n${stackTrace}${formatErrorCauses(error.causes, { stackTrace: true })}\n`;
}
}
}