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.
- package/git-user-log.js +150 -88
- 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
|
-
|
757
|
+
// 创建请求
|
758
|
+
const req = protocol.request(options, (res) => {
|
759
759
|
// 检查状态码
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
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
|
-
|
768
|
-
|
767
|
+
try {
|
768
|
+
const parsedError = JSON.parse(errorData);
|
769
769
|
errorMessage += `: ${JSON.stringify(parsedError)}`;
|
770
|
-
|
770
|
+
} catch (e) {
|
771
771
|
errorMessage += `: ${errorData}`;
|
772
772
|
}
|
773
773
|
if (spinner) spinner.fail(`❌ ${errorMessage}`);
|
774
774
|
reject(new Error(errorMessage));
|
775
|
-
|
776
|
-
|
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
|
-
|
788
|
-
let
|
789
|
-
const dataRegex = /data: (.*?)\n\n/gs;
|
779
|
+
let fullContent = '';
|
780
|
+
let buffer = '';
|
790
781
|
|
791
|
-
|
792
|
-
|
782
|
+
// 处理数据
|
783
|
+
res.on('data', (chunk) => {
|
784
|
+
// 将新的数据添加到缓冲区
|
785
|
+
const data = chunk.toString();
|
786
|
+
buffer += data;
|
793
787
|
|
794
|
-
//
|
795
|
-
|
788
|
+
// 尝试从缓冲区中提取完整的SSE消息
|
789
|
+
const messages = buffer.split('\n\n');
|
796
790
|
|
797
|
-
|
798
|
-
|
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
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
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
|
-
|
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('🔄
|
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
|
-
|
960
|
+
const data = chunk.toString();
|
961
|
+
buffer += data;
|
930
962
|
|
931
963
|
// 尝试从缓冲区中提取完整的SSE消息
|
932
|
-
|
933
|
-
const dataRegex = /data: (.*?)\n\n/gs;
|
964
|
+
const messages = buffer.split('\n\n');
|
934
965
|
|
935
|
-
|
936
|
-
|
966
|
+
// 处理除了最后一个可能不完整的消息之外的所有消息
|
967
|
+
for (let i = 0; i < messages.length - 1; i++) {
|
968
|
+
const message = messages[i].trim();
|
969
|
+
if (!message) continue; // 跳过空消息
|
937
970
|
|
938
|
-
//
|
939
|
-
if (data
|
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 (
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
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
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
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
|
-
|
1045
|
+
req.end();
|
984
1046
|
} catch (error) {
|
985
1047
|
if (spinner) spinner.fail(`❌ DeepSeek API错误: ${error.message}`);
|
986
1048
|
reject(error);
|