Skip to content

Commit 026faf2

Browse files
committed
refactor(ai-code-assistant): Restructure module organization and consolidate configurations
1 parent 998161d commit 026faf2

35 files changed

Lines changed: 471 additions & 740 deletions

packages/plugins/script/src/Main.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { useHelp, useLayout, getMergeMeta } from '@opentiny/tiny-engine-meta-reg
3939
import { initCompletion } from '@opentiny/tiny-engine-common/js/completion'
4040
import { initLinter } from '@opentiny/tiny-engine-common/js/linter'
4141
import { initAICodeAssistant } from './ai-code-assistant/index'
42-
import type { AICodeAssistant } from './ai-code-assistant/types/index'
42+
import type { AICodeAssistant } from './ai-code-assistant/types'
4343
import useMethod, { saveMethod, highlightMethod, getMethodNameList, getMethods } from './js/method'
4444
4545
export const api = {

packages/plugins/script/src/ai-code-assistant/adapters/DeepSeekAdapter.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

packages/plugins/script/src/ai-code-assistant/adapters/constants.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

packages/plugins/script/src/ai-code-assistant/adapters/ModelAdapter.ts renamed to packages/plugins/script/src/ai-code-assistant/api/ModelAdapter.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
*/
55

66
import { getMetaApi, META_SERVICE } from '@opentiny/tiny-engine-meta-register'
7-
import { FIMPromptBuilder } from '../prompts/fim/builder'
8-
import { buildNESUserPrompt, parseNESResponse } from '../prompts/nes/builder'
9-
import { NES_SYSTEM_PROMPT } from '../prompts/nes/systemPrompt'
10-
import { cleanCompletion, buildLowcodeMetadata } from '../utils/CompletionUtils.ts'
11-
import { callQwenChat } from './QwenAdapter.ts'
12-
import { QWEN_CONFIG, FIXED_MODEL_CONFIG, MODEL_CONFIG } from './constants.ts'
7+
import { FIMPromptBuilder, FIM_SYSTEM_PROMPT } from '../prompts/fim'
8+
import { buildNESUserPrompt, parseNESResponse } from '../prompts/nes-builder'
9+
import { NES_SYSTEM_PROMPT } from '../prompts/nes-system'
10+
import { buildLowcodeMetadata, cleanCompletion } from '../utils/code'
11+
import { callQwenChat } from './QwenClient'
12+
import { QWEN_CONFIG, FIXED_MODEL_CONFIG, MODEL_CONFIG } from '../config'
1313

1414
/**
1515
* 统一的模型适配器(固定使用 Qwen)
@@ -71,8 +71,6 @@ export class ModelAdapter {
7171
fimMetadata
7272
)
7373

74-
// 获取 System Prompt 和 Instruction
75-
const { FIM_SYSTEM_PROMPT } = await import('../prompts/fim/systemPrompt')
7674
const instruction = this.fimBuilder.selectInstruction(cursorContext, fimMetadata.language || 'javascript')
7775

7876
// 使用 Chat API(支持 System Prompt)

packages/plugins/script/src/ai-code-assistant/adapters/QwenAdapter.ts renamed to packages/plugins/script/src/ai-code-assistant/api/QwenClient.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Qwen API Adapter
33
*/
44

5-
import { QWEN_CONFIG, HTTP_CONFIG, ERROR_MESSAGES } from './constants'
5+
import { QWEN_CONFIG, HTTP_CONFIG, ERROR_MESSAGES } from '../config'
66

77
/**
88
* Qwen FIM API (Completion)
@@ -14,11 +14,11 @@ export async function callQwenFIM(fimPrompt: string, config: any, apiKey: string
1414
model: config.model,
1515
prompt: fimPrompt,
1616
max_tokens: config.maxTokens,
17-
temperature: config.temperature || QWEN_CONFIG.DEFAULT_TEMPERATURE,
18-
top_p: config.top_p || QWEN_CONFIG.TOP_P,
17+
temperature: config.temperature ?? QWEN_CONFIG.DEFAULT_TEMPERATURE,
18+
top_p: config.top_p ?? QWEN_CONFIG.TOP_P,
1919
stream: HTTP_CONFIG.STREAM,
2020
stop: config.stopSequences,
21-
presence_penalty: QWEN_CONFIG.PRESENCE_PENALTY
21+
presence_penalty: config.presence_penalty ?? QWEN_CONFIG.PRESENCE_PENALTY
2222
}
2323

2424
const response = await fetch(completionsUrl, {
@@ -49,8 +49,8 @@ export async function callQwenChat(messages: any[], config: any, apiKey: string,
4949
model: config.model,
5050
messages,
5151
max_tokens: config.maxTokens,
52-
temperature: config.temperature || QWEN_CONFIG.DEFAULT_TEMPERATURE,
53-
top_p: config.top_p || QWEN_CONFIG.TOP_P,
52+
temperature: config.temperature ?? QWEN_CONFIG.DEFAULT_TEMPERATURE,
53+
top_p: config.top_p ?? QWEN_CONFIG.TOP_P,
5454
stream: HTTP_CONFIG.STREAM
5555
}
5656

packages/plugins/script/src/ai-code-assistant/config.ts

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/**
2-
* AI Code Assistant - 配置文件
2+
* AI Code Assistant - 统一配置文件
3+
* 合并所有常量配置
34
*/
45

6+
// ==================== 运行时配置 ====================
7+
58
export const DEFAULT_CONFIG = {
69
fim: {
710
enabled: true,
@@ -28,3 +31,138 @@ export const WINDOW_CONFIG = {
2831
MAX_PREDICTIONS: 5,
2932
MAX_EDIT_HISTORY: 10
3033
}
34+
35+
// ==================== 模型配置 ====================
36+
37+
/**
38+
* 固定使用 Qwen 模型配置(与 server 对齐)
39+
*/
40+
export const FIXED_MODEL_CONFIG = {
41+
FIM: {
42+
MODEL: 'qwen2.5-coder-32b-instruct',
43+
API_URL: 'https://dashscope.aliyuncs.com/compatible-mode/v1/completions',
44+
MAX_TOKENS: 48,
45+
TEMPERATURE: 0,
46+
TOP_P: 0.95,
47+
PRESENCE_PENALTY: 0 // FIM 使用 0
48+
},
49+
NES: {
50+
MODEL: 'qwen3-coder-plus',
51+
API_URL: 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions',
52+
MAX_TOKENS: 1024,
53+
TEMPERATURE: 0.1,
54+
TOP_P: 0.95,
55+
PRESENCE_PENALTY: 0.2 // Chat 使用 0.2
56+
}
57+
}
58+
59+
export const QWEN_CONFIG = {
60+
COMPLETION_PATH: '/completions',
61+
CHAT_PATH: '/chat/completions',
62+
DEFAULT_TEMPERATURE: 0,
63+
TOP_P: 0.95,
64+
PRESENCE_PENALTY: 0,
65+
FIM: {
66+
MAX_PREFIX_LINES: 100,
67+
MAX_SUFFIX_LINES: 50
68+
}
69+
}
70+
71+
export const DEEPSEEK_CONFIG = {
72+
COMPLETION_PATH: '/beta',
73+
PATH_REPLACE: '/v1',
74+
DEFAULT_TEMPERATURE: 0,
75+
TOP_P: 1.0,
76+
FIM: {
77+
MAX_PREFIX_LINES: 100,
78+
MAX_SUFFIX_LINES: 50,
79+
MAX_TOKENS: 4096
80+
}
81+
}
82+
83+
export const MODEL_CONFIG = {
84+
QWEN: {
85+
TYPE: 'qwen',
86+
KEYWORDS: ['qwen']
87+
},
88+
DEEPSEEK: {
89+
TYPE: 'deepseek',
90+
KEYWORDS: ['deepseek']
91+
},
92+
UNKNOWN: {
93+
TYPE: 'unknown',
94+
KEYWORDS: []
95+
}
96+
}
97+
98+
export const HTTP_CONFIG = {
99+
METHOD: 'POST',
100+
CONTENT_TYPE: 'application/json',
101+
STREAM: false
102+
}
103+
104+
export const ERROR_MESSAGES = {
105+
CONFIG_MISSING: 'AI 配置未设置(缺少 model/apiKey/baseUrl)',
106+
NO_COMPLETION: '未收到有效的补全结果',
107+
REQUEST_FAILED: '请求失败',
108+
QWEN_API_ERROR: 'Qwen API 错误'
109+
}
110+
111+
// ==================== 提示词配置 ====================
112+
113+
/**
114+
* FIM 标记配置(与 server 对齐)
115+
*/
116+
export const FIM_CONFIG = {
117+
MARKERS: {
118+
// 标准 FIM 格式(与 server 一致)
119+
PREFIX: '<|fim_prefix|>',
120+
SUFFIX: '<|fim_suffix|>',
121+
MIDDLE: '<|fim_middle|>',
122+
// 通用标记
123+
CURSOR: '[CURSOR]'
124+
}
125+
}
126+
127+
/**
128+
* FIM 停止符配置(限制 16 个以内)
129+
* 用于 Qwen FIM Completions API
130+
*/
131+
export const FIM_STOP_SEQUENCES = [
132+
' {',
133+
'\n{',
134+
';',
135+
'\n\n',
136+
'\n}',
137+
'\nfunction ',
138+
'\nclass ',
139+
'\nconst ',
140+
'\nlet ',
141+
'\nexport ',
142+
'\nimport ',
143+
'\n//',
144+
'```'
145+
] // 总计: 14 个停止符
146+
147+
/**
148+
* Chat API 停止符配置
149+
* 用于 Qwen Chat API(NES 预测不需要停止符)
150+
*/
151+
export const CHAT_STOP_SEQUENCES: string[] = []
152+
153+
/**
154+
* 代码上下文分析配置
155+
*/
156+
export const CONTEXT_CONFIG = {
157+
MAX_LINES_TO_SCAN: 20
158+
}
159+
160+
/**
161+
* 代码模式匹配(JS/TS)
162+
*/
163+
export const CODE_PATTERNS = {
164+
FUNCTION: /function\s+(\w+)|const\s+(\w+)\s*=.*=>|(\w+)\s*\([^)]*\)\s*{/,
165+
CLASS: /class\s+(\w+)/,
166+
INTERFACE: /interface\s+(\w+)/,
167+
TYPE: /type\s+(\w+)/
168+
}

packages/plugins/script/src/ai-code-assistant/managers/EditDispatcher.ts renamed to packages/plugins/script/src/ai-code-assistant/core/EditDispatcher.ts

File renamed without changes.

packages/plugins/script/src/ai-code-assistant/managers/EditHistoryManager.ts renamed to packages/plugins/script/src/ai-code-assistant/core/EditHistoryManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import type * as monaco from 'monaco-editor'
6-
import type { EditRecord } from '../types/index'
6+
import type { EditRecord } from '../types'
77

88
export class EditHistoryManager {
99
private editHistory: EditRecord[] = []

packages/plugins/script/src/ai-code-assistant/fim/FIMEngine.ts renamed to packages/plugins/script/src/ai-code-assistant/core/FIMEngine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import * as monaco from 'monaco-editor'
6-
import { ModelAdapter } from '../adapters/ModelAdapter'
6+
import { ModelAdapter } from '../api/ModelAdapter'
77

88
export class FIMEngine {
99
private disposable: monaco.IDisposable | null = null

packages/plugins/script/src/ai-code-assistant/nes/NESEngine.ts renamed to packages/plugins/script/src/ai-code-assistant/core/NESEngine.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
*/
55

66
import * as monaco from 'monaco-editor'
7-
import type { EditRecord, Prediction, NESConfig } from '../types/index'
8-
import { SymptomDetector } from '../managers/SymptomDetector'
9-
import { SuggestionQueue } from './SuggestionQueue'
10-
import { NESRenderer } from './NESRenderer'
11-
import { DiffCalculator } from '../utils/DiffCalculator'
12-
import { ModelAdapter } from '../adapters/ModelAdapter'
7+
import type { EditRecord, Prediction, NESConfig } from '../types'
8+
import { SymptomDetector } from '../utils/symptom'
9+
import { SuggestionQueue } from '../ui/SuggestionQueue'
10+
import { NESRenderer } from '../ui/NESRenderer'
11+
import { DiffCalculator } from '../utils/diff'
12+
import { ModelAdapter } from '../api/ModelAdapter'
1313

1414
export class NESEngine {
1515
private state: 'SLEEPING' | 'DIAGNOSING' | 'SUGGESTING' = 'SLEEPING'

0 commit comments

Comments
 (0)