diff --git a/atlasbot/knowledge/loader.py b/atlasbot/knowledge/loader.py index 9e0a09d..8310ed2 100644 --- a/atlasbot/knowledge/loader.py +++ b/atlasbot/knowledge/loader.py @@ -106,7 +106,7 @@ class KnowledgeBase: except Exception: return lines.append("KB: atlas.json") - lines.extend(atlas_json.splitlines()) + self._extend_with_limit(lines, atlas_json.splitlines(), max_chars) def _append_runbooks(self, lines: list[str]) -> None: if not self._runbooks: @@ -134,8 +134,19 @@ class KnowledgeBase: if not text: continue lines.append(f"KB File: {path.relative_to(self._base)}") - lines.extend(text.splitlines()) + if not self._extend_with_limit(lines, text.splitlines(), max_chars): + break @staticmethod def _within_limit(lines: list[str], max_chars: int) -> bool: return sum(len(line) for line in lines) < max_chars + + @staticmethod + def _extend_with_limit(lines: list[str], new_lines: list[str], max_chars: int) -> bool: + total = sum(len(line) for line in lines) + for line in new_lines: + if total + len(line) >= max_chars: + return False + lines.append(line) + total += len(line) + return True