22// Licensed under the MIT License.
33
44import { RetryPolicy } from "../retry/retry-policy" ;
5+ import { AsyncRetryHandler , RetryHandler , toAsyncRetryHandler } from "../retry/retry-handler" ;
6+
7+ /**
8+ * Union type representing either a RetryPolicy for declarative retry or an AsyncRetryHandler for imperative retry.
9+ */
10+ export type TaskRetryOptions = RetryPolicy | AsyncRetryHandler ;
511
612/**
713 * Options that can be used to control the behavior of orchestrator task execution.
814 */
915export interface TaskOptions {
1016 /**
11- * The retry policy for the task.
12- * Controls how many times a task is retried and the delay between retries.
17+ * The retry options for the task.
18+ * Can be either a RetryPolicy for declarative retry control,
19+ * or an AsyncRetryHandler for imperative (programmatic) retry control.
1320 */
14- retry ?: RetryPolicy ;
21+ retry ?: TaskRetryOptions ;
1522 /**
1623 * The tags to associate with the task.
1724 */
@@ -81,6 +88,48 @@ export function taskOptionsFromRetryPolicy(policy: RetryPolicy): TaskOptions {
8188 return { retry : policy } ;
8289}
8390
91+ /**
92+ * Creates a TaskOptions instance from an AsyncRetryHandler.
93+ *
94+ * @param handler - The async retry handler to use
95+ * @returns A TaskOptions instance configured with the retry handler
96+ *
97+ * @example
98+ * ```typescript
99+ * const handler: AsyncRetryHandler = async (context) => {
100+ * if (context.lastAttemptNumber >= 5) return false;
101+ * if (context.lastFailure.errorType === "ValidationError") return false;
102+ * return true;
103+ * };
104+ *
105+ * const options = taskOptionsFromRetryHandler(handler);
106+ * await ctx.callActivity("myActivity", input, options);
107+ * ```
108+ */
109+ export function taskOptionsFromRetryHandler ( handler : AsyncRetryHandler ) : TaskOptions {
110+ return { retry : handler } ;
111+ }
112+
113+ /**
114+ * Creates a TaskOptions instance from a synchronous RetryHandler.
115+ *
116+ * @param handler - The sync retry handler to use (will be wrapped in a Promise)
117+ * @returns A TaskOptions instance configured with the retry handler
118+ *
119+ * @example
120+ * ```typescript
121+ * const handler: RetryHandler = (context) => {
122+ * return context.lastAttemptNumber < 3;
123+ * };
124+ *
125+ * const options = taskOptionsFromSyncRetryHandler(handler);
126+ * await ctx.callActivity("myActivity", input, options);
127+ * ```
128+ */
129+ export function taskOptionsFromSyncRetryHandler ( handler : RetryHandler ) : TaskOptions {
130+ return { retry : toAsyncRetryHandler ( handler ) } ;
131+ }
132+
84133/**
85134 * Creates a SubOrchestrationOptions instance from a RetryPolicy and optional instance ID.
86135 *
@@ -104,3 +153,46 @@ export function subOrchestrationOptionsFromRetryPolicy(
104153) : SubOrchestrationOptions {
105154 return { retry : policy , instanceId } ;
106155}
156+
157+ /**
158+ * Creates a SubOrchestrationOptions instance from an AsyncRetryHandler and optional instance ID.
159+ *
160+ * @param handler - The async retry handler to use
161+ * @param instanceId - Optional instance ID for the sub-orchestration
162+ * @returns A SubOrchestrationOptions instance configured with the retry handler
163+ *
164+ * @example
165+ * ```typescript
166+ * const handler: AsyncRetryHandler = async (context) => {
167+ * return context.lastAttemptNumber < 3;
168+ * };
169+ *
170+ * const options = subOrchestrationOptionsFromRetryHandler(handler, "my-sub-orch-123");
171+ * ```
172+ */
173+ export function subOrchestrationOptionsFromRetryHandler (
174+ handler : AsyncRetryHandler ,
175+ instanceId ?: string ,
176+ ) : SubOrchestrationOptions {
177+ return { retry : handler , instanceId } ;
178+ }
179+
180+ /**
181+ * Type guard to check if the retry option is a RetryPolicy.
182+ *
183+ * @param retry - The retry option to check
184+ * @returns true if the retry option is a RetryPolicy, false otherwise
185+ */
186+ export function isRetryPolicy ( retry : TaskRetryOptions | undefined ) : retry is RetryPolicy {
187+ return retry instanceof RetryPolicy ;
188+ }
189+
190+ /**
191+ * Type guard to check if the retry option is an AsyncRetryHandler.
192+ *
193+ * @param retry - The retry option to check
194+ * @returns true if the retry option is an AsyncRetryHandler, false otherwise
195+ */
196+ export function isAsyncRetryHandler ( retry : TaskRetryOptions | undefined ) : retry is AsyncRetryHandler {
197+ return typeof retry === "function" ;
198+ }
0 commit comments