iflow-feishu 1.1.2 → 1.1.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/package.json +1 -1
- package/src/core/stream-handler.js +33 -0
package/package.json
CHANGED
|
@@ -30,8 +30,16 @@ class StreamHandler {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
const maxTokens = this.getMaxTokens(modelName);
|
|
33
|
+
|
|
34
|
+
// 首先尝试从 Execution Info 提取
|
|
33
35
|
let contentLeftPercent = this.extractTokenUsage(text, maxTokens);
|
|
34
36
|
|
|
37
|
+
// 如果没有,尝试从回复文本中提取(三元会在回复中报告上下文剩余)
|
|
38
|
+
if (contentLeftPercent === null) {
|
|
39
|
+
contentLeftPercent = this.extractContextFromContent(text);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 最后尝试从日志读取
|
|
35
43
|
if (contentLeftPercent === null && chatId) {
|
|
36
44
|
contentLeftPercent = this.getTokenUsageFromLogs(modelName);
|
|
37
45
|
}
|
|
@@ -43,6 +51,31 @@ class StreamHandler {
|
|
|
43
51
|
return { reasoning, content: content || '(无法提取响应)', contentLeftPercent };
|
|
44
52
|
}
|
|
45
53
|
|
|
54
|
+
/**
|
|
55
|
+
* 从回复内容中提取上下文剩余百分比
|
|
56
|
+
* 匹配格式: "📊 剩余 ~77%", "剩余 ~77%", "剩余 77%" 等
|
|
57
|
+
*/
|
|
58
|
+
extractContextFromContent(text) {
|
|
59
|
+
// 匹配多种格式: 剩余 ~77%, 剩余 77%, 📊 剩余 ~77%
|
|
60
|
+
const patterns = [
|
|
61
|
+
/剩余\s*~?(\d+)%/i,
|
|
62
|
+
/📊\s*剩余\s*~?(\d+)%/i,
|
|
63
|
+
/remaining[:\s]*~?(\d+)%/i
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
for (const pattern of patterns) {
|
|
67
|
+
const match = text.match(pattern);
|
|
68
|
+
if (match) {
|
|
69
|
+
const percent = parseInt(match[1], 10);
|
|
70
|
+
if (percent >= 0 && percent <= 100) {
|
|
71
|
+
logger.info(`📊 从回复内容提取上下文剩余: ${percent}%`);
|
|
72
|
+
return percent;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
46
79
|
/**
|
|
47
80
|
* 获取模型最大 token 数
|
|
48
81
|
*/
|