@@ -15,10 +15,15 @@ export class AiService {
1515
1616 /**
1717 * 执行生成策略:
18- * 1. 发起快速请求 (V3) 生成 "Concise" (简短) + "Conventional" (规范) 风格 。
19- * 2. 数据可用时立即调用 onUpdate 。
18+ * 1. 发起快速请求 (V3) 生成结果 。
19+ * 2. 注入 Project Meta 和 Recent Commits 上下文 。
2020 */
21- async generate ( diff : string , onUpdate : ( options : CommitOption [ ] ) => void ) {
21+ async generate (
22+ diff : string ,
23+ projectMeta : string ,
24+ recentCommits : string [ ] ,
25+ onUpdate : ( options : CommitOption [ ] ) => void
26+ ) {
2227 const apiKey = this . config . get < string > ( "apiKey" ) ;
2328 const baseUrl = this . config . get < string > ( "baseUrl" ) ;
2429 const fastModel = this . config . get < string > ( "model" ) || "deepseek-chat" ;
@@ -28,21 +33,38 @@ export class AiService {
2833 }
2934
3035 try {
36+ // 准备 System Prompt 注入
37+ let systemPrompt = FAST_PROMPT ;
38+
39+ // 注入 Project Meta
40+ systemPrompt = systemPrompt . replace ( "{{PROJECT_META}}" , projectMeta || "(None)" ) ;
41+
42+ // 注入 Recent Commits
43+ const commitsStr = recentCommits . length > 0
44+ ? recentCommits . map ( c => `- ${ c } ` ) . join ( "\n" )
45+ : "(None)" ;
46+ systemPrompt = systemPrompt . replace ( "{{RECENT_COMMITS}}" , commitsStr ) ;
47+
3148 const options = await this . fetchFastOptions (
3249 baseUrl ! ,
3350 apiKey ,
3451 fastModel ,
52+ systemPrompt ,
3553 diff
3654 ) ;
55+
56+ this . sortOptions ( options ) ;
3757 onUpdate ( options ) ;
3858 } catch ( err ) {
3959 Logger . error ( "快速模型 (Fast Model) 请求失败" , err ) ;
4060 console . error ( "快速模型失败:" , err ) ;
61+ throw err ; // Re-throw to let upstream handle UI error
4162 }
4263 }
4364
4465 private sortOptions ( options : CommitOption [ ] ) {
45- const order = [ "Emoji" , "Conventional" , "StandardShort" , "Smart" ] ;
66+ // Define sort order
67+ const order = [ "Emoji" , "StandardShort" , "Conventional" , "Historical" , "Smart" ] ;
4668 options . sort ( ( a , b ) => {
4769 let ia = order . indexOf ( a . type ) ;
4870 let ib = order . indexOf ( b . type ) ;
@@ -56,9 +78,10 @@ export class AiService {
5678 baseUrl : string ,
5779 apiKey : string ,
5880 model : string ,
81+ systemPrompt : string ,
5982 diff : string
6083 ) : Promise < CommitOption [ ] > {
61- return this . callApi ( baseUrl , apiKey , model , FAST_PROMPT , diff ) ;
84+ return this . callApi ( baseUrl , apiKey , model , systemPrompt , diff ) ;
6285 }
6386
6487 private async callApi (
@@ -77,10 +100,6 @@ export class AiService {
77100 stream : false ,
78101 } ;
79102
80- // [DEBUG] 打印完整请求参数
81- // console.log("--- [AI Commit Request Payload] ---");
82- // console.log(JSON.stringify(requestBody, null, 2));
83- // console.log("-----------------------------------");
84103 Logger . info ( `[AI 请求] 模型: ${ model } , 字符数: ${ userContent . length } ` ) ;
85104
86105 try {
@@ -105,8 +124,8 @@ export class AiService {
105124 return this . parseResponse ( content ) ;
106125 } catch ( error ) {
107126 Logger . error ( `API 调用异常: ${ model } ` , error ) ;
108- console . error ( error ) ;
109- return [ ] ; // Return empty on failure to not crash the whole Promise.all
127+ // Don't swallow here, let caller handle
128+ throw error ;
110129 }
111130 }
112131
@@ -120,13 +139,12 @@ export class AiService {
120139 if ( Array . isArray ( parsed ) ) {
121140 return parsed ;
122141 } else if ( typeof parsed === "object" ) {
123- // Should not happen with new prompts, but fallback
124142 return [ parsed ] ;
125143 }
126144 return [ ] ;
127145 } catch ( e ) {
128146 Logger . error ( "AI 响应解析失败" , e ) ;
129- console . error ( "Parse error" , content ) ;
147+ console . error ( "Parse error content: " , content ) ;
130148 return [ ] ;
131149 }
132150 }
0 commit comments