koishi-plugin-aka-ai-generator 0.6.10 → 0.6.11

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/lib/index.js +290 -28
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -410,18 +410,60 @@ var GptGodProvider = class {
410
410
  throw new Error(`生成失败:${shortError}`);
411
411
  }
412
412
  }
413
+ logger.debug("开始流式处理图片 (GPTGod)", {
414
+ imagesCount: images.length,
415
+ hasCallback: !!onImageGenerated,
416
+ current: i + 1,
417
+ total: numImages
418
+ });
413
419
  for (let imgIdx = 0; imgIdx < images.length; imgIdx++) {
414
420
  const imageUrl = images[imgIdx];
415
421
  const currentIndex = allImages.length;
416
422
  allImages.push(imageUrl);
423
+ logger.debug("准备处理单张图片 (GPTGod)", {
424
+ imgIdx,
425
+ currentIndex,
426
+ total: numImages,
427
+ imageUrlType: typeof imageUrl,
428
+ imageUrlLength: imageUrl?.length || 0,
429
+ imageUrlPrefix: imageUrl?.substring(0, 50) || "null"
430
+ });
417
431
  if (onImageGenerated) {
432
+ logger.info("准备调用图片生成回调函数 (GPTGod)", {
433
+ hasCallback: true,
434
+ currentIndex,
435
+ total: numImages,
436
+ imageUrlLength: imageUrl?.length || 0
437
+ });
418
438
  try {
419
439
  await onImageGenerated(imageUrl, currentIndex, numImages);
440
+ logger.info("图片生成回调函数执行成功 (GPTGod)", {
441
+ currentIndex,
442
+ total: numImages,
443
+ imageUrlLength: imageUrl?.length || 0
444
+ });
420
445
  } catch (callbackError) {
421
- logger.warn("图片生成回调函数执行失败", { error: sanitizeError(callbackError) });
446
+ logger.error("图片生成回调函数执行失败 (GPTGod)", {
447
+ error: sanitizeError(callbackError),
448
+ errorMessage: callbackError?.message,
449
+ errorStack: callbackError?.stack,
450
+ currentIndex,
451
+ total: numImages,
452
+ imageUrlLength: imageUrl?.length || 0
453
+ });
422
454
  }
455
+ } else {
456
+ logger.warn("图片生成回调函数未提供,跳过流式发送 (GPTGod)", {
457
+ currentIndex,
458
+ total: numImages,
459
+ imageUrlLength: imageUrl?.length || 0
460
+ });
423
461
  }
424
462
  }
463
+ logger.debug("流式处理图片完成 (GPTGod)", {
464
+ processedCount: images.length,
465
+ allImagesCount: allImages.length
466
+ });
425
467
  logger.success("GPTGod 图像编辑 API 调用成功", { current: i + 1, total: numImages });
426
468
  success = true;
427
469
  break;
