1
1
import OpenAI from "openai" ;
2
2
import { ChatCompletionMessageParam } from "openai/resources" ;
3
- import { AIProvider , AIRequestParams , AIResponse , AIModel } from "../types" ;
4
- import { generateWithRetry , getSystemPrompt } from "../utils/generateHelper" ;
3
+ import {
4
+ AIProvider ,
5
+ AIRequestParams ,
6
+ AIResponse ,
7
+ AIModel ,
8
+ type CodeReviewResult ,
9
+ } from "../types" ;
10
+ import {
11
+ generateWithRetry ,
12
+ getCodeReviewPrompt ,
13
+ getSystemPrompt ,
14
+ } from "../utils/generateHelper" ;
5
15
import { LocalizationManager } from "../../utils/LocalizationManager" ;
6
16
import { getWeeklyReportPrompt } from "../../prompt/weeklyReport" ;
17
+ import { CodeReviewReportGenerator } from "../../utils/CodeReviewReportGenerator" ;
7
18
19
+ /**
20
+ * OpenAI提供者配置项接口
21
+ */
8
22
export interface OpenAIProviderConfig {
23
+ /** OpenAI API密钥 */
9
24
apiKey : string ;
25
+ /** API基础URL,对于非官方OpenAI端点可自定义 */
10
26
baseURL ?: string ;
27
+ /** API版本号 */
11
28
apiVersion ?: string ;
29
+ /** 提供者唯一标识符 */
12
30
providerId : string ;
31
+ /** 提供者显示名称 */
13
32
providerName : string ;
33
+ /** 默认使用的模型ID */
14
34
defaultModel ?: string ;
35
+ /** 支持的模型列表 */
15
36
models : AIModel [ ] ;
16
37
}
17
38
39
+ /**
40
+ * OpenAI API基础提供者抽象类
41
+ * 实现了OpenAI API的基本功能,可被具体提供者继承和扩展
42
+ */
18
43
export abstract class BaseOpenAIProvider implements AIProvider {
44
+ /** OpenAI API客户端实例 */
19
45
protected openai : OpenAI ;
46
+ /** 提供者配置信息 */
20
47
protected config : OpenAIProviderConfig ;
48
+ /** 提供者标识信息 */
21
49
protected provider : { id : string ; name : string } ;
22
50
51
+ /**
52
+ * 创建基础OpenAI提供者实例
53
+ * @param config - 提供者配置对象
54
+ */
23
55
constructor ( config : OpenAIProviderConfig ) {
24
56
this . config = config ;
25
57
this . provider = {
@@ -29,6 +61,11 @@ export abstract class BaseOpenAIProvider implements AIProvider {
29
61
this . openai = this . createClient ( ) ;
30
62
}
31
63
64
+ /**
65
+ * 创建OpenAI API客户端
66
+ * @returns OpenAI客户端实例
67
+ * @protected
68
+ */
32
69
protected createClient ( ) : OpenAI {
33
70
const config : any = {
34
71
apiKey : this . config . apiKey ,
@@ -41,11 +78,17 @@ export abstract class BaseOpenAIProvider implements AIProvider {
41
78
config . defaultHeaders = { "api-key" : this . config . apiKey } ;
42
79
}
43
80
}
44
- console . log ( "config" , config ) ;
45
81
46
82
return new OpenAI ( config ) ;
47
83
}
48
84
85
+ /**
86
+ * 生成AI回复内容
87
+ * 使用重试机制处理可能的失败情况
88
+ *
89
+ * @param params - AI请求参数
90
+ * @returns 包含生成内容和使用统计的Promise
91
+ */
49
92
async generateResponse ( params : AIRequestParams ) : Promise < AIResponse > {
50
93
return generateWithRetry (
51
94
params ,
@@ -85,12 +128,92 @@ export abstract class BaseOpenAIProvider implements AIProvider {
85
128
) ;
86
129
}
87
130
131
+ /**
132
+ * 生成代码评审报告
133
+ * 将diff内容转换为结构化的评审结果
134
+ *
135
+ * @param params - 代码评审请求参数
136
+ * @returns 包含评审报告的Promise
137
+ * @throws 如果AI响应解析失败或生成过程出错
138
+ */
139
+ async generateCodeReview ( params : AIRequestParams ) : Promise < AIResponse > {
140
+ return generateWithRetry (
141
+ params ,
142
+ async ( truncatedInput ) => {
143
+ const messages : ChatCompletionMessageParam [ ] = [
144
+ {
145
+ role : "system" ,
146
+ content : getCodeReviewPrompt ( params ) ,
147
+ } ,
148
+ {
149
+ role : "user" ,
150
+ content : truncatedInput ,
151
+ } ,
152
+ ] ;
153
+
154
+ try {
155
+ const completion = await this . openai . chat . completions . create ( {
156
+ model :
157
+ ( params . model && params . model . id ) ||
158
+ this . config . defaultModel ||
159
+ "gpt-3.5-turbo" ,
160
+ messages,
161
+ temperature : 0.3 ,
162
+ } ) ;
163
+
164
+ const responseContent = completion . choices [ 0 ] ?. message ?. content ;
165
+ if ( ! responseContent ) {
166
+ throw new Error ( "No response content from AI" ) ;
167
+ }
168
+
169
+ let reviewResult : CodeReviewResult ;
170
+ try {
171
+ reviewResult = JSON . parse ( responseContent ) ;
172
+ } catch ( e ) {
173
+ throw new Error (
174
+ `Failed to parse AI response as JSON: ${
175
+ e instanceof Error ? e . message : String ( e )
176
+ } `
177
+ ) ;
178
+ }
179
+ return {
180
+ content :
181
+ CodeReviewReportGenerator . generateMarkdownReport ( reviewResult ) ,
182
+ usage : {
183
+ promptTokens : completion . usage ?. prompt_tokens ,
184
+ completionTokens : completion . usage ?. completion_tokens ,
185
+ totalTokens : completion . usage ?. total_tokens ,
186
+ } ,
187
+ } ;
188
+ } catch ( error ) {
189
+ const message = LocalizationManager . getInstance ( ) . format (
190
+ "codeReview.generation.failed" ,
191
+ error instanceof Error ? error . message : String ( error )
192
+ ) ;
193
+ throw new Error ( message ) ;
194
+ }
195
+ } ,
196
+ {
197
+ initialMaxLength : params . model ?. maxTokens ?. input || 16385 ,
198
+ provider : this . getId ( ) ,
199
+ }
200
+ ) ;
201
+ }
202
+
203
+ /**
204
+ * 基于提交记录生成周报
205
+ * 总结一段时间内的代码提交活动
206
+ *
207
+ * @param commits - 提交记录数组
208
+ * @param model - 可选的指定模型
209
+ * @returns 包含周报内容的Promise
210
+ * @throws 如果生成失败会抛出本地化的错误信息
211
+ */
88
212
async generateWeeklyReport (
89
213
commits : string [ ] ,
90
214
model ?: AIModel
91
215
) : Promise < AIResponse > {
92
216
try {
93
- console . log ( "commits" , commits ) ;
94
217
const response = await this . openai . chat . completions . create ( {
95
218
model : model ?. id || this . config . defaultModel || "gpt-3.5-turbo" ,
96
219
messages : [
@@ -104,7 +227,6 @@ export abstract class BaseOpenAIProvider implements AIProvider {
104
227
} ,
105
228
] ,
106
229
} ) ;
107
- console . log ( "response" , response ) ;
108
230
return {
109
231
content : response . choices [ 0 ] ?. message ?. content || "" ,
110
232
usage : {
@@ -123,11 +245,16 @@ export abstract class BaseOpenAIProvider implements AIProvider {
123
245
}
124
246
}
125
247
248
+ /**
249
+ * 获取当前支持的AI模型列表
250
+ * 优先从API获取,如果失败则返回配置的静态列表
251
+ *
252
+ * @returns Promise<AIModel[]> 支持的模型配置数组
253
+ */
126
254
async getModels ( ) : Promise < AIModel [ ] | any [ ] > {
127
255
try {
128
256
const response = await this . openai . models . list ( ) ;
129
257
return response . data . map ( ( model : any ) => {
130
- console . log ( "model" , model ) ;
131
258
return {
132
259
id : model . id ,
133
260
name : model . id ,
@@ -144,18 +271,32 @@ export abstract class BaseOpenAIProvider implements AIProvider {
144
271
}
145
272
}
146
273
274
+ /**
275
+ * 刷新并返回可用的模型ID列表
276
+ * @returns Promise<string[]> 模型ID数组
277
+ */
147
278
async refreshModels ( ) : Promise < string [ ] > {
148
279
const models = await this . getModels ( ) ;
149
280
return models . map ( ( m ) => m . id ) ;
150
281
}
151
282
283
+ /**
284
+ * 获取提供者显示名称
285
+ */
152
286
getName ( ) : string {
153
287
return this . provider . name ;
154
288
}
155
289
290
+ /**
291
+ * 获取提供者唯一标识符
292
+ */
156
293
getId ( ) : string {
157
294
return this . provider . id ;
158
295
}
159
296
297
+ /**
298
+ * 检查服务是否可用的抽象方法
299
+ * 需要由具体提供者实现
300
+ */
160
301
abstract isAvailable ( ) : Promise < boolean > ;
161
302
}
0 commit comments