AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): rank connected MCP and first-party tools (#15011)
Bently7d74afe481c0
diff --git a/autogpt_platform/backend/backend/copilot/capabilities/chat_platform_search_test.py b/autogpt_platform/backend/backend/copilot/capabilities/chat_platform_search_test.pynew file mode 100644index 0000000000..0efa2bb417--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/capabilities/chat_platform_search_test.py@@ -0,0 +1,99 @@+"""post_to_chat_platform is found when a search names one chat platform.++Discord, Slack and Telegram each have their own blocks, so a query naming+one is a service query restricted to that service. The first-party tool posts+as the AutoGPT bot with no user credential, and must stay in that list rather+than leave the model with only the per-platform send blocks.+"""++import pytest++from backend.copilot.tools import TOOL_GROUPS, TOOL_REGISTRY, chat_platform+from backend.copilot.tools.chat_platform import (+ SUPPORTED_PLATFORMS,+ PostToChatPlatformTool,+)++from .index import CapabilityIndex+from .ranking import ConnectionState+from .registry import build_entries++TOOL = "post_to_chat_platform"+# How each platform is spelled in a request, and its legacy send block.+SPELLINGS: dict[str, tuple[tuple[str, ...], str | None]] = {+ "discord": (("discord",), "SendDiscordMessageBlock"),+ "slack": (("slack",), "SendSlackMessageBlock"),+ "telegram": (("telegram",), "SendTelegramMessageBlock"),+ "teams": (("teams", "microsoft teams"), None),+}+QUERIES = [+ (platform, query.format(name=name), block)+ for platform, (names, block) in SPELLINGS.items()+ for name in names+ for query in ("{name}", "post a message to {name}", "send a {name} message")+]+++def _index(tools) -> CapabilityIndex:+ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(chat_platform, "_any_chat_platform_configured", lambda: True)+ return CapabilityIndex(build_entries(tools, TOOL_GROUPS))++[email protected](scope="module")+def index() -> CapabilityIndex:+ return _index(TOOL_REGISTRY)+++def test_every_supported_platform_has_queries():+ assert set(SPELLINGS) == set(SUPPORTED_PLATFORMS)++[email protected]("connected", [False, True], ids=["unlinked", "linked"])[email protected]("platform,query,block", QUERIES)+def test_naming_one_platform_finds_the_tool(index, platform, query, block, connected):+ connections = ConnectionState(+ providers=frozenset({platform}) if connected else frozenset()+ )+ names = index.search(query, connections=connections).names+ assert TOOL in names, f"{query!r} -> {names}"+ if block in names:+ assert names.index(TOOL) < names.index(block), f"{query!r} -> {names}"+++def test_the_listing_says_it_needs_no_credential(index):+ """The model picks from the listed purpose; one that reads like any other+ integration loses to a block it has seen before."""+ entry = index.get(f"tool:{TOOL}")+ assert entry is not None+ assert "AutoGPT bot" in entry.purpose+ assert "no credential" in entry.purpose+++ "query",+ ["post this report to our discord channel", "post a report to slack"],+)+def test_asking_to_post_a_report_lands_on_post_not_edit(index, query):+ """The edit tool once matched "report" through "is always reported" and+ outranked the tool that posts."""+ assert index.search(query).names[0] == TOOL+++class _TerseTool(PostToChatPlatformTool):+ @property+ def description(self) -> str:+ return "Post a message."++[email protected](scope="module")+def terse_index() -> CapabilityIndex:+ return _index({**TOOL_REGISTRY, TOOL: _TerseTool()})++[email protected]("platform", SUPPORTED_PLATFORMS)+def test_found_by_platform_whatever_the_description_says(terse_index, platform):+ """Each supported platform is indexed by name, so trimming the+ description cannot drop the tool from that platform's search."""+ names = terse_index.search(f"post to {platform}").names+ assert TOOL in names, f"{platform!r} -> {names}"diff --git a/autogpt_platform/backend/backend/copilot/capabilities/index.py b/autogpt_platform/backend/backend/copilot/capabilities/index.pyindex 862845b8ca..3ea6dc691d 100644--- a/autogpt_platform/backend/backend/copilot/capabilities/index.py+++ b/autogpt_platform/backend/backend/copilot/capabilities/index.py@@ -3,8 +3,9 @@A search returns ranked entries filtered by context, kind and the turn'spermissions, with primitives weighted below matching services. When thequery names a service ("linear", "gmail", "mcp.sentry.dev") the main list-is restricted to that service and up to three primitives are returned-separately as ``fallback`` so the model can still build the request by hand.+is restricted to that service plus the platform's own tools, and up to three+primitives are returned separately as ``fallback`` so the model can still+build the request by hand."""from __future__ import annotations@@ -18,7 +19,8 @@ from pydantic import BaseModelfrom rank_bm25 import BM25Okapifrom .models import SKILL_TOOL, CapabilityContext, CapabilityEntry, CapabilityKindName-from .ranking import ConnectionState, class_weight, resolve_connected, tier+from .ordering import rank_hits+from .ranking import ConnectionState, class_weight, resolve_connectedfrom .text import normalize_name, query_groups, tokenizeif TYPE_CHECKING:@@ -34,6 +36,9 @@ _NAME_WEIGHT = 2 # repeat name tokens so the name outweighs the purpose textDEFAULT_LIMIT = 8DEFAULT_FALLBACK_LIMIT = 3+# Platform tools that help connect an account and never do a service's job,+# so a query naming a service does not pull them into its list.+CONNECTION_HELPER_TOOLS = frozenset({"connect_integration"})class SearchHit(BaseModel):@@ -41,9 +46,9 @@ class SearchHit(BaseModel):score: float# Query concepts the entry matched: 1 per token matched as written, 0.5# when only a synonym matched. Ranking is coverage first, then- # connection, then BM25: two entries that both match "create" and- # "issue" are ordered by whether the user has connected them, not by- # which description happens to be shorter.+ # connection and platform tools (see ``rank_hits``), then BM25: two entries+ # that both match "create" and "issue" are ordered by whether the user has+ # connected them, not by which description happens to be shorter.coverage: float = 0.0connected: bool | None = Nonereason: str = "search" # "exact_id" | "exact_name" | "search"@@ -162,31 +167,47 @@ class CapabilityIndex:to_hit(idx, "exact_id" if _UUID_RE.match(query) else "exact_name")for idx in exact]- service, service_indices = self._service_query(query)+ service, service_indices, service_names = self._service_query(query)rest = [idx for idx in scores if idx not in exact]fallback: list[SearchHit] = []if service_indices is not None:- # A skill is not a service, but the one written for the named- # service is the best answer there is, so skills stay in; so do- # experts, since "someone to run my LinkedIn" names one.+ # A platform tool is first-party and belongs to no single service,+ # so it carries no service tag and the restriction dropped it:+ # "post a message to discord" could not return+ # post_to_chat_platform at all, only the Discord blocks. A tool+ # that names the service in its own text is about that service and+ # belongs here; one that merely shares a verb ("read a notion+ # page" matching read_workspace_file) does not. A skill is not a+ # service either, but the one written for the named service is+ # the best answer there is, so skills stay in; so do experts,+ # since "someone to run my LinkedIn" names one.main = [idxfor idx in restif idx in service_indicesor self.entries[idx].kind in ("skill", "expert")+ or self._tool_names(idx, service_names)]- others = [idx for idx in rest if idx not in main]- fallback = _ranked(+ chosen = set(main)+ others = [idx for idx in rest if idx not in chosen]+ fallback = rank_hits([to_hit(idx, "search")for idx in othersif self.entries[idx].klass == "primitive"])[:fallback_limit]+ unpinned = self._unpinned_exact(query, exact, main, connections)+ if unpinned:+ hits = [+ to_hit(idx, "exact_name") for idx in exact if idx not in unpinned+ ]+ main += unpinnedelse:main = rest- hits += _ranked(+ hits += rank_hits([to_hit(idx, "search") for idx in main],+ lift=service_indices is not None,mcp_first=prefer_mcpand service_indices is not Noneand any(self.entries[idx].kind == "mcp_server" for idx in main),@@ -277,7 +298,73 @@ class CapabilityIndex:indices += self._by_name.get(key[: -len("block")], [])return [idx for idx in dict.fromkeys(indices) if idx in allowed]- def _service_query(self, query: str) -> tuple[str | None, set[int] | None]:+ def _tool_names(self, idx: int, service_names: list[str]) -> bool:+ """Whether entry *idx* is a platform tool whose name, purpose or tags+ name one of the services the query asked for.++ Not the whole description: connect_integration says "do NOT call it+ for Google, Gmail, Slack", and matching that put it in every Google,+ Slack and GitHub query. A connection helper never joins, whatever it+ names: connect_integration's purpose lists "github", and it led every+ GitHub query over the GitHub blocks and MCP server."""+ entry = self.entries[idx]+ if entry.kind != "tool" or entry.name in CONNECTION_HELPER_TOOLS:+ return False+ document = {+ *tokenize(entry.name),+ *tokenize(entry.purpose),+ *(token for tag in entry.tags for token in tokenize(tag)),+ }+ for name in service_names:+ tokens = tokenize(name.replace("_", " "))+ if tokens and all(token in document for token in tokens):+ return True+ return False++ def _unpinned_exact(+ self,+ query: str,+ exact: list[int],+ main: list[int],+ connections: ConnectionState | None,+ ) -> list[int]:+ """Exact-name hits that rank with the rest instead of leading.++ An exact name normally leads the list, but "Linear create issue" is+ how AutoPilot phrases a job, not a request for the block that happens+ to be called that, and the pin put ``LinearCreateIssueBlock`` over+ the user's connected Linear MCP server. So a block the user holds no+ credential for is ranked like any other hit when the service query+ also found a connected server or a platform tool. A query that+ spells the class name ("LinearCreateIssueBlock", "linear create+ issue block") still asks for the block and keeps the pin, and so does+ a block the user has connected.+ """+ words = query.lower().split()+ if len(words) < 2 or words[-1] == "block":+ return []+ # An exact hit can be the alternative too: a tool sharing the block's+ # name is pinned itself, so it is never in *main*.+ alternative = any(+ self.entries[idx].kind == "tool"+ or (+ self.entries[idx].kind == "mcp_server"+ and resolve_connected(self.entries[idx], connections)+ )+ for idx in (*exact, *main)+ )+ if not alternative:+ return []+ return [+ idx+ for idx in exact+ if self.entries[idx].kind == "block"+ and resolve_connected(self.entries[idx], connections) is False+ ]++ def _service_query(+ self, query: str+ ) -> tuple[str | None, set[int] | None, list[str]]:"""Services named in the query restrict the main list to them.Every named service counts, not just the first: "send a linear issue@@ -315,8 +402,8 @@ class CapabilityIndex:if token in self._service_tags:take(token)if not named:- return None, None- return " ".join(named), indices+ return None, None, []+ return " ".join(named), indices, nameddef _coverage(groups: list[list[str]], doc: frozenset[str]) -> float:@@ -354,60 +441,3 @@ def _service_tags(entry: CapabilityEntry) -> Iterable[str]:else len(entry.tags))yield from (tag.lower() for tag in entry.tags[marker + 1 :])---def _ranked(hits: list[SearchHit], *, mcp_first: bool = False) -> list[SearchHit]:- """Coverage first; among equals a connected capability, then a platform- tool (first-party, no credentials, already trusted by the model), then- the class-weighted BM25 score (``score`` already carries the weight).-- ``mcp_first`` is for an expert chat that named a service: connection- still leads, but within a tier the service's MCP server comes before- its blocks, which stay listed for what the server cannot do."""- if mcp_first:- ranked = sorted(- hits,- key=lambda h: (- tier(h.entry, h.connected),- -h.coverage,- h.entry.kind != "tool",- -h.score,- h.entry.name.lower(),- ),- )- service_hits: dict[str, list[SearchHit]] = defaultdict(list)- for hit in ranked:- if hit.entry.service:- service_hits[hit.entry.service].append(hit)- for service, entries in service_hits.items():- service_hits[service] = sorted(- entries,- key=lambda h: (- tier(h.entry, h.connected),- h.entry.kind != "mcp_server",- -h.coverage,- h.entry.kind != "tool",- -h.score,- h.entry.name.lower(),- ),- )- offsets: dict[str, int] = defaultdict(int)- result: list[SearchHit] = []- for hit in ranked:- service = hit.entry.service- if not service:- result.append(hit)- continue- result.append(service_hits[service][offsets[service]])- offsets[service] += 1- return result- return sorted(- hits,- key=lambda h: (- -h.coverage,- tier(h.entry, h.connected),- h.entry.kind != "tool",- -h.score,- h.entry.name.lower(),- ),- )diff --git a/autogpt_platform/backend/backend/copilot/capabilities/index_test.py b/autogpt_platform/backend/backend/copilot/capabilities/index_test.pyindex cfb9e34b87..fd6dddde1f 100644--- a/autogpt_platform/backend/backend/copilot/capabilities/index_test.py+++ b/autogpt_platform/backend/backend/copilot/capabilities/index_test.py@@ -232,6 +232,324 @@ def test_eager_tools_are_flagged_in_listing(index):assert hit.entry.listing()["eager"] is True+# --- SECRT-2676: connected MCP and first-party tools must not lose to blocks ---++DISCORD_ID = "55555555-5555-5555-5555-555555555556"+DISCORD_READ_ID = "66666666-6666-6666-6666-666666666666"+LINEAR_MCP_URL = "https://mcp.linear.app/mcp"+SENTRY_MCP_URL = "https://mcp.sentry.dev/mcp"+SLACK_MCP_URL = "https://mcp.slack.com/mcp"+++def _mcp(entry_id, name, purpose, url, tags):+ return CapabilityEntry(+ id=entry_id,+ kind="mcp_server",+ name=name,+ purpose=purpose,+ tags=tags,+ context="direct",+ implementations=[Implementation(kind="mcp_server", ref=url)],+ connection=Connection(required=True, key_type="server_url", key=url),+ )+++def rival_index() -> CapabilityIndex:+ """A block and a connected service competing for the same job."""+ return CapabilityIndex(+ [+ _block(+ LINEAR_ID,+ "LinearCreateIssueBlock",+ "Create a new issue in Linear.",+ provider="linear",+ args=("title", "team"),+ ),+ _block(+ DISCORD_ID,+ "SendDiscordMessageBlock",+ "Send a message to a Discord channel.",+ provider="discord",+ args=("channel", "message"),+ ),+ # Covers every concept of the Discord query, so it sets the best+ # coverage the lift is measured against. Without a block of this+ # shape the fixture cannot reproduce a connected family of blocks+ # being carried over the first-party tool.+ _block(+ DISCORD_READ_ID,+ "ReadDiscordMessagesBlock",+ "Read and post messages on a Discord channel.",+ provider="discord",+ args=("channel",),+ ),+ _mcp(+ "mcp:mcp.linear.app",+ "Linear",+ "Search issues, projects, and documents, with optional "+ "updates to your team's work.",+ LINEAR_MCP_URL,+ ["linear", "mcp", "linear", "mcp.linear.app"],+ ),+ _mcp(+ "mcp:mcp.sentry.dev",+ "Sentry",+ "Investigate errors and performance, triage issues.",+ SENTRY_MCP_URL,+ ["sentry", "mcp", "sentry", "mcp.sentry.dev"],+ ),+ CapabilityEntry(+ id="tool:read_workspace_file",+ kind="tool",+ name="read_workspace_file",+ purpose="Read a file from the agent workspace.",+ tags=["workspace", "file"],+ context="direct",+ implementations=[+ Implementation(+ kind="tool", ref="read_workspace_file", context="direct"+ )+ ],+ argument_names=["path"],+ ),+ CapabilityEntry(+ id="tool:post_to_chat_platform",+ kind="tool",+ name="post_to_chat_platform",+ purpose=(+ "Post to a linked chat platform (Discord, Slack, "+ "Telegram or Microsoft Teams)."+ ),+ tags=["chat", "platform", "post"],+ context="direct",+ implementations=[+ Implementation(+ kind="tool", ref="post_to_chat_platform", context="direct"+ )+ ],+ argument_names=["platform", "channel", "content"],+ ),+ ]+ )+++def test_platform_tool_survives_a_service_query(rival_index):+ """Naming a service must not hide the first-party tool that does the job.++ The service restriction used to drop every tool, so this query could only+ ever return the Discord blocks.+ """+ result = rival_index.search("post a message to discord")+ assert result.service == "discord"+ assert "post_to_chat_platform" in result.names+ # It need not lead — an entry that covers every concept of the query may+ # legitimately rank above it — but it must beat the write block the bug+ # report watched it lose to.+ assert result.names.index("post_to_chat_platform") < result.names.index(+ "SendDiscordMessageBlock"+ )+++def test_platform_tool_survives_even_when_the_block_provider_is_connected(rival_index):+ """A Discord credential may be a read-only OAuth the write block cannot+ use, so the tool stays listed even when every block reads connected.++ A connected family of blocks must not be carried over the tool by the+ coverage lift: ReadDiscordMessagesBlock sets the best coverage here, and+ lifting the connected 2.0 blocks to it once pushed the tool off the list.+ """+ state = ConnectionState(providers=frozenset({"discord"}))+ result = rival_index.search("post a message to discord", connections=state)+ assert "post_to_chat_platform" in result.names+ assert result.names.index("post_to_chat_platform") < result.names.index(+ "SendDiscordMessageBlock"+ )+++def test_service_query_does_not_pull_in_unrelated_tools(rival_index):+ """The SECRT-2433 boundary again: a tool only joins a service list when it+ names that service, not merely because it shares a verb."""+ result = rival_index.search("read a discord message", connections=None)+ assert result.service == "discord"+ assert "read_workspace_file" not in result.names+ assert "post_to_chat_platform" in result.names+++def test_connected_mcp_outranks_a_block_for_the_same_job(rival_index):+ state = ConnectionState(server_urls=frozenset({LINEAR_MCP_URL}))+ result = rival_index.search("create a linear issue", connections=state)+ assert result.names[0] == "Linear"+ assert result.hits[0].connected is True+ assert "LinearCreateIssueBlock" in result.names+++def test_block_still_wins_when_nothing_is_connected(rival_index):+ """The SECRT-2433 boundary: with no connected alternative the block ranks+ exactly as it did before."""+ result = rival_index.search("create a linear issue", connections=ConnectionState())+ assert result.names[0] == "LinearCreateIssueBlock"+++def test_an_exact_block_name_in_a_phrase_does_not_pin_it_over_a_connected_mcp(+ rival_index,+):+ """AutoPilot searches "Linear create issue", which is also the block's+ name. With no Linear credential for the block, the connected server+ leads, and the block is still listed."""+ state = ConnectionState(server_urls=frozenset({LINEAR_MCP_URL}))+ result = rival_index.search("Linear create issue", connections=state)+ assert result.names[0] == "Linear"+ assert "LinearCreateIssueBlock" in result.names+ assert all(hit.reason == "search" for hit in result.hits)+++def test_an_exact_block_name_keeps_its_pin_when_the_block_is_connected(rival_index):+ state = ConnectionState(+ providers=frozenset({"linear"}), server_urls=frozenset({LINEAR_MCP_URL})+ )+ result = rival_index.search("Linear create issue", connections=state)+ assert result.names[0] == "LinearCreateIssueBlock"+ assert result.hits[0].reason == "exact_name"+++def test_a_query_spelling_the_class_name_keeps_the_pin(rival_index):+ state = ConnectionState(server_urls=frozenset({LINEAR_MCP_URL}))+ for query in ("LinearCreateIssueBlock", "linear create issue block"):+ result = rival_index.search(query, connections=state)+ assert result.names[0] == "LinearCreateIssueBlock", query+ assert result.hits[0].reason == "exact_name", query+++def test_an_exact_block_name_keeps_its_pin_with_no_connected_alternative(+ rival_index,+):+ for state in (ConnectionState(), None):+ result = rival_index.search("Linear create issue", connections=state)+ assert result.names[0] == "LinearCreateIssueBlock"+ assert result.hits[0].reason == "exact_name"+++def test_an_exact_tool_name_unpins_the_block_that_shares_it(rival_index):+ """A tool with the block's name is an exact hit itself, so it never+ reached the list of alternatives and the block kept its pin above it."""+ tool = CapabilityEntry(+ id="tool:linear_create_issue",+ kind="tool",+ name="linear_create_issue",+ purpose="Create an issue in Linear.",+ tags=["linear", "create", "issue"],+ context="direct",+ implementations=[+ Implementation(kind="tool", ref="linear_create_issue", context="direct")+ ],+ )+ result = rival_index.with_entries([tool]).search(+ "Linear create issue", connections=ConnectionState()+ )+ assert result.names[0] == "linear_create_issue"+ block = next(h for h in result.hits if h.entry.name == "LinearCreateIssueBlock")+ assert block.reason == "search"+++def test_graph_context_keeps_the_block_order(rival_index):+ """Building an agent lists blocks only: tools and MCP servers are+ direct-only, so neither the tool-first order nor the MCP lift nor the+ unpin can move a buildable block, and graph-only blocks stay listed."""+ layered = rival_index.with_entries(+ [_block(INPUT_ID, "AgentInputBlock", "Graph input.", context="graph")]+ )+ state = ConnectionState(+ providers=frozenset({"discord"}), server_urls=frozenset({LINEAR_MCP_URL})+ )+ pinned = layered.search("Linear create issue", context="graph", connections=state)+ assert pinned.names[0] == "LinearCreateIssueBlock"+ assert pinned.hits[0].reason == "exact_name"+ for query in ("post a message to discord", "Linear create issue"):+ result = layered.search(query, context="graph", connections=state)+ assert {h.entry.kind for h in result.hits} == {"block"}, query+ assert layered.search("agent input", context="graph").names == ["AgentInputBlock"]+++def test_a_distant_connected_service_is_not_lifted(rival_index):+ """A connected server the query does not name competes on coverage like+ anything else: Sentry matches only "issue" here, so it must not displace+ the block that matches both words. The query names no service, so+ nothing is filtered out before ranking and the lift itself is tested."""+ state = ConnectionState(server_urls=frozenset({SENTRY_MCP_URL}))+ result = rival_index.search("create issue", connections=state)+ assert result.service is None+ assert "Sentry" in result.names+ assert result.names[0] == "LinearCreateIssueBlock"+++def test_a_connected_server_is_not_lifted_on_a_query_that_does_not_name_it(+ rival_index,+):+ """kcze's case on #15011: Linear matches "update" through "updates" in+ its description, half of a two-word query, and was lifted over the block+ that matches both words the moment it was connected."""+ sheets = _block(+ "77777777-7777-7777-7777-777777777777",+ "GoogleSheetsUpdateCellBlock",+ "Update a single cell in a Google Sheets spreadsheet.",+ provider="google_sheets",+ args=("spreadsheet_id", "cell", "value"),+ tags=("spreadsheet",),+ )+ index = rival_index.with_entries([sheets])+ state = ConnectionState(server_urls=frozenset({LINEAR_MCP_URL}))+ result = index.search("update spreadsheet", connections=state)+ assert result.service is None+ assert "Linear" in result.names+ assert result.names[0] == "GoogleSheetsUpdateCellBlock"+ unconnected = index.search("update spreadsheet", connections=None)+ assert unconnected.names[0] == "GoogleSheetsUpdateCellBlock"+++def test_an_unrelated_connected_server_does_not_lead_a_service_less_query(+ rival_index,+):+ """A connected Slack server shares only "send" with "send email"; the+ email block that matches both words still leads. The block is not+ called SendEmailBlock, which the query would pin as an exact name."""+ email = _block(+ "88888888-8888-8888-8888-888888888888",+ "GmailSendBlock",+ "Send an email from a Gmail account.",+ provider="google",+ args=("to", "subject", "body"),+ tags=("email",),+ )+ slack = _mcp(+ "mcp:mcp.slack.com",+ "Slack",+ "Send messages, search channels and read threads in Slack.",+ SLACK_MCP_URL,+ ["slack", "mcp", "slack", "mcp.slack.com"],+ )+ state = ConnectionState(server_urls=frozenset({SLACK_MCP_URL}))+ result = rival_index.with_entries([email, slack]).search(+ "send email", connections=state+ )+ assert result.service is None+ assert "Slack" in result.names+ assert result.names[0] == "GmailSendBlock"+++def test_the_lift_stays_one_concept_wide_on_a_query_naming_the_server(+ rival_index,+):+ """Naming the service lets its connected server be lifted, but only past a+ one-concept gap: here the block matches two more words than Linear."""+ state = ConnectionState(server_urls=frozenset({LINEAR_MCP_URL}))+ result = rival_index.search("linear create issue title", connections=state)+ assert result.service == "linear"+ by_name = {hit.entry.name: hit.coverage for hit in result.hits}+ assert by_name["LinearCreateIssueBlock"] - by_name["Linear"] > 1+ assert result.names[0] == "LinearCreateIssueBlock"++# ------------------------------------------------- the per-session skill layer@@ -300,7 +618,136 @@ def test_a_platform_entry_keeps_a_bare_ref_a_skill_shares(index):assert layered.get("skill:web_search") is clone-def _mcp(host: str, name: str, service: str, purpose: str):+def test_a_service_query_keeps_both_skills_and_platform_tools(rival_index):+ """Both layers that carry no service tag stay in a service query's main+ list: the owner's skill for the service and the first-party tool."""+ announce = _skill("discord-announce", "Post a release announcement to Discord.")+ result = rival_index.with_entries([announce]).search("post to discord")+ assert result.service == "discord"+ assert {"discord-announce", "post_to_chat_platform"} <= set(result.names)+ assert not {"discord-announce", "post_to_chat_platform"} & {+ h.entry.name for h in result.fallback+ }+++# --- SECRT-2820: a kind filter must not hide the user's connected blocks ---++GMAIL_LIST_ID = "99999999-9999-9999-9999-999999999991"+GMAIL_READ_ID = "99999999-9999-9999-9999-999999999992"+GOOGLE = ConnectionState(providers=frozenset({"google"}))+++def _tool(name, purpose, *, description="", tags=()):+ return CapabilityEntry(+ id=f"tool:{name}",+ kind="tool",+ name=name,+ purpose=purpose,+ description=description or purpose,+ tags=sorted({*name.split("_"), *tags}),+ context="direct",+ implementations=[Implementation(kind="tool", ref=name, context="direct")],+ )+++CONNECT_INTEGRATION = _tool(+ "connect_integration",+ "Prompt the user to connect a required integration. Supported providers: "+ "'github'.",+ description=(+ "Prompt the user to connect a required integration. Supported "+ "providers: 'github'. ONLY call this tool for one of the supported "+ "providers listed above - do NOT call it for Google, Gmail, Slack, or "+ "any other provider not in the list."+ ),+)+++def gmail_index(rival_index) -> CapabilityIndex:+ """The 2901dcfb shape: Gmail is a family of Google blocks, and the tools+ that share a word with the query only by accident."""+ return rival_index.with_entries(+ [+ _block(+ GMAIL_LIST_ID,+ "GmailListLabelsBlock",+ "Retrieve all labels from a Gmail account for organising emails.",+ provider="google",+ tags=("email",),+ ),+ _block(+ GMAIL_READ_ID,+ "GmailReadBlock",+ "Read recent emails from a Gmail inbox.",+ provider="google",+ args=("query", "max_results"),+ tags=("email",),+ ),+ CONNECT_INTEGRATION,+ _tool("web_search", "Search the web for live info (news, recent docs)."),+ _tool("list_skills", "List the skills available here."),+ _tool(+ "create_feature_request",+ "File a feature request; the team follows up by email.",+ ),+ ]+ )+++def test_a_tool_does_not_lead_connected_blocks_on_a_query_naming_no_service(+ gmail_index,+):+ """ "email" names no service; create_feature_request only ties on+ coverage, so the user's connected Gmail blocks lead. #15011 put tools+ first for every query and moved 19 such queries."""+ result = gmail_index.search("email", connections=GOOGLE)+ assert result.service is None+ assert result.hits[0].entry.kind == "block"+ assert result.hits[0].connected is True+ assert "create_feature_request" in result.names+++def test_a_negative_mention_does_not_pull_a_tool_into_a_service_query(gmail_index):+ """connect_integration says "do NOT call it for Google, Gmail, Slack":+ only a tool's name, purpose and tags count as naming the service."""+ slack = _mcp(+ "mcp:mcp.slack.com",+ "Slack",+ "Send messages, search channels and read threads in Slack.",+ SLACK_MCP_URL,+ ["slack", "mcp", "slack", "mcp.slack.com"],+ )+ result = gmail_index.with_entries([slack]).search("slack send message")+ assert result.service == "slack"+ assert "connect_integration" not in result.names+ assert "post_to_chat_platform" in result.names+++def test_a_connection_helper_does_not_join_a_service_query(index):+ """connect_integration's purpose names "github" and its description+ "credential issues", so it tied the GitHub blocks on "github issue" and,+ as a tool, led the list. It only connects an account for the sandbox."""+ helper = _tool(+ "connect_integration",+ "Prompt the user to connect a required integration. Supported "+ "providers: 'github'.",+ description=(+ "Prompt the user to connect a required integration. Supported "+ "providers: 'github'. Do NOT call this for agent block credential "+ "issues."+ ),+ )+ result = index.with_entries([helper]).search("github issue")+ assert result.service == "github"+ assert result.names[0] == "GithubMakeIssueBlock"+ assert "connect_integration" not in result.names+++# --- #15199: an expert chat lists a service's MCP server before its blocks ---+++def _service_mcp(host: str, name: str, service: str, purpose: str):return CapabilityEntry(id=f"mcp:{host}",kind="mcp_server",@@ -330,13 +777,15 @@ def twin_index() -> CapabilityIndex:return CapabilityIndex([linear_block,- _mcp(+ _service_mcp("mcp.linear.app","Linear","linear","Issues, projects and cycles in Linear.",),- _mcp("mcp.sentry.dev", "Sentry", "sentry", "Errors and traces in Sentry."),+ _service_mcp(+ "mcp.sentry.dev", "Sentry", "sentry", "Errors and traces in Sentry."+ ),])@@ -407,7 +856,7 @@ def test_prefer_mcp_does_not_move_one_service_ahead_of_another():[linear_block,github_block,- _mcp(+ _service_mcp("mcp.linear.app","Linear","linear",@@ -426,3 +875,43 @@ def test_prefer_mcp_does_not_move_one_service_ahead_of_another():assert result.ids.index("mcp:mcp.linear.app") < result.ids.index(f"block:{LINEAR_ID}")+++def test_an_expert_chat_with_a_service_mcp_server_ranks_connection_first(+ rival_index,+):+ """Where both orders could apply, #15199's wins: an expert chat whose+ service query found that service's MCP server ranks connection first, so+ the connected Discord block leads the first-party tool it would otherwise+ follow. The tool is still listed."""+ discord_ids = {f"block:{DISCORD_ID}", f"block:{DISCORD_READ_ID}"}+ index = CapabilityIndex(+ [+ (+ entry.model_copy(update={"service": "discord"})+ if entry.id in discord_ids+ else entry+ )+ for entry in rival_index.entries+ ]+ + [+ _service_mcp(+ "mcp.discord.example",+ "Discord",+ "discord",+ "Read and post messages in Discord channels.",+ )+ ]+ )+ state = ConnectionState(providers=frozenset({"discord"}))+ query = "post a message to discord"++ expert = index.search(query, connections=state, prefer_mcp=True)+ assert expert.names.index("SendDiscordMessageBlock") < expert.names.index(+ "post_to_chat_platform"+ )++ default = index.search(query, connections=state)+ assert default.names.index("post_to_chat_platform") < default.names.index(+ "SendDiscordMessageBlock"+ )diff --git a/autogpt_platform/backend/backend/copilot/capabilities/ordering.py b/autogpt_platform/backend/backend/copilot/capabilities/ordering.pynew file mode 100644index 0000000000..12bbd8083f--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/capabilities/ordering.py@@ -0,0 +1,91 @@+"""Service-aware ordering of capability search hits."""++from __future__ import annotations++from collections import defaultdict+from typing import TYPE_CHECKING++from .ranking import tier++if TYPE_CHECKING:+ from .index import SearchHit++CONNECTED_COVERAGE_MARGIN = 1.0+++def rank_hits(+ hits: list[SearchHit], *, lift: bool = False, mcp_first: bool = False+) -> list[SearchHit]:+ """Rank coverage before connection, first-party tools, and weighted BM25.++ ``lift`` applies only after filtering to a named service. It prioritizes+ tools on coverage ties and lets a connected MCP server cover one fewer+ concept: server descriptions name the service, not every action it offers.+ Queries without a service retain strict coverage and connection ordering.++ ``mcp_first`` replaces that order for expert service searches: connection+ leads, with MCP ahead of the same service's blocks within a tier.+ """+ if mcp_first:+ return _mcp_order(hits)+ return _coverage_order(hits, lift=lift)+++def _mcp_order(hits: list[SearchHit]) -> list[SearchHit]:+ ranked = sorted(+ hits,+ key=lambda h: (+ tier(h.entry, h.connected),+ -h.coverage,+ h.entry.kind != "tool",+ -h.score,+ h.entry.name.lower(),+ ),+ )+ service_hits: dict[str, list[SearchHit]] = defaultdict(list)+ for hit in ranked:+ if hit.entry.service:+ service_hits[hit.entry.service].append(hit)+ for service, entries in service_hits.items():+ service_hits[service] = sorted(entries, key=_mcp_service_key)+ offsets: dict[str, int] = defaultdict(int)+ result: list[SearchHit] = []+ for hit in ranked:+ service = hit.entry.service+ if not service:+ result.append(hit)+ continue+ result.append(service_hits[service][offsets[service]])+ offsets[service] += 1+ return result+++def _mcp_service_key(hit: SearchHit) -> tuple[int, bool, float, bool, float, str]:+ return (+ tier(hit.entry, hit.connected),+ hit.entry.kind != "mcp_server",+ -hit.coverage,+ hit.entry.kind != "tool",+ -hit.score,+ hit.entry.name.lower(),+ )+++def _coverage_order(hits: list[SearchHit], *, lift: bool) -> list[SearchHit]:+ best = max((h.coverage for h in hits), default=0.0)++ def key(hit: SearchHit) -> tuple[float, int, int, float, str]:+ rank = tier(hit.entry, hit.connected)+ not_tool = int(hit.entry.kind != "tool")+ coverage = hit.coverage+ if (+ lift+ and hit.entry.kind == "mcp_server"+ and rank == 0+ and coverage >= best - CONNECTED_COVERAGE_MARGIN+ ):+ coverage = best+ first, second = (not_tool, rank) if lift else (rank, not_tool)+ return (-coverage, first, second, -hit.score, hit.entry.name.lower())++ return sorted(hits, key=key)diff --git a/autogpt_platform/backend/backend/copilot/capabilities/ranking.py b/autogpt_platform/backend/backend/copilot/capabilities/ranking.pyindex 187a189beb..8964232ff1 100644--- a/autogpt_platform/backend/backend/copilot/capabilities/ranking.py+++ b/autogpt_platform/backend/backend/copilot/capabilities/ranking.py@@ -3,7 +3,7 @@Order of preference, from the plan: connected service > unconnected service> primitive with credentials for the host > bare primitive. It decidesbetween entries that match the query equally well; it never lifts a weak-lexical match over a strong one (see ``index._ranked``).+lexical match over a strong one (see ``ordering.rank_hits``)."""from pydantic import BaseModel, ConfigDict, Fielddiff --git a/autogpt_platform/backend/backend/copilot/capabilities/sources/static_tools.py b/autogpt_platform/backend/backend/copilot/capabilities/sources/static_tools.pyindex 5716930c0c..7758f165ad 100644--- a/autogpt_platform/backend/backend/copilot/capabilities/sources/static_tools.py+++ b/autogpt_platform/backend/backend/copilot/capabilities/sources/static_tools.py@@ -83,6 +83,7 @@ def _tool_entry(name: str, tool: "BaseTool", group: str | None) -> CapabilityEnttags = sorted(set(tokenize(name)))if group:tags.append(group)+ tags += tool.search_keywordsreturn CapabilityEntry(id=f"tool:{name}",kind="tool",diff --git a/autogpt_platform/backend/backend/copilot/prompting.py b/autogpt_platform/backend/backend/copilot/prompting.pyindex 77d970a8ee..626f8cfcee 100644--- a/autogpt_platform/backend/backend/copilot/prompting.py+++ b/autogpt_platform/backend/backend/copilot/prompting.py@@ -204,6 +204,16 @@ three primitives under `fallback`; `SendAuthenticatedWebRequestBlock` calls avendor API directly with the user's host-scoped credentials when nothing elsecovers the service.+When more than one capability covers the job, prefer what the user has+connected (`connected: true`), then a platform tool (`tool:<name>`) over a+block. Use a block when only it covers the job, or when the user asked for it.++To post to Discord, Slack, Telegram or Teams, use `tool:post_to_chat_platform`+when its listing names that platform in `linked` (`dm_linked` for a DM to the+user): it posts as the AutoGPT bot with no credential into a linked server's+channels or the user's own DMs. For anywhere else, or with nothing linked, use+the user's connected block, or tell them they can link the platform.+If `find_capability` returns nothing for a named service, `web_search` for"<service> MCP server" and call `run_capability` with the server URL as `id`.Verify the hostname belongs to the vendor first; if several candidates exist,diff --git a/autogpt_platform/backend/backend/copilot/tools/base.py b/autogpt_platform/backend/backend/copilot/tools/base.pyindex b838d44329..79d8295e03 100644--- a/autogpt_platform/backend/backend/copilot/tools/base.py+++ b/autogpt_platform/backend/backend/copilot/tools/base.py@@ -379,6 +379,14 @@ class BaseTool:"""``parameters`` as the model is sent them: without the card's hints."""return without_card_hints(self.parameters)+ @property+ def search_keywords(self) -> tuple[str, ...]:+ """Extra words ``find_capability`` indexes for this tool.++ Index-only: never sent to the model, so they cost no schema budget.+ """+ return ()+@propertydef requires_auth(self) -> bool:"""Whether this tool requires authentication."""diff --git a/autogpt_platform/backend/backend/copilot/tools/chat_platform.py b/autogpt_platform/backend/backend/copilot/tools/chat_platform.pyindex b05fea824d..3a9ca7f00a 100644--- a/autogpt_platform/backend/backend/copilot/tools/chat_platform.py+++ b/autogpt_platform/backend/backend/copilot/tools/chat_platform.py@@ -45,6 +45,11 @@ logger = logging.getLogger(__name__)# the tool surface stays the same.SUPPORTED_PLATFORMS: tuple[str, ...] = ("discord", "slack", "telegram", "teams")+# A search naming a platform ("discord") keeps a platform tool only when its+# indexed text names that platform, so each one is indexed by name whatever+# the description says; "microsoft" for "microsoft teams".+_PLATFORM_KEYWORDS: tuple[str, ...] = (*SUPPORTED_PLATFORMS, "microsoft")+# Telegram's Bot API can't enumerate a bot's chats, so name→ID resolution is# impossible there — posts must target a linked group's numeric chat ID._TELEGRAM_TARGETING_HINT = (@@ -123,9 +128,13 @@ _ERROR_MESSAGES: dict[str, str] = {}-@lru_cache(maxsize=1)def _any_chat_platform_configured() -> bool:- """Whether any chat-platform bot is configured, gating tool availability.+ return bool(_configured_chat_platforms())+++@lru_cache(maxsize=1)+def _configured_chat_platforms() -> frozenset[str]:+ """The chat platforms this deployment can deliver to.Cached because ``get_available_tools`` reads ``is_available`` for every toolon every request, and constructing ``Settings()`` re-parses the env each@@ -159,11 +168,15 @@ def _any_chat_platform_configured() -> bool:and secrets.microsoft_client_secretand secrets.microsoft_tenant_id)- return bool(- secrets.autopilot_bot_discord_token- or slack_configured- or telegram_configured- or teams_configured+ return frozenset(+ platform+ for platform, configured in {+ "discord": bool(secrets.autopilot_bot_discord_token),+ "slack": slack_configured,+ "telegram": telegram_configured,+ "teams": teams_configured,+ }.items()+ if configured)@@ -222,21 +235,25 @@ class PostToChatPlatformTool(BaseTool):@propertydef description(self) -> str:return (- "Post to a linked chat platform (Discord, Slack, Telegram or "- "Microsoft Teams). target='dm' sends to the user's own DMs with "- "the bot. mode='thread' opens a thread (needs thread_name; "- "channels only). 'channel' is a name (#standup) or numeric ID — "- "on Telegram, a linked group's numeric chat ID. Teams supports "- "target='dm' only; its channels cannot be posted to yet. Pair "- "with tool:schedule_followup for recurring posts; call "+ "Send a message, report or update as the AutoGPT bot to Discord, "+ "Slack, Telegram or Microsoft Teams where the user linked it; no "+ "credential needed. target='dm' DMs the user from the bot. "+ "mode='thread' opens a thread (needs thread_name; channels only). "+ "'channel' is a name (#standup) or numeric ID — on Telegram, a "+ "linked group's numeric chat ID. Teams supports target='dm' only. "+ "Pair with tool:schedule_followup for recurring posts; call ""tool:list_chat_platform_channels if a Discord/Slack channel won't "- "resolve. Whatever this posts can later be changed with "- "tool:edit_chat_platform_message using the channel_id and ref_id it "- "returns — for mode='thread' those address the body message "- "inside the new thread, so posting again with that channel_id "+ "resolve. Edit a post later with tool:edit_chat_platform_message "+ "using the channel_id and ref_id it returns — for mode='thread' "+ "those address the body message inside the new thread, so posting "+ "again with that channel_id ""continues the thread.")+ @property+ def search_keywords(self) -> tuple[str, ...]:+ return _PLATFORM_KEYWORDS+@propertydef requires_auth(self) -> bool:return True@@ -439,7 +456,7 @@ class EditChatPlatformMessageTool(BaseTool):"the bot's replies to anyone, not another user's posts, and not a ""long post that was split across several messages. A failure ""(message too old, deleted, not yours, or the platform rejecting "- "the edit) is always reported, never silent."+ "the edit) returns an error, never silent.")@propertydiff --git a/autogpt_platform/backend/backend/copilot/tools/chat_platform_hint.py b/autogpt_platform/backend/backend/copilot/tools/chat_platform_hint.pynew file mode 100644index 0000000000..674a612d96--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/tools/chat_platform_hint.py@@ -0,0 +1,121 @@+"""Point the model at the AutoGPT bot where a send block wants a bot token.++``SendDiscordMessageBlock`` and its kin log in with a bot token the user must+bring. ``post_to_chat_platform`` posts as the AutoGPT bot with no credential+wherever the user linked that platform to it, which no credential records:+the ``find_capability`` listing says where that is, and a send block's setup+card names the bot as the other way to post.+"""++import asyncio+import logging+import re+from typing import Any++from pydantic import BaseModel++from backend.copilot.context import get_current_permissions+from backend.data.db_accessors import platform_linking_db++from .chat_platform import _configured_chat_platforms++logger = logging.getLogger(__name__)++POST_TOOL = "post_to_chat_platform"++# Blocks whose whole job post_to_chat_platform does, by the platform it posts+# to. Embeds, files, reads and lookups are not posts the tool can make, and+# neither is SendDiscordDMBlock: it DMs any user, the bot only the user.+BOT_TOKEN_SEND_BLOCKS: dict[str, str] = {+ "SendDiscordMessageBlock": "discord",+ "CreateDiscordThreadBlock": "discord",+ "SendSlackMessageBlock": "slack",+ "SendTelegramMessageBlock": "telegram",+}+++def bot_link_hint(block_name: str, input_data: dict[str, Any]) -> str:+ """The other way to post, appended to the card when the bot could post+ this once the user links where it goes."""+ platform = _bot_platform(block_name)+ if platform is None or _beyond_the_bot(block_name, input_data):+ return ""+ where = _PLACE[platform]+ if platform != "telegram":+ where += " that channel is in"+ return (+ f" Or, with no token: if the user links the {platform.title()} {where} "+ f"to the AutoGPT bot (its setup command), tool:{POST_TOOL} can post "+ "there as the bot."+ )+++# What the user links to the bot on each platform.+_PLACE = {"discord": "server", "slack": "workspace", "telegram": "group"}++# A Slack member (U…, W…) or DM conversation (D…): the bot DMs only the user.+_SLACK_DM = re.compile(r"^(@|[UWD][A-Z0-9]{7,}$)")+++def _beyond_the_bot(block_name: str, input_data: dict[str, Any]) -> bool:+ """Whether the block was asked for something the bot never does, linked+ or not: a DM to someone else, a reply into a thread, a private thread."""+ if block_name == "SendSlackMessageBlock":+ channel = str(input_data.get("channel") or "").strip()+ return bool(input_data.get("thread_ts")) or bool(_SLACK_DM.match(channel))+ if block_name == "CreateDiscordThreadBlock":+ return bool(input_data.get("is_private"))+ if block_name == "SendTelegramMessageBlock":+ if input_data.get("reply_to_message_id"):+ return True+ # A positive chat id is a private chat with a person, not a group.+ chat_id = _telegram_chat_id(input_data)+ return chat_id is not None and int(chat_id) > 0+ return False+++def _telegram_chat_id(input_data: dict[str, Any]) -> str | None:+ chat_id = input_data.get("chat_id")+ if chat_id is None:+ return None+ try:+ return str(int(chat_id))+ except (TypeError, ValueError):+ return None+++def _bot_platform(block_name: str) -> str | None:+ """The platform the bot could post to in this block's place, when this+ deployment runs the bot and this turn may call the tool."""+ platform = BOT_TOKEN_SEND_BLOCKS.get(block_name)+ if platform is None or platform not in _configured_chat_platforms():+ return None+ permissions = get_current_permissions()+ if permissions is not None and POST_TOOL not in (+ permissions.effective_allowed_tools(frozenset({POST_TOOL}))+ ):+ return None+ return platform+++class ChatPlatformLinks(BaseModel):+ """Platforms the user linked to the bot: a server, where it can post into+ channels, or their own DMs, where it can DM them."""++ servers: frozenset[str] = frozenset()+ dms: frozenset[str] = frozenset()+++async def chat_platform_links(user_id: str) -> ChatPlatformLinks:+ try:+ db = platform_linking_db()+ servers, users = await asyncio.gather(+ db.list_server_links(user_id), db.list_user_links(user_id)+ )+ except Exception:+ logger.warning("Could not read chat platform links", exc_info=True)+ return ChatPlatformLinks()+ return ChatPlatformLinks(+ servers=frozenset(link.platform.lower() for link in servers),+ dms=frozenset(link.platform.lower() for link in users),+ )diff --git a/autogpt_platform/backend/backend/copilot/tools/chat_platform_hint_config_test.py b/autogpt_platform/backend/backend/copilot/tools/chat_platform_hint_config_test.pynew file mode 100644index 0000000000..35ef7d99f4--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/tools/chat_platform_hint_config_test.py@@ -0,0 +1,81 @@+from types import SimpleNamespace+from unittest.mock import patch++import pytest++from . import chat_platform+from .chat_platform_hint import bot_link_hint+++def bot_secrets():+ secrets = SimpleNamespace(+ autopilot_bot_discord_token="",+ autopilot_bot_slack_token="",+ autopilot_bot_slack_signing_secret="",+ autopilot_bot_slack_client_id="",+ autopilot_bot_slack_client_secret="",+ autopilot_bot_telegram_token="",+ autopilot_bot_telegram_webhook_secret="",+ microsoft_client_id="",+ microsoft_client_secret="",+ microsoft_tenant_id="",+ )+ chat_platform._configured_chat_platforms.cache_clear()+ with (+ patch.object(+ chat_platform, "Settings", return_value=SimpleNamespace(secrets=secrets)+ ),+ patch(+ "backend.copilot.tools.chat_platform_hint.get_current_permissions",+ return_value=None,+ ),+ ):+ yield secrets+ chat_platform._configured_chat_platforms.cache_clear()+++ "platform,overrides",+ [+ ("discord", {"autopilot_bot_discord_token": "test-token"}),+ (+ "slack",+ {+ "autopilot_bot_slack_token": "test-token",+ "autopilot_bot_slack_signing_secret": "test-secret",+ },+ ),+ (+ "telegram",+ {+ "autopilot_bot_telegram_token": "test-token",+ "autopilot_bot_telegram_webhook_secret": "test-secret",+ },+ ),+ (+ "teams",+ {+ "microsoft_client_id": "test-id",+ "microsoft_client_secret": "test-secret",+ "microsoft_tenant_id": "test-tenant",+ },+ ),+ ],+)+ "block_name,block_platform",+ [+ ("SendDiscordMessageBlock", "discord"),+ ("CreateDiscordThreadBlock", "discord"),+ ("SendSlackMessageBlock", "slack"),+ ("SendTelegramMessageBlock", "telegram"),+ ],+)+def test_hint_requires_its_own_platform(+ bot_secrets, platform, overrides, block_name, block_platform+):+ for name, value in overrides.items():+ setattr(bot_secrets, name, value)++ assert bool(bot_link_hint(block_name, {})) is (block_platform == platform)diff --git a/autogpt_platform/backend/backend/copilot/tools/chat_platform_hint_test.py b/autogpt_platform/backend/backend/copilot/tools/chat_platform_hint_test.pynew file mode 100644index 0000000000..96071053ab--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/tools/chat_platform_hint_test.py@@ -0,0 +1,397 @@+"""A bot-token send block with no token names the AutoGPT bot as the other+way to post, where the bot could post it, and find_capability says where the+user linked a platform to the bot."""++from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.copilot.capabilities.index import CapabilityIndex+from backend.copilot.capabilities.ranking import ConnectionState+from backend.copilot.capabilities.registry import build_entries+from backend.copilot.permissions import CopilotPermissions+from backend.copilot.tools import TOOL_GROUPS, TOOL_REGISTRY, chat_platform++from ._test_data import make_session+from .chat_platform_hint import POST_TOOL+from .find_capability import FindCapabilityTool+from .helpers import prepare_block_for_execution+from .models import CapabilityListResponse, ErrorResponse, SetupRequirementsResponse++_USER = "hint-user"+_SESSION = "hint-session"+_HINT = "backend.copilot.tools.chat_platform_hint"+++def _block(name: str) -> MagicMock:+ block = MagicMock()+ block.id = "blk-send"+ block.name = name+ block.disabled = False+ block.description = ""+ block.block_type = MagicMock()+ block.input_schema.jsonschema.return_value = {+ "type": "object",+ "properties": {"message_content": {"type": "string"}},+ "required": [],+ }+ block.input_schema.get_credentials_fields.return_value = {}+ block.input_schema.get_credentials_fields_info.return_value = {}+ return block+++_DISCORD = {"channel_name": "#general", "message_content": "test"}+++def _links(*platforms: str, dm: tuple[str, ...] = ()) -> MagicMock:+ db = MagicMock()+ db.list_server_links = AsyncMock(+ return_value=[SimpleNamespace(platform=p) for p in platforms]+ )+ db.list_user_links = AsyncMock(+ return_value=[SimpleNamespace(platform=p) for p in dm]+ )+ return db+++async def _prepare(+ block_name: str,+ *,+ bot_configured: bool = True,+ unattended: bool = False,+ permissions: CopilotPermissions | None = None,+ pins: dict | None = None,+ grant_hint: str = "",+ input_data: dict | None = None,+):+ session = MagicMock()+ session.session_id = _SESSION+ session.expert_id = None+ missing = MagicMock()+ missing.provider = "discord"+ with (+ patch(+ "backend.copilot.tools.helpers.get_block",+ return_value=_block(block_name),+ ),+ patch(+ "backend.copilot.capabilities.block_meta.COPILOT_EXCLUDED_BLOCK_IDS",+ new=set(),+ ),+ patch(+ "backend.copilot.capabilities.block_meta.COPILOT_EXCLUDED_BLOCK_TYPES",+ new=set(),+ ),+ patch(+ "backend.copilot.tools.helpers.resolve_block_credentials",+ AsyncMock(return_value=({}, [missing])),+ ),+ patch(+ "backend.copilot.tools.helpers.build_missing_credentials_from_field_info",+ return_value={"credentials": {"provider": "discord"}},+ ),+ patch(+ "backend.copilot.tools.helpers.is_unattended_turn",+ return_value=unattended,+ ),+ patch(+ f"{_HINT}._configured_chat_platforms",+ return_value=(+ frozenset({"discord", "slack", "telegram"})+ if bot_configured+ else frozenset()+ ),+ ),+ patch(+ "backend.copilot.tools.helpers.turn_credential_pins",+ return_value=pins or {},+ ),+ patch(+ "backend.copilot.tools.helpers.ungranted_credential_hint",+ AsyncMock(return_value=grant_hint),+ ),+ patch(+ "backend.copilot.tools.helpers.unattended_missing_credentials_error",+ AsyncMock(side_effect=_unattended_error),+ ),+ patch(f"{_HINT}.get_current_permissions", return_value=permissions),+ patch(+ "backend.copilot.tools.helpers.get_current_permissions",+ return_value=permissions,+ ),+ ):+ return await prepare_block_for_execution(+ block_id="blk-send",+ input_data=_DISCORD if input_data is None else input_data,+ user_id=_USER,+ session=session,+ session_id=_SESSION,+ dry_run=False,+ )+++async def _unattended_error(subject, missing, session_id, user_id, expert_id):+ return ErrorResponse(+ message=f"{subject} has no discord credential connected, so this step "+ "did not run.",+ error="missing_credentials",+ session_id=session_id,+ )+++async def test_a_send_block_keeps_the_card_and_names_the_bot_as_the_other_way():+ result = await _prepare("SendDiscordMessageBlock")++ assert isinstance(result, SetupRequirementsResponse)+ assert "links the Discord server that channel is in" in result.message+ assert "tool:post_to_chat_platform" in result.message+++async def test_a_scheduled_turn_still_skips_and_names_the_bot():+ result = await _prepare("SendDiscordMessageBlock", unattended=True)++ assert isinstance(result, ErrorResponse)+ assert result.error == "missing_credentials"+ assert "links the Discord server that channel is in" in result.message+++async def test_the_dm_block_gets_no_bot_hint():+ """SendDiscordDMBlock DMs any user id; the bot can DM only the user."""+ result = await _prepare("SendDiscordDMBlock")++ assert isinstance(result, SetupRequirementsResponse)+ assert "post_to_chat_platform" not in result.message+++async def test_a_block_the_tool_cannot_replace_gets_no_bot_hint():+ result = await _prepare("DiscordGetCurrentUserBlock")++ assert isinstance(result, SetupRequirementsResponse)+ assert "post_to_chat_platform" not in result.message+++async def test_no_bot_on_this_deployment_gets_no_bot_hint():+ result = await _prepare("SendDiscordMessageBlock", bot_configured=False)++ assert isinstance(result, SetupRequirementsResponse)+ assert "post_to_chat_platform" not in result.message+++async def test_a_turn_that_may_not_post_gets_no_bot_hint():+ result = await _prepare(+ "SendDiscordMessageBlock",+ permissions=CopilotPermissions(+ tools=["post_to_chat_platform"], tools_exclude=True+ ),+ )++ assert isinstance(result, SetupRequirementsResponse)+ assert "post_to_chat_platform" not in result.message+++async def test_a_schedule_pinned_to_a_discord_account_keeps_its_own_error():+ """The owner picked a Discord account for this routine; a run that cannot+ use it must say so, not point at the bot instead."""+ result = await _prepare(+ "SendDiscordMessageBlock", unattended=True, pins={"discord": MagicMock()}+ )++ assert isinstance(result, ErrorResponse)+ assert result.error == "missing_credentials"+ assert "post_to_chat_platform" not in result.message+++async def test_an_ungranted_discord_credential_keeps_the_grant_card():+ """The account owns a Discord credential this expert was not granted:+ granting it is the fix the owner set up, not the bot."""+ hint = " The account already has a discord credential: grant it."+ result = await _prepare("SendDiscordMessageBlock", grant_hint=hint)++ assert isinstance(result, SetupRequirementsResponse)+ assert hint in result.message+ assert "post_to_chat_platform" not in result.message+++async def test_an_ungranted_credential_on_a_scheduled_turn_gets_no_bot_hint():+ hint = " The account already has a discord credential: grant it."+ result = await _prepare("SendDiscordMessageBlock", unattended=True, grant_hint=hint)++ assert isinstance(result, ErrorResponse)+ assert "post_to_chat_platform" not in result.message+++# The bot posts only into a public channel or thread of a linked server or+# group, or DMs the user: the hint stays off anything else it can't do.+++async def test_a_private_discord_thread_gets_no_bot_hint():+ """The bot opens public threads only."""+ result = await _prepare(+ "CreateDiscordThreadBlock",+ input_data={+ "channel_name": "#general",+ "thread_name": "Standup",+ "is_private": True,+ },+ )++ assert isinstance(result, SetupRequirementsResponse)+ assert "post_to_chat_platform" not in result.message+++ "channel",+ [+ "U0MEMBER01",+ "W0MEMBER01",+ "D0DIRECT01",+ "@someone",+ ],+)+async def test_a_slack_dm_to_another_member_gets_no_bot_hint(channel):+ """The bot DMs only the user, never another member."""+ result = await _prepare(+ "SendSlackMessageBlock", input_data={"channel": channel, "text": "hi"}+ )++ assert isinstance(result, SetupRequirementsResponse)+ assert "post_to_chat_platform" not in result.message+++ "channel",+ [+ "C0CHANNEL1",+ "#general",+ "deployments",+ "upcoming",+ "watercooler",+ "deployments2026",+ "u0member01",+ "w0member01",+ "d0direct01",+ ],+)+async def test_a_slack_channel_still_gets_a_bot_hint(channel):+ result = await _prepare(+ "SendSlackMessageBlock", input_data={"channel": channel, "text": "hi"}+ )++ assert isinstance(result, SetupRequirementsResponse)+ assert "links the Slack workspace" in result.message+++async def test_a_slack_reply_into_a_thread_gets_no_bot_hint():+ """The tool cannot reply into an existing thread."""+ result = await _prepare(+ "SendSlackMessageBlock",+ input_data={"channel": "#general", "text": "hi", "thread_ts": "171.5"},+ )++ assert isinstance(result, SetupRequirementsResponse)+ assert "post_to_chat_platform" not in result.message+++async def test_a_telegram_group_names_the_bot_as_the_other_way():+ result = await _prepare(+ "SendTelegramMessageBlock", input_data={"chat_id": -200, "text": "hi"}+ )++ assert isinstance(result, SetupRequirementsResponse)+ assert "links the Telegram group" in result.message+++async def test_a_telegram_private_chat_gets_no_bot_hint():+ result = await _prepare(+ "SendTelegramMessageBlock", input_data={"chat_id": 4242, "text": "hi"}+ )++ assert isinstance(result, SetupRequirementsResponse)+ assert "post_to_chat_platform" not in result.message+++async def test_a_telegram_reply_gets_no_bot_hint():+ result = await _prepare(+ "SendTelegramMessageBlock",+ input_data={"chat_id": -100, "text": "hi", "reply_to_message_id": 7},+ )++ assert isinstance(result, SetupRequirementsResponse)+ assert "post_to_chat_platform" not in result.message+++async def _find(query: str, links: MagicMock):+ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(chat_platform, "_any_chat_platform_configured", lambda: True)+ index = CapabilityIndex(build_entries(TOOL_REGISTRY, TOOL_GROUPS))+ with (+ patch(+ "backend.copilot.tools.find_capability.load_connection_state",+ AsyncMock(return_value=ConnectionState()),+ ),+ patch(+ "backend.copilot.tools.find_capability.session_registry",+ AsyncMock(return_value=index),+ ),+ patch(f"{_HINT}.platform_linking_db", return_value=links),+ ):+ return await FindCapabilityTool()._execute(+ _USER, make_session(_USER), query=query+ )+++async def test_the_listing_says_which_platforms_the_bot_can_post_to():+ result = await _find("send a discord message", _links("DISCORD", dm=("SLACK",)))++ assert isinstance(result, CapabilityListResponse)+ tool = next(c for c in result.capabilities if c["name"] == POST_TOOL)+ assert tool["linked"] == ["discord"]+ assert tool["dm_linked"] == ["slack"]+ assert all("linked" not in c for c in result.capabilities if c is not tool)+++async def test_the_listing_says_nothing_is_linked():+ result = await _find("send a discord message", _links())++ assert isinstance(result, CapabilityListResponse)+ tool = next(c for c in result.capabilities if c["name"] == POST_TOOL)+ assert tool["linked"] == [] and tool["dm_linked"] == []+++async def test_a_failed_link_lookup_lists_nothing_as_linked():+ links = _links()+ links.list_server_links.side_effect = RuntimeError("db down")+ result = await _find("send a discord message", links)++ assert isinstance(result, CapabilityListResponse)+ tool = next(c for c in result.capabilities if c["name"] == POST_TOOL)+ assert tool["linked"] == [] and tool["dm_linked"] == []+++async def test_a_search_without_the_tool_reads_no_links():+ links = _links("DISCORD")+ result = await _find("web search", links)++ assert isinstance(result, CapabilityListResponse)+ links.list_server_links.assert_not_called()diff --git a/autogpt_platform/backend/backend/copilot/tools/chat_platform_test.py b/autogpt_platform/backend/backend/copilot/tools/chat_platform_test.pyindex 90ffd8c9fc..244294f665 100644--- a/autogpt_platform/backend/backend/copilot/tools/chat_platform_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/chat_platform_test.py@@ -11,6 +11,7 @@ from backend.copilot.tools.chat_platform import (ListChatPlatformChannelsTool,PostToChatPlatformTool,_any_chat_platform_configured,+ _configured_chat_platforms,)from backend.copilot.tools.models import (ChatPlatformChannelListResponse,@@ -618,9 +619,9 @@ def _secrets(**overrides) -> MagicMock:],)def test_configured_gate_mirrors_adapter_gates(overrides, expected):- _any_chat_platform_configured.cache_clear()+ _configured_chat_platforms.cache_clear()settings = MagicMock()settings.secrets = _secrets(**overrides)with patch(f"{_PATH}.Settings", return_value=settings):assert _any_chat_platform_configured() is expected- _any_chat_platform_configured.cache_clear()+ _configured_chat_platforms.cache_clear()diff --git a/autogpt_platform/backend/backend/copilot/tools/find_capability.py b/autogpt_platform/backend/backend/copilot/tools/find_capability.pyindex 4cf313f385..ec2d61c924 100644--- a/autogpt_platform/backend/backend/copilot/tools/find_capability.py+++ b/autogpt_platform/backend/backend/copilot/tools/find_capability.py@@ -16,6 +16,7 @@ from backend.copilot.context import get_current_permissionsfrom backend.copilot.model import ChatSessionfrom .base import BaseTool+from .chat_platform_hint import POST_TOOL, ChatPlatformLinks, chat_platform_linksfrom .models import (CapabilityListResponse,ErrorResponse,@@ -143,8 +144,13 @@ class FindCapabilityTool(BaseTool):],session_id=session_id,)- capabilities = [_listing(hit, connections) for hit in result.hits]- fallback = [_listing(hit, connections) for hit in result.fallback]+ linked = (+ await chat_platform_links(user_id)+ if POST_TOOL in result.names+ else ChatPlatformLinks()+ )+ capabilities = [_listing(hit, connections, linked) for hit in result.hits]+ fallback = [_listing(hit, connections, linked) for hit in result.fallback]return CapabilityListResponse(message=_message(result.service,@@ -166,8 +172,17 @@ class FindCapabilityTool(BaseTool):)-def _listing(hit: SearchHit, connections: ConnectionState) -> dict[str, Any]:+def _listing(+ hit: SearchHit, connections: ConnectionState, linked: ChatPlatformLinks+) -> dict[str, Any]:listing = hit.entry.listing()+ # The bot posts only where the user linked a platform to it, which no+ # credential records: without this the model cannot tell a tool that+ # will post from one that answers "nothing is linked". Channels need a+ # linked server; a DM link lets it DM the user only.+ if hit.entry.name == POST_TOOL:+ listing["linked"] = sorted(linked.servers)+ listing["dm_linked"] = sorted(linked.dms)if hit.entry.connection.required:listing["connected"] = (NEEDS_EXPERT_GRANTdiff --git a/autogpt_platform/backend/backend/copilot/tools/helpers.py b/autogpt_platform/backend/backend/copilot/tools/helpers.pyindex a59a55b3b5..f841ed9461 100644--- a/autogpt_platform/backend/backend/copilot/tools/helpers.py+++ b/autogpt_platform/backend/backend/copilot/tools/helpers.py@@ -57,6 +57,7 @@ from backend.util.type import coerce_inputs_to_schemafrom .ayrshare_managed import is_ayrshare, is_not_linked_errorfrom .ayrshare_managed import not_linked_response as ayrshare_not_linked_responsefrom .ayrshare_managed import provision_for_block as provision_ayrshare_for_block+from .chat_platform_hint import bot_link_hintfrom .expert_scope import (annotate_expert_grants,provider_slug,@@ -1138,28 +1139,47 @@ async def prepare_block_for_execution(missing_entries = build_missing_credentials_from_field_info(credentials_fields_info, set(matched_credentials.keys()))+ missing_providers = {provider_slug(m.provider) for m in missing_credentials}+ grant_hint = (+ await ungranted_credential_hint(+ user_id,+ session.expert_id,+ missing_providers,+ missing_entries.values(),+ )+ if missing_credentials+ else ""+ )+ # A pinned account or an ungranted credential is the fix the owner+ # set up, so its error or grant card stands; the bot is the hint+ # only when nothing of theirs would be.+ owners_fix = bool(grant_hint) or bool(+ set(turn_credential_pins()) & missing_providers+ )+ bot_hint = "" if owners_fix else bot_link_hint(block.name, input_data)if missing_credentials and is_unattended_turn():- return await unattended_missing_credentials_error(+ unattended = await unattended_missing_credentials_error(f"Block '{block.name}'",missing_entries,session_id,user_id,session.expert_id,)+ unattended.message += bot_hint+ return unattendedmissing_creds_dict = await annotate_expert_grants(user_id, session.expert_id, missing_entries)missing_creds_list = list(missing_creds_dict.values())if missing_credentials:message = (- f"Block '{block.name}' requires credentials that are not "- "configured. Please set up the required credentials before "- "running this block."- ) + await ungranted_credential_hint(- user_id,- session.expert_id,- {provider_slug(m.provider) for m in missing_credentials},- missing_entries.values(),+ (+ f"Block '{block.name}' requires credentials that are not "+ "configured. Please set up the required credentials before "+ "running this block."+ )+ + grant_hint+ + bot_hint)else:message = (