g2log 1.4.2 → 1.4.3

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/git-user-log.js +150 -88
  2. package/package.json +1 -1
package/git-user-log.js CHANGED
@@ -754,76 +754,107 @@ async function getOpenAIResponse(apiKey, prompt, modelName, apiBaseURL, spinner
754
754
  // 确定使用http还是https
755
755
  const protocol = urlObj.protocol === 'https:' ? require('https') : require('http');
756
756
 
757
- // 创建请求
758
- const req = protocol.request(options, (res) => {
757
+ // 创建请求
758
+ const req = protocol.request(options, (res) => {
759
759
  // 检查状态码
760
- if (res.statusCode !== 200) {
761
- let errorData = '';
762
- res.on('data', chunk => {
763
- errorData += chunk.toString();
764
- });
765
- res.on('end', () => {
760
+ if (res.statusCode !== 200) {
761
+ let errorData = '';
762
+ res.on('data', chunk => {
763
+ errorData += chunk.toString();
764
+ });
765
+ res.on('end', () => {
766
766
  let errorMessage = `OpenAI API请求失败 (${res.statusCode})`;
767
- try {
768
- const parsedError = JSON.parse(errorData);
767
+ try {
768
+ const parsedError = JSON.parse(errorData);
769
769
  errorMessage += `: ${JSON.stringify(parsedError)}`;
770
- } catch (e) {
770
+ } catch (e) {
771
771
  errorMessage += `: ${errorData}`;
772
772
  }
773
773
  if (spinner) spinner.fail(`❌ ${errorMessage}`);
774
774
  reject(new Error(errorMessage));
775
- });
776
- return;
777
- }
778
-
779
- let fullContent = '';
780
- let buffer = '';
781
-
782
- // 处理数据
783
- res.on('data', (chunk) => {
784
- // 将新的数据添加到缓冲区
785
- buffer += chunk.toString();
775
+ });
776
+ return;
777
+ }
786
778
 
787
- // 尝试从缓冲区中提取完整的SSE消息
788
- let match;
789
- const dataRegex = /data: (.*?)\n\n/gs;
779
+ let fullContent = '';
780
+ let buffer = '';
790
781
 
791
- while ((match = dataRegex.exec(buffer)) !== null) {
792
- const data = match[1];
782
+ // 处理数据
783
+ res.on('data', (chunk) => {
784
+ // 将新的数据添加到缓冲区
785
+ const data = chunk.toString();
786
+ buffer += data;
793
787
 
794
- // 跳过 [DONE] 消息
795
- if (data === '[DONE]') continue;
788
+ // 尝试从缓冲区中提取完整的SSE消息
789
+ const messages = buffer.split('\n\n');
796
790
 
797
- try {
798
- const parsedData = JSON.parse(data);
791
+ // 处理除了最后一个可能不完整的消息之外的所有消息
792
+ for (let i = 0; i < messages.length - 1; i++) {
793
+ const message = messages[i].trim();
794
+ if (!message) continue; // 跳过空消息
799
795
 
800
- // 获取内容增量
801
- if (parsedData.choices &&
802
- parsedData.choices[0] &&
803
- parsedData.choices[0].delta &&
804
- parsedData.choices[0].delta.content) {
805
- const content = parsedData.choices[0].delta.content;
806
- fullContent += content;
807
-
808
- // 直接输出内容增量到控制台
809
- process.stdout.write(content);
796
+ // 处理SSE格式的消息
797
+ if (message.startsWith('data: ')) {
798
+ const content = message.substring(6); // 移除 'data: ' 前缀
799
+
800
+ // 跳过[DONE]消息
801
+ if (content === '[DONE]') continue;
802
+
803
+ try {
804
+ const parsed = JSON.parse(content);
805
+ if (parsed.choices &&
806
+ parsed.choices[0] &&
807
+ parsed.choices[0].delta &&
808
+ parsed.choices[0].delta.content) {
809
+ const contentPart = parsed.choices[0].delta.content;
810
+ fullContent += contentPart;
811
+
812
+ // 直接输出内容增量到控制台
813
+ process.stdout.write(contentPart);
814
+ }
815
+ } catch (err) {
816
+ // 忽略解析错误,但在调试模式下输出
817
+ if (process.argv.includes('--debug')) {
818
+ console.error(`解析错误: ${err.message} for content: ${content}`);
819
+ }
810
820
  }
811
- } catch (err) {
812
- // 忽略解析错误
813
821
  }
814
822
  }
815
823
 
816
- // 保留可能不完整的最后一部分
817
- const lastIndex = buffer.lastIndexOf('\n\n');
818
- if (lastIndex !== -1) {
819
- buffer = buffer.substring(lastIndex + 2);
820
- }
824
+ // 保留最后一个可能不完整的消息
825
+ buffer = messages[messages.length - 1];
821
826
  });
822
827
 
823
- // 处理结束
828
+ // 处理响应结束
824
829
  res.on('end', () => {
830
+ // 处理缓冲区中可能剩余的内容
831
+ if (buffer.trim()) {
832
+ const lines = buffer.split('\n');
833
+ for (const line of lines) {
834
+ if (line.startsWith('data: ') && line.substring(6) !== '[DONE]') {
835
+ try {
836
+ const parsed = JSON.parse(line.substring(6));
837
+ if (parsed.choices &&
838
+ parsed.choices[0] &&
839
+ parsed.choices[0].delta &&
840
+ parsed.choices[0].delta.content) {
841
+ const contentPart = parsed.choices[0].delta.content;
842
+ fullContent += contentPart;
843
+
844
+ // 直接输出内容增量到控制台
845
+ process.stdout.write(contentPart);
846
+ }
847
+ } catch (err) {
848
+ // 忽略解析错误
849
+ }
850
+ }
851
+ }
852
+ }
853
+
854
+ // 确保输出后有换行符
855
+ console.log(); // 强制添加换行符
856
+
825
857
  if (spinner) spinner.stop('✅ AI响应已结束');
826
- console.log(); // 添加换行符
827
858
  resolve(fullContent);
828
859
  });
829
860
  });
@@ -879,7 +910,7 @@ async function getDeepSeekResponse(apiKey, prompt, modelName, apiBaseURL, spinne
879
910
  console.log(colorize('📄 系统角色: ' + data.messages[0].content, 'dim'));
880
911
  console.log(colorize('💬 提示内容预览: ' + data.messages[1].content.substring(0, 150) + '...', 'dim'));
881
912
 
882
- if (spinner) spinner.update('🔄 正在AI发送请求...\n');
913
+ if (spinner) spinner.update('🔄 正在向AI发送请求...\n');
883
914
 
884
915
  return new Promise((resolve, reject) => {
885
916
  try {
@@ -926,52 +957,83 @@ async function getDeepSeekResponse(apiKey, prompt, modelName, apiBaseURL, spinne
926
957
  // 处理数据
927
958
  res.on('data', (chunk) => {
928
959
  // 将新的数据添加到缓冲区
929
- buffer += chunk.toString();
960
+ const data = chunk.toString();
961
+ buffer += data;
930
962
 
931
963
  // 尝试从缓冲区中提取完整的SSE消息
932
- let match;
933
- const dataRegex = /data: (.*?)\n\n/gs;
964
+ const messages = buffer.split('\n\n');
934
965
 
935
- while ((match = dataRegex.exec(buffer)) !== null) {
936
- const data = match[1];
966
+ // 处理除了最后一个可能不完整的消息之外的所有消息
967
+ for (let i = 0; i < messages.length - 1; i++) {
968
+ const message = messages[i].trim();
969
+ if (!message) continue; // 跳过空消息
937
970
 
938
- // 跳过 [DONE] 消息
939
- if (data === '[DONE]') continue;
940
-
941
- try {
942
- const parsedData = JSON.parse(data);
971
+ // 处理SSE格式的消息
972
+ if (message.startsWith('data: ')) {
973
+ const content = message.substring(6); // 移除 'data: ' 前缀
943
974
 
944
- // 获取内容增量
945
- if (parsedData.choices &&
946
- parsedData.choices[0] &&
947
- parsedData.choices[0].delta &&
948
- parsedData.choices[0].delta.content) {
949
- const content = parsedData.choices[0].delta.content;
950
- fullContent += content;
951
-
952
- // 直接输出内容增量到控制台
953
- process.stdout.write(content);
975
+ // 跳过[DONE]消息
976
+ if (content === '[DONE]') continue;
977
+
978
+ try {
979
+ const parsed = JSON.parse(content);
980
+ if (parsed.choices &&
981
+ parsed.choices[0] &&
982
+ parsed.choices[0].delta &&
983
+ parsed.choices[0].delta.content) {
984
+ const contentPart = parsed.choices[0].delta.content;
985
+ fullContent += contentPart;
986
+
987
+ // 直接输出内容增量到控制台
988
+ process.stdout.write(contentPart);
989
+ }
990
+ } catch (err) {
991
+ // 忽略解析错误,但在调试模式下输出
992
+ if (process.argv.includes('--debug')) {
993
+ console.error(`解析错误: ${err.message} for content: ${content}`);
994
+ }
995
+ }
954
996
  }
955
- } catch (err) {
956
- // 忽略解析错误
957
997
  }
958
- }
998
+
999
+ // 保留最后一个可能不完整的消息
1000
+ buffer = messages[messages.length - 1];
1001
+ });
959
1002
 
960
- // 保留可能不完整的最后一部分
961
- const lastIndex = buffer.lastIndexOf('\n\n');
962
- if (lastIndex !== -1) {
963
- buffer = buffer.substring(lastIndex + 2);
964
- }
965
- });
966
-
967
- // 处理结束
968
- res.on('end', () => {
969
- if (spinner) spinner.stop('✅ AI响应已接收');
970
- console.log(); // 添加换行符
1003
+ // 处理响应结束
1004
+ res.on('end', () => {
1005
+ // 处理缓冲区中可能剩余的内容
1006
+ if (buffer.trim()) {
1007
+ const lines = buffer.split('\n');
1008
+ for (const line of lines) {
1009
+ if (line.startsWith('data: ') && line.substring(6) !== '[DONE]') {
1010
+ try {
1011
+ const parsed = JSON.parse(line.substring(6));
1012
+ if (parsed.choices &&
1013
+ parsed.choices[0] &&
1014
+ parsed.choices[0].delta &&
1015
+ parsed.choices[0].delta.content) {
1016
+ const contentPart = parsed.choices[0].delta.content;
1017
+ fullContent += contentPart;
1018
+
1019
+ // 直接输出内容增量到控制台
1020
+ process.stdout.write(contentPart);
1021
+ }
1022
+ } catch (err) {
1023
+ // 忽略解析错误
1024
+ }
1025
+ }
1026
+ }
1027
+ }
1028
+
1029
+ // 确保输出后有换行符
1030
+ console.log(); // 强制添加换行符
1031
+
1032
+ if (spinner) spinner.stop('✅ AI响应已结束');
971
1033
  resolve(fullContent);
1034
+ });
972
1035
  });
973
- });
974
-
1036
+
975
1037
  // 处理请求错误
976
1038
  req.on('error', (error) => {
977
1039
  if (spinner) spinner.fail(`❌ DeepSeek API网络错误: ${error.message}`);
@@ -980,7 +1042,7 @@ async function getDeepSeekResponse(apiKey, prompt, modelName, apiBaseURL, spinne
980
1042
 
981
1043
  // 发送请求体
982
1044
  req.write(JSON.stringify(data));
983
- req.end();
1045
+ req.end();
984
1046
  } catch (error) {
985
1047
  if (spinner) spinner.fail(`❌ DeepSeek API错误: ${error.message}`);
986
1048
  reject(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "g2log",
3
- "version": "1.4.2",
3
+ "version": "1.4.3",
4
4
  "description": "查询特定用户和时间范围的Git提交记录并通过AI进行总结,可通过npx直接运行",
5
5
  "main": "git-user-log.js",
6
6
  "bin": {