@@ -2,7 +2,9 @@ import { BaseChannel } from "async-channel";
22import { assert } from "convex-helpers" ;
33import { validate , ValidationError } from "convex-helpers/validators" ;
44import {
5+ createFunctionHandle ,
56 internalMutationGeneric ,
7+ makeFunctionReference ,
68 type RegisteredMutation ,
79} from "convex/server" ;
810import {
@@ -22,11 +24,18 @@ import { type RunResult, type WorkpoolOptions } from "@convex-dev/workpool";
2224import { type WorkflowComponent } from "./types.js" ;
2325import { vWorkflowId } from "../types.js" ;
2426import { formatErrorWithStack } from "../shared.js" ;
27+ import { safeFunctionName } from "./safeFunctionName.js" ;
2528
26- const workflowArgs = v . object ( {
27- workflowId : vWorkflowId ,
28- generationNumber : v . number ( ) ,
29- } ) ;
29+ const workflowArgs = v . union (
30+ v . object ( {
31+ workflowId : vWorkflowId ,
32+ generationNumber : v . number ( ) ,
33+ } ) ,
34+ v . object ( {
35+ fn : v . string ( ) ,
36+ args : v . any ( ) ,
37+ } ) ,
38+ ) ;
3039const INVALID_WORKFLOW_MESSAGE = `Invalid arguments for workflow: Did you invoke the workflow with ctx.runMutation() instead of workflow.start()?` ;
3140
3241// This function is defined in the calling component but then gets passed by
@@ -47,6 +56,16 @@ export function workflowMutation<ArgsValidator extends PropertyValidators>(
4756 if ( ! validate ( workflowArgs , args ) ) {
4857 throw new Error ( INVALID_WORKFLOW_MESSAGE ) ;
4958 }
59+ if ( "fn" in args ) {
60+ const fn = makeFunctionReference ( args . fn ) ;
61+ await ctx . runMutation ( component . workflow . create , {
62+ workflowName : safeFunctionName ( fn ) ,
63+ workflowHandle : await createFunctionHandle ( fn ) ,
64+ workflowArgs : args . args ,
65+ maxParallelism : workpoolOptions . maxParallelism ,
66+ } ) ;
67+ return ;
68+ }
5069 const { workflowId, generationNumber } = args ;
5170 const { workflow, logLevel, journalEntries, ok } = await ctx . runQuery (
5271 component . journal . load ,
0 commit comments