Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add execute to createWorkflow context #17

Closed
wants to merge 10 commits into from
Closed

Conversation

EvanBoyle
Copy link
Member

@EvanBoyle EvanBoyle commented Dec 19, 2024

Adds an execute method to the context provided by createWorkflow so that every component can evaluate subgraphs at the javascript layer.

This enables enables things like the fanout pattern and conditional execution in favor of the pattern we tried in #15

Here's the new example:

export const FanoutWorkflow = createWorkflow<
  FanoutWorkflowInputs,
  FanoutWorkflowOutputs
>(async (props, { resolve, execute }) => {
// this executes a subgraph at the JS layer
  const doubledNums = props.numbers.map(n =>
    execute(DoubleNumber, { input: n }),
  );

// execute another subgraph at the JS layer
  const doubledStrings = props.strings.map(s =>
    execute(DoubleString, { input: s }),
  );

// let operations run in parallel and resolve them. 
  const resolvedNums = await Promise.all(doubledNums);
  const resolvedStrings = await Promise.all(doubledStrings);

  return (
    <SumNumbers numbers={resolvedNums}>
      {sum => (
        <ConcatStrings strings={resolvedStrings}>
          {str => resolve({ num: sum, str })}
        </ConcatStrings>
      )}
    </SumNumbers>
  );
});

Notice that the execute API has an interesting signature:

execute(DoubleNumber, { input: n })

// execute(Component, props)

This is required in order to get type safety. I originally tried to do this via passing in JSX to execute, but unfortunately we lose all type safety.

execute doesn't currently support passing in children for a component. I'm not inclined to add support for it as it will add a lot of complexity (the return type of the thing becomes the return type of the last child, have to have patterns for chaining inputs/outputs together). But we can always add it later if we find a use case.

@EvanBoyle EvanBoyle changed the title Add execute to createWorkflow context feat: Add execute to createWorkflow context Dec 19, 2024
@EvanBoyle EvanBoyle requested a review from jmoseley December 19, 2024 17:03
@EvanBoyle
Copy link
Member Author

After some testing and getting internal and external feedback, we've decided that this approach has too many concepts and idiosyncrasies. We believe that the tradeoff to maintain type safety isn't worth it.

Plan is to ship the approach in #20 and follow up with experiments on typescript compiler extensions and LSP plugins to get the type safety we need until microsoft/TypeScript#21699 is fixed (if it ever gets fixed).

@EvanBoyle EvanBoyle closed this Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant