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: piece branching #4599

Closed
wants to merge 37 commits into from
Closed

feat: piece branching #4599

wants to merge 37 commits into from

Conversation

islamaf
Copy link
Collaborator

@islamaf islamaf commented Apr 30, 2024

What does this PR do?

Fixes #2065

@islamaf islamaf requested a review from abuaboud April 30, 2024 19:36
Copy link

nx-cloud bot commented Apr 30, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 138bd25. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 3 targets

Sent with 💌 from NxCloud.

@islamaf islamaf self-assigned this May 1, 2024
packages/pieces/community/framework/src/lib/context.ts Outdated Show resolved Hide resolved
},
},
// Here we define the names of the outputs expected to be branched from the piece
outputs: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a name since you are using it as an id

We can change this to an object where the key is the name of the branch

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue would be mainly with adding other properties to the branch in case we would need to add this later, if we would go with an object, I suggest this way:

{
    branchName: {
       expectedOutput: true,
       ...otherProperties,
    },
} 

by this way, we would be able to extend the functionality further in the future. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map of objects work

packages/shared/src/lib/flows/actions/action.ts Outdated Show resolved Hide resolved
packages/shared/src/lib/flows/flow-helper.ts Outdated Show resolved Hide resolved
},
},
// Here we define the names of the outputs expected to be branched from the piece
outputs: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map of objects work

@@ -58,6 +58,9 @@ export const PieceActionSettings = Type.Object({
actionName: Type.Optional(Type.String({})),
input: Type.Record(Type.String({}), Type.Any()),
inputUiInfo: SampleDataSettingsObject,
outputs: Type.Optional(Type.Array(Type.Object({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Editing flow definition is bad idea and this information is already duplicated.

You can use action definition in the piece to obtain the branch names.

image

@@ -190,6 +193,10 @@ export const Action = Type.Recursive(action => Type.Union([
})]),
Type.Intersect([PieceActionSchema, Type.Object({
nextAction: Type.Optional(action),
children: Type.Record(
Type.String({}),
Type.Union([action, Type.Undefined()]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

action as value should be enough

@@ -65,6 +66,23 @@ function deleteAction(
parentStep.nextAction = stepToUpdate.nextAction
}
switch (parentStep.type) {
case ActionType.PIECE: {
if (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The frontend should tell us the branch name (which is the same as what is defined in the outputs in the piece), and we should update the action in children map. We should never hardcode 'onFailureAction' and 'onSuccessAction'.

@@ -349,6 +403,22 @@ function updateAction(
parentStep.firstLoopAction = createAction(request, actions)
}
}
if (isPieceBranched(parentStep)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above this is hardcoded.


return branchedPieceResponse()
} else {
return branchedPieceResponse({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are depending on the value to decide where to branch this is wrong, what if both were true.

What if i want to include information like it were approved by who? I think you should use key as to execute branch or not this can be rewritten like that

async function runBranchablePieceWithVersion({
    pieceOutput,
    executionState,
    action,
    constants,
    stepOutput,
    version = 'v1',
}: RunBranchablePieceWithVersion): Promise<FlowExecutorContext> {
    const versions = {
        v1: async (): Promise<FlowExecutorContext> => {
            let newExecutionContext = executionState
            for (const [branchKey, _branchValue] of pieceOutput.output.entries()) {
                const childAction = action.children[branchKey]
                if (isNil(childAction)) {
                    continue
                }
                newExecutionContext = await flowExecutor.execute({
                    action: childAction,
                    executionState: newExecutionContext,
                    constants,
                })
                break;
            }
            return newExecutionContext.upsertStep(action.name, stepOutput
                .setOutput(pieceOutput.output))
                .increaseTask()
                .setVerdict(ExecutionVerdict.RUNNING, undefined)
        },
    }

    return versions[version]()
}

Also you can change generate approval link to

 async run(ctx) {
    return {
      approveLink: ctx.generateResumeUrl({
        queryParams: { action: 'approve' },
      }),
      denyLink: ctx.generateResumeUrl({
        queryParams: { action: 'deny' },
      }),
    };
    ```

import { createAction } from '@activepieces/pieces-framework';
import { ExecutionType, PauseType, branchedPieceResponse } from '@activepieces/shared';

export const waitForApprovalLink = createAction({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will also have to bump package.json

@@ -415,6 +415,7 @@ export class PieceInputFormComponent extends InputFormCore {
...selectedTriggerOrAction.props,
...spreadIfDefined(AUTHENTICATION_PROPERTY_NAME, authProperty),
},
outputs: selectedTriggerOrAction.outputs,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be removed, It's duplicated information, you already have it in definition selectedTriggerOrAction

@@ -383,6 +383,7 @@ export class StepTypeSidebarComponent implements AfterViewInit {
pieceVersion:
flowItemDetails.extra?.pieceVersion ?? 'NO_APP_VERSION',
actionName: suggestion?.name,
outputs: suggestion?.outputs,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be removed, It's duplicated information, you already have it in definition suggestion.outputs

if (step.type === ActionType.BRANCH) {
return index === 0 ? $localize`True` : $localize`False`;
} else {
return index === 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You already allow array in the framework, this will not work for the documentation you have wrote.

The children array / outputs array in the piece can be longer than 2 elements

@abuaboud abuaboud closed this May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve experience for approval links
2 participants