anyfast 0.0.1
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 +189 -0
- package/README.md +281 -0
- package/dist/asset.d.ts +14 -0
- package/dist/asset.js +34 -0
- package/dist/chat.d.ts +35 -0
- package/dist/chat.js +63 -0
- package/dist/client.d.ts +26 -0
- package/dist/client.js +186 -0
- package/dist/file.d.ts +8 -0
- package/dist/file.js +65 -0
- package/dist/image.d.ts +11 -0
- package/dist/image.js +28 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +35 -0
- package/dist/sse-parser.d.ts +2 -0
- package/dist/sse-parser.js +66 -0
- package/dist/task-utils.d.ts +24 -0
- package/dist/task-utils.js +59 -0
- package/dist/types/asset.d.ts +62 -0
- package/dist/types/asset.js +3 -0
- package/dist/types/chat.d.ts +192 -0
- package/dist/types/chat.js +3 -0
- package/dist/types/common.d.ts +40 -0
- package/dist/types/common.js +13 -0
- package/dist/types/file.d.ts +22 -0
- package/dist/types/file.js +2 -0
- package/dist/types/image.d.ts +20 -0
- package/dist/types/image.js +2 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.js +22 -0
- package/dist/types/video.d.ts +95 -0
- package/dist/types/video.js +3 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.js +18 -0
- package/dist/upload/s3-uploader.d.ts +10 -0
- package/dist/upload/s3-uploader.js +123 -0
- package/dist/video.d.ts +17 -0
- package/dist/video.js +64 -0
- package/package.json +36 -0
package/dist/client.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.AnyFast = void 0;
|
|
40
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
41
|
+
const fs = __importStar(require("fs"));
|
|
42
|
+
const path = __importStar(require("path"));
|
|
43
|
+
const types_1 = require("./types");
|
|
44
|
+
const chat_1 = require("./chat");
|
|
45
|
+
const image_1 = require("./image");
|
|
46
|
+
const video_1 = require("./video");
|
|
47
|
+
const asset_1 = require("./asset");
|
|
48
|
+
const file_1 = require("./file");
|
|
49
|
+
const MIME_TYPES = {
|
|
50
|
+
'.jpg': 'image/jpeg',
|
|
51
|
+
'.jpeg': 'image/jpeg',
|
|
52
|
+
'.png': 'image/png',
|
|
53
|
+
'.gif': 'image/gif',
|
|
54
|
+
'.webp': 'image/webp',
|
|
55
|
+
'.bmp': 'image/bmp',
|
|
56
|
+
'.svg': 'image/svg+xml',
|
|
57
|
+
'.mp4': 'video/mp4',
|
|
58
|
+
'.webm': 'video/webm',
|
|
59
|
+
'.mov': 'video/quicktime',
|
|
60
|
+
'.avi': 'video/x-msvideo',
|
|
61
|
+
'.glb': 'model/gltf-binary',
|
|
62
|
+
'.gltf': 'model/gltf+json',
|
|
63
|
+
'.obj': 'model/obj',
|
|
64
|
+
'.fbx': 'application/octet-stream',
|
|
65
|
+
};
|
|
66
|
+
class AnyFast {
|
|
67
|
+
constructor(options = {}) {
|
|
68
|
+
this.apiKey = options.apiKey || process.env.ANYFAST_API_KEY || '';
|
|
69
|
+
if (!this.apiKey) {
|
|
70
|
+
throw new types_1.AnyFastError('API key is required. Pass it via options.apiKey or set ANYFAST_API_KEY environment variable.');
|
|
71
|
+
}
|
|
72
|
+
this.baseURL = options.baseURL || 'https://www.anyfast.ai';
|
|
73
|
+
this.gatewayURL = options.gatewayURL || 'https://www.anyfast.ai';
|
|
74
|
+
this.timeout = options.timeout || 30000;
|
|
75
|
+
this.chat = new chat_1.Chat(this);
|
|
76
|
+
this.image = new image_1.Image(this);
|
|
77
|
+
this.video = new video_1.Video(this);
|
|
78
|
+
this.asset = new asset_1.Asset(this);
|
|
79
|
+
this.file = new file_1.File(this);
|
|
80
|
+
}
|
|
81
|
+
async makeRequest(endpoint, options = {}) {
|
|
82
|
+
const url = `${this.baseURL}${endpoint}`;
|
|
83
|
+
const headers = {
|
|
84
|
+
'Content-Type': 'application/json',
|
|
85
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
86
|
+
...options.headers,
|
|
87
|
+
};
|
|
88
|
+
const controller = new AbortController();
|
|
89
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
90
|
+
try {
|
|
91
|
+
const response = await (0, node_fetch_1.default)(url, {
|
|
92
|
+
method: options.method || 'GET',
|
|
93
|
+
headers,
|
|
94
|
+
body: options.body ? JSON.stringify(options.body) : undefined,
|
|
95
|
+
signal: controller.signal,
|
|
96
|
+
});
|
|
97
|
+
const data = await response.json();
|
|
98
|
+
if (!response.ok) {
|
|
99
|
+
throw new types_1.AnyFastError(data.message || `Request failed with status ${response.status}`, response.status, data.code, data);
|
|
100
|
+
}
|
|
101
|
+
return data;
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
clearTimeout(timeoutId);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async makeStreamingRequest(endpoint, options = {}) {
|
|
108
|
+
const url = `${this.baseURL}${endpoint}`;
|
|
109
|
+
const headers = {
|
|
110
|
+
'Content-Type': 'application/json',
|
|
111
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
112
|
+
Accept: 'text/event-stream',
|
|
113
|
+
...options.headers,
|
|
114
|
+
};
|
|
115
|
+
const controller = new AbortController();
|
|
116
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
117
|
+
try {
|
|
118
|
+
const response = await (0, node_fetch_1.default)(url, {
|
|
119
|
+
method: options.method || 'POST',
|
|
120
|
+
headers,
|
|
121
|
+
body: options.body ? JSON.stringify(options.body) : undefined,
|
|
122
|
+
signal: controller.signal,
|
|
123
|
+
});
|
|
124
|
+
if (!response.ok) {
|
|
125
|
+
clearTimeout(timeoutId);
|
|
126
|
+
const data = await response.json();
|
|
127
|
+
throw new types_1.AnyFastError(data.message || `Request failed with status ${response.status}`, response.status, data.code, data);
|
|
128
|
+
}
|
|
129
|
+
// Clear timeout for streaming - the stream manages its own lifecycle
|
|
130
|
+
clearTimeout(timeoutId);
|
|
131
|
+
return response;
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
clearTimeout(timeoutId);
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
async makeGatewayRequest(endpoint, options = {}) {
|
|
139
|
+
const url = `${this.gatewayURL}${endpoint}`;
|
|
140
|
+
const headers = {
|
|
141
|
+
'Content-Type': 'application/json',
|
|
142
|
+
'API-KEY': this.apiKey,
|
|
143
|
+
...options.headers,
|
|
144
|
+
};
|
|
145
|
+
const controller = new AbortController();
|
|
146
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
147
|
+
try {
|
|
148
|
+
const response = await (0, node_fetch_1.default)(url, {
|
|
149
|
+
method: options.method || 'GET',
|
|
150
|
+
headers,
|
|
151
|
+
body: options.body ? JSON.stringify(options.body) : undefined,
|
|
152
|
+
signal: controller.signal,
|
|
153
|
+
});
|
|
154
|
+
const data = await response.json();
|
|
155
|
+
if (!response.ok) {
|
|
156
|
+
throw new types_1.AnyFastError(data.message || `Gateway request failed with status ${response.status}`, response.status, data.code, data);
|
|
157
|
+
}
|
|
158
|
+
return data;
|
|
159
|
+
}
|
|
160
|
+
finally {
|
|
161
|
+
clearTimeout(timeoutId);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
async post(endpoint, body) {
|
|
165
|
+
return this.makeRequest(endpoint, { method: 'POST', body });
|
|
166
|
+
}
|
|
167
|
+
async get(endpoint) {
|
|
168
|
+
return this.makeRequest(endpoint, { method: 'GET' });
|
|
169
|
+
}
|
|
170
|
+
loadFromLocal(filePath) {
|
|
171
|
+
const absolutePath = path.resolve(filePath);
|
|
172
|
+
if (!fs.existsSync(absolutePath)) {
|
|
173
|
+
throw new types_1.AnyFastError(`File not found: ${absolutePath}`);
|
|
174
|
+
}
|
|
175
|
+
const ext = path.extname(absolutePath).toLowerCase();
|
|
176
|
+
const mimeType = MIME_TYPES[ext] || 'application/octet-stream';
|
|
177
|
+
const buffer = fs.readFileSync(absolutePath);
|
|
178
|
+
const base64 = buffer.toString('base64');
|
|
179
|
+
return `data:${mimeType};base64,${base64}`;
|
|
180
|
+
}
|
|
181
|
+
static getMimeType(filePath) {
|
|
182
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
183
|
+
return MIME_TYPES[ext] || 'application/octet-stream';
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
exports.AnyFast = AnyFast;
|
package/dist/file.d.ts
ADDED
package/dist/file.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.File = void 0;
|
|
37
|
+
const fs = __importStar(require("fs"));
|
|
38
|
+
const path = __importStar(require("path"));
|
|
39
|
+
const client_1 = require("./client");
|
|
40
|
+
const common_1 = require("./types/common");
|
|
41
|
+
const s3_uploader_1 = require("./upload/s3-uploader");
|
|
42
|
+
class File {
|
|
43
|
+
constructor(client) {
|
|
44
|
+
this.client = client;
|
|
45
|
+
}
|
|
46
|
+
async getSTSToken() {
|
|
47
|
+
return this.client.makeGatewayRequest('/api/model-verse/sts-token', {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
async upload(filePath) {
|
|
52
|
+
const absolutePath = path.resolve(filePath);
|
|
53
|
+
if (!fs.existsSync(absolutePath)) {
|
|
54
|
+
throw new common_1.AnyFastError(`File not found: ${absolutePath}`);
|
|
55
|
+
}
|
|
56
|
+
const stsResponse = await this.getSTSToken();
|
|
57
|
+
const { credentials, bucket, region, endpoint, prefix } = stsResponse.data;
|
|
58
|
+
const fileName = path.basename(absolutePath);
|
|
59
|
+
const key = `${prefix}${fileName}`;
|
|
60
|
+
const contentType = client_1.AnyFast.getMimeType(absolutePath);
|
|
61
|
+
const uploader = new s3_uploader_1.S3Uploader(credentials, bucket, region, endpoint);
|
|
62
|
+
return uploader.uploadFile(absolutePath, key, contentType);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.File = File;
|
package/dist/image.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AnyFast } from './client';
|
|
2
|
+
import { ImageGenerationOptions, ImageGenerationResponse, BlockingRunOptions } from './types';
|
|
3
|
+
import { GenerationResponseImpl, TaskStatusImpl } from './task-utils';
|
|
4
|
+
export declare class Image {
|
|
5
|
+
private client;
|
|
6
|
+
constructor(client: AnyFast);
|
|
7
|
+
generate(options: ImageGenerationOptions): Promise<ImageGenerationResponse>;
|
|
8
|
+
generateAsync(options: ImageGenerationOptions): Promise<GenerationResponseImpl>;
|
|
9
|
+
queryTask(taskId: string): Promise<TaskStatusImpl>;
|
|
10
|
+
run(options: ImageGenerationOptions, runOptions?: BlockingRunOptions): Promise<TaskStatusImpl>;
|
|
11
|
+
}
|
package/dist/image.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Image = void 0;
|
|
4
|
+
const task_utils_1 = require("./task-utils");
|
|
5
|
+
class Image {
|
|
6
|
+
constructor(client) {
|
|
7
|
+
this.client = client;
|
|
8
|
+
}
|
|
9
|
+
async generate(options) {
|
|
10
|
+
return this.client.post('/v1/images/generations', options);
|
|
11
|
+
}
|
|
12
|
+
async generateAsync(options) {
|
|
13
|
+
const data = await this.client.post('/v1/images/generations', options);
|
|
14
|
+
return new task_utils_1.GenerationResponseImpl(data);
|
|
15
|
+
}
|
|
16
|
+
async queryTask(taskId) {
|
|
17
|
+
const data = await this.client.get(`/${taskId}`);
|
|
18
|
+
return new task_utils_1.TaskStatusImpl(data);
|
|
19
|
+
}
|
|
20
|
+
async run(options, runOptions = {}) {
|
|
21
|
+
const response = await this.generateAsync(options);
|
|
22
|
+
if (!response.taskId) {
|
|
23
|
+
throw new Error('No task ID returned from generation request');
|
|
24
|
+
}
|
|
25
|
+
return (0, task_utils_1.waitForTaskCompletion)((taskId) => this.queryTask(taskId), response.taskId, runOptions);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.Image = Image;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { AnyFast } from './client';
|
|
2
|
+
export { Chat } from './chat';
|
|
3
|
+
export { Image } from './image';
|
|
4
|
+
export { Video } from './video';
|
|
5
|
+
export { Asset } from './asset';
|
|
6
|
+
export { File } from './file';
|
|
7
|
+
export { GenerationResponseImpl, TaskStatusImpl } from './task-utils';
|
|
8
|
+
export { parseSSE } from './sse-parser';
|
|
9
|
+
export * from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.parseSSE = exports.TaskStatusImpl = exports.GenerationResponseImpl = exports.File = exports.Asset = exports.Video = exports.Image = exports.Chat = exports.AnyFast = void 0;
|
|
18
|
+
var client_1 = require("./client");
|
|
19
|
+
Object.defineProperty(exports, "AnyFast", { enumerable: true, get: function () { return client_1.AnyFast; } });
|
|
20
|
+
var chat_1 = require("./chat");
|
|
21
|
+
Object.defineProperty(exports, "Chat", { enumerable: true, get: function () { return chat_1.Chat; } });
|
|
22
|
+
var image_1 = require("./image");
|
|
23
|
+
Object.defineProperty(exports, "Image", { enumerable: true, get: function () { return image_1.Image; } });
|
|
24
|
+
var video_1 = require("./video");
|
|
25
|
+
Object.defineProperty(exports, "Video", { enumerable: true, get: function () { return video_1.Video; } });
|
|
26
|
+
var asset_1 = require("./asset");
|
|
27
|
+
Object.defineProperty(exports, "Asset", { enumerable: true, get: function () { return asset_1.Asset; } });
|
|
28
|
+
var file_1 = require("./file");
|
|
29
|
+
Object.defineProperty(exports, "File", { enumerable: true, get: function () { return file_1.File; } });
|
|
30
|
+
var task_utils_1 = require("./task-utils");
|
|
31
|
+
Object.defineProperty(exports, "GenerationResponseImpl", { enumerable: true, get: function () { return task_utils_1.GenerationResponseImpl; } });
|
|
32
|
+
Object.defineProperty(exports, "TaskStatusImpl", { enumerable: true, get: function () { return task_utils_1.TaskStatusImpl; } });
|
|
33
|
+
var sse_parser_1 = require("./sse-parser");
|
|
34
|
+
Object.defineProperty(exports, "parseSSE", { enumerable: true, get: function () { return sse_parser_1.parseSSE; } });
|
|
35
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseSSE = parseSSE;
|
|
4
|
+
async function* parseSSE(stream) {
|
|
5
|
+
let buffer = '';
|
|
6
|
+
for await (const chunk of stream) {
|
|
7
|
+
buffer += typeof chunk === 'string' ? chunk : chunk.toString('utf-8');
|
|
8
|
+
const parts = buffer.split('\n\n');
|
|
9
|
+
buffer = parts.pop() || '';
|
|
10
|
+
for (const part of parts) {
|
|
11
|
+
const trimmed = part.trim();
|
|
12
|
+
if (!trimmed)
|
|
13
|
+
continue;
|
|
14
|
+
const event = { data: '' };
|
|
15
|
+
const lines = trimmed.split('\n');
|
|
16
|
+
for (const line of lines) {
|
|
17
|
+
if (line.startsWith('data: ')) {
|
|
18
|
+
const value = line.slice(6);
|
|
19
|
+
if (value === '[DONE]')
|
|
20
|
+
return;
|
|
21
|
+
event.data = event.data ? event.data + '\n' + value : value;
|
|
22
|
+
}
|
|
23
|
+
else if (line.startsWith('event: ')) {
|
|
24
|
+
event.event = line.slice(7);
|
|
25
|
+
}
|
|
26
|
+
else if (line.startsWith('id: ')) {
|
|
27
|
+
event.id = line.slice(4);
|
|
28
|
+
}
|
|
29
|
+
else if (line.startsWith('retry: ')) {
|
|
30
|
+
event.retry = parseInt(line.slice(7), 10);
|
|
31
|
+
}
|
|
32
|
+
else if (line === 'data:') {
|
|
33
|
+
event.data = event.data ? event.data + '\n' : '';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (event.data !== undefined) {
|
|
37
|
+
yield event;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Handle remaining buffer
|
|
42
|
+
if (buffer.trim()) {
|
|
43
|
+
const event = { data: '' };
|
|
44
|
+
const lines = buffer.trim().split('\n');
|
|
45
|
+
for (const line of lines) {
|
|
46
|
+
if (line.startsWith('data: ')) {
|
|
47
|
+
const value = line.slice(6);
|
|
48
|
+
if (value === '[DONE]')
|
|
49
|
+
return;
|
|
50
|
+
event.data = event.data ? event.data + '\n' + value : value;
|
|
51
|
+
}
|
|
52
|
+
else if (line.startsWith('event: ')) {
|
|
53
|
+
event.event = line.slice(7);
|
|
54
|
+
}
|
|
55
|
+
else if (line.startsWith('id: ')) {
|
|
56
|
+
event.id = line.slice(4);
|
|
57
|
+
}
|
|
58
|
+
else if (line.startsWith('retry: ')) {
|
|
59
|
+
event.retry = parseInt(line.slice(7), 10);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (event.data) {
|
|
63
|
+
yield event;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { GenerationResponse, TaskStatus, BlockingRunOptions } from './types/common';
|
|
2
|
+
export declare class GenerationResponseImpl implements GenerationResponse {
|
|
3
|
+
taskId: string | null;
|
|
4
|
+
data: any;
|
|
5
|
+
constructor(data: any);
|
|
6
|
+
}
|
|
7
|
+
export declare class TaskStatusImpl implements TaskStatus {
|
|
8
|
+
id: string;
|
|
9
|
+
action: string;
|
|
10
|
+
status: string;
|
|
11
|
+
completion: string;
|
|
12
|
+
created_at: string;
|
|
13
|
+
updated_at: string;
|
|
14
|
+
progress: string;
|
|
15
|
+
fail_reason: string;
|
|
16
|
+
result: any;
|
|
17
|
+
constructor(data: any);
|
|
18
|
+
isCompleted(): boolean;
|
|
19
|
+
isFailed(): boolean;
|
|
20
|
+
isProcessing(): boolean;
|
|
21
|
+
get progressPercent(): number;
|
|
22
|
+
}
|
|
23
|
+
export declare function delay(ms: number): Promise<void>;
|
|
24
|
+
export declare function waitForTaskCompletion(queryFn: (taskId: string) => Promise<TaskStatusImpl>, taskId: string, options?: BlockingRunOptions): Promise<TaskStatusImpl>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TaskStatusImpl = exports.GenerationResponseImpl = void 0;
|
|
4
|
+
exports.delay = delay;
|
|
5
|
+
exports.waitForTaskCompletion = waitForTaskCompletion;
|
|
6
|
+
class GenerationResponseImpl {
|
|
7
|
+
constructor(data) {
|
|
8
|
+
this.data = data;
|
|
9
|
+
this.taskId = data?.task_id || data?.taskId || null;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.GenerationResponseImpl = GenerationResponseImpl;
|
|
13
|
+
class TaskStatusImpl {
|
|
14
|
+
constructor(data) {
|
|
15
|
+
this.id = data.id || '';
|
|
16
|
+
this.action = data.action || '';
|
|
17
|
+
this.status = data.status || '';
|
|
18
|
+
this.completion = data.completion || '';
|
|
19
|
+
this.created_at = data.created_at || '';
|
|
20
|
+
this.updated_at = data.updated_at || '';
|
|
21
|
+
this.progress = data.progress || '';
|
|
22
|
+
this.fail_reason = data.fail_reason || '';
|
|
23
|
+
this.result = data.result || null;
|
|
24
|
+
}
|
|
25
|
+
isCompleted() {
|
|
26
|
+
return this.status === 'SUCCESS';
|
|
27
|
+
}
|
|
28
|
+
isFailed() {
|
|
29
|
+
return this.status === 'FAILURE';
|
|
30
|
+
}
|
|
31
|
+
isProcessing() {
|
|
32
|
+
return ['PENDING', 'PROCESSING', 'RUNNING', 'QUEUED'].includes(this.status);
|
|
33
|
+
}
|
|
34
|
+
get progressPercent() {
|
|
35
|
+
if (!this.progress)
|
|
36
|
+
return 0;
|
|
37
|
+
const match = this.progress.match(/(\d+)/);
|
|
38
|
+
return match ? parseInt(match[1], 10) : 0;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.TaskStatusImpl = TaskStatusImpl;
|
|
42
|
+
function delay(ms) {
|
|
43
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
44
|
+
}
|
|
45
|
+
async function waitForTaskCompletion(queryFn, taskId, options = {}) {
|
|
46
|
+
const pollInterval = options.pollIntervalMs || 2000;
|
|
47
|
+
const timeout = options.timeoutMs || 5 * 60 * 1000;
|
|
48
|
+
const startTime = Date.now();
|
|
49
|
+
while (true) {
|
|
50
|
+
const status = await queryFn(taskId);
|
|
51
|
+
if (status.isCompleted() || status.isFailed()) {
|
|
52
|
+
return status;
|
|
53
|
+
}
|
|
54
|
+
if (Date.now() - startTime > timeout) {
|
|
55
|
+
throw new Error(`Task ${taskId} timed out after ${timeout}ms`);
|
|
56
|
+
}
|
|
57
|
+
await delay(pollInterval);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export interface CreateAssetGroupRequest {
|
|
2
|
+
model?: string;
|
|
3
|
+
Name: string;
|
|
4
|
+
}
|
|
5
|
+
export interface AssetGroup {
|
|
6
|
+
Id: string;
|
|
7
|
+
Name: string;
|
|
8
|
+
Title?: string;
|
|
9
|
+
GroupType?: string;
|
|
10
|
+
ProjectName?: string;
|
|
11
|
+
CreateTime?: string;
|
|
12
|
+
UpdateTime?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ListAssetGroupsRequest {
|
|
15
|
+
model?: string;
|
|
16
|
+
Filter?: {
|
|
17
|
+
Name?: string;
|
|
18
|
+
GroupIds?: string[];
|
|
19
|
+
};
|
|
20
|
+
PageNumber?: number;
|
|
21
|
+
PageSize?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface ListAssetGroupsResponse {
|
|
24
|
+
Items: AssetGroup[];
|
|
25
|
+
PageNumber: number;
|
|
26
|
+
PageSize: number;
|
|
27
|
+
TotalCount: number;
|
|
28
|
+
}
|
|
29
|
+
export interface CreateAssetRequest {
|
|
30
|
+
model?: string;
|
|
31
|
+
GroupId: string;
|
|
32
|
+
Name?: string;
|
|
33
|
+
AssetType?: string;
|
|
34
|
+
URL: string;
|
|
35
|
+
}
|
|
36
|
+
export interface Asset {
|
|
37
|
+
Id: string;
|
|
38
|
+
Name: string;
|
|
39
|
+
URL: string;
|
|
40
|
+
AssetType: string;
|
|
41
|
+
GroupId: string;
|
|
42
|
+
Status: string;
|
|
43
|
+
ProjectName?: string;
|
|
44
|
+
CreateTime?: string;
|
|
45
|
+
UpdateTime?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface ListAssetsRequest {
|
|
48
|
+
model?: string;
|
|
49
|
+
Filter?: {
|
|
50
|
+
Name?: string;
|
|
51
|
+
GroupIds?: string[];
|
|
52
|
+
GroupType?: string;
|
|
53
|
+
};
|
|
54
|
+
PageNumber?: number;
|
|
55
|
+
PageSize?: number;
|
|
56
|
+
}
|
|
57
|
+
export interface ListAssetsResponse {
|
|
58
|
+
Items: Asset[];
|
|
59
|
+
PageNumber: number;
|
|
60
|
+
PageSize: number;
|
|
61
|
+
TotalCount: number;
|
|
62
|
+
}
|