MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / canonicalize_fields

Function canonicalize_fields

atomic-agent/src/hooks/grok.rs:333–354  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

331/// The first spelling in [`FIELD_SPELLINGS`] with a non-null value wins.
332/// Every other key passes through unchanged.
333fn 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///

Callers 1

parse_eventMethod · 0.85

Calls 5

getMethod · 0.65
cloneMethod · 0.45
iterMethod · 0.45
containsMethod · 0.45
collectMethod · 0.45

Tested by

no test coverage detected