Skip to content

Commit c948a94

Browse files
jcorteslcaresia
authored andcommitted
[Components] fal_ai - New action components (#14545)
1 parent 56539dd commit c948a94

File tree

7 files changed

+342
-7
lines changed

7 files changed

+342
-7
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import app from "../../fal_ai.app.mjs";
2+
3+
export default {
4+
key: "fal_ai-add-request-to-queue",
5+
name: "Add Request to Queue",
6+
description: "Adds a request to the queue for asynchronous processing, including specifying a webhook URL for receiving updates. [See the documentation](https://fal.ai/docs/model-endpoints/queue#queue-endpoints).",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
appId: {
12+
propDefinition: [
13+
app,
14+
"appId",
15+
],
16+
},
17+
data: {
18+
type: "object",
19+
label: "Data",
20+
description: "Additional data to include with the request. [See the documentation](https://fal.ai/models/fal-ai/lora/api#schema-input) for more input fields.",
21+
default: {
22+
model_name: "stabilityai/stable-diffusion-xl-base-1.0",
23+
prompt: "Photo of a european medieval 40 year old queen, silver hair, highly detailed face, detailed eyes, head shot, intricate crown, age spots, wrinkles",
24+
},
25+
},
26+
reRunEnabled: {
27+
type: "boolean",
28+
label: "Rerun Enabled",
29+
description: "Enable the step to rerun to retrieve the request response. [See the documentation](https://pipedream.com/docs/code/nodejs/rerun/#flowrerun).",
30+
optional: true,
31+
reloadProps: true,
32+
default: false,
33+
},
34+
},
35+
additionalProps() {
36+
if (this.reRunEnabled) {
37+
return {
38+
reRunTimeoutInSecs: {
39+
type: "integer",
40+
label: "Rerun Timeout",
41+
description: "The time in seconds to wait before rerunning the step to retrieve the request response. Eg. `30`. [See the documentation](https://pipedream.com/docs/code/nodejs/rerun/#flowrerun).",
42+
optional: true,
43+
min: 10,
44+
},
45+
};
46+
}
47+
48+
return {
49+
falWebhook: {
50+
type: "string",
51+
label: "Webhook URL",
52+
description: "The URL to receive updates via webhook.",
53+
optional: true,
54+
},
55+
};
56+
},
57+
methods: {
58+
addToQueue({
59+
appId, ...args
60+
} = {}) {
61+
return this.app.post({
62+
path: `/${appId}`,
63+
...args,
64+
});
65+
},
66+
},
67+
async run({ $ }) {
68+
const {
69+
context: {
70+
run: {
71+
runs,
72+
callback_request: callbackRequest,
73+
},
74+
},
75+
} = $;
76+
77+
const {
78+
app,
79+
addToQueue,
80+
appId,
81+
data,
82+
falWebhook,
83+
reRunEnabled,
84+
reRunTimeoutInSecs,
85+
} = this;
86+
87+
if (!reRunEnabled) {
88+
const response = await addToQueue({
89+
$,
90+
appId,
91+
params: {
92+
fal_webhook: falWebhook,
93+
},
94+
data,
95+
});
96+
97+
$.export("$summary", `Successfully added the request to the queue with ID \`${response.request_id}\`.`);
98+
return response;
99+
}
100+
101+
if (runs === 1) {
102+
const timeout = 1000 * (reRunTimeoutInSecs || 10);
103+
const { resume_url: resumeUrl } = $.flow.rerun(timeout, null, 1);
104+
105+
return addToQueue({
106+
$,
107+
appId,
108+
params: {
109+
fal_webhook: resumeUrl,
110+
},
111+
data,
112+
});
113+
}
114+
115+
const response = await app.getRequestResponse({
116+
$,
117+
appId,
118+
requestId: callbackRequest.body?.request_id,
119+
});
120+
121+
$.export("$summary", "Successfully retrieved the request response.");
122+
return response;
123+
},
124+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import app from "../../fal_ai.app.mjs";
2+
3+
export default {
4+
key: "fal_ai-cancel-request",
5+
name: "Cancel Request",
6+
description: "Cancels a request in the queue. This allows you to stop a long-running task if it's no longer needed. [See the documentation](https://fal.ai/docs/model-endpoints/queue#queue-endpoints).",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
appId: {
12+
propDefinition: [
13+
app,
14+
"appId",
15+
],
16+
},
17+
requestId: {
18+
propDefinition: [
19+
app,
20+
"requestId",
21+
],
22+
},
23+
},
24+
methods: {
25+
cancelRequest({
26+
appId, requestId, ...args
27+
} = {}) {
28+
return this.app.put({
29+
path: `/${appId}/requests/${requestId}/cancel`,
30+
...args,
31+
});
32+
},
33+
},
34+
async run({ $ }) {
35+
const {
36+
cancelRequest,
37+
appId,
38+
requestId,
39+
} = this;
40+
41+
const response = await cancelRequest({
42+
$,
43+
appId,
44+
requestId,
45+
});
46+
47+
$.export("$summary", "Successfully canceled request.");
48+
return response;
49+
},
50+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import app from "../../fal_ai.app.mjs";
2+
3+
export default {
4+
key: "fal_ai-get-request-response",
5+
name: "Get Request Response",
6+
description: "Gets the response of a completed request in the queue. This retrieves the results of your asynchronous task. [See the documentation](https://fal.ai/docs/model-endpoints/queue#queue-endpoints).",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
appId: {
12+
propDefinition: [
13+
app,
14+
"appId",
15+
],
16+
},
17+
requestId: {
18+
propDefinition: [
19+
app,
20+
"requestId",
21+
],
22+
},
23+
},
24+
async run({ $ }) {
25+
const {
26+
app,
27+
appId,
28+
requestId,
29+
} = this;
30+
31+
const response = await app.getRequestResponse({
32+
$,
33+
appId,
34+
requestId,
35+
});
36+
37+
$.export("$summary", "Successfully retrieved the request response.");
38+
return response;
39+
},
40+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import app from "../../fal_ai.app.mjs";
2+
3+
export default {
4+
key: "fal_ai-get-request-status",
5+
name: "Get Request Status",
6+
description: "Gets the status of a request in the queue. This allows you to monitor the progress of your asynchronous tasks. [See the documentation](https://fal.ai/docs/model-endpoints/queue#queue-endpoints).",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
appId: {
12+
propDefinition: [
13+
app,
14+
"appId",
15+
],
16+
},
17+
requestId: {
18+
propDefinition: [
19+
app,
20+
"requestId",
21+
],
22+
},
23+
logs: {
24+
propDefinition: [
25+
app,
26+
"logs",
27+
],
28+
},
29+
},
30+
async run({ $ }) {
31+
const {
32+
app,
33+
appId,
34+
requestId,
35+
logs,
36+
} = this;
37+
38+
const response = await app.getRequestStatus({
39+
$,
40+
appId,
41+
requestId,
42+
params: {
43+
logs: logs
44+
? 1
45+
: undefined,
46+
},
47+
});
48+
49+
$.export("$summary", `Successfully retrieved status as \`${response.status}\`.`);
50+
51+
return response;
52+
},
53+
};

