@@ -56,11 +56,11 @@ public async Task<bool> InitializeFromLLMAsync(int startTimeMs, int endTimeMs, C
5656 public bool InitializeFromLLM ( int startTimeMs , int endTimeMs )
5757 => InitializeFromLLMAsync ( startTimeMs , endTimeMs ) . GetAwaiter ( ) . GetResult ( ) ;
5858
59- public async Task < string ? > AskWithPromptAsync ( string prompt , CancellationToken cancellationToken = default )
59+ public async Task < string ? > AskWithPromptAsync ( string prompt , string apiKey = "" , CancellationToken cancellationToken = default )
6060 {
6161 try
6262 {
63- return await RequestTextFromLLMAsync ( prompt , cancellationToken ) ;
63+ return await RequestTextFromLLMAsync ( prompt , apiKey , cancellationToken ) ;
6464 }
6565 catch ( Exception ex )
6666 {
@@ -69,10 +69,10 @@ public bool InitializeFromLLM(int startTimeMs, int endTimeMs)
6969 }
7070 }
7171
72- public string ? AskWithPrompt ( string prompt )
72+ public string ? AskWithPrompt ( string prompt , string apiKey = "" )
7373 {
7474 using var cts = new CancellationTokenSource ( GameData . AskAITimeoutMs ) ;
75- return AskWithPromptAsync ( prompt , cts . Token ) . GetAwaiter ( ) . GetResult ( ) ;
75+ return AskWithPromptAsync ( prompt , apiKey , cts . Token ) . GetAwaiter ( ) . GetResult ( ) ;
7676 }
7777
7878 public void InitDefault ( )
@@ -180,6 +180,16 @@ private sealed class GeneratedEvent
180180
181181 private static readonly HttpClient httpClient = new ( ) ;
182182
183+ private static HttpRequestMessage BuildLLMRequest ( string jsonBody , string apiKey )
184+ {
185+ var msg = new HttpRequestMessage ( HttpMethod . Post , GameData . API_url )
186+ {
187+ Content = new StringContent ( jsonBody , Encoding . UTF8 , "application/json" )
188+ } ;
189+ msg . Headers . Authorization = new AuthenticationHeaderValue ( "Bearer" , apiKey ) ;
190+ return msg ;
191+ }
192+
183193 private static async Task < GeneratedEvent ? > RequestEventFromLLMAsync ( CancellationToken cancellationToken )
184194 {
185195 if ( string . IsNullOrWhiteSpace ( GameData . API_key ) ||
@@ -190,10 +200,7 @@ private sealed class GeneratedEvent
190200 return null ;
191201 }
192202
193- if ( httpClient . DefaultRequestHeaders . Authorization == null )
194- httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , GameData . API_key ) ;
195-
196- var req = new ChatRequest
203+ var chatReq = new ChatRequest
197204 {
198205 Model = GameData . ModelName ,
199206 Messages =
@@ -211,9 +218,9 @@ private sealed class GeneratedEvent
211218 ]
212219 } ;
213220
214- var json = JsonSerializer . Serialize ( req ) ;
215- using var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
216- using var resp = await httpClient . PostAsync ( GameData . API_url , content , cancellationToken ) ;
221+ var json = JsonSerializer . Serialize ( chatReq ) ;
222+ using var request = BuildLLMRequest ( json , GameData . API_key ) ;
223+ using var resp = await httpClient . SendAsync ( request , cancellationToken ) ;
217224 if ( ! resp . IsSuccessStatusCode )
218225 {
219226 LogicLogging . logger . LogError ( $ "Event LLM HTTP failed: { ( int ) resp . StatusCode } ") ;
@@ -237,20 +244,22 @@ private sealed class GeneratedEvent
237244 return generated ;
238245 }
239246
240- private static async Task < string ? > RequestTextFromLLMAsync ( string prompt , CancellationToken cancellationToken )
247+ private static async Task < string ? > RequestTextFromLLMAsync ( string prompt , string apiKey , CancellationToken cancellationToken = default )
241248 {
242- if ( string . IsNullOrWhiteSpace ( GameData . API_key ) ||
243- string . IsNullOrWhiteSpace ( GameData . API_url ) ||
249+ if ( string . IsNullOrWhiteSpace ( apiKey ) )
250+ {
251+ LogicLogging . logger . LogError ( "AskAI failed: team API key not configured." ) ;
252+ return null ;
253+ }
254+
255+ if ( string . IsNullOrWhiteSpace ( GameData . API_url ) ||
244256 string . IsNullOrWhiteSpace ( GameData . ModelName ) )
245257 {
246258 LogicLogging . logger . LogError ( "AskAI config missing in GameData." ) ;
247259 return null ;
248260 }
249261
250- if ( httpClient . DefaultRequestHeaders . Authorization == null )
251- httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , GameData . API_key ) ;
252-
253- var req = new ChatRequest
262+ var chatReq = new ChatRequest
254263 {
255264 Model = GameData . ModelName ,
256265 MaxTokens = 512 ,
@@ -269,9 +278,9 @@ private sealed class GeneratedEvent
269278 ]
270279 } ;
271280
272- var json = JsonSerializer . Serialize ( req ) ;
273- using var content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
274- using var resp = await httpClient . PostAsync ( GameData . API_url , content , cancellationToken ) ;
281+ var json = JsonSerializer . Serialize ( chatReq ) ;
282+ using var request = BuildLLMRequest ( json , apiKey ) ;
283+ using var resp = await httpClient . SendAsync ( request , cancellationToken ) ;
275284 if ( ! resp . IsSuccessStatusCode )
276285 {
277286 LogicLogging . logger . LogError ( $ "AskAI HTTP failed: { ( int ) resp . StatusCode } ") ;
0 commit comments