Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion services/kb_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def _chunk_text(text: str) -> list[str]:
current = f"{current}\n{piece}"
else:
chunks.append(current)
tail = current[-CHUNK_OVERLAP:] if CHUNK_OVERLAP else ""
overlap = min(CHUNK_OVERLAP, max(0, CHUNK_SIZE - len(piece) - 1))
tail = current[-overlap:] if overlap else ""
current = (tail + "\n" + piece).strip() if tail else piece
if current:
chunks.append(current)
Expand All @@ -69,6 +70,8 @@ def _split_long(text: str) -> list[str]:
current = ""
for s in sentences:
if len(s) > CHUNK_SIZE:
if current:
out.append(current)
# Hard-split an oversized sentence on word boundaries.
for i in range(0, len(s), CHUNK_SIZE):
out.append(s[i : i + CHUNK_SIZE])
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/services/test_kb_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ def test_split_long_hard_splits_oversized_sentence():
assert "".join(pieces) == "X" * 1000


def test_split_long_preserves_buffer_before_oversized_sentence():
prefix = "Warranty lasts two years."
oversized = "X" * (kb.CHUNK_SIZE + 25)
text = f"{prefix} {oversized}"

pieces = kb._split_long(text)
chunks = kb._chunk_text(text)

assert pieces[0] == prefix
assert "".join(pieces[1:]) == oversized
assert all(len(piece) <= kb.CHUNK_SIZE for piece in pieces)
assert any(prefix in chunk for chunk in chunks)
assert all(len(chunk) <= kb.CHUNK_SIZE for chunk in chunks)


# --- build_context_block -----------------------------------------------------
def test_context_block_empty():
assert kb.build_context_block([]) == ""
Expand Down
Loading