Fire GenerateEnd hook event after an LLM call.
(
&self,
session_id: &str,
prompt: &str,
response: &LlmResponse,
duration_ms: u64,
)
| 451 | |
| 452 | /// Fire GenerateEnd hook event after an LLM call. |
| 453 | async fn fire_generate_end( |
| 454 | &self, |
| 455 | session_id: &str, |
| 456 | prompt: &str, |
| 457 | response: &LlmResponse, |
| 458 | duration_ms: u64, |
| 459 | ) { |
| 460 | if let Some(he) = &self.config.hook_engine { |
| 461 | let tool_calls: Vec<ToolCallInfo> = response |
| 462 | .tool_calls() |
| 463 | .iter() |
| 464 | .map(|tc| { |
| 465 | let args = if tc.args.is_null() { |
| 466 | serde_json::Value::Object(Default::default()) |
| 467 | } else { |
| 468 | tc.args.clone() |
| 469 | }; |
| 470 | ToolCallInfo { |
| 471 | name: tc.name.clone(), |
| 472 | args, |
| 473 | } |
| 474 | }) |
| 475 | .collect(); |
| 476 | |
| 477 | let event = HookEvent::GenerateEnd(GenerateEndEvent { |
| 478 | session_id: session_id.to_string(), |
| 479 | prompt: prompt.to_string(), |
| 480 | response_text: response.text().to_string(), |
| 481 | tool_calls, |
| 482 | usage: TokenUsageInfo { |
| 483 | prompt_tokens: response.usage.prompt_tokens as i32, |
| 484 | completion_tokens: response.usage.completion_tokens as i32, |
| 485 | total_tokens: response.usage.total_tokens as i32, |
| 486 | }, |
| 487 | duration_ms, |
| 488 | }); |
| 489 | let _ = he.fire(&event).await; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /// Cheap, framework-internal estimator of prompt tokens for |
no test coverage detected