yuanflow-cli 0.1.49 → 0.1.50
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/package.json +1 -1
- package/src/ai-tools.js +34 -1
package/package.json
CHANGED
package/src/ai-tools.js
CHANGED
|
@@ -566,7 +566,8 @@ async function callBinary(apiPath, options, body) {
|
|
|
566
566
|
const text = await response.text();
|
|
567
567
|
throw new Error(`请求失败:HTTP ${response.status} ${text}`);
|
|
568
568
|
}
|
|
569
|
-
const
|
|
569
|
+
const rawBytes = Buffer.from(await response.arrayBuffer());
|
|
570
|
+
const bytes = normalizeAudioResponseBytes(rawBytes, response.headers.get('content-type') || '');
|
|
570
571
|
await writeFile(options.output, bytes);
|
|
571
572
|
return {
|
|
572
573
|
ok: true,
|
|
@@ -576,6 +577,38 @@ async function callBinary(apiPath, options, body) {
|
|
|
576
577
|
};
|
|
577
578
|
}
|
|
578
579
|
|
|
580
|
+
export function normalizeAudioResponseBytes(rawBytes, contentType = '') {
|
|
581
|
+
const normalizedContentType = String(contentType || '').toLowerCase();
|
|
582
|
+
if (!normalizedContentType.includes('json') && !normalizedContentType.includes('text')) {
|
|
583
|
+
return rawBytes;
|
|
584
|
+
}
|
|
585
|
+
const text = rawBytes.toString('utf8').trim();
|
|
586
|
+
if (!text || (!text.startsWith('{') && !text.startsWith('['))) {
|
|
587
|
+
return rawBytes;
|
|
588
|
+
}
|
|
589
|
+
const chunks = [];
|
|
590
|
+
for (const line of text.split(/\r?\n/)) {
|
|
591
|
+
const trimmed = line.trim();
|
|
592
|
+
if (!trimmed) {
|
|
593
|
+
continue;
|
|
594
|
+
}
|
|
595
|
+
let payload;
|
|
596
|
+
try {
|
|
597
|
+
payload = JSON.parse(trimmed);
|
|
598
|
+
} catch {
|
|
599
|
+
return rawBytes;
|
|
600
|
+
}
|
|
601
|
+
if (payload && payload.code !== undefined && Number(payload.code) !== 0) {
|
|
602
|
+
throw new Error(payload.message || '音频生成失败。');
|
|
603
|
+
}
|
|
604
|
+
if (!payload || typeof payload.data !== 'string' || !payload.data) {
|
|
605
|
+
return rawBytes;
|
|
606
|
+
}
|
|
607
|
+
chunks.push(Buffer.from(payload.data, 'base64'));
|
|
608
|
+
}
|
|
609
|
+
return chunks.length > 0 ? Buffer.concat(chunks) : rawBytes;
|
|
610
|
+
}
|
|
611
|
+
|
|
579
612
|
async function buildRequest(apiPath, options, method, body) {
|
|
580
613
|
const config = await readConfig();
|
|
581
614
|
const baseUrl = cleanBaseUrl(options.baseUrl || config.baseUrl);
|