Skip to content

Commit

Permalink
chore: Blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ramedina86 committed Sep 30, 2024
1 parent 2118c61 commit a533a7a
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/writer/workflows_blocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from writer.workflows_blocks.setstate import SetState
from writer.workflows_blocks.writerclassification import WriterClassification
from writer.workflows_blocks.writercompletion import WriterCompletion

from writer.workflows_blocks.runworkflow import RunWorkflow

SetState.register("workflows_setstate")
WriterClassification.register("workflows_writerclassification")
WriterCompletion.register("workflows_writercompletion")
HTTPRequest.register("workflows_httprequest")
HTTPRequest.register("workflows_httprequest")
RunWorkflow.register("workflows_runworkflow")
17 changes: 6 additions & 11 deletions src/writer/workflows_blocks/blocks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from typing import TYPE_CHECKING
from typing import Dict
import writer.workflows_blocks
import writer.core

if TYPE_CHECKING:
from writer.core_ui import Component
import writer.core_ui

block_map = {}

Expand All @@ -13,21 +11,18 @@ class WorkflowBlock:
def register(cls, type: str):
block_map[type] = cls

def __init__(self, component: "Component", execution: dict, session: "writer.core.WriterSession", result: dict):
def __init__(self, component: "writer.core_ui.Component", execution: Dict, session: "writer.core.WriterSession", execution_env: Dict):
self.outcome = None
self.component = component
self.execution = execution
self.session = session
self.result = result
self.execution_env = execution_env
self.result = None
self.evaluator = writer.core.Evaluator(session.session_state, session.session_component_tree)
self.instance_path = [{"componentId": self.component.id, "instanceNumber": 0}]

def _get_field(self, field_key: str, as_json=False):
base_context = {
"result": self.result
}

v = self.evaluator.evaluate_field(self.instance_path, field_key, base_context=base_context, as_json=as_json)
v = self.evaluator.evaluate_field(self.instance_path, field_key, base_context=self.execution_env, as_json=as_json)

return v

Expand Down
64 changes: 64 additions & 0 deletions src/writer/workflows_blocks/foreach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from writer.abstract import register_abstract_template
from writer.ss_types import AbstractTemplate
from writer.workflows_blocks.blocks import WorkflowBlock
import writer.workflows

class ForEach(WorkflowBlock):

@classmethod
def register(cls, type: str):
super(ForEach, cls).register(type)
register_abstract_template(type, AbstractTemplate(
baseType="workflows_node",
writer={
"name": "For-each loop",
"description": "Executes a workflow repeatedly, based on the items provided.",
"category": "Content",
"fields": {
"workflowKey": {
"name": "Workflow key",
"desc": "The workflow which will be executed for each item.",
"type": "Text",
},
"items": {
"name": "Items",
"desc": "The item value will be passed in the execution context and will be available via @{item}.",
"default": "{}",
"type": "Object",
"control": "Textarea"
},
"context": {
"name": "Context",
"desc": "You can add other values to the execution context.",
"default": "{}",
"type": "Object",
"control": "Textarea"
},
},
"outs": {
"success": {
"name": "Success",
"description": "The workflow wasn't executed successfully.",
"style": "success",
},
"error": {
"name": "Error",
"description": "The workflow wasn't executed successfully.",
"style": "error",
},
},
}
))

def run(self):
workflow_key = self._get_field("workflowKey")
items = self._get_field("items")
context = self._get_field("context")

try:
for item in items:
writer.workflows.run_workflow_by_key(self.session, workflow_key)
self.outcome = "success"
except Exception as e:
self.result = "HTTP call failed."
self.outcome = "connectionError"
1 change: 0 additions & 1 deletion src/writer/workflows_blocks/httprequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def register(cls, type: str):
"name": "HTTP Request",
"description": "Executes an HTTP request",
"category": "Content",
"allowedParentTypes": ["workflows_workflow"],
"fields": {
"method": {
"name": "Method",
Expand Down
55 changes: 55 additions & 0 deletions src/writer/workflows_blocks/runworkflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from writer.abstract import register_abstract_template
from writer.ss_types import AbstractTemplate
from writer.workflows_blocks.blocks import WorkflowBlock
import writer.workflows

class RunWorkflow(WorkflowBlock):

@classmethod
def register(cls, type: str):
super(RunWorkflow, cls).register(type)
register_abstract_template(type, AbstractTemplate(
baseType="workflows_node",
writer={
"name": "Run workflow",
"description": "Executes a workflow",
"category": "Content",
"fields": {
"workflowKey": {
"name": "Workflow key",
"type": "Text",
},
"context": {
"name": "Context",
"desc": "Values passed in the context will be available using the template syntax i.e. @{my_context_var}",
"default": "{}",
"type": "Object",
"control": "Textarea"
},
},
"outs": {
"success": {
"name": "Success",
"description": "The request was successful.",
"style": "success",
},
"error": {
"name": "Error",
"description": "The workflow was executed successfully.",
"style": "error",
},
},
}
))

def run(self):
workflow_key = self._get_field("workflowKey")
context = self._get_field("context", as_json=True)

try:
writer.workflows.run_workflow_by_key(self.session, workflow_key, context)
self.outcome = "success"
except Exception as e:
print("running the other workflow " + repr(e))
self.result = "Running workflow failed."
self.outcome = "error"
1 change: 0 additions & 1 deletion src/writer/workflows_blocks/setstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def register(cls, type: str):
"name": "Set state",
"description": "Set the value for a state element",
"category": "Content",
"allowedParentTypes": ["workflows_workflow"],
"fields": {
"element": {
"name": "State element",
Expand Down
1 change: 0 additions & 1 deletion src/writer/workflows_blocks/writerclassification.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def register(cls, type: str):
"name": "Writer Classification",
"description": "Classify a text.",
"category": "Content",
"allowedParentTypes": ["workflows_workflow"],
"fields": {
"text": {
"name": "Text",
Expand Down
1 change: 0 additions & 1 deletion src/writer/workflows_blocks/writercompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def register(cls, type: str):
"name": "Writer Completion",
"description": "Set the value for a state element",
"category": "Content",
"allowedParentTypes": ["workflows_workflow"],
"fields": {
"prompt": {
"name": "Prompt",
Expand Down

0 comments on commit a533a7a

Please sign in to comment.