-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
75 lines (67 loc) · 2.76 KB
/
Copy pathindex.mjs
File metadata and controls
75 lines (67 loc) · 2.76 KB
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
import { existsSync, readFileSync } from "node:fs";
import { resolve } from "node:path";
for (const envPath of [resolve(process.cwd(), ".env"), resolve(process.cwd(), "../.env")]) {
if (!existsSync(envPath)) continue;
for (const line of readFileSync(envPath, "utf8").split(/\r?\n/)) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith("#")) continue;
const index = trimmed.indexOf("=");
if (index < 0) continue;
const key = trimmed.slice(0, index).trim();
const value = trimmed.slice(index + 1).trim();
if (!process.env[key]) process.env[key] = value;
}
}
const apiKey = process.env.POYO_API_KEY;
const baseUrl = process.env.POYO_BASE_URL || "https://api.poyo.ai";
if (!apiKey || apiKey === "YOUR_POYO_API_KEY_HERE") {
console.error("Set POYO_API_KEY in your environment or repo-root .env file.");
process.exit(1);
}
async function requestJson(path, options) {
const response = await fetch(`${baseUrl}${path}`, options);
const text = await response.text();
let body = {};
if (text) {
try { body = JSON.parse(text); } catch { body = { raw: text }; }
}
const apiCode = Number(body?.code);
const hasApiError = Number.isFinite(apiCode) && apiCode !== 0 && apiCode !== 200;
if (!response.ok || hasApiError) {
throw new Error(`PoYo request failed: ${JSON.stringify({ http_status: response.status, api_code: body?.code, body }, null, 2)}`);
}
return body;
}
const payload = {
"model": "qwen-image-3",
"input": {
"prompt": "Editorial travel poster of Hangzhou at dawn, West Lake mist, restrained teal and vermilion palette, crisp Chinese-inspired geometric layout, no logo",
"size": "3:2",
"resolution": "2K",
"output_format": "png",
"prompt_extend": true,
"enable_safety_checker": true
}
};
if (process.env.POYO_CALLBACK_URL) payload.callback_url = process.env.POYO_CALLBACK_URL;
const submitted = await requestJson("/api/generate/submit", {
method: "POST",
headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
const taskId = submitted.data?.task_id;
if (!taskId) throw new Error(`Missing data.task_id: ${JSON.stringify(submitted)}`);
console.log(`submitted task: ${taskId}`);
for (let attempt = 1; attempt <= 60; attempt += 1) {
const result = await requestJson(`/api/generate/status/${taskId}`, {
headers: { Authorization: `Bearer ${apiKey}` },
});
const status = result.data?.status;
console.log(`poll ${attempt}: ${status || "unknown"}`);
if (status === "finished" || status === "failed") {
console.log(JSON.stringify(result, null, 2));
process.exit(status === "finished" ? 0 : 1);
}
await new Promise((resolveSleep) => setTimeout(resolveSleep, 5000));
}
throw new Error(`Timed out waiting for task ${taskId}`);