iflow-feishu 1.1.1 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iflow-feishu",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "iFlow CLI 飞书插件 - 将 iFlow AI 助手接入飞书机器人",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -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
  */
@@ -59,14 +92,19 @@ class StreamHandler {
59
92
  if (execInfoMatch) {
60
93
  try {
61
94
  const execInfo = JSON.parse(execInfoMatch[1]);
95
+ logger.info(`📊 解析到 Execution Info: ${JSON.stringify(execInfo.tokenUsage)}`);
62
96
  if (execInfo.tokenUsage && execInfo.tokenUsage.total > 0) {
63
97
  const used = execInfo.tokenUsage.total;
64
98
  const remaining = Math.max(0, maxTokens - used);
65
- return Math.round((remaining / maxTokens) * 100);
99
+ const percent = Math.round((remaining / maxTokens) * 100);
100
+ logger.info(`📊 Token 计算: 已用=${used}, 剩余=${remaining}, 百分比=${percent}%`);
101
+ return percent;
66
102
  }
67
103
  } catch (e) {
68
- // 解析失败,忽略
104
+ logger.warn(`📊 解析 Execution Info 失败: ${e.message}`);
69
105
  }
106
+ } else {
107
+ logger.info(`📊 未找到 Execution Info 标签`);
70
108
  }
71
109
  return null;
72
110
  }