11using Microsoft . GS . DPS . Model . ChatHost ;
22using Microsoft . GS . DPS . API ;
3-
43using Microsoft . AspNetCore . Mvc ;
54using Microsoft . AspNetCore . Http . HttpResults ;
65using System . Text ;
76using System . Text . Json ;
7+ using Microsoft . GS . DPSHost . Helpers ;
88
99namespace Microsoft . GS . DPSHost . API
1010{
@@ -15,16 +15,48 @@ public static void AddAPIs(WebApplication app)
1515 //RegisterAsync the chat API
1616 app . MapPost ( "/chat" , async ( ChatRequest request ,
1717 ChatRequestValidator validator ,
18- ChatHost chatHost ) =>
18+ ChatHost chatHost ,
19+ TelemetryHelper telemetryHelper ,
20+ ILogger < Chat > logger ) =>
1921 {
20- if ( validator . Validate ( request ) . IsValid == false )
22+ try
2123 {
22- return Results . BadRequest ( ) ;
23- }
24+ if ( validator . Validate ( request ) . IsValid == false )
25+ {
26+ telemetryHelper . TrackEvent ( "ChatRequestValidationFailed" , new Dictionary < string , string >
27+ {
28+ { "endpoint" , "/chat" }
29+ } ) ;
30+ return Results . BadRequest ( ) ;
31+ }
32+
33+ var result = await chatHost . Chat ( request ) ;
34+
35+ // Track successful chat request
36+ telemetryHelper . TrackEvent ( "ChatRequestSuccess" , new Dictionary < string , string >
37+ {
38+ { "chatSessionId" , result . ChatSessionId ?? "unknown" } ,
39+ { "documentCount" , result . DocumentIds ? . Length . ToString ( ) ?? "0" }
40+ } ) ;
2441
25- var result = await chatHost . Chat ( request ) ;
26- return Results . Ok < ChatResponse > ( result ) ;
42+ // Set correlation ID for tracing
43+ if ( ! string . IsNullOrEmpty ( result . ChatSessionId ) )
44+ {
45+ telemetryHelper . SetActivityTag ( "chatSessionId" , result . ChatSessionId ) ;
46+ }
2747
48+ return Results . Ok < ChatResponse > ( result ) ;
49+ }
50+ catch ( Exception ex )
51+ {
52+ logger . LogError ( ex , "Error processing chat request" ) ;
53+ telemetryHelper . TrackException ( ex , new Dictionary < string , string >
54+ {
55+ { "endpoint" , "/chat" } ,
56+ { "errorType" , ex . GetType ( ) . Name }
57+ } ) ;
58+ throw ;
59+ }
2860 } )
2961 . DisableAntiforgery ( ) ;
3062
@@ -35,36 +67,68 @@ public static void AddAPIs(WebApplication app)
3567 app . MapPost ( "/chatAsync" , async ( HttpContext ctx ,
3668 ChatRequest request ,
3769 ChatRequestValidator validator ,
38- ChatHost chatHost ) =>
70+ ChatHost chatHost ,
71+ TelemetryHelper telemetryHelper ,
72+ ILogger < Chat > logger ) =>
3973 {
40- if ( validator . Validate ( request ) . IsValid == false )
74+ try
4175 {
42- return Results . BadRequest ( ) ;
43- }
76+ if ( validator . Validate ( request ) . IsValid == false )
77+ {
78+ telemetryHelper . TrackEvent ( "ChatAsyncRequestValidationFailed" , new Dictionary < string , string >
79+ {
80+ { "endpoint" , "/chatAsync" }
81+ } ) ;
82+ return Results . BadRequest ( ) ;
83+ }
4484
45- ctx . Response . ContentType = "text/plain" ;
85+ ctx . Response . ContentType = "text/plain" ;
4686
47- //Make a response as a stream
48- var result = chatHost . ChatAsync ( request ) . Result ;
87+ //Make a response as a stream
88+ var result = chatHost . ChatAsync ( request ) . Result ;
4989
50- //Create a dynamic object to store the response
51- var response = new
52- {
53- result . ChatSessionId ,
54- result . DocumentIds ,
55- result . SuggestingQuestions
56- } ;
90+ //Create a dynamic object to store the response
91+ var response = new
92+ {
93+ result . ChatSessionId ,
94+ result . DocumentIds ,
95+ result . SuggestingQuestions
96+ } ;
97+
98+ //Add the response to the header
99+ ctx . Response . Headers . Add ( "RESPONSE" , JsonSerializer . Serialize ( response ) ) ;
57100
58- //Add the response to the header
59- ctx . Response . Headers . Add ( "RESPONSE" , JsonSerializer . Serialize ( response ) ) ;
101+ // Track successful chat async request
102+ telemetryHelper . TrackEvent ( "ChatAsyncRequestSuccess" , new Dictionary < string , string >
103+ {
104+ { "chatSessionId" , result . ChatSessionId ?? "unknown" } ,
105+ { "documentCount" , result . DocumentIds ? . Length . ToString ( ) ?? "0" }
106+ } ) ;
60107
61- // Stream the response
62- await foreach ( var word in result . AnswerWords )
108+ // Set correlation ID for tracing
109+ if ( ! string . IsNullOrEmpty ( result . ChatSessionId ) )
110+ {
111+ telemetryHelper . SetActivityTag ( "chatSessionId" , result . ChatSessionId ) ;
112+ }
113+
114+ // Stream the response
115+ await foreach ( var word in result . AnswerWords )
116+ {
117+ await ctx . Response . WriteAsync ( word ) ;
118+ await ctx . Response . WriteAsync ( " " ) ;
119+ }
120+ return Results . Ok ( ) ;
121+ }
122+ catch ( Exception ex )
63123 {
64- await ctx . Response . WriteAsync ( word ) ;
65- await ctx . Response . WriteAsync ( " " ) ;
124+ logger . LogError ( ex , "Error processing chat async request" ) ;
125+ telemetryHelper . TrackException ( ex , new Dictionary < string , string >
126+ {
127+ { "endpoint" , "/chatAsync" } ,
128+ { "errorType" , ex . GetType ( ) . Name }
129+ } ) ;
130+ throw ;
66131 }
67- return Results . Ok ( ) ;
68132 } )
69133 . DisableAntiforgery ( ) ;
70134 }
0 commit comments