MCPcopy Create free account
hub / github.com/OpenBMB/ChatDev / clear_input

Method clear_input

entity/configs/node/node.py:238–271  ·  view source on GitHub ↗

Clear queued inputs according to the node's context window semantics.

(self, *, preserve_kept: bool = False, context_window: int = 0)

Source from the content-addressed store, hash-verified

236 self.output.append(payload)
237
238 def clear_input(self, *, preserve_kept: bool = False, context_window: int = 0) -> int:
239 """Clear queued inputs according to the node's context window semantics."""
240 if not preserve_kept:
241 self.input = []
242 return len(self.input)
243
244 if context_window < 0:
245 return len(self.input)
246
247 if context_window == 0:
248 self.input = [message for message in self.input if getattr(message, "keep", False)]
249 return len(self.input)
250
251 # context_window > 0 => retain the newest messages up to the specified
252 # capacity, but never drop messages flagged with keep=True. Those kept
253 # messages still count toward the window, effectively consuming slots that
254 # would otherwise be available for non-kept inputs.
255 keep_count = sum(1 for message in self.input if getattr(message, "keep", False))
256 allowed_non_keep = max(0, context_window - keep_count)
257 non_keep_total = sum(1 for message in self.input if not getattr(message, "keep", False))
258 non_keep_to_drop = max(0, non_keep_total - allowed_non_keep)
259
260 trimmed_inputs: List[Message] = []
261 for message in self.input:
262 if getattr(message, "keep", False):
263 trimmed_inputs.append(message)
264 continue
265 if non_keep_to_drop > 0:
266 non_keep_to_drop -= 1
267 continue
268 trimmed_inputs.append(message)
269
270 self.input = trimmed_inputs
271 return len(self.input)
272
273 def clear_inputs_by_flag(self, *, drop_non_keep: bool, drop_keep: bool) -> Tuple[int, int]:
274 """Clear queued inputs according to keep markers."""

Callers 3

runMethod · 0.80
_execute_nodeMethod · 0.80
runMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected