-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add server api for apps + Add external hooks for apps #101
Conversation
Add external hooks for appsnew package: Description
Installyarn add @encrejs/hooks HooksuseAppHandler
Event handlers are used for
Note Before using Usageimport { useAppHandler } from "@encrejs/hooks"; Encre Workflow Handler ConfigurationTo use The schema of configuration shows as follows: {
"workflow": "<absolute-path-encre-workflow-file>",
"handlers": {
"<input-handler-1>": {
"handlerType": "input",
"description": "<handler-description>",
"event": "<handler-event-name>",
...
},
...,
"<action-handler-1>": {
"handlerType": "action",
"description": "<handler-description>",
"event": "<handler-event-name>",
...
},
...,
"<output-handler-1>": {
"handlerType": "output",
"description": "<handler-description>",
"event": "<handler-event-name>",
...
},
...
}
} Note Currently, only one workflow is supported in a single APP. If you are looking for multiple workflows support, please follow #108 Handlers
|
|
||
import { EncreData } from './types'; | ||
|
||
export interface EncreCache { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this interface for specifically, I understand that it is storing a graph and a node, but just want to make sure I understand the software, what part fo the process does EncreCache speeds up specifically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good Question! EncreCache
is used for storing incoming data from input handlers. You can image a scenario where there is a message box that connects to an input handler, so whenever user change the message, the input handler should store that changed value. But this changed value is not sent to the workflow immediately, since no action is triggered by the uset. So we need a cache to store this value.
} from '../types'; | ||
import { coerceEncreData } from '../utils'; | ||
|
||
const runGraphFetch = async ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure I understand this, is the purpose of this reads what's in the graph and populate important information in Record<> form?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and if the callbacks are unknown, what are the purposes of these callbacks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you reference the code chunk? I need know what callbacks you are asking here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, this:
nodeCallbacks: {
[key in string]: {
onNodeStart?: (
nodeId: string,
nodeTitle: string,
inputs: Record<string, EncreData>,
) => unknown;
onNodeFinish?: (
nodeId: string,
nodeTitle: string,
outputs: Record<string, EncreData>,
) => unknown;
};
},
graphCallbacks: {
onDone?: (
graphOutput: Record<string, Record<string, EncreData>>,
) => unknown;
onError?: (error: string) => unknown;
},
What are these use for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These callbacks are coming from users. Basically, when user called onOutput
function, they can write their own onRead
, onDone
, onError
callbacks. Those callbacks will be formatted into the corresponding nodeCallbacks
, graphCallbacks
based on the hooks logic. So we won't know the output of those callbacks are. However, we will coerce those outputs so that we can ensure the data is formatted correctly to those output Components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Hooks documentation may have more detailed description for those callbacks.
nodeId: json['nodeId'], | ||
nodeTitle: json['nodeTitle'], | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is metadata encoded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for sending the data to the ReadableStream
|
||
// console.log(`encodedText: ${encodedText}`); | ||
|
||
const queue = encoder.encode(encodedText); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the purpose of encoding an encodedText?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above. This is for sending the data to the ReadableStream
Add server api for apps
add new server api:
/app/run
- Run the Encre workflow file (e.g. xxx.encre) and listening the workflow events through SSE (Server-Sent Events)/app/run
- Run Encre WorkflowMethod
POST
Authentication required
No
Body
userInputs
(GraphInputs
) - Required. The inputs for running the Encre workflow, following the type schemaGraphInputs
. The schema is shown as follows:appPath
(string
) - Required. The absolute path to the Encre workflow file.context
(Record<string, Data>
) - Optional. The context for running the Encre workflow.filter
(ProcessStreamEventFilter
) - Optional. A set of flags for indicating whether showing the workflow running result for the corresponding workflow event. These workflow events can be shown if those present in the filter:nodeStart
- whether betrue
, or an array of node id that indicates those are the nodes that require sending running results.nodeFinish
- whether betrue
, or an array of node id that indicates those are the nodes that require sending running results.done
- can betrue
.error
- can betrue
.Note
For the
userInputs
attribute, please provide node value for the START nodes in the workflow, any nodes that are not from the START nodes will not overwrite their port values fromuserInputs
.If you are looking for how to add user inputs for nodes during the workflow running, please look at PR
Warning
For the
filter
attribute, if not provided, then no event result can be sent from the server.Response
This API is a SSE (Server-Sent Events), so you should keep the connection open to receive the complete response.
text/event-stream
chunked
no-cache
keep-alive
There is a detailed description of the event data format:
Event Types
Node Start
Emit event when a node processing begins.
nodeStart
nodeId
(string
): node ID.nodeTitle
(string
): node title (default is node name).inputs
(ProcessInputMap
): the value for the node input ports, following the schema:Node Finish
Emit event when a node processing is finished successfully.
nodeFinish
nodeId
(string
): node ID.nodeTitle
(string
): node title (default is node name).outputs
(ProcessOutputMap
): the value for the node output ports, sharing the same schema asProcessInputMap
.Graph Processing Done
Emit event when the workflow finished processing (on whether success or not).
done
graphOutput
(GraphOutputs
): the final outputs from the graph processing, sharing the same schema asGraphInputs
. If any error happens, no value should be sent from this event.Graph Processing Error
Emit event when the workflow finished processing with errors.
error
error
(string
): the stacked error messages in some node processing error.Note
By rules, the "Graph Processing Done" event is the last event that can be emitted from the workflow processing. However, it is recommanded to wait the
data: [DONE]
message before closing the SSE connection.