fix: count_posts paginated before aggregation

This commit is contained in:
2026-05-01 22:56:29 +02:00
parent fef722c4c9
commit 64a5eb525d
4 changed files with 106 additions and 3 deletions

View File

@@ -220,7 +220,7 @@ defmodule BDS.AI.ChatTools do
def execute("count_posts", arguments, project_id) do
project_id = project_id || active_project_id()
group_by = List.wrap(arguments["groupBy"] || arguments["group_by"]) |> Enum.map(&to_string/1)
{:ok, result} = Search.search_posts(project_id, "", search_filters(arguments))
result = search_all_counted_posts(project_id, arguments)
groups =
result.posts
@@ -882,6 +882,16 @@ defmodule BDS.AI.ChatTools do
|> Map.put(:limit, normalize_limit(arguments["limit"]))
end
defp search_all_counted_posts(project_id, arguments) do
filters = search_filters(arguments) |> Map.put(:offset, 0) |> Map.put(:limit, 1)
{:ok, %{total: total}} = Search.search_posts(project_id, "", filters)
filters = Map.put(filters, :limit, max(total, 1))
{:ok, result} = Search.search_posts(project_id, "", filters)
result
end
defp maybe_put(map, _key, nil), do: map
defp maybe_put(map, _key, ""), do: map
defp maybe_put(map, key, value), do: Map.put(map, key, value)

View File

@@ -390,8 +390,7 @@ defmodule BDS.MCP.Tools do
defp count_posts(params) do
project = Queries.active_project!()
group_by = map_get(params, :groupBy, []) |> Enum.map(&to_string/1)
filters = Queries.search_filters(params)
{:ok, result} = Search.search_posts(project.id, "", filters)
result = search_all_counted_posts(project.id, params)
groups =
result.posts
@@ -403,6 +402,16 @@ defmodule BDS.MCP.Tools do
%{"groups" => groups, "total_posts" => result.total}
end
defp search_all_counted_posts(project_id, params) do
filters = Queries.search_filters(params) |> Map.put(:offset, 0) |> Map.put(:limit, 1)
{:ok, %{total: total}} = Search.search_posts(project_id, "", filters)
filters = Map.put(filters, :limit, max(total, 1))
{:ok, result} = Search.search_posts(project_id, "", filters)
result
end
defp read_post_by_slug(%{"slug" => slug} = params),
do: read_post_by_slug(Map.put_new(params, :slug, slug))