components/fal_ai/fal_ai.app.mjs

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,73 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "fal_ai",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
appId: {
8+
type: "string",
9+
label: "App ID",
10+
description: "The unique identifier for the app. Eg. `lora`.",
11+
},
12+
requestId: {
13+
type: "string",
14+
label: "Request ID",
15+
description: "The unique identifier for the request.",
16+
},
17+
logs: {
18+
type: "boolean",
19+
label: "Enable Logs",
20+
description: "Specify if logs should be enabled for the request status.",
21+
optional: true,
22+
},
23+
},
524
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
25+
getUrl(path) {
26+
return `https://queue.fal.run/fal-ai${path}`;
27+
},
28+
getHeaders(headers) {
29+
return {
30+
...headers,
31+
"Authorization": `Key ${this.$auth.api_key}`,
32+
"Content-Type": "application/json",
33+
};
34+
},
35+
_makeRequest({
36+
$ = this, path, headers, ...args
37+
} = {}) {
38+
return axios($, {
39+
...args,
40+
url: this.getUrl(path),
41+
headers: this.getHeaders(headers),
42+
});
43+
},
44+
post(args = {}) {
45+
return this._makeRequest({
46+
method: "POST",
47+
...args,
48+
});
49+
},
50+
put(args = {}) {
51+
return this._makeRequest({
52+
method: "PUT",
53+
...args,
54+
});
55+
},
56+
getRequestStatus({
57+
appId, requestId, ...args
58+
} = {}) {
59+
return this._makeRequest({
60+
path: `/${appId}/requests/${requestId}/status`,
61+
...args,
62+
});
63+
},
64+
getRequestResponse({
65+
appId, requestId, ...args
66+
} = {}) {
67+
return this._makeRequest({
68+
path: `/${appId}/requests/${requestId}`,
69+
...args,
70+
});
971
},
1072
},
1173
};

components/fal_ai/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/fal_ai",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream fal.ai Components",
55
"main": "fal_ai.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "3.0.3"
1417
}
15-
}
18+
}

pnpm-lock.yaml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)