1use crate::types::{PartialResponse, Response, ToolCall, ToolResult};
4use serde::de::DeserializeOwned;
5use serde::Serialize;
6
7pub fn create_content_response(content: impl Into<String>) -> Response {
13 Response {
14 content: Some(content.into()),
15 ..Default::default()
16 }
17}
18
19pub fn create_error_response(message: impl Into<String>) -> Response {
23 create_content_response(format!("Error: {}", message.into()))
24}
25
26pub fn create_function_response<T: Serialize>(
31 tool_call_id: impl Into<String>,
32 result: &T,
33) -> Result<Response, serde_json::Error> {
34 let tool_result = create_tool_result(tool_call_id, result)?;
35 Ok(Response {
36 tool_results: Some(vec![tool_result]),
37 ..Default::default()
38 })
39}
40
41pub fn create_streaming_content_chunk(content: impl Into<String>) -> PartialResponse {
45 PartialResponse {
46 response: create_content_response(content),
47 }
48}
49
50pub fn create_streaming_error_chunk(message: impl Into<String>) -> PartialResponse {
52 PartialResponse {
53 response: create_error_response(message),
54 }
55}
56
57pub fn create_streaming_tool_result_chunk<T: Serialize>(
61 tool_call_id: impl Into<String>,
62 result: &T,
63) -> Result<PartialResponse, serde_json::Error> {
64 let response = create_function_response(tool_call_id, result)?;
65 Ok(PartialResponse { response })
66}
67
68pub fn create_tool_result<T: Serialize>(
75 tool_call_id: impl Into<String>,
76 content: &T,
77) -> Result<ToolResult, serde_json::Error> {
78 let content_str = serde_json::to_string(content)?;
79 Ok(ToolResult {
80 tool_call_id: tool_call_id.into(),
81 content: content_str,
82 })
83}
84
85pub fn parse_function_args<T: DeserializeOwned>(
90 tool_call: &ToolCall,
91) -> Result<T, serde_json::Error> {
92 serde_json::from_str(&tool_call.function.arguments)
93}