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
1 change: 1 addition & 0 deletions atomic-forge/tools/bocha_search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ You can install the tool using any of the following options:
### Parameters

- `api_key` (str): The api key of the BoCha user.
- `freshness` (str, optional): `noLimit` (default), `oneDay`, `oneWeek`, `oneMonth`, `oneYear`, a real date such as `2026-11-07`, or an inclusive date range such as `2026-11-01..2026-11-07`. Invalid calendar dates, format placeholders, and reversed ranges are rejected during configuration.
- `count` (int, optional): The maximum number of search results to return.

### Example
Expand Down
50 changes: 49 additions & 1 deletion atomic-forge/tools/bocha_search/tests/test_bocha_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
import pytest
from pydantic import ValidationError
from unittest.mock import patch, AsyncMock, MagicMock
from types import SimpleNamespace
import asyncio
Expand Down Expand Up @@ -197,7 +198,7 @@ async def test_bocha_search_tool_concurrent_queries(mock_aiohttp_session):
@pytest.mark.asyncio
async def test_bocha_search_tool_config_params_real_case_cn(mock_aiohttp_session):
"""Test that config params freshness/include/exclude are included in request payload with Chinese websites"""
mock_api_key = os.getenv("BOCHA_API_KEY")
mock_api_key = "mock-api-key"

# Tool configuration with domestic websites
tool = BoChaSearchTool(
Expand Down Expand Up @@ -250,5 +251,52 @@ async def test_bocha_search_tool_malformed_response(mock_aiohttp_session):
assert len(result2.results) == 0


@pytest.mark.parametrize(
"freshness",
[
"noLimit",
"oneDay",
"oneWeek",
"oneMonth",
"oneYear",
"2026-11-07",
"2024-02-29",
"2026-11-01..2026-11-07",
"2026-11-07..2026-11-07",
],
)
def test_freshness_accepts_presets_and_dates(freshness):
assert BoChaSearchToolConfig(freshness=freshness).freshness == freshness


@pytest.mark.parametrize(
"freshness",
[
"YYYY-MM-DD",
"YYYY-MM-DD..YYYY-MM-DD",
"2026-02-29",
"2026-13-01",
"2026-11-07..2026-11-01",
"2026-11-01..2026-02-30",
"20261107",
"2026-1-7",
"2026-11-01..",
"",
"someday",
],
)
def test_freshness_rejects_invalid_dates(freshness):
with pytest.raises(ValidationError):
BoChaSearchToolConfig(freshness=freshness)


@pytest.mark.asyncio
@pytest.mark.parametrize("freshness", ["2026-11-07", "2026-11-01..2026-11-07"])
async def test_freshness_date_is_sent_unchanged(mock_aiohttp_session, freshness):
tool = BoChaSearchTool(BoChaSearchToolConfig(api_key="mock-key", freshness=freshness))
await tool.run_async(BoChaSearchToolInputSchema(queries=["test query"]))
assert mock_aiohttp_session.post.call_args.kwargs["json"]["freshness"] == freshness


if __name__ == "__main__":
pytest.main([__file__])
28 changes: 24 additions & 4 deletions atomic-forge/tools/bocha_search/tool/bocha_search.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
from typing import List, Optional, Literal
import re
from datetime import date
from typing import List, Optional
import asyncio
import contextvars
from concurrent.futures import ThreadPoolExecutor
import logging
import aiohttp
from pydantic import Field
from pydantic import Field, field_validator

from atomic_agents import BaseIOSchema, BaseTool, BaseToolConfig

Expand Down Expand Up @@ -46,13 +48,31 @@ class BoChaSearchToolOutputSchema(BaseIOSchema):
class BoChaSearchToolConfig(BaseToolConfig):

api_key: str = ""
freshness: Literal["noLimit", "oneDay", "oneWeek", "oneMonth", "oneYear", "YYYY-MM-DD..YYYY-MM-DD", "YYYY-MM-DD"] = (
"noLimit"
freshness: str = Field(
default="noLimit",
description="Time filter: noLimit, oneDay, oneWeek, oneMonth, oneYear, YYYY-MM-DD, or YYYY-MM-DD..YYYY-MM-DD.",
)
include: Optional[str] = None
exclude: Optional[str] = None
count: int = 10

@field_validator("freshness")
@classmethod
def validate_freshness(cls, value: str) -> str:
"""Accept supported presets or real, chronologically ordered ISO dates."""
if value in {"noLimit", "oneDay", "oneWeek", "oneMonth", "oneYear"}:
return value
message = "freshness must be a supported preset, a YYYY-MM-DD date, or an ordered YYYY-MM-DD..YYYY-MM-DD range"
if not re.fullmatch(r"[0-9]{4}-[0-9]{2}-[0-9]{2}(?:\.\.[0-9]{4}-[0-9]{2}-[0-9]{2})?", value):
raise ValueError(message)
try:
dates = [date.fromisoformat(part) for part in value.split("..")]
except ValueError as error:
raise ValueError(message) from error
if len(dates) == 2 and dates[0] > dates[1]:
raise ValueError(message)
return value


class BoChaSearchTool(BaseTool[BoChaSearchToolInputSchema, BoChaSearchToolOutputSchema]):
"""
Expand Down
Loading