Overview
Events in Sample workflows allow you to trigger workflows and control their execution based on external or internal events. This creates a reactive workflow system where workflows can respond to changes in your application state.Events functionality requires samplehc >= 0.3.0 and workflows-py >= 0.1.18.
Emitting Events
You can emit events from your workflow functions using the SampleHC client. Events consist of a name and an optional payload.Basic Event Emission
Event Structure
Events have the following structure:- name: A string identifier for the event type (e.g., “order-updated”, “order-created”)
- payload: Any JSON-serializable data relevant to the event
Listening to Events
Workflows can listen to events using two mechanisms:start_on and cancel_on.
Starting Workflows on Events
Usestart_on to automatically start a workflow when specific events are received:
Start Handler Return Values
Thestart_on handler function can return:
- An object with optional
start_datafield: Starts the workflow with the provided start data None: Does not start the workflow
Canceling Workflows on Events
Usecancel_on to automatically cancel running workflows when specific events are received:
Cancel Handler Return Values
Thecancel_on handler function should return:
True: Cancel the workflow runFalse: Keep the workflow running
Event Context
When handling events, you have access to an event context that provides:StartOnContext
Available instart_on handlers:
CancelOnContext
Available incancel_on handlers:
Common Patterns
Order Processing Workflow
Multi-Event Workflow
Best Practices
Event Naming
- Use descriptive, consistent event names (e.g., “order-created”, “patient-admitted”)
- Include entity type and action (e.g., “user-registered”, “document-processed”)
- Use kebab-case for event names
Payload Design
- Include essential identifiers in the payload (IDs, timestamps)
- Keep payloads lightweight - avoid large objects
- Include context needed for decision making
Error Handling
Performance Considerations
- Keep event handlers lightweight and fast
- Avoid heavy computations in
start_onandcancel_onhandlers
Browser Agents
Runs
Help Request
- Event name:
sample:browser-agent:help-request - Description: Emitted by a running browser agent when it needs help from a human operator (for example, to resolve ambiguity or request manual intervention).
- Payload structure:
browserAgentRunId: The ID of the browser agent run that requested helpbrowserAgentSlug: The slug identifier of the browser agentrequest: The help request message from the browser agent
- Start or unlock a workflow step awaiting human input from a browser agent.
- Notify an operations dashboard that a specific browser-agent run needs assistance.
Resolving Help Requests
Once a help request has been addressed, you can resolve it using the browser agents API:- Use specific event matching to avoid unnecessary workflow starts/cancellations
Sample System Emitted Events
Sample automatically emits certain events when specific actions occur in your system. These events are prefixed withsample: and can be used to trigger workflows automatically.
sample:email:* pattern.
received
When an email is sent to your sample mailbox ([email protected]), Sample automatically emits a sample:email:received event.
Event Name: sample:email:received
Payload Structure:
Kno2
All Kno2-related events are emitted under thesample:kno2:* pattern when messages are received through your Kno2 integration.
message-receive
When a message (such as a fax) is received through your Kno2 connection, Sample automatically emits asample:kno2:${connection.slug}:message-receive event, where ${connection.slug} corresponds to the specific Kno2 connection that received the message.
Event Name: sample:kno2:${connection.slug}:message-receive
Payload Structure:
message.id: Unique identifier for the messagemessage.subject: Subject line of the fax/messagemessage.toAddress: Recipient fax numbermessage.fromAddress: Sender fax numbermessage.sourceType: Type of message (e.g., “Fax”)message.attachments: Array of attachment metadata from Kno2attachments: Array of Sample file metadata IDs for downloaded attachments
Troubleshooting
Common Issues
Workflow not starting on events:- Verify event names match exactly between emit and listen
- Check that handler functions return appropriate values
- Ensure version requirements are met
- Review
cancel_onhandler logic - Check event payload structure
- Verify cancellation conditions are specific enough
- Ensure event payload contains required fields
- Check that
get_start_data()returns expected structure - Verify event emission includes necessary context