@acedatacloud/piece-gpt-image 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +36 -2
- package/README.zh-CN.md +37 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +17 -0
- package/dist/src/lib/actions/generate-image.d.ts +7 -0
- package/dist/src/lib/actions/generate-image.js +79 -0
- package/dist/src/lib/actions/get-task.d.ts +4 -0
- package/dist/src/lib/actions/get-task.js +37 -0
- package/dist/src/lib/auth.d.ts +1 -0
- package/dist/src/lib/auth.js +9 -0
- package/dist/src/lib/common/client.d.ts +25 -0
- package/dist/src/lib/common/client.js +132 -0
- package/package.json +28 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ace Data Cloud
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
-
#
|
|
1
|
+
# GPT Image for Activepieces
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Create a GPT Image task and retrieve the **same task ID** through Ace Data Cloud. The Piece has two actions: **Create GPT Image task** and **Get GPT Image task**. Submission returns an ID; the image is ready only after the lookup reports `succeeded`.
|
|
4
|
+
|
|
5
|
+
**Distribution:** Install `@acedatacloud/piece-gpt-image@0.1.0` as a custom Piece in your Activepieces instance. A public npm package does not automatically enter the shared Activepieces catalog. [中文指南](README.zh-CN.md)
|
|
6
|
+
|
|
7
|
+
## Install and connect
|
|
8
|
+
|
|
9
|
+
In Activepieces 0.92.2+, open **Platform Admin → Catalogue → Pieces → Install Piece → NPM Registry**. Enter `@acedatacloud/piece-gpt-image` and version `0.1.0`, then install it. Search for **GPT Image by Ace Data Cloud** when adding a flow action. Confirm the Ace Data Cloud logo and both actions.
|
|
10
|
+
|
|
11
|
+
Create a connection using an Ace Data Cloud API key with access to both `/openai/images/generations` and `/openai/tasks`. Enter only the key, without `Bearer `. Keep it in the connection, never in a flow export. Check current [pricing](https://platform.acedata.cloud/models) and balance before generation.
|
|
12
|
+
|
|
13
|
+
## First flow
|
|
14
|
+
|
|
15
|
+
1. Add **Create GPT Image task**. Enter a prompt, choose `gpt-image-2`, quality `low`, size `1024x1024`, and image count `1`.
|
|
16
|
+
2. Run creation **once** and save its `task_id`. The action uses `callback_url` to submit asynchronously and has no HTTP retry. If a timeout or 5xx occurs, inspect request history before submitting again: the task may already exist.
|
|
17
|
+
3. Add **Get GPT Image task**. Map its **Task ID** from the creation output. Query this same ID until `status=succeeded` or `failed`. Pending media is never returned as final output.
|
|
18
|
+
4. Open a URL from `media_urls` only after `success=true`. The lookup can alternatively use a trace ID if the original request supplied a custom trace ID and its task ID was lost. This creation action does not set a custom trace ID.
|
|
19
|
+
|
|
20
|
+
The [credential-free quickstart flow](https://raw.githubusercontent.com/AceDataCloud/GPTImageActivepieces/main/examples/gpt-image-quickstart.json) imports with both connections empty and the task-ID mapping intact. Add your own connection before running it. Its creation action has automatic retries disabled.
|
|
21
|
+
|
|
22
|
+
Image count is 1–10 and each successful image can incur a charge. `size` must use `WIDTHxHEIGHT`; the GPT Image 2 public guide gives current custom-size limits. A task lookup does not create or bill another image.
|
|
23
|
+
|
|
24
|
+
An isolated Activepieces CE 0.92.2 instance accepted the build archive and ran **Get GPT Image task** against an existing completed task. The flow returned `succeeded` and one final image URL. The creation action has not been used to submit a new paid image in this validation.
|
|
25
|
+
|
|
26
|
+
## Troubleshoot
|
|
27
|
+
|
|
28
|
+
| Result | Action |
|
|
29
|
+
| --- | --- |
|
|
30
|
+
| 401 or 403 | Check the API key, GPT Image access, and Allowed APIs for both paths. |
|
|
31
|
+
| 400 | Check the prompt, model, `WIDTHxHEIGHT` size, and image count. |
|
|
32
|
+
| `pending` | Wait and query the same task ID. Do not rerun creation. |
|
|
33
|
+
| 429 | Reduce concurrency and wait; do not automatically retry paid creation. |
|
|
34
|
+
| Creation timeout or 5xx | Inspect request history before another submission. |
|
|
35
|
+
| `failed` or missing task | Check the task or trace ID and retention window. |
|
|
36
|
+
|
|
37
|
+
The Piece sends chosen inputs and the API key only to `https://api.acedata.cloud`. It returns final media URLs and removes internal routing fields from task results. [Source](https://github.com/AceDataCloud/GPTImageActivepieces) · [Support](https://github.com/AceDataCloud/GPTImageActivepieces/issues) · dev@acedata.cloud.
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# GPT Image for Activepieces
|
|
2
|
+
|
|
3
|
+
通过 Ace Data Cloud 创建 GPT Image 任务,并按**同一个任务 ID**查询结果。Piece 提供 **Create GPT Image task** 和 **Get GPT Image task** 两个动作。提交返回 ID 并不代表图片已完成;查询到 `succeeded` 后才可使用成品。[English](README.md)
|
|
4
|
+
|
|
5
|
+
**分发方式:**在 Activepieces 实例中将 `@acedatacloud/piece-gpt-image@0.1.0` 作为自定义 Piece 安装。公开 npm 包不会自动进入 Activepieces 共享目录。
|
|
6
|
+
|
|
7
|
+
## 安装与连接
|
|
8
|
+
|
|
9
|
+
Activepieces 0.92.2+ 中进入 **Platform Admin → Catalogue → Pieces → Install Piece → NPM Registry**,填写 `@acedatacloud/piece-gpt-image` 和版本 `0.1.0`。在流程中搜索 **GPT Image by Ace Data Cloud**,核对 Ace Data Cloud Logo 和两个动作。
|
|
10
|
+
|
|
11
|
+
创建连接时使用同时有 `/openai/images/generations` 与 `/openai/tasks` 权限的 Ace Data Cloud API Key。只填写 Key 本身,不加 `Bearer `;Key 留在连接中,不写入流程导出。生成前查看当前[价格](https://platform.acedata.cloud/models)与余额。
|
|
12
|
+
|
|
13
|
+
## 首次流程
|
|
14
|
+
|
|
15
|
+
1. 添加 **Create GPT Image task**,填写提示词,选择 `gpt-image-2`、`low`、`1024x1024` 和图片数量 `1`。
|
|
16
|
+
2. **只运行一次**创建动作,保存输出的 `task_id`。动作使用 `callback_url` 异步提交,不设置 HTTP 重试。若超时或返回 5xx,先查请求历史再考虑重提,任务可能已被接收。
|
|
17
|
+
3. 添加 **Get GPT Image task**,把 **Task ID** 映射为创建动作的输出。只查询同一个 ID,直至 `status=succeeded` 或 `failed`。pending 时不会把预览当作最终媒体。
|
|
18
|
+
4. 仅当 `success=true` 时打开 `media_urls`。若原始请求自带自定义 Trace ID 且丢失任务 ID,也可按 Trace ID 查询;本 Piece 的创建动作不设置自定义 Trace ID。
|
|
19
|
+
|
|
20
|
+
[无密钥首跑流程](https://raw.githubusercontent.com/AceDataCloud/GPTImageActivepieces/main/examples/gpt-image-quickstart.json) 导入后两个连接均为空,任务 ID 映射保留。运行前请配置自己的连接;创建动作已关闭自动重试。
|
|
21
|
+
|
|
22
|
+
图片数量为 1–10;每张成功图片可能单独计费。`size` 使用 `WIDTHxHEIGHT`,自定义尺寸限制以 GPT Image 2 公开指南为准。查询任务不会再次生成或计费。
|
|
23
|
+
|
|
24
|
+
隔离的 Activepieces CE 0.92.2 实例已接受构建归档,并通过 **Get GPT Image task** 查询一条既有完成任务;流程返回 `succeeded` 和 1 个最终图片 URL。本轮未通过创建动作提交新的付费图片任务。
|
|
25
|
+
|
|
26
|
+
## 排障
|
|
27
|
+
|
|
28
|
+
| 现象 | 处理 |
|
|
29
|
+
| --- | --- |
|
|
30
|
+
| 401 / 403 | 核对 Key、GPT Image 权限和两个路径的 Allowed APIs。 |
|
|
31
|
+
| 400 | 核对提示词、模型、`WIDTHxHEIGHT` 尺寸和图片数量。 |
|
|
32
|
+
| `pending` | 等待并查询同一任务 ID,不重跑创建。 |
|
|
33
|
+
| 429 | 降低并发,避免自动重试付费创建。 |
|
|
34
|
+
| 创建超时或 5xx | 先检查请求历史,再考虑再次提交。 |
|
|
35
|
+
| `failed` 或任务不存在 | 检查任务或 Trace ID 及保留期限。 |
|
|
36
|
+
|
|
37
|
+
Piece 只向 `https://api.acedata.cloud` 发送所选输入和 API Key,仅在终态返回媒体 URL,并过滤内部路由字段。[源码](https://github.com/AceDataCloud/GPTImageActivepieces) · [支持](https://github.com/AceDataCloud/GPTImageActivepieces/issues) · dev@acedata.cloud。
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const gptImage: import("@activepieces/pieces-framework").Piece<import("@activepieces/pieces-framework").SecretTextProperty<true>>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.gptImage = void 0;
|
|
4
|
+
const pieces_framework_1 = require("@activepieces/pieces-framework");
|
|
5
|
+
const generate_image_1 = require("./lib/actions/generate-image");
|
|
6
|
+
const get_task_1 = require("./lib/actions/get-task");
|
|
7
|
+
const auth_1 = require("./lib/auth");
|
|
8
|
+
exports.gptImage = (0, pieces_framework_1.createPiece)({
|
|
9
|
+
displayName: 'GPT Image by Ace Data Cloud',
|
|
10
|
+
description: 'Create GPT Image tasks and retrieve the same task through Ace Data Cloud.',
|
|
11
|
+
logoUrl: 'https://cdn.acedata.cloud/assets/logo.png',
|
|
12
|
+
authors: ['Ace Data Cloud'],
|
|
13
|
+
minimumSupportedRelease: '0.92.2',
|
|
14
|
+
auth: auth_1.gptImageAuth,
|
|
15
|
+
actions: [generate_image_1.generateImage, get_task_1.getTask],
|
|
16
|
+
triggers: [],
|
|
17
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const generateImage: import("@activepieces/pieces-framework").IAction<import("@activepieces/pieces-framework").SecretTextProperty<true>, {
|
|
2
|
+
prompt: import("@activepieces/pieces-framework").LongTextProperty<true>;
|
|
3
|
+
model: import("@activepieces/pieces-framework").StaticDropdownProperty<string, true>;
|
|
4
|
+
quality: import("@activepieces/pieces-framework").StaticDropdownProperty<string, true>;
|
|
5
|
+
size: import("@activepieces/pieces-framework").ShortTextProperty<true>;
|
|
6
|
+
count: import("@activepieces/pieces-framework").NumberProperty<true>;
|
|
7
|
+
}>;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateImage = void 0;
|
|
4
|
+
const pieces_framework_1 = require("@activepieces/pieces-framework");
|
|
5
|
+
const auth_1 = require("../auth");
|
|
6
|
+
const client_1 = require("../common/client");
|
|
7
|
+
exports.generateImage = (0, pieces_framework_1.createAction)({
|
|
8
|
+
name: 'generate_image',
|
|
9
|
+
displayName: 'Create GPT Image task',
|
|
10
|
+
description: 'Submit one image generation and return its task ID. This action does not wait for the final image.',
|
|
11
|
+
auth: auth_1.gptImageAuth,
|
|
12
|
+
props: {
|
|
13
|
+
prompt: pieces_framework_1.Property.LongText({
|
|
14
|
+
displayName: 'Image description',
|
|
15
|
+
description: 'Describe the image. Use Get GPT Image task with the returned task ID to check the result.',
|
|
16
|
+
required: true,
|
|
17
|
+
}),
|
|
18
|
+
model: pieces_framework_1.Property.StaticDropdown({
|
|
19
|
+
displayName: 'Model',
|
|
20
|
+
required: true,
|
|
21
|
+
defaultValue: 'gpt-image-2',
|
|
22
|
+
options: { options: [{ label: 'GPT Image 2', value: 'gpt-image-2' }] },
|
|
23
|
+
}),
|
|
24
|
+
quality: pieces_framework_1.Property.StaticDropdown({
|
|
25
|
+
displayName: 'Quality',
|
|
26
|
+
required: true,
|
|
27
|
+
defaultValue: 'low',
|
|
28
|
+
options: {
|
|
29
|
+
options: [
|
|
30
|
+
{ label: 'Low', value: 'low' },
|
|
31
|
+
{ label: 'Medium', value: 'medium' },
|
|
32
|
+
{ label: 'High', value: 'high' },
|
|
33
|
+
{ label: 'Auto', value: 'auto' },
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
}),
|
|
37
|
+
size: pieces_framework_1.Property.ShortText({
|
|
38
|
+
displayName: 'Size',
|
|
39
|
+
description: 'WIDTHxHEIGHT, such as 1024x1024. Read current model limits before using a custom size.',
|
|
40
|
+
required: true,
|
|
41
|
+
defaultValue: '1024x1024',
|
|
42
|
+
}),
|
|
43
|
+
count: pieces_framework_1.Property.Number({
|
|
44
|
+
displayName: 'Image count',
|
|
45
|
+
description: 'Each generated image is billed. Choose a whole number from 1 to 10.',
|
|
46
|
+
required: true,
|
|
47
|
+
defaultValue: 1,
|
|
48
|
+
}),
|
|
49
|
+
},
|
|
50
|
+
async run(context) {
|
|
51
|
+
const { prompt, model, quality, size, count } = context.propsValue;
|
|
52
|
+
if (!prompt.trim()) {
|
|
53
|
+
throw new Error('An image description is required.');
|
|
54
|
+
}
|
|
55
|
+
if (!/^\d+x\d+$/.test(size)) {
|
|
56
|
+
throw new Error('Size must use WIDTHxHEIGHT, such as 1024x1024.');
|
|
57
|
+
}
|
|
58
|
+
if (!Number.isInteger(count) || count < 1 || count > 10) {
|
|
59
|
+
throw new Error('Image count must be a whole number from 1 to 10.');
|
|
60
|
+
}
|
|
61
|
+
const response = await client_1.gptImageClient.post({
|
|
62
|
+
token: context.auth.secret_text,
|
|
63
|
+
path: '/openai/images/generations',
|
|
64
|
+
body: {
|
|
65
|
+
model,
|
|
66
|
+
prompt,
|
|
67
|
+
quality,
|
|
68
|
+
size,
|
|
69
|
+
n: count,
|
|
70
|
+
callback_url: 'https://api.acedata.cloud/health',
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
const taskId = client_1.gptImageClient.taskIdFromSubmission(response);
|
|
74
|
+
if (!taskId) {
|
|
75
|
+
throw new Error('No task ID was returned. Inspect request history before submitting again.');
|
|
76
|
+
}
|
|
77
|
+
return { task_id: taskId, status: 'pending', success: false };
|
|
78
|
+
},
|
|
79
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const getTask: import("@activepieces/pieces-framework").IAction<import("@activepieces/pieces-framework").SecretTextProperty<true>, {
|
|
2
|
+
taskId: import("@activepieces/pieces-framework").ShortTextProperty<false>;
|
|
3
|
+
traceId: import("@activepieces/pieces-framework").ShortTextProperty<false>;
|
|
4
|
+
}>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTask = void 0;
|
|
4
|
+
const pieces_framework_1 = require("@activepieces/pieces-framework");
|
|
5
|
+
const auth_1 = require("../auth");
|
|
6
|
+
const client_1 = require("../common/client");
|
|
7
|
+
exports.getTask = (0, pieces_framework_1.createAction)({
|
|
8
|
+
name: 'get_task',
|
|
9
|
+
displayName: 'Get GPT Image task',
|
|
10
|
+
description: 'Query one existing task ID, or a trace ID supplied on the original request. Pending data is not final media.',
|
|
11
|
+
auth: auth_1.gptImageAuth,
|
|
12
|
+
props: {
|
|
13
|
+
taskId: pieces_framework_1.Property.ShortText({
|
|
14
|
+
displayName: 'Task ID',
|
|
15
|
+
description: 'Map task_id from Create GPT Image task. Query this same ID until completion.',
|
|
16
|
+
required: false,
|
|
17
|
+
}),
|
|
18
|
+
traceId: pieces_framework_1.Property.ShortText({
|
|
19
|
+
displayName: 'Trace ID',
|
|
20
|
+
description: 'Optional recovery lookup when a prior request supplied a custom trace ID but timed out before returning task_id.',
|
|
21
|
+
required: false,
|
|
22
|
+
}),
|
|
23
|
+
},
|
|
24
|
+
async run(context) {
|
|
25
|
+
const taskId = context.propsValue.taskId?.trim() ?? '';
|
|
26
|
+
const traceId = context.propsValue.traceId?.trim() ?? '';
|
|
27
|
+
if (!taskId && !traceId) {
|
|
28
|
+
throw new Error('A task ID or trace ID is required.');
|
|
29
|
+
}
|
|
30
|
+
const response = await client_1.gptImageClient.post({
|
|
31
|
+
token: context.auth.secret_text,
|
|
32
|
+
path: '/openai/tasks',
|
|
33
|
+
body: { action: 'retrieve', ...(taskId ? { id: taskId } : { trace_id: traceId }) },
|
|
34
|
+
});
|
|
35
|
+
return client_1.gptImageClient.normalizeTask({ body: response, taskId, traceId });
|
|
36
|
+
},
|
|
37
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const gptImageAuth: import("@activepieces/pieces-framework").SecretTextProperty<true>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.gptImageAuth = void 0;
|
|
4
|
+
const pieces_framework_1 = require("@activepieces/pieces-framework");
|
|
5
|
+
exports.gptImageAuth = pieces_framework_1.PieceAuth.SecretText({
|
|
6
|
+
displayName: 'Ace Data Cloud API key',
|
|
7
|
+
description: 'Use an API key with GPT Image access from https://platform.acedata.cloud/console/applications. Enter only the key, without Bearer.',
|
|
8
|
+
required: true,
|
|
9
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
declare function post({ token, path, body, }: {
|
|
2
|
+
token: string;
|
|
3
|
+
path: '/openai/images/generations' | '/openai/tasks';
|
|
4
|
+
body: Record<string, unknown>;
|
|
5
|
+
}): Promise<unknown>;
|
|
6
|
+
declare function normalizeTask({ body, taskId, traceId }: {
|
|
7
|
+
body: unknown;
|
|
8
|
+
taskId: string;
|
|
9
|
+
traceId?: string;
|
|
10
|
+
}): TaskResult;
|
|
11
|
+
declare function taskIdFromSubmission(body: unknown): string;
|
|
12
|
+
export declare const gptImageClient: {
|
|
13
|
+
post: typeof post;
|
|
14
|
+
normalizeTask: typeof normalizeTask;
|
|
15
|
+
taskIdFromSubmission: typeof taskIdFromSubmission;
|
|
16
|
+
};
|
|
17
|
+
export type TaskResult = {
|
|
18
|
+
task_id: string;
|
|
19
|
+
status: 'pending' | 'succeeded' | 'failed';
|
|
20
|
+
success: boolean;
|
|
21
|
+
trace_id: string;
|
|
22
|
+
media_urls: string[];
|
|
23
|
+
result: unknown;
|
|
24
|
+
};
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.gptImageClient = void 0;
|
|
4
|
+
const pieces_common_1 = require("@activepieces/pieces-common");
|
|
5
|
+
async function post({ token, path, body, }) {
|
|
6
|
+
const response = await pieces_common_1.httpClient.sendRequest({
|
|
7
|
+
method: pieces_common_1.HttpMethod.POST,
|
|
8
|
+
url: `https://api.acedata.cloud${path}`,
|
|
9
|
+
headers: {
|
|
10
|
+
Authorization: `Bearer ${token}`,
|
|
11
|
+
'Content-Type': 'application/json',
|
|
12
|
+
},
|
|
13
|
+
body,
|
|
14
|
+
retries: 0,
|
|
15
|
+
});
|
|
16
|
+
return response.body;
|
|
17
|
+
}
|
|
18
|
+
function normalizeTask({ body, taskId, traceId }) {
|
|
19
|
+
const envelope = record(body);
|
|
20
|
+
if (Object.keys(envelope).length === 0) {
|
|
21
|
+
throw new Error('Task not found. Check the task or trace ID and its retention window.');
|
|
22
|
+
}
|
|
23
|
+
const payload = parsePayload('response' in envelope ? envelope.response : body);
|
|
24
|
+
const values = record(payload);
|
|
25
|
+
const states = collectStates(payload);
|
|
26
|
+
const failed = states.some((state) => FAILED_STATES.has(state)) || hasFailure(payload) || Boolean(envelope.error);
|
|
27
|
+
const unfinished = 'finished_at' in envelope && envelope.finished_at === null;
|
|
28
|
+
const finished = !unfinished &&
|
|
29
|
+
!failed &&
|
|
30
|
+
((states.length > 0 && states.every((state) => COMPLETE_STATES.has(state))) ||
|
|
31
|
+
Boolean(envelope.finished_at && hasContent(payload)));
|
|
32
|
+
const foundTraceId = stringValue(values.trace_id) || stringValue(envelope.trace_id) || traceId || '';
|
|
33
|
+
return {
|
|
34
|
+
task_id: stringValue(envelope.id) || taskId,
|
|
35
|
+
status: failed ? 'failed' : finished ? 'succeeded' : 'pending',
|
|
36
|
+
success: finished,
|
|
37
|
+
trace_id: foundTraceId,
|
|
38
|
+
media_urls: finished ? collectMediaUrls(payload) : [],
|
|
39
|
+
result: sanitize(payload),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function taskIdFromSubmission(body) {
|
|
43
|
+
const value = record(body);
|
|
44
|
+
const nestedTask = record(value.task);
|
|
45
|
+
return stringValue(value.task_id) || stringValue(nestedTask.id);
|
|
46
|
+
}
|
|
47
|
+
function parsePayload(value) {
|
|
48
|
+
if (typeof value !== 'string') {
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return JSON.parse(value);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return {};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function record(value) {
|
|
59
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
60
|
+
? Object.fromEntries(Object.entries(value))
|
|
61
|
+
: {};
|
|
62
|
+
}
|
|
63
|
+
function stringValue(value) {
|
|
64
|
+
return typeof value === 'string' ? value : '';
|
|
65
|
+
}
|
|
66
|
+
function collectStates(value) {
|
|
67
|
+
if (Array.isArray(value)) {
|
|
68
|
+
return value.flatMap(collectStates);
|
|
69
|
+
}
|
|
70
|
+
if (value === null || typeof value !== 'object') {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
const item = record(value);
|
|
74
|
+
return [
|
|
75
|
+
...['state', 'status'].flatMap((key) => {
|
|
76
|
+
const state = stringValue(item[key]);
|
|
77
|
+
return state ? [state.toLowerCase()] : [];
|
|
78
|
+
}),
|
|
79
|
+
...['data', 'content', 'task'].flatMap((key) => collectStates(item[key])),
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
function hasFailure(value) {
|
|
83
|
+
if (Array.isArray(value)) {
|
|
84
|
+
return value.some(hasFailure);
|
|
85
|
+
}
|
|
86
|
+
if (value === null || typeof value !== 'object') {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
const item = record(value);
|
|
90
|
+
return Boolean(item.error) ||
|
|
91
|
+
(item.success === false && !['pending', 'running', 'processing'].includes(stringValue(item.status || item.state).toLowerCase())) ||
|
|
92
|
+
['data', 'content', 'task'].some((key) => hasFailure(item[key]));
|
|
93
|
+
}
|
|
94
|
+
function collectMediaUrls(value) {
|
|
95
|
+
if (Array.isArray(value)) {
|
|
96
|
+
return [...new Set(value.flatMap(collectMediaUrls))];
|
|
97
|
+
}
|
|
98
|
+
if (value === null || typeof value !== 'object') {
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
const item = record(value);
|
|
102
|
+
const urls = Object.entries(item).flatMap(([key, child]) => {
|
|
103
|
+
if (['url', 'image_url'].includes(key) && typeof child === 'string') {
|
|
104
|
+
return child.startsWith('https://') ? [child] : [];
|
|
105
|
+
}
|
|
106
|
+
return collectMediaUrls(child);
|
|
107
|
+
});
|
|
108
|
+
return [...new Set(urls)];
|
|
109
|
+
}
|
|
110
|
+
function hasContent(value) {
|
|
111
|
+
return Array.isArray(value) ? value.length > 0 : Object.keys(record(value)).length > 0;
|
|
112
|
+
}
|
|
113
|
+
function sanitize(value) {
|
|
114
|
+
if (Array.isArray(value)) {
|
|
115
|
+
return value.map(sanitize);
|
|
116
|
+
}
|
|
117
|
+
if (value === null || typeof value !== 'object') {
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
return Object.fromEntries(Object.entries(value)
|
|
121
|
+
.filter(([key]) => !SENSITIVE_KEYS.has(key.toLowerCase()) && !SENSITIVE_KEY_PATTERN.test(key))
|
|
122
|
+
.map(([key, child]) => [key, key === 'error' ? 'Task failed; inspect its task or trace ID.' : sanitize(child)]));
|
|
123
|
+
}
|
|
124
|
+
exports.gptImageClient = { post, normalizeTask, taskIdFromSubmission };
|
|
125
|
+
const COMPLETE_STATES = new Set(['complete', 'completed', 'succeeded', 'succeed', 'success', 'finished']);
|
|
126
|
+
const FAILED_STATES = new Set(['failed', 'error', 'cancelled', 'canceled', 'rejected']);
|
|
127
|
+
const SENSITIVE_KEYS = new Set([
|
|
128
|
+
'request', 'request_body', 'user_id', 'actor_user_id', 'credential_id', 'authorization_id',
|
|
129
|
+
'application_id', 'api_key', 'access_token', 'refresh_token', 'authorization', 'headers',
|
|
130
|
+
'supplier', 'supplier_id', 'provider_route', 'upstream', 'upstream_model', 'actual_model', 'secret',
|
|
131
|
+
]);
|
|
132
|
+
const SENSITIVE_KEY_PATTERN = /(^internal(?:_|$)|supplier|upstream|(?:^|_)secret(?:_|$))/i;
|
package/package.json
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acedatacloud/piece-gpt-image",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "GPT Image generation and task lookup through Ace Data Cloud for Activepieces",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./dist/src/index.js",
|
|
7
|
+
"types": "./dist/src/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc -p tsconfig.json",
|
|
13
|
+
"test": "npm run build && node --test test/*.test.cjs",
|
|
14
|
+
"pack:check": "npm pack --dry-run"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/AceDataCloud/GPTImageActivepieces.git"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/AceDataCloud/GPTImageActivepieces#readme",
|
|
21
|
+
"bugs": "https://github.com/AceDataCloud/GPTImageActivepieces/issues",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@activepieces/pieces-common": "0.12.5",
|
|
25
|
+
"@activepieces/pieces-framework": "0.32.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"typescript": "5.9.3"
|
|
29
|
+
}
|
|
30
|
+
}
|