Skip to content

Commit decc555

Browse files
committed
refactor(ai-completion): Restructure stop sequences and context handling
1 parent 7e25b0a commit decc555

2 files changed

Lines changed: 47 additions & 65 deletions

File tree

packages/plugins/script/src/ai-completion/constants.js

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -117,54 +117,28 @@ export const MODEL_COMMON_CONFIG = {
117117
}
118118
}
119119

120-
/**
121-
* 通用停止符配置(JS/TS)
122-
*/
123-
export const STOP_SEQUENCES = [
124-
// 通用停止符
125-
'\n\n',
126-
'```',
127-
128-
// JS/TS 语言特性
129-
'\nfunction ',
130-
'\nclass ',
131-
'\nconst ',
132-
'\nlet ',
133-
'\nvar ',
134-
'\nexport ',
135-
'\nimport ',
136-
'\ninterface ',
137-
'\ntype ',
138-
'\nenum ',
139-
140-
// 注释边界
141-
'\n//',
142-
'\n/*',
143-
144-
// 代码块边界
145-
'\n}',
146-
'\n};'
147-
]
120+
// 停止符配置(API 限制:最多 16 个)
121+
export const STOP_SEQUENCES = {
122+
CORE: ['\n\n', '```'],
123+
NEW_SCOPE: ['\nfunction ', '\nclass ', '\nexport ', '\nimport '],
124+
BLOCK_END: ['\n}', '\n};']
125+
}
148126

149-
/**
150-
* FIM (Fill-In-the-Middle) 配置
151-
*/
127+
// 上下文特定停止符
128+
export const CONTEXT_STOP_SEQUENCES = {
129+
EXPRESSION: [';', ',', '\n)'],
130+
COMMENT: ['\n\n', '*/'],
131+
OBJECT: ['\n}', '\n};'],
132+
FUNCTION: ['\n}', '\nfunction ', '\nreturn ']
133+
}
134+
135+
// FIM 标记配置
152136
export const FIM_CONFIG = {
153137
MARKERS: {
154138
PREFIX: '<|fim_prefix|>',
155139
SUFFIX: '<|fim_suffix|>',
156140
MIDDLE: '<|fim_middle|>',
157141
CURSOR: '[CURSOR]'
158-
},
159-
160-
// FIM 专用停止符(会与 STOP_SEQUENCES 合并)
161-
FIM_MARKERS_STOPS: ['<|fim_prefix|>', '<|fim_suffix|>', '<|fim_middle|>'],
162-
163-
// 上下文特定的额外停止符
164-
CONTEXT_STOPS: {
165-
EXPRESSION: [';', '\n)', ','],
166-
STATEMENT: [], // 使用通用停止符即可
167-
OBJECT: [] // 使用通用停止符即可
168142
}
169143
}
170144

packages/plugins/script/src/ai-completion/utils/modelUtils.js

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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'
22

33
/**
44
* 检测模型类型
@@ -46,33 +46,41 @@ export function calculateTokens(cursorContext) {
4646
return limits.DEFAULT
4747
}
4848

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 = []
5852

59-
// Qwen 模型添加 FIM 标记
60-
if (modelType === 'qwen') {
61-
stops.push(...FIM_CONFIG.FIM_MARKERS_STOPS)
62-
}
53+
// 核心停止符
54+
stops.push(...STOP_SEQUENCES.CORE)
6355

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)
6675
}
6776

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)
7583
}
7684

77-
return stops
85+
return uniqueStops
7886
}

0 commit comments

Comments
 (0)