MCPcopy Create free account
hub / github.com/InfinitiBit/graphbit / parse_response

Method parse_response

core/src/llm/anthropic.rs:150–222  ·  view source on GitHub ↗

Parse `Anthropic` response to `GraphBit` response

(&self, response: AnthropicResponse)

Source from the content-addressed store, hash-verified

148
149 /// Parse `Anthropic` response to `GraphBit` response
150 fn parse_response(&self, response: AnthropicResponse) -> GraphBitResult<LlmResponse> {
151 let mut content_text = String::new();
152 let mut tool_calls = Vec::new();
153
154 // Process content blocks
155 for block in &response.content {
156 match block.r#type.as_str() {
157 "text" => {
158 if let Some(text) = &block.text {
159 if !content_text.is_empty() {
160 content_text.push('\n');
161 }
162 content_text.push_str(text);
163 }
164 }
165 "tool_use" => {
166 if let (Some(id), Some(name), Some(input)) =
167 (&block.id, &block.name, &block.input)
168 {
169 tool_calls.push(LlmToolCall {
170 id: id.clone(),
171 name: name.clone(),
172 parameters: input.clone(),
173 });
174 }
175 }
176 _ => {
177 // Handle other content types if needed
178 }
179 }
180 }
181
182 let finish_reason = match response.stop_reason.as_deref() {
183 Some("end_turn" | "stop_sequence") => FinishReason::Stop,
184 Some("max_tokens") => FinishReason::Length,
185 Some("tool_use") => FinishReason::Other("tool_use".into()),
186 Some(other) => FinishReason::Other(other.to_string()),
187 None => FinishReason::Stop,
188 };
189
190 let usage = LlmUsage::new_with_cache(
191 response.usage.input_tokens,
192 response.usage.output_tokens,
193 response.usage.cache_read_input_tokens,
194 response.usage.cache_creation_input_tokens,
195 );
196
197 // Log cache savings when caching is active
198 if let Some(read) = response.usage.cache_read_input_tokens {
199 if read > 0 {
200 tracing::debug!(
201 "Anthropic cache hit: {} tokens read from cache, {} creation tokens",
202 read,
203 response
204 .usage
205 .cache_creation_input_tokens
206 .unwrap_or(0)
207 );

Callers 1

completeMethod · 0.45

Calls 7

cloneMethod · 0.80
to_stringMethod · 0.80
is_emptyMethod · 0.45
with_idMethod · 0.45
with_finish_reasonMethod · 0.45
with_usageMethod · 0.45
with_tool_callsMethod · 0.45

Tested by

no test coverage detected