-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrun_workflow_streaming.js
87 lines (79 loc) · 2.17 KB
/
run_workflow_streaming.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const fetch = require("node-fetch");
const readline = require("readline");
const API_URL = "http://localhost:8000/api";
const API_TOKEN = "local_token"; // can be anything for local
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
async function runWorkflow(workflowId, params = {}) {
const response = await fetch(`${API_URL}/jobs/run?stream=true`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${API_TOKEN}`,
},
body: JSON.stringify({
workflow_id: workflowId,
params: params,
}),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
for await (const chunk of response.body) {
const lines = chunk.toString().split("\n");
for (const line of lines) {
if (line.trim() === "") continue;
try {
const message = JSON.parse(line);
handleMessage(message);
} catch (error) {
console.error("Error parsing message:", error);
}
}
}
}
function handleMessage(message) {
switch (message.type) {
case "job_update":
console.log("Job status:", message.status);
if (message.status === "completed") {
console.log("Workflow completed:", message.result);
}
break;
case "node_progress":
console.log(
"Node progress:",
message.node_id,
`${((message.progress / message.total) * 100).toFixed(2)}%`
);
break;
case "node_update":
console.log(
"Node update:",
message.node_id,
message.status,
message.error || ""
);
if (message.logs) console.log("Logs:", message.logs);
if (message.result) console.log("Result:", message.result);
break;
default:
console.log("Unknown message type:", message);
}
}
rl.question("Workflow ID: ", async (workflowId) => {
try {
console.log("Running workflow...");
await runWorkflow(workflowId);
console.log("Workflow execution completed.");
} catch (error) {
console.error("Error running workflow:", error);
} finally {
rl.close();
}
});
rl.on("close", () => {
process.exit(0);
});