-
Notifications
You must be signed in to change notification settings - Fork 0
First slack tool pydantic ai #10
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,13 +1,5 @@ | ||
| from collections.abc import Callable | ||
| from pydantic_ai.toolsets import FunctionToolset | ||
|
|
||
| from slack_sdk.web.slack_response import SlackResponse | ||
| from app.tools.slack import slack_toolset | ||
|
|
||
| from app.models.tools import SlackChatPostMessageParams | ||
| from app.tools.slack import post_message | ||
|
|
||
| TOOLS: dict[str, dict[str, type | Callable[..., SlackResponse]]] = { | ||
| "slack.chat.postMessage": { | ||
| "schema": SlackChatPostMessageParams, | ||
| "fn": post_message, | ||
| }, | ||
| } | ||
| TOOLS: dict[str, FunctionToolset] = {"slack.tools": slack_toolset} |
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 |
|---|---|---|
| @@ -1,9 +1,26 @@ | ||
| from dataclasses import dataclass | ||
|
|
||
| from pydantic_ai import RunContext | ||
| from pydantic_ai.toolsets import FunctionToolset | ||
| from slack_sdk import WebClient | ||
| from slack_sdk.web.slack_response import SlackResponse | ||
|
|
||
| from app.models.tools import SlackChatPostMessageParams | ||
|
|
||
|
|
||
| def post_message(token: str, params: SlackChatPostMessageParams) -> SlackResponse: | ||
| client = WebClient(token=token) | ||
| return client.chat_postMessage(channel=params.channel, text=params.text) | ||
| @dataclass | ||
| class Deps: | ||
| client: WebClient | ||
|
|
||
|
|
||
| slack_toolset = FunctionToolset() | ||
|
|
||
|
|
||
| @slack_toolset.tool(name="slack.chat.postMessage") | ||
| def post_message( | ||
| ctx: RunContext[Deps], params: SlackChatPostMessageParams | ||
| ) -> SlackResponse: | ||
| """ | ||
| Use this function to post a message in the specified channel | ||
| """ | ||
| return ctx.deps.client.chat_postMessage(channel=params.channel, text=params.text) | ||
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 |
|---|---|---|
|
|
@@ -20,4 +20,5 @@ dev = [ | |
| "pre-commit", | ||
| "coverage", | ||
| "ruff", | ||
| "ipython>=8.37.0", | ||
| ] | ||
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,77 @@ | ||
| import os | ||
|
|
||
| import pytest | ||
| from dotenv import load_dotenv | ||
| from pydantic_ai import Agent, RunContext, RunUsage | ||
| from pydantic_ai.models.test import TestModel | ||
| from slack_sdk import WebClient | ||
| from slack_sdk.errors import SlackApiError | ||
|
|
||
| from app.models import tools | ||
| from app.tools import slack | ||
|
|
||
| _ = load_dotenv() | ||
|
|
||
|
|
||
| def test_health_ava_can_post_message(): | ||
| token = os.environ.get("SLACK_BOT_TOKEN") | ||
| if not token: | ||
| pytest.skip("SLACK_BOT_TOKEN not set; live Slack test skipped") | ||
|
|
||
| client = WebClient(token=token) | ||
| message_text = "AVA backend live smoke :rocket:" | ||
|
|
||
| try: | ||
| response = client.chat_postMessage(channel="sandbox", text=message_text) | ||
| except SlackApiError as exc: | ||
| pytest.fail(f"Slack API error: {exc.response['error']}") | ||
|
|
||
| assert response["ok"] is True | ||
| assert response["message"]["text"] == message_text # type: ignore | ||
| assert "ts" in response | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "channel,text,token", | ||
| [ | ||
| ( | ||
| "sandbox", | ||
| "Hello, AVA backend live smoke :rocket:", | ||
| os.environ.get("SLACK_BOT_TOKEN"), | ||
| ), | ||
| ( | ||
| "general", | ||
| "Hello, User is live smoke :rocket:", | ||
| os.environ.get("SLACK_USER_TOKEN"), | ||
| ), | ||
| ], | ||
| ) | ||
| def test_post_message_with_ctx(channel: str, text: str, token: str | None): | ||
| if token is not None: | ||
| client = WebClient(token=token) | ||
| ctx = RunContext( | ||
| deps=slack.Deps(client=client), | ||
| model=TestModel(), | ||
| usage=RunUsage(), | ||
| ) | ||
| tool_params = tools.SlackChatPostMessageParams(channel=channel, text=text) | ||
| try: | ||
| response = slack.post_message(ctx=ctx, params=tool_params) | ||
| except SlackApiError as exc: | ||
| pytest.fail(f"Slack API error: {exc.response['error']}") | ||
|
|
||
| assert response["ok"] is True | ||
| assert response["message"]["text"] == text | ||
| assert "ts" in response | ||
|
|
||
|
|
||
| def test_slack_tools_are_synced(): | ||
| token = os.environ.get("SLACK_BOT_TOKEN") | ||
| client = WebClient(token=token) | ||
| test_model = TestModel(call_tools=[]) | ||
| agent = Agent(test_model, toolsets=[slack.slack_toolset], deps_type=slack.Deps) | ||
| _ = agent.run_sync("What tools are available?", deps=slack.Deps(client=client)) | ||
|
|
||
| assert [ | ||
| t.name for t in test_model.last_model_request_parameters.function_tools | ||
| ] == ["slack.chat.postMessage"] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Return SlackResponse from tool prevents serialization
The new
post_messagetool returns theSlackResponseobject directly. When this function is executed viaFunctionToolset, the agent has to JSON‑serialize the tool’s output to send it back to the model.SlackResponsecarries non‑serializable attributes (e.g., the underlyingWebClientandrequests.Response), so the agent will raise aTypeErrorinstead of reporting the Slack call result. Consider returning a plaindict/BaseModelsuch asresponse.dataso the tool output can be encoded.Useful? React with 👍 / 👎.