@@ -515,6 +515,10 @@ impl AgentContext for AgentCtx {
515515}
516516
517517impl CompletionFeatures for AgentCtx {
518+ fn model_name ( & self ) -> String {
519+ self . model . model_name ( )
520+ }
521+
518522 /// Executes a completion request with automatic tool call handling.
519523 ///
520524 /// This method handles the completion request in a loop, automatically executing
@@ -991,33 +995,37 @@ impl CompletionRunner {
991995 self . step += 1 ;
992996
993997 let mut output = self . model . completion ( self . req . clone ( ) ) . await ?;
998+ output. model = Some ( self . model . model_name ( ) ) ;
999+
9941000 self . usage . accumulate ( & output. usage ) ;
9951001
9961002 // If the primary model returns a failed result (failed_reason exists),
9971003 // and a fallback model is configured, switch to the fallback model and retry.
9981004 // After switching, subsequent steps will keep using the fallback model.
9991005 if output. failed_reason . is_some ( )
1000- && let Some ( fallback) = self . fallback_model . take ( ) {
1001- let primary_reason = output
1002- . failed_reason
1003- . clone ( )
1004- . unwrap_or_else ( || "unknown error" . to_string ( ) ) ;
1005-
1006- self . model = fallback;
1007- let mut output2 = self . model . completion ( self . req . clone ( ) ) . await ?;
1008- self . usage . accumulate ( & output2. usage ) ;
1009-
1010- if let Some ( fallback_reason) = output2. failed_reason . clone ( ) {
1011- output2. failed_reason = Some ( format ! (
1012- "primary model failed: {}; fallback model failed: {}" ,
1013- primary_reason, fallback_reason
1014- ) ) ;
1015- return Ok ( Some ( self . final_output ( output2) ) ) ;
1016- }
1017-
1018- output = output2;
1006+ && let Some ( fallback) = self . fallback_model . take ( )
1007+ {
1008+ let primary_reason = output
1009+ . failed_reason
1010+ . clone ( )
1011+ . unwrap_or_else ( || "unknown error" . to_string ( ) ) ;
1012+
1013+ self . model = fallback;
1014+ let mut output2 = self . model . completion ( self . req . clone ( ) ) . await ?;
1015+ output2. model = Some ( self . model . model_name ( ) ) ;
1016+ self . usage . accumulate ( & output2. usage ) ;
1017+
1018+ if let Some ( fallback_reason) = output2. failed_reason . clone ( ) {
1019+ output2. failed_reason = Some ( format ! (
1020+ "primary model failed: {}; fallback model failed: {}" ,
1021+ primary_reason, fallback_reason
1022+ ) ) ;
1023+ return Ok ( Some ( self . final_output ( output2) ) ) ;
10191024 }
10201025
1026+ output = output2;
1027+ }
1028+
10211029 // 累计所有原始对话历史(包含初始的 req.raw_history 和 req.chat_history)
10221030 self . req . raw_history . append ( & mut output. raw_history ) ;
10231031 // 累计所有对话历史(不包含初始的 req.chat_history)
@@ -1220,6 +1228,10 @@ mod tests {
12201228 struct AlwaysFailCompleter ;
12211229
12221230 impl CompletionFeaturesDyn for AlwaysFailCompleter {
1231+ fn model_name ( & self ) -> String {
1232+ "always_fail" . to_string ( )
1233+ }
1234+
12231235 fn completion (
12241236 & self ,
12251237 _req : CompletionRequest ,
@@ -1235,6 +1247,10 @@ mod tests {
12351247 struct AlwaysOkCompleter ;
12361248
12371249 impl CompletionFeaturesDyn for AlwaysOkCompleter {
1250+ fn model_name ( & self ) -> String {
1251+ "always_ok" . to_string ( )
1252+ }
1253+
12381254 fn completion (
12391255 & self ,
12401256 _req : CompletionRequest ,
0 commit comments