-
-
Notifications
You must be signed in to change notification settings - Fork 71
feat: defining tools at the resource level #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zachdaniel
merged 6 commits into
ash-project:main
from
ThaddeusJiang:feat-resource-tools
Apr 22, 2026
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bd57aaf
feat: defining tools at the resource level
ThaddeusJiang 604865b
docs: add resource-level tool definition examples to README
ThaddeusJiang d74f8a6
feat: add AshAi.Macros
ThaddeusJiang 4d13b6c
refactor(tools): make resource arg optional via transformer
ThaddeusJiang 417a503
raise on duplicate domain/resource tool names
ThaddeusJiang 1f4ad96
fix spark.formater and cheat_sheet --check
ThaddeusJiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # SPDX-FileCopyrightText: 2024 ash_ai contributors <https://github.com/ash-project/ash_ai/graphs/contributors> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| defmodule AshAi.Macros do | ||
| @moduledoc false | ||
|
|
||
| @doc """ | ||
| Resource-level shorthand for AshAi tools. | ||
|
|
||
| Allows defining tools inside an `Ash.Resource` as: | ||
|
|
||
| tools do | ||
| tool :list_posts, :read | ||
| end | ||
|
|
||
| This expands to the standard 3-argument tool form using the current module | ||
| as the tool resource. | ||
| """ | ||
| defmacro tool(name, action) do | ||
| caller_module = __CALLER__.module | ||
|
|
||
| quote do | ||
| require AshAi.Tools.Tool | ||
| AshAi.Tools.Tool.tool(unquote(name), unquote(caller_module), unquote(action)) | ||
|
ThaddeusJiang marked this conversation as resolved.
Outdated
|
||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # SPDX-FileCopyrightText: 2024 ash_ai contributors <https://github.com/ash-project/ash_ai/graphs/contributors> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| defmodule AshAi.ResourceToolsTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| alias __MODULE__.{ResourceToolDomain, ResourceToolResource} | ||
|
|
||
| defmodule ResourceToolResource do | ||
| use Ash.Resource, | ||
| domain: ResourceToolDomain, | ||
| extensions: [AshAi], | ||
| data_layer: Ash.DataLayer.Ets, | ||
| validate_domain_inclusion?: false | ||
|
|
||
| attributes do | ||
| uuid_v7_primary_key(:id, writable?: true) | ||
| attribute :name, :string, public?: true | ||
| end | ||
|
|
||
| actions do | ||
| default_accept([:id, :name]) | ||
| defaults([:read, :create]) | ||
| end | ||
|
|
||
| tools do | ||
| tool(:resource_read, :read) | ||
| tool(:resource_create, :create) | ||
| end | ||
| end | ||
|
|
||
| defmodule ResourceToolDomain do | ||
| use Ash.Domain, extensions: [AshAi], validate_config_inclusion?: false | ||
|
|
||
| resources do | ||
| resource ResourceToolResource | ||
| end | ||
|
|
||
| tools do | ||
| tool :domain_create_alias, ResourceToolResource, :create | ||
| end | ||
| end | ||
|
|
||
| describe "resource-level tools shorthand" do | ||
| test "tool :name, :action populates resource and action metadata" do | ||
| tool = | ||
| ResourceToolResource | ||
| |> AshAi.Info.tools() | ||
| |> Enum.find(&(&1.name == :resource_read)) | ||
|
|
||
| assert tool.resource == ResourceToolResource | ||
| assert tool.action == :read | ||
| end | ||
| end | ||
|
|
||
| describe "AshAi.exposed_tools/1 discovery" do | ||
| test "actions filter includes both domain-level and resource-level tools" do | ||
| tools = AshAi.exposed_tools(actions: [{ResourceToolResource, :*}]) | ||
|
|
||
| assert tools | ||
| |> Enum.map(& &1.name) | ||
| |> MapSet.new() == | ||
| MapSet.new([:resource_read, :resource_create, :domain_create_alias]) | ||
| end | ||
|
|
||
| test "actions filter by specific action keeps matching tools from both levels" do | ||
| tools = AshAi.exposed_tools(actions: [{ResourceToolResource, [:create]}]) | ||
|
|
||
| assert tools | ||
| |> Enum.map(& &1.name) | ||
| |> MapSet.new() == MapSet.new([:resource_create, :domain_create_alias]) | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.