fix: AI tools better described now

This commit is contained in:
2026-05-01 20:32:46 +02:00
parent dd0c05b785
commit b5ebea6ff2
5 changed files with 629 additions and 31 deletions

View File

@@ -5,9 +5,11 @@ defmodule BDS.AI.ChatTools do
alias BDS.AI.Chat
alias BDS.Media.Media
alias BDS.MCP.Queries
alias BDS.Posts.Post
alias BDS.Projects.Project
alias BDS.Repo
alias BDS.Search
@spec execute(String.t(), map(), String.t() | nil) :: map()
def execute("blog_stats", _arguments, project_id) do
@@ -23,20 +25,79 @@ defmodule BDS.AI.ChatTools do
}
end
def execute("list_posts", arguments, project_id) do
limit = normalize_limit(arguments["limit"])
def execute("check_term", arguments, project_id) do
project_id = project_id || active_project_id()
term = normalize_term(arguments["term"])
Repo.all(
from(post in Post,
where: post.project_id == ^project_id,
order_by: [desc: post.updated_at],
limit: ^limit,
select: %{id: post.id, title: post.title, slug: post.slug, status: post.status}
)
)
posts = Repo.all(from post in Post, where: post.project_id == ^project_id)
tag_post_count =
Enum.count(posts, fn post ->
Enum.any?(post.tags || [], &(normalize_term(&1) == term))
end)
category_post_count =
Enum.count(posts, fn post ->
Enum.any?(post.categories || [], &(normalize_term(&1) == term))
end)
%{
is_category: category_post_count > 0,
category_post_count: category_post_count,
is_tag: tag_post_count > 0,
tag_post_count: tag_post_count
}
end
def execute("search_posts", arguments, project_id) do
project_id = project_id || active_project_id()
filters = search_filters(arguments)
{:ok, result} = Search.search_posts(project_id, arguments["query"] || "", filters)
%{
posts: Enum.map(result.posts, &Queries.post_summary/1),
total: result.total,
offset: result.offset,
limit: result.limit,
has_more: result.offset + result.limit < result.total
}
end
def execute("read_post_by_slug", arguments, project_id) do
project_id = project_id || active_project_id()
case Repo.get_by(Post, project_id: project_id, slug: arguments["slug"]) do
%Post{} = post -> %{post: Queries.post_detail(post)}
nil -> %{error: "not_found"}
end
end
def execute("list_posts", arguments, project_id) do
project_id = project_id || active_project_id()
limit = normalize_limit(arguments["limit"])
offset = normalize_offset(arguments["offset"])
filters = search_filters(arguments) |> Map.merge(%{limit: limit, offset: offset})
{:ok, result} = Search.search_posts(project_id, "", filters)
%{
posts:
Enum.map(result.posts, fn post ->
post
|> Queries.post_summary()
|> Map.put("url", "/posts/#{post.slug}")
|> Map.put("updated_at", post.updated_at)
end),
total: result.total,
offset: result.offset,
limit: result.limit,
has_more: result.offset + result.limit < result.total
}
end
def execute("list_media", arguments, project_id) do
project_id = project_id || active_project_id()
limit = normalize_limit(arguments["limit"])
Repo.all(
@@ -48,12 +109,46 @@ defmodule BDS.AI.ChatTools do
id: media.id,
title: media.title,
mime_type: media.mime_type,
filename: media.filename
filename: media.filename,
updated_at: media.updated_at
}
)
)
end
def execute("list_tags", _arguments, project_id) do
project_id = project_id || active_project_id()
%{
tags: counted_terms(project_id, :tags),
count: length(counted_terms(project_id, :tags))
}
end
def execute("list_categories", _arguments, project_id) do
project_id = project_id || active_project_id()
%{
categories: counted_terms(project_id, :categories),
count: length(counted_terms(project_id, :categories))
}
end
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))
groups =
result.posts
|> Enum.flat_map(&Queries.group_rows(&1, group_by))
|> Enum.group_by(& &1, fn _row -> 1 end)
|> Enum.map(fn {row, counts} -> Map.put(row, "count", length(counts)) end)
|> Enum.sort_by(&Map.to_list/1)
%{groups: groups, total_posts: result.total}
end
def execute("render_table", arguments, _project_id) do
%{
type: "table",
@@ -142,19 +237,88 @@ defmodule BDS.AI.ChatTools do
"properties" => %{}
})
},
%{
name: "check_term",
spec:
tool_spec(
"check_term",
"Check whether a term exists as a category, tag, or both. Returns post counts for each. Use before search_posts or list_posts when unsure whether a term is a category or tag.",
%{
"type" => "object",
"properties" => %{"term" => %{"type" => "string"}},
"required" => ["term"]
}
)
},
%{
name: "search_posts",
spec:
tool_spec(
"search_posts",
"Search blog posts using full-text search. Can filter by category, tags, language, missing translation language, year, month, or status. Returns paginated concrete post data with titles, slugs, tags, categories, backlinks, and links_to.",
post_search_schema(true)
)
},
%{
name: "read_post_by_slug",
spec:
tool_spec(
"read_post_by_slug",
"Read full content and metadata of a specific blog post by slug. Includes backlinks, links_to, tags, categories, excerpt, status, language, and available languages.",
%{
"type" => "object",
"properties" => %{"slug" => %{"type" => "string"}},
"required" => ["slug"]
}
)
},
%{
name: "list_posts",
spec:
tool_spec("list_posts", "List recent posts in the active project", limit_schema())
tool_spec(
"list_posts",
"List blog posts with optional filtering by status, category, tags, language, year, or month. Returns paginated concrete post data with titles, slugs, URLs, statuses, tags, categories, backlinks, and links_to. Use for recent, latest, top, or title-list requests.",
post_search_schema(false)
)
},
%{
name: "list_media",
spec:
tool_spec(
"list_media",
"List recent media items in the active project",
"List concrete media data in the active project, including titles, filenames, MIME types, and update times.",
limit_schema()
)
},
%{
name: "list_tags",
spec:
tool_spec(
"list_tags",
"List all tags used across blog posts with post counts.",
%{
"type" => "object",
"properties" => %{}
}
)
},
%{
name: "list_categories",
spec:
tool_spec(
"list_categories",
"List all categories used across blog posts with post counts.",
%{"type" => "object", "properties" => %{}}
)
},
%{
name: "count_posts",
spec:
tool_spec(
"count_posts",
"Count posts grouped by dimensions such as year, month, tag, category, or status. Use for analytics, distributions, and heat maps without transferring full post content.",
count_posts_schema()
)
}
]
else
@@ -245,6 +409,47 @@ defmodule BDS.AI.ChatTools do
}
end
defp post_search_schema(require_query) do
schema = %{
"type" => "object",
"properties" => %{
"query" => %{"type" => "string"},
"status" => %{"type" => "string", "enum" => ["draft", "published", "archived"]},
"category" => %{"type" => "string"},
"tags" => %{"type" => "array", "items" => %{"type" => "string"}},
"language" => %{"type" => "string"},
"missingTranslationLanguage" => %{"type" => "string"},
"year" => %{"type" => "integer"},
"month" => %{"type" => "integer", "minimum" => 1, "maximum" => 12},
"limit" => %{"type" => "integer", "minimum" => 1, "maximum" => 50},
"offset" => %{"type" => "integer", "minimum" => 0}
}
}
if require_query, do: Map.put(schema, "required", ["query"]), else: schema
end
defp count_posts_schema do
%{
"type" => "object",
"properties" => %{
"groupBy" => %{
"type" => "array",
"items" => %{
"type" => "string",
"enum" => ["year", "month", "tag", "category", "status"]
}
},
"year" => %{"type" => "integer"},
"month" => %{"type" => "integer", "minimum" => 1, "maximum" => 12},
"status" => %{"type" => "string", "enum" => ["draft", "published", "archived"]},
"category" => %{"type" => "string"},
"tags" => %{"type" => "array", "items" => %{"type" => "string"}}
},
"required" => ["groupBy"]
}
end
defp render_table_schema do
%{
"type" => "object",
@@ -334,6 +539,41 @@ defmodule BDS.AI.ChatTools do
defp normalize_limit(value) when is_integer(value) and value > 0 and value <= 50, do: value
defp normalize_limit(_value), do: 10
defp normalize_offset(value) when is_integer(value) and value >= 0, do: value
defp normalize_offset(_value), do: 0
defp search_filters(arguments) do
%{}
|> maybe_put(:category, arguments["category"])
|> maybe_put(:tags, arguments["tags"])
|> maybe_put(:language, arguments["language"])
|> maybe_put(:missing_translation_language, arguments["missingTranslationLanguage"])
|> maybe_put(:year, arguments["year"])
|> maybe_put(:month, arguments["month"])
|> maybe_put(:status, BDS.BoundedAtoms.post_status(arguments["status"]))
|> Map.put(:offset, normalize_offset(arguments["offset"]))
|> Map.put(:limit, normalize_limit(arguments["limit"]))
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)
defp counted_terms(project_id, field) do
Repo.all(
from post in Post, where: post.project_id == ^project_id, select: field(post, ^field)
)
|> List.flatten()
|> Enum.reject(&blank?/1)
|> Enum.frequencies()
|> Enum.map(fn {term, count} -> %{name: term, count: count} end)
|> Enum.sort_by(&String.downcase(to_string(&1.name)))
end
defp blank?(value), do: is_nil(value) or String.trim(to_string(value)) == ""
defp normalize_term(value), do: value |> to_string() |> String.downcase()
defp active_project_id do
Repo.one(from(project in Project, where: project.is_active == true, select: project.id))
end