Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 2 additions & 4 deletions src/a2a/utils/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ def apply_history_length(task: Task, history_length: int | None) -> Task:
A new task object with limited history
"""
# Apply historyLength parameter if specified
if history_length is not None and task.history:
if history_length is not None and history_length > 0 and task.history:
# Limit history to the most recent N messages
limited_history = (
task.history[-history_length:] if history_length > 0 else []
)
limited_history = task.history[-history_length:]
# Create a new task instance with limited history
return task.model_copy(update={'history': limited_history})

Expand Down
13 changes: 7 additions & 6 deletions tests/server/request_handlers/test_default_request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ async def test_on_message_send_limit_history():
configuration=MessageSendConfiguration(
blocking=True,
accepted_output_modes=['text/plain'],
history_length=0,
history_length=1,
),
)

Expand All @@ -866,13 +866,13 @@ async def test_on_message_send_limit_history():
# verify that history_length is honored
assert result is not None
assert isinstance(result, Task)
assert result.history is not None and len(result.history) == 0
assert result.history is not None and len(result.history) == 1
assert result.status.state == TaskState.completed

# verify that history is still persisted to the store
task = await task_store.get(result.id)
assert task is not None
assert task.history is not None and len(task.history) > 0
assert task.history is not None and len(task.history) > 1


@pytest.mark.asyncio
Expand All @@ -892,7 +892,8 @@ async def test_on_task_get_limit_history():
parts=[Part(root=TextPart(text='Hi'))],
),
configuration=MessageSendConfiguration(
blocking=True, accepted_output_modes=['text/plain']
blocking=True,
accepted_output_modes=['text/plain'],
),
)

Expand All @@ -904,14 +905,14 @@ async def test_on_task_get_limit_history():
assert isinstance(result, Task)

get_task_result = await request_handler.on_get_task(
TaskQueryParams(id=result.id, history_length=0),
TaskQueryParams(id=result.id, history_length=1),
create_server_call_context(),
)
assert get_task_result is not None
assert isinstance(get_task_result, Task)
assert (
get_task_result.history is not None
and len(get_task_result.history) == 0
and len(get_task_result.history) == 1
)


Expand Down
Loading