Skip to content

Commit e34e520

Browse files
committed
more hooks
1 parent 597b7ba commit e34e520

File tree

9 files changed

+276
-48
lines changed

9 files changed

+276
-48
lines changed

packages/core/src/v3/lifecycle-hooks-api.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ export type {
1212
TaskStartHookParams,
1313
OnStartHookFunction,
1414
AnyOnStartHookFunction,
15+
TaskFailureHookParams,
16+
AnyOnFailureHookFunction,
17+
TaskSuccessHookParams,
18+
AnyOnSuccessHookFunction,
19+
TaskCompleteHookParams,
20+
AnyOnCompleteHookFunction,
21+
TaskWaitHookParams,
22+
AnyOnWaitHookFunction,
23+
TaskResumeHookParams,
24+
AnyOnResumeHookFunction,
25+
TaskCatchErrorHookParams,
26+
AnyOnCatchErrorHookFunction,
27+
TaskCompleteResult,
1528
} from "./lifecycleHooks/types.js";
1629

1730
export * as lifecycleHooksAdapters from "./lifecycleHooks/adapters.js";

packages/core/src/v3/lifecycleHooks/adapters.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
AnyOnStartHookFunction,
55
AnyOnFailureHookFunction,
66
AnyOnSuccessHookFunction,
7+
AnyOnCatchErrorHookFunction,
78
} from "./types.js";
89

910
export function createInitHookAdapter<TPayload>(
@@ -67,3 +68,11 @@ export function createSuccessHookAdapter<TPayload, TOutput>(
6768
);
6869
};
6970
}
71+
72+
export function createHandleErrorHookAdapter<TPayload>(
73+
fn: NonNullable<TaskOptions<string, TPayload, unknown, any>["handleError"]>
74+
): AnyOnCatchErrorHookFunction {
75+
return async (params) => {
76+
return await fn(params.payload as unknown as TPayload, params.error, params);
77+
};
78+
}

packages/core/src/v3/lifecycleHooks/index.ts

