|
1 | | -import { MODEL_CONFIG, MODEL_COMMON_CONFIG, STOP_SEQUENCES, FIM_CONFIG } from '../constants.js' |
| 1 | +import { MODEL_CONFIG, MODEL_COMMON_CONFIG, STOP_SEQUENCES, CONTEXT_STOP_SEQUENCES } from '../constants.js' |
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * 检测模型类型 |
@@ -46,33 +46,41 @@ export function calculateTokens(cursorContext) { |
46 | 46 | return limits.DEFAULT |
47 | 47 | } |
48 | 48 |
|
49 | | -/** |
50 | | - * 获取动态停止符 |
51 | | - * @param {Object} cursorContext - 光标上下文 |
52 | | - * @param {string} modelType - 模型类型 |
53 | | - * @returns {string[]} 停止符数组 |
54 | | - */ |
55 | | -export function getStopSequences(cursorContext, modelType) { |
56 | | - // 基础停止符:通用停止符 |
57 | | - const stops = [...STOP_SEQUENCES] |
| 49 | +// 获取动态停止符(最多 16 个) |
| 50 | +export function getStopSequences(cursorContext, _modelType) { |
| 51 | + const stops = [] |
58 | 52 |
|
59 | | - // Qwen 模型添加 FIM 标记 |
60 | | - if (modelType === 'qwen') { |
61 | | - stops.push(...FIM_CONFIG.FIM_MARKERS_STOPS) |
62 | | - } |
| 53 | + // 核心停止符 |
| 54 | + stops.push(...STOP_SEQUENCES.CORE) |
63 | 55 |
|
64 | | - if (!cursorContext) { |
65 | | - return stops |
| 56 | + if (cursorContext) { |
| 57 | + if (cursorContext.inBlockComment || cursorContext.inLineComment) { |
| 58 | + stops.push(...CONTEXT_STOP_SEQUENCES.COMMENT) |
| 59 | + } else if (cursorContext.needsExpression) { |
| 60 | + stops.push(...CONTEXT_STOP_SEQUENCES.EXPRESSION) |
| 61 | + stops.push(...STOP_SEQUENCES.NEW_SCOPE) |
| 62 | + } else if (cursorContext.inObject) { |
| 63 | + stops.push(...CONTEXT_STOP_SEQUENCES.OBJECT) |
| 64 | + stops.push(...STOP_SEQUENCES.NEW_SCOPE) |
| 65 | + } else if (cursorContext.inFunction) { |
| 66 | + stops.push(...CONTEXT_STOP_SEQUENCES.FUNCTION) |
| 67 | + stops.push(...STOP_SEQUENCES.BLOCK_END) |
| 68 | + } else { |
| 69 | + stops.push(...STOP_SEQUENCES.NEW_SCOPE) |
| 70 | + stops.push(...STOP_SEQUENCES.BLOCK_END) |
| 71 | + } |
| 72 | + } else { |
| 73 | + stops.push(...STOP_SEQUENCES.NEW_SCOPE) |
| 74 | + stops.push(...STOP_SEQUENCES.BLOCK_END) |
66 | 75 | } |
67 | 76 |
|
68 | | - // 根据上下文添加特定停止符 |
69 | | - if (cursorContext.needsExpression) { |
70 | | - stops.push(...FIM_CONFIG.CONTEXT_STOPS.EXPRESSION) |
71 | | - } else if (cursorContext.needsStatement) { |
72 | | - stops.push(...FIM_CONFIG.CONTEXT_STOPS.STATEMENT) |
73 | | - } else if (cursorContext.inObject) { |
74 | | - stops.push(...FIM_CONFIG.CONTEXT_STOPS.OBJECT) |
| 77 | + const uniqueStops = [...new Set(stops)] |
| 78 | + |
| 79 | + if (uniqueStops.length > 16) { |
| 80 | + // eslint-disable-next-line no-console |
| 81 | + console.warn(`⚠️ 停止符超过限制: ${uniqueStops.length},截断到 16 个`) |
| 82 | + return uniqueStops.slice(0, 16) |
75 | 83 | } |
76 | 84 |
|
77 | | - return stops |
| 85 | + return uniqueStops |
78 | 86 | } |
0 commit comments