Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 139 additions & 60 deletions example/convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,38 @@ export declare const components: {
journalEntries: Array<{
_creationTime: number;
_id: string;
step: {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
};
step:
| {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
type: "execution";
workId?: string;
}
| {
args: any;
argsSize: number;
completedAt?: number;
inProgress: boolean;
name: string;
onPauseHandle?: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
type: "pause";
workId?: string;
};
stepNumber: number;
workflowId: string;
}>;
Expand All @@ -91,6 +108,17 @@ export declare const components: {
};
}
>;
resume: FunctionReference<
"mutation",
"internal",
{
name?: string;
resumeValue: any;
workflowHandle: string;
workflowId: string;
},
null
>;
startSteps: FunctionReference<
"mutation",
"internal",
Expand All @@ -101,21 +129,38 @@ export declare const components: {
| boolean
| { base: number; initialBackoffMs: number; maxAttempts: number };
schedulerOptions?: { runAt?: number } | { runAfter?: number };
step: {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
};
step:
| {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
type: "execution";
workId?: string;
}
| {
args: any;
argsSize: number;
completedAt?: number;
inProgress: boolean;
name: string;
onPauseHandle?: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
type: "pause";
workId?: string;
};
}>;
workflowId: string;
workpoolOptions?: {
Expand All @@ -132,21 +177,38 @@ export declare const components: {
Array<{
_creationTime: number;
_id: string;
step: {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
};
step:
| {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
type: "execution";
workId?: string;
}
| {
args: any;
argsSize: number;
completedAt?: number;
inProgress: boolean;
name: string;
onPauseHandle?: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
type: "pause";
workId?: string;
};
stepNumber: number;
workflowId: string;
}>
Expand Down Expand Up @@ -199,21 +261,38 @@ export declare const components: {
inProgress: Array<{
_creationTime: number;
_id: string;
step: {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
};
step:
| {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
type: "execution";
workId?: string;
}
| {
args: any;
argsSize: number;
completedAt?: number;
inProgress: boolean;
name: string;
onPauseHandle?: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
type: "pause";
workId?: string;
};
stepNumber: number;
workflowId: string;
}>;
Expand Down
2 changes: 1 addition & 1 deletion example/convex/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import schema from "./schema";
export const modules = import.meta.glob("./**/*.*s");

// Sorry about everything
import componentSchema from "../../node_modules/@convex-dev/workflow/src/component/schema";
import componentSchema from "../../src/component/schema";
export { componentSchema };
export const componentModules = import.meta.glob(
"../../node_modules/@convex-dev/workflow/src/component/**/*.ts",
Expand Down
32 changes: 32 additions & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,38 @@ export class WorkflowManager {
workflowId,
});
}

/**
* Resume a paused workflow with a type-safe value.
*
* @param ctx - The Convex context.
* @param workflow - The workflow function reference for type safety.
* @param workflowId - The workflow ID.
* @param resumeValue - The value to pass to the paused step.
* @param opts - Options including the validator for type inference and optional step name.
*/
async resume<
F extends FunctionReference<"mutation", "internal">,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
V extends Validator<any, "optional", any>,
>(
ctx: RunMutationCtx,
workflow: F,
workflowId: WorkflowId,
resumeValue: unknown,
opts?: {
returns?: V;
name?: string;
},
): Promise<void> {
const handle = await createFunctionHandle(workflow);
await ctx.runMutation(this.component.journal.resume, {
workflowHandle: handle,
workflowId,
resumeValue,
name: opts?.name,
});
}
}

type RunQueryCtx = {
Expand Down
68 changes: 51 additions & 17 deletions src/client/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,30 @@ export type WorkerResult =
| { type: "handlerDone"; runResult: RunResult }
| { type: "executorBlocked" };

export type StepRequest = {
export type ExecutionStepRequest = {
type: "execution";
name: string;
functionType: FunctionType;
function: FunctionReference<FunctionType, "internal">;
args: unknown;
retry: RetryBehavior | boolean | undefined;
schedulerOptions: SchedulerOptions;
resolve: (result: unknown) => void;
reject: (error: unknown) => void;
};

export type PauseStepRequest = {
type: "pause";
name: string;
onPauseFunction?: FunctionReference<"mutation", "internal">;
args: unknown;
schedulerOptions: SchedulerOptions;
resolve: (result: unknown) => void;
reject: (error: unknown) => void;
};

export type StepRequest = ExecutionStepRequest | PauseStepRequest;

const MAX_JOURNAL_SIZE = 8 << 20;

export class StepExecutor {
Expand Down Expand Up @@ -131,22 +143,44 @@ export class StepExecutor {
async startSteps(messages: StepRequest[]): Promise<JournalEntry[]> {
const steps = await Promise.all(
messages.map(async (message) => {
const step = {
inProgress: true,
name: message.name,
functionType: message.functionType,
handle: await createFunctionHandle(message.function),
args: message.args,
argsSize: valueSize(message.args as Value),
outcome: undefined,
startedAt: this.now,
completedAt: undefined,
};
return {
retry: message.retry,
schedulerOptions: message.schedulerOptions,
step,
};
if (message.type === "execution") {
const step = {
type: "execution" as const,
inProgress: true,
name: message.name,
functionType: message.functionType,
handle: await createFunctionHandle(message.function),
args: message.args,
argsSize: valueSize(message.args as Value),
runResult: undefined,
startedAt: this.now,
completedAt: undefined,
};
return {
retry: message.retry,
schedulerOptions: message.schedulerOptions,
step,
};
} else {
const step = {
type: "pause" as const,
inProgress: true,
name: message.name,
onPauseHandle: message.onPauseFunction
? await createFunctionHandle(message.onPauseFunction)
: undefined,
args: message.args,
argsSize: valueSize(message.args as Value),
runResult: undefined,
startedAt: this.now,
completedAt: undefined,
};
return {
retry: undefined,
schedulerOptions: message.schedulerOptions,
step,
};
}
}),
);
const entries = (await this.ctx.runMutation(
Expand Down
Loading