(events, is_json=False)
| 170 | |
| 171 | |
| 172 | def normalize_events(events, is_json=False): |
| 173 | normalized = [] |
| 174 | for e in events: |
| 175 | if is_json: |
| 176 | d = dict(e) |
| 177 | for k in EXCLUDED_EVENT_FIELDS: |
| 178 | d.pop(k, None) |
| 179 | d.pop(alias_generators.to_camel(k), None) |
| 180 | d = {k: v for k, v in d.items() if v is not None} |
| 181 | else: |
| 182 | d = e.model_dump( |
| 183 | mode="json", |
| 184 | by_alias=True, |
| 185 | exclude=EXCLUDED_EVENT_FIELDS, |
| 186 | exclude_none=True, |
| 187 | ) |
| 188 | |
| 189 | if "content" in d and isinstance(d["content"], dict): |
| 190 | content = d["content"] |
| 191 | if "parts" in content and isinstance(content["parts"], list): |
| 192 | for part in content["parts"]: |
| 193 | if isinstance(part, dict) and "thoughtSignature" in part: |
| 194 | del part["thoughtSignature"] |
| 195 | |
| 196 | if "longRunningToolIds" in d: |
| 197 | if isinstance(d["longRunningToolIds"], list): |
| 198 | if not d["longRunningToolIds"]: |
| 199 | del d["longRunningToolIds"] |
| 200 | else: |
| 201 | d["longRunningToolIds"] = sorted(d["longRunningToolIds"]) |
| 202 | |
| 203 | if "actions" in d: |
| 204 | actions = d["actions"] |
| 205 | if isinstance(actions, dict): |
| 206 | # Remove empty dicts inside actions |
| 207 | for k in list(actions.keys()): |
| 208 | if actions[k] == {}: |
| 209 | del actions[k] |
| 210 | # If actions itself is now empty, remove it! |
| 211 | if not actions: |
| 212 | del d["actions"] |
| 213 | |
| 214 | actions = d.get("actions", {}) |
| 215 | state_delta = actions.get("stateDelta", {}) if actions else {} |
| 216 | if state_delta: |
| 217 | keys_to_remove = [k for k in state_delta if k.endswith("_join_state")] |
| 218 | for k in keys_to_remove: |
| 219 | del state_delta[k] |
| 220 | |
| 221 | normalized.append(d) |
| 222 | return normalized |
| 223 | |
| 224 | |
| 225 | def make_sort_key(d): |
no test coverage detected