@@ -224,6 +224,124 @@ class Deepseek extends Component {
224224 return response . choices [ 0 ] . message . content || '' ;
225225 }
226226
227+ /**
228+ * 流式处理对话消息
229+ * @param messages 对话消息数组
230+ * @param options 可选的调用选项,包含临时工具列表
231+ * @returns 生成的流式响应消息异步生成器
232+ */
233+ async * streamChatCompletion (
234+ messages : Message [ ] ,
235+ options ?: { temporaryTools ?: unknown [ ] }
236+ ) : AsyncGenerator < Partial < Message > > {
237+ // 转换消息格式 (复用现有逻辑)
238+ const formattedMessages : Array < OpenAI . Chat . ChatCompletionMessageParam > = [ ] ;
239+
240+ // 添加系统提示
241+ if ( this . systemPrompt ) {
242+ formattedMessages . push ( {
243+ role : 'system' ,
244+ content : this . systemPrompt ,
245+ } ) ;
246+ }
247+
248+ // 添加用户提供的消息
249+ formattedMessages . push (
250+ ...messages . map ( msg => {
251+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
252+ const formattedMsg : any = {
253+ role : msg . role ,
254+ content : msg . content ,
255+ } ;
256+
257+ if ( msg . role === 'tool' && msg . tool_call_id ) {
258+ formattedMsg . tool_call_id = msg . tool_call_id ;
259+ }
260+
261+ return formattedMsg ;
262+ } )
263+ ) ;
264+
265+ // 创建请求参数
266+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
267+ const requestParams : any = {
268+ model : this . model ,
269+ messages : formattedMessages ,
270+ temperature : this . temperature ,
271+ max_tokens : this . maxTokens ,
272+ top_p : this . topP ,
273+ stream : true , // 开启流式模式
274+ } ;
275+
276+ // 决定使用哪些工具
277+ const toolsToUse = options ?. temporaryTools || this . tools ;
278+
279+ // 如果有工具定义,添加到请求中
280+ if ( toolsToUse && toolsToUse . length > 0 ) {
281+ const formattedTools = toolsToUse . map ( tool => {
282+ type ToolType = {
283+ type ?: string ;
284+ name ?: string ;
285+ description ?: string ;
286+ parameters ?: Record < string , unknown > ;
287+ } ;
288+
289+ const typedTool = tool as ToolType ;
290+
291+ if ( typedTool . type === 'function' ) {
292+ return tool ;
293+ }
294+
295+ return {
296+ type : 'function' ,
297+ function : {
298+ name : typedTool . name ,
299+ description : typedTool . description ,
300+ parameters : typedTool . parameters || { } ,
301+ } ,
302+ } ;
303+ } ) ;
304+
305+ requestParams . tools = formattedTools ;
306+ }
307+
308+ // 调用流式 API
309+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
310+ const stream = ( await this . client . chat . completions . create ( requestParams ) ) as any ;
311+
312+ // 处理流式响应
313+ for await ( const chunk of stream ) {
314+ const delta = chunk . choices [ 0 ] ?. delta ;
315+
316+ if ( delta ?. content ) {
317+ // 流式文本内容
318+ yield {
319+ role : 'assistant' ,
320+ content : delta . content ,
321+ } ;
322+ }
323+
324+ if ( delta ?. tool_calls ) {
325+ // 流式工具调用(如果需要)
326+ yield {
327+ role : 'assistant' ,
328+ content : '' ,
329+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
330+ tool_calls : delta . tool_calls . map ( ( toolCall : any ) => ( {
331+ id : toolCall . id || '' ,
332+ function : {
333+ name : toolCall . function ?. name || '' ,
334+ arguments : toolCall . function ?. arguments || '' ,
335+ } ,
336+ type : 'function' ,
337+ tool_name : toolCall . function ?. name || '' ,
338+ arguments : JSON . parse ( toolCall . function ?. arguments || '{}' ) ,
339+ } ) ) ,
340+ } ;
341+ }
342+ }
343+ }
344+
227345 /**
228346 * 处理对话消息
229347 * @param messages 对话消息数组
0 commit comments