import { EventType } from 'cloudcruise';
const handle = await client.runs.start({
workflow_id: "workflow-123",
run_input_variables: { target: "https://example.com" },
});
handle.on(EventType.ExecutionStep, ({ data: { payload }, timestamp }) => {
const { current_step, next_step } = payload;
console.log(`[SESSION: ${handle.sessionId}] STEP: ${current_step} -> ${next_step} @ ${timestamp}`);
});
handle.on(EventType.ExecutionStart, ({ data, timestamp }) => {
console.log(`[SESSION: ${handle.sessionId}] START: ${data.payload.workflow_id} @ ${timestamp}`);
});
handle.on('error', (error) => {
const msg = error instanceof Error ? error.message : String(error);
console.error(`[SESSION: ${handle.sessionId}] ERROR:`, msg);
});
handle.on('end', ({ type }) => {
console.log(`[SESSION: ${handle.sessionId}] Workflow completed${type ? ` with status: ${type}` : ''}!`);
handle.close();
});
// Alternatively, a single catch-all:
// handle.on('run.event', (message) => {
// if (message.event !== 'run.event') return;
// const { event, payload, timestamp } = message.data;
// switch (event) {
// case EventType.ExecutionStep: {
// const { current_step, next_step } = payload;
// console.log(`[SESSION: ${handle.sessionId}] STEP: ${current_step} -> ${next_step} @ ${timestamp}`);
// break;
// }
// default: {
// console.log(`[SESSION: ${handle.sessionId}] EVENT:`, JSON.stringify(message, null, 2));
// }
// }
// });