@@ -42,51 +42,38 @@ public async Task GenerateCommitMessage(string repo, string changeList, Action<s
4242
4343 do
4444 {
45+ ChatCompletion completion = await chatClient . CompleteChatAsync ( messages , options , cancellation ) ;
4546 var inProgress = false ;
46- var updates = chatClient . CompleteChatStreamingAsync ( messages , options ) . WithCancellation ( cancellation ) ;
47- var toolCalls = new ToolCallsBuilder ( ) ;
48- var contentBuilder = new StringBuilder ( ) ;
4947
50- await foreach ( var update in updates )
48+ switch ( completion . FinishReason )
5149 {
52- foreach ( var contentPart in update . ContentUpdate )
53- contentBuilder . Append ( contentPart . Text ) ;
50+ case ChatFinishReason . Stop :
51+ onUpdate ? . Invoke ( string . Empty ) ;
52+ onUpdate ? . Invoke ( "[Assistant]:" ) ;
53+ if ( completion . Content . Count > 0 )
54+ onUpdate ? . Invoke ( completion . Content [ 0 ] . Text ) ;
55+ else
56+ onUpdate ? . Invoke ( "[No content was generated.]" ) ;
57+ break ;
58+ case ChatFinishReason . Length :
59+ throw new Exception ( "The response was cut off because it reached the maximum length. Consider increasing the max tokens limit." ) ;
60+ case ChatFinishReason . ToolCalls :
61+ {
62+ messages . Add ( new AssistantChatMessage ( completion ) ) ;
5463
55- foreach ( var toolCall in update . ToolCallUpdates )
56- toolCalls . Append ( toolCall ) ;
57-
58- switch ( update . FinishReason )
59- {
60- case ChatFinishReason . Stop :
61- onUpdate ? . Invoke ( string . Empty ) ;
62- onUpdate ? . Invoke ( "[Assistant]:" ) ;
63- onUpdate ? . Invoke ( contentBuilder . ToString ( ) ) ;
64- break ;
65- case ChatFinishReason . Length :
66- throw new Exception ( "The response was cut off because it reached the maximum length. Consider increasing the max tokens limit." ) ;
67- case ChatFinishReason . ToolCalls :
64+ foreach ( var call in completion . ToolCalls )
6865 {
69- var calls = toolCalls . Build ( ) ;
70- var assistantMessage = new AssistantChatMessage ( calls ) ;
71- if ( contentBuilder . Length > 0 )
72- assistantMessage . Content . Add ( ChatMessageContentPart . CreateTextPart ( contentBuilder . ToString ( ) ) ) ;
73- messages . Add ( assistantMessage ) ;
74-
75- foreach ( var call in calls )
76- {
77- var result = await ChatTools . Process ( call , onUpdate ) ;
78- messages . Add ( result ) ;
79- }
80-
81- inProgress = true ;
82- break ;
66+ var result = await ChatTools . Process ( call , onUpdate ) ;
67+ messages . Add ( result ) ;
8368 }
84- case ChatFinishReason . ContentFilter :
85- throw new Exception ( "Ommitted content due to a content filter flag" ) ;
86- default :
87- break ;
88- }
8969
70+ inProgress = true ;
71+ break ;
72+ }
73+ case ChatFinishReason . ContentFilter :
74+ throw new Exception ( "Ommitted content due to a content filter flag" ) ;
75+ default :
76+ break ;
9077 }
9178
9279 if ( ! inProgress )
0 commit comments