yuanflow-cli 0.1.49 → 0.1.51

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/ai-tools.js +58 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yuanflow-cli",
3
- "version": "0.1.49",
3
+ "version": "0.1.51",
4
4
  "description": "YuanFlow 自媒体 API CLI 与 Skill 安装器。",
5
5
  "type": "module",
6
6
  "license": "MIT",
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 bytes = Buffer.from(await response.arrayBuffer());
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,62 @@ 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 splitJsonBase64AudioRecords(text)) {
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
+ const code = payload && payload.code !== undefined ? Number(payload.code) : 0;
602
+ if (payload && payload.code !== undefined && code !== 0 && code !== 20000000) {
603
+ throw new Error(payload.message || '音频生成失败。');
604
+ }
605
+ if (!payload || typeof payload.data !== 'string' || !payload.data) {
606
+ if (chunks.length > 0) {
607
+ continue;
608
+ }
609
+ return rawBytes;
610
+ }
611
+ chunks.push(Buffer.from(payload.data, 'base64'));
612
+ }
613
+ return chunks.length > 0 ? Buffer.concat(chunks) : rawBytes;
614
+ }
615
+
616
+ function splitJsonBase64AudioRecords(text) {
617
+ const lines = text.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
618
+ if (lines.length > 1) {
619
+ return lines;
620
+ }
621
+ return text
622
+ .split(/}\s*{/)
623
+ .map((part, index, parts) => {
624
+ let value = part.trim();
625
+ if (index > 0) {
626
+ value = `{${value}`;
627
+ }
628
+ if (index < parts.length - 1) {
629
+ value = `${value}}`;
630
+ }
631
+ return value;
632
+ })
633
+ .filter(Boolean);
634
+ }
635
+
579
636
  async function buildRequest(apiPath, options, method, body) {
580
637
  const config = await readConfig();
581
638
  const baseUrl = cleanBaseUrl(options.baseUrl || config.baseUrl);