@@ -492,6 +534,11 @@ var GptGodProvider = class {
492
534
  function parseGeminiResponse(response, logger) {
493
535
  try {
494
536
  const images = [];
537
+ logger?.debug("开始解析 Gemini API 响应", {
538
+ hasResponse: !!response,
539
+ responseType: typeof response,
540
+ responseKeys: response ? Object.keys(response) : []
541
+ });
495
542
  if (!response) {
496
543
  logger?.error("Gemini API 响应为空");
497
544
  return [];
@@ -537,7 +584,16 @@ function parseGeminiResponse(response, logger) {
537
584
  }
538
585
  }
539
586
  if (response.candidates && response.candidates.length > 0) {
540
- for (const candidate of response.candidates) {
587
+ logger?.debug("找到 candidates", { candidatesCount: response.candidates.length });
588
+ for (let candIdx = 0; candIdx < response.candidates.length; candIdx++) {
589
+ const candidate = response.candidates[candIdx];
590
+ logger?.debug("处理 candidate", {
591
+ index: candIdx,
592
+ hasContent: !!candidate.content,
593
+ hasParts: !!candidate.content?.parts,
594
+ partsCount: candidate.content?.parts?.length || 0,
595
+ finishReason: candidate.finishReason
596
+ });
541
597
  if (candidate.finishReason && candidate.finishReason !== "STOP") {
542
598
  logger?.warn("Gemini 响应 finishReason 异常", {
543
599
  finishReason: candidate.finishReason,
@@ -551,28 +607,67 @@ function parseGeminiResponse(response, logger) {
551
607
  }
552
608
  }
553
609
  if (candidate.content && candidate.content.parts) {
554
- for (const part of candidate.content.parts) {
610
+ logger?.debug("处理 candidate.content.parts", {
611
+ partsCount: candidate.content.parts.length,
612
+ partsKeys: candidate.content.parts.map((p) => Object.keys(p))
613
+ });
614
+ for (let partIdx = 0; partIdx < candidate.content.parts.length; partIdx++) {
615
+ const part = candidate.content.parts[partIdx];
616
+ const partKeys = Object.keys(part);
617
+ logger?.debug("处理 part", {
618
+ partIndex: partIdx,
619
+ partKeys,
620
+ hasInlineData: !!part.inlineData,
621
+ hasInline_data: !!part.inline_data,
622
+ hasFileData: !!part.fileData,
623
+ hasText: !!part.text
624
+ });
555
625
  if (part.inlineData && part.inlineData.data) {
556
626
  const base64Data = part.inlineData.data;
557
627
  const mimeType = part.inlineData.mimeType || "image/jpeg";
558
628
  const dataUrl = `data:${mimeType};base64,${base64Data}`;
559
629
  images.push(dataUrl);
560
- logger?.debug("从响应中提取到图片 (inlineData)", { mimeType, dataLength: base64Data.length });
630
+ logger?.info("从响应中提取到图片 (inlineData)", {
631
+ mimeType,
632
+ dataLength: base64Data.length,
633
+ dataUrlLength: dataUrl.length,
634
+ imageIndex: images.length - 1
635
+ });
561
636
  } else if (part.inline_data && part.inline_data.data) {
562
637
  const base64Data = part.inline_data.data;
563
638
  const mimeType = part.inline_data.mime_type || "image/jpeg";
564
639
  const dataUrl = `data:${mimeType};base64,${base64Data}`;
565
640
  images.push(dataUrl);
566
- logger?.debug("从响应中提取到图片 (inline_data)", { mimeType, dataLength: base64Data.length });
641
+ logger?.info("从响应中提取到图片 (inline_data)", {
642
+ mimeType,
643
+ dataLength: base64Data.length,
644
+ dataUrlLength: dataUrl.length,
645
+ imageIndex: images.length - 1
646
+ });
567
647
  } else if (part.fileData && part.fileData.fileUri) {
568
648
  images.push(part.fileData.fileUri);
569
- logger?.debug("从响应中提取到图片 (fileData)", { fileUri: part.fileData.fileUri });
649
+ logger?.info("从响应中提取到图片 (fileData)", {
650
+ fileUri: part.fileData.fileUri,
651
+ imageIndex: images.length - 1
652
+ });
570
653
  } else if (part.text) {
571
- logger?.warn("响应中包含文本而非图片", { text: part.text.substring(0, 100) });
654
+ logger?.warn("响应中包含文本而非图片", {
655
+ text: part.text.substring(0, 100),
656
+ textLength: part.text.length
657
+ });
658
+ } else {
659
+ logger?.warn("part 中没有找到图片或文本数据", {
660
+ partKeys,
661
+ part: JSON.stringify(part).substring(0, 200)
662
+ });
572
663
  }
573
664
  }
574
665
  } else {
575
- logger?.warn("候选响应中没有 content.parts", { candidate: JSON.stringify(candidate).substring(0, 200) });
666
+ logger?.warn("候选响应中没有 content.parts", {
667
+ candidateIndex: candIdx,
668
+ candidateKeys: Object.keys(candidate),
669
+ candidate: JSON.stringify(candidate).substring(0, 200)
670
+ });
576
671
  }
577
672
  }
578
673
  } else {
@@ -592,13 +687,19 @@ function parseGeminiResponse(response, logger) {
592
687
  });
593
688
  }
594
689
  }
690
+ logger?.debug("parseGeminiResponse 完成", {
691
+ extractedImagesCount: images.length,
692
+ hasCandidates: !!response.candidates,
693
+ candidatesCount: response.candidates?.length || 0
694
+ });
595
695
  if (images.length === 0) {
596
696
  logger?.error("未能从 Gemini API 响应中提取到任何图片", {
597
697
  hasCandidates: !!response.candidates,
598
698
  candidatesCount: response.candidates?.length || 0,
599
699
  responseKeys: Object.keys(response),
600
- firstCandidate: response.candidates?.[0] ? JSON.stringify(response.candidates[0]).substring(0, 300) : null,
601
- promptFeedback: response.promptFeedback ? JSON.stringify(response.promptFeedback) : null
700
+ firstCandidate: response.candidates?.[0] ? JSON.stringify(response.candidates[0]).substring(0, 500) : null,
701
+ promptFeedback: response.promptFeedback ? JSON.stringify(response.promptFeedback) : null,
702
+ fullResponse: JSON.stringify(response).substring(0, 1e3)
602
703
  });
603
704
  }
604
705
  return images;
@@ -693,18 +794,60 @@ var GeminiProvider = class {
693
794
  });
694
795
  } else {
695
796
  logger.success("Gemini API 调用成功", { current: i + 1, total: numImages, imagesCount: images.length });
797
+ logger.debug("开始流式处理图片", {
798
+ imagesCount: images.length,
799
+ hasCallback: !!onImageGenerated,
800
+ current: i + 1,
801
+ total: numImages
802
+ });
696
803
  for (let imgIdx = 0; imgIdx < images.length; imgIdx++) {
697
804
  const imageUrl = images[imgIdx];
698
805
  const currentIndex = allImages.length;
699
806
  allImages.push(imageUrl);
807
+ logger.debug("准备处理单张图片", {
808
+ imgIdx,
809
+ currentIndex,
810
+ total: numImages,
811
+ imageUrlType: typeof imageUrl,
812
+ imageUrlLength: imageUrl?.length || 0,
813
+ imageUrlPrefix: imageUrl?.substring(0, 50) || "null"
814
+ });
700
815
  if (onImageGenerated) {
816
+ logger.info("准备调用图片生成回调函数", {
817
+ hasCallback: true,
818
+ currentIndex,
819
+ total: numImages,
820
+ imageUrlLength: imageUrl?.length || 0
821
+ });
701
822
  try {
702
823
  await onImageGenerated(imageUrl, currentIndex, numImages);
824
+ logger.info("图片生成回调函数执行成功", {
825
+ currentIndex,
826
+ total: numImages,
827
+ imageUrlLength: imageUrl?.length || 0
828
+ });
703
829
  } catch (callbackError) {
704
- logger.warn("图片生成回调函数执行失败", { error: sanitizeError(callbackError) });
830
+ logger.error("图片生成回调函数执行失败", {
831
+ error: sanitizeError(callbackError),
832
+ errorMessage: callbackError?.message,
833
+ errorStack: callbackError?.stack,
834
+ currentIndex,
835
+ total: numImages,
836
+ imageUrlLength: imageUrl?.length || 0
837
+ });
705
838
  }
839
+ } else {
840
+ logger.warn("图片生成回调函数未提供,跳过流式发送", {
841
+ currentIndex,
842
+ total: numImages,
843
+ imageUrlLength: imageUrl?.length || 0
844
+ });
706
845
  }
707
846
  }
847
+ logger.debug("流式处理图片完成", {
848
+ processedCount: images.length,
849
+ allImagesCount: allImages.length
850
+ });
708
851
  }
709
852
  } catch (error) {
710
853
  const sanitizedError = sanitizeError(error);
@@ -1531,14 +1674,29 @@ function apply(ctx, config) {
1531
1674
  const providerType = requestContext?.provider || config.provider;
1532
1675
  const targetModelId = requestContext?.modelId;
1533
1676
  const providerInstance = getProviderInstance(providerType, targetModelId);
1534
- if (config.logLevel === "debug") {
1535
- logger.debug("准备调用图像供应商", {
1677
+ logger.info("requestProviderImages 调用", {
1678
+ providerType,
1679
+ modelId: targetModelId || "default",
1680
+ numImages,
1681
+ hasCallback: !!onImageGenerated,
1682
+ promptLength: prompt.length,
1683
+ imageUrlsCount: Array.isArray(imageUrls) ? imageUrls.length : imageUrls ? 1 : 0
1684
+ });
1685
+ try {
1686
+ const result = await providerInstance.generateImages(prompt, imageUrls, numImages, onImageGenerated);
1687
+ logger.info("requestProviderImages 完成", {
1688
+ providerType,
1689
+ resultCount: result.length
1690
+ });
1691
+ return result;
1692
+ } catch (error) {
1693
+ logger.error("requestProviderImages 失败", {
1536
1694
  providerType,
1537
- modelId: targetModelId || "default",
1538
- numImages
1695
+ error: sanitizeError(error),
1696
+ errorMessage: error?.message
1539
1697
  });
1698
+ throw error;
1540
1699
  }
1541
- return await providerInstance.generateImages(prompt, imageUrls, numImages, onImageGenerated);
1542
1700
  }
1543
1701
  __name(requestProviderImages, "requestProviderImages");
1544
1702
  async function processImageWithTimeout(session, img, prompt, styleName, requestContext, displayInfo, mode = "single") {
@@ -1637,26 +1795,78 @@ ${infoParts.join("\n")}`;
1637
1795
  const generatedImages = [];
1638
1796
  let creditDeducted = false;
1639
1797
  const onImageGenerated = /* @__PURE__ */ __name(async (imageUrl, index, total) => {
1798
+ logger.info("流式回调被调用", {
1799
+ userId,
1800
+ index,
1801
+ total,
1802
+ imageUrlType: typeof imageUrl,
1803
+ imageUrlLength: imageUrl?.length || 0,
1804
+ imageUrlPrefix: imageUrl?.substring(0, 50) || "null",
1805
+ hasImageUrl: !!imageUrl
1806
+ });
1640
1807
  if (checkTimeout && checkTimeout()) {
1808
+ logger.error("流式回调:检测到超时", { userId, index, total });
1641
1809
  throw new Error("命令执行超时");
1642
1810
  }
1643
1811
  generatedImages.push(imageUrl);
1812
+ logger.debug("图片已添加到 generatedImages", {
1813
+ userId,
1814
+ currentCount: generatedImages.length,
1815
+ index,
1816
+ total
1817
+ });
1644
1818
  if (!creditDeducted && generatedImages.length > 0) {
1645
1819
  creditDeducted = true;
1646
- await recordUserUsage(session, styleName, total);
1647
- logger.info("流式处理:第一张图片生成,积分已扣除", {
1820
+ logger.info("准备扣除积分", { userId, totalImages: total, currentIndex: index });
1821
+ try {
1822
+ await recordUserUsage(session, styleName, total);
1823
+ logger.info("流式处理:第一张图片生成,积分已扣除", {
1824
+ userId,
1825
+ totalImages: total,
1826
+ currentIndex: index
1827
+ });
1828
+ } catch (creditError) {
1829
+ logger.error("扣除积分失败", {
1830
+ userId,
1831
+ error: sanitizeError(creditError),
1832
+ totalImages: total
1833
+ });
1834
+ throw creditError;
1835
+ }
1836
+ }
1837
+ logger.info("准备发送图片", { userId, index: index + 1, total, imageUrlLength: imageUrl?.length || 0 });
1838
+ try {
1839
+ await session.send(import_koishi2.h.image(imageUrl));
1840
+ logger.info("流式处理:图片已发送", { index: index + 1, total, userId });
1841
+ } catch (sendError) {
1842
+ logger.error("发送图片失败", {
1648
1843
  userId,
1649
- totalImages: total,
1650
- currentIndex: index
1844
+ error: sanitizeError(sendError),
1845
+ errorMessage: sendError?.message,
1846
+ index: index + 1,
1847
+ total
1651
1848
  });
1849
+ throw sendError;
1652
1850
  }
1653
- await session.send(import_koishi2.h.image(imageUrl));
1654
- logger.debug("流式处理:图片已发送", { index: index + 1, total });
1655
1851
  if (total > 1 && index < total - 1) {
1852
+ logger.debug("多张图片,添加延时", { index, total });
1656
1853
  await new Promise((resolve) => setTimeout(resolve, 1e3));
1657
1854
  }
1658
1855
  }, "onImageGenerated");
1856
+ logger.info("准备调用 requestProviderImages,已设置回调函数", {
1857
+ userId,
1858
+ hasCallback: !!onImageGenerated,
1859
+ imageCount,
1860
+ promptLength: finalPrompt.length,
1861
+ imageUrlsCount: Array.isArray(imageUrls) ? imageUrls.length : imageUrls ? 1 : 0
1862
+ });
1659
1863
  const images = await requestProviderImages(finalPrompt, imageUrls, imageCount, requestContext, onImageGenerated);
1864
+ logger.info("requestProviderImages 返回", {
1865
+ userId,
1866
+ imagesCount: images.length,
1867
+ generatedImagesCount: generatedImages.length,
1868
+ creditDeducted
1869
+ });
1660
1870
  if (checkTimeout && checkTimeout()) throw new Error("命令执行超时");
1661
1871
  if (images.length === 0) {
1662
1872
  return "图像处理失败:未能生成图片";
@@ -1819,26 +2029,78 @@ Prompt: ${prompt}`);
1819
2029
  const generatedImages = [];
1820
2030
  let creditDeducted = false;
1821
2031
  const onImageGenerated = /* @__PURE__ */ __name(async (imageUrl, index, total) => {
2032
+ logger.info("流式回调被调用 (COMPOSE_IMAGE)", {
2033
+ userId,
2034
+ index,
2035
+ total,
2036
+ imageUrlType: typeof imageUrl,
2037
+ imageUrlLength: imageUrl?.length || 0,
2038
+ imageUrlPrefix: imageUrl?.substring(0, 50) || "null",
2039
+ hasImageUrl: !!imageUrl
2040
+ });
1822
2041
  if (isTimeout) {
2042
+ logger.error("流式回调:检测到超时 (COMPOSE_IMAGE)", { userId, index, total });
1823
2043
  throw new Error("命令执行超时");
1824
2044
  }
1825
2045
  generatedImages.push(imageUrl);
2046
+ logger.debug("图片已添加到 generatedImages (COMPOSE_IMAGE)", {
2047
+ userId,
2048
+ currentCount: generatedImages.length,
2049
+ index,
2050
+ total
2051
+ });
1826
2052
  if (!creditDeducted && generatedImages.length > 0) {
1827
2053
  creditDeducted = true;
1828
- await recordUserUsage(session, COMMANDS.COMPOSE_IMAGE, total);
1829
- logger.info("流式处理:第一张图片生成,积分已扣除", {
2054
+ logger.info("准备扣除积分 (COMPOSE_IMAGE)", { userId, totalImages: total, currentIndex: index });
2055
+ try {
2056
+ await recordUserUsage(session, COMMANDS.COMPOSE_IMAGE, total);
2057
+ logger.info("流式处理:第一张图片生成,积分已扣除 (COMPOSE_IMAGE)", {
2058
+ userId,
2059
+ totalImages: total,
2060
+ currentIndex: index
2061
+ });
2062
+ } catch (creditError) {
2063
+ logger.error("扣除积分失败 (COMPOSE_IMAGE)", {
2064
+ userId,
2065
+ error: sanitizeError(creditError),
2066
+ totalImages: total
2067
+ });
2068
+ throw creditError;
2069
+ }
2070
+ }
2071
+ logger.info("准备发送图片 (COMPOSE_IMAGE)", { userId, index: index + 1, total, imageUrlLength: imageUrl?.length || 0 });
2072
+ try {
2073
+ await session.send(import_koishi2.h.image(imageUrl));
2074
+ logger.info("流式处理:图片已发送 (COMPOSE_IMAGE)", { index: index + 1, total, userId });
2075
+ } catch (sendError) {
2076
+ logger.error("发送图片失败 (COMPOSE_IMAGE)", {
1830
2077
  userId,
1831
- totalImages: total,
1832
- currentIndex: index
2078
+ error: sanitizeError(sendError),
2079
+ errorMessage: sendError?.message,
2080
+ index: index + 1,
2081
+ total
1833
2082
  });
2083
+ throw sendError;
1834
2084
  }
1835
- await session.send(import_koishi2.h.image(imageUrl));
1836
- logger.debug("流式处理:图片已发送", { index: index + 1, total });
1837
2085
  if (total > 1 && index < total - 1) {
2086
+ logger.debug("多张图片,添加延时 (COMPOSE_IMAGE)", { index, total });
1838
2087
  await new Promise((resolve) => setTimeout(resolve, 1e3));
1839
2088
  }
1840
2089
  }, "onImageGenerated");
2090
+ logger.info("准备调用 requestProviderImages (COMPOSE_IMAGE),已设置回调函数", {
2091
+ userId,
2092
+ hasCallback: !!onImageGenerated,
2093
+ imageCount,
2094
+ promptLength: prompt.length,
2095
+ collectedImagesCount: collectedImages.length
2096
+ });
1841
2097
  const resultImages = await requestProviderImages(prompt, collectedImages, imageCount, void 0, onImageGenerated);
2098
+ logger.info("requestProviderImages 返回 (COMPOSE_IMAGE)", {
2099
+ userId,
2100
+ imagesCount: resultImages.length,
2101
+ generatedImagesCount: generatedImages.length,
2102
+ creditDeducted
2103
+ });
1842
2104
  if (isTimeout) throw new Error("命令执行超时");
1843
2105
  if (resultImages.length === 0) {
1844
2106
  return "图片合成失败:未能生成图片";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-aka-ai-generator",
3
3
  "description": "自用AI生成插件(GPTGod & Yunwu)",
4
- "version": "0.6.10",
4
+ "version": "0.6.11",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [