smart-review 1.0.1

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.
@@ -0,0 +1,46 @@
1
+ /**
2
+ * 全局并发限速器:统一管理AI请求的并发占用(批次与分段共享)
3
+ */
4
+ export class ConcurrencyLimiter {
5
+ constructor(limit = 1) {
6
+ this.limit = Math.max(1, Number(limit) || 1);
7
+ this.active = 0;
8
+ this.queue = [];
9
+ }
10
+
11
+ async acquire() {
12
+ if (this.active < this.limit) {
13
+ this.active++;
14
+ return this._release.bind(this);
15
+ }
16
+ return new Promise((resolve) => {
17
+ this.queue.push(resolve);
18
+ }).then(() => {
19
+ this.active++;
20
+ return this._release.bind(this);
21
+ });
22
+ }
23
+
24
+ _release() {
25
+ if (this.active > 0) this.active--;
26
+ const next = this.queue.shift();
27
+ if (next) next();
28
+ }
29
+
30
+ async withPermit(fn) {
31
+ const release = await this.acquire();
32
+ try {
33
+ return await fn();
34
+ } finally {
35
+ release();
36
+ }
37
+ }
38
+
39
+ getAvailable() {
40
+ return Math.max(0, this.limit - this.active);
41
+ }
42
+
43
+ getActive() {
44
+ return this.active;
45
+ }
46
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * 项目常量定义
3
+ * 用于替换代码中的魔法数字,提高代码可读性和可维护性
4
+ */
5
+
6
+ // AI 相关常量
7
+ export const AI_CONSTANTS = {
8
+ // Token 相关
9
+ DEFAULT_CONTEXT_WINDOW: 8192,
10
+ DEFAULT_OUTPUT_RESERVE_TOKENS: 512,
11
+ DEFAULT_MAX_RESPONSE_TOKENS: 2000,
12
+ DEFAULT_MAX_REQUEST_TOKENS: 8000,
13
+ MIN_DYNAMIC_BUDGET: 256,
14
+ MAX_DYNAMIC_BUDGET: 8192,
15
+ MAX_RESPONSE_TOKENS_LIMIT: 8192,
16
+
17
+ // 字符到Token的估算比例
18
+ CHARS_PER_TOKEN: 4,
19
+
20
+ // 重试相关
21
+ DEFAULT_REQUEST_RETRIES: 3,
22
+ DEFAULT_REQUEST_BACKOFF_MS: 500,
23
+
24
+ // 文件大小限制
25
+ DEFAULT_MAX_FILE_SIZE_KB: 100,
26
+
27
+ // 确认文本长度限制
28
+ MAX_CONFIRMATION_TEXT_LENGTH: 100
29
+ };
30
+
31
+ // 分段分析相关常量
32
+ export const SEGMENTATION_CONSTANTS = {
33
+ // 每段最大行数
34
+ DEFAULT_MAX_LINES_PER_SEGMENT: 100,
35
+
36
+ // 上下文重叠行数
37
+ DEFAULT_CONTEXT_OVERLAP_LINES: 5,
38
+
39
+ // 每段最大Token数
40
+ DEFAULT_MAX_TOKENS_PER_SEGMENT: 3000,
41
+
42
+ // Token估算比例
43
+ DEFAULT_TOKEN_RATIO: 4,
44
+
45
+ // 摘要最大长度
46
+ DEFAULT_MAX_SUMMARY_LENGTH: 500
47
+ };
48
+
49
+ // 文件权限相关常量
50
+ export const FILE_PERMISSIONS = {
51
+ EXECUTABLE: '755'
52
+ };
53
+
54
+ // 批处理相关常量
55
+ export const BATCH_CONSTANTS = {
56
+ DEFAULT_MAX_REQUEST_TOKENS: 4000,
57
+ TOKEN_RATIO: 4,
58
+ CHUNK_OVERLAP_LINES: 5,
59
+ MIN_FILES_PER_BATCH: 3,
60
+ MAX_FILES_PER_BATCH: 50,
61
+ UTILIZATION_THRESHOLD: 0.9,
62
+ BUFFER_SIZE: 100,
63
+ SNIPPET_LENGTH: 1000,
64
+ CHINESE_CHAR_MULTIPLIER: 1.5,
65
+ CODE_STRUCTURE_BONUS: 0.1,
66
+
67
+ // 利用率计算相关
68
+ UTILIZATION_PERCENTAGE_MULTIPLIER: 100,
69
+ VALUE_CALCULATION_MULTIPLIER: 1000,
70
+
71
+ // 缓冲区大小
72
+ MAX_BUFFER_SIZE: 10 * 1024 * 1024, // 10MB
73
+
74
+ // 代码片段长度限制
75
+ MAX_SNIPPET_LENGTH: 100,
76
+ MAX_SNIPPET_PART_LENGTH: 200,
77
+
78
+ // 对象池大小限制
79
+ MAX_BATCH_POOL_SIZE: 20,
80
+ MAX_ITEM_POOL_SIZE: 100,
81
+ MAX_CHUNK_POOL_SIZE: 50,
82
+ MAX_STATS_POOL_SIZE: 20,
83
+
84
+ // 对象池初始化大小
85
+ INITIAL_BATCH_POOL_SIZE: 10,
86
+ INITIAL_ITEM_POOL_SIZE: 50,
87
+ INITIAL_CHUNK_POOL_SIZE: 20,
88
+ INITIAL_STATS_POOL_SIZE: 10,
89
+
90
+ // 缓存大小限制
91
+ MAX_TOKEN_CACHE_SIZE: 1000,
92
+ MAX_REGEX_CACHE_SIZE: 1000,
93
+ MAX_COMMENT_RANGE_CACHE_SIZE: 500,
94
+ MAX_DISABLE_RANGE_CACHE_SIZE: 500,
95
+
96
+ // 循环限制
97
+ MAX_DIRECTORY_SEARCH_DEPTH: 20,
98
+
99
+ // 文件大小相关
100
+ FILE_SIZE_KB_THRESHOLD: 1024, // 1MB
101
+ CACHE_KEY_PREFIX_LENGTH: 100
102
+ };
103
+
104
+ // HTTP 状态码相关常量
105
+ export const HTTP_STATUS = {
106
+ TOO_MANY_REQUESTS: 429,
107
+ SERVER_ERROR_START: 500,
108
+ SERVER_ERROR_END: 600
109
+ };
110
+
111
+ // 默认配置值
112
+ export const DEFAULT_CONFIG = {
113
+ MAX_RESPONSE_TOKENS: 4096,
114
+ MAX_FILE_SIZE_KB: 100,
115
+ MAX_REQUEST_TOKENS: 8000,
116
+ TEMPERATURE: 0.1
117
+ };