File tree Expand file tree Collapse file tree
src/BE/Services/Models/ChatServices/Anthropic Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -21,9 +21,26 @@ public override async IAsyncEnumerable<ChatSegment> ChatStreamed(ChatRequest req
2121 int toolCallIndex = - 1 ;
2222 await foreach ( RawMessageStreamEvent stream in anthropicClient . Messages . CreateStreaming ( message , cancellationToken ) )
2323 {
24- if ( stream . TryPickStart ( out RawMessageStartEvent ? start ) )
24+ string ? type = stream . Json . GetProperty ( "type" ) . GetString ( ) ;
25+ if ( type == "message_start" )
2526 {
26- yield return ChatSegment . FromUsageOnly ( ( int ) start . Message . Usage . InputTokens , ( int ) start . Message . Usage . OutputTokens ) ;
27+ if ( stream . TryPickStart ( out RawMessageStartEvent ? start ) )
28+ {
29+ yield return ExtractUsageFromStart ( start ) ;
30+ }
31+ else
32+ {
33+ RawMessageStartEvent ? start2 = stream . Json . Deserialize < RawMessageStartEvent > ( ) ;
34+ if ( start2 != null )
35+ {
36+ yield return ExtractUsageFromStart ( start2 ) ;
37+ }
38+ }
39+
40+ static ChatSegment ExtractUsageFromStart ( RawMessageStartEvent start )
41+ {
42+ return ChatSegment . FromUsageOnly ( ( int ) start . Message . Usage . InputTokens , ( int ) start . Message . Usage . OutputTokens ) ;
43+ }
2744 }
2845 else if ( stream . TryPickContentBlockStart ( out RawContentBlockStartEvent ? contentStart ) )
2946 {
@@ -220,7 +237,7 @@ static MessageCreateParams ConvertOptions(ChatRequest request)
220237 [
221238 .. request . Tools . Select ( ConvertTool ) ,
222239 .. request . ChatConfig . WebSearchEnabled ? [ new ToolUnion ( new WebSearchTool20250305 ( ) ) ] : Array . Empty < ToolUnion > ( ) ,
223- ]
240+ ] ,
224241 } ;
225242
226243 static ToolUnion ConvertTool ( ChatTool tool )
You can’t perform that action at this time.
0 commit comments