Lines changed: 131 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ const API_NAME = "lifecycle-hooks";
33
import { getGlobal, registerGlobal, unregisterGlobal } from "../utils/globals.js";
44
import { NoopLifecycleHooksManager } from "./manager.js";
55
import {
6+
AnyOnCatchErrorHookFunction,
7+
AnyOnCompleteHookFunction,
8+
AnyOnFailureHookFunction,
69
AnyOnInitHookFunction,
10+
AnyOnResumeHookFunction,
711
AnyOnStartHookFunction,
12+
AnyOnSuccessHookFunction,
13+
AnyOnWaitHookFunction,
814
RegisteredHookFunction,
915
RegisterHookFunctionParams,
1016
type LifecycleHooksManager,
@@ -52,6 +58,13 @@ export class LifecycleHooksAPI {
5258
return this.#getManager().getGlobalInitHooks();
5359
}
5460

61+
public registerTaskStartHook(
62+
taskId: string,
63+
hook: RegisterHookFunctionParams<AnyOnStartHookFunction>
64+
): void {
65+
this.#getManager().registerTaskStartHook(taskId, hook);
66+
}
67+
5568
public registerGlobalStartHook(hook: RegisterHookFunctionParams<AnyOnStartHookFunction>): void {
5669
this.#getManager().registerGlobalStartHook(hook);
5770
}
@@ -64,11 +77,126 @@ export class LifecycleHooksAPI {
6477
return this.#getManager().getGlobalStartHooks();
6578
}
6679

67-
public registerTaskStartHook(
80+
public registerGlobalFailureHook(
81+
hook: RegisterHookFunctionParams<AnyOnFailureHookFunction>
82+
): void {
83+
this.#getManager().registerGlobalFailureHook(hook);
84+
}
85+
86+
public registerTaskFailureHook(
6887
taskId: string,
69-
hook: RegisterHookFunctionParams<AnyOnStartHookFunction>
88+
hook: RegisterHookFunctionParams<AnyOnFailureHookFunction>
7089
): void {
71-
this.#getManager().registerTaskStartHook(taskId, hook);
90+
this.#getManager().registerTaskFailureHook(taskId, hook);
91+
}
92+
93+
public getTaskFailureHook(taskId: string): AnyOnFailureHookFunction | undefined {
94+
return this.#getManager().getTaskFailureHook(taskId);
95+
}
96+
97+
public getGlobalFailureHooks(): RegisteredHookFunction<AnyOnFailureHookFunction>[] {
98+
return this.#getManager().getGlobalFailureHooks();
99+
}
100+
101+
public registerGlobalSuccessHook(
102+
hook: RegisterHookFunctionParams<AnyOnSuccessHookFunction>
103+
): void {
104+
this.#getManager().registerGlobalSuccessHook(hook);
105+
}
106+
107+
public registerTaskSuccessHook(
108+
taskId: string,
109+
hook: RegisterHookFunctionParams<AnyOnSuccessHookFunction>
110+
): void {
111+
this.#getManager().registerTaskSuccessHook(taskId, hook);
112+
}
113+
114+
public getTaskSuccessHook(taskId: string): AnyOnSuccessHookFunction | undefined {
115+
return this.#getManager().getTaskSuccessHook(taskId);
116+
}
117+
118+
public getGlobalSuccessHooks(): RegisteredHookFunction<AnyOnSuccessHookFunction>[] {
119+
return this.#getManager().getGlobalSuccessHooks();
120+
}
121+
122+
public registerGlobalCompleteHook(
123+
hook: RegisterHookFunctionParams<AnyOnCompleteHookFunction>
124+
): void {
125+
this.#getManager().registerGlobalCompleteHook(hook);
126+
}
127+
128+
public registerTaskCompleteHook(
129+
taskId: string,
130+
hook: RegisterHookFunctionParams<AnyOnCompleteHookFunction>
131+
): void {
132+
this.#getManager().registerTaskCompleteHook(taskId, hook);
133+
}
134+
135+
public getTaskCompleteHook(taskId: string): AnyOnCompleteHookFunction | undefined {
136+
return this.#getManager().getTaskCompleteHook(taskId);
137+
}
138+
139+
public getGlobalCompleteHooks(): RegisteredHookFunction<AnyOnCompleteHookFunction>[] {
140+
return this.#getManager().getGlobalCompleteHooks();
141+
}
142+
143+
public registerGlobalWaitHook(hook: RegisterHookFunctionParams<AnyOnWaitHookFunction>): void {
144+
this.#getManager().registerGlobalWaitHook(hook);
145+
}
146+
147+
public registerTaskWaitHook(
148+
taskId: string,
149+
hook: RegisterHookFunctionParams<AnyOnWaitHookFunction>
150+
): void {
151+
this.#getManager().registerTaskWaitHook(taskId, hook);
152+
}
153+
154+
public getTaskWaitHook(taskId: string): AnyOnWaitHookFunction | undefined {
155+
return this.#getManager().getTaskWaitHook(taskId);
156+
}
157+
158+
public getGlobalWaitHooks(): RegisteredHookFunction<AnyOnWaitHookFunction>[] {
159+
return this.#getManager().getGlobalWaitHooks();
160+
}
161+
162+
public registerGlobalResumeHook(hook: RegisterHookFunctionParams<AnyOnResumeHookFunction>): void {
163+
this.#getManager().registerGlobalResumeHook(hook);
164+
}
165+
166+
public registerTaskResumeHook(
167+
taskId: string,
168+
hook: RegisterHookFunctionParams<AnyOnResumeHookFunction>
169+
): void {
170+
this.#getManager().registerTaskResumeHook(taskId, hook);
171+
}
172+
173+
public getTaskResumeHook(taskId: string): AnyOnResumeHookFunction | undefined {
174+
return this.#getManager().getTaskResumeHook(taskId);
175+
}
176+
177+
public getGlobalResumeHooks(): RegisteredHookFunction<AnyOnResumeHookFunction>[] {
178+
return this.#getManager().getGlobalResumeHooks();
179+
}
180+
181+
public registerGlobalCatchErrorHook(
182+
hook: RegisterHookFunctionParams<AnyOnCatchErrorHookFunction>
183+
): void {
184+
this.#getManager().registerGlobalCatchErrorHook(hook);
185+
}
186+
187+
public registerTaskCatchErrorHook(
188+
taskId: string,
189+
hook: RegisterHookFunctionParams<AnyOnCatchErrorHookFunction>
190+
): void {
191+
this.#getManager().registerTaskCatchErrorHook(taskId, hook);
192+
}
193+
194+
public getTaskCatchErrorHook(taskId: string): AnyOnCatchErrorHookFunction | undefined {
195+
return this.#getManager().getTaskCatchErrorHook(taskId);
196+
}
197+
198+
public getGlobalCatchErrorHooks(): RegisteredHookFunction<AnyOnCatchErrorHookFunction>[] {
199+
return this.#getManager().getGlobalCatchErrorHooks();
72200
}
73201

74202
#getManager(): LifecycleHooksManager {

packages/core/src/v3/lifecycleHooks/manager.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
AnyOnCompleteHookFunction,
1010
AnyOnWaitHookFunction,
1111
AnyOnResumeHookFunction,
12-
AnyOnHandleErrorHookFunction,
12+
AnyOnCatchErrorHookFunction,
1313
} from "./types.js";
1414

1515
export class StandardLifecycleHooksManager implements LifecycleHooksManager {
@@ -41,11 +41,9 @@ export class StandardLifecycleHooksManager implements LifecycleHooksManager {
4141
new Map();
4242
private taskResumeHooks: Map<string, RegisteredHookFunction<AnyOnResumeHookFunction>> = new Map();
4343

44-
private globalHandleErrorHooks: Map<
45-
string,
46-
RegisteredHookFunction<AnyOnHandleErrorHookFunction>
47-
> = new Map();
48-
private taskHandleErrorHooks: Map<string, RegisteredHookFunction<AnyOnHandleErrorHookFunction>> =
44+
private globalCatchErrorHooks: Map<string, RegisteredHookFunction<AnyOnCatchErrorHookFunction>> =
45+
new Map();
46+
private taskCatchErrorHooks: Map<string, RegisteredHookFunction<AnyOnCatchErrorHookFunction>> =
4947
new Map();
5048

5149
registerGlobalStartHook(hook: RegisterHookFunctionParams<AnyOnStartHookFunction>): void {
@@ -268,37 +266,37 @@ export class StandardLifecycleHooksManager implements LifecycleHooksManager {
268266
return Array.from(this.globalResumeHooks.values());
269267
}
270268

271-
registerGlobalHandleErrorHook(
272-
hook: RegisterHookFunctionParams<AnyOnHandleErrorHookFunction>
269+
registerGlobalCatchErrorHook(
270+
hook: RegisterHookFunctionParams<AnyOnCatchErrorHookFunction>
273271
): void {
274272
const id = generateHookId(hook);
275273

276-
this.globalHandleErrorHooks.set(id, {
274+
this.globalCatchErrorHooks.set(id, {
277275
id,
278276
name: hook.id ?? hook.fn.name ? (hook.fn.name === "" ? undefined : hook.fn.name) : undefined,
279277
fn: hook.fn,
280278
});
281279
}
282280

283-
registerTaskHandleErrorHook(
281+
registerTaskCatchErrorHook(
284282
taskId: string,
285-
hook: RegisterHookFunctionParams<AnyOnHandleErrorHookFunction>
283+
hook: RegisterHookFunctionParams<AnyOnCatchErrorHookFunction>
286284
): void {
287285
const id = generateHookId(hook);
288286

289-
this.taskHandleErrorHooks.set(taskId, {
287+
this.taskCatchErrorHooks.set(taskId, {
290288
id,
291289
name: hook.id ?? hook.fn.name ? (hook.fn.name === "" ? undefined : hook.fn.name) : undefined,
292290
fn: hook.fn,
293291
});
294292
}
295293

296-
getTaskHandleErrorHook(taskId: string): AnyOnHandleErrorHookFunction | undefined {
297-
return this.taskHandleErrorHooks.get(taskId)?.fn;
294+
getTaskCatchErrorHook(taskId: string): AnyOnCatchErrorHookFunction | undefined {
295+
return this.taskCatchErrorHooks.get(taskId)?.fn;
298296
}
299297

300-
getGlobalHandleErrorHooks(): RegisteredHookFunction<AnyOnHandleErrorHookFunction>[] {
301-
return Array.from(this.globalHandleErrorHooks.values());
298+
getGlobalCatchErrorHooks(): RegisteredHookFunction<AnyOnCatchErrorHookFunction>[] {
299+
return Array.from(this.globalCatchErrorHooks.values());
302300
}
303301
}
304302

@@ -436,24 +434,19 @@ export class NoopLifecycleHooksManager implements LifecycleHooksManager {
436434
return [];
437435
}
438436

439-
registerGlobalHandleErrorHook(
440-
hook: RegisterHookFunctionParams<AnyOnHandleErrorHookFunction>
441-
): void {
437+
registerGlobalCatchErrorHook(): void {
442438
// Noop
443439
}
444440

445-
registerTaskHandleErrorHook(
446-
taskId: string,
447-
hook: RegisterHookFunctionParams<AnyOnHandleErrorHookFunction>
448-
): void {
441+
registerTaskCatchErrorHook(): void {
449442
// Noop
450443
}
451444

452-
getTaskHandleErrorHook(taskId: string): AnyOnHandleErrorHookFunction | undefined {
445+
getTaskCatchErrorHook(): undefined {
453446
return undefined;
454447
}
455448

456-
getGlobalHandleErrorHooks(): RegisteredHookFunction<AnyOnHandleErrorHookFunction>[] {
449+
getGlobalCatchErrorHooks(): [] {
457450
return [];
458451
}
459452
}

packages/core/src/v3/lifecycleHooks/types.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export type RegisteredHookFunction<THookFunction extends (params: any) => any> =
120120
fn: THookFunction;
121121
};
122122

123-
export type TaskHandleErrorHookParams<TPayload = unknown> = {
123+
export type TaskCatchErrorHookParams<TPayload = unknown> = {
124124
ctx: TaskRunContext;
125125
payload: TPayload;
126126
task: string;
@@ -131,11 +131,11 @@ export type TaskHandleErrorHookParams<TPayload = unknown> = {
131131
signal?: AbortSignal;
132132
};
133133

134-
export type OnHandleErrorHookFunction<TPayload> = (
135-
params: TaskHandleErrorHookParams<TPayload>
134+
export type OnCatchErrorHookFunction<TPayload> = (
135+
params: TaskCatchErrorHookParams<TPayload>
136136
) => HandleErrorResult;
137137

138-
export type AnyOnHandleErrorHookFunction = OnHandleErrorHookFunction<unknown>;
138+
export type AnyOnCatchErrorHookFunction = OnCatchErrorHookFunction<unknown>;
139139

140140
export interface LifecycleHooksManager {
141141
registerGlobalInitHook(hook: RegisterHookFunctionParams<AnyOnInitHookFunction>): void;
@@ -187,13 +187,11 @@ export interface LifecycleHooksManager {
187187
): void;
188188
getTaskResumeHook(taskId: string): AnyOnResumeHookFunction | undefined;
189189
getGlobalResumeHooks(): RegisteredHookFunction<AnyOnResumeHookFunction>[];
190-
registerGlobalHandleErrorHook(
191-
hook: RegisterHookFunctionParams<AnyOnHandleErrorHookFunction>
192-
): void;
193-
registerTaskHandleErrorHook(
190+
registerGlobalCatchErrorHook(hook: RegisterHookFunctionParams<AnyOnCatchErrorHookFunction>): void;
191+
registerTaskCatchErrorHook(
194192
taskId: string,
195-
hook: RegisterHookFunctionParams<AnyOnHandleErrorHookFunction>
193+
hook: RegisterHookFunctionParams<AnyOnCatchErrorHookFunction>
196194
): void;
197-
getTaskHandleErrorHook(taskId: string): AnyOnHandleErrorHookFunction | undefined;
198-
getGlobalHandleErrorHooks(): RegisteredHookFunction<AnyOnHandleErrorHookFunction>[];
195+
getTaskCatchErrorHook(taskId: string): AnyOnCatchErrorHookFunction | undefined;
196+
getGlobalCatchErrorHooks(): RegisteredHookFunction<AnyOnCatchErrorHookFunction>[];
199197
}

packages/core/src/v3/types/tasks.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import { AnySchemaParseFn, inferSchemaIn, inferSchemaOut, Schema } from "./schem
1414
import { Prettify } from "./utils.js";
1515
import { inferToolParameters, ToolTaskParameters } from "./tools.js";
1616
import { QueueOptions } from "./queues.js";
17+
import {
18+
OnCatchErrorHookFunction,
19+
OnCompleteHookFunction,
20+
OnResumeHookFunction,
21+
OnWaitHookFunction,
22+
} from "../lifecycleHooks/types.js";
1723

1824
export type Queue = QueueOptions;
1925
export type TaskSchema = Schema;
@@ -268,13 +274,24 @@ type CommonTaskOptions<
268274

269275
/**
270276
* handleError is called when the run function throws an error. It can be used to modify the error or return new retry options.
277+
*
278+
* @deprecated Use catchError instead
271279
*/
272280
handleError?: (
273281
payload: TPayload,
274282
error: unknown,
275283
params: HandleErrorFnParams<TInitOutput>
276284
) => HandleErrorResult;
277285

286+
/**
287+
* catchError is called when the run function throws an error. It can be used to modify the error or return new retry options.
288+
*/
289+
catchError?: OnCatchErrorHookFunction<TPayload>;
290+
291+
onResume?: OnResumeHookFunction<TPayload>;
292+
onWait?: OnWaitHookFunction<TPayload>;
293+
onComplete?: OnCompleteHookFunction<TPayload, TOutput>;
294+
278295
/**
279296
* middleware allows you to run code "around" the run function. This can be useful for logging, metrics, or other cross-cutting concerns.
280297
*

0 commit comments

Comments
 (0)