Rewrite the envelope so each [`CommonInput`] field appears once, under its own name. The first spelling in [`FIELD_SPELLINGS`] with a non-null value wins. Every other key passes through unchanged.
(raw: &Value)
| 331 | /// The first spelling in [`FIELD_SPELLINGS`] with a non-null value wins. |
| 332 | /// Every other key passes through unchanged. |
| 333 | fn canonicalize_fields(raw: &Value) -> Value { |
| 334 | let Some(obj) = raw.as_object() else { |
| 335 | return raw.clone(); |
| 336 | }; |
| 337 | let is_spelling = |key: &str| { |
| 338 | FIELD_SPELLINGS |
| 339 | .iter() |
| 340 | .any(|(_, spellings)| spellings.contains(&key)) |
| 341 | }; |
| 342 | let untouched = obj |
| 343 | .iter() |
| 344 | .filter(|(key, _)| !is_spelling(key)) |
| 345 | .map(|(key, value)| (key.clone(), value.clone())); |
| 346 | let folded = FIELD_SPELLINGS.iter().filter_map(|(field, spellings)| { |
| 347 | spellings |
| 348 | .iter() |
| 349 | .filter_map(|spelling| obj.get(*spelling)) |
| 350 | .find(|value| !value.is_null()) |
| 351 | .map(|value| (field.to_string(), value.clone())) |
| 352 | }); |
| 353 | Value::Object(untouched.chain(folded).collect()) |
| 354 | } |
| 355 | |
| 356 | /// Strip Grok harness wrappers from a user prompt. |
| 357 | /// |